Skip to content

Commit

Permalink
simplify find_eqvar to return name and value separately
Browse files Browse the repository at this point in the history
  • Loading branch information
Yujie Xu committed Aug 12, 2024
1 parent 17341b0 commit 473e566
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 19 deletions.
70 changes: 55 additions & 15 deletions src/EnergyPlus/ExtendedHI.cc
Original file line number Diff line number Diff line change
Expand Up @@ -346,8 +346,56 @@ namespace ExtendedHI {
return dTcdt;
}

int find_eqvar_name(EnergyPlusData &state, Real64 Ta, Real64 RH)
{

Real64 Pa = RH * pvstar(Ta);
Real64 Rs = 0.0387;
Real64 phi = 0.84;
Real64 m = (Pc - Pa) / (Zs(Rs) + Za);
Real64 m_bar = (Pc - Pa) / (Zs(Rs) + Za_bar);

int SolFla;
Real64 Ts;
General::SolveRoot(
state,
tol,
maxIter,
SolFla,
Ts,
[&](Real64 Ts) { return (Ts - Ta) / Ra(Ts, Ta) + (Pc - Pa) / (Zs(Rs) + Za) - (Tc - Ts) / Rs; },
std::max(0.0, std::min(Tc, Ta) - Rs * std::abs(m)),
std::max(Tc, Ta) + Rs * std::abs(m));

Real64 Tf;
General::SolveRoot(
state,
tol,
maxIter,
SolFla,
Tf,
[&](Real64 Tf) { return (Tf - Ta) / Ra_bar(Tf, Ta) + (Pc - Pa) / (Zs(Rs) + Za_bar) - (Tc - Tf) / Rs; },
std::max(0.0, std::min(Tc, Ta) - Rs * std::abs(m_bar)),
std::max(Tc, Ta) + Rs * std::abs(m_bar));
Real64 flux1 = Q - Qv(Ta, Pa) - (1.0 - phi) * (Tc - Ts) / Rs;
Real64 flux2 = Q - Qv(Ta, Pa) - (1.0 - phi) * (Tc - Ts) / Rs - phi * (Tc - Tf) / Rs;

if (flux1 <= 0.0) {
return static_cast<int>(EqvarName::Phi);
} else if (flux2 <= 0.0) {
return static_cast<int>(EqvarName::Rf);
} else {
Real64 flux3 = Q - Qv(Ta, Pa) - (Tc - Ta) / Ra_un(Tc, Ta) - (phi_salt * pvstar(Tc) - Pa) / Za_un;
if (flux3 < 0.0) {
return static_cast<int>(EqvarName::Rs);
} else {
return static_cast<int>(EqvarName::DTcdt);
}
}
}

// given T and RH, returns a key and value pair
std::tuple<int, Real64> find_eqvar(EnergyPlusData &state, Real64 Ta, Real64 RH)
Real64 find_eqvar_value(EnergyPlusData &state, Real64 Ta, Real64 RH)
{
Real64 Pa = RH * pvstar(Ta);
Real64 Rs = 0.0387;
Expand Down Expand Up @@ -380,16 +428,12 @@ namespace ExtendedHI {
std::max(Tc, Ta) + Rs * std::abs(m_bar));
Real64 flux1 = Q - Qv(Ta, Pa) - (1.0 - phi) * (Tc - Ts) / Rs;
Real64 flux2 = Q - Qv(Ta, Pa) - (1.0 - phi) * (Tc - Ts) / Rs - phi * (Tc - Tf) / Rs;
int eqvar_name;
Real64 Rf;

if (flux1 <= 0.0) {
eqvar_name = static_cast<int>(EqvarName::Phi);
phi = 1.0 - (Q - Qv(Ta, Pa)) * Rs / (Tc - Ts);
Rf = std::numeric_limits<Real64>::infinity();
return {eqvar_name, phi};
return phi;
} else if (flux2 <= 0.0) {
eqvar_name = static_cast<int>(EqvarName::Rf);
Real64 Ts_bar = Tc - (Q - Qv(Ta, Pa)) * Rs / phi + (1.0 / phi - 1.0) * (Tc - Ts);
General::SolveRoot(
state,
Expand All @@ -404,9 +448,8 @@ namespace ExtendedHI {
Ta,
Ts_bar);
Rf = Ra_bar(Tf, Ta) * (Ts_bar - Tf) / (Tf - Ta);
return {eqvar_name, Rf};
return Rf;
} else {
Rf = 0.0;
Real64 flux3 = Q - Qv(Ta, Pa) - (Tc - Ta) / Ra_un(Tc, Ta) - (phi_salt * pvstar(Tc) - Pa) / Za_un;
if (flux3 < 0.0) {
General::SolveRoot(
Expand All @@ -419,7 +462,6 @@ namespace ExtendedHI {
0.0,
Tc);
Rs = (Tc - Ts) / (Q - Qv(Ta, Pa));
eqvar_name = static_cast<int>(EqvarName::Rs);
Real64 Ps = Pc - (Pc - Pa) * Zs(Rs) / (Zs(Rs) + Za_un);
if (Ps > phi_salt * pvstar(Ts)) {
General::SolveRoot(
Expand All @@ -433,12 +475,11 @@ namespace ExtendedHI {
Tc);
Rs = (Tc - Ts) / (Q - Qv(Ta, Pa));
}
return {eqvar_name, Rs};
return Rs;
} else {
Rs = 0.0;
eqvar_name = static_cast<int>(EqvarName::DTcdt);
dTcdt = (1.0 / C) * flux3;
return {eqvar_name, dTcdt};
return dTcdt;
}
}
}
Expand Down Expand Up @@ -480,9 +521,8 @@ namespace ExtendedHI {
// RH: relative humidity in range of 0.0 to 1.0
// The function computes the extended heat index, in Kelvinn

auto eqvars = find_eqvar(state, Ta, RH);
int eqvar_name = std::get<0>(eqvars);
Real64 eqvar_value = std::get<1>(eqvars);
int eqvar_name = find_eqvar_name(state, Ta, RH);
Real64 eqvar_value = find_eqvar_value(state, Ta, RH);

Real64 T = find_T(state, eqvar_name, eqvar_value);

Expand Down
3 changes: 2 additions & 1 deletion src/EnergyPlus/ExtendedHI.hh
Original file line number Diff line number Diff line change
Expand Up @@ -77,10 +77,11 @@ namespace ExtendedHI {
Real64 Ra(Real64 Ts, Real64 Ta);
Real64 Ra_bar(Real64 Tf, Real64 Ta);
Real64 Ra_un(Real64 Ts, Real64 Ta);
int find_eqvar_name(EnergyPlusData &state, Real64 Ta, Real64 RH);
Real64 find_eqvar_value(EnergyPlusData &state, Real64 Ta, Real64 RH);
Real64 find_eqvar_phi(EnergyPlusData &state, Real64 Ta, Real64 RH);
Real64 find_eqvar_Rf(EnergyPlusData &state, Real64 Ta, Real64 RH);
Real64 find_eqvar_rs(EnergyPlusData &state, Real64 Ta, Real64 RH);
std::tuple<int, double> find_eqvar(EnergyPlusData &state, double Ta, double RH);
Real64 find_T(EnergyPlusData &state, int eqvar_name, Real64 eqvar);
Real64 heatindex(EnergyPlusData &state, Real64 Ta, Real64 RH);

Expand Down
5 changes: 2 additions & 3 deletions tst/EnergyPlus/unit/ExtendedHI.unit.cc
Original file line number Diff line number Diff line change
Expand Up @@ -273,9 +273,8 @@ TEST_F(EnergyPlusFixture, extendedHI_find_eqvar)

for (size_t i = 0; i < Ta_values.size(); ++i) {
for (size_t j = 0; j < RH_values.size(); ++j) {
auto const output = ExtendedHI::find_eqvar(*state, Ta_values[i], RH_values[j]);
EXPECT_EQ(std::get<0>(output), result_0[i][j]);
EXPECT_NEAR(std::get<1>(output), result_1[i][j], tol);
EXPECT_EQ(find_eqvar_name(*state, Ta_values[i], RH_values[j]), result_0[i][j]);
EXPECT_NEAR(find_eqvar_value(*state, Ta_values[i], RH_values[j]), result_1[i][j], tol);
}
}
}
Expand Down

5 comments on commit 473e566

@nrel-bot-3
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

extendedHI (Unknown) - x86_64-MacOS-10.18-clang-15.0.0: OK (3587 of 3655 tests passed, 725 test warnings)

Messages:\n

  • 787 tests had: EIO diffs.
  • 219 tests had: ESO small diffs.
  • 194 tests had: MTR small diffs.
  • 142 tests had: Table small diffs.
  • 51 tests had: Table string diffs.
  • 13 tests had: ESO big diffs.
  • 19 tests had: ERR diffs.
  • 1 test had: JSON small diffs.
  • 30 tests had: Table big diffs.
  • 2 tests had: EDD diffs.
  • 1 test had: JSON big diffs.
  • 1 test had: MTR big diffs.

Failures:\n

EnergyPlusFixture Test Summary

  • Passed: 1576
  • Failed: 2

regression Test Summary

  • Passed: 725
  • Failed: 66

Build Badge Test Badge

@nrel-bot-2c
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

extendedHI (Unknown) - x86_64-Linux-Ubuntu-22.04-gcc-11.4-IntegrationCoverage-Debug: Tests Failed (148 of 795 tests passed, 0 test warnings)

Failures:\n

integration Test Summary

  • Passed: 148
  • Failed: 647

Build Badge Test Badge Coverage Badge

@nrel-bot-2b
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

extendedHI (Unknown) - x86_64-Linux-Ubuntu-22.04-gcc-11.4-UnitTestsCoverage-Debug: OK (2054 of 2071 tests passed, 0 test warnings)

Failures:\n

API Test Summary

  • Passed: 5
  • Failed: 1
  • NUMERICAL: 9

EnergyPlusFixture Test Summary

  • Passed: 1576
  • Failed: 2

Build Badge Test Badge Coverage Badge

@nrel-bot-2
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

extendedHI (Unknown) - x86_64-Linux-Ubuntu-22.04-gcc-11.4: OK (3627 of 3696 tests passed, 728 test warnings)

Messages:\n

  • 791 tests had: EIO diffs.
  • 227 tests had: ESO small diffs.
  • 196 tests had: MTR small diffs.
  • 148 tests had: Table small diffs.
  • 12 tests had: ESO big diffs.
  • 17 tests had: ERR diffs.
  • 53 tests had: Table string diffs.
  • 32 tests had: Table big diffs.
  • 2 tests had: EDD diffs.
  • 1 test had: JSON big diffs.
  • 1 test had: MTR big diffs.

Failures:\n

EnergyPlusFixture Test Summary

  • Passed: 1576
  • Failed: 2

regression Test Summary

  • Passed: 744
  • Failed: 67

Build Badge Test Badge

@nrel-bot
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

extendedHI (Unknown) - Win64-Windows-10-VisualStudio-16: OK (2861 of 2863 tests passed, 0 test warnings)

Failures:\n

EnergyPlusFixture Test Summary

  • Passed: 1574
  • Failed: 2

Build Badge Test Badge

Please sign in to comment.