units.h
Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014 #ifndef CT_UNITS_H
00015 #define CT_UNITS_H
00016
00017 #include "ct_defs.h"
00018 #include "ctexceptions.h"
00019
00020 #include <string>
00021
00022 #if defined(THREAD_SAFE_CANTERA)
00023 #include <boost/thread/mutex.hpp>
00024 #endif
00025
00026 namespace Cantera {
00027
00028
00029
00030
00031
00032
00033 class Unit {
00034 public:
00035
00036
00037 static Unit* units() {
00038 #if defined(THREAD_SAFE_CANTERA)
00039 boost::mutex::scoped_lock lock(units_mutex) ;
00040 #endif
00041 if (!s_u) s_u = new Unit;
00042 return s_u;
00043 }
00044
00045
00046
00047
00048
00049 static void deleteUnit() {
00050 #if defined(THREAD_SAFE_CANTERA)
00051 boost::mutex::scoped_lock lock(units_mutex) ;
00052 #endif
00053 if (s_u) {
00054 delete s_u;
00055 s_u = 0;
00056 }
00057 }
00058
00059
00060 virtual ~Unit() {}
00061
00062
00063
00064
00065
00066
00067 doublereal actEnergyToSI(std::string units) {
00068 if (m_act_u.find(units) != m_act_u.end()) {
00069 return m_act_u[units];
00070 }
00071 else {
00072 return toSI(units);
00073 }
00074 }
00075
00076
00077
00078
00079
00080
00081
00082
00083
00084
00085 doublereal toSI(std::string units) {
00086
00087
00088 if (units == "") return 1.0;
00089
00090 doublereal f = 1.0, fctr;
00091 int tsize;
00092 std::string u = units, tok, tsub;
00093 std::string::size_type k;
00094 char action = '-';
00095
00096 while (1 > 0) {
00097
00098
00099
00100 k = u.find_first_of("/-");
00101 if (k != std::string::npos)
00102 tok = u.substr(0,k);
00103 else
00104 tok = u;
00105 tsize = static_cast<int>(tok.size());
00106 if (tsize == 0)
00107 fctr = 1.0;
00108 else if (tok[tsize - 1] == '2') {
00109 tsub = tok.substr(0,tsize-1);
00110 fctr = m_u[tsub];
00111 fctr *= fctr;
00112 }
00113 else if (tok[tsize - 1] == '3') {
00114 tsub = tok.substr(0,tsize-1);
00115 fctr = m_u[tsub];
00116 fctr *= fctr*fctr;
00117 }
00118 else if (tok[tsize - 1] == '4') {
00119 tsub = tok.substr(0,tsize-1);
00120 fctr = m_u[tsub];
00121 fctr *= fctr*fctr*fctr;
00122 }
00123 else if (tok[tsize - 1] == '5') {
00124 tsub = tok.substr(0,tsize-1);
00125 fctr = m_u[tsub];
00126 fctr *= fctr*fctr*fctr*fctr;
00127 }
00128 else if (tok[tsize - 1] == '6') {
00129 tsub = tok.substr(0,tsize-1);
00130 fctr = m_u[tsub];
00131 fctr *= fctr*fctr*fctr*fctr*fctr;
00132 }
00133 else {
00134 tsub = tok;
00135 fctr = m_u[tok];
00136 }
00137
00138
00139
00140 if (fctr == 0)
00141 throw CanteraError("toSI","unknown unit: "+tsub);
00142 if (action == '-') f *= fctr;
00143 else if (action == '/') f /= fctr;
00144 if (k == std::string::npos) break;
00145 action = u[k];
00146 u = u.substr(k+1,u.size());
00147 }
00148 return f;
00149 }
00150
00151 private:
00152
00153
00154 static Unit* s_u;
00155
00156
00157
00158
00159
00160
00161
00162 std::map<std::string, doublereal> m_u;
00163
00164
00165
00166
00167
00168
00169 std::map<std::string, doublereal> m_act_u;
00170
00171 #if defined(THREAD_SAFE_CANTERA)
00172
00173 static boost::mutex units_mutex;
00174 #endif
00175
00176
00177
00178
00179
00180 Unit(){
00181
00182
00183 m_u["m"] = 1.0;
00184 m_u["cm"] = 0.01;
00185 m_u["km"] = 1.0e3;
00186 m_u["mm"] = 1.0e-3;
00187 m_u["micron"] = 1.0e-6;
00188 m_u["nm"] = 1.0e-9;
00189 m_u["A"] = 1.0e-10;
00190 m_u["Angstrom"] = 1.0e-10;
00191 m_u["Angstroms"] = 1.0e-10;
00192
00193
00194 m_u["J"] = 1.0;
00195 m_u["kJ"] = 1.0e3;
00196 m_u["cal"] = 4.184;
00197 m_u["kcal"] = 4184.0;
00198 m_u["eV"] = Faraday;
00199
00200
00201 m_u["mol"] = 1.0e-3;
00202 m_u["gmol"] = 1.0e-3;
00203 m_u["mole"] = 1.0e-3;
00204 m_u["kmol"] = 1.0;
00205 m_u["kgmol"] = 1.0;
00206 m_u["molec"] = 1.0/Avogadro;
00207
00208
00209 m_u["K"] = 1.0;
00210 m_u["C"] = 1.0;
00211
00212
00213 m_u["g"] = 1.0e-3;
00214 m_u["kg"] = 1.0;
00215
00216
00217 m_u["atm"] = 1.01325e5;
00218 m_u["bar"] = 1.0e5;
00219 m_u["Pa"] = 1.0;
00220
00221
00222 m_u["s"] = 1.0;
00223 m_u["min"] = 60.0;
00224 m_u["hr"] = 3600.0;
00225 m_u["ms"] = 0.001;
00226
00227
00228 m_u["hZ"] = 0.01/(lightSpeed);
00229 m_u["cm^-1"] = 1.0;
00230 m_u["m^-1"] = 0.1;
00231 m_u["cm-1"] = m_u["cm^-1"];
00232 m_u["m-1"] = m_u["m^-1"];
00233 m_u["wavenumbers"] = m_u["cm^-1"];
00234
00235
00236 m_u["poise"] = 0.1;
00237 m_u["centipoise"] = 0.001;
00238
00239 m_act_u["eV"] = m_u["eV"];
00240 m_act_u["K"] = GasConstant;
00241 m_act_u["Kelvin"] = GasConstant;
00242 m_act_u["Dimensionless"] = (GasConstant * 273.15);
00243 }
00244 };
00245 }
00246
00247 #endif
00248