update boost
This commit is contained in:
@@ -19,6 +19,8 @@
|
||||
#define BOOST_NUMERIC_ODEINT_ALGEBRA_MULTI_ARRAY_ALGEBRA_HPP_DEFINED
|
||||
|
||||
|
||||
#include <boost/multi_array.hpp>
|
||||
|
||||
#include <boost/numeric/odeint/algebra/detail/for_each.hpp>
|
||||
#include <boost/numeric/odeint/algebra/detail/norm_inf.hpp>
|
||||
#include <boost/numeric/odeint/algebra/norm_result_type.hpp>
|
||||
@@ -121,7 +123,7 @@ struct multi_array_algebra
|
||||
{
|
||||
detail::for_each15( s1.data() , s1.data() + s1.num_elements() , s2.data() , s3.data() , s4.data() , s5.data() , s6.data() , s7.data() , s8.data() , s9.data() , s10.data() , s11.data() , s12.data() , s13.data() , s14.data() , s15.data() , op );
|
||||
}
|
||||
|
||||
|
||||
template< typename S >
|
||||
static typename norm_result_type<S>::type norm_inf( const S &s )
|
||||
{
|
||||
@@ -129,15 +131,11 @@ struct multi_array_algebra
|
||||
}
|
||||
};
|
||||
|
||||
// template< class T , size_t N >
|
||||
// struct algebra_dispatcher< boost::array< T , N > >
|
||||
// {
|
||||
// typedef array_algebra algebra_type;
|
||||
// };
|
||||
|
||||
|
||||
|
||||
|
||||
template< class T , size_t N >
|
||||
struct algebra_dispatcher< boost::multi_array< T , N > >
|
||||
{
|
||||
typedef multi_array_algebra algebra_type;
|
||||
};
|
||||
|
||||
|
||||
} // namespace odeint
|
||||
|
||||
@@ -30,7 +30,7 @@ namespace odeint {
|
||||
/*
|
||||
* This class template has to be overload in order to call vector_space_algebra::norm_inf
|
||||
*/
|
||||
template< class State > struct vector_space_norm_inf;
|
||||
template< class State, class Enabler = void > struct vector_space_norm_inf;
|
||||
|
||||
/*
|
||||
* Example: instantiation for sole doubles and complex
|
||||
|
||||
@@ -25,42 +25,36 @@
|
||||
// from odeint
|
||||
// (that is, it lets odeint treat the eigen matrices correctly, knowing
|
||||
// how to add, multiply, compute the norm, etc)
|
||||
|
||||
namespace Eigen {
|
||||
|
||||
|
||||
template<typename D>
|
||||
inline const
|
||||
typename Eigen::CwiseUnaryOp<
|
||||
typename Eigen::internal::scalar_add_op<
|
||||
typename Eigen::internal::traits<D>::Scalar>,
|
||||
const D >
|
||||
typename Eigen::CwiseBinaryOp<
|
||||
internal::scalar_sum_op<typename internal::traits<D>::Scalar>,
|
||||
typename DenseBase<D>::ConstantReturnType,
|
||||
const D>
|
||||
operator+(const typename Eigen::MatrixBase<D> &m,
|
||||
const typename Eigen::internal::traits<D>::Scalar &s) {
|
||||
return Eigen::CwiseUnaryOp<
|
||||
typename Eigen::internal::scalar_add_op<
|
||||
typename Eigen::internal::traits<D>::Scalar>,
|
||||
const D >(m.derived(),Eigen::internal::scalar_add_op<
|
||||
typename Eigen::internal::traits<D>::Scalar>(s));
|
||||
return CwiseBinaryOp<
|
||||
internal::scalar_sum_op<typename internal::traits<D>::Scalar>,
|
||||
typename DenseBase<D>::ConstantReturnType,
|
||||
const D>(DenseBase<D>::Constant(m.rows(), m.cols(), s), m.derived());
|
||||
}
|
||||
|
||||
template<typename D>
|
||||
inline const
|
||||
typename Eigen::CwiseUnaryOp<
|
||||
typename Eigen::internal::scalar_add_op<
|
||||
typename Eigen::internal::traits<D>::Scalar>,
|
||||
const D >
|
||||
typename Eigen::CwiseBinaryOp<
|
||||
internal::scalar_sum_op<typename internal::traits<D>::Scalar>,
|
||||
typename DenseBase<D>::ConstantReturnType,
|
||||
const D>
|
||||
operator+(const typename Eigen::internal::traits<D>::Scalar &s,
|
||||
const typename Eigen::MatrixBase<D> &m) {
|
||||
return Eigen::CwiseUnaryOp<
|
||||
typename Eigen::internal::scalar_add_op<
|
||||
typename Eigen::internal::traits<D>::Scalar>,
|
||||
const D >(m.derived(),Eigen::internal::scalar_add_op<
|
||||
typename Eigen::internal::traits<D>::Scalar>(s));
|
||||
const typename Eigen::MatrixBase<D> &m) {
|
||||
return CwiseBinaryOp<
|
||||
internal::scalar_sum_op<typename internal::traits<D>::Scalar>,
|
||||
typename DenseBase<D>::ConstantReturnType,
|
||||
const D>(DenseBase<D>::Constant(m.rows(), m.cols(), s), m.derived());
|
||||
}
|
||||
|
||||
|
||||
|
||||
template<typename D1,typename D2>
|
||||
inline const
|
||||
typename Eigen::CwiseBinaryOp<
|
||||
@@ -82,14 +76,9 @@ abs( const Eigen::MatrixBase< D > &m ) {
|
||||
return m.cwiseAbs();
|
||||
}
|
||||
|
||||
|
||||
|
||||
} // end Eigen namespace
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
namespace boost {
|
||||
namespace numeric {
|
||||
namespace odeint {
|
||||
|
||||
@@ -0,0 +1,237 @@
|
||||
/*
|
||||
boost/numeric/odeint/stepper/detail/adaptive_adams_bashforth_moulton.hpp
|
||||
|
||||
[begin_description]
|
||||
Implemetation of an adaptive adams bashforth moulton stepper.
|
||||
Used as the stepper 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_ADAPTIVE_ADAMS_BASHFORTH_MOULTON_HPP_INCLUDED
|
||||
#define BOOST_NUMERIC_ODEINT_STEPPER_ADAPTIVE_ADAMS_BASHFORTH_MOULTON_HPP_INCLUDED
|
||||
|
||||
#include <boost/numeric/odeint/stepper/detail/adaptive_adams_coefficients.hpp>
|
||||
|
||||
#include <boost/numeric/odeint/util/unwrap_reference.hpp>
|
||||
#include <boost/numeric/odeint/util/bind.hpp>
|
||||
#include <boost/numeric/odeint/util/copy.hpp>
|
||||
|
||||
#include <boost/numeric/odeint/algebra/default_operations.hpp>
|
||||
#include <boost/numeric/odeint/algebra/algebra_dispatcher.hpp>
|
||||
#include <boost/numeric/odeint/algebra/operations_dispatcher.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/stepper/stepper_categories.hpp>
|
||||
|
||||
#include <boost/numeric/odeint/stepper/base/algebra_stepper_base.hpp>
|
||||
#include <boost/numeric/odeint/stepper/detail/rotating_buffer.hpp>
|
||||
|
||||
namespace boost {
|
||||
namespace numeric {
|
||||
namespace odeint {
|
||||
|
||||
template<
|
||||
size_t Steps,
|
||||
class State,
|
||||
class Value = double,
|
||||
class Deriv = State,
|
||||
class Time = Value,
|
||||
class Algebra = typename algebra_dispatcher< State >::algebra_type ,
|
||||
class Operations = typename operations_dispatcher< State >::operations_type,
|
||||
class Resizer = initially_resizer
|
||||
>
|
||||
class adaptive_adams_bashforth_moulton: public algebra_stepper_base< Algebra , Operations >
|
||||
{
|
||||
public:
|
||||
static const size_t steps = Steps;
|
||||
|
||||
typedef unsigned short order_type;
|
||||
static const order_type order_value = steps;
|
||||
|
||||
typedef State state_type;
|
||||
typedef Value value_type;
|
||||
typedef Deriv deriv_type;
|
||||
typedef Time time_type;
|
||||
|
||||
typedef state_wrapper< state_type > wrapped_state_type;
|
||||
typedef state_wrapper< deriv_type > wrapped_deriv_type;
|
||||
|
||||
typedef algebra_stepper_base< Algebra , Operations > algebra_stepper_base_type;
|
||||
typedef typename algebra_stepper_base_type::algebra_type algebra_type;
|
||||
typedef typename algebra_stepper_base_type::operations_type operations_type;
|
||||
typedef Resizer resizer_type;
|
||||
typedef error_stepper_tag stepper_category;
|
||||
|
||||
typedef detail::adaptive_adams_coefficients< Steps , Deriv , Value , Time , Algebra , Operations , Resizer > coeff_type;
|
||||
typedef adaptive_adams_bashforth_moulton< Steps , State , Value , Deriv , Time , Algebra , Operations , Resizer > stepper_type;
|
||||
|
||||
order_type order() const { return order_value; };
|
||||
order_type stepper_order() const { return order_value + 1; };
|
||||
order_type error_order() const { return order_value; };
|
||||
|
||||
adaptive_adams_bashforth_moulton( const algebra_type &algebra = algebra_type() )
|
||||
:algebra_stepper_base_type( algebra ), m_coeff(),
|
||||
m_dxdt_resizer(), m_xnew_resizer(), m_xerr_resizer()
|
||||
{};
|
||||
|
||||
template< class System >
|
||||
void do_step(System system, state_type &inOut, time_type t, time_type dt )
|
||||
{
|
||||
m_xnew_resizer.adjust_size( inOut , detail::bind( &stepper_type::template resize_xnew_impl< state_type > , detail::ref( *this ) , detail::_1 ) );
|
||||
|
||||
do_step(system, inOut, t, m_xnew.m_v, dt, m_xerr.m_v);
|
||||
boost::numeric::odeint::copy( m_xnew.m_v , inOut);
|
||||
};
|
||||
|
||||
template< class System >
|
||||
void do_step(System system, const state_type &in, time_type t, state_type &out, time_type dt )
|
||||
{
|
||||
do_step(system, in, t, out, dt, m_xerr.m_v);
|
||||
};
|
||||
|
||||
template< class System >
|
||||
void do_step(System system, state_type &inOut, time_type t, time_type dt, state_type &xerr)
|
||||
{
|
||||
m_xnew_resizer.adjust_size( inOut , detail::bind( &stepper_type::template resize_xnew_impl< state_type > , detail::ref( *this ) , detail::_1 ) );
|
||||
|
||||
do_step(system, inOut, t, m_xnew.m_v, dt, xerr);
|
||||
boost::numeric::odeint::copy( m_xnew.m_v , inOut);
|
||||
};
|
||||
|
||||
template< class System >
|
||||
void do_step(System system, const state_type &in, time_type t, state_type &out, time_type dt , state_type &xerr)
|
||||
{
|
||||
do_step_impl(system, in, t, out, dt, xerr);
|
||||
|
||||
system(out, m_dxdt.m_v, t+dt);
|
||||
m_coeff.do_step(m_dxdt.m_v);
|
||||
m_coeff.confirm();
|
||||
|
||||
if(m_coeff.m_eo < order_value)
|
||||
{
|
||||
m_coeff.m_eo ++;
|
||||
}
|
||||
};
|
||||
|
||||
template< class ExplicitStepper, class System >
|
||||
void initialize(ExplicitStepper stepper, System system, state_type &inOut, time_type &t, time_type dt)
|
||||
{
|
||||
reset();
|
||||
dt = dt/static_cast< time_type >(order_value);
|
||||
|
||||
m_dxdt_resizer.adjust_size( inOut , detail::bind( &stepper_type::template resize_dxdt_impl< state_type > , detail::ref( *this ) , detail::_1 ) );
|
||||
|
||||
system( inOut , m_dxdt.m_v , t );
|
||||
for( size_t i=0 ; i<order_value; ++i )
|
||||
{
|
||||
stepper.do_step_dxdt_impl( system, inOut, m_dxdt.m_v, t, dt );
|
||||
|
||||
system( inOut , m_dxdt.m_v , t + dt);
|
||||
|
||||
m_coeff.predict(t, dt);
|
||||
m_coeff.do_step(m_dxdt.m_v);
|
||||
m_coeff.confirm();
|
||||
|
||||
t += dt;
|
||||
|
||||
if(m_coeff.m_eo < order_value)
|
||||
{
|
||||
++m_coeff.m_eo;
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
template< class System >
|
||||
void initialize(System system, state_type &inOut, time_type &t, time_type dt)
|
||||
{
|
||||
reset();
|
||||
dt = dt/static_cast< time_type >(order_value);
|
||||
|
||||
for(size_t i=0; i<order_value; ++i)
|
||||
{
|
||||
this->do_step(system, inOut, t, dt);
|
||||
t += dt;
|
||||
};
|
||||
};
|
||||
|
||||
template< class System >
|
||||
void do_step_impl(System system, const state_type & in, time_type t, state_type & out, time_type &dt, state_type &xerr)
|
||||
{
|
||||
size_t eO = m_coeff.m_eo;
|
||||
|
||||
m_xerr_resizer.adjust_size( in , detail::bind( &stepper_type::template resize_xerr_impl< state_type > , detail::ref( *this ) , detail::_1 ) );
|
||||
m_dxdt_resizer.adjust_size( in , detail::bind( &stepper_type::template resize_dxdt_impl< state_type > , detail::ref( *this ) , detail::_1 ) );
|
||||
|
||||
m_coeff.predict(t, dt);
|
||||
if (m_coeff.m_steps_init == 1)
|
||||
{
|
||||
system(in, m_dxdt.m_v, t);
|
||||
m_coeff.do_step(m_dxdt.m_v, 1);
|
||||
}
|
||||
|
||||
boost::numeric::odeint::copy( in , out );
|
||||
for(size_t i=0; i<eO; ++i)
|
||||
{
|
||||
this->m_algebra.for_each3(out, out, m_coeff.phi[1][i].m_v,
|
||||
typename Operations::template scale_sum2<double, double>(1.0, dt*m_coeff.g[i]*m_coeff.beta[0][i]));
|
||||
}
|
||||
|
||||
system(out, m_dxdt.m_v, t+dt);
|
||||
m_coeff.do_step(m_dxdt.m_v);
|
||||
|
||||
this->m_algebra.for_each3(out, out, m_coeff.phi[0][eO].m_v,
|
||||
typename Operations::template scale_sum2<double, double>(1.0, dt*m_coeff.g[eO]));
|
||||
|
||||
// error for current order
|
||||
this->m_algebra.for_each2(xerr, m_coeff.phi[0][eO].m_v,
|
||||
typename Operations::template scale_sum1<double>(dt*(m_coeff.g[eO])));
|
||||
};
|
||||
|
||||
const coeff_type& coeff() const { return m_coeff; };
|
||||
coeff_type & coeff() { return m_coeff; };
|
||||
|
||||
void reset() { m_coeff.reset(); };
|
||||
const deriv_type & dxdt() const { return m_dxdt.m_v; };
|
||||
|
||||
private:
|
||||
template< class StateType >
|
||||
bool resize_dxdt_impl( const StateType &x )
|
||||
{
|
||||
return adjust_size_by_resizeability( m_dxdt, x, typename is_resizeable<deriv_type>::type() );
|
||||
};
|
||||
template< class StateType >
|
||||
bool resize_xnew_impl( const StateType &x )
|
||||
{
|
||||
return adjust_size_by_resizeability( m_xnew, x, typename is_resizeable<state_type>::type() );
|
||||
};
|
||||
template< class StateType >
|
||||
bool resize_xerr_impl( const StateType &x )
|
||||
{
|
||||
return adjust_size_by_resizeability( m_xerr, x, typename is_resizeable<state_type>::type() );
|
||||
};
|
||||
|
||||
coeff_type m_coeff;
|
||||
|
||||
resizer_type m_dxdt_resizer;
|
||||
resizer_type m_xnew_resizer;
|
||||
resizer_type m_xerr_resizer;
|
||||
|
||||
wrapped_deriv_type m_dxdt;
|
||||
wrapped_state_type m_xnew;
|
||||
wrapped_state_type m_xerr;
|
||||
};
|
||||
|
||||
} // odeint
|
||||
} // numeric
|
||||
} // boost
|
||||
|
||||
#endif
|
||||
@@ -391,6 +391,11 @@ public:
|
||||
}
|
||||
|
||||
|
||||
protected:
|
||||
|
||||
time_type m_max_dt;
|
||||
|
||||
|
||||
private:
|
||||
|
||||
template< class StateInOut , class StateVector >
|
||||
@@ -667,8 +672,6 @@ private:
|
||||
default_error_checker< value_type, algebra_type , operations_type > m_error_checker;
|
||||
modified_midpoint_dense_out< state_type , value_type , deriv_type , time_type , algebra_type , operations_type , resizer_type > m_midpoint;
|
||||
|
||||
time_type m_max_dt;
|
||||
|
||||
bool m_control_interpolation;
|
||||
|
||||
bool m_last_step_rejected;
|
||||
|
||||
@@ -0,0 +1,322 @@
|
||||
/*
|
||||
boost/numeric/odeint/stepper/detail/controlled_adams_bashforth_moulton.hpp
|
||||
|
||||
[begin_description]
|
||||
Implemetation of an 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_CONTROLLED_ADAMS_BASHFORTH_MOULTON_HPP_INCLUDED
|
||||
#define BOOST_NUMERIC_ODEINT_STEPPER_CONTROLLED_ADAMS_BASHFORTH_MOULTON_HPP_INCLUDED
|
||||
|
||||
#include <boost/numeric/odeint/stepper/stepper_categories.hpp>
|
||||
#include <boost/numeric/odeint/stepper/controlled_step_result.hpp>
|
||||
|
||||
#include <boost/numeric/odeint/stepper/adaptive_adams_bashforth_moulton.hpp>
|
||||
#include <boost/numeric/odeint/stepper/detail/pid_step_adjuster.hpp>
|
||||
|
||||
#include <boost/numeric/odeint/util/unwrap_reference.hpp>
|
||||
#include <boost/numeric/odeint/util/is_resizeable.hpp>
|
||||
#include <boost/numeric/odeint/util/resizer.hpp>
|
||||
|
||||
#include <boost/numeric/odeint/util/copy.hpp>
|
||||
#include <boost/numeric/odeint/util/bind.hpp>
|
||||
|
||||
#include <iostream>
|
||||
|
||||
namespace boost {
|
||||
namespace numeric {
|
||||
namespace odeint {
|
||||
|
||||
template<
|
||||
size_t MaxOrder,
|
||||
class State,
|
||||
class Value = double,
|
||||
class Algebra = typename algebra_dispatcher< State >::algebra_type
|
||||
>
|
||||
class default_order_adjuster
|
||||
{
|
||||
public:
|
||||
typedef State state_type;
|
||||
typedef Value value_type;
|
||||
typedef state_wrapper< state_type > wrapped_state_type;
|
||||
|
||||
typedef Algebra algebra_type;
|
||||
|
||||
default_order_adjuster( const algebra_type &algebra = algebra_type() )
|
||||
: m_algebra( algebra )
|
||||
{};
|
||||
|
||||
size_t adjust_order(size_t order, size_t init, boost::array<wrapped_state_type, 4> &xerr)
|
||||
{
|
||||
using std::abs;
|
||||
|
||||
value_type errc = abs(m_algebra.norm_inf(xerr[2].m_v));
|
||||
|
||||
value_type errm1 = 3*errc;
|
||||
value_type errm2 = 3*errc;
|
||||
|
||||
if(order > 2)
|
||||
{
|
||||
errm2 = abs(m_algebra.norm_inf(xerr[0].m_v));
|
||||
}
|
||||
if(order >= 2)
|
||||
{
|
||||
errm1 = abs(m_algebra.norm_inf(xerr[1].m_v));
|
||||
}
|
||||
|
||||
size_t o_new = order;
|
||||
|
||||
if(order == 2 && errm1 <= 0.5*errc)
|
||||
{
|
||||
o_new = order - 1;
|
||||
}
|
||||
else if(order > 2 && errm2 < errc && errm1 < errc)
|
||||
{
|
||||
o_new = order - 1;
|
||||
}
|
||||
|
||||
if(init < order)
|
||||
{
|
||||
return order+1;
|
||||
}
|
||||
else if(o_new == order - 1)
|
||||
{
|
||||
return order-1;
|
||||
}
|
||||
else if(order <= MaxOrder)
|
||||
{
|
||||
value_type errp = abs(m_algebra.norm_inf(xerr[3].m_v));
|
||||
|
||||
if(order > 1 && errm1 < errc && errp)
|
||||
{
|
||||
return order-1;
|
||||
}
|
||||
else if(order < MaxOrder && errp < (0.5-0.25*order/MaxOrder) * errc)
|
||||
{
|
||||
return order+1;
|
||||
}
|
||||
}
|
||||
|
||||
return order;
|
||||
};
|
||||
private:
|
||||
algebra_type m_algebra;
|
||||
};
|
||||
|
||||
template<
|
||||
class ErrorStepper,
|
||||
class StepAdjuster = detail::pid_step_adjuster< typename ErrorStepper::state_type,
|
||||
typename ErrorStepper::value_type,
|
||||
typename ErrorStepper::deriv_type,
|
||||
typename ErrorStepper::time_type,
|
||||
typename ErrorStepper::algebra_type,
|
||||
typename ErrorStepper::operations_type,
|
||||
detail::H211PI
|
||||
>,
|
||||
class OrderAdjuster = default_order_adjuster< ErrorStepper::order_value,
|
||||
typename ErrorStepper::state_type,
|
||||
typename ErrorStepper::value_type,
|
||||
typename ErrorStepper::algebra_type
|
||||
>,
|
||||
class Resizer = initially_resizer
|
||||
>
|
||||
class controlled_adams_bashforth_moulton
|
||||
{
|
||||
public:
|
||||
typedef ErrorStepper stepper_type;
|
||||
|
||||
static const typename stepper_type::order_type order_value = stepper_type::order_value;
|
||||
|
||||
typedef typename stepper_type::state_type state_type;
|
||||
typedef typename stepper_type::value_type value_type;
|
||||
typedef typename stepper_type::deriv_type deriv_type;
|
||||
typedef typename stepper_type::time_type time_type;
|
||||
|
||||
typedef typename stepper_type::algebra_type algebra_type;
|
||||
typedef typename stepper_type::operations_type operations_type;
|
||||
typedef Resizer resizer_type;
|
||||
|
||||
typedef StepAdjuster step_adjuster_type;
|
||||
typedef OrderAdjuster order_adjuster_type;
|
||||
typedef controlled_stepper_tag stepper_category;
|
||||
|
||||
typedef typename stepper_type::wrapped_state_type wrapped_state_type;
|
||||
typedef typename stepper_type::wrapped_deriv_type wrapped_deriv_type;
|
||||
typedef boost::array< wrapped_state_type , 4 > error_storage_type;
|
||||
|
||||
typedef typename stepper_type::coeff_type coeff_type;
|
||||
typedef controlled_adams_bashforth_moulton< ErrorStepper , StepAdjuster , OrderAdjuster , Resizer > controlled_stepper_type;
|
||||
|
||||
controlled_adams_bashforth_moulton(step_adjuster_type step_adjuster = step_adjuster_type())
|
||||
:m_stepper(),
|
||||
m_dxdt_resizer(), m_xerr_resizer(), m_xnew_resizer(),
|
||||
m_step_adjuster( step_adjuster ), m_order_adjuster()
|
||||
{};
|
||||
|
||||
template< class ExplicitStepper, class System >
|
||||
void initialize(ExplicitStepper stepper, System system, state_type &inOut, time_type &t, time_type dt)
|
||||
{
|
||||
m_stepper.initialize(stepper, system, inOut, t, dt);
|
||||
};
|
||||
|
||||
template< class System >
|
||||
void initialize(System system, state_type &inOut, time_type &t, time_type dt)
|
||||
{
|
||||
m_stepper.initialize(system, inOut, t, dt);
|
||||
};
|
||||
|
||||
template< class ExplicitStepper, class System >
|
||||
void initialize_controlled(ExplicitStepper stepper, System system, state_type &inOut, time_type &t, time_type &dt)
|
||||
{
|
||||
reset();
|
||||
coeff_type &coeff = m_stepper.coeff();
|
||||
|
||||
m_dxdt_resizer.adjust_size( inOut , detail::bind( &controlled_stepper_type::template resize_dxdt_impl< state_type > , detail::ref( *this ) , detail::_1 ) );
|
||||
|
||||
controlled_step_result res = fail;
|
||||
|
||||
for( size_t i=0 ; i<order_value; ++i )
|
||||
{
|
||||
do
|
||||
{
|
||||
res = stepper.try_step( system, inOut, t, dt );
|
||||
}
|
||||
while(res != success);
|
||||
|
||||
system( inOut , m_dxdt.m_v , t );
|
||||
|
||||
coeff.predict(t-dt, dt);
|
||||
coeff.do_step(m_dxdt.m_v);
|
||||
coeff.confirm();
|
||||
|
||||
if(coeff.m_eo < order_value)
|
||||
{
|
||||
++coeff.m_eo;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
template< class System >
|
||||
controlled_step_result try_step(System system, state_type & inOut, time_type &t, time_type &dt)
|
||||
{
|
||||
m_xnew_resizer.adjust_size( inOut , detail::bind( &controlled_stepper_type::template resize_xnew_impl< state_type > , detail::ref( *this ) , detail::_1 ) );
|
||||
|
||||
controlled_step_result res = try_step(system, inOut, t, m_xnew.m_v, dt);
|
||||
|
||||
if(res == success)
|
||||
{
|
||||
boost::numeric::odeint::copy( m_xnew.m_v , inOut);
|
||||
}
|
||||
|
||||
return res;
|
||||
};
|
||||
|
||||
template< class System >
|
||||
controlled_step_result try_step(System system, const state_type & in, time_type &t, state_type & out, time_type &dt)
|
||||
{
|
||||
m_xerr_resizer.adjust_size( in , detail::bind( &controlled_stepper_type::template resize_xerr_impl< state_type > , detail::ref( *this ) , detail::_1 ) );
|
||||
m_dxdt_resizer.adjust_size( in , detail::bind( &controlled_stepper_type::template resize_dxdt_impl< state_type > , detail::ref( *this ) , detail::_1 ) );
|
||||
|
||||
m_stepper.do_step_impl(system, in, t, out, dt, m_xerr[2].m_v);
|
||||
|
||||
coeff_type &coeff = m_stepper.coeff();
|
||||
|
||||
time_type dtPrev = dt;
|
||||
dt = m_step_adjuster.adjust_stepsize(coeff.m_eo, dt, m_xerr[2].m_v, out, m_stepper.dxdt() );
|
||||
|
||||
if(dt / dtPrev >= step_adjuster_type::threshold())
|
||||
{
|
||||
system(out, m_dxdt.m_v, t+dtPrev);
|
||||
|
||||
coeff.do_step(m_dxdt.m_v);
|
||||
coeff.confirm();
|
||||
|
||||
t += dtPrev;
|
||||
|
||||
size_t eo = coeff.m_eo;
|
||||
|
||||
// estimate errors for next step
|
||||
double factor = 1;
|
||||
algebra_type m_algebra;
|
||||
|
||||
m_algebra.for_each2(m_xerr[2].m_v, coeff.phi[1][eo].m_v,
|
||||
typename operations_type::template scale_sum1<double>(factor*dt*(coeff.gs[eo])));
|
||||
|
||||
if(eo > 1)
|
||||
{
|
||||
m_algebra.for_each2(m_xerr[1].m_v, coeff.phi[1][eo-1].m_v,
|
||||
typename operations_type::template scale_sum1<double>(factor*dt*(coeff.gs[eo-1])));
|
||||
}
|
||||
if(eo > 2)
|
||||
{
|
||||
m_algebra.for_each2(m_xerr[0].m_v, coeff.phi[1][eo-2].m_v,
|
||||
typename operations_type::template scale_sum1<double>(factor*dt*(coeff.gs[eo-2])));
|
||||
}
|
||||
if(eo < order_value && coeff.m_eo < coeff.m_steps_init-1)
|
||||
{
|
||||
m_algebra.for_each2(m_xerr[3].m_v, coeff.phi[1][eo+1].m_v,
|
||||
typename operations_type::template scale_sum1<double>(factor*dt*(coeff.gs[eo+1])));
|
||||
}
|
||||
|
||||
// adjust order
|
||||
coeff.m_eo = m_order_adjuster.adjust_order(coeff.m_eo, coeff.m_steps_init-1, m_xerr);
|
||||
|
||||
return success;
|
||||
}
|
||||
else
|
||||
{
|
||||
return fail;
|
||||
}
|
||||
};
|
||||
|
||||
void reset() { m_stepper.reset(); };
|
||||
|
||||
private:
|
||||
template< class StateType >
|
||||
bool resize_dxdt_impl( const StateType &x )
|
||||
{
|
||||
return adjust_size_by_resizeability( m_dxdt, x, typename is_resizeable<deriv_type>::type() );
|
||||
};
|
||||
template< class StateType >
|
||||
bool resize_xerr_impl( const StateType &x )
|
||||
{
|
||||
bool resized( false );
|
||||
|
||||
for(size_t i=0; i<m_xerr.size(); ++i)
|
||||
{
|
||||
resized |= adjust_size_by_resizeability( m_xerr[i], x, typename is_resizeable<state_type>::type() );
|
||||
}
|
||||
return resized;
|
||||
};
|
||||
template< class StateType >
|
||||
bool resize_xnew_impl( const StateType &x )
|
||||
{
|
||||
return adjust_size_by_resizeability( m_xnew, x, typename is_resizeable<state_type>::type() );
|
||||
};
|
||||
|
||||
stepper_type m_stepper;
|
||||
|
||||
wrapped_deriv_type m_dxdt;
|
||||
error_storage_type m_xerr;
|
||||
wrapped_state_type m_xnew;
|
||||
|
||||
resizer_type m_dxdt_resizer;
|
||||
resizer_type m_xerr_resizer;
|
||||
resizer_type m_xnew_resizer;
|
||||
|
||||
step_adjuster_type m_step_adjuster;
|
||||
order_adjuster_type m_order_adjuster;
|
||||
};
|
||||
|
||||
} // odeint
|
||||
} // numeric
|
||||
} // boost
|
||||
|
||||
#endif
|
||||
@@ -163,7 +163,7 @@ public:
|
||||
|
||||
time_type get_max_dt() { return m_max_dt; }
|
||||
|
||||
private:
|
||||
protected:
|
||||
time_type m_max_dt;
|
||||
};
|
||||
|
||||
@@ -411,10 +411,11 @@ public:
|
||||
template< class System , class StateIn , class DerivIn , class StateOut >
|
||||
controlled_step_result try_step( System system , const StateIn &in , const DerivIn &dxdt , time_type &t , StateOut &out , time_type &dt )
|
||||
{
|
||||
if( !m_step_adjuster.check_step_size_limit(dt) )
|
||||
unwrapped_step_adjuster &step_adjuster = m_step_adjuster;
|
||||
if( !step_adjuster.check_step_size_limit(dt) )
|
||||
{
|
||||
// given dt was above step size limit - adjust and return fail;
|
||||
dt = m_step_adjuster.get_max_dt();
|
||||
dt = step_adjuster.get_max_dt();
|
||||
return fail;
|
||||
}
|
||||
|
||||
@@ -428,13 +429,13 @@ public:
|
||||
if( max_rel_err > 1.0 )
|
||||
{
|
||||
// error too big, decrease step size and reject this step
|
||||
dt = m_step_adjuster.decrease_step(dt, max_rel_err, m_stepper.error_order());
|
||||
dt = step_adjuster.decrease_step(dt, max_rel_err, m_stepper.error_order());
|
||||
return fail;
|
||||
} else
|
||||
{
|
||||
// otherwise, increase step size and accept
|
||||
t += dt;
|
||||
dt = m_step_adjuster.increase_step(dt, max_rel_err, m_stepper.stepper_order());
|
||||
dt = step_adjuster.increase_step(dt, max_rel_err, m_stepper.stepper_order());
|
||||
return success;
|
||||
}
|
||||
}
|
||||
@@ -505,6 +506,7 @@ private:
|
||||
stepper_type m_stepper;
|
||||
error_checker_type m_error_checker;
|
||||
step_adjuster_type m_step_adjuster;
|
||||
typedef typename unwrap_reference< step_adjuster_type >::type unwrapped_step_adjuster;
|
||||
|
||||
resizer_type m_dxdt_resizer;
|
||||
resizer_type m_xerr_resizer;
|
||||
@@ -584,7 +586,7 @@ public:
|
||||
const step_adjuster_type &step_adjuster = step_adjuster_type() ,
|
||||
const stepper_type &stepper = stepper_type()
|
||||
)
|
||||
: m_stepper( stepper ) , m_error_checker( error_checker ) , m_step_adjuster(step_adjuster) ,
|
||||
: m_stepper( stepper ) , m_error_checker( error_checker ) , m_step_adjuster(step_adjuster) ,
|
||||
m_first_call( true )
|
||||
{ }
|
||||
|
||||
@@ -751,10 +753,11 @@ public:
|
||||
controlled_step_result try_step( System system , const StateIn &in , const DerivIn &dxdt_in , time_type &t ,
|
||||
StateOut &out , DerivOut &dxdt_out , time_type &dt )
|
||||
{
|
||||
if( !m_step_adjuster.check_step_size_limit(dt) )
|
||||
unwrapped_step_adjuster &step_adjuster = m_step_adjuster;
|
||||
if( !step_adjuster.check_step_size_limit(dt) )
|
||||
{
|
||||
// given dt was above step size limit - adjust and return fail;
|
||||
dt = m_step_adjuster.get_max_dt();
|
||||
dt = step_adjuster.get_max_dt();
|
||||
return fail;
|
||||
}
|
||||
|
||||
@@ -770,12 +773,12 @@ public:
|
||||
if( max_rel_err > 1.0 )
|
||||
{
|
||||
// error too big, decrease step size and reject this step
|
||||
dt = m_step_adjuster.decrease_step(dt, max_rel_err, m_stepper.error_order());
|
||||
dt = step_adjuster.decrease_step(dt, max_rel_err, m_stepper.error_order());
|
||||
return fail;
|
||||
}
|
||||
// otherwise, increase step size and accept
|
||||
t += dt;
|
||||
dt = m_step_adjuster.increase_step(dt, max_rel_err, m_stepper.stepper_order());
|
||||
dt = step_adjuster.increase_step(dt, max_rel_err, m_stepper.stepper_order());
|
||||
return success;
|
||||
}
|
||||
|
||||
@@ -903,6 +906,7 @@ private:
|
||||
stepper_type m_stepper;
|
||||
error_checker_type m_error_checker;
|
||||
step_adjuster_type m_step_adjuster;
|
||||
typedef typename unwrap_reference< step_adjuster_type >::type unwrapped_step_adjuster;
|
||||
|
||||
resizer_type m_dxdt_resizer;
|
||||
resizer_type m_xerr_resizer;
|
||||
|
||||
@@ -126,7 +126,7 @@ public:
|
||||
m_t_old = m_t;
|
||||
m_t += m_dt;
|
||||
toggle_current_state();
|
||||
return std::make_pair( m_t_old , m_dt );
|
||||
return std::make_pair( m_t_old , m_t );
|
||||
}
|
||||
|
||||
/*
|
||||
|
||||
@@ -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
|
||||
@@ -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
|
||||
@@ -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
|
||||
@@ -29,6 +29,7 @@
|
||||
#include <boost/numeric/odeint/stepper/generation/generation_runge_kutta_dopri5.hpp>
|
||||
#include <boost/numeric/odeint/stepper/generation/generation_runge_kutta_fehlberg78.hpp>
|
||||
|
||||
#include <boost/numeric/odeint/stepper/generation/generation_controlled_adams_bashforth_moulton.hpp>
|
||||
|
||||
#include <boost/numeric/odeint/stepper/generation/generation_rosenbrock4.hpp>
|
||||
|
||||
|
||||
@@ -0,0 +1,59 @@
|
||||
/*
|
||||
boost/numeric/odeint/stepper/detail/generation_controlled_adams_bashforth_moulton.hpp
|
||||
|
||||
[begin_description]
|
||||
Spezialization of the generation functions for creation of 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 GENERATION_CONTROLLED_ADAMS_BASHFORTH_MOULTON_HPP_INCLUDED
|
||||
#define GENERATION_CONTROLLED_ADAMS_BASHFORTH_MOULTON_HPP_INCLUDED
|
||||
|
||||
#include <boost/numeric/odeint/stepper/adaptive_adams_bashforth_moulton.hpp>
|
||||
#include <boost/numeric/odeint/stepper/controlled_adams_bashforth_moulton.hpp>
|
||||
#include <boost/numeric/odeint/stepper/generation/make_controlled.hpp>
|
||||
|
||||
namespace boost {
|
||||
namespace numeric {
|
||||
namespace odeint {
|
||||
|
||||
template< size_t Steps, class State , class Value , class Deriv , class Time , class Algebra , class Operations , class Resizer >
|
||||
struct get_controller< adaptive_adams_bashforth_moulton< Steps, State , Value , Deriv , Time , Algebra , Operations , Resizer > >
|
||||
{
|
||||
typedef adaptive_adams_bashforth_moulton<Steps, State, Value, Deriv, Time, Algebra, Operations, Resizer> stepper_type;
|
||||
typedef controlled_adams_bashforth_moulton< stepper_type > type;
|
||||
};
|
||||
|
||||
// controller factory for controlled_adams_bashforth_moulton
|
||||
template< class Stepper >
|
||||
struct controller_factory< Stepper , controlled_adams_bashforth_moulton< Stepper > >
|
||||
{
|
||||
typedef Stepper stepper_type;
|
||||
typedef controlled_adams_bashforth_moulton< stepper_type > controller_type;
|
||||
typedef typename controller_type::step_adjuster_type step_adjuster_type;
|
||||
typedef typename stepper_type::value_type value_type;
|
||||
typedef typename stepper_type::value_type time_type;
|
||||
|
||||
controller_type operator()( value_type abs_error , value_type rel_error , const stepper_type &stepper )
|
||||
{
|
||||
return controller_type(step_adjuster_type(abs_error, rel_error));
|
||||
}
|
||||
|
||||
controller_type operator()( value_type abs_error , value_type rel_error ,
|
||||
time_type max_dt, const stepper_type &stepper )
|
||||
{
|
||||
return controller_type( step_adjuster_type(abs_error, rel_error, max_dt));
|
||||
}
|
||||
};
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#endif
|
||||
@@ -200,7 +200,7 @@ public:
|
||||
|
||||
|
||||
|
||||
private:
|
||||
protected:
|
||||
|
||||
template< class StateIn >
|
||||
bool resize_m_xerr( const StateIn &x )
|
||||
|
||||
@@ -41,7 +41,8 @@ class rosenbrock4_dense_output
|
||||
public:
|
||||
|
||||
typedef ControlledStepper controlled_stepper_type;
|
||||
typedef typename controlled_stepper_type::stepper_type stepper_type;
|
||||
typedef typename unwrap_reference< controlled_stepper_type >::type unwrapped_controlled_stepper_type;
|
||||
typedef typename unwrapped_controlled_stepper_type::stepper_type stepper_type;
|
||||
typedef typename stepper_type::value_type value_type;
|
||||
typedef typename stepper_type::state_type state_type;
|
||||
typedef typename stepper_type::wrapped_state_type wrapped_state_type;
|
||||
@@ -54,7 +55,7 @@ public:
|
||||
typedef rosenbrock4_dense_output< ControlledStepper > dense_output_stepper_type;
|
||||
|
||||
rosenbrock4_dense_output( const controlled_stepper_type &stepper = controlled_stepper_type() )
|
||||
: m_stepper( stepper ) ,
|
||||
: m_stepper( stepper ) ,
|
||||
m_x1() , m_x2() ,
|
||||
m_current_state_x1( true ) ,
|
||||
m_t() , m_t_old() , m_dt()
|
||||
@@ -75,16 +76,17 @@ public:
|
||||
template< class System >
|
||||
std::pair< time_type , time_type > do_step( System system )
|
||||
{
|
||||
unwrapped_controlled_stepper_type &stepper = m_stepper;
|
||||
failed_step_checker fail_checker; // to throw a runtime_error if step size adjustment fails
|
||||
controlled_step_result res = fail;
|
||||
m_t_old = m_t;
|
||||
do
|
||||
{
|
||||
res = m_stepper.try_step( system , get_current_state() , m_t , get_old_state() , m_dt );
|
||||
res = stepper.try_step( system , get_current_state() , m_t , get_old_state() , m_dt );
|
||||
fail_checker(); // check for overflow of failed steps
|
||||
}
|
||||
while( res == fail );
|
||||
m_stepper.stepper().prepare_dense_output();
|
||||
stepper.stepper().prepare_dense_output();
|
||||
this->toggle_current_state();
|
||||
return std::make_pair( m_t_old , m_t );
|
||||
}
|
||||
@@ -96,20 +98,23 @@ public:
|
||||
template< class StateOut >
|
||||
void calc_state( time_type t , StateOut &x )
|
||||
{
|
||||
m_stepper.stepper().calc_state( t , x , get_old_state() , m_t_old , get_current_state() , m_t );
|
||||
unwrapped_controlled_stepper_type &stepper = m_stepper;
|
||||
stepper.stepper().calc_state( t , x , get_old_state() , m_t_old , get_current_state() , m_t );
|
||||
}
|
||||
|
||||
template< class StateOut >
|
||||
void calc_state( time_type t , const StateOut &x )
|
||||
{
|
||||
m_stepper.stepper().calc_state( t , x , get_old_state() , m_t_old , get_current_state() , m_t );
|
||||
unwrapped_controlled_stepper_type &stepper = m_stepper;
|
||||
stepper.stepper().calc_state( t , x , get_old_state() , m_t_old , get_current_state() , m_t );
|
||||
}
|
||||
|
||||
|
||||
template< class StateType >
|
||||
void adjust_size( const StateType &x )
|
||||
{
|
||||
m_stepper.adjust_size( x );
|
||||
unwrapped_controlled_stepper_type &stepper = m_stepper;
|
||||
stepper.adjust_size( x );
|
||||
resize_impl( x );
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user