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

Fix #10899 - Output:Table:Monthly: SumOrAverageDuringHoursShown doesn't follow previous variable #10901

Open
wants to merge 3 commits into
base: develop
Choose a base branch
from

Conversation

jmarrec
Copy link
Contributor

@jmarrec jmarrec commented Jan 21, 2025

Pull request overview

Pull Request Author

Add to this list or remove from it as applicable. This is a simple templated set of guidelines.

  • Title of PR should be user-synopsis style (clearly understandable in a standalone changelog context)
  • Label the PR with at least one of: Defect, Refactoring, NewFeature, Performance, and/or DoNoPublish
  • Pull requests that impact EnergyPlus code must also include unit tests to cover enhancement or defect repair
  • Author should provide a "walkthrough" of relevant code changes using a GitHub code review comment process
  • If any diffs are expected, author must demonstrate they are justified using plots and descriptions
  • If changes fix a defect, the fix should be demonstrated in plots and descriptions
  • If any defect files are updated to a more recent version, upload new versions here or on DevSupport
  • If IDD requires transition, transition source, rules, ExpandObjects, and IDFs must be updated, and add IDDChange label
  • If structural output changes, add to output rules file and add OutputChange label
  • If adding/removing any LaTeX docs or figures, update that document's CMakeLists file dependencies

Reviewer

This will not be exhaustively relevant to every PR.

  • Perform a Code Review on GitHub
  • If branch is behind develop, merge develop and build locally to check for side effects of the merge
  • If defect, verify by running develop branch and reproducing defect, then running PR and reproducing fix
  • If feature, test running new feature, try creative ways to break it
  • CI status: all green or justified
  • Check that performance is not impacted (CI Linux results include performance check)
  • Run Unit Test(s) locally
  • Check any new function arguments for performance impacts
  • Verify IDF naming conventions and styles, memos and notes and defaults
  • If new idf included, locally check the err file and other outputs

@jmarrec jmarrec added the Defect Includes code to repair a defect in EnergyPlus label Jan 21, 2025
@jmarrec jmarrec self-assigned this Jan 21, 2025
Comment on lines +178 to +192
constexpr std::array<std::string_view, (int)AggType::Num> AggTypeNamesUC{
"SUMORAVERAGE",
"MAXIMUM",
"MINIMUM",
"VALUEWHENMAXIMUMORMINIMUM",
"HOURSZERO",
"HOURSNONZERO",
"HOURSPOSITIVE",
"HOURSNONPOSITIVE",
"HOURSNEGATIVE",
"HOURSNONNEGATIVE",
"SUMORAVERAGEDURINGHOURSSHOWN",
"MAXIMUMDURINGHOURSSHOWN",
"MINIMUMDURINGHOURSSHOWN",
};
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Unrelated, but started by adding a getEnumValues call to replace a big if-else (1/2)

Comment on lines +394 to +397
// kind of aggregation identified (see AggType parameters)
AggType curAggType = static_cast<AggType>(getEnumValue(AggTypeNamesUC, Util::makeUPPER(curAggString)));
// set accumulator values to default as appropriate for aggregation type
if (Util::SameString(curAggString, "SumOrAverage")) {
curAggType = AggType::SumOrAvg;
} else if (Util::SameString(curAggString, "Maximum")) {
curAggType = AggType::Maximum;
} else if (Util::SameString(curAggString, "Minimum")) {
curAggType = AggType::Minimum;
} else if (Util::SameString(curAggString, "ValueWhenMaximumOrMinimum")) {
curAggType = AggType::ValueWhenMaxMin;
} else if (Util::SameString(curAggString, "HoursZero")) {
curAggType = AggType::HoursZero;
} else if (Util::SameString(curAggString, "HoursNonzero")) {
curAggType = AggType::HoursNonZero;
} else if (Util::SameString(curAggString, "HoursPositive")) {
curAggType = AggType::HoursPositive;
} else if (Util::SameString(curAggString, "HoursNonpositive")) {
curAggType = AggType::HoursNonPositive;
} else if (Util::SameString(curAggString, "HoursNegative")) {
curAggType = AggType::HoursNegative;
} else if (Util::SameString(curAggString, "HoursNonnegative")) {
curAggType = AggType::HoursNonNegative;
} else if (Util::SameString(curAggString, "SumOrAverageDuringHoursShown")) {
curAggType = AggType::SumOrAverageHoursShown;
} else if (Util::SameString(curAggString, "MaximumDuringHoursShown")) {
curAggType = AggType::MaximumDuringHoursShown;
} else if (Util::SameString(curAggString, "MinimumDuringHoursShown")) {
curAggType = AggType::MinimumDuringHoursShown;
} else {
curAggType = AggType::SumOrAvg;
if (curAggType == AggType::Invalid) {
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Unrelated, but started by adding a getEnumValues call to replace a big if-else (2/2)

@@ -370,44 +388,18 @@ void GetInputTabularMonthly(EnergyPlusData &state)
format("{}: Blank column specified in '{}', need to provide a variable or meter name ",
CurrentModuleObject,
ort->MonthlyInput(TabNum).name));
continue;
Copy link
Contributor Author

Choose a reason for hiding this comment

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

This is because the previous chain was

for (int jField = 2; jField <= NumAlphas; jField += 2) {
    if (AlphArray(jField).empty())  {
        ShowWarningError(...);
    }
    AggType curAggType= ...;
    if (!AlphArray(jField).empty()) {
        AddMonthlyFieldSetInput(state, curTable, AlphArray(jField), "", curAggType);
    }
}

I just simplified it (if the field is blank and is going to be ignored, there's not really a good value addition of checking the AggType is valid anyways):

for (int jField = 2; jField <= NumAlphas; jField += 2) {
    if (AlphArray(jField).empty())  {
        ShowWarningError(...);
        continue;
    }

    AggType curAggType= ...;
    AddMonthlyFieldSetInput(state, curTable, AlphArray(jField), "", curAggType);
}

src/EnergyPlus/OutputReportTabular.cc Outdated Show resolved Hide resolved
@@ -3775,7 +3767,11 @@ void GatherMonthlyResultsForTimestep(EnergyPlusData &state, OutputProcessor::Tim
// If the hours variable is active then scan through the rest of the variables
// and accumulate
if (activeHoursShown) {
bool exit_loop = false;
Copy link
Contributor Author

Choose a reason for hiding this comment

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

sentinal variable to know when to break the for loop

@@ -3789,6 +3785,7 @@ void GatherMonthlyResultsForTimestep(EnergyPlusData &state, OutputProcessor::Tim
case AggType::HoursNegative:
case AggType::HoursNonNegative:
// end scanning since these might reset
exit_loop = true;
Copy link
Contributor Author

Choose a reason for hiding this comment

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

The issue was that an if else ladder inside a loop was incorrectly refactored to a switch case:

This was the original thing

for (int i = ....) {
  SELECT_CASE_var = XXXX
  if (SELECT_CASE_var == blabla) {
    break; // THIS BREAKS THE FOR LOOP
   } else ...
}

The refactor:

for (int i = ....) {
  switch (XXX) {
  case blabla:
    break; // THIS DO NOT BREAK THE FOR LOOP, it just prevents other `case`s to be processed (fallthrough)
  }
  ...
 }

tst/EnergyPlus/unit/OutputReportTabular.unit.cc Outdated Show resolved Hide resolved
Comment on lines 13683 to 13696
std::string const idf_objects = delimited_string({

"Output:Table:Monthly,",
" Test, !- Name",
" 3, !- Digits After Decimal",
" ConditionA, !- Variable or Meter 1 Name",
" HoursNonZero, !- Aggregation Type for Variable or Meter 1",
" VariableToBeSummedDuringHoursShown, !- Variable or Meter 2 Name",
" SumOrAverageDuringHoursShown, !- Aggregation Type for Variable or Meter 2",
" ConditionA_Inverse, !- Variable or Meter 3 Name",
" HoursNonZero, !- Aggregation Type for Variable or Meter 3",
" VariableToBeSummedDuringHoursShown, !- Variable or Meter 4 Name",
" SumOrAverageDuringHoursShown, !- Aggregation Type for Variable or Meter 4",
" VariableToBeSummedDuringHoursShown, !- Variable or Meter 5 Name",
" SumOrAverage; !- Aggregation Type for Variable or Meter 5",
Copy link
Contributor Author

Choose a reason for hiding this comment

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

test input data, mimics the defect file in #10899

Comment on lines 13703 to 13720
Real64 /* const */ VariableToBeSummedDuringHourShown = 0.0;
Real64 ConditionA = 0.0;
Real64 ConditionNotA = 0.0;

Real64 expectedTotalConditionA = 0.0;
Real64 expectedTotalConditionNotA = 0.0;

auto setConditionAB = [&ConditionA, &ConditionNotA, &expectedTotalConditionA, &expectedTotalConditionNotA](bool isConditionA) {
if (isConditionA) {
ConditionA = 1.0;
ConditionNotA = 0.0;
} else {
ConditionA = 0.0;
ConditionNotA = 1.0;
}
expectedTotalConditionA += ConditionA;
expectedTotalConditionNotA += ConditionNotA;
};
Copy link
Contributor Author

Choose a reason for hiding this comment

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

I'm going to be checking:

  • VariableToBeSummedDuringHourShown when Condition A
  • VariableToBeSummedDuringHourShown when Condition is not(A)
  • VariableToBeSummedDuringHourShown with no condition

Comment on lines 13785 to 13793
{
VariableToBeSummedDuringHourShown = 1.0;
setConditionAB(true);
GatherMonthlyResultsForTimestep(*state, OutputProcessor::TimeStepType::Zone);
compare_err_stream("");
EXPECT_EQ(1.0, ort->MonthlyColumns(colConditionA).reslt(12));
EXPECT_EQ(1.0, ort->MonthlyColumns(colValueWhenConditionA).reslt(12));
EXPECT_EQ(0.0, ort->MonthlyColumns(colConditionNotA).reslt(12));
EXPECT_EQ(0.0, ort->MonthlyColumns(colValueWhenConditionNotA).reslt(12));
EXPECT_EQ(1.0, ort->MonthlyColumns(colValue).reslt(12));
Copy link
Contributor Author

Choose a reason for hiding this comment

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

It already goes sideways before fix here.

You expect colValueWhenConditionNotA to be zero, yet it's 1.0 here.

Full test output before fix

[ RUN      ] EnergyPlusFixture.OutputReportTabularMonthly_HandleMultipleDuringHoursShown
/home/julien/Software/Others/EnergyPlus/tst/EnergyPlus/unit/OutputReportTabular.unit.cc:13785: Failure
Expected equality of these values:
  0.0
    Which is: 0
  ort->MonthlyColumns(colValueWhenConditionNotA).reslt(12)
    Which is: 1
/home/julien/Software/Others/EnergyPlus/tst/EnergyPlus/unit/OutputReportTabular.unit.cc:13795: Failure
Expected equality of these values:
  0.0
    Which is: 0
  ort->MonthlyColumns(colValueWhenConditionNotA).reslt(12)
    Which is: 2
/home/julien/Software/Others/EnergyPlus/tst/EnergyPlus/unit/OutputReportTabular.unit.cc:13805: Failure
Expected equality of these values:
  1.0
    Which is: 1
  ort->MonthlyColumns(colValueWhenConditionNotA).reslt(12)
    Which is: 3
/home/julien/Software/Others/EnergyPlus/tst/EnergyPlus/unit/OutputReportTabular.unit.cc:13815: Failure
Expected equality of these values:
  2.0
    Which is: 2
  ort->MonthlyColumns(colValueWhenConditionNotA).reslt(12)
    Which is: 4
/home/julien/Software/Others/EnergyPlus/tst/EnergyPlus/unit/OutputReportTabular.unit.cc:13824: Failure
Expected equality of these values:
  2.0
    Which is: 2
  ort->MonthlyColumns(colValueWhenConditionNotA).reslt(12)
    Which is: 5
[  FAILED  ] EnergyPlusFixture.OutputReportTabularMonthly_HandleMultipleDuringHoursShown (2280 ms)

```
[ RUN      ] EnergyPlusFixture.OutputReportTabularMonthly_HandleMultipleDuringHoursShown
/home/julien/Software/Others/EnergyPlus/tst/EnergyPlus/unit/OutputReportTabular.unit.cc:13785: Failure
Expected equality of these values:
  0.0
    Which is: 0
  ort->MonthlyColumns(colValueWhenConditionNotA).reslt(12)
    Which is: 1
/home/julien/Software/Others/EnergyPlus/tst/EnergyPlus/unit/OutputReportTabular.unit.cc:13795: Failure
Expected equality of these values:
  0.0
    Which is: 0
  ort->MonthlyColumns(colValueWhenConditionNotA).reslt(12)
    Which is: 2
/home/julien/Software/Others/EnergyPlus/tst/EnergyPlus/unit/OutputReportTabular.unit.cc:13805: Failure
Expected equality of these values:
  1.0
    Which is: 1
  ort->MonthlyColumns(colValueWhenConditionNotA).reslt(12)
    Which is: 3
/home/julien/Software/Others/EnergyPlus/tst/EnergyPlus/unit/OutputReportTabular.unit.cc:13815: Failure
Expected equality of these values:
  2.0
    Which is: 2
  ort->MonthlyColumns(colValueWhenConditionNotA).reslt(12)
    Which is: 4
/home/julien/Software/Others/EnergyPlus/tst/EnergyPlus/unit/OutputReportTabular.unit.cc:13824: Failure
Expected equality of these values:
  2.0
    Which is: 2
  ort->MonthlyColumns(colValueWhenConditionNotA).reslt(12)
    Which is: 5
[  FAILED  ] EnergyPlusFixture.OutputReportTabularMonthly_HandleMultipleDuringHoursShown (2280 ms)
```
The issue was that an if else ladder inside a loop was incorrectly refactored to a switch case:

This was the original thing
```c++
for (int i = ....) {
  SELECT_CASE_var = XXXX
  if (SELECT_CASE_var == blabla) {
    break; // THIS BREAKS THE FOR LOOP
   } else ...
}
```

The refactor:

```c++
for (int i = ....) {
  switch (XXX) {
  case blabla:
    break; // THIS DO NOT BREAK THE FOR LOOP, it just prevents other `case`s to be processed (fallthrough)
  }
  ...
 }
```

255e7e939fc?w=1#diff-79e778e07bedf79acf027aa76c1235badd89a6f3014b6f0cdadc4ada74402ae3R3817-R3827
@jmarrec jmarrec force-pushed the 10899_OutputTableMonthly_DuringHours branch from ed14832 to 78f9058 Compare January 21, 2025 08:41
@jmarrec jmarrec requested a review from Myoldmopar January 23, 2025 09:11
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Defect Includes code to repair a defect in EnergyPlus
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Output:Table:Monthly: SumOrAverageDuringHoursShown doesn't follow previous variable
2 participants