These are recently written functions that have not made it into the main documentation
In [5]:
# you would normaly install eppy by doing
# python setup.py install
# or
# pip install eppy
# or
# easy_install eppy
# if you have not done so, uncomment the following three lines
import sys
# pathnameto_eppy = 'c:/eppy'
pathnameto_eppy = '../'
sys.path.append(pathnameto_eppy)
When things go wrong in your eppy script, you get "Errors and Exceptions".
To know more about how this works in python and eppy, take a look at Python: Errors and Exceptions
When you work with Energyplus you are working with idf files (files that have the extension *.idf). There is another file that is very important, called the idd file. This is the file that defines all the objects in Energyplus. Esch version of Energyplus has a different idd file.
So eppy needs to know which idd file to use. Only one idd file can be used in a script or program. This means that you cannot change the idd file once you have selected it. Of course you have to first select an idd file before eppy can work.
If you use eppy and break the above rules, eppy will raise an exception. So let us use eppy incorrectly and make eppy raise the exception, just see how that happens.
First let us try to open an idf file without setting an idd file.
In [6]:
from eppy import modeleditor
from eppy.modeleditor import IDF
fname1 = "../eppy/resources/idffiles/V_7_2/smallfile.idf"
Now let us open file fname1 without setting the idd file
In [7]:
try:
idf1 = IDF(fname1)
except Exception, e:
raise e
OK. It does not let you do that and it raises an exception
So let us set the idd file and then open the idf file
In [8]:
iddfile = "../eppy/resources/iddfiles/Energy+V7_2_0.idd"
IDF.setiddname(iddfile)
idf1 = IDF(fname1)
That worked without raising an exception
Now let us try to change the idd file. Eppy should not let you do this and should raise an exception.
In [9]:
try:
IDF.setiddname("anotheridd.idd")
except Exception, e:
raise e
Excellent!! It raised the exception we were expecting.
The fields of idf objects often have a range of legal values. The following functions will let you discover what that range is and test if your value lies within that range
demonstrate two new functions:
In [10]:
from eppy import modeleditor
from eppy.modeleditor import IDF
iddfile = "../eppy/resources/iddfiles/Energy+V7_2_0.idd"
fname1 = "../eppy/resources/idffiles/V_7_2/smallfile.idf"
In [11]:
# IDF.setiddname(iddfile)# idd ws set further up in this page
idf1 = IDF(fname1)
In [12]:
building = idf1.idfobjects['building'.upper()][0]
print building
In [13]:
print building.getrange("Loads_Convergence_Tolerance_Value")
In [14]:
print building.checkrange("Loads_Convergence_Tolerance_Value")
Let us set these values outside the range and see what happens
In [15]:
building.Loads_Convergence_Tolerance_Value = 0.6
from eppy.bunch_subclass import RangeError
try:
print building.checkrange("Loads_Convergence_Tolerance_Value")
except RangeError, e:
raise e
So the Range Check works
We have seen how to check the range of field in the idf object. What if you want to do a range check on all the fields in an idf object ? To do this we will need a list of all the fields in the idf object. We can do this easily by the following line
In [16]:
print building.fieldnames
So let us use this
In [17]:
for fieldname in building.fieldnames:
print "%s = %s" % (fieldname, building[fieldname])
Now let us test if the values are in the legal range. We know that "Loads_Convergence_Tolerance_Value" is out of range
In [18]:
from eppy.bunch_subclass import RangeError
for fieldname in building.fieldnames:
try:
building.checkrange(fieldname)
print "%s = %s #-in range" % (fieldname, building[fieldname],)
except RangeError as e:
print "%s = %s #-****OUT OF RANGE****" % (fieldname, building[fieldname],)
You see, we caught the out of range value
Until now in all our examples, we have been reading an idf file from disk:
Here are the steps to do that
In [19]:
# some initial steps
from eppy.modeleditor import IDF
iddfile = "../eppy/resources/iddfiles/Energy+V7_2_0.idd"
# IDF.setiddname(iddfile) # Has already been set
# - Let us first open a file from the disk
fname1 = "../eppy/resources/idffiles/V_7_2/smallfile.idf"
idf_fromfilename = IDF(fname1) # initialize the IDF object with the file name
idf_fromfilename.printidf()
In [20]:
# - now let us open a file from the disk differently
fname1 = "../eppy/resources/idffiles/V_7_2/smallfile.idf"
fhandle = open(fname1, 'r') # open the file for reading and assign it a file handle
idf_fromfilehandle = IDF(fhandle) # initialize the IDF object with the file handle
idf_fromfilehandle.printidf()
In [21]:
# So IDF object can be initialized with either a file name or a file handle
# - How do I create a blank new idf file
idftxt = "" # empty string
from StringIO import StringIO
fhandle = StringIO(idftxt) # we can make a file handle of a string
idf_emptyfile = IDF(fhandle) # initialize the IDF object with the file handle
idf_emptyfile.printidf()
It did not print anything. Why should it. It was empty.
What if we give it a string that was not blank
In [22]:
# - The string does not have to be blank
idftxt = "VERSION, 7.3;" # Not an emplty string. has just the version number
fhandle = StringIO(idftxt) # we can make a file handle of a string
idf_notemptyfile = IDF(fhandle) # initialize the IDF object with the file handle
idf_notemptyfile.printidf()
Aha !
Now let us give it a file name
In [23]:
# - give it a file name
idf_notemptyfile.idfname = "notemptyfile.idf"
# - Save it to the disk
idf_notemptyfile.save()
Let us confirm that the file was saved to disk
In [24]:
txt = open("notemptyfile.idf", 'r').read()# read the file from the disk
print txt
Yup ! that file was saved. Let us delete it since we were just playing
In [25]:
import os
os.remove("notemptyfile.idf")
Let us start with a blank idf file and make some new "MATERIAL" objects in it
In [26]:
# making a blank idf object
blankstr = ""
from StringIO import StringIO
idf = IDF(StringIO(blankstr))
To make and add a new idfobject object, we use the function IDF.newidfobject(). We want to make an object of type "MATERIAL"
In [27]:
newobject = idf.newidfobject("material".upper()) # the key for the object type has to be in upper case
# .upper() makes it upper case
In [28]:
print newobject
Let us give this a name, say "Shiny new material object"
In [29]:
newobject.Name = "Shiny new material object"
print newobject
In [30]:
anothermaterial = idf.newidfobject("material".upper())
anothermaterial.Name = "Lousy material"
thirdmaterial = idf.newidfobject("material".upper())
thirdmaterial.Name = "third material"
print thirdmaterial
Let us look at all the "MATERIAL" objects
In [31]:
print idf.idfobjects["MATERIAL"]
As we can see there are three MATERIAL idfobjects. They are:
Let us remove 2. Lousy material. It is the second material in the list. So let us remove the second material
In [32]:
idf.popidfobject('MATERIAL', 1) # first material is '0', second is '1'
Out[32]:
In [33]:
print idf.idfobjects['MATERIAL']
You can see that the second material is gone ! Now let us remove the first material, but do it using a different function
In [34]:
firstmaterial = idf.idfobjects['MATERIAL'][-1]
In [35]:
idf.removeidfobject(firstmaterial)
In [36]:
print idf.idfobjects['MATERIAL']
So we have two ways of deleting an idf object:
Having deleted two "MATERIAL" objects, we have only one left. Let us make a copy of this object and add it to our idf file
In [37]:
onlymaterial = idf.idfobjects["MATERIAL"][0]
In [38]:
idf.copyidfobject(onlymaterial)
In [39]:
print idf.idfobjects["MATERIAL"]
So now we have a copy of the material. You can use this method to copy idf objects from other idf files too.
What if we wanted to make an idf object with values for it's fields? We can do that too.
In [40]:
gypboard = idf.newidfobject('MATERIAL', Name="G01a 19mm gypsum board",
Roughness="MediumSmooth",
Thickness=0.019,
Conductivity=0.16,
Density=800,
Specific_Heat=1090)
In [41]:
print gypboard
newidfobject() also fills in the default values like "Thermal Absorptance", "Solar Absorptance", etc.
In [42]:
print idf.idfobjects["MATERIAL"]
It is easy to rename an idf object. If we want to rename the gypboard object that we created above, we simply say:
But this could create a problem. What if this gypboard is part of a "CONSTRUCTION" object. The construction object will refer to the gypboard by name. If we change the name of the gypboard, we should change it in the construction object.
But there may be many constructions objects using the gypboard. Now we will have to change it in all those construction objects. Sounds painfull.
Let us try this with an example:
In [43]:
interiorwall = idf.newidfobject("CONSTRUCTION", Name="Interior Wall",
Outside_Layer="G01a 19mm gypsum board",
Layer_2="Shiny new material object",
Layer_3="G01a 19mm gypsum board")
print interiorwall
to rename gypboard and have that name change in all the places we call modeleditor.rename(idf, key, oldname, newname)
In [44]:
modeleditor.rename(idf, "MATERIAL", "G01a 19mm gypsum board", "peanut butter")
Out[44]:
In [45]:
print interiorwall
Now we have "peanut butter" everywhere. At least where we need it. Let us look at the entir idf file, just to be sure
In [46]:
idf.printidf()
Can I turn off the defautl values. Yes you can:
In [49]:
defaultmaterial = idf.newidfobject("MATERIAL",
Name='with default')
print defaultmaterial
nodefaultmaterial = idf.newidfobject("MATERIAL",
Name='Without default',
defaultvalues=False)
print nodefaultmaterial
DAYLIGHTING:CONTROLS
, and you will see the need for defaultvalues=False
Of course, internally EnergyPlus will still use the default values it it is left blank. If just won't turn up in the IDF file.
The idf file has zones with surfaces and windows. It is easy to get the attributes of the surfaces and windows as we have seen in the tutorial. Let us review this once more:
In [43]:
from eppy import modeleditor
from eppy.modeleditor import IDF
iddfile = "../eppy/resources/iddfiles/Energy+V7_2_0.idd"
fname1 = "../eppy/resources/idffiles/V_7_2/box.idf"
# IDF.setiddname(iddfile)
In [44]:
idf = IDF(fname1)
In [45]:
surfaces = idf.idfobjects["BuildingSurface:Detailed".upper()]
surface = surfaces[0]
print "area = %s" % (surface.area, )
print "tilt = %s" % (surface.tilt, )
print "azimuth = %s" % (surface.azimuth, )
Can we do the same for zones ?
Not yet .. not yet. Not in this version on eppy
But we can still get the area and volume of the zone
In [46]:
zones = idf.idfobjects["ZONE"]
zone = zones[0]
area = modeleditor.zonearea(idf, zone.Name)
volume = modeleditor.zonevolume(idf, zone.Name)
print "zone area = %s" % (area, )
print "zone volume = %s" % (volume, )
Not as slick, but still pretty easy
Some notes on the zone area calculation:
we are going to update idf1
using json. First let us print the idf1
before changing it, so we can see what has changed once we make an update
In [47]:
idf1.printidf()
In [48]:
import eppy.json_functions as json_functions
json_str = {"idf.VERSION..Version_Identifier":8.5,
"idf.SIMULATIONCONTROL..Do_Zone_Sizing_Calculation": "No",
"idf.SIMULATIONCONTROL..Do_System_Sizing_Calculation": "No",
"idf.SIMULATIONCONTROL..Do_Plant_Sizing_Calculation": "No",
"idf.BUILDING.Empire State Building.North_Axis": 52,
"idf.BUILDING.Empire State Building.Terrain": "Rural",
}
json_functions.updateidf(idf1, json_str)
In [49]:
idf1.printidf()
Compare the first printidf() and the second printidf().
The syntax of the json string is described below::
You can also create a new object using JSON, using the same syntax. Take a look at this:
In [50]:
json_str = {"idf.BUILDING.Taj.Terrain": "Rural",}
json_functions.updateidf(idf1, json_str)
idf1.idfobjects['building'.upper()]
# of course, you are creating an invalid E+ file. But we are just playing here.
Out[50]:
What if you object name had a dot .
in it? Will the json_function get confused?
If the name has a dot in it, there are two ways of doing this.
In [51]:
# first way
json_str = {"idf.BUILDING.Taj.with.dot.Terrain": "Rural",}
json_functions.updateidf(idf1, json_str)
# second way (put the name in single quotes)
json_str = {"idf.BUILDING.'Another.Taj.with.dot'.Terrain": "Rural",}
json_functions.updateidf(idf1, json_str)
In [52]:
idf1.idfobjects['building'.upper()]
Out[52]:
Note When you us the json update function:
Name
field to have a value.Name
field, the results may be unexpected (undefined ? :-). So don't do this.Name
field (some don't), changes are made to the first object in the list. Which should be fine, since usually there is only one item in the listIf you have an eppy running on a remote server somewhere on the internet, you can change an idf file by sending it a JSON over the internet. This is very useful if you ever need it. If you don't need it, you shouldn't care :-)
It is rather cumbersome to open an IDF file in eppy. From the tutorial, the steps look like this:
In [ ]:
from eppy import modeleditor
from eppy.modeleditor import IDF
iddfile = "../eppy/resources/iddfiles/Energy+V7_2_0.idd"
fname = "../eppy/resources/idffiles/V_7_2/smallfile.idf"
IDF.setiddname(iddfile)
idf = IDF(fname)
Why can’t you just open the IDF file without jumping thru all those hoops. Why do you have to find the IDD file. What is the point of having a computer, if it does not do the grunt work for you.
The function easyopen will do the grunt work for you. It will automatically read the version number from the IDF file, locate the correct IDD file and set it in eppy and then open your file. It works like this:
In [ ]:
from eppy.easyopen import easyopen
fname = './eppy/resources/idffiles/V8_8/smallfile.idf'
idf = easyopen(fname)
For this to work,
If easyopen does not work, use the long winded steps shown in the tutorial. That is guaranteed to work
We normally think of fan power in terms of Brake Horsepower (BHP), Watts. Also when working with IP units it is useful to think of fan flow volume in terms of cubic feet per minute (cfm).
Energyplus does not have fields for those values. With eppy we have functions that will calculate the values
It will work for the following objects:
The sample code would look like this:
In [ ]:
thefans = idf.idfobjects['Fan:VariableVolume'.upper()]
thefan = thefans[0]
bhp = thefan.fanpower_bhp
watts = thefan.fanpower_watts
cfm = thefan.fan_maxcfm
Note: This code was hacked together quickly. Needs peer review in ../eppy/fanpower.py