Class: OpenStudio::Analysis::Formulation
- Inherits:
-
Object
- Object
- OpenStudio::Analysis::Formulation
- Defined in:
- lib/openstudio/analysis/formulation.rb
Instance Attribute Summary collapse
-
#algorithm ⇒ Object
Returns the value of attribute algorithm.
-
#analysis_type ⇒ Object
Returns the value of attribute analysis_type.
-
#display_name ⇒ Object
Returns the value of attribute display_name.
-
#libraries ⇒ Object
readonly
Returns the value of attribute libraries.
-
#osw_path ⇒ Object
Returns the value of attribute osw_path.
-
#outputs ⇒ Object
readonly
Returns the value of attribute outputs.
-
#seed_model ⇒ Object
Returns the value of attribute seed_model.
-
#seed_models ⇒ Object
readonly
Returns the value of attribute seed_models.
-
#server_scripts ⇒ Object
readonly
Returns the value of attribute server_scripts.
-
#weather_file ⇒ Object
Returns the value of attribute weather_file.
-
#weather_files ⇒ Object
readonly
the attributes below are used for packaging data into the analysis zip file.
-
#worker_finalizes ⇒ Object
readonly
Returns the value of attribute worker_finalizes.
-
#worker_inits ⇒ Object
readonly
Returns the value of attribute worker_inits.
-
#workflow ⇒ Object
Returns the value of attribute workflow.
Class Method Summary collapse
-
.from_hash(h, seed_dir = nil, weather_dir = nil) ⇒ Object
Load the analysis JSON from a hash (with symbolized keys).
Instance Method Summary collapse
- #add_directory_to_zip(zipfile, local_directory, relative_zip_directory) ⇒ Object
- #add_directory_to_zip_osa(zipfile, local_directory, relative_zip_directory) ⇒ Object
-
#add_output(output_hash) ⇒ Object
Add an output of interest to the problem formulation.
-
#convert_osw(osw_filename, *measure_paths) ⇒ Object
convert an OSW to an OSA osw_filename is the full path to the OSW file assumes the associated files and directories are in the same location /example.osw /measures /seeds /weather.
-
#initialize(display_name) ⇒ Object
constructor
Create an instance of the OpenStudio::Analysis::Formulation.
-
#name ⇒ Object
return the machine name of the analysis.
-
#save(filename, version = 1) ⇒ Boolean
save the file to JSON.
- #save_osa_zip(filename, all_weather_files = false, all_seed_files = false) ⇒ Object
-
#save_static_data_point(filename, version = 1) ⇒ Boolean
save the data point JSON with the variables set to the static values.
-
#save_zip(filename) ⇒ Boolean
save the analysis zip file which contains the measures, seed model, weather file, and init/final scripts.
-
#to_hash(version = 1) ⇒ Hash
return a hash.
-
#to_static_data_point_hash(version = 1) ⇒ Hash
return a hash of the data point with the static variables set.
Constructor Details
#initialize(display_name) ⇒ Object
Create an instance of the OpenStudio::Analysis::Formulation
75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 |
# File 'lib/openstudio/analysis/formulation.rb', line 75 def initialize(display_name) @display_name = display_name @analysis_type = nil @outputs = [] @workflow = OpenStudio::Analysis::Workflow.new # Initialize child objects (expect workflow) @weather_file = WeatherFile.new @seed_model = SeedModel.new @algorithm = OpenStudio::Analysis::AlgorithmAttributes.new # Analysis Zip attributes @weather_files = SupportFiles.new @seed_models = SupportFiles.new @worker_inits = SupportFiles.new @worker_finalizes = SupportFiles.new @libraries = SupportFiles.new @server_scripts = ServerScripts.new end |
Instance Attribute Details
#algorithm ⇒ Object
Returns the value of attribute algorithm.
60 61 62 |
# File 'lib/openstudio/analysis/formulation.rb', line 60 def algorithm @algorithm end |
#analysis_type ⇒ Object
Returns the value of attribute analysis_type.
56 57 58 |
# File 'lib/openstudio/analysis/formulation.rb', line 56 def analysis_type @analysis_type end |
#display_name ⇒ Object
Returns the value of attribute display_name.
58 59 60 |
# File 'lib/openstudio/analysis/formulation.rb', line 58 def display_name @display_name end |
#libraries ⇒ Object (readonly)
Returns the value of attribute libraries.
68 69 70 |
# File 'lib/openstudio/analysis/formulation.rb', line 68 def libraries @libraries end |
#osw_path ⇒ Object
Returns the value of attribute osw_path.
61 62 63 |
# File 'lib/openstudio/analysis/formulation.rb', line 61 def osw_path @osw_path end |
#outputs ⇒ Object (readonly)
Returns the value of attribute outputs.
57 58 59 |
# File 'lib/openstudio/analysis/formulation.rb', line 57 def outputs @outputs end |
#seed_model ⇒ Object
Returns the value of attribute seed_model.
54 55 56 |
# File 'lib/openstudio/analysis/formulation.rb', line 54 def seed_model @seed_model end |
#seed_models ⇒ Object (readonly)
Returns the value of attribute seed_models.
65 66 67 |
# File 'lib/openstudio/analysis/formulation.rb', line 65 def seed_models @seed_models end |
#server_scripts ⇒ Object (readonly)
Returns the value of attribute server_scripts.
69 70 71 |
# File 'lib/openstudio/analysis/formulation.rb', line 69 def server_scripts @server_scripts end |
#weather_file ⇒ Object
Returns the value of attribute weather_file.
55 56 57 |
# File 'lib/openstudio/analysis/formulation.rb', line 55 def weather_file @weather_file end |
#weather_files ⇒ Object (readonly)
the attributes below are used for packaging data into the analysis zip file
64 65 66 |
# File 'lib/openstudio/analysis/formulation.rb', line 64 def weather_files @weather_files end |
#worker_finalizes ⇒ Object (readonly)
Returns the value of attribute worker_finalizes.
67 68 69 |
# File 'lib/openstudio/analysis/formulation.rb', line 67 def worker_finalizes @worker_finalizes end |
#worker_inits ⇒ Object (readonly)
Returns the value of attribute worker_inits.
66 67 68 |
# File 'lib/openstudio/analysis/formulation.rb', line 66 def worker_inits @worker_inits end |
#workflow ⇒ Object
Returns the value of attribute workflow.
59 60 61 |
# File 'lib/openstudio/analysis/formulation.rb', line 59 def workflow @workflow end |
Class Method Details
.from_hash(h, seed_dir = nil, weather_dir = nil) ⇒ Object
Load the analysis JSON from a hash (with symbolized keys)
261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 |
# File 'lib/openstudio/analysis/formulation.rb', line 261 def self.from_hash(h, seed_dir = nil, weather_dir = nil) o = OpenStudio::Analysis::Formulation.new(h[:analysis][:display_name]) version = 1 if version == 1 h[:analysis][:output_variables].each do |ov| o.add_output(ov) end o.workflow = OpenStudio::Analysis::Workflow.load(workflow: h[:analysis][:problem][:workflow]) if weather_dir o.weather_file "#{weather_path}/#{File.basename(h[:analysis][:weather_file][:path])}" else o.weather_file = h[:analysis][:weather_file][:path] end if seed_dir o.seed_model "#{weather_path}/#{File.basename(h[:analysis][:seed][:path])}" else o.seed_model = h[:analysis][:seed][:path] end else raise "Version #{version} not defined for #{self.class} and #{__method__}" end o end |
Instance Method Details
#add_directory_to_zip(zipfile, local_directory, relative_zip_directory) ⇒ Object
662 663 664 665 666 667 668 669 |
# File 'lib/openstudio/analysis/formulation.rb', line 662 def add_directory_to_zip(zipfile, local_directory, relative_zip_directory) # puts "Add Directory #{local_directory}" Dir[File.join(local_directory.to_s, '**', '**')].each do |file| # puts "Adding File #{file}" zipfile.add(file.sub(local_directory, relative_zip_directory), file) end zipfile end |
#add_directory_to_zip_osa(zipfile, local_directory, relative_zip_directory) ⇒ Object
491 492 493 494 495 496 497 498 |
# File 'lib/openstudio/analysis/formulation.rb', line 491 def add_directory_to_zip_osa(zipfile, local_directory, relative_zip_directory) puts "Add Directory #{local_directory}" Dir[File.join(local_directory.to_s, '**', '**')].each do |file| puts "Adding File #{file}" zipfile.add(file.sub(local_directory, relative_zip_directory), file) end zipfile end |
#add_output(output_hash) ⇒ Object
Add an output of interest to the problem formulation
139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 |
# File 'lib/openstudio/analysis/formulation.rb', line 139 def add_output(output_hash) # Check if the name is already been added. exist = @outputs.find_index { |o| o[:name] == output_hash[:name] } # if so, update the fields but keep objective_function_index the same if exist original = @outputs[exist] if original[:objective_function] && !output_hash[:objective_function] return @outputs end output = original.merge(output_hash) output[:objective_function_index] = original[:objective_function_index] @outputs[exist] = output else output = { units: '', objective_function: false, objective_function_index: nil, objective_function_target: nil, #set default to nil or 1 if objective_function is true and this is not set objective_function_group: (output_hash[:objective_function] ? 1 : nil), scaling_factor: nil, #set default to false or true if objective_function is true and this is not set visualize: (output_hash[:objective_function] ? true : false), metadata_id: nil, export: true, }.merge(output_hash) #set display_name default to be name if its not set output[:display_name] = output_hash[:display_name] ? output_hash[:display_name] : output_hash[:name] #set display_name_short default to be display_name if its not set, this can be null if :display_name not set output[:display_name_short] = output_hash[:display_name_short] ? output_hash[:display_name_short] : output_hash[:display_name] # if the variable is an objective_function, then increment and # assign and objective function index if output[:objective_function] values = @outputs.select { |o| o[:objective_function] } output[:objective_function_index] = values.size end @outputs << output end @outputs end |
#convert_osw(osw_filename, *measure_paths) ⇒ Object
convert an OSW to an OSA osw_filename is the full path to the OSW file assumes the associated files and directories are in the same location
/example.osw
/measures
/seeds
/weather
370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 |
# File 'lib/openstudio/analysis/formulation.rb', line 370 def convert_osw(osw_filename, *measure_paths) # load OSW so we can loop over [:steps] if File.exist? osw_filename #will this work for both rel and abs paths? osw = JSON.parse(File.read(osw_filename), symbolize_names: true) @osw_path = File.(osw_filename) else raise "Could not find workflow file #{osw_filename}" end # set the weather and seed files if set in OSW # use :file_paths and look for files to set if osw[:file_paths] # seed_model, check if in OSW and not found in path search already if osw[:seed_file] osw[:file_paths].each do |path| puts "searching for seed at: #{File.join(File.(path), osw[:seed_file])}" if File.exist?(File.join(File.(path), osw[:seed_file])) puts "found seed_file: #{osw[:seed_file]}" self.seed_model = File.join(File.(path), osw[:seed_file]) break end end else warn "osw[:seed_file] is not defined" end # weather_file, check if in OSW and not found in path search already if osw[:weather_file] osw[:file_paths].each do |path| puts "searching for weather at: #{File.join(File.(path), osw[:weather_file])}" if File.exist?(File.join(File.(path), osw[:weather_file])) puts "found weather_file: #{osw[:weather_file]}" self.weather_file = File.join(File.(path), osw[:weather_file]) break end end else warn "osw[:weather_file] is not defined" end # file_paths is not defined in OSW, so warn and try to set else warn ":file_paths is not defined in the OSW." self.weather_file = osw[:weather_file] ? osw[:weather_file] : nil self.seed_model = osw[:seed_file] ? osw[:seed_file] : nil end #set analysis_type default to Single_Run self.analysis_type = 'single_run' #loop over OSW 'steps' and map over measures #there is no name/display name in the OSW. Just measure directory name #read measure.XML from directory to get name / display name #increment name by +_1 if there are duplicates #add measure #change default args to osw arg values osw[:steps].each do |step| #get measure directory measure_dir = step[:measure_dir_name] measure_name = measure_dir.split("measures/").last puts "measure_dir_name: #{measure_name}" #get XML # Loop over possible user defined *measure_paths, including the dir of the osw_filename path and :measure_paths, to find the measure, # then set measure_dir_abs_path to that path measure_dir_abs_path = '' paths_to_parse = [File.dirname(osw_filename), osw[:measure_paths], *measure_paths].flatten.compact.map { |path| File.join(File.(path), measure_dir, 'measure.xml') } puts "searching for xml's in: #{paths_to_parse}" xml = {} paths_to_parse.each do |path| if File.exist?(path) puts "found xml: #{path}" xml = parse_measure_xml(path) if !xml.empty? measure_dir_abs_path = path break end end end raise "measure #{measure_name} not found" if xml.empty? puts "" #add check for previous names _+1 count = 1 name = xml[:name] display_name = xml[:display_name] loop do measure = @workflow.find_measure(name) break if measure.nil? count += 1 name = "#{xml[:name]}_#{count}" display_name = "#{xml[:display_name]} #{count}" end #Add Measure to workflow @workflow.add_measure_from_path(name, display_name, measure_dir_abs_path) #this forces to an absolute path which seems constent with PAT #@workflow.add_measure_from_path(name, display_name, measure_dir) #this uses the path in the OSW which could be relative #Change the default argument values to the osw values #1. find measure in @workflow m = @workflow.find_measure(name) #2. loop thru osw args #check if the :argument is missing from the measure step, it shouldnt be but just in case give a clean message if step[:arguments].nil? raise "measure #{name} step has no arguments: #{step}" else step[:arguments].each do |k,v| #check if argument is in measure, otherwise setting argument_value will crash raise "OSW arg: #{k} is not in Measure: #{name}" if m.arguments.find_all { |a| a[:name] == k.to_s }.empty? #set measure arg to match osw arg m.argument_value(k.to_s, v) end end end end |
#name ⇒ Object
return the machine name of the analysis
183 184 185 |
# File 'lib/openstudio/analysis/formulation.rb', line 183 def name @display_name.to_underscore end |
#save(filename, version = 1) ⇒ Boolean
save the file to JSON. Will overwrite the file if it already exists
318 319 320 321 322 323 324 325 |
# File 'lib/openstudio/analysis/formulation.rb', line 318 def save(filename, version = 1) filename += '.json' if File.extname(filename) == '' FileUtils.mkdir_p File.dirname(filename) unless Dir.exist? File.dirname(filename) File.open(filename, 'w') { |f| f << JSON.pretty_generate(to_hash(version)) } true end |
#save_osa_zip(filename, all_weather_files = false, all_seed_files = false) ⇒ Object
354 355 356 357 358 359 360 |
# File 'lib/openstudio/analysis/formulation.rb', line 354 def save_osa_zip(filename, all_weather_files = false, all_seed_files = false) filename += '.zip' if File.extname(filename) == '' FileUtils.mkdir_p File.dirname(filename) unless Dir.exist? File.dirname(filename) save_analysis_zip_osa(filename, all_weather_files, all_seed_files) end |
#save_static_data_point(filename, version = 1) ⇒ Boolean
save the data point JSON with the variables set to the static values. Will overwrite the file if it already exists
332 333 334 335 336 337 338 339 |
# File 'lib/openstudio/analysis/formulation.rb', line 332 def save_static_data_point(filename, version = 1) filename += '.json' if File.extname(filename) == '' FileUtils.mkdir_p File.dirname(filename) unless Dir.exist? File.dirname(filename) File.open(filename, 'w') { |f| f << JSON.pretty_generate(to_static_data_point_hash(version)) } true end |
#save_zip(filename) ⇒ Boolean
save the analysis zip file which contains the measures, seed model, weather file, and init/final scripts
345 346 347 348 349 350 351 |
# File 'lib/openstudio/analysis/formulation.rb', line 345 def save_zip(filename) filename += '.zip' if File.extname(filename) == '' FileUtils.mkdir_p File.dirname(filename) unless Dir.exist? File.dirname(filename) save_analysis_zip(filename) end |
#to_hash(version = 1) ⇒ Hash
return a hash.
191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 |
# File 'lib/openstudio/analysis/formulation.rb', line 191 def to_hash(version = 1) # fail 'Must define an analysis type' unless @analysis_type if version == 1 h = { analysis: { display_name: @display_name, name: name, output_variables: @outputs, problem: { analysis_type: @analysis_type, algorithm: algorithm.to_hash(version), workflow: workflow.to_hash(version) } } } if @seed_model[:file] h[:analysis][:seed] = { file_type: File.extname(@seed_model[:file]).delete('.').upcase, path: "./seed/#{File.basename(@seed_model[:file])}" } else h[:analysis][:seed] = nil end # silly catch for if weather_file is not set wf = nil if @weather_file[:file] wf = @weather_file elsif !@weather_files.empty? # get the first EPW file (not the first file) wf = @weather_files.find { |w| File.extname(w[:file]).casecmp('.epw').zero? } end if wf h[:analysis][:weather_file] = { file_type: File.extname(wf[:file]).delete('.').upcase, path: "./weather/#{File.basename(wf[:file])}" } else # log: could not find weather file warn 'Could not resolve a valid weather file. Check paths to weather files' end h[:analysis][:file_format_version] = version h[:analysis][:cli_debug] = "--debug" h[:analysis][:cli_verbose] = "--verbose" h[:analysis][:run_workflow_timeout] = 28800 h[:analysis][:upload_results_timeout] = 28800 h[:analysis][:initialize_worker_timeout] = 28800 #-BLB I dont think this does anything. server_scripts are run if they are in #the /scripts/analysis or /scripts/data_point directories #but nothing is ever checked in the OSA. # h[:analysis][:server_scripts] = {} # This is a hack right now, but after the initial hash is created go back and add in the objective functions # to the the algorithm as defined in the output_variables list ofs = @outputs.map { |i| i[:name] if i[:objective_function] }.compact if h[:analysis][:problem][:algorithm] h[:analysis][:problem][:algorithm][:objective_functions] = ofs end h else raise "Version #{version} not defined for #{self.class} and #{__method__}" end end |
#to_static_data_point_hash(version = 1) ⇒ Hash
return a hash of the data point with the static variables set
294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 |
# File 'lib/openstudio/analysis/formulation.rb', line 294 def to_static_data_point_hash(version = 1) if version == 1 static_hash = {} # TODO: this method should be on the workflow step and bubbled up to this interface @workflow.items.map do |item| item.variables.map { |v| static_hash[v[:uuid]] = v[:static_value] } end h = { data_point: { set_variable_values: static_hash, status: 'na', uuid: SecureRandom.uuid } } h end end |