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