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

How can I change the conductivity of a material using the Python API

asked 2022-06-22 11:04:35 -0600

RafaB's avatar

updated 2022-06-22 18:12:37 -0600

I am using the Python API and I would like to set the value of the conductivity of a material using Python. Any ideas?

edit retag flag offensive close merge delete

Comments

Which Material class are you looking to change? If it is an OpaqueMaterial you can do foo.setThermalConductivity(bar)

Luis Lara's avatar Luis Lara  ( 2022-06-28 11:53:24 -0600 )edit

The material is defined in the idl file.

RafaB's avatar RafaB  ( 2022-06-28 13:36:37 -0600 )edit

2 Answers

Sort by » oldest newest most voted
1

answered 2022-07-11 02:10:12 -0600

updated 2022-07-11 02:18:12 -0600

If you're trying to just change the conductivity before running the simulation, you're indeed better off doing it by manipulating the IDF file then.

Reading and writing the IDF will have almost no overhead, and if the overhead is a concern to you and you think openstudio (or eppy, or whatever you want to use) is too slow, just have an IDF template file with a specific placeholder, eg %CONDUCTIVITY%, load it in python, do a string replace, then write again. That will takes about 400 µs for a 6000-lines-long IDF file per the testing I just did.

In [1]: %%timeit
   ...: with open('tmp.idf', 'r') as f:
   ...:     content = f.read()
   ...: with open('in.idf', 'w') as f:
   ...:     f.write(content.replace('%CONDUCTIVITY%', '0.029'))
403 µs ± 7.94 µs per loop (mean ± std. dev. of 7 runs, 1000 loops each)

But you could also preload the IDF into openstudio or eppy or whatever, then keep changing the IDF and just pay the price of the change + save (instead of having to load too). But anyways, my point is that your bottleneck will be by far the E+ simulation time anyways, so you shouldn't be too concerned about the conductivity part.

Anyways, here is the same exact thing, using the openstudio Python bindings (PyPi openstudio, just do pip install openstudio on Python 3.7 to 3.9) (since your stack is Python, makes sense to stay in Python) to manipulate the IDF file (not using an OSM model if you don't have one to begin with). It's slower as expected, but you get a much smarter API, that allows you to get objects by type / name, will validate your entries based on the IDD constraints (if I try to set a negative conductivity it will not allow me to do so)

In [1]: import openstudio

In [2]: %%timeit
    ...: w = openstudio.Workspace.load('tmp.idf').get()
    ...: mat = w.getObjectByTypeAndName('Material', 'Wall Insulation [42]').get()
    ...: # Conductivity is the 4th field, so 3 (0-indexed)
    ...: assert mat.setDouble(3, 0.029)
    ...: w.save('in.idf', True)
78.7 ms ± 583 µs per loop (mean ± std. dev. of 7 runs, 10 loops each)
edit flag offensive delete link more

Comments

Thank you Julien for such a detailed answer. I very much appreciate it.

RafaB's avatar RafaB  ( 2022-07-12 13:33:57 -0600 )edit

If your question is resolved, please mark one of the two answers as accepted (the green check mark below the upvote/downvote buttons), so the thread is marked as resolved. Thanks!

Julien Marrec's avatar Julien Marrec  ( 2022-07-16 05:28:07 -0600 )edit
0

answered 2022-06-28 12:03:24 -0600

You can try something like

import openstudio

m = openstudio.model.Model()
material = openstudio.model.StandardOpaqueMaterial(m)
original_conductivity = material.thermalConductivity()
material.setThermalConductivity(0.2)

print ("The original conductivity was"+" "+str(original_conductivity)+" and the new conductivity is "+str(material.thermalConductivity()))

The original conductivity was 0.1 and the new conductivity is 0.2

Replace the material object with the object you want to change.

edit flag offensive delete link more

Comments

Also, you can refer to the documentation for every Class on https://openstudio-sdk-documentation....

Luis Lara's avatar Luis Lara  ( 2022-06-28 12:11:56 -0600 )edit

Thanks a lot Luis!

RafaB's avatar RafaB  ( 2022-06-28 13:32:49 -0600 )edit

I think @RafaB meant with the EnergyPlus Python API (Python EMS), not with openstudio. Didn't you? Given the tags..

Julien Marrec's avatar Julien Marrec  ( 2022-06-29 02:03:59 -0600 )edit

@Julien Marrec is right. I am using the Python API, but I am open to other approaches as well.

RafaB's avatar RafaB  ( 2022-06-29 08:57:58 -0600 )edit

Are you trying to change the conductivity at runtime (during the simulation)?

Julien Marrec's avatar Julien Marrec  ( 2022-07-04 03:59:36 -0600 )edit

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: 2022-06-22 11:04:35 -0600

Seen: 577 times

Last updated: Jul 11 '22