COPASI API  4.16.103
Public Member Functions | Protected Member Functions | Static Protected Member Functions | Protected Attributes | Private Member Functions | Static Private Member Functions | Private Attributes | Friends | List of all members
CStochMethod Class Referenceabstract

#include <CStochMethod.h>

Inheritance diagram for CStochMethod:
Inheritance graph
[legend]
Collaboration diagram for CStochMethod:
Collaboration graph
[legend]

Public Member Functions

 CStochMethod (const CStochMethod &src, const CCopasiContainer *pParent=NULL)
 
virtual bool elevateChildren ()
 
virtual bool isValidProblem (const CCopasiProblem *pProblem)
 
virtual void start (const CState *initialState)
 
virtual Status step (const double &deltaT)
 
 ~CStochMethod ()
 
- Public Member Functions inherited from CTrajectoryMethod
 CTrajectoryMethod (const CTrajectoryMethod &src, const CCopasiContainer *pParent=NULL)
 
const CVector< C_INT > & getRoots () const
 
void setCurrentState (CState *currentState)
 
void setProblem (CTrajectoryProblem *problem)
 
virtual void stateChanged ()
 
 ~CTrajectoryMethod ()
 
- Public Member Functions inherited from CCopasiMethod
 CCopasiMethod (const CCopasiMethod &src, const CCopasiContainer *pParent=NULL)
 
const CCopasiMethod::SubTypegetSubType () const
 
const CCopasiTask::TypegetType () const
 
virtual void load (CReadConfig &configBuffer, CReadConfig::Mode mode=CReadConfig::SEARCH)
 
virtual void print (std::ostream *ostream) const
 
virtual void printResult (std::ostream *ostream) const
 
virtual bool setCallBack (CProcessReport *pCallBack)
 
virtual ~CCopasiMethod ()
 
- Public Member Functions inherited from CCopasiParameterGroup
bool addGroup (const std::string &name)
 
bool addParameter (const CCopasiParameter &parameter)
 
bool addParameter (const std::string &name, const CCopasiParameter::Type type)
 
template<class CType >
bool addParameter (const std::string &name, const CCopasiParameter::Type type, const CType &value)
 
void addParameter (CCopasiParameter *pParameter)
 
CCopasiParameterGroupassertGroup (const std::string &name)
 
template<class CType >
CCopasiParameterassertParameter (const std::string &name, const CCopasiParameter::Type type, const CType &defaultValue)
 
index_iterator beginIndex () const
 
name_iterator beginName () const
 
 CCopasiParameterGroup (const CCopasiParameterGroup &src, const CCopasiContainer *pParent=NULL)
 
 CCopasiParameterGroup (const std::string &name, const CCopasiContainer *pParent=NULL, const std::string &objectType="ParameterGroup")
 
void clear ()
 
index_iterator endIndex () const
 
name_iterator endName () const
 
CCopasiParameterGroupgetGroup (const std::string &name)
 
const CCopasiParameterGroupgetGroup (const std::string &name) const
 
CCopasiParameterGroupgetGroup (const size_t &index)
 
const CCopasiParameterGroupgetGroup (const size_t &index) const
 
size_t getIndex (const std::string &name) const
 
std::string getKey (const std::string &name) const
 
std::string getKey (const size_t &index) const
 
virtual const std::string & getName (const size_t &index) const
 
virtual const CObjectInterfacegetObject (const CCopasiObjectName &cn) const
 
CCopasiParametergetParameter (const std::string &name)
 
const CCopasiParametergetParameter (const std::string &name) const
 
CCopasiParametergetParameter (const size_t &index)
 
const CCopasiParametergetParameter (const size_t &index) const
 
CCopasiParameter::Type getType (const std::string &name) const
 
CCopasiParameter::Type getType (const size_t &index) const
 
std::string getUniqueParameterName (const CCopasiParameter *pParameter) const
 
const CCopasiParameter::ValuegetValue (const std::string &name) const
 
const CCopasiParameter::ValuegetValue (const size_t &index) const
 
CCopasiParameter::ValuegetValue (const std::string &name)
 
CCopasiParameter::ValuegetValue (const size_t &index)
 
CCopasiParameterGroupoperator= (const CCopasiParameterGroup &rhs)
 
bool removeParameter (const std::string &name)
 
bool removeParameter (const size_t &index)
 
template<class CType >
bool setValue (const std::string &name, const CType &value)
 
template<class CType >
bool setValue (const size_t &index, const CType &value)
 
size_t size () const
 
bool swap (const size_t &iFrom, const size_t &iTo)
 
bool swap (index_iterator &from, index_iterator &to)
 
virtual ~CCopasiParameterGroup ()
 
- Public Member Functions inherited from CCopasiParameter
 CCopasiParameter (const CCopasiParameter &src, const CCopasiContainer *pParent=NULL)
 
 CCopasiParameter (const std::string &name, const Type &type, const void *pValue=NULL, const CCopasiContainer *pParent=NULL, const std::string &objectType="Parameter")
 
virtual CCopasiObjectName getCN () const
 
virtual const std::string & getKey () const
 
virtual std::string getObjectDisplayName (bool regular=true, bool richtext=false) const
 
const CCopasiParameter::TypegetType () const
 
const ValuegetValue () const
 
ValuegetValue ()
 
virtual voidgetValuePointer () const
 
CCopasiObjectgetValueReference () const
 
bool isValidValue (const C_FLOAT64 &value) const
 
bool isValidValue (const C_INT32 &value) const
 
bool isValidValue (const unsigned C_INT32 &value) const
 
bool isValidValue (const bool &value) const
 
bool isValidValue (const std::string &value) const
 
bool isValidValue (const CCopasiObjectName &value) const
 
bool isValidValue (const std::vector< CCopasiParameter * > &value) const
 
CCopasiParameteroperator= (const CCopasiParameter &rhs)
 
template<class CType >
bool setValue (const CType &value)
 
bool setValue (const std::vector< CCopasiParameter * > &value)
 
virtual ~CCopasiParameter ()
 
- Public Member Functions inherited from CCopasiContainer
virtual bool add (CCopasiObject *pObject, const bool &adopt=true)
 
 CCopasiContainer (const std::string &name, const CCopasiContainer *pParent=NULL, const std::string &type="CN", const unsigned C_INT32 &flag=CCopasiObject::Container)
 
 CCopasiContainer (const CCopasiContainer &src, const CCopasiContainer *pParent=NULL)
 
virtual std::string getChildObjectUnits (const CCopasiObject *pObject) const
 
virtual const objectMapgetObjects () const
 
virtual std::string getUnits () const
 
virtual const CCopasiObjectgetValueObject () const
 
virtual bool remove (CCopasiObject *pObject)
 
virtual ~CCopasiContainer ()
 
- Public Member Functions inherited from CCopasiObject
void addDirectDependency (const CCopasiObject *pObject)
 
 CCopasiObject (const CCopasiObject &src, const CCopasiContainer *pParent=NULL)
 
void clearDirectDependencies ()
 
void clearRefresh ()
 
bool dependsOn (DataObjectSet candidates, const DataObjectSet &context=DataObjectSet()) const
 
void getAllDependencies (DataObjectSet &dependencies, const DataObjectSet &context) const
 
virtual const DataObjectSetgetDirectDependencies (const DataObjectSet &context=DataObjectSet()) const
 
CCopasiContainergetObjectAncestor (const std::string &type) const
 
CCopasiDataModelgetObjectDataModel ()
 
const CCopasiDataModelgetObjectDataModel () const
 
const std::string & getObjectName () const
 
CCopasiContainergetObjectParent () const
 
const std::string & getObjectType () const
 
virtual const
CObjectInterface::ObjectSet
getPrerequisites () const
 
virtual RefreshgetRefresh () const
 
UpdateMethodgetUpdateMethod () const
 
bool hasCircularDependencies (DataObjectSet &candidates, DataObjectSet &verified, const DataObjectSet &context) const
 
bool hasUpdateMethod () const
 
bool isArray () const
 
bool isContainer () const
 
bool isDataModel () const
 
bool isMatrix () const
 
bool isNameVector () const
 
bool isNonUniqueName () const
 
virtual bool isPrerequisiteForContext (const CObjectInterface *pObject, const CMath::SimulationContextFlag &context, const CObjectInterface::ObjectSet &changedObjects) const
 
bool isReference () const
 
bool isRoot () const
 
bool isSeparator () const
 
bool isStaticString () const
 
bool isValueBool () const
 
bool isValueDbl () const
 
bool isValueInt () const
 
bool isValueInt64 () const
 
bool isValueString () const
 
bool isVector () const
 
virtual bool mustBeDeleted (const DataObjectSet &deletedObjects) const
 
void removeDirectDependency (const CCopasiObject *pObject)
 
void setDirectDependencies (const DataObjectSet &directDependencies)
 
bool setObjectName (const std::string &name)
 
virtual bool setObjectParent (const CCopasiContainer *pParent)
 
void setObjectValue (const C_FLOAT64 &value)
 
void setObjectValue (const C_INT32 &value)
 
void setObjectValue (const bool &value)
 
template<class CType >
void setRefresh (CType *pType, void(CType::*method)(void))
 
template<class CType >
void setUpdateMethod (CType *pType, void(CType::*method)(const C_FLOAT64 &))
 
template<class CType >
void setUpdateMethod (CType *pType, void(CType::*method)(const C_INT32 &))
 
template<class CType >
void setUpdateMethod (CType *pType, void(CType::*method)(const bool &))
 
virtual ~CCopasiObject ()
 
- Public Member Functions inherited from CObjectInterface
 CObjectInterface ()
 
virtual ~CObjectInterface ()
 

Protected Member Functions

C_INT32 calculateAmu (size_t reaction_index)
 
 CStochMethod (const CCopasiContainer *pParent=NULL)
 
virtual C_FLOAT64 doSingleStep (C_FLOAT64 time, C_FLOAT64 endtime)=0
 
size_t generateReactionIndex ()
 
C_FLOAT64 generateReactionTime ()
 
C_FLOAT64 generateReactionTime (size_t reaction_index)
 
virtual void initMethod (C_FLOAT64 start_time)=0
 
void setupDependencyGraphAndBalances ()
 
C_INT32 updatePropensities ()
 
C_INT32 updateSystemState (size_t reaction_index, const C_FLOAT64 &time)
 
- Protected Member Functions inherited from CTrajectoryMethod
 CTrajectoryMethod (const CCopasiMethod::SubType &subType, const CCopasiContainer *pParent=NULL)
 
- Protected Member Functions inherited from CCopasiMethod
 CCopasiMethod (const CCopasiTask::Type &taskType, const SubType &subType, const CCopasiContainer *pParent=NULL)
 
- Protected Member Functions inherited from CCopasiParameterGroup
 CCopasiParameterGroup ()
 
- Protected Member Functions inherited from CCopasiContainer
template<class CType >
CCopasiObjectaddMatrixReference (const std::string &name, CType &reference, const unsigned C_INT32 &flag=0)
 
template<class CType >
CCopasiObjectaddObjectReference (const std::string &name, CType &reference, const unsigned C_INT32 &flag=0)
 
template<class CType >
CCopasiObjectaddVectorReference (const std::string &name, CType &reference, const unsigned C_INT32 &flag=0)
 
void initObjects ()
 
- Protected Member Functions inherited from CCopasiObject
 CCopasiObject ()
 
 CCopasiObject (const std::string &name, const CCopasiContainer *pParent=NULL, const std::string &type="CN", const unsigned C_INT32 &flag=0)
 

Static Protected Member Functions

static bool modelHasAssignments (const CModel *pModel)
 

Protected Attributes

C_FLOAT64 mA0
 
std::vector< C_FLOAT64mAmu
 
std::vector< C_FLOAT64mAmuOld
 
CDependencyGraph mDG
 
bool mDoCorrection
 
size_t mFirstMetabIndex
 
bool mHasAssignments
 
std::vector< std::vector
< CStochBalance > > 
mLocalBalances
 
std::vector< std::vector
< CStochBalance > > 
mLocalSubstrates
 
std::vector< C_INT64mNumbers
 
size_t mNumReactions
 
CModelmpModel
 
CRandommpRandomGenerator
 
- Protected Attributes inherited from CTrajectoryMethod
CStatempCurrentState
 
CTrajectoryProblemmpProblem
 
CVector< C_INTmRoots
 
- Protected Attributes inherited from CCopasiMethod
CProcessReportmpCallBack
 
- Protected Attributes inherited from CCopasiParameter
std::string mKey
 
CCopasiObjectmpValueReference
 
size_t mSize
 
Value mValue
 
- Protected Attributes inherited from CCopasiContainer
objectMap mObjects
 

Private Member Functions

std::set< std::string > * getAffects (size_t reaction_index)
 
std::set< std::string > * getDependsOn (size_t reaction_index)
 
void initializeParameter ()
 

Static Private Member Functions

static C_INT32 checkModel (CModel *pmodel)
 

Private Attributes

C_INT32 mMaxBalance
 
C_INT64 mMaxIntBeforeStep
 
size_t mMaxSteps
 
bool mMaxStepsReached
 

Friends

CTrajectoryMethodCTrajectoryMethod::createMethod (CCopasiMethod::SubType subType)
 

Additional Inherited Members

- Public Types inherited from CTrajectoryMethod
enum  Status { FAILURE = -1, NORMAL = 0, ROOT = 1 }
 
- Public Types inherited from CCopasiMethod
enum  SubType {
  unset = 0, RandomSearch, RandomSearchMaster, SimulatedAnnealing,
  CoranaWalk, DifferentialEvolution, ScatterSearch, GeneticAlgorithm,
  EvolutionaryProgram, SteepestDescent, HybridGASA, GeneticAlgorithmSR,
  HookeJeeves, LevenbergMarquardt, NelderMead, SRES,
  Statistics, ParticleSwarm, Praxis, TruncatedNewton,
  Newton, deterministic, LSODAR, directMethod,
  stochastic, tauLeap, adaptiveSA, hybrid,
  hybridLSODA, hybridODE45, DsaLsodar, tssILDM,
  tssILDMModified, tssCSP, mcaMethodReder, scanMethod,
  lyapWolf, sensMethod, EFMAlgorithm, EFMBitPatternTreeAlgorithm,
  EFMBitPatternAlgorithm, Householder, crossSectionMethod, linearNoiseApproximation
}
 
- Public Types inherited from CCopasiParameterGroup
typedef parameterGroup::iterator index_iterator
 
typedef
CCopasiContainer::objectMap::iterator 
name_iterator
 
typedef std::vector
< CCopasiParameter * > 
parameterGroup
 
- Public Types inherited from CCopasiParameter
enum  Type {
  DOUBLE = 0, UDOUBLE, INT, UINT,
  BOOL, GROUP, STRING, CN,
  KEY, FILE, EXPRESSION, INVALID
}
 
- Public Types inherited from CCopasiContainer
typedef std::multimap
< std::string, CCopasiObject * > 
objectMap
 
- Public Types inherited from CCopasiObject
typedef std::set< const
CCopasiObject * > 
DataObjectSet
 
typedef std::vector< Refresh * > DataUpdateSequence
 
- Public Types inherited from CObjectInterface
typedef std::set< const
CObjectInterface * > 
ObjectSet
 
typedef std::vector
< CObjectInterface * > 
UpdateSequence
 
- Static Public Member Functions inherited from CTrajectoryMethod
static CTrajectoryMethodcreateMethod (CCopasiMethod::SubType subType=CCopasiMethod::deterministic)
 
- Static Public Member Functions inherited from CCopasiObject
static std::vector< Refresh * > buildUpdateSequence (const DataObjectSet &objects, const DataObjectSet &uptoDateObjects, const DataObjectSet &context=DataObjectSet())
 
static void setRenameHandler (CRenameHandler *rh)
 
- Static Public Attributes inherited from CCopasiMethod
static const std::string SubTypeName []
 
static const char * XMLSubType []
 
- Static Public Attributes inherited from CCopasiParameter
static const std::string TypeName []
 
static const char * XMLType []
 
- Static Public Attributes inherited from CCopasiContainer
static const std::vector
< CCopasiContainer * > 
EmptyList
 
- Protected Types inherited from CCopasiObject
enum  Flag {
  Container = 0x1, Vector = 0x2, Matrix = 0x4, NameVector = 0x8,
  Reference = 0x10, ValueBool = 0x20, ValueInt = 0x40, ValueInt64 = 0x80,
  ValueDbl = 0x100, NonUniqueName = 0x200, StaticString = 0x400, ValueString = 0x800,
  Separator = 0x1000, ModelEntity = 0x2000, Array = 0x4000, DataModel = 0x8000,
  Root = 0x10000, Gui = 0x20000
}
 
- Static Protected Attributes inherited from CCopasiObject
static CRenameHandlersmpRenameHandler = NULL
 

Detailed Description

Definition at line 47 of file CStochMethod.h.

Constructor & Destructor Documentation

CStochMethod::CStochMethod ( const CCopasiContainer pParent = NULL)
protected

Default constructor.

Parameters
constCCopasiContainer * pParent (default: NULL)

Definition at line 48 of file CStochMethod.cpp.

References CRandom::createGenerator(), initializeParameter(), mpRandomGenerator, and CRandom::mt19937.

48  :
50 {
53 }
void initializeParameter()
static CRandom * createGenerator(CRandom::Type type=CRandom::mt19937, unsigned C_INT32 seed=0)
Definition: CRandom.cpp:49
CRandom * mpRandomGenerator
Definition: CStochMethod.h:241
CStochMethod::CStochMethod ( const CStochMethod src,
const CCopasiContainer pParent = NULL 
)

Copy constructor.

Parameters
constCStochMethod & src,
constCCopasiContainer * pParent (Default: NULL)

Definition at line 55 of file CStochMethod.cpp.

References CRandom::createGenerator(), initializeParameter(), mpRandomGenerator, and CRandom::mt19937.

56  :
57  CTrajectoryMethod(src, pParent)
58 {
61 }
void initializeParameter()
static CRandom * createGenerator(CRandom::Type type=CRandom::mt19937, unsigned C_INT32 seed=0)
Definition: CRandom.cpp:49
CRandom * mpRandomGenerator
Definition: CStochMethod.h:241
CStochMethod::~CStochMethod ( )

Destructor.

Definition at line 63 of file CStochMethod.cpp.

References mpRandomGenerator.

64 {
65  delete mpRandomGenerator;
66  mpRandomGenerator = NULL;
67 }
CRandom * mpRandomGenerator
Definition: CStochMethod.h:241

Member Function Documentation

C_INT32 CStochMethod::calculateAmu ( size_t  reaction_index)
protected

Calculate one of the propensities

Returns
mFail
See Also
mFail

Definition at line 237 of file CStochMethod.cpp.

References C_FLOAT64, C_INT32, C_INT64, CModel::getReactions(), mAmu, mDoCorrection, mLocalSubstrates, mNumbers, and mpModel.

Referenced by updatePropensities(), and updateSystemState().

238 {
239  C_FLOAT64 rate_factor = mpModel->getReactions()[index]->calculateParticleFlux();
240 
241  if (rate_factor < 0.0)
242  {
243  // TODO CRITICAL Create a warning message
244  rate_factor = 0.0;
245  }
246 
247  if (!mDoCorrection)
248  {
249  mAmu[index] = rate_factor;
250  return 0;
251  }
252 
253  // We need the product of the cmu and hmu for this step.
254  // We calculate this in one go, as there are fewer steps to
255  // perform and we eliminate some possible rounding errors.
256  C_FLOAT64 amu = 1; // initially
257  //size_t total_substrates = 0;
258  C_INT32 num_ident = 0;
259  C_INT64 number = 0;
260  C_INT64 lower_bound;
261  // substrate_factor - The substrates, raised to their multiplicities,
262  // multiplied with one another. If there are, e.g. m substrates of type m,
263  // and n of type N, then substrate_factor = M^m * N^n.
264  C_FLOAT64 substrate_factor = 1;
265  // First, find the reaction associated with this index.
266  // Keep a pointer to this.
267  // Iterate through each substrate in the reaction
268  const std::vector<CStochBalance> & substrates = mLocalSubstrates[index];
269 
270  int flag = 0;
271 
272  for (size_t i = 0; i < substrates.size(); i++)
273  {
274  num_ident = substrates[i].mMultiplicity;
275 
276  if (num_ident > 1)
277  {
278  flag = 1;
279  number = mNumbers[substrates[i].mIndex];
280  lower_bound = number - num_ident;
281  substrate_factor = substrate_factor * pow((double) number, (int)(num_ident - 1)); //optimization
282 
283  number--; //optimization
284 
285  while (number > lower_bound)
286  {
287  amu *= number;
288  number--;
289  }
290  }
291  }
292 
293  if ((amu == 0) || (substrate_factor == 0)) // at least one substrate particle number is zero
294  {
295  mAmu[index] = 0;
296  return 0;
297  }
298 
299  // rate_factor is the rate function divided by substrate_factor.
300  // It would be more efficient if this was generated directly, since in effect we
301  // are multiplying and then dividing by the same thing (substrate_factor)!
302 
303  if (flag)
304  {
305  //cout << "Rate factor = " << rate_factor << endl;
306  amu *= rate_factor / substrate_factor;
307  mAmu[index] = amu;
308  }
309  else
310  {
311  mAmu[index] = rate_factor;
312  }
313 
314  return 0;
315 }
std::vector< C_FLOAT64 > mAmu
Definition: CStochMethod.h:57
bool mDoCorrection
Definition: CStochMethod.h:72
CModel * mpModel
Definition: CStochMethod.h:246
#define C_INT64
Definition: copasi.h:88
std::vector< std::vector< CStochBalance > > mLocalSubstrates
Definition: CStochMethod.h:262
#define C_INT32
Definition: copasi.h:90
long int flag
Definition: f2c.h:52
#define C_FLOAT64
Definition: copasi.h:92
CCopasiVectorNS< CReaction > & getReactions()
Definition: CModel.cpp:1039
std::vector< C_INT64 > mNumbers
Definition: CStochMethod.h:267
C_INT32 CStochMethod::checkModel ( CModel pmodel)
staticprivate

This checks if a model is suitable for stochastic simulation. It returns a suggestion which method to use

Parameters
modelThe model to check
Returns
1: direct method, 2: next reaction method, -1: no stochastic simulation possible

Definition at line 42 of file CStochMethod.cpp.

43 {
44  // Here several checks will be performed to validate the model
45  return 2; // suggest next reaction method
46 }
virtual C_FLOAT64 CStochMethod::doSingleStep ( C_FLOAT64  time,
C_FLOAT64  endtime 
)
protectedpure virtual

Do one iteration of the simulation

Returns
Current simulation time or -1 if error.

Implemented in CStochNextReactionMethod.

Referenced by step().

bool CStochMethod::elevateChildren ( )
virtual

This methods must be called to elevate subgroups to derived objects. The default implementation does nothing.

Returns
bool success

Reimplemented from CCopasiParameterGroup.

Definition at line 104 of file CStochMethod.cpp.

References initializeParameter().

105 {
107  return true;
108 }
void initializeParameter()
size_t CStochMethod::generateReactionIndex ( )
protected

Generate the index of a putative reaction.

Returns
The reaction index

Definition at line 389 of file CStochMethod.cpp.

References C_FLOAT64, CRandom::getRandomOO(), CModel::getReactions(), mA0, mAmu, mpModel, mpRandomGenerator, and CCopasiVector< T >::size().

390 {
392  C_FLOAT64 sum = 0;
393  size_t index = 0;
394 
395  while (index < (mpModel->getReactions().size() - 1))
396  {
397  sum += mAmu[index] /* /mA0 */;
398 
399  if (rand1 <= sum)
400  {return index;}
401 
402  index++;
403  }
404 
405  return index;
406 }
std::vector< C_FLOAT64 > mAmu
Definition: CStochMethod.h:57
virtual C_FLOAT64 getRandomOO()
Definition: CRandom.cpp:254
virtual size_t size() const
CModel * mpModel
Definition: CStochMethod.h:246
#define C_FLOAT64
Definition: copasi.h:92
CCopasiVectorNS< CReaction > & getReactions()
Definition: CModel.cpp:1039
CRandom * mpRandomGenerator
Definition: CStochMethod.h:241
C_FLOAT64 mA0
Definition: CStochMethod.h:62
C_FLOAT64 CStochMethod::generateReactionTime ( )
protected

Generate the putative time taken before any reaction takes place

Returns
The time before the reaction

Definition at line 408 of file CStochMethod.cpp.

References C_FLOAT64, CRandom::getRandomOO(), mA0, and mpRandomGenerator.

Referenced by CStochNextReactionMethod::setupPriorityQueue(), and CStochNextReactionMethod::updatePriorityQueue().

409 {
410  if (mA0 == 0) return std::numeric_limits<C_FLOAT64>::infinity();
411 
413  return - 1 * log(rand2) / mA0;
414 }
virtual C_FLOAT64 getRandomOO()
Definition: CRandom.cpp:254
#define C_FLOAT64
Definition: copasi.h:92
CRandom * mpRandomGenerator
Definition: CStochMethod.h:241
C_FLOAT64 mA0
Definition: CStochMethod.h:62
C_FLOAT64 CStochMethod::generateReactionTime ( size_t  reaction_index)
protected

Generate the putative time taken before a special reaction takes place

Returns
The time before the reaction

Definition at line 416 of file CStochMethod.cpp.

References C_FLOAT64, CRandom::getRandomOO(), mAmu, and mpRandomGenerator.

417 {
418  if (mAmu[reaction_index] == 0) return std::numeric_limits<C_FLOAT64>::infinity();
419 
421  return - 1 * log(rand2) / mAmu[reaction_index];
422 }
std::vector< C_FLOAT64 > mAmu
Definition: CStochMethod.h:57
virtual C_FLOAT64 getRandomOO()
Definition: CRandom.cpp:254
#define C_FLOAT64
Definition: copasi.h:92
CRandom * mpRandomGenerator
Definition: CStochMethod.h:241
std::set< std::string > * CStochMethod::getAffects ( size_t  reaction_index)
private

Get the set of metabolites which change number when a given reaction is executed.

Parameters
reaction_indexThe index of the reaction being executed.
Returns
The set of affected metabolites.

Definition at line 560 of file CStochMethod.cpp.

References CModelEntity::FIXED, CCopasiParameter::getKey(), CModel::getReactions(), mpModel, and CCopasiVector< T >::size().

Referenced by setupDependencyGraphAndBalances().

561 {
562  std::set<std::string> *retset = new std::set<std::string>;
563 
564  // Get the balances associated with the reaction at this index
565  // XXX We first get the chemical equation, then the balances, since the getBalances method in CReaction is unimplemented!
566  const CCopasiVector<CChemEqElement> & balances = mpModel->getReactions()[reaction_index]->getChemEq().getBalances();
567 
568  for (size_t i = 0; i < balances.size(); i++)
569  {
570  if (!balances[i]->getMetabolite()) continue;
571 
572  if (fabs(balances[i]->getMultiplicity()) >= 0.1)
573  if (balances[i]->getMetabolite()->getStatus() != CModelEntity::FIXED)
574  {
575  retset->insert(balances[i]->getMetabolite()->getKey());
576  }
577  }
578 
579  return retset;
580 }
virtual size_t size() const
CModel * mpModel
Definition: CStochMethod.h:246
virtual const std::string & getKey() const
CCopasiVectorNS< CReaction > & getReactions()
Definition: CModel.cpp:1039
std::set< std::string > * CStochMethod::getDependsOn ( size_t  reaction_index)
private

Get the set of metabolites on which a given reaction depends.

Parameters
reaction_indexThe index of the reaction being executed.
Returns
The set of metabolites depended on.

Definition at line 534 of file CStochMethod.cpp.

References CModel::getReactions(), mpModel, CFunctionParameter::PARAMETER, and CCopasiVector< T >::size().

Referenced by setupDependencyGraphAndBalances().

535 {
536  std::set<std::string> *retset = new std::set<std::string>;
537 
538  size_t i, imax = mpModel->getReactions()[reaction_index]->getFunctionParameters().size();
539  size_t j, jmax;
540 
541  for (i = 0; i < imax; ++i)
542  {
543  if (mpModel->getReactions()[reaction_index]->getFunctionParameters()[i]->getUsage() == CFunctionParameter::PARAMETER)
544  continue;
545 
546  //metablist = mpModel->getReactions()[reaction_index]->getParameterMappingMetab(i);
547  const std::vector <std::string> & metabKeylist =
548  mpModel->getReactions()[reaction_index]->getParameterMappings()[i];
549  jmax = metabKeylist.size();
550 
551  for (j = 0; j < jmax; ++j)
552  {
553  retset->insert(metabKeylist[j]);
554  }
555  }
556 
557  return retset;
558 }
virtual size_t size() const
CModel * mpModel
Definition: CStochMethod.h:246
CCopasiVectorNS< CReaction > & getReactions()
Definition: CModel.cpp:1039
void CStochMethod::initializeParameter ( )
private

Intialize the method parameter

Definition at line 69 of file CStochMethod.cpp.

References CCopasiParameterGroup::assertParameter(), CCopasiParameter::BOOL, C_INT32, CCopasiParameterGroup::getParameter(), CCopasiParameter::getValue(), CCopasiParameter::INT, CCopasiParameter::Value::pBOOL, CCopasiParameter::Value::pINT, CCopasiParameter::Value::pUINT, CCopasiParameterGroup::removeParameter(), CCopasiParameterGroup::setValue(), and CCopasiParameter::UINT.

Referenced by CStochMethod(), and elevateChildren().

70 {
71  CCopasiParameter *pParm;
72 
73  assertParameter("Max Internal Steps", CCopasiParameter::INT, (C_INT32) 1000000);
74  assertParameter("Subtype", CCopasiParameter::UINT, (unsigned C_INT32) 2);
75  assertParameter("Use Random Seed", CCopasiParameter::BOOL, false);
76  assertParameter("Random Seed", CCopasiParameter::UINT, (unsigned C_INT32) 1);
77 
78  // Check whether we have a method with the old parameter names
79  if ((pParm = getParameter("STOCH.MaxSteps")) != NULL)
80  {
81  setValue("Max Internal Steps", *pParm->getValue().pINT);
82  removeParameter("STOCH.MaxSteps");
83 
84  if ((pParm = getParameter("STOCH.Subtype")) != NULL)
85  {
86  setValue("Subtype", *pParm->getValue().pUINT);
87  removeParameter("STOCH.Subtype");
88  }
89 
90  if ((pParm = getParameter("STOCH.UseRandomSeed")) != NULL)
91  {
92  setValue("Use Random Seed", *pParm->getValue().pBOOL);
93  removeParameter("STOCH.UseRandomSeed");
94  }
95 
96  if ((pParm = getParameter("STOCH.RandomSeed")) != NULL)
97  {
98  setValue("Random Seed", *pParm->getValue().pUINT);
99  removeParameter("STOCH.RandomSeed");
100  }
101  }
102 }
#define C_INT32
Definition: copasi.h:90
bool removeParameter(const std::string &name)
const Value & getValue() const
bool setValue(const std::string &name, const CType &value)
unsigned C_INT32 * pUINT
CCopasiParameter * getParameter(const std::string &name)
CCopasiParameter * assertParameter(const std::string &name, const CCopasiParameter::Type type, const CType &defaultValue)
virtual void CStochMethod::initMethod ( C_FLOAT64  start_time)
protectedpure virtual

Initialization.

Implemented in CStochNextReactionMethod.

Referenced by start().

bool CStochMethod::isValidProblem ( const CCopasiProblem pProblem)
virtual

Check if the method is suitable for this problem

Returns
bool suitability of the method

Reimplemented from CTrajectoryMethod.

Definition at line 583 of file CStochMethod.cpp.

References CCopasiMessage::ERROR, CModel::getCompartments(), CTrajectoryProblem::getDuration(), CModel::getEvents(), CModel::getMetabolites(), CCopasiProblem::getModel(), CModel::getModelValues(), CModel::getNumMetabs(), CModel::getNumModelValues(), CModel::getTotSteps(), CCopasiParameter::getValue(), CTrajectoryMethod::isValidProblem(), MCTrajectoryMethod, CModelEntity::ODE, CCopasiParameter::Value::pINT, CCopasiVector< T >::size(), and CModel::suitableForStochasticSimulation().

584 {
585  if (!CTrajectoryMethod::isValidProblem(pProblem)) return false;
586 
587  const CTrajectoryProblem * pTP = dynamic_cast<const CTrajectoryProblem *>(pProblem);
588 
589  if (pTP->getDuration() < 0.0)
590  {
591  //back integration not possible
593  return false;
594  }
595 
596  if (pTP->getModel()->getTotSteps() < 1)
597  {
598  //at least one reaction necessary
600  return false;
601  }
602 
603  //check for rules
604  size_t i, imax = pTP->getModel()->getNumModelValues();
605 
606  for (i = 0; i < imax; ++i)
607  {
608  if (pTP->getModel()->getModelValues()[i]->getStatus() == CModelEntity::ODE)
609  {
610  //ode rule found
612  return false;
613  }
614  }
615 
616  imax = pTP->getModel()->getNumMetabs();
617 
618  for (i = 0; i < imax; ++i)
619  {
620  if (pTP->getModel()->getMetabolites()[i]->getStatus() == CModelEntity::ODE)
621  {
622  //ode rule found
624  return false;
625  }
626  }
627 
628  imax = pTP->getModel()->getCompartments().size();
629 
630  for (i = 0; i < imax; ++i)
631  {
632  if (pTP->getModel()->getCompartments()[i]->getStatus() == CModelEntity::ODE)
633  {
634  //ode rule found
636  return false;
637  }
638  }
639 
640  // if (modelHasAssignments(pTP->getModel()))
641  // {
642  // CCopasiMessage(CCopasiMessage::EXCEPTION, MCTrajectoryMethod + 19);
643  // return false;
644  //}
645  // this test is disabled since we have preliminary support for assignments
646 
647  //TODO: rewrite CModel::suitableForStochasticSimulation() to use
648  // CCopasiMessage
649  std::string message = pTP->getModel()->suitableForStochasticSimulation();
650 
651  if (message != "")
652  {
653  //model not suitable, message describes the problem
654  CCopasiMessage(CCopasiMessage::ERROR, message.c_str());
655  return false;
656  }
657 
658  if (* getValue("Max Internal Steps").pINT <= 0)
659  {
660  //max steps should be at least 1
662  return false;
663  }
664 
665  //events are not supported at the moment
666  if (pTP->getModel()->getEvents().size() > 0)
667  {
669  return false;
670  }
671 
672  return true;
673 }
CCopasiVectorN< CEvent > & getEvents()
Definition: CModel.cpp:1110
const CCopasiVector< CMetab > & getMetabolites() const
Definition: CModel.cpp:1051
const CCopasiVectorN< CModelValue > & getModelValues() const
Definition: CModel.cpp:1060
virtual size_t size() const
size_t getNumMetabs() const
Definition: CModel.cpp:1118
virtual bool isValidProblem(const CCopasiProblem *pProblem)
size_t getTotSteps() const
Definition: CModel.cpp:1136
#define MCTrajectoryMethod
const C_FLOAT64 & getDuration() const
const Value & getValue() const
CCopasiVectorNS< CCompartment > & getCompartments()
Definition: CModel.cpp:1145
size_t getNumModelValues() const
Definition: CModel.cpp:1139
std::string suitableForStochasticSimulation() const
Definition: CModel.cpp:3901
CModel * getModel() const
bool CStochMethod::modelHasAssignments ( const CModel pModel)
staticprotected

tests if the model contains a global value with an assignment rule that is used in calculations

Definition at line 676 of file CStochMethod.cpp.

References CModelEntity::ASSIGNMENT, CModel::getCompartments(), CModel::getMetabolites(), CModel::getModelValues(), CModel::getNumMetabs(), CModel::getNumModelValues(), and CCopasiVector< T >::size().

Referenced by start().

677 {
678  size_t i, imax = pModel->getNumModelValues();
679 
680  for (i = 0; i < imax; ++i)
681  {
682  if (pModel->getModelValues()[i]->getStatus() == CModelEntity::ASSIGNMENT)
683  if (pModel->getModelValues()[i]->isUsed())
684  {
685  //used assignment found
686  return true;
687  }
688  }
689 
690  imax = pModel->getNumMetabs();
691 
692  for (i = 0; i < imax; ++i)
693  {
694  if (pModel->getMetabolites()[i]->getStatus() == CModelEntity::ASSIGNMENT)
695  if (pModel->getMetabolites()[i]->isUsed())
696  {
697  //used assignment found
698  return true;
699  }
700  }
701 
702  imax = pModel->getCompartments().size();
703 
704  for (i = 0; i < imax; ++i)
705  {
706  if (pModel->getCompartments()[i]->getStatus() == CModelEntity::ASSIGNMENT)
707  if (pModel->getCompartments()[i]->isUsed())
708  {
709  //used assignment found
710  return true;
711  }
712  }
713 
714  return false;
715 }
const CCopasiVector< CMetab > & getMetabolites() const
Definition: CModel.cpp:1051
const CCopasiVectorN< CModelValue > & getModelValues() const
Definition: CModel.cpp:1060
virtual size_t size() const
size_t getNumMetabs() const
Definition: CModel.cpp:1118
CCopasiVectorNS< CCompartment > & getCompartments()
Definition: CModel.cpp:1145
size_t getNumModelValues() const
Definition: CModel.cpp:1139
void CStochMethod::setupDependencyGraphAndBalances ( )
protected

Set up the dependency graph and the balances

Definition at line 424 of file CStochMethod.cpp.

References CDependencyGraph::addDependent(), C_INT32, CDependencyGraph::clear(), CModelEntity::FIXED, getAffects(), getDependsOn(), CCopasiVector< T >::getIndex(), CModel::getMetabolitesX(), CModel::getReactions(), max, mDG, CStochBalance::mIndex, mLocalBalances, mLocalSubstrates, mMaxBalance, mMaxIntBeforeStep, mMaxSteps, CStochBalance::mMultiplicity, mNumReactions, mpModel, CDependencyGraph::resize(), and CCopasiVector< T >::size().

Referenced by start().

425 {
426  mDG.clear();
427  std::vector< std::set<std::string>* > DependsOn;
428  std::vector< std::set<std::string>* > Affects;
429  size_t i, j;
430  // Do for each reaction:
431 
432  for (i = 0; i < mNumReactions; i++)
433  {
434  // Get the set of metabolites which affect the value of amu for this
435  // reaction i.e. the set on which amu depends. This may be more than
436  // the set of substrates, since the kinetics can involve other
437  // reactants, e.g. catalysts. We thus need to step through the
438  // rate function and pick out every reactant which can vary.
439  DependsOn.push_back(getDependsOn(i));
440  // Get the set of metabolites which are affected when this reaction takes place
441  Affects.push_back(getAffects(i));
442  }
443 
444  // For each possible pair of reactions i and j, if the intersection of
445  // Affects(i) with DependsOn(j) is non-empty, add a dependency edge from i to j.
446  mDG.resize(mNumReactions);
447 
448  for (i = 0; i < mNumReactions; i++)
449  {
450  for (j = 0; j < mNumReactions; j++)
451  {
452  // Determine whether the intersection of these two sets is non-empty
453  // Could also do this with set_intersection generic algorithm, but that
454  // would require operator<() to be defined on the set elements.
455 
456  std::set<std::string>::iterator iter = Affects[i]->begin();
457 
458  for (; iter != Affects[i]->end(); iter++)
459  {
460  if (DependsOn[j]->count(*iter))
461  {
462  // The set intersection is non-empty
463  mDG.addDependent(i, j);
464  break;
465  }
466  }
467  }
468 
469  // Ensure that self edges are included
470  //mDG.addDependent(i, i);
471  }
472 
473  // Create local copy of balances and substrates list
474  CStochBalance bb;
475  C_INT32 maxBalance = 0;
476 
477  mLocalBalances.clear();
478  mLocalBalances.resize(mNumReactions);
479  mLocalSubstrates.clear();
480  mLocalSubstrates.resize(mNumReactions);
481 
482  for (i = 0; i < mNumReactions; i++)
483  {
484  const CCopasiVector<CChemEqElement> * bbb;
485 
486  bbb = &mpModel->getReactions()[i]->getChemEq().getBalances();
487 
488  //TODO clear old local balances and substrates
489  for (j = 0; j < bbb->size(); j++)
490  {
491  //bb.mIndex = mpModel->getMetabolites().getIndex((*bbb)[j]->getMetabolite().getObjectName(());
492  //bb.mIndex = mpModel->findMetabByKey((*bbb)[j]->getMetaboliteKey());
493  assert((*bbb)[j]->getMetabolite());
494  bb.mIndex = mpModel->getMetabolitesX().getIndex((*bbb)[j]->getMetabolite());
495  bb.mMultiplicity = static_cast<C_INT32>(floor((*bbb)[j]->getMultiplicity() + 0.5));
496 
497  if (((*bbb)[j]->getMetabolite()->getStatus()) != CModelEntity::FIXED)
498  {
499  if (bb.mMultiplicity > maxBalance) maxBalance = bb.mMultiplicity;
500 
501  mLocalBalances[i].push_back(bb);
502  }
503  }
504 
505  bbb = &mpModel->getReactions()[i]->getChemEq().getSubstrates();
506 
507  for (j = 0; j < bbb->size(); j++)
508  {
509  //bb.mIndex = mpModel->getMetabolites().getIndex((*bbb)[j]->getMetabolite().getObjectName(());
510  //bb.mIndex = mpModel->findMetabByKey((*bbb)[j]->getMetaboliteKey());
511  assert((*bbb)[j]->getMetabolite());
512  bb.mIndex = mpModel->getMetabolitesX().getIndex((*bbb)[j]->getMetabolite());
513  bb.mMultiplicity = static_cast<C_INT32>(floor((*bbb)[j]->getMultiplicity() + 0.5));
514 
515  if (1)
516  {
517  mLocalSubstrates[i].push_back(bb);
518  }
519  }
520  }
521 
522  mMaxBalance = maxBalance;
524 
525  // Delete the memory allocated in getDependsOn() and getAffects()
526  // since this is allocated in other functions.
527  for (i = 0; i < mNumReactions; i++)
528  {
529  delete DependsOn[i];
530  delete Affects[i];
531  }
532 }
std::set< std::string > * getAffects(size_t reaction_index)
std::set< std::string > * getDependsOn(size_t reaction_index)
std::vector< std::vector< CStochBalance > > mLocalBalances
Definition: CStochMethod.h:257
virtual size_t size() const
void addDependent(const size_t &node, const size_t &dependent)
CModel * mpModel
Definition: CStochMethod.h:246
std::vector< std::vector< CStochBalance > > mLocalSubstrates
Definition: CStochMethod.h:262
CDependencyGraph mDG
Definition: CStochMethod.h:252
#define C_INT32
Definition: copasi.h:90
void resize(const size_t &n)
size_t mNumReactions
Definition: CStochMethod.h:269
C_INT32 mMultiplicity
Definition: CStochMethod.h:42
size_t mMaxSteps
Definition: CStochMethod.h:164
virtual size_t getIndex(const CCopasiObject *pObject) const
C_INT64 mMaxIntBeforeStep
Definition: CStochMethod.h:174
CCopasiVectorNS< CReaction > & getReactions()
Definition: CModel.cpp:1039
const CCopasiVector< CMetab > & getMetabolitesX() const
Definition: CModel.cpp:1057
C_INT32 mMaxBalance
Definition: CStochMethod.h:169
#define max(a, b)
Definition: f2c.h:176
void CStochMethod::start ( const CState initialState)
virtual

This instructs the method to prepare for integration starting with the initialState given.

Parameters
const CState *initialState

Reimplemented from CTrajectoryMethod.

Definition at line 160 of file CStochMethod.cpp.

References C_FLOAT64, C_INT32, C_INT64, CModel::deterministic, CStateTemplate::getIndex(), CModel::getMetabolitesX(), CCopasiProblem::getModel(), CModel::getModelType(), CModel::getReactions(), CModel::getStateTemplate(), CState::getTime(), CCopasiParameter::getValue(), CRandom::initialize(), initMethod(), mAmu, mAmuOld, mDoCorrection, mFirstMetabIndex, mHasAssignments, mMaxSteps, mMaxStepsReached, mNumbers, mNumReactions, modelHasAssignments(), CTrajectoryMethod::mpCurrentState, mpModel, CTrajectoryMethod::mpProblem, mpRandomGenerator, CCopasiParameter::Value::pBOOL, CCopasiParameter::Value::pINT, CCopasiParameter::Value::pUINT, setupDependencyGraphAndBalances(), CCopasiVector< T >::size(), updatePropensities(), and CModel::updateSimulatedValues().

161 {
162  /* get configuration data */
163  mMaxSteps = * getValue("Max Internal Steps").pINT;
164 
165  bool useRandomSeed = * getValue("Use Random Seed").pBOOL;
166  unsigned C_INT32 randomSeed = * getValue("Random Seed").pUINT;
167 
168  if (useRandomSeed) mpRandomGenerator->initialize(randomSeed);
169 
170  //mpCurrentState is initialized. This state is not used internally in the
171  //stochastic solver, but it is used for returning the result after each step.
172  *mpCurrentState = *initialState;
173 
175  assert(mpModel);
176 
178  mDoCorrection = true;
179  else
180  mDoCorrection = false;
181 
183 
184  size_t i;
185 
186  //initialize the vector of ints that contains the particle numbers
187  //for the discrete simulation. This also floors all particle numbers in the model.
188  mNumbers.resize(mpModel->getMetabolitesX().size());
189 
190  for (i = 0; i < mNumbers.size(); ++i)
191  {
192  mNumbers[i] = (C_INT64) mpModel->getMetabolitesX()[i]->getValue();
193  mpModel->getMetabolitesX()[i]->setValue((C_FLOAT64) mNumbers[i]);
194  mpModel->getMetabolitesX()[i]->refreshConcentration();
195  }
196 
198 
199  mpModel->updateSimulatedValues(false); //for assignments
200  //mpModel->updateNonSimulatedValues(); //for assignments
201 
203 
204  mAmu.clear(); mAmuOld.clear();
205 
206  for (i = 0; i < mNumReactions; i++)
207  {
208  mAmu.push_back(0);
209  mAmuOld.push_back(0);
210  }
211 
214 
215  // call init of the specific simulation method
217 
218  mMaxStepsReached = false;
219  return;
220 }
bool mMaxStepsReached
Definition: CStochMethod.h:176
std::vector< C_FLOAT64 > mAmu
Definition: CStochMethod.h:57
virtual void initMethod(C_FLOAT64 start_time)=0
C_INT32 updatePropensities()
virtual size_t size() const
std::vector< C_FLOAT64 > mAmuOld
Definition: CStochMethod.h:67
bool mDoCorrection
Definition: CStochMethod.h:72
void updateSimulatedValues(const bool &updateMoieties)
Definition: CModel.cpp:1851
CModel * mpModel
Definition: CStochMethod.h:246
CTrajectoryProblem * mpProblem
size_t getIndex(const CModelEntity *entity) const
Definition: CState.cpp:231
#define C_INT64
Definition: copasi.h:88
#define C_INT32
Definition: copasi.h:90
size_t mNumReactions
Definition: CStochMethod.h:269
bool mHasAssignments
Definition: CStochMethod.h:79
const Value & getValue() const
unsigned C_INT32 * pUINT
size_t mMaxSteps
Definition: CStochMethod.h:164
const C_FLOAT64 & getTime() const
Definition: CState.cpp:325
#define C_FLOAT64
Definition: copasi.h:92
const CStateTemplate & getStateTemplate() const
Definition: CModel.cpp:1172
const ModelType & getModelType() const
Definition: CModel.cpp:2339
CCopasiVectorNS< CReaction > & getReactions()
Definition: CModel.cpp:1039
void setupDependencyGraphAndBalances()
CRandom * mpRandomGenerator
Definition: CStochMethod.h:241
const CCopasiVector< CMetab > & getMetabolitesX() const
Definition: CModel.cpp:1057
std::vector< C_INT64 > mNumbers
Definition: CStochMethod.h:267
CModel * getModel() const
virtual void initialize(unsigned C_INT32 seed=CRandom::getSystemSeed())
Definition: CRandom.cpp:162
size_t mFirstMetabIndex
Definition: CStochMethod.h:274
static bool modelHasAssignments(const CModel *pModel)
CTrajectoryMethod::Status CStochMethod::step ( const double &  deltaT)
virtual

This instructs the method to calculate a time step of deltaT starting with the current state, i.e., the result of the previous step. The new state (after deltaT) is expected in the current state. The return value is the actual timestep taken.

Parameters
const double &deltaT
Returns
Status status

Reimplemented from CTrajectoryMethod.

Definition at line 110 of file CStochMethod.cpp.

References CState::beginIndependent(), C_FLOAT64, doSingleStep(), CCopasiMessage::EXCEPTION, CModel::getMetabolitesX(), CCopasiProblem::getModel(), CModel::getNumVariableMetabs(), CModel::getState(), CState::getTime(), MCTrajectoryMethod, mFirstMetabIndex, mMaxIntBeforeStep, mMaxSteps, CTrajectoryMethod::mpCurrentState, CTrajectoryMethod::mpProblem, CTrajectoryMethod::NORMAL, and CState::setTime().

111 {
112  // write the current state to the model:
113  //mpProblem->getModel()->setState(mpCurrentState); //?
114 
115  // check for possible overflows:
116  size_t i;
117  size_t imax;
118 
119  // :TODO: Bug 774: This assumes that the number of variable metabs is the number
120  // of metabs determined by reaction. In addition they are expected at the beginning of the
121  // MetabolitesX which is not the case if we have metabolites of type ODE.
122  for (i = 0, imax = mpProblem->getModel()->getNumVariableMetabs(); i < imax; i++)
123  if (mpProblem->getModel()->getMetabolitesX()[i]->getValue() >= mMaxIntBeforeStep)
124  {
125  CCopasiMessage(CCopasiMessage::EXCEPTION, "at least one particle number got to big.");
126  // TODO:throw exception or something like that
127  }
128 
129  // do several steps:
130  C_FLOAT64 time = mpCurrentState->getTime();
131  C_FLOAT64 endtime = time + deltaT;
132 
133  size_t Steps = 0;
134 
135  while (time < endtime)
136  {
137  time = doSingleStep(time, endtime);
138  mpCurrentState->setTime(time);
139 
140  if (++Steps > mMaxSteps)
141  {
143  }
144  }
145 
147  mpCurrentState->setTime(time);
148 
149  // get back the particle numbers:
150 
151  /* Set the variable Metabolites */
153 
154  for (i = 0, imax = mpProblem->getModel()->getNumVariableMetabs(); i < imax; i++, Dbl++)
155  *Dbl = mpProblem->getModel()->getMetabolitesX()[i]->getValue();
156 
157  return NORMAL;
158 }
virtual C_FLOAT64 doSingleStep(C_FLOAT64 time, C_FLOAT64 endtime)=0
CTrajectoryProblem * mpProblem
void setTime(const C_FLOAT64 &time)
Definition: CState.cpp:326
#define MCTrajectoryMethod
size_t getNumVariableMetabs() const
Definition: CModel.cpp:1121
size_t mMaxSteps
Definition: CStochMethod.h:164
const C_FLOAT64 & getTime() const
Definition: CState.cpp:325
#define C_FLOAT64
Definition: copasi.h:92
C_INT64 mMaxIntBeforeStep
Definition: CStochMethod.h:174
const CState & getState() const
Definition: CModel.cpp:1771
const CCopasiVector< CMetab > & getMetabolitesX() const
Definition: CModel.cpp:1057
CModel * getModel() const
C_FLOAT64 * beginIndependent()
Definition: CState.cpp:328
size_t mFirstMetabIndex
Definition: CStochMethod.h:274
C_INT32 CStochMethod::updatePropensities ( )
protected

Calculate the propensities for all reactions

Returns
mFail
See Also
mFail

Definition at line 222 of file CStochMethod.cpp.

References calculateAmu(), mA0, mAmu, mAmuOld, and mNumReactions.

Referenced by start(), and updateSystemState().

223 {
224  //mA0Old = mA0;
225  mA0 = 0;
226 
227  for (size_t i = 0; i < mNumReactions; i++)
228  {
229  mAmuOld[i] = mAmu[i];
230  calculateAmu(i);
231  mA0 += mAmu[i];
232  }
233 
234  return 0;
235 }
std::vector< C_FLOAT64 > mAmu
Definition: CStochMethod.h:57
std::vector< C_FLOAT64 > mAmuOld
Definition: CStochMethod.h:67
size_t mNumReactions
Definition: CStochMethod.h:269
C_INT32 calculateAmu(size_t reaction_index)
C_FLOAT64 mA0
Definition: CStochMethod.h:62
C_INT32 CStochMethod::updateSystemState ( size_t  reaction_index,
const C_FLOAT64 time 
)
protected

Update the particle numbers according to which reaction ocurred

Returns
mFail
See Also
mFail

Definition at line 317 of file CStochMethod.cpp.

References CModelEntity::ASSIGNMENT, C_FLOAT64, C_INT64, calculateAmu(), CDependencyGraph::getDependents(), CModel::getMetabolitesX(), mA0, mAmu, mAmuOld, mDG, mHasAssignments, mLocalBalances, mNumbers, mpModel, CMetab::refreshConcentration(), CModel::setTime(), CModelEntity::setValue(), updatePropensities(), and CModel::updateSimulatedValues().

Referenced by CStochNextReactionMethod::doSingleStep().

318 {
319  // Change the particle numbers according to which step took place.
320  // First, get the vector of balances in the reaction we've got.
321  // (This vector expresses the number change of each metabolite
322  // in the reaction.) Then step through each balance, using its
323  // multiplicity to calculate a new value for the associated
324  // metabolite. Finally, update the metabolite.
325 
326  const std::vector<CStochBalance> & bals = mLocalBalances[rxn];
327 
328  std::vector<CStochBalance>::const_iterator bi;
329  CMetab* pTmpMetab;
330 
331  for (bi = bals.begin(); bi != bals.end(); bi++)
332  {
333  mNumbers[bi->mIndex] = mNumbers[bi->mIndex] + bi->mMultiplicity;
334  pTmpMetab = mpModel->getMetabolitesX()[bi->mIndex];
335  pTmpMetab->setValue((C_FLOAT64) mNumbers[bi->mIndex]);
336  pTmpMetab->refreshConcentration();
337  }
338 
339  if (mHasAssignments)
340  {
341  // this is less efficient but can deal with assignments.
342  //TODO: handle dependencies for assignments also.
343  mpModel->setTime(time);
345 
346  //now potentially species with assignments have non integer
347  //particle numbers. This needs to be rounded. Also the updated
348  //particle numbers need to be copied to the vector of integers.
349  //(the integer values may be used to calculate the propensities for
350  //higher order kinetics).
351  size_t i, imax = mNumbers.size();
352 
353  for (i = 0; i < imax; ++i)
354  {
355  if (mpModel->getMetabolitesX()[i]->getStatus() == CModelEntity::ASSIGNMENT)
356  {
357  mNumbers[i] = (C_INT64) mpModel->getMetabolitesX()[i]->getValue();
358  mpModel->getMetabolitesX()[i]->setValue((C_FLOAT64) mNumbers[i]);
359  mpModel->getMetabolitesX()[i]->refreshConcentration();
360  }
361  }
362 
363  //now the propensities can be updated
365  }
366  else
367  {
368  const std::set<size_t> & dep_nodes = mDG.getDependents(rxn);
369 
370  std::set<size_t>::const_iterator it;
371  size_t ii;
372 
373  for (it = dep_nodes.begin(); it != dep_nodes.end(); it++)
374  {
375  ii = *it;
376  mAmuOld[ii] = mAmu[ii];
377  calculateAmu(ii);
378  }
379 
380  //mA0Old = mA0;
381 
382  mA0 = 0;
383  mA0 = std::accumulate(mAmu.begin(), mAmu.end(), mA0);
384  }
385 
386  return 0;
387 }
std::vector< C_FLOAT64 > mAmu
Definition: CStochMethod.h:57
C_INT32 updatePropensities()
std::vector< std::vector< CStochBalance > > mLocalBalances
Definition: CStochMethod.h:257
std::vector< C_FLOAT64 > mAmuOld
Definition: CStochMethod.h:67
void updateSimulatedValues(const bool &updateMoieties)
Definition: CModel.cpp:1851
CModel * mpModel
Definition: CStochMethod.h:246
const std::set< size_t > & getDependents(const size_t &node) const
virtual void setValue(const C_FLOAT64 &value)
#define C_INT64
Definition: copasi.h:88
CDependencyGraph mDG
Definition: CStochMethod.h:252
Definition: CMetab.h:178
bool mHasAssignments
Definition: CStochMethod.h:79
void setTime(const C_FLOAT64 &time)
Definition: CModel.cpp:1187
void refreshConcentration()
Definition: CMetab.cpp:281
#define C_FLOAT64
Definition: copasi.h:92
C_INT32 calculateAmu(size_t reaction_index)
const CCopasiVector< CMetab > & getMetabolitesX() const
Definition: CModel.cpp:1057
C_FLOAT64 mA0
Definition: CStochMethod.h:62
std::vector< C_INT64 > mNumbers
Definition: CStochMethod.h:267

Friends And Related Function Documentation

Member Data Documentation

C_FLOAT64 CStochMethod::mA0
protected

The sum of the propensities

Definition at line 62 of file CStochMethod.h.

Referenced by generateReactionIndex(), generateReactionTime(), updatePropensities(), and updateSystemState().

std::vector<C_FLOAT64> CStochMethod::mAmu
protected
std::vector<C_FLOAT64> CStochMethod::mAmuOld
protected

The stored propensities for the reactions before the last update

Definition at line 67 of file CStochMethod.h.

Referenced by start(), CStochNextReactionMethod::updatePriorityQueue(), updatePropensities(), and updateSystemState().

CDependencyGraph CStochMethod::mDG
protected

The graph of reactions and their dependent reactions. When a reaction is executed, the propensities for each of its dependents must be updated.

Definition at line 252 of file CStochMethod.h.

Referenced by setupDependencyGraphAndBalances(), CStochNextReactionMethod::updatePriorityQueue(), and updateSystemState().

bool CStochMethod::mDoCorrection
protected

indicates if the correction N^2 -> N*(N-1) should be performed

Definition at line 72 of file CStochMethod.h.

Referenced by calculateAmu(), and start().

size_t CStochMethod::mFirstMetabIndex
protected

index of first metab in a CState

Definition at line 274 of file CStochMethod.h.

Referenced by start(), and step().

bool CStochMethod::mHasAssignments
protected

Indicates whether the model has global quantities with assignment rules. If it has, we will use a less efficient way to update the model state to handle this.

Definition at line 79 of file CStochMethod.h.

Referenced by start(), CStochNextReactionMethod::updatePriorityQueue(), and updateSystemState().

std::vector< std::vector <CStochBalance> > CStochMethod::mLocalBalances
protected

The balances of the reactions as integers

Definition at line 257 of file CStochMethod.h.

Referenced by setupDependencyGraphAndBalances(), and updateSystemState().

std::vector< std::vector <CStochBalance> > CStochMethod::mLocalSubstrates
protected

the substrates of each reaction with their multiplicities

Definition at line 262 of file CStochMethod.h.

Referenced by calculateAmu(), and setupDependencyGraphAndBalances().

C_INT32 CStochMethod::mMaxBalance
private

maximal increase of a particle number in one step.

Definition at line 169 of file CStochMethod.h.

Referenced by setupDependencyGraphAndBalances().

C_INT64 CStochMethod::mMaxIntBeforeStep
private

This is set to maxint - mMaxSteps*mMaxBalance

Definition at line 174 of file CStochMethod.h.

Referenced by setupDependencyGraphAndBalances(), and step().

size_t CStochMethod::mMaxSteps
private

max number of single stochastic steps to do in one step()

Definition at line 164 of file CStochMethod.h.

Referenced by setupDependencyGraphAndBalances(), start(), and step().

bool CStochMethod::mMaxStepsReached
private

Definition at line 176 of file CStochMethod.h.

Referenced by start().

std::vector<C_INT64> CStochMethod::mNumbers
protected

The particle numbers

Definition at line 267 of file CStochMethod.h.

Referenced by calculateAmu(), start(), and updateSystemState().

size_t CStochMethod::mNumReactions
protected
CModel* CStochMethod::mpModel
protected
CRandom* CStochMethod::mpRandomGenerator
protected

The random number generator

Definition at line 241 of file CStochMethod.h.

Referenced by CStochMethod(), generateReactionIndex(), generateReactionTime(), start(), and ~CStochMethod().


The documentation for this class was generated from the following files: