COPASI API  4.16.103
CLyapTask.cpp
Go to the documentation of this file.
1 // Begin CVS Header
2 // $Source: /Volumes/Home/Users/shoops/cvs/copasi_dev/copasi/lyap/CLyapTask.cpp,v $
3 // $Revision: 1.20 $
4 // $Name: $
5 // $Author: shoops $
6 // $Date: 2012/06/15 15:31:12 $
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 /**
24  * CLyapTask class.
25  */
26 
27 #include <string>
28 
29 #include "copasi.h"
30 
31 #include "CLyapTask.h"
32 #include "CLyapProblem.h"
33 #include "CLyapMethod.h"
34 #include "model/CModel.h"
35 #include "model/CState.h"
36 #include "report/CKeyFactory.h"
37 #include "report/CReport.h"
43 #include "report/CCopasiTimer.h"
44 
45 #define XXXX_Reporting
46 
47 //this is about the preliminary reporting. The first LYAP_NUM_REF exponents
48 //get an individual object reference. This will be obsolete when we have
49 //working annotated vectors
50 #define LYAP_NUM_REF 10
51 
53  CCopasiTask(CCopasiTask::lyap, pParent),
54  //mTimeSeriesRequested(true),
55  //mTimeSeries(),
56  mpLyapProblem(NULL),
57  mpLyapMethod(NULL),
58  mLocalExponents(),
59  mExponents(),
60  mSumOfExponents(0.0),
61  mSumOfLocalExponents(0.0),
62  mIntervalDivergence(0.0),
63  mAverageDivergence(0.0),
64  mResultAvailable(false),
65  mResultHasDivergence(false),
66  mModelVariablesInResult(0)
67 {
68  mpProblem = new CLyapProblem(this);
69  mpMethod =
71  this->add(mpMethod, true);
72 
73  initObjects();
74 }
75 
77 {
78  cleanup();
79 }
80 
82 {}
83 
85 {
86  size_t i;
87 
89 
90  for (i = 0; i < LYAP_NUM_REF; ++i) mExponents[i] = 0.0;
91 
92  mvExpRef.resize(LYAP_NUM_REF);
93 
94  mLocalExponents.resize(LYAP_NUM_REF);
95 
96  for (i = 0; i < LYAP_NUM_REF; ++i) mLocalExponents[i] = 0.0;
97 
98  mvLocExpRef.resize(LYAP_NUM_REF);
99 
102 
103  for (i = 0; i < LYAP_NUM_REF; ++i)
104  {
105  std::ostringstream sss;
106  sss << "Exponent " << i + 1;
107  mvExpRef[i] = dynamic_cast<CCopasiObjectReference<C_FLOAT64> *>
109  assert(mvExpRef[i]);
110 
111  std::ostringstream sss2;
112  sss2 << "Local exponent " << i + 1;
115  assert(mvLocExpRef[i]);
116  }
117 
120  //addObjectReference("Local divergence", mDivergence, CCopasiObject::ValueDbl);
123 }
124 
126  COutputHandler * pOutputHandler,
127  std::ostream * pOstream)
128 {
129  assert(mpProblem && mpMethod);
130 
131  mpLyapProblem = dynamic_cast<CLyapProblem *>(mpProblem);
132  assert(mpLyapProblem);
133 
134  mpLyapMethod = dynamic_cast<CLyapMethod *>(mpMethod);
135  assert(mpLyapMethod);
136 
138 
139  bool success = mpMethod->isValidProblem(mpProblem);
140 
141  size_t nnn;
142 
145  else
146  nnn = LYAP_NUM_REF;
147 
148  mLocalExponents.resize(nnn);
149  mExponents.resize(nnn);
150 
151  //update object references because the object may have been
152  //moved by the resize above
153  size_t i;
154 
155  for (i = 0; i < LYAP_NUM_REF; ++i)
156  {
157  mvExpRef[i]->setReference(mExponents.array()[i]);
158  mvLocExpRef[i]->setReference(mLocalExponents.array()[i]);
159  }
160 
161  if (!CCopasiTask::initialize(of, pOutputHandler, pOstream)) success = false;
162 
163  //mTimeSeriesRequested = mpLyapProblem->timeSeriesRequested();
164 
165  return success;
166 }
167 
168 bool CLyapTask::process(const bool & useInitialValues)
169 {
170  if (useInitialValues)
172 
174 
175  // bool flagProceed = true;
176  mPercentage = 0;
177 
178  if (mpCallBack)
179  {
180  mpCallBack->setName("performing lyapunov exponent calculation...");
181  C_FLOAT64 hundred = 100;
182  mhProcess = mpCallBack->addItem("Completion",
183  mPercentage,
184  &hundred);
185  }
186 
187  try
188  {
190  }
191  catch (CCopasiException & Exception)
192  {
193  //mpLyapProblem->getModel()->setState(*mpCurrentState);
195 
198 
200 
202 
203  throw CCopasiException(Exception.getMessage());
204  }
205 
207 
210 
211  mResultAvailable = true;
215 
216  return true;
217 }
218 
220 {
221  bool success = CCopasiTask::restore();
222 
223  if (mUpdateModel)
224  {
225  CModel * pModel = mpProblem->getModel();
226 
227  //TODO
228  //pModel->setState(*mpCurrentState);
229  pModel->updateSimulatedValues(true);
230  pModel->setInitialState(pModel->getState());
231  pModel->updateInitialValues();
232  }
233 
234  return success;
235 }
236 
237 bool CLyapTask::setMethodType(const int & type)
238 {
240 
241  if (!CLyapMethod::isValidSubType(Type)) return false;
242 
243  if (mpMethod->getSubType() == Type) return true;
244 
245  pdelete(mpMethod);
246  mpMethod = createMethod(Type);
247  this->add(mpMethod, true);
248 
249  return true;
250 }
251 
252 // virtual
253 CCopasiMethod * CLyapTask::createMethod(const int & type) const
254 {
256 
257  return CLyapMethod::createMethod(Type);
258 }
259 
260 bool CLyapTask::methodCallback(const C_FLOAT64 & percentage, bool onlyProgress)
261 {
262  if (!onlyProgress)
263  {
266  }
267 
268  mPercentage = percentage;
269 
270  if (mpCallBack)
271  {
273  }
274 
275  return true;
276 }
277 
279 {
280  mSumOfExponents = 0;
282 
284 
285  for (i = 0; i < imax; ++i)
286  {
289  }
290 }
291 
292 void CLyapTask::printResult(std::ostream * ostream) const
293 {
294  std::ostream & os = *ostream;
295 
296  // CCopasiTimeVariable CPUTime = const_cast<COptProblem *>(this)->mCPUTime.getElapsedTime();
297  // os << " CPU Time [s]:\t"
298  // << CCopasiTimeVariable::LL2String(CPUTime.getSeconds(), 1) << "."
299  // << CCopasiTimeVariable::LL2String(CPUTime.getMilliSeconds(true), 3) << std::endl;
300 
301  os << "Lyapunov Exponents:" << std::endl;
302 
303  if (!mpLyapProblem) //this means that task was not yet executed
304  {
305  os << "No results available." << std::endl;
306  return;
307  }
308 
309  unsigned C_INT32 i, imax = mpLyapProblem->getExponentNumber();
310 
311  for (i = 0; i < imax; ++i)
312  os << mExponents[i] << " ";
313 
314  os << std::endl;
315 
317  os << std::endl << "Average divergence: " << mAverageDivergence << std::endl;
318 }
319 
321 {
322  return mResultAvailable;
323 }
324 
326 {
327  return mResultHasDivergence;
328 }
329 
331 {
333 }
334 
336 {
338 }
virtual bool initialize(const OutputFlag &of, COutputHandler *pOutputHandler, std::ostream *pOstream)
Definition: CLyapTask.cpp:125
size_t modelVariablesInResult() const
Definition: CLyapTask.cpp:330
CCopasiMethod * mpMethod
Definition: CCopasiTask.h:239
#define pdelete(p)
Definition: copasi.h:215
void setProblem(CLyapProblem *problem)
CVector< C_FLOAT64 > mLocalExponents
Definition: CLyapTask.h:58
virtual bool setMethodType(const int &type)
Definition: CLyapTask.cpp:237
virtual CCopasiMethod * createMethod(const int &type) const
Definition: CLyapTask.cpp:253
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
size_t numberOfExponentsCalculated() const
Definition: CLyapTask.cpp:335
CLyapTask(const CCopasiContainer *pParent=NULL)
Definition: CLyapTask.cpp:52
void initObjects()
Definition: CLyapTask.cpp:84
bool methodCallback(const C_FLOAT64 &percentage, bool onlyProgress)
Definition: CLyapTask.cpp:260
bool resultAvailable() const
Definition: CLyapTask.cpp:320
void resize(size_t size, const bool &copy=false)
Definition: CVector.h:301
virtual void output(const COutputInterface::Activity &activity)
const CCopasiMethod::SubType & getSubType() const
#define C_INT32
Definition: copasi.h:90
std::vector< CCopasiObjectReference< C_FLOAT64 > * > mvLocExpRef
Definition: CLyapTask.h:80
void applyInitialValues()
Definition: CModel.cpp:1236
virtual bool progressItem(const size_t &handle)
size_t mhProcess
Definition: CLyapTask.h:76
virtual bool isValidProblem(const CCopasiProblem *pProblem)
CCopasiProblem * mpProblem
Definition: CCopasiTask.h:234
C_FLOAT64 mPercentage
Definition: CLyapTask.h:75
size_t addItem(const std::string &name, const std::string &value, const std::string *pEndValue=NULL)
C_FLOAT64 mIntervalDivergence
Definition: CLyapTask.h:67
size_t getNumIndependent() const
Definition: CState.cpp:342
std::vector< CCopasiObjectReference< C_FLOAT64 > * > mvExpRef
Definition: CLyapTask.h:79
CVector< C_FLOAT64 > mExponents
Definition: CLyapTask.h:59
static CLyapMethod * createMethod(CCopasiMethod::SubType subType=CCopasiMethod::lyapWolf)
Definition: CLyapMethod.cpp:58
virtual bool finishItem(const size_t &handle)
bool mUpdateModel
Definition: CCopasiTask.h:223
const unsigned C_INT32 & getExponentNumber() const
virtual bool process(const bool &useInitialValues)
Definition: CLyapTask.cpp:168
static bool isValidSubType(const CCopasiMethod::SubType &subType)
Definition: CLyapMethod.cpp:48
size_t mNumExponentsCalculated
Definition: CLyapTask.h:85
void cleanup()
Definition: CLyapTask.cpp:81
void printResult(std::ostream *ostream) const
Definition: CLyapTask.cpp:292
virtual bool restore()
Definition: CLyapTask.cpp:219
#define C_FLOAT64
Definition: copasi.h:92
C_FLOAT64 mSumOfLocalExponents
Definition: CLyapTask.h:62
CType * array()
Definition: CVector.h:139
virtual bool calculate()=0
size_t mModelVariablesInResult
Definition: CLyapTask.h:84
bool divergenceRequested() const
C_FLOAT64 mSumOfExponents
Definition: CLyapTask.h:61
virtual bool add(CCopasiObject *pObject, const bool &adopt=true)
bool updateInitialValues()
Definition: CModel.cpp:1461
bool resultHasDivergence() const
Definition: CLyapTask.cpp:325
bool mResultAvailable
Definition: CLyapTask.h:82
#define LYAP_NUM_REF
Definition: CLyapTask.cpp:50
CCopasiObject * addVectorReference(const std::string &name, CType &reference, const unsigned C_INT32 &flag=0)
Definition: CModel.h:50
C_FLOAT64 mAverageDivergence
Definition: CLyapTask.h:72
const CState & getState() const
Definition: CModel.cpp:1771
virtual bool restore()
CLyapProblem * mpLyapProblem
Definition: CLyapTask.h:48
void calculationsBeforeOutput()
Definition: CLyapTask.cpp:278
bool mResultHasDivergence
Definition: CLyapTask.h:83
CLyapMethod * mpLyapMethod
Definition: CLyapTask.h:53
CCopasiObject * addObjectReference(const std::string &name, CType &reference, const unsigned C_INT32 &flag=0)
CModel * getModel() const