Skip to content

Commit

Permalink
Move helper traits classes out of Quantity.hh
Browse files Browse the repository at this point in the history
  • Loading branch information
sethrj committed Jan 2, 2024
1 parent 1cf1921 commit 5e5ad81
Show file tree
Hide file tree
Showing 5 changed files with 143 additions and 109 deletions.
30 changes: 14 additions & 16 deletions src/celeritas/UnitTypes.hh
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
#pragma once

#include "celeritas_config.h"
#include "corecel/math/UnitUtils.hh"

#include "Constants.hh"
#include "Units.hh"
Expand Down Expand Up @@ -45,22 +46,14 @@ struct Mev
};

//! "Natural units" for mass
struct MevPerCsq
struct MevPerCsq : UnitDivide<Mev, UnitProduct<CLight, CLight>>
{
static CELER_CONSTEXPR_FUNCTION real_type value()
{
return Mev::value() / (constants::c_light * constants::c_light);
}
static char const* label() { return "MeV/c^2"; }
};

//! "Natural units" for momentum
struct MevPerC
struct MevPerC : UnitDivide<Mev, CLight>
{
static CELER_CONSTEXPR_FUNCTION real_type value()
{
return Mev::value() / constants::c_light;
}
static char const* label() { return "MeV/c"; }
};

Expand All @@ -78,6 +71,16 @@ struct EElectron
static char const* label() { return "e"; }
};

//! Quantity of substance
struct Mol
{
static CELER_CONSTEXPR_FUNCTION real_type value()
{
return constants::na_avogadro;
}
static char const* label() { return "mol"; }
};

//!@}
//---------------------------------------------------------------------------//
//!@{
Expand Down Expand Up @@ -144,13 +147,8 @@ struct InvCentimeterCubed
};

//! Molar density
struct MolPerCentimeterCubed
struct MolPerCentimeterCubed : UnitProduct<Mol, InvCentimeterCubed>
{
static CELER_CONSTEXPR_FUNCTION real_type value()
{
return constants::na_avogadro
/ (units::centimeter * units::centimeter * units::centimeter);
}
static char const* label() { return "mol/cm^3"; }
};

Expand Down
3 changes: 3 additions & 0 deletions src/celeritas/em/interactor/detail/PhysicsConstants.hh
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,9 @@
#include "corecel/Macros.hh"
#include "corecel/Types.hh"
#include "corecel/math/Algorithms.hh"
#include "corecel/math/UnitUtils.hh"
#include "celeritas/Constants.hh"
#include "celeritas/UnitTypes.hh"

namespace celeritas
{
Expand All @@ -28,6 +30,7 @@ CELER_CONSTEXPR_FUNCTION real_type migdal_constant()
return 4 * pi * r_electron * ipow<2>(lambdabar_electron);
}

//---------------------------------------------------------------------------//
/*!
* Landau-Pomeranchuk-Migdal constant [MeV / len].
*
Expand Down
95 changes: 2 additions & 93 deletions src/corecel/math/Quantity.hh
Original file line number Diff line number Diff line change
Expand Up @@ -12,73 +12,17 @@
#include "corecel/Macros.hh"
#include "corecel/Types.hh"

#include "NumericLimits.hh"
#include "detail/QuantityImpl.hh"

namespace celeritas
{
//---------------------------------------------------------------------------//
namespace detail
{

// Forward declaration
template<class, class>
struct LdgLoader;

//---------------------------------------------------------------------------//
//! Helper tag for special unitless values
enum class QConstant
{
neg_max = -1,
zero = 0,
max = 1
};

//! Convert unitless values into a particular type
template<class T>
CELER_CONSTEXPR_FUNCTION T get_constant(QConstant qc)
{
if constexpr (std::is_floating_point_v<T>)
{
// Return +/- infinity
return qc == QConstant::neg_max ? -numeric_limits<T>::infinity()
: qc == QConstant::max ? numeric_limits<T>::infinity()
: 0;
}
else
{
// Return lowest and highest values
return qc == QConstant::neg_max ? numeric_limits<T>::lowest()
: qc == QConstant::max ? numeric_limits<T>::max()
: 0;
}
}

//! Tag class for creating a nonnumeric value comparable to Quantity.
template<QConstant QC>
struct UnitlessQuantity
{
};

//! Helper class for getting attributes about a member function
template<class T>
struct AccessorTraits;

//! \cond
//! Access the return type using AccessorTraits<decltype(&Foo::bar)>
template<class ResultType, class ClassType>
struct AccessorTraits<ResultType (ClassType::*)() const>
{
using type = ClassType;
using result_type = ResultType;
};
//! \endcond

//! Get the result type of a class accessor
template<class T>
using AccessorResultType = typename AccessorTraits<T>::result_type;

//---------------------------------------------------------------------------//
} // namespace detail

//---------------------------------------------------------------------------//
/*!
* A numerical value tagged with a unit.
Expand Down Expand Up @@ -273,41 +217,6 @@ CELER_CONSTEXPR_FUNCTION auto operator/(Quantity<U, T> lhs, T2 rhs) noexcept
//!@!}

//! \endcond
//---------------------------------------------------------------------------//
//! Value is 1 / C1::value()
template<class C1>
struct UnitInverse
{
//! Get the conversion factor of the resulting unit
static CELER_CONSTEXPR_FUNCTION auto value() noexcept -> decltype(auto)
{
return 1 / C1::value();
}
};

//---------------------------------------------------------------------------//
//! Value is C1::value() / C2::value()
template<class C1, class C2>
struct UnitDivide
{
//! Get the conversion factor of the resulting unit
static CELER_CONSTEXPR_FUNCTION auto value() noexcept -> decltype(auto)
{
return C1::value() / C2::value();
}
};

//! Value is C1::value() * C2::value()
template<class C1, class C2>
struct UnitProduct
{
//! Get the conversion factor of the resulting unit
static CELER_CONSTEXPR_FUNCTION auto value() noexcept -> decltype(auto)
{
return C1::value() * C2::value();
}
};

//---------------------------------------------------------------------------//
// FREE FUNCTIONS
//---------------------------------------------------------------------------//
Expand Down
51 changes: 51 additions & 0 deletions src/corecel/math/UnitUtils.hh
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
//----------------------------------*-C++-*----------------------------------//
// Copyright 2024 UT-Battelle, LLC, and other Celeritas developers.
// See the top-level COPYRIGHT file for details.
// SPDX-License-Identifier: (Apache-2.0 OR MIT)
//---------------------------------------------------------------------------//
//! \file corecel/math/UnitUtils.hh
//! \brief Helpers for unit trait classes
//---------------------------------------------------------------------------//
#pragma once

#include "corecel/Macros.hh"

namespace celeritas
{
//---------------------------------------------------------------------------//
//! Value is 1 / C1::value()
template<class C1>
struct UnitInverse
{
//! Get the conversion factor of the resulting unit
static CELER_CONSTEXPR_FUNCTION auto value() noexcept -> decltype(auto)
{
return 1 / C1::value();
}
};

//---------------------------------------------------------------------------//
//! Value is C1::value() / C2::value()
template<class C1, class C2>
struct UnitDivide
{
//! Get the conversion factor of the resulting unit
static CELER_CONSTEXPR_FUNCTION auto value() noexcept -> decltype(auto)
{
return C1::value() / C2::value();
}
};

//! Value is C1::value() * C2::value()
template<class C1, class C2>
struct UnitProduct
{
//! Get the conversion factor of the resulting unit
static CELER_CONSTEXPR_FUNCTION auto value() noexcept -> decltype(auto)
{
return C1::value() * C2::value();
}
};

//---------------------------------------------------------------------------//
} // namespace celeritas
73 changes: 73 additions & 0 deletions src/corecel/math/detail/QuantityImpl.hh
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
//----------------------------------*-C++-*----------------------------------//
// Copyright 2024 UT-Battelle, LLC, and other Celeritas developers.
// See the top-level COPYRIGHT file for details.
// SPDX-License-Identifier: (Apache-2.0 OR MIT)
//---------------------------------------------------------------------------//
//! \file corecel/math/detail/QuantityImpl.hh
//---------------------------------------------------------------------------//
#pragma once

#include "corecel/Macros.hh"

#include "NumericLimits.hh"

namespace celeritas
{
namespace detail
{
//---------------------------------------------------------------------------//
//! Helper tag for special unitless values
enum class QConstant
{
neg_max = -1,
zero = 0,
max = 1
};

//! Convert unitless values into a particular type
template<class T>
CELER_CONSTEXPR_FUNCTION T get_constant(QConstant qc)
{
if constexpr (std::is_floating_point_v<T>)
{
// Return +/- infinity
return qc == QConstant::neg_max ? -numeric_limits<T>::infinity()
: qc == QConstant::max ? numeric_limits<T>::infinity()
: 0;
}
else
{
// Return lowest and highest values
return qc == QConstant::neg_max ? numeric_limits<T>::lowest()
: qc == QConstant::max ? numeric_limits<T>::max()
: 0;
}
}

//! Tag class for creating a nonnumeric value comparable to Quantity.
template<QConstant QC>
struct UnitlessQuantity
{
};

//! Helper class for getting attributes about a member function
template<class T>
struct AccessorTraits;

//! \cond
//! Access the return type using AccessorTraits<decltype(&Foo::bar)>
template<class ResultType, class ClassType>
struct AccessorTraits<ResultType (ClassType::*)() const>
{
using type = ClassType;
using result_type = ResultType;
};
//! \endcond

//! Get the result type of a class accessor
template<class T>
using AccessorResultType = typename AccessorTraits<T>::result_type;

//---------------------------------------------------------------------------//
} // namespace detail
} // namespace celeritas

0 comments on commit 5e5ad81

Please sign in to comment.