COPASI API  4.16.103
CTSSATask.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 /**
16  * CTSSATask class.
17  *
18  * This class implements a time scale separation analysis task which is comprised of a
19  * of a problem and a method. Additionally calls to the reporting
20  * methods are done when initialized.
21  *
22  */
23 
24 #include <string>
25 
26 #include "copasi.h"
27 
28 #include "CTSSATask.h"
29 #include "CTSSAProblem.h"
30 #include "CTSSAMethod.h"
31 #include "model/CModel.h"
32 #include "model/CState.h"
33 #include "report/CKeyFactory.h"
34 #include "report/CReport.h"
38 
39 #define XXXX_Reporting
40 
41 bool tfle(const C_FLOAT64 & d1, const C_FLOAT64 & d2)
42 {return (d1 <= d2);}
43 
44 bool tfl(const C_FLOAT64 & d1, const C_FLOAT64 & d2)
45 {return (d1 < d2);}
46 
47 bool tble(const C_FLOAT64 & d1, const C_FLOAT64 & d2)
48 {return (d1 >= d2);}
49 
50 bool tbl(const C_FLOAT64 & d1, const C_FLOAT64 & d2)
51 {return (d1 > d2);}
52 
53 const unsigned int CTSSATask::ValidMethods[] =
54 {
59 };
60 
62  CCopasiTask(CCopasiTask::tssAnalysis, pParent),
63  mTimeSeriesRequested(true),
64  mTimeSeries(),
65  mpTSSAProblem(NULL),
66  mpTSSAMethod(NULL),
67  mpCurrentState(NULL),
68  mpCurrentTime(NULL)
69 {
70  mpProblem = new CTSSAProblem(this);
72  this->add(mpMethod, true);
73 
74  CCopasiParameter * pParameter = mpMethod->getParameter("Integrate Reduced Model");
75 
76  if (pParameter != NULL)
77  mUpdateMoieties = *pParameter->getValue().pBOOL;
78  else
79  mUpdateMoieties = false;
80 }
81 
83  const CCopasiContainer * pParent):
84  CCopasiTask(src, pParent),
85  mTimeSeriesRequested(src.mTimeSeriesRequested),
86  mTimeSeries(),
87  mpTSSAProblem(NULL),
88  mpTSSAMethod(NULL),
89  mpCurrentState(NULL),
90  mpCurrentTime(NULL)
91 {
92  mpProblem =
93  new CTSSAProblem(*static_cast< CTSSAProblem * >(src.mpProblem), this);
94 
96  * mpMethod = * src.mpMethod;
97  mpMethod->elevateChildren();
98 
99  this->add(mpMethod, true);
100 
101  CCopasiParameter * pParameter = mpMethod->getParameter("Integrate Reduced Model");
102 
103  if (pParameter != NULL)
104  mUpdateMoieties = *pParameter->getValue().pBOOL;
105  else
106  mUpdateMoieties = false;
107 }
108 
110 {
111  cleanup();
112 }
113 
115 {
117 }
118 
120 {
121  assert(mpProblem != NULL && mpMethod != NULL);
122 
123  assert(dynamic_cast<CTSSAProblem *>(mpProblem) != NULL);
124 
125  if (!mpMethod->isValidProblem(mpProblem)) return false;
126 
127  CTSSAMethod * pMethod = dynamic_cast<CTSSAMethod*>(mpMethod);
128 
129  if (!pMethod) return false;
130 
131  pMethod->setModel(mpProblem->getModel());
132 
133  pMethod->predifineAnnotation();
134 
135  return true;
136 }
137 
139  COutputHandler * pOutputHandler,
140  std::ostream * pOstream)
141 {
142  assert(mpProblem && mpMethod);
143 
144  mpTSSAProblem = dynamic_cast<CTSSAProblem *>(mpProblem);
145  assert(mpTSSAProblem);
146 
147  mpTSSAMethod = dynamic_cast<CTSSAMethod *>(mpMethod);
148  assert(mpTSSAMethod);
149 
151 
152  bool success = mpMethod->isValidProblem(mpProblem);
153 
154  CCopasiParameter * pParameter = mpMethod->getParameter("Integrate Reduced Model");
155 
156  if (pParameter != NULL)
157  mUpdateMoieties = *pParameter->getValue().pBOOL;
158  else
159  mUpdateMoieties = false;
160 
164 
165  // Handle the time series as a regular output.
167 
168  if (pOutputHandler != NULL)
169  {
171  {
173  pOutputHandler->addInterface(&mTimeSeries);
174  }
175  else
176  {
177  mTimeSeries.clear();
178  }
179  }
180 
181 //NEW
182 
184 
186 
187 //
188 
189  if (!CCopasiTask::initialize(of, pOutputHandler, pOstream)) success = false;
190 
191  return success;
192 }
193 
194 bool CTSSATask::process(const bool & useInitialValues)
195 {
196  //*****
197 
198  processStart(useInitialValues);
199 
200  //*****
201 
202  C_FLOAT64 StepSize = mpTSSAProblem->getStepSize();
203  C_FLOAT64 NextTimeToReport;
204 
205  const C_FLOAT64 EndTime = *mpCurrentTime + mpTSSAProblem->getDuration();
206  const C_FLOAT64 StartTime = *mpCurrentTime;
207 
208  C_FLOAT64 StepNumber = (mpTSSAProblem->getDuration()) / StepSize;
209 
210  bool (*LE)(const C_FLOAT64 &, const C_FLOAT64 &);
211  bool (*L)(const C_FLOAT64 &, const C_FLOAT64 &);
212 
213  if (StepSize < 0.0)
214  {
215  LE = &tble;
216  L = &tbl;
217  }
218  else
219  {
220  LE = &tfle;
221  L = &tfl;
222  }
223 
224  size_t StepCounter = 1;
225 
226  C_FLOAT64 outputStartTime = mpTSSAProblem->getOutputStartTime();
227 
228  if (StepSize == 0.0 && mpTSSAProblem->getDuration() != 0.0)
229  {
231  return false;
232  }
233 
235 
236  bool flagProceed = true;
237  C_FLOAT64 handlerFactor = 100.0 / mpTSSAProblem->getDuration();
238 
239  C_FLOAT64 Percentage = 0;
240  size_t hProcess;
241 
242  if (mpCallBack)
243  {
244  mpCallBack->setName("performing simulation...");
245  C_FLOAT64 hundred = 100;
246  hProcess = mpCallBack->addItem("Completion",
247  Percentage,
248  &hundred);
249  }
250 
251  //if ((*LE)(outputStartTime, *mpCurrentTime)) output(COutputInterface::DURING);
252 
253  try
254  {
255  do
256  {
257  // This is numerically more stable then adding
258  // mpTSSAProblem->getStepSize().
259  NextTimeToReport =
260  StartTime + (EndTime - StartTime) * StepCounter++ / StepNumber;
261 
262  flagProceed &= processStep(NextTimeToReport);
263 
264  if (mpCallBack)
265  {
266  Percentage = (*mpCurrentTime - StartTime) * handlerFactor;
267  flagProceed &= mpCallBack->progressItem(hProcess);
268  }
269 
270  if ((*LE)(outputStartTime, *mpCurrentTime))
271  {
273  }
274  }
275  while ((*L)(*mpCurrentTime, EndTime) && flagProceed);
276  }
277 
278  catch (int)
279  {
282 
283  if ((*LE)(outputStartTime, *mpCurrentTime))
284  {
286  }
287 
288  if (mpCallBack) mpCallBack->finishItem(hProcess);
289 
291 
293  }
294 
295  catch (CCopasiException & Exception)
296  {
299 
300  if ((*LE)(outputStartTime, *mpCurrentTime))
301  {
303  }
304 
305  if (mpCallBack) mpCallBack->finishItem(hProcess);
306 
308 
309  throw CCopasiException(Exception.getMessage());
310  }
311 
312  if (mpCallBack) mpCallBack->finishItem(hProcess);
313 
315 
316  return true;
317 }
318 
319 void CTSSATask::processStart(const bool & useInitialValues)
320 {
321  if (useInitialValues)
323 
325 
328 
329  return;
330 }
331 
332 bool CTSSATask::processStep(const C_FLOAT64 & nextTime)
333 {
334  C_FLOAT64 CompareTime = nextTime - 100.0 * fabs(nextTime) * std::numeric_limits< C_FLOAT64 >::epsilon();
335 
336  if (*mpCurrentTime <= CompareTime)
337  {
338  do
339  {
340  mpTSSAMethod->step(nextTime - *mpCurrentTime);
341 
342  if (*mpCurrentTime > CompareTime) break;
343 
344  /* Here we will do conditional event processing */
345 
346  /* Currently this is correct since no events are processed. */
348  }
349  while (true);
350 
353 
354  return true;
355  }
356 
357  CompareTime = nextTime + 100.0 * fabs(nextTime) * std::numeric_limits< C_FLOAT64 >::epsilon();
358 
359  if (*mpCurrentTime >= CompareTime)
360  {
361  do
362  {
363  mpTSSAMethod->step(nextTime - *mpCurrentTime);
364 
365  if (*mpCurrentTime < CompareTime) break;
366 
367  /* Here we will do conditional event processing */
368 
369  /* Currently this is correct since no events are processed. */
371  }
372  while (true);
373 
376 
377  return true;
378  }
379 
380  // Current time is approximately nextTime;
381  return false;
382 }
383 
385 {
386  bool success = CCopasiTask::restore();
387 
388  if (mUpdateModel)
389  {
390  CModel * pModel = mpProblem->getModel();
391 
392  pModel->setState(*mpCurrentState);
394  pModel->setInitialState(pModel->getState());
395  pModel->updateInitialValues();
396  }
397 
398  return success;
399 }
400 
401 bool CTSSATask::setMethodType(const int & type)
402 {
404 
405  if (!isValidMethod(Type, ValidMethods)) return false;
406 
407  if (mpMethod->getSubType() == Type) return true;
408 
409  pdelete(mpMethod);
410  mpMethod = createMethod(Type);
411  this->add(mpMethod, true);
412  //mpMethod->setObjectParent(this);
413 
414  return true;
415 }
416 
417 // virtual
418 CCopasiMethod * CTSSATask::createMethod(const int & type) const
419 {
421 
422  return CTSSAMethod::createMethod(Type);
423 }
424 
426 {return mpCurrentState;}
427 
429 {return mTimeSeries;}
#define MCTSSAMethod
const C_FLOAT64 & getOutputStartTime() const
CCopasiMethod * mpMethod
Definition: CCopasiTask.h:239
#define pdelete(p)
Definition: copasi.h:215
CTSSAMethod * mpTSSAMethod
Definition: CTSSATask.h:72
void setCurrentState(CState *currentState)
bool timeSeriesRequested() const
bool tfle(const C_FLOAT64 &d1, const C_FLOAT64 &d2)
Definition: CTSSATask.cpp:41
const C_FLOAT64 * mpCurrentTime
Definition: CTSSATask.h:87
const CTimeSeries & getTimeSeries() const
Definition: CTSSATask.cpp:428
virtual bool initialize(const OutputFlag &of, COutputHandler *pOutputHandler, std::ostream *pOstream)
Definition: CTSSATask.cpp:138
void clear()
Definition: CTimeSeries.cpp:96
virtual bool initialize(const OutputFlag &of, COutputHandler *pOutputHandler, std::ostream *pOstream)
const CCopasiMessage & getMessage() const
virtual bool setName(const std::string &name)
void setInitialState(const CState &state)
Definition: CModel.cpp:1774
void updateSimulatedValues(const bool &updateMoieties)
Definition: CModel.cpp:1851
CProcessReport * mpCallBack
Definition: CCopasiTask.h:249
CTSSAProblem * mpTSSAProblem
Definition: CTSSATask.h:67
virtual void predifineAnnotation()
static CTSSAMethod * createMethod(CCopasiMethod::SubType subType=CCopasiMethod::unset)
Definition: CTSSAMethod.cpp:44
void allocate(const size_t &steps)
Definition: CTimeSeries.cpp:73
Definition: CState.h:305
virtual bool updateMatrices()
Definition: CTSSATask.cpp:119
bool tfl(const C_FLOAT64 &d1, const C_FLOAT64 &d2)
Definition: CTSSATask.cpp:44
virtual void output(const COutputInterface::Activity &activity)
const CCopasiMethod::SubType & getSubType() const
void applyInitialValues()
Definition: CModel.cpp:1236
virtual bool restore()
Definition: CTSSATask.cpp:384
virtual bool progressItem(const size_t &handle)
virtual bool isValidProblem(const CCopasiProblem *pProblem)
const C_FLOAT64 & getStepSize() const
CCopasiProblem * mpProblem
Definition: CCopasiTask.h:234
CState * mpCurrentState
Definition: CTSSATask.h:82
bool tbl(const C_FLOAT64 &d1, const C_FLOAT64 &d2)
Definition: CTSSATask.cpp:50
virtual void addInterface(COutputInterface *pInterface)
virtual bool setMethodType(const int &type)
Definition: CTSSATask.cpp:401
CTSSATask(const CCopasiContainer *pParent=NULL)
Definition: CTSSATask.cpp:61
void setState(const CState &state)
Definition: CModel.cpp:1785
size_t addItem(const std::string &name, const std::string &value, const std::string *pEndValue=NULL)
const Value & getValue() const
const unsigned C_INT32 & getStepNumber() const
void setModel(CModel *model)
virtual bool finishItem(const size_t &handle)
void cleanup()
Definition: CTSSATask.cpp:114
CCopasiParameter * getParameter(const std::string &name)
bool mUpdateModel
Definition: CCopasiTask.h:223
CTimeSeries mTimeSeries
Definition: CTSSATask.h:62
#define MCTSSAProblem
static const unsigned int ValidMethods[]
Definition: CTSSATask.h:50
bool mUpdateMoieties
Definition: CTSSATask.h:77
const C_FLOAT64 & getTime() const
Definition: CState.cpp:325
virtual bool process(const bool &useInitialValues)
Definition: CTSSATask.cpp:194
virtual CCopasiMethod * createMethod(const int &type) const
Definition: CTSSATask.cpp:418
#define C_FLOAT64
Definition: copasi.h:92
void setProblem(CTSSAProblem *problem)
virtual void start(const CState *initialState)
static bool isValidMethod(const unsigned int &method, const unsigned int *validMethods)
Definition: CCopasiTask.cpp:89
bool tble(const C_FLOAT64 &d1, const C_FLOAT64 &d2)
Definition: CTSSATask.cpp:47
virtual bool add(CCopasiObject *pObject, const bool &adopt=true)
bool updateInitialValues()
Definition: CModel.cpp:1461
void processStart(const bool &useInitialValues)
Definition: CTSSATask.cpp:319
Definition: CModel.h:50
const CState & getState() const
Definition: CModel.cpp:1771
virtual bool restore()
CState * getState()
Definition: CTSSATask.cpp:425
bool processStep(const C_FLOAT64 &nextTime)
Definition: CTSSATask.cpp:332
virtual void step(const double &deltaT)
CModel * getModel() const
bool mTimeSeriesRequested
Definition: CTSSATask.h:57
const C_FLOAT64 & getDuration() const