Class: GLHEProExportLoadsforGroundHeatExchangerSizing

Inherits:
OpenStudio::Measure::ReportingMeasure
  • Object
show all
Defined in:
lib/measures/GLHEProExportLoadsforGroundHeatExchangerSizing/measure.rb

Overview

start the measure

Instance Method Summary collapse

Instance Method Details

#arguments(model = nil) ⇒ Object

define the arguments that the user will input



26
27
28
29
30
# File 'lib/measures/GLHEProExportLoadsforGroundHeatExchangerSizing/measure.rb', line 26

def arguments(model = nil)
  args = OpenStudio::Measure::OSArgumentVector.new

  return args
end

#energyPlusOutputRequests(runner, user_arguments) ⇒ Object



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/measures/GLHEProExportLoadsforGroundHeatExchangerSizing/measure.rb', line 32

def energyPlusOutputRequests(runner, user_arguments)
  super(runner, user_arguments)

  result = OpenStudio::IdfObjectVector.new

  # use the built-in error checking
  unless runner.validateUserArguments(arguments, user_arguments)
    return result
  end

  # note: these variable requests replace the functionality of GLHEProSetupExportLoadsforGroundHeatExchangerSizing measure

  result << OpenStudio::IdfObject.load('Output:Variable,,District Heating Water Rate,hourly;').get
  result << OpenStudio::IdfObject.load('Output:Variable,,District Cooling Water Rate,hourly;').get

  # get the last model
  model = runner.lastOpenStudioModel
  if model.empty?
    runner.registerError('Cannot find last model.')
    return false
  end
  model = model.get

  # Report the outlet node conditions for each plant loop in the model
  # Rename the outlet node so that it makes sense in the report
  outlet_node_variable_names = []
  outlet_node_variable_names << 'System Node Temperature'
  outlet_node_variable_names << 'System Node Setpoint Temperature'
  outlet_node_variable_names << 'System Node Mass Flow Rate'
  model.getPlantLoops.each do |plant_loop|
    outlet_node = plant_loop.supplyOutletNode
    outlet_node_name = "#{plant_loop.name} Supply Outlet Node"
    outlet_node.setName(outlet_node_name)
    outlet_node_variable_names.each do |outlet_node_variable_name|
      result << OpenStudio::IdfObject.load("Output:Variable,#{outlet_node_name},#{outlet_node_variable_name},hourly;").get
    end
  end

  result
end

#nameObject

define the name that a user will see, this method may be deprecated as the display name in PAT comes from the name field in measure.xml



21
22
23
# File 'lib/measures/GLHEProExportLoadsforGroundHeatExchangerSizing/measure.rb', line 21

def name
  return 'GLHEProExportLoadsforGroundHeatExchangerSizing'
end

#run(runner, user_arguments) ⇒ Object

define what happens when the measure is run



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
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
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
259
260
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
289
290
291
292
293
294
295
296
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
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
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
# File 'lib/measures/GLHEProExportLoadsforGroundHeatExchangerSizing/measure.rb', line 74

def run(runner, user_arguments)
  super(runner, user_arguments)

  # use the built-in error checking
  if !runner.validateUserArguments(arguments, user_arguments)
    return false
  end

  # Get the model and sql file
  model = runner.lastOpenStudioModel
  if model.empty?
    runner.registerError('Cannot find last model.')
    return false
  end
  model = model.get

  sql = runner.lastEnergyPlusSqlFile
  if sql.empty?
    runner.registerError('Cannot find last sql file.')
    return false
  end
  sql = sql.get
  model.setSqlFile(sql)

  # Method to translate from OpenStudio's time formatting
  # to Javascript time formatting
  # OpenStudio time
  # 2009-May-14 00:10:00   Raw string
  # Javascript time
  # 2009/07/12 12:34:56
  def to_JSTime(os_time)
    js_time = os_time.to_s
    # Replace the '-' with '/'
    js_time = js_time.tr('-', '/')
    # Replace month abbreviations with numbers
    js_time = js_time.gsub('Jan', '01')
    js_time = js_time.gsub('Feb', '02')
    js_time = js_time.gsub('Mar', '03')
    js_time = js_time.gsub('Apr', '04')
    js_time = js_time.gsub('May', '05')
    js_time = js_time.gsub('Jun', '06')
    js_time = js_time.gsub('Jul', '07')
    js_time = js_time.gsub('Aug', '08')
    js_time = js_time.gsub('Sep', '09')
    js_time = js_time.gsub('Oct', '10')
    js_time = js_time.gsub('Nov', '11')
    js_time = js_time.gsub('Dec', '12')

    return js_time
  end

  # Get the weather file (as opposed to design day) run period
  annEnvPd = nil
  sql.availableEnvPeriods.each do |envPd|
    envType = sql.environmentType(envPd)
    if !envType.empty?
      if envType.get == 'WeatherRunPeriod'.to_EnvironmentType
        annEnvPd = envPd
      end
    end
  end

  # Find the names of all plant loops in the model that contain both a
  # district heating and district cooling object
  loop_names = []
  model.getPlantLoops.each do |loop|
    runner.registerInfo("Checking '#{loop.name}' for district heating and district cooling.")
    dist_htg_name = nil
    dist_clg_name = nil
    loop.supplyComponents.each do |sc|
      if sc.to_DistrictHeating.is_initialized
        dist_htg_name = sc.name.get
      elsif sc.to_DistrictCooling.is_initialized
        dist_clg_name = sc.name.get
      end
    end

    if dist_htg_name && dist_clg_name
      loop_names << [loop.name.get, dist_htg_name, dist_clg_name]
    end
  end

  # Report any loops that were found that appear to be
  # GLHE loops
  if loop_names.empty?
    runner.registerInfo('No loops found with both district heating and district cooling.')
  else
    runner.registerInfo("Loops with district heating and district cooling: #{loop_names.join(',')}.")
  end

  # TODO: temp workaround to hardcode year
  iannEnvPd = sql.execAndReturnFirstInt("SELECT EnvironmentPeriodIndex FROM EnvironmentPeriods WHERE EnvironmentName = '#{annEnvPd}'").get
  startYear = sql.execAndReturnFirstInt("SELECT MIN(YEAR) FROM Time WHERE EnvironmentPeriodIndex=#{iannEnvPd}").get

  # Define the start and end day for each month
  months = {}
  months[1] = [OpenStudio::Date.new(OpenStudio::MonthOfYear.new('January'), 1, startYear),
               OpenStudio::Date.new(OpenStudio::MonthOfYear.new('January'), 31, startYear)]

  months[2] = [OpenStudio::Date.new(OpenStudio::MonthOfYear.new('February'), 1, startYear),
               OpenStudio::Date.new(OpenStudio::MonthOfYear.new('February'), 28, startYear)]

  months[3] = [OpenStudio::Date.new(OpenStudio::MonthOfYear.new('March'), 1, startYear),
               OpenStudio::Date.new(OpenStudio::MonthOfYear.new('March'), 31, startYear)]

  months[4] = [OpenStudio::Date.new(OpenStudio::MonthOfYear.new('April'), 1, startYear),
               OpenStudio::Date.new(OpenStudio::MonthOfYear.new('April'), 30, startYear)]

  months[5] = [OpenStudio::Date.new(OpenStudio::MonthOfYear.new('May'), 1, startYear),
               OpenStudio::Date.new(OpenStudio::MonthOfYear.new('May'), 31, startYear)]

  months[6] = [OpenStudio::Date.new(OpenStudio::MonthOfYear.new('June'), 1, startYear),
               OpenStudio::Date.new(OpenStudio::MonthOfYear.new('June'), 30, startYear)]

  months[7] = [OpenStudio::Date.new(OpenStudio::MonthOfYear.new('July'), 1, startYear),
               OpenStudio::Date.new(OpenStudio::MonthOfYear.new('July'), 31, startYear)]

  months[8] = [OpenStudio::Date.new(OpenStudio::MonthOfYear.new('August'), 1, startYear),
               OpenStudio::Date.new(OpenStudio::MonthOfYear.new('August'), 31, startYear)]

  months[9] = [OpenStudio::Date.new(OpenStudio::MonthOfYear.new('September'), 1, startYear),
               OpenStudio::Date.new(OpenStudio::MonthOfYear.new('September'), 30, startYear)]

  months[10] = [OpenStudio::Date.new(OpenStudio::MonthOfYear.new('October'), 1, startYear),
                OpenStudio::Date.new(OpenStudio::MonthOfYear.new('October'), 31, startYear)]

  months[11] = [OpenStudio::Date.new(OpenStudio::MonthOfYear.new('November'), 1, startYear),
                OpenStudio::Date.new(OpenStudio::MonthOfYear.new('November'), 30, startYear)]

  months[12] = [OpenStudio::Date.new(OpenStudio::MonthOfYear.new('December'), 1, startYear),
                OpenStudio::Date.new(OpenStudio::MonthOfYear.new('December'), 31, startYear)]

  # Define the start and end time for each day
  start_time = OpenStudio::Time.new(0, 0, 0, 0)
  end_time = OpenStudio::Time.new(0, 24, 0, 0)

  # Get the heating and cooling loads for each loop
  # in hourly resolution for reporting, monthly resolution for GLHEPro
  annualGraphData = []
  monthlyTableData = []
  loop_names.each do |loop_name, dist_htg_name, dist_clg_name|
    runner.registerInfo("Getting monthly load data for #{loop_name}.")

    # Get the hourly annual heating load in Watts
    ann_hourly_htg_w = sql.timeSeries(annEnvPd, 'Hourly', 'District Heating Water Rate', dist_htg_name.upcase)
    if ann_hourly_htg_w.empty?
      runner.registerWarning("No hourly heating data found for '#{dist_htg_name}' on '#{loop_name}'")
      next
    else
      ann_hourly_htg_w = ann_hourly_htg_w.get
    end

    # Get the hourly annual cooling load in Watts
    ann_hourly_clg_w = sql.timeSeries(annEnvPd, 'Hourly', 'District Cooling Water Rate', dist_clg_name.upcase)
    if ann_hourly_clg_w.empty?
      runner.registerWarning("No hourly cooling data found for '#{dist_clg_name}' on '#{loop_name}'")
      next
    else
      ann_hourly_clg_w = ann_hourly_clg_w.get
    end

    # Convert time stamp format to be more readable
    js_date_times = []
    ann_hourly_htg_w.dateTimes.each do |date_time|
      js_date_times << to_JSTime(date_time)
    end

    # Convert the hourly heating load from W to Btu/hr
    ann_hourly_htg_btu_per_hr_vals = []
    ann_hourly_htg_w_vals = ann_hourly_htg_w.values
    for i in 0..(ann_hourly_htg_w_vals.size - 1)
      htg_w = ann_hourly_htg_w_vals[i]
      htg_btu_per_hr = OpenStudio.convert(htg_w.to_f, 'W', 'kBtu/hr').get
      ann_hourly_htg_btu_per_hr_vals << htg_btu_per_hr
    end

    # Convert the hourly cooling load from W to Btu/hr
    ann_hourly_clg_btu_per_hr_vals = []
    ann_hourly_clg_w_vals = ann_hourly_clg_w.values
    for i in 0..(ann_hourly_clg_w_vals.size - 1)
      clg_w = ann_hourly_clg_w_vals[i]
      clg_btu_per_hr = OpenStudio.convert(clg_w.to_f, 'W', 'kBtu/hr').get
      ann_hourly_clg_btu_per_hr_vals << clg_btu_per_hr
    end

    # Create an array of arrays [timestamp, htg_btu_per_hr, clg_btu_per_hr]
    hourly_vals = js_date_times.zip(ann_hourly_htg_btu_per_hr_vals, ann_hourly_clg_btu_per_hr_vals)

    # Add the hourly load data to JSON for the report.html
    graph = {}
    graph['title'] = "#{loop_name} - Hourly Heating and Cooling Power"
    graph['xaxislabel'] = 'Time'
    graph['yaxislabel'] = 'Power (kBtu/hr)'
    graph['labels'] = ['Date', 'Heating', 'Cooling']
    graph['colors'] = ['#FF5050', '#0066FF']
    graph['timeseries'] = hourly_vals

    # This measure requires ruby 2.0.0 to create the JSON for the report graph
    if RUBY_VERSION >= '2.0.0'
      annualGraphData << graph
    end

    # Save out hourly load data to CSV
    File.open("./Annual Hourly Loads for #{loop_name}.csv", 'w') do |file|
      file.puts "Annual Hourly Loads for #{loop_name}"
      file.puts 'Date/Time,Heating (kBtu/hr),Cooling (kBtu/hr)'
      hourly_vals.each do |timestamp, htg_btu_per_hr, clg_btu_per_hr|
        file.puts "#{timestamp},#{htg_btu_per_hr},#{clg_btu_per_hr}"
      end
    end

    # Find monthly loads for GLHEPro
    mon_htg_cons = []
    mon_clg_cons = []
    mon_htg_dmd = []
    mon_clg_dmd = []

    # Loop through months and find total heating and cooling energy
    # and peak heating and cooling rate for each month
    # and store in arrays defined above
    (1..12).each do |i|
      # Create the start and end date/time for the month
      start_date = months[i][0]
      end_date = months[i][1]
      start_t = OpenStudio::DateTime.new(start_date, start_time)
      end_t = OpenStudio::DateTime.new(end_date, end_time)
      runner.registerInfo("Month #{i}: #{start_t} to #{end_t}.")

      # Determine the monthly heating information
      mon_hourly_htg_w = ann_hourly_htg_w.values(start_t, end_t)
      # if mon_hourly_htg_w.empty?
      #  runner.registerWarning("No heating data for #{start_t} to #{end_t}, check the run period of your simulation.")
      #  next
      # end
      mon_hourly_htg_w_arr = []
      for i in 0..(mon_hourly_htg_w.size - 1)
        mon_hourly_htg_w_arr << mon_hourly_htg_w[i].to_f
      end
      mon_htg_cons_w_hr = mon_hourly_htg_w_arr.sum
      mon_htg_cons_kBtu = OpenStudio.convert(mon_htg_cons_w_hr.to_f, 'W*hr', 'kBtu').get
      mon_htg_peak_dmd_w = mon_hourly_htg_w_arr.max
      mon_htg_peak_dmd_Btu_hr = OpenStudio.convert(mon_htg_peak_dmd_w.to_f, 'W', 'Btu/hr').get

      # Determine the monthly cooling information
      mon_hourly_clg_w = ann_hourly_clg_w.values(start_t, end_t)
      # if mon_hourly_clg_w.empty?
      #  runner.registerWarning("No cooling data for #{start_t} to #{end_t}, check the run period of your simulation.")
      #  next
      # end
      mon_hourly_clg_w_arr = []
      for i in 0..(mon_hourly_clg_w.size - 1)
        mon_hourly_clg_w_arr << mon_hourly_clg_w[i].to_f
      end
      mon_clg_cons_w_hr = mon_hourly_clg_w_arr.sum
      mon_clg_cons_kBtu = OpenStudio.convert(mon_clg_cons_w_hr.to_f, 'W*hr', 'kBtu').get
      mon_clg_peak_dmd_w = mon_hourly_clg_w_arr.max
      mon_clg_peak_dmd_Btu_hr = OpenStudio.convert(mon_clg_peak_dmd_w.to_f, 'W', 'Btu/hr').get

      # Report out the monthly values and add to the array
      runner.registerInfo("htg: #{mon_htg_cons_kBtu} kBtu, clg: #{mon_clg_cons_kBtu} kBtu, htg peak: #{mon_htg_peak_dmd_Btu_hr} Btu/hr, clg peak: #{mon_clg_peak_dmd_Btu_hr} Btu/hr.")
      mon_htg_cons << OpenStudio.toNeatString(mon_htg_cons_kBtu, 0, false).to_i
      mon_clg_cons << OpenStudio.toNeatString(mon_clg_cons_kBtu, 0, false).to_i
      mon_htg_dmd << OpenStudio.toNeatString(mon_htg_peak_dmd_Btu_hr, 0, false).to_i
      mon_clg_dmd << OpenStudio.toNeatString(mon_clg_peak_dmd_Btu_hr, 0, false).to_i
    end

    # Log the annual numbers
    ann_htg_cons = mon_htg_cons.sum
    ann_htg_cons = OpenStudio.toNeatString(ann_htg_cons, 0, false).to_i

    ann_clg_cons = mon_clg_cons.sum
    ann_clg_cons = OpenStudio.toNeatString(ann_clg_cons, 0, false).to_i

    ann_htg_dmd = mon_htg_dmd.max
    ann_htg_dmd = OpenStudio.toNeatString(ann_htg_dmd, 0, false).to_i

    ann_clg_dmd = mon_clg_dmd.max
    ann_clg_dmd = OpenStudio.toNeatString(ann_clg_dmd, 0, false).to_i

    runner.registerInfo('Annual energy and peak demand.')
    runner.registerInfo("htg: #{ann_clg_cons} kBtu, clg: #{ann_clg_cons} kBtu, htg peak: #{ann_htg_dmd} Btu/hr, clg peak: #{ann_clg_dmd} Btu/hr.")

    # Save the monthly load data for import into GLHEPro (.gt1)
    File.open("./Monthly Loads for #{loop_name}.gt1", 'w') do |file|
      file.puts 'Clg/Htg Consumption (kBtu),'\
                "#{mon_clg_cons.join(',')},"\
                "#{ann_clg_cons},"\
                "#{mon_htg_cons.join(',')},"\
                "#{ann_htg_cons}"
      file.puts 'Clg/Htg Demand (Btuh),'\
                "#{mon_clg_dmd.join(',')},"\
                "#{ann_clg_dmd},"\
                "#{mon_htg_dmd.join(',')},"\
                "#{ann_htg_dmd}"
    end

    monthlyTableData = []
  end

  # Convert the graph data to JSON
  # This measure requires ruby 2.0.0 to create the JSON for the report graph
  if RUBY_VERSION >= '2.0.0'
    require 'json'
    annualGraphData = annualGraphData.to_json
  else
    runner.registerInfo("This Measure needs Ruby 2.0.0 to generate timeseries graphs on the report.  This does not impact the GLHEPro export at all.  You have Ruby #{RUBY_VERSION}.  OpenStudio 1.4.2 and higher user Ruby 2.0.0.")
  end

  # Read in the HTML report template
  html_in_path = "#{File.dirname(__FILE__)}/resources/report.html.in"
  if File.exist?(html_in_path)
    html_in_path = html_in_path
  else
    html_in_path = "#{File.dirname(__FILE__)}/report.html.in"
  end
  html_in = ''
  File.open(html_in_path, 'r') do |file|
    html_in = file.read
  end

  # Configure HTML template with variable values
  renderer = ERB.new(html_in)
  html_out = renderer.result(binding)

  # Write out the HTML template
  html_out_path = './report.html'
  File.open(html_out_path, 'w') do |file|
    file << html_out
    # Make sure HTML file is written to the disk one way or the other
    begin
      file.fsync
    rescue StandardError
      file.flush
    end
  end

  # Close the sql file
  sql.close

  return true
end

#to_JSTime(os_time) ⇒ Object

Method to translate from OpenStudio’s time formatting to Javascript time formatting OpenStudio time 2009-May-14 00:10:00 Raw string Javascript time 2009/07/12 12:34:56



104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
# File 'lib/measures/GLHEProExportLoadsforGroundHeatExchangerSizing/measure.rb', line 104

def to_JSTime(os_time)
  js_time = os_time.to_s
  # Replace the '-' with '/'
  js_time = js_time.tr('-', '/')
  # Replace month abbreviations with numbers
  js_time = js_time.gsub('Jan', '01')
  js_time = js_time.gsub('Feb', '02')
  js_time = js_time.gsub('Mar', '03')
  js_time = js_time.gsub('Apr', '04')
  js_time = js_time.gsub('May', '05')
  js_time = js_time.gsub('Jun', '06')
  js_time = js_time.gsub('Jul', '07')
  js_time = js_time.gsub('Aug', '08')
  js_time = js_time.gsub('Sep', '09')
  js_time = js_time.gsub('Oct', '10')
  js_time = js_time.gsub('Nov', '11')
  js_time = js_time.gsub('Dec', '12')

  return js_time
end