Skip to content

Commit

Permalink
add constexpr function for generating nullptr of specific pointer type
Browse files Browse the repository at this point in the history
  • Loading branch information
jonahm-LANL committed Apr 25, 2024
1 parent 32bcf9c commit f594ee1
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 6 deletions.
10 changes: 6 additions & 4 deletions python/module.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
// clang-format off
#include <pybind11/pybind11.h>
#include <pybind11/numpy.h>
#include <singularity-eos/base/variadic_utils.hpp>
#include <singularity-eos/eos/eos.hpp>
#include <map>
#include <string>
Expand All @@ -24,6 +25,7 @@

namespace py = pybind11;
using namespace singularity;
using singularity::variadic_utils::np;

// Helper function to convert lambda numpy array to double* buffer
// With std::optional we would add support for a default value of lambda=None
Expand All @@ -34,7 +36,7 @@ Real two_params(const T& self, const Real a, const Real b, py::array_t<Real> lam

template<typename T, PORTABLE_FUNCTION Real(T::*Func)(const Real, const Real, Real*&&) const>
Real two_params_no_lambda(const T& self, const Real a, const Real b) {
return (self.*Func)(a, b, static_cast<Real*>(nullptr));
return (self.*Func)(a, b, np<Real>());
}

class LambdaHelper {
Expand Down Expand Up @@ -326,7 +328,7 @@ py::class_<T> eos_class(py::module_ & m, std::string name) {
auto lambda = kwargs["lmbda"].cast<py::array_t<Real>>();
self.FillEos(s.density, s.temperature, s.specific_internal_energy, s.pressure, s.specific_heat, s.bulk_modulus, output, lambda.mutable_data());
} else {
self.FillEos(s.density, s.temperature, s.specific_internal_energy, s.pressure, s.specific_heat, s.bulk_modulus, output, static_cast<Real*>(nullptr));
self.FillEos(s.density, s.temperature, s.specific_internal_energy, s.pressure, s.specific_heat, s.bulk_modulus, output, np<Real>());
}
return s;
})
Expand All @@ -337,7 +339,7 @@ py::class_<T> eos_class(py::module_ & m, std::string name) {
})
.def("ValuesAtReferenceState", [](const T & self){
EOSState s;
self.ValuesAtReferenceState(s.density, s.temperature, s.specific_internal_energy, s.pressure, s.specific_heat, s.bulk_modulus, s.dpde, s.dvdt, static_cast<Real*>(nullptr));
self.ValuesAtReferenceState(s.density, s.temperature, s.specific_internal_energy, s.pressure, s.specific_heat, s.bulk_modulus, s.dpde, s.dvdt, np<Real>());
return s;
})

Expand All @@ -354,7 +356,7 @@ py::class_<T> eos_class(py::module_ & m, std::string name) {
}, py::arg("press"), py::arg("temp"), py::arg("lmbda"))
.def("DensityEnergyFromPressureTemperature", [](const T & self, const Real press, const Real temp) {
Real rho, sie;
self.DensityEnergyFromPressureTemperature(press, temp, static_cast<Real*>(nullptr), rho, sie);
self.DensityEnergyFromPressureTemperature(press, temp, np<Real>(), rho, sie);
return std::pair<Real, Real>(rho, sie);
}, py::arg("press"), py::arg("temp"))
.def("Finalize", &T::Finalize)
Expand Down
6 changes: 6 additions & 0 deletions singularity-eos/base/variadic_utils.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,12 @@ namespace variadic_utils {
// Some generic variatic utilities
// ======================================================================

// Useful for generating nullptr of a specific pointer type
template <typename T>
inline constexpr T *np() {
return nullptr;
}

// C++14 implementation of std::remove_cvref (available since C++20)
// credit to CJ + Diego
template <typename T>
Expand Down
5 changes: 3 additions & 2 deletions test/test_eos_tabulated.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
#include <ports-of-call/portability.hpp>
#include <ports-of-call/portable_arrays.hpp>
#include <ports-of-call/portable_errors.hpp>
#include <singularity-eos/base/variadic_utils.hpp>
#include <singularity-eos/eos/eos.hpp>

#ifndef CATCH_CONFIG_FAST_COMPILE
Expand All @@ -40,6 +41,7 @@ using singularity::EOSPAC;
#endif

namespace thermalqs = singularity::thermalqs;
using singularity::variadic_utils::np;

const std::string eosName = "../materials.sp5";
const std::string airName = "air";
Expand Down Expand Up @@ -113,8 +115,7 @@ SCENARIO("SpinerEOS depends on Rho and T", "[SpinerEOS],[DependsRhoT][EOSPAC]")
std::vector<Real> lambda(steelEOS_host_polymorphic.nlambda());
steelEOS_host_polymorphic.DensityEnergyFromPressureTemperature(
P, T, lambda.data(), rho, sie);
eospac.DensityEnergyFromPressureTemperature(P, T, static_cast<Real *>(nullptr),
rho_pac, sie_pac);
eospac.DensityEnergyFromPressureTemperature(P, T, np<Real>(), rho_pac, sie_pac);
REQUIRE(isClose(rho, rho_pac));
}
}
Expand Down

0 comments on commit f594ee1

Please sign in to comment.