Question-and-Answer Resource for the Building Energy Modeling Community
Get started with the Help page
Ask Your Question
1

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

asked 2023-07-28 15:19:59 -0500

Ali-Khosravani's avatar

updated 2023-07-30 11:53:31 -0500

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.

edit retag flag offensive close merge delete

2 Answers

Sort by ยป oldest newest most voted
2

answered 2023-07-31 01:44:47 -0500

kem's avatar

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.

edit flag offensive delete link more
0

answered 2023-07-31 14:10:46 -0500

cdouglass's avatar

updated 2023-07-31 15:38:00 -0500

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.

edit flag offensive delete link more

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

Training Workshops

Careers

Question Tools

1 follower

Stats

Asked: 2023-07-28 15:19:59 -0500

Seen: 381 times

Last updated: Jul 31 '23