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

Openstudio measure: deal with blank argument

asked 2015-04-07 04:59:49 -0500

updated 2017-08-05 13:15:56 -0500

I want to include an optional argument for a measure, that the user would leave blank if not needed. The problem is that when I'm trying to retrieve the value, if left blank it crashes.

Here's an example of what I want to do:

def arguments(model)
  args = OpenStudio::Ruleset::OSArgumentVector.new

  #Optional argument
  fan_pressure_rise = OpenStudio::Ruleset::OSArgument::makeDoubleArgument('fan_pressure_rise', false)
  fan_pressure_rise.setDisplayName('Fan Pressure Rise (Pa)')
  fan_pressure_rise.setDescription('Leave blank for default value')
  args << fan_pressure_rise

  return args
end # end the arguments method

def run(model, runner, user_arguments)
  super(model, runner, user_arguments)

  # use the built-in error checking 
  if not runner.validateUserArguments(arguments(model), user_arguments)
    return false
  end

  fan_pressure_rise = runner.getDoubleArgumentValue('fan_pressure_rise', user_arguments)

  [...truncated...]

  if !fan_pressure_rise.empty?
    selected_fan.setPressureRise(fan_pressure_rise)
  end


  return true

end # end the run method

I get the following error:

'getDoubleArgumentValue': No value found for argument 'fan_pressure_rise' (RuntimeError)

How can I deal with optional, potentially blank arguments in OpenStudio measures?

Update

I figured I should use

fan_pressure_rise = runner.getOptionalDoubleArgumentValue('fan_pressure_rise', user_arguments)

For reference, the list of get...ArgumentValue available is in the OSRunner Class Reference.

Problem is, selected_fan.setPressureRise(fan_pressure_rise) crashes because it expects a Double, not an OptionalDouble.

I can't find how to convert an OptionalDouble to a Double. I tried fan_pressure_rise_double = OpenStudio::Double.new(fan_pressure_rise) but that doesn't work.

edit retag flag offensive close merge delete

Comments

@Julien Marrec, you are a beast!

__AmirRoth__'s avatar __AmirRoth__  ( 2015-04-07 06:48:37 -0500 )edit

What makes me a beast? More importantly, is that a good thing or a bad thing :) ?

Julien Marrec's avatar Julien Marrec  ( 2015-04-07 07:21:47 -0500 )edit

I'm referring to your productivity on this site. And presumably off of it. I wish there were a 'beast' tag. :)

__AmirRoth__'s avatar __AmirRoth__  ( 2015-04-07 08:02:17 -0500 )edit

It's a good thing, definition: beast UK Slang- a person who is very good at something. Example: He's an absolute beast when it comes to football.

And @Amir Roth is right, you are a beast!

macumber's avatar macumber  ( 2015-04-07 09:00:21 -0500 )edit

3 Answers

Sort by ยป oldest newest most voted
5

answered 2015-04-07 09:02:19 -0500

@Julien Marrec you are on the right track with getOptionalDoubleArgumentValue, because your argument is not required as indicated by the false in OpenStudio::Ruleset::OSArgument::makeDoubleArgument('fan_pressure_rise', false). And you are correct that all you need to do is retrieve the double from the optional double if it is not empty?. To do that simply use optional's get method like this selected_fan.setPressureRise(fan_pressure_rise.get).

Take a look at the Measure Writing Guide. In the run section there is a deeper explanation of the optionals.

edit flag offensive delete link more

Comments

Thanks @Kyle Benne. I find datatypes hard to grasp a little, and I don't know if it's because I don't know Ruby or if it's OS specific. I'll get there :)

Julien Marrec's avatar Julien Marrec  ( 2015-04-07 09:53:26 -0500 )edit
6

answered 2015-04-07 09:08:18 -0500

updated 2015-04-07 09:09:52 -0500

This needs to be documented better in the measure writing guide but there are two options:

Option 1 is to provide a default value that will be used if the user leaves the argument blank. This is done in the arguments section with:

argument.setDefaultValue(100)

Option 2 is to get the value using getOptionalDoubleArgumentValue in the run section, if you do this you have to check that the value is initialized before calling get to retrieve the value:

value = runner.getOptionalDoubleArgumentValue('argument', arguments)
if value.is_initialized
  value = value.get
else
   value = nil
end
edit flag offensive delete link more
1

answered 2015-04-07 08:31:07 -0500

updated 2015-04-07 08:33:13 -0500

I'm not familiar with the .setDescription function and typically use the following for double arguments:

Arguments Method

fan_rise = OpenStudio::Ruleset::OSArgument::makeDoubleArgument("fan_rise", true)
fan_rise.setDisplayName("Pressure Rise {inH2O}")
fan_rise.setDefaultValue(-1)
args << fan_rise

This is from a measure I wrote to Set Fan Inputs which allows the user to select a fan type and change any or all of the inputs. I use a negative default value as a flag to check whether the user wants to change a field. Not the most elegant solution but it works.

Run Method

fans.each do |fan|

  if fan.name.to_s.include? string or string == "*.*"

    # set common inputs
    if fan_eff_tot > 0
      fan.setFanEfficiency(fan_eff_tot)
    end
    if fan_rise > 0
      fan.setPressureRise(fan_rise_si)
    end
    ...
edit flag offensive delete link more

Comments

Similarly, I had it with setdefaultvalue(0) and the text was "leave 0 for default" but I'm quite sure there's no need for a workaround like this. For a string, I check if empty, otherwise I do string.to_s and everything's fine. setDescription adds a small font description of a field... check it out. like this

Julien Marrec's avatar Julien Marrec  ( 2015-04-07 08:35:29 -0500 )edit

I like it, will start using this in my measures.

MatthewSteen's avatar MatthewSteen  ( 2015-04-07 09:20:19 -0500 )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

2 followers

Stats

Asked: 2015-04-07 04:59:49 -0500

Seen: 313 times

Last updated: Apr 07 '15