COPASI API  4.16.103
CLNATask.cpp
Go to the documentation of this file.
1 // Copyright (C) 2011 - 2015 by Pedro Mendes, Virginia Tech Intellectual
2 // Properties, Inc., University of Heidelberg, and The University
3 // of Manchester.
4 // All rights reserved.
5 
6 /**
7  * CLNATask class.
8  *
9  * This class implements a linear noise approximation analysis task which is comprised of a
10  * problem and a method.
11  *
12  * Created for Copasi by Juergen Pahle 2010
13  */
14 
15 #include "copasi.h"
16 
17 #include "CLNATask.h"
18 #include "CLNAProblem.h"
19 #include "CLNAMethod.h"
21 
23 #include "model/CModel.h"
24 #include "model/CState.h"
25 #include "report/CKeyFactory.h"
26 #include "report/CReport.h"
27 
28 #define XXXX_Reporting
29 
31  CCopasiTask(CCopasiTask::lna, pParent)
32 {
33  mpProblem = new CLNAProblem(this);
34 
36  this->add(mpMethod, true);
37 }
38 
40  const CCopasiContainer * pParent):
41  CCopasiTask(src, pParent)
42 {
43  mpProblem =
44  new CLNAProblem(*(CLNAProblem *) src.mpProblem, this);
45 
47  this->add(mpMethod, true);
48 }
49 
51 {}
52 
53 // virtual
54 CCopasiMethod * CLNATask::createMethod(const int & type) const
55 {
57 
58  return CLNAMethod::createMethod(Type);
59 }
60 
62 {}
63 
64 void CLNATask::load(CReadConfig & configBuffer)
65 {
66  configBuffer.getVariable("LNA", "bool", &mScheduled,
68 
69  ((CLNAProblem *) mpProblem)->load(configBuffer);
70 
71  ((CLNAMethod *) mpMethod)->load(configBuffer);
72 }
73 
75 {
76  assert(mpProblem && mpMethod);
77 
78  CLNAProblem* pProblem =
79  dynamic_cast<CLNAProblem *>(mpProblem);
80  assert(pProblem);
81 
82  if (!mpMethod->isValidProblem(pProblem)) return false;
83 
84  CLNAMethod * pMethod = dynamic_cast<CLNAMethod*>(mpMethod);
85 
86  if (!pMethod) return false;
87 
88  pMethod->setModel(mpProblem->getModel());
89  pMethod->resizeAllMatrices();
90 
91  return true;
92 }
93 
95  COutputHandler * pOutputHandler,
96  std::ostream * pOstream)
97 {
98  assert(mpProblem && mpMethod);
99 
100  CLNAProblem* pProblem =
101  dynamic_cast<CLNAProblem *>(mpProblem);
102  assert(pProblem);
103 
104  if (!mpMethod->isValidProblem(mpProblem)) return false;
105 
106  //we need to resize and initialize the result matrices before initializing the output
107  if (!updateMatrices()) return false;
108 
109  bool success = true;
110 
111  //initialize reporting
112  if (!CCopasiTask::initialize(of, pOutputHandler, pOstream)) success = false;
113 
114  if (!pProblem->getModel()->compileIfNecessary(mpCallBack)) success = false;
115 
116  CSteadyStateTask *pSubTask = pProblem->getSubTask();
117 
118  if (pSubTask)
119  success = pSubTask->initialize(CCopasiTask::NO_OUTPUT, NULL, mReport.getStream());
120 
121  return success;
122 }
123 
124 bool CLNATask::process(const bool & useInitialValues)
125 {
126  bool success = true;
127  bool stabilityAnalysisRequested = true;
128 
129  assert(mpMethod);
130 
131  CLNAMethod* pMethod = dynamic_cast<CLNAMethod *>(mpMethod);
132  assert(pMethod);
133 
134  CSteadyStateTask *pSubTask =
135  dynamic_cast<CLNAProblem *>(mpProblem)->getSubTask();
136 
137  if (pSubTask)
138  {
139  // LNA needs the calculation of stability information
140  CSteadyStateProblem* pSSProblem = dynamic_cast<CSteadyStateProblem *>(pSubTask->getProblem());
141  assert(pSSProblem);
142  pSSProblem->setStabilityAnalysisRequested(stabilityAnalysisRequested);
143 
144  pSubTask->setCallBack(mpCallBack);
145  pSubTask->process(useInitialValues);
146  CSteadyStateMethod::ReturnCode SSstatus = pSubTask->getResult();
147  pMethod->setSteadyStateStatus(SSstatus);
148  success &= ((SSstatus == CSteadyStateMethod::found) || (SSstatus == CSteadyStateMethod::foundEquilibrium));
149 
150  if (!success && useInitialValues)
151  {
153  }
154 
155  // check for positive or zero Eigen values
156  if (success)
157  {
158  const CEigen &mEigenReduced = pSubTask->getEigenValuesReduced();
159 
160  if ((mEigenReduced.getNposreal() + mEigenReduced.getNimag() + mEigenReduced.getNzero()) > 0)
161  {
163  success = false;
164  }
165  else
166  {
168  }
169  }
170  }
171  else
172  {
174 
175  if (useInitialValues)
176  {
178  }
179 
180  success = false;
181  }
182 
184 
185  success &= pMethod->process();
186 
189 
190  return success;
191 }
192 
194 {
195  bool success = CCopasiTask::restore();
196 
197  CSteadyStateTask *pSubTask =
198  dynamic_cast<CLNAProblem *>(mpProblem)->getSubTask();
199 
200  if (pSubTask)
201  success &= pSubTask->restore();
202 
203  return success;
204 }
205 
206 std::ostream &operator<<(std::ostream &os, const CLNATask & C_UNUSED(A))
207 {
208  os << std::endl;
209 
210  os << std::endl;
211 
212  return os;
213 }
214 
215 void CLNATask::printResult(std::ostream * ostream) const
216 {
217  assert(mpProblem && mpMethod);
218 
219  CLNAProblem* pProblem =
220  dynamic_cast<CLNAProblem *>(mpProblem);
221  assert(pProblem);
222 
223  CLNAMethod* pMethod = dynamic_cast<CLNAMethod *>(mpMethod);
224  assert(pMethod);
225  pMethod->setModel(mpProblem->getModel());
226 
227  std::ostream & os = *ostream;
228 
229  bool showSS = false; //show Steady State result?
230 
231  if (pProblem->isSteadyStateRequested())
232  {
234  {
235  os << "A steady state was found. All coefficients are shown." << std::endl;
236  showSS = true;
237  }
238 
240  {
241  os << "An equilibrium steady state was found. All coefficients are shown." << std::endl;
242  showSS = true;
243  }
244 
246  {
247  os << "Invalid steady state found (negative concentrations)." << std::endl;
248  showSS = true;
249  }
250 
252  {
253  os << "No steady state found." << std::endl;
254  }
255  }
256  else
257  {
258  os << "No steady state calculation was requested." << std::endl;
259  }
260 
261  os << std::endl;
262  os << *pMethod->getCovarianceMatrixAnn() << std::endl;
263 
264  if (showSS)
265  {
266  if (!pProblem->getSubTask())
267  {
268  os << "Problem with steady state calculation. Please report as bug!" << std::endl;
269  return;
270  }
271 
272  os << "Results of the steady state subtask (the state for which the LNA was performed):" << std::endl;
273  os << *pProblem->getSubTask();
274  }
275 }
virtual bool setCallBack(CProcessReport *pCallBack)
const CSteadyStateMethod::ReturnCode & getResult() const
CCopasiMethod * mpMethod
Definition: CCopasiTask.h:239
virtual ~CLNATask()
Definition: CLNATask.cpp:50
virtual bool process(const bool &useInitialValues)
CCopasiProblem * getProblem()
std::ostream & operator<<(std::ostream &os, const CLNATask &C_UNUSED(A))
Definition: CLNATask.cpp:206
const CSteadyStateMethod::ReturnCode & getSteadyStateStatus() const
Definition: CLNAMethod.h:185
Definition: CEigen.h:36
virtual bool initialize(const OutputFlag &of, COutputHandler *pOutputHandler, std::ostream *pOstream)
virtual bool process(const bool &useInitialValues)
Definition: CLNATask.cpp:124
virtual bool process()
Definition: CLNAMethod.cpp:636
bool mScheduled
Definition: CCopasiTask.h:217
virtual void resizeAllMatrices()
Definition: CLNAMethod.cpp:107
CProcessReport * mpCallBack
Definition: CCopasiTask.h:249
static CLNAMethod * createMethod(CCopasiMethod::SubType subType=CCopasiMethod::linearNoiseApproximation)
Definition: CLNAMethod.cpp:21
virtual void output(const COutputInterface::Activity &activity)
const CCopasiMethod::SubType & getSubType() const
#define C_UNUSED(p)
Definition: copasi.h:220
CReport mReport
Definition: CCopasiTask.h:244
void setEigenValueStatus(CLNAMethod::EVStatus status)
Definition: CLNAMethod.cpp:647
virtual CCopasiMethod * createMethod(const int &type) const
Definition: CLNATask.cpp:54
void applyInitialValues()
Definition: CModel.cpp:1236
virtual bool isValidProblem(const CCopasiProblem *pProblem)
CCopasiProblem * mpProblem
Definition: CCopasiTask.h:234
const size_t & getNimag() const
Definition: CEigen.cpp:661
virtual bool updateMatrices()
Definition: CLNATask.cpp:74
std::ostream * getStream() const
Definition: CReport.cpp:392
const CArrayAnnotation * getCovarianceMatrixAnn() const
Definition: CLNAMethod.h:110
void printResult(std::ostream *ostream) const
Definition: CLNATask.cpp:215
const size_t & getNposreal() const
Definition: CEigen.cpp:674
virtual bool restore()
Definition: CLNATask.cpp:193
CSteadyStateTask * getSubTask() const
CLNATask(const CCopasiContainer *pParent=NULL)
Definition: CLNATask.cpp:30
void setModel(CModel *model)
Definition: CLNAMethod.cpp:591
const CEigen & getEigenValuesReduced() const
bool compileIfNecessary(CProcessReport *pProcessReport)
Definition: CModel.cpp:612
virtual bool initialize(const OutputFlag &of, COutputHandler *pOutputHandler, std::ostream *pOstream)
void load(CReadConfig &configBuffer)
Definition: CLNATask.cpp:64
void setSteadyStateStatus(CSteadyStateMethod::ReturnCode SSStatus)
Definition: CLNAMethod.cpp:642
virtual bool add(CCopasiObject *pObject, const bool &adopt=true)
void setStabilityAnalysisRequested(bool &stabilityAnalysisRequested)
C_INT32 getVariable(const std::string &name, const std::string &type, void *pout, CReadConfig::Mode mode=CReadConfig::NEXT)
Definition: CReadConfig.cpp:81
virtual bool restore()
bool isSteadyStateRequested() const
const size_t & getNzero() const
Definition: CEigen.cpp:208
virtual bool restore()
void cleanup()
Definition: CLNATask.cpp:61
virtual bool initialize(const OutputFlag &of, COutputHandler *pOutputHandler, std::ostream *pOstream)
Definition: CLNATask.cpp:94
CModel * getModel() const