Skip to content

Commit

Permalink
fix spiner version
Browse files Browse the repository at this point in the history
  • Loading branch information
brryan committed Sep 25, 2024
1 parent cf7eb13 commit b195ff0
Show file tree
Hide file tree
Showing 3 changed files with 326 additions and 2 deletions.
3 changes: 2 additions & 1 deletion test/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,10 @@ PRIVATE
test_thermal_dist.cpp
test_scalefree_opacities.cpp
test_gray_opacities.cpp
test_epbremsstrahlung_opacities.cpp
test_gray_s_opacities.cpp
test_epbremsstrahlung_opacities.cpp
test_brt_opacities.cpp
test_powerlaw_opacities.cpp
test_thomson_s_opacities.cpp
test_chebyshev.cpp
test_spiner_opac_neutrinos.cpp
Expand Down
323 changes: 323 additions & 0 deletions test/test_powerlaw_opacities.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,323 @@
// ======================================================================
// © 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.
// Department of Energy/National Nuclear Security Administration. All
// rights in the program are reserved by Triad National Security, LLC,
// and the U.S. Department of Energy/National Nuclear Security
// Administration. The Government is granted for itself and others
// acting on its behalf a nonexclusive, paid-up, irrevocable worldwide
// license in this material to reproduce, prepare derivative works,
// distribute copies to the public, perform publicly and display
// publicly, and to permit others to do so.
// ======================================================================

#include <cmath>
#include <iostream>

#include <catch2/catch.hpp>

#include <ports-of-call/portability.hpp>
#include <ports-of-call/portable_arrays.hpp>
#include <spiner/databox.hpp>

#include <singularity-opac/base/indexers.hpp>
#include <singularity-opac/base/radiation_types.hpp>
#include <singularity-opac/chebyshev/chebyshev.hpp>
#include <singularity-opac/constants/constants.hpp>
#include <singularity-opac/neutrinos/opac_neutrinos.hpp>
#include <singularity-opac/photons/opac_photons.hpp>

using namespace singularity;

using pc = PhysicalConstantsCGS;
using DataBox = Spiner::DataBox<Real>;

#ifdef PORTABILITY_STRATEGY_KOKKOS
using atomic_view = Kokkos::MemoryTraits<Kokkos::Atomic>;
#endif

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);
}
constexpr Real EPS_TEST = 1e-3;

TEST_CASE("Power law photon opacities", "[PowerLawPhotonOpacities]") {
WHEN("We initialize a power law photon opacity") {
constexpr Real rho = 1.e0; // g/cc
constexpr Real temp = 1.e3; // K
constexpr Real nu = 1.e14; // Hz

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

THEN("The emissivity per nu omega is consistent with the emissity per nu") {
int n_wrong_h = 0;
#ifdef PORTABILITY_STRATEGY_KOKKOS
Kokkos::View<int, atomic_view> n_wrong_d("wrong");
#else
PortableMDArray<int> n_wrong_d(&n_wrong_h, 1);
#endif

portableFor(
"calc emissivities", 0, 100, PORTABLE_LAMBDA(const int &i) {
Real jnu = opac.EmissivityPerNuOmega(rho, temp, Ye, type, nu);
Real Jnu = opac.EmissivityPerNu(rho, temp, Ye, type, nu);
if (FractionalDifference(Jnu, 4 * M_PI * jnu) > EPS_TEST) {
n_wrong_d() += 1;
}
});

#ifdef PORTABILITY_STRATEGY_KOKKOS
Kokkos::deep_copy(n_wrong_h, n_wrong_d);
#endif
REQUIRE(n_wrong_h == 0);
}

WHEN("We create a gray opacity object in non-cgs units") {
constexpr Real time_unit = 123;
constexpr Real mass_unit = 456;
constexpr Real length_unit = 789;
constexpr Real temp_unit = 276;
constexpr Real rho_unit =
mass_unit / (length_unit * length_unit * length_unit);
constexpr Real j_unit = mass_unit / (length_unit * time_unit * time_unit);
neutrinos::Opacity funny_units_host =
neutrinos::NonCGSUnits<neutrinos::Gray>(
neutrinos::Gray(1), time_unit, mass_unit, length_unit, temp_unit);
auto funny_units = funny_units_host.GetOnDevice();

THEN("We can convert meaningfully into and out of funny units") {
int n_wrong_h = 0;
#ifdef PORTABILITY_STRATEGY_KOKKOS
Kokkos::View<int, atomic_view> n_wrong_d("wrong");
#else
PortableMDArray<int> n_wrong_d(&n_wrong_h, 1);
#endif

portableFor(
"emissivities in funny units", 0, 100,
PORTABLE_LAMBDA(const int &i) {
Real jnu_funny = funny_units.EmissivityPerNuOmega(
rho / rho_unit, temp / temp_unit, Ye, type, nu * time_unit);
Real jnu = opac.EmissivityPerNuOmega(rho, temp, Ye, type, nu);
if (FractionalDifference(jnu, jnu_funny * j_unit) > EPS_TEST) {
n_wrong_d() += 1;
}
});
#ifdef PORTABILITY_STRATEGY_KOKKOS
Kokkos::deep_copy(n_wrong_h, n_wrong_d);
#endif
REQUIRE(n_wrong_h == 0);
}
}

THEN("We can fill an indexer allocated in a cell") {
int n_wrong_h = 0;
#ifdef PORTABILITY_STRATEGY_KOKKOS
Kokkos::View<int, atomic_view> n_wrong_d("wrong");
#else
PortableMDArray<int> n_wrong_d(&n_wrong_h, 1);
#endif
constexpr int nbins = 9;
constexpr int ntemps = 100;

Real lnu_min = 0 + std::log10(MeV2Hz);
Real lnu_max = 1 + std::log10(MeV2Hz);
Real nu_min = std::pow(10, lnu_min);
Real nu_max = std::pow(10, lnu_max);

constexpr Real lt_min = -1;
constexpr Real lt_max = 2;
Real dt = (lt_max - lt_min) / (Real)(ntemps - 1);

Real *nu_bins = (Real *)PORTABLE_MALLOC(nbins * sizeof(Real));
Real *lnu_bins = (Real *)PORTABLE_MALLOC(nbins * sizeof(Real));
Real *temp_bins = (Real *)PORTABLE_MALLOC(ntemps * sizeof(Real));
portableFor(
"set nu bins", 0, 1, PORTABLE_LAMBDA(const int &i) {
chebyshev::GetPoints(lnu_min, lnu_max, nbins, lnu_bins);
for (int j = 0; j < nbins; ++j) {
nu_bins[j] = std::pow(10, lnu_bins[j]);
}
});
portableFor(
"set temp bins", 0, ntemps, PORTABLE_LAMBDA(const int &i) {
temp_bins[i] = std::pow(10, lt_min + dt * i) * MeV2K;
});

Real *vm9 = (Real *)PORTABLE_MALLOC(9 * 9 * sizeof(Real));
portableFor(
"Fill vm", 0, 1,
PORTABLE_LAMBDA(const int &i) { chebyshev::get_vmbox(vm9); });

portableFor(
"Fill the indexers", 0, ntemps, PORTABLE_LAMBDA(const int &i) {
Real temp = temp_bins[i];

Real *nu_data = (Real *)malloc(nbins * sizeof(Real));
Real *lnu_data = (Real *)malloc(nbins * sizeof(Real));
Real *nu_coeffs = (Real *)malloc(nbins * sizeof(Real));
indexers::LogCheb<nbins, Real *> J_cheb(nu_data, lnu_data,
nu_coeffs, nu_min, nu_max);
opac.EmissivityPerNu(rho, temp, Ye, type, nu_bins, J_cheb, nbins);
Real Jtrue = opac.EmissivityPerNu(rho, temp, Ye, type, nu);
J_cheb.SetInterpCoeffs(DataBox(vm9, 9, 9));
if (std::isnan(J_cheb(nu)) ||
((std::abs(Jtrue) >= 1e-14 || J_cheb(nu) >= 1e-14) &&
FractionalDifference(J_cheb(nu), Jtrue) > EPS_TEST)) {
n_wrong_d() += 1;
}
free(nu_data);
free(lnu_data);
free(nu_coeffs);
});

#ifdef PORTABILITY_STRATEGY_KOKKOS
Kokkos::deep_copy(n_wrong_h, n_wrong_d);
#endif

REQUIRE(n_wrong_h == 0);

PORTABLE_FREE(vm9);
PORTABLE_FREE(nu_bins);
PORTABLE_FREE(lnu_bins);
PORTABLE_FREE(temp_bins);
}

opac.Finalize();
}
}

TEST_CASE("Gray photon opacities", "[GrayPhotons]") {
WHEN("We initialize a gray photon opacity") {
constexpr Real rho = 1e3; // g/cc.
constexpr Real temp = 1e5; // Kelvin.
constexpr Real nu = 3e9; // Hz. UHF microwave

photons::Opacity opac_host = photons::Gray(1);
photons::Opacity opac = opac_host.GetOnDevice();
THEN("The emissivity per nu omega is consistent with the emissity per nu") {
int n_wrong_h = 0;
#ifdef PORTABILITY_STRATEGY_KOKKOS
Kokkos::View<int, atomic_view> n_wrong_d("wrong");
#else
PortableMDArray<int> n_wrong_d(&n_wrong_h, 1);
#endif
portableFor(
"calc emissivities", 0, 100, PORTABLE_LAMBDA(const int &i) {
Real jnu = opac.EmissivityPerNuOmega(rho, temp, nu);
Real Jnu = opac.EmissivityPerNu(rho, temp, nu);
if (FractionalDifference(Jnu, 4 * M_PI * jnu) > EPS_TEST) {
n_wrong_d() += 1;
}
});
#ifdef PORTABILITY_STRATEGY_KOKKOS
Kokkos::deep_copy(n_wrong_h, n_wrong_d);
#endif
REQUIRE(n_wrong_h == 0);
}

WHEN("We create a gray opacity object in non-cgs units") {
constexpr Real time_unit = 123;
constexpr Real mass_unit = 456;
constexpr Real length_unit = 789;
constexpr Real temp_unit = 276;
constexpr Real rho_unit =
mass_unit / (length_unit * length_unit * length_unit);
constexpr Real j_unit = mass_unit / (length_unit * time_unit * time_unit);
photons::Opacity funny_units_host = photons::NonCGSUnits<photons::Gray>(
photons::Gray(1), time_unit, mass_unit, length_unit, temp_unit);
auto funny_units = funny_units_host.GetOnDevice();

THEN("We can convert meaningfully into and out of funny units") {
int n_wrong_h = 0;
#ifdef PORTABILITY_STRATEGY_KOKKOS
Kokkos::View<int, atomic_view> n_wrong_d("wrong");
#else
PortableMDArray<int> n_wrong_d(&n_wrong_h, 1);
#endif

portableFor(
"emissivities in funny units", 0, 100,
PORTABLE_LAMBDA(const int &i) {
Real jnu_funny = funny_units.EmissivityPerNuOmega(
rho / rho_unit, temp / temp_unit, nu * time_unit);
Real jnu = opac.EmissivityPerNuOmega(rho, temp, nu);
if (FractionalDifference(jnu, jnu_funny * j_unit) > EPS_TEST) {
n_wrong_d() += 1;
}
});
#ifdef PORTABILITY_STRATEGY_KOKKOS
Kokkos::deep_copy(n_wrong_h, n_wrong_d);
#endif
REQUIRE(n_wrong_h == 0);
}
}

THEN("We can fill an indexer allocated in a cell") {
int n_wrong_h = 0;
#ifdef PORTABILITY_STRATEGY_KOKKOS
Kokkos::View<int, atomic_view> n_wrong_d("wrong");
#else
PortableMDArray<int> n_wrong_d(&n_wrong_h, 1);
#endif
constexpr int nbins = 100;
constexpr int ntemps = 100;

constexpr Real lnu_min = 8;
constexpr Real lnu_max = 10;
Real nu_min = std::pow(10, lnu_min);
Real nu_max = std::pow(10, lnu_max);
Real dnu = (lnu_max - lnu_min) / (Real)(nbins - 1);

constexpr Real lt_min = 2;
constexpr Real lt_max = 4;
Real dt = (lt_max - lt_min) / (Real)(ntemps - 1);

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);

portableFor(
"set nu bins", 0, nbins, PORTABLE_LAMBDA(const int &i) {
nu_bins[i] = std::pow(10, lnu_min + dnu * i);
});
portableFor(
"set temp bins", 0, ntemps, PORTABLE_LAMBDA(const int &i) {
temp_bins[i] = std::pow(10, lt_min + dt * i);
});

portableFor(
"Fill the indexers", 0, ntemps, PORTABLE_LAMBDA(const int &i) {
Real temp = temp_bins[i];
auto bins = loglin_bins.slice(i);
indexers::LogLinear J_log(bins, nu_min, nu_max, nbins);
opac.EmissivityPerNu(rho, temp, nu_bins, J_log, nbins);
Real Jtrue = opac.EmissivityPerNu(rho, temp, nu);
if (FractionalDifference(Jtrue, J_log(nu)) > EPS_TEST) {
n_wrong_d() += 1;
}
});

#ifdef PORTABILITY_STRATEGY_KOKKOS
Kokkos::deep_copy(n_wrong_h, n_wrong_d);
#endif
REQUIRE(n_wrong_h == 0);

PORTABLE_FREE(nu_bins);
PORTABLE_FREE(temp_bins);
free(loglin_bins);
}

opac.Finalize();
}
}

TEST_CASE("Tophat Opacities", "[TopHat]") {
WHEN("We initialize a tophat neutrino opacity") {
neutrinos::Opacity opac = neutrinos::Tophat(1, 1e-2, 1e2);
}
}
2 changes: 1 addition & 1 deletion utils/spiner
Submodule spiner updated 54 files
+2 −1 .github/PULL_REQUEST_TEMPLATE.md
+46 −0 .github/workflows/deps.yml
+40 −0 .github/workflows/docs.yml
+24 −0 .github/workflows/formatting.yml
+36 −0 .github/workflows/install.yml
+11 −6 .github/workflows/tests.yml
+5 −0 .gitignore
+157 −0 .gitlab-ci.yml
+5 −0 .gitlab-ci/config/spack/upstreams.yaml
+3 −4 .gitmodules
+213 −0 CMakeLists.txt
+0 −1 Catch2
+93 −32 README.md
+66 −0 cmake/Format.cmake
+232 −0 cmake/content.cmake
+22 −0 cmake/spinerConfig.cmake.in
+10 −0 doc/index.html
+1 −0 doc/sphinx/.gitignore
+27 −0 doc/sphinx/Makefile
+3 −0 doc/sphinx/README
+1 −0 doc/sphinx/_static/placeholder
+27 −0 doc/sphinx/_templates/versions.html
+57 −0 doc/sphinx/conf.py
+68 −0 doc/sphinx/index.rst
+35 −0 doc/sphinx/make.bat
+93 −0 doc/sphinx/src/building.rst
+503 −0 doc/sphinx/src/databox.rst
+54 −0 doc/sphinx/src/getting-started.rst
+178 −0 doc/sphinx/src/interpolation.rst
+102 −0 doc/sphinx/src/sphinx-howto.rst
+70 −0 doc/sphinx/src/statement-of-need.rst
+ figs/spiner_interpolation_benchmark.png
+1 −0 installtest/.gitignore
+43 −0 installtest/CMakeLists.txt
+7 −0 installtest/libtest.cpp
+0 −79 ports-of-call/README.md
+0 −232 ports-of-call/portability.hpp
+0 −492 ports-of-call/portable_arrays.hpp
+50 −0 spack-repo/packages/ports-of-call/package.py
+106 −0 spack-repo/packages/spiner/package.py
+6 −0 spack-repo/repo.yaml
+206 −162 spiner/databox.hpp
+21 −0 spiner/interpolation.hpp
+221 −0 spiner/piecewise_grid_1d.hpp
+57 −34 spiner/regular_grid_1d.hpp
+5 −0 spiner/sp5.hpp
+0 −0 spiner/spiner_types.hpp
+112 −0 test/CMakeLists.txt
+0 −71 test/Makefile
+0 −78 test/Makefile.kokkos
+135 −0 test/benchmark.cpp
+8 −7 test/convergence.cpp
+7 −9 test/plot_convergence.py
+196 −22 test/test.cpp

0 comments on commit b195ff0

Please sign in to comment.