MOOSE - Multiscale Object Oriented Simulation Environment
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
testKsolve.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-2014 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 #include "header.h"
10 #include "../shell/Shell.h"
11 #include "RateTerm.h"
12 #include "muParser.h"
13 #include "FuncTerm.h"
14 #include "SparseMatrix.h"
15 #include "KinSparseMatrix.h"
16 #include "VoxelPoolsBase.h"
17 #include "../mesh/VoxelJunction.h"
18 #include "XferInfo.h"
19 #include "ZombiePoolInterface.h"
20 #include "Stoich.h"
21 
35 {
36  double simDt = 0.1;
37  double plotDt = 0.1;
38  Shell* s = reinterpret_cast< Shell* >( Id().eref().data() );
39 
40  Id pools[10];
41  unsigned int i = 0;
42  // Make the objects.
43  Id kin = s->doCreate( "CubeMesh", Id(), "kinetics", 1 );
44  Id tab = s->doCreate( "StimulusTable", kin, "tab", 1 );
45  Id T = pools[i++] = s->doCreate( "BufPool", kin, "T", 1 );
46  Id A = pools[i++] = s->doCreate( "Pool", kin, "A", 1 );
47  Id B = pools[i++] = s->doCreate( "Pool", kin, "B", 1 );
48  Id C = pools[i++] = s->doCreate( "Pool", kin, "C", 1 );
49  Id D = pools[i++] = s->doCreate( "Pool", kin, "D", 1 );
50  Id E = pools[i++] = s->doCreate( "Pool", kin, "E", 1 );
51  Id tot1 = pools[i++] = s->doCreate( "BufPool", kin, "tot1", 1 );
52  Id sum = s->doCreate( "Function", tot1, "func", 1 ); // Silly that it has to have this name.
53  Id sumInput( sum.value() + 1 );
54  Id e1Pool = s->doCreate( "Pool", kin, "e1Pool", 1 );
55  Id e2Pool = s->doCreate( "Pool", kin, "e2Pool", 1 );
56  Id e1 = s->doCreate( "Enz", e1Pool, "e1", 1 );
57  Id cplx = s->doCreate( "Pool", e1, "cplx", 1 );
58  Id e2 = s->doCreate( "MMenz", e2Pool, "e2", 1 );
59  Id r1 = s->doCreate( "Reac", kin, "r1", 1 );
60  Id r2 = s->doCreate( "Reac", kin, "r2", 1 );
61  Id plots = s->doCreate( "Table2", kin, "plots", 7 );
62 
63  // Connect them up
64  s->doAddMsg( "Single", tab, "output", T, "setN" );
65 
66  s->doAddMsg( "Single", r1, "sub", T, "reac" );
67  s->doAddMsg( "Single", r1, "sub", A, "reac" );
68  s->doAddMsg( "Single", r1, "prd", B, "reac" );
69 
70  Field< unsigned int >::set( sum, "numVars", 2 );
71  s->doAddMsg( "Single", A, "nOut", ObjId( sumInput, 0, 0 ), "input" );
72  s->doAddMsg( "Single", B, "nOut", ObjId( sumInput, 0, 1 ), "input" );
73  s->doAddMsg( "Single", sum, "valueOut", tot1, "setN" );
74 
75  s->doAddMsg( "Single", r2, "sub", B, "reac" );
76  s->doAddMsg( "Single", r2, "sub", B, "reac" );
77  s->doAddMsg( "Single", r2, "prd", C, "reac" );
78 
79  s->doAddMsg( "Single", e1, "sub", C, "reac" );
80  s->doAddMsg( "Single", e1, "enz", e1Pool, "reac" );
81  s->doAddMsg( "Single", e1, "cplx", cplx, "reac" );
82  s->doAddMsg( "Single", e1, "prd", D, "reac" );
83 
84  s->doAddMsg( "Single", e2, "sub", D, "reac" );
85  s->doAddMsg( "Single", e2Pool, "nOut", e2, "enzDest" );
86  s->doAddMsg( "Single", e2, "prd", E, "reac" );
87 
88  // Set parameters.
89  Field< double >::set( A, "concInit", 2 );
90  Field< double >::set( e1Pool, "concInit", 1 );
91  Field< double >::set( e2Pool, "concInit", 1 );
92  Field< string >::set( sum, "expr", "x0+x1" );
93  Field< double >::set( r1, "Kf", 0.2 );
94  Field< double >::set( r1, "Kb", 0.1 );
95  Field< double >::set( r2, "Kf", 0.1 );
96  Field< double >::set( r2, "Kb", 0.0 );
97  Field< double >::set( e1, "Km", 5 );
98  Field< double >::set( e1, "kcat", 1 );
99  Field< double >::set( e1, "ratio", 4 );
100  Field< double >::set( e2, "Km", 5 );
101  Field< double >::set( e2, "kcat", 1 );
102  vector< double > stim( 100, 0.0 );
103  double vol = Field< double >::get( kin, "volume" );
104  for ( unsigned int i = 0; i< 100; ++i ) {
105  stim[i] = vol * NA * (1.0 + sin( i * 2.0 * PI / 100.0 ) );
106  }
107  Field< vector< double > >::set( tab, "vector", stim );
108  Field< double >::set( tab, "stepSize", 0.0 );
109  Field< double >::set( tab, "stopTime", 10.0 );
110  Field< double >::set( tab, "loopTime", 10.0 );
111  Field< bool >::set( tab, "doLoop", true );
112 
113 
114  // Connect outputs
115  for ( unsigned int i = 0; i < 7; ++i )
116  s->doAddMsg( "Single", ObjId( plots,i),
117  "requestOut", pools[i], "getConc" );
118 
119  // Schedule it.
120  for ( unsigned int i = 11; i < 18; ++i )
121  s->doSetClock( i, simDt );
122  s->doSetClock( 18, plotDt );
123  /*
124  s->doUseClock( "/kinetics/##[ISA=Reac],/kinetics/##[ISA=EnzBase],/kinetics/##[ISA=SumFunc]",
125  "process", 4 );
126  s->doUseClock( "/kinetics/##[ISA=PoolBase]", "process", 5 );
127  s->doUseClock( "/kinetics/##[ISA=StimulusTable]",
128  "process", 4 );
129  s->doUseClock( "/kinetics/##[ISA=Table]", "process", 8 );
130  s->doSetClock( 4, simDt );
131  s->doSetClock( 5, simDt );
132  s->doSetClock( 8, plotDt );
133  */
134  return kin;
135 }
136 
138 {
139  Shell* s = reinterpret_cast< Shell* >( Id().eref().data() );
140  Id kin = makeReacTest();
141  s->doReinit();
142  s->doStart( 20.0 );
143  Id plots( "/kinetics/plots" );
144  /*
145  for ( unsigned int i = 0; i < 7; ++i ) {
146  stringstream ss;
147  ss << "plot." << i;
148  SetGet2< string, string >::set( ObjId( plots, i ), "xplot",
149  "tsr.plot", ss.str() );
150  }
151  */
152  s->doDelete( kin );
153  cout << "." << flush;
154 }
155 
157 {
158  // In the updated version, we have reordered all pools and reacs by
159  // Id number, modulo varPools then bufPOols.
160  // Matrix looks like:
161  // Reac Name R1 R2 e1a e1b e2
162  // MolName
163  // D -1 0 0 0 0
164  // A -1 0 0 0 0
165  // B +1 -2 0 0 0
166  // C 0 +1 -1 0 0
167  // enz1 0 0 -1 +1 0
168  // e1cplx 0 0 +1 -1 0
169  // E 0 0 0 +1 -1
170  // F 0 0 0 0 +1
171  // enz2 0 0 0 0 0
172  // tot1 0 0 0 0 0
173  //
174  // This has been shuffled to:
175  // A -1 0 0 0 0
176  // B +1 -2 0 0 0
177  // C 0 +1 -1 0 0
178  // E 0 0 0 +1 -1
179  // F 0 0 0 0 +1
180  // enz1 0 0 -1 +1 0
181  // enz2 0 0 0 0 0
182  // e1cplx 0 0 +1 -1 0
183  // D -1 0 0 0 0
184  // tot1 0 0 0 0 0
185  //
186  // (This is the output of the print command on the sparse matrix.)
187  //
188  Shell* s = reinterpret_cast< Shell* >( Id().eref().data() );
189  Id kin = makeReacTest();
190  Id ksolve = s->doCreate( "Ksolve", kin, "ksolve", 1 );
191  Id stoich = s->doCreate( "Stoich", ksolve, "stoich", 1 );
192  Field< Id >::set( stoich, "compartment", kin );
193  Field< Id >::set( stoich, "ksolve", ksolve );
194 
195  // Used to get at the stoich matrix from gdb.
196 #ifndef NDEBUG
197  Stoich* stoichPtr = reinterpret_cast< Stoich* >( stoich.eref().data() );
198 #endif
199 
200  Field< string >::set( stoich, "path", "/kinetics/##" );
201 
202  unsigned int n = Field< unsigned int >::get( stoich, "numAllPools" );
203  assert( n == 10 );
204  unsigned int r = Field< unsigned int >::get( stoich, "numRates" );
205  assert( r == 5 ); // One each for reacs and MMenz, two for Enz.
206 
207  vector< int > entries = Field< vector< int > >::get( stoich,
208  "matrixEntry" );
209  vector< unsigned int > colIndex = Field< vector< unsigned int > >::get(
210  stoich, "columnIndex" );
211  vector< unsigned int > rowStart = Field< vector< unsigned int > >::get(
212  stoich, "rowStart" );
213 
214  assert( rowStart.size() == n + 1 );
215  assert( entries.size() == colIndex.size() );
216  assert( entries.size() == 13 );
217  assert( entries[0] == -1 );
218  assert( entries[1] == 1 );
219  assert( entries[2] == -2 );
220  assert( entries[3] == 1 );
221  assert( entries[4] == -1 );
222  assert( entries[5] == 1 );
223  assert( entries[6] == -1 );
224  assert( entries[7] == 1 );
225  assert( entries[8] == -1 );
226  assert( entries[9] == 1 );
227  assert( entries[10] == 1 );
228  assert( entries[11] == -1 );
229  assert( entries[12] == -1 );
230 
231  s->doDelete( kin );
232  cout << "." << flush;
233 }
234 
236 {
237  double simDt = 0.1;
238  // double plotDt = 0.1;
239  Shell* s = reinterpret_cast< Shell* >( Id().eref().data() );
240  Id kin = makeReacTest();
241  Id ksolve = s->doCreate( "Ksolve", kin, "ksolve", 1 );
242  Id stoich = s->doCreate( "Stoich", ksolve, "stoich", 1 );
243  Field< Id >::set( stoich, "compartment", kin );
244  Field< Id >::set( stoich, "ksolve", ksolve );
245  Field< string >::set( stoich, "path", "/kinetics/##" );
246  s->doUseClock( "/kinetics/ksolve", "process", 4 );
247  s->doSetClock( 4, simDt );
248 
249  s->doReinit();
250  s->doStart( 20.0 );
251  Id plots( "/kinetics/plots" );
252  for ( unsigned int i = 0; i < 7; ++i ) {
253  stringstream ss;
254  ss << "plot." << i;
255  SetGet2< string, string >::set( ObjId( plots, i ), "xplot",
256  "tsr2.plot", ss.str() );
257  }
258  s->doDelete( kin );
259  cout << "." << flush;
260 }
261 
263 {
264  double simDt = 0.1;
265  // double plotDt = 0.1;
266  Shell* s = reinterpret_cast< Shell* >( Id().eref().data() );
267  Id kin = makeReacTest();
268  double volume = 1e-21;
269  Field< double >::set( kin, "volume", volume );
270  Field< double >::set( ObjId( "/kinetics/A" ), "concInit", 2 );
271  Field< double >::set( ObjId( "/kinetics/e1Pool" ), "concInit", 1 );
272  Field< double >::set( ObjId( "/kinetics/e2Pool" ), "concInit", 1 );
273  Id e1( "/kinetics/e1Pool/e1" );
274  Field< double >::set( e1, "Km", 5 );
275  Field< double >::set( e1, "kcat", 1 );
276  vector< double > stim( 100, 0.0 );
277  for ( unsigned int i = 0; i< 100; ++i ) {
278  stim[i] = volume * NA * (1.0 + sin( i * 2.0 * PI / 100.0 ) );
279  }
280  Field< vector< double > >::set( ObjId( "/kinetics/tab" ), "vector", stim );
281 
282 
283  Id gsolve = s->doCreate( "Gsolve", kin, "gsolve", 1 );
284  Id stoich = s->doCreate( "Stoich", gsolve, "stoich", 1 );
285  Field< Id >::set( stoich, "compartment", kin );
286  Field< Id >::set( stoich, "ksolve", gsolve );
287 
288  Field< string >::set( stoich, "path", "/kinetics/##" );
289  s->doUseClock( "/kinetics/gsolve", "process", 4 );
290  s->doSetClock( 4, simDt );
291 
292  s->doReinit();
293  s->doStart( 20.0 );
294  Id plots( "/kinetics/plots" );
295  for ( unsigned int i = 0; i < 7; ++i ) {
296  stringstream ss;
297  ss << "plot." << i;
298  SetGet2< string, string >::set( ObjId( plots, i ), "xplot",
299  "tsr3.plot", ss.str() );
300  }
301  s->doDelete( kin );
302  cout << "." << flush;
303 }
304 
306 {
307  FuncTerm ft;
308  ft.setExpr( "x0 + x1 * t" );
309  double args[] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
310 
311  // First check that it doesn't die even if we forget to set up anything.
312  double ans = ft( args, 2.0 );
313 
314  vector< unsigned int > mol( 2, 0 );
315  mol[0] = 2;
316  mol[1] = 0;
317  ft.setReactantIndex( mol );
318 
319  ans = ft( args, 10.0 );
320  assert( doubleEq( ans, 13.0 ) );
321  mol[0] = 0;
322  mol[1] = 9;
323  ft.setReactantIndex( mol );
324  ans = ft( args, 2.0 );
325  assert( doubleEq( ans, 21.0 ) );
326  cout << "." << flush;
327 }
328 
330 {
331  testSetupReac();
332  testBuildStoich();
333  testRunKsolve();
334  testRunGsolve();
335  testFuncTerm();
336 }
337 
339 {
340 ;
341 }
void setExpr(const string &e)
Definition: FuncTerm.cpp:83
void testBuildStoich()
Definition: testKsolve.cpp:156
void doStart(double runtime, bool notify=false)
Definition: Shell.cpp:332
char * data() const
Definition: Eref.cpp:41
const double NA
Definition: consts.cpp:15
void testFuncTerm()
Definition: testKsolve.cpp:305
void doSetClock(unsigned int tickNum, double dt)
Definition: Shell.cpp:377
unsigned int value() const
Definition: Id.cpp:197
void testKsolveProcess()
Definition: testKsolve.cpp:338
Definition: SetGet.h:236
void testRunGsolve()
Definition: testKsolve.cpp:262
void setReactantIndex(const vector< unsigned int > &mol)
Definition: FuncTerm.cpp:47
void testSetupReac()
Definition: testKsolve.cpp:137
void testKsolve()
Definition: testKsolve.cpp:329
Id makeReacTest()
Definition: testKsolve.cpp:34
void testRunKsolve()
Definition: testKsolve.cpp:235
Definition: ObjId.h:20
Eref eref() const
Definition: Id.cpp:125
static bool set(const ObjId &dest, const string &field, A arg)
Definition: SetGet.h:245
Id doCreate(string type, ObjId parent, string name, unsigned int numData, NodePolicy nodePolicy=MooseBlockBalance, unsigned int preferredNode=1)
Definition: Shell.cpp:181
Definition: Stoich.h:49
bool doubleEq(double x, double y)
Definition: doubleEq.cpp:16
void doReinit()
Definition: Shell.cpp:362
ObjId doAddMsg(const string &msgType, ObjId src, const string &srcField, ObjId dest, const string &destField)
Definition: Shell.cpp:269
const double PI
Definition: consts.cpp:12
bool doDelete(ObjId oid)
Definition: Shell.cpp:259
Definition: Id.h:17
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
Definition: Shell.h:43
static bool set(const ObjId &dest, const string &field, A1 arg1, A2 arg2)
Definition: SetGet.h:365