ODESystem.cpp

Go to the documentation of this file.
00001 #include "ODESystem.h"
00002 using namespace odeiv;
00003 
00004 #include "PCSIMException.h"
00005 
00006 #include <iostream>
00007 using std::cerr;
00008 using std::endl;
00009 
00010 FixedStepSolver::FixedStepSolver( ODESystem & sys, GslStepperType const& step ) : Solver( sys, step ) {
00011     dydt_in = new double[ sys.dimension() ];
00012     dydt_out = new double[ sys.dimension() ];
00013     y_err = new double[ sys.dimension() ];
00014 }
00015 
00016 FixedStepSolver::~FixedStepSolver() {
00017     if ( dydt_in )
00018         delete dydt_in;
00019     if ( dydt_out )
00020         delete dydt_out;
00021     if ( y_err )
00022         delete y_err;
00023 }
00024 
00025 void FixedStepSolver::reset( double dt, ODESystem & sys, const double y0[] ) {
00026     for ( int i = 0; i < sys.dimension(); i++ ) {
00027         _y_[ i ] = y0[ i ];
00028     }
00029     sys.derivatives( 0.0, _y_, dydt_in );
00030     stepper.reset( );
00031 }
00032 
00033 void FixedStepSolver::advance( double t, double dt, ODESystem & sys ) {
00034     // cerr << "FIXED STEP: " << dt ;
00035     stepper.apply( t, dt, _y_, y_err, dydt_in, dydt_out, sys );
00036     for ( int i = 0; i < sys.dimension(); i++ )
00037         dydt_in[ i ] = dydt_out[ i ];
00038     // cerr << " : Vm = " << _y_[ 0 ] << endl;
00039 }
00040 
00042 
00043 AdaptiveStepSolver::AdaptiveStepSolver( ODESystem & sys, GslStepperType const& step ) : Solver( sys, step ) {
00044     int_control = gsl_odeiv_control_y_new( 1e-6, 1e-6 );
00045     int_evolve = gsl_odeiv_evolve_alloc( sys.dimension() );
00046     h = 1e-6;
00047     //cerr << "AdaptiveStepSolver::AdaptiveStepSolver" << endl;
00048 }
00049 
00050 
00051 AdaptiveStepSolver::~AdaptiveStepSolver() {
00052   //cerr << "DESTRUCTOR AdaptiveStepSolver::AdaptiveStepSolver" << endl;
00053     if ( int_control != NULL )
00054         gsl_odeiv_control_free( int_control );
00055     if ( int_evolve != NULL )
00056         gsl_odeiv_evolve_free( int_evolve );
00057 }
00058 
00059 void AdaptiveStepSolver::reset( double dt, ODESystem & sys, const double y0[] ) {
00060     // cerr << "ADAPT RESET: " << dt << " ep=" << int_evolve << " sp=" << stepper->int_step << "sd=" << ( stepper->int_step ) ->dimension << endl;
00061     for ( int i = 0; i < sys.dimension(); i++ ) {
00062         _y_[ i ] = y0[ i ];
00063     }
00064     stepper.reset( );
00065     gsl_odeiv_evolve_reset( int_evolve );
00066     h = dt / 100.0;
00067     // cerr << "ADAPT RESET: " << dt << " ep=" << int_evolve << endl;
00068 }
00069 
00070 void AdaptiveStepSolver::advance( double t, double dt, ODESystem & sys ) {
00071     // cerr << "ADAPT STEP: " << " t=" << t << " dt=" << dt << endl ;
00072     double t1 = t + dt;
00073     //cerr << "ADAPT STEP: dt=" << dt << ", h=" << h << ", y = ";
00074     //for ( int i = 0; i < sys.dimension(); i++ ) {
00075     //  cerr << _y_[ i ] << ", " ;
00076     //}
00077     while ( t < t1 ) { 
00078         // cerr << "while ( t < t1 ) { " << dt << " ep=" << int_evolve->dimension << " sp=" << ( stepper->int_step ) ->dimension << endl;
00079         if ( gsl_odeiv_evolve_apply( int_evolve, int_control, stepper.int_step, &sys.gsl_sys, &t, t1, &h, _y_ ) != GSL_SUCCESS ) {
00080             throw( PCSIM::Exception( "AdaptiveStepSolver::advance", "ODE Solver error!" ) );
00081         }
00082         // cerr << " -> " << t << " ";
00083     }
00084     //cerr << ": Vm(" << t1 << ") = " << _y_[ 0 ] << endl;
00085 }
00086 

Generated on Wed Jul 9 16:34:39 2008 for PCSIM by  doxygen 1.5.5