Design Parametric Study - How to Run EnergyPlus in JAVA
Sometimes it is just frustrating to manually change one design parameter in EnergyPlus at a time for design parametric study. My friend and I have worked on an energy model calibration project, which required thousands of parametric simulations. We used excel to create the list of variables in a comma separate format (CSV) and manually move the variables to the parametric module in EnergyPlus. To create the variables is already a tons of work, and what's worse, the parametric module in EnergyPlus is terribly labor intensive
Well, the good news is today, we have so many good scripting tools available to automate this process. So we ended up creating a simple program to complete our work (check it out here!). Recently, one of my friend asked me about how to run EnergyPlus in Python. Her question reminded me that the very first barrier to built our simple program was running EnergyPlus in JAVA. So It might be worthwhile writing it down here, to save many other EnergyPlus modelers time. It should be noted that all the code below is only tested under Window OS, which means the code is not compatible with Mac or Linux OS.
Let's take a brief look at the JAVA code below:
So this is a simple run EnergyPlus method in JAVA. There are three inputs required for this method.
IdfFileDir: The directory of a .idf file in a String format (e.g. "C://users//Desktop//example.idf"). .idf file is the standard EnergyPlus input dictionary file. Software such as OpenStudio, DesignBuilder, Sefaira, or BuildSimHub can help you produce such file.
weatherFileDir: The directory of a .epw file in a String format (e.g. "C://users//Desktop//NewYorkCity_International_Airport.epw"). Weather data is very complicated, but for now, all we need to do is go to here to download a well developed .epw file.
BatchFileDir: This is the file that actually calls EnergyPlus to run. The installation package of EnergyPlus Window version comes with four different batch files. Besides the RunReadESO.bat, the Epl-run.bat, RunEplus.bat and RunDirMulti.bat all can be used to run EnergyPlus simulation. The differences among them are:
RunEplus.bat: runs a single simulation with 2 inputs (IDF file directory, weather file directory - we used this batch file for our application!)
Epl-run.bat: basically does the same thing as RunEplus.bat, but it allows you to specify up to 9 simulation environment variables (input directory, output directory, weather file, maximum output column, etc)
RunDirMulti.bat: This batch file is used to run EnergyPlus simulations using the RunEplus.bat for all the files in the current directory. So it mainly used for group simulations.
Apparently, the first thing in the method is to setup the directories. Line 3 to 4 sets the File object for EnergyPlus input file.
Line 7 and 8 is doing the same thing except line 8 copies the weather file from its original directory to the same directory as the input file by calling the "copyWeatherFile()" method. Through our test, we found that EnergyPlus is always try to find the weather file under its WeatherData folder or the same folder as the input file, so if you place your weather file in somewhere else, the simulation probably will stop with a message "cannot find the weather information". So, it is necessary to make sure weather file is placed in either of the location.
Similarly, line 11 copies the batch file to the input file folder. Similar to the weather file, this operation is just let JAVA/EnergyPlus easier to find the file they need. While copying a batch file, we should set the program path (the path of the EnergyPlus.exe) and the weather path (we just copied the weather file!).
All set, then we should create a new String object called commandline, which contains 1. the copied batch file directory, 2. EnergyPlus input file directory, 3. weather file directory.
The try-catch block contains the code that execute the simulation. First of all, we open a Process and execute the commandline. The order of inputs for .exec() method should be the commandline, null, and the directory of your EnergyPlus input file.
Now, the code we have is sufficient to run an EnergyPlus simulation!! However, if you stop here, then JAVA will think it has completed the job, and the method should be stopped even though the EnergyPlus is still running the simulation.The following code is really asking JAVA to wait for the EnergyPlus simulation.
The Thread class in JAVA can do the same job, however, if you want the eclipse console to export EnergyPlus simulation status, you can add the following code:
Here is the code for copying the weather file
This code copies the batch file:
That's it! Having fun programming with EnergyPlus!