COPASI API  4.16.103
CFixLocalReactionParameters.cpp
Go to the documentation of this file.
1 // Begin CVS Header
2 // $Source: /Volumes/Home/Users/shoops/cvs/copasi_dev/copasi/xml/CFixLocalReactionParameters.cpp,v $
3 // $Revision: 1.4 $
4 // $Name: $
5 // $Author: shoops $
6 // $Date: 2009/01/07 19:40:34 $
7 // End CVS Header
8 
9 // Copyright (C) 2008 by Pedro Mendes, Virginia Tech Intellectual
10 // Properties, Inc., EML Research, gGmbH, University of Heidelberg,
11 // and The University of Manchester.
12 // All rights reserved.
13 
14 #include "copasi.h"
15 
17 
18 #include "model/CModel.h"
19 #include "model/CReaction.h"
21 #include "function/CExpression.h"
22 
24  mpModel(NULL),
25  mChanges()
26 {}
27 
29 {}
30 
32 {
33  if (pModel == NULL) return;
34 
35  mpModel = pModel;
36  checkModel();
37 
38  if (mChanges.size() == 0) return;
39 
40  changeModel();
41 }
42 
44 {
45  // Clear recorded changes
46  mChanges.clear();
47 
48  // Check for all reactions
51 
52  for (; itReaction != endReaction; ++itReaction)
53  {
54  // Check for all local parameters
55  CCopasiParameterGroup::index_iterator itParameter = (*itReaction)->getParameters().beginIndex();
56  CCopasiParameterGroup::index_iterator endParameter = (*itReaction)->getParameters().endIndex();
57 
58  for (; itParameter != endParameter; ++itParameter)
59  {
60  // Check for all entities' expressions and initial expressions
61  // whether the CN of the parameter appears in the infix.
62  // Note '>' is already properly escaped in the CN
63  std::string CN = "<" + (*itParameter)->getCN() + ",Reference=Value>";
64 
65  CModelEntity *const*ppEntity = mpModel->getStateTemplate().getEntities();
66  CModelEntity *const*ppEntityEnd = ppEntity + mpModel->getStateTemplate().size();
67  for (; ppEntity != ppEntityEnd; ++ppEntity)
68  {
69  if ((*ppEntity)->getExpression().find(CN) != std::string::npos)
70  mChanges.insert
71  (std::pair< CCopasiParameter * const, CExpression * >(*itParameter, (*ppEntity)->getExpressionPtr()));
72 
73  if ((*ppEntity)->getInitialExpression().find(CN) != std::string::npos)
74  mChanges.insert
75  (std::pair< CCopasiParameter * const, CExpression * >(*itParameter, (*ppEntity)->getInitialExpressionPtr()));
76  }
77  }
78  }
79 }
80 
82 {
83  CCopasiParameter * pParameter = NULL;
84  CModelValue * pModelValue = NULL;
85  CReaction * pReaction = NULL;
86 
87  std::stringstream NameStream;
88  std::stringstream Message;
89 
90  std::string OldCN;
91  std::string NewCNBase;
92  std::string NewCN;
93  std::string Infix;
94 
95  std::string::size_type Start;
96 
97  // Loop through all changes.
98  std::multimap< CCopasiParameter *, CExpression * >::const_iterator itChanges = mChanges.begin();
99  std::multimap< CCopasiParameter *, CExpression * >::const_iterator endChanges = mChanges.end();
100 
101  for (; itChanges != endChanges; ++itChanges)
102  {
103  if (pParameter != itChanges->first)
104  {
105  // We have a new parameter
106  pParameter = itChanges->first;
107  OldCN = "<" + pParameter->getCN() + ",Reference=";
108 
109  // Create a global quantity of type FIXED.
110  std::string Name = pParameter->getObjectName();
111  pReaction = static_cast< CReaction * >(pParameter->getObjectAncestor("Reaction"));
112  Name += "{" + pReaction->getObjectName() + "}";
113 
114  pModelValue = mpModel->createModelValue(Name,
115  *pParameter->getValue().pDOUBLE);
116 
117  // In case the created name is not unique we append _n with increasing n
118  // until we succeed;
119  C_INT32 index = 0;
120  while (pModelValue == NULL)
121  {
122  NameStream.str("");
123  NameStream << Name << "_" << index++;
124  pModelValue = mpModel->createModelValue(NameStream.str(),
125  *pParameter->getValue().pDOUBLE);
126  }
127  NewCNBase = "<" + pModelValue->getCN() + ",Reference=";
128 
129  // If the parameter is actually used in the reaction
130  // it is changed to the global quantity.
131  if (pReaction->isLocalParameter(pParameter->getObjectName()))
132  pReaction->setParameterMapping(pParameter->getObjectName(), pModelValue->getKey());
133 
134  Message << " " << pParameter->getObjectName() << " in " << pReaction->getObjectName()
135  << " is replaced by " << pModelValue->getObjectName() << std::endl;
136  }
137 
138  // We need to distinguish between initial and other expressions.
139  if (itChanges->second->getObjectName().compare(0, 7, "Initial") == 0)
140  NewCN = NewCNBase + "Initial";
141  else
142  NewCN = NewCNBase;
143 
144  // Replace the OldCN of the parameter with the NewCN of global quantity in all expressions.
145  Infix = itChanges->second->getInfix();
146 
147  // There may be more than one occurrence.
148  Start = 0;
149  while ((Start = Infix.find(OldCN), Start) != std::string::npos)
150  Infix.replace(Start, OldCN.length(), NewCN);
151 
152  itChanges->second->setInfix(Infix);
153  }
154 
155  CCopasiMessage(CCopasiMessage::WARNING, MCXML + 14, Message.str().c_str());
156 }
Header file of class CExpression.
CCopasiContainer * getObjectAncestor(const std::string &type) const
virtual CCopasiObjectName getCN() const
const std::string & getObjectName() const
iterator begin()
std::multimap< CCopasiParameter *, CExpression * > mChanges
bool isLocalParameter(const size_t &index) const
Definition: CReaction.cpp:449
#define MCXML
CModelValue * createModelValue(const std::string &name, const C_FLOAT64 &value=0.0)
Definition: CModel.cpp:2838
#define C_INT32
Definition: copasi.h:90
const size_t & size() const
Definition: CState.cpp:242
iterator end()
std::vector< CType * >::const_iterator const_iterator
Definition: CCopasiVector.h:57
virtual const std::string & getKey() const
struct MESSAGES Message
const Value & getValue() const
virtual CCopasiObjectName getCN() const
const CStateTemplate & getStateTemplate() const
Definition: CModel.cpp:1172
void setParameterMapping(const size_t &index, const std::string &key)
Definition: CReaction.cpp:339
CCopasiVectorNS< CReaction > & getReactions()
Definition: CModel.cpp:1039
Definition: CModel.h:50
parameterGroup::iterator index_iterator
CModelEntity ** getEntities()
Definition: CState.cpp:204