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 {
"liter",
Units(0.001, 0, 3, 0)},
70 {
"L",
Units(0.001, 0, 3, 0)},
71 {
"l",
Units(0.001, 0, 3, 0)},
72 {
"cc",
Units(1.0e-6, 0, 3, 0)},
75 {
"ohm",
Units(1.0, 1, 2, -3, 0, -2)},
76 {
"V",
Units(1.0, 1, 2, -3, 0, -1)},
77 {
"coulomb",
Units(1.0, 0, 0, 1, 0, 1)},
80 {
"J/kmol",
Units(1.0, 1, 2, -2, 0, 0, -1)},
83const map<string, double> prefixes{
110 double temperature,
double current,
double quantity)
113 , m_length_dim(length)
115 , m_temperature_dim(temperature)
116 , m_current_dim(current)
117 , m_quantity_dim(quantity)
119 if (mass != 0 && length == -mass && time == -2 * mass
120 && temperature == 0 && current == 0 && quantity == 0) {
123 }
else if (mass != 0 && length == 2 * mass && time == -2 * mass
124 && temperature == 0 && current == 0 && quantity == 0)
136 static std::regex regexp(
"[+-]?([0-9]+([.][0-9]*)?|[.][0-9]+)");
138 std::regex_search(name, matched, regexp);
139 if (matched.size()) {
140 string factor = *matched.begin();
141 if (name.find(
factor) == 0) {
149 size_t stop = name.find_first_of(
"*/", start);
150 size_t caret = name.find(
'^', start);
156 name.substr(start, std::min(caret, stop) - start));
158 double exponent = 1.0;
160 exponent =
fpValueCheck(name.substr(caret+1, stop-caret-1));
162 if (start != 0 && name[start-1] ==
'/') {
164 exponent = -exponent;
167 if (knownUnits.find(unit) != knownUnits.end()) {
169 *
this *= knownUnits.at(unit).pow(exponent);
172 string prefix = unit.substr(0, 1);
173 string suffix = unit.substr(1);
174 if (prefixes.find(prefix) != prefixes.end() &&
175 knownUnits.find(suffix) != knownUnits.end()) {
176 Units u = knownUnits.at(suffix);
177 u.
scale(prefixes.at(prefix));
178 *
this *= u.
pow(exponent);
181 "Unknown unit '{}' in unit string '{}'", unit, name);
193 "Detected non-unity conversion factor:\n"
194 "input '{}' is equivalent to '{}' where the conversion factor is '{}'",
201 return (m_mass_dim == other.m_mass_dim &&
202 m_length_dim == other.m_length_dim &&
203 m_time_dim == other.m_time_dim &&
204 m_temperature_dim == other.m_temperature_dim &&
205 m_current_dim == other.m_current_dim &&
206 m_quantity_dim == other.m_quantity_dim);
212 m_mass_dim += other.m_mass_dim;
213 m_length_dim += other.m_length_dim;
214 m_time_dim += other.m_time_dim;
215 m_temperature_dim += other.m_temperature_dim;
216 m_current_dim += other.m_current_dim;
217 m_quantity_dim += other.m_quantity_dim;
226 m_mass_dim * exponent,
227 m_length_dim * exponent,
228 m_time_dim * exponent,
229 m_temperature_dim * exponent,
230 m_current_dim * exponent,
231 m_quantity_dim * exponent);
236 map<string, double> dims{
240 {
"K", m_temperature_dim},
241 {
"A", m_current_dim},
242 {
"kmol", m_quantity_dim},
247 for (
auto const& [
dimension, exponent] : dims) {
248 int rounded = (int)round(exponent);
249 if (exponent == 0.) {
251 }
else if (exponent == 1.) {
252 num.append(fmt::format(
" * {}",
dimension));
253 }
else if (exponent == -1.) {
254 den.append(fmt::format(
" / {}",
dimension));
255 }
else if (exponent == rounded && rounded > 0) {
256 num.append(fmt::format(
" * {}^{}",
dimension, rounded));
257 }
else if (exponent == rounded) {
258 den.append(fmt::format(
" / {}^{}",
dimension, -rounded));
259 }
else if (exponent > 0) {
260 num.append(fmt::format(
" * {}^{}",
dimension, exponent));
262 den.append(fmt::format(
" / {}^{}",
dimension, -exponent));
268 return fmt::format(
"{}{}", num.substr(3), den);
271 return fmt::format(
"1{}", den);
284 return fmt::format(
"{} {}{}",
factor, num.substr(3), den);
287 return fmt::format(
"{}{}",
factor, den);
290bool Units::operator==(
const Units& other)
const
293 && m_mass_dim == other.m_mass_dim
294 && m_length_dim == other.m_length_dim
295 && m_time_dim == other.m_time_dim
296 && m_temperature_dim == other.m_temperature_dim
297 && m_current_dim == other.m_current_dim
298 && m_quantity_dim == other.m_quantity_dim
305 if (primary ==
"mass") {
307 }
else if (primary ==
"length") {
309 }
else if (primary ==
"time") {
311 }
else if (primary ==
"temperature") {
312 return m_temperature_dim;
313 }
else if (primary ==
"current") {
314 return m_current_dim;
315 }
else if (primary ==
"quantity") {
316 return m_quantity_dim;
319 "Unknown primary unit '{}'.", primary);
328 return stack[0].first;
337 if (
stack[0].second != 0.) {
339 "Standard unit is already defined.");
349 return stack[0].second;
356 "Standard unit is not defined.");
358 stack[0].second += exponent;
365 for (
auto& [current_unit, current_exp] :
stack) {
366 if (current_unit == units) {
367 current_exp += exponent;
373 stack.emplace_back(units, exponent);
383 for (
auto& [units, exponent] :
stack) {
387 out *= units.
pow(exponent);
401 map<string, string> units{
405 {
"quantity",
"kmol"},
408 {
"temperature",
"K"},
410 {
"activation-energy",
"J / kmol"},
414 for (
const auto& [dimension, default_unit] :
m_defaults) {
415 units[dimension] = default_unit;
421 units[
"activation-energy"] = fmt::format(
422 "{} / {}", units[
"energy"], units[
"quantity"]);
430 for (
const auto& name : units) {
431 auto unit =
Units(name);
432 if (unit.convertible(knownUnits.at(
"kg"))) {
435 }
else if (unit.convertible(knownUnits.at(
"m"))) {
438 }
else if (unit.convertible(knownUnits.at(
"s"))) {
441 }
else if (unit.convertible(knownUnits.at(
"kmol"))) {
444 }
else if (unit.convertible(knownUnits.at(
"Pa"))) {
447 }
else if (unit.convertible(knownUnits.at(
"J"))) {
450 }
else if (unit.convertible(knownUnits.at(
"K"))) {
452 if (unit.factor() != 1.) {
453 throw CanteraError(
"UnitSystem::setDefaults",
"Temperature scales "
454 "with non-unity conversion factor from Kelvin are not supported.");
456 }
else if (unit.convertible(knownUnits.at(
"A"))) {
458 if (unit.factor() != 1.) {
459 throw CanteraError(
"UnitSystem::setDefaults",
"Current scales "
460 "with non-unity conversion factor from Ampere are not supported.");
464 "Unable to match unit '{}' to a basic dimension", name);
474 for (
const auto& [dimension, name] : units) {
476 if (dimension ==
"mass" && unit.
convertible(knownUnits.at(
"kg"))) {
479 }
else if (dimension ==
"length" && unit.
convertible(knownUnits.at(
"m"))) {
482 }
else if (dimension ==
"time" && unit.
convertible(knownUnits.at(
"s"))) {
485 }
else if (dimension ==
"temperature" && unit.
convertible(knownUnits.at(
"K"))) {
487 if (unit.
factor() != 1.) {
488 throw CanteraError(
"UnitSystem::setDefaults",
"Temperature scales "
489 "with non-unity conversion factor from Kelvin are not supported.");
491 }
else if (dimension ==
"current" && unit.
convertible(knownUnits.at(
"A"))) {
493 if (unit.
factor() != 1.) {
494 throw CanteraError(
"UnitSystem::setDefaults",
"Current scales "
495 "with non-unity conversion factor from Ampere are not supported.");
497 }
else if (dimension ==
"quantity" && unit.
convertible(knownUnits.at(
"kmol"))) {
500 }
else if (dimension ==
"pressure" && unit.
convertible(knownUnits.at(
"Pa"))) {
503 }
else if (dimension ==
"energy" && unit.
convertible(knownUnits.at(
"J"))) {
506 }
else if (dimension ==
"activation-energy") {
510 "Unable to set default unit for '{}' to '{}' ({}).",
511 dimension, name, unit.
str());
514 if (units.find(
"activation-energy") != units.end()) {
533 "Unable to match unit '{}' to a unit of activation energy", e_units);
544 const Units& dest)
const
548 "Incompatible units:\n Units({}) and\n Units({})",
561 return value / dest.
factor()
577 return value * src.
factor()
586static pair<double, string> split_unit(
const AnyValue& v) {
587 if (v.
is<
string>()) {
590 size_t space = val_units.find(
" ");
593 "Couldn't parse '{}' as a space-separated value/unit pair\n",
597 val_units.substr(space+1)};
618 auto [value, units] = split_unit(v);
638 auto [value, units] = split_unit(v);
646 Units sourceUnits(units);
647 if (fabs(sourceUnits.
factor() - 1.0) < 1e-14) {
653 "Unable to convert value with non-default units to undefined units,\n"
654 "likely while creating a standalone ReactionRate object.");
658 const string& dest)
const
664 const Units& dest)
const
667 for (
const auto& val : vals) {
668 out.emplace_back(
convert(val, dest));
674 const string& dest)
const
685 throw CanteraError(
"UnitSystem::convertActivationEnergy",
686 "Don't understand units '{}' as an activation energy", src);
698 throw CanteraError(
"UnitSystem::convertActivationEnergy",
699 "Don't understand units '{}' as an activation energy", dest);
711 const Units& dest)
const
717 }
else if (dest.
convertible(knownUnits.at(
"eV"))) {
720 throw CanteraError(
"UnitSystem::convertActivationEnergyTo",
721 "'{}' is not a unit of activation energy", dest.
str());
732 }
else if (usrc.
convertible(knownUnits.at(
"eV"))) {
735 throw CanteraError(
"UnitSystem::convertActivationEnergyFrom",
736 "'{}' is not a unit of activation energy", src);
743 auto [value, units] = split_unit(v);
753 "UnitSystem::convertActivationEnergy", v, err.
getMessage());
762 const auto& get = getValue<string, string>;
764 delta[
"mass"] = get(
m_defaults,
"mass",
"kg");
767 delta[
"length"] = get(
m_defaults,
"length",
"m");
773 delta[
"pressure"] = get(
m_defaults,
"pressure",
"Pa");
776 delta[
"energy"] = get(
m_defaults,
"energy",
"J");
779 delta[
"quantity"] = get(
m_defaults,
"quantity",
"kmol");
785 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...