Skip to content

Commit

Permalink
Return physical constants object from opacities
Browse files Browse the repository at this point in the history
  • Loading branch information
brryan committed Sep 26, 2024
1 parent 54875f5 commit eddf3b3
Show file tree
Hide file tree
Showing 9 changed files with 60 additions and 9 deletions.
3 changes: 3 additions & 0 deletions singularity-opac/neutrinos/gray_opacity_neutrinos.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,9 @@ class GrayOpacity {
return dist_.NumberDensityFromTemperature(temp, type, lambda);
}

PORTABLE_INLINE_FUNCTION
pc GetPhysicalConstants() const { return pc(); }

private:
Real kappa_; // Opacity. Units of cm^2/g
ThermalDistribution dist_;
Expand Down
12 changes: 11 additions & 1 deletion singularity-opac/neutrinos/mean_neutrino_variant.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ namespace singularity {
namespace neutrinos {
namespace impl {

template <typename... Opacs>
template <typename CONSTANTS, typename... Opacs>
class MeanVariant {
private:
opac_variant<Opacs...> opac_;
Expand Down Expand Up @@ -88,6 +88,16 @@ class MeanVariant {
opac_);
}

PORTABLE_INLINE_FUNCTION CONSTANTS GetPhysicalConstants() const {
return mpark::visit([](auto &opac) { return opac.GetPhysicalConstants(); },
opac_);
}
// static CONSTANTS GetPhysicalConstants() {
// return mpark::visit(
// [](auto &opac) { return decltype(opac)::GetPhysicalConstants(); },
// opac_);
// }

inline void Finalize() noexcept {
return mpark::visit([](auto &opac) { return opac.Finalize(); }, opac_);
}
Expand Down
4 changes: 4 additions & 0 deletions singularity-opac/neutrinos/mean_opacity_neutrinos.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,10 @@ class MeanOpacity {
return other;
}

PORTABLE_INLINE_FUNCTION
pc GetPhysicalConstants() const { return pc(); }
// static pc GetPhysicalConstants() { return pc(); }

void Finalize() {
lkappaPlanck_.finalize();
lkappaRosseland_.finalize();
Expand Down
3 changes: 3 additions & 0 deletions singularity-opac/photons/gray_opacity_photons.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,9 @@ class GrayOpacity {
return dist_.NumberDensityFromTemperature(temp, lambda);
}

PORTABLE_INLINE_FUNCTION
pc GetPhysicalConstants() const { return pc(); }

private:
Real kappa_; // Opacity. Units of cm^2/g
PlanckDistribution<pc> dist_;
Expand Down
3 changes: 3 additions & 0 deletions singularity-opac/photons/mean_opacity_photons.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,9 @@ class MeanOpacity {
return rho * fromLog_(lkappaRosseland_.interpToReal(lRho, lT));
}

PORTABLE_INLINE_FUNCTION
pc GetPhysicalConstants() const { return pc(); }

private:
template <typename Opacity, bool AUTOFREQ>
void MeanOpacityImpl_(const Opacity &opac, const Real lRhoMin,
Expand Down
7 changes: 6 additions & 1 deletion singularity-opac/photons/mean_photon_variant.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ namespace singularity {
namespace photons {
namespace impl {

template <typename... Opacs>
template <typename CONSTANTS, typename... Opacs>
class MeanVariant {
private:
opac_variant<Opacs...> opac_;
Expand Down Expand Up @@ -86,6 +86,11 @@ class MeanVariant {
opac_);
}

PORTABLE_INLINE_FUNCTION CONSTANTS GetPhysicalConstants() const {
return mpark::visit([](auto &opac) { return opac.GetPhysicalConstants(); },
opac_);
}

inline void Finalize() noexcept {
return mpark::visit([](auto &opac) { return opac.Finalize(); }, opac_);
}
Expand Down
7 changes: 6 additions & 1 deletion singularity-opac/photons/photon_variant.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ namespace impl {
template <typename... Ts>
using opac_variant = mpark::variant<Ts...>;

template <typename... Opacs>
template <typename CONSTANTS, typename... Opacs>
class Variant {
private:
opac_variant<Opacs...> opac_;
Expand Down Expand Up @@ -275,6 +275,11 @@ class Variant {
opac_);
}

PORTABLE_INLINE_FUNCTION CONSTANTS GetPhysicalConstants() const {
return mpark::visit([](auto &opac) { return opac.GetPhysicalConstants(); },
opac_);
}

inline void Finalize() noexcept {
return mpark::visit([](auto &opac) { return opac.Finalize(); }, opac_);
}
Expand Down
6 changes: 4 additions & 2 deletions test/test_gray_opacities.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,9 @@ TEST_CASE("Gray neutrino opacities", "[GrayNeutrinos]") {
neutrinos::Gray opac_host(1);
neutrinos::Opacity opac = opac_host.GetOnDevice();

auto constants = opac_host.GetPhysicalConstants();
printf("cl: %e\n", constants.c);

THEN("The emissivity per nu omega is consistent with the emissity per nu") {
int n_wrong_h = 0;
#ifdef PORTABILITY_STRATEGY_KOKKOS
Expand Down Expand Up @@ -283,8 +286,7 @@ TEST_CASE("Gray photon opacities", "[GrayPhotons]") {

Real *nu_bins = (Real *)PORTABLE_MALLOC(nbins * sizeof(Real));
Real *temp_bins = (Real *)PORTABLE_MALLOC(ntemps * sizeof(Real));
DataBox loglin_bins(Spiner::AllocationTarget::Device, ntemps,
nbins);
DataBox loglin_bins(Spiner::AllocationTarget::Device, ntemps, nbins);

portableFor(
"set nu bins", 0, nbins, PORTABLE_LAMBDA(const int &i) {
Expand Down
24 changes: 20 additions & 4 deletions test/test_mean_opacities.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// ======================================================================
// © 2022. Triad National Security, LLC. All rights reserved. This
// © 2022-2024. Triad National Security, LLC. All rights reserved. This
// program was produced under U.S. Government contract
// 89233218CNA000001 for Los Alamos National Laboratory (LANL), which
// is operated by Triad National Security, LLC for the U.S.
Expand Down Expand Up @@ -46,7 +46,7 @@ using atomic_view = Kokkos::MemoryTraits<Kokkos::Atomic>;

template <typename T>
PORTABLE_INLINE_FUNCTION T FractionalDifference(const T &a, const T &b) {
return 2 * std::abs(b - a) / (std::abs(a) + std::abs(b) + 1e-20);
return 2 * std::abs(b - a) / (std::abs(a) + std::abs(b) + 1e-100);
}
constexpr Real EPS_TEST = 1e-3;
template <typename T>
Expand Down Expand Up @@ -89,9 +89,24 @@ TEST_CASE("Mean neutrino opacities", "[MeanNeutrinos]") {

neutrinos::MeanOpacityCGS mean_opac_host(
opac_host, lRhoMin, lRhoMax, NRho, lTMin, lTMax, NT, YeMin, YeMax, NYe);
// neutrinos::MeanOpacity mean_opac = mean_opac_host.GetOnDevice();
auto mean_opac = mean_opac_host.GetOnDevice();

// Check constants from mean opacity
THEN("Check constants from mean opacity for consistency") {
auto constants = mean_opac_host.GetPhysicalConstants();
int n_wrong = 0;
if (FractionalDifference(pc::eV, constants.eV) > EPS_TEST) {
n_wrong += 1;
}
if (FractionalDifference(pc::kb, constants.kb) > EPS_TEST) {
n_wrong += 1;
}
if (FractionalDifference(pc::h, constants.h) > EPS_TEST) {
n_wrong += 1;
}
REQUIRE(n_wrong == 0);
}

THEN("The emissivity per nu omega is consistent with the emissity per nu") {
int n_wrong_h = 0;
#ifdef PORTABILITY_STRATEGY_KOKKOS
Expand Down Expand Up @@ -611,7 +626,8 @@ TEST_CASE("Mean photon scattering opacities", "[MeanPhotonS]") {
int n_wrong = 0;
portableReduce(
"rebuilt table vs gray", 0, NRho, 0, NT, 0, 0,
PORTABLE_LAMBDA(const int iRho, const int iT, const int igarbage, int &accumulate) {
PORTABLE_LAMBDA(const int iRho, const int iT, const int igarbage,
int &accumulate) {
const Real lRho =
lRhoMin + (lRhoMax - lRhoMin) / (NRho - 1) * iRho;
const Real rho = std::pow(10, lRho);
Expand Down

0 comments on commit eddf3b3

Please sign in to comment.