Class: RunReportingMeasures

Inherits:
Object
  • Object
show all
Includes:
OpenStudio::Workflow::ApplyMeasures
Defined in:
lib/openstudio/workflow/jobs/run_reporting_measures/run_reporting_measures.rb

Constant Summary

Constants included from OpenStudio::Workflow::ApplyMeasures

OpenStudio::Workflow::ApplyMeasures::MEASURE_TYPES

Instance Method Summary collapse

Methods included from OpenStudio::Workflow::ApplyMeasures

#apply_arguments, #apply_measure, #apply_measures, #apply_variables

Constructor Details

#initialize(directory, logger, time_logger, adapter, options = {}) ⇒ RunReportingMeasures

Returns a new instance of RunReportingMeasures.



28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/openstudio/workflow/jobs/run_reporting_measures/run_reporting_measures.rb', line 28

def initialize(directory, logger, time_logger, adapter, options = {})
  defaults = {}
  @options = defaults.merge(options)
  @directory = directory
  @run_directory = "#{@directory}/run"
  @adapter = adapter
  @logger = logger
  @time_logger = time_logger
  @results = {}
  @output_attributes = {}

  # TODO: we shouldn't have to keep loading this file if we need it. It should be availabe for any job.
  # TODO: passing in the options everytime is ridiculuous
  @analysis_json = @adapter.get_problem(@directory, @options)

  @logger.info "#{self.class} passed the following options #{@options}"

  @model = load_model @options[:run_openstudio][:osm]

  # TODO: should read the name of the sql output file via the :run_openstudio options hash
  # I want to reiterate that this is cheezy!
  @sql_filename = "#{@run_directory}/eplusout.sql"
  fail "EnergyPlus SQL file did not exist #{@sql_filename}" unless File.exist? @sql_filename

  @objective_functions = {}
end

Instance Method Details

#add_data(sql, query, hdr, area, val) ⇒ Object

add results from sql method



325
326
327
328
329
330
331
332
333
334
335
# File 'lib/openstudio/workflow/jobs/run_reporting_measures/run_reporting_measures.rb', line 325

def add_data(sql, query, hdr, area, val)
  row = []
  val = sql_query(sql, 'AnnualBuildingUtilityPerformanceSummary', query) if val.nil?
  row << hdr
  if area.nil?
    row << val
  else
    row << (val * 1000) / area
  end
  row
end

#add_data2(sql, query, hdr, area, val) ⇒ Object

add results from sql method



338
339
340
341
342
343
344
345
346
347
348
# File 'lib/openstudio/workflow/jobs/run_reporting_measures/run_reporting_measures.rb', line 338

def add_data2(sql, query, hdr, area, val)
  row = []
  val = sql_query(sql, 'BUILDING ENERGY PERFORMANCE - ELECTRICITY', query) if val.nil?
  row << hdr
  if area.nil?
    row << val
  else
    row << (val * 1000) / area
  end
  row
end

#add_data3(sql, query, hdr, area, val) ⇒ Object

add results from sql method



351
352
353
354
355
356
357
358
359
360
361
# File 'lib/openstudio/workflow/jobs/run_reporting_measures/run_reporting_measures.rb', line 351

def add_data3(sql, query, hdr, area, val)
  row = []
  val = sql_query(sql, 'BUILDING ENERGY PERFORMANCE - NATURAL GAS', query) if val.nil?
  row << hdr
  if area.nil?
    row << val
  else
    row << (val * 1000) / area
  end
  row
end

#add_data4(sql, query, hdr, area, val) ⇒ Object

add results from sql method



364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
# File 'lib/openstudio/workflow/jobs/run_reporting_measures/run_reporting_measures.rb', line 364

def add_data4(sql, query, hdr, area, val)
  row = []

  if val.nil?
    val = 0

    ['INTERIORLIGHTS:ELECTRICITY', 'EXTERIORLIGHTS:ELECTRICITY', 'INTERIOREQUIPMENT:ELECTRICITY', 'EXTERIOREQUIPMENT:ELECTRICITY',
     'FANS:ELECTRICITY', 'PUMPS:ELECTRICITY', 'HEATING:ELECTRICITY', 'COOLING:ELECTRICITY', 'HEATREJECTION:ELECTRICITY',
     'HUMIDIFIER:ELECTRICITY', 'HEATRECOVERY:ELECTRICITY', 'WATERSYSTEMS:ELECTRICITY', 'COGENERATION:ELECTRICITY', 'REFRIGERATION:ELECTRICITY'].each do |end_use|
      tmp_query = query + " AND ColumnName='#{end_use}'"
      tmp_val = sql_query(sql, 'BUILDING ENERGY PERFORMANCE - ELECTRICITY', tmp_query)
      val += tmp_val unless tmp_val.nil?
    end
  end

  row << hdr
  if area.nil?
    row << val
  else
    row << (val * 1000) / area
  end
  row
end

#add_element(hash, var_name, value, xpath = nil) ⇒ Object



297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
# File 'lib/openstudio/workflow/jobs/run_reporting_measures/run_reporting_measures.rb', line 297

def add_element(hash, var_name, value, xpath = nil)
  values_hash = {}
  values_hash['name'] = var_name

  # store correct datatype
  store_val = nil
  if value.nil?
    store_val = nil
  elsif value == 'true'
    store_val = true
  elsif value == 'false'
    store_val = false
  else
    test = value.to_s
    value = test.match('\.').nil? ? Integer(test) : Float(test) rescue test.to_s
    if value.is_a?(Fixnum) || value.is_a?(Float)
      store_val = value.to_f
    else
      store_val = value.to_s
    end
  end
  values_hash['value'] = store_val
  values_hash['xpath'] = xpath unless xpath.nil?

  hash['data']['variables'] << values_hash
end

#performObject



55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
# File 'lib/openstudio/workflow/jobs/run_reporting_measures/run_reporting_measures.rb', line 55

def perform
  @logger.info "Calling #{__method__} in the #{self.class} class"
  @logger.info 'RunPostProcess Retrieving datapoint and problem'

  begin
    @datapoint_json = @adapter.get_datapoint(@directory, @options)
    @analysis_json = @adapter.get_problem(@directory, @options)

    @time_logger.start('Running standard post process')
    if @options[:use_monthly_reports]
      run_monthly_postprocess
    else
      run_standard_postprocess
    end
    @time_logger.stop('Running standard post process')

    translate_csv_to_json

    run_packaged_measures

    if @analysis_json && @analysis_json[:analysis]
      apply_measures(:reporting_measure)
    end

    @logger.info 'Saving reporting measures output attributes JSON'
    File.open("#{@run_directory}/reporting_measure_attributes.json", 'w') do |f|
      f << JSON.pretty_generate(@output_attributes)
    end

    run_extract_inputs_and_outputs

    @logger.info "Objective Function JSON is #{@objective_functions}"
    obj_fun_file = "#{@directory}/objectives.json"
    FileUtils.rm_f(obj_fun_file) if File.exist?(obj_fun_file)
    File.open(obj_fun_file, 'w') { |f| f << JSON.pretty_generate(@objective_functions) }

  rescue => e
    log_message = "Runner error #{__FILE__} failed with #{e.message}, #{e.backtrace.join("\n")}"
    raise log_message
  end

  @results
end

#run_extract_inputs_and_outputsObject



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
134
135
136
137
138
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
181
182
183
184
185
186
187
188
189
190
191
192
# File 'lib/openstudio/workflow/jobs/run_reporting_measures/run_reporting_measures.rb', line 99

def run_extract_inputs_and_outputs
  # For xml, the measure attributes are in the measure_attributes_xml.json file
  # TODO: somehow pass the metadata around on which JSONs to suck into the database
  if File.exist? "#{@run_directory}/measure_attributes_xml.json"
    h = JSON.parse(File.read("#{@run_directory}/measure_attributes_xml.json"), symbolize_names: true)
    h = rename_hash_keys(h)
    @results.merge! h
  end

  # Inputs are in the measure_attributes.json file
  if File.exist? "#{@run_directory}/measure_attributes.json"
    h = JSON.parse(File.read("#{@run_directory}/measure_attributes.json"), symbolize_names: true)
    h = rename_hash_keys(h)
    @results.merge! h
  end

  # Inputs are in the reporting_measure_attributes.jsonfile
  if File.exist? "#{@run_directory}/reporting_measure_attributes.json"
    h = JSON.parse(File.read("#{@run_directory}/reporting_measure_attributes.json"), symbolize_names: true)
    h = rename_hash_keys(h)
    @results.merge! h
  end

  # Initialize the objective function variable.
  @objective_functions = {}
  if File.exist? "#{@run_directory}/standard_report_legacy.json"
    h = JSON.parse(File.read("#{@run_directory}/standard_report_legacy.json"), symbolize_names: true)
    h = rename_hash_keys(h)
    @results[:standard_report_legacy] = h
  end

  @logger.info 'Saving the result hash to file'
  File.open("#{@run_directory}/results.json", 'w') { |f| f << JSON.pretty_generate(@results) }

  @logger.info 'Iterating over Analysis JSON Output Variables'
  # Save the objective functions to the object for sending back to the simulation executive

  if @analysis_json[:analysis] && @analysis_json[:analysis][:output_variables]
    @analysis_json[:analysis][:output_variables].each do |variable|
      # determine which ones are the objective functions (code smell: todo: use enumerator)
      if variable[:objective_function]
        @logger.info "Looking for objective function #{variable[:name]}"
        # TODO: move this to cleaner logic. Use ostruct?
        if variable[:name].include? '.'
          k, v = variable[:name].split('.')
          if @results.key?(k.to_sym) && @results[k.to_sym][v.to_sym]
            @objective_functions["objective_function_#{variable[:objective_function_index] + 1}"] = @results[k.to_sym][v.to_sym]
            if variable[:objective_function_target]
              @logger.info "Found objective function target for #{variable[:name]}"
              @objective_functions["objective_function_target_#{variable[:objective_function_index] + 1}"] = variable[:objective_function_target].to_f
            end
            if variable[:scaling_factor]
              @logger.info "Found scaling factor for #{variable[:name]}"
              @objective_functions["scaling_factor_#{variable[:objective_function_index] + 1}"] = variable[:scaling_factor].to_f
            end
            if variable[:objective_function_group]
              @logger.info "Found objective function group for #{variable[:name]}"
              @objective_functions["objective_function_group_#{variable[:objective_function_index] + 1}"] = variable[:objective_function_group].to_f
            end
          else
            @logger.warn "No results for objective function #{variable[:name]}"
            @objective_functions["objective_function_#{variable[:objective_function_index] + 1}"] = Float::MAX
            @objective_functions["objective_function_target_#{variable[:objective_function_index] + 1}"] = nil
            @objective_functions["scaling_factor_#{variable[:objective_function_index] + 1}"] = nil
            @objective_functions["objective_function_group_#{variable[:objective_function_index] + 1}"] = nil
          end
        else
          # variable name is not nested with the '.' -- this is for legacy purposes and should be deleted on 9/30/2014
          if @results[variable[:name]]
            @objective_functions["objective_function_#{variable[:objective_function_index] + 1}"] = @results[k.to_sym][v.to_sym]
            if variable[:objective_function_target]
              @logger.info "Found objective function target for #{variable[:name]}"
              @objective_functions["objective_function_target_#{variable[:objective_function_index] + 1}"] = variable[:objective_function_target].to_f
            end
            if variable[:scaling_factor]
              @logger.info "Found scaling factor for #{variable[:name]}"
              @objective_functions["scaling_factor_#{variable[:objective_function_index] + 1}"] = variable[:scaling_factor].to_f
            end
            if variable[:objective_function_group]
              @logger.info "Found objective function group for #{variable[:name]}"
              @objective_functions["objective_function_group_#{variable[:objective_function_index] + 1}"] = variable[:objective_function_group].to_f
            end
          else
            @logger.warn "No results for objective function #{variable[:name]}"
            @objective_functions["objective_function_#{variable[:objective_function_index] + 1}"] = Float::MAX
            @objective_functions["objective_function_target_#{variable[:objective_function_index] + 1}"] = nil
            @objective_functions["scaling_factor_#{variable[:objective_function_index] + 1}"] = nil
            @objective_functions["objective_function_group_#{variable[:objective_function_index] + 1}"] = nil
          end
        end
      end
    end
  end
end

#sql_query(sql, report_name, query) ⇒ Object



280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
# File 'lib/openstudio/workflow/jobs/run_reporting_measures/run_reporting_measures.rb', line 280

def sql_query(sql, report_name, query)
  val = nil
  result = sql.execAndReturnFirstDouble("SELECT Value FROM TabularDataWithStrings WHERE ReportName='#{report_name}' AND #{query}")
  if result.empty?
    @logger.warn "Query for run_monthly_postprocess failed for #{query}"
  else
    begin
      val = result.get
    rescue => e
      @logger.info "#{__FILE__} failed with #{e.message}, #{e.backtrace.join("\n")}"
      val = nil
    end
  end

  val
end