LCOV - code coverage report
Current view: top level - units - unit.cpp (source / functions) Hit Total Coverage
Test: coverage.info Lines: 141 142 99.3 %
Date: 2024-04-28 01:25:40 Functions: 46 46 100.0 %

          Line data    Source code
       1             : // SPDX-License-Identifier: BSD-3-Clause
       2             : // Copyright (c) 2023 Scipp contributors (https://github.com/scipp)
       3             : /// @file
       4             : /// @author Simon Heybrock
       5             : /// @author Neil Vaytet
       6             : #include <regex>
       7             : #include <stdexcept>
       8             : 
       9             : #include <units/units.hpp>
      10             : #include <units/units_util.hpp>
      11             : 
      12             : #include "scipp/units/except.h"
      13             : #include "scipp/units/unit.h"
      14             : 
      15             : namespace scipp::units {
      16             : 
      17             : namespace {
      18       27965 : std::string map_unit_string(const std::string &unit) {
      19             :   // custom dimensionless name
      20       27965 :   return unit == "dimensionless" ? ""
      21             :          // Use Gregorian months and years by default.
      22       27761 :          : unit == "y" || unit == "Y" || unit == "year" ? "a_g"
      23             :          // Overwrite M to mean month instead of molarity for numpy
      24             :          // interop.
      25       27558 :          : unit == "M" || unit == "month" ? "mog"
      26       59492 :                                           : unit;
      27             : }
      28             : 
      29       27965 : bool is_special_unit(const llnl::units::precise_unit &unit) {
      30             :   using namespace llnl::units::precise::custom;
      31       27965 :   const auto &base = unit.base_units();
      32             : 
      33             :   // Allowing custom_count_unit_number == 1 because that is 'arbitrary unit'
      34       55915 :   return is_custom_unit(base) ||
      35       55921 :          (is_custom_count_unit(base) && custom_count_unit_number(base) != 1) ||
      36       55913 :          unit.commodity() != 0;
      37             : }
      38             : } // namespace
      39             : 
      40       27965 : Unit::Unit(const std::string &unit)
      41       55930 :     : Unit(llnl::units::unit_from_string(map_unit_string(unit),
      42       27965 :                                          llnl::units::strict_si)) {
      43       27965 :   if (const auto &u = m_unit.value(); is_special_unit(u) || !is_valid(u))
      44          19 :     throw except::UnitError("Failed to convert string `" + unit +
      45          38 :                             "` to valid unit.");
      46       27946 : }
      47             : 
      48        3399 : std::string Unit::name() const {
      49        3399 :   if (!has_value())
      50          29 :     return "None";
      51        3370 :   if (*this == Unit{"month"}) {
      52          72 :     return "M";
      53             :   }
      54        3298 :   auto repr = to_string(*m_unit);
      55        3298 :   repr = std::regex_replace(repr, std::regex("^u"), "ยต");
      56        3298 :   repr = std::regex_replace(repr, std::regex("item"), "count");
      57        3298 :   repr = std::regex_replace(repr, std::regex("count(?!s)"), "counts");
      58        3298 :   repr = std::regex_replace(repr, std::regex("day"), "D");
      59        3298 :   repr = std::regex_replace(repr, std::regex("a_g"), "Y");
      60        3298 :   return repr.empty() ? "dimensionless" : repr;
      61        3298 : }
      62             : 
      63          19 : bool Unit::isCounts() const { return *this == counts; }
      64             : 
      65           8 : bool Unit::isCountDensity() const {
      66           8 :   return has_value() && !isCounts() && m_unit->base_units().count() != 0;
      67             : }
      68             : 
      69         554 : bool Unit::has_same_base(const Unit &other) const {
      70         554 :   return has_value() && m_unit->has_same_base(other.underlying());
      71             : }
      72             : 
      73     2889992 : bool Unit::operator==(const Unit &other) const {
      74     2889992 :   return m_unit == other.m_unit;
      75             : }
      76             : 
      77     2248408 : bool Unit::operator!=(const Unit &other) const { return !(*this == other); }
      78             : 
      79       15701 : Unit &Unit::operator+=(const Unit &other) { return *this = *this + other; }
      80             : 
      81         122 : Unit &Unit::operator-=(const Unit &other) { return *this = *this - other; }
      82             : 
      83        2800 : Unit &Unit::operator*=(const Unit &other) { return *this = *this * other; }
      84             : 
      85         100 : Unit &Unit::operator/=(const Unit &other) { return *this = *this / other; }
      86             : 
      87          20 : Unit &Unit::operator%=(const Unit &other) { return *this = *this % other; }
      88             : 
      89      318260 : Unit operator+(const Unit &a, const Unit &b) {
      90      318260 :   if (a == b)
      91      318243 :     return a;
      92          17 :   throw except::UnitError("Cannot add " + a.name() + " and " + b.name() + ".");
      93             : }
      94             : 
      95       33125 : Unit operator-(const Unit &a, const Unit &b) {
      96       33125 :   if (a == b)
      97       33121 :     return a;
      98           4 :   throw except::UnitError("Cannot subtract " + a.name() + " and " + b.name() +
      99           8 :                           ".");
     100             : }
     101             : 
     102             : namespace {
     103      306678 : void expect_not_none(const Unit &u, const std::string &name) {
     104      306678 :   if (!u.has_value())
     105          11 :     throw except::UnitError("Cannot " + name + " with operand of unit 'None'.");
     106      306667 : }
     107             : } // namespace
     108             : 
     109      147821 : Unit operator*(const Unit &a, const Unit &b) {
     110      147821 :   if (a == none && b == none)
     111        1466 :     return none;
     112      146359 :   expect_not_none(a, "multiply");
     113      146357 :   expect_not_none(b, "multiply");
     114      146351 :   if (llnl::units::times_overflows(a.underlying(), b.underlying()))
     115           2 :     throw except::UnitError("Unsupported unit as result of multiplication: (" +
     116           3 :                             a.name() + ") * (" + b.name() + ')');
     117      146350 :   return Unit{a.underlying() * b.underlying()};
     118             : }
     119             : 
     120        6984 : Unit operator/(const Unit &a, const Unit &b) {
     121        6984 :   if (a == none && b == none)
     122          25 :     return none;
     123        6963 :   expect_not_none(a, "divide");
     124        6961 :   expect_not_none(b, "divide");
     125        6955 :   if (llnl::units::divides_overflows(a.underlying(), b.underlying()))
     126           4 :     throw except::UnitError("Unsupported unit as result of division: (" +
     127           6 :                             a.name() + ") / (" + b.name() + ')');
     128        6953 :   return Unit{a.underlying() / b.underlying()};
     129             : }
     130             : 
     131         100 : Unit operator%(const Unit &a, const Unit &b) {
     132         100 :   if (a == b)
     133          91 :     return a;
     134          27 :   throw except::UnitError("Cannot perform modulo operation with " + a.name() +
     135          36 :                           " and " + b.name() + ". Units must be the same.");
     136             : }
     137             : 
     138        1089 : Unit operator-(const Unit &a) { return a; }
     139             : 
     140         518 : Unit abs(const Unit &a) { return a; }
     141             : 
     142           7 : Unit floor(const Unit &a) { return a; }
     143             : 
     144           5 : Unit ceil(const Unit &a) { return a; }
     145             : 
     146           6 : Unit rint(const Unit &a) { return a; }
     147             : 
     148         185 : Unit sqrt(const Unit &a) {
     149         185 :   if (a == none)
     150           2 :     return a;
     151         183 :   if (llnl::units::is_error(sqrt(a.underlying())))
     152           8 :     throw except::UnitError("Unsupported unit as result of sqrt: sqrt(" +
     153          12 :                             a.name() + ").");
     154         179 :   return Unit{sqrt(a.underlying())};
     155             : }
     156             : 
     157         715 : Unit pow(const Unit &a, const int64_t power) {
     158         715 :   if (a == none)
     159           0 :     return a;
     160         715 :   if (llnl::units::pow_overflows(a.underlying(), static_cast<int>(power)))
     161           2 :     throw except::UnitError("Unsupported unit as result of pow: pow(" +
     162           3 :                             a.name() + ", " + std::to_string(power) + ").");
     163         714 :   return Unit{a.underlying().pow(static_cast<int>(power))};
     164             : }
     165             : 
     166         154 : Unit trigonometric(const Unit &a) {
     167         154 :   if (a == units::rad || a == units::deg)
     168         142 :     return units::dimensionless;
     169          12 :   throw except::UnitError(
     170          24 :       "Trigonometric function requires rad or deg unit, got " + a.name() + ".");
     171             : }
     172             : 
     173          51 : Unit inverse_trigonometric(const Unit &a) {
     174          51 :   if (a == units::dimensionless)
     175          36 :     return units::rad;
     176          15 :   throw except::UnitError(
     177          15 :       "Inverse trigonometric function requires dimensionless unit, got " +
     178          45 :       a.name() + ".");
     179             : }
     180             : 
     181         117 : Unit sin(const Unit &a) { return trigonometric(a); }
     182          18 : Unit cos(const Unit &a) { return trigonometric(a); }
     183          19 : Unit tan(const Unit &a) { return trigonometric(a); }
     184          17 : Unit asin(const Unit &a) { return inverse_trigonometric(a); }
     185          17 : Unit acos(const Unit &a) { return inverse_trigonometric(a); }
     186          17 : Unit atan(const Unit &a) { return inverse_trigonometric(a); }
     187          28 : Unit atan2(const Unit &y, const Unit &x) {
     188          32 :   expect_not_none(x, "atan2");
     189          28 :   expect_not_none(y, "atan2");
     190          25 :   if (x == y)
     191          18 :     return units::rad;
     192           7 :   throw except::UnitError(
     193          14 :       "atan2 function requires matching units for input, got a " + x.name() +
     194          28 :       " b " + y.name() + ".");
     195             : }
     196             : 
     197          24 : Unit hyperbolic(const Unit &a) {
     198          24 :   if (a == units::dimensionless)
     199          18 :     return units::dimensionless;
     200           6 :   throw except::UnitError(
     201          12 :       "Hyperbolic function requires dimensionless input, got " + a.name() +
     202          12 :       ".");
     203             : }
     204             : 
     205           7 : Unit sinh(const Unit &a) { return hyperbolic(a); }
     206           3 : Unit cosh(const Unit &a) { return hyperbolic(a); }
     207           3 : Unit tanh(const Unit &a) { return hyperbolic(a); }
     208           3 : Unit asinh(const Unit &a) { return hyperbolic(a); }
     209           5 : Unit acosh(const Unit &a) { return hyperbolic(a); }
     210           3 : Unit atanh(const Unit &a) { return hyperbolic(a); }
     211             : 
     212          11 : bool identical(const Unit &a, const Unit &b) {
     213          22 :   return a.has_value() && b.has_value() &&
     214          22 :          a.underlying().is_exactly_the_same(b.underlying());
     215             : }
     216             : 
     217          39 : void add_unit_alias(const std::string &name, const Unit &unit) {
     218          39 :   llnl::units::addUserDefinedUnit(name, unit.underlying());
     219          39 : }
     220             : 
     221         129 : void clear_unit_aliases() { llnl::units::clearUserDefinedUnits(); }
     222             : 
     223             : } // namespace scipp::units

Generated by: LCOV version 1.14