src/utilities/units/OSQuantityVector.cpp
| 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 | #include "OSQuantityVector.hpp" | ||
| 7 | #include "Quantity.hpp" | ||
| 8 | #include "ScaleFactory.hpp" | ||
| 9 | #include "TemperatureUnit.hpp" | ||
| 10 | #include "TemperatureUnit_Impl.hpp" | ||
| 11 | #include "IPUnit.hpp" | ||
| 12 | #include "IPUnit_Impl.hpp" | ||
| 13 | |||
| 14 | #include "../math/FloatCompare.hpp" | ||
| 15 | #include "../data/Vector.hpp" | ||
| 16 | |||
| 17 | #include "../core/Containers.hpp" | ||
| 18 | #include "../core/Assert.hpp" | ||
| 19 | |||
| 20 | namespace openstudio { | ||
| 21 | |||
| 22 | 719 | OSQuantityVector::OSQuantityVector(const Unit& units) : m_units(units.clone()) {} | |
| 23 | |||
| 24 |
1/1✓ Branch 2 taken 8 times.
|
24 | OSQuantityVector::OSQuantityVector(const Unit& units, unsigned n, double value) : m_units(units.clone()), m_values(n, value) {} |
| 25 | |||
| 26 |
1/1✓ Branch 2 taken 178 times.
|
178 | OSQuantityVector::OSQuantityVector(const Unit& units, const std::vector<double>& values) : m_units(units.clone()), m_values(values) {} |
| 27 | |||
| 28 | ✗ | OSQuantityVector::OSQuantityVector(const std::vector<Quantity>& values) : m_units(), m_values(values.size(), 0.0) { | |
| 29 | ✗ | if (!values.empty()) { | |
| 30 | ✗ | m_units = values[0].units(); | |
| 31 | } | ||
| 32 | ✗ | const Scale& myScale = scale(); | |
| 33 | ✗ | for (unsigned i = 0, n = size(); i < n; ++i) { | |
| 34 | ✗ | if (!(values[i].units() == m_units)) { | |
| 35 | ✗ | LOG_AND_THROW("Quantity " << values[i] << " has units inconsistent with the first " << "Quantity in vector values, " << values[0] << "."); | |
| 36 | } | ||
| 37 | ✗ | if (values[i].scale() != myScale) { | |
| 38 | ✗ | Quantity working = values[i]; | |
| 39 | ✗ | working.setScale(myScale.exponent); | |
| 40 | ✗ | m_values[i] = working.value(); | |
| 41 | ✗ | } else { | |
| 42 | ✗ | m_values[i] = values[i].value(); | |
| 43 | } | ||
| 44 | } | ||
| 45 | ✗ | } | |
| 46 | |||
| 47 |
1/1✓ Branch 2 taken 56 times.
|
56 | OSQuantityVector::OSQuantityVector(const OSQuantityVector& other) : m_units(other.units()), m_values(other.values()) {} |
| 48 | |||
| 49 | 105 | OSQuantityVector& OSQuantityVector::operator=(const OSQuantityVector& other) { | |
| 50 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 105 times.
|
105 | if (this == &other) { |
| 51 | ✗ | return *this; | |
| 52 | } | ||
| 53 |
1/1✓ Branch 1 taken 105 times.
|
105 | m_units = other.units(); |
| 54 |
1/1✓ Branch 1 taken 105 times.
|
105 | m_values = other.values(); |
| 55 | 105 | return *this; | |
| 56 | } | ||
| 57 | |||
| 58 | 310 | Unit OSQuantityVector::units() const { | |
| 59 | 310 | return m_units.clone(); | |
| 60 | } | ||
| 61 | |||
| 62 | 26 | UnitSystem OSQuantityVector::system() const { | |
| 63 | 26 | return m_units.system(); | |
| 64 | } | ||
| 65 | |||
| 66 | 53 | Scale OSQuantityVector::scale() const { | |
| 67 | 53 | return m_units.scale(); | |
| 68 | } | ||
| 69 | |||
| 70 | 2 | std::vector<Quantity> OSQuantityVector::quantities() const { | |
| 71 | 2 | QuantityVector result; | |
| 72 |
3/3✓ Branch 1 taken 2 times.
✓ Branch 7 taken 55 times.
✓ Branch 8 taken 2 times.
|
57 | for (double value : values()) { |
| 73 |
3/3✓ Branch 1 taken 55 times.
✓ Branch 4 taken 55 times.
✓ Branch 7 taken 55 times.
|
55 | result.push_back(Quantity(value, units())); |
| 74 | 2 | } | |
| 75 | 2 | return result; | |
| 76 | ✗ | } | |
| 77 | |||
| 78 | 182 | std::vector<double> OSQuantityVector::values() const { | |
| 79 | 182 | return m_values; | |
| 80 | } | ||
| 81 | |||
| 82 | 40 | Quantity OSQuantityVector::getQuantity(unsigned i) const { | |
| 83 |
2/2✓ Branch 1 taken 40 times.
✓ Branch 5 taken 40 times.
|
40 | return Quantity(m_values[i], units()); |
| 84 | } | ||
| 85 | |||
| 86 | 2 | bool OSQuantityVector::empty() const { | |
| 87 | 2 | return m_values.empty(); | |
| 88 | } | ||
| 89 | |||
| 90 | 81 | unsigned OSQuantityVector::size() const { | |
| 91 | 81 | return m_values.size(); | |
| 92 | } | ||
| 93 | |||
| 94 | ✗ | bool OSQuantityVector::setScale(int scaleExponent) { | |
| 95 | ✗ | ScaleConstant candidate = ScaleFactory::instance().createScale(scaleExponent); | |
| 96 | ✗ | double candidateValue = candidate().value; | |
| 97 | ✗ | if (candidateValue == 0.0) { | |
| 98 | ✗ | return false; | |
| 99 | } | ||
| 100 | ✗ | (*this) *= (scale().value / candidateValue); | |
| 101 | ✗ | bool ok = m_units.setScale(scaleExponent); | |
| 102 | ✗ | OS_ASSERT(ok); | |
| 103 | ✗ | return true; | |
| 104 | ✗ | } | |
| 105 | |||
| 106 | ✗ | bool OSQuantityVector::setScale(const std::string& scaleAbbreviation) { | |
| 107 | ✗ | ScaleConstant candidate = ScaleFactory::instance().createScale(scaleAbbreviation); | |
| 108 | ✗ | double candidateValue = candidate().value; | |
| 109 | ✗ | if (candidateValue == 0.0) { | |
| 110 | ✗ | return false; | |
| 111 | } | ||
| 112 | ✗ | (*this) *= (scale().value / candidateValue); | |
| 113 | ✗ | bool ok = m_units.setScale(scaleAbbreviation); | |
| 114 | ✗ | OS_ASSERT(ok); | |
| 115 | ✗ | return true; | |
| 116 | ✗ | } | |
| 117 | |||
| 118 | 3 | void OSQuantityVector::push_back(Quantity q) { | |
| 119 |
5/5✓ Branch 1 taken 3 times.
✓ Branch 4 taken 3 times.
✓ Branch 7 taken 3 times.
✓ Branch 11 taken 1 time.
✓ Branch 12 taken 2 times.
|
3 | if (!(q.units() == units())) { |
| 120 |
23/23✓ Branch 1 taken 1 time.
✓ Branch 4 taken 1 time.
✓ Branch 7 taken 1 time.
✓ Branch 10 taken 1 time.
✓ Branch 13 taken 1 time.
✓ Branch 16 taken 1 time.
✓ Branch 19 taken 1 time.
✓ Branch 23 taken 1 time.
✓ Branch 26 taken 1 time.
✓ Branch 29 taken 1 time.
✓ Branch 35 taken 1 time.
✓ Branch 38 taken 1 time.
✓ Branch 41 taken 1 time.
✓ Branch 44 taken 1 time.
✓ Branch 47 taken 1 time.
✓ Branch 50 taken 1 time.
✓ Branch 53 taken 1 time.
✓ Branch 56 taken 1 time.
✓ Branch 59 taken 1 time.
✓ Branch 62 taken 1 time.
✓ Branch 65 taken 1 time.
✓ Branch 70 taken 1 time.
✓ Branch 73 taken 1 time.
|
1 | LOG_AND_THROW("Quantity " << q |
| 121 | << " is incompatible with this OSQuantityVector, which has " | ||
| 122 | "units " | ||
| 123 | << units() << "."); | ||
| 124 |
4/5✓ Branch 1 taken 2 times.
✓ Branch 4 taken 2 times.
✓ Branch 7 taken 2 times.
✗ Branch 11 not taken.
✓ Branch 12 taken 2 times.
|
2 | } else if (q.scale() != scale()) { |
| 125 | ✗ | q.setScale(scale().exponent); | |
| 126 | } | ||
| 127 |
2/2✓ Branch 1 taken 2 times.
✓ Branch 4 taken 2 times.
|
2 | m_values.push_back(q.value()); |
| 128 | 2 | } | |
| 129 | |||
| 130 | ✗ | void OSQuantityVector::pop_back() { | |
| 131 | ✗ | m_values.pop_back(); | |
| 132 | ✗ | } | |
| 133 | |||
| 134 | 1 | void OSQuantityVector::resize(unsigned n, double value) { | |
| 135 | 1 | m_values.resize(n, value); | |
| 136 | 1 | } | |
| 137 | |||
| 138 | ✗ | void OSQuantityVector::clear() { | |
| 139 | ✗ | m_values.clear(); | |
| 140 | ✗ | } | |
| 141 | |||
| 142 | 33 | bool OSQuantityVector::isTemperature() const { | |
| 143 |
1/1✓ Branch 1 taken 33 times.
|
33 | return m_units.optionalCast<TemperatureUnit>().has_value(); |
| 144 | } | ||
| 145 | |||
| 146 | 34 | bool OSQuantityVector::isAbsolute() const { | |
| 147 |
2/3✓ Branch 1 taken 34 times.
✓ Branch 3 taken 34 times.
✗ Branch 4 not taken.
|
68 | if (OptionalTemperatureUnit tu = m_units.optionalCast<TemperatureUnit>()) { |
| 148 |
2/2✓ Branch 1 taken 34 times.
✓ Branch 4 taken 34 times.
|
34 | return tu->isAbsolute(); |
| 149 | 34 | } | |
| 150 | ✗ | LOG_AND_THROW("Could not evaluate OSQuantityVector::isAbsolute because the units are in system " << system().valueName() | |
| 151 | << ", not Celsius or Fahrenheit."); | ||
| 152 | return false; | ||
| 153 | } | ||
| 154 | |||
| 155 | 5 | bool OSQuantityVector::isRelative() const { | |
| 156 | 5 | return !isAbsolute(); | |
| 157 | } | ||
| 158 | |||
| 159 | 4 | void OSQuantityVector::setAsAbsolute() { | |
| 160 |
2/3✓ Branch 1 taken 4 times.
✓ Branch 3 taken 4 times.
✗ Branch 4 not taken.
|
8 | if (OptionalTemperatureUnit tu = m_units.optionalCast<TemperatureUnit>()) { |
| 161 |
2/2✓ Branch 1 taken 4 times.
✓ Branch 4 taken 4 times.
|
4 | tu->setAsAbsolute(); |
| 162 | } else { | ||
| 163 | ✗ | LOG_AND_THROW("Could not OSQuantityVector::setAsAbsolute because the units are in system " << system().valueName() | |
| 164 | << ", not Celsius or Fahrenheit."); | ||
| 165 | 4 | } | |
| 166 | 4 | } | |
| 167 | |||
| 168 | 4 | void OSQuantityVector::setAsRelative() { | |
| 169 |
2/3✓ Branch 1 taken 4 times.
✓ Branch 3 taken 4 times.
✗ Branch 4 not taken.
|
8 | if (OptionalTemperatureUnit tu = m_units.optionalCast<TemperatureUnit>()) { |
| 170 |
2/2✓ Branch 1 taken 4 times.
✓ Branch 4 taken 4 times.
|
4 | tu->setAsRelative(); |
| 171 | } else { | ||
| 172 | ✗ | LOG_AND_THROW("Could not OSQuantityVector::setAsRelative because the units are in system " << system().valueName() | |
| 173 | << ", not Celsius or Fahrenheit."); | ||
| 174 | 4 | } | |
| 175 | 4 | } | |
| 176 | |||
| 177 | ✗ | void OSQuantityVector::lbmToLbf() { | |
| 178 | ✗ | if (OptionalIPUnit iu = m_units.optionalCast<IPUnit>()) { | |
| 179 | ✗ | int x = m_units.baseUnitExponent("lb_m"); | |
| 180 | ✗ | if (x != 0) { | |
| 181 | ✗ | (*this) /= std::pow(IPUnit::gc(), x); | |
| 182 | ✗ | iu->lbmToLbf(); | |
| 183 | } | ||
| 184 | ✗ | OS_ASSERT(m_units.baseUnitExponent("lb_m") == 0); | |
| 185 | ✗ | } | |
| 186 | ✗ | LOG_AND_THROW("Cannot convert non-IP units " << m_units << " in system " << system().valueName() << " from lb_m to lb_f."); | |
| 187 | } | ||
| 188 | |||
| 189 | ✗ | void OSQuantityVector::lbfToLbm() { | |
| 190 | ✗ | if (OptionalIPUnit iu = m_units.optionalCast<IPUnit>()) { | |
| 191 | ✗ | int x = m_units.baseUnitExponent("lb_f"); | |
| 192 | ✗ | if (x != 0) { | |
| 193 | ✗ | (*this) *= std::pow(IPUnit::gc(), x); | |
| 194 | ✗ | iu->lbmToLbf(); | |
| 195 | } | ||
| 196 | ✗ | OS_ASSERT(m_units.baseUnitExponent("lb_f") == 0); | |
| 197 | ✗ | } | |
| 198 | ✗ | LOG_AND_THROW("Cannot convert non-IP units " << m_units << " in system " << system().valueName() << " from lb_f to lb_m."); | |
| 199 | } | ||
| 200 | |||
| 201 | 5 | OSQuantityVector& OSQuantityVector::operator+=(OSQuantityVector rVector) { | |
| 202 | |||
| 203 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 5 times.
|
5 | if (this == &rVector) { |
| 204 | ✗ | (*this) *= 2.0; | |
| 205 | ✗ | return *this; | |
| 206 | } | ||
| 207 | |||
| 208 |
7/8✓ Branch 1 taken 5 times.
✓ Branch 3 taken 3 times.
✓ Branch 4 taken 2 times.
✓ Branch 6 taken 3 times.
✓ Branch 8 taken 3 times.
✗ Branch 9 not taken.
✓ Branch 10 taken 3 times.
✓ Branch 11 taken 2 times.
|
5 | if (isTemperature() && rVector.isTemperature()) { |
| 209 |
8/8✓ Branch 1 taken 3 times.
✓ Branch 3 taken 2 times.
✓ Branch 4 taken 1 time.
✓ Branch 6 taken 2 times.
✓ Branch 8 taken 1 time.
✓ Branch 9 taken 1 time.
✓ Branch 10 taken 1 time.
✓ Branch 11 taken 2 times.
|
3 | if (!isAbsolute() && rVector.isAbsolute()) { |
| 210 |
1/1✓ Branch 1 taken 1 time.
|
1 | setAsAbsolute(); |
| 211 |
7/8✓ Branch 1 taken 2 times.
✓ Branch 3 taken 1 time.
✓ Branch 4 taken 1 time.
✓ Branch 6 taken 1 time.
✓ Branch 8 taken 1 time.
✗ Branch 9 not taken.
✓ Branch 10 taken 1 time.
✓ Branch 11 taken 1 time.
|
2 | } else if (isAbsolute() && !rVector.isAbsolute()) { |
| 212 |
1/1✓ Branch 1 taken 1 time.
|
1 | rVector.setAsAbsolute(); |
| 213 | } | ||
| 214 | } | ||
| 215 | |||
| 216 |
4/5✓ Branch 1 taken 5 times.
✓ Branch 4 taken 5 times.
✓ Branch 7 taken 5 times.
✗ Branch 11 not taken.
✓ Branch 12 taken 5 times.
|
5 | if (units() != rVector.units()) { |
| 217 | ✗ | LOG_AND_THROW("Cannot add OSQuantityVectors with different units (" << units() << " and " << rVector.units() << ")."); | |
| 218 | } | ||
| 219 | |||
| 220 |
1/1✓ Branch 1 taken 5 times.
|
5 | unsigned n = size(); |
| 221 |
2/3✓ Branch 1 taken 5 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 5 times.
|
5 | if (rVector.size() != n) { |
| 222 | ✗ | LOG_AND_THROW("Cannot add vectors of different sizes."); | |
| 223 | } | ||
| 224 | |||
| 225 |
4/5✓ Branch 1 taken 5 times.
✓ Branch 4 taken 5 times.
✓ Branch 7 taken 5 times.
✗ Branch 11 not taken.
✓ Branch 12 taken 5 times.
|
5 | if (scale() != rVector.scale()) { |
| 226 | ✗ | rVector.setScale(scale().exponent); | |
| 227 | } | ||
| 228 | |||
| 229 |
1/1✓ Branch 1 taken 5 times.
|
5 | DoubleVector rValues = rVector.values(); |
| 230 |
2/2✓ Branch 0 taken 10 times.
✓ Branch 1 taken 5 times.
|
15 | for (unsigned i = 0; i < n; ++i) { |
| 231 | 10 | m_values[i] += rValues[i]; | |
| 232 | } | ||
| 233 | |||
| 234 | 5 | return *this; | |
| 235 | 5 | } | |
| 236 | |||
| 237 | 16 | OSQuantityVector& OSQuantityVector::operator+=(Quantity rQuantity) { | |
| 238 | |||
| 239 |
5/6✓ Branch 1 taken 2 times.
✓ Branch 2 taken 14 times.
✓ Branch 4 taken 2 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 2 times.
✓ Branch 7 taken 14 times.
|
16 | if (isTemperature() && rQuantity.isTemperature()) { |
| 240 |
4/6✓ Branch 1 taken 1 time.
✓ Branch 2 taken 1 time.
✗ Branch 4 not taken.
✓ Branch 5 taken 1 time.
✗ Branch 6 not taken.
✓ Branch 7 taken 2 times.
|
2 | if (!isAbsolute() && rQuantity.isAbsolute()) { |
| 241 | ✗ | setAsAbsolute(); | |
| 242 |
4/6✓ Branch 1 taken 1 time.
✓ Branch 2 taken 1 time.
✗ Branch 4 not taken.
✓ Branch 5 taken 1 time.
✗ Branch 6 not taken.
✓ Branch 7 taken 2 times.
|
2 | } else if (isAbsolute() && !rQuantity.isAbsolute()) { |
| 243 | ✗ | rQuantity.setAsAbsolute(); | |
| 244 | } | ||
| 245 | } | ||
| 246 | |||
| 247 |
4/5✓ Branch 1 taken 16 times.
✓ Branch 4 taken 16 times.
✓ Branch 7 taken 16 times.
✗ Branch 11 not taken.
✓ Branch 12 taken 16 times.
|
16 | if (units() != rQuantity.units()) { |
| 248 | ✗ | LOG_AND_THROW("Cannot add OSQuantityVector and Quantity with different units (" << units() << " and " << rQuantity.units() << ")."); | |
| 249 | } | ||
| 250 | |||
| 251 |
4/5✓ Branch 1 taken 16 times.
✓ Branch 4 taken 16 times.
✓ Branch 7 taken 16 times.
✗ Branch 11 not taken.
✓ Branch 12 taken 16 times.
|
16 | if (scale() != rQuantity.scale()) { |
| 252 | ✗ | rQuantity.setScale(scale().exponent); | |
| 253 | } | ||
| 254 | |||
| 255 | 16 | double value = rQuantity.value(); | |
| 256 |
2/2✓ Branch 1 taken 96370 times.
✓ Branch 2 taken 16 times.
|
96386 | for (unsigned i = 0, n = size(); i < n; ++i) { |
| 257 | 96370 | m_values[i] += value; | |
| 258 | } | ||
| 259 | |||
| 260 | 16 | return *this; | |
| 261 | } | ||
| 262 | |||
| 263 | 5 | OSQuantityVector& OSQuantityVector::operator-=(OSQuantityVector rVector) { | |
| 264 | |||
| 265 |
1/1✓ Branch 1 taken 5 times.
|
5 | unsigned n = size(); |
| 266 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 5 times.
|
5 | if (this == &rVector) { |
| 267 | ✗ | clear(); | |
| 268 | ✗ | resize(n, 0.0); | |
| 269 | ✗ | return *this; | |
| 270 | } | ||
| 271 | |||
| 272 |
7/8✓ Branch 1 taken 5 times.
✓ Branch 3 taken 3 times.
✓ Branch 4 taken 2 times.
✓ Branch 6 taken 3 times.
✓ Branch 8 taken 3 times.
✗ Branch 9 not taken.
✓ Branch 10 taken 3 times.
✓ Branch 11 taken 2 times.
|
5 | if (isTemperature() && rVector.isTemperature()) { |
| 273 |
8/8✓ Branch 1 taken 3 times.
✓ Branch 3 taken 2 times.
✓ Branch 4 taken 1 time.
✓ Branch 6 taken 2 times.
✓ Branch 8 taken 1 time.
✓ Branch 9 taken 1 time.
✓ Branch 10 taken 1 time.
✓ Branch 11 taken 2 times.
|
3 | if (isAbsolute() && rVector.isAbsolute()) { |
| 274 | // units must be the same, check that exponent on this is 1 | ||
| 275 |
1/1✓ Branch 1 taken 1 time.
|
1 | std::vector<std::string> bus = m_units.baseUnits(); |
| 276 | assert(bus.size() == 1); | ||
| 277 |
2/3✓ Branch 2 taken 1 time.
✓ Branch 4 taken 1 time.
✗ Branch 5 not taken.
|
1 | if (m_units.baseUnitExponent(bus[0]) == 1) { |
| 278 |
1/1✓ Branch 1 taken 1 time.
|
1 | setAsRelative(); |
| 279 |
1/1✓ Branch 1 taken 1 time.
|
1 | rVector.setAsRelative(); |
| 280 | } | ||
| 281 |
7/8✓ Branch 2 taken 2 times.
✓ Branch 4 taken 1 time.
✓ Branch 5 taken 1 time.
✓ Branch 7 taken 1 time.
✓ Branch 9 taken 1 time.
✗ Branch 10 not taken.
✓ Branch 11 taken 1 time.
✓ Branch 12 taken 1 time.
|
3 | } else if (!isAbsolute() && rVector.isAbsolute()) { |
| 282 |
1/1✓ Branch 1 taken 1 time.
|
1 | setAsAbsolute(); |
| 283 |
5/8✓ Branch 1 taken 1 time.
✓ Branch 3 taken 1 time.
✗ Branch 4 not taken.
✓ Branch 6 taken 1 time.
✓ Branch 8 taken 1 time.
✗ Branch 9 not taken.
✓ Branch 10 taken 1 time.
✗ Branch 11 not taken.
|
1 | } else if (isAbsolute() && !rVector.isAbsolute()) { |
| 284 |
1/1✓ Branch 1 taken 1 time.
|
1 | rVector.setAsAbsolute(); |
| 285 | } | ||
| 286 | } | ||
| 287 | |||
| 288 |
4/5✓ Branch 1 taken 5 times.
✓ Branch 4 taken 5 times.
✓ Branch 7 taken 5 times.
✗ Branch 11 not taken.
✓ Branch 12 taken 5 times.
|
5 | if (units() != rVector.units()) { |
| 289 | ✗ | LOG_AND_THROW("Cannot subtract OSQuantityVectors with different units (" << units() << " and " << rVector.units() << ")."); | |
| 290 | } | ||
| 291 | |||
| 292 |
2/3✓ Branch 1 taken 5 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 5 times.
|
5 | if (rVector.size() != n) { |
| 293 | ✗ | LOG_AND_THROW("Cannot subtract vectors of different sizes."); | |
| 294 | } | ||
| 295 | |||
| 296 |
4/5✓ Branch 1 taken 5 times.
✓ Branch 4 taken 5 times.
✓ Branch 7 taken 5 times.
✗ Branch 11 not taken.
✓ Branch 12 taken 5 times.
|
5 | if (scale() != rVector.scale()) { |
| 297 | ✗ | rVector.setScale(scale().exponent); | |
| 298 | } | ||
| 299 | |||
| 300 |
1/1✓ Branch 1 taken 5 times.
|
5 | DoubleVector rValues = rVector.values(); |
| 301 |
2/2✓ Branch 0 taken 10 times.
✓ Branch 1 taken 5 times.
|
15 | for (unsigned i = 0; i < n; ++i) { |
| 302 | 10 | m_values[i] -= rValues[i]; | |
| 303 | } | ||
| 304 | |||
| 305 | 5 | return *this; | |
| 306 | 5 | } | |
| 307 | |||
| 308 | 1 | OSQuantityVector& OSQuantityVector::operator-=(Quantity rQuantity) { | |
| 309 | |||
| 310 |
2/6✗ Branch 1 not taken.
✓ Branch 2 taken 1 time.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
✗ Branch 6 not taken.
✓ Branch 7 taken 1 time.
|
1 | if (isTemperature() && rQuantity.isTemperature()) { |
| 311 | ✗ | if (isAbsolute() && rQuantity.isAbsolute()) { | |
| 312 | // units must be the same, check that exponent on this is 1 | ||
| 313 | ✗ | std::vector<std::string> bus = m_units.baseUnits(); | |
| 314 | assert(bus.size() == 1); | ||
| 315 | ✗ | if (m_units.baseUnitExponent(bus[0]) == 1) { | |
| 316 | ✗ | setAsRelative(); | |
| 317 | ✗ | rQuantity.setAsRelative(); | |
| 318 | } | ||
| 319 | ✗ | } else if (!isAbsolute() && rQuantity.isAbsolute()) { | |
| 320 | ✗ | setAsAbsolute(); | |
| 321 | ✗ | } else if (isAbsolute() && !rQuantity.isAbsolute()) { | |
| 322 | ✗ | rQuantity.setAsAbsolute(); | |
| 323 | } | ||
| 324 | } | ||
| 325 | |||
| 326 |
4/5✓ Branch 1 taken 1 time.
✓ Branch 4 taken 1 time.
✓ Branch 7 taken 1 time.
✗ Branch 11 not taken.
✓ Branch 12 taken 1 time.
|
1 | if (units() != rQuantity.units()) { |
| 327 | ✗ | LOG_AND_THROW("Cannot subtract OSQuantityVector and Quantity with different units (" << units() << " and " << rQuantity.units() << ")."); | |
| 328 | } | ||
| 329 | |||
| 330 |
4/5✓ Branch 1 taken 1 time.
✓ Branch 4 taken 1 time.
✓ Branch 7 taken 1 time.
✗ Branch 11 not taken.
✓ Branch 12 taken 1 time.
|
1 | if (scale() != rQuantity.scale()) { |
| 331 | ✗ | rQuantity.setScale(scale().exponent); | |
| 332 | } | ||
| 333 | |||
| 334 | 1 | double value = rQuantity.value(); | |
| 335 |
2/2✓ Branch 1 taken 2 times.
✓ Branch 2 taken 1 time.
|
3 | for (unsigned i = 0, n = size(); i < n; ++i) { |
| 336 | 2 | m_values[i] -= value; | |
| 337 | } | ||
| 338 | |||
| 339 | 1 | return *this; | |
| 340 | } | ||
| 341 | |||
| 342 | 11 | OSQuantityVector& OSQuantityVector::operator*=(const Quantity& rQuantity) { | |
| 343 | // unit *= will throw if m_units is not base-class and rQuantity.m_units is different system | ||
| 344 |
9/10✓ Branch 1 taken 11 times.
✓ Branch 4 taken 11 times.
✓ Branch 7 taken 10 times.
✓ Branch 8 taken 1 time.
✓ Branch 10 taken 10 times.
✓ Branch 13 taken 10 times.
✓ Branch 16 taken 10 times.
✗ Branch 17 not taken.
✓ Branch 18 taken 10 times.
✓ Branch 19 taken 1 time.
|
11 | if ((system() != rQuantity.system()) && (system() != UnitSystem::Mixed)) { |
| 345 |
1/1✓ Branch 1 taken 10 times.
|
10 | m_units = m_units.cloneToMixed(); |
| 346 | } | ||
| 347 | // ETH@20100517 ugly workaround for temperature units | ||
| 348 |
1/1✓ Branch 1 taken 11 times.
|
11 | Unit rUnits = rQuantity.units(); |
| 349 |
1/1✓ Branch 1 taken 11 times.
|
11 | OptionalTemperatureUnit tu1 = m_units.optionalCast<TemperatureUnit>(); |
| 350 |
1/1✓ Branch 1 taken 11 times.
|
11 | OptionalTemperatureUnit tu2 = rUnits.optionalCast<TemperatureUnit>(); |
| 351 |
2/6✗ Branch 0 not taken.
✓ Branch 1 taken 11 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✓ Branch 5 taken 11 times.
|
11 | if (tu1 && tu2) { |
| 352 | ✗ | *tu1 *= *tu2; | |
| 353 | } else { | ||
| 354 |
1/1✓ Branch 1 taken 11 times.
|
11 | m_units *= rUnits; |
| 355 | } | ||
| 356 |
3/3✓ Branch 1 taken 11 times.
✓ Branch 4 taken 11 times.
✓ Branch 7 taken 11 times.
|
11 | ScaleOpReturnType resultScale = scale() * rQuantity.scale(); |
| 357 |
2/2✓ Branch 1 taken 11 times.
✓ Branch 4 taken 11 times.
|
11 | (*this) *= (rQuantity.value() * resultScale.second); |
| 358 | |||
| 359 | 11 | return *this; | |
| 360 | 11 | } | |
| 361 | |||
| 362 | 1 | OSQuantityVector& OSQuantityVector::operator/=(const Quantity& rQuantity) { | |
| 363 | // unit *= will throw if m_units is not base-class and rQuantity.m_units is different system | ||
| 364 |
4/10✓ Branch 1 taken 1 time.
✓ Branch 4 taken 1 time.
✗ Branch 7 not taken.
✓ Branch 8 taken 1 time.
✗ Branch 10 not taken.
✗ Branch 13 not taken.
✗ Branch 16 not taken.
✗ Branch 17 not taken.
✗ Branch 18 not taken.
✓ Branch 19 taken 1 time.
|
1 | if ((system() != rQuantity.system()) && (system() != UnitSystem::Mixed)) { |
| 365 | ✗ | m_units = m_units.cloneToMixed(); | |
| 366 | } | ||
| 367 | // ETH@20100517 ugly workaround for temperature units | ||
| 368 |
1/1✓ Branch 1 taken 1 time.
|
1 | Unit rUnits = rQuantity.units(); |
| 369 |
1/1✓ Branch 1 taken 1 time.
|
1 | OptionalTemperatureUnit tu1 = m_units.optionalCast<TemperatureUnit>(); |
| 370 |
1/1✓ Branch 1 taken 1 time.
|
1 | OptionalTemperatureUnit tu2 = rUnits.optionalCast<TemperatureUnit>(); |
| 371 |
2/6✗ Branch 0 not taken.
✓ Branch 1 taken 1 time.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✓ Branch 5 taken 1 time.
|
1 | if (tu1 && tu2) { |
| 372 | ✗ | *tu1 /= *tu2; | |
| 373 | } else { | ||
| 374 |
1/1✓ Branch 1 taken 1 time.
|
1 | m_units /= rUnits; |
| 375 | } | ||
| 376 |
3/3✓ Branch 1 taken 1 time.
✓ Branch 4 taken 1 time.
✓ Branch 7 taken 1 time.
|
1 | ScaleOpReturnType resultScale = scale() / rQuantity.scale(); |
| 377 |
2/2✓ Branch 1 taken 1 time.
✓ Branch 4 taken 1 time.
|
1 | (*this) *= (resultScale.second / rQuantity.value()); |
| 378 | |||
| 379 | 1 | return *this; | |
| 380 | 1 | } | |
| 381 | |||
| 382 | 20 | OSQuantityVector& OSQuantityVector::operator*=(double d) { | |
| 383 |
2/2✓ Branch 1 taken 96378 times.
✓ Branch 2 taken 20 times.
|
96398 | for (unsigned i = 0, n = size(); i < n; ++i) { |
| 384 | 96378 | m_values[i] *= d; | |
| 385 | } | ||
| 386 | 20 | return *this; | |
| 387 | } | ||
| 388 | |||
| 389 | 1 | OSQuantityVector& OSQuantityVector::operator/=(double d) { | |
| 390 |
2/2✓ Branch 1 taken 2 times.
✓ Branch 2 taken 1 time.
|
3 | for (unsigned i = 0, n = size(); i < n; ++i) { |
| 391 | 2 | m_values[i] /= d; | |
| 392 | } | ||
| 393 | 1 | return *this; | |
| 394 | } | ||
| 395 | |||
| 396 | ✗ | OSQuantityVector operator-(const OSQuantityVector& rVector) { | |
| 397 | ✗ | OSQuantityVector result(rVector); | |
| 398 | ✗ | result *= -1.0; | |
| 399 | ✗ | return result; | |
| 400 | ✗ | } | |
| 401 | |||
| 402 | 4 | OSQuantityVector operator+(const OSQuantityVector& lVector, const OSQuantityVector& rVector) { | |
| 403 | 4 | OSQuantityVector result(lVector); | |
| 404 |
2/2✓ Branch 1 taken 4 times.
✓ Branch 4 taken 4 times.
|
4 | result += rVector; |
| 405 | 4 | return result; | |
| 406 | ✗ | } | |
| 407 | |||
| 408 | 16 | OSQuantityVector operator+(const OSQuantityVector& lVector, const Quantity& rQuantity) { | |
| 409 | 16 | OSQuantityVector result(lVector); | |
| 410 |
2/2✓ Branch 1 taken 16 times.
✓ Branch 4 taken 16 times.
|
16 | result += rQuantity; |
| 411 | 16 | return result; | |
| 412 | ✗ | } | |
| 413 | |||
| 414 | ✗ | OSQuantityVector operator+(const Quantity& lQuantity, const OSQuantityVector& rVector) { | |
| 415 | ✗ | OSQuantityVector result(rVector); | |
| 416 | ✗ | result += lQuantity; | |
| 417 | ✗ | return result; | |
| 418 | ✗ | } | |
| 419 | |||
| 420 | 4 | OSQuantityVector operator-(const OSQuantityVector& lVector, const OSQuantityVector& rVector) { | |
| 421 | 4 | OSQuantityVector result(lVector); | |
| 422 |
2/2✓ Branch 1 taken 4 times.
✓ Branch 4 taken 4 times.
|
4 | result -= rVector; |
| 423 | 4 | return result; | |
| 424 | ✗ | } | |
| 425 | |||
| 426 | 1 | OSQuantityVector operator-(const OSQuantityVector& lVector, const Quantity& rQuantity) { | |
| 427 | 1 | OSQuantityVector result(lVector); | |
| 428 |
2/2✓ Branch 1 taken 1 time.
✓ Branch 4 taken 1 time.
|
1 | result -= rQuantity; |
| 429 | 1 | return result; | |
| 430 | ✗ | } | |
| 431 | |||
| 432 | ✗ | OSQuantityVector operator-(const Quantity& lQuantity, const OSQuantityVector& rVector) { | |
| 433 | ✗ | OSQuantityVector result(rVector); | |
| 434 | ✗ | result = -result; | |
| 435 | ✗ | result += lQuantity; | |
| 436 | ✗ | return result; | |
| 437 | ✗ | } | |
| 438 | |||
| 439 | 11 | OSQuantityVector operator*(const OSQuantityVector& lVector, const Quantity& rQuantity) { | |
| 440 | 11 | OSQuantityVector result(lVector); | |
| 441 |
1/1✓ Branch 1 taken 11 times.
|
11 | result *= rQuantity; |
| 442 | 11 | return result; | |
| 443 | ✗ | } | |
| 444 | |||
| 445 | 5 | OSQuantityVector operator*(const OSQuantityVector& lVector, double d) { | |
| 446 | 5 | OSQuantityVector result(lVector); | |
| 447 |
1/1✓ Branch 1 taken 5 times.
|
5 | result *= d; |
| 448 | 5 | return result; | |
| 449 | ✗ | } | |
| 450 | |||
| 451 | ✗ | OSQuantityVector operator*(const Quantity& lQuantity, const OSQuantityVector& rVector) { | |
| 452 | ✗ | OSQuantityVector result(rVector); | |
| 453 | ✗ | result *= lQuantity; | |
| 454 | ✗ | return result; | |
| 455 | ✗ | } | |
| 456 | |||
| 457 | 2 | OSQuantityVector operator*(double d, const OSQuantityVector& rVector) { | |
| 458 | 2 | OSQuantityVector result(rVector); | |
| 459 |
1/1✓ Branch 1 taken 2 times.
|
2 | result *= d; |
| 460 | 2 | return result; | |
| 461 | ✗ | } | |
| 462 | |||
| 463 | 1 | OSQuantityVector operator/(const OSQuantityVector& lVector, const Quantity& rQuantity) { | |
| 464 | 1 | OSQuantityVector result(lVector); | |
| 465 |
1/1✓ Branch 1 taken 1 time.
|
1 | result /= rQuantity; |
| 466 | 1 | return result; | |
| 467 | ✗ | } | |
| 468 | |||
| 469 | 1 | OSQuantityVector operator/(const OSQuantityVector& lVector, double d) { | |
| 470 | 1 | OSQuantityVector result(lVector); | |
| 471 |
1/1✓ Branch 1 taken 1 time.
|
1 | result /= d; |
| 472 | 1 | return result; | |
| 473 | ✗ | } | |
| 474 | |||
| 475 | 1 | Quantity dot(OSQuantityVector lVector, const OSQuantityVector& rVector) { | |
| 476 |
1/1✓ Branch 1 taken 1 time.
|
1 | Unit lUnits(lVector.units()); |
| 477 |
1/1✓ Branch 1 taken 1 time.
|
1 | Unit rUnits(rVector.units()); |
| 478 |
1/1✓ Branch 1 taken 1 time.
|
1 | OptionalTemperatureUnit ltu = lUnits.optionalCast<TemperatureUnit>(); |
| 479 |
1/1✓ Branch 1 taken 1 time.
|
1 | OptionalTemperatureUnit rtu = rUnits.optionalCast<TemperatureUnit>(); |
| 480 |
2/2✓ Branch 1 taken 1 time.
✓ Branch 4 taken 1 time.
|
2 | Unit resultUnits; |
| 481 |
2/6✗ Branch 0 not taken.
✓ Branch 1 taken 1 time.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✓ Branch 5 taken 1 time.
|
1 | if (ltu && rtu) { |
| 482 | ✗ | resultUnits = ltu.get() * rtu.get(); | |
| 483 | } else { | ||
| 484 |
1/1✓ Branch 1 taken 1 time.
|
1 | resultUnits = lUnits * rUnits; |
| 485 | } | ||
| 486 |
3/3✓ Branch 1 taken 1 time.
✓ Branch 4 taken 1 time.
✓ Branch 7 taken 1 time.
|
1 | ScaleOpReturnType resultScale = lVector.scale() * rVector.scale(); |
| 487 |
1/1✓ Branch 1 taken 1 time.
|
1 | lVector *= resultScale.second; |
| 488 |
1/1✓ Branch 1 taken 1 time.
|
1 | DoubleVector lValues(lVector.values()); |
| 489 |
1/1✓ Branch 1 taken 1 time.
|
1 | DoubleVector rValues(rVector.values()); |
| 490 |
3/3✓ Branch 1 taken 1 time.
✓ Branch 4 taken 1 time.
✓ Branch 7 taken 1 time.
|
1 | double resultValue = dot(createVector(lValues), createVector(rValues)); |
| 491 |
1/1✓ Branch 1 taken 1 time.
|
2 | return Quantity(resultValue, resultUnits); |
| 492 | 1 | } | |
| 493 | |||
| 494 | 1 | bool operator==(const OSQuantityVector& lVector, const OSQuantityVector& rVector) { | |
| 495 |
13/28✓ Branch 0 taken 1 time.
✗ Branch 1 not taken.
✓ Branch 3 taken 1 time.
✓ Branch 6 taken 1 time.
✓ Branch 9 taken 1 time.
✓ Branch 11 taken 1 time.
✗ Branch 12 not taken.
✓ Branch 14 taken 1 time.
✓ Branch 17 taken 1 time.
✓ Branch 20 taken 1 time.
✓ Branch 22 taken 1 time.
✗ Branch 23 not taken.
✓ Branch 24 taken 1 time.
✗ Branch 25 not taken.
✓ Branch 27 taken 1 time.
✗ Branch 28 not taken.
✓ Branch 30 taken 1 time.
✗ Branch 31 not taken.
✓ Branch 33 taken 1 time.
✗ Branch 34 not taken.
✗ Branch 36 not taken.
✗ Branch 37 not taken.
✗ Branch 39 not taken.
✗ Branch 40 not taken.
✗ Branch 42 not taken.
✗ Branch 43 not taken.
✗ Branch 45 not taken.
✗ Branch 46 not taken.
|
1 | return (&lVector == &rVector) || ((lVector.units() == rVector.units()) && (lVector.values() == rVector.values())); |
| 496 | } | ||
| 497 | |||
| 498 | ✗ | bool operator!=(const OSQuantityVector& lVector, const OSQuantityVector& rVector) { | |
| 499 | ✗ | return !(lVector == rVector); | |
| 500 | } | ||
| 501 | |||
| 502 | } // namespace openstudio | ||
| 503 |