12 #include <gsl/gsl_errno.h>
13 #include <gsl/gsl_matrix.h>
14 #include <gsl/gsl_odeiv2.h>
16 #include <boost/numeric/odeint.hpp>
17 using namespace boost::numeric;
27 #include "../mesh/VoxelJunction.h"
45 for (
unsigned int i = 0; i < rates_.size(); ++i )
49 gsl_odeiv2_driver_free( driver_ );
62 gsl_odeiv2_driver_reset( driver_ );
63 gsl_odeiv2_driver_reset_hstart( driver_, dt / 10.0 );
75 gsl_odeiv2_driver_free( driver_ );
77 driver_ = gsl_odeiv2_driver_alloc_y_new(
93 int status = gsl_odeiv2_driver_apply( driver_, &t, p->
currTime, varS());
94 if ( status != GSL_SUCCESS )
96 cout <<
"Error: VoxelPools::advance: GSL integration error at time "
98 cout <<
"Error info: " << status <<
", " <<
99 gsl_strerror( status ) << endl;
100 if ( status == GSL_EMAXITER )
101 cout <<
"Max number of steps exceeded\n";
102 else if ( status == GSL_ENOPROG )
103 cout <<
"Timestep has gotten too small\n";
104 else if ( status == GSL_EBADFUNC )
105 cout <<
"Internal error\n";
137 double absTol = sys_.epsAbs;
138 double relTol = sys_.epsRel;
147 const double fixedDt = 0.1;
149 if( sys_.method ==
"rk2" )
150 odeint::integrate_const( rk_midpoint_stepper_type_()
154 else if( sys_.method ==
"rk4" )
155 odeint::integrate_const( rk4_stepper_type_()
159 else if( sys_.method ==
"rk5")
160 odeint::integrate_const( rk_karp_stepper_type_()
164 else if( sys_.method ==
"rk5a")
165 odeint::integrate_adaptive(
166 odeint::make_controlled<rk_karp_stepper_type_>( absTol, relTol)
173 else if (
"rk54" == sys_.method )
174 odeint::integrate_const( rk_karp_stepper_type_()
178 else if (
"rk54a" == sys_.method )
179 odeint::integrate_adaptive(
180 odeint::make_controlled<rk_karp_stepper_type_>( absTol, relTol )
186 else if (
"rk5" == sys_.method )
187 odeint::integrate_const( rk_dopri_stepper_type_()
191 else if (
"rk5a" == sys_.method )
192 odeint::integrate_adaptive(
193 odeint::make_controlled<rk_dopri_stepper_type_>( absTol, relTol )
199 else if( sys_.method ==
"rk8" )
200 odeint::integrate_const( rk_felhberg_stepper_type_()
204 else if( sys_.method ==
"rk8a" )
205 odeint::integrate_adaptive(
206 odeint::make_controlled<rk_felhberg_stepper_type_>( absTol, relTol )
214 odeint::integrate_adaptive(
215 odeint::make_controlled<rk_karp_stepper_type_>( absTol, relTol )
222 if ( !stoichPtr_->getAllowNegative() )
224 unsigned int nv = stoichPtr_->getNumVarPools();
226 for (
unsigned int i = 0; i < nv; ++i )
228 if ( signbit(vs[i]) )
237 gsl_odeiv2_driver_reset_hstart( driver_, dt );
243 int VoxelPools::gslFunc(
double t,
const double* y,
double *dydt,
248 double* q =
const_cast< double*
>( y );
270 void VoxelPools::evalRates(
271 const vector_type_& y, vector_type_& dydt,
const double t,
VoxelPools* vp
283 unsigned int numCoreRates )
286 for (
unsigned int i = 0; i < rates_.size(); ++i )
289 rates_.resize( rates.size() );
290 for (
unsigned int i = 0; i < numCoreRates; ++i )
291 rates_[i] = rates[i]->copyWithVolScaling( getVolume(), 1, 1 );
292 for (
unsigned int i = numCoreRates; i < rates.size(); ++i )
294 rates_[i] = rates[i]->copyWithVolScaling( getVolume(),
295 getXreacScaleSubstrates(i - numCoreRates),
296 getXreacScaleProducts(i - numCoreRates ) );
301 unsigned int numCoreRates,
unsigned int index )
305 if ( index >= rates_.size() )
307 delete( rates_[index] );
308 if ( index >= numCoreRates )
309 rates_[index] = rates[index]->copyWithVolScaling(
311 getXreacScaleSubstrates(index - numCoreRates),
312 getXreacScaleProducts(index - numCoreRates ) );
314 rates_[index] = rates[index]->copyWithVolScaling(
315 getVolume(), 1.0, 1.0 );
321 vector< double > v( N.
nColumns(), 0.0 );
322 vector< double >::iterator j = v.begin();
324 unsigned int totVar = stoichPtr_->getNumVarPools() +
325 stoichPtr_->getNumProxyPools();
327 unsigned int totInvar = stoichPtr_->getNumBufPools();
329 N.
nRows() == stoichPtr_->getNumAllPools() );
330 assert( N.
nColumns() == rates_.size() );
332 for ( vector< RateTerm* >::const_iterator
333 i = rates_.begin(); i != rates_.end(); i++)
336 assert( !std::isnan( *( j-1 ) ) );
339 for (
unsigned int i = 0; i < totVar; ++i)
341 for (
unsigned int i = 0; i < totInvar ; ++i)
351 const double* s, vector< double >& v )
const
354 assert( N.
nColumns() == rates_.size() );
356 vector< RateTerm* >::const_iterator i;
358 v.resize( rates_.size(), 0.0 );
359 vector< double >::iterator j = v.begin();
361 for ( i = rates_.begin(); i != rates_.end(); i++)
364 assert( !std::isnan( *( j-1 ) ) );
371 cout <<
"numAllRates = " << rates_.size() <<
372 ", numLocalRates= " << stoichPtr_->getNumCoreRates() << endl;
383 updateAllRateTerms( stoichPtr_->getRateTerms(),
384 stoichPtr_->getNumCoreRates() );
void updateFuncs(double *s, double t) const
Updates the function values, within s.
const Stoich * stoichPtr_
void print() const
Debugging utility.
void advance(const ProcInfo *p)
Do the numerical integration. Advance the simulation.
unsigned int nColumns() const
void updateRateTerms(const vector< RateTerm * > &rates, unsigned int numCoreRates, unsigned int index)
unsigned int nRows() const
void setStoich(Stoich *stoich, const OdeSystem *ode)
double computeRowRate(unsigned int row, const vector< double > &v) const
void updateRates(const double *s, double *yprime) const
void print() const
Used for debugging.
virtual void setVolumeAndDependencies(double vol)
void updateReacVelocities(const double *s, vector< double > &v) const
void updateAllRateTerms(const vector< RateTerm * > &rates, unsigned int numCoreRates)
Updates all the rate constants from the reference rates vector.
void setVolumeAndDependencies(double vol)
Handles volume change and subsequent cascading updates.
void setInitDt(double dt)
Set initial timestep to use by the solver.