2
0
mirror of https://github.com/boostorg/odeint.git synced 2026-01-25 18:32:14 +00:00

added const step iterator

This commit is contained in:
Karsten Ahnert
2012-06-27 08:26:09 +02:00
parent 0f126ac9e4
commit 5b48c56451
5 changed files with 259 additions and 1 deletions

View File

@@ -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 <boost/iterator/iterator_facade.hpp>
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

View File

@@ -34,6 +34,7 @@ exe simple1d : simple1d.cpp ;
exe stochastic_euler : stochastic_euler.cpp : : <cxxflags>-std=c++0x ;
exe generation_functions : generation_functions.cpp : : <cxxflags>-std=c++0x ;
exe heun : heun.cpp : : <cxxflags>-std=c++0x ;
exe const_step_iterator : const_step_iterator.cpp ;
# build-project mtl ;
# build-project ublas ;

View File

@@ -0,0 +1,60 @@
/*
* const_step_ode_iterator.cpp
*
* Created on: Jun 26, 2012
* Author: karsten
*/
#include <iostream>
#include <utility>
#include <algorithm>
#include <boost/array.hpp>
#include <boost/numeric/odeint/stepper/runge_kutta4.hpp>
#include <boost/numeric/odeint/iterator/const_step_iterator.hpp>
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;
}

View File

@@ -45,5 +45,6 @@ test-suite "odeint"
[ run integrate_implicit.cpp ]
[ run generation.cpp ]
[ run trivial_state.cpp ]
[ run const_step_iterator.cpp ]
: <testing.launcher>valgrind
;
;

View File

@@ -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 <iostream>
#include <utility>
#include <algorithm>
#include <boost/test/unit_test.hpp>
#include <boost/static_assert.hpp>
#include <boost/array.hpp>
#include <boost/numeric/odeint/stepper/runge_kutta4.hpp>
#include <boost/numeric/odeint/iterator/const_step_iterator.hpp>
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()