src/utilities/units/QuantityConverter.hpp
| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | /*********************************************************************************************************************** | ||
| 2 | * OpenStudio(R), Copyright (c) Alliance for Energy Innovation, LLC. | ||
| 3 | * See also https://openstudio.net/license | ||
| 4 | ***********************************************************************************************************************/ | ||
| 5 | |||
| 6 | #ifndef UTILITIES_UNITS_QUANTITYCONVERTER_HPP | ||
| 7 | #define UTILITIES_UNITS_QUANTITYCONVERTER_HPP | ||
| 8 | |||
| 9 | #include "../UtilitiesAPI.hpp" | ||
| 10 | #include "../core/Logger.hpp" | ||
| 11 | |||
| 12 | #include "Unit.hpp" | ||
| 13 | #include <string> | ||
| 14 | #include <map> | ||
| 15 | |||
| 16 | namespace openstudio { | ||
| 17 | |||
| 18 | class Quantity; | ||
| 19 | class OSQuantityVector; | ||
| 20 | |||
| 21 | // JMT@20100902 - it's necessary to move the temperature conversion | ||
| 22 | // rule enum into a class that is *not* %ignored by swig, if we want | ||
| 23 | // it usable by swig | ||
| 24 | // ETH@20120918 - at some point, the TemperatureConversionRule was removed from the interface, | ||
| 25 | // but it may need to be re-added when we start converting more temperature data. | ||
| 26 | struct TemperatureConversionRule | ||
| 27 | { | ||
| 28 | /** Enum to allow user to communicate temperature conversion preferences. | ||
| 29 | * | ||
| 30 | * In STND mode, convertToCelsiusUnits and convertToFahrenheit units assume that temperatures | ||
| 31 | * like 273 K and 480 R are absolute temperatures in need of offset, but that quantities with | ||
| 32 | * other exponents on K or R, or other base units mixed in, are relative temperature quantities | ||
| 33 | * whose values can be transferred directly. | ||
| 34 | * | ||
| 35 | * In ABS mode, convertToCelsiusUnits and convertToFahrenheit units assume that all quantities | ||
| 36 | * were derived from absolute temperatures, and the offset should be applied accordingly, as in | ||
| 37 | * 280 K^3 = (pow(280,1.0/3.0) - 273.15)^3 C^3. | ||
| 38 | * | ||
| 39 | * In REL mode, convertToCelsiusUnits and convertToFahrenheit units assume that all | ||
| 40 | * temperatures are relative, such that values can be transferred directly, i.e., 2 K = 2 C. | ||
| 41 | * | ||
| 42 | * When converting Celsius and Fahrenheit to other units, either ABS or REL mode is used, | ||
| 43 | * depending on the value of q.isAbsolute(). | ||
| 44 | */ | ||
| 45 | enum domain | ||
| 46 | { | ||
| 47 | STND, | ||
| 48 | ABS, | ||
| 49 | REL | ||
| 50 | }; | ||
| 51 | }; | ||
| 52 | |||
| 53 | /** A struct to store all the information necessary to convert from one base unit to | ||
| 54 | * another. All conversions will be handled by going through SI, so these ConversionFactors | ||
| 55 | * will be stored in either the toSI or fromSI maps. | ||
| 56 | */ | ||
| 57 | struct baseUnitConversionFactor | ||
| 58 | { | ||
| 59 | UnitSystem system; | ||
| 60 | std::string originalUnit; | ||
| 61 | std::string targetUnit; | ||
| 62 | double factor; | ||
| 63 | double offset; | ||
| 64 | }; | ||
| 65 | |||
| 66 | /** Singleton for converting quantities to different \link UnitSystem unit systems \endlink or | ||
| 67 | * to targeted \link Unit units \endlink */ | ||
| 68 | class UTILITIES_API QuantityConverter | ||
| 69 | { | ||
| 70 | public: | ||
| 71 | static QuantityConverter& instance(); | ||
| 72 | |||
| 73 | QuantityConverter(const QuantityConverter& other) = delete; | ||
| 74 | QuantityConverter(QuantityConverter&& other) = delete; | ||
| 75 | QuantityConverter& operator=(const QuantityConverter&) = delete; | ||
| 76 | QuantityConverter& operator=(QuantityConverter&&) = delete; | ||
| 77 | |||
| 78 | boost::optional<Quantity> convert(const Quantity& q, UnitSystem sys) const; | ||
| 79 | |||
| 80 | boost::optional<Quantity> convert(const Quantity& original, const Unit& targetUnits) const; | ||
| 81 | |||
| 82 | private: | ||
| 83 | − | REGISTER_LOGGER("openstudio.units.QuantityConverter"); | |
| 84 | QuantityConverter(); | ||
| 85 | 64 | ~QuantityConverter() = default; | |
| 86 | |||
| 87 | using BaseUnitConversionMap = std::map<std::string, baseUnitConversionFactor>; | ||
| 88 | using UnitSystemConversionMultiMap = std::multimap<UnitSystem, baseUnitConversionFactor>; | ||
| 89 | |||
| 90 | BaseUnitConversionMap m_toSImap; | ||
| 91 | UnitSystemConversionMultiMap m_fromSIBySystemMap; | ||
| 92 | |||
| 93 | boost::optional<Quantity> m_convertToSI(const Quantity& original) const; | ||
| 94 | |||
| 95 | Quantity m_convertFromSI(const Quantity& original, const UnitSystem& targetSys) const; | ||
| 96 | |||
| 97 | boost::optional<Quantity> m_convertToTargetFromSI(const Quantity& original, const Unit& targetUnits) const; | ||
| 98 | }; | ||
| 99 | |||
| 100 | /** Non-member function to simplify interface for users. \relates QuantityConverter */ | ||
| 101 | UTILITIES_API boost::optional<double> convert(double original, const std::string& originalUnits, const std::string& finalUnits); | ||
| 102 | |||
| 103 | /** Non-member function to simplify interface for users. \relates QuantityConverter */ | ||
| 104 | UTILITIES_API boost::optional<Quantity> convert(const Quantity& original, UnitSystem sys); | ||
| 105 | |||
| 106 | /** Non-member function that uses just two calls to QuantityConverter to convert an entire | ||
| 107 | * OSQuantityVector. \relates QuantityConverter \relates OSQuantityVector */ | ||
| 108 | UTILITIES_API OSQuantityVector convert(const OSQuantityVector& original, UnitSystem sys); | ||
| 109 | |||
| 110 | /** Non-member function to simplify interface for users. \relates QuantityConverter */ | ||
| 111 | UTILITIES_API boost::optional<Quantity> convert(const Quantity& original, const Unit& targetUnits); | ||
| 112 | |||
| 113 | /** Non-member function that uses just two calls to QuantityConverter to convert an entire | ||
| 114 | * OSQuantityVector. \relates QuantityConverter \relates OSQuantityVector */ | ||
| 115 | UTILITIES_API OSQuantityVector convert(const OSQuantityVector& original, const Unit& targetUnits); | ||
| 116 | |||
| 117 | } // namespace openstudio | ||
| 118 | |||
| 119 | #endif // UTILITIES_UNITS_QUANTITYCONVERTER_HPP | ||
| 120 |