Class: OpenStudio::Geb::Runner

Inherits:
Object
  • Object
show all
Defined in:
lib/openstudio/geb/run.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(baseline_dir_str, measure_dict, run_output_path, weather_file_path) ⇒ Runner

Returns a new instance of Runner.



12
13
14
15
16
17
# File 'lib/openstudio/geb/run.rb', line 12

def initialize(baseline_dir_str, measure_dict, run_output_path, weather_file_path)
  @baseline_dir_str = baseline_dir_str
  @measure_dict = measure_dict
  @run_output_path = run_output_path
  @weather_file_path = weather_file_path
end

Instance Attribute Details

#baseline_osmObject

baseline OpenStudio model Type: OpenStudio::Model::Model



174
175
176
# File 'lib/openstudio/geb/run.rb', line 174

def baseline_osm
  @baseline_osm
end

#measure_dictObject

dictionary of GEB measures Type: hash



178
179
180
# File 'lib/openstudio/geb/run.rb', line 178

def measure_dict
  @measure_dict
end

#run_output_pathObject

run test output path Type: string



182
183
184
# File 'lib/openstudio/geb/run.rb', line 182

def run_output_path
  @run_output_path
end

Instance Method Details

#apply_measures(osw_result_folder) ⇒ Object

measure_dict: hash of measures and their arguments => arguments



74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
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
134
135
# File 'lib/openstudio/geb/run.rb', line 74

def apply_measures(osw_result_folder)
  # create osw_file
  osw_path = File.join(osw_result_folder, "geb.osw")
  # get the folder of measures
  measures_path = File.expand_path("../../../measures/", __FILE__ )

  steps = []
  # measure_dict: hash of measures and their arguments => {measure_name: arguments}
  # first adjust the measure sequence to be OpenStudio measure => EnergyPlus measure => Reporting measure
  energyplus_measures = {}
  report_measures = {}
  @measure_dict.each_pair do |m_name, para_dict|
    # get all EnergyPlus and Reporting measures out and remove original ones
    # now measure_dir_name uses folder name rather than absolute path, adding "measure_paths" to the osw file
    File.open("#{measures_path}/#{para_dict['measure_dir_name']}/measure.rb", 'r') do |file|
      file.each_line do |line|
        if line =~ /class.+EnergyPlusMeasure/
          energyplus_measures[m_name] = @measure_dict[m_name]
          @measure_dict.delete(m_name)
          break
        elsif line =~ /class.+ReportingMeasure/
          report_measures[m_name] = @measure_dict[m_name]
          @measure_dict.delete(m_name)
          break
        end
      end
    end
  end

  # add back the EnergyPlusMeasures to the end of the measure list
  unless energyplus_measures.empty?
    energyplus_measures.each_key do |m_name|
      @measure_dict[m_name] = energyplus_measures[m_name]
    end
  end
  # add back the ReportingMeasures to the very end of the measure list
  unless report_measures.empty?
    report_measures.each_key do |m_name|
      @measure_dict[m_name] = report_measures[m_name]
    end
  end

  # add the final meausres to the osw steps in order
  @measure_dict.each_pair do |m_name, para_dict|
    measure = {}
    measure['measure_dir_name'] = para_dict['measure_dir_name']
    measure['arguments'] = para_dict['arguments']
    steps << measure
  end

  osw = {}
  osw["measure_paths"] = [measures_path]
  osw["weather_file"] = @weather_file_path
  osw["seed_file"] = @baseline_dir_str
  osw["steps"] = steps

  File.open(osw_path, 'w') do |f|
    f << JSON.pretty_generate(osw)
  end

  return osw_path
end

#report_and_save_errorsObject



163
164
165
166
167
168
169
# File 'lib/openstudio/geb/run.rb', line 163

def report_and_save_errors
  # Report out Info, Warning, and Errors
  log_file_path = "#{@run_output_path}/openstudio-standards.log"
  log_messages_to_file(log_file_path, false)
  errors = get_logs(OpenStudio::Error)
  return errors
end

#runObject



19
20
21
22
23
24
25
26
27
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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/openstudio/geb/run.rb', line 19

def run
  # load baseline model
  @baseline_osm = safe_load_osm(@baseline_dir_str)

  # run baseline first. This is mainly for reporting GEB metrics purpose
  # remove old osw_file
  baseline_osw_result_folder = File.join(@run_output_path, "baseline")
  rm_old_folder_and_create_new(baseline_osw_result_folder)

  # add facility net purchased electricity rate as output variable to the baseline
  demand_var = OpenStudio::Model::OutputVariable.new('Facility Net Purchased Electricity Rate', @baseline_osm)
  demand_var.setReportingFrequency('timestep')
  @baseline_osm.getTimestep.setNumberOfTimestepsPerHour(4)  # change to 15min timestep
  @baseline_dir_str = File.join(baseline_osw_result_folder, "baseline.osm")  # update baseline model
  @baseline_osm.save(@baseline_dir_str, true)

  # create baseline osw
  baseline_osw_path = File.join(baseline_osw_result_folder, "baseline.osw")
  baseline_osw = {}
  baseline_osw["weather_file"] = @weather_file_path
  baseline_osw["seed_file"] = @baseline_dir_str

  # TODO: remove
  # baseline_osw["root"] = "/Users/sky/Sites/Openstudio-GEB-gem/"

  File.open(baseline_osw_path, 'w') do |f|
    f << JSON.pretty_generate(baseline_osw)
  end

  # run baseline osw
  return false unless run_osw(baseline_osw_path, baseline_osw_result_folder)

  # remove old osw_file
  geb_osw_result_folder = File.join(@run_output_path, "run_geb_measures")
  rm_old_folder_and_create_new(geb_osw_result_folder)

  # apply measures and create osw file
  geb_osw_path = apply_measures(geb_osw_result_folder)

  # run geb measure osw
  return false unless run_osw(geb_osw_path, geb_osw_result_folder)
  # runner = OpenStudio::Extension::Runner.new(@run_output_path)
  # failures = runner.run_osws([baseline_osw_path, geb_osw_path], num_parallel = 2)
  # puts "failures: #{failures.inspect}"

  # if failures.empty?
  #   return true
  # else
  #   return false
  # end

  return true
end

#run_osw(osw_path, osw_result_folder) ⇒ Object



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
# File 'lib/openstudio/geb/run.rb', line 137

def run_osw(osw_path, osw_result_folder)
  cli_path = OpenStudio.getOpenStudioCLI
  cmd = "\"#{cli_path}\" run -w \"#{osw_path}\""
  puts cmd
  system(cmd)

  result_osw = postprocess_out_osw(osw_result_folder)
  if result_osw[:completed_status] == 'Success'
    return true
  else
    return false
  end

  # Open3.capture3 can't capture error in this case
  # stdout_str, stderr_str, status = Open3.capture3(get_run_env, cmd)
  # if status.success? && status.success? != nil
  #   OpenStudio.logFree(OpenStudio::Debug, 'openstudio.standards.command', "Successfully ran command: '#{cmd}'")
  #   return true
  # else
  #   OpenStudio.logFree(OpenStudio::Error, 'openstudio.standards.command', "Error running command: '#{cmd}'")
  #   OpenStudio.logFree(OpenStudio::Error, 'openstudio.standards.command', "stdout: #{stdout_str}")
  #   OpenStudio.logFree(OpenStudio::Error, 'openstudio.standards.command', "stderr: #{stderr_str}")
  #   return false
  # end
end