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

CppCheck expm1 log1p HeatRecovery #10880

Merged
merged 1 commit into from
Jan 9, 2025
Merged
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
8 changes: 4 additions & 4 deletions src/EnergyPlus/HeatRecovery.cc
Original file line number Diff line number Diff line change
Expand Up @@ -3133,7 +3133,7 @@ namespace HeatRecovery {
} break;
case HXConfiguration::CrossFlowBothUnmixed: { // CROSS FLOW BOTH UNMIXED
Temp = Z * std::pow(NTU, -0.22);
Eps = 1.0 - std::exp((std::exp(-NTU * Temp) - 1.0) / Temp);
Eps = 1.0 - std::exp(std::expm1(-NTU * Temp) / Temp);
Copy link
Member

@Myoldmopar Myoldmopar Jan 9, 2025

Choose a reason for hiding this comment

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

It's mildly confusing to me that

expm1(n) is the same as exp(n) - 1

but

log1p(n) is the same as log(n + 1).

I understand the difference, but at first glance I thought maybe that was the cause of diffs here.

Copy link
Contributor Author

@rraustad rraustad Jan 9, 2025

Choose a reason for hiding this comment

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

These are supposed to be more precise. So consider these improvements that could possibly change the answer slightly.

log1p(n) = log(1 + n)

I did these individually by module because there were diffs when I combined them all in 1 branch and I wanted to be sure which change caused which diffs.

Copy link
Member

Choose a reason for hiding this comment

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

Indeed it all makes sense. And it really can't be worth the time to investigate the diffs further. You've done a great job already, I feel comfortable merging the three log/exp branches, as it is crystal clear that they are just precision improvements.

} break;
case HXConfiguration::CrossFlowOther: { // CROSS FLOW, Cmax MIXED, Cmin UNMIXED
Eps = (1.0 - std::exp(-Z * (1.0 - std::exp(-NTU)))) / Z;
Expand Down Expand Up @@ -3221,13 +3221,13 @@ namespace HeatRecovery {
}
} break;
case HXConfiguration::ParallelFlow: { // PARALLEL FLOW
NTU = -std::log(-Eps - Eps * Z + 1.0) / (Z + 1.0);
NTU = -std::log1p(-Eps - Eps * Z) / (Z + 1.0);
} break;
case HXConfiguration::CrossFlowBothUnmixed: { // CROSS FLOW BOTH UNMIXED
NTU = GetNTUforCrossFlowBothUnmixed(state, Eps, Z);
} break;
case HXConfiguration::CrossFlowOther: { // CROSS FLOW, Cmax MIXED, Cmin UNMIXED
NTU = -std::log(1.0 + std::log(1.0 - Eps * Z) / Z);
NTU = -std::log1p(std::log(1.0 - Eps * Z) / Z);
} break;
default: {
ShowFatalError(state, format("HeatRecovery: Illegal flow arrangement in CalculateNTUfromEpsAndZ, Value={}", FlowArr));
Expand Down Expand Up @@ -3273,7 +3273,7 @@ namespace HeatRecovery {
int SolFla; // Flag of solver
Real64 constexpr NTU0(0.0); // lower bound for NTU
Real64 constexpr NTU1(50.0); // upper bound for NTU
auto f = [Eps, Z](Real64 const NTU) { return 1.0 - std::exp((std::exp(-std::pow(NTU, 0.78) * Z) - 1.0) / Z * std::pow(NTU, 0.22)) - Eps; };
auto f = [Eps, Z](Real64 const NTU) { return 1.0 - std::exp(std::expm1(-std::pow(NTU, 0.78) * Z) / Z * std::pow(NTU, 0.22)) - Eps; };
General::SolveRoot(state, Acc, MaxIte, SolFla, NTU, f, NTU0, NTU1);

if (SolFla == -2) {
Expand Down
Loading