Class: AddCeilingFan

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

Overview

start the measure

Instance Method Summary collapse

Instance Method Details

#adjust_cool_sch(model, sch, cool_stp_inc, start_date, end_date, start_time, end_time) ⇒ Object



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

def adjust_cool_sch(model, sch, cool_stp_inc, start_date, end_date, start_time, end_time)
  new_sch = sch.get.clone(model)
  new_sch = new_sch.to_Schedule.get
  new_sch.setName("#{sch.get.name.to_s} increased by #{cool_stp_inc}C due to ceiling fan")
  year_start = OpenStudio::Date.new(OpenStudio::MonthOfYear.new(1), 1, model.assumedYear)
  year_end = OpenStudio::Date.new(OpenStudio::MonthOfYear.new(12), 31, model.assumedYear)

  # make cooling schedule adjustments and rename. Put in check to skip and warn if schedule not ruleset
  if !new_sch.to_ScheduleRuleset.empty?
    schedule = new_sch.to_ScheduleRuleset.get
    default_rule = schedule.defaultDaySchedule
    rules = schedule.scheduleRules
    days_covered = Array.new(7, false)

    # TODO: when ruleset has multiple rules for each month or couple of months instead of a full year, should first see if the period overlaps with summer/winter
    if rules.length > 0
      rules.each do |rule|
        unless start_date == year_start
          unchanged_rule1 = copy_sch_rule_for_period(model, rule, rule.daySchedule, year_start, start_date)
        end

        unless end_date == year_end
          unchanged_rule2 = copy_sch_rule_for_period(model, rule, rule.daySchedule, end_date, year_end)
        end

        change_rule = rule
        checkDaysCovered(change_rule, days_covered)
        change_rule.setStartDate(start_date)
        change_rule.setEndDate(end_date)

        change_day = change_rule.daySchedule
        day_time_vector = change_day.times
        day_value_vector = change_day.values
        change_day.clearValues

        change_day = updateDaySchedule(change_day, day_time_vector, day_value_vector, start_time, end_time, cool_stp_inc)
      end
    else
      runner.registerWarning("Cooling setpoint schedule '#{sch.get.name.to_s}' is a ScheduleRuleSet, but has no ScheduleRules associated. It won't be altered by this measure.")
    end

    # for days not covered (not defined specifically), modify default schedule
    if days_covered.include?(false)
      unless start_date == year_start
        unchanged_rule1 = create_sch_rule_from_default(model, schedule, default_rule, year_start, start_date)
      end

      unless end_date == year_end
        unchanged_rule2 = create_sch_rule_from_default(model, schedule, default_rule, end_date, year_end)
      end

      coverMissingDays(unchanged_rule1, days_covered)
      checkDaysCovered(unchanged_rule1, days_covered)

      change_rule = copy_sch_rule_for_period(model, unchanged_rule1, default_rule, start_date, end_date)

      change_day = change_rule.daySchedule
      day_time_vector = change_day.times
      day_value_vector = change_day.values
      change_day.clearValues

      change_day = updateDaySchedule(change_day, day_time_vector, day_value_vector, start_time, end_time, cool_stp_inc)
    end

    ######################################################################
  else
    runner.registerWarning("Schedule '#{sch.get.name.to_s}' isn't a ScheduleRuleset object and won't be altered by this measure.")
    new_sch.remove # remove un-used clone
  end

  return new_sch
end

#arguments(model) ⇒ Object

define the arguments that the user will input



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
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
98
99
100
101
102
103
# File 'lib/measures/add_ceiling_fan/measure.rb', line 28

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

  # building type: residential or commercial
  # this determines inputs like watts per floor area and diversity factor
  bldg_type = OpenStudio::Measure::OSArgument::makeChoiceArgument('bldg_type', ['residential', 'commercial'], true)
  bldg_type.setDisplayName('Select building type:')
  bldg_type.setDescription('Building type (residential or commercial)')
  bldg_type.setDefaultValue('commercial')
  args << bldg_type

  # cooling setpoint increase
  # default value comes from https://www.sciencedirect.com/science/article/abs/pii/S0360132321004133
  cool_stp_increase_C = OpenStudio::Measure::OSArgument.makeDoubleArgument('cool_stp_increase_C', true)
  cool_stp_increase_C.setDisplayName('Cooling setpoint increase - C')
  cool_stp_increase_C.setDescription('Cooling setpoint increase in degree C')
  cool_stp_increase_C.setDefaultValue(3)
  args << cool_stp_increase_C

  # ceiling fan motor type: AC or DC
  motor_type = OpenStudio::Measure::OSArgument::makeChoiceArgument('motor_type', ['DC', 'AC'], true)
  motor_type.setDisplayName('Select ceiling fan motor type:')
  motor_type.setDescription('Ceiling fan motor type')
  motor_type.setDefaultValue('DC')
  args << motor_type

  # ceiling fan EUI in watts per floor area (optional)
  # if not user assigned, use default values in run function below, which comes from discussion with Center for Built Environment based on their research
  watts_per_m2 = OpenStudio::Measure::OSArgument.makeDoubleArgument('watts_per_m2', false)
  watts_per_m2.setDisplayName('Ceiling fan EUI in watts per floor area')
  watts_per_m2.setDescription('Ceiling fan watts per m2')
  args << watts_per_m2

  # make an argument for the start date of the reduction
  start_date = OpenStudio::Ruleset::OSArgument.makeStringArgument('start_date', false)
  start_date.setDisplayName('First start date for the Reduction')
  start_date.setDescription('In MM-DD format')
  start_date.setDefaultValue('05-01')
  args << start_date

  # make an argument for the end date of the reduction
  end_date = OpenStudio::Ruleset::OSArgument.makeStringArgument('end_date', false)
  end_date.setDisplayName('First end date for the Reduction')
  end_date.setDescription('In MM-DD format')
  end_date.setDefaultValue('09-30')
  args << end_date

  # make an argument for the start time of the reduction
  start_time = OpenStudio::Measure::OSArgument.makeStringArgument('start_time', false)
  start_time.setDisplayName('Start Time for the Reduction')
  start_time.setDescription('In HH:MM:SS format')
  start_time.setDefaultValue('08:00:00')
  args << start_time

  # make an argument for the end time of the reduction
  end_time = OpenStudio::Measure::OSArgument.makeStringArgument('end_time', false)
  end_time.setDisplayName('End Time for the Reduction')
  end_time.setDescription('In HH:MM:SS format')
  end_time.setDefaultValue('18:00:00')
  args << end_time

  # diversity factor
  diversity_factor = OpenStudio::Measure::OSArgument.makeDoubleArgument('diversity_factor', false)
  diversity_factor.setDisplayName('Diversity factor')
  diversity_factor.setDescription('Diversity factor')
  args << diversity_factor

  # people air velocity
  people_air_velocity = OpenStudio::Measure::OSArgument.makeDoubleArgument('people_air_velocity', false)
  people_air_velocity.setDisplayName('People air velocity')
  people_air_velocity.setDescription('Air velocity surrounding people (m/s)')
  people_air_velocity.setDefaultValue(0.8)
  args << people_air_velocity

  return args
end

#checkDaysCovered(sch_rule, sch_day_covered) ⇒ Object



360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
# File 'lib/measures/add_ceiling_fan/measure.rb', line 360

def checkDaysCovered(sch_rule, sch_day_covered)
  if sch_rule.applySunday
    sch_day_covered[0] = true
  end
  if sch_rule.applyMonday
    sch_day_covered[1] = true
  end
  if sch_rule.applyTuesday
    sch_day_covered[2] = true
  end
  if sch_rule.applyWednesday
    sch_day_covered[3] = true
  end
  if sch_rule.applyThursday
    sch_day_covered[4] = true
  end
  if sch_rule.applyFriday
    sch_day_covered[5] = true
  end
  if sch_rule.applySaturday
    sch_day_covered[6] = true
  end
end

#copy_sch_rule_for_period(model, sch_rule, sch_day, start_date, end_date) ⇒ Object

copy ScheduleRule sch_rule, copy ScheduleDay sch_day and assign to new schedule rule



338
339
340
341
342
343
344
345
346
347
# File 'lib/measures/add_ceiling_fan/measure.rb', line 338

def copy_sch_rule_for_period(model, sch_rule, sch_day, start_date, end_date)
  new_rule = sch_rule.clone(model).to_ScheduleRule.get
  new_rule.setStartDate(start_date)
  new_rule.setEndDate(end_date)

  new_day_sch = sch_day.clone(model)
  new_day_sch.setParent(new_rule)

  return new_rule
end

#coverMissingDays(sch_rule, sch_day_covered) ⇒ Object



384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
# File 'lib/measures/add_ceiling_fan/measure.rb', line 384

def coverMissingDays(sch_rule, sch_day_covered)
  if sch_day_covered[0] == false
    sch_rule.setApplySunday(true)
  end
  if sch_day_covered[1] == false
    sch_rule.setApplyMonday(true)
  end
  if sch_day_covered[2] == false
    sch_rule.setApplyTuesday(true)
  end
  if sch_day_covered[3] == false
    sch_rule.setApplyWednesday(true)
  end
  if sch_day_covered[4] == false
    sch_rule.setApplyThursday(true)
  end
  if sch_day_covered[5] == false
    sch_rule.setApplyFriday(true)
  end
  if sch_day_covered[6] == false
    sch_rule.setApplySaturday(true)
  end

end

#create_cooling_and_fan_schedule(model, start_date, end_date, start_time, end_time, diversity_factor) ⇒ Object

create cooling setpoint schedule and fan operation schedule for ceiling or portable fans



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

def create_cooling_and_fan_schedule(model, start_date, end_date, start_time, end_time, diversity_factor)
  year_start = OpenStudio::Date.new(OpenStudio::MonthOfYear.new(1), 1, model.assumedYear)
  year_end = OpenStudio::Date.new(OpenStudio::MonthOfYear.new(12), 31, model.assumedYear)

  fan_sch_week = OpenStudio::Model::ScheduleWeek.new(model)
  fan_sch_week.setName("Ceiling fan operation sch week")

  ## Check for Schedule Type Limits and Create if Needed
  if !model.getModelObjectByName('OnOff').empty?
    sched_limits_onoff = model.getModelObjectByName('OnOff').get.to_ScheduleTypeLimits.get
  else
    sched_limits_onoff = OpenStudio::Model::ScheduleTypeLimits.new(model)
    sched_limits_onoff.setName('OnOff')
    sched_limits_onoff.setNumericType('Discrete')
    sched_limits_onoff.setUnitType('Availability')
    sched_limits_onoff.setLowerLimitValue(0.0)
    sched_limits_onoff.setUpperLimitValue(1.0)
  end

  fan_sch_day = OpenStudio::Model::ScheduleDay.new(model)
  fan_sch_day.setName("Ceiling fan operation sch default")
  fan_sch_day.setScheduleTypeLimits(sched_limits_onoff)
  fan_sch_day.addValue(start_time, 0)
  if end_time < OpenStudio::Time.new('24:00:00')
    fan_sch_day.addValue(end_time, 1.0 * diversity_factor)
    fan_sch_day.addValue(OpenStudio::Time.new('24:00:00'), 0)
  else
    fan_sch_day.addValue(OpenStudio::Time.new('24:00:00'), 1.0 * diversity_factor)
  end
  fan_sch_week.setAllSchedules(fan_sch_day)

  fan_off_day = OpenStudio::Model::ScheduleDay.new(model)
  fan_off_day.setName("Ceiling fan off sch day")
  fan_off_day.setScheduleTypeLimits(sched_limits_onoff)
  fan_off_day.addValue(OpenStudio::Time.new(0,24,0,0), 0)
  fan_off_week = OpenStudio::Model::ScheduleWeek.new(model)
  fan_off_week.setName("Ceiling fan off sch week")
  fan_off_week.setAllSchedules(fan_off_day)

  fan_sch = OpenStudio::Model::ScheduleYear.new(model)
  fan_sch.setName("Ceiling fan sch")
  fan_sch.setScheduleTypeLimits(sched_limits_onoff)

  # if ceiling fan only operates during a specific period
  unless start_date == year_start
    fan_sch.addScheduleWeek(start_date, fan_off_week)
  end

  if end_date == year_end
    fan_sch.addScheduleWeek(year_end, fan_sch_week)
  else
    fan_sch.addScheduleWeek(end_date, fan_sch_week)
    fan_sch.addScheduleWeek(year_end, fan_off_week)
  end

  puts "fan_sch: #{fan_sch}"

  return fan_sch
end

#create_sch_rule_from_default(model, sch_ruleset, default_sch_fule, start_date, end_date) ⇒ Object



349
350
351
352
353
354
355
356
357
358
# File 'lib/measures/add_ceiling_fan/measure.rb', line 349

def create_sch_rule_from_default(model, sch_ruleset, default_sch_fule, start_date, end_date)
  new_rule = OpenStudio::Model::ScheduleRule.new(sch_ruleset)
  new_rule.setStartDate(start_date)
  new_rule.setEndDate(end_date)

  new_day_sch = default_sch_fule.clone(model)
  new_day_sch.setParent(new_rule)

  return new_rule
end

#descriptionObject

human readable description



18
19
20
# File 'lib/measures/add_ceiling_fan/measure.rb', line 18

def description
  return 'Install ceiling fans in buildings to increase air circulation. Ceiling fans effectively cool by introducing slow movement to induce evaporative cooling, rather than directly conditioning the air. A diversity factor is added to consider the simultaneous usage among the household.'
end

#modeler_descriptionObject

human readable description of modeling approach



23
24
25
# File 'lib/measures/add_ceiling_fan/measure.rb', line 23

def modeler_description
  return 'Ceiling fan is modeled by increasing air velocity in the People objects and adding electric equipment to consider extra fan energy use. Cooling setpoint is increased by certain degrees in the presence of ceiling fans. A schedule is also introduced to simulate ceiling fan operation. A diversity factor (different in commercial and residential buildings) is added to consider the simultaneous usage among the building/household.'
end

#nameObject

human readable name



12
13
14
15
# File 'lib/measures/add_ceiling_fan/measure.rb', line 12

def name
  # Measure name should be the title case of the class name.
  return 'Add Ceiling Fan'
end

#run(model, runner, user_arguments) ⇒ Object

define what happens when the measure is run



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
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
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
# File 'lib/measures/add_ceiling_fan/measure.rb', line 106

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

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

  # assign the user inputs to variables
  bldg_type = runner.getStringArgumentValue('bldg_type', user_arguments)
  motor_type = runner.getStringArgumentValue('motor_type', user_arguments)
  cool_stp_increase_C = runner.getDoubleArgumentValue('cool_stp_increase_C', user_arguments)
  start_date = runner.getStringArgumentValue('start_date', user_arguments)
  end_date = runner.getStringArgumentValue('end_date', user_arguments)
  start_time = runner.getStringArgumentValue('start_time', user_arguments)
  end_time = runner.getStringArgumentValue('end_time', user_arguments)
  watts_per_m2 = runner.getOptionalDoubleArgumentValue('watts_per_m2', user_arguments)
  diversity_factor = runner.getOptionalDoubleArgumentValue('diversity_factor', user_arguments)
  people_air_velocity = runner.getOptionalDoubleArgumentValue('people_air_velocity', user_arguments)

  if bldg_type != 'commercial' && bldg_type != 'residential'
    runner.registerError("Wrong building type #{bldg_type} entered. Value must be either 'commercial' or 'residential'.")
    return false
  end

  if motor_type != 'AC' && motor_type != 'DC'
    runner.registerError("Wrong ceiling fan motor type #{motor_type} entered. Value must be either 'AC' or 'DC'.")
    return false
  end

  # validate the cooling setpoint increase
  if cool_stp_increase_C <= 0
    runner.registerError('The cooling setpoint increase should be positive.')
    return false
  elsif cool_stp_increase_C > 10
    runner.registerWarning('The cooling setpoint increase is abnormally large, normally it is between 2-5C.')
  end

  if watts_per_m2.empty?
    # use default value based on building type and motor type
    case bldg_type
    when 'commercial'
      if motor_type == 'DC'
        watts_per_m2 = 0.16146   #0.015W/ft2
      else # 'AC'
        watts_per_m2 = 0.48   # 0.015W/ft2 * 3 (double/triple of DC per CBE Paul Raftery)
      end
    when 'residential'
      if motor_type == 'DC'
        watts_per_m2 = 0.35   # 0.13W/ft2 * 25% (The average power when the fan was running was between 20-25% of max)
      else # 'AC'
        watts_per_m2 = 1.05   # 0.13W/ft2 * 3 (double/triple of DC per CBE Paul Raftery)
      end
    end
  elsif watts_per_m2.to_f <= 0
    runner.registerError("Invalid ceiling fan watts per m2 #{watts_per_m2} entered. Value must be >0.")
    return false
  else
    watts_per_m2 = watts_per_m2.to_f
  end

  # default value references: https://electricalnotes.wordpress.com/2018/04/01/thumb-rules-14-quick-reference-demand-diversity-factor/
  # https://www.electricallicenserenewal.com/Electrical-Continuing-Education-Courses/NEC-Content.php?sectionID=836.0
  if diversity_factor.empty?
    case bldg_type
    when 'commercial'
      diversity_factor = 0.9
    when 'residential'
      diversity_factor = 0.66
    end
  elsif diversity_factor.to_f <= 0 or diversity_factor.to_f > 1
    runner.registerError("Invalid diversity factor #{diversity_factor} entered. Value must be >0 and <=1.")
    return false
  else
    diversity_factor = diversity_factor.to_f
  end

  if people_air_velocity.empty?
    people_air_velocity = 0.8  # default 0.8m/s for ceiling fan operation
  elsif people_air_velocity.to_f <= 0
    runner.registerError("Invalid people air velocity #{people_air_velocity} entered. Value must be positive.")
    return false
  elsif people_air_velocity.to_f > 2
    runner.registerWarning('The people air velocity is abnormally high, normally it is between 0.2-2m/s.')
    people_air_velocity = people_air_velocity.to_f
  elsif people_air_velocity.to_f < 0.2
    runner.registerWarning('The people air velocity is abnormally low, normally it is between 0.2-2m/s.')
    people_air_velocity = people_air_velocity.to_f
  else
    people_air_velocity = people_air_velocity.to_f
  end

  # validate and assign operational period date and time
  if /(\d\d):(\d\d):(\d\d)/.match(start_time)
    os_start_time = OpenStudio::Time.new(start_time)
  else
    runner.registerError('Start time must be in HH-MM-SS format.')
    return false
  end

  if /(\d\d):(\d\d):(\d\d)/.match(end_time)
    os_end_time = OpenStudio::Time.new(end_time)
  else
    runner.registerError('End time must be in HH-MM-SS format.')
    return false
  end

  if start_time.to_f > end_time.to_f
    runner.registerError('The start time cannot be later than the end time.')
    return false
  end

  start_month = nil
  start_day = nil
  md = /(\d\d)-(\d\d)/.match(start_date)
  if md
    start_month = md[1].to_i
    start_day = md[2].to_i
  else
    runner.registerError('Start date must be in MM-DD format.')
    return false
  end
  end_month = nil
  end_day = nil
  md = /(\d\d)-(\d\d)/.match(end_date)
  if md
    end_month = md[1].to_i
    end_day = md[2].to_i
  else
    runner.registerError('End date must be in MM-DD format.')
    return false
  end

  # create cooling setpoint schedule and fan operation schedule for ceiling or portable fans
  def create_cooling_and_fan_schedule(model, start_date, end_date, start_time, end_time, diversity_factor)
    year_start = OpenStudio::Date.new(OpenStudio::MonthOfYear.new(1), 1, model.assumedYear)
    year_end = OpenStudio::Date.new(OpenStudio::MonthOfYear.new(12), 31, model.assumedYear)

    fan_sch_week = OpenStudio::Model::ScheduleWeek.new(model)
    fan_sch_week.setName("Ceiling fan operation sch week")

    ## Check for Schedule Type Limits and Create if Needed
    if !model.getModelObjectByName('OnOff').empty?
      sched_limits_onoff = model.getModelObjectByName('OnOff').get.to_ScheduleTypeLimits.get
    else
      sched_limits_onoff = OpenStudio::Model::ScheduleTypeLimits.new(model)
      sched_limits_onoff.setName('OnOff')
      sched_limits_onoff.setNumericType('Discrete')
      sched_limits_onoff.setUnitType('Availability')
      sched_limits_onoff.setLowerLimitValue(0.0)
      sched_limits_onoff.setUpperLimitValue(1.0)
    end

    fan_sch_day = OpenStudio::Model::ScheduleDay.new(model)
    fan_sch_day.setName("Ceiling fan operation sch default")
    fan_sch_day.setScheduleTypeLimits(sched_limits_onoff)
    fan_sch_day.addValue(start_time, 0)
    if end_time < OpenStudio::Time.new('24:00:00')
      fan_sch_day.addValue(end_time, 1.0 * diversity_factor)
      fan_sch_day.addValue(OpenStudio::Time.new('24:00:00'), 0)
    else
      fan_sch_day.addValue(OpenStudio::Time.new('24:00:00'), 1.0 * diversity_factor)
    end
    fan_sch_week.setAllSchedules(fan_sch_day)

    fan_off_day = OpenStudio::Model::ScheduleDay.new(model)
    fan_off_day.setName("Ceiling fan off sch day")
    fan_off_day.setScheduleTypeLimits(sched_limits_onoff)
    fan_off_day.addValue(OpenStudio::Time.new(0,24,0,0), 0)
    fan_off_week = OpenStudio::Model::ScheduleWeek.new(model)
    fan_off_week.setName("Ceiling fan off sch week")
    fan_off_week.setAllSchedules(fan_off_day)

    fan_sch = OpenStudio::Model::ScheduleYear.new(model)
    fan_sch.setName("Ceiling fan sch")
    fan_sch.setScheduleTypeLimits(sched_limits_onoff)

    # if ceiling fan only operates during a specific period
    unless start_date == year_start
      fan_sch.addScheduleWeek(start_date, fan_off_week)
    end

    if end_date == year_end
      fan_sch.addScheduleWeek(year_end, fan_sch_week)
    else
      fan_sch.addScheduleWeek(end_date, fan_sch_week)
      fan_sch.addScheduleWeek(year_end, fan_off_week)
    end

    puts "fan_sch: #{fan_sch}"

    return fan_sch
  end

  def updateDaySchedule(sch_day, vec_time, vec_value, time_begin, time_end, add_value)
    count = 0
    vec_time.each_with_index do |exist_timestamp, i|
      new_value = vec_value[i] + add_value
      if exist_timestamp > time_begin && exist_timestamp < time_end && count == 0
        sch_day.addValue(time_begin, vec_value[i])
        sch_day.addValue(exist_timestamp, new_value)
        count = 1
      elsif exist_timestamp == time_end && count == 0
        sch_day.addValue(time_begin, vec_value[i])
        sch_day.addValue(exist_timestamp, new_value)
        count = 2
      elsif exist_timestamp == time_begin && count == 0
        sch_day.addValue(exist_timestamp, vec_value[i])
        count = 1
      elsif exist_timestamp > time_end && count == 0
        sch_day.addValue(time_begin, vec_value[i])
        sch_day.addValue(time_end, new_value)
        sch_day.addValue(exist_timestamp,  vec_value[i])
        count = 2
      elsif exist_timestamp > time_begin && exist_timestamp < time_end && count==1
        sch_day.addValue(exist_timestamp, new_value)
      elsif exist_timestamp == time_end && count==1
        sch_day.addValue(exist_timestamp, new_value)
        count = 2
      elsif exist_timestamp > time_end && count == 1
        sch_day.addValue(time_end, new_value)
        sch_day.addValue(exist_timestamp, vec_value[i])
        count = 2
      else
        sch_day.addValue(exist_timestamp, vec_value[i])
      end
    end

    return sch_day
  end

  # copy ScheduleRule sch_rule, copy ScheduleDay sch_day and assign to new schedule rule
  def copy_sch_rule_for_period(model, sch_rule, sch_day, start_date, end_date)
    new_rule = sch_rule.clone(model).to_ScheduleRule.get
    new_rule.setStartDate(start_date)
    new_rule.setEndDate(end_date)

    new_day_sch = sch_day.clone(model)
    new_day_sch.setParent(new_rule)

    return new_rule
  end

  def create_sch_rule_from_default(model, sch_ruleset, default_sch_fule, start_date, end_date)
    new_rule = OpenStudio::Model::ScheduleRule.new(sch_ruleset)
    new_rule.setStartDate(start_date)
    new_rule.setEndDate(end_date)

    new_day_sch = default_sch_fule.clone(model)
    new_day_sch.setParent(new_rule)

    return new_rule
  end

  def checkDaysCovered(sch_rule, sch_day_covered)
    if sch_rule.applySunday
      sch_day_covered[0] = true
    end
    if sch_rule.applyMonday
      sch_day_covered[1] = true
    end
    if sch_rule.applyTuesday
      sch_day_covered[2] = true
    end
    if sch_rule.applyWednesday
      sch_day_covered[3] = true
    end
    if sch_rule.applyThursday
      sch_day_covered[4] = true
    end
    if sch_rule.applyFriday
      sch_day_covered[5] = true
    end
    if sch_rule.applySaturday
      sch_day_covered[6] = true
    end
  end

  def coverMissingDays(sch_rule, sch_day_covered)
    if sch_day_covered[0] == false
      sch_rule.setApplySunday(true)
    end
    if sch_day_covered[1] == false
      sch_rule.setApplyMonday(true)
    end
    if sch_day_covered[2] == false
      sch_rule.setApplyTuesday(true)
    end
    if sch_day_covered[3] == false
      sch_rule.setApplyWednesday(true)
    end
    if sch_day_covered[4] == false
      sch_rule.setApplyThursday(true)
    end
    if sch_day_covered[5] == false
      sch_rule.setApplyFriday(true)
    end
    if sch_day_covered[6] == false
      sch_rule.setApplySaturday(true)
    end

  end

  def adjust_cool_sch(model, sch, cool_stp_inc, start_date, end_date, start_time, end_time)
    new_sch = sch.get.clone(model)
    new_sch = new_sch.to_Schedule.get
    new_sch.setName("#{sch.get.name.to_s} increased by #{cool_stp_inc}C due to ceiling fan")
    year_start = OpenStudio::Date.new(OpenStudio::MonthOfYear.new(1), 1, model.assumedYear)
    year_end = OpenStudio::Date.new(OpenStudio::MonthOfYear.new(12), 31, model.assumedYear)

    # make cooling schedule adjustments and rename. Put in check to skip and warn if schedule not ruleset
    if !new_sch.to_ScheduleRuleset.empty?
      schedule = new_sch.to_ScheduleRuleset.get
      default_rule = schedule.defaultDaySchedule
      rules = schedule.scheduleRules
      days_covered = Array.new(7, false)

      # TODO: when ruleset has multiple rules for each month or couple of months instead of a full year, should first see if the period overlaps with summer/winter
      if rules.length > 0
        rules.each do |rule|
          unless start_date == year_start
            unchanged_rule1 = copy_sch_rule_for_period(model, rule, rule.daySchedule, year_start, start_date)
          end

          unless end_date == year_end
            unchanged_rule2 = copy_sch_rule_for_period(model, rule, rule.daySchedule, end_date, year_end)
          end

          change_rule = rule
          checkDaysCovered(change_rule, days_covered)
          change_rule.setStartDate(start_date)
          change_rule.setEndDate(end_date)

          change_day = change_rule.daySchedule
          day_time_vector = change_day.times
          day_value_vector = change_day.values
          change_day.clearValues

          change_day = updateDaySchedule(change_day, day_time_vector, day_value_vector, start_time, end_time, cool_stp_inc)
        end
      else
        runner.registerWarning("Cooling setpoint schedule '#{sch.get.name.to_s}' is a ScheduleRuleSet, but has no ScheduleRules associated. It won't be altered by this measure.")
      end

      # for days not covered (not defined specifically), modify default schedule
      if days_covered.include?(false)
        unless start_date == year_start
          unchanged_rule1 = create_sch_rule_from_default(model, schedule, default_rule, year_start, start_date)
        end

        unless end_date == year_end
          unchanged_rule2 = create_sch_rule_from_default(model, schedule, default_rule, end_date, year_end)
        end

        coverMissingDays(unchanged_rule1, days_covered)
        checkDaysCovered(unchanged_rule1, days_covered)

        change_rule = copy_sch_rule_for_period(model, unchanged_rule1, default_rule, start_date, end_date)

        change_day = change_rule.daySchedule
        day_time_vector = change_day.times
        day_value_vector = change_day.values
        change_day.clearValues

        change_day = updateDaySchedule(change_day, day_time_vector, day_value_vector, start_time, end_time, cool_stp_inc)
      end

      ######################################################################
    else
      runner.registerWarning("Schedule '#{sch.get.name.to_s}' isn't a ScheduleRuleset object and won't be altered by this measure.")
      new_sch.remove # remove un-used clone
    end

    return new_sch
  end

  # add model's assumed year to make sure addScheduleWeek to scheduleYear can go through
  os_start_date = OpenStudio::Date.new(OpenStudio::MonthOfYear.new(start_month), start_day, model.assumedYear)
  os_end_date = OpenStudio::Date.new(OpenStudio::MonthOfYear.new(end_month), end_day, model.assumedYear)

  # generate fan on and off week schedule
  ceiling_fan_sch = create_cooling_and_fan_schedule(model, os_start_date, os_end_date, os_start_time, os_end_time, diversity_factor)

  # add ceiling fan electric equipment object
  ceiling_fan_def = OpenStudio::Model::ElectricEquipmentDefinition.new(model)
  ceiling_fan_def.setName("Ceiling fan def")
  ceiling_fan_def.setWattsperSpaceFloorArea(watts_per_m2)
  ceiling_fan_def.setFractionLatent(0.0)
  ceiling_fan_def.setFractionRadiant(0.0)
  ceiling_fan_def.setFractionLost(0.0)  # all convective heat gain

  # create people air velocity schedule
  air_velo_sch = OpenStudio::Model::ScheduleRuleset.new(model)
  air_velo_sch.setName('Air Velocity Schedule')
  air_velo_sch.defaultDaySchedule.setName('Air Velocity Schedule Default')
  air_velo_sch.defaultDaySchedule.addValue(OpenStudio::Time.new(0, 24, 0, 0), people_air_velocity)

  ceiling_fan_count = 0 # init

  # add ceiling fan to occupied spaces
  # don't need to loop through space types as thermostat is attached to thermalZone only, which is connected to space
  model.getSpaces.each do |space|
    # only add to occupied zones
    unless space.numberOfPeople > 0
      runner.registerWarning("Skip adding ceiling fan to #{space.name.to_s} because it is not occupied.")
      next
    end

    # only add to space => thermal zone that has cooling setpoint assigned
    if space.thermalZone.empty?
      runner.registerWarning("Skip adding ceiling fan to #{space.name.to_s} because it is not associated with a thermal zone.")
      next
    end

    thermal_zone = space.thermalZone.get
    if thermal_zone.thermostatSetpointDualSetpoint.empty?
      runner.registerWarning("Skip adding ceiling fan to #{space.name.to_s} because it doesn't have thermostat assigned.")
      next
    end

    thermostat = thermal_zone.thermostatSetpointDualSetpoint.get
    # setup new cooling setpoint schedule
    clg_set_sch = thermostat.coolingSetpointTemperatureSchedule
    if clg_set_sch.empty?
      runner.registerWarning("Skip adding ceiling fan to #{space.name.to_s} because its thermostat doesn't have cooling setpoint schedule assigned.")
      next
    end

    runner.registerInfo("adjusting schedule #{clg_set_sch.get.name.to_s}")
    new_sch = adjust_cool_sch(model, clg_set_sch, cool_stp_increase_C, os_start_date, os_end_date, os_start_time, os_end_time)

    # hook up clone to thermostat
    thermostat.setCoolingSetpointTemperatureSchedule(new_sch)

    # add ceiling fan electric equipment
    ceiling_fan = OpenStudio::Model::ElectricEquipment.new(ceiling_fan_def)
    ceiling_fan.setName("#{space.name.to_s} ceiling fan")
    ceiling_fan.setSchedule(ceiling_fan_sch)
    ceiling_fan.setSpace(space)

    # assign people air velocity schedule
    space.people.each do |people_inst|
      people_inst.setAirVelocitySchedule(air_velo_sch)
    end

    ceiling_fan_count += 1
  end

  # echo the new space's name back to the user
  runner.registerInfo("#{ceiling_fan_count} ceiling fans were added to occupied conditioned spaces with #{watts_per_m2}w/m2.")

  # report final condition of model
  runner.registerFinalCondition("Ceiling fans were added to the building.")

  return true
end

#updateDaySchedule(sch_day, vec_time, vec_value, time_begin, time_end, add_value) ⇒ Object



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

def updateDaySchedule(sch_day, vec_time, vec_value, time_begin, time_end, add_value)
  count = 0
  vec_time.each_with_index do |exist_timestamp, i|
    new_value = vec_value[i] + add_value
    if exist_timestamp > time_begin && exist_timestamp < time_end && count == 0
      sch_day.addValue(time_begin, vec_value[i])
      sch_day.addValue(exist_timestamp, new_value)
      count = 1
    elsif exist_timestamp == time_end && count == 0
      sch_day.addValue(time_begin, vec_value[i])
      sch_day.addValue(exist_timestamp, new_value)
      count = 2
    elsif exist_timestamp == time_begin && count == 0
      sch_day.addValue(exist_timestamp, vec_value[i])
      count = 1
    elsif exist_timestamp > time_end && count == 0
      sch_day.addValue(time_begin, vec_value[i])
      sch_day.addValue(time_end, new_value)
      sch_day.addValue(exist_timestamp,  vec_value[i])
      count = 2
    elsif exist_timestamp > time_begin && exist_timestamp < time_end && count==1
      sch_day.addValue(exist_timestamp, new_value)
    elsif exist_timestamp == time_end && count==1
      sch_day.addValue(exist_timestamp, new_value)
      count = 2
    elsif exist_timestamp > time_end && count == 1
      sch_day.addValue(time_end, new_value)
      sch_day.addValue(exist_timestamp, vec_value[i])
      count = 2
    else
      sch_day.addValue(exist_timestamp, vec_value[i])
    end
  end

  return sch_day
end