17const map<string, Units> knownUnits{
22 {
"kg",
Units(1.0, 1, 0, 0)},
23 {
"g",
Units(1e-3, 1, 0, 0)},
26 {
"m",
Units(1.0, 0, 1, 0)},
27 {
"micron",
Units(1e-6, 0, 1, 0)},
28 {
"angstrom",
Units(1e-10, 0, 1, 0)},
29 {
"Å",
Units(1e-10, 0, 1, 0)},
32 {
"s",
Units(1.0, 0, 0, 1)},
33 {
"min",
Units(60, 0, 0, 1)},
34 {
"hr",
Units(3600, 0, 0, 1)},
37 {
"K",
Units(1.0, 0, 0, 0, 1)},
38 {
"C",
Units(1.0, 0, 0, 0, 1)},
41 {
"A",
Units(1.0, 0, 0, 0, 0, 1)},
44 {
"mol",
Units(1e-3, 0, 0, 0, 0, 0, 1)},
45 {
"gmol",
Units(1e-3, 0, 0, 0, 0, 0, 1)},
46 {
"mole",
Units(1e-3, 0, 0, 0, 0, 0, 1)},
47 {
"kmol",
Units(1.0, 0, 0, 0, 0, 0, 1)},
48 {
"kgmol",
Units(1.0, 0, 0, 0, 0, 0, 1)},
52 {
"J",
Units(1.0, 1, 2, -2)},
53 {
"cal",
Units(4.184, 1, 2, -2)},
54 {
"erg",
Units(1e-7, 1, 2, -2)},
58 {
"N",
Units(1.0, 1, 1, -2)},
59 {
"dyn",
Units(1e-5, 1, 1, -2)},
62 {
"Pa",
Units(1.0, 1, -1, -2)},
64 {
"bar",
Units(1.0e5, 1, -1, -2)},
65 {
"dyn/cm^2",
Units(0.1, 1, -1, -2)},
68 {
"m^3",
Units(1.0, 0, 3, 0)},
69 {
"m³",
Units(1.0, 0, 3, 0)},
70 {
"liter",
Units(0.001, 0, 3, 0)},
71 {
"L",
Units(0.001, 0, 3, 0)},
72 {
"l",
Units(0.001, 0, 3, 0)},
73 {
"cc",
Units(1.0e-6, 0, 3, 0)},
76 {
"ohm",
Units(1.0, 1, 2, -3, 0, -2)},
77 {
"V",
Units(1.0, 1, 2, -3, 0, -1)},
78 {
"coulomb",
Units(1.0, 0, 0, 1, 0, 1)},
81 {
"J/kmol",
Units(1.0, 1, 2, -2, 0, 0, -1)},
84const map<string, double> prefixes{
111 double temperature,
double current,
double quantity)
114 , m_length_dim(length)
116 , m_temperature_dim(temperature)
117 , m_current_dim(current)
118 , m_quantity_dim(quantity)
120 if (mass != 0 && length == -mass && time == -2 * mass
121 && temperature == 0 && current == 0 && quantity == 0) {
124 }
else if (mass != 0 && length == 2 * mass && time == -2 * mass
125 && temperature == 0 && current == 0 && quantity == 0)
137 static std::regex regexp(
"[+-]?([0-9]+([.][0-9]*)?|[.][0-9]+)");
139 std::regex_search(name, matched, regexp);
140 if (matched.size()) {
141 string factor = *matched.begin();
142 if (name.find(
factor) == 0) {
150 size_t stop = name.find_first_of(
"*/", start);
151 size_t caret = name.find(
'^', start);
157 name.substr(start, std::min(caret, stop) - start));
159 double exponent = 1.0;
161 exponent =
fpValueCheck(name.substr(caret+1, stop-caret-1));
163 if (start != 0 && name[start-1] ==
'/') {
165 exponent = -exponent;
168 if (knownUnits.find(unit) != knownUnits.end()) {
170 *
this *= knownUnits.at(unit).pow(exponent);
173 string prefix = unit.substr(0, 1);
174 string suffix = unit.substr(1);
175 if (prefixes.find(prefix) != prefixes.end() &&
176 knownUnits.find(suffix) != knownUnits.end()) {
177 Units u = knownUnits.at(suffix);
178 u.
scale(prefixes.at(prefix));
179 *
this *= u.
pow(exponent);
182 "Unknown unit '{}' in unit string '{}'", unit, name);
194 "Detected non-unity conversion factor:\n"
195 "input '{}' is equivalent to '{}' where the conversion factor is '{}'",
202 return (m_mass_dim == other.m_mass_dim &&
203 m_length_dim == other.m_length_dim &&
204 m_time_dim == other.m_time_dim &&
205 m_temperature_dim == other.m_temperature_dim &&
206 m_current_dim == other.m_current_dim &&
207 m_quantity_dim == other.m_quantity_dim);
213 m_mass_dim += other.m_mass_dim;
214 m_length_dim += other.m_length_dim;
215 m_time_dim += other.m_time_dim;
216 m_temperature_dim += other.m_temperature_dim;
217 m_current_dim += other.m_current_dim;
218 m_quantity_dim += other.m_quantity_dim;
227 m_mass_dim * exponent,
228 m_length_dim * exponent,
229 m_time_dim * exponent,
230 m_temperature_dim * exponent,
231 m_current_dim * exponent,
232 m_quantity_dim * exponent);
237 map<string, double> dims{
241 {
"K", m_temperature_dim},
242 {
"A", m_current_dim},
243 {
"kmol", m_quantity_dim},
248 for (
auto const& [
dimension, exponent] : dims) {
249 int rounded = (int)round(exponent);
250 if (exponent == 0.) {
252 }
else if (exponent == 1.) {
253 num.append(fmt::format(
" * {}",
dimension));
254 }
else if (exponent == -1.) {
255 den.append(fmt::format(
" / {}",
dimension));
256 }
else if (exponent == rounded && rounded > 0) {
257 num.append(fmt::format(
" * {}^{}",
dimension, rounded));
258 }
else if (exponent == rounded) {
259 den.append(fmt::format(
" / {}^{}",
dimension, -rounded));
260 }
else if (exponent > 0) {
261 num.append(fmt::format(
" * {}^{}",
dimension, exponent));
263 den.append(fmt::format(
" / {}^{}",
dimension, -exponent));
269 return fmt::format(
"{}{}", num.substr(3), den);
272 return fmt::format(
"1{}", den);
285 return fmt::format(
"{} {}{}",
factor, num.substr(3), den);
288 return fmt::format(
"{}{}",
factor, den);
291bool Units::operator==(
const Units& other)
const
294 && m_mass_dim == other.m_mass_dim
295 && m_length_dim == other.m_length_dim
296 && m_time_dim == other.m_time_dim
297 && m_temperature_dim == other.m_temperature_dim
298 && m_current_dim == other.m_current_dim
299 && m_quantity_dim == other.m_quantity_dim
306 if (primary ==
"mass") {
308 }
else if (primary ==
"length") {
310 }
else if (primary ==
"time") {
312 }
else if (primary ==
"temperature") {
313 return m_temperature_dim;
314 }
else if (primary ==
"current") {
315 return m_current_dim;
316 }
else if (primary ==
"quantity") {
317 return m_quantity_dim;
320 "Unknown primary unit '{}'.", primary);
329 return stack[0].first;
338 if (
stack[0].second != 0.) {
340 "Standard unit is already defined.");
350 return stack[0].second;
357 "Standard unit is not defined.");
359 stack[0].second += exponent;
366 for (
auto& [current_unit, current_exp] :
stack) {
367 if (current_unit == units) {
368 current_exp += exponent;
374 stack.emplace_back(units, exponent);
384 for (
auto& [units, exponent] :
stack) {
388 out *= units.
pow(exponent);
402 map<string, string> units{
406 {
"quantity",
"kmol"},
409 {
"temperature",
"K"},
411 {
"activation-energy",
"J / kmol"},
415 for (
const auto& [dimension, default_unit] :
m_defaults) {
416 units[dimension] = default_unit;
422 units[
"activation-energy"] = fmt::format(
423 "{} / {}", units[
"energy"], units[
"quantity"]);
431 for (
const auto& name : units) {
432 auto unit =
Units(name);
433 if (unit.convertible(knownUnits.at(
"kg"))) {
436 }
else if (unit.convertible(knownUnits.at(
"m"))) {
439 }
else if (unit.convertible(knownUnits.at(
"s"))) {
442 }
else if (unit.convertible(knownUnits.at(
"kmol"))) {
445 }
else if (unit.convertible(knownUnits.at(
"Pa"))) {
448 }
else if (unit.convertible(knownUnits.at(
"J"))) {
451 }
else if (unit.convertible(knownUnits.at(
"K"))) {
453 if (unit.factor() != 1.) {
454 throw CanteraError(
"UnitSystem::setDefaults",
"Temperature scales "
455 "with non-unity conversion factor from Kelvin are not supported.");
457 }
else if (unit.convertible(knownUnits.at(
"A"))) {
459 if (unit.factor() != 1.) {
460 throw CanteraError(
"UnitSystem::setDefaults",
"Current scales "
461 "with non-unity conversion factor from Ampere are not supported.");
465 "Unable to match unit '{}' to a basic dimension", name);
475 for (
const auto& [dimension, name] : units) {
477 if (dimension ==
"mass" && unit.
convertible(knownUnits.at(
"kg"))) {
480 }
else if (dimension ==
"length" && unit.
convertible(knownUnits.at(
"m"))) {
483 }
else if (dimension ==
"time" && unit.
convertible(knownUnits.at(
"s"))) {
486 }
else if (dimension ==
"temperature" && unit.
convertible(knownUnits.at(
"K"))) {
488 if (unit.
factor() != 1.) {
489 throw CanteraError(
"UnitSystem::setDefaults",
"Temperature scales "
490 "with non-unity conversion factor from Kelvin are not supported.");
492 }
else if (dimension ==
"current" && unit.
convertible(knownUnits.at(
"A"))) {
494 if (unit.
factor() != 1.) {
495 throw CanteraError(
"UnitSystem::setDefaults",
"Current scales "
496 "with non-unity conversion factor from Ampere are not supported.");
498 }
else if (dimension ==
"quantity" && unit.
convertible(knownUnits.at(
"kmol"))) {
501 }
else if (dimension ==
"pressure" && unit.
convertible(knownUnits.at(
"Pa"))) {
504 }
else if (dimension ==
"energy" && unit.
convertible(knownUnits.at(
"J"))) {
507 }
else if (dimension ==
"activation-energy") {
511 "Unable to set default unit for '{}' to '{}' ({}).",
512 dimension, name, unit.
str());
515 if (units.find(
"activation-energy") != units.end()) {
534 "Unable to match unit '{}' to a unit of activation energy", e_units);
545 const Units& dest)
const
549 "Incompatible units:\n Units({}) and\n Units({})",
562 return value / dest.
factor()
578 return value * src.
factor()
587static pair<double, string> split_unit(
const AnyValue& v) {
588 if (v.
is<
string>()) {
591 size_t space = val_units.find(
" ");
594 "Couldn't parse '{}' as a space-separated value/unit pair\n",
598 val_units.substr(space+1)};
619 auto [value, units] = split_unit(v);
639 auto [value, units] = split_unit(v);
647 Units sourceUnits(units);
648 if (fabs(sourceUnits.
factor() - 1.0) < 1e-14) {
654 "Unable to convert value with non-default units to undefined units,\n"
655 "likely while creating a standalone ReactionRate object.");
659 const string& dest)
const
665 const Units& dest)
const
668 for (
const auto& val : vals) {
669 out.emplace_back(
convert(val, dest));
675 const string& dest)
const
686 throw CanteraError(
"UnitSystem::convertActivationEnergy",
687 "Don't understand units '{}' as an activation energy", src);
699 throw CanteraError(
"UnitSystem::convertActivationEnergy",
700 "Don't understand units '{}' as an activation energy", dest);
712 const Units& dest)
const
718 }
else if (dest.
convertible(knownUnits.at(
"eV"))) {
721 throw CanteraError(
"UnitSystem::convertActivationEnergyTo",
722 "'{}' is not a unit of activation energy", dest.
str());
733 }
else if (usrc.
convertible(knownUnits.at(
"eV"))) {
736 throw CanteraError(
"UnitSystem::convertActivationEnergyFrom",
737 "'{}' is not a unit of activation energy", src);
744 auto [value, units] = split_unit(v);
754 "UnitSystem::convertActivationEnergy", v, err.
getMessage());
763 const auto& get = getValue<string, string>;
765 delta[
"mass"] = get(
m_defaults,
"mass",
"kg");
768 delta[
"length"] = get(
m_defaults,
"length",
"m");
774 delta[
"pressure"] = get(
m_defaults,
"pressure",
"Pa");
777 delta[
"energy"] = get(
m_defaults,
"energy",
"J");
780 delta[
"quantity"] = get(
m_defaults,
"quantity",
"kmol");
786 delta[
"activation-energy"] = get(
m_defaults,
"activation-energy",
"J/kmol");
Header for unit conversion utilities, which are used to translate user input from input files (See In...
A map of string keys to values whose type can vary at runtime.
A wrapper for a variable whose type is determined at runtime.
const string & asString() const
Return the held value, if it is a string.
double & asDouble()
Return the held value as a double, if it is a double or a long int.
bool is() const
Returns true if the held value is of the specified type.
Base class for exceptions thrown by Cantera classes.
virtual string getMessage() const
Method overridden by derived classes to format the error message.
double m_activation_energy_factor
Factor to convert activation energy from this unit system to J/kmol.
bool m_explicit_activation_energy
True if activation energy units are set explicitly, rather than as a combination of energy and quanti...
double convertFrom(double value, const string &src) const
Convert value from the specified src units to units appropriate for this unit system (defined by setD...
double convertActivationEnergyTo(double value, const string &dest) const
Convert value to the units specified by dest from the default activation energy units.
double m_time_factor
Factor to convert time from this unit system to seconds.
double convertTo(double value, const string &dest) const
Convert value to the specified dest units from the appropriate units for this unit system (defined by...
double m_pressure_factor
Factor to convert pressure from this unit system to Pa.
UnitSystem()
Default constructor for unit system (needed as VS2019 does not recognize an optional argument with a ...
double m_energy_factor
Factor to convert energy from this unit system to J.
double convertRateCoeff(const AnyValue &val, const Units &dest) const
Convert a generic AnyValue node representing a reaction rate coefficient to the units specified in de...
double m_length_factor
Factor to convert length from this unit system to meters.
double convertActivationEnergyFrom(double value, const string &src) const
Convert value from the units specified by src to the default activation energy units.
map< string, string > m_defaults
Map of dimensions (mass, length, etc.) to names of specified default units.
double convertActivationEnergy(double value, const string &src, const string &dest) const
Convert value from the units of src to the units of dest, allowing for the different dimensions that ...
void setDefaults(std::initializer_list< string > units)
Set the default units to convert from when explicit units are not provided.
double m_mass_factor
Factor to convert mass from this unit system to kg.
double convert(double value, const string &src, const string &dest) const
Convert value from the units of src to the units of dest.
double m_quantity_factor
Factor to convert quantity from this unit system to kmol.
AnyMap getDelta(const UnitSystem &other) const
Get the changes to the defaults from other to this UnitSystem.
map< string, string > defaults() const
Return default units used by the unit system.
void setDefaultActivationEnergy(const string &e_units)
Set the default units to convert from when using the convertActivationEnergy function.
A representation of the units associated with a dimensional quantity.
double m_energy_dim
pseudo-dimension to track explicit energy units
double m_pressure_dim
pseudo-dimension to track explicit pressure units
void scale(double k)
Scale the unit by the factor k
double m_factor
conversion factor to Cantera base units
Units pow(double exponent) const
Raise these Units to a power, changing both the conversion factor and the dimensions of these Units.
Units & operator*=(const Units &other)
Multiply two Units objects, combining their conversion factors and dimensions.
string str(bool skip_unity=true) const
Provide a string representation of these Units.
double dimension(const string &primary) const
Return dimension of primary unit component ("mass", "length", "time", "temperature",...
bool convertible(const Units &other) const
Returns true if the specified Units are dimensionally consistent.
double factor() const
Return the factor for converting from this unit to Cantera's base units.
Units(double factor=1.0, double mass=0, double length=0, double time=0, double temperature=0, double current=0, double quantity=0)
Create a Units object with the specified dimensions.
Definitions for the classes that are thrown when Cantera experiences an error condition (also contain...
This file contains definitions for utility functions and text for modules, inputfiles and logging,...
string trimCopy(const string &input)
Trim.
double fpValueCheck(const string &val)
Translate a string into one double value, with error checking.
const double Avogadro
Avogadro's Number [number/kmol].
const double OneAtm
One atmosphere [Pa].
const double GasConstant
Universal Gas Constant [J/kmol/K].
const double ElectronCharge
Elementary charge [C].
Namespace for the Cantera kernel.
const size_t npos
index returned by functions to indicate "no position"
const double SmallNumber
smallest number to compare to zero.
Contains declarations for string manipulation functions within Cantera.
double standardExponent() const
Effective exponent of standard unit.
void update(const Units &units, double exponent)
Update exponent of item with matching units; if it does not exist, add unit-exponent pair at end of s...
Units standardUnits() const
Get standard unit used by UnitStack.
Units product() const
Calculate product of units-exponent stack.
void join(double exponent)
Join (update) exponent of standard units, where the updated exponent is the sum of the pre-existing e...
vector< pair< Units, double > > stack
Stack uses vector of pairs.
void setStandardUnits(Units &standardUnits)
Set standard units.
Various templated functions that carry out common vector and polynomial operations (see Templated Arr...