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

Convert compressor operation from int constexpr to enum #9199

Merged
merged 26 commits into from
Jan 6, 2022
Merged

Conversation

jmythms
Copy link
Contributor

@jmythms jmythms commented Nov 30, 2021

This PR attempts to convert compressor operation in multiple files specified by int constexpr to enum class.

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

@jmythms jmythms added the Refactoring Includes code changes that don't change the functionality of the program, just perform refactoring label Nov 30, 2021
@jmythms jmythms added this to the EnergyPlus 22.1 milestone Nov 30, 2021
@jmythms jmythms self-assigned this Nov 30, 2021
Comment on lines 369 to 376
// Compressor operation
enum class CompressorOperation
{
Unassigned = -1,
Off, // signal DXCoil that compressor shouldn't run
On, // normal compressor operation
Num
};
Copy link
Contributor Author

Choose a reason for hiding this comment

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

CompressorOperation enum class exists in DataHVACGlobals.hh.

@@ -9794,7 +9869,7 @@ namespace Furnaces {
FirstHVACIteration = false;
}
FanOpMode = int(Par(3));
CompOp = int(Par(4));
CompOp = static_cast<CompressorOperation>(Par(4));
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 par(*) values that are parameters and results of the residual functions had to be static_cast.

Copy link
Collaborator

Choose a reason for hiding this comment

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

These things need to be converted to lambdas anyway. There is already an open issue about that. That conversion will make these casts unnecessary.

@@ -54,6 +54,7 @@

// EnergyPlus Headers
#include <EnergyPlus/Data/BaseData.hh>
#include <EnergyPlus/DataHVACGlobals.hh>
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Had to include the DataHVACGlobals.hh here.

Comment on lines 6276 to 6277
CompressorOperation constexpr iZero(CompressorOperation::Off);
CompressorOperation constexpr iOne(CompressorOperation::On);
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Will refactor 😅

@@ -233,7 +231,7 @@ namespace UnitarySystems {
Real64 &sysOutputProvided,
Real64 &latOutputProvided)
{
int CompOn = 0;
DataHVACGlobals::CompressorOperation CompOn = DataHVACGlobals::CompressorOperation::Off;
Copy link
Contributor Author

Choose a reason for hiding this comment

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

A using namespace DataHVACGlobals; statement will shorten these lines, make them more readable. Is that recommended?

Copy link
Collaborator

Choose a reason for hiding this comment

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

I think we are trying to avoid using statements in general. The thing to do here is to shorten the name of the namespace itself. There should only be a dozen or so namespaces (there is no one-to-one relationship between namespace and file) and they should have names like HVAC and Const not DataGlobalConstants and DataHVACGlobals. You don't need long names when you have only 12 of something.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Got it. Should be a quick refactor when it happens.

Comment on lines 11201 to 11205
if (CompOn == DataHVACGlobals::CompressorOperation::On) {
this->m_CoolCompPartLoadRatio = PartLoadRatio;
} else {
this->m_CoolCompPartLoadRatio = 0;
}
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Unrolled these multiplying with double(CompOn) to if else statements checking if the Compressor is on or of.

Copy link
Collaborator

Choose a reason for hiding this comment

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

A shorthand for this idiom is:

this->m_CoolCompPartLoadRatio = DataHVACGlobals::CompressorOperation::On ? PartLoadRatio : 0.0;

Copy link
Contributor

Choose a reason for hiding this comment

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

Um ...

this->m_CoolCompPartLoadRatio = CompOn ? PartLoadRatio : 0.0;

Copy link
Collaborator

@amirroth amirroth Nov 30, 2021

Choose a reason for hiding this comment

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

You caught me! Actually for clarity and type safety it should be:

this->m_CoolCompPartLoadRatio = (compOp == DataHVACGlobals::CompressorOperation::On) ? PartLoadRatio : 0.0;

Copy link
Collaborator

Choose a reason for hiding this comment

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

By the way, I made that mistake on purpose to see if anyone was paying attention! 😉

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Will make the changes, thanks for the suggestion! 😃

Copy link
Contributor

Choose a reason for hiding this comment

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

#9052 should easily merge with all these changes ...

@@ -54,6 +54,7 @@

// EnergyPlus headers
#include <EnergyPlus/Data/BaseData.hh>
#include <EnergyPlus/DataHVACGlobals.hh>
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Another #include <EnergyPlus/DataHVACGlobals.hh> statement addition here.

int const AirLoopNum, // index to air loop
bool const FirstHVACIteration, // True when first HVAC iteration
Real64 const PartLoadRatio, // coil operating part-load ratio
DataHVACGlobals::CompressorOperation CompOn, // compressor control (0=off, 1=on)
Copy link
Contributor Author

@jmythms jmythms Nov 30, 2021

Choose a reason for hiding this comment

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

I removed const qualifiers in declarations wherever I saw them in the code I was changing. Suggestion from clang-tidy.

int const CompOp, // compressor operation; 1=on, 0=off
bool const FirstHVACIteration, // True when first HVAC iteration
std::string_view CompName, // name of the fan coil unit
CompressorOperation const CompOp, // compressor operation; 1=on, 0=off
Copy link
Collaborator

Choose a reason for hiding this comment

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

I hate to be pedantic (actually, I love it) but I think ComprOp or CompressOp or CompressorOp is better than CompOp. We are already using Comp to mean component (see preceding parameter), we shouldn't also use it to mean compressor.

Copy link
Contributor Author

@jmythms jmythms Nov 30, 2021

Choose a reason for hiding this comment

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

Will change to CompressorOp 👍🏽

@@ -229,19 +229,21 @@ void SimDXCoil(EnergyPlusData &state,
// call the HPWHDXCoil routine to calculate water side performance set up the DX coil info for air-side calcs
CalcHPWHDXCoil(state, DXCoilNum, PartLoadRatio);
// CALL CalcDoe2DXCoil(state, DXCoilNum, CompOp, FirstHVACIteration,PartLoadRatio), perform air-side calculations
CalcDoe2DXCoil(state, DXCoilNum, 1, FirstHVACIteration, PartLoadRatio, FanOpMode);
CalcDoe2DXCoil(state, DXCoilNum, CompressorOperation::On, FirstHVACIteration, PartLoadRatio, FanOpMode);
Copy link
Collaborator

Choose a reason for hiding this comment

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

I am not sure how many more of these kinds of things we have in EnergyPlus, but replacing integers with symbols is always a good thing. Make the code much more understandable and maintainable. This is a pattern we should propagate maximally.

S1PLR = PartLoadRatio;
S2PLR = 0.0;
} else {
// If a two-stage coil
// Run stage 1 at full load
PerfMode = DehumidMode * 2 + 1;
CalcDoe2DXCoil(state, DXCoilNum, On, FirstHVACIteration, 1.0, FanOpMode, PerfMode);
CalcDoe2DXCoil(state, DXCoilNum, CompressorOperation::On, FirstHVACIteration, 1.0, FanOpMode, PerfMode);
Copy link
Collaborator

Choose a reason for hiding this comment

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

Not part of this PR, but I would also like to see some kind of pattern where non-symbolic arguments are documented inline, e.g.,:

CalcDoe2DXCoil(state, DXCoilNum, CompressorOperation::On, FirstHVACIteration, /* PartLoadRatio */1.0, FanOpMode, PerfMode);

Or

CalcDoe2DXCoil(state, DXCoilNum, CompressorOperation::On, FirstHVACIteration, 
                             1.0, // PartLoadRatio 
                             FanOpMode, PerfMode);

Copy link
Contributor

Choose a reason for hiding this comment

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

I like the first one, and never even thought of doing that.

Copy link
Collaborator

Choose a reason for hiding this comment

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

I like the first one also, but I think we are trying to avoid using C-style comments. @Myoldmopar, can you advise?

Copy link
Member

Choose a reason for hiding this comment

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

Correct, we agreed to make C style comments illegal because they make parsing the code during refactors more difficult. We have a CI script that will fail if you add C style arguments, so the C++ style will be the way to go.

@@ -9534,7 +9536,7 @@ void CalcHPWHDXCoil(EnergyPlusData &state,

void CalcDoe2DXCoil(EnergyPlusData &state,
int const DXCoilNum, // the number of the DX coil to be simulated
int const CompOp, // compressor operation; 1=on, 0=off
CompressorOperation const CompOp, // compressor operation; 1=on, 0=off
Copy link
Collaborator

Choose a reason for hiding this comment

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

Same comment about name CompOp.

// Compressor operation
enum class CompressorOperation
{
Unassigned = -1,
Copy link
Collaborator

Choose a reason for hiding this comment

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

I think @mitchute is doing a pass that is converting Unassigned to 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.

I will change this one in this PR.

} else {
SetOnOffMassFlowRate(state, FurnaceNum, AirLoopNum, OnOffAirFlowRatio, OpMode, QZnReq, MoistureLoad, PartLoadRatio);
CalcFurnaceOutput(
state, FurnaceNum, false, 0, Off, 0.0, 0.0, 0.0, 0.0, SensibleOutput, LatentOutput, OnOffAirFlowRatio, false);
CalcFurnaceOutput(state,
Copy link
Collaborator

Choose a reason for hiding this comment

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

Why is this function called CalcFurnaceOutput and not CalcFurnace?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

There is another function called CalcFurnaceResidual that calls CalcFurnaceOutput. Shall I rename CalcFurnaceOutput to CalcFurnace?

Copy link
Collaborator

Choose a reason for hiding this comment

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

Don't bother. I shouldn't have said anything.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

😬

@@ -977,7 +977,7 @@ namespace SZVAVModel {
bool const &HXUnitOn,
Copy link
Collaborator

Choose a reason for hiding this comment

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

I thought we had gotten rid of these bool const & and int const & parameters.


} else if (SELECT_CASE_var == DataHVACGlobals::CoilDX_Cooling) { // CoilCoolingDX

bool const singleMode = (this->m_SingleMode == 1);
if (this->m_ControlType == ControlType::Setpoint) {
if (this->m_CoolingSpeedNum > 1) {
Copy link
Collaborator

@amirroth amirroth Nov 30, 2021

Choose a reason for hiding this comment

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

Yeah, this type of stuff (casting enum to double) is not good. Should scrub this idiom.

@jmythms jmythms marked this pull request as ready for review December 1, 2021 15:14
Comment on lines -69 to -71
// coil operation
constexpr int CoilOn(1); // normal coil operation
constexpr int CoilOff(0); // signal coil shouldn't run
Copy link
Contributor Author

Choose a reason for hiding this comment

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

These CoilOn and CoilOff variables were replaced with CompressorOperation::On and CompressorOperation::Off respectively. Reason: the argument of SimHXAssistedCoolingCoil() where they are used signals compressor operation.

@jmythms
Copy link
Contributor Author

jmythms commented Dec 1, 2021

The comments have been addressed and this is ready for review.

@Myoldmopar
Copy link
Member

This one next? Seems like it is still ready for review, I'll pull develop and apply Clang Format here as well.

@Myoldmopar
Copy link
Member

Super happy here, merging.

@Myoldmopar Myoldmopar merged commit 6b569b8 into develop Jan 6, 2022
@Myoldmopar Myoldmopar deleted the enumCompOp branch January 6, 2022 16:00
@jmythms
Copy link
Contributor Author

jmythms commented Jan 6, 2022

Thank you! :)

yujiex pushed a commit that referenced this pull request Jan 27, 2022
Convert compressor operation from int constexpr to enum
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Refactoring Includes code changes that don't change the functionality of the program, just perform refactoring
Projects
None yet
Development

Successfully merging this pull request may close these issues.

9 participants