Documentation


solver.h

Go to the documentation of this file.
00001 #ifndef ANIMAL_INTEGRATION_SOLVER_H
00002 #define ANIMAL_INTEGRATION_SOLVER_H
00003 
00004 #include <pending/animal/numerics.hpp>
00005 #include <animal/io.h>
00006 
00007 
00008 
00009 namespace animal { namespace integration {
00010 
00011 
00012 
00013 // /** \class SolverBase 
00014 //  Base class for all time differential equation solvers.
00015 
00016 //  A SolverBase integrates a function of time over a given time step. Contrary with a Stepper, a Solver can NOT decide to subdivide the given time step for purposes of accuracy.
00017     
00018 
00019 //*/
00020 
00021 //class SolverBase {
00022 
00023 //  /// Destructor does nothing
00024 //  virtual ~Solver(){}
00025 
00026 //};
00027 
00028 
00029 
00030 
00031 
00032 // ----------------------------------
00033 //  
00061 //  
00062 // ----------------------------------
00063 
00064 template 
00065 <
00066   class DerivativeF,
00067   class StepF,
00068   class TraitsT
00069 >
00070 
00071 struct Solver
00072 {
00073 
00075   typedef TraitsT                     Traits;
00076 
00078   typedef typename Traits::Real       Real;
00079 
00081   typedef typename Traits::State      State;
00082 
00084   typedef typename Traits::CopyState      CopyState;
00085 
00087   typedef typename Traits::Derivative Derivative;
00088 
00090   typedef typename Traits::SizeType   SizeType;
00091 
00092 
00097 
00101   Traits       traits;
00102 
00106     DerivativeF  writeDerivative;
00107 
00110   StepF        applyStep;
00111 
00113 
00114   
00119 
00123   Solver
00124     (
00125         const DerivativeF& df = DerivativeF(), 
00126         const StepF& sf = StepF(), 
00127         const Traits& tr = Traits()
00128     )
00129   : traits( tr )
00130     , writeDerivative(df)
00131     , applyStep(sf)
00132   {}
00133   
00135   virtual ~Solver(){}
00136 
00138   
00143 
00151   virtual void doStep
00152     (
00153           const State& initial_S,
00154           State& final_S,
00155           const Derivative& initial_D,
00156           const Real t, 
00157             const Real h
00158     )
00159     = 0;    // pure virtual method
00160     
00162     
00167 
00169     template< class S >
00170     void operator()
00171     (
00172         const State& initial_S,
00173         S& final_S,
00174         const Real t, 
00175         const Real h
00176     )
00177   {
00178         //cout<<"write derivative:"<<endl;
00179     writeDerivative( initial_S, D1, t );
00180     //cout<<"Euler, D1 = "; print(D1);
00181     doStep( initial_S, final_S, D1, t, h );
00182   }
00183     
00184     
00186     template< class S >
00187   void operator ()
00188     (
00189         S& s,
00190         const Real t, 
00191         const Real h
00192     )
00193   {
00194     operator()( s, s, t, h );
00195   }
00196 
00198 
00199 
00206   void resize( const SizeType size )
00207     { 
00208         traits.resize( D1, size );
00209         setSize( size );
00210     }
00211 
00213     
00214 protected:
00215 
00217     Derivative D1;
00218     
00220     virtual void setSize( const SizeType size ) = 0;
00221 
00222 };
00223 
00224 
00225 // \example solver_basic.cpp
00226 //  Time integration of the cos(t) function  
00227 //
00228 // \example solver_dimN.cpp
00229 //  Time integration of an n-dimensional function of time  
00230 
00231 
00232 //============================================================================
00233 
00249 // An example of a different CopyStateT is given in example solver_big.cpp
00250  
00251 template <
00252   class StateT,
00253   class DerivativeT,
00254     class CopyStateT = StateT,
00255   class RealT = double,
00256   class NumTraitsT = animal::Numerics<RealT>,
00257     class SizeTypeT = unsigned int
00258 >
00259 
00260 struct Solver_Traits
00261 {
00263   typedef RealT Real;
00264   
00267   typedef StateT State;
00268     
00273     typedef CopyStateT CopyState;
00274   
00277   typedef DerivativeT Derivative;
00278   
00280   typedef  NumTraitsT Numerics;
00281   
00283   typedef  SizeTypeT SizeType;
00284 
00286   void average( const State& s1, const State& s2, State& result ) const 
00287   {
00288       result = (s1 + s2)/2;
00289   }
00290 
00292   void rk4Step ( 
00293       const Derivative& d1,
00294       const Derivative& d2,
00295       const Derivative& d3,
00296       const Derivative& d4,
00297       Derivative& sum
00298   )
00299   const
00300   {
00301       sum = d1 + (d2 + d3)*2 + d4;
00302   }
00303 
00305   void copy( 
00306       const State& s,
00307       CopyState& cs
00308   )
00309   const
00310   {
00311       cs = s;
00312   }
00313 
00315   template <class Container>
00316   void resize ( Container& , const unsigned int) const{}
00317 
00318 };
00319 
00320 
00321 
00322 
00323 //==============================================================================
00324 
00325 
00332 template <
00333   class StateT,
00334   class DerivativeT,
00335     class CopyStateT = StateT,
00336   class RealT = double,
00337   class NumTraitsT = animal::Numerics<RealT>,
00338     class SizeTypeT = unsigned int
00339 >
00340 
00341 struct Solver_Traits_M
00342 {
00344   typedef RealT Real;
00345   
00360   typedef StateT State;
00361     
00366     typedef CopyStateT CopyState;
00367   
00382   typedef DerivativeT Derivative;
00383   
00385   typedef  NumTraitsT Numerics;
00386   
00388   typedef  SizeTypeT SizeType;
00389 
00391   template <class Container>
00392   void resize ( Container& c, const unsigned int size ) const
00393   {
00394       c.resize( size );
00395   }     
00396 
00398   void average( const State& s1, const State& s2, State& result ) const 
00399   {
00400       typename State::const_iterator is1=s1.begin(), end=s1.end();
00401       typename State::const_iterator is2=s2.begin();
00402       typename State::iterator r=result.begin();
00403       for( ; is1!=end; ++is1, ++is2, ++r )
00404           *r = (*is1 + *is2) *0.5;
00405   }
00406 
00408   void rk4Step ( 
00409       const Derivative& d1,
00410       const Derivative& d2,
00411       const Derivative& d3,
00412       const Derivative& d4,
00413       Derivative& sum
00414   )
00415   const
00416   {
00417       typename Derivative::const_iterator id1 = d1.begin(), end=d1.end();
00418       typename Derivative::const_iterator id2 = d2.begin();
00419       typename Derivative::const_iterator id3 = d3.begin();
00420       typename Derivative::const_iterator id4 = d4.begin();
00421       typename Derivative::iterator s = sum.begin();
00422       for( ; id1!=end; ++id1, ++id2, ++id3, ++id4, ++s )
00423           *s = *id1 + (*id2 + *id3)*2 + *id4;
00424   }
00425 
00427   void copy( 
00428       const State& s,
00429       CopyState& cs
00430   )
00431   const
00432   {
00433       cs = s;
00434   }
00435     
00436 };
00437 // \example solver_big.cpp
00438 //  Customization of Solver_Traits so that we avoid copying irrelevant data.
00439 
00440 
00441 
00442 
00445 template <class TraitsT>
00446 
00447 struct Derivative_Function
00448 {
00449   typedef typename TraitsT::Real       Real;       
00450   typedef typename TraitsT::State      State;      
00451   typedef typename TraitsT::Derivative Derivative; 
00452   typedef typename TraitsT::Numerics   Numerics;   
00453   
00455   virtual void operator()
00456     (
00457         const State& S,
00458         Derivative& D,
00459         const Real t
00460     ) 
00461     const
00462     = 0;
00463     
00464 };
00465 
00466 
00467 
00470 template <class TraitsT>
00471 
00472 struct Step_Function
00473 {
00474   typedef typename TraitsT::Real       Real;        
00475   typedef typename TraitsT::State      State;       
00476   typedef typename TraitsT::Derivative Derivative;  
00477   typedef typename TraitsT::Numerics   Numerics;    
00478   
00480   virtual void operator()
00481     (
00482         const State& initial_S,
00483         State& final_S,
00484         const Derivative& D,
00485         const Real h
00486     ) 
00487     const
00488     = 0;
00489     
00490 };
00491 
00492 } // end namespace integration
00493 } // end namespace animal
00494 
00495 
00496 
00497 #endif // ANIMAL_INTEGRATION_SOLVER_H

Generated on Thu Dec 23 13:52:27 2004 by doxygen 1.3.6