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

'Undefined Method' Error when testing portions of OS Measure Test Code

asked 2015-03-18 15:39:52 -0500

dpud12's avatar

updated 2017-08-05 13:16:57 -0500

I am using a text editor to build an OS measure which sets the occupancy load of a building. In building the measure, I am using the following code to test what the old definitions for occupancy load are:

require 'openstudio'
require 'openstudio/ruleset/ShowRunnerOutput'
require 'test/unit'

class SetNew_Test < Test::Unit::TestCase

def test_SetNew_a

# load the test model
translator = OpenStudio::OSVersion::VersionTranslator.new
path = OpenStudio::Path.new(File.dirname(__FILE__) + "/OldModel.osm")
model = translator.loadModel(path)
assert((not model.empty?))
model = model.get
puts "Model Loaded"

model.getSpaceTypes.each do |space_tp|
    #loop through all people definitions in the space type
    space_tp.people.each do |people|
      #get the old od from the existing definition, if exists  
        old_people = people.peopleDefinition.peoplePerFloorArea.get
        puts old_people
    end
end

end
end

I checked the file in OS and the building does define the occupancy load in terms of people per floor area, however, I get the following error from the command line:

undefined method 'peoplePerFloorArea'

I have tried several different classes that should exist, such as lighting power density, schedules, etc. However, each time I get the same error that the method does not exist . . . I would appreciate any comments that might lend some insight into what I am doing wrong

edit retag flag offensive close merge delete

2 Answers

Sort by ยป oldest newest most voted
5

answered 2015-03-18 15:51:13 -0500

updated 2015-03-19 08:52:28 -0500

The method peopleperSpaceFloorArea returns an optional double (see PeopleDefinition Class Reference). Check out the section "Optionals and .get" from the Measure Writing Guide, and @David Goldwasser's answer.

*Edit to correct method name

edit flag offensive delete link more

Comments

I see your point, but I don't think this is the source of my error, when I used the following code intead

model.getLightss.each do |light| 
    test = light.lightsDefinition.fractionRadiant.empty?
    # radiant = light.lightsDefinition.fractionRadiant.get
    puts test
end

It also gave me a no method error, and reported the following:

undefined method 'empty?'

This leads me to believe there is something else that I am doing wrong, since even this method throws an error

dpud12's avatar dpud12  ( 2015-03-19 07:53:17 -0500 )edit

lightsDefinition.fractionRadiant will return a double value, not an 'optional', so it doesn't need the .empty? check.

ericringold's avatar ericringold  ( 2015-03-19 08:50:40 -0500 )edit

Ahh, I didn't know that the method empty? only works for optional returns. Thanks for the insight. Would I be correct then in assuming that the get method should only be used with optionals then as well?

dpud12's avatar dpud12  ( 2015-03-19 13:50:43 -0500 )edit

I think you still need to use .get to assign the value to the variable. For instance, the line you have commented out in the code above should successfully assign the fractionRadiant value to the variable 'radiant'. That said, my knowledge of OpenStudio Ruby measures is far from complete; I recommend lots of documentation reading and searching the OpenStudio code on github.

ericringold's avatar ericringold  ( 2015-03-19 14:29:59 -0500 )edit

After some experimentation it seems that .get is not necessary with double returns, and in fact causes errors when it is used on non-optional returns. And thanks I'll check out the documentation to gain a better understanding of why this occurs

dpud12's avatar dpud12  ( 2015-03-19 14:37:26 -0500 )edit
2

answered 2015-03-18 22:33:21 -0500

updated 2015-03-19 08:16:34 -0500

"peopleperSpaceFloorArea" will return a value if the definition has a value for this field, but otherwise the optional will be empty. Putting it in an if statement protects against the ruby error if the optional is empty.

if peopleDefinition.peopleperSpaceFloorArea.is_initialized
  peopleDefinition.peopleperSpaceFloorArea.get
end

"getPeoplePerFloorArea" will return a value regardless of which input field use used for the definition, but you do have to pass it the floor to use of the calculation.

peopleDefinition.getPeoplePerFloorArea(area)

Use the link provided in @Eric Ringold's answer for more options for this definition.

edit flag offensive delete link more

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

Careers

Question Tools

1 follower

Stats

Asked: 2015-03-18 15:39:52 -0500

Seen: 330 times

Last updated: Mar 19 '15