Python for Energy Modelers - Part 2 - Simple Pre-processing

After a great introduction by Clayton Miller, let’s dig in and see how easy and powerful it is to mix a little Python into your simulation workflow!

Computers are fantastic at performing repetitive tasks, let them handle the boring stuff so you can focus on creating better models and reports. Now think about your simulation tool as an engineer; many simulation tools have one main input file which is executed by the tool, and out come a bunch of results files. We can think of this like a black box model, where we are not so concerned about the inner details. In fact, this model can be generalized to many simulation tools important for energy analysis and building performance design. From personal experience I use this “ASCII text file input - text file output” black box model for EnergyPlus, TRNSYS, and Radiance. Wouldn’t it be nice if we had a general way to modify input files and process our output files automatically? This is the subject of this blog series!

I am going to start by looking at a common task in an ASHRAE 90.1 baseline model (for LEED or similar rating system); creating four input files for each of the four rotations. I do these models on a weekly basis, and this was one of the first things I wanted to automate.

Let’s first identify the problem in one sentence: We want to create four input files with four different rotation angles; 0, 90, 180, and 270, based off of a common “template” file. We are going to use EnergyPlus here, but this method works for any text based simulation file where you can access the angle parameter!

Here is our “template” file section of interest:

!- =========== ALL OBJECTS IN CLASS: BUILDING ===========

Building,
Baseline 0, !- Name
$$ReplaceThis$$, !- North Axis {deg}
CITY, !- Terrain
0.040000000000000001, !- Loads Convergence Tolerance Value
0.40000000000000002, !- Temperature Convergence Tolerance Value {deltaC}
FullExterior, !- Solar Distribution
25, !- Maximum Number of Warmup Days
; !- Minimum Number of Warmup Days

Notice line 5, where I have edited the EnergyPlus rotation angle field and used my own very unique text value; “”. This is a common and simple technique in parametrization of a text input file.

If you were to do this exercise by hand, what steps would you take? This is where you need to think like a computer, but you’ll see that you as an engineer and the computer think along similar lines. This is a compliment, I swear. Write all the basic steps out in pseudocode, or plain english (or whatever you prefer as a native language);

# Find the simulation input file
# Name the string are we looking for
# Name the replacement values
# For every replacement value in our list of values:
# Open the input file
# Open the output file
# For every line in the input file:
# If search string is found, replace it with replacement value
# Write the line to the output file

Notice that I am using the # sign for every line, and also notice the indenting! The # sign is a comment in Python, # lines will not be executed, and are for your benefit. We will use this pseudocode as a framework, and translate it into Python in our next step. Python also has a unique syntax feature: indented lines indicate which statement block the particular line belongs to. You will recall from introductory programming courses that a block is simply a group of lines that will be executed together. In this case, we want to loop over each replacement value (four times for our four angles). The second loop scans down through the input “template” file and looks for the search string. This second loop is nested inside the first one, so the template will be scanned four times.

So to summarize: * Lines 1-3 set up our problem with the template file, the unique parameter string which will be , and our replacement values. * Lines 4-6 loop over each replacement value, open the template, and open the output file. * Lines 7-9 then scan through the template replacing each instance of with our desired value, and write the lines out to our new output file.

Let’s expand pseudocode lines 1-3;

# Find the simulation input file
simulationInputFile = "C:SimulationsBaseline Short.idf"
# What string are we looking for?
searchString = "$$ReplaceThis$$"
# What do we want to replace it with?
angles = "0","90","180","270"

In this section, we can see three variables created; 2 strings and one list for our four angles. A list is a way to group variables together, and functions like an array. You can index lists, loop over them, slice, etc.

Now that we have all of our parameters declared, let’s move to expand pseudocode lines 4-9;

# For every replacement value in list of values:
for thisAngle in angles:
# Open the input file
inputFile = open(simulationInputFile)
# Open the output file
outputFileName = "c:Simulationsoutput "; + str(thisAngle) + " degree rotation.idf";
outputFile = open(outputFileName,'w')
print "Now writing file:", outputFileName

# For every line in the input file:
for line in inputFile:
# If searchString is found, replace it with thisAngle
newLine = line.replace(searchString, thisAngle)
# Write the line to the output file
outputFile.write(newLine)
outputFile.close()
print "Finished writing!"

inputFile.close()
print "Finished the whole script!"

The first thing we can see is that the syntax of Python is very clean; we have a minimum of brackets, braces, semicolons, etc. The for-loop has a very natural style, which is similar to the way I wrote the pseudocode english;

For every_object in a_list_of_objects:
do_something_with_this_object

We need the colon and then an indented block to follow. So in my case, I “do something” with every “thisAngle” in the list of “angles”. The psuedocode comments explain each step; the open() function opens a file, and if we add the ‘w’ option, we can also write to the file.

The next loop is also surprisingly simple; looping over each line in the input file. In Python, a file is “iterable”, just like a list, so we can read down through the file just as if you were to read the file yourself in a text editor. Each “line” object is returned as a string.

Line 12 calls the “replace” function on the line string, and has the syntax string.replace(“searchValue”, “replaceValue”). So in our case, Python looks for the string “” in the current line, and if it finds it, replaces it with the new angle value from the outer for loop. Otherwise, it returns the original line, unchanged.

Bonus exercises!

  1. Let’s say the architect want to study the effect of rotation on energy usage. How would you modify the above code to create 360 output files, 1 file for each angle?
  2. Now this architect wants to study using _three _brands of windows, with g-values (SHGC) 0.34, 0.51, 0.63; for each rotation. She first wants to see if there these parameters are interdependent and then to try and minimize the cooling demand in the concept phase, across these two parameters. 3x360 = 1080 simulation files! Depending on your political, contractual, and personal relationship with your architect you can either say;
  • That’s going to take three weeks and more money! or;
  • Sure, tomorrow afternoon is ok?
  • In either case, Python is here to the rescue!
  • How would you generalize the Python code to handle any type of variable, like g-Value, U-Value, etc.?
  • How would you modify the pseudocode to handle two variables?

Quick, post your solutions below!

To recap the strategy in incorporating scripting in your simulation workflow;

  1. Identify in your simulation workflow something that you need to do manually many times for each simulation, or something you need to do for each of your many simulations. If you can write the problem down in one sentence, it’s a great candidate for scripting.
  2. Next, break up the problem into many small simple steps, this is your pseudocode.
  3. Now try to write it out in Python. Remember, Google is your best friend, if you have a question about something in Python, chances are there have been hundreds of others out there with the same question!
  4. Finally, once it all works, clean up and comment your code, think about how you or someone else might use it in a year!

So there you have it, I hope you enjoyed learning a little about Python and I hope you have gotten a glimpse at the potential of Scripting+Simulation!

These are our first entries in what we hope to be a long running series. Please let us know below if you need help setting up Python, if this level of writing is appropriate (Simpler? More complex?), if you have a project where you could use scripting and which we could feature here (!), or if you think this is all just plum crazy, let us know!

Cheers from Vienna,

Marcus