Class: URBANopt::Scenario::OpenDSSPostProcessor

Inherits:
Object
  • Object
show all
Defined in:
lib/urbanopt/scenario/scenario_post_processor_opendss.rb

Instance Method Summary collapse

Constructor Details

#initialize(scenario_report, opendss_results_dir_name = 'opendss') ⇒ OpenDSSPostProcessor

OpenDSSPostProcessor post-processes OpenDSS results to selected OpenDSS results and integrate them in scenario and feature reports.

parameters:
  • scenario_report - ScenarioBase - An object of Scenario_report class.

  • opendss_results_dir_name - _directory name of opendss results



57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/urbanopt/scenario/scenario_post_processor_opendss.rb', line 57

def initialize(scenario_report, opendss_results_dir_name = 'opendss')
  if !scenario_report.nil?
    @scenario_report = scenario_report
    @opendss_results_dir = File.join(@scenario_report.directory_name, opendss_results_dir_name)
  else
    raise 'scenario_report is not valid'
  end

  # hash of column_name to array of values, does not get serialized to hash
  @mutex = Mutex.new

  # initialize opendss data
  @opendss_data = {}

  # initialize feature_reports data
  @feature_reports_data = {}

  # initialize logger
  @@logger ||= URBANopt::Reporting::DefaultReports.logger
end

Instance Method Details

#add_summary_results(feature_report) ⇒ Object

create opendss json report results

parameters:
  • feature_report - _feature report object_ - An onject of the feature report



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
# File 'lib/urbanopt/scenario/scenario_post_processor_opendss.rb', line 226

def add_summary_results(feature_report)
  under_voltage_hrs = 0
  over_voltage_hrs = 0

  id = feature_report.id
  @opendss_data[id].each_with_index do |row, i|
    if !row[1].include? 'voltage'

      if row[1].to_f > 1.05
        over_voltage_hrs += 1
      end

      if row[1].to_f < 0.95
        under_voltage_hrs += 1
      end

    end
  end

  # assign results to feature report
  feature_report.power_distribution.over_voltage_hours = over_voltage_hrs
  feature_report.power_distribution.under_voltage_hours = under_voltage_hrs

  return feature_report
end

#load_dataObject

load feature report data and opendss data



122
123
124
125
126
127
# File 'lib/urbanopt/scenario/scenario_post_processor_opendss.rb', line 122

def load_data
  # load selected opendss data
  load_opendss_data
  # load selected feature reports data
  load_feature_report_data
end

#load_feature_report_dataObject

load feature report data



112
113
114
115
116
117
118
119
# File 'lib/urbanopt/scenario/scenario_post_processor_opendss.rb', line 112

def load_feature_report_data
  @scenario_report.feature_reports.each do |feature_report|
    # read feature results
    feature_csv = CSV.read(File.join(feature_report.timeseries_csv.path))
    # add results to data
    @feature_reports_data[feature_report.id] = feature_csv
  end
end

#load_opendss_dataObject

load opendss data



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
# File 'lib/urbanopt/scenario/scenario_post_processor_opendss.rb', line 79

def load_opendss_data
  # load building features data
  @scenario_report.feature_reports.each do |feature_report|
    # read results from opendss
    opendss_csv = CSV.read(File.join(@opendss_results_dir, 'results', 'Features', feature_report.id + '.csv'))
    # add results to data
    @opendss_data[feature_report.id] = opendss_csv
  end

  ## load transformers data

  # transformers results directory path
  tf_results_path = File.join(@opendss_results_dir, 'results', 'Transformers')

  # get transformer ids
  transformer_ids = []
  Dir.entries(tf_results_path.to_s).select do |f|
    if !File.directory? f
      fn = File.basename(f, '.csv')
      transformer_ids << fn
    end
  end

  # add transformer results to @opendss_data
  transformer_ids.each do |id|
    # read results from transformers
    transformer_csv = CSV.read(File.join(tf_results_path, id + '.csv'))
    # add results to data
    @opendss_data[id] = transformer_csv
  end
end

#merge_data(feature_report_data, opendss_data) ⇒ Object

merge data



130
131
132
133
134
135
136
137
138
139
140
141
# File 'lib/urbanopt/scenario/scenario_post_processor_opendss.rb', line 130

def merge_data(feature_report_data, opendss_data)
  output = CSV.generate do |csv|
    opendss_data.each_with_index do |row, i|
      if row.include? 'Datetime'
        row.map { |header| header.prepend('opendss_') }
      end
      csv << (feature_report_data[i] + row[1..-1])
    end
  end

  return output
end

#runObject

run opendss post_processor



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
# File 'lib/urbanopt/scenario/scenario_post_processor_opendss.rb', line 255

def run
  @scenario_report.feature_reports.each do |feature_report|
    # load data
    load_data

    # puts " @opendss data = #{@opendss_data}"

    # get summary results
    add_summary_results(feature_report)

    # merge csv data
    id = feature_report.id
    updated_feature_csv = merge_data(@feature_reports_data[id], @opendss_data[id])

    # save fetaure reports
    feature_report.save_json_report('default_feature_report_opendss')

    # resave updated csv report
    save_csv(feature_report, updated_feature_csv, 'default_feature_report_opendss')
  end

  # add transformer reports
  save_transformers_reports

  # save the updated scenario reports
  # set save_feature_reports to false since only the scenario reports should be saved now
  @scenario_report.save(file_name = 'scenario_report_opendss', save_feature_reports = false)
end

#save_csv(feature_report, updated_feature_report_csv, file_name = 'default_feature_report') ⇒ Object

Save csv report method

parameters:
  • feature_report - _feature report object_ - An onject of the feature report

  • updated_feature_report_csv - CSV - An updated feature report csv

  • file_name - String - Assigned name to save the file with no extension



217
218
219
# File 'lib/urbanopt/scenario/scenario_post_processor_opendss.rb', line 217

def save_csv(feature_report, updated_feature_report_csv, file_name = 'default_feature_report')
  File.write(File.join(feature_report.directory_name, 'feature_reports', "#{file_name}.csv"), updated_feature_report_csv)
end

#save_transformers_reportsObject

add feature reports for transformers



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
# File 'lib/urbanopt/scenario/scenario_post_processor_opendss.rb', line 144

def save_transformers_reports
  @opendss_data.keys.each do |k|
    if k.include? 'Transformer'

      # create transformer directory
      transformer_dir = File.join(@scenario_report.directory_name, k)
      FileUtils.mkdir_p(File.join(transformer_dir, 'feature_reports'))

      # write data to csv
      # store under voltages and over voltages
      under_voltage_hrs = 0
      over_voltage_hrs = 0

      transformer_csv = CSV.generate do |csv|
        @opendss_data[k].each_with_index do |row, i|
          csv << row

          if !row[1].include? 'loading'
            if row[1].to_f > 1.05
              over_voltage_hrs += 1
            end

            if row[1].to_f < 0.95
              under_voltage_hrs += 1
            end
          end
        end
      end

      # save transformer CSV report
      File.write(File.join(transformer_dir, 'feature_reports', 'default_feature_report_opendss' + '.csv'), transformer_csv)

      # create transformer report
      transformer_report = URBANopt::Reporting::DefaultReports::FeatureReport.new(id: k, name: k, directory_name: transformer_dir, feature_type: 'Transformer',
                                                                                  timesteps_per_hour: @scenario_report.timesteps_per_hour,
                                                                                  simulation_status: 'complete')

      # assign results to transfomrer report
      transformer_report.power_distribution.over_voltage_hours = over_voltage_hrs
      transformer_report.power_distribution.under_voltage_hours = under_voltage_hrs

      ## save transformer JSON file
      # transformer_hash
      transformer_hash = transformer_report.to_hash
      # transformer_hash.delete_if { |k, v| v.nil? }

      json_name_path = File.join(transformer_dir, 'feature_reports', 'default_feature_report_opendss' + '.json')

      # save the json file
      File.open(json_name_path, 'w') do |f|
        f.puts JSON.pretty_generate(transformer_hash)
        # make sure data is written to the disk one way or the other
        begin
          f.fsync
        rescue StandardError
          f.flush
        end
      end

      # add transformers reports to scenario_report
      @scenario_report.feature_reports << transformer_report

    end
  end
end