COPASI API  4.16.103
CState.cpp
Go to the documentation of this file.
1 // Begin CVS Header
2 // $Source: /Volumes/Home/Users/shoops/cvs/copasi_dev/copasi/model/CState.cpp,v $
3 // $Revision: 1.76 $
4 // $Name: $
5 // $Author: shoops $
6 // $Date: 2012/04/23 21:11:04 $
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 // CSate.cpp
24 //
25 // (C) Stefan Hoops 2002
26 //
27 
28 #include <string.h>
29 #include <cmath>
30 
31 #include "copasi.h"
32 
33 #include "CState.h"
34 #include "CModelValue.h"
35 #include "CModel.h"
36 
37 //**********************************************************************
38 // CStateTemplate
39 //**********************************************************************
40 
41 CStateTemplate::CStateTemplate(CModel & model, CState & initialState, CState & currentState):
42  mModel(model),
43  mInitialState(initialState),
44  mCurrentState(currentState),
45  mpEntities(NULL),
46  mSize(0),
47  mpBeginIndependent(NULL),
48  mpBeginDependent(NULL),
49  mpBeginFixed(NULL),
50  mpEnd(NULL),
51  mInsert(0),
52  mIndexMap(),
53  mUserOrder(0),
54  mpInitialValues(NULL),
55  mpCurrentValues(NULL)
56 {}
57 
59 {
61 }
62 
64 {
65  if (mIndexMap.count(entity))
66  return;
67 
68  if (mInsert == mSize) resize();
69 
70  mpEntities[mInsert] = entity;
71 
74 
75  mIndexMap[entity] = mInsert++;
76 
77  mModel.setCompileFlag(true);
78 }
79 
81 {
82  std::map< CModelEntity *, size_t >::iterator it =
83  mIndexMap.find(entity);
84 
85  if (it == mIndexMap.end())
86  return;
87 
88  entity->setInitialValuePtr(NULL);
89  entity->setValuePtr(NULL);
90 
91  mpEntities[it->second] = NULL;
92 
93  mIndexMap.erase(it);
94 
95  mModel.setCompileFlag(true);
96 }
97 
99 {
100  assert(entitiesX.size() + 1 == mIndexMap.size());
101 
102  // Update mpEntities to reflect the new order;
103 
104  memcpy(mpEntities + 1, entitiesX.array(), sizeof(CModelEntity *) * entitiesX.size());
105  mInsert = entitiesX.size() + 1;
106 
107  std::map< CModelEntity *, size_t >::iterator found;
108  CVector<C_FLOAT64> InitialValues(entitiesX.size());
109  CVector<C_FLOAT64> CurrentValues(entitiesX.size());
110 
111  CModelEntity *const*it = entitiesX.array();
112  CModelEntity *const*end = it + entitiesX.size();
113 
114  size_t i;
115  size_t Independent, Dependent, Fixed;
116  Independent = Dependent = Fixed = 0;
117 
118  for (i = 1; it != end; ++it, i++)
119  {
120  found = mIndexMap.find(*it);
121  assert(found != mIndexMap.end());
122 
123  // Build new order
124  InitialValues[i - 1] = *(mpInitialValues + found->second);
125  CurrentValues[i - 1] = *(mpCurrentValues + found->second);
126 
127  // Update pointer if necessary
128  if (i != found->second)
129  {
130  found->second = i;
131  found->first->setInitialValuePtr(mpInitialValues + found->second);
132  found->first->setValuePtr(mpCurrentValues + found->second);
133  }
134 
135  // Count numbers for each status type;
136  if ((*it)->isUsed())
137  switch ((*it)->getStatus())
138  {
139  case CModelEntity::FIXED:
140  Fixed++;
141  break;
142 
144 
145  if (static_cast< CMetab * >(*it)->isDependent())
146  {
147  assert(Fixed == 0);
148  Dependent++;
149  }
150  else
151  {
152  assert(Dependent == 0);
153  Independent++;
154  }
155 
156  break;
157 
158  case CModelEntity::ODE:
159  assert(Dependent == 0);
160  Independent++;
161  break;
162 
164  assert(Fixed == 0);
165  Dependent++;
166  break;
167 
168  case CModelEntity::TIME:
169  assert(false);
170  break;
171  }
172  else
173  Fixed++;
174  }
175 
177  mpBeginDependent = mpBeginIndependent + Independent;
178  mpBeginFixed = mpBeginDependent + Dependent;
179  mpEnd = mpBeginFixed + Fixed;
180 
181  memcpy(mpInitialValues + 1, InitialValues.array(), sizeof(C_FLOAT64) * entitiesX.size());
182  memcpy(mpCurrentValues + 1, CurrentValues.array(), sizeof(C_FLOAT64) * entitiesX.size());
183 
184  // Update the iterators of the states
185  mInitialState.updateIterator(Independent, Dependent, Fixed);
186  mCurrentState.updateIterator(Independent, Dependent, Fixed);
187 }
188 
190 {
191  mUserOrder.resize(entities.size() + 1);
192  size_t * pUserOrder = mUserOrder.array();
193  *pUserOrder++ = 0; // for time
194 
195  CModelEntity *const *it = entities.array();
196  CModelEntity *const *end = it + entities.size();
197 
198  while (it != end) *pUserOrder++ = getIndex(*it++);
199 }
200 
202 {return mUserOrder;}
203 
206 
207 //CModelEntity ** getEntities();
214 
220 CModelEntity *const* CStateTemplate::endFixed() const {return mpEnd;}
221 
225 {return mpBeginFixed - mpBeginDependent;}
229 {return mpEnd - mpBeginFixed;}
230 
231 size_t CStateTemplate::getIndex(const CModelEntity * entity) const
232 {
233  std::map< CModelEntity *, size_t >::const_iterator found =
234  mIndexMap.find(const_cast< CModelEntity * >(entity));
235 
236  if (found != mIndexMap.end())
237  return found->second;
238 
239  return C_INVALID_INDEX;
240 }
241 
242 const size_t & CStateTemplate::size() const
243 {return mInsert;}
244 
246 {
247  size_t OldSize = mSize;
248 
249  if (mSize)
250  mSize *= 2;
251  else
252  mSize = 16;
253 
254  // Resize the entities array.
255  CModelEntity ** pTmp = mpEntities;
256  mpEntities = new CModelEntity * [mSize];
257  memcpy(mpEntities, pTmp, sizeof(CModelEntity *) * OldSize);
258  pdeletev(pTmp);
259 
260  // Resize the states
261  C_FLOAT64 * pInitialValues = mpInitialValues = mInitialState.resize(mSize);
262  C_FLOAT64 * pCurrentValues = mpCurrentValues = mCurrentState.resize(mSize);
263 
264  // Update all pointers in the entities
265  pTmp = mpEntities;
266  CModelEntity ** pEnd = pTmp + mInsert;
267 
268  for (; pTmp != pEnd; ++pTmp, ++pInitialValues, ++pCurrentValues)
269  if (*pTmp != NULL)
270  {
271  (*pTmp)->setInitialValuePtr(pInitialValues);
272  (*pTmp)->setValuePtr(pCurrentValues);
273  }
274 }
275 
276 /*************************/
277 /* Code for class CState */
278 /*************************/
280  mpValues(NULL),
281  mSize(0),
282  mpBeginIndependent(NULL),
283  mpBeginDependent(NULL),
284  mpBeginFixed(NULL),
285  mpEnd(NULL),
286  mUpdateDependentRequired(false)
287 {}
288 
289 CState::CState(const CState & src):
290  mpValues(new C_FLOAT64[src.mSize]),
291  mSize(src.mSize),
292  mpBeginIndependent(mpValues + (src.mpBeginIndependent - src.mpValues)),
293  mpBeginDependent(mpValues + (src.mpBeginDependent - src.mpValues)),
294  mpBeginFixed(mpValues + (src.mpBeginFixed - src.mpValues)),
295  mpEnd(mpValues + (src.mpEnd - src.mpValues)),
296  mUpdateDependentRequired(src.mUpdateDependentRequired)
297 {
298  memcpy(mpValues, src.mpValues, sizeof(C_FLOAT64) * mSize);
299 }
300 
302 
304 {
305  if (this != &rhs)
306  {
307  if (mSize != rhs.mSize)
308  {
310  mpValues = new C_FLOAT64[rhs.mSize];
311  mSize = rhs.mSize;
312  }
313 
314  memcpy(mpValues, rhs.mpValues, sizeof(C_FLOAT64) * mSize);
317  mpBeginFixed = mpValues + (rhs.mpBeginFixed - rhs.mpValues);
318  mpEnd = mpValues + (rhs.mpEnd - rhs.mpValues);
320  }
321 
322  return *this;
323 }
324 
325 const C_FLOAT64 & CState::getTime() const {return *mpValues;}
326 void CState::setTime(const C_FLOAT64 & time) {*mpValues = time;}
327 
334 
338 const C_FLOAT64 * CState::endDependent() const {return mpBeginFixed;}
339 const C_FLOAT64 * CState::beginFixed() const {return mpBeginFixed;}
340 const C_FLOAT64 * CState::endFixed() const {return mpEnd;}
341 
345 {return mpBeginFixed - mpBeginDependent;}
348 size_t CState::getNumFixed() const
349 {return mpEnd - mpBeginFixed;}
350 
351 bool CState::isValid() const
352 {
353  const C_FLOAT64 * pIt = mpBeginIndependent;
354  const C_FLOAT64 * pEnd = mpBeginDependent;
355 
356  for (; pIt != pEnd; ++pIt)
357  {
358  if (isnan(*pIt))
359  return false;
360  }
361 
362  return true;
363 }
364 
365 C_FLOAT64 * CState::resize(const size_t & size)
366 {
367  if (mSize != size)
368  {
369  C_FLOAT64 * pTmp = mpValues;
370 
371  if (size > 0)
372  {
373  mpValues = new C_FLOAT64[size];
374  memcpy(mpValues, pTmp, sizeof(C_FLOAT64) * std::min(mSize, size));
375  }
376  else
377  mpValues = NULL;
378 
379  pdeletev(pTmp);
380  mSize = size;
381  }
382 
384 
385  return mpValues;
386 }
387 
388 void CState::updateIterator(const size_t & numIndependent,
389  const size_t & numDependent,
390  const size_t & numFixed)
391 {
392  mpBeginIndependent = mpValues + 1; // One for Time
393  mpBeginDependent = mpBeginIndependent + numIndependent;
394  mpBeginFixed = mpBeginDependent + numDependent;
395  mpEnd = mpBeginFixed + numFixed;
396 }
397 
398 std::ostream &operator << (std::ostream & os, const CState & s)
399 {
400  os << "(";
401 
402  const C_FLOAT64 * pValue = s.mpValues;
403 
404  for (; pValue != s.mpEnd; ++pValue)
405  {
406  os << "\t" << *pValue;
407  }
408 
409  os << "\t)";
410 
411  return os;
412 }
C_FLOAT64 * mpEnd
Definition: CState.h:495
void remove(CModelEntity *entity)
Definition: CState.cpp:80
CState()
Definition: CState.cpp:279
CVector< size_t > mUserOrder
Definition: CState.h:292
Header file of class CModelEntity and CModelValue.
void setUserOrder(const CVector< CModelEntity * > &userOrder)
Definition: CState.cpp:189
size_t getNumFixed() const
Definition: CState.cpp:228
size_t getNumIndependent() const
Definition: CState.cpp:222
void add(CModelEntity *entity)
Definition: CState.cpp:63
C_FLOAT64 * mpBeginFixed
Definition: CState.h:490
C_FLOAT64 * mpBeginIndependent
Definition: CState.h:480
CModelEntity ** mpEnd
Definition: CState.h:277
Definition: CState.h:305
std::ostream & operator<<(std::ostream &os, const CState &s)
Definition: CState.cpp:398
void resize(size_t size, const bool &copy=false)
Definition: CVector.h:301
#define C_INVALID_INDEX
Definition: copasi.h:222
size_t getIndex(const CModelEntity *entity) const
Definition: CState.cpp:231
CModelEntity ** endDependent()
Definition: CState.cpp:211
size_t getNumVariable() const
Definition: CState.cpp:346
const size_t & size() const
Definition: CState.cpp:242
CModelEntity ** mpBeginDependent
Definition: CState.h:267
CModelEntity ** beginDependent()
Definition: CState.cpp:210
void setTime(const C_FLOAT64 &time)
Definition: CState.cpp:326
CModelEntity ** mpBeginIndependent
Definition: CState.h:262
C_FLOAT64 * mpBeginDependent
Definition: CState.h:485
C_FLOAT64 * endDependent()
Definition: CState.cpp:331
C_FLOAT64 * endIndependent()
Definition: CState.cpp:329
C_FLOAT64 * endFixed()
Definition: CState.cpp:333
#define pdeletev(p)
Definition: copasi.h:216
size_t getNumDependent() const
Definition: CState.cpp:344
bool mUpdateDependentRequired
Definition: CState.h:500
size_t getNumFixed() const
Definition: CState.cpp:348
C_FLOAT64 * mpCurrentValues
Definition: CState.h:302
CModelEntity ** endFixed()
Definition: CState.cpp:213
void setValuePtr(C_FLOAT64 *pValue)
CModelEntity ** mpEntities
Definition: CState.h:252
CState & mInitialState
Definition: CState.h:242
CModelEntity ** beginFixed()
Definition: CState.cpp:212
size_t getNumIndependent() const
Definition: CState.cpp:342
CModelEntity ** beginIndependent()
Definition: CState.cpp:208
size_t mSize
Definition: CState.h:475
CState & mCurrentState
Definition: CState.h:247
void setCompileFlag(bool flag=true)
Definition: CModel.cpp:607
std::map< CModelEntity *, size_t > mIndexMap
Definition: CState.h:287
bool isValid() const
Definition: CState.cpp:351
size_t size() const
Definition: CVector.h:100
const C_FLOAT64 & getTime() const
Definition: CState.cpp:325
void resize()
Definition: CState.cpp:245
size_t getNumDependent() const
Definition: CState.cpp:224
#define C_FLOAT64
Definition: copasi.h:92
void updateIterator(const size_t &numIndependent, const size_t &numDependent, const size_t &numFixed)
Definition: CState.cpp:388
CType * array()
Definition: CVector.h:139
size_t mInsert
Definition: CState.h:282
size_t mSize
Definition: CState.h:257
C_FLOAT64 * mpValues
Definition: CState.h:470
void setInitialValuePtr(C_FLOAT64 *pInitialValue)
Definition: CModel.h:50
C_FLOAT64 * beginFixed()
Definition: CState.cpp:332
const CVector< size_t > & getUserOrder() const
Definition: CState.cpp:201
C_FLOAT64 * beginDependent()
Definition: CState.cpp:330
CModel & mModel
Definition: CState.h:237
C_FLOAT64 * resize(const size_t &size)
Definition: CState.cpp:365
~CState()
Definition: CState.cpp:301
CModelEntity ** endIndependent()
Definition: CState.cpp:209
#define min(a, b)
Definition: f2c.h:175
CModelEntity ** mpBeginFixed
Definition: CState.h:272
C_FLOAT64 * beginIndependent()
Definition: CState.cpp:328
void reorder(const CVector< CModelEntity * > &newOrder)
Definition: CState.cpp:98
CModelEntity ** getEntities()
Definition: CState.cpp:204
C_FLOAT64 * mpInitialValues
Definition: CState.h:297
CState & operator=(const CState &rhs)
Definition: CState.cpp:303
size_t getNumVariable() const
Definition: CState.cpp:226