Cantera  2.2.1
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
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  delete s_u;
44  s_u = 0;
45  }
46 
47  //! Empty Destructor
48  virtual ~Unit() {}
49 
50  /**
51  * Return the multiplier required to convert an activation
52  * energy to SI units.
53  * @param units_ activation energy units
54  */
55  doublereal actEnergyToSI(const std::string& units_) {
56  if (m_act_u.find(units_) != m_act_u.end()) {
57  return m_act_u[units_];
58  } else {
59  return toSI(units_);
60  }
61  }
62 
63  /**
64  * Return the multiplier required to convert a dimensional quantity
65  * with units specified by string 'units' to SI units.
66  * The list of recognized units is stored as a stl map
67  * <string, doublereal>called m_u[] and m_act_u for activity
68  * coefficients. These maps are initialized with likely values.
69  *
70  * @param units_ String containing the units description
71  */
72  doublereal toSI(const std::string& units_) {
73 
74  // if dimensionless, return 1.0
75  if (units_ == "") {
76  return 1.0;
77  }
78 
79  doublereal f = 1.0, fctr;
80  std::string u = units_, tok, tsub;
81  std::string::size_type k;
82  char action = '-';
83 
84  while (1 > 0) {
85 
86  // get token consisting of all characters up to the next
87  // dash, slash, or the end of the string
88  k = u.find_first_of("/-");
89  if (k != std::string::npos) {
90  tok = u.substr(0,k);
91  } else {
92  tok = u;
93  }
94  size_t tsize = tok.size();
95  if (tsize == 0) {
96  fctr = 1.0;
97  } else if (tok[tsize - 1] == '2') {
98  tsub = tok.substr(0,tsize-1);
99  fctr = m_u[tsub];
100  fctr *= fctr;
101  } else if (tok[tsize - 1] == '3') {
102  tsub = tok.substr(0,tsize-1);
103  fctr = m_u[tsub];
104  fctr *= fctr*fctr;
105  } else if (tok[tsize - 1] == '4') {
106  tsub = tok.substr(0,tsize-1);
107  fctr = m_u[tsub];
108  fctr *= fctr*fctr*fctr;
109  } else if (tok[tsize - 1] == '5') {
110  tsub = tok.substr(0,tsize-1);
111  fctr = m_u[tsub];
112  fctr *= fctr*fctr*fctr*fctr;
113  } else if (tok[tsize - 1] == '6') {
114  tsub = tok.substr(0,tsize-1);
115  fctr = m_u[tsub];
116  fctr *= fctr*fctr*fctr*fctr*fctr;
117  } else {
118  tsub = tok;
119  fctr = m_u[tok];
120  }
121 
122  // tok is not one of the entries in map m_u, then
123  // m_u[tok] returns 0.0. Check for this.
124  if (fctr == 0) {
125  throw CanteraError("toSI","unknown unit: "+tsub);
126  }
127  if (action == '-') {
128  f *= fctr;
129  } else if (action == '/') {
130  f /= fctr;
131  }
132  if (k == std::string::npos) {
133  break;
134  }
135  action = u[k];
136  u = u.substr(k+1,u.size());
137  }
138  return f;
139  }
140 
141 private:
142  /// pointer to the single instance of Unit
143  static Unit* s_u;
144 
145  //! Map between a string and a units double value
146  /*!
147  * This map maps the dimension string to the units value adjustment. Example
148  * - m_u["m"] = 1.0;
149  * - m_u["cm"] = 0.01;
150  */
151  std::map<std::string, doublereal> m_u;
152 
153  //! Map between a string and a units double value for activation energy units
154  /*!
155  * This map maps the dimension string to the units value adjustment. Example
156  * - m_act_u["K"] = GasConstant;
157  */
158  std::map<std::string, doublereal> m_act_u;
159 
160  //! Decl for static locker for Units singleton
161  static mutex_t units_mutex;
162 
163  //! Units class constructor, containing the default mappings between
164  //! strings and units.
165  Unit() {
166 
167  // unity
168  m_u["1"] = 1.0;
169 
170  // length
171  m_u["m"] = 1.0;
172  m_u["cm"] = 0.01;
173  m_u["km"] = 1.0e3;
174  m_u["mm"] = 1.0e-3;
175  m_u["micron"] = 1.0e-6;
176  m_u["nm"] = 1.0e-9;
177  m_u["A"] = 1.0e-10;
178  m_u["Angstrom"] = 1.0e-10;
179  m_u["Angstroms"] = 1.0e-10;
180 
181  // energy
182  m_u["J"] = 1.0;
183  m_u["kJ"] = 1.0e3;
184  m_u["cal"] = 4.184;
185  m_u["kcal"] = 4184.0;
186  m_u["eV"] = Faraday; //1.60217733e-19;
187 
188  // resistance
189  m_u["ohm"] = 1.0;
190 
191  // quantity
192  m_u["mol"] = 1.0e-3;
193  m_u["gmol"] = 1.0e-3;
194  m_u["mole"] = 1.0e-3;
195  m_u["kmol"] = 1.0;
196  m_u["kgmol"] = 1.0;
197  m_u["molec"] = 1.0/Avogadro;
198 
199  // temperature
200  m_u["K"] = 1.0;
201  m_u["C"] = 1.0;
202  m_u["Kelvin"] = 1.0;
203 
204  // mass
205  m_u["gm"] = 1.0e-3;
206  m_u["g"] = 1.0e-3;
207  m_u["kg"] = 1.0;
208 
209  // pressure
210  m_u["atm"] = 1.01325e5;
211  m_u["bar"] = 1.0e5;
212  m_u["Pa"] = 1.0;
213 
214  // time
215  m_u["s"] = 1.0;
216  m_u["min"] = 60.0;
217  m_u["hr"] = 3600.0;
218  m_u["ms"] = 0.001;
219 
220  // electric potential
221  m_u["volt"] = 1.0;
222 
223  // charge
224  m_u["coulomb"] = 1.0;
225 
226  /*
227  // frequency - Took frequency out to reevaluate it. Inverse cm is probably the wrong default unit
228  m_u["hZ"] = 0.01/(lightSpeed);
229  m_u["cm^-1"] = 1.0;
230  m_u["m^-1"] = 0.1;
231  m_u["cm-1"] = m_u["cm^-1"];
232  m_u["m-1"] = m_u["m^-1"];
233  m_u["wavenumbers"] = m_u["cm^-1"];
234  */
235 
236  // viscosity
237  m_u["Pa-s"] = 1;
238  m_u["poise"] = 0.1;
239  m_u["centipoise"] = 0.001;
240  m_u["P"] = 0.1;
241  m_u["cP"] = 0.001;
242 
243  // volume
244  m_u["kL"] = 1.0;
245  m_u["liter"] = 0.001;
246  m_u["L"] = 0.001;
247  m_u["l"] = 0.001;
248  m_u["mL"] = 1.0e-6;
249  m_u["ml"] = 1.0e-6;
250  m_u["cc"] = 1.0e-6;
251 
252  m_act_u["eV"] = m_u["eV"]; // /m_u["molec"];
253  m_act_u["K"] = GasConstant;
254  m_act_u["Kelvin"] = GasConstant;
255  m_act_u["Dimensionless"] = (GasConstant * 273.15);
256  }
257 };
258 }
259 
260 #endif
std::map< std::string, doublereal > m_u
Map between a string and a units double value.
Definition: units.h:151
doublereal actEnergyToSI(const std::string &units_)
Return the multiplier required to convert an activation energy to SI units.
Definition: units.h:55
Unit()
Units class constructor, containing the default mappings between strings and units.
Definition: units.h:165
virtual ~Unit()
Empty Destructor.
Definition: units.h:48
const size_t npos
index returned by functions to indicate "no position"
Definition: ct_defs.h:165
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:143
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:72
std::map< std::string, doublereal > m_act_u
Map between a string and a units double value for activation energy units.
Definition: units.h:158
static Unit * units()
Initialize the static Unit class.
Definition: units.h:29
Base class for exceptions thrown by Cantera classes.
Definition: ctexceptions.h:99
const doublereal Avogadro
Avogadro's Number [number/kmol].
Definition: ct_defs.h:61
const doublereal GasConstant
Universal Gas Constant. [J/kmol/K].
Definition: ct_defs.h:64
static mutex_t units_mutex
Decl for static locker for Units singleton.
Definition: units.h:161
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