COPASI API  4.16.103
CMoiety.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) 2001 - 2007 by Pedro Mendes, Virginia Tech Intellectual
12 // Properties, Inc. and EML Research, gGmbH.
13 // All rights reserved.
14 
15 #include <stdio.h>
16 #include <sstream>
17 
18 #include "copasi.h"
19 #include "CMoiety.h"
20 #include "CModel.h"
21 #include "CCompartment.h"
22 #include "CMetabNameInterface.h"
23 
25 #include "report/CKeyFactory.h"//By G
26 #include "function/CExpression.h"
28 #include "utilities/CReadConfig.h"
30 #include "utilities/utility.h"
32 
33 // static
35 
36 CMoiety::CMoiety(const std::string & name,
37  const CCopasiContainer * pParent):
38  CCopasiContainer(name, pParent, "Moiety"),
39  mKey(CCopasiRootContainer::getKeyFactory()->add("Moiety", this)), //By G
40  mNumber(0),
41  mINumber(0),
42  mIAmount(0),
43  mEquation(),
44  mpINumberReference(NULL),
45  mpNumberReference(NULL),
46  mpDNumberReference(NULL),
47  mpConversionFactor(&CMoiety::DefaultFactor)
48 {
49  initObjects();
51 }
52 
54  const CCopasiContainer * pParent):
55  CCopasiContainer(src, pParent),
56  mKey(CCopasiRootContainer::getKeyFactory()->add("Moiety", this)), //By G
57  mNumber(src.mNumber),
58  mINumber(src.mINumber),
59  mIAmount(src.mIAmount),
60  mEquation(src.mEquation),
61  mpINumberReference(NULL),
62  mpNumberReference(NULL),
63  mpDNumberReference(NULL),
64  mpConversionFactor(src.mpConversionFactor)
65 {
66  initObjects();
68 }
69 
71 {
74 }
75 
77 {
79 
80  mpINumberReference = new CTotalNumberReference("InitialValue", this, mINumber);
82 
83  mpNumberReference = new CTotalNumberReference("Value", this, mNumber);
85 
86  mpDNumberReference = new CDependentNumberReference("DependentValue", this, mNumber);
88 
90  pObject->setRefresh(this, &CMoiety::refreshAmount);
92 
93  return;
94 }
95 
96 void CMoiety::add(C_FLOAT64 value, CMetab * pMetabolite)
97 {
98  if (!mEquation.size())
99  pMetabolite->setDependentOn(this);
100  else
102 
105 
106  std::pair<C_FLOAT64, CMetab *> element;
107 
108  element.first = value;
109  element.second = pMetabolite;
110 
111  mEquation.push_back(element);
112 }
113 
115 {
117  mEquation.clear();
120 }
121 
123 {
124  mNumber = mINumber;
125 
126  std::vector< std::pair< C_FLOAT64, CMetab * > >::iterator it = mEquation.begin() + 1;
127  std::vector< std::pair< C_FLOAT64, CMetab * > >::iterator end = mEquation.end();
128 
129  for (; it != end; ++it)
130  mNumber -= it->first * it->second->getValue();
131 
132  // It is save to update the particle number of the dependent species here.
133  // We are mimicking the behavior of the new math container by doing it.
134  mEquation.begin()->second->setValue(mNumber);
135 
136  return;
137 }
138 
140 {
142  return mNumber;
143 }
144 
146 {return mNumber;}
147 
149 {return mpNumberReference;}
150 
152 {return mpDNumberReference;}
153 
154 const std::string & CMoiety::getKey() const {return mKey;} //By G
155 
157 {
158  bool success = CCopasiContainer::setObjectParent(pParent);
160 
161  return success;
162 }
163 
164 std::string CMoiety::getDescription(const CModel * model) const
165 {
166  std::ostringstream Description;
167  Description.imbue(std::locale::classic());
168  Description.precision(16);
169 
170  std::vector< std::pair< C_FLOAT64, CMetab * > >::const_iterator it = mEquation.begin();
171  std::vector< std::pair< C_FLOAT64, CMetab * > >::const_iterator end = mEquation.end();
172 
173  for (; it != end; ++it)
174  {
175  if (it != mEquation.begin())
176  {
177  if (it->first < 0.0)
178  Description << " - ";
179  else
180  Description << " + ";
181  }
182 
183  if (fabs(it->first) > 1.0 + 100.0 * std::numeric_limits< C_FLOAT64 >::epsilon() ||
184  fabs(it->first) < 1.0 - 100.0 * std::numeric_limits< C_FLOAT64 >::epsilon())
185  Description << fabs(it->first) << "*";
186 
187  Description << CMetabNameInterface::getDisplayName(model, *it->second, false);
188  }
189 
190  return Description.str();
191 }
192 
194 {
195  mINumber = 0;
196 
197  std::vector< std::pair< C_FLOAT64, CMetab * > >::iterator it = mEquation.begin();
198  std::vector< std::pair< C_FLOAT64, CMetab * > >::iterator end = mEquation.end();
199 
200  for (; it != end; ++it)
201  mINumber += it->first * it->second->getInitialValue();
202 
203  return;
204 }
205 
207 {return mpINumberReference;}
208 
210 {
211  return mINumber;
212 }
213 
215 {
216  std::vector< std::pair< C_FLOAT64, CMetab * > >::iterator it = mEquation.begin();
217  std::vector< std::pair< C_FLOAT64, CMetab * > >::iterator end = mEquation.end();
218 
219  for (; it != end; ++it)
220  mINumber += it->first * it->second->getValue();
221 }
222 
224 {
225  return mpNumberReference;
226 }
227 
228 std::string CMoiety::getExpression() const
229 {
230  std::ostringstream Infix;
231  Infix.imbue(std::locale::classic());
232  Infix.precision(16);
233 
234  std::vector< std::pair< C_FLOAT64, CMetab * > >::const_iterator it = mEquation.begin();
235  std::vector< std::pair< C_FLOAT64, CMetab * > >::const_iterator end = mEquation.end();
236 
237  for (; it != end; ++it)
238  {
239  if (it != mEquation.begin())
240  {
241  if (it->first < 0.0)
242  Infix << "-";
243  else
244  Infix << "+";
245  }
246 
247  if (fabs(it->first) > 1.0 + 100.0 * std::numeric_limits< C_FLOAT64 >::epsilon() ||
248  fabs(it->first) < 1.0 - 100.0 * std::numeric_limits< C_FLOAT64 >::epsilon())
249  Infix << fabs(it->first) << "*";
250 
251  Infix << "<" << it->second->getInitialValueReference()->getCN() << ">";
252  }
253 
254  return Infix.str();
255 }
256 
258 {
259  return mIAmount;
260 }
261 
263 {
265 }
266 
267 const std::vector<std::pair< C_FLOAT64, CMetab * > > & CMoiety::getEquation() const
268 {
269  return mEquation;
270 }
271 
273 {
274  const CModel * pModel = dynamic_cast< const CModel * >(getObjectAncestor("Model"));
275 
276  if (pModel != NULL)
277  {
279  }
280  else
281  {
283  }
284 }
285 
287  const CCopasiContainer * pParent,
288  C_FLOAT64 & reference):
289  CCopasiObjectReference< C_FLOAT64 >(name, pParent, reference)
290 {}
291 
293  const CCopasiContainer * pParent):
294  CCopasiObjectReference< C_FLOAT64 >(src, pParent)
295 {}
296 
298 {}
299 
300 // virtual
302  const CMath::SimulationContextFlag & context,
303  const CObjectInterface::ObjectSet & /* changedObjects */) const
304 {
305  return (context & CMath::UpdateMoieties);
306 }
307 
309  const CCopasiContainer * pParent,
310  C_FLOAT64 & reference):
311  CCopasiObjectReference< C_FLOAT64 >(name, pParent, reference)
312 {}
313 
315  const CCopasiContainer * pParent):
316  CCopasiObjectReference< C_FLOAT64 >(src, pParent)
317 {}
318 
320 {}
321 
322 // virtual
324  const CMath::SimulationContextFlag & context,
325  const CObjectInterface::ObjectSet & /* changedObjects */) const
326 {
327  return (context & CMath::UseMoieties);
328 }
const C_FLOAT64 & dependentNumber()
Definition: CMoiety.cpp:139
Header file of class CExpression.
virtual bool setObjectParent(const CCopasiContainer *pParent)
C_FLOAT64 mIAmount
Definition: CMoiety.h:146
CCopasiContainer * getObjectAncestor(const std::string &type) const
bool remove(const std::string &key)
void clearDirectDependencies()
virtual bool setObjectParent(const CCopasiContainer *pParent)
Definition: CMoiety.cpp:156
void setRefresh(CType *pType, void(CType::*method)(void))
CCopasiObjectReference< C_FLOAT64 > * mpIValueReference
Definition: CModelValue.h:356
CCopasiObject * getValueReference() const
Definition: CMoiety.cpp:223
CCopasiObject * getTotalNumberReference() const
Definition: CMoiety.cpp:148
~CMoiety()
Definition: CMoiety.cpp:70
void initObjects()
Definition: CMoiety.cpp:76
static const C_FLOAT64 DefaultFactor
Definition: CMoiety.h:126
void cleanup()
Definition: CMoiety.cpp:114
const std::vector< std::pair< C_FLOAT64, CMetab * > > & getEquation() const
Definition: CMoiety.cpp:267
std::vector< std::pair< C_FLOAT64, CMetab * > > mEquation
Definition: CMoiety.h:153
std::string getDescription(const CModel *model) const
Definition: CMoiety.cpp:164
static std::string getDisplayName(const CModel *model, const std::string &key, const bool &quoted)
Definition: CMetab.h:178
CTotalNumberReference * mpNumberReference
Definition: CMoiety.h:164
void initConversionFactor()
Definition: CMoiety.cpp:272
CDependentNumberReference * mpDNumberReference
Definition: CMoiety.h:169
C_FLOAT64 mNumber
Definition: CMoiety.h:136
void refreshValue()
Definition: CMoiety.cpp:214
#define DESTRUCTOR_TRACE
Definition: copasi.h:206
void addDirectDependency(const CCopasiObject *pObject)
void setDependentOn(const CMoiety *pMoiety)
Definition: CMetab.cpp:986
const C_FLOAT64 & getNumber2QuantityFactor() const
Definition: CModel.cpp:2357
std::string getExpression() const
Definition: CMoiety.cpp:228
const C_FLOAT64 & getDependentNumber() const
Definition: CMoiety.cpp:145
CCopasiObjectReference< C_FLOAT64 > * mpValueReference
Definition: CModelValue.h:357
C_FLOAT64 getNumber() const
Definition: CMoiety.cpp:209
std::set< const CObjectInterface * > ObjectSet
static CKeyFactory * getKeyFactory()
const C_FLOAT64 * mpConversionFactor
Definition: CMoiety.h:174
void add(C_FLOAT64 value, CMetab *metabolite)
Definition: CMoiety.cpp:96
#define C_FLOAT64
Definition: copasi.h:92
void refreshDependentNumber()
Definition: CMoiety.cpp:122
virtual bool isPrerequisiteForContext(const CObjectInterface *pObject, const CMath::SimulationContextFlag &context, const CObjectInterface::ObjectSet &changedObjects) const
Definition: CMoiety.cpp:301
void refreshAmount()
Definition: CMoiety.cpp:262
C_FLOAT64 mINumber
Definition: CMoiety.h:141
Definition: CModel.h:50
std::string mKey
Definition: CMoiety.h:131
virtual const std::string & getKey() const
Definition: CMoiety.cpp:154
CTotalNumberReference * mpINumberReference
Definition: CMoiety.h:158
CCopasiObject * addObjectReference(const std::string &name, CType &reference, const unsigned C_INT32 &flag=0)
CCopasiObject * getInitialValueReference() const
Definition: CMoiety.cpp:206
const C_FLOAT64 & getAmount() const
Definition: CMoiety.cpp:257
CMoiety(const std::string &name="NoName", const CCopasiContainer *pParent=NULL)
Definition: CMoiety.cpp:36
CCopasiObject * getDependentNumberReference() const
Definition: CMoiety.cpp:151
virtual bool isPrerequisiteForContext(const CObjectInterface *pObject, const CMath::SimulationContextFlag &context, const CObjectInterface::ObjectSet &changedObjects) const
Definition: CMoiety.cpp:323
#define CONSTRUCTOR_TRACE
Definition: copasi.h:202
void refreshInitialValue()
Definition: CMoiety.cpp:193