First time here? Check our help page!
1

OpenStudio 3.2.1 - trouble with object creation in measure

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?

mattkoch's avatar
1.1k
mattkoch
asked 2021-12-31 14:36:52 -0500
edit flag offensive 0 remove flag 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's avatar MatthewSteen (2021-12-31 16:49:17 -0500) 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's avatar David Goldwasser (2021-12-31 17:53:28 -0500) 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's avatar mattkoch (2021-12-31 18:35:33 -0500) 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's avatar mattkoch (2021-12-31 18:37:26 -0500) 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's avatar David Goldwasser (2021-12-31 18:45:36 -0500) edit
add a comment see more comments

1 Answer

1

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.

mattkoch's avatar
1.1k
mattkoch
answered 2022-01-01 01:34:09 -0500
edit flag offensive 0 remove flag delete link

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's avatar David Goldwasser (2022-01-01 18:33:31 -0500) edit

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

mattkoch's avatar mattkoch (2022-01-01 22:57:12 -0500) edit
add a comment see more comments