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