COPASI API  4.16.103
CScanTask.cpp
Go to the documentation of this file.
1 // Copyright (C) 2010 - 2014 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) 2003 - 2007 by Pedro Mendes, Virginia Tech Intellectual
12 // Properties, Inc. and EML Research, gGmbH.
13 // All rights reserved.
14 
15 /**
16  * CScanTask class.
17  *
18  * This class implements a scan task which is comprised of a
19  * of a problem and a method.
20  *
21  */
22 #include "copasi.h"
23 #include "CScanTask.h"
24 #include "CScanProblem.h"
25 #include "CScanMethod.h"
26 #include "utilities/CReadConfig.h"
27 #include "report/CKeyFactory.h"
28 #include "report/CReport.h"
29 
30 #include "model/CModel.h"
31 #include "model/CState.h"
32 
34 
39 #include "lna/CLNAProblem.h"
40 #include "lna/CLNATask.h"
46 
48  CCopasiTask(CCopasiTask::scan, pParent),
49  mProgress(0),
50  mhProgress(C_INVALID_INDEX),
51  mpSubtask(NULL),
52  mOutputInSubtask(false),
53  mUseInitialValues(true)
54 {
55  mpProblem = new CScanProblem(this);
57  this->add(mpMethod, true);
58  static_cast< CScanMethod * >(mpMethod)->setProblem(static_cast< CScanProblem * >(mpProblem));
59 }
60 
62  const CCopasiContainer * pParent):
63  CCopasiTask(src, pParent),
64  mProgress(0),
65  mhProgress(C_INVALID_INDEX),
66  mpSubtask(NULL),
67  mOutputInSubtask(false),
68  mUseInitialValues(true)
69 {
70  mpProblem = new CScanProblem(*(CScanProblem *) src.mpProblem, this);
72  this->add(mpMethod, true);
73  static_cast< CScanMethod * >(mpMethod)->setProblem(static_cast< CScanProblem * >(mpProblem));
74 }
75 
77 {cleanup();}
78 
79 // virtual
80 CCopasiMethod * CScanTask::createMethod(const int & type) const
81 {
83 
84  return CScanMethod::createMethod(Type);
85 }
86 
88 {}
89 
91  COutputHandler * pOutputHandler,
92  std::ostream * pOstream)
93 {
94  assert(mpProblem && mpMethod);
95 
97 
98  bool success = true;
99 
100  if ((of & REPORT) &&
101  pOutputHandler != NULL)
102  {
103  if (mReport.open(getObjectDataModel(), pOstream))
104  pOutputHandler->addInterface(&mReport);
105  else
107  }
108 
109  success &= initSubtask(of, pOutputHandler, mReport.getStream());
110  success &= CCopasiTask::initialize(of, pOutputHandler, mReport.getStream());
111 
112  return success;
113 }
114 
115 void CScanTask::load(CReadConfig & C_UNUSED(configBuffer))
116 {}
117 
118 bool CScanTask::process(const bool & useInitialValues)
119 {
120  if (!mpProblem) fatalError();
121 
122  if (!mpMethod) fatalError();
123 
124  CScanProblem * pProblem = dynamic_cast<CScanProblem *>(mpProblem);
125 
126  if (!pProblem) fatalError();
127 
128  CScanMethod * pMethod = dynamic_cast<CScanMethod *>(mpMethod);
129 
130  if (!pMethod) fatalError();
131 
132  bool success = true;
133 
134  CCrossSectionTask* task = dynamic_cast<CCrossSectionTask*>(mpSubtask);
135 
136  if (task != NULL)
137  task->createEvent();
138 
139  if (useInitialValues)
140  {
142  }
143 
144  //TODO: reports
145 
146  // initialize the method (parsing the ScanItems)
147  pMethod->setProblem(pProblem);
148 
149  if (!pMethod->init()) return false;
150 
151  // init progress bar
152  mProgress = 0;
153 
154  if (mpCallBack)
155  {
156  mpCallBack->setName("performing parameter scan...");
157 
158  unsigned C_INT32 totalSteps = (unsigned C_INT32) pMethod->getTotalNumberOfSteps();
159  mhProgress = mpCallBack->addItem("Number of Steps",
160  mProgress,
161  &totalSteps);
162 
163  if (mpSubtask)
165  }
166 
167  // init output handler (plotting)
169 
170  //calling the scanner, output is done in the callback
171  if (!pMethod->scan()) success = false;
172 
173  if (task != NULL)
174  task->removeEvent();
175 
176  //finishing progress bar and output
177  //if (mpCallBack) mpCallBack->finish();
178  //if (mpOutputHandler) mpOutputHandler->finish();
180 
181  if (mpSubtask)
182  mpSubtask->setCallBack(NULL);
183 
184  return success;
185 }
186 
188 {
189  bool success = true;
190 
191  if (mpSubtask != NULL)
192  {
193  success &= mpSubtask->restore();
194  }
195 
196  success = CCopasiTask::restore();
197 
198  return success;
199 }
200 
202 {
203  bool success = mpSubtask->process(mUseInitialValues);
204 
205  //do output
206  if (success && !mOutputInSubtask)
208 
209  if (mpSubtask->isUpdateModel())
210  {
211  COptProblem* problem = dynamic_cast<COptProblem*>(mpSubtask->getProblem());
212 
213  if (problem != NULL)
214  {
215  problem->restoreModel(true);
216  }
217  }
218 
219  //do progress bar
220  ++mProgress;
221 
223 
224  return true;
225 }
226 
228 {
229  if ((!isLast) || mOutputInSubtask)
231 
232  return true;
233 }
234 
235 bool CScanTask::initSubtask(const OutputFlag & /* of */,
236  COutputHandler * pOutputHandler,
237  std::ostream * pOstream)
238 {
239  if (!mpProblem) fatalError();
240 
241  CScanProblem * pProblem = dynamic_cast<CScanProblem *>(mpProblem);
242 
243  if (!pProblem) fatalError();
244 
245  //get the parameters from the problem
246  CCopasiTask::Type type = *(CCopasiTask::Type*) pProblem->getValue("Subtask").pUINT;
247  CCopasiDataModel* pDataModel = getObjectDataModel();
248  assert(pDataModel != NULL);
249 
250  switch (type)
251  {
253  mpSubtask = dynamic_cast<CCopasiTask*>
254  ((*pDataModel->getTaskList())["Steady-State"]);
255  break;
256 
258  mpSubtask = dynamic_cast<CCopasiTask*>
259  ((*pDataModel->getTaskList())["Time-Course"]);
260  break;
261 
262  case CCopasiTask::mca:
263  mpSubtask = dynamic_cast<CCopasiTask*>
264  ((*pDataModel->getTaskList())["Metabolic Control Analysis"]);
265  break;
266 
267  case CCopasiTask::lyap:
268  mpSubtask = dynamic_cast<CCopasiTask*>
269  ((*pDataModel->getTaskList())["Lyapunov Exponents"]);
270  break;
271 
273  mpSubtask = dynamic_cast<CCopasiTask*>
274  ((*pDataModel->getTaskList())["Optimization"]);
275  break;
276 
278  mpSubtask = dynamic_cast<CCopasiTask*>
279  ((*pDataModel->getTaskList())["Parameter Estimation"]);
280  break;
281 
282  case CCopasiTask::sens:
283  mpSubtask = dynamic_cast<CCopasiTask*>
284  ((*pDataModel->getTaskList())["Sensitivities"]);
285  break;
286 
287  case CCopasiTask::lna:
288  mpSubtask = dynamic_cast<CCopasiTask*>
289  ((*pDataModel->getTaskList())["Linear Noise Approximation"]);
290  break;
291 
293  mpSubtask = dynamic_cast<CCopasiTask*>
294  ((*pDataModel->getTaskList())[CCopasiTask::TypeName[tssAnalysis]]);
295  break;
296 
298  mpSubtask = dynamic_cast<CCopasiTask*>
299  ((*pDataModel->getTaskList())["Cross Section"]);
300  break;
301 
302  default:
303  mpSubtask = NULL;
304  }
305 
306  mOutputInSubtask = * pProblem->getValue("Output in subtask").pBOOL;
308 
309  if (!mpSubtask) return false;
310 
311  mpSubtask->getProblem()->setModel(pDataModel->getModel()); //TODO
312  mpSubtask->setCallBack(NULL);
313 
314  if (mOutputInSubtask)
315  return mpSubtask->initialize(OUTPUT, pOutputHandler, pOstream);
316  else
317  return mpSubtask->initialize(NO_OUTPUT, pOutputHandler, pOstream);
318 
319  return true;
320 }
321 
323 {
324  CScanProblem * pProblem = dynamic_cast< CScanProblem * >(mpProblem);
325 
326  if (pProblem == NULL) return;
327 
328  pProblem->fixBuild81();
329 
330  return;
331 }
CCopasiDataModel * getObjectDataModel()
virtual bool setCallBack(CProcessReport *pCallBack)
CCopasiMethod * mpMethod
Definition: CCopasiTask.h:239
virtual void separate(const COutputInterface::Activity &activity)
static const std::string TypeName[]
Definition: CCopasiTask.h:67
CCopasiProblem * getProblem()
void setProblem(CScanProblem *problem)
const std::string & getObjectName() const
static CScanMethod * createMethod(CCopasiMethod::SubType subType=CCopasiMethod::scanMethod)
void restoreModel(const bool &updateModel)
bool mOutputInSubtask
Definition: CScanTask.h:59
bool mUseInitialValues
Definition: CScanTask.h:61
virtual bool process(const bool &useInitialValues)
Definition: CScanTask.cpp:118
virtual bool initialize(const OutputFlag &of, COutputHandler *pOutputHandler, std::ostream *pOstream)
const bool & isUpdateModel() const
virtual bool setName(const std::string &name)
CScanTask(const CCopasiContainer *pParent=NULL)
Definition: CScanTask.cpp:47
CProcessReport * mpCallBack
Definition: CCopasiTask.h:249
#define fatalError()
bool outputSeparatorCallback(bool isLast=false)
Definition: CScanTask.cpp:227
#define C_INVALID_INDEX
Definition: copasi.h:222
virtual void output(const COutputInterface::Activity &activity)
bool initSubtask(const OutputFlag &of, COutputHandler *pOutputHandler, std::ostream *pOstream)
Definition: CScanTask.cpp:235
#define C_UNUSED(p)
Definition: copasi.h:220
CReport mReport
Definition: CCopasiTask.h:244
#define C_INT32
Definition: copasi.h:90
void applyInitialValues()
Definition: CModel.cpp:1236
void cleanup()
Definition: CScanTask.cpp:87
virtual bool progressItem(const size_t &handle)
virtual bool setModel(CModel *pModel)
virtual bool isValidProblem(const CCopasiProblem *pProblem)
void load(CReadConfig &configBuffer)
Definition: CScanTask.cpp:115
CCopasiProblem * mpProblem
Definition: CCopasiTask.h:234
#define MCCopasiTask
virtual void addInterface(COutputInterface *pInterface)
unsigned C_INT32 mProgress
Definition: CScanTask.h:47
CCopasiTask * mpSubtask
Definition: CScanTask.h:57
bool processCallback()
Definition: CScanTask.cpp:201
std::ostream * getStream() const
Definition: CReport.cpp:392
size_t addItem(const std::string &name, const std::string &value, const std::string *pEndValue=NULL)
CCopasiVectorN< CCopasiTask > * getTaskList()
virtual bool initialize(const OutputFlag &of, COutputHandler *pOutputHandler, std::ostream *pOstream)
Definition: CScanTask.cpp:90
unsigned C_INT32 * pUINT
virtual bool restore()
Definition: CScanTask.cpp:187
bool getContinueFromCurrentState() const
virtual bool process(const bool &useInitialValues)
size_t mhProgress
Definition: CScanTask.h:52
std::ostream * open(const CCopasiDataModel *pDataModel, std::ostream *pOstream=NULL)
Definition: CReport.cpp:336
size_t getTotalNumberOfSteps() const
Definition: CScanMethod.h:243
virtual bool add(CCopasiObject *pObject, const bool &adopt=true)
const CCopasiParameter::Value & getValue(const std::string &name) const
virtual CCopasiMethod * createMethod(const int &type) const
Definition: CScanTask.cpp:80
virtual bool restore()
void fixBuild81()
Definition: CScanTask.cpp:322
CModel * getModel() const