99#include < sys/timeb.h>
1010#include < sstream>
1111#include < string>
12-
13- #ifdef USE_MESSAGING
14- #include < VCELL/SimulationMessaging.h>
15- #endif
16-
1712#include < memory.h>
18-
1913#include < cvode/cvode.h> /* prototypes for CVODE fcts. and consts. */
2014#include < nvector/nvector_serial.h> /* serial N_Vector types, fcts., and macros */
2115#include < cvode/cvode_dense.h> /* prototype for CVDense */
2216// #include <cvode/cvode_spgmr.h> /* prototype for CVSPGMR */
2317#include < sundials/sundials_dense.h> /* definitions DenseMat DENSE_ELEM */
2418#include < sundials/sundials_types.h> /* definition of type realtype */
25-
19+ #ifdef USE_MESSAGING
20+ #include < VCELL/SimulationMessaging.h>
21+ #endif
2622#define ToleranceType CV_SS
2723
2824/* *
@@ -151,13 +147,13 @@ VCellCVodeSolver::VCellCVodeSolver() : VCellSundialsSolver() {
151147VCellCVodeSolver::~VCellCVodeSolver () {
152148 CVodeFree (&solver);
153149
154- for (int i = 0 ; i < NEQ ; i ++) {
150+ for (int i = 0 ; i < NUM_EQUATIONS ; i ++) {
155151 delete rateExpressions[i];
156152 }
157153 delete[] rateExpressions;
158154}
159155
160- /*
156+ /* *----------------------------------------------------
161157Input format:
162158 STARTING_TIME 0.0
163159 ENDING_TIME 0.1
@@ -172,15 +168,15 @@ Input format:
172168 RATE ((20.0 * x_o * D_B0) - (50.0 * x_i));
173169 ODE x_o INIT 0.0;
174170 RATE ( - ((20.0 * x_o * D_B0) - (50.0 * x_i)) + (1505000.0 * (3.322259136212625E-4 - (3.322259136212625E-4 * x_o) - (3.322259136212625E-4 * x_i))) - (100.0 * x_o));
175- */
171+ -------------------------------------------------------------- */
176172void VCellCVodeSolver::readEquations (std::istream& inputstream) {
177173 try {
178174 std::string name;
179175 std::string exp;
180176
181- rateExpressions = new Expression*[NEQ];
177+ rateExpressions = new Expression*[NUM_EQUATIONS];
182178
183- for (int i = 0 ; i < NEQ ; i ++) {
179+ for (int i = 0 ; i < NUM_EQUATIONS ; i ++) {
184180 // ODE
185181 inputstream >> name >> variableNames[i];
186182
@@ -209,11 +205,35 @@ void VCellCVodeSolver::readEquations(std::istream& inputstream) {
209205 }
210206}
211207
208+ void VCellCVodeSolver::readEquations (VCellSolverInputBreakdown& inputBreakdown) {
209+ try {
210+ for (int i = 0 ; i < NUM_EQUATIONS; i ++) {
211+ variableNames[i] = inputBreakdown.modelSettings .VARIABLE_NAMES [i];
212+ try {
213+ initialConditionExpressions[i] = new Expression (inputBreakdown.modelSettings .INITIAL_CONDITION_EXPRESSIONS [i]);
214+ } catch (VCell::Exception& ex) {
215+ throw VCell::Exception (std::string (" Initial condition expression for [" ) + variableNames[i] + " ] " + ex.getMessage ());
216+ }
217+ }
218+
219+ rateExpressions = new Expression*[inputBreakdown.modelSettings .RATE_EXPRESSIONS .size ()];
220+ for (int i = 0 ; i < inputBreakdown.modelSettings .RATE_EXPRESSIONS .size (); i++) {
221+ rateExpressions[i] = new Expression (inputBreakdown.modelSettings .RATE_EXPRESSIONS [i]);
222+ }
223+ } catch (char * ex) {
224+ throw VCell::Exception (std::string (" VCellCVodeSolver::readInput() : " ) + ex);
225+ } catch (VCell::Exception& ex) {
226+ throw VCell::Exception (std::string (" VCellCVodeSolver::readInput() : " ) + ex.getMessage ());
227+ } catch (...) {
228+ throw " VCellCVodeSolver::readInput() : caught unknown exception" ;
229+ }
230+ }
231+
212232void VCellCVodeSolver::initialize () {
213233 VCellSundialsSolver::initialize ();
214234
215235 // rate can be function of variables, parameters and discontinuities.
216- for (int i = 0 ; i < NEQ ; i ++) {
236+ for (int i = 0 ; i < NUM_EQUATIONS ; i ++) {
217237 rateExpressions[i]->bindExpression (defaultSymbolTable);
218238 }
219239}
@@ -222,7 +242,7 @@ int VCellCVodeSolver::RHS (realtype t, N_Vector y, N_Vector r) {
222242 try {
223243 updateTandVariableValues (t, y);
224244 double * r_data = NV_DATA_S (r);
225- for (int i = 0 ; i < NEQ ; i ++) {
245+ for (int i = 0 ; i < NUM_EQUATIONS ; i ++) {
226246 r_data[i] = rateExpressions[i]->evaluateVector (values);
227247 }
228248 recoverableErrMsg = " " ;
@@ -264,17 +284,17 @@ void VCellCVodeSolver::solve(double* paramValues, bool bPrintProgress, FILE* out
264284 this ->odeResultSet ->clearData ();
265285
266286 // copy parameter values to the end of values, these will stay the same during solving
267- memset (values, 0 , (NEQ + 1 ) * sizeof (double ));
268- memcpy (values + 1 + NEQ , paramValues, NPARAM * sizeof (double ));
269- memset (values + 1 + NEQ + NPARAM , 0 , numDiscontinuities * sizeof (double ));
287+ memset (values, 0 , (NUM_EQUATIONS + 1 ) * sizeof (double ));
288+ memcpy (values + 1 + NUM_EQUATIONS , paramValues, NUM_PARAMETERS * sizeof (double ));
289+ memset (values + 1 + NUM_EQUATIONS + NUM_PARAMETERS , 0 , numDiscontinuities * sizeof (double ));
270290
271291 initCVode (paramValues);
272292 cvodeSolve (bPrintProgress, outputFile, checkStopRequested);
273293}
274294
275295void VCellCVodeSolver::initCVode (double * paramValues) {
276296 // Initialize y, variable portion of values
277- for (int i = 0 ; i < NEQ ; i ++) {
297+ for (int i = 0 ; i < NUM_EQUATIONS ; i ++) {
278298 NV_Ith_S (y, i) = initialConditionExpressions[i]->evaluateVector (paramValues);
279299 }
280300
@@ -305,7 +325,7 @@ void VCellCVodeSolver::reInit(double t) {
305325
306326 flag = CVodeSetFdata (solver, this );
307327 checkCVodeFlag (flag);
308- flag = CVDense (solver, NEQ );
328+ flag = CVDense (solver, NUM_EQUATIONS );
309329 // flag = CVSpgmr(solver, PREC_NONE, 0);
310330 checkCVodeFlag (flag);
311331
@@ -317,8 +337,8 @@ void VCellCVodeSolver::reInit(double t) {
317337}
318338
319339bool VCellCVodeSolver::fixInitialDiscontinuities (double t) {
320- double * oldy = new double [NEQ ];
321- memcpy (oldy, NV_DATA_S (y), NEQ * sizeof (realtype));
340+ double * oldy = new double [NUM_EQUATIONS ];
341+ memcpy (oldy, NV_DATA_S (y), NUM_EQUATIONS * sizeof (realtype));
322342
323343 double epsilon = std::max (1e-15 , ENDING_TIME * 1e-10 );
324344 double currentTime = t;
@@ -341,11 +361,11 @@ bool VCellCVodeSolver::fixInitialDiscontinuities(double t) {
341361 }
342362 }
343363 if (bInitChanged) {
344- memcpy (values + 1 + NEQ + NPARAM , discontinuityValues, numDiscontinuities * sizeof (double ));
364+ memcpy (values + 1 + NUM_EQUATIONS + NUM_PARAMETERS , discontinuityValues, numDiscontinuities * sizeof (double ));
345365 }
346366
347367 // revert y
348- memcpy (NV_DATA_S (y), oldy, NEQ * sizeof (realtype));
368+ memcpy (NV_DATA_S (y), oldy, NUM_EQUATIONS * sizeof (realtype));
349369 reInit (t);
350370
351371 delete[] oldy;
@@ -427,7 +447,7 @@ void VCellCVodeSolver::cvodeSolve(bool bPrintProgress, FILE* outputFile, void (*
427447
428448 if (returnCode == CV_ROOT_RETURN || iterationCount % keepEvery == 0 || Time >= ENDING_TIME){
429449 outputCount++;
430- if (outputCount * (NEQ + 1 ) * bytesPerSample > MaxFileSizeBytes){
450+ if (outputCount * (NUM_EQUATIONS + 1 ) * bytesPerSample > MaxFileSizeBytes){
431451 /* if more than one gigabyte, then fail */
432452 const std::string msg{" output exceeded maximum " + std::to_string (MaxFileSizeBytes) + " bytes" };
433453 throw VCell::Exception (msg.c_str ());
@@ -487,5 +507,5 @@ void VCellCVodeSolver::cvodeSolve(bool bPrintProgress, FILE* outputFile, void (*
487507
488508void VCellCVodeSolver::updateTandVariableValues (realtype t, N_Vector y) {
489509 values[0 ] = t;
490- memcpy (values + 1 , NV_DATA_S (y), NEQ * sizeof (realtype));
510+ memcpy (values + 1 , NV_DATA_S (y), NUM_EQUATIONS * sizeof (realtype));
491511}
0 commit comments