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

Understanding code for "Adding EnergyPlus Workspace Objects"

asked 2018-07-25 17:34:33 -0600

mldichter gravatar image

From the OpenStudio Measure Writer's Reference Guide, I am trying to understand, in detail, what this code does from the "Adding EnergyPlus Workspace Objects" section.

  # array to hold new IDF objects
  string_objects = []

  # create sorted (.sort) list of unique (.uniq) constructions from all 
  # constructions in model, adding ComponentCost:LineItem for each

  used_constructions_names.sort.uniq.each do |used_construction_name|

    #IDF object text for ComponentCost:LineItem
    string_objects << "
      ComponentCost:LineItem,
        #{used_construction_name}_TakeOff,    !- Name
        ,                                     !- Type
        Construction,                         !- Line Item Type
        #{used_construction_name},            !- Item Name
        ,                                     !- Object End Use Key
        ,                                     !- Cost per Each {$}
        0.000000000001;                       !- Cost per Area {$/m2}
        "
  end

  # add all of the strings to workspace to create IDF objects
  string_objects.each do |string_object|
    idfObject = OpenStudio::IdfObject::load(string_object)
    object = idfObject.get
    wsObject = workspace.addObject(object)
  end

In particular, I'm trying to understand what these three lines do.

    idfObject = OpenStudio::IdfObject::load(string_object)
    object = idfObject.get
    wsObject = workspace.addObject(object)

The first line is the function

static boost::optional<IdfObject> openstudio::IdfObject::load(const std::string & text)

which can be viewed at this LINK.

I then got stuck because on the second line

    object = idfObject.get

there is no reference to "get" in the IdfObject Class, which is the object type returned by the first line. Nor is there a "get" in the WorkspaceObject Class from the "Inheritance diagram for openstudio::IdfObject.

image description

But looking up the workspace.addObject(object) from the third line in the documentation HERE, the second line most likely returns an openstudio::IdfObject. But that would be strange since the first line already returned an openstudio::IdfObject. Not really sure what the second line does.

The the third add the object to the workspace, which is obvious from the function name. This somehow gets the object into the IDF file. This raises another question though. What is the workspace exactly? I was unable to find what the workspace object in a measure intuitively contains or represents. Or maybe better said, what the workspace object can and can't do.

edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted
1

answered 2018-07-25 23:41:26 -0600

updated 2018-07-25 23:55:49 -0600

idfObject = OpenStudio::IdfObject::load(string_object) returns an optional IdfObject, which is to say, it returns an object that may or may not actually have an IdfObject inside it. If there's an IdfObject inside it, then you use .get to retrieve the actual IdfObject. There are additional methods available (e.g., .empty?) to check if the object is there before trying to retrieve it.

All of this is described well in the Measure Writing Guide. Specifically look at the section: "OpenStudio Measures and the boost::optional Type".

The Workspace class is described here. Look at the "Detailed Description". As an approximation, the model object can be thought of as an OSM while the workspace object can be thought of as an IDF.

edit flag offensive delete link more

Comments

@shorowit Thank you! That is exactly what I was looking for. I ask because I've recently been having to explain how to write measures to someone with only matlab experience. I do have a little bit of computer science in my background, so the measures are not outside my ability. Difficult though for someone coming directly from matlab, and I kept getting asked detailed questions I don't know the answer to, like this.

I will use your comment to post an answer tomorrow. Thanks!!!

mldichter gravatar image mldichter  ( 2018-07-26 00:30:32 -0600 )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

1 follower

Stats

Asked: 2018-07-25 17:34:33 -0600

Seen: 339 times

Last updated: Jul 25 '18