Cantera  2.1.2
units.h
Go to the documentation of this file.
1 /**
2  * @file units.h
3  * Header for units conversion utilities, which are used to translate
4  * user input from input files (See \ref inputfiles and
5  * class \link Cantera::Unit Unit\endlink).
6  *
7  * This header is included only by file misc.cpp.
8  */
9 // Copyright 2002 California Institute of Technology
10 
11 #ifndef CT_UNITS_H
12 #define CT_UNITS_H
13 
14 #include "cantera/base/ct_defs.h"
16 #include "cantera/base/ct_thread.h"
17 
18 namespace Cantera
19 {
20 
21 //! Unit conversion utility
22 /*!
23  * @ingroup inputfiles
24  */
25 class Unit
26 {
27 public:
28  //! Initialize the static Unit class.
29  static Unit* units() {
30  ScopedLock lock(units_mutex);
31  if (!s_u) {
32  s_u = new Unit;
33  }
34  return s_u;
35  }
36 
37  //! Destroy the static Unit class
38  /*!
39  * Note this can't be done in a destructor.
40  */
41  static void deleteUnit() {
42  ScopedLock lock(units_mutex);
43  if (s_u) {
44  delete s_u;
45  s_u = 0;
46  }
47  }
48 
49  //! Empty Destructor
50  virtual ~Unit() {}
51 
52  /**
53  * Return the multiplier required to convert an activation
54  * energy to SI units.
55  * @param units_ activation energy units
56  */
57  doublereal actEnergyToSI(const std::string& units_) {
58  if (m_act_u.find(units_) != m_act_u.end()) {
59  return m_act_u[units_];
60  } else {
61  return toSI(units_);
62  }
63  }
64 
65  /**
66  * Return the multiplier required to convert a dimensional quantity
67  * with units specified by string 'units' to SI units.
68  * The list of recognized units is stored as a stl map
69  * <string, doublereal>called m_u[] and m_act_u for activity
70  * coefficients. These maps are initialized with likely values.
71  *
72  * @param units_ String containing the units description
73  */
74  doublereal toSI(const std::string& units_) {
75 
76  // if dimensionless, return 1.0
77  if (units_ == "") {
78  return 1.0;
79  }
80 
81  doublereal f = 1.0, fctr;
82  int tsize;
83  std::string u = units_, tok, tsub;
84  std::string::size_type k;
85  char action = '-';
86 
87  while (1 > 0) {
88 
89  // get token consisting of all characters up to the next
90  // dash, slash, or the end of the string
91  k = u.find_first_of("/-");
92  if (k != std::string::npos) {
93  tok = u.substr(0,k);
94  } else {
95  tok = u;
96  }
97  tsize = static_cast<int>(tok.size());
98  if (tsize == 0) {
99  fctr = 1.0;
100  } else if (tok[tsize - 1] == '2') {
101  tsub = tok.substr(0,tsize-1);
102  fctr = m_u[tsub];
103  fctr *= fctr;
104  } else if (tok[tsize - 1] == '3') {
105  tsub = tok.substr(0,tsize-1);
106  fctr = m_u[tsub];
107  fctr *= fctr*fctr;
108  } else if (tok[tsize - 1] == '4') {
109  tsub = tok.substr(0,tsize-1);
110  fctr = m_u[tsub];
111  fctr *= fctr*fctr*fctr;
112  } else if (tok[tsize - 1] == '5') {
113  tsub = tok.substr(0,tsize-1);
114  fctr = m_u[tsub];
115  fctr *= fctr*fctr*fctr*fctr;
116  } else if (tok[tsize - 1] == '6') {
117  tsub = tok.substr(0,tsize-1);
118  fctr = m_u[tsub];
119  fctr *= fctr*fctr*fctr*fctr*fctr;
120  } else {
121  tsub = tok;
122  fctr = m_u[tok];
123  }
124 
125  // tok is not one of the entries in map m_u, then
126  // m_u[tok] returns 0.0. Check for this.
127  if (fctr == 0) {
128  throw CanteraError("toSI","unknown unit: "+tsub);
129  }
130  if (action == '-') {
131  f *= fctr;
132  } else if (action == '/') {
133  f /= fctr;
134  }
135  if (k == std::string::npos) {
136  break;
137  }
138  action = u[k];
139  u = u.substr(k+1,u.size());
140  }
141  return f;
142  }
143 
144 private:
145  /// pointer to the single instance of Unit
146  static Unit* s_u;
147 
148  //! Map between a string and a units double value
149  /*!
150  * This map maps the dimension string to the units value adjustment. Example
151  * - m_u["m"] = 1.0;
152  * - m_u["cm"] = 0.01;
153  */
154  std::map<std::string, doublereal> m_u;
155 
156  //! Map between a string and a units double value for activation energy units
157  /*!
158  * This map maps the dimension string to the units value adjustment. Example
159  * - m_act_u["K"] = GasConstant;
160  */
161  std::map<std::string, doublereal> m_act_u;
162 
163  //! Decl for static locker for Units singleton
164  static mutex_t units_mutex;
165 
166  //! Units class constructor, containing the default mappings between
167  //! strings and units.
168  Unit() :
169  m_u(),
170  m_act_u() {
171 
172  // unity
173  m_u["1"] = 1.0;
174 
175  // length
176  m_u["m"] = 1.0;
177  m_u["cm"] = 0.01;
178  m_u["km"] = 1.0e3;
179  m_u["mm"] = 1.0e-3;
180  m_u["micron"] = 1.0e-6;
181  m_u["nm"] = 1.0e-9;
182  m_u["A"] = 1.0e-10;
183  m_u["Angstrom"] = 1.0e-10;
184  m_u["Angstroms"] = 1.0e-10;
185 
186  // energy
187  m_u["J"] = 1.0;
188  m_u["kJ"] = 1.0e3;
189  m_u["cal"] = 4.184;
190  m_u["kcal"] = 4184.0;
191  m_u["eV"] = Faraday; //1.60217733e-19;
192 
193  // resistance
194  m_u["ohm"] = 1.0;
195 
196  // quantity
197  m_u["mol"] = 1.0e-3;
198  m_u["gmol"] = 1.0e-3;
199  m_u["mole"] = 1.0e-3;
200  m_u["kmol"] = 1.0;
201  m_u["kgmol"] = 1.0;
202  m_u["molec"] = 1.0/Avogadro;
203 
204  // temperature
205  m_u["K"] = 1.0;
206  m_u["C"] = 1.0;
207  m_u["Kelvin"] = 1.0;
208 
209  // mass
210  m_u["gm"] = 1.0e-3;
211  m_u["g"] = 1.0e-3;
212  m_u["kg"] = 1.0;
213 
214  // pressure
215  m_u["atm"] = 1.01325e5;
216  m_u["bar"] = 1.0e5;
217  m_u["Pa"] = 1.0;
218 
219  // time
220  m_u["s"] = 1.0;
221  m_u["min"] = 60.0;
222  m_u["hr"] = 3600.0;
223  m_u["ms"] = 0.001;
224 
225  // electric potential
226  m_u["volt"] = 1.0;
227 
228  // charge
229  m_u["coulomb"] = 1.0;
230 
231  /*
232  // frequency - Took frequency out to reevaluate it. Inverse cm is probably the wrong default unit
233  m_u["hZ"] = 0.01/(lightSpeed);
234  m_u["cm^-1"] = 1.0;
235  m_u["m^-1"] = 0.1;
236  m_u["cm-1"] = m_u["cm^-1"];
237  m_u["m-1"] = m_u["m^-1"];
238  m_u["wavenumbers"] = m_u["cm^-1"];
239  */
240 
241  // viscosity
242  m_u["Pa-s"] = 1;
243  m_u["poise"] = 0.1;
244  m_u["centipoise"] = 0.001;
245  m_u["P"] = 0.1;
246  m_u["cP"] = 0.001;
247 
248  // volume
249  m_u["kL"] = 1.0;
250  m_u["liter"] = 0.001;
251  m_u["L"] = 0.001;
252  m_u["l"] = 0.001;
253  m_u["mL"] = 1.0e-6;
254  m_u["ml"] = 1.0e-6;
255  m_u["cc"] = 1.0e-6;
256 
257  m_act_u["eV"] = m_u["eV"]; // /m_u["molec"];
258  m_act_u["K"] = GasConstant;
259  m_act_u["Kelvin"] = GasConstant;
260  m_act_u["Dimensionless"] = (GasConstant * 273.15);
261  }
262 };
263 }
264 
265 #endif
std::map< std::string, doublereal > m_u
Map between a string and a units double value.
Definition: units.h:154
doublereal actEnergyToSI(const std::string &units_)
Return the multiplier required to convert an activation energy to SI units.
Definition: units.h:57
Unit()
Units class constructor, containing the default mappings between strings and units.
Definition: units.h:168
virtual ~Unit()
Empty Destructor.
Definition: units.h:50
const size_t npos
index returned by functions to indicate "no position"
Definition: ct_defs.h:173
This file contains definitions of terms that are used in internal routines and are unlikely to need m...
static Unit * s_u
pointer to the single instance of Unit
Definition: units.h:146
Unit conversion utility.
Definition: units.h:25
doublereal toSI(const std::string &units_)
Return the multiplier required to convert a dimensional quantity with units specified by string 'unit...
Definition: units.h:74
std::map< std::string, doublereal > m_act_u
Map between a string and a units double value for activation energy units.
Definition: units.h:161
static Unit * units()
Initialize the static Unit class.
Definition: units.h:29
Base class for exceptions thrown by Cantera classes.
Definition: ctexceptions.h:68
const doublereal Avogadro
Avogadro's Number [number/kmol].
Definition: ct_defs.h:63
const doublereal GasConstant
Universal Gas Constant. [J/kmol/K].
Definition: ct_defs.h:66
static mutex_t units_mutex
Decl for static locker for Units singleton.
Definition: units.h:164
Definitions for the classes that are thrown when Cantera experiences an error condition (also contain...
static void deleteUnit()
Destroy the static Unit class.
Definition: units.h:41