MOOSE - Multiscale Object Oriented Simulation Environment
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
SteadyState Class Reference

#include <SteadyStateBoost.h>

+ Collaboration diagram for SteadyState:

Public Member Functions

bool badStoichiometry () const
 
bool badStoichiometry () const
 
void classifyState (const double *T)
 
void classifyState (const double *T)
 
void fitConservationRules (boost::numeric::ublas::matrix< value_type_ > &U, const vector< value_type_ > &eliminatedTotal, vector< value_type_ > &yi)
 
double getConvergenceCriterion () const
 
double getConvergenceCriterion () const
 
double getEigenvalue (const unsigned int i) const
 
double getEigenvalue (const unsigned int i) const
 
unsigned int getMaxIter () const
 
unsigned int getMaxIter () const
 
unsigned int getNiter () const
 
unsigned int getNiter () const
 
unsigned int getNnegEigenvalues () const
 
unsigned int getNnegEigenvalues () const
 
unsigned int getNposEigenvalues () const
 
unsigned int getNposEigenvalues () const
 
unsigned int getNumVarPools () const
 
unsigned int getNumVarPools () const
 
unsigned int getRank () const
 
unsigned int getRank () const
 
unsigned int getSolutionStatus () const
 
unsigned int getSolutionStatus () const
 
unsigned int getStateType () const
 
unsigned int getStateType () const
 
string getStatus () const
 
string getStatus () const
 
Id getStoich () const
 
Id getStoich () const
 
double getTotal (const unsigned int i) const
 
double getTotal (const unsigned int i) const
 
bool isInitialized () const
 
bool isInitialized () const
 
void randomizeInitialCondition (const Eref &e)
 
void randomizeInitialCondition (const Eref &e)
 
void resettleFunc ()
 
void resettleFunc ()
 
void setConvergenceCriterion (double value)
 
void setConvergenceCriterion (double value)
 
void setEigenvalue (double val, const unsigned int i)
 
void setEigenvalue (double val, const unsigned int i)
 
void setMaxIter (unsigned int value)
 
void setMaxIter (unsigned int value)
 
void setStoich (Id s)
 
void setStoich (Id s)
 
void settle (bool forceSetup)
 
void settle (bool forceSetup)
 
void settleFunc ()
 
void settleFunc ()
 
void setTotal (const unsigned int i, double val)
 
void setTotal (const unsigned int i, double val)
 
void setupMatrix ()
 
void setupMatrix ()
 
void showMatrices ()
 
void showMatrices ()
 
void showMatricesFunc ()
 
void showMatricesFunc ()
 
 SteadyState ()
 
 SteadyState ()
 
 ~SteadyState ()
 
 ~SteadyState ()
 

Static Public Member Functions

static void assignY (double *S)
 
static void assignY (double *S)
 
static const CinfoinitCinfo ()
 
static const CinfoinitCinfo ()
 
static void setMolN (double y, unsigned int i)
 
static void setMolN (double y, unsigned int i)
 

Static Public Attributes

static const double DELTA = 1e-6
 
static const double EPSILON = 1e-9
 

Private Member Functions

void setupSSmatrix ()
 
void setupSSmatrix ()
 

Private Attributes

bool badStoichiometry_
 
double convergenceCriterion_
 
vector< double > eigenvalues_
 
boost::numeric::ublas::matrix
< value_type_ > 
gamma_
 
bool isInitialized_
 
bool isSetup_
 
boost::numeric::ublas::matrix
< value_type_ > 
LU_
 
unsigned int maxIter_
 
unsigned int nIter_
 
unsigned int nNegEigenvalues_
 
unsigned int nPosEigenvalues_
 
boost::numeric::ublas::matrix
< value_type_ > 
Nr_
 
unsigned int nReacs_
 
unsigned int numFailed_
 
unsigned int numVarPools_
 
size_t numVarPools_
 
VoxelPools pool_
 
unsigned int rank_
 
bool reassignTotal_
 
unsigned int solutionStatus_
 
unsigned int stateType_
 
string status_
 
Id stoich_
 
vector< double > total_
 

Detailed Description

Definition at line 20 of file SteadyStateBoost.h.

Constructor & Destructor Documentation

SteadyState::SteadyState ( )

Definition at line 322 of file SteadyStateBoost.cpp.

323  :
324  nIter_( 0 ),
325  maxIter_( 100 ),
326  badStoichiometry_( 0 ),
327  status_( "OK" ),
328  isInitialized_( 0 ),
329  isSetup_( 0 ),
330  convergenceCriterion_( 1e-7 ),
331  stoich_(),
332  numVarPools_( 0 ),
333  nReacs_( 0 ),
334  rank_( 0 ),
335  reassignTotal_( 0 ),
336  nNegEigenvalues_( 0 ),
337  nPosEigenvalues_( 0 ),
338  stateType_( 0 ),
339  solutionStatus_( 0 ),
340  numFailed_( 0 )
341 {
342 }
unsigned int numFailed_
double convergenceCriterion_
unsigned int rank_
unsigned int nPosEigenvalues_
unsigned int stateType_
unsigned int nIter_
unsigned int nNegEigenvalues_
unsigned int maxIter_
unsigned int nReacs_
unsigned int solutionStatus_
SteadyState::~SteadyState ( )

Definition at line 344 of file SteadyStateBoost.cpp.

345 {
346 
347 }
SteadyState::SteadyState ( )
SteadyState::~SteadyState ( )

Member Function Documentation

static void SteadyState::assignY ( double *  S)
static
void SteadyState::assignY ( double *  S)
static

Definition at line 505 of file SteadyStateBoost.cpp.

506 {
507 }
bool SteadyState::badStoichiometry ( ) const
bool SteadyState::badStoichiometry ( ) const

Definition at line 380 of file SteadyStateBoost.cpp.

References badStoichiometry_.

Referenced by initCinfo().

381 {
382  return badStoichiometry_;
383 }

+ Here is the caller graph for this function:

void SteadyState::classifyState ( const double *  T)
void SteadyState::classifyState ( const double *  T)

This does the iteration, using the specified method. First try gsl_multiroot_fsolver_hybrids If that doesn't work try gsl_multiroot_fsolver_dnewton Returns the gsl status.

Definition at line 609 of file SteadyStateBoost.cpp.

References Eref::data(), DELTA, eigenvalues_, EPSILON, Id::eref(), Stoich::getKsolve(), nNegEigenvalues_, nPosEigenvalues_, numVarPools_, pool_, rank_, solutionStatus_, stateType_, stoich_, and VoxelPools::updateRates().

610 {
611  /* column_major trait is needed for fortran */
612  ublas::matrix<double, ublas::column_major> J(numVarPools_, numVarPools_);
613 
614  double tot = 0.0;
615  Stoich* s = reinterpret_cast< Stoich* >( stoich_.eref().data() );
616  vector< double > nVec = LookupField< unsigned int, vector< double > >::get(
617  s->getKsolve(), "nVec", 0 );
618  for ( unsigned int i = 0; i < numVarPools_; ++i )
619  tot += nVec[i];
620 
621  tot *= DELTA;
622 
623  vector< double > yprime( nVec.size(), 0.0 );
624  // Fill up Jacobian
625  for ( unsigned int i = 0; i < numVarPools_; ++i )
626  {
627  double orig = nVec[i];
628  if ( std::isnan( orig ) or std::isinf( orig ) )
629  {
630  cout << "Warning: SteadyState::classifyState: orig=nan\n";
631  solutionStatus_ = 2; // Steady state OK, eig failed
632  J.clear();
633  return;
634  }
635  if ( std::isnan( tot ) or std::isinf( tot ))
636  {
637  cout << "Warning: SteadyState::classifyState: tot=nan\n";
638  solutionStatus_ = 2; // Steady state OK, eig failed
639  J.clear();
640  return;
641  }
642  nVec[i] = orig + tot;
643 
644  pool_.updateRates( &nVec[0], &yprime[0] );
645  nVec[i] = orig;
646 
647  // Assign the rates for each mol.
648  for ( unsigned int j = 0; j < numVarPools_; ++j )
649  {
650  if( std::isnan( yprime[j] ) or std::isinf( yprime[j] ) )
651  {
652  cout << "Warning: Overflow/underflow. Can't continue " << endl;
653  solutionStatus_ = 2;
654  return;
655  }
656  J(i, j) = yprime[j];
657  }
658  }
659 
660  // Jacobian is now ready. Find eigenvalues.
661  ublas::vector< std::complex< double > > eigenVec ( J.size1() );
662 
663  ublas::matrix< std::complex<double>, ublas::column_major >* vl, *vr;
664  vl = NULL; vr = NULL;
665 
666  /*-----------------------------------------------------------------------------
667  * INFO: Calling lapack routine geev to compute eigen vector of matrix J.
668  *
669  * Argument 3 and 4 are left- and right-eigenvectors. Since we do not need
670  * them, they are set to NULL. Argument 2 holds eigen-vector and result is
671  * copied to it (output ).
672  *-----------------------------------------------------------------------------*/
673  int status = lapack::geev( J, eigenVec, vl, vr, lapack::optimal_workspace() );
674 
675  eigenvalues_.clear();
676  eigenvalues_.resize( numVarPools_, 0.0 );
677  if ( status != 0 )
678  {
679  cout << "Warning: SteadyState::classifyState failed to find eigenvalues. Status = " <<
680  status << endl;
681  solutionStatus_ = 2; // Steady state OK, eig classification failed
682  }
683  else // Eigenvalues are ready. Classify state.
684  {
685  nNegEigenvalues_ = 0;
686  nPosEigenvalues_ = 0;
687  for ( unsigned int i = 0; i < numVarPools_; ++i )
688  {
689  std::complex<value_type> z = eigenVec[ i ];
690  double r = z.real();
691  nNegEigenvalues_ += ( r < -EPSILON );
692  nPosEigenvalues_ += ( r > EPSILON );
693  eigenvalues_[i] = r;
694  // We have a problem here because numVarPools_ usually > rank
695  // This means we have several zero eigenvalues.
696  }
697  if ( nNegEigenvalues_ == rank_ )
698  stateType_ = 0; // Stable
699  else if ( nPosEigenvalues_ == rank_ ) // Never see it.
700  stateType_ = 1; // Unstable
701  else if (nPosEigenvalues_ == 1)
702  stateType_ = 2; // Saddle
703  else if ( nPosEigenvalues_ >= 2 )
704  stateType_ = 3; // putative oscillatory
705  else if ( nNegEigenvalues_ == ( rank_ - 1) && nPosEigenvalues_ == 0 )
706  stateType_ = 4; // one zero or unclassified eigenvalue. Messy.
707  else
708  stateType_ = 5; // Other
709  }
710 }
char * data() const
Definition: Eref.cpp:41
Id getKsolve() const
Definition: Stoich.cpp:446
vector< double > eigenvalues_
Eref eref() const
Definition: Id.cpp:125
unsigned int rank_
Definition: Stoich.h:49
unsigned int nPosEigenvalues_
static const double DELTA
unsigned int stateType_
VoxelPools pool_
unsigned int nNegEigenvalues_
static const double EPSILON
void updateRates(const double *s, double *yprime) const
Definition: VoxelPools.cpp:318
unsigned int solutionStatus_

+ Here is the call graph for this function:

void SteadyState::fitConservationRules ( boost::numeric::ublas::matrix< value_type_ > &  U,
const vector< value_type_ > &  eliminatedTotal,
vector< value_type_ > &  yi 
)

This does the actual work of generating random numbers and making sure they fit.

Definition at line 1030 of file SteadyStateBoost.cpp.

References EPSILON, moose::mtrand(), numVarPools_, and total_.

Referenced by randomizeInitialCondition().

1034 {
1035  int numConsv = total_.size();
1036  int lastJ = numVarPools_;
1037  for ( int i = numConsv - 1; i >= 0; --i )
1038  {
1039  for ( unsigned int j = 0; j < numVarPools_; ++j )
1040  {
1041  double g = U( i, j );
1042  if ( fabs( g ) > EPSILON )
1043  {
1044  // double ytot = calcTot( g, i, j, lastJ );
1045  double ytot = 0.0;
1046  for ( int k = j; k < lastJ; ++k )
1047  {
1048  y[k] = moose::mtrand();
1049  ytot += y[k] * U( i, k );
1050  }
1051  assert( fabs( ytot ) > EPSILON );
1052  double lastYtot = 0.0;
1053  for ( unsigned int k = lastJ; k < numVarPools_; ++k )
1054  {
1055  lastYtot += y[k] * U( i, k );
1056  }
1057  double scale = ( eliminatedTotal[i] - lastYtot ) / ytot;
1058  for ( int k = j; k < lastJ; ++k )
1059  {
1060  y[k] *= scale;
1061  }
1062  lastJ = j;
1063  break;
1064  }
1065  }
1066  }
1067 }
vector< double > total_
static const double EPSILON
double mtrand(void)
Generate a random double between 0 and 1.
Definition: global.cpp:97

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

double SteadyState::getConvergenceCriterion ( ) const
double SteadyState::getConvergenceCriterion ( ) const

Definition at line 450 of file SteadyStateBoost.cpp.

References convergenceCriterion_.

Referenced by initCinfo().

451 {
452  return convergenceCriterion_;
453 }
double convergenceCriterion_

+ Here is the caller graph for this function:

double SteadyState::getEigenvalue ( const unsigned int  i) const
double SteadyState::getEigenvalue ( const unsigned int  i) const

Definition at line 476 of file SteadyStateBoost.cpp.

References eigenvalues_.

Referenced by initCinfo().

477 {
478  if ( i < eigenvalues_.size() )
479  return eigenvalues_[i];
480  cout << "Warning: SteadyState::getEigenvalue: index " << i <<
481  " out of range " << eigenvalues_.size() << endl;
482  return 0.0;
483 }
vector< double > eigenvalues_

+ Here is the caller graph for this function:

unsigned int SteadyState::getMaxIter ( ) const
unsigned int SteadyState::getMaxIter ( ) const

Definition at line 400 of file SteadyStateBoost.cpp.

References maxIter_.

Referenced by initCinfo().

401 {
402  return maxIter_;
403 }
unsigned int maxIter_

+ Here is the caller graph for this function:

unsigned int SteadyState::getNiter ( ) const
unsigned int SteadyState::getNiter ( ) const

Definition at line 390 of file SteadyStateBoost.cpp.

References nIter_.

Referenced by initCinfo().

391 {
392  return nIter_;
393 }
unsigned int nIter_

+ Here is the caller graph for this function:

unsigned int SteadyState::getNnegEigenvalues ( ) const
unsigned int SteadyState::getNnegEigenvalues ( ) const

Definition at line 425 of file SteadyStateBoost.cpp.

References nNegEigenvalues_.

Referenced by initCinfo().

426 {
427  return nNegEigenvalues_;
428 }
unsigned int nNegEigenvalues_

+ Here is the caller graph for this function:

unsigned int SteadyState::getNposEigenvalues ( ) const
unsigned int SteadyState::getNposEigenvalues ( ) const

Definition at line 430 of file SteadyStateBoost.cpp.

References nPosEigenvalues_.

Referenced by initCinfo().

431 {
432  return nPosEigenvalues_;
433 }
unsigned int nPosEigenvalues_

+ Here is the caller graph for this function:

unsigned int SteadyState::getNumVarPools ( ) const
unsigned int SteadyState::getNumVarPools ( ) const

Definition at line 415 of file SteadyStateBoost.cpp.

References numVarPools_.

Referenced by initCinfo().

416 {
417  return numVarPools_;
418 }

+ Here is the caller graph for this function:

unsigned int SteadyState::getRank ( ) const
unsigned int SteadyState::getRank ( ) const

Definition at line 410 of file SteadyStateBoost.cpp.

References rank_.

Referenced by initCinfo().

411 {
412  return rank_;
413 }
unsigned int rank_

+ Here is the caller graph for this function:

unsigned int SteadyState::getSolutionStatus ( ) const
unsigned int SteadyState::getSolutionStatus ( ) const

Definition at line 435 of file SteadyStateBoost.cpp.

References solutionStatus_.

Referenced by initCinfo().

436 {
437  return solutionStatus_;
438 }
unsigned int solutionStatus_

+ Here is the caller graph for this function:

unsigned int SteadyState::getStateType ( ) const
unsigned int SteadyState::getStateType ( ) const

Definition at line 420 of file SteadyStateBoost.cpp.

References stateType_.

Referenced by initCinfo().

421 {
422  return stateType_;
423 }
unsigned int stateType_

+ Here is the caller graph for this function:

string SteadyState::getStatus ( ) const
string SteadyState::getStatus ( ) const

Definition at line 395 of file SteadyStateBoost.cpp.

References status_.

Referenced by initCinfo().

396 {
397  return status_;
398 }

+ Here is the caller graph for this function:

Id SteadyState::getStoich ( ) const
Id SteadyState::getStoich ( ) const

Definition at line 353 of file SteadyStateBoost.cpp.

References stoich_.

Referenced by initCinfo().

354 {
355  return stoich_;
356 }

+ Here is the caller graph for this function:

double SteadyState::getTotal ( const unsigned int  i) const
double SteadyState::getTotal ( const unsigned int  i) const

Definition at line 455 of file SteadyStateBoost.cpp.

References total_.

Referenced by initCinfo().

456 {
457  if ( i < total_.size() )
458  return total_[i];
459  cout << "Warning: SteadyState::getTotal: index " << i <<
460  " out of range " << total_.size() << endl;
461  return 0.0;
462 }
vector< double > total_

+ Here is the caller graph for this function:

static const Cinfo* SteadyState::initCinfo ( )
static
const Cinfo * SteadyState::initCinfo ( )
static

This picks up the entire Stoich data structure static Finfo* gslShared[] = { new SrcFinfo( "reinitSrc", Ftype0() ), new DestFinfo( "assignStoich", Ftype1< void* >(), RFCAST( &SteadyState::assignStoichFunc ) ), new DestFinfo( "setMolN", Ftype2< double, unsigned int >(), RFCAST( &SteadyState::setMolN ) ), new SrcFinfo( "requestYsrc", Ftype0() ), new DestFinfo( "assignY", Ftype1< double* >(), RFCAST( &SteadyState::assignY ) ), };

These are the fields of the SteadyState class

Definition at line 86 of file SteadyStateBoost.cpp.

References badStoichiometry(), getConvergenceCriterion(), getEigenvalue(), getMaxIter(), getNiter(), getNnegEigenvalues(), getNposEigenvalues(), getNumVarPools(), getRank(), getSolutionStatus(), getStateType(), getStatus(), getStoich(), getTotal(), Neutral::initCinfo(), isInitialized(), randomizeInitialCondition(), resettleFunc(), setConvergenceCriterion(), setMaxIter(), setStoich(), settleFunc(), setTotal(), setupMatrix(), showMatrices(), and steadyStateCinfo.

87 {
112  // Field definitions
115  static ValueFinfo< SteadyState, Id > stoich(
116  "stoich",
117  "Specify the Id of the stoichiometry system to use",
120  );
122  "badStoichiometry",
123  "Bool: True if there is a problem with the stoichiometry",
125  );
127  "isInitialized",
128  "True if the model has been initialized successfully",
130  );
132  "nIter",
133  "Number of iterations done by steady state solver",
135  );
137  "status",
138  "Status of solver",
140  );
142  "maxIter",
143  "Max permissible number of iterations to try before giving up",
146  );
147  static ValueFinfo< SteadyState, double> convergenceCriterion(
148  "convergenceCriterion",
149  "Fractional accuracy required to accept convergence",
152  );
154  "numVarPools",
155  "Number of variable molecules in reaction system.",
157  );
159  "rank",
160  "Number of independent molecules in reaction system",
162  );
164  "stateType",
165  "0: stable; 1: unstable; 2: saddle; 3: osc?; 4: one near-zero eigenvalue; 5: other",
167  );
169  nNegEigenvalues (
170  "nNegEigenvalues",
171  "Number of negative eigenvalues: indicates type of solution",
173  );
175  nPosEigenvalues(
176  "nPosEigenvalues",
177  "Number of positive eigenvalues: indicates type of solution",
179  );
181  "solutionStatus",
182  "0: Good; 1: Failed to find steady states; "
183  "2: Failed to find eigenvalues",
185  );
187  "total",
188  "Totals table for conservation laws. The exact mapping of"
189  "this to various sums of molecules is given by the "
190  "conservation matrix, and is currently a bit opaque."
191  "The value of 'total' is set to initial conditions when"
192  "the 'SteadyState::settle' function is called."
193  "Assigning values to the total is a special operation:"
194  "it rescales the concentrations of all the affected"
195  "molecules so that they are at the specified total."
196  "This happens the next time 'settle' is called.",
199  );
201  SteadyState, unsigned int, double > eigenvalues(
202  "eigenvalues",
203  "Eigenvalues computed for steady state",
205  );
207  // MsgDest definitions
209  static DestFinfo setupMatrix( "setupMatrix",
210  "This function initializes and rebuilds the matrices used "
211  "in the calculation.",
213  );
214 
215  static DestFinfo settle( "settle",
216  "Finds the nearest steady state to the current initial "
217  "conditions. This function rebuilds the entire calculation "
218  "only if the object has not yet been initialized.",
220  );
221  static DestFinfo resettle( "resettle",
222  "Finds the nearest steady state to the current initial "
223  "conditions. This function rebuilds the entire calculation ",
225  );
226  static DestFinfo showMatrices( "showMatrices",
227  "Utility function to show the matrices derived for the calculations on the reaction system. Shows the Nr, gamma, and total matrices",
229  );
230  static DestFinfo randomInit( "randomInit",
231  "Generate random initial conditions consistent with the mass"
232  "conservation rules. Typically invoked in order to scan"
233  "states",
236  );
238  // Shared definitions
240 
241  static Finfo * steadyStateFinfos[] =
242  {
243  &stoich, // Value
244  &badStoichiometry, // ReadOnlyValue
245  &isInitialized, // ReadOnlyValue
246  &nIter, // ReadOnlyValue
247  &status, // ReadOnlyValue
248  &maxIter, // Value
249  &convergenceCriterion, // ReadOnlyValue
250  &numVarPools, // ReadOnlyValue
251  &rank, // ReadOnlyValue
252  &stateType, // ReadOnlyValue
253  &nNegEigenvalues, // ReadOnlyValue
254  &nPosEigenvalues, // ReadOnlyValue
255  &solutionStatus, // ReadOnlyValue
256  &total, // LookupValue
257  &eigenvalues, // ReadOnlyLookupValue
258  &setupMatrix, // DestFinfo
259  &settle, // DestFinfo
260  &resettle, // DestFinfo
261  &showMatrices, // DestFinfo
262  &randomInit, // DestFinfo
263 
264 
265  };
266 
267  static string doc[] =
268  {
269  "Name", "SteadyState",
270  "Author", "Upinder S. Bhalla, 2009, updated 2014, NCBS",
271  "Description", "SteadyState: works out a steady-state value for "
272  "a reaction system. "
273  "This class uses the multidimensional root finder algorithms "
274  "to find the fixed points closest to the "
275  "current molecular concentrations. "
276  "When it finds the fixed points, it figures out eigenvalues of "
277  "the solution, as a way to help classify the fixed points. "
278  "Note that the method finds unstable as well as stable fixed "
279  "points.\n "
280  "The SteadyState class also provides a utility function "
281  "*randomInit()* to "
282  "randomly initialize the concentrations, within the constraints "
283  "of stoichiometry. This is useful if you are trying to find "
284  "the major fixed points of the system. Note that this is "
285  "probabilistic. If a fixed point is in a very narrow range of "
286  "state space the probability of finding it is small and you "
287  "will have to run many iterations with different initial "
288  "conditions to find it.\n "
289  "The numerical calculations used by the SteadyState solver are "
290  "prone to failing on individual calculations. All is not lost, "
291  "because the system reports the solutionStatus. "
292  "It is recommended that you test this field after every "
293  "calculation, so you can simply ignore "
294  "cases where it failed and try again with different starting "
295  "conditions.\n "
296  "Another rule of thumb is that the SteadyState object is more "
297  "likely to succeed in finding solutions from a new starting point "
298  "if you numerically integrate the chemical system for a short "
299  "time (typically under 1 second) before asking it to find the "
300  "fixed point. "
301  };
302 
303  static Dinfo< SteadyState > dinfo;
304  static Cinfo steadyStateCinfo(
305  "SteadyState",
307  steadyStateFinfos,
308  sizeof( steadyStateFinfos )/sizeof(Finfo *),
309  &dinfo,
310  doc, sizeof( doc ) / sizeof( string )
311  );
312 
313  return &steadyStateCinfo;
314 }
void randomizeInitialCondition(const Eref &e)
unsigned int getMaxIter() const
unsigned int getNposEigenvalues() const
unsigned int getNiter() const
unsigned int getNumVarPools() const
Definition: Dinfo.h:60
double getConvergenceCriterion() const
unsigned int getRank() const
bool badStoichiometry() const
bool isInitialized() const
double getEigenvalue(const unsigned int i) const
string getStatus() const
double getTotal(const unsigned int i) const
void setMaxIter(unsigned int value)
Definition: EpFunc.h:49
static const Cinfo * steadyStateCinfo
unsigned int getSolutionStatus() const
Id getStoich() const
unsigned int getStateType() const
unsigned int getNnegEigenvalues() const
void setConvergenceCriterion(double value)
Definition: OpFunc.h:13
void setStoich(Id s)
static const Cinfo * initCinfo()
Definition: Neutral.cpp:16
Definition: Cinfo.h:18
void settle(bool forceSetup)
void setTotal(const unsigned int i, double val)
Definition: Finfo.h:12

+ Here is the call graph for this function:

bool SteadyState::isInitialized ( ) const
bool SteadyState::isInitialized ( ) const

Definition at line 385 of file SteadyStateBoost.cpp.

References isInitialized_.

Referenced by initCinfo().

386 {
387  return isInitialized_;
388 }

+ Here is the caller graph for this function:

void SteadyState::randomizeInitialCondition ( const Eref e)
void SteadyState::randomizeInitialCondition ( const Eref me)

Generates a new set of values for the S vector that is a) random and b) obeys the conservation rules.

Definition at line 968 of file SteadyStateBoost.cpp.

References checkAboveZero(), EPSILON, fitConservationRules(), gamma_, Field< A >::get(), numVarPools_, rankUsingBoost(), recalcTotal(), stoich_, and total_.

Referenced by initCinfo().

969 {
970  Id ksolve = Field< Id >::get( stoich_, "ksolve" );
971  vector< double > nVec =
973  ksolve,"nVec", 0 );
974  int numConsv = total_.size();
975  recalcTotal( total_, gamma_, &nVec[0] );
976  // The reorderRows function likes to have an I matrix at the end of
977  // numVarPools_, so we provide space for it, although only its first
978  // column is used for the total vector.
979  ublas::matrix<double> U(numConsv, numVarPools_ + numConsv, 0);
980 
981  for ( int i = 0; i < numConsv; ++i )
982  {
983  for ( unsigned int j = 0; j < numVarPools_; ++j )
984  U(i, j) = gamma_(i, j);
985 
986  U(i, numVarPools_) = total_[i];
987  }
988  // Do the forward elimination
989  int rank = rankUsingBoost( U );
990  assert( rank = numConsv );
991 
992  vector< double > eliminatedTotal( numConsv, 0.0 );
993  for ( int i = 0; i < numConsv; ++i )
994  eliminatedTotal[i] = U( i, numVarPools_ );
995 
996  // Put Find a vector Y that fits the consv rules.
997  vector< double > y( numVarPools_, 0.0 );
998  do
999  {
1000  fitConservationRules( U, eliminatedTotal, y );
1001  }
1002  while ( !checkAboveZero( y ) );
1003 
1004  // Sanity check. Try the new vector with the old gamma and tots
1005  for ( int i = 0; i < numConsv; ++i )
1006  {
1007  double tot = 0.0;
1008  for ( unsigned int j = 0; j < numVarPools_; ++j )
1009  {
1010  tot += y[j] * gamma_( i, j );
1011  }
1012  assert( fabs( tot - total_[i] ) / tot < EPSILON );
1013  }
1014 
1015  // Put the new values into S.
1016  // cout << endl;
1017  for ( unsigned int j = 0; j < numVarPools_; ++j )
1018  {
1019  nVec[j] = y[j];
1020  // cout << y[j] << " ";
1021  }
1023  ksolve,"nVec", 0, nVec );
1024 }
boost::numeric::ublas::matrix< value_type_ > gamma_
static bool checkAboveZero(const vector< double > &y)
void recalcTotal(vector< double > &tot, ublas::matrix< double > &g, const double *S)
Utility funtion to doing scans for steady states.
vector< double > total_
static const double EPSILON
void fitConservationRules(boost::numeric::ublas::matrix< value_type_ > &U, const vector< value_type_ > &eliminatedTotal, vector< value_type_ > &yi)
Definition: Id.h:17
static A get(const ObjId &dest, const string &field)
Definition: SetGet.h:284
unsigned int rankUsingBoost(ublas::matrix< double > &U)

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

void SteadyState::resettleFunc ( )
void SteadyState::resettleFunc ( )

Definition at line 499 of file SteadyStateBoost.cpp.

References settle().

Referenced by initCinfo().

500 {
501  settle( 1 );
502 }
void settle(bool forceSetup)

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

void SteadyState::setConvergenceCriterion ( double  value)
void SteadyState::setConvergenceCriterion ( double  value)

Definition at line 440 of file SteadyStateBoost.cpp.

References convergenceCriterion_, and value.

Referenced by initCinfo().

441 {
442  if ( value > 1e-10 )
444  else
445  cout << "Warning: Convergence criterion " << value <<
446  " too small. Old value " <<
447  convergenceCriterion_ << " retained\n";
448 }
uint32_t value
Definition: moosemodule.h:42
double convergenceCriterion_

+ Here is the caller graph for this function:

void SteadyState::setEigenvalue ( double  val,
const unsigned int  i 
)
void SteadyState::setEigenvalue ( double  val,
const unsigned int  i 
)
void SteadyState::setMaxIter ( unsigned int  value)
void SteadyState::setMaxIter ( unsigned int  value)

Definition at line 405 of file SteadyStateBoost.cpp.

References maxIter_, and value.

Referenced by initCinfo().

406 {
407  maxIter_ = value;
408 }
uint32_t value
Definition: moosemodule.h:42
unsigned int maxIter_

+ Here is the caller graph for this function:

static void SteadyState::setMolN ( double  y,
unsigned int  i 
)
static
static void SteadyState::setMolN ( double  y,
unsigned int  i 
)
static
void SteadyState::setStoich ( Id  s)
void SteadyState::setStoich ( Id  s)

Definition at line 358 of file SteadyStateBoost.cpp.

References Element::cinfo(), Eref::data(), Id::element(), Id::eref(), Field< A >::get(), LookupField< L, A >::get(), Stoich::getCompartment(), Stoich::getNumCoreRates(), Stoich::getRateTerms(), Cinfo::isA(), isInitialized_, nReacs_, numVarPools_, pool_, VoxelPools::setStoich(), setupSSmatrix(), VoxelPoolsBase::setVolume(), stoich_, VoxelPools::updateAllRateTerms(), and value.

Referenced by initCinfo().

359 {
360  if ( !value.element()->cinfo()->isA( "Stoich" ) )
361  {
362  cout << "Error: SteadyState::setStoich: Must be of Stoich class\n";
363  return;
364  }
365 
366  stoich_ = value;
367  Stoich* stoichPtr = reinterpret_cast< Stoich* >( value.eref().data());
368  numVarPools_ = Field< unsigned int >::get( stoich_, "numVarPools" );
369  nReacs_ = Field< unsigned int >::get( stoich_, "numRates" );
370  setupSSmatrix();
372  stoichPtr->getCompartment(), "oneVoxelVolume", 0 );
373  pool_.setVolume( vol );
374  pool_.setStoich( stoichPtr, 0 );
375  pool_.updateAllRateTerms( stoichPtr->getRateTerms(),
376  stoichPtr->getNumCoreRates() );
377  isInitialized_ = 1;
378 }
uint32_t value
Definition: moosemodule.h:42
Id getCompartment() const
Definition: Stoich.cpp:505
static A get(const ObjId &dest, const string &field, L index)
Definition: SetGet.h:532
void setStoich(Stoich *stoich, const OdeSystem *ode)
Definition: VoxelPools.cpp:67
Definition: Stoich.h:49
VoxelPools pool_
void setVolume(double vol)
Just assigns the volume without any cascading to other values.
unsigned int getNumCoreRates() const
Number of rate terms for reactions purely on this compartment.
Definition: Stoich.cpp:574
static A get(const ObjId &dest, const string &field)
Definition: SetGet.h:284
const vector< RateTerm * > & getRateTerms() const
Returns a reference to the entire rates_ vector.
Definition: Stoich.cpp:589
unsigned int nReacs_
void updateAllRateTerms(const vector< RateTerm * > &rates, unsigned int numCoreRates)
Updates all the rate constants from the reference rates vector.
Definition: VoxelPools.cpp:282

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

void SteadyState::settle ( bool  forceSetup)
void SteadyState::settle ( bool  forceSetup)

The settle function computes the steady state nearest the initial conditions.

Definition at line 735 of file SteadyStateBoost.cpp.

References convergenceCriterion_, gamma_, Field< A >::get(), init(), isInitialized_, isSetup_, isSolutionValid(), Nr_, nReacs_, numVarPools_, pool_, rank_, reassignTotal_, setupSSmatrix(), solutionStatus_, status_, stoich_, and total_.

Referenced by resettleFunc(), and settleFunc().

736 {
737 
738  if ( !isInitialized_ )
739  {
740  cout << "Error: SteadyState object has not been initialized. No calculations done\n";
741  return;
742  }
743 
744  if ( forceSetup || isSetup_ == 0 )
745  setupSSmatrix();
746 
747  // Setting up matrices and vectors for the calculation.
748  unsigned int nConsv = numVarPools_ - rank_;
749  double * T = (double *) calloc( nConsv, sizeof( double ) );
750 
751  // Setting up matrices and vectors for the calculation.
752  Id ksolve = Field< Id >::get( stoich_, "ksolve" );
753 
754  ss = new NonlinearSystem( numVarPools_ );
755 
756  ss->ri.rank = rank_;
757  ss->ri.num_reacs = nReacs_;
758  ss->ri.num_mols = numVarPools_;
759  ss->ri.T = T;
760  ss->ri.Nr = Nr_;
761  ss->ri.gamma = gamma_;
762  ss->ri.pool = &pool_;
763  ss->ri.nVec = LookupField< unsigned int, vector< double > >::get(
764  ksolve,"nVec", 0
765  );
766  ss->ri.convergenceCriterion = convergenceCriterion_;
767 
768  // This gives the starting point for finding the solution.
769  vector<double> init( numVarPools_ );
770 
771  // Instead of starting at sqrt( x ),
772  for( size_t i = 0; i < numVarPools_; ++i )
773  init[i] = max( 0.0, sqrt(ss->ri.nVec[i]) );
774 
775  ss->initialize<vector<double>>( init );
776 
777  // Fill up boundary condition values
778  if ( reassignTotal_ ) // The user has defined new conservation values.
779  {
780  for (size_t i = 0; i < nConsv; ++i )
781  T[i] = total_[i];
782  reassignTotal_ = 0;
783  }
784  else
785  {
786  for ( size_t i = 0; i < nConsv; ++i )
787  for ( size_t j = 0; j < numVarPools_; ++j )
788  T[i] += gamma_( i, j ) * ss->ri.nVec[ j ];
789  total_.assign( T, T + nConsv );
790  }
791 
792  vector< double > repair( numVarPools_, 0.0 );
793 
794  for ( unsigned int j = 0; j < numVarPools_; ++j )
795  repair[j] = ss->ri.nVec[j];
796 
797 
798  int status = 1;
799 
800  // Find roots . If successful, set status to 0.
801  if( ss->find_roots_gnewton( ) )
802  status = 0;
803 
804  if ( status == 0 && isSolutionValid( ss->ri.nVec ) )
805  {
806  solutionStatus_ = 0; // Good solution
807 
809  ksolve, "nVec", 0, ss->ri.nVec
810  );
811  // Check what we set
812  vector<double> t = LookupField< unsigned int, vector< double > >::get(
813  ksolve,"nVec", 0
814  );
815 
816  //classifyState( T );
817  }
818  else
819  {
820  cout << "Warning: SteadyState iteration failed, status = " <<
821  status_ << ", nIter = " << ss->ri.nIter << endl;
822 
823  for ( unsigned int j = 0; j < numVarPools_; j++ )
824  ss->ri.nVec[j] = repair[j];
825 
826  solutionStatus_ = 1; // Steady state failed.
827  LookupField< unsigned int, vector< double > >::set(
828  ksolve, "nVec", 0, ss->ri.nVec
829  );
830 
831  }
832 
833  // Clean up.
834  free( T );
835 }
Id init(int argc, char **argv, bool &doUnitTests, bool &doRegressionTests, unsigned int &benchmark)
Definition: main.cpp:150
boost::numeric::ublas::matrix< value_type_ > gamma_
double convergenceCriterion_
unsigned int rank_
vector< double > total_
VoxelPools pool_
static bool isSolutionValid(const vector< double > &x)
Definition: Id.h:17
static A get(const ObjId &dest, const string &field)
Definition: SetGet.h:284
unsigned int nReacs_
boost::numeric::ublas::matrix< value_type_ > Nr_
unsigned int solutionStatus_

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

void SteadyState::settleFunc ( )
void SteadyState::settleFunc ( )

Definition at line 494 of file SteadyStateBoost.cpp.

References settle().

Referenced by initCinfo().

495 {
496  settle( 0 );
497 }
void settle(bool forceSetup)

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

void SteadyState::setTotal ( const unsigned int  i,
double  val 
)
void SteadyState::setTotal ( const unsigned int  i,
double  val 
)

Definition at line 464 of file SteadyStateBoost.cpp.

References reassignTotal_, and total_.

Referenced by initCinfo().

465 {
466  if ( i < total_.size() )
467  {
468  total_[i] = val;
469  reassignTotal_ = 1;
470  return;
471  }
472  cout << "Warning: SteadyState::setTotal: index " << i <<
473  " out of range " << total_.size() << endl;
474 }
vector< double > total_

+ Here is the caller graph for this function:

void SteadyState::setupMatrix ( )
void SteadyState::setupMatrix ( )

Definition at line 490 of file SteadyStateBoost.cpp.

References setupSSmatrix().

Referenced by initCinfo().

491 {
492  setupSSmatrix();
493 }

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

void SteadyState::setupSSmatrix ( )
private
void SteadyState::setupSSmatrix ( )
private

Definition at line 525 of file SteadyStateBoost.cpp.

References gamma_, Field< A >::get(), isSetup_, LU_, Nr_, nReacs_, numVarPools_, rank_, rankUsingBoost(), stoich_, and total_.

Referenced by setStoich(), settle(), and setupMatrix().

526 {
527 
528  if ( numVarPools_ == 0 || nReacs_ == 0 )
529  return;
530 
531  int nTot = numVarPools_ + nReacs_;
532 
533  ublas::matrix<double> N(numVarPools_, nReacs_, 0.0);
534 
535  LU_ = ublas::matrix< double >( numVarPools_, nTot, 0.0);
536 
537  vector< int > entry = Field< vector< int > >::get(
538  stoich_, "matrixEntry" );
539  vector< unsigned int > colIndex = Field< vector< unsigned int > >::get(
540  stoich_, "columnIndex" );
541  vector< unsigned int > rowStart = Field< vector< unsigned int > >::get(
542  stoich_, "rowStart" );
543 
544  for ( unsigned int i = 0; i < numVarPools_; ++i )
545  {
546  LU_(i, i + nReacs_) = 1;
547  unsigned int k = rowStart[i];
548  for ( unsigned int j = 0; j < nReacs_; ++j )
549  {
550  double x = 0;
551  if ( j == colIndex[k] && k < rowStart[i+1] )
552  {
553  x = entry[k++];
554  }
555  N(i,j) = x;
556  LU_(i,j) = x;
557  }
558  }
559 
560  // This function reorgranize LU_.
561  rank_ = rankUsingBoost( LU_ );
562  Nr_ = ublas::matrix< double >( rank_, nReacs_ );
563  Nr_.assign( ublas::zero_matrix< double >( rank_, nReacs_ ) );
564 
565  unsigned int nConsv = numVarPools_ - rank_;
566  if ( nConsv == 0 )
567  {
568  cout << "SteadyState::setupSSmatrix(): Number of conserved species == 0. Aborting\n";
569  return;
570  }
571 
572  // Fill up Nr.
573  for ( unsigned int i = 0; i < rank_; i++)
574  for ( unsigned int j = i; j < nReacs_; j++)
575  Nr_(i,j) = LU_(i, j);
576 
577  gamma_ = ublas::matrix< double >( nConsv, numVarPools_, 0.0 );
578 
579  // Fill up gamma
580  for ( unsigned int i = rank_; i < numVarPools_; ++i )
581  for ( unsigned int j = 0; j < numVarPools_; ++j )
582  gamma_(i-rank_, j) = LU_(i, j+nReacs_);
583 
584  // Fill up boundary condition values
585  total_.resize( nConsv );
586  total_.assign( nConsv, 0.0 );
587 
588  Id ksolve = Field< Id >::get( stoich_, "ksolve" );
589  vector< double > nVec =
591  ksolve,"nVec", 0 );
592 
593  if ( nVec.size() >= numVarPools_ )
594  {
595  for ( unsigned int i = 0; i < nConsv; ++i )
596  for ( unsigned int j = 0; j < numVarPools_; ++j )
597  total_[i] += gamma_(i,j) * nVec[ j ];
598  isSetup_ = 1;
599  }
600  else
601  {
602  cout << "Error: SteadyState::setupSSmatrix(): unable to get"
603  "pool numbers from ksolve.\n";
604  isSetup_ = 0;
605  }
606 
607 }
boost::numeric::ublas::matrix< value_type_ > gamma_
Definition: SetGet.h:236
unsigned int rank_
vector< double > total_
Definition: Id.h:17
boost::numeric::ublas::matrix< value_type_ > LU_
static A get(const ObjId &dest, const string &field)
Definition: SetGet.h:284
unsigned int nReacs_
boost::numeric::ublas::matrix< value_type_ > Nr_
unsigned int rankUsingBoost(ublas::matrix< double > &U)

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

void SteadyState::showMatrices ( )
void SteadyState::showMatrices ( )

Definition at line 509 of file SteadyStateBoost.cpp.

References gamma_, isInitialized_, LU_, Nr_, numVarPools_, rank_, and total_.

Referenced by initCinfo().

510 {
511  if ( !isInitialized_ ) {
512  cout << "SteadyState::showMatrices: Sorry, the system is not yet initialized.\n";
513  return;
514  }
515  int numConsv = numVarPools_ - rank_;
516  cout << "Totals: ";
517  for ( int i = 0; i < numConsv; ++i )
518  cout << total_[i] << " ";
519  cout << endl;
520  cout << "gamma " << gamma_ << endl;
521  cout << "Nr " << Nr_ << endl;
522  cout << "LU " << LU_ << endl;
523 }
boost::numeric::ublas::matrix< value_type_ > gamma_
unsigned int rank_
vector< double > total_
boost::numeric::ublas::matrix< value_type_ > LU_
boost::numeric::ublas::matrix< value_type_ > Nr_

+ Here is the caller graph for this function:

void SteadyState::showMatricesFunc ( )
void SteadyState::showMatricesFunc ( )

Member Data Documentation

bool SteadyState::badStoichiometry_
private

Definition at line 103 of file SteadyStateBoost.h.

Referenced by badStoichiometry().

double SteadyState::convergenceCriterion_
private

Definition at line 107 of file SteadyStateBoost.h.

Referenced by getConvergenceCriterion(), setConvergenceCriterion(), and settle().

static const double SteadyState::DELTA = 1e-6
static

Definition at line 90 of file SteadyStateBoost.h.

Referenced by classifyState().

vector< double > SteadyState::eigenvalues_
private

Definition at line 122 of file SteadyStateBoost.h.

Referenced by classifyState(), and getEigenvalue().

static const double SteadyState::EPSILON = 1e-9
static
boost::numeric::ublas::matrix< value_type_ > SteadyState::gamma_
private

Definition at line 111 of file SteadyStateBoost.h.

Referenced by randomizeInitialCondition(), settle(), setupSSmatrix(), and showMatrices().

bool SteadyState::isInitialized_
private

Definition at line 105 of file SteadyStateBoost.h.

Referenced by isInitialized(), setStoich(), settle(), and showMatrices().

bool SteadyState::isSetup_
private

Definition at line 106 of file SteadyStateBoost.h.

Referenced by settle(), and setupSSmatrix().

boost::numeric::ublas::matrix< value_type_ > SteadyState::LU_
private

Definition at line 109 of file SteadyStateBoost.h.

Referenced by setupSSmatrix(), and showMatrices().

unsigned int SteadyState::maxIter_
private

Definition at line 102 of file SteadyStateBoost.h.

Referenced by getMaxIter(), and setMaxIter().

unsigned int SteadyState::nIter_
private

Definition at line 101 of file SteadyStateBoost.h.

Referenced by getNiter().

unsigned int SteadyState::nNegEigenvalues_
private

Definition at line 120 of file SteadyStateBoost.h.

Referenced by classifyState(), and getNnegEigenvalues().

unsigned int SteadyState::nPosEigenvalues_
private

Definition at line 121 of file SteadyStateBoost.h.

Referenced by classifyState(), and getNposEigenvalues().

boost::numeric::ublas::matrix< value_type_ > SteadyState::Nr_
private

Definition at line 110 of file SteadyStateBoost.h.

Referenced by settle(), setupSSmatrix(), and showMatrices().

unsigned int SteadyState::nReacs_
private

Definition at line 115 of file SteadyStateBoost.h.

Referenced by setStoich(), settle(), and setupSSmatrix().

unsigned int SteadyState::numFailed_
private

Definition at line 125 of file SteadyStateBoost.h.

unsigned int SteadyState::numVarPools_
private

Definition at line 108 of file SteadyStateGsl.h.

size_t SteadyState::numVarPools_
private
VoxelPools SteadyState::pool_
private

Definition at line 126 of file SteadyStateBoost.h.

Referenced by classifyState(), setStoich(), and settle().

unsigned int SteadyState::rank_
private

Definition at line 116 of file SteadyStateBoost.h.

Referenced by classifyState(), getRank(), settle(), setupSSmatrix(), and showMatrices().

bool SteadyState::reassignTotal_
private

Definition at line 119 of file SteadyStateBoost.h.

Referenced by settle(), and setTotal().

unsigned int SteadyState::solutionStatus_
private

Definition at line 124 of file SteadyStateBoost.h.

Referenced by classifyState(), getSolutionStatus(), and settle().

unsigned int SteadyState::stateType_
private

Definition at line 123 of file SteadyStateBoost.h.

Referenced by classifyState(), and getStateType().

string SteadyState::status_
private

Definition at line 104 of file SteadyStateBoost.h.

Referenced by getStatus(), and settle().

Id SteadyState::stoich_
private
vector< double > SteadyState::total_
private

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