src/utilities/units/Quantity.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_QUANTITY_HPP | ||
| 7 | #define UTILITIES_UNITS_QUANTITY_HPP | ||
| 8 | |||
| 9 | #include "../UtilitiesAPI.hpp" | ||
| 10 | |||
| 11 | #include "Unit.hpp" | ||
| 12 | |||
| 13 | namespace openstudio { | ||
| 14 | |||
| 15 | /** Quantity class defines value, unit pairs. Units are set at construction and are modified by | ||
| 16 | * arithmetic operations. The constructors and assignment operator ensure that this class | ||
| 17 | * behaves as "plain old data" (POD) by cloning any units information (to avoid multiple objects | ||
| 18 | * pointing to the exact same unit data). */ | ||
| 19 | class UTILITIES_API Quantity | ||
| 20 | { | ||
| 21 | public: | ||
| 22 | /** @name Constructors */ | ||
| 23 | //@{ | ||
| 24 | |||
| 25 | explicit Quantity(const UnitSystem& system = UnitSystem::Mixed); | ||
| 26 | |||
| 27 | explicit Quantity(double value, const UnitSystem& system = UnitSystem::Mixed); | ||
| 28 | |||
| 29 | Quantity(double value, const Unit& units); | ||
| 30 | |||
| 31 | Quantity(const Quantity& q); | ||
| 32 | |||
| 33 | Quantity& operator=(const Quantity& q); | ||
| 34 | |||
| 35 | 2503767 | virtual ~Quantity() = default; | |
| 36 | |||
| 37 | //@} | ||
| 38 | /** @name Value */ | ||
| 39 | //@{ | ||
| 40 | |||
| 41 | double value() const; | ||
| 42 | |||
| 43 | /** Value setter. */ | ||
| 44 | void setValue(double newValue); | ||
| 45 | |||
| 46 | //@} | ||
| 47 | /** @name Units */ | ||
| 48 | //@{ | ||
| 49 | |||
| 50 | /** Returns deep copy (clone) of this Quantity's units. */ | ||
| 51 | Unit units() const; | ||
| 52 | |||
| 53 | UnitSystem system() const; | ||
| 54 | |||
| 55 | /** Returns true if units().optionalCast<TemperatureUnit>(). */ | ||
| 56 | bool isTemperature() const; | ||
| 57 | |||
| 58 | /** Vector of base units available to the Quantity. */ | ||
| 59 | std::vector<std::string> baseUnits() const; | ||
| 60 | |||
| 61 | /** Base unit exponent getter. Returns 0 if baseUnit not present. */ | ||
| 62 | int baseUnitExponent(const std::string& baseUnit) const; | ||
| 63 | |||
| 64 | /** Sets the exponent of a base unit in this Quantity's m_units. This function will throw | ||
| 65 | * if baseUnit is not a valid option for this type of unit. A generic Unit allows any base | ||
| 66 | * unit to be set. */ | ||
| 67 | void setBaseUnitExponent(const std::string& baseUnit, int exponent); | ||
| 68 | |||
| 69 | /** Units in string form. LaTeX mathematical formatting--_ for subscripting, ^ for | ||
| 70 | * superscripting. Curly braces {} are used for grouping. \sa Unit::standardUnitsString */ | ||
| 71 | std::string standardUnitsString(bool withScale = true) const; | ||
| 72 | |||
| 73 | /** Pretty string for this quantity's units. For instance N = kg*m/s^2. | ||
| 74 | * \sa Unit::prettyUnitsString */ | ||
| 75 | std::string prettyUnitsString(bool withScale = true) const; | ||
| 76 | |||
| 77 | /** Sets the prettystring for this Quantity's m_units. openstudio::Unit attempts to keep up | ||
| 78 | * with prettyStrings, but makes no promises. Precondition: isCompoundUnit(str). Otherwise | ||
| 79 | * throws. */ | ||
| 80 | void setPrettyUnitsString(const std::string& str); | ||
| 81 | |||
| 82 | /** Scale getter. */ | ||
| 83 | Scale scale() const; | ||
| 84 | |||
| 85 | /// Sets the scale to the one registered in ScaleFactory for 10^scaleExponent. | ||
| 86 | bool setScale(int scaleExponent); | ||
| 87 | |||
| 88 | /// Sets the scale to the one registered in ScaleFactory under scaleAbbreviation. | ||
| 89 | bool setScale(const std::string& scaleAbbreviation); | ||
| 90 | |||
| 91 | //@} | ||
| 92 | /** @name Temperature Methods | ||
| 93 | * | ||
| 94 | * Quantities that have units of temperature need several extra functions to distinguish | ||
| 95 | * between absolute and relative measures. */ | ||
| 96 | //@{ | ||
| 97 | |||
| 98 | /** Throws if !units().optionalCast<TemperatureUnit>(). */ | ||
| 99 | bool isAbsolute() const; | ||
| 100 | |||
| 101 | /** Throws if !units().optionalCast<TemperatureUnit>(). */ | ||
| 102 | bool isRelative() const; | ||
| 103 | |||
| 104 | /** Set temperature as absolute. Throws if !units().optionalCast<TemperatureUnit>(). */ | ||
| 105 | void setAsAbsolute(); | ||
| 106 | |||
| 107 | /** Set temperature as relative. Throws if !units().optionalCast<TemperatureUnit>(). */ | ||
| 108 | void setAsRelative(); | ||
| 109 | |||
| 110 | //@} | ||
| 111 | /** @name IP Methods */ | ||
| 112 | //@{ | ||
| 113 | |||
| 114 | /** Conversions for pounds mass to pounds force. These functions will throw if | ||
| 115 | * !units().optionalCast<IPUnit>(). */ | ||
| 116 | void lbmToLbf(); | ||
| 117 | |||
| 118 | /** Conversions for pounds mass to pounds force. These functions will throw if | ||
| 119 | * !units().optionalCast<IPUnit>(). */ | ||
| 120 | void lbfToLbm(); | ||
| 121 | |||
| 122 | //@} | ||
| 123 | /** @name Mathematical Operators */ | ||
| 124 | //@{ | ||
| 125 | |||
| 126 | /** Add and assign operator. Throws if units are not equal. */ | ||
| 127 | Quantity& operator+=(const Quantity& rQuantity); | ||
| 128 | |||
| 129 | /** Subtract and assign operator. Throws if units are not equal. */ | ||
| 130 | Quantity& operator-=(const Quantity& rQuantity); | ||
| 131 | |||
| 132 | /** Multiply and assign operator. Throws if Quantity is a particular system and rQuantity is not | ||
| 133 | * the same system. */ | ||
| 134 | Quantity& operator*=(const Quantity& rQuantity); | ||
| 135 | |||
| 136 | /** Divide and assign operator. Throws if Quantity is a particular system and rQuantity is not | ||
| 137 | * the same system. */ | ||
| 138 | Quantity& operator/=(const Quantity& rQuantity); | ||
| 139 | |||
| 140 | /** Multiply by double. No impact to units or scale. Does not throw. */ | ||
| 141 | Quantity& operator*=(double d); | ||
| 142 | |||
| 143 | /** Divide by double. No impact to units or scale. Does not throw. */ | ||
| 144 | Quantity& operator/=(double d); | ||
| 145 | |||
| 146 | /** Raise Quantity to a rational power. Throws openstudio::Exception if expDenom is not a common divisor | ||
| 147 | * for all baseUnit and scale exponents. Returned scale exponent may differ from expectation | ||
| 148 | * based on initialization of openstudio::ScaleFactory, see Scale operators declared in | ||
| 149 | * ScaleFactory.hpp. */ | ||
| 150 | Quantity& pow(int expNum, int expDenom = 1); | ||
| 151 | |||
| 152 | //@} | ||
| 153 | |||
| 154 | friend UTILITIES_API std::ostream& operator<<(std::ostream& os, const Quantity& q); | ||
| 155 | |||
| 156 | protected: | ||
| 157 | double m_value; | ||
| 158 | Unit m_units; | ||
| 159 | |||
| 160 | private: | ||
| 161 | − | REGISTER_LOGGER("openstudio.units.Quantity"); | |
| 162 | }; | ||
| 163 | |||
| 164 | /** \relates Quantity */ | ||
| 165 | UTILITIES_API std::ostream& operator<<(std::ostream& os, const Quantity& q); | ||
| 166 | |||
| 167 | /** \relates Quantity */ | ||
| 168 | using OptionalQuantity = boost::optional<Quantity>; | ||
| 169 | |||
| 170 | /** \relates Quantity */ | ||
| 171 | using QuantityVector = std::vector<Quantity>; | ||
| 172 | |||
| 173 | /** Negate a Quantity. */ | ||
| 174 | UTILITIES_API Quantity operator-(const Quantity& rQuantity); | ||
| 175 | /** Add Quantities. Used with base class Quantities and mixed Quantity types. */ | ||
| 176 | UTILITIES_API Quantity operator+(const Quantity& lQuantity, const Quantity& rQuantity); | ||
| 177 | /** Subtract Quantities. Used with base class Quantities and mixed Quantity types. */ | ||
| 178 | UTILITIES_API Quantity operator-(const Quantity& lQuantity, const Quantity& rQuantity); | ||
| 179 | /** Multiply Quantities. Used with base class Quantities and mixed Quantity types. */ | ||
| 180 | UTILITIES_API Quantity operator*(const Quantity& lQuantity, const Quantity& rQuantity); | ||
| 181 | /** Divide Quantities. Used with base class Quantities and mixed Quantity types. */ | ||
| 182 | UTILITIES_API Quantity operator/(const Quantity& lQuantity, const Quantity& rQuantity); | ||
| 183 | |||
| 184 | /** Raise rQuantity to a rational power. Throws openstudio::Exception if the scale exponent or | ||
| 185 | * any base unit exponent is not cleanly divisible by expDenom. */ | ||
| 186 | UTILITIES_API Quantity pow(const Quantity& rQuantity, int expNum, int expDenom = 1); | ||
| 187 | |||
| 188 | /** Multiply a Quantity by a double. */ | ||
| 189 | UTILITIES_API Quantity operator*(const Quantity& lQuantity, double d); | ||
| 190 | /** Multiply a Quantity by a double. */ | ||
| 191 | UTILITIES_API Quantity operator*(double d, const Quantity& rQuantity); | ||
| 192 | /** Divide a Quantity by a double. */ | ||
| 193 | UTILITIES_API Quantity operator/(const Quantity& lQuantity, double d); | ||
| 194 | /** Divide a double by a Quantity. */ | ||
| 195 | UTILITIES_API Quantity operator/(double d, const Quantity& rQuantity); | ||
| 196 | |||
| 197 | UTILITIES_API bool operator==(const Quantity& lQuantity, const Quantity& rQuantity); | ||
| 198 | |||
| 199 | UTILITIES_API bool operator!=(const Quantity& lQuantity, const Quantity& rQuantity); | ||
| 200 | |||
| 201 | } // namespace openstudio | ||
| 202 | |||
| 203 | #endif // UTILITIES_UNITS_QUANTITY_HPP | ||
| 204 |