Cantera  2.5.1
Units.h
Go to the documentation of this file.
1 /**
2  * @file Units.h
3  * Header for unit conversion utilities, which are used to translate
4  * user input from input files (See \ref inputfiles and
5  * class \link Cantera::Units Units\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_UNITS_H
12 #define CT_UNITS_H
13 
14 #include "cantera/base/ct_defs.h"
15 
16 namespace Cantera
17 {
18 
19 class AnyValue;
20 class AnyMap;
21 
22 //! A representation of the units associated with a dimensional quantity.
23 /*!
24  * Used for converting quantities between unit systems and checking for
25  * dimensional consistency. Units objects are mainly used within UnitSystem
26  * class to convert values from a user-specified Unit system to Cantera's
27  * base units (SI + kmol).
28  */
29 class Units
30 {
31 public:
32  //! Create a Units object with the specified dimensions.
33  explicit Units(double factor=1.0, double mass=0, double length=0,
34  double time=0, double temperature=0, double current=0,
35  double quantity=0);
36 
37  //! Create an object with the specified dimensions
38  explicit Units(const std::string& name);
39 
40  //! Returns `true` if the specified Units are dimensionally consistent
41  bool convertible(const Units& other) const;
42 
43  //! Return the factor for converting from this unit to Cantera's base
44  //! units.
45  double factor() const { return m_factor; }
46 
47  //! Multiply two Units objects, combining their conversion factors and
48  //! dimensions
49  Units& operator*=(const Units& other);
50 
51  //! Provide a string representation of these Units
52  std::string str() const;
53 
54  //! Raise these Units to a power, changing both the conversion factor and
55  //! the dimensions of these Units.
56  Units pow(double expoonent) const;
57 
58 private:
59  //! Scale the unit by the factor `k`
60  void scale(double k) { m_factor *= k; }
61 
62  double m_factor; //!< conversion factor to Cantera base units
63  double m_mass_dim;
64  double m_length_dim;
65  double m_time_dim;
66  double m_temperature_dim;
67  double m_current_dim;
68  double m_quantity_dim;
69  double m_pressure_dim; //!< pseudo-dimension to track explicit pressure units
70  double m_energy_dim; //!< pseudo-dimension to track explicit energy units
71 
72  friend class UnitSystem;
73 };
74 
75 
76 //! Unit conversion utility
77 /*!
78  * Provides functions for converting dimensional values from a given unit system.
79  * The main use is for converting values specified in input files to Cantera's
80  * native unit system, which is SI units except for the use of kmol as the base
81  * unit of quantity, i.e. kilogram, meter, second, kelvin, ampere, and kmol.
82  *
83  * String representations of units can be written using multiplication,
84  * division, and exponentiation. Spaces are ignored. Positive, negative, and
85  * decimal exponents are permitted. Examples:
86  *
87  * kg*m/s^2
88  * J/kmol
89  * m*s^-2
90  * J/kg/K
91  *
92  * Metric prefixes are recognized for all units, e.g. nm, hPa, mg, EJ, mL, kcal.
93  *
94  * Special functions for converting activation energies allow these values to be
95  * expressed as either energy per quantity, energy (e.g. eV), or temperature by
96  * applying a factor of the Avogadro number or the gas constant where needed.
97  *
98  * @ingroup inputfiles
99  */
101 {
102 public:
103  //! Create a unit system with the specified default units
104  UnitSystem(std::initializer_list<std::string> units);
105 
106  //! Default constructor for unit system (needed as VS2019 does not
107  //! recognize an optional argument with a default value)
109 
110  //! Set the default units to convert from when explicit units are not
111  //! provided. Defaults can be set for mass, length, time, quantity, energy,
112  //! and pressure. Conversion using the pressure or energy units is done only
113  //! when the target units explicitly contain pressure or energy units.
114  //!
115  //! * To use SI+kmol: `setDefaults({"kg", "m", "s", "Pa", "J", "kmol"});`
116  //! * To use CGS+mol: `setDefaults({"cm", "g", "dyn/cm^2", "erg", "mol"});`
117  void setDefaults(std::initializer_list<std::string> units);
118 
119  //! Set the default units using a map of dimension to unit pairs.
120  //!
121  //! Defaults for dimensions not specified will be left unchanged. To use
122  //! Cantera's default units:
123  //! ```
124  //! UnitSystem system;
125  //! std::map<string, string> defaults{
126  //! {"length", "m"}, {"mass", "kg"}, {"time", "s"},
127  //! {"quantity", "kmol"}, {"pressure", "Pa"}, {"energy", "J"},
128  //! {"activation-energy", "J/kmol"}
129  //! };
130  //! setDefaults(defaults);
131  //! ```
132  void setDefaults(const std::map<std::string, std::string>& units);
133 
134  //! Set the default units to convert from when using the
135  //! `convertActivationEnergy` function.
136  void setDefaultActivationEnergy(const std::string& e_units);
137 
138  //! Convert `value` from the units of `src` to the units of `dest`.
139  double convert(double value, const std::string& src,
140  const std::string& dest) const;
141  double convert(double value, const Units& src, const Units& dest) const;
142 
143  //! Convert `value` from this unit system (defined by `setDefaults`) to the
144  //! specified units.
145  //!
146  //! @warning This function is an experimental part of the %Cantera API and
147  //! may be changed or removed without notice.
148  double convert(double value, const std::string& dest) const;
149  double convert(double value, const Units& dest) const;
150 
151  //! Convert a generic AnyValue node to the units specified in `dest`. If the
152  //! input is a double, convert it using the default units. If the input is a
153  //! string, treat this as a dimensioned value, e.g. '988 kg/m^3' and convert
154  //! from the specified units.
155  double convert(const AnyValue& val, const std::string& dest) const;
156  double convert(const AnyValue& val, const Units& dest) const;
157 
158  //! Convert an array of AnyValue nodes to the units specified in `dest`. For
159  //! each node, if the value is a double, convert it using the default units,
160  //! and if it is a string, treat it as a value with the given dimensions.
161  vector_fp convert(const std::vector<AnyValue>& vals,
162  const std::string& dest) const;
163  vector_fp convert(const std::vector<AnyValue>& vals,
164  const Units& dest) const;
165 
166  //! Convert `value` from the units of `src` to the units of `dest`, allowing
167  //! for the different dimensions that can be used for activation energies
168  double convertActivationEnergy(double value, const std::string& src,
169  const std::string& dest) const;
170 
171  //! Convert `value` from the default activation energy units to the
172  //! specified units
173  //!
174  //! @warning This function is an experimental part of the %Cantera API and
175  //! may be changed or removed without notice.
176  double convertActivationEnergy(double value, const std::string& dest) const;
177 
178  //! Convert a generic AnyValue node to the units specified in `dest`. If the
179  //! input is a double, convert it using the default units. If the input is a
180  //! string, treat this as a dimensioned value, e.g. '2.7e4 J/kmol' and
181  //! convert from the specified units.
182  double convertActivationEnergy(const AnyValue& val,
183  const std::string& dest) const;
184 
185 private:
186  //! Factor to convert mass from this unit system to kg
188 
189  //! Factor to convert length from this unit system to meters
191 
192  //! Factor to convert time from this unit system to seconds
194 
195  //! Factor to convert pressure from this unit system to Pa
197 
198  //! Factor to convert energy from this unit system to J
200 
201  //! Factor to convert activation energy from this unit system to J/kmol
203 
204  //! Factor to convert quantity from this unit system to kmol
206 
207  //! True if activation energy units are set explicitly, rather than as a
208  //! combination of energy and quantity units
210 };
211 
212 }
213 
214 #endif
Unit conversion utility.
Definition: Units.h:101
double m_activation_energy_factor
Factor to convert activation energy from this unit system to J/kmol.
Definition: Units.h:202
bool m_explicit_activation_energy
True if activation energy units are set explicitly, rather than as a combination of energy and quanti...
Definition: Units.h:209
double m_time_factor
Factor to convert time from this unit system to seconds.
Definition: Units.h:193
double m_pressure_factor
Factor to convert pressure from this unit system to Pa.
Definition: Units.h:196
UnitSystem()
Default constructor for unit system (needed as VS2019 does not recognize an optional argument with a ...
Definition: Units.h:108
double m_energy_factor
Factor to convert energy from this unit system to J.
Definition: Units.h:199
double m_length_factor
Factor to convert length from this unit system to meters.
Definition: Units.h:190
double convert(double value, const std::string &src, const std::string &dest) const
Convert value from the units of src to the units of dest.
Definition: Units.cpp:322
void setDefaultActivationEnergy(const std::string &e_units)
Set the default units to convert from when using the convertActivationEnergy function.
Definition: Units.cpp:306
double convertActivationEnergy(double value, const std::string &src, const std::string &dest) const
Convert value from the units of src to the units of dest, allowing for the different dimensions that ...
Definition: Units.cpp:405
double m_mass_factor
Factor to convert mass from this unit system to kg.
Definition: Units.h:187
double m_quantity_factor
Factor to convert quantity from this unit system to kmol.
Definition: Units.h:205
void setDefaults(std::initializer_list< std::string > units)
Set the default units to convert from when explicit units are not provided.
Definition: Units.cpp:241
A representation of the units associated with a dimensional quantity.
Definition: Units.h:30
Units pow(double expoonent) const
Raise these Units to a power, changing both the conversion factor and the dimensions of these Units.
Definition: Units.cpp:212
double m_energy_dim
pseudo-dimension to track explicit energy units
Definition: Units.h:70
double m_pressure_dim
pseudo-dimension to track explicit pressure units
Definition: Units.h:69
void scale(double k)
Scale the unit by the factor k
Definition: Units.h:60
double m_factor
conversion factor to Cantera base units
Definition: Units.h:62
Units & operator*=(const Units &other)
Multiply two Units objects, combining their conversion factors and dimensions.
Definition: Units.cpp:198
bool convertible(const Units &other) const
Returns true if the specified Units are dimensionally consistent.
Definition: Units.cpp:188
double factor() const
Return the factor for converting from this unit to Cantera's base units.
Definition: Units.h:45
std::string str() const
Provide a string representation of these Units.
Definition: Units.cpp:222
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.
Definition: Units.cpp:107
This file contains definitions of terms that are used in internal routines and are unlikely to need m...
std::vector< double > vector_fp
Turn on the use of stl vectors for the basic array type within cantera Vector of doubles.
Definition: ct_defs.h:180
Namespace for the Cantera kernel.
Definition: AnyMap.cpp:264