diff --git a/CHANGELOG.md b/CHANGELOG.md index d98fecd3d5d..e934c0e823d 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -3,9 +3,9 @@ ## Current develop ### Added (new features/APIs/variables/...) -- [[PR658]](https://github.com/lanl/singularity-eos/pull/658) Add `MinimumInternalEnergy`/`MaximumInternalEnergy` to the EOS introspection API, so energy bounds are reachable through modifiers and the `singularity::EOS` variant +- [[PR668]](https://github.com/lanl/singularity-eos/pull/668) Add analytic `InternalEnergyFromDensityPressure` methods to Davis Reactants, Davis Products, MGUsup, and powerMG overriding the iterative solve previously used. - [[PR659]](https://github.com/lanl/singularity-eos/pull/659) Add `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. -- [[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. +- [[PR658]](https://github.com/lanl/singularity-eos/pull/658) Add `MinimumInternalEnergy`/`MaximumInternalEnergy` to the EOS introspection API, so energy bounds are reachable through modifiers and the `singularity::EOS` variant ### Fixed (Repair bugs, etc) - [[PR667]](https://github.com/lanl/singularity-eos/pull/667) Various fixes to shift/unit/scale EOS modifiers. diff --git a/singularity-eos/eos/eos_davis.hpp b/singularity-eos/eos/eos_davis.hpp index 34a23fb35cf..56cb3742b02 100644 --- a/singularity-eos/eos/eos_davis.hpp +++ b/singularity-eos/eos/eos_davis.hpp @@ -83,6 +83,24 @@ class DavisReactants : public EosBase { _Cv0 * t_s * _i1pa * (std::pow(robust::ratio(temp, t_s), 1.0 + _alpha) - 1.0); } template + PORTABLE_INLINE_FUNCTION void InternalEnergyFromDensityPressure( + const Real rho, const Real P, Real &sie, + Indexer_t &&lambda = static_cast(nullptr)) const { + + const Real Es_ = Es(rho); + const Real Ps_ = Ps(rho); + const Real Gamma_ = Gamma(rho); + sie = Es_ + robust::ratio(P - Ps_, Gamma_ * rho); + } + template + PORTABLE_INLINE_FUNCTION Real InternalEnergyFromDensityPressure( + const Real rho, const Real P, + Indexer_t &&lambda = static_cast(nullptr)) const { + Real sie; + InternalEnergyFromDensityPressure(rho, P, sie, lambda); + return sie; + } + template PORTABLE_INLINE_FUNCTION Real PressureFromDensityTemperature( const Real rho, const Real temp, Indexer_t &&lambda = static_cast(nullptr)) const { @@ -239,6 +257,20 @@ class DavisProducts : public EosBase { return _Cv * (temp - Ts(rho)) + Es(rho); } template + PORTABLE_INLINE_FUNCTION void InternalEnergyFromDensityPressure( + const Real rho, const Real P, Real &sie, + Indexer_t &&lambda = static_cast(nullptr)) const { + sie = Es(rho) + robust::ratio(P - Ps(rho), rho * Gamma(rho)); + } + template + PORTABLE_INLINE_FUNCTION Real InternalEnergyFromDensityPressure( + const Real rho, const Real P, + Indexer_t &&lambda = static_cast(nullptr)) const { + Real sie; + InternalEnergyFromDensityPressure(rho, P, sie, lambda); + return sie; + } + template PORTABLE_INLINE_FUNCTION Real PressureFromDensityTemperature( const Real rho, const Real temp, Indexer_t &&lambda = static_cast(nullptr)) const { diff --git a/singularity-eos/eos/eos_mgusup.hpp b/singularity-eos/eos/eos_mgusup.hpp index 0d653be7422..78b35623376 100644 --- a/singularity-eos/eos/eos_mgusup.hpp +++ b/singularity-eos/eos/eos_mgusup.hpp @@ -64,6 +64,18 @@ class MGUsup : public EosBase { const Real rho, const Real sie, Indexer_t &&lambda = static_cast(nullptr)) const; template + PORTABLE_INLINE_FUNCTION void InternalEnergyFromDensityPressure( + const Real rho, const Real P, Real &sie, + Indexer_t &&lambda = static_cast(nullptr)) const; + template + PORTABLE_INLINE_FUNCTION Real InternalEnergyFromDensityPressure( + const Real rho, const Real P, + Indexer_t &&lambda = static_cast(nullptr)) const { + Real sie; + InternalEnergyFromDensityPressure(rho, P, sie, lambda); + return sie; + } + template PORTABLE_INLINE_FUNCTION Real EntropyFromDensityTemperature(const Real rho, const Real temp, Indexer_t &&lambda = static_cast(nullptr)) const; @@ -330,6 +342,13 @@ PORTABLE_INLINE_FUNCTION Real MGUsup::PressureFromDensityInternalEnergy( return value; } template +PORTABLE_INLINE_FUNCTION void +MGUsup::InternalEnergyFromDensityPressure(const Real rho, const Real P, Real &sie, + Indexer_t &&lambda) const { + const Real Ph_ = HugPressureFromDensity(rho); + sie = HugInternalEnergyFromDensity(rho) + robust::ratio(P - Ph_, _rho0 * _G0); +} +template PORTABLE_INLINE_FUNCTION Real MGUsup::EntropyFromDensityInternalEnergy( const Real rho, const Real sie, Indexer_t &&lambda) const { Real eta = 1.0 - robust::ratio(_rho0, rho); diff --git a/singularity-eos/eos/eos_powermg.hpp b/singularity-eos/eos/eos_powermg.hpp index 822f35bab7a..c7110e70acc 100644 --- a/singularity-eos/eos/eos_powermg.hpp +++ b/singularity-eos/eos/eos_powermg.hpp @@ -70,6 +70,18 @@ class PowerMG : public EosBase { const Real rho, const Real sie, Indexer_t &&lambda = static_cast(nullptr)) const; template + PORTABLE_INLINE_FUNCTION void InternalEnergyFromDensityPressure( + const Real rho, const Real P, Real &sie, + Indexer_t &&lambda = static_cast(nullptr)) const; + template + PORTABLE_INLINE_FUNCTION Real InternalEnergyFromDensityPressure( + const Real rho, const Real P, + Indexer_t &&lambda = static_cast(nullptr)) const { + Real sie; + InternalEnergyFromDensityPressure(rho, P, sie, lambda); + return sie; + } + template PORTABLE_INLINE_FUNCTION Real EntropyFromDensityTemperature(const Real rho, const Real temp, Indexer_t &&lambda = static_cast(nullptr)) const; @@ -400,7 +412,6 @@ PORTABLE_INLINE_FUNCTION Real PowerMG::BulkModulusFromDensityInternalEnergy( } return value; } - template PORTABLE_INLINE_FUNCTION Real PowerMG::TemperatureFromDensityInternalEnergy( const Real rho, const Real sie, Indexer_t &&lambda) const { @@ -421,6 +432,14 @@ PORTABLE_INLINE_FUNCTION Real PowerMG::PressureFromDensityInternalEnergy( _rho0 * _G0 * (sie - AllHugInternalEnergyFromDensity(rho)); return value; } +template +PORTABLE_INLINE_FUNCTION void +PowerMG::InternalEnergyFromDensityPressure(const Real rho, const Real P, Real &sie, + Indexer_t &&lambda) const { + const Real Ph_ = AllHugPressureFromDensity(rho); + sie = AllHugInternalEnergyFromDensity(rho) + robust::ratio(P - Ph_, _rho0 * _G0); +} + template PORTABLE_INLINE_FUNCTION Real PowerMG::EntropyFromDensityInternalEnergy( const Real rho, const Real sie, Indexer_t &&lambda) const { diff --git a/test/test_eos_davis.cpp b/test/test_eos_davis.cpp index 275e951ae87..ec9676d056a 100644 --- a/test/test_eos_davis.cpp +++ b/test/test_eos_davis.cpp @@ -130,6 +130,50 @@ SCENARIO("Davis reactants EOS basic calls", "[DavisReactants]") { } } + WHEN("Internal energy is computed from density and pressure") { + THEN("It inverts the pressure lookup over a range of states") { + int nwrong = 0; + portableReduce( + "Round trip sie -> P -> sie for DavisReactants", 0, N, + PORTABLE_LAMBDA(const int i, int &nw) { + // sweep both density and energy across the grid + const Real rho = rho0 * (0.5 + 2.5 * i / (N - 1.0)); + const Real sie = e0 * (0.25 + 1.75 * i / (N - 1.0)); + const Real P = eos.PressureFromDensityInternalEnergy(rho, sie); + + Real sie_out = 0.0; + eos.InternalEnergyFromDensityPressure(rho, P, sie_out); + nw += !isClose(sie_out, sie, 1e-12); + + // and the recovered energy must reproduce the pressure + nw += + !isClose(eos.PressureFromDensityInternalEnergy(rho, sie_out), P, 1e-12); + }, + nwrong); + REQUIRE(nwrong == 0); + } + + THEN("A supplied initial guess does not change the answer") { + int nwrong = 0; + portableReduce( + "Initial guess independence for DavisReactants", 0, N, + PORTABLE_LAMBDA(const int i, int &nw) { + const Real rho = rho0 * (0.5 + 2.5 * i / (N - 1.0)); + const Real sie = e0 * (0.25 + 1.75 * i / (N - 1.0)); + const Real P = eos.PressureFromDensityInternalEnergy(rho, sie); + // the analytic inverse should ignore the guess entirely + Real sie_lo = 0.1 * e0; + Real sie_hi = 10.0 * e0; + eos.InternalEnergyFromDensityPressure(rho, P, sie_lo); + eos.InternalEnergyFromDensityPressure(rho, P, sie_hi); + nw += !isClose(sie_lo, sie_hi, 1e-12); + nw += !isClose(sie_lo, sie, 1e-12); + }, + nwrong); + REQUIRE(nwrong == 0); + } + } + eos_host.Finalize(); eos.Finalize(); } @@ -194,6 +238,50 @@ SCENARIO("Davis products EOS basic calls", "[DavisProducts]") { } } + WHEN("Internal energy is computed from density and pressure") { + THEN("It inverts the pressure lookup over a range of states") { + int nwrong = 0; + portableReduce( + "Round trip sie -> P -> sie for DavisProducts", 0, N, + PORTABLE_LAMBDA(const int i, int &nw) { + // sweep both density and energy across the grid + const Real rho = rho0 * (0.5 + 2.5 * i / (N - 1.0)); + const Real sie = e0 * (0.25 + 1.75 * i / (N - 1.0)); + const Real P = eos.PressureFromDensityInternalEnergy(rho, sie); + + Real sie_out = 0.0; + eos.InternalEnergyFromDensityPressure(rho, P, sie_out); + nw += !isClose(sie_out, sie, 1e-12); + + // and the recovered energy must reproduce the pressure + nw += + !isClose(eos.PressureFromDensityInternalEnergy(rho, sie_out), P, 1e-12); + }, + nwrong); + REQUIRE(nwrong == 0); + } + + THEN("A supplied initial guess does not change the answer") { + int nwrong = 0; + portableReduce( + "Initial guess independence for DavisProducts", 0, N, + PORTABLE_LAMBDA(const int i, int &nw) { + const Real rho = rho0 * (0.5 + 2.5 * i / (N - 1.0)); + const Real sie = e0 * (0.25 + 1.75 * i / (N - 1.0)); + const Real P = eos.PressureFromDensityInternalEnergy(rho, sie); + // the analytic inverse should ignore the guess entirely + Real sie_lo = 0.1 * e0; + Real sie_hi = 10.0 * e0; + eos.InternalEnergyFromDensityPressure(rho, P, sie_lo); + eos.InternalEnergyFromDensityPressure(rho, P, sie_hi); + nw += !isClose(sie_lo, sie_hi, 1e-12); + nw += !isClose(sie_lo, sie, 1e-12); + }, + nwrong); + REQUIRE(nwrong == 0); + } + } + eos_host.Finalize(); eos.Finalize(); } diff --git a/test/test_eos_mgusup.cpp b/test/test_eos_mgusup.cpp index 162709cc0cb..f201abd20ed 100644 --- a/test/test_eos_mgusup.cpp +++ b/test/test_eos_mgusup.cpp @@ -1,5 +1,5 @@ //------------------------------------------------------------------------------ -// © 2021-2024. Triad National Security, LLC. All rights reserved. This +// © 2021-2026. 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 @@ -536,3 +536,76 @@ SCENARIO("MGUsup EOS SetUp", "[VectorEOS][MGUsupEOS]") { } } } + +SCENARIO("MGUsup EOS SetUp", "PofRE EofRP are inverse operations") { + GIVEN("Parameters for a MGUsup EOS") { + + constexpr int N = 100; + // Unit conversions + constexpr Real Mbcc_per_g = 1e12; + // MGUsup parameters for copper + Real rho0 = 7.285; + Real T0 = 298.0; + Real Cs = 2766.0e2; + Real s = 1.5344; + Real G0 = 2.4659; + Real Cv0 = 0.2149e-05 * Mbcc_per_g; + constexpr Real E0 = 0.658e-03 * Mbcc_per_g; + constexpr Real S0 = 0.4419e-05 * Mbcc_per_g; + // Create the EOS + EOS eos = MGUsup(rho0, T0, Cs, s, G0, Cv0, E0, S0); + + // The MGUsup Hugoniot has a (1 - s*eta)^2 in the denominator, so the + // reference curve blows up as rho -> rho0 / (1 - 1/s), which is about + // 2.87 * rho0 here. Keep the sweep well below that, and scale the energies + // to the local reference energy: a fixed absolute sie becomes a vanishing + // fraction of Es(rho) at high compression, and recovering it from P is then + // a difference of two much larger numbers. + WHEN("Internal energy is computed from density and pressure") { + THEN("It inverts the pressure lookup over a range of states") { + int nwrong = 0; + portableReduce( + "Round trip sie -> P -> sie for MGUsup", 0, N, + PORTABLE_LAMBDA(const int i, int &nw) { + // sweep both density and energy across the grid + const Real rho = rho0 * (0.5 + 2.0 * i / (N - 1.0)); + const Real es = eos.InternalEnergyFromDensityTemperature(rho, T0); + const Real sie = es * (0.25 + 1.75 * i / (N - 1.0)); + const Real P = eos.PressureFromDensityInternalEnergy(rho, sie); + + Real sie_out = 0.0; + eos.InternalEnergyFromDensityPressure(rho, P, sie_out); + nw += !isClose(sie_out, sie, 1e-12); + + // and the recovered energy must reproduce the pressure + nw += + !isClose(eos.PressureFromDensityInternalEnergy(rho, sie_out), P, 1e-12); + }, + nwrong); + REQUIRE(nwrong == 0); + } + + THEN("A supplied initial guess does not change the answer") { + int nwrong = 0; + portableReduce( + "Initial guess independence for MGUsup", 0, N, + PORTABLE_LAMBDA(const int i, int &nw) { + const Real rho = rho0 * (0.5 + 2.0 * i / (N - 1.0)); + const Real es = eos.InternalEnergyFromDensityTemperature(rho, T0); + const Real sie = es * (0.25 + 1.75 * i / (N - 1.0)); + const Real P = eos.PressureFromDensityInternalEnergy(rho, sie); + // the analytic inverse should ignore the guess entirely + Real sie_lo = 0.1 * es; + Real sie_hi = 10.0 * es; + eos.InternalEnergyFromDensityPressure(rho, P, sie_lo); + eos.InternalEnergyFromDensityPressure(rho, P, sie_hi); + nw += !isClose(sie_lo, sie_hi, 1e-12); + nw += !isClose(sie_lo, sie, 1e-12); + }, + nwrong); + REQUIRE(nwrong == 0); + } + } + eos.Finalize(); + } +} \ No newline at end of file diff --git a/test/test_eos_modifiers_minimal.cpp b/test/test_eos_modifiers_minimal.cpp index f7f4563bc89..76690a3527b 100644 --- a/test/test_eos_modifiers_minimal.cpp +++ b/test/test_eos_modifiers_minimal.cpp @@ -57,7 +57,21 @@ namespace { template class ModifierTestEOS : public singularity::EosBase> { public: - SG_ADD_BASE_CLASS_USINGS(ModifierTestEOS) + using Base = EosBase>; + + using Base::BulkModulusFromDensityInternalEnergy; + using Base::BulkModulusFromDensityTemperature; + using Base::EntropyFromDensityInternalEnergy; + using Base::EntropyFromDensityTemperature; + using Base::GruneisenParamFromDensityInternalEnergy; + using Base::GruneisenParamFromDensityTemperature; + using Base::InternalEnergyFromDensityTemperature; + using Base::PressureFromDensityInternalEnergy; + using Base::PressureFromDensityTemperature; + using Base::SpecificHeatFromDensityInternalEnergy; + using Base::SpecificHeatFromDensityTemperature; + using Base::TemperatureFromDensityInternalEnergy; + // Intentionally not: using Base::MinInternalEnergyFromDensity; PORTABLE_FUNCTION void CheckParams() const {} static constexpr unsigned long PreferredInput() { return thermalqs::density | (EnergyPreferred ? thermalqs::specific_internal_energy diff --git a/test/test_eos_powermg.cpp b/test/test_eos_powermg.cpp index 2c0e7e7eace..2e7df54d640 100644 --- a/test/test_eos_powermg.cpp +++ b/test/test_eos_powermg.cpp @@ -1,5 +1,5 @@ //------------------------------------------------------------------------------ -// © 2021-2024. Triad National Security, LLC. All rights reserved. This +// © 2021-2026. 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 @@ -680,3 +680,83 @@ SCENARIO("PowerMG EOS SetUp", "[VectorEOS][PowerMGEOS]") { } } } + +SCENARIO("PowerMG EOS rho P", "[VectorEOS][PowerMGEOS]") { + GIVEN("Parameters for a PowerMG EOS") { + constexpr int N = 100; + // Unit conversions + constexpr Real Mbcc_per_g = 1e12; + // PowerMG parameters for tin beta phase + constexpr Real rho0 = 7.285; + constexpr Real T0 = 298.0; + constexpr Real Cs = 2766.0e2; + constexpr Real s = 1.5344; + constexpr Real G0 = 2.4659; + constexpr Real Cv0 = 0.2149e-05 * Mbcc_per_g; + constexpr Real E0 = 0.658e-03 * Mbcc_per_g; + constexpr Real S0 = 0.4419e-05 * Mbcc_per_g; + constexpr Real Pmin = -0.001; + Real K0toK40[41] = {Cs * Cs * rho0}; + for (int ind = 1; ind <= 20; ind++) { + K0toK40[ind] = (ind + 1) * pow(s, ind); + } + EOS host_eos = PowerMG(rho0, T0, G0, Cv0, E0, S0, Pmin, K0toK40); + EOS eos = host_eos.GetOnDevice(); + + // PressureFromDensityInternalEnergy is affine in sie, so + // InternalEnergyFromDensityPressure is analytic rather than a root find. + // Energies are scaled to the local e(rho, T0): a fixed absolute sie becomes + // a vanishing fraction of the reference energy under compression, and + // recovering it from P is then a difference of two much larger numbers. + // The sweep starts at 0.2 * rho0 so the Pmin-clamped branch of + // AllHugPressureFromDensity is exercised too; that clamp is a function of + // density only, so it does not spoil the inversion. + WHEN("Internal energy is computed from density and pressure") { + THEN("It inverts the pressure lookup over a range of states") { + int nwrong = 0; + portableReduce( + "Round trip sie -> P -> sie for PowerMG", 0, N, + PORTABLE_LAMBDA(const int i, int &nw) { + // sweep both density and energy across the grid + const Real rho = rho0 * (0.2 + 2.3 * i / (N - 1.0)); + const Real es = eos.InternalEnergyFromDensityTemperature(rho, T0); + const Real sie = es * (0.25 + 1.75 * i / (N - 1.0)); + const Real P = eos.PressureFromDensityInternalEnergy(rho, sie); + + Real sie_out = 0.0; + eos.InternalEnergyFromDensityPressure(rho, P, sie_out); + nw += !isClose(sie_out, sie, 1e-12); + + // and the recovered energy must reproduce the pressure + nw += + !isClose(eos.PressureFromDensityInternalEnergy(rho, sie_out), P, 1e-12); + }, + nwrong); + REQUIRE(nwrong == 0); + } + + THEN("A supplied initial guess does not change the answer") { + int nwrong = 0; + portableReduce( + "Initial guess independence for PowerMG", 0, N, + PORTABLE_LAMBDA(const int i, int &nw) { + const Real rho = rho0 * (0.2 + 2.3 * i / (N - 1.0)); + const Real es = eos.InternalEnergyFromDensityTemperature(rho, T0); + const Real sie = es * (0.25 + 1.75 * i / (N - 1.0)); + const Real P = eos.PressureFromDensityInternalEnergy(rho, sie); + // the analytic inverse should ignore the guess entirely + Real sie_lo = 0.1 * es; + Real sie_hi = 10.0 * es; + eos.InternalEnergyFromDensityPressure(rho, P, sie_lo); + eos.InternalEnergyFromDensityPressure(rho, P, sie_hi); + nw += !isClose(sie_lo, sie_hi, 1e-12); + nw += !isClose(sie_lo, sie, 1e-12); + }, + nwrong); + REQUIRE(nwrong == 0); + } + } + + eos.Finalize(); + } +}