Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Return physical constants object from opacities #52

Merged
merged 8 commits into from
Sep 28, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 40 additions & 1 deletion singularity-opac/constants/constants.hpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// ======================================================================
// © 2021. Triad National Security, LLC. All rights reserved. This
// © 2021-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 @@ -194,6 +194,45 @@ struct PhysicalConstants {
static constexpr Real gA = axial_vector_coupling_constant;
};

struct RuntimePhysicalConstants {
template <typename T>
RuntimePhysicalConstants(T pc)
: na(pc.na), alpha(pc.alpha), h(pc.h), hbar(pc.hbar), kb(pc.kb),
r_gas(pc.r_gas), qe(pc.qe), c(pc.c), g_newt(pc.g_newt),
g_accel(pc.g_accel), me(pc.me), mp(pc.mp), mn(pc.mn), amu(pc.amu),
sb(pc.sb), ar(pc.ar), faraday(pc.faraday), mu0(pc.mu0), eps0(pc.eps0),
eV(pc.eV), Fc(pc.Fc), nu_sigma0(pc.nu_sigma0), gA(pc.gA) {}

const Real na;
const Real alpha;
const Real h;
const Real hbar;
const Real kb;
const Real r_gas;
const Real qe;
const Real c;
const Real g_newt;
const Real g_accel;
const Real me;
const Real mp;
const Real mn;
const Real amu;
const Real sb;
const Real ar;
const Real faraday;
const Real mu0;
const Real eps0;
const Real eV;
const Real Fc;
const Real nu_sigma0;
const Real gA;
};

template <typename T>
RuntimePhysicalConstants GetRuntimePhysicalConstants(T phys_constants) {
return RuntimePhysicalConstants(phys_constants);
}

using PhysicalConstantsUnity =
PhysicalConstants<BaseUnity, UnitConversionDefault>;
using PhysicalConstantsSI =
Expand Down
4 changes: 3 additions & 1 deletion singularity-opac/neutrinos/brt_neutrinos.hpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// ======================================================================
// © 2021. Triad National Security, LLC. All rights reserved. This
// © 2021-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 @@ -32,6 +32,8 @@ namespace neutrinos {
template <typename ThermalDistribution, typename pc = PhysicalConstantsCGS>
class BRTOpacity {
public:
using PC = pc;

BRTOpacity() = default;
BRTOpacity(const ThermalDistribution &dist) : dist_(dist) {}
BRTOpacity GetOnDevice() { return *this; }
Expand Down
4 changes: 3 additions & 1 deletion singularity-opac/neutrinos/gray_opacity_neutrinos.hpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// ======================================================================
// © 2021. Triad National Security, LLC. All rights reserved. This
// © 2021-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 @@ -30,6 +30,8 @@ namespace neutrinos {
template <typename ThermalDistribution, typename pc = PhysicalConstantsCGS>
class GrayOpacity {
public:
using PC = pc;

GrayOpacity() = default;
GrayOpacity(const Real kappa) : kappa_(kappa) {}
GrayOpacity(const ThermalDistribution &dist, const Real kappa)
Expand Down
18 changes: 17 additions & 1 deletion singularity-opac/neutrinos/mean_neutrino_variant.hpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// ======================================================================
// © 2021. Triad National Security, LLC. All rights reserved. This
// © 2021-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 @@ -69,6 +69,16 @@ class MeanVariant {
opac_);
}

PORTABLE_INLINE_FUNCTION RuntimePhysicalConstants
GetRuntimePhysicalConstants() const {
return mpark::visit(
[=](const auto &opac) {
using PC = typename std::decay_t<decltype(opac)>::PC;
return singularity::GetRuntimePhysicalConstants(PC());
},
opac_);
}

PORTABLE_INLINE_FUNCTION Real PlanckMeanAbsorptionCoefficient(
const Real rho, const Real temp, const Real Ye,
const RadiationType type) const {
Expand All @@ -91,6 +101,12 @@ class MeanVariant {
inline void Finalize() noexcept {
return mpark::visit([](auto &opac) { return opac.Finalize(); }, opac_);
}

#ifdef SPINER_USE_HDF
void Save(const std::string &filename) const {
return mpark::visit([=](auto &opac) { return opac.Save(filename); }, opac_);
}
#endif
};

} // namespace impl
Expand Down
16 changes: 8 additions & 8 deletions singularity-opac/neutrinos/mean_opacity_neutrinos.hpp
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 @@ -37,7 +37,6 @@ namespace impl {
// TODO(BRR) Note: It is assumed that lambda is constant for all densities,
// temperatures, and Ye

template <typename pc = PhysicalConstantsCGS>
class MeanOpacity {

public:
Expand Down Expand Up @@ -134,6 +133,8 @@ class MeanOpacity {
const Real lTMax, const int NT, const Real YeMin,
const Real YeMax, const int NYe, Real lNuMin,
Real lNuMax, const int NNu, Real *lambda = nullptr) {
using PC = typename Opacity::PC;

lkappaPlanck_.resize(NRho, NT, NYe, NEUTRINO_NTYPES);
// index 0 is the species and is not interpolatable
lkappaPlanck_.setRange(1, YeMin, YeMax, NYe);
Expand All @@ -159,8 +160,8 @@ class MeanOpacity {
// Choose default temperature-specific frequency grid if frequency
// grid not specified
if (AUTOFREQ) {
lNuMin = toLog_(1.e-3 * pc::kb * fromLog_(lTMin) / pc::h);
lNuMax = toLog_(1.e3 * pc::kb * fromLog_(lTMax) / pc::h);
lNuMin = toLog_(1.e-3 * PC::kb * fromLog_(lTMin) / PC::h);
lNuMax = toLog_(1.e3 * PC::kb * fromLog_(lTMax) / PC::h);
}
const Real dlnu = (lNuMax - lNuMin) / (NNu - 1);
// Integrate over frequency
Expand Down Expand Up @@ -219,10 +220,9 @@ class MeanOpacity {

} // namespace impl

using MeanOpacityScaleFree = impl::MeanOpacity<PhysicalConstantsUnity>;
using MeanOpacityCGS = impl::MeanOpacity<PhysicalConstantsCGS>;
using MeanOpacity = impl::MeanVariant<MeanOpacityScaleFree, MeanOpacityCGS,
MeanNonCGSUnits<MeanOpacityCGS>>;
using MeanOpacityBase = impl::MeanOpacity;
using MeanOpacity =
impl::MeanVariant<MeanOpacityBase, MeanNonCGSUnits<MeanOpacityBase>>;

} // namespace neutrinos
} // namespace singularity
Expand Down
12 changes: 11 additions & 1 deletion singularity-opac/neutrinos/neutrino_variant.hpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// ======================================================================
// © 2021. Triad National Security, LLC. All rights reserved. This
// © 2021-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 @@ -71,6 +71,16 @@ class Variant {
opac_);
}

PORTABLE_INLINE_FUNCTION RuntimePhysicalConstants
GetRuntimePhysicalConstants() const {
return mpark::visit(
[=](const auto &opac) {
using PC = typename std::decay_t<decltype(opac)>::PC;
return singularity::GetRuntimePhysicalConstants(PC());
},
opac_);
}

// Directional absorption coefficient with units of 1/length
// Signature should be at least
// rho, temp, Ye, type, nu, lambda
Expand Down
20 changes: 15 additions & 5 deletions singularity-opac/neutrinos/non_cgs_neutrinos.hpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// ======================================================================
// © 2021. Triad National Security, LLC. All rights reserved. This
// © 2021-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 @@ -29,6 +29,8 @@ namespace neutrinos {
template <typename Opac>
class NonCGSUnits {
public:
using PC = typename Opac::PC;

NonCGSUnits() = default;
NonCGSUnits(Opac &&opac, const Real time_unit, const Real mass_unit,
const Real length_unit, const Real temp_unit)
Expand All @@ -47,6 +49,7 @@ class NonCGSUnits {
return NonCGSUnits<Opac>(opac_.GetOnDevice(), time_unit_, mass_unit_,
length_unit_, temp_unit_);
}

inline void Finalize() noexcept { opac_.Finalize(); }

PORTABLE_INLINE_FUNCTION
Expand All @@ -66,10 +69,11 @@ class NonCGSUnits {
}

template <typename FrequencyIndexer, typename DataIndexer>
PORTABLE_INLINE_FUNCTION void AbsorptionCoefficient(
const Real rho, const Real temp, const Real Ye, RadiationType type,
FrequencyIndexer &nu_bins, DataIndexer &coeffs, const int nbins,
Real *lambda = nullptr) const {
PORTABLE_INLINE_FUNCTION void
AbsorptionCoefficient(const Real rho, const Real temp, const Real Ye,
RadiationType type, FrequencyIndexer &nu_bins,
DataIndexer &coeffs, const int nbins,
Real *lambda = nullptr) const {
for (int i = 0; i < nbins; ++i) {
nu_bins[i] *= freq_unit_;
}
Expand Down Expand Up @@ -262,6 +266,12 @@ class MeanNonCGSUnits {
PORTABLE_INLINE_FUNCTION
int nlambda() const noexcept { return mean_opac_.nlambda(); }

#ifdef SPINER_USE_HDF
void Save(const std::string &filename) const {
return mean_opac_.Save(filename);
}
#endif

PORTABLE_INLINE_FUNCTION
Real PlanckMeanAbsorptionCoefficient(const Real rho, const Real temp,
const Real Ye,
Expand Down
15 changes: 9 additions & 6 deletions singularity-opac/neutrinos/spiner_opac_neutrinos.hpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// ======================================================================
// © 2021. Triad National Security, LLC. All rights reserved. This
// © 2021-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 @@ -61,7 +61,9 @@ enum class DataStatus { Deallocated, OnDevice, OnHost };
template <typename ThermalDistribution, typename pc = PhysicalConstantsCGS>
class SpinerOpacity {
public:
using PC = pc;
using DataBox = Spiner::DataBox<Real>;

static constexpr Real EPS = 10.0 * std::numeric_limits<Real>::min();
static constexpr Real Hz2MeV = pc::h / (1e6 * pc::eV);
static constexpr Real MeV2Hz = 1 / Hz2MeV;
Expand Down Expand Up @@ -110,7 +112,8 @@ class SpinerOpacity {
Real J = std::max(opac.Emissivity(rho, T * MeV2K, Ye, type), 0.0);
Real lJ = toLog_(J);
lJ_(iRho, iT, iYe, idx) = lJ;
Real JYe = std::max(opac.NumberEmissivity(rho, T * MeV2K, Ye, type), 0.0);
Real JYe =
std::max(opac.NumberEmissivity(rho, T * MeV2K, Ye, type), 0.0);
lJYe_(iRho, iT, iYe, idx) = toLog_(JYe);
for (int ie = 0; ie < Ne; ++ie) {
Real lE = lalphanu_.range(0).x(ie);
Expand All @@ -119,8 +122,8 @@ class SpinerOpacity {
Real alpha = std::max(
opac.AbsorptionCoefficient(rho, T, Ye, type, nu), 0.0);
lalphanu_(iRho, iT, iYe, idx, ie) = toLog_(alpha);
Real j = std::max(opac.EmissivityPerNuOmega(rho, T * MeV2K, Ye, type, nu),
0.0);
Real j = std::max(
opac.EmissivityPerNuOmega(rho, T * MeV2K, Ye, type, nu), 0.0);
ljnu_(iRho, iT, iYe, idx, ie) = toLog_(j);
}
}
Expand All @@ -131,8 +134,8 @@ class SpinerOpacity {

// DataBox constructor. Note that this constructor *shallow* copies
// the databoxes, so they must be managed externally.
SpinerOpacity(const DataBox &lalphanu, const DataBox ljnu,
const DataBox lJ, const DataBox lJYe)
SpinerOpacity(const DataBox &lalphanu, const DataBox ljnu, const DataBox lJ,
const DataBox lJYe)
: memoryStatus_(impl::DataStatus::OnHost), lalphanu_(lalphanu),
ljnu_(ljnu), lJ_(lJ), lJYe_(lJYe) {}

Expand Down
5 changes: 4 additions & 1 deletion singularity-opac/neutrinos/tophat_emissivity_neutrinos.hpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// ======================================================================
// © 2021. Triad National Security, LLC. All rights reserved. This
// © 2021-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 @@ -32,6 +32,8 @@ namespace neutrinos {
template <typename ThermalDistribution, typename pc = PhysicalConstantsCGS>
class TophatEmissivity {
public:
using PC = pc;

TophatEmissivity(const Real C, const Real numin, const Real numax)
: C_(C), numin_(numin), numax_(numax) {}
TophatEmissivity(const ThermalDistribution &dist, const Real C,
Expand All @@ -46,6 +48,7 @@ class TophatEmissivity {
printf("Tophat emissivity. C, numin, numax = %g, %g, %g\n", C_, numin_,
numax_);
}

inline void Finalize() noexcept {}

PORTABLE_INLINE_FUNCTION
Expand Down
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 @@ -32,6 +32,8 @@ namespace photons {
template <typename pc = PhysicalConstantsCGS>
class EPBremsstrahlungOpacity {
public:
using PC = pc;

EPBremsstrahlungOpacity() = default;
EPBremsstrahlungOpacity(const PlanckDistribution<pc> &dist) : dist_(dist) {}

Expand All @@ -42,6 +44,10 @@ class EPBremsstrahlungOpacity {
void PrintParams() const noexcept {
printf("Electron-proton bremsstrahlung opacity.\n");
}

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

inline void Finalize() noexcept {}

PORTABLE_INLINE_FUNCTION
Expand Down
4 changes: 3 additions & 1 deletion singularity-opac/photons/gray_opacity_photons.hpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// ======================================================================
// © 2021. Triad National Security, LLC. All rights reserved. This
// © 2021-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 @@ -30,6 +30,8 @@ namespace photons {
template <typename pc = PhysicalConstantsCGS>
class GrayOpacity {
public:
using PC = pc;

GrayOpacity() = default;
GrayOpacity(const Real kappa) : kappa_(kappa) {}
GrayOpacity(const PlanckDistribution<pc> &dist, const Real kappa)
Expand Down
Loading
Loading