update boost

This commit is contained in:
2023-11-24 12:56:13 -06:00
parent cfc99971af
commit 19d727037a
9260 changed files with 849256 additions and 299957 deletions

View File

@@ -0,0 +1,207 @@
/*
boost/numeric/odeint/stepper/detail/adaptive_adams_coefficients.hpp
[begin_description]
Calculation of the coefficients for the adaptive adams stepper.
[end_description]
Copyright 2017 Valentin Noah Hartmann
Distributed under the Boost Software License, Version 1.0.
(See accompanying file LICENSE_1_0.txt or
copy at http://www.boost.org/LICENSE_1_0.txt)
*/
#ifndef BOOST_NUMERIC_ODEINT_STEPPER_DETAIL_ADAPTIVE_ADAMS_COEFFICIENTS_HPP_INCLUDED
#define BOOST_NUMERIC_ODEINT_STEPPER_DETAIL_ADAPTIVE_ADAMS_COEFFICIENTS_HPP_INCLUDED
#include <boost/numeric/odeint/stepper/detail/rotating_buffer.hpp>
#include <boost/numeric/odeint/util/state_wrapper.hpp>
#include <boost/numeric/odeint/util/is_resizeable.hpp>
#include <boost/numeric/odeint/util/resizer.hpp>
#include <boost/numeric/odeint/util/unwrap_reference.hpp>
#include <boost/numeric/odeint/util/bind.hpp>
#include <boost/numeric/odeint/algebra/algebra_dispatcher.hpp>
#include <boost/numeric/odeint/algebra/operations_dispatcher.hpp>
#include <boost/array.hpp>
namespace boost {
namespace numeric {
namespace odeint {
namespace detail {
template<
size_t Steps,
class Deriv,
class Value = double,
class Time = double,
class Algebra = typename algebra_dispatcher< Deriv >::algebra_type,
class Operations = typename operations_dispatcher< Deriv >::operations_type,
class Resizer = initially_resizer
>
class adaptive_adams_coefficients
{
public:
static const size_t steps = Steps;
typedef unsigned short order_type;
static const order_type order_value = steps;
typedef Value value_type;
typedef Deriv deriv_type;
typedef Time time_type;
typedef state_wrapper< deriv_type > wrapped_deriv_type;
typedef rotating_buffer< time_type , steps+1 > time_storage_type;
typedef Algebra algebra_type;
typedef Operations operations_type;
typedef Resizer resizer_type;
typedef adaptive_adams_coefficients< Steps , Deriv , Value , Time , Algebra , Operations , Resizer > aac_type;
adaptive_adams_coefficients( const algebra_type &algebra = algebra_type())
:m_eo(1), m_steps_init(1), beta(), phi(), m_ns(0), m_time_storage(),
m_algebra(algebra),
m_phi_resizer()
{
for (size_t i=0; i<order_value+2; ++i)
{
c[i] = 1.0/(i+1);
c[c_size+i] = 1.0/((i+1)*(i+2));
}
g[0] = c[0];
g[1] = c[c_size];
beta[0][0] = 1;
beta[1][0] = 1;
gs[0] = 1.0;
gs[1] = -1.0/2;
gs[2] = -1.0/12;
gs[3] = -1.0/24;
gs[4] = -19.0/720;
gs[5] = -3.0/160;
gs[6] = -863.0/60480;
gs[7] = -275.0/24192;
gs[8] = -33953.0/3628800;
gs[9] = 35.0/4436;
gs[10] = 40.0/5891;
gs[11] = 37.0/6250;
gs[12] = 25.0/4771;
gs[13] = 40.0/8547;
};
void predict(time_type t, time_type dt)
{
using std::abs;
m_time_storage[0] = t;
if (abs(m_time_storage[0] - m_time_storage[1] - dt) > 1e-16 || m_eo >= m_ns)
{
m_ns = 0;
}
else if (m_ns < order_value + 2)
{
m_ns++;
}
for(size_t i=1+m_ns; i<m_eo+1 && i<m_steps_init; ++i)
{
time_type diff = m_time_storage[0] - m_time_storage[i];
beta[0][i] = beta[0][i-1]*(m_time_storage[0] + dt - m_time_storage[i-1])/diff;
}
for(size_t i=2+m_ns; i<m_eo+2 && i<m_steps_init+1; ++i)
{
time_type diff = m_time_storage[0] + dt - m_time_storage[i-1];
for(size_t j=0; j<m_eo+1-i+1; ++j)
{
c[c_size*i+j] = c[c_size*(i-1)+j] - c[c_size*(i-1)+j+1]*dt/diff;
}
g[i] = c[c_size*i];
}
};
void do_step(const deriv_type &dxdt, const int o = 0)
{
m_phi_resizer.adjust_size( dxdt , detail::bind( &aac_type::template resize_phi_impl< deriv_type > , detail::ref( *this ) , detail::_1 ) );
phi[o][0].m_v = dxdt;
for(size_t i=1; i<m_eo+3 && i<m_steps_init+2 && i<order_value+2; ++i)
{
if (o == 0)
{
this->m_algebra.for_each3(phi[o][i].m_v, phi[o][i-1].m_v, phi[o+1][i-1].m_v,
typename Operations::template scale_sum2<value_type, value_type>(1.0, -beta[o][i-1]));
}
else
{
this->m_algebra.for_each2(phi[o][i].m_v, phi[o][i-1].m_v,
typename Operations::template scale_sum1<value_type>(1.0));
}
}
};
void confirm()
{
beta.rotate();
phi.rotate();
m_time_storage.rotate();
if(m_steps_init < order_value+1)
{
++m_steps_init;
}
};
void reset() { m_eo = 1; m_steps_init = 1; };
size_t m_eo;
size_t m_steps_init;
rotating_buffer<boost::array<value_type, order_value+1>, 2> beta; // beta[0] = beta(n)
rotating_buffer<boost::array<wrapped_deriv_type, order_value+2>, 3> phi; // phi[0] = phi(n+1)
boost::array<value_type, order_value + 2> g;
boost::array<value_type, 14> gs;
private:
template< class StateType >
bool resize_phi_impl( const StateType &x )
{
bool resized( false );
for(size_t i=0; i<(order_value + 2); ++i)
{
resized |= adjust_size_by_resizeability( phi[0][i], x, typename is_resizeable<deriv_type>::type() );
resized |= adjust_size_by_resizeability( phi[1][i], x, typename is_resizeable<deriv_type>::type() );
resized |= adjust_size_by_resizeability( phi[2][i], x, typename is_resizeable<deriv_type>::type() );
}
return resized;
};
size_t m_ns;
time_storage_type m_time_storage;
static const size_t c_size = order_value + 2;
boost::array<value_type, c_size*c_size> c;
algebra_type m_algebra;
resizer_type m_phi_resizer;
};
} // detail
} // odeint
} // numeric
} // boost
#endif

View File

@@ -0,0 +1,199 @@
/*
boost/numeric/odeint/stepper/detail/pid_step_adjuster.hpp
[begin_description]
Implementation of the stepsize controller for the controlled adams bashforth moulton stepper.
[end_description]
Copyright 2017 Valentin Noah Hartmann
Distributed under the Boost Software License, Version 1.0.
(See accompanying file LICENSE_1_0.txt or
copy at http://www.boost.org/LICENSE_1_0.txt)
*/
#ifndef BOOST_NUMERIC_ODEINT_STEPPER_DETAIL_PID_STEP_ADJUSTER_HPP_INCLUDED
#define BOOST_NUMERIC_ODEINT_STEPPER_DETAIL_PID_STEP_ADJUSTER_HPP_INCLUDED
#include <boost/numeric/odeint/stepper/detail/rotating_buffer.hpp>
#include <boost/numeric/odeint/stepper/detail/pid_step_adjuster_coefficients.hpp>
#include <boost/numeric/odeint/algebra/algebra_dispatcher.hpp>
#include <boost/numeric/odeint/algebra/operations_dispatcher.hpp>
#include <math.h>
namespace boost {
namespace numeric {
namespace odeint {
namespace detail {
template<
class Value = double,
class Time = double
>
struct pid_op
{
public:
typedef Value value_type;
typedef Time time_type;
const double beta1;
const double beta2;
const double beta3;
const double alpha1;
const double alpha2;
const time_type dt1;
const time_type dt2;
const time_type dt3;
const size_t m_steps;
pid_op(const size_t steps, const double _dt1, const double _dt2, const double _dt3,
const double b1 = 1, const double b2 = 0, const double b3 = 0, const double a1 = 0, const double a2 = 0)
:beta1(b1), beta2(b2), beta3(b3), alpha1(a1), alpha2(a2),
dt1(_dt1), dt2(_dt2), dt3(_dt3),
m_steps(steps)
{};
template<class T1, class T2, class T3, class T4>
void operator()(T1 &t1, const T2 &t2, const T3 &t3, const T4 &t4)
{
using std::abs;
t1 = adapted_pow(abs(t2), -beta1/(m_steps + 1)) *
adapted_pow(abs(t3), -beta2/(m_steps + 1)) *
adapted_pow(abs(t4), -beta3/(m_steps + 1)) *
adapted_pow(abs(dt1/dt2), -alpha1/(m_steps + 1))*
adapted_pow(abs(dt2/dt3), -alpha2/(m_steps + 1));
t1 = 1/t1;
};
template<class T1, class T2>
void operator()(T1 &t1, const T2 &t2)
{
using std::abs;
t1 = adapted_pow(abs(t2), -beta1/(m_steps + 1));
t1 = 1/t1;
};
private:
template<class T>
inline value_type adapted_pow(T base, double exp)
{
if(exp == 0)
{
return 1;
}
else if (exp > 0)
{
return pow(base, exp);
}
else
{
return 1/pow(base, -exp);
}
};
};
template<
class State,
class Value = double,
class Deriv = State,
class Time = double,
class Algebra = typename algebra_dispatcher< State >::algebra_type,
class Operations = typename operations_dispatcher< Deriv >::operations_type,
size_t Type = BASIC
>
struct pid_step_adjuster
{
public:
static double threshold() { return 0.9; };
typedef State state_type;
typedef Value value_type;
typedef Deriv deriv_type;
typedef Time time_type;
typedef Algebra algebra_type;
typedef Operations operations_type;
typedef rotating_buffer<state_type, 3> error_storage_type;
typedef rotating_buffer<time_type, 3> time_storage_type;
typedef pid_step_adjuster_coefficients<Type> coeff_type;
pid_step_adjuster(double abs_tol = 1e-6, double rel_tol = 1e-6, time_type dtmax = 1.0)
:m_dtmax(dtmax), m_error_storage(), m_dt_storage(), m_init(0),
m_abs_tol(abs_tol), m_rel_tol(rel_tol)
{};
time_type adjust_stepsize(const size_t steps, time_type dt, state_type &err, const state_type &x, const deriv_type &dxdt)
{
using std::abs;
m_algebra.for_each3( err , x , dxdt ,
typename operations_type::template rel_error< value_type >( m_abs_tol , m_rel_tol , 1.0 , 1.0 * abs(get_unit_value( dt )) ) );
m_error_storage[0] = err;
m_dt_storage[0] = dt;
if(m_init >= 2)
{
m_algebra.for_each4(err, m_error_storage[0], m_error_storage[1], m_error_storage[2],
pid_op<>(steps, m_dt_storage[0], m_dt_storage[1], m_dt_storage[2],
m_coeff[0], m_coeff[1], m_coeff[2], m_coeff[3], m_coeff[4]));
}
else
{
m_algebra.for_each2(err, m_error_storage[0],
pid_op<>(steps, m_dt_storage[0], m_dt_storage[1], m_dt_storage[2], 0.7));
}
value_type ratio = 1 / m_algebra.norm_inf(err);
value_type kappa = 1.0;
ratio = 1.0 + kappa*atan((ratio - 1) / kappa);
if(ratio*dt >= m_dtmax)
{
ratio = m_dtmax / dt;
}
if(ratio >= threshold())
{
m_error_storage.rotate();
m_dt_storage.rotate();
++m_init;
}
else
{
m_init = 0;
}
return dt * static_cast<time_type>(ratio);
};
private:
algebra_type m_algebra;
time_type m_dtmax;
error_storage_type m_error_storage;
time_storage_type m_dt_storage;
size_t m_init;
double m_abs_tol;
double m_rel_tol;
coeff_type m_coeff;
};
} // detail
} // odeint
} // numeric
} // boost
#endif

View File

@@ -0,0 +1,180 @@
/*
boost/numeric/odeint/stepper/detail/pid_step_adjuster_coefficients.hpp
[begin_description]
Coefficients for the PID stepsize controller.
[end_description]
Copyright 2017 Valentin Noah Hartmann
Distributed under the Boost Software License, Version 1.0.
(See accompanying file LICENSE_1_0.txt or
copy at http://www.boost.org/LICENSE_1_0.txt)
*/
#ifndef BOOST_NUMERIC_ODEINT_STEPPER_DETAIL_PID_STEP_ADJUSTER_COEFFICIENTS_HPP_INCLUDED
#define BOOST_NUMERIC_ODEINT_STEPPER_DETAIL_PID_STEP_ADJUSTER_COEFFICIENTS_HPP_INCLUDED
#include <boost/array.hpp>
namespace boost {
namespace numeric {
namespace odeint {
namespace detail {
enum adjuster_type{
BASIC,
H0211,
H211b,
H211PI,
H0312,
H312b,
H312PID,
H0321,
H321
};
template<int Type>
class pid_step_adjuster_coefficients;
template<>
class pid_step_adjuster_coefficients<BASIC> : public boost::array<double, 5>
{
public:
pid_step_adjuster_coefficients()
: boost::array<double, 5>()
{
(*this)[0] = 1.0;
(*this)[1] = 0.0;
(*this)[2] = 0.0;
(*this)[3] = 0.0;
(*this)[4] = 0.0;
}
};
template<>
class pid_step_adjuster_coefficients<H0211> : public boost::array<double, 5>
{
public:
pid_step_adjuster_coefficients()
: boost::array<double, 5>()
{
(*this)[0] = 1.0 / 2.0;
(*this)[1] = 1.0 / 2.0;
(*this)[2] = 0.0;
(*this)[3] = 1.0 / 2.0;
(*this)[4] = 0.0;
}
};
template<>
class pid_step_adjuster_coefficients<H211b> : public boost::array<double, 5>
{
public:
pid_step_adjuster_coefficients()
: boost::array<double, 5>()
{
(*this)[0] = 1.0 / 5.0;
(*this)[1] = 2.0 / 5.0;
(*this)[2] = 0.0;
(*this)[3] = 1.0 / 5.0;
(*this)[4] = 0.0;
}
};
template<>
class pid_step_adjuster_coefficients<H211PI> : public boost::array<double, 5>
{
public:
pid_step_adjuster_coefficients()
: boost::array<double, 5>()
{
(*this)[0] = 1.0 / 6.0;
(*this)[1] = 2.0 / 6.0;
(*this)[2] = 0.0;
(*this)[3] = 0.0;
(*this)[4] = 0.0;
}
};
template<>
class pid_step_adjuster_coefficients<H0312> : public boost::array<double, 5>
{
public:
pid_step_adjuster_coefficients()
: boost::array<double, 5>()
{
(*this)[0] = 1.0 / 4.0;
(*this)[1] = 2.0 / 2.0;
(*this)[2] = 1.0 / 4.0;
(*this)[3] = 3.0 / 4.0;
(*this)[4] = 1.0 / 4.0;
}
};
template<>
class pid_step_adjuster_coefficients<H312b> : public boost::array<double, 5>
{
public:
pid_step_adjuster_coefficients()
: boost::array<double, 5>()
{
(*this)[0] = 1.0 / 6.0;
(*this)[1] = 2.0 / 6.0;
(*this)[2] = 1.0 / 6.0;
(*this)[3] = 3.0 / 6.0;
(*this)[4] = 1.0 / 6.0;
}
};
template<>
class pid_step_adjuster_coefficients<H312PID> : public boost::array<double, 5>
{
public:
pid_step_adjuster_coefficients()
: boost::array<double, 5>()
{
(*this)[0] = 1.0 / 18.0;
(*this)[1] = 2.0 / 9.0;
(*this)[2] = 1.0 / 18.0;
(*this)[3] = 0.0;
(*this)[4] = 0.0;
}
};
template<>
class pid_step_adjuster_coefficients<H0321> : public boost::array<double, 5>
{
public:
pid_step_adjuster_coefficients()
: boost::array<double, 5>()
{
(*this)[0] = 5.0 / 4.0;
(*this)[1] = 1.0 / 2.0;
(*this)[2] = -3.0 / 4.0;
(*this)[3] = -1.0 / 4.0;
(*this)[4] = -3.0 / 4.0;
}
};
template<>
class pid_step_adjuster_coefficients<H321> : public boost::array<double, 5>
{
public:
pid_step_adjuster_coefficients()
: boost::array<double, 5>()
{
(*this)[0] = 1.0 / 3.0;
(*this)[1] = 1.0 / 18.0;
(*this)[2] = -5.0 / 18.0;
(*this)[3] = -5.0 / 16.0;
(*this)[4] = -1.0 / 6.0;
}
};
} // detail
} // odeint
} // numeric
} // boost
#endif