| 1 |
// SPDX-License-Identifier: LGPL-2.1-or-later |
| 2 |
/**************************************************************************** |
| 3 |
* * |
| 4 |
* This file is part of FreeCAD. * |
| 5 |
* * |
| 6 |
* FreeCAD is free software: you can redistribute it and/or modify it * |
| 7 |
* under the terms of the GNU Lesser General Public License as * |
| 8 |
* published by the Free Software Foundation, either version 2.1 of the * |
| 9 |
* License, or (at your option) any later version. * |
| 10 |
* * |
| 11 |
* FreeCAD is distributed in the hope that it will be useful, but * |
| 12 |
* WITHOUT ANY WARRANTY; without even the implied warranty of * |
| 13 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * |
| 14 |
* Lesser General Public License for more details. * |
| 15 |
* * |
| 16 |
* You should have received a copy of the GNU Lesser General Public * |
| 17 |
* License along with FreeCAD. If not, see * |
| 18 |
* <https://www.gnu.org/licenses/>. * |
| 19 |
* * |
| 20 |
***************************************************************************/ |
| 21 |
|
| 22 |
#include <algorithm> |
| 23 |
#include <array> |
| 24 |
#include <cmath> |
| 25 |
#include <cstring> |
| 26 |
#include <limits> |
| 27 |
#include <ranges> |
| 28 |
#include <vector> |
| 29 |
|
| 30 |
#include <fmt/format.h> |
| 31 |
#include <fmt/ranges.h> |
| 32 |
|
| 33 |
#include "Unit.h" |
| 34 |
|
| 35 |
using namespace Base; |
| 36 |
|
| 37 |
struct UnitSpec |
| 38 |
{ |
| 39 |
std::string_view name; |
| 40 |
UnitExponents exps; |
| 41 |
}; |
| 42 |
|
| 43 |
constexpr auto unitSpecs = std::to_array<UnitSpec>({ |
| 44 |
// clang-format off |
| 45 |
// Length |
| 46 |
// . Mass |
| 47 |
// . . Time |
| 48 |
// . . . ElectricCurrent |
| 49 |
// . . . . ThermodynamicTemperature |
| 50 |
// . . . . . AmountOfSubstance |
| 51 |
// . . . . . . LuminousIntensity |
| 52 |
// . . . . . . . Angle |
| 53 |
{ "1" , { 0, 0, 0, 0, 0, 0, 0, 0 } }, |
| 54 |
{ "Length" , { 1 } }, |
| 55 |
{ "Mass" , { 0, 1 } }, |
| 56 |
{ "TimeSpan" , { 0, 0, 1 } }, |
| 57 |
{ "ElectricCurrent" , { 0, 0, 0, 1 } }, |
| 58 |
{ "Temperature" , { 0, 0, 0, 0, 1 } }, |
| 59 |
{ "AmountOfSubstance" , { 0, 0, 0, 0, 0, 1 } }, |
| 60 |
{ "LuminousIntensity" , { 0, 0, 0, 0, 0, 0, 1 } }, |
| 61 |
{ "Angle" , { 0, 0, 0, 0, 0, 0, 0, 1 } }, |
| 62 |
{ "Acceleration" , { 1, 0, -2 } }, |
| 63 |
{ "AngleOfFriction" , { 0, 0, 0, 0, 0, 0, 0, 1 } }, |
| 64 |
{ "Area" , { 2 } }, |
| 65 |
{ "CurrentDensity" , { -2, 0, 0, 1 } }, |
| 66 |
{ "Density" , { -3, 1 } }, |
| 67 |
{ "DissipationRate" , { 2, 0, -3 } }, |
| 68 |
{ "DynamicViscosity" , { -1, 1, -1 } }, |
| 69 |
{ "ElectricalCapacitance" , { -2, -1, 4, 2 } }, |
| 70 |
{ "ElectricalConductance" , { -2, -1, 3, 2 } }, |
| 71 |
{ "ElectricalConductivity" , { -3, -1, 3, 2 } }, |
| 72 |
{ "ElectricalInductance" , { 2, 1, -2, -2 } }, |
| 73 |
{ "ElectricalResistance" , { 2, 1, -3, -2 } }, |
| 74 |
{ "ElectricCharge" , { 0, 0, 1, 1 } }, |
| 75 |
{ "ElectricPotential" , { 2, 1, -3, -1 } }, |
| 76 |
{ "ElectromagneticPotential" , { 1, 1, -2, -1 } }, |
| 77 |
{ "Force" , { 1, 1, -2 } }, |
| 78 |
{ "Frequency" , { 0, 0, -1 } }, |
| 79 |
{ "HeatFlux" , { 0, 1, -3 } }, |
| 80 |
{ "InverseArea" , { -2 } }, |
| 81 |
{ "InverseLength" , { -1 } }, |
| 82 |
{ "InverseVolume" , { -3 } }, |
| 83 |
{ "KinematicViscosity" , { 2, 0, -1 } }, |
| 84 |
{ "MagneticFieldStrength" , { -1, 0, 0, 1 } }, |
| 85 |
{ "MagneticFlux" , { 2, 1, -2, -1 } }, |
| 86 |
{ "MagneticFluxDensity" , { 0, 1, -2, -1 } }, |
| 87 |
{ "Magnetization" , { -1, 0, 0, 1 } }, |
| 88 |
{ "Moment" , { 2, 1, -2 } }, |
| 89 |
{ "Pressure" , { -1, 1, -2 } }, |
| 90 |
{ "Power" , { 2, 1, -3 } }, |
| 91 |
{ "ShearModulus" , { -1, 1, -2 } }, |
| 92 |
{ "SpecificEnergy" , { 2, 0, -2 } }, |
| 93 |
{ "SpecificHeat" , { 2, 0, -2, 0, -1 } }, |
| 94 |
{ "Stiffness" , { 0, 1, -2 } }, |
| 95 |
{ "StiffnessDensity" , { -2, 1, -2 } }, |
| 96 |
{ "Stress" , { -1, 1, -2 } }, |
| 97 |
{ "SurfaceChargeDensity" , { -2, 0, 1, 1 } }, |
| 98 |
{ "ThermalConductivity" , { 1, 1, -3, 0, -1 } }, |
| 99 |
{ "ThermalExpansionCoefficient" , { 0, 0, 0, 0, -1 } }, |
| 100 |
{ "ThermalTransferCoefficient" , { 0, 1, -3, 0, -1 } }, |
| 101 |
{ "UltimateTensileStrength" , { -1, 1, -2 } }, |
| 102 |
{ "VacuumPermittivity" , { -3, -1, 4, 2 } }, |
| 103 |
{ "Velocity" , { 1, 0, -1 } }, |
| 104 |
{ "Volume" , { 3 } }, |
| 105 |
{ "VolumeChargeDensity" , { -3, 0, 1, 1 } }, |
| 106 |
{ "VolumeFlowRate" , { 3, 0, -1 } }, |
| 107 |
{ "VolumetricThermalExpansionCoefficient" , { 0, 0, 0, 0, -1 } }, |
| 108 |
{ "Work" , { 2, 1, -2 } }, |
| 109 |
{ "YieldStrength" , { -1, 1, -2 } }, |
| 110 |
{ "YoungsModulus" , { -1, 1, -2 } }, |
| 111 |
}); // clang-format on |
| 112 |
|
| 113 |
Unit::Unit( |
| 114 |
const int length, // NOLINT |
| 115 |
const int mass, |
| 116 |
const int time, |
| 117 |
const int electricCurrent, |
| 118 |
const int thermodynamicTemperature, |
| 119 |
const int amountOfSubstance, |
| 120 |
const int luminousIntensity, |
| 121 |
const int angle |
| 122 |
) |
| 123 |
: _name {""} |
| 124 |
{ |
| 125 |
auto cast = [](auto val) { |
| 126 |
return static_cast<int8_t>(std::clamp<decltype(val)>( |
| 127 |
val, |
| 128 |
std::numeric_limits<int8_t>::min(), |
| 129 |
std::numeric_limits<int8_t>::max() |
| 130 |
)); |
| 131 |
}; |
| 132 |
|
| 133 |
_exps[0] = cast(length); |
| 134 |
_exps[1] = cast(mass); |
| 135 |
_exps[2] = cast(time); |
| 136 |
_exps[3] = cast(electricCurrent); |
| 137 |
_exps[4] = cast(thermodynamicTemperature); |
| 138 |
_exps[5] = cast(amountOfSubstance); |
| 139 |
_exps[6] = cast(luminousIntensity); |
| 140 |
_exps[7] = cast(angle); |
| 141 |
|
| 142 |
checkRange(); |
| 143 |
} |
| 144 |
|
| 145 |
bool Unit::operator==(const Unit& that) const |
| 146 |
{ |
| 147 |
return _exps == that._exps; |
| 148 |
} |
| 149 |
|
| 150 |
bool Unit::operator!=(const Unit& that) const |
| 151 |
{ |
| 152 |
return _exps != that._exps; |
| 153 |
} |
| 154 |
|
| 155 |
Unit& Unit::operator*=(const Unit& that) |
| 156 |
{ |
| 157 |
*this = *this * that; |
| 158 |
return *this; |
| 159 |
} |
| 160 |
|
| 161 |
Unit& Unit::operator/=(const Unit& that) |
| 162 |
{ |
| 163 |
*this = *this / that; |
| 164 |
return *this; |
| 165 |
} |
| 166 |
|
| 167 |
Unit Unit::operator*(const Unit& right) const |
| 168 |
{ |
| 169 |
auto mult = [&](auto leftExponent, auto rightExponent) { |
| 170 |
return leftExponent + rightExponent; |
| 171 |
}; |
| 172 |
|
| 173 |
UnitExponents res {}; |
| 174 |
std::transform(_exps.begin(), _exps.end(), right._exps.begin(), res.begin(), mult); |
| 175 |
|
| 176 |
return Unit {res}; |
| 177 |
} |
| 178 |
|
| 179 |
Unit Unit::operator/(const Unit& right) const |
| 180 |
{ |
| 181 |
auto div = [&](auto leftExponent, auto rightExponent) { |
| 182 |
return leftExponent - rightExponent; |
| 183 |
}; |
| 184 |
|
| 185 |
UnitExponents res {}; |
| 186 |
std::transform(_exps.begin(), _exps.end(), right._exps.begin(), res.begin(), div); |
| 187 |
|
| 188 |
return Unit {res}; |
| 189 |
} |
| 190 |
|
| 191 |
Unit Unit::root(const uint8_t num) const |
| 192 |
{ |
| 193 |
auto apply = [&](auto val) { |
| 194 |
if (val % num != 0) { |
| 195 |
throw UnitsMismatchError("unit values must be divisible by root"); |
| 196 |
} |
| 197 |
return static_cast<decltype(val)>(val / num); |
| 198 |
}; |
| 199 |
|
| 200 |
if (num < 1) { |
| 201 |
throw UnitsMismatchError("root must be > 0"); |
| 202 |
} |
| 203 |
|
| 204 |
UnitExponents res {}; |
| 205 |
std::transform(_exps.begin(), _exps.end(), res.begin(), apply); |
| 206 |
|
| 207 |
return Unit {res}; |
| 208 |
} |
| 209 |
|
| 210 |
Unit Unit::pow(const double exp) const |
| 211 |
{ |
| 212 |
auto apply = [&](const auto val) { |
| 213 |
const auto num {val * exp}; |
| 214 |
if (std::fabs(std::round(num) - num) >= std::numeric_limits<double>::epsilon()) { |
| 215 |
throw UnitsMismatchError("pow() of unit not possible"); |
| 216 |
} |
| 217 |
|
| 218 |
return static_cast<decltype(val)>(val * exp); |
| 219 |
}; |
| 220 |
|
| 221 |
UnitExponents res {}; |
| 222 |
std::transform(_exps.begin(), _exps.end(), res.begin(), apply); |
| 223 |
|
| 224 |
return Unit {res}; |
| 225 |
} |
| 226 |
|
| 227 |
UnitExponents Unit::exponents() const |
| 228 |
{ |
| 229 |
return _exps; |
| 230 |
} |
| 231 |
|
| 232 |
int Unit::length() const |
| 233 |
{ |
| 234 |
return _exps[0]; |
| 235 |
} |
| 236 |
|
| 237 |
std::string Unit::getString() const |
| 238 |
{ |
| 239 |
auto buildSubStr = [&](auto index) { |
| 240 |
const std::string unitStrString {unitSymbols.at(index)}; |
| 241 |
const auto absol {abs(_exps.at(index))}; |
| 242 |
|
| 243 |
return absol <= 1 ? unitStrString |
| 244 |
: fmt::format("{}^{}", unitStrString, std::to_string(absol)); |
| 245 |
}; |
| 246 |
|
| 247 |
auto buildStr = [&](auto indexes) { |
| 248 |
std::vector<std::string> subStrings {}; |
| 249 |
std::transform(indexes.begin(), indexes.end(), std::back_inserter(subStrings), buildSubStr); |
| 250 |
|
| 251 |
return fmt::format("{}", fmt::join(subStrings, "*")); |
| 252 |
}; |
| 253 |
|
| 254 |
//------------------------------------------------------------------------------ |
| 255 |
|
| 256 |
auto [posValIndexes, negValIndexes] = nonZeroValsIndexes(); |
| 257 |
auto numeratorStr = buildStr(posValIndexes); |
| 258 |
if (negValIndexes.empty()) { |
| 259 |
return numeratorStr; |
| 260 |
} |
| 261 |
|
| 262 |
auto denominatorStr = buildStr(negValIndexes); |
| 263 |
|
| 264 |
return fmt::format( |
| 265 |
"{}/{}", |
| 266 |
numeratorStr.empty() ? "1" : numeratorStr, |
| 267 |
negValIndexes.size() > 1 ? fmt::format("({})", denominatorStr) : denominatorStr |
| 268 |
); |
| 269 |
} |
| 270 |
|
| 271 |
std::string Unit::representation() const |
| 272 |
{ |
| 273 |
auto name = getTypeString(); |
| 274 |
auto inParen = fmt::format("Unit: {} ({})", getString(), fmt::join(_exps, ",")); |
| 275 |
return name.empty() ? inParen : fmt::format("{} [{}]", inParen, name); |
| 276 |
} |
| 277 |
|
| 278 |
std::string Unit::getTypeString() const |
| 279 |
{ |
| 280 |
if (_name.empty()) { |
| 281 |
const auto spec = std::ranges::find(unitSpecs, _exps, &UnitSpec::exps); |
| 282 |
return std::string(spec == unitSpecs.end() ? "" : spec->name); |
| 283 |
} |
| 284 |
|
| 285 |
return std::string {_name.data(), _name.size()}; |
| 286 |
} |
| 287 |
|
| 288 |
std::pair<std::vector<size_t>, std::vector<size_t>> Unit::nonZeroValsIndexes() const |
| 289 |
{ |
| 290 |
std::vector<size_t> pos {}; |
| 291 |
std::vector<size_t> neg {}; |
| 292 |
|
| 293 |
auto posNeg = [&, index {0}](auto val) mutable { |
| 294 |
if (val != 0) { |
| 295 |
(val > 0 ? pos : neg).push_back(index); |
| 296 |
} |
| 297 |
++index; |
| 298 |
}; |
| 299 |
|
| 300 |
std::ranges::for_each(_exps, posNeg); |
| 301 |
|
| 302 |
return {pos, neg}; |
| 303 |
} |
| 304 |
|
| 305 |
|
| 306 |
constexpr Unit make(const std::string_view name) |
| 307 |
{ |
| 308 |
if (const auto spec = std::ranges::find(unitSpecs, name, &UnitSpec::name); |
| 309 |
spec != unitSpecs.end()) { |
| 310 |
return Unit {spec->exps, spec->name}; |
| 311 |
} |
| 312 |
throw NameError("Invalid unit name"); |
| 313 |
} |
| 314 |
|
| 315 |
// clang-format off |
| 316 |
constexpr Unit Unit::One = make("1" ); |
| 317 |
|
| 318 |
constexpr Unit Unit::Length = make("Length" ); |
| 319 |
constexpr Unit Unit::Mass = make("Mass" ); |
| 320 |
constexpr Unit Unit::TimeSpan = make("TimeSpan" ); |
| 321 |
constexpr Unit Unit::ElectricCurrent = make("ElectricCurrent" ); |
| 322 |
constexpr Unit Unit::Temperature = make("Temperature" ); |
| 323 |
constexpr Unit Unit::AmountOfSubstance = make("AmountOfSubstance" ); |
| 324 |
constexpr Unit Unit::LuminousIntensity = make("LuminousIntensity" ); |
| 325 |
constexpr Unit Unit::Angle = make("Angle" ); |
| 326 |
|
| 327 |
constexpr Unit Unit::Acceleration = make("Acceleration" ); |
| 328 |
constexpr Unit Unit::AngleOfFriction = make("Angle" ); |
| 329 |
constexpr Unit Unit::Area = make("Area" ); |
| 330 |
constexpr Unit Unit::CompressiveStrength = make("Pressure" ); |
| 331 |
constexpr Unit Unit::CurrentDensity = make("CurrentDensity" ); |
| 332 |
constexpr Unit Unit::Density = make("Density" ); |
| 333 |
constexpr Unit Unit::DissipationRate = make("DissipationRate" ); |
| 334 |
constexpr Unit Unit::DynamicViscosity = make("DynamicViscosity" ); |
| 335 |
constexpr Unit Unit::ElectricalCapacitance = make("ElectricalCapacitance" ); |
| 336 |
constexpr Unit Unit::ElectricalConductance = make("ElectricalConductance" ); |
| 337 |
constexpr Unit Unit::ElectricalConductivity = make("ElectricalConductivity" ); |
| 338 |
constexpr Unit Unit::ElectricalInductance = make("ElectricalInductance" ); |
| 339 |
constexpr Unit Unit::ElectricalResistance = make("ElectricalResistance" ); |
| 340 |
constexpr Unit Unit::ElectricCharge = make("ElectricCharge" ); |
| 341 |
constexpr Unit Unit::ElectricPotential = make("ElectricPotential" ); |
| 342 |
constexpr Unit Unit::ElectromagneticPotential = make("ElectromagneticPotential" ); |
| 343 |
constexpr Unit Unit::Force = make("Force" ); |
| 344 |
constexpr Unit Unit::Frequency = make("Frequency" ); |
| 345 |
constexpr Unit Unit::HeatFlux = make("HeatFlux" ); |
| 346 |
constexpr Unit Unit::InverseArea = make("InverseArea" ); |
| 347 |
constexpr Unit Unit::InverseLength = make("InverseLength" ); |
| 348 |
constexpr Unit Unit::InverseVolume = make("InverseVolume" ); |
| 349 |
constexpr Unit Unit::KinematicViscosity = make("KinematicViscosity" ); |
| 350 |
constexpr Unit Unit::MagneticFieldStrength = make("MagneticFieldStrength" ); |
| 351 |
constexpr Unit Unit::MagneticFlux = make("MagneticFlux" ); |
| 352 |
constexpr Unit Unit::MagneticFluxDensity = make("MagneticFluxDensity" ); |
| 353 |
constexpr Unit Unit::Magnetization = make("Magnetization" ); |
| 354 |
constexpr Unit Unit::Moment = make("Moment" ); |
| 355 |
constexpr Unit Unit::Pressure = make("Pressure" ); |
| 356 |
constexpr Unit Unit::Power = make("Power" ); |
| 357 |
constexpr Unit Unit::ShearModulus = make("Pressure" ); |
| 358 |
constexpr Unit Unit::SpecificEnergy = make("SpecificEnergy" ); |
| 359 |
constexpr Unit Unit::SpecificHeat = make("SpecificHeat" ); |
| 360 |
constexpr Unit Unit::Stiffness = make("Stiffness" ); |
| 361 |
constexpr Unit Unit::StiffnessDensity = make("StiffnessDensity" ); |
| 362 |
constexpr Unit Unit::Stress = make("Pressure" ); |
| 363 |
constexpr Unit Unit::SurfaceChargeDensity = make("SurfaceChargeDensity" ); |
| 364 |
constexpr Unit Unit::ThermalConductivity = make("ThermalConductivity" ); |
| 365 |
constexpr Unit Unit::ThermalExpansionCoefficient = make("ThermalExpansionCoefficient" ); |
| 366 |
constexpr Unit Unit::ThermalTransferCoefficient = make("ThermalTransferCoefficient" ); |
| 367 |
constexpr Unit Unit::UltimateTensileStrength = make("Pressure" ); |
| 368 |
constexpr Unit Unit::VacuumPermittivity = make("VacuumPermittivity" ); |
| 369 |
constexpr Unit Unit::Velocity = make("Velocity" ); |
| 370 |
constexpr Unit Unit::Volume = make("Volume" ); |
| 371 |
constexpr Unit Unit::VolumeChargeDensity = make("VolumeChargeDensity" ); |
| 372 |
constexpr Unit Unit::VolumeFlowRate = make("VolumeFlowRate" ); |
| 373 |
constexpr Unit Unit::VolumetricThermalExpansionCoefficient = make("ThermalExpansionCoefficient" ); |
| 374 |
constexpr Unit Unit::Work = make("Work" ); |
| 375 |
constexpr Unit Unit::YieldStrength = make("Pressure" ); |
| 376 |
constexpr Unit Unit::YoungsModulus = make("Pressure" ); |