Boost logo

Boost-Commit :

Subject: [Boost-commit] svn:boost r57571 - in sandbox/odeint: boost/numeric/odeint libs/numeric/odeint/examples
From: mario.mulansky_at_[hidden]
Date: 2009-11-11 11:52:27


Author: mariomulansky
Date: 2009-11-11 11:52:26 EST (Wed, 11 Nov 2009)
New Revision: 57571
URL: http://svn.boost.org/trac/boost/changeset/57571

Log:
state_copy_observer

Text files modified:
   sandbox/odeint/boost/numeric/odeint/integrator.hpp | 45 +++++++++------------------------------
   sandbox/odeint/boost/numeric/odeint/observer.hpp | 27 ++++++++++++++++++++++++
   sandbox/odeint/libs/numeric/odeint/examples/lorenz_controlled.cpp | 4 --
   3 files changed, 39 insertions(+), 37 deletions(-)

Modified: sandbox/odeint/boost/numeric/odeint/integrator.hpp
==============================================================================
--- sandbox/odeint/boost/numeric/odeint/integrator.hpp (original)
+++ sandbox/odeint/boost/numeric/odeint/integrator.hpp 2009-11-11 11:52:26 EST (Wed, 11 Nov 2009)
@@ -15,6 +15,7 @@
 
 #include <boost/numeric/odeint/stepsize_controller_standard.hpp>
 #include <boost/numeric/odeint/resizer.hpp>
+#include <boost/numeric/odeint/observer.hpp>
 #include <vector>
 #include <limits>
 
@@ -41,12 +42,13 @@
         controlled_step_result result;
         size_t iterations = 0;
         typename Stepper::time_type t = start_time;
+ typename Stepper::time_type dt_ = dt;
 
         observer(t, state, system);
         
         while( t < end_time )
         {
- result = controller.controlled_step( stepper, system, state, t, dt );
+ result = controller.controlled_step( stepper, system, state, t, dt_ );
             if( result != STEP_SIZE_DECREASED )
             { // we actually did a step forward
                 observer(t, state, system);
@@ -54,13 +56,13 @@
             }
             while( result != SUCCESS )
             {
- result = controller.controlled_step( stepper, system, state, t, dt );
+ result = controller.controlled_step( stepper, system, state, t, dt_ );
                 if( result != STEP_SIZE_DECREASED )
                 { // we did a step
                     observer(t, state, system);
                     iterations++;
                 }
- if( !( t+dt > t) )
+ if( !( t+dt_ > t) )
                     throw; // we've reached machine precision with dt - no advancing in t
             }
         }
@@ -86,45 +88,20 @@
         class Stepper,
         class DynamicalSystem,
         class StepController,
+ class TimeContainer,
         class InsertIterator
>
     size_t integrate(Stepper &stepper,
                      DynamicalSystem &system,
                      StepController &controller,
                      typename Stepper::container_type &state,
- std::vector<typename Stepper::time_type> &times,
+ TimeContainer &times,
                      InsertIterator state_inserter,
- typename Stepper::time_type dt)
+ typename Stepper::time_type &dt)
     {
- // iterators for the time and state vectors
- typename std::vector<typename Stepper::time_type>::iterator t_iter = times.begin();
-
- controlled_step_result result;
- size_t iterations = 0;
- typename Stepper::time_type t = *t_iter;
-
- while( true ) { // loop will break from inside
-
- if( t >= *t_iter ) { // we've reached the next time point
- *state_inserter++ = state; // save the state
- t_iter++; // next time point
- }
-
- if( t_iter >= times.end() ) // reached end of integration time
- break; // stop loop
-
- result = controller.controlled_step( stepper, system, state, t, dt );
- if( result != STEP_SIZE_DECREASED )
- iterations++;
- while( result != SUCCESS ) {
- result = controller.controlled_step( stepper, system, state, t, dt );
- if( result != STEP_SIZE_DECREASED )
- iterations++;
- if( !( t+dt > t) )
- throw; // we've reached machine precision with dt - no advancing in t
- }
- }
- return iterations;
+ state_copy_observer<InsertIterator, TimeContainer> observer(times, state_inserter);
+ return integrate(stepper, system, controller, *(times.begin()),
+ dt, state, *(times.end()-1), observer);
     }
 
 

Modified: sandbox/odeint/boost/numeric/odeint/observer.hpp
==============================================================================
--- sandbox/odeint/boost/numeric/odeint/observer.hpp (original)
+++ sandbox/odeint/boost/numeric/odeint/observer.hpp 2009-11-11 11:52:26 EST (Wed, 11 Nov 2009)
@@ -13,6 +13,8 @@
 #ifndef BOOST_NUMERIC_ODEINT_OBSERVER_HPP_INCLUDED
 #define BOOST_NUMERIC_ODEINT_OBSERVER_HPP_INCLUDED
 
+#include<vector>
+
 namespace boost {
 namespace numeric {
 namespace odeint {
@@ -23,6 +25,31 @@
     {
     }
     
+
+ template< class InsertIterator, class TimeContainer = std::vector<double> >
+ class state_copy_observer {
+
+ private:
+ TimeContainer *m_times;
+ InsertIterator m_state_inserter;
+ typename TimeContainer::iterator m_time_iter;
+
+ typedef typename TimeContainer::value_type time_type;
+
+ public:
+ state_copy_observer( TimeContainer &times, InsertIterator state_inserter )
+ : m_times(&times), m_state_inserter(state_inserter), m_time_iter(m_times->begin())
+ { }
+
+ template< class Container, class System >
+ void operator () (time_type t, Container &state, System &system ) {
+ if( t >= *m_time_iter ) { // we've reached the next time point
+ *m_state_inserter++ = state; // insert the state
+ m_time_iter++; // next time point
+ }
+ }
+
+ };
    
 
 } // odeint

Modified: sandbox/odeint/libs/numeric/odeint/examples/lorenz_controlled.cpp
==============================================================================
--- sandbox/odeint/libs/numeric/odeint/examples/lorenz_controlled.cpp (original)
+++ sandbox/odeint/libs/numeric/odeint/examples/lorenz_controlled.cpp 2009-11-11 11:52:26 EST (Wed, 11 Nov 2009)
@@ -61,9 +61,7 @@
     x[2] = 20.0;
 
     ode_step_half_step< ode_step_euler< state_type > > euler;
-// ode_step_euler< state_type > euler;
- step_controller_standard< state_type, double >
- controller( eps_abs , eps_rel, 1.0, 1.0);
+ step_controller_standard< state_type, double > controller( eps_abs , eps_rel, 1.0, 1.0);
     
     cout.precision(5);
     cout.setf(ios::fixed,ios::floatfield);


Boost-Commit list run by bdawes at acm.org, david.abrahams at rcn.com, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk