Class: URBANopt::Reporting::DefaultReports::ReportingPeriod

Inherits:
Object
  • Object
show all
Defined in:
lib/urbanopt/reporting/default_reports/reporting_period.rb

Overview

ReportingPeriod includes all the results of a specific reporting period.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(hash = {}) ⇒ ReportingPeriod

ReportingPeriod class initializes the reporting period attributes: :id , :name , :multiplier , :start_date , :end_date , :month , :day_of_month , :year , :total_site_energy , :total_source_energy , :net_site_energy , :net_source_energy , +:total_utility_cost , :net_utility_cost , :utility_costs , :electricity , :natural_gas , :additional_fuel , :district_cooling , :district_heating , :water , :electricity_produced , :end_uses , :energy_production , :photovoltaic , :fuel_type , :total_cost , :usage_cost , :demand_cost , :comfort_result , :time_setpoint_not_met_during_occupied_cooling , :time_setpoint_not_met_during_occupied_heating , :time_setpoint_not_met_during_occupied_hours

parameters:

hash - Hash - A hash which may contain a deserialized reporting_period.



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
# File 'lib/urbanopt/reporting/default_reports/reporting_period.rb', line 61

def initialize(hash = {})
  hash.delete_if { |k, v| v.nil? }
  hash = defaults.merge(hash)

  @id = hash[:id]
  @name = hash[:name]
  @multiplier = hash[:multiplier]
  @start_date = Date.new(hash[:start_date])
  @end_date = Date.new(hash[:end_date])

  @total_site_energy = hash[:total_site_energy]
  @total_source_energy = hash[:total_source_energy]
  @net_site_energy = hash [:net_site_energy]
  @net_source_energy = hash [:net_source_energy]
  @net_utility_cost = hash [:net_utility_cost]
  @total_utility_cost = hash [:total_utility_cost]
  @electricity = hash [:electricity]
  @natural_gas = hash [:natural_gas]
  @additional_fuel = hash [:additional_fuel]
  @district_cooling = hash [:district_cooling]
  @district_heating = hash[:district_heating]
  @water = hash[:water]
  @electricity_produced = hash[:electricity_produced]
  @end_uses = EndUses.new(hash[:end_uses])

  @energy_production = hash[:energy_production]

  @utility_costs = hash[:utility_costs]

  @comfort_result = hash[:comfort_result]

  # initialize class variables @@validator and @@schema
  @@validator ||= Validator.new
  @@schema ||= @@validator.schema
end

Instance Attribute Details

#additional_fuelObject

Returns the value of attribute additional_fuel.



46
47
48
# File 'lib/urbanopt/reporting/default_reports/reporting_period.rb', line 46

def additional_fuel
  @additional_fuel
end

#comfort_resultObject

Returns the value of attribute comfort_result.



46
47
48
# File 'lib/urbanopt/reporting/default_reports/reporting_period.rb', line 46

def comfort_result
  @comfort_result
end

#day_of_monthObject

Returns the value of attribute day_of_month.



46
47
48
# File 'lib/urbanopt/reporting/default_reports/reporting_period.rb', line 46

def day_of_month
  @day_of_month
end

#demand_costObject

Returns the value of attribute demand_cost.



46
47
48
# File 'lib/urbanopt/reporting/default_reports/reporting_period.rb', line 46

def demand_cost
  @demand_cost
end

#district_coolingObject

Returns the value of attribute district_cooling.



46
47
48
# File 'lib/urbanopt/reporting/default_reports/reporting_period.rb', line 46

def district_cooling
  @district_cooling
end

#district_heatingObject

Returns the value of attribute district_heating.



46
47
48
# File 'lib/urbanopt/reporting/default_reports/reporting_period.rb', line 46

def district_heating
  @district_heating
end

#electricityObject

Returns the value of attribute electricity.



46
47
48
# File 'lib/urbanopt/reporting/default_reports/reporting_period.rb', line 46

def electricity
  @electricity
end

#electricity_producedObject

Returns the value of attribute electricity_produced.



46
47
48
# File 'lib/urbanopt/reporting/default_reports/reporting_period.rb', line 46

def electricity_produced
  @electricity_produced
end

#end_dateObject

Returns the value of attribute end_date.



46
47
48
# File 'lib/urbanopt/reporting/default_reports/reporting_period.rb', line 46

def end_date
  @end_date
end

#end_usesObject

Returns the value of attribute end_uses.



46
47
48
# File 'lib/urbanopt/reporting/default_reports/reporting_period.rb', line 46

def end_uses
  @end_uses
end

#energy_productionObject

Returns the value of attribute energy_production.



46
47
48
# File 'lib/urbanopt/reporting/default_reports/reporting_period.rb', line 46

def energy_production
  @energy_production
end

#fuel_typeObject

Returns the value of attribute fuel_type.



46
47
48
# File 'lib/urbanopt/reporting/default_reports/reporting_period.rb', line 46

def fuel_type
  @fuel_type
end

#hours_out_of_comfort_bounds_PMVObject

Returns the value of attribute hours_out_of_comfort_bounds_PMV.



46
47
48
# File 'lib/urbanopt/reporting/default_reports/reporting_period.rb', line 46

def hours_out_of_comfort_bounds_PMV
  @hours_out_of_comfort_bounds_PMV
end

#hours_out_of_comfort_bounds_PPDObject

Returns the value of attribute hours_out_of_comfort_bounds_PPD.



46
47
48
# File 'lib/urbanopt/reporting/default_reports/reporting_period.rb', line 46

def hours_out_of_comfort_bounds_PPD
  @hours_out_of_comfort_bounds_PPD
end

#idObject

Returns the value of attribute id.



46
47
48
# File 'lib/urbanopt/reporting/default_reports/reporting_period.rb', line 46

def id
  @id
end

#monthObject

Returns the value of attribute month.



46
47
48
# File 'lib/urbanopt/reporting/default_reports/reporting_period.rb', line 46

def month
  @month
end

#multiplierObject

Returns the value of attribute multiplier.



46
47
48
# File 'lib/urbanopt/reporting/default_reports/reporting_period.rb', line 46

def multiplier
  @multiplier
end

#nameObject

Returns the value of attribute name.



46
47
48
# File 'lib/urbanopt/reporting/default_reports/reporting_period.rb', line 46

def name
  @name
end

#natural_gasObject

Returns the value of attribute natural_gas.



46
47
48
# File 'lib/urbanopt/reporting/default_reports/reporting_period.rb', line 46

def natural_gas
  @natural_gas
end

#net_site_energyObject

Returns the value of attribute net_site_energy.



46
47
48
# File 'lib/urbanopt/reporting/default_reports/reporting_period.rb', line 46

def net_site_energy
  @net_site_energy
end

#net_source_energyObject

Returns the value of attribute net_source_energy.



46
47
48
# File 'lib/urbanopt/reporting/default_reports/reporting_period.rb', line 46

def net_source_energy
  @net_source_energy
end

#net_utility_costObject

Returns the value of attribute net_utility_cost.



46
47
48
# File 'lib/urbanopt/reporting/default_reports/reporting_period.rb', line 46

def net_utility_cost
  @net_utility_cost
end

#photovoltaicObject

Returns the value of attribute photovoltaic.



46
47
48
# File 'lib/urbanopt/reporting/default_reports/reporting_period.rb', line 46

def photovoltaic
  @photovoltaic
end

#start_dateObject

Returns the value of attribute start_date.



46
47
48
# File 'lib/urbanopt/reporting/default_reports/reporting_period.rb', line 46

def start_date
  @start_date
end

#time_setpoint_not_met_during_occupied_coolingObject

Returns the value of attribute time_setpoint_not_met_during_occupied_cooling.



46
47
48
# File 'lib/urbanopt/reporting/default_reports/reporting_period.rb', line 46

def time_setpoint_not_met_during_occupied_cooling
  @time_setpoint_not_met_during_occupied_cooling
end

#time_setpoint_not_met_during_occupied_heatingObject

Returns the value of attribute time_setpoint_not_met_during_occupied_heating.



46
47
48
# File 'lib/urbanopt/reporting/default_reports/reporting_period.rb', line 46

def time_setpoint_not_met_during_occupied_heating
  @time_setpoint_not_met_during_occupied_heating
end

#time_setpoint_not_met_during_occupied_hoursObject

Returns the value of attribute time_setpoint_not_met_during_occupied_hours.



46
47
48
# File 'lib/urbanopt/reporting/default_reports/reporting_period.rb', line 46

def time_setpoint_not_met_during_occupied_hours
  @time_setpoint_not_met_during_occupied_hours
end

#total_costObject

Returns the value of attribute total_cost.



46
47
48
# File 'lib/urbanopt/reporting/default_reports/reporting_period.rb', line 46

def total_cost
  @total_cost
end

#total_site_energyObject

Returns the value of attribute total_site_energy.



46
47
48
# File 'lib/urbanopt/reporting/default_reports/reporting_period.rb', line 46

def total_site_energy
  @total_site_energy
end

#total_source_energyObject

Returns the value of attribute total_source_energy.



46
47
48
# File 'lib/urbanopt/reporting/default_reports/reporting_period.rb', line 46

def total_source_energy
  @total_source_energy
end

#total_utility_costObject

Returns the value of attribute total_utility_cost.



46
47
48
# File 'lib/urbanopt/reporting/default_reports/reporting_period.rb', line 46

def total_utility_cost
  @total_utility_cost
end

#usage_costObject

Returns the value of attribute usage_cost.



46
47
48
# File 'lib/urbanopt/reporting/default_reports/reporting_period.rb', line 46

def usage_cost
  @usage_cost
end

#utility_costsObject

Returns the value of attribute utility_costs.



46
47
48
# File 'lib/urbanopt/reporting/default_reports/reporting_period.rb', line 46

def utility_costs
  @utility_costs
end

#waterObject

Returns the value of attribute water.



46
47
48
# File 'lib/urbanopt/reporting/default_reports/reporting_period.rb', line 46

def water
  @water
end

#yearObject

Returns the value of attribute year.



46
47
48
# File 'lib/urbanopt/reporting/default_reports/reporting_period.rb', line 46

def year
  @year
end

Class Method Details

.add_values(existing_value, new_value) ⇒ Object

Adds up existing_value and new_values if not nill.

parameter:

existing_value - Float - A value corresponding to a ReportingPeriod attribute.

new_value - Float - A value corresponding to a ReportingPeriod attribute.



195
196
197
198
199
200
201
202
# File 'lib/urbanopt/reporting/default_reports/reporting_period.rb', line 195

def self.add_values(existing_value, new_value)
  if existing_value && new_value
    existing_value += new_value
  elsif new_value
    existing_value = new_value
  end
  return existing_value
end

.merge_reporting_period(existing_period, new_period) ⇒ Object

Merges an existing_period with a new_period if not nil.

Parameters:

existing_period - ReportingPeriod - An object of ReportingPeriod class.

new_period - ReportingPeriod - An object of ReportingPeriod class.



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
# File 'lib/urbanopt/reporting/default_reports/reporting_period.rb', line 212

def self.merge_reporting_period(existing_period, new_period)
  # modify the existing_period by summing up the results
  existing_period.total_site_energy = add_values(existing_period.total_site_energy, new_period.total_site_energy)
  existing_period.total_source_energy = add_values(existing_period.total_source_energy, new_period.total_source_energy)
  existing_period.net_source_energy = add_values(existing_period.net_source_energy, new_period.net_source_energy)
  existing_period.net_utility_cost = add_values(existing_period.net_utility_cost, new_period.net_utility_cost)
  existing_period.total_utility_cost = add_values(existing_period.total_utility_cost, new_period.total_utility_cost)
  existing_period.electricity = add_values(existing_period.electricity, new_period.electricity)
  existing_period.natural_gas = add_values(existing_period.natural_gas, new_period.natural_gas)
  existing_period.additional_fuel = add_values(existing_period.additional_fuel, new_period.additional_fuel)
  existing_period.district_cooling = add_values(existing_period.district_cooling, new_period.district_cooling)
  existing_period.district_heating = add_values(existing_period.district_heating, new_period.district_heating)
  existing_period.water = add_values(existing_period.water, new_period.water)
  existing_period.electricity_produced = add_values(existing_period.electricity_produced, new_period.electricity_produced)

  # merge end uses
  new_end_uses = new_period.end_uses
  existing_period.end_uses&.merge_end_uses!(new_end_uses)

  if existing_period.energy_production
    if existing_period.energy_production[:electricity_produced]
      existing_period.energy_production[:electricity_produced][:photovoltaic] = add_values(existing_period.energy_production[:electricity_produced][:photovoltaic], new_period.energy_production[:electricity_produced][:photovoltaic])
    end
  end

  existing_period.utility_costs&.each_with_index do |item, i|
    existing_period.utility_costs[i][:fuel_type] = existing_period.utility_costs[i][:fuel_type]
    existing_period.utility_costs[i][:total_cost] = add_values(existing_period.utility_costs[i][:total_cost], new_period.utility_costs[i][:total_cost])
    existing_period.utility_costs[i][:usage_cost] = add_values(existing_period.utility_costs[i][:usage_cost], new_period.utility_costs[i][:usage_cost])
    existing_period.utility_costs[i][:demand_cost] = add_values(existing_period.utility_costs[i][:demand_cost], new_period.utility_costs[i][:demand_cost])
  end

  if existing_period.comfort_result
    existing_period.comfort_result[:time_setpoint_not_met_during_occupied_cooling] = add_values(existing_period.comfort_result[:time_setpoint_not_met_during_occupied_cooling], new_period.comfort_result[:time_setpoint_not_met_during_occupied_cooling])
    existing_period.comfort_result[:time_setpoint_not_met_during_occupied_heating] = add_values(existing_period.comfort_result[:time_setpoint_not_met_during_occupied_heating], new_period.comfort_result[:time_setpoint_not_met_during_occupied_heating])
    existing_period.comfort_result[:time_setpoint_not_met_during_occupied_hours] = add_values(existing_period.comfort_result[:time_setpoint_not_met_during_occupied_hours], new_period.comfort_result[:time_setpoint_not_met_during_occupied_hours])
    existing_period.comfort_result[:hours_out_of_comfort_bounds_PMV] = add_values(existing_period.comfort_result[:hours_out_of_comfort_bounds_PMV], new_period.comfort_result[:hours_out_of_comfort_bounds_PMV])
    existing_period.comfort_result[:hours_out_of_comfort_bounds_PPD] = add_values(existing_period.comfort_result[:hours_out_of_comfort_bounds_PPD], new_period.comfort_result[:hours_out_of_comfort_bounds_PPD])
  end

  return existing_period
end

.merge_reporting_periods(existing_periods, new_periods) ⇒ Object

Merges multiple reporting periods together.

  • If existing_periods and new_periods ids are equal,

modify the existing_periods by merging the new periods results

  • If existing periods are empty, initialize with new_periods.

  • Raise an error if the existing periods are not identical with new periods (cannot have different reporting period ids).

parameters:

existing_periods - Array - An array of ReportingPeriod objects.

new_periods - Array - An array of ReportingPeriod objects.



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
# File 'lib/urbanopt/reporting/default_reports/reporting_period.rb', line 268

def self.merge_reporting_periods(existing_periods, new_periods)
  id_list_existing = []
  id_list_new = []
  id_list_existing = existing_periods.collect(&:id)
  id_list_new = new_periods.collect(&:id)

  if id_list_existing == id_list_new

    existing_periods.each_index do |index|
      # if +existing_periods+ and +new_periods+ ids are equal,
      # modify the existing_periods by merging the new periods results
      existing_periods[index] = merge_reporting_period(existing_periods[index], new_periods[index])
    end

  elsif existing_periods.empty?

    # if existing periods are empty, initialize with new_periods
    # the = operator would link existing_periods and new_periods to the same object in memory
    # we want to initialize with a deep clone of new_periods
    existing_periods = Marshal.load(Marshal.dump(new_periods))

  else
    # raise an error if the existing periods are not identical with new periods (cannot have different reporting period ids)
    raise 'cannot merge different reporting periods'

  end

  return existing_periods
end

Instance Method Details

#defaultsObject

Assigns default values if values do not exist.



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
# File 'lib/urbanopt/reporting/default_reports/reporting_period.rb', line 100

def defaults
  hash = {}

  hash[:id] = nil
  hash[:name] = nil
  hash[:multiplier] = nil
  hash[:start_date] = Date.new.to_hash
  hash[:end_date] = Date.new.to_hash

  hash[:total_site_energy] = nil
  hash[:total_source_energy] = nil
  hash [:net_site_energy] = nil
  hash [:net_source_energy] = nil
  hash [:net_utility_cost] = nil
  hash [:total_utility_cost] = nil
  hash [:electricity] = nil
  hash [:natural_gas] = nil
  hash [:additional_fuel] = nil
  hash [:district_cooling] = nil
  hash[:district_heating] = nil

  hash[:electricity_produced] = nil
  hash[:end_uses] = EndUses.new.to_hash
  hash[:energy_production] = { electricity_produced: { photovoltaic: nil } }
  hash[:utility_costs] = [{ fuel_type: nil, total_cost: nil, usage_cost: nil, demand_cost: nil }]
  hash[:comfort_result] = { time_setpoint_not_met_during_occupied_cooling: nil, time_setpoint_not_met_during_occupied_heating: nil,
                            time_setpoint_not_met_during_occupied_hours: nil, hours_out_of_comfort_bounds_PMV: nil, hours_out_of_comfort_bounds_PPD: nil }

  return hash
end

#to_hashObject

Converts to a Hash equivalent for JSON serialization.

  • Exclude attributes with nil values.

  • Validate reporting_period hash properties against schema.



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
# File 'lib/urbanopt/reporting/default_reports/reporting_period.rb', line 137

def to_hash
  result = {}

  result[:id] = @id if @id
  result[:name] = @name if @name
  result[:multiplier] = @multiplier if @multiplier
  result[:start_date] = @start_date.to_hash if @start_date
  result[:end_date] = @end_date.to_hash if @end_date
  result[:total_site_energy] = @total_site_energy if @total_site_energy
  result[:total_source_energy] = @total_source_energy if @total_source_energy
  result[:net_site_energy] = @net_site_energy if @net_site_energy
  result[:net_source_energy] = @net_source_energy if @net_source_energy
  result[:net_utility_cost] = @net_utility_cost if @net_utility_cost
  result[:total_utility_cost] = @total_utility_cost if @total_utility_cost
  result[:electricity] = @electricity if @electricity
  result[:natural_gas] = @natural_gas if @natural_gas
  result[:additional_fuel] = @additional_fuel if @additional_fuel
  result[:district_cooling] = @district_cooling if @district_cooling
  result[:district_heating] = @district_heating if @district_heating
  result[:water] = @water if @water
  result[:electricity_produced] = @electricity_produced if @electricity_produced
  result[:end_uses] = @end_uses.to_hash if @end_uses

  energy_production_hash = @energy_production if @energy_production
  energy_production_hash.delete_if { |k, v| v.nil? }
  energy_production_hash.each do |eph|
    eph.delete_if { |k, v| v.nil? }
  end

  result[:energy_production] = energy_production_hash if @energy_production

  if @utility_costs.any?
    result[:utility_costs] = @utility_costs
    @utility_costs.each do |uc|
      uc&.delete_if { |k, v| v.nil? }
    end
  end

  comfort_result_hash = @comfort_result if @comfort_result
  comfort_result_hash.delete_if { |k, v| v.nil? }
  result[:comfort_result] = comfort_result_hash if @comfort_result

  # validates +reporting_period+ properties against schema for reporting period.
  if @@validator.validate(@@schema[:definitions][:ReportingPeriod][:properties], result).any?
    raise "feature_report properties does not match schema: #{@@validator.validate(@@schema[:definitions][:ReportingPeriod][:properties], result)}"
  end

  return result
end