COPASI API  4.16.103
CODEExporter.cpp
Go to the documentation of this file.
1 // Copyright (C) 2010 - 2013 by Pedro Mendes, Virginia Tech Intellectual
2 // Properties, Inc., University of Heidelberg, and The University
3 // of Manchester.
4 // All rights reserved.
5 
6 // Copyright (C) 2008 - 2009 by Pedro Mendes, Virginia Tech Intellectual
7 // Properties, Inc., EML Research, gGmbH, University of Heidelberg,
8 // and The University of Manchester.
9 // All rights reserved.
10 
11 // Copyright (C) 2007 by Pedro Mendes, Virginia Tech Intellectual
12 // Properties, Inc. and EML Research, gGmbH.
13 // All rights reserved.
14 
15 #include <locale>
16 #include <cmath>
17 #include <iostream>
18 #include <fstream>
19 #include <sstream>
20 #include <ctype.h>
21 
22 #include "copasi.h"
23 
26 
27 #include "CODEExporter.h"
28 
29 #include "model/CModel.h"
30 #include "model/CMetab.h"
33 #include "model/CReaction.h"
34 #include "model/CMoiety.h"
35 #include "model/CChemEqElement.h"
36 #include "function/CFunction.h"
37 #include "function/CExpression.h"
38 #include "function/CFunctionDB.h"
39 #include "report/CKeyFactory.h"
45 #include "utilities/CCopasiTree.h"
48 
51 
52 /**
53  ** Constructor for the exporter.
54  */
56  : mExportedFunctions()
57 {}
58 
59 /**
60  ** Destructor for the exporter.
61  */
63 {}
64 
66 {
68 }
69 
71 {
72  if (pNode == NULL) return;
73 
76 
77  while (treeIt != NULL)
78  {
79  if (CEvaluationNode::type(treeIt->getType()) == CEvaluationNode::CALL)
80  {
81  const CFunction* ifunc;
82  ifunc = static_cast<CFunction*>(pFunctionDB->findFunction((*treeIt).getData()));
83 
84  findFunctionsCalls(ifunc->getRoot());
85 
86  if (ifunc->getType() != CEvaluationTree::MassAction)
87  {
88  if (!exportSingleFunction(ifunc)) return;
89  }
90  }
91 
92  ++treeIt;
93  }
94 }
95 
97 {
98  size_t i, size = copasiModel->getNumModelValues();
99 
100  for (i = 0; i < size; ++i)
101  {
102  CModelEntity* entity = copasiModel->getModelValues()[i];
103 
104  if (entity->getStatus() == CModelEntity::ASSIGNMENT)
105  {
106  if (entity->getExpressionPtr() == NULL || entity->getExpressionPtr()->getRoot() == NULL)
107  continue;
108 
111  }
112  }
113 
114  size = copasiModel->getNumMetabs();
115 
116  for (i = 0; i < size; ++i)
117  {
118  CModelEntity* entity = copasiModel->getMetabolites()[i];
119 
120  if (entity->getStatus() == CModelEntity::ASSIGNMENT)
121  {
122  if (entity->getExpressionPtr() == NULL || entity->getExpressionPtr()->getRoot() == NULL)
123  continue;
124 
127  }
128  }
129 
130  size = copasiModel->getCompartments().size();
131 
132  for (i = 0; i < size; ++i)
133  {
134  CModelEntity* entity = copasiModel->getCompartments()[i];
135 
136  if (entity->getStatus() == CModelEntity::ASSIGNMENT)
137  {
138  if (entity->getExpressionPtr() == NULL || entity->getExpressionPtr()->getRoot() == NULL)
139  continue;
140 
143  }
144  }
145 
146  return true;
147 }
148 
149 bool CODEExporter::exportSingleFunction(const CFunction *func, std::set<std::string>& isExported)
150 {
151  return true;
152 }
153 
154 bool CODEExporter::exportToStream(const CCopasiDataModel* pDataModel, std::ostream & os)
155 {
156  mExportedFunctions.clear();
157 
158  /* translate COPASI data names in exporter syntax */
159  if (!preprocess(pDataModel->getModel())) return false;
160 
161  /* export COPASI data */
162 
163  if (!exportMetabolites(pDataModel->getModel())) return false;
164 
165  if (!exportCompartments(pDataModel->getModel())) return false;
166 
167  if (!exportModelValues(pDataModel->getModel())) return false;
168 
169  if (!exportReacParamsAndFuncs(pDataModel->getModel())) return false;
170 
171  if (!exportKineticFunctionGroup(pDataModel->getModel())) return false;
172 
173  if (!exportODEs(pDataModel->getModel())) return false;
174 
175  if (!exportModelValuesExpressions(pDataModel->getModel())) return false;
176 
177  exportObjectNodesFromModel(pDataModel);
178 
179  if (!exportMetabolitesConcentrations(pDataModel->getModel())) return false;
180 
181  // export title data, so that a list of additionally created entities can be exported.
182  // NOTE: this does not change the order in the file
183  if (!exportTitleData(pDataModel->getModel(), os)) return false;
184 
185  os << std::endl << exportTitleString(INITIAL) << std::endl << initial.str() << exportClosingString(INITIAL);
186  os << std::endl << exportTitleString(FIXED) << std::endl << fixed.str() << exportClosingString(FIXED);
187  os << std::endl << exportTitleString(ASSIGNMENT) << std::endl << assignment.str() << exportClosingString(ASSIGNMENT);
188  os << std::endl << exportTitleString(HEADERS) << std::endl << headers.str() << exportClosingString(HEADERS);
189  os << std::endl << exportTitleString(FUNCTIONS) << std::endl << functions.str() << exportClosingString(FUNCTIONS);
190  os << std::endl << exportTitleString(ODEs) << std::endl << ode.str() << exportClosingString(ODEs);
191 
192  if (!exportClosingData(pDataModel->getModel(), os)) return false;
193 
194  return true;
195 }
196 
198 {
199 
200  size_t i, imax;
201 
202  imax = pDataModel->getModel()->getListOfSimulatedRefreshes().size();
203 
204  for (i = 0; i < imax; ++i)
205  {
206  CCopasiObject * obj = findObjectFromRefresh(pDataModel, pDataModel->getModel()->getListOfSimulatedRefreshes()[i]);
207 
208  if (obj) exportSimulatedObject(obj, pDataModel);
209  }
210 }
211 
213 {
214 
215  CCopasiObject* obj = NULL;
216 
217  if (tmp->isContainer())
218  {
219 
220  CCopasiContainer* container;
221  container = (CCopasiContainer*)tmp;
222 
223  CCopasiContainer::objectMap::const_iterator it = container->getObjects().begin();
224 
225  for (; it != container->getObjects().end(); ++it)
226  {
227 
228  //skip if the contained object is not owned by this container
229 
230  if (it->second->getObjectParent() != container) continue;
231 
232  if (it->second->getRefresh() == ref)
233  {
234  obj = it->second;
235 
236  return obj;
237  }
238 
239  //the next line skips name references...
240 
241  if (it->second->getObjectName() == "Name") continue;
242 
243  if (it->second->getObjectType() == "Function") continue;
244 
245  obj = findObjectFromRefresh(it->second, ref);
246 
247  if (obj) return obj;
248  }
249 
250  return NULL;
251  }
252 
253  return NULL;
254 }
255 
257 {
258  if (obj == NULL || pDataModel == NULL)
259  return;
260 
261  if (obj->isReference())
262  {
263  CCopasiObject* parent = obj->getObjectParent();
264  assert(parent);
265  std::string typeString = parent->getObjectType();
266  std::string name = obj->getObjectName();
267 
268  if (typeString == "Metabolite" || typeString == "ModelValue" || typeString == "Compartment")
269  {
270  if (name == "Concentration" || name == "Value"
271  || name == "Volume" || name == "Rate"
272  || name == "ParticleNumberRate")
273  {
274  if (!exportModelEntityExpression(obj, pDataModel))
275  {
276  return;
277  }
278  else
279  {
280  return;
281  }
282  }
283  }
284 
285  //TODO warning for initial assignments
286  }
287 
288  return;
289 }
290 
292 {
293  if (obj == NULL || pDataModel == NULL)
294  return false;
295 
296  if (obj->isReference())
297  {
298  CCopasiObject* parent = obj->getObjectParent();
299  return exportModelEntityExpression(parent, pDataModel);
300  }
301  else
302  {
303  std::string typeString = obj->getObjectType();
304 
305  CModelEntity* tmp;
306  tmp = dynamic_cast< CModelEntity * >(obj);
307 
308  std::ostringstream comments;
309  std::ostringstream expression;
310 
311  std::string str1;
312  std::string str2;
313 
314  comments << "model entity \'" << tmp->getObjectName() << "\':" << CModelEntity::StatusName[tmp->getStatus()];
315 
316  if (tmp->getStatus() == CModelEntity::ODE)
317  {
318  const CExpression* pExpression = tmp->getExpressionPtr();
319  assert(pExpression);
320 
321  findFunctionsCalls(pExpression->getRoot());
322 
323  std::string result;
324  result = isModelEntityExpressionODEExporterCompatible(tmp, pExpression, pDataModel);
325 
326  if ((isEmptyString(result)))
327  {
328  expression << exportExpression(pExpression, pDataModel);
329  }
330 
331  equations[tmp->getKey()] = expression.str();
332  }
333 
334  switch (tmp->getStatus())
335  {
336  case CModelEntity::FIXED:
337  break;
338 
340  {
341 
342  const CExpression* pExpression = tmp->getExpressionPtr();
343  assert(pExpression);
344 
345  findFunctionsCalls(pExpression->getRoot());
346 
347  std::string result;
348  result = isModelEntityExpressionODEExporterCompatible(tmp, pExpression, pDataModel);
349 
350  if (!(isEmptyString(result)))
351  comments << result;
352  else
353  {
354  expression << exportExpression(pExpression, pDataModel);
355  }
356 
357  str1 = expression.str();
358  str2 = comments.str();
359 
360  CMetab* metab;
361  metab = dynamic_cast< CMetab * >(tmp);
362 
363  if (metab)
364  {
365  std::ostringstream convert;
366  const CCompartment * comp = metab->getCompartment();
367  convert << " * " << NameMap[comp->getKey()];
368  str1 += convert.str();
369  }
370 
371  if (!exportSingleModelEntity(tmp, str1, str2)) return false;
372 
373  break;
374  }
375 
376  case CModelEntity::ODE:
377  {
378 
379  const CExpression* pExpression = tmp->getExpressionPtr();
380  assert(pExpression);
381 
382  findFunctionsCalls(pExpression->getRoot());
383 
384  std::string result;
385  result = isModelEntityExpressionODEExporterCompatible(tmp, pExpression, pDataModel);
386 
387  if (!(isEmptyString(result)))
388  comments << result;
389 
390  str1 = equations[tmp->getKey()];
391  str2 = comments.str();
392 
393  CMetab* metab;
394  metab = dynamic_cast< CMetab * >(tmp);
395 
396  if (metab)
397  {
398  std::ostringstream convert;
399  const CCompartment * comp = metab->getCompartment();
400  convert << " * " << NameMap[comp->getKey()];
401  str1 += convert.str();
402  }
403 
404  if (!exportSingleODE(tmp, str1, str2)) return false;
405 
406  break;
407  }
408 
409  default:
410  return false;
411  break;
412  }
413  }
414 
415  return true;
416 }
417 
419 {
420  return "";
421 }
422 
424 {
425 
426  std::ostringstream result;
427  const std::vector<CEvaluationNode*>& objectNodes = pExpression->getNodeList();
428  size_t j, jMax = objectNodes.size();
429  assert(pDataModel != NULL);
430 
431  for (j = 0; j < jMax; ++j)
432  {
433  if (CEvaluationNode::type(objectNodes[j]->getType()) == CEvaluationNode::OBJECT)
434  {
435  const CEvaluationNodeObject* pObjectNode = dynamic_cast<const CEvaluationNodeObject*>(objectNodes[j]);
436  assert(pObjectNode);
437  std::vector<CCopasiContainer*> containers;
438  containers.push_back(const_cast<CCopasiDataModel*>(pDataModel)->getModel());
439  const CCopasiObject* pObject = pDataModel->ObjectFromName(containers, pObjectNode->getObjectCN());
440  assert(pObject);
441 
442  if (pObject->isReference())
443  {
444  CCopasiObject* pObjectParent = pObject->getObjectParent();
445  assert(pObjectParent);
446  std::string typeString = pObjectParent->getObjectType();
447 
448  if (typeString == "Compartment")
449  {
450  if (pObject->getObjectName() != "Volume"
451  && pObject->getObjectName() != "InitialVolume"
452  && pObject->getObjectName() != "Rate")
453  {
454  result << std::endl << getSingleLineComment() << "WARNING : reference to property other than transient volume for compartment \"" << pObjectParent->getObjectName() << "\" in expression for \"" << tmp->getObjectType() << "\" \"" << tmp->getObjectName() << "\".";
455  }
456  }
457  else if (typeString == "Metabolite")
458  {
459 
460  if (pObject->getObjectName() != "Concentration"
461  && pObject->getObjectName() != "InitialConcentration"
462  && pObject->getObjectName() != "ParticleNumber"
463  && pObject->getObjectName() != "Rate")
464  {
465 
466  result << std::endl << getSingleLineComment() << "WARNING : reference to property other than transient concentration, initial concentration or concentrations rate for metabolite \"" << pObjectParent->getObjectName() << "\" in expression for \"" << tmp->getObjectType() << "\" \"" << tmp->getObjectName() << "\".";
467  }
468 
469  CMetab* metab;
470  metab = dynamic_cast< CMetab * >(pObjectParent);
471 
472  if ((metab->getStatus() == CModelEntity::REACTIONS && metab->isDependent()) && pObject->getObjectName() == "Rate")
473  {
474 
475  result << std::endl << getSingleLineComment() << "WARNING : reference to rate of dependent (defined from moiety) metabolite \"" << pObjectParent->getObjectName() << "\" in expression for \"" << tmp->getObjectType() << "\" \"" << tmp->getObjectName() << "\".";
476  }
477  }
478  else if (typeString == "ModelValue")
479  {
480  if (pObject->getObjectName() != "Value"
481  && pObject->getObjectName() != "InitialValue"
482  && pObject->getObjectName() != "Rate")
483  {
484 
485  result << std::endl << getSingleLineComment() << "WARNING : reference to property other than transient value, initial value or rate for \"" << typeString << "\" \"" << pObjectParent->getObjectName() << "\" in expression for \"" << tmp->getObjectType() << "\" \"" << tmp->getObjectName() << "\".";
486  }
487  }
488  else if (typeString == "Model")
489  {
490 
491  if (pObject->getObjectName() != "Time" && pObject->getObjectName() != "Initial Time" && pObject->getObjectName() != "Avogadro Constant")
492  {
493 
494  result << std::endl << getSingleLineComment() << "WARNING : reference to property other than initial time or transient time for model \"" << pObjectParent->getObjectName() << "\" in expression for \"" << tmp->getObjectType() << "\" \"" << tmp->getObjectName() << "\".";
495  }
496  }
497  else if (typeString == "Parameter")
498  {
499 
500  if (pObject->getObjectName() != "Value")
501  {
502 
503  result << std::endl << getSingleLineComment() << "WARNING : reference to property other than initial time or transient time for model \"" << pObjectParent->getObjectName() << "\" in expression for \"" << tmp->getObjectType() << "\" \"" << tmp->getObjectName() << "\".";
504  }
505  }
506  else if (typeString == "Reaction")
507  {
508  if (pObject->getObjectName() != "Flux")
509  {
510  result << std::endl << getSingleLineComment() << "WARNING : reference to property other than Flux for Reaction \"" << pObjectParent->getObjectName() << "\" in expression for \"" << tmp->getObjectType() << "\" \"" << tmp->getObjectName() << "\".";
511  }
512  }
513  else
514  {
515  result << std::endl << getSingleLineComment() << "WARNING : expression for \"" << tmp->getObjectType() << "\" \"" << tmp->getObjectName() << "\" contains reference to a value in object \"" << pObjectParent->getObjectName() << "\" of type \"" << typeString << "\" which is not supported in this ODE exporter Version.";
516  }
517  }
518  else
519  {
520  result << std::endl << getSingleLineComment() << "WARNING : expression for \"" << tmp->getObjectType() << "\" \"" << tmp->getObjectName() << "\" contains reference to a object named \"" << pObject->getObjectName() << "\" of type \"" << pObject->getObjectType() << "\" which is not supported in this ODE exporter Version.";
521  }
522  }
523  }
524 
525  return result.str();
526 }
527 
528 size_t getReactionIndex(const CCopasiVector< CReaction > & reacs, const CReaction *react)
529 {
530  for (size_t i = 0; i < reacs.size(); ++i)
531  {
532  if (reacs[i] == react)
533  return i;
534  }
535 
536  return C_INVALID_INDEX;
537 }
538 
539 std::string getQuantityParameterOrValue(const std::map< std::string, std::string >& map, const CCopasiDataModel* pDataModel)
540 {
541  double val = pDataModel->getModel()->getQuantity2NumberFactor();
542  const CCopasiVectorN< CModelValue >& vals = pDataModel->getModel()->getModelValues();
544 
545  for (; it != vals.end(); ++it)
546  {
547  std::map< std::string, std::string >::const_iterator key = map.find((*it)->getKey());
548 
549  if ((*it)->getInitialValue() == val && key != map.end())
550  return key->second;
551  }
552 
553  std::ostringstream str;
554  str << val;
555  return str.str();
556 }
557 
558 std::string CODEExporter::exportExpression(const CExpression* pExpression, const CCopasiDataModel* pDataModel)
559 {
560 
561  std::string result;
562 
563  CExpression* tmpExpression;
564  tmpExpression = new CExpression(*pExpression, pDataModel);
565  assert(pDataModel != NULL);
566 
567  const std::vector<CEvaluationNode*>& objectNodes = tmpExpression->getNodeList();
568  size_t j, jMax = objectNodes.size();
569 
570  for (j = 0; j < jMax; ++j)
571  {
572  if (CEvaluationNode::type(objectNodes[j]->getType()) == CEvaluationNode::CALL)
573  {
574  CEvaluationNodeCall* pObjectNode = dynamic_cast<CEvaluationNodeCall*>(objectNodes[j]);
575  assert(pObjectNode);
576  objectNodes[j]->setData(NameMap[pObjectNode->getCalledTree()->getKey()]);
577  }
578  else if (CEvaluationNode::type(objectNodes[j]->getType()) == CEvaluationNode::OBJECT)
579  {
580  CEvaluationNodeObject* pObjectNode = dynamic_cast<CEvaluationNodeObject*>(objectNodes[j]);
581  assert(pObjectNode);
582  CCopasiObjectName cn = pObjectNode->getObjectCN();
583 
584  const CCopasiObject* pObject = pDataModel->getDataObject(cn);
585  assert(pObject);
586  std::string objectName = pObject->getObjectName();
587 
588  if (pObject->isReference()) pObject = pObject->getObjectParent();
589 
590  std::string objectType = pObject->getObjectType();
591 
592  if (objectType == "Model")
593  {
594  if (objectName == "Time")
595  objectNodes[j]->setData(NameMap[timeKey]);
596 
597  if (objectName == "Avogadro Constant")
598  {
599  std::ostringstream value;
600  value << pDataModel->getModel()->getQuantity2NumberFactor();
601  objectNodes[j]->setData(value.str());
602  }
603 
604  if (objectName == "Initial Time")
605  {
606  const CTrajectoryTask * pTrajectory =
607  dynamic_cast<CTrajectoryTask *>((*const_cast<CCopasiDataModel*>(pDataModel)->getTaskList())["Time-Course"]);
608 
609  const CTrajectoryProblem * pTrajectoryProblem =
610  dynamic_cast<const CTrajectoryProblem *>(pTrajectory->getProblem());
611 
612  std::ostringstream value;
613  value << pTrajectoryProblem->getOutputStartTime();
614  objectNodes[j]->setData(value.str());
615  }
616  }
617  else if (objectType == "ModelValue")
618  {
619  if (objectName == "Value") objectNodes[j]->setData(NameMap[pObject->getKey()]);
620 
621  if (objectName == "InitialValue")
622  {
623  const CModelValue* modval;
624  modval = dynamic_cast<const CModelValue * >(pObject);
625  std::ostringstream value;
626  value << exportNumber(modval->getInitialValue());
627  objectNodes[j]->setData(value.str());
628  }
629 
630  if (objectName == "Rate")
631  {
632  const CModelValue* modval;
633  modval = dynamic_cast<const CModelValue * >(pObject);
634 
635  if (modval->getStatus() == CModelEntity::ODE)
636  {
637 
638  if ((isEmptyString(equations[modval->getKey()])))
639  {
640  std::ostringstream odeKey;
641  odeKey << "ode_" << modval->getKey();
642  objectNodes[j]->setData(NameMap[odeKey.str()]);
643  }
644  else
645  {
646  std::ostringstream str1;
647  str1 << "(" << equations[modval->getKey()] << ")";
648  objectNodes[j]->setData(str1.str());
649  }
650  }
651  }
652  }
653  else if (objectType == "Metabolite")
654  {
655  if (objectName == "Concentration")
656  objectNodes[j]->setData(NameMap[pObject->getKey()]);
657 
658  if (objectName == "ParticleNumber")
659  {
660  std::ostringstream str;
661  str << NameMap["sm_" + pObject->getKey()] << " * "
662  << getQuantityParameterOrValue(NameMap, pDataModel)
663  << " ";
664  objectNodes[j]->setData(str.str());
665  }
666 
667  if (objectName == "InitialConcentration")
668  {
669  const CMetab* metab;
670  metab = dynamic_cast<const CMetab * >(pObject);
671  std::ostringstream value;
672  value << metab->getInitialConcentration();
673  objectNodes[j]->setData(value.str());
674  }
675 
676  if (objectName == "Rate")
677  {
678  const CMetab* metab;
679  metab = dynamic_cast<const CMetab * >(pObject);
680 
681  if ((metab->getStatus() == CModelEntity::REACTIONS && !metab->isDependent()) || metab->getStatus() == CModelEntity::ODE)
682  {
683  if ((isEmptyString(equations[metab->getKey()])))
684  {
685  std::ostringstream odeKey;
686  odeKey << "ode_" << metab->getKey();
687  objectNodes[j]->setData(NameMap[odeKey.str()]);
688  }
689  else
690  {
691  std::ostringstream str1;
692  str1 << "(" << equations[metab->getKey()] << ")";
693  objectNodes[j]->setData(str1.str());
694  }
695  }
696  }
697  }
698  else if (objectType == "Compartment")
699  {
700  if (objectName == "Volume")
701  objectNodes[j]->setData(NameMap[pObject->getKey()]);
702 
703  if (objectName == "InitialVolume")
704  {
705  const CCompartment* comp;
706  comp = dynamic_cast<const CCompartment * >(pObject);
707  std::ostringstream value;
708  value << exportNumber(comp-> getInitialValue());
709  objectNodes[j]->setData(value.str());
710  }
711 
712  if (objectName == "Rate")
713  {
714 
715  const CCompartment* comp;
716  comp = dynamic_cast<const CCompartment * >(pObject);
717 
718  if (comp->getStatus() == CModelEntity::ODE)
719  {
720 
721  if ((isEmptyString(equations[comp->getKey()])))
722  {
723  std::ostringstream odeKey;
724  odeKey << "ode_" << comp->getKey();
725  objectNodes[j]->setData(NameMap[odeKey.str()]);
726  }
727  else
728  {
729  std::ostringstream str1;
730  str1 << "(" << equations[comp->getKey()] << ")";
731  objectNodes[j]->setData(str1.str());
732  }
733  }
734  }
735  }
736  else if (objectType == "Parameter")
737  {
738  if (objectName == "Value")
739  objectNodes[j]->setData(NameMap[pObject->getKey()]);
740  }
741  else if (objectType == "Reaction")
742  {
743  if (objectName == "Flux")
744  {
745  const CModel* copasiModel = pDataModel->getModel();
746  const CReaction *react = static_cast<const CReaction*>(pObject);
747  const CCopasiVector< CReaction > & reacs = copasiModel->getReactions();
748 
749  size_t index = getReactionIndex(reacs, react);
750 
751  if (index == C_INVALID_INDEX)
752  {
753  objectNodes[j]->setData("0");
754  continue;
755  }
756 
757  const CCopasiVector< CMetab > & metabs = copasiModel->getMetabolitesX();
758  size_t indep_size = copasiModel->getNumIndependentReactionMetabs();
759  size_t ode_size = copasiModel->getNumODEMetabs();
760  const CMatrix< C_FLOAT64 > & redStoi = copasiModel->getRedStoi();
761 
762  std::ostringstream jequation;
763 
764  for (size_t j1 = 0; j1 < indep_size; ++j1)
765  {
766  if (fabs(redStoi[j1][index]) > 0.0)
767  {
768  jequation << equations[metabs[ode_size + j1]->getKey()];
769  }
770  }
771 
772  objectNodes[j]->setData(jequation.str());
773  }
774  }
775  }
776  }
777 
778  result = getDisplayExpressionString(tmpExpression);
779 
780  return result;
781 }
782 
783 bool CODEExporter::preprocess(const CModel* copasiModel)
784 {
785  size_t i, j;
786 
788 
789  timeKey = "time";
790 
792 
793  const CCopasiVector< CMetab > & metabs = copasiModel->getMetabolitesX();
794  size_t metabs_size = metabs.size();
795 
796  for (i = 0; i < metabs_size; i++)
797  {
798  CMetab * metab = metabs[i];
799 
800  //if (metab->isUsed()) //changed
801  {
802 
803  std::string name = translateObjectName(metab->getObjectName());
804 
805  std::ostringstream smKey;
806  smKey << "sm_" << metab->getKey();
807 
808  NameMap[smKey.str()] = name; // mass fraction
809 
810  NameMap[metab->getKey()] = setConcentrationName(name); //concentration
811 
812  if ((metab->getStatus() == CModelEntity::REACTIONS && !metab->isDependent()) || metab->getStatus() == CModelEntity::ODE)
813  {
814  std::ostringstream odeKey;
815  odeKey << "ode_" << metab->getKey();
816  NameMap[odeKey.str()] = setODEName(name);
817  }
818  }
819  }
820 
821  size_t comps_size = copasiModel->getCompartments().size();
822  const CCopasiVector< CCompartment > & comps = copasiModel->getCompartments();
823 
824  for (i = 0; i < comps_size; i++)
825  {
826  CCompartment* comp = comps[i];
827  std::string name = translateObjectName(comp->getObjectName());
828  NameMap[comp->getKey()] = name;
829 
830  if (comp->getStatus() == CModelEntity::ODE)
831  {
832  std::ostringstream odeKey;
833  odeKey << "ode_" << comp->getKey();
834  NameMap[odeKey.str()] = setODEName(name);
835  }
836  }
837 
838  size_t modvals_size = copasiModel->getModelValues().size();
839  const CCopasiVector< CModelValue > & modvals = copasiModel->getModelValues();
840 
841  for (i = 0; i < modvals_size; i++)
842  {
843  CModelValue* modval = modvals[i];
844  std::string name = translateObjectName(modval->getObjectName());
845  NameMap[modval->getKey()] = name;
846 
847  if (modval->getStatus() == CModelEntity::ODE)
848  {
849  std::ostringstream odeKey;
850  odeKey << "ode_" << modval->getKey();
851  NameMap[odeKey.str()] = setODEName(name);
852  }
853  }
854 
855  size_t reacs_size = copasiModel->getReactions().size();
856 
857  const CCopasiVector< CReaction > & reacs = copasiModel->getReactions();
858 
859  for (i = 0; i < reacs_size; ++i)
860  {
861  size_t params_size;
862 
863  params_size = reacs[i]->getParameters().size();
864 
865  for (j = 0; j < params_size; ++j)
866  {
867  if (reacs[i]->isLocalParameter(j))
868  {
869  NameMap[reacs[i]->getParameters().getParameter(j)->getKey()] =
870  translateObjectName(reacs[i]->getParameters().getParameter(j)->getObjectName());
871  }
872  }
873  }
874 
875  return true;
876 }
877 
878 bool CODEExporter::exportTitleData(const CModel* /* copasiModel */,
879  std::ostream & /* os */)
880 {return true;}
881 
882 std::string CODEExporter::exportNumber(double number)
883 {
884  std::stringstream str; str << number;
885  return str.str();
886 }
887 
888 /* export metabolites */
889 bool CODEExporter::exportMetabolites(const CModel* copasiModel)
890 {
891 
892  const CCopasiVector< CMetab > & metabs = copasiModel->getMetabolitesX();
893  const CLinkMatrixView & L = copasiModel->getL();
894 
895  size_t metabs_size = metabs.size();
896  size_t indep_size = copasiModel->getNumIndependentReactionMetabs();
897  size_t ode_size = copasiModel->getNumODEMetabs();
898  size_t conservedTotals = 0;
899  size_t i, j;
900  C_FLOAT64 value;
901 
902  for (i = 0; i < metabs_size; i++)
903  {
904  const CMetab * metab;
905  metab = metabs[i];
906 
907  //if (!metab->isUsed()) continue;
908 
909  std::ostringstream expression;
910  std::ostringstream comments;
911 
912  std::string str1;
913  std::string str2;
914 
915  comments << "metabolite \'" << CMetabNameInterface::getDisplayName(copasiModel, *metab, false)
916  << "\': " << CModelEntity::StatusName[metab->getStatus()];
917 
918  switch (metab->getStatus())
919  {
920  case CModelEntity::FIXED:
921  {
922  const CCompartment * comp;
923  comp = metab->getCompartment();
924  C_FLOAT64 volume = comp->getInitialValue();
925 
926  value = metab->getInitialConcentration() * volume;
927 
928  expression << exportNumber(value);
929 
930  break;
931  }
932 
934 
935  break;
936 
937  case CModelEntity::ODE:
938  {
939 
940  const CCompartment * comp;
941  comp = metab->getCompartment();
942  C_FLOAT64 volume = comp->getInitialValue();
943 
944  value = metab->getInitialConcentration() * volume;
945 
946  expression << exportNumber(value);
947 
948  break;
949  }
950 
952  {
953  const CCompartment * comp;
954  comp = metab->getCompartment();
955  C_FLOAT64 volume = comp->getInitialValue();
956 
957  std::ostringstream tmp;
958  std::ostringstream more;
959  value = metab->getInitialConcentration() * volume;
960  double eps = std::numeric_limits<double>::epsilon();
961  more.precision(16);
962 
963  if (metab->isDependent())
964  {
965  for (j = 0; j < indep_size; j++)
966  {
967 
968  if (fabs(L(i - ode_size, j)) > eps)
969  {
970 
971  if (L(i - ode_size, j) < 0.0)
972  {
973  tmp << "-";
974  }
975  else
976  {
977  tmp << "+";
978  }
979 
980  if (fabs(fabs(L(i - ode_size, j)) - 1.0) > eps)
981  {
982  tmp << fabs(L(i - ode_size, j)) << "*";
983  }
984 
985  std::ostringstream jsmKey;
986  jsmKey << "sm_" << metabs[ode_size + j]->getKey();
987 
988  tmp << NameMap[jsmKey.str()];
989  const CCompartment * compj = metabs[ode_size + j]->getCompartment();
990 
991  value -= L(i - ode_size, j) * metabs[ode_size + j]->getInitialConcentration() * compj->getInitialValue();
992  }
993 
994  // comments << " dependent ";
995  }
996 
997  more << value;
998  std::stringstream stream; stream << "ct[" << (conservedTotals++) << "]";
999  std::string ctName(stream.str());
1000  NameMap["CT " + metab->getObjectName()] = ctName;
1001  stream.clear(); stream << " conserved total for '" << metab->getObjectName() << "'";
1002  exportSingleObject(fixed, ctName, more.str(), stream.str());
1003  expression << ctName << tmp.str();
1004  }
1005  else
1006  {
1007  more << exportNumber(value);
1008  expression << more.str() << tmp.str();
1009  }
1010 
1011  break;
1012  }
1013 
1014  default:
1015 
1016  return false;
1017 
1018  break;
1019  }
1020 
1021  str1 = expression.str();
1022  str2 = comments.str();
1023 
1024  if (metab->getStatus() != CModelEntity::ASSIGNMENT)
1025  if (!exportSingleMetabolite(metab, str1, str2)) return false;
1026  }
1027 
1028  return true;
1029 }
1030 
1031 /* export metabolites concentrations */
1033 {
1034 
1035  const CCopasiVector< CMetab > & metabs = copasiModel->getMetabolitesX();
1036 
1037  size_t metabs_size = metabs.size();
1038 
1039  size_t i;
1040 
1041  for (i = 0; i < metabs_size; i++)
1042  {
1043  const CMetab * metab;
1044  metab = metabs[i];
1045 
1046  //if (!metab->isUsed()) continue;
1047 
1048  std::string str1;
1049  std::string str2;
1050 
1051  std::ostringstream expression;
1052  std::ostringstream comments;
1053 
1054  comments << "concentration of metabolite \'" << CMetabNameInterface::getDisplayName(copasiModel, *metab, false)
1055  << "\': " << CModelEntity::StatusName[metab->getStatus()];
1056 
1057  std::ostringstream smKey;
1058  smKey << "sm_" << metab->getKey();
1059 
1060  expression << NameMap[smKey.str()] << "/" << NameMap[metabs[i]->getCompartment()->getKey()];
1061 
1062  str1 = expression.str();
1063  str2 = comments.str();
1064 
1065  if (!exportSingleObject(assignment, NameMap[metab->getKey()], str1, str2))
1066  return false;
1067  }
1068 
1069  return true;
1070 }
1071 
1072 /* export compartments */
1073 
1075 {
1076  const CCopasiVector< CCompartment > & comps = copasiModel->getCompartments();
1077 
1078  size_t comps_size = comps.size();
1079  size_t i;
1080 
1081  for (i = 0; i < comps_size; i++)
1082  {
1083  CCompartment* comp;
1084  comp = comps[i];
1085 
1086  std::ostringstream comments;
1087  std::ostringstream expression;
1088 
1089  std::string str1;
1090  std::string str2;
1091 
1092  comments << "compartment \'" << comp->getObjectName() << "\':" << CModelEntity::StatusName[comp->getStatus()];
1093 
1094  switch (comp->getStatus())
1095  {
1096  case CModelEntity::FIXED:
1097  {
1098  expression << exportNumber(comp->getInitialValue());
1099 
1100  break;
1101  }
1102 
1104 
1105  break;
1106 
1107  case CModelEntity::ODE:
1108  {
1109 
1110  expression << exportNumber(comp->getInitialValue());
1111 
1112  break;
1113  }
1114 
1115  default:
1116  return false;
1117  break;
1118  }
1119 
1120  str1 = expression.str();
1121  str2 = comments.str();
1122 
1123  if (comp->getStatus() != CModelEntity::ASSIGNMENT)
1124  if (!exportSingleCompartment(comp, str1, str2)) return false;
1125  }
1126 
1127  return true;
1128 }
1129 
1130 /* export model values */
1131 
1132 bool CODEExporter::exportModelValues(const CModel* copasiModel)
1133 {
1134  const CCopasiVector< CModelValue > & modvals = copasiModel->getModelValues();
1135 
1136  size_t modvals_size = modvals.size();
1137  size_t i;
1138 
1139  for (i = 0; i < modvals_size; i++)
1140  {
1141  CModelValue* modval;
1142  modval = modvals[i];
1143 
1144  std::ostringstream comments;
1145  std::ostringstream expression;
1146 
1147  std::string str1;
1148  std::string str2;
1149 
1150  comments << "global quantity \'" << modval->getObjectName() << "\':" << CModelEntity::StatusName[modval->getStatus()];
1151 
1152  switch (modval->getStatus())
1153  {
1154  case CModelEntity::FIXED:
1155  {
1156  expression << exportNumber(modval->getInitialValue());
1157 
1158  break;
1159  }
1160 
1162  //{
1163  //CExpression *cexpression = new CExpression(*modval->getExpressionPtr());
1164  //CEvaluationNode *node = cexpression->getRoot();
1165  //const std::string &key = modval->getKey();
1166  //size_t index = 0;
1167  //if (!exportSingleFunction(node, key, index))
1168  // return false;
1169  //expression << getDisplayExpressionString(cexpression);
1170  //}
1171  break;
1172 
1173  case CModelEntity::ODE:
1174  {
1175  expression << exportNumber(modval->getInitialValue());
1176 
1177  break;
1178  }
1179 
1180  default:
1181  return false;
1182  break;
1183  }
1184 
1185  str1 = expression.str();
1186  str2 = comments.str();
1187 
1188  if (modval->getStatus() != CModelEntity::ASSIGNMENT)
1189  if (!exportSingleModVal(modval, str1, str2)) return false;
1190  }
1191 
1192  return true;
1193 }
1194 
1195 /* export reaction parameters and kinetic functions */
1196 
1198 {
1199 
1200  const CCopasiVector< CMetab > & metabs = copasiModel->getMetabolitesX();
1201  const CCopasiVector< CReaction > & reacs = copasiModel->getReactions();
1202 
1203  size_t reacs_size = reacs.size();
1204  size_t indep_size = copasiModel->getNumIndependentReactionMetabs();
1205  size_t ode_size = copasiModel->getNumODEMetabs();
1206 
1207  const CMatrix< C_FLOAT64 > & redStoi = copasiModel->getRedStoi();
1208 
1209  size_t i, j;
1210 
1211  for (i = 0; i < reacs_size; ++i)
1212  {
1213  CReaction* reac;
1214  reac = reacs[i];
1215 
1216  std::string name = reac->getObjectName();
1217 
1218  size_t params_size = reac->getParameters().size();
1219 
1220  for (j = 0; j < params_size; ++j)
1221  {
1222  if (!reac->isLocalParameter(j))
1223  continue;
1224 
1225  std::ostringstream comments;
1226  std::ostringstream expression;
1227 
1228  CCopasiParameter* param;
1229 
1230  param = reac->getParameters().getParameter(j);
1231  expression << *param->getValue().pDOUBLE;
1232 
1233  comments << "reaction \'" << name << "\': " <<
1234  " kinetic parameter \'" << param->getObjectName() << "\'";
1235 
1236  std::string str1 = expression.str();
1237  std::string str2 = comments.str();
1238 
1239  if (!exportSingleParameter(param, str1, str2)) return false;
1240  }
1241 
1242  if (!exportKineticFunction(reac)) return false;
1243 
1244  for (j = 0; j < indep_size; ++j)
1245  {
1246  std::ostringstream jequation;
1247 
1248  if (fabs(redStoi[j][i]) > 0.0)
1249  {
1250  if (redStoi[j][i] < 0.0)
1251  jequation << "-";
1252  else
1253  {
1254  std::string str1 = equations[metabs[ode_size + j]->getKey()];
1255 
1256  if (!(isEmptyString(str1)))
1257  jequation << "+";
1258  }
1259 
1260  if (fabs(redStoi[j][i]) != 1.0)
1261  jequation << fabs(redStoi[j][i]) << "*";
1262 
1263  jequation << KineticFunction2ODEmember(reac);
1264 
1265  if (reac->getCompartmentNumber() == 1) jequation << "*" << NameMap[metabs[ode_size + j]->getCompartment()->getKey()];
1266 
1267  equations[metabs[ode_size + j]->getKey()] += jequation.str();
1268  }
1269  }
1270  }
1271 
1272  return true;
1273 }
1274 
1275 /* export ODEs */
1276 bool CODEExporter::exportODEs(const CModel* copasiModel)
1277 {
1278  const CCopasiVector< CMetab > & metabs = copasiModel->getMetabolitesX();
1279  size_t indep_size = copasiModel->getNumIndependentReactionMetabs();
1280  size_t ode_size = copasiModel->getNumODEMetabs();
1281  size_t metabs_size = metabs.size();
1282 
1283  size_t i;
1284 
1285  for (i = 0; i < indep_size && i + ode_size < metabs_size; ++i)
1286  {
1287  CMetab * metab = metabs[ode_size + i];
1288 
1289  std::string str1 = equations[metab->getKey()];
1290  std::string str2 = " ";
1291 
1292  if ((metab->getStatus() == CModelEntity::REACTIONS && !(metab->isDependent())))
1293  if (!exportSingleODE(metab, str1, str2)) return false;
1294  }
1295 
1296  for (i = indep_size; i + ode_size < metabs_size; ++i)
1297  {
1298  CMetab * metab = metabs[ode_size + i];
1299 
1300  if (metab->getStatus() == CModelEntity::REACTIONS && !metab->isDependent())
1301  {
1302  std::string str1 = "0";
1303  std::string str2 = " ";
1304 
1305  if (!exportSingleODE(metab, str1, str2)) return false;
1306  }
1307  }
1308 
1309  return true;
1310 }
1311 
1312 bool CODEExporter::exportClosingData(const CModel* /* copasiModel */,
1313  std::ostream & /* os */)
1314 {return true;}
1315 
1317 {return;}
1318 
1320 {return " ";}
1321 
1322 std::string CODEExporter::translateObjectName(const std::string & /* realName */)
1323 {return " ";}
1324 
1325 std::string CODEExporter::setODEName(const std::string & /* objName */)
1326 {return " ";}
1327 
1328 std::string CODEExporter::setConcentrationName(const std::string & /* objName */)
1329 {return " ";}
1330 
1331 bool CODEExporter::exportSingleObject(std::ostringstream & /* which */,
1332  const std::string & /* name */,
1333  const std::string & /* expression */,
1334  const std::string & /* comments */)
1335 {return true;}
1336 
1338  std::string & /* expression */,
1339  std::string & /* comments */)
1340 {return true;}
1341 
1343  std::string & /* expression */,
1344  std::string & /* comments */)
1345 {return true;}
1346 
1348  std::string & /* expression */,
1349  std::string & /* comments */)
1350 {return true;}
1351 
1353  std::string & /* expression */,
1354  std::string & /* comments */)
1355 {return true;}
1356 
1358  std::string & /* expression */,
1359  std::string & /* comments */)
1360 {return true;}
1361 
1363 {
1364 
1365  std::ostringstream comments;
1366  std::ostringstream expression;
1367 
1368  const CFunction* func;
1369 
1370  func = reac->getFunction();
1371 
1372  comments << "reaction \'" << reac->getObjectName() << "\': "
1373  << "kinetic function \'" << func ->getObjectName() << "\'";
1374 
1375  if (func->getType() != CEvaluationTree::MassAction)
1376  {
1377  CFunction* tmpfunc;
1378  tmpfunc = new CFunction(*func);
1379 
1380  const std::vector<std::vector<std::string> > & keyMap = reac->getParameterMappings();
1382 
1383  modifyTreeForMassAction(tmpfunc);
1384 
1385  while (treeIt != NULL)
1386  {
1387  if (CEvaluationNode::type(treeIt->getType()) == CEvaluationNode::VARIABLE)
1388  {
1389 
1390  size_t index;
1392  std::string tmpname;
1393  std::string name;
1394 
1395  name = tmpfunc->getVariables()[treeIt->getData()]->getObjectName();
1396  index = tmpfunc->getVariableIndex(name);
1397  role = tmpfunc->getVariables()[index]->getUsage();
1398 
1399  CCopasiObject * obj = CCopasiRootContainer::getKeyFactory()->get(keyMap[index][0]);
1400 
1401  if ((role == CFunctionParameter::SUBSTRATE)
1402  || (role == CFunctionParameter::PRODUCT)
1403  || (role == CFunctionParameter::MODIFIER))
1404  {
1405  if (obj)
1406  tmpname = NameMap[obj->getKey()];
1407  else
1408  tmpname = "unknown";
1409  }
1410 
1411  if (role == CFunctionParameter::PARAMETER)
1412  {
1413  if (!(reac->isLocalParameter(index)))
1414  {
1415  CModelValue* modval;
1416  modval = dynamic_cast< CModelValue * >(obj);
1417  tmpname = NameMap[modval ->getKey()];
1418  }
1419  else
1420  {
1421  CCopasiParameter* param;
1422  param = dynamic_cast< CCopasiParameter * >(obj);
1423  tmpname = NameMap[param->getKey()];
1424  }
1425  }
1426 
1427  if (role == CFunctionParameter::VOLUME)
1428  {
1429  CCompartment* comp;
1430  comp = dynamic_cast< CCompartment * >(obj);
1431  tmpname = NameMap[comp->getKey()];
1432  }
1433 
1434  if (role == CFunctionParameter::TIME)
1435  tmpname = NameMap[timeKey];
1436 
1437  treeIt->setData(tmpname);
1438  }
1439 
1440  ++treeIt;
1441  }
1442 
1443  size_t index = 0;
1444 
1445  if (!exportSingleFunction(tmpfunc->getRoot(), reac, index)) return false;
1446 
1447  expression << getDisplayFunctionString(tmpfunc);
1448  }
1449  else
1450  {
1451 
1452  const CCopasiVector<CChemEqElement> & substrs = reac->getChemEq().getSubstrates();
1453  const CCopasiVector<CChemEqElement> & prods = reac->getChemEq().getProducts();
1454  const std::vector<std::vector<std::string> > & keyMap = reac->getParameterMappings();
1455  CCopasiObject * obj;
1456 
1457  size_t substrs_size = substrs.size(), prods_size = prods.size();
1458  size_t k, m, mult;
1459 
1460  CChemEqElement* substr;
1461  CChemEqElement* prod;
1462 
1463  const CMassAction cMassAction = *static_cast<const CMassAction*>(reac->getFunction());
1464 
1465  obj = CCopasiRootContainer::getKeyFactory()->get(keyMap[0][0]);
1466 
1467  if (!(reac->isLocalParameter(0)))
1468  {
1469  CModelValue* modval;
1470  modval = dynamic_cast< CModelValue * >(obj);
1471  expression << NameMap[modval ->getKey()];
1472  }
1473  else
1474  {
1475  CCopasiParameter * param;
1476  param = dynamic_cast< CCopasiParameter * >(obj);
1477  expression << NameMap[param->getKey()];
1478  }
1479 
1480  for (k = 0; k < substrs_size; ++k)
1481  {
1482  substr = substrs[k];
1483  mult = (size_t) substr->getMultiplicity();
1484 
1485  expression << "*" << NameMap[substr->getMetaboliteKey()];
1486 
1487  if (mult > 1)
1488  for (m = 1; m < mult; ++m)
1489  expression << "*" << NameMap[substr->getMetaboliteKey()];
1490  }
1491 
1492  if (cMassAction.isReversible() == TriTrue)
1493  {
1494  expression << "-";
1495 
1496  obj = CCopasiRootContainer::getKeyFactory()->get(keyMap[2][0]);
1497 
1498  if (!(reac->isLocalParameter(2)))
1499  {
1500  CModelValue * modval;
1501  modval = dynamic_cast< CModelValue * >(obj);
1502  expression << NameMap[modval ->getKey()];
1503  }
1504  else
1505  {
1506  CCopasiParameter * param;
1507  param = dynamic_cast< CCopasiParameter * >(obj);
1508  expression << NameMap[param->getKey()];
1509  }
1510 
1511  for (k = 0; k < prods_size; ++k)
1512  {
1513  prod = prods[k];
1514  mult = (size_t) prod->getMultiplicity();
1515 
1516  expression << "*" << NameMap[prod->getMetaboliteKey()];
1517 
1518  if (mult > 1)
1519  for (m = 1; m < mult; ++m)
1520  expression << "*" << NameMap[prod->getMetaboliteKey()];
1521  }
1522  }
1523  }
1524 
1525  std::string name = translateObjectName(reac->getFunction()->getObjectName());
1526  std::ostringstream localKey;
1527  localKey << reac->getKey() << "_root_func";
1528  NameMap[localKey.str()] = name;
1529 
1530  std::string str1 = expression.str();
1531  std::string str2 = comments.str();
1532 
1533  //functions << std::endl;
1534 
1535  if (!exportSingleObject(functions, name, str1, str2)) return false;
1536 
1537  //functions << std::endl;
1538 
1539  return true;
1540 }
1541 
1542 bool CODEExporter::exportSingleFunction(CEvaluationNode* pNode, const CReaction *reac, size_t &index)
1543 {
1544  return exportSingleFunction(pNode, reac->getKey(), index);
1545 }
1546 
1547 bool CODEExporter::exportSingleFunction(CEvaluationNode* pNode, const std::string& key, size_t &index)
1548 {
1549  if (pNode)
1550  {
1553 
1554  while (treeIt != NULL)
1555  {
1556  if (CEvaluationNode::type(treeIt->getType()) == CEvaluationNode::CALL)
1557  {
1558  const CFunction* func;
1559  func = static_cast<CFunction*>(pFunctionDB->findFunction((*treeIt).getData()));
1560 
1561  CFunction* tmpfunc = NULL;
1562  tmpfunc = new CFunction(*func);
1563 
1564  std::ostringstream localKey;
1565 
1566  if (index)
1567  localKey << key << "_func_" << index;
1568  else
1569  localKey << key << "_root_func";
1570 
1571  index++;
1572 
1573  std::string name = translateObjectName(func->getObjectName());
1574  NameMap[localKey.str()] = name;
1575 
1576  treeIt->setData(name);
1577 
1579 
1580  size_t i, vindex;
1581  CEvaluationNode* tmproot = tmpfunc->getRoot();
1582  CCopasiTree<CEvaluationNode>::iterator iIt, newIt = tmproot;
1583 
1584  if (CEvaluationNode::type(newIt->getType()) == CEvaluationNode::VARIABLE)
1585  {
1586  CEvaluationNode* child = dynamic_cast<CEvaluationNode*>(treeIt->getChild());
1587  tmproot = child->copyBranch();
1588  }
1589  else while (newIt != NULL)
1590  {
1591  if (CEvaluationNode::type(newIt->getType()) == CEvaluationNode::VARIABLE)
1592  {
1593  vindex = tmpfunc->getVariableIndex((*newIt).getData());
1594 
1595  CEvaluationNode* child = dynamic_cast<CEvaluationNode*>(treeIt->getChild());
1596 
1597  for (i = 0; i < vindex ; i++)
1598  child = dynamic_cast<CEvaluationNode*>((child)->getSibling());
1599 
1600  CEvaluationNode* parent = dynamic_cast<CEvaluationNode*>(newIt->getParent());
1601  CEvaluationNode* newnode = child->copyBranch();
1602 
1603  iIt = newIt;
1604 
1605  ++newIt;
1606 
1607  if (parent)
1608  {
1609  parent->addChild(newnode, &(*iIt));
1610  parent->removeChild(&(*iIt));
1611  }
1612 
1613  delete &(*iIt);
1614  }
1615  else
1616  ++newIt;
1617  }
1618 
1619  if (!exportSingleFunction(tmproot, key, index)) return false;
1620 
1621  std::string expression;
1622  std::string comments = " ";
1623 
1624  expression = getDisplayFunctionString(tmpfunc);
1625 
1626  //functions << std::endl;
1627 
1628  if (!exportSingleObject(functions, name, expression, comments)) return false;
1629 
1630  //functions << std::endl;
1631  }
1632 
1633  ++treeIt;
1634  }
1635  }
1636 
1637  return true;
1638 }
1639 
1641 {return " ";}
1642 
1644 {return " ";}
1645 
1646 bool CODEExporter::exportSingleODE(const CModelEntity * /* mentity */,
1647  std::string & /* equation */,
1648  std::string & /* comments */)
1649 {return true;}
1650 
1651 bool CODEExporter::exportKineticFunctionGroup(const CModel * /* copasiModel */)
1652 {return true;}
1653 
1655 {return " ";}
1656 
1657 std::string CODEExporter::exportTitleString(const size_t /* tmp */)
1658 {return " ";}
1659 
1660 std::string CODEExporter::exportClosingString(const size_t /* tmp */)
1661 {return " ";}
1662 
1663 /**
1664  ** This method tests if a string consists only of whitespace characters
1665  **/
1666 bool CODEExporter::isEmptyString(std::string & str)
1667 {return (str.find_first_not_of(" \n\t\r") == std::string::npos);}
1668 
1669 /**
1670  ** This method modifies the export tree of the function for internal calls of Mass Action
1671  **/
1673 {
1675 
1677 
1678  while (treeIt != NULL)
1679  {
1680  if (CEvaluationNode::type(treeIt->getType()) == CEvaluationNode::CALL)
1681  {
1682  const CFunction* callfunc;
1683  callfunc = static_cast<CFunction*>(pFunctionDB->findFunction((*treeIt).getData()));
1684 
1685  if (callfunc->getType() == CEvaluationTree::MassAction)
1686  {
1687  CEvaluationNode* parent = dynamic_cast<CEvaluationNode*>(treeIt->getParent());
1688  CEvaluationNode* child1 = dynamic_cast<CEvaluationNode*>(treeIt->getChild());
1689  CEvaluationNode* child2 = dynamic_cast<CEvaluationNode*>((treeIt->getChild())->getSibling());
1690 
1691  CEvaluationNode* newNode = NULL;
1694 
1695  CODEExporter::assembleSubTreeForMassAction(newNode1, child1, child2);
1696 
1697  if (callfunc->getObjectName() == "Mass action (irreversible)") newNode = newNode1;
1698 
1699  if (callfunc->getObjectName() == "Mass action (reversible)")
1700  {
1703  newNode->addChild(newNode1, NULL);
1704 
1707  CEvaluationNode* child3 = dynamic_cast<CEvaluationNode*>((child2)->getSibling());
1708  CEvaluationNode* child4 = dynamic_cast<CEvaluationNode*>((child3)->getSibling());
1709 
1710  CODEExporter::assembleSubTreeForMassAction(newNode2, child3, child4);
1711 
1712  newNode->addChild(newNode2, newNode1);
1713  }
1714 
1715  if (parent)
1716  {
1717  parent->addChild(newNode, &(*treeIt));
1718  parent->removeChild(&(*treeIt));
1719  }
1720 
1721  delete &(*treeIt);
1722  treeIt = newNode;
1723  }
1724  }
1725 
1726  ++treeIt;
1727  }
1728 }
1729 
1730 /**
1731  ** This method assembles an expression sub tree for some internal call of Mass Action.
1732  ** The sub tree has to be included in the tree of corresponding root kinetic function in order to
1733  ** export this function whithout the user defined internall Mass Action calls
1734  **/
1736 {
1737  CEvaluationNode* newparent = newNode;
1738  CEvaluationNode* newchild1 = child1->copyBranch();
1739  newparent->addChild(newchild1, NULL);
1740  CEvaluationNode* newchild2;
1741 
1743  {
1746  newparent->addChild(newchild2, newchild1);
1747  newparent = newchild2;
1748  newchild1 = child2->copyBranch();
1749  newparent->addChild(newchild1, NULL);
1750  newchild2 = child2->copyBranch();
1751  newparent->addChild(newchild2, newchild1);
1752  }
1753 
1754  if (0) // *************** TODO: the current Copasi version does not support the case bellow, the following part is not tested
1756  {
1757  const std::vector<CEvaluationNode *> & vector = dynamic_cast< CEvaluationNodeVector *>(child2) ->getVector();
1758  std::vector<CEvaluationNode *>::const_iterator it = vector.begin();
1759  std::vector<CEvaluationNode *>::const_iterator end = vector.end();
1760 
1761  size_t i = 0;
1762 
1763  while (it != end)
1764  {
1767  newparent->addChild(newchild2, newchild1);
1768 
1769  newparent = newchild2;
1770  newchild1 = dynamic_cast<CEvaluationNode*>(vector[i]);
1771  newparent->addChild(newchild1, NULL);
1772  it++;
1773  i++;
1774  }
1775 
1776  if (it == end)
1777  {
1778  newchild2 = dynamic_cast<CEvaluationNode*>(vector[i]);
1779  newparent->addChild(newchild2, newchild1);
1780  }
1781  } // END of this TODO. ****************************************************************************************************
1782 }
const CExpression * getExpressionPtr() const
Header file of class CExpression.
CCopasiDataModel * getObjectDataModel()
bool isContainer() const
std::string isModelEntityExpressionODEExporterCompatible(CModelEntity *tmp, const CExpression *pExpression, const CCopasiDataModel *pDataModel)
virtual bool exportKineticFunction(CReaction *reac)
virtual bool exportSingleModVal(const CModelValue *modval, std::string &expression, std::string &comments)
void exportSimulatedObject(CCopasiObject *obj, const CCopasiDataModel *pDataModel)
CCopasiObject * getDataObject(const CCopasiObjectName &CN) const
CEvaluationNode * copyBranch() const
std::ostringstream headers
Definition: CODEExporter.h:159
const CEvaluationTree * getCalledTree() const
objectType
virtual std::string exportTitleString(const size_t tmp)
virtual bool exportTitleData(const CModel *copasiModel, std::ostream &os)
CCopasiProblem * getProblem()
virtual bool exportMetabolitesConcentrations(const CModel *copasiModel)
static const std::string StatusName[]
Definition: CModelValue.h:67
const CLinkMatrixView & getL() const
Definition: CModel.cpp:1166
virtual size_t getVariableIndex(const std::string &name) const
Definition: CFunction.cpp:133
virtual std::string setODEName(const std::string &objName)
const CCopasiVector< CMetab > & getMetabolites() const
Definition: CModel.cpp:1051
virtual bool preprocess(const CModel *copasiModel)
const std::string & getObjectName() const
const CCopasiVectorN< CModelValue > & getModelValues() const
Definition: CModel.cpp:1060
virtual size_t size() const
const C_FLOAT64 & getInitialConcentration() const
Definition: CMetab.cpp:220
const CRegisteredObjectName & getObjectCN() const
virtual bool exportMetabolites(const CModel *copasiModel)
CCopasiObject * get(const std::string &key)
size_t getNumMetabs() const
Definition: CModel.cpp:1118
virtual const std::string & getKey() const
Definition: CReaction.cpp:190
virtual bool exportSingleObject(std::ostringstream &which, const std::string &name, const std::string &expression, const std::string &comments)
iterator begin()
virtual const objectMap & getObjects() const
const Type & getType() const
virtual std::string getDisplayExpressionString(CExpression *tmp)
static CEvaluationNode * create(const Type &type, const Data &data)
bool isLocalParameter(const size_t &index) const
Definition: CReaction.cpp:449
virtual ~CODEExporter()
virtual std::string setConcentrationName(const std::string &objName)
const CEvaluationTree::Type & getType() const
const std::string & getObjectType() const
#define C_INVALID_INDEX
Definition: copasi.h:222
void assembleSubTreeForMassAction(CEvaluationNode *newNode, CEvaluationNode *child1, CEvaluationNode *child2)
size_t getNumODEMetabs() const
Definition: CModel.cpp:1124
std::ostringstream initial
Definition: CODEExporter.h:155
virtual void setReservedNames()
bool exportCompartments(const CModel *copasiModel)
static std::string getDisplayName(const CModel *model, const std::string &key, const bool &quoted)
virtual std::string exportClosingString(const size_t tmp)
Definition: CMetab.h:178
const std::vector< Refresh * > & getListOfSimulatedRefreshes() const
Definition: CModel.cpp:4136
const CMatrix< C_FLOAT64 > & getRedStoi() const
Definition: CModel.cpp:1154
std::map< std::string, std::string > equations
Definition: CODEExporter.h:153
std::string exportExpression(const CExpression *pExpression, const CCopasiDataModel *pDataModel)
virtual const std::string & getKey() const
const C_FLOAT64 & getInitialValue() const
virtual bool isEmptyString(std::string &str)
const CCopasiVector< CChemEqElement > & getProducts() const
Definition: CChemEq.cpp:63
static Type type(const Type &type)
const CFunction * getFunction() const
Definition: CReaction.cpp:252
iterator end()
const C_FLOAT64 & getQuantity2NumberFactor() const
Definition: CModel.cpp:2354
virtual std::string getSingleLineComment()
bool isReference() const
std::ostringstream ode
Definition: CODEExporter.h:160
std::ostringstream functions
Definition: CODEExporter.h:158
bool exportModelValuesExpressions(const CModel *copasiModel)
const TriLogic & isReversible() const
Definition: CFunction.cpp:145
virtual const std::string & getKey() const
virtual const std::string & getKey() const
virtual bool exportKineticFunctionGroup(const CModel *copasiModel)
virtual bool exportSingleModelEntity(const CModelEntity *tmp, std::string &expression, std::string &comments)
virtual bool addChild(CCopasiNode< Data > *pChild, CCopasiNode< Data > *pAfter=NULL)
Definition: CCopasiNode.h:156
const C_FLOAT64 & getMultiplicity() const
const std::string & getMetaboliteKey() const
CCopasiObject * findObjectFromRefresh(const CCopasiObject *tmp, const Refresh *ref)
const Value & getValue() const
virtual bool exportSingleCompartment(const CCompartment *comp, std::string &expression, std::string &comments)
virtual bool exportSingleParameter(const CCopasiParameter *param, std::string &expression, std::string &comments)
std::string getQuantityParameterOrValue(const std::map< std::string, std::string > &map, const CCopasiDataModel *pDataModel)
bool exportModelEntityExpression(CCopasiObject *obj, const CCopasiDataModel *pDataModel)
virtual std::string translateTimeVariableName()
CCopasiParameter * getParameter(const std::string &name)
const CCopasiVector< CChemEqElement > & getSubstrates() const
Definition: CChemEq.cpp:60
static CFunctionDB * getFunctionList()
size_t getNumIndependentReactionMetabs() const
Definition: CModel.cpp:1130
bool isDependent() const
Definition: CMetab.cpp:989
virtual bool exportSingleODE(const CModelEntity *mentity, std::string &equation, std::string &comments)
virtual std::string exportNumber(double number)
virtual std::string translateObjectName(const std::string &realName)
void modifyTreeForMassAction(CFunction *tmpfunc)
CCopasiVectorNS< CCompartment > & getCompartments()
Definition: CModel.cpp:1145
bool exportReacParamsAndFuncs(const CModel *copasiModel)
bool exportToStream(const CCopasiDataModel *pDataModel, std::ostream &os)
static CKeyFactory * getKeyFactory()
const C_FLOAT64 & getOutputStartTime() const
bool exportModelValues(const CModel *copasiModel)
#define C_FLOAT64
Definition: copasi.h:92
std::map< std::string, std::string > NameMap
Definition: CODEExporter.h:151
const CCopasiParameterGroup & getParameters() const
Definition: CReaction.cpp:333
std::string timeKey
Definition: CODEExporter.h:152
size_t getCompartmentNumber() const
Definition: CReaction.cpp:873
size_t getNumModelValues() const
Definition: CModel.cpp:1139
std::ostringstream assignment
Definition: CODEExporter.h:157
The class for handling a chemical kinetic function.
Definition: CFunction.h:29
CCopasiVectorNS< CReaction > & getReactions()
Definition: CModel.cpp:1039
virtual std::string KineticFunction2ODEmember(const CReaction *reac)
void findFunctionsCalls(const CEvaluationNode *pNode)
virtual bool removeChild(CCopasiNode< Data > *pChild)
Definition: CCopasiNode.h:181
Definition: CModel.h:50
std::set< std::string > mExportedFunctions
Definition: CODEExporter.h:30
virtual bool exportSingleMetabolite(const CMetab *metab, std::string &expression, std::string &comments)
std::ostringstream fixed
Definition: CODEExporter.h:156
void exportObjectNodesFromModel(const CCopasiDataModel *pDataModel)
virtual bool exportClosingData(const CModel *copasiModel, std::ostream &os)
const CModelEntity::Status & getStatus() const
const CCopasiVector< CMetab > & getMetabolitesX() const
Definition: CModel.cpp:1057
bool exportODEs(const CModel *copasiModel)
const std::vector< std::vector< std::string > > & getParameterMappings() const
Definition: CReaction.h:285
CFunction * findFunction(const std::string &functionName)
CCopasiObject * ObjectFromName(const std::vector< CCopasiContainer * > &listOfContainer, const CCopasiObjectName &CN) const
const std::vector< CEvaluationNode * > & getNodeList() const
const CCompartment * getCompartment() const
Definition: CMetab.cpp:222
virtual std::string getDisplayFunctionString(CFunction *func)
size_t getReactionIndex(const CCopasiVector< CReaction > &reacs, const CReaction *react)
const CChemEq & getChemEq() const
Definition: CReaction.cpp:223
CEvaluationNode * getRoot()
bool exportSingleFunction(const CFunction *func)
CFunctionParameters & getVariables()
Definition: CFunction.cpp:148
CCopasiContainer * getObjectParent() const