add units

This commit is contained in:
AndreaRigoni
2026-03-13 21:40:14 +00:00
parent 61052f80bc
commit a142c5d060
7 changed files with 160 additions and 10 deletions

51
src/Math/Units.h Normal file
View File

@@ -0,0 +1,51 @@
#ifndef ULIB_MATH_UNITS_H
#define ULIB_MATH_UNITS_H
#include <CLHEP/Units/SystemOfUnits.h>
namespace uLib {
using namespace CLHEP;
inline namespace literals {
constexpr double operator"" _m(long double v) { return static_cast<double>(v) * CLHEP::meter; }
constexpr double operator"" _cm(long double v) { return static_cast<double>(v) * CLHEP::centimeter; }
constexpr double operator"" _mm(long double v) { return static_cast<double>(v) * CLHEP::millimeter; }
constexpr double operator"" _um(long double v) { return static_cast<double>(v) * CLHEP::micrometer; }
constexpr double operator"" _nm(long double v) { return static_cast<double>(v) * CLHEP::nanometer; }
constexpr double operator"" _km(long double v) { return static_cast<double>(v) * CLHEP::kilometer; }
constexpr double operator"" _m(unsigned long long v) { return static_cast<double>(v) * CLHEP::meter; }
constexpr double operator"" _cm(unsigned long long v) { return static_cast<double>(v) * CLHEP::centimeter; }
constexpr double operator"" _mm(unsigned long long v) { return static_cast<double>(v) * CLHEP::millimeter; }
constexpr double operator"" _um(unsigned long long v) { return static_cast<double>(v) * CLHEP::micrometer; }
constexpr double operator"" _nm(unsigned long long v) { return static_cast<double>(v) * CLHEP::nanometer; }
constexpr double operator"" _km(unsigned long long v) { return static_cast<double>(v) * CLHEP::kilometer; }
constexpr double operator"" _deg(long double v) { return static_cast<double>(v) * CLHEP::degree; }
constexpr double operator"" _rad(long double v) { return static_cast<double>(v) * CLHEP::radian; }
constexpr double operator"" _deg(unsigned long long v) { return static_cast<double>(v) * CLHEP::degree; }
constexpr double operator"" _rad(unsigned long long v) { return static_cast<double>(v) * CLHEP::radian; }
constexpr double operator"" _ns(long double v) { return static_cast<double>(v) * CLHEP::nanosecond; }
constexpr double operator"" _s(long double v) { return static_cast<double>(v) * CLHEP::second; }
constexpr double operator"" _ms(long double v) { return static_cast<double>(v) * CLHEP::millisecond; }
constexpr double operator"" _ns(unsigned long long v) { return static_cast<double>(v) * CLHEP::nanosecond; }
constexpr double operator"" _s(unsigned long long v) { return static_cast<double>(v) * CLHEP::second; }
constexpr double operator"" _ms(unsigned long long v) { return static_cast<double>(v) * CLHEP::millisecond; }
constexpr double operator"" _MeV(long double v) { return static_cast<double>(v) * CLHEP::megaelectronvolt; }
constexpr double operator"" _eV(long double v) { return static_cast<double>(v) * CLHEP::electronvolt; }
constexpr double operator"" _keV(long double v) { return static_cast<double>(v) * CLHEP::kiloelectronvolt; }
constexpr double operator"" _GeV(long double v) { return static_cast<double>(v) * CLHEP::gigaelectronvolt; }
constexpr double operator"" _TeV(long double v) { return static_cast<double>(v) * CLHEP::teraelectronvolt; }
constexpr double operator"" _MeV(unsigned long long v) { return static_cast<double>(v) * CLHEP::megaelectronvolt; }
constexpr double operator"" _eV(unsigned long long v) { return static_cast<double>(v) * CLHEP::electronvolt; }
constexpr double operator"" _keV(unsigned long long v) { return static_cast<double>(v) * CLHEP::kiloelectronvolt; }
constexpr double operator"" _GeV(unsigned long long v) { return static_cast<double>(v) * CLHEP::gigaelectronvolt; }
}
}
#endif

View File

@@ -13,6 +13,7 @@ set(TESTS
VoxImageCopyTest
TriangleMeshTest
BitCodeTest
UnitsTest
)
set(LIBRARIES

View File

@@ -14,7 +14,9 @@ TESTS = MathVectorTest \
PolicyTest \
AccumulatorTest \
VoxImageCopyTest \
TriangleMeshTest
TriangleMeshTest \
BitCodeTest \
UnitsTest
# else
# TEST =

View File

@@ -0,0 +1,71 @@
/*//////////////////////////////////////////////////////////////////////////////
// CMT Cosmic Muon Tomography project //////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////
Copyright (c) 2014, Universita' degli Studi di Padova, INFN sez. di Padova
All rights reserved
Authors: Andrea Rigoni Garola < andrea.rigoni@pd.infn.it >
------------------------------------------------------------------
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 3.0 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library.
//////////////////////////////////////////////////////////////////////////////*/
#include "testing-prototype.h"
#include "Math/Units.h"
#include <cmath>
using namespace uLib;
int main()
{
BEGIN_TESTING(Math Units);
// Length
TEST0(1.0_m != 1000.0);
TEST0(1_m != 1000.0);
TEST0(1.0_cm != 10.0);
TEST0(1_cm != 10.0);
TEST0(1.0_mm != 1.0);
TEST0(1_mm != 1.0);
TEST0(1.0_km != 1000000.0);
TEST0(1_km != 1000000.0);
TEST0(std::abs(1.0_um - 1e-3) > 1e-12);
TEST0(std::abs(1.0_nm - 1e-6) > 1e-15);
// Angle
TEST0(std::abs(180.0_deg - M_PI) > 1e-9);
TEST0(std::abs(180_deg - M_PI) > 1e-9);
TEST0(std::abs(1.0_rad - 1.0) > 1e-9);
// Time
TEST0(1.0_s != 1e9);
TEST0(1_s != 1e9);
TEST0(1.0_ms != 1e6);
TEST0(1_ms != 1e6);
TEST0(1.0_ns != 1.0);
TEST0(1_ns != 1.0);
// Energy
TEST0(1.0_MeV != 1.0);
TEST0(1_MeV != 1.0);
TEST0(1.0_GeV != 1000.0);
TEST0(1_GeV != 1000.0);
TEST0(1.0_TeV != 1000000.0);
TEST0(1.0_keV != 1e-3);
TEST0(1.0_eV != 1e-6);
END_TESTING;
}