From c74483f8707b18835aae5d7e9f7720d6abee86b7 Mon Sep 17 00:00:00 2001 From: amirroth Date: Thu, 6 Apr 2023 14:04:27 -0400 Subject: [PATCH 1/4] Simplify glycol specific-heat caching --- src/EnergyPlus/FluidProperties.cc | 45 ++++++++++++++++++++++++------- src/EnergyPlus/FluidProperties.hh | 30 +++++++-------------- 2 files changed, 45 insertions(+), 30 deletions(-) diff --git a/src/EnergyPlus/FluidProperties.cc b/src/EnergyPlus/FluidProperties.cc index a052741fba4..39c5688708e 100644 --- a/src/EnergyPlus/FluidProperties.cc +++ b/src/EnergyPlus/FluidProperties.cc @@ -7463,26 +7463,53 @@ namespace FluidProperties { std::string_view const CalledFrom // routine this function was called from (error messages) ) { - std::uint64_t constexpr Grid_Shift = 64 - 12 - t_sh_precision_bits; + // Get the input if we haven't already + if (state.dataFluidProps->GetInput) { + GetFluidPropertiesData(state); + state.dataFluidProps->GetInput = false; + } + + // If no glycols, no fluid properties can be evaluated + int GlycolNum(0); + if (state.dataFluidProps->NumOfGlycols == 0) + ReportFatalGlycolErrors( + state, state.dataFluidProps->NumOfGlycols, GlycolNum, true, Glycol, "GetSpecificHeatGlycol", "specific heat", CalledFrom); + + // If glycol index has not yet been found for this fluid, find its value now + if (GlycolIndex > 0) { + GlycolNum = GlycolIndex; + } else { // Find which glycol (index) is being requested + GlycolNum = FindGlycol(state, Glycol); + if (GlycolNum == 0) { + ReportFatalGlycolErrors( + state, state.dataFluidProps->NumOfGlycols, GlycolNum, true, Glycol, "GetSpecificHeatGlycol", "specific heat", CalledFrom); + } + GlycolIndex = GlycolNum; + } + + int constexpr SpecificHeatPrecisionBits = 24; + std::uint64_t constexpr SpecificHeatMask = (FluidPropsGlycolData::SpecificHeatCacheSize - 1); + std::uint64_t constexpr SpecificHeatShift = 64 - 12 - SpecificHeatPrecisionBits; - double const t(Temperature + 1000 * GlycolIndex); + double const t = Temperature; DISABLE_WARNING_PUSH DISABLE_WARNING_STRICT_ALIASING DISABLE_WARNING_UNINITIALIZED // cppcheck-suppress invalidPointerCast - std::uint64_t const T_tag(*reinterpret_cast(&t) >> Grid_Shift); + std::uint64_t const tagTemp = *reinterpret_cast(&t) >> SpecificHeatShift; DISABLE_WARNING_POP - std::uint64_t const hash(T_tag & t_sh_cache_mask); - auto &cTsh(state.dataFluidProps->cached_t_sh[hash]); + std::uint64_t const hash = tagTemp & SpecificHeatMask; + auto &glycol = state.dataFluidProps->GlycolData(GlycolIndex); + auto &cacheEntry = glycol.specificHeatCache[hash]; - if (cTsh.iT != T_tag) { - cTsh.iT = T_tag; - cTsh.sh = GetSpecificHeatGlycol_raw(state, Glycol, Temperature, GlycolIndex, CalledFrom); + if (cacheEntry.tagTemp != tagTemp) { + cacheEntry.tagTemp = tagTemp; + cacheEntry.specificHeat = GetSpecificHeatGlycol_raw(state, Glycol, Temperature, GlycolIndex, CalledFrom); } - return cTsh.sh; // saturation pressure {Pascals} + return cacheEntry.specificHeat; // saturation pressure {Pascals} } Real64 GetSpecificHeatGlycol_raw(EnergyPlusData &state, diff --git a/src/EnergyPlus/FluidProperties.hh b/src/EnergyPlus/FluidProperties.hh index 10748a41bb1..b5e4a34f481 100644 --- a/src/EnergyPlus/FluidProperties.hh +++ b/src/EnergyPlus/FluidProperties.hh @@ -93,12 +93,6 @@ namespace FluidProperties { constexpr static std::string_view EthyleneGlycol("EthyleneGlycol"); constexpr static std::string_view PropyleneGlycol("PropyleneGlycol"); -#ifdef EP_cache_GlycolSpecificHeat - int constexpr t_sh_cache_size = 1024 * 1024; - int constexpr t_sh_precision_bits = 24; - std::uint64_t constexpr t_sh_cache_mask = (t_sh_cache_size - 1); -#endif - struct FluidPropsRefrigerantData { // Members @@ -249,6 +243,15 @@ namespace FluidProperties { Array1D ViscTemps; // Temperatures for viscosity of glycol Array1D ViscValues; // viscosity values (mPa-s) +#ifdef EP_cache_GlycolSpecificHeat + struct SpecificHeatCacheEntry { + std::uint64_t tagTemp = 0; + Real64 specificHeat = 0.0; + }; + + static int constexpr SpecificHeatCacheSize = 1024; + std::array specificHeatCache; +#endif // Default Constructor FluidPropsGlycolData() : GlycolIndex(0), Concentration(1.0), CpDataPresent(false), CpLowTempValue(0.0), CpHighTempValue(0.0), CpLowTempIndex(0), @@ -332,18 +335,6 @@ namespace FluidProperties { } }; - struct cached_tsh - { - // Members - std::uint64_t iT; - Real64 sh; - - // Default Constructor - cached_tsh() : iT(1000), sh(0.0) - { - } - }; - void InitializeGlycRoutines(); void GetFluidPropertiesData(EnergyPlusData &state); @@ -658,9 +649,6 @@ struct FluidPropertiesData : BaseGlobalStruct int TempRangeErrCountGetInterpolatedSatProp = 0; int TempRangeErrIndexGetInterpolatedSatProp = 0; -#ifdef EP_cache_GlycolSpecificHeat - std::array cached_t_sh; -#endif void clear_state() override { From bc1bc3ca9c273f249f7faa0807ad2d1c2f36eab0 Mon Sep 17 00:00:00 2001 From: amirroth Date: Thu, 6 Apr 2023 21:46:22 -0400 Subject: [PATCH 2/4] Well that was stupid --- src/EnergyPlus/FluidProperties.hh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/EnergyPlus/FluidProperties.hh b/src/EnergyPlus/FluidProperties.hh index b5e4a34f481..b5cb7a587c1 100644 --- a/src/EnergyPlus/FluidProperties.hh +++ b/src/EnergyPlus/FluidProperties.hh @@ -245,7 +245,7 @@ namespace FluidProperties { #ifdef EP_cache_GlycolSpecificHeat struct SpecificHeatCacheEntry { - std::uint64_t tagTemp = 0; + std::uint64_t tagTemp = 0x8000000000000000; // Initialize with something that is not a valid tag Real64 specificHeat = 0.0; }; From b7d7131c86c6b840dae634a411e0eb7ce9bf8d19 Mon Sep 17 00:00:00 2001 From: amirroth Date: Fri, 7 Apr 2023 08:30:19 -0400 Subject: [PATCH 3/4] Take 3, reducing precision bits --- src/EnergyPlus/FluidProperties.cc | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/EnergyPlus/FluidProperties.cc b/src/EnergyPlus/FluidProperties.cc index 39c5688708e..034a24b28be 100644 --- a/src/EnergyPlus/FluidProperties.cc +++ b/src/EnergyPlus/FluidProperties.cc @@ -7487,7 +7487,7 @@ namespace FluidProperties { GlycolIndex = GlycolNum; } - int constexpr SpecificHeatPrecisionBits = 24; + int constexpr SpecificHeatPrecisionBits = 16; std::uint64_t constexpr SpecificHeatMask = (FluidPropsGlycolData::SpecificHeatCacheSize - 1); std::uint64_t constexpr SpecificHeatShift = 64 - 12 - SpecificHeatPrecisionBits; From fb0905b875994e5adedd78883292b15502171540 Mon Sep 17 00:00:00 2001 From: amirroth Date: Sat, 8 Apr 2023 12:02:53 -0400 Subject: [PATCH 4/4] Take 4: a quick performance test --- src/EnergyPlus/FluidProperties.hh | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/src/EnergyPlus/FluidProperties.hh b/src/EnergyPlus/FluidProperties.hh index b5cb7a587c1..03bdc40bdac 100644 --- a/src/EnergyPlus/FluidProperties.hh +++ b/src/EnergyPlus/FluidProperties.hh @@ -245,7 +245,7 @@ namespace FluidProperties { #ifdef EP_cache_GlycolSpecificHeat struct SpecificHeatCacheEntry { - std::uint64_t tagTemp = 0x8000000000000000; // Initialize with something that is not a valid tag + std::uint64_t tagTemp = 0; // 0x8000000000000000 Initialize with something that is not a valid tag Real64 specificHeat = 0.0; }; @@ -260,6 +260,9 @@ namespace FluidProperties { CondLowTempIndex(0), CondHighTempIndex(0), ViscDataPresent(false), NumViscTempPts(0), ViscLowTempValue(0.0), ViscHighTempValue(0.0), ViscLowTempIndex(0), ViscHighTempIndex(0) { +#ifdef EP_cache_GlycolSpecificHeat + specificHeatCache[0].tagTemp = 0x8000000000000000; // 0 is a valid tag for entry 0, so need to initialize with something else; +#endif } };