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

Run EnergyPlus from python?

asked 2017-01-17 15:40:03 -0500

Prateek gravatar image

updated 2017-01-17 17:12:59 -0500

I am confused to run energyplus from python script using subprocess.call. I am not able to run the energyplus simulations with weather file as argument.

I have tried:

 subprocess.call(['C:\EnergyPlusV8-5-0\energyplus.exe', 'PackagedTerminalHeatPump.idf'])

which runs the energyplus without weather file. But I am not able to pass weather file as argument.

edit retag flag offensive close merge delete

Comments

@Prateek: welcome to Unmet Hours! In the future please do not include greetings ("hi, thanks, etc") in your post per our convention. Also, to format code, use CTRL+K or the button that looks like 0 and 1s. Thanks!

Julien Marrec gravatar imageJulien Marrec ( 2017-01-17 17:12:17 -0500 )edit

2 Answers

Sort by ยป oldest newest most voted
6

answered 2017-01-17 19:12:07 -0500

The most direct fix to your line of code is:

subprocess.call(['C:\EnergyPlusV8-5-0\energyplus.exe', '-w', 'path/to/weather.epw', 'PackagedTerminalHeatPump.idf'])

The EnergyPlus executable always comes first in the list of arguments passed to subprocess.call (or check_call or Popen), and the IDF comes last. For the other options, you specify the option, then its argument (where required).

Here is the CLI help which you may find useful in building up your command.

EnergyPlus, Version 8.5.0-c87e61b44b
Usage: energyplus [options] [input-file]
Options:
  -a, --annual                 Force annual simulation
  -d, --output-directory ARG   Output directory path (default: current
                               directory)
  -D, --design-day             Force design-day-only simulation
  -h, --help                   Display help information
  -i, --idd ARG                Input data dictionary path (default: Energy+.idd
                               in executable directory)
  -m, --epmacro                Run EPMacro prior to simulation
  -p, --output-prefix ARG      Prefix for output file names (default: eplus)
  -r, --readvars               Run ReadVarsESO after simulation
  -s, --output-suffix ARG      Suffix style for output file names (default: L)
                                  L: Legacy (e.g., eplustbl.csv)
                                  C: Capital (e.g., eplusTable.csv)
                                  D: Dash (e.g., eplus-table.csv)
  -v, --version                Display version information
  -w, --weather ARG            Weather file path (default: in.epw in current
                               directory)
  -x, --expandobjects          Run ExpandObjects prior to simulation
Example: energyplus -w weather.epw -r input.idf

I've wrapped this up in a little module you can find here. So long as EnergyPlus is installed in the usual place for your system and the install directory is on your path then this ought to work straight out of the box.

edit flag offensive delete link more

Comments

@Jamie Bull: +1 for runner.py, nice job! Just wondering, is there a way to do this without having the E+ install directory as one's path? I have tried shutil.which() but looks like it has the same restriction?

Waseem gravatar imageWaseem ( 2017-03-10 09:22:22 -0500 )edit
5

answered 2017-01-17 16:45:30 -0500

updated 2017-01-17 17:15:36 -0500

Using Popen with multiple args and piping the output to display the actual error message

Since subprocess.check_call shouldn't have stdout and stderr pipes, you should use Popeninstead.

See the subprocess documentation especially check_call and for Popen.

import subprocess

eplus_path = '/Applications/EnergyPlus-8-6-0/energyplus'
eplus_file = '5ZoneAirCooled.idf'
weather_file = 'USA_CO_Golden-NREL.724666_TMY3.epw'

df = subprocess.Popen([eplus_path, "-w", weather_file, eplus_file], stdout=subprocess.PIPE)        
output, err = df.communicate()
print(output.decode('utf-8'))
if not err is None:
    print(err.decode('utf-8'))

In a case where it goes well, I'll see something that starts with:

EnergyPlus Starting
EnergyPlus, Version 8.6.0-198c6a3cff, YMD=2017.01.17 23:46
Processing Data Dictionary
Processing Input File
... etc

In a case where for example I don't have the right idf file in my folder:

ERROR: Could not find input data file: /Users/julien/Software/RunningEPlusFromPython/2ZoneAirCooled.idf.
Type 'energyplus --help' for usage.

Shlex as helper to parse a command

Note that the subprocess calls expects a list of arguments. Using shlex to decompose a command line (that you can try directly in a terminal/cmd prompt) can be useful:

In [1]:
import shlex
command_line = '{eplus_path} -w {weather_file} {eplus_file}'.format(eplus_path= eplus_path, weather_file=weather_file, eplus_file=eplus_file)
print(command_line)

Out [1]:
/Applications/EnergyPlus-8-6-0/energyplus -w USA_CO_Golden-NREL.724666_TMY3.epw 5ZoneAirCooled.idf

In [2]: shlex.split(command_line)
Out[2]: ['/Applications/EnergyPlus-8-6-0/energyplus',
         '-w',
         'USA_CO_Golden-NREL.724666_TMY3.epw',
         '5ZoneAirCooled.idf']

Seeing the EnergyPlus simulation output in real time as the simulation runs

If you need to do this, here's how:

import subprocess
# If python 2.7: `from __future__ import print_function`

popen = subprocess.Popen([eplus_path, "-w", weather_file, eplus_file],
                         stdout=subprocess.PIPE, universal_newlines=True)

# Might need to use `#for stdout_line in iter(popen.stdout.readline, ""):` instead in python 2.7
for stdout_line in popen.stdout:
    print(stdout_line)

popen.stdout.close()
return_code = popen.wait()

if return_code:
    raise subprocess.CalledProcessError(return_code, popen.args)

Side note

From the docs on the energyplus CLI (command line interface), if you don't supply -w weather_file_path it searches for in.epw in the current directory (where it's called from). So a quick fix would be to make sure that your weather file is there an named in.epw. Just saying.

edit flag offensive delete link more

Comments

@Prateek: I'm definitely answering questions you didn't ask, but I added more info on shlex and how to see the E+ simulation output in real time in the hope you'll find it helpful and especially that people will land on this post in the future...

Julien Marrec gravatar imageJulien Marrec ( 2017-01-17 17:09:46 -0500 )edit
2

Great answer @Julien Marrec. For what it's worth, I've written a Python wrapper for the CLI which can be found here

Jamie Bull gravatar imageJamie Bull ( 2017-01-17 17:24:42 -0500 )edit
1

I did not have this information one hour ago lol :) Well, at least this will serve as an explanation of the underlying concepts (glass half full!)

Julien Marrec gravatar imageJulien Marrec ( 2017-01-17 17:30:09 -0500 )edit
1

@Julien Marrec and @Jamie Bull. Thanks a lot. That helped me a lot.

Prateek gravatar imagePrateek ( 2017-01-18 07:48:40 -0500 )edit

@Julien Marrec do you know if your subprocess.popen code works with E+ V8.0.0 ? I tried to run it and it is giving me errors. It seems to ignore the file paths given in my script and looks for in.idf, in.epw and Energy+.ini in the script directory. It works when I provide those files in the directory, but I want it to run with filepaths so that I can use for loops to do multiple runs

hnagda gravatar imagehnagda ( 2019-04-03 12:20:29 -0500 )edit

Your Answer

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

Add Answer

 



Question Tools

3 followers

Stats

Asked: 2017-01-17 15:40:03 -0500

Seen: 1,968 times

Last updated: Jan 17 '17