openergy / opyplus

A package to work with EnergyPlus in python
Mozilla Public License 2.0
38 stars 13 forks source link

[BUG]: Alphabetical order in Epm.save causes an error in the IDF file. #59

Closed AlejandroCN7 closed 2 years ago

AlejandroCN7 commented 2 years ago

Describe the bug If I save an IDF with opyplus, it saves the records and tables in alphabetical order, which causes the subsequent simulation with energyplus to generate an error.

To Reproduce Steps to reproduce the behavior:

  1. Download IDF file ASHRAE9012016_Warehouse_Denver.zip (With On/Off issue #58 fixed using Any Number instead of On/Off type) and EPW file USA_WA_Port.Angeles-William.R.Fairchild.Intl.AP.727885_TMY3.zip.
  2. Extract IDF and EPW files.
  3. Run energyplus -w USA_WA_Port.Angeles-William.R.Fairchild.Intl.AP.727885_TMY3.epw ASHRAE9012016_Warehouse_Denver.idf. You can check that it works.
  4. Now load IDF and save again with opyplus (with no change):
    
    from opyplus import Epm
    import os

building = Epm.from_idf( 'ASHRAE9012016_Warehouse_Denver.idf', check_length=False)

building.save('saved.idf')

5. Now execute the same energyplus simulation with **saved.idf**: `energyplus -w USA_WA_Port.Angeles-William.R.Fairchild.Intl.AP.727885_TMY3.epw saved.idf`. You will have the next error in simulation:

Severe Problem found in EMS EnergyPlus Runtime Language. ~~~ Erl program name: PSZ_FINE_SET_FAN_PAR ~~~ Erl program line number: 1 ~~~ Erl program line text: IF PSZ_FINE_NUMBER_OF_STAGES == 1 ~~~ Error message: Error: EvaluateExpression: Variable = 'PSZ_FINE_NUMBER_OF_STAGES' used in expression has not been initialized! ~~~ During Warmup, Environment=WILLIAM R FAIRCHILD ANN CLG .4% CONDNS DB=>MWB, at Simulation time=01/01 24:00 - 24:15 Fatal Previous EMS error caused program termination.



**Expected behavior**
Since nothing has been modified, the saved IDF should work in the same way as the original one. However, this doesn't happen.
I have been doing some research on this error, and it is because all items are stored in alphabetical order. Could this order be made optional or configurable?
thomascerbelaud commented 2 years ago

@AlejandroCN7, thank you for reporting this. I succeeded in reproducing this bug, however it seems it goes against EnergyPlus fundamentals that "no order is needed in the input data file! Data that is not needed by a particular simulation is not processed." (From the Input/Output reference, section 1.1.1). I will try to figure out what is the best way to deal with it. A simple fix would be to remove the alphabetical ordering indeed, but there may be a better way to fix it.

@geoffroy-destaintot is there any reason why we sort objects when saving the Epm? Besides comparing two models?

thomascerbelaud commented 2 years ago

@AlejandroCN7 I had the time to dig into this issue, and actually the order matter in the IDF for EnergyManagementSystem:ProgramCallingManager. While the order can be specified within this object by referencing multiple programs in the right order under the right calling point - which would make the IDF robust to change in object order - I think it is reasonable to remove the alphabetical ordering when saving the Epm for these objects specifically, so the order is preserved (whether it is the order from loading the model or for adding the objects into the Epm directly using opyplus).

@geoffroy-destaintot More generally, we may want to emphasise that the most robust way of specifying order in calling the different EMS programs is within the same EnergyManagementSystem:ProgramCallingManager, for each calling point. This way it is easier to modify an existing model.