Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Simplify Fluid Caching #9946

Closed
wants to merge 5 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 36 additions & 9 deletions src/EnergyPlus/FluidProperties.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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 = 16;
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<std::uint64_t const *>(&t) >> Grid_Shift);
std::uint64_t const tagTemp = *reinterpret_cast<std::uint64_t const *>(&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,
Expand Down
33 changes: 12 additions & 21 deletions src/EnergyPlus/FluidProperties.hh
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -249,6 +243,15 @@ namespace FluidProperties {
Array1D<Real64> ViscTemps; // Temperatures for viscosity of glycol
Array1D<Real64> ViscValues; // viscosity values (mPa-s)

#ifdef EP_cache_GlycolSpecificHeat
struct SpecificHeatCacheEntry {
std::uint64_t tagTemp = 0; // 0x8000000000000000 Initialize with something that is not a valid tag
Real64 specificHeat = 0.0;
};

static int constexpr SpecificHeatCacheSize = 1024;
std::array<SpecificHeatCacheEntry, SpecificHeatCacheSize> specificHeatCache;
#endif
// Default Constructor
FluidPropsGlycolData()
: GlycolIndex(0), Concentration(1.0), CpDataPresent(false), CpLowTempValue(0.0), CpHighTempValue(0.0), CpLowTempIndex(0),
Expand All @@ -257,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
}
};

Expand Down Expand Up @@ -332,18 +338,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);
Expand Down Expand Up @@ -658,9 +652,6 @@ struct FluidPropertiesData : BaseGlobalStruct
int TempRangeErrCountGetInterpolatedSatProp = 0;
int TempRangeErrIndexGetInterpolatedSatProp = 0;

#ifdef EP_cache_GlycolSpecificHeat
std::array<FluidProperties::cached_tsh, FluidProperties::t_sh_cache_size> cached_t_sh;
#endif

void clear_state() override
{
Expand Down