diff --git a/CHANGELOG.md b/CHANGELOG.md index fa1a313868d..d98fecd3d5d 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -8,6 +8,7 @@ - [[PR659]](https://github.com/lanl/singularity-eos/pull/659) Added `sesame2spiner::saveAllMaterials` overloads taking a list of matids instead of a list of input files, with optional per-material `Params` overrides, so host codes can generate an sp5 file without writing input decks to disk. Also added an overload taking an already-open `hid_t`, plus `writeSP5RootAttributes`, so a single sp5 file can be built up one material at a time. ### Fixed (Repair bugs, etc) +- [[PR667]](https://github.com/lanl/singularity-eos/pull/667) Various fixes to shift/unit/scale EOS modifiers. - [[PR666]](https://github.com/lanl/singularity-eos/pull/666) Add missing "template" to cast calls for compiler compatibility - [[PR661]](https://github.com/lanl/singularity-eos/pull/661) Fix bug in InternalEnergyFromDensityPressure where the lambda wasn't threaded all the way through. - `sesame2spiner` now validates the metadata returned by `eosGetMetadata` before using it. A matid absent from the sesame file previously produced all-zero bounds with no error, which reached `log()` and generated NaN grids; it is now reported and skipped. diff --git a/singularity-eos/eos/modifiers/eos_unitsystem.hpp b/singularity-eos/eos/modifiers/eos_unitsystem.hpp index 0897bbac47b..729842d574a 100644 --- a/singularity-eos/eos/modifiers/eos_unitsystem.hpp +++ b/singularity-eos/eos/modifiers/eos_unitsystem.hpp @@ -328,7 +328,7 @@ class UnitSystem : public EosBase> { transform.y.apply(sie_unit_); transform.f.apply(inv_press_unit_);) SG_MODIFIER_FORWARD_1IN_1OUT(MinInternalEnergyFromDensity, transform.x.apply(rho_unit_); - transform.f.apply(sie_unit_);) + transform.f.apply(inv_sie_unit_);) SG_MODIFIER_FORWARD_2IN_1OUT(SpecificHeatFromDensityTemperature, transform.x.apply(rho_unit_); transform.y.apply(temp_unit_); diff --git a/singularity-eos/eos/modifiers/scaled_eos.hpp b/singularity-eos/eos/modifiers/scaled_eos.hpp index 50d59f8d79b..f9c6055a193 100644 --- a/singularity-eos/eos/modifiers/scaled_eos.hpp +++ b/singularity-eos/eos/modifiers/scaled_eos.hpp @@ -80,7 +80,8 @@ class ScaledEOS : public EosBase> { template PORTABLE_FUNCTION Real InternalEnergyFromDensityTemperature( const Real rho, const Real temperature, Indexer_t &&lambda = nullptr) const { - Real energy = t_.InternalEnergyFromDensityTemperature(rho, temperature, lambda); + Real energy = + t_.InternalEnergyFromDensityTemperature(scale_ * rho, temperature, lambda); return scale_ * energy; } template @@ -91,7 +92,7 @@ class ScaledEOS : public EosBase> { template PORTABLE_FUNCTION Real MinInternalEnergyFromDensity(const Real rho, Indexer_t &&lambda = nullptr) const { - return t_.MinInternalEnergyFromDensity(scale_ * rho, lambda); + return scale_ * t_.MinInternalEnergyFromDensity(scale_ * rho, lambda); } template PORTABLE_FUNCTION Real EntropyFromDensityInternalEnergy( @@ -102,8 +103,8 @@ class ScaledEOS : public EosBase> { template PORTABLE_FUNCTION Real SpecificHeatFromDensityInternalEnergy( const Real rho, const Real sie, Indexer_t &&lambda = nullptr) const { - return t_.SpecificHeatFromDensityInternalEnergy(scale_ * rho, inv_scale_ * sie, - lambda); + return scale_ * t_.SpecificHeatFromDensityInternalEnergy(scale_ * rho, + inv_scale_ * sie, lambda); } template PORTABLE_FUNCTION Real BulkModulusFromDensityInternalEnergy( @@ -130,7 +131,8 @@ class ScaledEOS : public EosBase> { template PORTABLE_FUNCTION Real SpecificHeatFromDensityTemperature( const Real rho, const Real temperature, Indexer_t &&lambda = nullptr) const { - return t_.SpecificHeatFromDensityTemperature(scale_ * rho, temperature, lambda); + return scale_ * + t_.SpecificHeatFromDensityTemperature(scale_ * rho, temperature, lambda); } template PORTABLE_FUNCTION Real BulkModulusFromDensityTemperature( @@ -146,7 +148,9 @@ class ScaledEOS : public EosBase> { PORTABLE_FUNCTION void InternalEnergyFromDensityPressure(const Real rho, const Real P, Real &sie, Indexer_t &&lambda = nullptr) const { + sie *= inv_scale_; t_.InternalEnergyFromDensityPressure(scale_ * rho, P, sie, lambda); + sie *= scale_; } template @@ -166,21 +170,14 @@ class ScaledEOS : public EosBase> { PORTABLE_FUNCTION void FillEos(Real &rho, Real &temp, Real &energy, Real &press, Real &cv, Real &bmod, const unsigned long output, Indexer_t &&lambda = nullptr) const { - Real srho, senergy; - switch (t_.PreferredInput()) { - case thermalqs::density | thermalqs::temperature: - srho = scale_ * rho; - t_.FillEos(srho, temp, energy, press, cv, bmod, output, lambda); - energy = scale_ * energy; - break; - case thermalqs::density | thermalqs::specific_internal_energy: - srho = scale_ * rho; - senergy = inv_scale_ * energy; - t_.FillEos(srho, temp, senergy, press, cv, bmod, output, lambda); - break; - default: - EOS_ERROR("Didn't find a valid input for ScaledEOS::FillEOS\n"); - } + Real srho = (output & thermalqs::density) ? 0 : scale_ * rho; + Real senergy = + (output & thermalqs::specific_internal_energy) ? 0 : inv_scale_ * energy; + Real scv = 0; + t_.FillEos(srho, temp, senergy, press, scv, bmod, output, lambda); + if (output & thermalqs::density) rho = inv_scale_ * srho; + if (output & thermalqs::specific_internal_energy) energy = scale_ * senergy; + if (output & thermalqs::specific_heat) cv = scale_ * scv; } template @@ -191,6 +188,9 @@ class ScaledEOS : public EosBase> { t_.ValuesAtReferenceState(rho, temp, sie, press, cv, bmod, dpde, dvdt, lambda); rho *= inv_scale_; sie *= scale_; + cv *= scale_; + dpde *= inv_scale_; + dvdt *= scale_; } // vector implementations @@ -201,12 +201,14 @@ class ScaledEOS : public EosBase> { SG_MODIFIER_FORWARD_2IN_1OUT(PressureFromDensityInternalEnergy, transform.x.apply(scale_); transform.y.apply(inv_scale_);) - SG_MODIFIER_FORWARD_1IN_1OUT(MinInternalEnergyFromDensity, transform.x.apply(scale_);) + SG_MODIFIER_FORWARD_1IN_1OUT(MinInternalEnergyFromDensity, transform.x.apply(scale_); + transform.f.apply(scale_);) SG_MODIFIER_FORWARD_2IN_1OUT(SpecificHeatFromDensityTemperature, - transform.x.apply(scale_);) + transform.x.apply(scale_); + transform.f.apply(scale_);) SG_MODIFIER_FORWARD_2IN_1OUT(SpecificHeatFromDensityInternalEnergy, transform.x.apply(scale_); - transform.y.apply(inv_scale_);) + transform.y.apply(inv_scale_); transform.f.apply(scale_);) SG_MODIFIER_FORWARD_2IN_1OUT(BulkModulusFromDensityTemperature, transform.x.apply(scale_);) SG_MODIFIER_FORWARD_2IN_1OUT(BulkModulusFromDensityInternalEnergy, @@ -218,6 +220,7 @@ class ScaledEOS : public EosBase> { transform.x.apply(scale_); transform.y.apply(inv_scale_);) SG_MODIFIER_FORWARD_2IN_1OUT(InternalEnergyFromDensityTemperature, + transform.x.apply(scale_); transform.f.apply(scale_);) SG_MODIFIER_FORWARD_2IN_1OUT(EntropyFromDensityTemperature, transform.x.apply(scale_); transform.f.apply(scale_);) diff --git a/singularity-eos/eos/modifiers/shifted_eos.hpp b/singularity-eos/eos/modifiers/shifted_eos.hpp index 20071eae733..13f82120925 100644 --- a/singularity-eos/eos/modifiers/shifted_eos.hpp +++ b/singularity-eos/eos/modifiers/shifted_eos.hpp @@ -151,42 +151,33 @@ class ShiftedEOS : public EosBase> { PORTABLE_FUNCTION void FillEos(Real &rho, Real &temp, Real &energy, Real &press, Real &cv, Real &bmod, const unsigned long output, Indexer_t &&lambda = nullptr) const { - Real senergy; - switch (t_.PreferredInput()) { - case thermalqs::density | thermalqs::temperature: - t_.FillEos(rho, temp, energy, press, cv, bmod, output, lambda); - energy = energy + shift_; - break; - case thermalqs::density | thermalqs::specific_internal_energy: - senergy = energy - shift_; - t_.FillEos(rho, temp, senergy, press, cv, bmod, output, lambda); - break; - default: - EOS_ERROR("Didn't find a valid input for ShiftedEOS::FillEOS\n"); - } + Real senergy = (output & thermalqs::specific_internal_energy) ? 0 : energy - shift_; + t_.FillEos(rho, temp, senergy, press, cv, bmod, output, lambda); + if (output & thermalqs::specific_internal_energy) energy = senergy + shift_; } // vector implementations template >> - inline void shift_sies(const Space &s, const Real *sies, Real *shifted, - const int num) const { + inline void shift_sies(const Space &s, const Real *sies, Real *shifted, const int num, + const Real input_scale = 1) const { static auto const name = singularity::mfuncname::member_func_name(typeid(ShiftedEOS).name(), __func__); static auto const cname = name.c_str(); const auto shift_val = shift_; portableFor( cname, s, 0, num, - PORTABLE_LAMBDA(const int i) { shifted[i] = sies[i] - shift_val; }); + PORTABLE_LAMBDA(const int i) { shifted[i] = input_scale * sies[i] - shift_val; }); } template >> - inline void unshift_sies(const Space &s, Real *sies, const int num) const { + inline void unshift_sies(const Space &s, Real *sies, const int num, + const Real output_scale = 1) const { static auto const name = singularity::mfuncname::member_func_name(typeid(ShiftedEOS).name(), __func__); static auto const cname = name.c_str(); - const auto shift_val = shift_; + const auto shift_val = output_scale * shift_; portableFor( cname, s, 0, num, PORTABLE_LAMBDA(const int i) { sies[i] += shift_val; }); } @@ -200,7 +191,10 @@ class ShiftedEOS : public EosBase> { LambdaIndexer &&lambdas, Transform &&transform = Transform()) const { Real *shifted_sies = scratch; - shift_sies(s, sies, shifted_sies, num); + // Apply an outer modifier's energy conversion before subtracting this shift. + const Real input_scale = transform.y.is_set() ? transform.y.get() : Real(1); + shift_sies(s, sies, shifted_sies, num, input_scale); + transform.y.clear(); t_.TemperatureFromDensityInternalEnergy( s, rhos, shifted_sies, temperatures, &scratch[num], num, std::forward(lambdas), std::forward(transform)); @@ -226,7 +220,10 @@ class ShiftedEOS : public EosBase> { const Space &s, const Real *rhos, const Real *sies, Real *pressures, Real *scratch, const int num, LambdaIndexer &&lambdas, Transform &&transform = Transform()) const { Real *shifted_sies = scratch; - shift_sies(s, sies, shifted_sies, num); + // Apply an outer modifier's energy conversion before subtracting this shift. + const Real input_scale = transform.y.is_set() ? transform.y.get() : Real(1); + shift_sies(s, sies, shifted_sies, num, input_scale); + transform.y.clear(); t_.PressureFromDensityInternalEnergy(s, rhos, shifted_sies, pressures, &scratch[num], num, std::forward(lambdas), std::forward(transform)); @@ -239,10 +236,11 @@ class ShiftedEOS : public EosBase> { Real *scratch, const int num, LambdaIndexer &&lambdas, Transform &&transform = Transform()) const { - t_.MinInternalEnergyFromDensity(s, rhos, sies, &scratch[num], num, + const Real output_scale = transform.f.is_set() ? transform.f.get() : Real(1); + t_.MinInternalEnergyFromDensity(s, rhos, sies, scratch, num, std::forward(lambdas), std::forward(transform)); - unshift_sies(s, sies, num); + unshift_sies(s, sies, num, output_scale); } template < @@ -265,7 +263,10 @@ class ShiftedEOS : public EosBase> { const Space &s, const Real *rhos, const Real *sies, Real *cvs, Real *scratch, const int num, LambdaIndexer &&lambdas, Transform &&transform = Transform()) const { Real *shifted_sies = scratch; - shift_sies(s, sies, shifted_sies, num); + // Apply an outer modifier's energy conversion before subtracting this shift. + const Real input_scale = transform.y.is_set() ? transform.y.get() : Real(1); + shift_sies(s, sies, shifted_sies, num, input_scale); + transform.y.clear(); t_.SpecificHeatFromDensityInternalEnergy(s, rhos, shifted_sies, cvs, &scratch[num], num, std::forward(lambdas), std::forward(transform)); @@ -291,7 +292,10 @@ class ShiftedEOS : public EosBase> { const Space &s, const Real *rhos, const Real *sies, Real *bmods, Real *scratch, const int num, LambdaIndexer &&lambdas, Transform &&transform = Transform()) const { Real *shifted_sies = scratch; - shift_sies(s, sies, shifted_sies, num); + // Apply an outer modifier's energy conversion before subtracting this shift. + const Real input_scale = transform.y.is_set() ? transform.y.get() : Real(1); + shift_sies(s, sies, shifted_sies, num, input_scale); + transform.y.clear(); t_.BulkModulusFromDensityInternalEnergy(s, rhos, shifted_sies, bmods, &scratch[num], num, std::forward(lambdas), std::forward(transform)); @@ -316,7 +320,10 @@ class ShiftedEOS : public EosBase> { const Space &s, const Real *rhos, const Real *sies, Real *gm1s, Real *scratch, const int num, LambdaIndexer &&lambdas, Transform &&transform = Transform()) const { Real *shifted_sies = scratch; - shift_sies(s, sies, shifted_sies, num); + // Apply an outer modifier's energy conversion before subtracting this shift. + const Real input_scale = transform.y.is_set() ? transform.y.get() : Real(1); + shift_sies(s, sies, shifted_sies, num, input_scale); + transform.y.clear(); t_.GruneisenParamFromDensityInternalEnergy(s, rhos, shifted_sies, gm1s, &scratch[num], num, std::forward(lambdas), std::forward(transform)); @@ -329,10 +336,12 @@ class ShiftedEOS : public EosBase> { const Space &s, const Real *rhos, const Real *temperatures, Real *sies, Real *scratch, const int num, LambdaIndexer &&lambdas, Transform &&transform = Transform()) const { + // Capture the outer output conversion before inner modifiers compose it. + const Real output_scale = transform.f.is_set() ? transform.f.get() : Real(1); t_.InternalEnergyFromDensityTemperature(s, rhos, temperatures, sies, scratch, num, std::forward(lambdas), std::forward(transform)); - unshift_sies(s, sies, num); + unshift_sies(s, sies, num, output_scale); } template < @@ -355,7 +364,10 @@ class ShiftedEOS : public EosBase> { const Space &s, const Real *rhos, const Real *sies, Real *entropies, Real *scratch, const int num, LambdaIndexer &&lambdas, Transform &&transform = Transform()) const { Real *shifted_sies = scratch; - shift_sies(s, sies, shifted_sies, num); + // Apply an outer modifier's energy conversion before subtracting this shift. + const Real input_scale = transform.y.is_set() ? transform.y.get() : Real(1); + shift_sies(s, sies, shifted_sies, num, input_scale); + transform.y.clear(); t_.EntropyFromDensityInternalEnergy(s, rhos, shifted_sies, entropies, &scratch[num], num, std::forward(lambdas), std::forward(transform)); diff --git a/test/test_eos_modifiers_minimal.cpp b/test/test_eos_modifiers_minimal.cpp index 636bcbdf2d2..f7f4563bc89 100644 --- a/test/test_eos_modifiers_minimal.cpp +++ b/test/test_eos_modifiers_minimal.cpp @@ -11,7 +11,13 @@ // prepare derivative works, distribute copies to the public, perform // publicly and display publicly, and to permit others to do so. //------------------------------------------------------------------------------ +#include +#include #include +#include +#include +#include +#include #include #include @@ -37,6 +43,7 @@ namespace variadic_utils = singularity::variadic_utils; using EOSBuilder::Modify; using singularity::BilinearRampEOS; +using singularity::EosBase; using singularity::Gruneisen; using singularity::IdealGas; using singularity::RelativisticEOS; @@ -45,6 +52,311 @@ using singularity::ShiftedEOS; using singularity::UnitSystem; using singularity::Variant; +namespace { +// A cold energy term exposes density-conversion errors hidden by an ideal gas. +template +class ModifierTestEOS : public singularity::EosBase> { + public: + SG_ADD_BASE_CLASS_USINGS(ModifierTestEOS) + PORTABLE_FUNCTION void CheckParams() const {} + static constexpr unsigned long PreferredInput() { + return thermalqs::density | (EnergyPreferred ? thermalqs::specific_internal_energy + : thermalqs::temperature); + } + template + PORTABLE_FUNCTION Real InternalEnergyFromDensityTemperature(Real rho, Real temp, + Lambda && = nullptr) const { + return rho * rho + 3 * temp - 5; + } + template + PORTABLE_FUNCTION Real TemperatureFromDensityInternalEnergy(Real rho, Real energy, + Lambda && = nullptr) const { + return (energy - rho * rho + 5) / 3; + } + template + PORTABLE_FUNCTION Real MinInternalEnergyFromDensity(Real rho, + Lambda && = nullptr) const { + return rho * rho - 5; + } + template + PORTABLE_FUNCTION Real PressureFromDensityTemperature(Real rho, Real temp, + Lambda && = nullptr) const { + return rho * (temp + rho); + } + template + PORTABLE_FUNCTION Real SpecificHeatFromDensityTemperature(Real, Real, + Lambda && = nullptr) const { + return 3; + } + template + PORTABLE_FUNCTION Real EntropyFromDensityTemperature(Real rho, Real temp, + Lambda && = nullptr) const { + return rho + 2 * temp; + } + template + PORTABLE_FUNCTION Real BulkModulusFromDensityTemperature(Real rho, Real temp, + Lambda && = nullptr) const { + return rho * (temp + 2 * rho) + rho / 3 * (temp + rho - 2 * rho * rho); + } + template + PORTABLE_FUNCTION Real GruneisenParamFromDensityTemperature(Real, Real, + Lambda && = nullptr) const { + return Real(1) / 3; + } +#define MODIFIER_TEST_FROM_ENERGY(NAME) \ + template \ + PORTABLE_FUNCTION Real NAME##FromDensityInternalEnergy( \ + Real rho, Real energy, Lambda &&lambda = nullptr) const { \ + return NAME##FromDensityTemperature( \ + rho, TemperatureFromDensityInternalEnergy(rho, energy), lambda); \ + } + MODIFIER_TEST_FROM_ENERGY(Pressure) + MODIFIER_TEST_FROM_ENERGY(SpecificHeat) + MODIFIER_TEST_FROM_ENERGY(Entropy) + MODIFIER_TEST_FROM_ENERGY(BulkModulus) + MODIFIER_TEST_FROM_ENERGY(GruneisenParam) +#undef MODIFIER_TEST_FROM_ENERGY + template + PORTABLE_FUNCTION void InternalEnergyFromDensityPressure(Real rho, Real pressure, + Real &energy, + Lambda && = nullptr) const { + energy = InternalEnergyFromDensityTemperature(rho, pressure / rho - rho); + } + template + PORTABLE_FUNCTION void FillEos(Real &rho, Real &temp, Real &energy, Real &pressure, + Real &cv, Real &bulk, unsigned long output, + Lambda && = nullptr) const { + if (output & thermalqs::density) { + rho = (std::sqrt(temp * temp + 4 * pressure) - temp) / 2; + } + if (output & thermalqs::temperature) + temp = TemperatureFromDensityInternalEnergy(rho, energy); + if (output & thermalqs::specific_internal_energy) + energy = InternalEnergyFromDensityTemperature(rho, temp); + if (output & thermalqs::pressure) + pressure = PressureFromDensityTemperature(rho, temp); + if (output & thermalqs::specific_heat) cv = 3; + if (output & thermalqs::bulk_modulus) + bulk = BulkModulusFromDensityTemperature(rho, temp); + } + template + PORTABLE_FUNCTION void + ValuesAtReferenceState(Real &rho, Real &temp, Real &energy, Real &pressure, Real &cv, + Real &bulk, Real &dpde, Real &dvdt, Lambda && = nullptr) const { + rho = 2; + temp = 4; + energy = 11; + pressure = 12; + cv = 3; + bulk = BulkModulusFromDensityTemperature(rho, temp); + dpde = Real(2) / 3; + dvdt = Real(1) / 16; + } +#define MODIFIER_TEST_VECTOR(NAME) \ + template >> \ + void NAME(const Space &, const Real *x, const Real *y, Real *out, Real *, int count, \ + Lambdas &&, singularity::Transform &&transform = {}) const { \ + for (int i = 0; i < count; ++i) \ + out[i] = \ + transform.f.get() * NAME(transform.x.get() * x[i], transform.y.get() * y[i]); \ + } + MODIFIER_TEST_VECTOR(InternalEnergyFromDensityTemperature) + MODIFIER_TEST_VECTOR(TemperatureFromDensityInternalEnergy) + MODIFIER_TEST_VECTOR(PressureFromDensityTemperature) + MODIFIER_TEST_VECTOR(PressureFromDensityInternalEnergy) + MODIFIER_TEST_VECTOR(SpecificHeatFromDensityTemperature) + MODIFIER_TEST_VECTOR(SpecificHeatFromDensityInternalEnergy) + MODIFIER_TEST_VECTOR(EntropyFromDensityTemperature) + MODIFIER_TEST_VECTOR(EntropyFromDensityInternalEnergy) + MODIFIER_TEST_VECTOR(BulkModulusFromDensityTemperature) + MODIFIER_TEST_VECTOR(BulkModulusFromDensityInternalEnergy) + MODIFIER_TEST_VECTOR(GruneisenParamFromDensityTemperature) + MODIFIER_TEST_VECTOR(GruneisenParamFromDensityInternalEnergy) +#undef MODIFIER_TEST_VECTOR + static unsigned long scratch_size(std::string method, unsigned int count) { + return method == "MinInternalEnergyFromDensity" ? count * sizeof(Real) : 0; + } + static unsigned long max_scratch_size(unsigned int count) { + return count * sizeof(Real); + } + template + void MinInternalEnergyFromDensity(const Space &, const Real *rhos, Real *out, + Real *scratch, int count, Lambdas &&, + singularity::Transform &&transform = {}) const { + for (int i = 0; i < count; ++i) { + scratch[i] = transform.x.get() * rhos[i]; + out[i] = transform.f.get() * MinInternalEnergyFromDensity(scratch[i]); + } + } +}; + +// rho_user=rho_factor*rho_base, e_user=energy_factor*e_base+shift, +// T_user=temp_factor*T_base. Compare against the analytic base at several states. +template +void checkModifierConversions(EOS &eos, Real rho_factor, Real energy_factor, + Real temp_factor, Real shift = 0) { + const ModifierTestEOS<> base; + constexpr int count = 3; + Real rhos[count], temps[count], energies[count], result[count]; + for (int i = 0; i < count; ++i) { + const Real rho = Real(0.7) + i, temp = 2 + i; + rhos[i] = rho_factor * rho; + temps[i] = temp_factor * temp; + energies[i] = + energy_factor * base.InternalEnergyFromDensityTemperature(rho, temp) + shift; + CHECK(isClose(eos.InternalEnergyFromDensityTemperature(rhos[i], temps[i]), + energies[i], 1.e-12)); + CHECK(isClose(eos.TemperatureFromDensityInternalEnergy(rhos[i], energies[i]), + temps[i], 1.e-12)); + Real r = rhos[i], t = 0, e = energies[i], p = 0, c = 0, b = 0; + eos.FillEos(r, t, e, p, c, b, + thermalqs::temperature | thermalqs::pressure | thermalqs::specific_heat | + thermalqs::bulk_modulus); + CHECK(e == energies[i]); + CHECK(isClose(t, temps[i], 1.e-12)); + CHECK(isClose(c, 3 * energy_factor / temp_factor, 1.e-12)); + CHECK(isClose( + p, rho_factor * energy_factor * base.PressureFromDensityTemperature(rho, temp), + 1.e-12)); + e = -999; + eos.FillEos(r, t, e, p, c, b, + thermalqs::specific_internal_energy | thermalqs::pressure | + thermalqs::specific_heat | thermalqs::bulk_modulus); + CHECK(isClose(e, energies[i], 1.e-12)); + CHECK(isClose(c, 3 * energy_factor / temp_factor, 1.e-12)); + Real solved_energy = energies[i]; + eos.InternalEnergyFromDensityPressure(rhos[i], p, solved_energy); + CHECK(isClose(solved_energy, energies[i], 1.e-12)); + r = -999; + e = -999; + eos.FillEos(r, t, e, p, c, b, + thermalqs::density | thermalqs::specific_internal_energy | + thermalqs::specific_heat | thermalqs::bulk_modulus); + CHECK(isClose(r, rhos[i], 1.e-12)); + CHECK(isClose(e, energies[i], 1.e-12)); + } + std::vector scratch( + std::max(std::size_t(1), + static_cast(EOS::max_scratch_size(count) / sizeof(Real)))); +#define CHECK_MODIFIER_VECTOR(NAME, Y, YFACTOR, YOFFSET, FACTOR, OFFSET) \ + eos.NAME(PortsOfCall::Exec::Host(), static_cast(rhos), \ + static_cast(Y), static_cast(result), scratch.data(), \ + count, singularity::NullIndexer{}); \ + for (int i = 0; i < count; ++i) { \ + const Real rho = rhos[i] / rho_factor; \ + const Real y = (Y[i] - (YOFFSET)) / (YFACTOR); \ + CHECK(isClose(result[i], (FACTOR) * base.NAME(rho, y) + (OFFSET), 1.e-12)); \ + } + CHECK_MODIFIER_VECTOR(InternalEnergyFromDensityTemperature, temps, temp_factor, 0, + energy_factor, shift) + CHECK_MODIFIER_VECTOR(TemperatureFromDensityInternalEnergy, energies, energy_factor, + shift, temp_factor, 0) + CHECK_MODIFIER_VECTOR(PressureFromDensityInternalEnergy, energies, energy_factor, shift, + rho_factor * energy_factor, 0) + CHECK_MODIFIER_VECTOR(SpecificHeatFromDensityTemperature, temps, temp_factor, 0, + energy_factor / temp_factor, 0) + CHECK_MODIFIER_VECTOR(SpecificHeatFromDensityInternalEnergy, energies, energy_factor, + shift, energy_factor / temp_factor, 0) + CHECK_MODIFIER_VECTOR(EntropyFromDensityInternalEnergy, energies, energy_factor, shift, + energy_factor / temp_factor, 0) + CHECK_MODIFIER_VECTOR(BulkModulusFromDensityInternalEnergy, energies, energy_factor, + shift, rho_factor * energy_factor, 0) + CHECK_MODIFIER_VECTOR(GruneisenParamFromDensityInternalEnergy, energies, energy_factor, + shift, 1, 0) +#undef CHECK_MODIFIER_VECTOR + const auto required = + EOS::scratch_size("MinInternalEnergyFromDensity", count) / sizeof(Real); + constexpr Real guard = 123456; + std::vector guarded(required + count + 2, guard); + eos.MinInternalEnergyFromDensity( + PortsOfCall::Exec::Host(), static_cast(rhos), + static_cast(result), guarded.data() + 1, count, singularity::NullIndexer{}); + for (int i = 0; i < count; ++i) + CHECK(isClose( + result[i], + energy_factor * base.MinInternalEnergyFromDensity(rhos[i] / rho_factor) + shift, + 1.e-12)); + CHECK(guarded.front() == guard); + for (std::size_t i = required + 1; i < guarded.size(); ++i) + CHECK(guarded[i] == guard); + Real r, t, e, p, c, b, dpde, dvdt; + eos.ValuesAtReferenceState(r, t, e, p, c, b, dpde, dvdt); + CHECK(isClose(r, 2 * rho_factor, 1.e-12)); + CHECK(isClose(t, 4 * temp_factor, 1.e-12)); + CHECK(isClose(e, 11 * energy_factor + shift, 1.e-12)); + CHECK(isClose(p, 12 * rho_factor * energy_factor, 1.e-12)); + CHECK(isClose(c, 3 * energy_factor / temp_factor, 1.e-12)); + CHECK(isClose(b, + base.BulkModulusFromDensityTemperature(2, 4) * rho_factor * energy_factor, + 1.e-12)); + CHECK(isClose(dpde, Real(2) / 3 * rho_factor, 1.e-12)); + CHECK(isClose(dvdt, Real(1) / (16 * rho_factor * temp_factor), 1.e-12)); +} +} // namespace + +TEST_CASE("Modifier conversions compose without tabulated dependencies", + "[modifiers][Transform]") { + using Base = ModifierTestEOS<>; + UnitSystem units(Base{}, 2, 4, 5); + checkModifierConversions(units, 0.5, 0.25, 0.2); + UnitSystem> units_shift(ShiftedEOS(Base{}, 10), 2, 4, 5); + checkModifierConversions(units_shift, 0.5, 0.25, 0.2, 2.5); + ShiftedEOS> shift_units(UnitSystem(Base{}, 2, 4, 5), 2.5); + checkModifierConversions(shift_units, 0.5, 0.25, 0.2, 2.5); + ScaledEOS scaled(Base{}, 2); + checkModifierConversions(scaled, 0.5, 2, 1); + ScaledEOS> scaled_shift(ShiftedEOS(Base{}, 10), 2); + checkModifierConversions(scaled_shift, 0.5, 2, 1, 20); + ShiftedEOS> shift_scaled(ScaledEOS(Base{}, 2), 20); + checkModifierConversions(shift_scaled, 0.5, 2, 1, 20); + // PreferredInput must not determine which quantities FillEos actually receives. + using EnergyBase = ModifierTestEOS; + ShiftedEOS shifted_energy(EnergyBase{}, 10); + checkModifierConversions(shifted_energy, 1, 1, 1, 10); + ScaledEOS scaled_energy(EnergyBase{}, 2); + checkModifierConversions(scaled_energy, 0.5, 2, 1); +} + +TEST_CASE("Shifted FillEos converts the actual energy input", "[ShiftedEOS][IdealGas]") { + IdealGas base(0.5, 3); + ShiftedEOS eos(IdealGas(0.5, 3), 10); + Real rho = 2, temp = 0, energy = 16, pressure = 0, cv = 0, bulk = 0; + eos.FillEos(rho, temp, energy, pressure, cv, bulk, + thermalqs::temperature | thermalqs::pressure | thermalqs::specific_heat | + thermalqs::bulk_modulus); + CHECK(isClose(temp, 2)); + CHECK(energy == 16); + CHECK(isClose(pressure, base.PressureFromDensityTemperature(rho, temp))); + CHECK(isClose(cv, 3)); + energy = -999; + eos.FillEos(rho, temp, energy, pressure, cv, bulk, + thermalqs::specific_internal_energy | thermalqs::pressure); + CHECK(isClose(energy, 16)); +} + +TEST_CASE("Scaling preserves energy and heat capacity", "[ScaledEOS][Gruneisen]") { + Gruneisen base(0.394e6, 1.489, 0, 0, 2.02, 0.47, 8.93, 297, 1.e6, 8.56796e7); + constexpr Real scale = 2, rho = 5, temp = 400; + ScaledEOS eos(Gruneisen(base), scale); + const Real energy = + scale * base.InternalEnergyFromDensityTemperature(scale * rho, temp); + const Real cv = scale * base.SpecificHeatFromDensityTemperature(scale * rho, temp); + CHECK(isClose(eos.InternalEnergyFromDensityTemperature(rho, temp), energy)); + CHECK(isClose(eos.TemperatureFromDensityInternalEnergy(rho, energy), temp)); + CHECK(isClose(eos.SpecificHeatFromDensityTemperature(rho, temp), cv)); + CHECK(isClose(eos.SpecificHeatFromDensityInternalEnergy(rho, energy), cv)); + CHECK(isClose(eos.MinInternalEnergyFromDensity(rho), + scale * base.MinInternalEnergyFromDensity(scale * rho))); + Real r = rho, t = 0, e = energy, p = 0, c = 0, b = 0; + eos.FillEos(r, t, e, p, c, b, + thermalqs::temperature | thermalqs::pressure | thermalqs::specific_heat | + thermalqs::bulk_modulus); + CHECK(isClose(t, temp)); + CHECK(e == energy); + CHECK(isClose(c, cv)); +} + SCENARIO("Relativistic EOS", "[EOSBuilder][RelativisticEOS][IdealGas]") { using EOS = Variant>; GIVEN("Parameters for an ideal gas") {