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

OpenStudio 3.2.1 - trouble with object creation in measure

asked 2021-12-31 14:36:52 -0600

mattkoch gravatar image

I am writing a measure to mass-assign materials to constructions and constructions to default construction sets. The following bit of code produces a new DefaultSurfaceConstructions with option = true, but not with option = false.

if option then
  exterior_surface_constructions = OpenStudio::Model::DefaultSurfaceConstructions.new(model)
  exterior_surface_constructions = exterior_surface_constructions.setName(exterior_surface_constructions_name)

  surface_constructions = model.getDefaultSurfaceConstructionss()
  surface_constructions.each do |surface_construction|
    runner.registerInfo("Default Surface Construction = #{surface_construction.name()}")
  end

  exterior_surface_constructions = surface_constructions[-1]
end

if not option then
  exterior_surface_constructions = model.getDefaultSurfaceConstructionsByName(exterior_surface_constructions_name)

  runner.registerInfo("ESC after ByName: #{exterior_surface_constructions}")

  if exterior_surface_constructions.is_initialized then
    exterior_surface_constructions = exterior_surface_constructions.get
    runner.registerInfo("ESC after Get: #{exterior_surface_constructions}")
  else
    exterior_surface_constructions = OpenStudio::Model::DefaultSurfaceConstructions.new(model)
    exterior_surface_constructions = exterior_surface_constructions.setName(exterior_surface_constructions_name)
    runner.registerInfo("ESC after new and name: #{exterior_surface_constructions}")
  end
end

I find that I am having to take a similar detour to generate a new DefaultConstructionSet, also. It seems that the option = false approach would be the more correct one as it first checks if the object already exists, and it works with other objects in OpenStudio 3.2.1, but not with these two?

edit retag flag offensive close merge delete

Comments

I don't have an answer, but here's the Ruby Style Guide in case you haven't seen it.

MatthewSteen gravatar image MatthewSteen  ( 2021-12-31 16:49:17 -0600 )edit

With the option = false, if it does find an object of that type with the correct name, it just gets an existing object and isn’t expected to produce a new object. How are you determine that an object by that name isn’t in the model. It would also be useful to test it with both an expect name of an existing object, and a name that doesn’t exist in the model, that will exercise both paths of the false code.

David Goldwasser gravatar image David Goldwasser  ( 2021-12-31 17:53:28 -0600 )edit

Well, I figure "exterior_surface_constructions.is_initialized" would evaluate to "true" if "exterior_surface_constructions = model.getDefaultSurfaceConstructionsByName(exterior_surface_constructions_name)" had indeed found an existing object in the model. In that case, I would just "get" it for subsequent use. If there was no such object in the model, I figure "exterior_surface_constructions.is_initialized" would evaluate to "false". In that case, "exterior_surface_constructions = OpenStudio::Model::DefaultSurfaceConstructions.new(model)" simply creates a new one for subsequent use?

mattkoch gravatar image mattkoch  ( 2021-12-31 18:35:33 -0600 )edit

Thanks Matthew. I had not seen that style guide yet. I think I am still at the "function over form" stage in my learning, but I am getting better. In fact, briefly reviewing the guide, I don't think I am all that far off?

mattkoch gravatar image mattkoch  ( 2021-12-31 18:37:26 -0600 )edit

@mattkoch. So there isn’t an ruby error at a specific line, so the code runs to completion. Based on output of the info statements is it finding a matching object, or is it claiming to make a new object (but you’d an’t find the then object?); or are you looping through this many times, expecting both to happen. What if you do a puts exterior_surface_constructions right after you make the new object, what does it return.

David Goldwasser gravatar image David Goldwasser  ( 2021-12-31 18:45:36 -0600 )edit

1 Answer

Sort by » oldest newest most voted
1

answered 2022-01-01 01:34:09 -0600

mattkoch gravatar image

David, it is as if the exterior_surface_constructions object disappears after I create it. As I just went through the code again, it became obvious that:

exterior_surface_constructions = exterior_surface_constructions.setName(exterior_surface_constructions_name)

is wrong and replaces the object with its name, while:

exterior_surface_constructions.setName(exterior_surface_constructions_name)

is correct and leaves the object intact. Sorry for having been so dense and causing unnecessary trouble. I think I need to take a break from measure writing for day or two.

edit flag offensive delete link more

Comments

@mattkoch, glad you figured out out. I should have noticed that the variable was being re-defined. So the object was still in the model, but the variable just wasn’t pointing to it anymore.

David Goldwasser gravatar image David Goldwasser  ( 2022-01-01 18:33:31 -0600 )edit

Yes, Sir. A classical case of fried brain late at night.

mattkoch gravatar image mattkoch  ( 2022-01-01 22:57:12 -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

Question Tools

1 follower

Stats

Asked: 2021-12-31 14:36:52 -0600

Seen: 100 times

Last updated: Dec 31 '21