MOOSE - Multiscale Object Oriented Simulation Environment
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
RateTerm.h
Go to the documentation of this file.
1 /**********************************************************************
2 ** This program is part of 'MOOSE', the
3 ** Messaging Object Oriented Simulation Environment,
4 ** also known as GENESIS 3 base code.
5 ** copyright (C) 2003-2010 Upinder S. Bhalla. and NCBS
6 ** It is made available under the terms of the
7 ** GNU Lesser General Public License version 2.1
8 ** See the file COPYING.LIB for the full notice.
9 **********************************************************************/
10 
11 /*
12 double assignRates( RateCalculation& r )
13 {
14  return r();
15 }
16 */
17 #include "../utility/numutil.h"
18 class RateTerm
19 {
20  public:
21  RateTerm() {;}
22  virtual ~RateTerm() {;}
24  virtual double operator() ( const double* S ) const = 0;
25 
29  virtual void setRates( double k1, double k2 ) = 0;
30 
32  virtual void setR1( double k1 ) = 0;
33 
35  virtual void setR2( double k2 ) = 0;
36 
38  virtual double getR1() const = 0;
39 
41  virtual double getR2() const = 0;
42 
50  virtual unsigned int getReactants(
51  vector< unsigned int >& molIndex ) const = 0;
52  static const double EPSILON;
53 
63  virtual void rescaleVolume( short comptIndex,
64  const vector< short >& compartmentLookup, double ratio ) = 0;
65 
76  double vol, double sub, double prd ) const = 0;
77 };
78 
79 // Base class MMEnzme for the purposes of setting rates
80 // Pure base class: cannot be instantiated, but useful as a handle.
81 class MMEnzymeBase: public RateTerm
82 {
83  public:
84  MMEnzymeBase( double Km, double kcat, unsigned int enz )
85  : Km_( Km ), kcat_( kcat ), enz_( enz )
86  {
87  assert( Km_ > 0.0 );
88  }
89 
90  void setKm( double Km ) {
91  if ( Km > 0.0 )
92  Km_ = Km;
93  }
94 
95  void setKcat( double kcat ) {
96  if ( kcat > 0 )
97  kcat_ = kcat;
98  }
99 
100  void setRates( double Km, double kcat ) {
101  setKm( Km );
102  setKcat( kcat );
103  }
104 
105  void setR1( double Km ) {
106  setKm( Km );
107  }
108 
109  void setR2( double kcat ) {
110  setKcat( kcat );
111  }
112 
113  double getR1() const {
114  return Km_;
115  }
116 
117  double getR2() const {
118  return kcat_;
119  }
120 
121  void rescaleVolume( short comptIndex,
122  const vector< short >& compartmentLookup, double ratio )
123  {
124  Km_ *= ratio;
125  }
126 
127  unsigned int getEnzIndex() const
128  {
129  return enz_;
130  }
131 
132  protected:
133  double Km_; // In # units, not conc units.
134  double kcat_;
135  unsigned int enz_;
136 };
137 
138 // Single substrate MMEnzyme: by far the most common.
139 class MMEnzyme1: public MMEnzymeBase
140 {
141  public:
142  MMEnzyme1( double Km, double kcat,
143  unsigned int enz, unsigned int sub )
144  : MMEnzymeBase( Km, kcat, enz ), sub_( sub )
145  {
146  ;
147  }
148 
149  double operator() ( const double* S ) const {
150  // assert( S[ sub_ ] >= -EPSILON );
151  return ( kcat_ * S[ sub_ ] * S[ enz_ ] ) / ( Km_ + S[ sub_ ] );
152  }
153 
154  unsigned int getReactants( vector< unsigned int >& molIndex ) const{
155  molIndex.resize( 2 );
156  molIndex[0] = enz_;
157  molIndex[1] = sub_;
158  return 2;
159  }
160 
162  double vol, double sub, double prd ) const
163  {
164  double ratio = vol * sub * NA;
165  return new MMEnzyme1( ratio * Km_, kcat_, enz_, sub_);
166  }
167 
168  private:
169  unsigned int sub_;
170 };
171 
172 class MMEnzyme: public MMEnzymeBase
173 {
174  public:
175  MMEnzyme( double Km, double kcat,
176  unsigned int enz, RateTerm* sub )
177  : MMEnzymeBase( Km, kcat, enz ), substrates_( sub )
178  {
179  ;
180  }
181 
182  double operator() ( const double* S ) const {
183  double sub = (*substrates_)( S );
184  // the subtrates_() operator returns the conc product.
185  assert( sub >= -EPSILON );
186  return ( sub * kcat_ * S[ enz_ ] ) / ( Km_ + sub );
187  }
188 
189  unsigned int getReactants( vector< unsigned int >& molIndex ) const{
190  substrates_->getReactants( molIndex );
191  molIndex.insert( molIndex.begin(), enz_ );
192  return molIndex.size();
193  }
194 
196  double vol, double sub, double prd ) const
197  {
198  double ratio = sub * vol * NA;
199  return new MMEnzyme( ratio * Km_, kcat_, enz_, substrates_ );
200  }
201  private:
203 };
204 
205 class ExternReac: public RateTerm
206 {
207  public:
208  // All the terms will have been updated separately, and
209  // a reply obtained to this pointer here:
210  double operator() ( const double* S ) const {
211  double ret = 0.0;
212  return ret;
213  }
214  void setRates( double k1, double k2 ) {
215  ; // Dummy function to keep compiler happy
216  }
217 
218  void setR1( double k1 ) {
219  ;
220  }
221 
222  void setR2( double k2 ) {
223  ;
224  }
225 
226  double getR1() const {
227  return 0.0;
228  }
229 
230  double getR2() const {
231  return 0.0;
232  }
233 
234  unsigned int getReactants( vector< unsigned int >& molIndex ) const{
235  molIndex.resize( 0 );
236  return 0;
237  }
238 
239  void rescaleVolume( short comptIndex,
240  const vector< short >& compartmentLookup, double ratio )
241  {
242  return; // Need to figure out what to do here.
243  }
244 
246  double vol, double sub, double prd ) const
247  {
248  return new ExternReac();
249  }
250 
251  private:
252 };
253 
254 class ZeroOrder: public RateTerm
255 {
256  public:
257  ZeroOrder( double k )
258  : k_( k )
259  {
260  assert( !std::isnan( k_ ) );
261  }
262 
263  double operator() ( const double* S ) const {
264  assert( !std::isnan( k_ ) );
265  return k_;
266  }
267 
268  void setK( double k ) {
269  assert( !std::isnan( k ) );
270  if ( k >= 0.0 )
271  k_ = k;
272  }
273 
274  void setRates( double k1, double k2 ) {
275  setK( k1 );
276  }
277 
278  void setR1( double k1 ) {
279  setK( k1 );
280  }
281 
282  void setR2( double k2 ) {
283  ;
284  }
285 
286  double getR1() const {
287  return k_;
288  }
289 
290  double getR2() const {
291  return 0.0;
292  }
293 
294  unsigned int getReactants( vector< unsigned int >& molIndex ) const{
295  molIndex.resize( 0 );
296  return 0;
297  }
298 
299  void rescaleVolume( short comptIndex,
300  const vector< short >& compartmentLookup, double ratio )
301  {
302  return; // Nothing needs to be scaled.
303  }
304 
306  double vol, double sub, double prd ) const
307  {
308  return new ZeroOrder( k_ );
309  }
310  protected:
311  double k_;
312 };
313 
320 class Flux: public ZeroOrder
321 {
322  public:
323  Flux( double k, unsigned int y )
324  : ZeroOrder( k ), y_( y )
325  {;}
326 
327  double operator() ( const double* S ) const {
328  assert( !std::isnan( S[ y_ ] ) );
329  return k_ * S[ y_ ];
330  }
331 
332  unsigned int getReactants( vector< unsigned int >& molIndex ) const{
333  molIndex.resize( 0 );
334  return 0;
335  }
336 
337  void rescaleVolume( short comptIndex,
338  const vector< short >& compartmentLookup, double ratio )
339  {
340  return; // Nothing needs to be scaled.
341  }
342 
344  double vol, double sub, double prd ) const
345  {
346  return new Flux( k_, y_ );
347  }
348 
349  private:
350  unsigned int y_;
351 };
352 
353 class FirstOrder: public ZeroOrder
354 {
355  public:
356  FirstOrder( double k, unsigned int y )
357  : ZeroOrder( k ), y_( y )
358  {;}
359 
360  double operator() ( const double* S ) const {
361  assert( !std::isnan( S[ y_ ] ) );
362  return k_ * S[ y_ ];
363  }
364 
365  unsigned int getReactants( vector< unsigned int >& molIndex ) const{
366  molIndex.resize( 1 );
367  molIndex[0] = y_;
368  return 1;
369  }
370 
371  void rescaleVolume( short comptIndex,
372  const vector< short >& compartmentLookup, double ratio )
373  {
374  return; // Nothing needs to be scaled.
375  }
376 
378  double vol, double sub, double prd ) const
379  {
380  return new FirstOrder( k_ / sub, y_ );
381  }
382 
383  private:
384  unsigned int y_;
385 };
386 
387 class SecondOrder: public ZeroOrder
388 {
389  public:
390  SecondOrder( double k, unsigned int y1, unsigned int y2 )
391  : ZeroOrder( k ), y1_( y1 ), y2_( y2 )
392  {;}
393 
394  double operator() ( const double* S ) const {
395  assert( !std::isnan( S[ y1_ ] ) );
396  assert( !std::isnan( S[ y2_ ] ) );
397  return k_ * S[ y1_ ] * S[ y2_ ];
398  }
399 
400  unsigned int getReactants( vector< unsigned int >& molIndex ) const{
401  molIndex.resize( 2 );
402  molIndex[0] = y1_;
403  molIndex[1] = y2_;
404  return 2;
405  }
406 
407  void rescaleVolume( short comptIndex,
408  const vector< short >& compartmentLookup, double ratio )
409  {
410  if ( comptIndex == compartmentLookup[ y1_ ] ||
411  comptIndex == compartmentLookup[ y2_ ] )
412  k_ /= ratio;
413  }
414 
416  double vol, double sub, double prd ) const
417  {
418  double ratio = sub * vol * NA;
419  return new SecondOrder( k_ / ratio, y1_, y2_ );
420  }
421 
422  private:
423  unsigned int y1_;
424  unsigned int y2_;
425 };
426 
435 {
436  public:
437  StochSecondOrderSingleSubstrate( double k, unsigned int y )
438  : ZeroOrder( k ), y_( y )
439  {;}
440 
441  double operator() ( const double* S ) const {
442  double y = S[ y_ ];
443  assert( !std::isnan( y ) );
444  return k_ * ( y - 1 ) * y;
445  }
446 
447  unsigned int getReactants( vector< unsigned int >& molIndex ) const{
448  molIndex.resize( 2 );
449  molIndex[0] = y_;
450  molIndex[1] = y_;
451  return 2;
452  }
453 
454  void rescaleVolume( short comptIndex,
455  const vector< short >& compartmentLookup, double ratio )
456  {
457  if ( comptIndex == compartmentLookup[ y_ ] )
458  k_ /= ratio;
459  }
460 
462  double vol, double sub, double prd ) const
463  {
464  double ratio = sub * vol * NA;
465  return new StochSecondOrderSingleSubstrate( k_ / ratio, y_ );
466  }
467 
468  private:
469  const unsigned int y_;
470 };
471 
472 class NOrder: public ZeroOrder
473 {
474  public:
475  NOrder( double k, vector< unsigned int > v )
476  : ZeroOrder( k ), v_( v )
477  {;}
478 
479  double operator() ( const double* S ) const {
480  double ret = k_;
481  vector< unsigned int >::const_iterator i;
482  for ( i = v_.begin(); i != v_.end(); i++) {
483  assert( !std::isnan( S[ *i ] ) );
484  ret *= S[ *i ];
485  }
486  return ret;
487  }
488 
489  unsigned int getReactants( vector< unsigned int >& molIndex ) const{
490  molIndex = v_;
491  return v_.size();
492  }
493 
494  void rescaleVolume( short comptIndex,
495  const vector< short >& compartmentLookup, double ratio )
496  {
497  for ( unsigned int i = 1; i < v_.size(); ++i ) {
498  if ( comptIndex == compartmentLookup[ v_[i] ] )
499  k_ /= ratio;
500  }
501  }
502 
504  double vol, double sub, double prd ) const
505  {
506  assert( v_.size() > 0 );
507  double ratio = sub * pow( NA * vol, (int)( v_.size() ) - 1 );
508  return new NOrder( k_ / ratio, v_ );
509  }
510 
511  protected:
512  vector< unsigned int > v_;
513 };
514 
522 class StochNOrder: public NOrder
523 {
524  public:
525  StochNOrder( double k, vector< unsigned int > v );
526 
527  double operator() ( const double* S ) const;
528 
530  double vol, double sub, double prd ) const
531  {
532  assert( v_.size() > 0 );
533  double ratio = sub * pow( vol * NA, (int)( v_.size() ) -1);
534  return new StochNOrder( k_ / ratio, v_ );
535  }
536 };
537 
538 extern class ZeroOrder*
539  makeHalfReaction( double k, vector< unsigned int > v );
540 
542 {
543  public:
545  ZeroOrder* forward, ZeroOrder* backward)
546  : forward_( forward ), backward_( backward )
547  { // Here we allocate internal forward and backward terms
548  // with the correct number of args.
549  ;
550  }
552  {
553  delete forward_;
554  delete backward_;
555  }
556 
557  double operator() ( const double* S ) const {
558  return (*forward_)( S ) - (*backward_)( S );
559  }
560 
561  void setRates( double kf, double kb ) {
562  forward_->setK( kf );
563  backward_->setK( kb );
564  }
565 
566  void setR1( double kf ) {
567  forward_->setK( kf );
568  }
569 
570  void setR2( double kb ) {
571  backward_->setK( kb );
572  }
573 
574  double getR1() const {
575  return forward_->getR1();
576  }
577 
578  double getR2() const {
579  return backward_->getR1();
580  }
581 
582  unsigned int getReactants( vector< unsigned int >& molIndex ) const{
583  forward_->getReactants( molIndex );
584  unsigned int ret = molIndex.size();
585  vector< unsigned int > temp;
586  backward_->getReactants( temp );
587  molIndex.insert( molIndex.end(), temp.begin(), temp.end() );
588  return ret;
589  }
590 
591  void rescaleVolume( short comptIndex,
592  const vector< short >& compartmentLookup, double ratio )
593  {
594  forward_->rescaleVolume( comptIndex, compartmentLookup, ratio );
595  backward_->rescaleVolume( comptIndex, compartmentLookup, ratio);
596  }
598  double vol, double sub, double prd ) const
599  {
600  ZeroOrder* f = static_cast< ZeroOrder* >(
601  forward_->copyWithVolScaling( vol, sub, 1 ) );
602  ZeroOrder* b = static_cast< ZeroOrder* >(
603  backward_->copyWithVolScaling( vol, prd, 1 ) );
604  return new BidirectionalReaction( f, b );
605  }
606 
607  private:
610 };
unsigned int getReactants(vector< unsigned int > &molIndex) const
Definition: RateTerm.h:582
ZeroOrder * forward_
Definition: RateTerm.h:608
void setRates(double kf, double kb)
Definition: RateTerm.h:561
void setK(double k)
Definition: RateTerm.h:268
BidirectionalReaction(ZeroOrder *forward, ZeroOrder *backward)
Definition: RateTerm.h:544
Flux(double k, unsigned int y)
Definition: RateTerm.h:323
double operator()(const double *S) const
Computes the rate. The argument is the molecule array.
Definition: RateTerm.h:263
ZeroOrder(double k)
Definition: RateTerm.h:257
virtual void rescaleVolume(short comptIndex, const vector< short > &compartmentLookup, double ratio)=0
double k_
Definition: RateTerm.h:311
void setR1(double kf)
Used by Zombie to assign rate terms.
Definition: RateTerm.h:566
unsigned int y_
Definition: RateTerm.h:384
static const double EPSILON
Definition: RateTerm.h:52
unsigned int enz_
Definition: RateTerm.h:135
void setRates(double k1, double k2)
Definition: RateTerm.h:214
virtual unsigned int getReactants(vector< unsigned int > &molIndex) const =0
RateTerm * copyWithVolScaling(double vol, double sub, double prd) const
Definition: RateTerm.h:461
unsigned int getReactants(vector< unsigned int > &molIndex) const
Definition: RateTerm.h:400
double getR2() const
Used by Zombie to return rate terms.
Definition: RateTerm.h:578
MMEnzyme1(double Km, double kcat, unsigned int enz, unsigned int sub)
Definition: RateTerm.h:142
void setR1(double k1)
Used by Zombie to assign rate terms.
Definition: RateTerm.h:218
double getR2() const
Used by Zombie to return rate terms.
Definition: RateTerm.h:117
StochSecondOrderSingleSubstrate(double k, unsigned int y)
Definition: RateTerm.h:437
RateTerm * substrates_
Definition: RateTerm.h:202
MMEnzymeBase(double Km, double kcat, unsigned int enz)
Definition: RateTerm.h:84
void rescaleVolume(short comptIndex, const vector< short > &compartmentLookup, double ratio)
Definition: RateTerm.h:299
const double NA
Definition: consts.cpp:15
double getR1() const
Used by Zombie to return rate terms.
Definition: RateTerm.h:226
unsigned int getReactants(vector< unsigned int > &molIndex) const
Definition: RateTerm.h:365
unsigned int getReactants(vector< unsigned int > &molIndex) const
Definition: RateTerm.h:154
virtual double operator()(const double *S) const =0
Computes the rate. The argument is the molecule array.
void setR1(double Km)
Used by Zombie to assign rate terms.
Definition: RateTerm.h:105
virtual double getR2() const =0
Used by Zombie to return rate terms.
unsigned int y1_
Definition: RateTerm.h:423
virtual void setRates(double k1, double k2)=0
RateTerm * copyWithVolScaling(double vol, double sub, double prd) const
Definition: RateTerm.h:377
unsigned int getReactants(vector< unsigned int > &molIndex) const
Definition: RateTerm.h:447
void rescaleVolume(short comptIndex, const vector< short > &compartmentLookup, double ratio)
Definition: RateTerm.h:337
unsigned int sub_
Definition: RateTerm.h:169
void rescaleVolume(short comptIndex, const vector< short > &compartmentLookup, double ratio)
Definition: RateTerm.h:494
void setR2(double kcat)
Used by Zombie to assign rate terms.
Definition: RateTerm.h:109
void setR1(double k1)
Used by Zombie to assign rate terms.
Definition: RateTerm.h:278
RateTerm * copyWithVolScaling(double vol, double sub, double prd) const
Definition: RateTerm.h:245
unsigned int getEnzIndex() const
Definition: RateTerm.h:127
RateTerm * copyWithVolScaling(double vol, double sub, double prd) const
Definition: RateTerm.h:195
void rescaleVolume(short comptIndex, const vector< short > &compartmentLookup, double ratio)
Definition: RateTerm.h:454
virtual void setR2(double k2)=0
Used by Zombie to assign rate terms.
double operator()(const double *S) const
Computes the rate. The argument is the molecule array.
Definition: RateTerm.cpp:30
double getR2() const
Used by Zombie to return rate terms.
Definition: RateTerm.h:290
double operator()(const double *S) const
Computes the rate. The argument is the molecule array.
Definition: RateTerm.h:210
void setR2(double k2)
Used by Zombie to assign rate terms.
Definition: RateTerm.h:282
double operator()(const double *S) const
Computes the rate. The argument is the molecule array.
Definition: RateTerm.h:394
void setKm(double Km)
Definition: RateTerm.h:90
unsigned int getReactants(vector< unsigned int > &molIndex) const
Definition: RateTerm.h:489
double getR1() const
Used by Zombie to return rate terms.
Definition: RateTerm.h:286
void rescaleVolume(short comptIndex, const vector< short > &compartmentLookup, double ratio)
Definition: RateTerm.h:239
class ZeroOrder * makeHalfReaction(double k, vector< unsigned int > v)
void setKcat(double kcat)
Definition: RateTerm.h:95
RateTerm * copyWithVolScaling(double vol, double sub, double prd) const
Definition: RateTerm.h:503
double operator()(const double *S) const
Computes the rate. The argument is the molecule array.
Definition: RateTerm.h:149
RateTerm * copyWithVolScaling(double vol, double sub, double prd) const
Definition: RateTerm.h:529
void rescaleVolume(short comptIndex, const vector< short > &compartmentLookup, double ratio)
Definition: RateTerm.h:371
FirstOrder(double k, unsigned int y)
Definition: RateTerm.h:356
void setRates(double Km, double kcat)
Definition: RateTerm.h:100
double operator()(const double *S) const
Computes the rate. The argument is the molecule array.
Definition: RateTerm.h:360
virtual double getR1() const =0
Used by Zombie to return rate terms.
NOrder(double k, vector< unsigned int > v)
Definition: RateTerm.h:475
void rescaleVolume(short comptIndex, const vector< short > &compartmentLookup, double ratio)
Definition: RateTerm.h:591
unsigned int getReactants(vector< unsigned int > &molIndex) const
Definition: RateTerm.h:189
double Km_
Definition: RateTerm.h:133
virtual ~RateTerm()
Definition: RateTerm.h:22
virtual RateTerm * copyWithVolScaling(double vol, double sub, double prd) const =0
StochNOrder(double k, vector< unsigned int > v)
Definition: RateTerm.cpp:21
double operator()(const double *S) const
Computes the rate. The argument is the molecule array.
Definition: RateTerm.h:441
double getR2() const
Used by Zombie to return rate terms.
Definition: RateTerm.h:230
RateTerm()
Definition: RateTerm.h:21
double operator()(const double *S) const
Computes the rate. The argument is the molecule array.
Definition: RateTerm.h:327
MMEnzyme(double Km, double kcat, unsigned int enz, RateTerm *sub)
Definition: RateTerm.h:175
double getR1() const
Used by Zombie to return rate terms.
Definition: RateTerm.h:113
void rescaleVolume(short comptIndex, const vector< short > &compartmentLookup, double ratio)
Definition: RateTerm.h:121
double operator()(const double *S) const
Computes the rate. The argument is the molecule array.
Definition: RateTerm.h:182
unsigned int getReactants(vector< unsigned int > &molIndex) const
Definition: RateTerm.h:332
double operator()(const double *S) const
Computes the rate. The argument is the molecule array.
Definition: RateTerm.h:479
RateTerm * copyWithVolScaling(double vol, double sub, double prd) const
Definition: RateTerm.h:597
void setR2(double k2)
Used by Zombie to assign rate terms.
Definition: RateTerm.h:222
unsigned int y2_
Definition: RateTerm.h:424
unsigned int y_
Definition: RateTerm.h:350
void setRates(double k1, double k2)
Definition: RateTerm.h:274
RateTerm * copyWithVolScaling(double vol, double sub, double prd) const
Definition: RateTerm.h:161
vector< unsigned int > v_
Definition: RateTerm.h:512
RateTerm * copyWithVolScaling(double vol, double sub, double prd) const
Definition: RateTerm.h:343
double kcat_
Definition: RateTerm.h:134
unsigned int getReactants(vector< unsigned int > &molIndex) const
Definition: RateTerm.h:234
RateTerm * copyWithVolScaling(double vol, double sub, double prd) const
Definition: RateTerm.h:415
const unsigned int y_
Definition: RateTerm.h:469
SecondOrder(double k, unsigned int y1, unsigned int y2)
Definition: RateTerm.h:390
double operator()(const double *S) const
Computes the rate. The argument is the molecule array.
Definition: RateTerm.h:557
double getR1() const
Used by Zombie to return rate terms.
Definition: RateTerm.h:574
void setR2(double kb)
Used by Zombie to assign rate terms.
Definition: RateTerm.h:570
unsigned int getReactants(vector< unsigned int > &molIndex) const
Definition: RateTerm.h:294
void rescaleVolume(short comptIndex, const vector< short > &compartmentLookup, double ratio)
Definition: RateTerm.h:407
ZeroOrder * backward_
Definition: RateTerm.h:609
Definition: RateTerm.h:320
virtual void setR1(double k1)=0
Used by Zombie to assign rate terms.
RateTerm * copyWithVolScaling(double vol, double sub, double prd) const
Definition: RateTerm.h:305