1

Using python to edit and run idf files. Any experience?

I want to use a Python script to call EnergyPluse and run it. I was able to do it using this code:

    import subprocess
    path_to_energyplus = r"C:\EnergyPlusV22-1-0\EP-Launch.exe"
    path_to_idf = r"C:\Example.idf"
    command = [path_to_energyplus, path_to_idf]
    result = subprocess.run(command)

Now, my objective is to read and edit heating and cooling setpoints schedules in the Python script. Any help, idea, and comment is appreciated.

Ali-Khosravani's avatar
65
Ali-Khosravani
asked 2023-07-28 15:19:59 -0500
Aaron Boranian's avatar
14.1k
Aaron Boranian
updated 2023-07-30 11:53:31 -0500
edit flag offensive 0 remove flag close merge delete

Comments

add a comment see more comments

2 Answers

2

To interact with E+ during runtime you can use either plugin or library API - here is the starting point: https://bigladdersoftware.com/epx/doc...

There are some python libraries designed to modify idf file - https://pypi.org/project/eppy/ , https://github.com/rbuffat/pyidf However, I don't have much experience with these projects. I modify the idf file using simple regex find & replace.

kem's avatar
51
kem
answered 2023-07-31 01:44:47 -0500
edit flag offensive 0 remove flag delete link

Comments

add a comment see more comments
0

I agree with the above, but there are other options as well, which you might find either easier or more flexible:

You can use the option of defining your schedules in an external .csv file. This would allow you to make edits via Python to the csv rather than to the idf file. It also depends how complicated your heating and cooling setpoint schedules are. If they are a constant value all year, using regex commands (or f-strings, my other suggestion below) might be your best bet. But if the setpoints vary quite a bit throughout the year, I find the EnergyPlus text format to be cumbersome.

Another option that I use a lot is to use f-strings in Python. These allow you to insert Python variables into text files very easily. For example, you could define a heating setpoint variable in python called "htg_stpt" and set it to a value. Then, in your IDF, you can have something like this:

Schedule:Constant,
  heating_sch,             !- Name
  Temperature,             !- Schedule Type Limits Name
  {htg_stpt};                     !- Hourly Value

You need to be a little careful with this, though, because IDFs also use the "{ }" characters to define units. So, everywhere EnergyPlus uses "{ }" to define units, I just change them to "{{" and "}}". That way Python will not try to interpret the units as f-strings as well.

I have an open source project on GitHub called REEDR (https://github.com/PtarmiganConsultin...) that is essentially all Python code to build and run IDF files. If you look at the file "genmodels.py", you can see where I use the f-string trick, because it will look like this:

with open(TextFile, 'r') as f:
        TextString = f"{f.read()}".format(**locals())

This code opens a text file, substitutes any f-strings you have defined, and saves it to a new text string. You can then save this new text string as a new IDF file, which you would pass to your subprocess command.

cdouglass's avatar
46
cdouglass
answered 2023-07-31 14:10:46 -0500
Aaron Boranian's avatar
14.1k
Aaron Boranian
updated 2023-07-31 15:38:00 -0500
edit flag offensive 0 remove flag delete link

Comments

add a comment see more comments