MOOSE - Multiscale Object Oriented Simulation Environment
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
testKinetics.cpp
Go to the documentation of this file.
1 /**********************************************************************
2 ** This program is part of 'MOOSE', the
3 ** Messaging Object Oriented Simulation Environment.
4 ** Copyright (C) 2003-2010 Upinder S. Bhalla. and NCBS
5 ** It is made available under the terms of the
6 ** GNU Lesser General Public License version 2.1
7 ** See the file COPYING.LIB for the full notice.
8 **********************************************************************/
9 
10 #include "header.h"
11 #include "../shell/Shell.h"
12 #include "ReadKkit.h"
13 #include "ReadCspace.h"
14 #include "EnzBase.h"
15 #include "MMenz.h"
16 #include "ReacBase.h"
17 #include "Reac.h"
18 
20 {
21  ReadKkit rk;
22  // rk.read( "test.g", "dend", 0 );
23  Id base = rk.read( "foo.g", "dend", Id() );
24  assert( base != Id() );
25  // Id kinetics = s->doFind( "/kinetics" );
26 
27  Shell* s = reinterpret_cast< Shell* >( Id().eref().data() );
28  rk.run();
29  rk.dumpPlots( "dend.plot" );
30 
31  s->doDelete( base );
32  cout << "." << flush;
33 }
34 
35 bool isClose( double x, double y, double tol )
36 {
37  return ( fabs( x - y ) < tol * 1e-8 );
38 }
39 
41 {
42  Shell* shell = reinterpret_cast< Shell* >( Id().eref().data() );
43  Id comptId = shell->doCreate( "CylMesh", Id(), "cyl", 1 );
44  Id meshId( comptId.value() + 1 );
45  Id poolId = shell->doCreate( "Pool", comptId, "pool", 1 );
46 
47  ObjId mid = shell->doAddMsg( "OneToOne",
48  ObjId( poolId, 0 ), "requestVolume",
49  ObjId( meshId, 0 ), "get_volume" );
50 
51  assert( mid != ObjId() );
52 
53  vector< double > coords( 9, 0.0 );
54  double x1 = 100e-6;
55  double r0 = 10e-6;
56  double r1 = 5e-6;
57  double lambda = x1;
58  coords[3] = x1;
59  coords[6] = r0;
60  coords[7] = r1;
61  coords[8] = lambda;
62 
63  Field< vector< double > >::set( comptId, "coords", coords );
64 
65  double volume = Field< double >::get( poolId, "volume" );
66  assert( doubleEq( volume, PI * x1 * (r0+r1) * (r0+r1) / 4.0 ) );
67 
68  Field< double >::set( poolId, "n", 400 );
69  double volscale = 1 / ( NA * volume );
70  double conc = Field< double >::get( poolId, "conc" );
71  assert( doubleEq( conc, 400 * volscale ) );
72  Field< double >::set( poolId, "conc", 500 * volscale );
73  double n = Field< double >::get( poolId, "n" );
74  assert( doubleEq( n, 500 ) );
75 
76  Field< double >::set( poolId, "nInit", 650 );
77  double concInit = Field< double >::get( poolId, "concInit" );
78  assert( doubleEq( concInit, 650 * volscale ) );
79  Field< double >::set( poolId, "concInit", 10 * volscale );
80  n = Field< double >::get( poolId, "nInit" );
81  assert( doubleEq( n, 10 ) );
82 
83  shell->doDelete( comptId );
84  cout << "." << flush;
85 }
86 
88 {
89  Shell* shell = reinterpret_cast< Shell* >( Id().eref().data() );
90  Id comptId = shell->doCreate( "CubeMesh", Id(), "cube", 1 );
91  Id meshId( comptId.value() + 1 );
92  Id subId = shell->doCreate( "Pool", comptId, "sub", 1 );
93  Id prdId = shell->doCreate( "Pool", comptId, "prd", 1 );
94  Id reacId = shell->doCreate( "Reac", comptId, "reac", 1 );
95 
96  double vol1 = 1e-15;
97 
98  ObjId mid = shell->doAddMsg( "OneToOne",
99  subId, "requestVolume", meshId, "get_volume" );
100  assert( mid != ObjId() );
101  mid = shell->doAddMsg( "OneToOne",
102  prdId, "requestVolume", meshId, "get_volume" );
103  assert( mid != ObjId() );
104 
105  vector< double > coords( 9, 10.0e-6 );
106  coords[0] = coords[1] = coords[2] = 0;
107 
108  Field< vector< double > >::set( comptId, "coords", coords );
109 
110  double volume = Field< double >::get( comptId, "volume" );
111  assert( doubleEq( volume, vol1 ) );
112 
113  ObjId ret = shell->doAddMsg( "Single", reacId, "sub", subId, "reac" );
114  assert( ret != ObjId() );
115  ret = shell->doAddMsg( "Single", reacId, "prd", prdId, "reac" );
116  assert( ret != ObjId() );
117 
118  Field< double >::set( reacId, "Kf", 2 );
119  Field< double >::set( reacId, "Kb", 3 );
120  double x = Field< double >::get( reacId, "kf" );
121  assert( doubleEq( x, 2 ) );
122  x = Field< double >::get( reacId, "kb" );
123  assert( doubleEq( x, 3 ) );
124 
125  ret = shell->doAddMsg( "Single", reacId, "sub", subId, "reac" );
126  assert( ret != ObjId() );
127  double conv = 1.0 / ( NA * vol1 );
128  x = Field< double >::get( reacId, "kf" );
129  assert( doubleEq( x, 2 * conv ) );
130  x = Field< double >::get( reacId, "kb" );
131  assert( doubleEq( x, 3 ) );
132 
133  ret = shell->doAddMsg( "Single", reacId, "sub", subId, "reac" );
134  assert( ret != ObjId() );
135  ret = shell->doAddMsg( "Single", reacId, "prd", prdId, "reac" );
136  assert( ret != ObjId() );
137  x = Field< double >::get( reacId, "kf" );
138  assert( doubleEq( x, 2 * conv * conv ) );
139  x = Field< double >::get( reacId, "kb" );
140  assert( doubleEq( x, 3 * conv ) );
141 
142  shell->doDelete( comptId );
143  cout << "." << flush;
144 }
145 
146 // See what Element::getNeighbors does with 2 sub <----> prd.
148 {
149  Shell* shell = reinterpret_cast< Shell* >( Id().eref().data() );
150  Id comptId = shell->doCreate( "CubeMesh", Id(), "cube", 1 );
151  Id meshId( comptId.value() + 1 );
152  Id subId = shell->doCreate( "Pool", comptId, "sub", 1 );
153  Id prdId = shell->doCreate( "Pool", comptId, "prd", 1 );
154  Id reacId = shell->doCreate( "Reac", comptId, "reac", 1 );
155 
156  ObjId mid = shell->doAddMsg( "OneToOne",
157  subId, "requestVolume", meshId, "get_volume" );
158  assert( mid != ObjId() );
159  mid = shell->doAddMsg( "OneToOne",
160  prdId, "requestVolume", meshId, "get_volume" );
161  assert( mid != ObjId() );
162 
163  ObjId ret = shell->doAddMsg( "Single", reacId, "sub", subId, "reac" );
164  assert( ret != ObjId() );
165  ret = shell->doAddMsg( "Single", reacId, "sub", subId, "reac" );
166  assert( ret != ObjId() );
167 
168  ret = shell->doAddMsg( "Single", reacId, "prd", prdId, "reac" );
169  assert( ret != ObjId() );
170 
171  vector< Id > pools;
172  unsigned int num = reacId.element()->getNeighbors( pools,
173  Reac::initCinfo()->findFinfo( "toSub" ) );
174  assert( num == 2 );
175  assert( pools[0] == subId );
176  assert( pools[1] == subId );
177 
178  pools.clear();
179  num = reacId.element()->getNeighbors( pools,
180  Reac::initCinfo()->findFinfo( "sub" ) );
181  assert( num == 2 );
182  assert( pools[0] == subId );
183  assert( pools[1] == subId );
184 
185  shell->doDelete( comptId );
186  cout << "." << flush;
187 }
188 
189 
190 
192 {
193  ReadCspace rc;
194  rc.testReadModel();
195  cout << "." << flush;
196 }
197 
198 void testMMenz()
199 {
200  Shell* shell = reinterpret_cast< Shell* >( Id().eref().data() );
201  Id mmid = shell->doCreate( "MMenz", Id(), "mm", 1 ); // mmenz
202  MMenz m;
203  ProcInfo p;
204 
205  m.vSetKm( mmid.eref(), 5.0 );
206  m.vSetKcat( mmid.eref(), 4.0 );
207  m.vReinit( mmid.eref(), &p );
208  m.vSub( 2 );
209  m.vEnz( 3 );
210  assert( doubleEq( m.vGetKm( mmid.eref() ), 5.0 ) );
211  assert( doubleEq( m.vGetKcat( mmid.eref() ), 4.0 ) );
212  m.vProcess( mmid.eref(), &p );
213 
214  shell->doDelete( mmid );
215  cout << "." << flush;
216 }
217 
219 // The equation for conc of substrate of an MMenz reaction is a nasty
220 // transcendental, so here we just work backwards and estimate t from
221 // the substrate concentration
223 double estT( double s )
224 {
225  double E = 1.0;
226  double Km = 1.0;
227  double kcat = 1.0;
228  double s0 = 1.0;
229  double c = -Km * log( s0 ) - s0;
230  double t = (-1.0 /( E * kcat ) ) * ( ( Km * log( s ) + s ) + c );
231  return t;
232 }
233 
235 {
236  Shell* shell = reinterpret_cast< Shell* >( Id().eref().data() );
238  // This set is the test kinetic calculation using MathFunc
240  Id nid = shell->doCreate( "Neutral", Id(), "n", 1 );
242  // This set is the reference kinetic calculation using MMEnz
244  Id pid = shell->doCreate( "Pool", nid, "p", 1 ); // substrate
245  Id qid = shell->doCreate( "Pool", nid, "q", 1 ); // enz mol
246  Id rid = shell->doCreate( "Pool", nid, "r", 1 ); // product
247  Id mmid = shell->doCreate( "MMenz", nid, "mm", 1 ); // mmenz
248 
249  Id tabid2 = shell->doCreate( "Table", nid, "tab2", 1 ); //output plot
250 
251  Field< double >::set( mmid, "Km", 1.0 );
252  Field< double >::set( mmid, "kcat", 1.0 );
253  Field< double >::set( pid, "nInit", 1.0 );
254  Field< double >::set( qid, "nInit", 1.0 );
255  Field< double >::set( rid, "nInit", 0.0 );
256 
257  shell->doAddMsg( "Single", ObjId( mmid ), "sub", ObjId( pid ), "reac" );
258  shell->doAddMsg( "Single", ObjId( mmid ), "prd", ObjId( rid ), "reac" );
259  shell->doAddMsg( "Single", ObjId( qid ), "nOut", ObjId( mmid ), "enzDest" );
260  shell->doAddMsg( "Single", ObjId( pid ), "nOut", ObjId( tabid2 ), "input" );
261  shell->doSetClock( 0, 0.01 );
262  shell->doSetClock( 1, 0.01 );
263  shell->doUseClock( "/n/mm,/n/tab2", "process", 0 );
264  shell->doUseClock( "/n/#[ISA=Pool]", "process", 1 );
265 
267  // Now run models and compare outputs
269 
270  shell->doReinit();
271  shell->doStart( 10 );
272 
273  vector< double > vec = Field< vector< double > >::get( tabid2, "vec" );
274  assert( vec.size() == 1001 );
275  for ( unsigned int i = 0; i < vec.size(); ++i ) {
276  double t = 0.01 * i;
277  double et = estT( vec[i] );
278  assert( doubleApprox( t, et ) );
279  }
280 
281  shell->doDelete( nid );
282  cout << "." << flush;
283 }
284 
285 void testWriteKkit( Id id )
286 {
287  extern void writeKkit( Id model, const string& s );
288  writeKkit( id, "kkitWriteTest.g" );
289  cout << "." << flush;
290 }
291 
293 {
294  vector< unsigned int > findVolOrder( const vector< double >& vols );
295  vector< double > vols( 8 );
296  vols[0] = 7;
297  vols[1] = 8;
298  vols[2] = 6;
299  vols[3] = 5;
300  vols[4] = 1;
301  vols[5] = 2;
302  vols[6] = 3;
303  vols[7] = 4;
304  vector< unsigned int > order = findVolOrder( vols );
305  // The order is the rank of the volume entry, largest should be 0.
306  assert( order[0] == 1 );
307  assert( order[1] == 0 );
308  assert( order[2] == 2 );
309  assert( order[3] == 3 );
310  assert( order[4] == 7 );
311  assert( order[5] == 6 );
312  assert( order[6] == 5 );
313  assert( order[7] == 4 );
314 
315  // This is a sequence which failed in a model test, despite the
316  // above test working.
317  vols.resize(5);
318  vols[0] = 1e-15;
319  vols[1] = 3e-15;
320  vols[2] = -1;
321  vols[3] = 2e-15;
322  vols[4] = 5e-15;
323  order = findVolOrder( vols );
324  assert( order[0] == 4 );
325  assert( order[1] == 1 );
326  assert( order[2] == 3 );
327  assert( order[3] == 0 );
328  assert( order[4] == 2 );
329 }
330 
332 {
334  testMMenz();
337  testReadCspace();
338  testVolSort();
339 
340  // This is now handled with real models in the regression tests.
341  // testWriteKkit( Id() );
342 }
343 
345 {
346 }
347 
349 {
351 }
double vGetKm(const Eref &e) const
Definition: MMenz.cpp:115
void doStart(double runtime, bool notify=false)
Definition: Shell.cpp:332
void vSetKm(const Eref &e, double v)
Definition: MMenz.cpp:108
char * data() const
Definition: Eref.cpp:41
void testMMenz()
void testMpiKinetics()
const double NA
Definition: consts.cpp:15
Element * element() const
Synonym for Id::operator()()
Definition: Id.cpp:113
void doSetClock(unsigned int tickNum, double dt)
Definition: Shell.cpp:377
unsigned int value() const
Definition: Id.cpp:197
Definition: SetGet.h:236
void testReacVolumeScaling()
void dumpPlots(const string &filename)
Definition: ReadKkit.cpp:347
Definition: ObjId.h:20
Eref eref() const
Definition: Id.cpp:125
void testTwoReacGetNeighbors()
static bool set(const ObjId &dest, const string &field, A arg)
Definition: SetGet.h:245
void testPoolVolumeScaling()
void vProcess(const Eref &e, ProcPtr p)
Definition: MMenz.cpp:82
Id doCreate(string type, ObjId parent, string name, unsigned int numData, NodePolicy nodePolicy=MooseBlockBalance, unsigned int preferredNode=1)
Definition: Shell.cpp:181
void log(string msg, serverity_level_ type=debug, bool redirectToConsole=true, bool removeTicks=true)
Log to console (and to a log-file)
bool doubleApprox(double x, double y)
Definition: doubleEq.cpp:24
void testMMenzProcess()
bool doubleEq(double x, double y)
Definition: doubleEq.cpp:16
static SrcFinfo0 s0("s0","")
void testVolSort()
void writeKkit(Id model, const string &fname)
Definition: WriteKkit.cpp:525
vector< unsigned int > findVolOrder(const vector< double > &vols)
Definition: ReadKkit.cpp:723
bool isClose(double x, double y, double tol)
void testKineticsProcess()
static const Cinfo * initCinfo()
Definition: Reac.cpp:18
Id read(const string &filename, const string &cellname, Id parent, const string &solverClass="Stoich")
Definition: ReadKkit.cpp:276
void testWriteKkit(Id id)
void doReinit()
Definition: Shell.cpp:362
void vSub(double n)
Definition: MMenz.cpp:72
double vGetKcat(const Eref &e) const
Definition: MMenz.cpp:141
void testReadModel()
Definition: ReadCspace.cpp:361
ObjId doAddMsg(const string &msgType, ObjId src, const string &srcField, ObjId dest, const string &destField)
Definition: Shell.cpp:269
void vSetKcat(const Eref &e, double v)
Definition: MMenz.cpp:134
void vEnz(double n)
Definition: MMenz.cpp:77
const double PI
Definition: consts.cpp:12
bool doDelete(ObjId oid)
Definition: Shell.cpp:259
Definition: Id.h:17
void run()
Definition: ReadKkit.cpp:321
unsigned int getNeighbors(vector< Id > &ret, const Finfo *finfo) const
Definition: Element.cpp:949
void doUseClock(string path, string field, unsigned int tick)
Definition: Shell.cpp:382
static A get(const ObjId &dest, const string &field)
Definition: SetGet.h:284
void testReadCspace()
void testReadKkit()
double estT(double s)
void vReinit(const Eref &e, ProcPtr p)
Definition: MMenz.cpp:91
void testKinetics()
Definition: MMenz.h:18
Definition: Shell.h:43