Class: HVACPsychrometricChart

Inherits:
OpenStudio::Measure::ReportingMeasure
  • Object
show all
Defined in:
lib/measures/hvac_psychrometric_chart/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



29
30
31
32
33
34
35
36
37
38
39
# File 'lib/measures/hvac_psychrometric_chart/measure.rb', line 29

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

  # make an argument for air loop
  air_loop_name = OpenStudio::Measure::OSArgument.makeStringArgument('air_loop_name', true)
  air_loop_name.setDisplayName('Air Loop Name')
  air_loop_name.setDescription('The name of an Air Loop to create a psychrometric chart for.  Case sensitive.')
  args << air_loop_name

  return args
end

#descriptionObject

human readable description



19
20
21
# File 'lib/measures/hvac_psychrometric_chart/measure.rb', line 19

def description
  return 'A psychrometric chart shows the relationship between air temperature and humidity conditions.'
end

#energyPlusOutputRequests(runner, user_arguments) ⇒ Object

return a vector of IdfObject’s to request EnergyPlus objects needed by the run method



50
51
52
53
54
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
# File 'lib/measures/hvac_psychrometric_chart/measure.rb', line 50

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

  result = OpenStudio::IdfObjectVector.new

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

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

  # Get the named air loop
  air_loop = nil
  air_loop_name = runner.getStringArgumentValue('air_loop_name', user_arguments)
  air_loop = model.getAirLoopHVACByName(air_loop_name)
  if air_loop.is_initialized
    air_loop = air_loop.get
  else
    runner.registerError("No air loop called '#{air_loop_name}' was found in the model. It may have been removed by another measure, or you may have typed the name wrong.")
    return false
  end

  # Request the dry bulb temperature and humidity ratio for each node
  # on the supply side of the air loop.
  air_loop.supplyComponents.each do |sup_comp|
    next unless sup_comp.to_Node.is_initialized
    node = sup_comp.to_Node.get
    result << OpenStudio::IdfObject.load("Output:Variable,#{node.name.get},System Node Temperature,hourly;").get
    result << OpenStudio::IdfObject.load("Output:Variable,#{node.name.get},System Node Humidity Ratio,hourly;").get
  end

  # Request Outdoor air dry bulb and humidity ratio
  result << OpenStudio::IdfObject.load('Output:Variable,*,Site Outdoor Air Drybulb Temperature,Hourly;').get
  result << OpenStudio::IdfObject.load('Output:Variable,*,Site Outdoor Air Humidity Ratio,hourly;').get

  return result
end

#modeler_descriptionObject

human readable description of modeling approach



24
25
26
# File 'lib/measures/hvac_psychrometric_chart/measure.rb', line 24

def modeler_description
  return 'WARNING: the report takes a long time to render (can be several minutes!) in the OpenStudio App.  Open it in a web browser if this is too slow for you. Creates a psychrometric chart in SI units that shows the air conditions at each node on the supply side of the selected air loop.  These conditions are obtained by requesting hourly temperature and humidity for these specific nodes.  '
end

#nameObject

human readable name



14
15
16
# File 'lib/measures/hvac_psychrometric_chart/measure.rb', line 14

def name
  return 'HVAC Psychrometric Chart'
end

#node_names(air_loop) ⇒ Object

helper method



42
43
44
45
46
47
# File 'lib/measures/hvac_psychrometric_chart/measure.rb', line 42

def node_names(air_loop)
  res = {}
  res['supply_inlet']
  res['supply_outlet']
  res['mixed_air']
end

#run(runner, user_arguments) ⇒ Object

define what happens when the measure is run



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
# File 'lib/measures/hvac_psychrometric_chart/measure.rb', line 96

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 last 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)

  # Get the weather file run period (as opposed to design day run period)
  ann_env_pd = nil
  sql.availableEnvPeriods.each do |env_pd|
    env_type = sql.environmentType(env_pd)
    if env_type.is_initialized
      if env_type.get == OpenStudio::EnvironmentType.new('WeatherRunPeriod')
        ann_env_pd = env_pd
      end
    end
  end

  if ann_env_pd == false
    runner.registerError("Can't find a weather runperiod, make sure you ran an annual simulation, not just the design days.")
    return false
  end

  # Get the named air loop
  air_loop = nil
  air_loop_name = runner.getStringArgumentValue('air_loop_name', user_arguments)
  air_loop = model.getAirLoopHVACByName(air_loop_name)
  if air_loop.is_initialized
    air_loop = air_loop.get
  else
    runner.registerError("No air loop called '#{air_loop_name}' was found in the model. It may have been removed by another measure, or you may have typed the name wrong.")
    return false
  end

  # 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

  # Create a new series like this
  # for each condition series we want to plot
  # {"name" : "series 1",
  # "color" : "purple",
  # "data" :[{ "tdb": 20, "w": 0.015, "time": "2009/07/12 12:34:56"},
  # { "tdb": 25, "w": 0.008, "time": "2009/07/12 12:34:56"},
  # { "tdb": 30, "w": 0.005, "time": "2009/07/12 12:34:56"}]
  # }
  all_series = []

  # Outdoor Air

  # Get the hourly annual dry bulb temp
  tdb_timeseries = sql.timeSeries(ann_env_pd, 'Hourly', 'Site Outdoor Air Drybulb Temperature', 'Environment')

  # Get the hourly annual humidity ratio
  w_timeseries = sql.timeSeries(ann_env_pd, 'Hourly', 'Site Outdoor Air Humidity Ratio', 'Environment')

  # Store the data if it exists
  if tdb_timeseries.is_initialized && w_timeseries.is_initialized
    tdb_vals = tdb_timeseries.get.values
    w_vals = w_timeseries.get.values

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

    # Store the timeseries data to hash for later
    # export to the HTML file
    series = {}
    series['name'] = 'Outdoor Air'
    series['color'] = 'blue'
    data = []
    for i in 0..(js_date_times.size - 1)
      point = {}
      point['tdb'] = tdb_vals[i].round(2)
      point['w'] = w_vals[i].round(4)
      point['time'] = js_date_times[i]
      data << point
    end
    series['data'] = data
    all_series << series
  end

  # Air Loop Node conditions

  air_loop_name = air_loop.name.get
  j = 0
  colors = ['red', 'green', 'orange', 'purple', 'cyan', 'mangenta']

  runner.registerInfo("Getting psychrometric data for #{air_loop_name}.")

  # Get the dry bulb temperature and humidity ratio for each node
  # on the supply side of the air loop.
  air_loop.supplyComponents.each do |sup_comp|
    next unless sup_comp.to_Node.is_initialized
    node = sup_comp.to_Node.get
    node_name = node.name.get

    prev_comp_name = node_name
    if node == air_loop.supplyInletNode
      prev_comp_name = 'Return Air'
    elsif node == air_loop.supplyOutletNode
      prev_comp_name = 'Supply Air'
    elsif node.inletModelObject.is_initialized
      prev_comp = node.inletModelObject.get
      prev_comp_name = "#{prev_comp.name.get} Outlet"
      if air_loop.airLoopHVACOutdoorAirSystem.is_initialized
        if prev_comp == air_loop.airLoopHVACOutdoorAirSystem.get
          prev_comp_name = 'Mixed Air'
        end
      end
    end

    # Get the hourly annual dry bulb temp
    tdb_timeseries = sql.timeSeries(ann_env_pd, 'Hourly', 'System Node Temperature', node_name.upcase)
    if tdb_timeseries.empty?
      runner.registerWarning("No hourly annual dry bulb temp found for '#{node_name}' on '#{air_loop_name}'")
      next
    else
      tdb_timeseries = tdb_timeseries.get
    end
    tdb_vals = tdb_timeseries.values

    # Get the hourly annual humidity ratio
    w_timeseries = sql.timeSeries(ann_env_pd, 'Hourly', 'System Node Humidity Ratio', node_name.upcase)
    if w_timeseries.empty?
      runner.registerWarning("No hourly annual humidity ratio found for '#{node_name}' on '#{air_loop_name}'")
      next
    else
      w_timeseries = w_timeseries.get
    end
    w_vals = w_timeseries.values

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

    # Store the timeseries data to hash for later
    # export to the HTML file
    series = {}
    series['name'] = prev_comp_name.to_s
    series['color'] = colors[j]
    data = []
    for i in 0..(js_date_times.size - 1)
      point = {}
      point['tdb'] = tdb_vals[i].round(2)
      point['w'] = w_vals[i].round(4)
      point['time'] = js_date_times[i]
      data << point
    end
    series['data'] = data
    all_series << series

    # increment color selection
    j += 1
  end

  # Convert all_series to JSON.
  # This JSON will be substituted
  # into the HTML file.
  require 'json'
  all_series = all_series.to_json

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

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

  # write html file
  html_out_path = './report.html'
  File.open(html_out_path, 'w') do |file|
    file << html_out
    # make sure data 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



153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
# File 'lib/measures/hvac_psychrometric_chart/measure.rb', line 153

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