Skip to content

Commit

Permalink
Move the default measurement type operations to a constexpr array wit…
Browse files Browse the repository at this point in the history
…h the other unit_conversion_maps
  • Loading branch information
phlptp committed May 13, 2022
1 parent d307253 commit 1b35c75
Show file tree
Hide file tree
Showing 3 changed files with 280 additions and 247 deletions.
24 changes: 24 additions & 0 deletions test/test_defined_units.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -79,3 +79,27 @@ TEST(unit_string_definitions, combined_duplicates)
<< units::defined_unit_strings_customary[ii].first;
}
}


TEST(unit_string_definitions, measurement_vector_length)
{
for (std::size_t ii = 0; ii < units::defined_measurement_types.size(); ++ii) {
EXPECT_TRUE(units::defined_measurement_types[ii].first != nullptr)
<< ii;
if (units::defined_measurement_types[ii].first == nullptr) {
break;
}
}
}

TEST(unit_string_definitions, measurement_duplicates)
{
std::map<std::string, units::precise_unit> testMap;
for (const auto& ustring : units::defined_measurement_types) {
if (ustring.first == nullptr) {
continue;
}
auto res = testMap.emplace(ustring.first, ustring.second);
EXPECT_TRUE(res.second) << "duplicate measurement type string " << ustring.first;
}
}
259 changes: 12 additions & 247 deletions units/units.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5014,256 +5014,21 @@ uncertain_measurement uncertain_measurement_from_string(
measurement_cast_from_string(measurement_string, match_flags), 0.0F);
}

// Mostly from https://en.wikipedia.org/wiki/International_System_of_Units
static const std::unordered_map<std::string, precise_unit> measurement_types{
{"", precise::defunit},
{"arb", precise::defunit},
{"arbitrary", precise::defunit},
{"length", precise::m},
{"len", precise::m},
{"distance", precise::m},
{"height", precise::m},
{"depth", precise::m},
{"width", precise::m},
{"diameter", precise::m},
{"thickness", precise::m},
{"gauge", precise::m},
{"invlen", precise::m.inv()},
{"volume", precise::m.pow(3)},
{"dryvolume", precise::m.pow(3)},
{"vol", precise::m.pow(3)},
{"fluidvolume", precise::L},
{"time", precise::s},
{"duration", precise::s},
{"mass", precise::kg},
{"weight", precise::kg},
{"current", precise::A},
{"electriccurrent", precise::A},
{"magnetomotiveforce", precise::A},
{"temperature", precise::K},
{u8"\u2C90", precise::K},
{u8"\u2129", precise::K},
{u8"\u03F4", precise::K},
{u8"\u0398", precise::K},
{"celsiustemperature", precise::degC},
{"temp", precise::K},
{"thermodynamictemperature", precise::K},
{"thermalconductivity", precise::W / precise::m / precise::K},
{"amount", precise::mol},
{"amountofsubstance", precise::mol},
{"substance", precise::mol},
{"sub", precise::mol},
{"luminousintensity", precise::cd},
{"area", precise::m.pow(2)},
{"speed", precise::m / precise::s},
{"velocity", precise::m / precise::s},
{"vel", precise::m / precise::s},
{"wavenumber", precise::m.inv()},
{"acceleration", precise::m / precise::s.pow(2)},
{"accel", precise::m / precise::s.pow(2)},
{"density", precise::kg / precise::m.pow(3)},
{"massdensity", precise::kg / precise::m.pow(3)},
{"massconcentration", precise::kg / precise::m.pow(3)},
{"surfacedensity", precise::kg / precise::m.pow(2)},
{"lineardensity", precise::kg / precise::m},
{"lineicmass", precise::kg / precise::m},
{"linearmassdensity", precise::kg / precise::m},
{"armass", precise::kg / precise::m.pow(2)},
{"specificvolume", precise::m.pow(3) / precise::kg},
{"vcnt", precise::L / precise::kg},
{"currentdensity", precise::A / precise::m.pow(2)},
{"magneticfieldstrength", precise::A / precise::m},
{"magneticfieldintensity", precise::A / precise::m},
{"magnetictension", precise::Pa / precise::m},
{"concentration", precise::mol / precise::m.pow(3)},
{"luminance", precise::cd / precise::m.pow(2)},
{"brightness", precise::cd / precise::m.pow(2)},
{"lum.intensitydensity", precise::cd / precise::m.pow(2)},
{"planeangle", precise::rad},
{"angle", precise::rad},
{"solidangle", precise::rad.pow(2)},
{"angular", precise::rad},
{"frequency", precise::Hz},
{"freq", precise::Hz},
{"frequencies", precise::Hz},
{"force", precise::N},
{"weight", precise::N},
{"pressure", precise::Pa},
{"pres", precise::Pa},
{"press", precise::Pa},
{"stress", precise::Pa},
{"energy", precise::J},
{"eng", precise::J},
{"enrg", precise::J},
{"work", precise::J},
{"heat", precise::J},
{"power", precise::W},
{"powerlevel", precise::W* precise::log::neper},
{"radiantflux", precise::W},
{"heatfluxdensity", precise::W / precise::m.pow(2)},
{"electriccharge", precise::C},
{"electricfluxdensity", precise::C / precise::m.pow(2)},
{"charge", precise::C},
{"electricchargedensity", precise::C / precise::m.pow(3)},
{"quantityofelectricity", precise::C},
{"voltage", precise::V},
{"electricalpotential", precise::V},
{"potentialdifference", precise::V},
{"electricpotentialdifference", precise::V},
{"elpot", precise::V},
{"electricpotential", precise::V},
{"electricpotentiallevel", precise::V* precise::log::neper},
{"logrtoelp", precise::V* precise::log::neper},
{"emf", precise::V},
{"electromotiveforce", precise::V},
{"capacitance", precise::F},
{"electriccapacitance", precise::F},
{"resistance", precise::ohm},
{"impedance", precise::ohm},
{"reactance", precise::ohm},
{"conductance", precise::siemens},
{"electricdipolemoment", precise::C* precise::m},
{"electricresistance", precise::ohm},
{"electricresistivity", precise::ohm* precise::m},
{"electricimpedance", precise::ohm},
{"electricreactance", precise::ohm},
{"electricalconductance", precise::siemens},
{"electricconductance", precise::siemens},
{"conduction", precise::siemens},
{"magneticflux", precise::Wb},
{"fluxofmagneticinduction", precise::Wb},
{"magneticfluxdensity", precise::T},
{"magneticinduction", precise::T},
{"fluxdensity", precise::T},
{"noisevoltagedensity", precise::V / precise::special::rootHertz},
{"inductance", precise::H},
{"induction", precise::H},
{"luminousflux", precise::lm},
{"illuminance", precise::lx},
{"illumination", precise::lx},
{"luminousemittance", precise::lx},
{"intensityoflight", precise::lx},
{"radioactivity", precise::Bq},
{"radionuclideactivity", precise::Bq},
{"radiation", precise::Bq},
{"activity", precise::Bq},
{"activities", precise::Bq},
{"logarithmofpowerratio", precise::log::dB},
{"absorbeddose", precise::Gy},
{"ionizingradiation", precise::Gy},
{"kerma", precise::Gy},
{"energydose", precise::Gy},
{"absorbeddoseenergy", precise::Gy},
{"engcnt", precise::Gy},
{"equivalentdose", precise::Sv},
{"radiationdose", precise::Sv},
{"ionizingradiationdose", precise::Sv},
{"effectivedose", precise::Sv},
{"operationaldose", precise::Sv},
{"committeddose", precise::Sv},
{"catalyticactivity", precise::kat},
{"specificenergy", precise::J / precise::kg},
{"specificheat", precise::J / precise::kg / precise::K},
{"engcnc", precise::J / precise::m.pow(3)},
{"momentofforce", precise::N* precise::m},
{"moment", precise::N* precise::m},
{"torque", precise::N* precise::m},
{"angularvelocity", precise::rad / precise::s},
{"rotationalspeed", precise::rad / precise::s},
{"rotationalvelocity", precise::rad / precise::s},
{"angularacceleration", precise::rad / precise::s.pow(2)},
{"surfacetension", precise::N / precise::m},
{"electricfield", precise::V / precise::m},
{"electricfieldstrength", precise::V / precise::m},
{"permittivity", precise::F / precise::m},
{"permeability", precise::H / precise::m},
{"electricpermittivity", precise::F / precise::m},
{"magneticpermeability", precise::H / precise::m},
{"exposure", precise::C / precise::kg},
{"heatcapacity", precise::J / precise::K},
{"entropy", precise::J / precise::K},
{"specificentropy", precise::J / precise::kg / precise::K},
{"specificenergy", precise::J / precise::kg},
{"dynamicviscosity", precise::Pa* precise::s},
{"viscosity", precise::Pa* precise::s},
{"visc", precise::Pa* precise::s},
{"fluidity", precise::one / precise::Pa / precise::s},
{"kinematicviscosity", precise::m.pow(2) / precise::s},
{"radiantintensity", precise::W / precise::rad.pow(2)},
{"radiance", precise::W / precise::rad.pow(2) / precise::m.pow(2)},
{"dimless", precise::one},
{"ratio", precise::one},
{"fraction", precise::one},
{"number", precise::one},
{"nfr", precise::one},
{"num", precise::one},
{"age", precise::time::ag},
{"impulse", precise::N* precise::s},
{"imp", precise::N* precise::s},
{"absorbeddoserate", precise::Gy / precise::s},
{"mcnc", precise::kg / precise::m.pow(3)},
{"massconcentration", precise::kg / precise::m.pow(3)},
{"scnc", precise::mol / precise::m.pow(3)},
{"scnt", precise::mol / precise::kg},
{"ncnc", precise::one / precise::m.pow(3)},
{"ncncn", precise::one / precise::m.pow(3)},
{"ncnt", precise::one / precise::kg},
{"substanceconcentration", precise::mol / precise::m.pow(3)},
{"level", precise::log::neper},
{"logrto", precise::log::neper},
{"arvol", precise::m.pow(3) / precise::m.pow(2)},
{"arsub", precise::mol / precise::m.pow(2)},
{"lineicvolume", precise::m.pow(3) / precise::m},
{"lineicnumber", precise::one / precise::m},
{"refraction", precise::one / precise::m},
{"naric", precise::one / precise::m.pow(2)},
{"nlen", precise::one / precise::m},
{"acidity", precise::laboratory::pH},
{"cact", precise::kat},
{"doseequivalent", precise::Sv},
{"equivalentdose", precise::Sv},
{"magneticfield", precise::T},
{"magnetic", precise::T},
{"absorbeddose", precise::Gy},
{"ionizingradiationdose", precise::Gy},
{"iondose", precise::Gy},
{"exposure", precise::C / precise::kg},
{"fluence", precise::one / precise::m.pow(2)},
{"activity", precise::Bq},
{"mcnt", precise::pu* precise::m},
{"ccnt", precise::kat / precise::kg},
{"ccnc", precise::kat / precise::L},
{"acnc", precise::one / precise::L},
{"velcnc", precise::m / precise::s / precise::L},
{"mrat", precise::kg / precise::s},
{"osmol", precise_unit(1.0, precise::mol, commodities::particles)},
{"massfraction", precise::pu* precise::kg},
{"mfr", precise::pu* precise::kg},
{"amplitudespectraldensity", precise::special::ASD},
{"fluidresistance", precise::Pa* precise::s / precise::m.pow(3)},
{"signaltransmissionrate", precise::bit / precise::s},
{"engmass", precise::J / precise::m.pow(3)},
{"massicenergy", precise::J / precise::m.pow(3)},
{"energydensity", precise::J / precise::m.pow(3)},
{"entsub", precise::mol},
{"mnum", precise::kg},
{"cmass", precise::kg / precise::kat},
{"stiffness", precise::N / precise::m},
{"signalingrate", precise::bit / precise::s},
{"datarate", precise::bit / precise::s},
{"elasticity", precise::N / precise::m.pow(2)},
{"compliance", precise::m / precise::N},
{"informationcapacity", precise::data::bit},
{"compli", precise::m / precise::N},
{"vascularresistance", precise::Pa* precise::s / precise::m.pow(3)},
{"enzymaticactivity", precise::kat},
{"catalyticconcentration", precise::kat / precise::m.pow(3)},
{"molarenergy", precise::J / precise::mol},
{"molarentropy", precise::J / precise::mol / precise::K},
};
static smap loadDefinedMeasurementTypes()
{
smap knownMeasurementTypes;
for (auto& pr : defined_measurement_types) {
if (pr.first != nullptr) {
knownMeasurementTypes.emplace(pr.first, pr.second);
}
}
return knownMeasurementTypes;
}

precise_unit default_unit(std::string unit_type)
{
static const smap measurement_types = loadDefinedMeasurementTypes();

if (unit_type.size() == 1) {
switch (unit_type[0]) {
case 'L':
Expand Down
Loading

0 comments on commit 1b35c75

Please sign in to comment.