Skip to content

Commit

Permalink
variant cant deduce template argument for different types across vari…
Browse files Browse the repository at this point in the history
…ants
  • Loading branch information
brryan committed Sep 26, 2024
1 parent eddf3b3 commit 6d5323c
Show file tree
Hide file tree
Showing 15 changed files with 104 additions and 28 deletions.
4 changes: 4 additions & 0 deletions singularity-opac/neutrinos/brt_neutrinos.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,10 @@ class BRTOpacity {
void PrintParams() const noexcept {
printf("Burrows-Reddy-Thompson analytic neutrino opacity.\n");
}

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

inline void Finalize() noexcept {}

PORTABLE_INLINE_FUNCTION
Expand Down
9 changes: 2 additions & 7 deletions 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 CONSTANTS, typename... Opacs>
template <typename PC, typename... Opacs>
class MeanVariant {
private:
opac_variant<Opacs...> opac_;
Expand Down Expand Up @@ -88,15 +88,10 @@ class MeanVariant {
opac_);
}

PORTABLE_INLINE_FUNCTION CONSTANTS GetPhysicalConstants() const {
PORTABLE_INLINE_FUNCTION PC 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
1 change: 0 additions & 1 deletion singularity-opac/neutrinos/mean_opacity_neutrinos.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,6 @@ class MeanOpacity {

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

void Finalize() {
lkappaPlanck_.finalize();
Expand Down
6 changes: 6 additions & 0 deletions singularity-opac/neutrinos/neutrino_variant.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -290,6 +290,12 @@ class Variant {
opac_);
}

template <typename T>
PORTABLE_INLINE_FUNCTION T 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
15 changes: 11 additions & 4 deletions singularity-opac/neutrinos/non_cgs_neutrinos.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,12 @@ class NonCGSUnits {
return NonCGSUnits<Opac>(opac_.GetOnDevice(), time_unit_, mass_unit_,
length_unit_, temp_unit_);
}

template <typename T>
PORTABLE_INLINE_FUNCTION T GetPhysicalConstants() const {
return opac_.GetPhysicalConstants();
}

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

PORTABLE_INLINE_FUNCTION
Expand All @@ -66,10 +72,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
4 changes: 4 additions & 0 deletions singularity-opac/neutrinos/tophat_emissivity_neutrinos.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,10 @@ class TophatEmissivity {
printf("Tophat emissivity. C, numin, numax = %g, %g, %g\n", C_, numin_,
numax_);
}

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

inline void Finalize() noexcept {}

PORTABLE_INLINE_FUNCTION
Expand Down
4 changes: 4 additions & 0 deletions singularity-opac/photons/epbremsstrahlung_opacity_photons.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,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
7 changes: 4 additions & 3 deletions singularity-opac/photons/gray_opacity_photons.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,10 @@ class GrayOpacity {
void PrintParams() const noexcept {
printf("Gray opacity. kappa = %g\n", kappa_);
}

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

inline void Finalize() noexcept {}

PORTABLE_INLINE_FUNCTION
Expand Down Expand Up @@ -165,9 +169,6 @@ 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
2 changes: 1 addition & 1 deletion singularity-opac/photons/mean_opacity_photons.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
6 changes: 3 additions & 3 deletions singularity-opac/photons/mean_photon_variant.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 @@ -28,7 +28,7 @@ namespace singularity {
namespace photons {
namespace impl {

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

PORTABLE_INLINE_FUNCTION CONSTANTS GetPhysicalConstants() const {
PORTABLE_INLINE_FUNCTION PC GetPhysicalConstants() const {
return mpark::visit([](auto &opac) { return opac.GetPhysicalConstants(); },
opac_);
}
Expand Down
12 changes: 9 additions & 3 deletions singularity-opac/photons/non_cgs_photons.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -65,9 +65,10 @@ class NonCGSUnits {
}

template <typename FrequencyIndexer, typename DataIndexer>
PORTABLE_INLINE_FUNCTION void AbsorptionCoefficient(
const Real rho, const Real temp, FrequencyIndexer &nu_bins,
DataIndexer &coeffs, const int nbins, Real *lambda = nullptr) const {
PORTABLE_INLINE_FUNCTION void
AbsorptionCoefficient(const Real rho, const Real temp,
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 @@ -219,6 +220,11 @@ class NonCGSUnits {
return NoH * mass_unit_ / rho_unit_;
}

template <typename T>
PORTABLE_INLINE_FUNCTION T GetPhysicalConstants() const {
return opac_.GetPhysicalConstants();
}

private:
Opac opac_;
Real time_unit_, mass_unit_, length_unit_, temp_unit_;
Expand Down
5 changes: 3 additions & 2 deletions 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 CONSTANTS, typename... Opacs>
template <typename... Opacs>
class Variant {
private:
opac_variant<Opacs...> opac_;
Expand Down Expand Up @@ -275,7 +275,8 @@ class Variant {
opac_);
}

PORTABLE_INLINE_FUNCTION CONSTANTS GetPhysicalConstants() const {
template <typename T>
PORTABLE_INLINE_FUNCTION T GetPhysicalConstants() const {
return mpark::visit([](auto &opac) { return opac.GetPhysicalConstants(); },
opac_);
}
Expand Down
4 changes: 4 additions & 0 deletions singularity-opac/photons/powerlaw_opacity_photons.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,10 @@ class PowerLawOpacity {
printf("Power law opacity. kappa0 = %g rho_exp = %g temp_exp = %g\n",
kappa0_, rho_exp_, temp_exp_);
}

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

inline void Finalize() noexcept {}

PORTABLE_INLINE_FUNCTION
Expand Down
36 changes: 33 additions & 3 deletions test/test_gray_opacities.cpp
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 @@ -57,8 +57,21 @@ 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);
// Check constants from mean opacity
THEN("Check constants from mean opacity for consistency") {
auto constants = 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;
Expand Down Expand Up @@ -206,6 +219,23 @@ TEST_CASE("Gray photon opacities", "[GrayPhotons]") {

photons::Opacity opac_host = photons::Gray(1);
photons::Opacity opac = opac_host.GetOnDevice();

// Check constants from mean opacity
THEN("Check constants from mean opacity for consistency") {
auto constants = 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
17 changes: 16 additions & 1 deletion test/test_mean_opacities.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -268,7 +268,6 @@ TEST_CASE("Mean neutrino scattering opacities", "[MeanNeutrinosS]") {

neutrinos::MeanSOpacityCGS 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();

THEN("The emissivity per nu omega is consistent with the emissity per nu") {
Expand Down Expand Up @@ -433,6 +432,22 @@ TEST_CASE("Mean photon opacities", "[MeanPhotons]") {
lTMin, lTMax, NT);
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

0 comments on commit 6d5323c

Please sign in to comment.