Class: Utilities

Inherits:
Object
  • Object
show all
Defined in:
lib/openstudio-standards/btap/utilities.rb

Class Method Summary collapse

Class Method Details

.check_bounds(left_value, left_operator, center_value, right_operator, right_value) ⇒ Object

This method checks the bounds and raise an exception if the value is out of bounds.

Parameters:

  • left_value (Number)
  • left_operator (String)
  • center_value (Number)
  • right_operator (String)
  • right_value (Number)

Author:



43
44
45
46
# File 'lib/openstudio-standards/btap/utilities.rb', line 43

def self.check_bounds(left_value,left_operator,center_value,right_operator,right_value)
  operation = left_value.to_s + " " + left_operator.to_s + " " + center_value.to_s + " or " + center_value.to_s + " " + right_operator.to_s + " " + right_value.to_s
  raise("Error: in bounds." + operation_1  )  unless eval(operation +" ? true :false")
end

.kdiff3_model_idf(model1, model2, model3 = "") ⇒ Object

This method will take 3 variables and bring up Kdiff3 for a 2 or 3-way diff view of the OSM file. The second argument is optional, it will compare the current model with the argument model(s). This is handy for quickly viewing changes to the file during runtime for debugging and QA.

Parameters:

  • model1 (OpenStudio::model::Model)

    A model object.

  • model2 (OpenStudio::model::Model)

    A model object.

  • model3 (OpenStudio::model::Model) (defaults to: "")

    A model object.

Author:



77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
# File 'lib/openstudio-standards/btap/utilities.rb', line 77

def self.kdiff3_model_idf(model1, model2, model3 = "")
  Dir::mkdir("C:\\kdiff_test") unless File.exists?("C:\\kdiff_test")

  OpenStudio::EnergyPlus::ForwardTranslator.new().translateModel(model1).toIdfFile().save(OpenStudio::Path.new("c:\\kdiff_test\\diffA.idf"),true)
  self.sort_idf_file("c:\\kdiff_test\\diffA.idf")
  OpenStudio::EnergyPlus::ForwardTranslator.new().translateModel(model2).toIdfFile().save(OpenStudio::Path.new("c:\\kdiff_test\\diffB.idf"),true)
  self.sort_idf_file("c:\\kdiff_test\\diffB.idf")
  if model3 == ""
    system(self.get_diff_client + "\\kdiff3.exe", "c:\\kdiff_test\\diffA.idf.sorted", "c:\\kdiff_test\\diffB.idf.sorted")
  else
    OpenStudio::EnergyPlus::ForwardTranslator.new().translateModel(model3).toIdfFile().save(OpenStudio::Path.new("c:\\kdiff_test\\diffC.idf"),true)
    self.sort_idf_file("c:\\kdiff_test\\diffC.idf")
    system(self.get_diff_client + "\\kdiff3.exe", "c:\\kdiff_test\\diffA.idf.sorted", "c:\\kdiff_test\diffB.idf.sorted", "c:\\kdiff_test\diffC.idf.sorted" )
  end
end

.kdiff3_model_osm(model1, model2, model3 = "") ⇒ Object

This method will take 3 variables and will bring up Kdiff3 for a 2 or 3-way diff view of the OSM file. argument model(s). This is handy for quickly viewing changes to the file during runtime for debugging and QA.

Parameters:

  • model1 (OpenStudio::model::Model)

    A model object.

  • model2 (OpenStudio::model::Model)

    A model object.

  • model3 (OpenStudio::model::Model) (defaults to: "")

    A model object.

Author:



55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/openstudio-standards/btap/utilities.rb', line 55

def self.kdiff3_model_osm(model1, model2, model3 = "")
  Dir::mkdir("C:\\kdiff_test") unless File.exists?("C:\\kdiff_test")
  model1.save(OpenStudio::Path.new("c:\\kdiff_test\\diffA.osm"))
  model2.save(OpenStudio::Path.new("c:\\kdiff_test\\diffB.osm"))
  if model3 == ""
    system(self.get_diff_client + "\\kdiff3.exe", "c:\\kdiff_test\\diffA.osm", "c:\\kdiff_test\\diffB.osm")
  else
    model3.save(OpenStudio::Path.new("c:\\kdiff_test\\C.osm"))
    system(self.get_diff_client + "\\kdiff3.exe", "c:\\kdiff_test\\diffA.osm", "c:\\kdiff_test\\diffB.osm", "c:\\kdiff_test\\diffC.osm" )
  end
  FileUtils.rm_rf("C:\\kdiff_test")

end

.sort_idf_file(idf_file) ⇒ Object

This method will sort an idf file and produce a sorted idf file. This is helpful for doing diffs on idf files.

Parameters:

Author:



96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
# File 'lib/openstudio-standards/btap/utilities.rb', line 96

def self.sort_idf_file(idf_file)
  idf_model = OpenStudio::IdfFile::load(OpenStudio::Path.new(idf_file), "EnergyPlus".to_IddFileType).get
  save_filename = idf_file + ".sorted"

  # Iterate over all the IDF objects and put into a ruby array.
  # Note that you must strip the object name because for some reason it has
  # trailing characters
  sorted_idf = {}
  verobj = idf_model.versionObject
  if not verobj.empty?
    verobj = verobj.get
    objname = verobj.getString(0).get
    obj = verobj.to_s.gsub("!-", "!")
    sorted_idf["#{verobj.iddObject.name()} #{objname}"] = obj
  end
  idf_model.objects.each do |object|
    if object.iddObject.type != "CommentOnly".to_IddObjectType
      objname = object.name()
      objname = objname.to_s.strip
      if objname == ""
        # puts "[DEBUG] ObjectName is Blank, using first field"
        objname = object.getString(0).get
      end
      # puts "Class Name: #{object.iddObject.name()}    Object Name: #{objname}      Size: #{object.numFields()}"

      # Clean up the comment field. They comments flags change on translation!
      obj = object.to_s.gsub("!-", "!")
      sorted_idf["#{object.iddObject.name()} #{objname}"] = obj
    end
  end

  out = sorted_idf.sort #returns a nested array, 0 is key, 1 is value
  File.open(save_filename, 'w') do |file|
    out.each do |value|
      file << value[1]
    end
  end
end