From 5b48c56451fd39ccfcc4a1fc9c3aff0fd15c2e77 Mon Sep 17 00:00:00 2001 From: Karsten Ahnert Date: Wed, 27 Jun 2012 08:26:09 +0200 Subject: [PATCH] added const step iterator --- .../odeint/iterator/const_step_iterator.hpp | 108 ++++++++++++++++++ libs/numeric/odeint/examples/Jamfile | 1 + .../odeint/examples/const_step_iterator.cpp | 60 ++++++++++ libs/numeric/odeint/test/Jamfile | 3 +- .../odeint/test/const_step_iterator.cpp | 88 ++++++++++++++ 5 files changed, 259 insertions(+), 1 deletion(-) create mode 100644 boost/numeric/odeint/iterator/const_step_iterator.hpp create mode 100644 libs/numeric/odeint/examples/const_step_iterator.cpp create mode 100644 libs/numeric/odeint/test/const_step_iterator.cpp diff --git a/boost/numeric/odeint/iterator/const_step_iterator.hpp b/boost/numeric/odeint/iterator/const_step_iterator.hpp new file mode 100644 index 00000000..45284d30 --- /dev/null +++ b/boost/numeric/odeint/iterator/const_step_iterator.hpp @@ -0,0 +1,108 @@ + +/* + [auto_generated] + boost/numeric/odeint/iterator/const_step_ode_iterator.hpp + + [begin_description] + Iterator for iterating throught the solution of an ODE with constant step size. + [end_description] + + Copyright 2009-2011 Karsten Ahnert + Copyright 2009-2011 Mario Mulansky + + 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_ITERATOR_CONST_STEP_ODE_ITERATOR_HPP_INCLUDED +#define BOOST_NUMERIC_ODEINT_ITERATOR_CONST_STEP_ODE_ITERATOR_HPP_INCLUDED + +# include + + +namespace boost { +namespace numeric { +namespace odeint { + + template< class Stepper , class System > + class const_step_iterator : public boost::iterator_facade + < + const_step_iterator< Stepper , System > , + typename Stepper::state_type , + boost::incrementable_traversal_tag + > + { + private: + + typedef Stepper stepper_type; + typedef System system_type; + typedef typename stepper_type::state_type state_type; + typedef typename stepper_type::time_type time_type; + + public: + + const_step_iterator( stepper_type stepper , system_type sys , state_type &s , time_type t , time_type dt ) + : m_stepper( stepper ) , m_system( sys ) , m_state( s ) , m_t( t ) , m_dt( dt ) {} + + private: + + + + friend class boost::iterator_core_access; + + void increment() + { + m_stepper.do_step( m_system , m_state , m_t , m_dt ); + m_t += m_dt; + } + + bool equal( const_step_iterator const& other ) const + { + return m_t > other.m_t; + } + + state_type& dereference() const + { + return m_state; + } + + stepper_type m_stepper; + system_type m_system; + state_type &m_state; + time_type m_t; + time_type m_dt; + + }; + + + template< class Stepper , class System > + const_step_iterator< Stepper , System > make_const_step_iterator( + Stepper stepper , + System system , + typename Stepper::state_type &x , + typename Stepper::time_type t , + typename Stepper::time_type dt ) + { + return const_step_iterator< Stepper , System >( stepper , system , x , t , dt ); + } + + + + + + + +// make_const_step_ode_iterator + + + + +} // namespace odeint +} // namespace numeric +} // namespace boost + + + +#endif // BOOST_NUMERIC_ODEINT_ITERATOR_CONST_STEP_ODE_ITERATOR_HPP_INCLUDED diff --git a/libs/numeric/odeint/examples/Jamfile b/libs/numeric/odeint/examples/Jamfile index 33b92518..64f1ae5f 100644 --- a/libs/numeric/odeint/examples/Jamfile +++ b/libs/numeric/odeint/examples/Jamfile @@ -34,6 +34,7 @@ exe simple1d : simple1d.cpp ; exe stochastic_euler : stochastic_euler.cpp : : -std=c++0x ; exe generation_functions : generation_functions.cpp : : -std=c++0x ; exe heun : heun.cpp : : -std=c++0x ; +exe const_step_iterator : const_step_iterator.cpp ; # build-project mtl ; # build-project ublas ; diff --git a/libs/numeric/odeint/examples/const_step_iterator.cpp b/libs/numeric/odeint/examples/const_step_iterator.cpp new file mode 100644 index 00000000..fe235267 --- /dev/null +++ b/libs/numeric/odeint/examples/const_step_iterator.cpp @@ -0,0 +1,60 @@ +/* + * const_step_ode_iterator.cpp + * + * Created on: Jun 26, 2012 + * Author: karsten + */ + +#include +#include +#include + +#include + +#include +#include + + +using namespace boost::numeric::odeint; + +const double sigma = 10.0; +const double R = 28.0; +const double b = 8.0 / 3.0; + +struct lorenz +{ + template< class State , class Deriv > + void operator()( const State &x_ , Deriv &dxdt_ , double t ) const + { + typename boost::range_iterator< const State >::type x = boost::begin( x_ ); + typename boost::range_iterator< Deriv >::type dxdt = boost::begin( dxdt_ ); + + dxdt[0] = sigma * ( x[1] - x[0] ); + dxdt[1] = R * x[0] - x[1] - x[0] * x[2]; + dxdt[2] = -b * x[2] + x[0] * x[1]; + } +}; + + +struct printer +{ + std::ostream &m_out ; + printer( std::ostream &out = std::cout ) : m_out( out ) { } + + template< class State > + void operator()( const State &s ) + { + m_out << s[0] << "\t" << s[1] << "\t" << s[2] << "\n"; + } +}; + +int main( int argc , char **argv ) +{ + typedef boost::array< double , 3 > state_type; + runge_kutta4< state_type > stepper; + state_type x = {{ 10.0 , 10.0 , 10.0 }}; + std::for_each( make_const_step_iterator( stepper , lorenz() , x , 0.0 , 0.01 ) , + make_const_step_iterator( stepper , lorenz() , x , 10.0 , 0.01 ) , printer() ); + + return 0; +} diff --git a/libs/numeric/odeint/test/Jamfile b/libs/numeric/odeint/test/Jamfile index 06736ed2..74e25b96 100644 --- a/libs/numeric/odeint/test/Jamfile +++ b/libs/numeric/odeint/test/Jamfile @@ -45,5 +45,6 @@ test-suite "odeint" [ run integrate_implicit.cpp ] [ run generation.cpp ] [ run trivial_state.cpp ] + [ run const_step_iterator.cpp ] : valgrind - ; \ No newline at end of file + ; diff --git a/libs/numeric/odeint/test/const_step_iterator.cpp b/libs/numeric/odeint/test/const_step_iterator.cpp new file mode 100644 index 00000000..8873675d --- /dev/null +++ b/libs/numeric/odeint/test/const_step_iterator.cpp @@ -0,0 +1,88 @@ +/* + * const_step_ode_iterator.cpp + * + * Created on: Jun 26, 2012 + * Author: karsten + */ + +#define BOOST_TEST_MODULE odeint_const_step_iterator + +#include +#include +#include + +#include +#include +#include + +#include +#include + + +using namespace boost::numeric::odeint; + +const double sigma = 10.0; +const double R = 28.0; +const double b = 8.0 / 3.0; + +//[ system_function_without_perturbations +struct lorenz +{ + template< class State , class Deriv > + void operator()( const State &x_ , Deriv &dxdt_ , double t ) const + { + typename boost::range_iterator< const State >::type x = boost::begin( x_ ); + typename boost::range_iterator< Deriv >::type dxdt = boost::begin( dxdt_ ); + + dxdt[0] = sigma * ( x[1] - x[0] ); + dxdt[1] = R * x[0] - x[1] - x[0] * x[2]; + dxdt[2] = -b * x[2] + x[0] * x[1]; + } +}; +//] + + + + +BOOST_AUTO_TEST_SUITE( const_step_iterator_test ) + +BOOST_AUTO_TEST_CASE( empty_test ) +{ +} + +struct printer +{ + template< class State > + void operator()( const State &s ) + { + std::cerr << "huhu" << std::endl; + } +}; + +BOOST_AUTO_TEST_CASE( compile_test ) +{ + typedef boost::array< double , 3 > state_type; + runge_kutta4< state_type > stepper; + state_type x = {{ 10.0 , 10.0 , 10.0 }}; + std::for_each( make_const_step_iterator( stepper , lorenz() , x , 0.0 , 0.01 ) , + make_const_step_iterator( stepper , lorenz() , x , 10.0 , 0.01 ) , printer() ); +} + +// BOOST_AUTO_TEST_CASE( test_is_pair ) +// { +// typedef std::pair< int , int > type1; +// typedef std::pair< int& , int > type2; +// typedef std::pair< int , int& > type3; +// typedef std::pair< int& , int& > type4; +// typedef std::pair< const int , int > type5; +// typedef std::pair< const int& , int > type6; + +// BOOST_STATIC_ASSERT(( is_pair< type1 >::value )); +// BOOST_STATIC_ASSERT(( is_pair< type2 >::value )); +// BOOST_STATIC_ASSERT(( is_pair< type3 >::value )); +// BOOST_STATIC_ASSERT(( is_pair< type4 >::value )); +// BOOST_STATIC_ASSERT(( is_pair< type5 >::value )); +// BOOST_STATIC_ASSERT(( is_pair< type6 >::value )); +// } + +BOOST_AUTO_TEST_SUITE_END()