Click here to Skip to main content
15,881,803 members
Articles / Programming Languages / C++

Solving ordinary differential equations in C++

Rate me:
Please Sign up or sign in to vote.
4.82/5 (36 votes)
19 Oct 2011CPOL8 min read 311K   3.8K   97  
This article explains a framework for solving ordinary differential equations, which is based on template metaprogramming.
/*
 boost header: numeric/odeint/integrator_constant_step.hpp

 Copyright 2009 Karsten Ahnert
 Copyright 2009 Mario Mulansky
 Copyright 2009 Andre Bergner

 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_INTEGRATOR_CONSTANT_STEPSIZE_HPP_INCLUDED
#define BOOST_NUMERIC_ODEINT_INTEGRATOR_CONSTANT_STEPSIZE_HPP_INCLUDED

#include <boost/numeric/odeint/observer.hpp>

namespace boost {
namespace numeric {
namespace odeint {


    template< 
        class Stepper ,
        class DynamicalSystem ,
        class Observer
        >
    size_t integrate_const(
	Stepper &stepper ,
	DynamicalSystem &system ,
	typename Stepper::container_type &state ,
	typename Stepper::time_type start_time ,
	typename Stepper::time_type end_time ,
	typename Stepper::time_type dt ,
	Observer &observer
	)
    {
        stepper.adjust_size( state );
	size_t iteration = 0;
        while( start_time < end_time )
	{
	    observer( start_time , state , system );
            stepper.do_step( system , state , start_time , dt );
            start_time += dt;
	    ++iteration;
        }
	observer( start_time , state , system );

	return iteration;
    }



    template<
        class Stepper ,
        class DynamicalSystem
        >
    size_t integrate_const(
	Stepper &stepper ,
	DynamicalSystem &system ,
	typename Stepper::container_type &state ,
	typename Stepper::time_type start_time ,
	typename Stepper::time_type end_time ,
	typename Stepper::time_type dt 
	)
    {
	return integrate_const(
                stepper , system , state, start_time , end_time , dt ,
                do_nothing_observer<
                typename Stepper::time_type ,
                typename Stepper::container_type ,
                DynamicalSystem >
                               );
    }



    template<
	class Stepper , 
        class DynamicalSystem ,
        class Observer
        >
    typename Stepper::time_type integrate_const_steps(
	Stepper &stepper ,
	DynamicalSystem &system ,
	typename Stepper::container_type &state ,
	typename Stepper::time_type start_time ,
	typename Stepper::time_type dt ,
	size_t num_of_steps ,
	Observer &observer
	)
    {
        stepper.adjust_size( state );
	size_t iteration = 0;
        while( iteration < num_of_steps )
	{
	    observer( start_time , state , system );
            stepper.do_step( system , state , start_time , dt );
            start_time += dt;
	    ++iteration;
        }
	observer( start_time , state , system );

	return start_time;
    }


    template<
	class Stepper , 
        class DynamicalSystem
	>
    typename Stepper::time_type integrate_const_steps(
	Stepper &stepper ,
	DynamicalSystem &system ,
	typename Stepper::container_type &state ,
	typename Stepper::time_type start_time ,
	typename Stepper::time_type dt ,
	size_t num_of_steps 
	)
    {
	return integrate_const_steps(
                stepper , system , state , start_time , dt , num_of_steps ,
                do_nothing_observer<
                typename Stepper::time_type ,
                typename Stepper::container_type ,
                DynamicalSystem >
                                     );
    }

    
    
   

} // odeint
} // numeric
} // boost

#endif //BOOST_NUMERIC_ODEINT_INTEGRATOR_CONSTANT_STEPSIZE_HPP_INCLUDED

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Germany Germany
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions