COPASI API  4.16.103
CSensProblem.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) 2005 - 2007 by Pedro Mendes, Virginia Tech Intellectual
12 // Properties, Inc. and EML Research, gGmbH.
13 // All rights reserved.
14 
15 #include <string>
16 #include <sstream>
17 
18 #include "copasi.h"
19 #include "CSensProblem.h"
21 
23  : mSingleObjectCN(),
24  mListType(CObjectLists::SINGLE_OBJECT)
25 {}
26 
28 {
30 }
31 
33 {
34  mSingleObjectCN = cn;
35  // if (cn != "")
37 }
38 
40 {
41  return mSingleObjectCN;
42 }
43 
44 std::string CSensItem::getSingleObjectDisplayName(const CCopasiDataModel* pDataModel) const
45 {
46  const CCopasiObject* tmpObject = pDataModel->getDataObject(mSingleObjectCN);
47 
48  if (tmpObject)
49  return tmpObject->getObjectDisplayName();
50  else
51  return "";
52 }
53 
55 {
56  mListType = lt;
57 }
58 
60 {
61  return mListType;
62 }
63 
65 {
67 }
68 
69 bool CSensItem::operator==(const CSensItem & rhs) const
70 {
71  if (isSingleObject() != rhs.isSingleObject())
72  return false;
73 
74  if (isSingleObject())
75  {
76  if (getSingleObjectCN() != rhs.getSingleObjectCN())
77  return false;
78  }
79  else if (getListType() != rhs.getListType())
80  return false;
81 
82  return true;
83 }
84 
85 bool CSensItem::operator!=(const CSensItem & rhs) const
86 {
87  return !(*this == rhs);
88 }
89 
90 std::vector<CCopasiObject*> CSensItem::getVariablesPointerList(CCopasiDataModel* pDataModel)
91 {
92  std::vector<CCopasiObject*> ret;
93 
94  if (isSingleObject())
95  {
96  CCopasiObject * tmpObject = pDataModel->getDataObject(getSingleObjectCN());
97 
98  if (!tmpObject) {return ret;} //return empty list
99 
100  if (!tmpObject->isValueDbl()) {return ret;} //return empty list
101 
102  ret.push_back(tmpObject);
103  }
104  else
105  {
106  ret = CObjectLists::getListOfObjects(getListType(), pDataModel->getModel());
107  }
108 
109  return ret;
110 }
111 
112 // size_t CSensItem::dimensionality() const
113 // {
114 //}
115 
116 //************************ CSensProblem ***************************
117 
118 const std::string CSensProblem::SubTaskName[] =
119 {
120  "Evaluation",
121  "Steady State",
122  "Time Series",
123  "Parameter Estimation",
124  "Optimization",
125  "Cross Section",
126  //"Lyapunov Exponents",
127  ""
128 };
129 
130 const char * CSensProblem::XMLSubTask[] =
131 {
132  "Evaluation",
133  "SteadyState",
134  "TimeSeries",
135  "ParameterEstimation",
136  "Optimization",
137  "CrossSection",
138  //"LyapunovExponents",
139  NULL
140 };
141 
142 //static
144 {
145  if (!pg) return;
146 
147  pg->addParameter("SingleObject", CCopasiParameter::CN, CCopasiObjectName(""));
148  pg->addParameter("ObjectListType", CCopasiParameter::UINT, (unsigned C_INT32) 0);
149 }
150 
151 //static
153 {
154  CCopasiObjectName cn("");
155 
156  if (!pg) return; if (!si) return;
157 
158  if (si->isSingleObject())
159  cn = si->getSingleObjectCN();
160 
161  pg->setValue("SingleObject", cn);
162  pg->setValue("ObjectListType", (unsigned C_INT32)si->getListType());
163 }
164 
165 //static
167 {
168 
169  if (!pg) return; if (!si) return;
170 
171  CCopasiObjectName* pCN = pg->getValue("SingleObject").pCN;
172  CObjectLists::ListType* pLT = (CObjectLists::ListType*)pg->getValue("ObjectListType").pUINT;
173 
174  CCopasiObjectName cn("");
175 
176  if (pCN) cn = *pCN;
177 
179 
180  if (pLT) lt = *pLT;
181 
182  // if (cn != "")
183  si->setSingleObjectCN(cn);
184  // else
185  si->setListType(lt);
186 }
187 
188 /**
189  * Default constructor.
190  * @param "CModel *" pModel
191  */
193  CCopasiProblem(CCopasiTask::sens, pParent),
194  mpSubTaskType(NULL),
195  mpTargetFunctions(NULL),
196  mpVariablesGroup(NULL),
197  mpResultAnnotation(NULL),
198  mpScaledResultAnnotation(NULL),
199  mpCollapsedResultAnnotation(NULL)
200 {
201  addParameter("SubtaskType", CCopasiParameter::UINT, (unsigned C_INT32) 0);
203 
204  //---------------------------------------------------
205  addGroup("TargetFunctions");
206  mpTargetFunctions = dynamic_cast<CCopasiParameterGroup*>(getParameter("TargetFunctions"));
208 
209  //---------------------------------------------------
210  addGroup("ListOfVariables");
211  mpVariablesGroup = dynamic_cast<CCopasiParameterGroup*>(getParameter("ListOfVariables"));
212 
213  //create a useful default problem
215 
216  CSensItem item;
217 
219  changeTargetFunctions(item);
220 
222  addVariables(item);
223 
224  // initDebugProblem();
225  initObjects();
227 }
228 
229 /**
230  * Copy constructor.
231  * @param "const CSensProblem &" src
232  */
234  const CCopasiContainer * pParent):
235  CCopasiProblem(src, pParent),
236  mpSubTaskType(NULL),
237  mpTargetFunctions(NULL),
238  mpVariablesGroup(NULL),
239  mpResultAnnotation(NULL),
240  mpScaledResultAnnotation(NULL),
241  mpCollapsedResultAnnotation(NULL)
242 {
244  mpTargetFunctions = dynamic_cast<CCopasiParameterGroup*>(getParameter("TargetFunctions"));
245  mpVariablesGroup = dynamic_cast<CCopasiParameterGroup*>(getParameter("ListOfVariables"));
246 
247  initObjects();
249 }
250 
252 {
253  mpResultAnnotation = new CArrayAnnotation("Sensitivities array", this, &mResult, false);
256 
257  mpScaledResultAnnotation = new CArrayAnnotation("Scaled sensitivities array", this, &mScaledResult, false);
260 
261  mpCollapsedResultAnnotation = new CArrayAnnotation("Summarized sensitivities array", this, &mCollapsedResult, false);
264 }
265 
266 /**
267  * Destructor.
268  */
271 
272 /**
273  * set the problem's SubTaskType:
274  */
275 void
277 {*mpSubTaskType = type;}
278 
279 /**
280  * get the problem's SubTaskType:
281  **/
283 {
284  if (mpSubTaskType)
285  return *mpSubTaskType;
286  else
288 }
289 
291 {
292  CSensItem ret;
294 
296 
297  copyParameterGroupToSensItem(tmp, &ret);
298 
299  return ret;
300 }
301 
303 {
305 }
306 
308 {
309  return mpVariablesGroup->size();
310 }
311 
313 {
314  CSensItem ret;
316 
318 
319  copyParameterGroupToSensItem(tmp, &ret);
320 
321  return ret;
322 }
323 
325 {
326  //create parameter group corresponding to sens item
328  mpVariablesGroup->addGroup("Variables");
330 
332 
333  copySensItemToParameterGroup(&item, tmp);
334 }
335 
337 {
338  size_t imax = mpVariablesGroup->size();
339 
340  for (size_t i = 0; i < imax; ++i)
342 
343  return true;
344 }
345 
347 {
348  return mpVariablesGroup->removeParameter(index);
349 }
350 
351 bool CSensProblem::changeVariables(size_t index, const CSensItem & item)
352 {
353  size_t num = getNumberOfVariables();
354 
355  if (index > num)
356  return false;
357 
358  if (index == num)
359  addVariables(item);
360  else
361  {
362  CCopasiParameterGroup * tmp =
364  copySensItemToParameterGroup(&item, tmp);
365  }
366 
367  return true;
368 }
369 
371 {
372  return mResult;
373 }
374 
376 {
377  return mResult;
378 }
379 
381 {
382  return mpResultAnnotation;
383 }
384 
386 {
387  return mpResultAnnotation;
388 }
389 
391 {
392  return mScaledResult;
393 }
394 
396 {
397  return mScaledResult;
398 }
399 
401 {
403 }
404 
406 {
408 }
409 
411 {
412  return mCollapsedResult;
413 }
414 
416 {
417  return mCollapsedResult;
418 }
419 
421 {
423 }
424 
426 {
428 }
429 
431 {
433 }
434 
435 //static
436 std::vector<CObjectLists::ListType>
438 {
439  std::vector<CObjectLists::ListType> list;
440 
441  //list.push_back(CObjectLists::EMPTY_LIST);
442 
443  // Add new functions here, under applicable SubTaskType case.
444  // Don't forget to provide for a string value in
445  // getTargetFunctionName()
446  switch (type)
447  {
449  list.push_back(CObjectLists::SINGLE_OBJECT);
450  list.push_back(CObjectLists::REACTION_CONC_FLUXES);
451  list.push_back(CObjectLists::REACTION_PART_FLUXES);
452  list.push_back(CObjectLists::METAB_CONC_RATES);
453  list.push_back(CObjectLists::METAB_PART_RATES);
454  list.push_back(CObjectLists::GLOBAL_PARAMETER_RATES);
455  break;
456 
458  list.push_back(CObjectLists::SINGLE_OBJECT);
459  list.push_back(CObjectLists::ALL_VARIABLES);
462  list.push_back(CObjectLists::METAB_CONC_RATES);
463  list.push_back(CObjectLists::METAB_PART_RATES);
465  list.push_back(CObjectLists::REACTION_CONC_FLUXES);
466  list.push_back(CObjectLists::REACTION_PART_FLUXES);
467  list.push_back(CObjectLists::REDUCED_JACOBIAN_EV_RE);
468  list.push_back(CObjectLists::REDUCED_JACOBIAN_EV_IM);
469  break;
470 
472  list.push_back(CObjectLists::SINGLE_OBJECT);
473  list.push_back(CObjectLists::ALL_VARIABLES);
476  list.push_back(CObjectLists::METAB_CONC_RATES);
477  list.push_back(CObjectLists::METAB_PART_RATES);
479  list.push_back(CObjectLists::REACTION_CONC_FLUXES);
480  list.push_back(CObjectLists::REACTION_PART_FLUXES);
481  //TODO all model variables
482  break;
483 
487  list.push_back(CObjectLists::SINGLE_OBJECT);
488  break;
489 
490  /*case (CSensProblem::LyapunovExp):
491  list.push_back(CObjectLists::SINGLE_OBJECT);
492  list.push_back(CObjectLists::NON_CONST_METAB_NUMBERS);
493  list.push_back(CObjectLists::NON_CONST_METAB_CONCENTRATIONS);
494  list.push_back(CObjectLists::REACTION_CONC_FLUXES);
495  list.push_back(CObjectLists::NON_CONST_METAB_PART_RATES);
496  break;*/
497  }
498 
499  return list;
500 }
501 
502 //static
503 std::vector<CObjectLists::ListType>
505 {
506  std::vector<CObjectLists::ListType> list;
507 
508  // the 'unset' type, abusing CObjectLists::ALL_METABS
509  list.push_back((CObjectLists::ListType) 0);
510 
511  // Add new variables here, to applicable SubTaskType case.
512  // Don't forget to provide for a string value in
513  // getVariableName()
514  switch (type)
515  {
516  case (Evaluation):
517  list.push_back(CObjectLists::SINGLE_OBJECT);
519  list.push_back(CObjectLists::METAB_CONCENTRATIONS);
524  list.push_back(CObjectLists::ALL_PARAMETER_VALUES);
525  break;
526 
527  case (SteadyState):
528  list.push_back(CObjectLists::SINGLE_OBJECT);
530  list.push_back(CObjectLists::ALL_PARAMETER_VALUES);
532  //TODO all const values, all model parameters
533  break;
534 
535  case (TimeSeries):
536  list.push_back(CObjectLists::SINGLE_OBJECT);
538  list.push_back(CObjectLists::ALL_PARAMETER_VALUES);
541  //TODO all const values, all model parameters, all initial values
542  break;
543 
546  list.push_back(CObjectLists::SINGLE_OBJECT);
548  list.push_back(CObjectLists::ALL_PARAMETER_VALUES);
551  break;
552 
554  list.push_back(CObjectLists::SINGLE_OBJECT);
556  list.push_back(CObjectLists::ALL_PARAMETER_VALUES);
559  break;
560 
561  /*case (LyapunovExp):
562  list.push_back(CObjectLists::SINGLE_OBJECT);
563  list.push_back(CObjectLists::NON_CONST_METAB_CONCENTRATIONS);
564  list.push_back(CObjectLists::GLOBAL_PARAMETER_VALUES);
565  list.push_back(CObjectLists::ALL_LOCAL_PARAMETER_VALUES);
566  list.push_back(CObjectLists::ALL_PARAMETER_VALUES);
567  list.push_back(CObjectLists::ALL_PARAMETER_AND_INITIAL_VALUES);
568  break;*/
569  }
570 
571  return list;
572 }
573 
574 void CSensProblem::printResult(std::ostream * ostream) const
575 {
576  std::ostream & os = *ostream;
577 
578  //os << "Sensitivities result." << std::endl;
579  if (mpResultAnnotation)
580  os << *mpResultAnnotation << std::endl;
581 
583  os << *mpScaledResultAnnotation << std::endl;
584 }
585 
586 /**
587  * Had to disable the output operator because the datamodel is needed to print
588  * a sensitivity item.
589 std::ostream &operator<<(std::ostream &os, const CSensItem & si)
590 {
591  if (si.isSingleObject())
592  os << si.getSingleObjectDisplayName();
593  else
594  os << si.getListTypeDisplayName();
595 
596  return os;
597 }
598  */
599 
600 std::string CSensItem::print(const CCopasiDataModel* pDataModel) const
601 {
602  std::ostringstream os;
603 
604  if (this->isSingleObject())
605  os << this->getSingleObjectDisplayName(pDataModel);
606  else
607  os << this->getListTypeDisplayName();
608 
609  return os.str();
610 }
611 
612 std::ostream &operator<<(std::ostream &os, const CSensProblem & o)
613 {
614  os << "Function(s) to be derived:" << std::endl;
615  const CCopasiDataModel* pDataModel = o.getObjectDataModel();
616  assert(pDataModel != NULL);
617  os << o.getTargetFunctions().print(pDataModel) << std::endl << std::endl;
618 
619  os << "Calculation to perform: "
620  << CSensProblem::SubTaskName[o.getSubTaskType()] << std::endl << std::endl;
621 
622  size_t i, imax = o.getNumberOfVariables();
623 
624  for (i = 0; i < imax; ++i)
625  {
626  os << "Variable(s) for " << i + 1 << ". derivation:" << std::endl;
627  os << o.getVariables(i).print(pDataModel) << std::endl << std::endl;
628  }
629 
630  return os;
631 }
632 
633 void CSensProblem::print(std::ostream * ostream) const
634 {*ostream << *this;}
635 
637 {
638  CSensItem item;
639 
640  item.setSingleObjectCN(this->getCN());
641  addVariables(item);
642 
644  addVariables(item);
645 }
CCopasiDataModel * getObjectDataModel()
CCopasiArray mScaledResult
Definition: CSensProblem.h:213
CCopasiObject * getDataObject(const CCopasiObjectName &CN) const
std::string getListTypeDisplayName() const
virtual std::string getObjectDisplayName(bool regular=true, bool richtext=false) const
bool collapsRequested() const
size_t getNumberOfVariables() const
CCopasiParameterGroup * mpTargetFunctions
Definition: CSensProblem.h:194
virtual void print(std::ostream *ostream) const
CSensItem getVariables(size_t index) const
static const std::string SubTaskName[]
Definition: CSensProblem.h:82
static const char * XMLSubTask[]
Definition: CSensProblem.h:84
void initDebugProblem()
CSensProblem::SubTaskType getSubTaskType() const
CArrayAnnotation * getCollapsedResultAnnotated()
CArrayAnnotation * mpCollapsedResultAnnotation
Definition: CSensProblem.h:222
void changeTargetFunctions(const CSensItem item)
static std::vector< CObjectLists::ListType > getPossibleVariables(CSensProblem::SubTaskType)
void setDescription(const std::string &s)
#define C_INT32
Definition: copasi.h:90
CCopasiArray mCollapsedResult
Definition: CSensProblem.h:220
std::vector< CCopasiObject * > getVariablesPointerList(CCopasiDataModel *pDataModel)
CArrayAnnotation * getScaledResultAnnotated()
void setSingleObjectCN(const CCopasiObjectName &cn)
CRegisteredObjectName * pCN
static void copyParameterGroupToSensItem(const CCopasiParameterGroup *pg, CSensItem *si)
CCopasiArray & getResult()
void initObjects()
static void createParametersInGroup(CCopasiParameterGroup *pg)
bool removeParameter(const std::string &name)
#define DESTRUCTOR_TRACE
Definition: copasi.h:206
CCopasiParameterGroup * mpVariablesGroup
Definition: CSensProblem.h:201
CSensItem getTargetFunctions() const
bool operator!=(const CSensItem &rhs) const
bool isSingleObject() const
bool addGroup(const std::string &name)
const CObjectLists::ListType & getListType() const
static std::vector< CObjectLists::ListType > getPossibleTargetFunctions(CSensProblem::SubTaskType)
bool operator==(const CSensItem &rhs) const
bool removeVariables()
CArrayAnnotation * getResultAnnotated()
CCopasiArray & getCollapsedResult()
const Value & getValue() const
CObjectLists::ListType mListType
Definition: CSensProblem.h:56
static const std::string ListTypeName[]
Definition: CObjectLists.h:98
CCopasiArray & getScaledResult()
bool setValue(const std::string &name, const CType &value)
unsigned C_INT32 * pUINT
CArrayAnnotation * mpResultAnnotation
Definition: CSensProblem.h:208
std::ostream & operator<<(std::ostream &os, const CSensProblem &o)
virtual void printResult(std::ostream *ostream) const
bool changeVariables(size_t index, const CSensItem &item)
CCopasiParameter * getParameter(const std::string &name)
CCopasiObjectName mSingleObjectCN
Definition: CSensProblem.h:55
void setMode(size_t d, Mode m)
virtual CCopasiObjectName getCN() const
virtual ~CSensProblem()
CSensProblem(const CCopasiContainer *pParent=NULL)
bool addParameter(const CCopasiParameter &parameter)
static std::vector< CCopasiObject * > getListOfObjects(ListType t, const CModel *model)
void addVariables(const CSensItem &item)
bool isValueDbl() const
const CCopasiParameter::Value & getValue(const std::string &name) const
void setListType(CObjectLists::ListType lt)
std::string getSingleObjectDisplayName(const CCopasiDataModel *pDataModel) const
CCopasiArray mResult
Definition: CSensProblem.h:206
std::string print(const CCopasiDataModel *pDataModel) const
void setSubTaskType(const CSensProblem::SubTaskType &type)
static void copySensItemToParameterGroup(const CSensItem *si, CCopasiParameterGroup *pg)
CArrayAnnotation * mpScaledResultAnnotation
Definition: CSensProblem.h:215
const CCopasiObjectName & getSingleObjectCN() const
CSensProblem::SubTaskType * mpSubTaskType
Definition: CSensProblem.h:192
#define CONSTRUCTOR_TRACE
Definition: copasi.h:202