COPASI API  4.16.103
CopasiSE.cpp
Go to the documentation of this file.
1 // Begin CVS Header
2 // $Source: /Volumes/Home/Users/shoops/cvs/copasi_dev/copasi/CopasiSE/CopasiSE.cpp,v $
3 // $Revision: 1.57 $
4 // $Name: $
5 // $Author: shoops $
6 // $Date: 2012/06/01 17:26:43 $
7 // End CVS Header
8 
9 // Copyright (C) 2012 - 2010 by Pedro Mendes, Virginia Tech Intellectual
10 // Properties, Inc., University of Heidelberg, and The University
11 // of Manchester.
12 // All rights reserved.
13 
14 // Copyright (C) 2008 by Pedro Mendes, Virginia Tech Intellectual
15 // Properties, Inc., EML Research, gGmbH, University of Heidelberg,
16 // and The University of Manchester.
17 // All rights reserved.
18 
19 // Copyright (C) 2001 - 2007 by Pedro Mendes, Virginia Tech Intellectual
20 // Properties, Inc. and EML Research, gGmbH.
21 // All rights reserved.
22 
23 // Main
24 //
25 // (C) Stefan Hoops 2002
26 //
27 
28 #include <stdlib.h>
29 #include <sstream>
30 #include <string>
31 #include <iostream>
32 
33 #define COPASI_MAIN
34 
35 #include "copasi.h"
36 
41 #include "utilities/CCopasiTask.h"
44 #include "commandline/COptions.h"
45 #include "function/CFunctionDB.h"
47 #include "function/CFunction.h"
48 
50 #include "report/CCopasiTimer.h"
51 #include "report/CKeyFactory.h"
52 #include "utilities/CVersion.h"
53 #include "utilities/CDirEntry.h"
56 
57 void writeLogo();
58 int validate();
59 int exportSBML();
60 
61 int main(int argc, char *argv[])
62 {
63  int retcode = 0;
64  CProcessReport * pProcessReport = NULL;
65  int MaxTime = 0;
66 
67 #ifdef XXXX
68  C_FLOAT64 sparseness = 0.00;
69  SparseMatrixTest(10, sparseness, 0, false, true, true, false);
70  SparseMatrixTest(25, sparseness, 0, false, true, true, false);
71  SparseMatrixTest(50, sparseness, 0, false, true, true, false);
72  SparseMatrixTest(100, sparseness, 0, false, true, true, false);
73  SparseMatrixTest(250, sparseness, 0, false, true, true, false);
74  SparseMatrixTest(500, sparseness, 0, false, true, true, false);
75  SparseMatrixTest(1000, sparseness, 0, false, true, true, false);
76  SparseMatrixTest(2500, sparseness, 0, false, true, true, false);
77  SparseMatrixTest(5000, sparseness, 0, false, true, true, false);
78 
79  return 0;
80 #endif // XXXX
81 
82  try
83  {
84  // Create the root container.
85  CCopasiRootContainer::init(argc, argv);
86  }
87 
88  catch (copasi::autoexcept &e)
89  {
90  writeLogo();
91 
92  switch (e.get_autothrow_id())
93  {
95  std::cerr << "Usage: " << CDirEntry::baseName(argv[0]) << " [options] [file]\n";
96  std::cerr << e.what();
97  break;
98  }
99 
100  retcode = 0;
101  goto finish;
102  }
103 
104  catch (copasi::option_error &e)
105  {
106  writeLogo();
107  std::cerr << CDirEntry::baseName(argv[0]) << ": " << e.what() << "\n";
108  std::cerr << e.get_help_comment() << std::endl;
109 
110  retcode = 1;
111  goto finish;
112  }
113 
114  writeLogo();
115 
116  bool License;
117  COptions::getValue("License", License);
118 
119  if (License)
120  {
121  std::cout << CCopasiRootContainer::getLicenseTxt() << std::endl;
122 
123  retcode = 0;
124  goto finish;
125  }
126 
127  COptions::getValue("MaxTime", MaxTime);
128 
129  if (MaxTime > 0)
130  {
131  pProcessReport = new CProcessReport(MaxTime);
132  }
133 
134  try
135  {
136  // Create the global data model.
138  assert(pDataModel != NULL);
139 
140 #ifdef XXXX
141  CCallParameters<C_FLOAT64> Variables(20);
142  size_t j, i, imax = Variables.size();
143  CRandom * pRandom = CRandom::createGenerator();
144 
145  for (i = 0; i < imax; i++)
146  {
147  C_FLOAT64 * pValue = new C_FLOAT64;
148  *pValue = 100.0 * pRandom->getRandomOO();
149  Variables[i].value = pValue;
150  }
151 
152  CCopasiTimer * pCPU =
153  const_cast<CCopasiTimer *>(static_cast<const CCopasiTimer *>(CCopasiRootContainer::getObject((std::string)"CN=Root,Timer=CPU Time")));
154  CCopasiTimer * pWall =
155  const_cast<CCopasiTimer *>(static_cast<const CCopasiTimer *>(CCopasiRootContainer::getObject((std::string)"CN=Root,Timer=Wall Clock Time")));
156 
158  CCopasiRootContainer::getFnctionList()->loadedFunctions();
159  CFunction * pFunction;
160 
161  for (i = 0, imax = Functions.size(); i < imax; i++)
162  {
163  pFunction = dynamic_cast<CFunction *>(Functions[i]);
164 
165  if (pFunction->getType() != CEvaluationTree::MassAction)
166  for (j = 0; j < 100000; j++)
167  pFunction->calcValue(Variables);
168  }
169 
170  pCPU->refresh();
171  pWall->refresh();
172 #endif // XXXX
173 
174  // Check whether we just have to validate
175  bool Validate = false;
176  COptions::getValue("Validate", Validate);
177 
179 
180  if (!COptions::compareValue("ImportSBML", std::string("")))
181  {
182  // Import the SBML File
183  std::string ImportSBML;
184  COptions::getValue("ImportSBML", ImportSBML);
185 
186  if (!pDataModel->importSBML(ImportSBML))
187  {
188  std::cerr << "SBML Import File: " << ImportSBML << std::endl;
189  std::cerr << CCopasiMessage::getAllMessageText() << std::endl;
190 
191  retcode = 1;
192  goto finish;
193  }
194 
195  // Validate and exit
196  if (Validate)
197  {
198  retcode = validate();
199  goto finish;
200  }
201 
202  // Check whether exporting to SBML is requested.
203  if (!COptions::compareValue("ExportSBML", std::string("")))
204  {
205  retcode = exportSBML();
206  goto finish;
207  }
208 
209  // Check whether exporting to C code is requested.
210  if (!COptions::compareValue("ExportC", std::string("")))
211  {
212  // Export the C code File
213  std::string ExportC;
214  COptions::getValue("ExportC", ExportC);
215 
216  if (!pDataModel->exportMathModel(ExportC, NULL, "C Files (*.c)", true))
217  {
218  std::cerr << "C File: " << ExportC << std::endl;
219  std::cerr << CCopasiMessage::getAllMessageText() << std::endl;
220  retcode = 1;
221  }
222 
223  goto finish;
224  }
225 
226  // Check whether exporting to Berkeley Madonna is requested.
227  if (!COptions::compareValue("ExportBerkeleyMadonna", std::string("")))
228  {
229  // Export the Berkeley Madonna File
230  std::string ExportBerkeleyMadonna;
231  COptions::getValue("ExportBerkeleyMadonna", ExportBerkeleyMadonna);
232 
233  if (!pDataModel->exportMathModel(ExportBerkeleyMadonna, NULL, "Berkeley Madonna Files (*.mmd)", true))
234  {
235  std::cerr << "Berkeley Madonna File: " << ExportBerkeleyMadonna << std::endl;
236  std::cerr << CCopasiMessage::getAllMessageText() << std::endl;
237  retcode = 1;
238  }
239 
240  goto finish;
241  }
242 
243  // Check whether exporting to XPPAUT is requested.
244  if (!COptions::compareValue("ExportXPPAUT", std::string("")))
245  {
246  // Export the Berkeley Madonna File
247  std::string ExportXPPAUT;
248  COptions::getValue("ExportXPPAUT", ExportXPPAUT);
249 
250  if (!pDataModel->exportMathModel(ExportXPPAUT, NULL, "XPPAUT (*.ode)", true))
251  {
252  std::cerr << "XPPAUT File: " << ExportXPPAUT << std::endl;
253  std::cerr << CCopasiMessage::getAllMessageText() << std::endl;
254  retcode = 1;
255  }
256 
257  goto finish;
258  }
259 
260  // If no export file was given, we write to the save file or
261  // the default file.
262  if (COptions::compareValue("ExportSBML", std::string("")))
263  {
264  std::string Save;
265  COptions::getValue("Save", Save);
266 
267  if (!pDataModel->saveModel(Save, NULL, true))
268  {
269  assert(CCopasiRootContainer::getDatamodelList()->size() != 0);
270  std::cerr << "Save File: " << pDataModel->getFileName() << std::endl;
271  std::cerr << CCopasiMessage::getAllMessageText() << std::endl;
272 
273  retcode = 1;
274  goto finish;
275  }
276  }
277  }
278  else
279  {
280  COptions::nonOptionType::const_iterator it = Files.begin();
281  COptions::nonOptionType::const_iterator end = Files.end();
282 
283  if (it == end) // Create a usage message
284  {
285  std::string Self;
286  COptions::getValue("Self", Self);
287 
288  char * Argv[2];
289  Argv[0] = strdup(Self.c_str());
290  Argv[1] = strdup("--help");
291 
292  copasi::COptionParser Parser;
293 
294  try
295  {
296  Parser.parse(2, Argv);
297  }
298 
299  catch (copasi::autoexcept &e)
300  {
301  switch (e.get_autothrow_id())
302  {
304  std::cerr << "Usage: " << CDirEntry::baseName(argv[0]) << " [options] [file]\n";
305  std::cerr << e.what();
306  break;
307  }
308  }
309 
310  retcode = 1;
311  goto finish;
312  }
313 
314  for (; it != end; ++it)
315  {
316  if (!pDataModel->loadModel(*it, NULL))
317  {
318  std::cerr << "File: " << *it << std::endl;
319  std::cerr << CCopasiMessage::getAllMessageText() << std::endl;
320 
321  retcode = 1;
322  continue;
323  }
324 
325  // Validate and exit
326  if (Validate)
327  {
328  retcode |= validate();
329  continue;
330  }
331 
332  // Check whether exporting to SBML is requested.
333  if (!COptions::compareValue("ExportSBML", std::string("")))
334  {
335  retcode = exportSBML();
336 
337  // Since only one export file name can be specified we
338  // stop execution.
339  break;
340  }
341 
342  // Check whether exporting to C code is requested.
343  if (!COptions::compareValue("ExportC", std::string("")))
344  {
345  // Export the C code File
346  std::string ExportC;
347  COptions::getValue("ExportC", ExportC);
348 
349  if (!pDataModel->exportMathModel(ExportC, NULL, "C Files (*.c)", true))
350  {
351  std::cerr << "C File: " << ExportC << std::endl;
352  std::cerr << CCopasiMessage::getAllMessageText() << std::endl;
353 
354  retcode = 1;
355  }
356 
357  // Since only one export file name can be specified we
358  // stop execution.
359  break;
360  }
361 
362  // Check whether exporting to Berkeley Madonna is requested.
363  if (!COptions::compareValue("ExportBerkeleyMadonna", std::string("")))
364  {
365  // Export the Berkeley Madonna File
366  std::string ExportBerkeleyMadonna;
367  COptions::getValue("ExportBerkeleyMadonna", ExportBerkeleyMadonna);
368 
369  if (!pDataModel->exportMathModel(ExportBerkeleyMadonna, NULL, "Berkeley Madonna Files (*.mmd)", true))
370  {
371  std::cerr << "Berkeley Madonna File: " << ExportBerkeleyMadonna << std::endl;
372  std::cerr << CCopasiMessage::getAllMessageText() << std::endl;
373 
374  retcode = 1;
375  }
376 
377  // Since only one export file name can be specified we
378  // stop execution.
379  break;
380  }
381 
382  // Check whether exporting to XPPAUT is requested.
383  if (!COptions::compareValue("ExportXPPAUT", std::string("")))
384  {
385  // Export the Berkeley Madonna File
386  std::string ExportXPPAUT;
387  COptions::getValue("ExportXPPAUT", ExportXPPAUT);
388 
389  if (!pDataModel->exportMathModel(ExportXPPAUT, NULL, "XPPAUT (*.ode)", true))
390  {
391  std::cerr << "XPPAUT File: " << ExportXPPAUT << std::endl;
392  std::cerr << CCopasiMessage::getAllMessageText() << std::endl;
393 
394  retcode = 1;
395  }
396 
397  // Since only one export file name can be specified we
398  // stop execution.
399  break;
400  }
401 
402  CCopasiVectorN< CCopasiTask > & TaskList = * pDataModel->getTaskList();
403  size_t i, imax = TaskList.size();
404 
405  for (i = 0; i < imax; i++)
406  if (TaskList[i]->isScheduled())
407  {
408  TaskList[i]->getProblem()->setModel(pDataModel->getModel());
409  TaskList[i]->setCallBack(pProcessReport);
410 
411  bool success = true;
412 
413  try
414  {
415  success = TaskList[i]->initialize(CCopasiTask::OUTPUT_SE, pDataModel, NULL);
416 
417  // We need to check whether the result is saved in any form.
418  // If not we need to stop right here to avoid wasting time.
420  (!TaskList[i]->isUpdateModel() ||
421  COptions::compareValue("Save", std::string(""))))
422  {
423  success = false;
424  }
425 
426  if (success)
427  success &= TaskList[i]->process(true);
428  }
429 
430  catch (...)
431  {
432  success = false;
433  }
434 
435  TaskList[i]->restore();
436 
437  if (!success)
438  {
439  std::cerr << "File: " << pDataModel->getFileName() << std::endl;
440  std::cerr << "Task: " << TaskList[i]->getObjectName() << std::endl;
441  std::cerr << CCopasiMessage::getAllMessageText() << std::endl;
442 
443  retcode = 1;
444  }
445 
446  if (pProcessReport != NULL)
447  {
448  pProcessReport->finish();
449  }
450 
451  TaskList[i]->setCallBack(NULL);
452  pDataModel->finish();
453  }
454 
455  // Check whether a file for saving the resulting model is given
456  if (!COptions::compareValue("Save", std::string("")))
457  {
458  std::string Save;
459  COptions::getValue("Save", Save);
460 
461  if (!pDataModel->saveModel(Save, NULL, true))
462  {
463  std::cerr << "Save File: " << Save << std::endl;
464  std::cerr << CCopasiMessage::getAllMessageText() << std::endl;
465 
466  retcode = 1;
467  }
468 
469  // Since only one save file name can be specified we
470  // stop execution.
471  break;
472  }
473  }
474  }
475  }
476 
477  catch (CCopasiException & Exception)
478  {
479  std::cerr << "Unhandled Exception:" << std::endl;
480  std::cerr << Exception.getMessage().getText() << std::endl;
481  }
482 
483 
484 finish:
486  pdelete(pProcessReport);
487 
488  return retcode;
489 }
490 
491 void writeLogo()
492 {
493  bool NoLogo = false;
494  COptions::getValue("NoLogo", NoLogo);
495 
496  if (NoLogo) return;
497 
498  std::cout << "COPASI "
499  << CVersion::VERSION.getVersion() << std::endl
500  << "The use of this software indicates the acceptance of the attached license." << std::endl
501  << "To view the license please use the option: --license" << std::endl
502  << std::endl;
503 }
504 
505 int validate()
506 {
507  int retcode = 0;
508 
509  // We are already sure that the COPASI model compiled. That means
510  // we only need to test the active tasks
511  assert(CCopasiRootContainer::getDatamodelList()->size() != 0);
513  CCopasiVectorN< CCopasiTask > & TaskList = * pDataModel->getTaskList();
514  size_t i, imax = TaskList.size();
515 
516  for (i = 0; i < imax; i++)
517  if (TaskList[i]->isScheduled())
518  {
519  bool success = true;
520  TaskList[i]->getProblem()->setModel(pDataModel->getModel());
521 
522  try
523  {
524  success =
525  TaskList[i]->initialize(CCopasiTask::OUTPUT_SE, pDataModel, NULL);
526 
527  // We need to check whether the result is saved in any form.
528  // If not we need to stop right here to avoid wasting time.
530  (!TaskList[i]->isUpdateModel() ||
531  COptions::compareValue("Save", std::string(""))))
532  success = false;
533  }
534 
535  catch (...)
536  {
537  success = false;
538  }
539 
540  TaskList[i]->restore();
541 
542  if (!success)
543  {
544  std::cerr << "File: " << pDataModel->getFileName() << std::endl;
545  std::cerr << "Task: " << TaskList[i]->getObjectName() << std::endl;
546  std::cerr << CCopasiMessage::getAllMessageText() << std::endl;
547 
548  retcode = 1;
549  }
550 
551  pDataModel->finish();
552  }
553 
554  return retcode;
555 }
556 
558 {
559  int retcode = 0;
560 
561  // Export the SBML File
562  std::string ExportSBML;
563  COptions::getValue("ExportSBML", ExportSBML);
564  copasi::SBMLSchema_enum SBMLSchema;
565  COptions::getValue("SBMLSchema", SBMLSchema);
566 
567  int Level;
568  int Version;
569 
570  switch (SBMLSchema)
571  {
573  Level = 1;
574  Version = 1;
575  break;
576 
578  Level = 1;
579  Version = 2;
580  break;
581 
583  Level = 2;
584  Version = 1;
585  break;
586 
588  Level = 2;
589  Version = 2;
590  break;
591 
593  Level = 2;
594  Version = 3;
595  break;
596 
598  Level = 2;
599  Version = 4;
600  break;
601 
603  Level = 3;
604  Version = 1;
605  break;
606 
607  default:
608  Level = 2;
609  Version = 4;
610  break;
611  }
612 
613  assert(CCopasiRootContainer::getDatamodelList()->size() != 0);
614 
615  if (!(*CCopasiRootContainer::getDatamodelList())[0]->exportSBML(ExportSBML, true, Level, Version))
616  {
617  std::cerr << "SBML Export File: " << ExportSBML << std::endl;
618  std::cerr << CCopasiMessage::getAllMessageText() << std::endl;
619  retcode = 1;
620  }
621 
622  return retcode;
623 }
#define pdelete(p)
Definition: copasi.h:215
autothrow get_autothrow_id(void) const
virtual bool finish()
const std::string & getObjectName() const
virtual C_FLOAT64 getRandomOO()
Definition: CRandom.cpp:254
virtual size_t size() const
const std::string & getText() const
const CCopasiMessage & getMessage() const
static std::string getAllMessageText(const bool &chronological=true)
bool importSBML(const std::string &fileName, CProcessReport *pImportHandler=NULL, const bool &deleteOldData=true)
const CEvaluationTree::Type & getType() const
const char * get_help_comment(void) const
static std::string baseName(const std::string &path)
Definition: CDirEntry.cpp:98
void writeLogo()
Definition: CopasiSE.cpp:491
const std::string & getFileName() const
static const nonOptionType & getNonOptions()
Definition: COptions.cpp:251
#define MCCopasiTask
static bool compareValue(const std::string &name, const CType &value)
Definition: COptions.h:128
static CRandom * createGenerator(CRandom::Type type=CRandom::mt19937, unsigned C_INT32 seed=0)
Definition: CRandom.cpp:49
static const char * getLicenseTxt()
CCopasiVectorN< CCopasiTask > * getTaskList()
static CCopasiVector< CCopasiDataModel > * getDatamodelList()
bool loadModel(std::istream &in, const std::string &pwd, CProcessReport *pProcessReport, const bool &deleteOldData=true)
bool saveModel(const std::string &fileName, CProcessReport *pProcessReport, bool overwriteFile=false, const bool &autoSave=false)
virtual const C_FLOAT64 & calcValue(const CCallParameters< C_FLOAT64 > &callParameters)
Definition: CFunction.cpp:159
static void getValue(const std::string &name, CType &value)
Definition: COptions.h:124
static CCopasiDataModel * addDatamodel()
#define C_FLOAT64
Definition: copasi.h:92
static bool checkForMessage(const size_t &number)
void parse(int argc, char *argv[], bool call_finalize=true)
parse the command line
size_t size() const
int main(int argc, char *argv[])
Definition: CopasiSE.cpp:61
The class for handling a chemical kinetic function.
Definition: CFunction.h:29
int validate()
Definition: CopasiSE.cpp:505
bool exportMathModel(const std::string &fileName, CProcessReport *pProcessReport, const std::string &filter, bool overwriteFile=false)
static void init(int argc, char *argv[], const bool &withGui=false)
std::vector< std::string > nonOptionType
Definition: COptions.h:109
virtual void finish()
virtual const CObjectInterface * getObject(const CCopasiObjectName &cn) const
const std::string & getVersion() const
Definition: CVersion.cpp:139
int exportSBML()
Definition: CopasiSE.cpp:557
static const CVersion VERSION
Definition: CVersion.h:164