COPASI API  4.16.103
CSBMLunit.cpp
Go to the documentation of this file.
1 // Copyright (C) 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 Sven Sahle and University of Heidelberg
7 // All rights reserved.
8 
9 #include "CSBMLunit.h"
10 #include <assert.h>
11 
12 CSBMLunit::CSBMLunit(unsigned int sbmlLevel, unsigned int sbmlVersion)
13  : mUD(UnitDefinition(sbmlLevel, sbmlVersion)),
14  mSymExp(),
15  mSymExpExp(0)
16 {
17  Unit *tmp = mUD.createUnit();
18  tmp->setKind(UNIT_KIND_DIMENSIONLESS);
19 
20 #if LIBSBML_VERSION > 40100
21  tmp->initDefaults();
22 #endif
23 }
24 
26  : mUD(src.mUD),
27  mSymExp(src.mSymExp),
28  mSymExpExp(src.mSymExpExp)
29 {
30  //TODO verify that this does indeed do a deep copy
31 }
32 
33 CSBMLunit::CSBMLunit(const UnitDefinition* ud)
34  : mUD(*ud),
35  mSymExp(),
36  mSymExpExp(0)
37 {
38 }
39 
41 {
42 }
43 
44 std::string CSBMLunit::getDisplayString() const
45 {
46  if (mSymExpExp == 0)
47  {
48  return UnitDefinition::printUnits(&mUD, true);
49  }
50  else if (mSymExpExp == 1)
51  {
52  return "(" + UnitDefinition::printUnits(&mUD, true) + ")^" + mSymExp.mID;
53  }
54  else
55  {
56  return "(" + UnitDefinition::printUnits(&mUD, true) + ")^" + mSymExp.mID + "^someExponent";
57  }
58 }
59 
60 bool CSBMLunit::multiply(const CSBMLunit & unit)
61 {
62  if (unit.isDimensionless())
63  return true;
64 
65  if (isDimensionless())
66  {
67  mUD = unit.mUD;
68  mSymExp = unit.mSymExp;
69  mSymExpExp = unit.mSymExpExp;
70  return true;
71  }
72 
73  if ((mSymExpExp == 0 && unit.mSymExpExp == 0)
74  || ((mSymExpExp == unit.mSymExpExp) && (mSymExp.isEqual(unit.mSymExp))))
75  {
76  UnitDefinition tmp = unit.mUD;
77  UnitDefinition * tmpPointerUD = UnitDefinition::combine(&this->mUD, &tmp);
78  mUD = *tmpPointerUD;
79  delete tmpPointerUD;
80  return true;
81  }
82 
83  // could not multiply units because of symbolic exponent !!!
84 
85  return false;
86 }
87 
89 {
90  if (isDimensionless())
91  return;
92 
93  if (mSymExpExp != 0)
94  {
95  // could not invert units because of symbolic exponent !!!
96  }
97 
98  unsigned int i, imax = mUD.getNumUnits();
99 
100  for (i = 0; i < imax; ++i)
101  mUD.getUnit(i)->setExponent(-mUD.getUnit(i)->getExponent());
102 }
103 
104 void CSBMLunit::applyExponent(double exp)
105 {
106  if (isDimensionless())
107  return;
108 
109  if (mSymExpExp != 0)
110  {
111  // could not apply numeric exponent because of symbolic exponent !!!
112  }
113 
114  unsigned int i, imax = mUD.getNumUnits();
115 
116  for (i = 0; i < imax; ++i)
117  {
118  mUD.getUnit(i)->setExponent(floor(0.5 + exp*(double)mUD.getUnit(i)->getExponent()));
119  }
120 }
121 
122 void CSBMLunit::applyExponent(const std::string& id, size_t frame)
123 {
124  if (isDimensionless())
125  return;
126 
127  if ((mSymExpExp == 0) || mSymExp.isEqual(SymbolicExponent(id, frame)))
128  {
129  // applied symbolic exponent
130  ++mSymExpExp;
131  mSymExp = SymbolicExponent(id, frame);
132  }
133  else
134  {
135  // could not apply symbolic exponent !!!
136  assert(false);
137  }
138 }
139 
140 void CSBMLunit::applyInverseExponent(const std::string& id, size_t frame)
141 {
142  if (isDimensionless())
143  return;
144 
145  if ((mSymExpExp == 0) || mSymExp.isEqual(SymbolicExponent(id, frame)))
146  {
147  // applied inverse symbolic exponent
148  --mSymExpExp;
149  mSymExp = SymbolicExponent(id, frame);
150  }
151  else
152  {
153  // could not apply inverse symbolic exponent !!!
154  assert(false);
155  }
156 }
157 
158 //static
159 bool CSBMLunit::isEqual(const CSBMLunit & unit1, const CSBMLunit & unit2)
160 {
161  if (unit1.isDimensionless() && unit2.isDimensionless())
162  return true;
163 
164  if (((unit1.mSymExpExp == 0) && (unit2.mSymExpExp == 0))
165  || (unit1.mSymExp.isEqual(unit2.mSymExp) && (unit1.mSymExpExp == unit2.mSymExpExp)))
166  {
167  return UnitDefinition::areEquivalent(&unit1.mUD, &unit2.mUD);
168  }
169  else
170  {
171  return false;
172  }
173 }
174 
176 {
177  return mUD.isVariantOfDimensionless();
178 }
179 
180 //************************************************
181 
182 CSBMLunitInformation::CSBMLunitInformation(unsigned int sbmlLevel, unsigned int sbmlVersion)
183  : CSBMLunit(sbmlLevel, sbmlVersion),
184  mInfo(UNKNOWN),
185  mConflict(false)
186 {
187 }
188 
189 CSBMLunitInformation::CSBMLunitInformation(unsigned int sbmlLevel, unsigned int sbmlVersion, INFO info, bool conflict)
190  : CSBMLunit(sbmlLevel, sbmlVersion),
191  mInfo(info),
192  mConflict(conflict)
193 {
194 }
195 
197  : CSBMLunit(u),
198  mInfo(info),
199  mConflict(conflict)
200 {
201 }
202 
203 CSBMLunitInformation::CSBMLunitInformation(const UnitDefinition* ud, INFO info, bool conflict)
204  : CSBMLunit(ud),
205  mInfo(info),
206  mConflict(conflict)
207 {
208 }
209 
211 {
212  std::string sss;
213 
214  switch (mInfo)
215  {
216  case UNKNOWN:
217  sss += "unknown";
218  break;
219  case DEFAULT:
220  sss += "default: " + CSBMLunit::getDisplayString();
221  break;
222  case GLOBAL:
223  sss += "global: " + CSBMLunit::getDisplayString();
224  break;
225  case PROVIDED:
226  sss += "provided: " + CSBMLunit::getDisplayString();
227  break;
228  case DERIVED:
229  sss += "derived: " + CSBMLunit::getDisplayString();
230  break;
231  };
232 
233  if (mConflict)
234  sss += "!!!Conflict!!!";
235 
236  return sss;
237 }
238 
239 //static
241 {
242  if (unit1.mInfo == unit2.mInfo && unit1.mConflict == unit2.mConflict)
243  return CSBMLunit::isEqual(unit1, unit2);
244  else
245  return false;
246 }
the units is determined from the sbml defaults
Definition: CSBMLunit.h:108
this class uniquely (in a given context) identifies a symbol
Definition: CSBMLunit.h:79
virtual std::string getDisplayString() const
Definition: CSBMLunit.cpp:210
bool isDimensionless() const
Definition: CSBMLunit.cpp:175
virtual std::string getDisplayString() const
Definition: CSBMLunit.cpp:44
UnitDefinition mUD
Definition: CSBMLunit.h:73
void applyExponent(double exp)
apply numeric exponent to the unit.
Definition: CSBMLunit.cpp:104
the units is unknown
Definition: CSBMLunit.h:106
bool mConflict
indicated whether a conflict occurred while trying to determine the unit.
Definition: CSBMLunit.h:146
CSBMLunit(unsigned int sbmlLevel, unsigned int sbmlVersion)
Default constructor.
Definition: CSBMLunit.cpp:12
static bool isEqual(const CSBMLunit &unit1, const CSBMLunit &unit2)
Definition: CSBMLunit.cpp:159
int mSymExpExp
Definition: CSBMLunit.h:91
INFO
this enum encodes the status of a units information
Definition: CSBMLunit.h:103
the units is determined from the model-wide definitions
Definition: CSBMLunit.h:110
void applyInverseExponent(const std::string &id, size_t frame)
apply inverse of symbolic exponent to the unit
Definition: CSBMLunit.cpp:140
the units if provided for a specific object explicitly
Definition: CSBMLunit.h:112
CSBMLunitInformation(unsigned int sbmlLevel, unsigned int sbmlVersion)
Default constructor.
Definition: CSBMLunit.cpp:182
SymbolicExponent mSymExp
Definition: CSBMLunit.h:90
static bool isEqual(const CSBMLunitInformation &unit1, const CSBMLunitInformation &unit2)
Definition: CSBMLunit.cpp:240
the units was determined by reasoning
Definition: CSBMLunit.h:114
bool multiply(const CSBMLunit &unit)
Definition: CSBMLunit.cpp:60
void invert()
Definition: CSBMLunit.cpp:88
bool isEqual(const SymbolicExponent &se) const
Definition: CSBMLunit.h:86
virtual ~CSBMLunit()
Definition: CSBMLunit.cpp:40