ODEBND
base_gsl.hpp
00001 // Copyright (C) 2012-2014 Benoit Chachuat, Imperial College London.
00002 // All Rights Reserved.
00003 // This code is published under the Eclipse Public License.
00004 
00005 #ifndef MC__BASE_GSL_HPP
00006 #define MC__BASE_GSL_HPP
00007 
00008 #include <iostream>
00009 #include <sys/time.h>
00010 
00011 #include "base_ode.hpp"
00012 
00013 #include "gsl/gsl_errno.h"
00014 #include "gsl/gsl_odeiv2.h"
00015 #include "gsl/gsl_interp.h"
00016 
00017 namespace mc
00018 {
00024 class BASE_GSL: public BASE_ODE
00025 {
00026 protected:
00028   double *_vec_state;
00029 
00031   double _h;
00032 
00033 public:
00038 
00039   BASE_GSL()
00040     : BASE_ODE(), _vec_state( 0 ), _h( 0. )
00041     {}
00042 
00044   virtual ~BASE_GSL()
00045     {     
00046       delete[] _vec_state;
00047     }
00048 
00050   struct Options
00051   {
00053     Options():
00054       INTMETH(RKF45), H0(1e-2), HMIN(0e0), HMAX(0e0), NMAX(0), RTOL(1e-7), ATOL(1e-7)
00055       {}
00057     template <typename U> Options& operator=
00058       ( U&options ){
00059         INTMETH   = options.INTMETH;
00060         H0        = options.H0;
00061         HMIN      = options.HMIN;
00062         HMAX      = options.HMAX;
00063         NMAX      = options.NMAX;
00064         RTOL      = options.RTOL;
00065         ATOL      = options.ATOL;
00066         return *this;
00067       }
00069     enum INTEGRATION_METHOD{
00070       RKF45=0,    
00071       RK8PD,      
00072       MSADAMS,    
00073       MSBDF    
00074     };
00076     INTEGRATION_METHOD INTMETH;
00078     double H0;
00080     double HMIN;
00082     double HMAX;
00084     unsigned int NMAX;
00086     double RTOL;
00088     double ATOL;
00089   };
00090 
00092   struct Stats
00093   {
00095     Stats():
00096       cputime(0.), numSteps(0), numRHS(0), numJAC(0)
00097       {}
00099     void reset()
00100       { cputime = 0.; numSteps = numRHS = numJAC = 0; }
00101 
00103     double cputime;
00105     unsigned int numSteps;
00107     unsigned int numRHS;
00109     unsigned int numJAC;
00110   };
00113 protected:
00115   static void _init_stats
00116     ( Stats&stats );
00117 
00119   static void _final_stats
00120     ( Stats&stats );
00121 
00123   static void _print_stats
00124     ( const Stats&stats, std::ostream&os=std::cout );
00125 
00127   BASE_GSL(const BASE_GSL&);
00128   BASE_GSL& operator=(const BASE_GSL&);
00129 };
00130 
00131 inline void
00132 BASE_GSL::_init_stats
00133 ( Stats&stats )
00134 {
00135   // Initialize statistics
00136   stats.reset();
00137   timeval time;
00138   gettimeofday(&time, 0) ;
00139   stats.cputime = - time.tv_sec - time.tv_usec*1e-6;
00140 }
00141 
00142 inline void
00143 BASE_GSL::_final_stats
00144 ( Stats&stats )
00145 {
00146   // Get final CPU time
00147   timeval time;
00148   gettimeofday(&time, 0);
00149   stats.cputime += time.tv_sec + time.tv_usec*1e-6;
00150 }
00151 
00152 inline void
00153 BASE_GSL::_print_stats
00154 ( const Stats&stats, std::ostream&os )
00155 {
00156   // Statistics
00157   os << " No STEPS  " << stats.numSteps
00158      << std::endl
00159      << " No EVALATIONS" << "   RHS: " << stats.numRHS
00160                          << "   JAC: " << stats.numJAC
00161      << std::endl
00162      << " CPU TIME (SEC)     " << std::fixed << std::left
00163                                << std::setprecision(5) << stats.cputime
00164      << std::endl;
00165   return;
00166 }
00167 
00168 } // end namescape mc
00169 
00170 #endif
00171