urbanopt-reopt 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (54) hide show
  1. checksums.yaml +7 -0
  2. data/.gitignore +27 -0
  3. data/.rakeTasks +7 -0
  4. data/.rdoc_options +37 -0
  5. data/.rspec +3 -0
  6. data/.rubocop.yml +9 -0
  7. data/.travis.yml +22 -0
  8. data/CHANGELOG.md +5 -0
  9. data/Gemfile +67 -0
  10. data/Jenkinsfile +10 -0
  11. data/LICENSE.md +27 -0
  12. data/RDOC_MAIN.md +176 -0
  13. data/README.md +177 -0
  14. data/Rakefile +30 -0
  15. data/deploy_docs.sh +5 -0
  16. data/developer_nrel_key.rb +31 -0
  17. data/doc_templates/LICENSE.md +27 -0
  18. data/doc_templates/README.md.erb +42 -0
  19. data/doc_templates/copyright_erb.txt +31 -0
  20. data/doc_templates/copyright_js.txt +4 -0
  21. data/doc_templates/copyright_ruby.txt +29 -0
  22. data/docs/.gitignore +3 -0
  23. data/docs/.vuepress/components/InnerJsonSchema.vue +78 -0
  24. data/docs/.vuepress/components/JsonSchema.vue +12 -0
  25. data/docs/.vuepress/components/ReoptInputSchema.vue +12 -0
  26. data/docs/.vuepress/components/ReoptOutputSchema.vue +12 -0
  27. data/docs/.vuepress/components/StaticLink.vue +8 -0
  28. data/docs/.vuepress/config.js +16 -0
  29. data/docs/.vuepress/highlight.js +8 -0
  30. data/docs/.vuepress/public/custom_rdoc_styles.css +58 -0
  31. data/docs/.vuepress/utils.js +17 -0
  32. data/docs/README.md +196 -0
  33. data/docs/package-lock.json +254 -0
  34. data/docs/package.json +22 -0
  35. data/docs/schemas/reopt-input-schema.md +57 -0
  36. data/docs/schemas/reopt-output-schema.md +66 -0
  37. data/index.html +1 -0
  38. data/index.md +176 -0
  39. data/lib/files/.gitkeep +0 -0
  40. data/lib/urbanopt/reopt/extension.rb +44 -0
  41. data/lib/urbanopt/reopt/feature_report_adapter.rb +364 -0
  42. data/lib/urbanopt/reopt/reopt_lite_api.rb +230 -0
  43. data/lib/urbanopt/reopt/reopt_logger.rb +42 -0
  44. data/lib/urbanopt/reopt/reopt_post_processor.rb +245 -0
  45. data/lib/urbanopt/reopt/reopt_schema/reopt_input_schema.json +1111 -0
  46. data/lib/urbanopt/reopt/reopt_schema/reopt_output_schema.json +538 -0
  47. data/lib/urbanopt/reopt/scenario/reopt_scenario_csv.rb +115 -0
  48. data/lib/urbanopt/reopt/scenario_report_adapter.rb +404 -0
  49. data/lib/urbanopt/reopt/version.rb +35 -0
  50. data/lib/urbanopt/reopt.rb +36 -0
  51. data/lib/urbanopt/reopt_scenario.rb +31 -0
  52. data/lib/urbanopt-reopt.rb +31 -0
  53. data/urbanopt-reopt.gemspec +33 -0
  54. metadata +194 -0
@@ -0,0 +1,245 @@
1
+ # *********************************************************************************
2
+ # URBANopt, Copyright (c) 2019-2020, Alliance for Sustainable Energy, LLC, and other
3
+ # contributors. All rights reserved.
4
+ #
5
+ # Redistribution and use in source and binary forms, with or without modification,
6
+ # are permitted provided that the following conditions are met:
7
+ #
8
+ # Redistributions of source code must retain the above copyright notice, this list
9
+ # of conditions and the following disclaimer.
10
+ #
11
+ # Redistributions in binary form must reproduce the above copyright notice, this
12
+ # list of conditions and the following disclaimer in the documentation and/or other
13
+ # materials provided with the distribution.
14
+ #
15
+ # Neither the name of the copyright holder nor the names of its contributors may be
16
+ # used to endorse or promote products derived from this software without specific
17
+ # prior written permission.
18
+ #
19
+ # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
20
+ # ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
21
+ # WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
22
+ # IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
23
+ # INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
24
+ # BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25
+ # DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
26
+ # LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
27
+ # OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
28
+ # OF THE POSSIBILITY OF SUCH DAMAGE.
29
+ # *********************************************************************************
30
+
31
+ require 'bundler/setup'
32
+ require 'urbanopt/scenario/default_reports'
33
+ require 'urbanopt/reopt/reopt_logger'
34
+ require 'urbanopt/reopt'
35
+ require 'csv'
36
+
37
+ module URBANopt # :nodoc:
38
+ module REopt # :nodoc:
39
+ class REoptPostProcessor
40
+ ##
41
+ # \REoptPostProcessor updates a ScenarioReport or FeatureReport based on \REopt Lite optimization response.
42
+ ##
43
+ #
44
+ # [*parameters:*]
45
+ #
46
+ # * +scenario_report+ - _ScenarioReport_ - Optional. A scenario report that has been returned from the URBANopt::Scenario::ScenarioDefaultPostProcessor - used in creating default output file names in \REopt Lite optimizations.
47
+ # * +scenario_reopt_assumptions_file+ - _String_ - Optional. JSON file formatted for a \REopt Lite analysis containing custom input parameters for optimizations at the Scenario Report level
48
+ # * +reopt_feature_assumptions+ - _Array_ - Optional. A list of JSON file formatted for a \REopt Lite analysis containing custom input parameters for optimizations at the Feature Report level. The order and number of files must match the Feature Reports in the scenario_report input.
49
+ # * +use_localhost+ - _Bool_ - If this is true, requests will be sent to a version of the \REopt Lite API running on localhost. Default is false, such that the production version of \REopt Lite is accessed.
50
+ # * +nrel_developer_key+ - _String_ - API used to access the \REopt Lite APi. Required only if +localhost+ is false. Obtain from https://developer.nrel.gov/signup/
51
+ ##
52
+ def initialize(scenario_report, scenario_reopt_assumptions_file = nil, reopt_feature_assumptions = [], nrel_developer_key = nil, localhost = false)
53
+ # initialize @@logger
54
+ @@logger ||= URBANopt::REopt.reopt_logger
55
+
56
+ if reopt_feature_assumptions.nil?
57
+ reopt_feature_assumptions = []
58
+ end
59
+ @nrel_developer_key = nrel_developer_key
60
+ @localhost = localhost
61
+ @reopt_base_post = { Scenario: { Site: { ElectricTariff: {}, LoadProfile: {}, Wind: { max_kw: 0 } } } }
62
+
63
+ @scenario_reopt_default_output_file = nil
64
+ @scenario_timeseries_default_output_file = nil
65
+ @scenario_reopt_default_assumptions_hash = nil
66
+ @feature_reports_reopt_default_assumption_hashes = []
67
+ @feature_reports_reopt_default_output_files = []
68
+ @feature_reports_timeseries_default_output_files = []
69
+
70
+ if !scenario_report.nil?
71
+ @scenario_report = scenario_report
72
+
73
+ @scenario_reopt_default_output_file = File.join(@scenario_report.directory_name, "scenario_report_#{@scenario_report.id}_reopt_run.json")
74
+ @scenario_timeseries_default_output_file = File.join(@scenario_report.directory_name, "scenario_report_#{@scenario_report.id}_timeseries.csv")
75
+
76
+ @scenario_report.feature_reports.each do |fr|
77
+ @feature_reports_reopt_default_output_files << File.join(fr.directory_name, "feature_report_#{fr.id}_reopt_run.json")
78
+ end
79
+
80
+ @scenario_report.feature_reports.each do |fr|
81
+ @feature_reports_timeseries_default_output_files << File.join(fr.directory_name, "feature_report_#{fr.id}_timeseries.csv")
82
+ end
83
+ end
84
+
85
+ if !scenario_reopt_assumptions_file.nil?
86
+ File.open(scenario_reopt_assumptions_file, 'r') do |file|
87
+ @scenario_reopt_default_assumptions_hash = JSON.parse(file.read, symbolize_names: true)
88
+ end
89
+ end
90
+
91
+ if !reopt_feature_assumptions.empty?
92
+ reopt_feature_assumptions.each do |file|
93
+ @feature_reports_reopt_default_assumption_hashes << JSON.parse(File.open(file, 'r').read, symbolize_names: true)
94
+ end
95
+ end
96
+ end
97
+
98
+ attr_accessor :scenario_reopt_default_assumptions_hash, :scenario_reopt_default_output_file, :scenario_timeseries_default_output_file
99
+ attr_accessor :feature_reports_reopt_default_assumption_hashes, :feature_reports_reopt_default_output_files, :feature_reports_timeseries_default_output_files
100
+
101
+ ##
102
+ # Updates a FeatureReport based on an optional set of \REopt Lite optimization assumptions.
103
+ ##
104
+ #
105
+ # [*parameters:*]
106
+ #
107
+ # * +feature_report+ - _URBANopt::Scenario::DefaultReports::FeatureReport_ - FeatureReport which will be used in creating and then updated by a \REopt Lite opimization response.
108
+ # * +reopt_assumptions_hash+ - _Hash_ - Optional. A \REopt Lite formatted hash containing default parameters (i.e. utility rate, escalation rate) which will be updated by the FeatureReport (i.e. location, roof availability)
109
+ # * +reopt_output_file+ - _String_ - Optional. Path to a file at which REpopt Lite responses will be saved.
110
+ # * +timeseries_csv_path+ - _String_ - Optional. Path to a file at which the new timeseries CSV for the FeatureReport will be saved.
111
+ #
112
+ # [*return:*] _URBANopt::Scenario::DefaultReports::FeatureReport_ - Returns an updated FeatureReport
113
+ ##
114
+ def run_feature_report(feature_report, reopt_assumptions_hash = nil, reopt_output_file = nil, timeseries_csv_path = nil)
115
+ api = URBANopt::REopt::REoptLiteAPI.new(@nrel_developer_key, @localhost)
116
+ adapter = URBANopt::REopt::FeatureReportAdapter.new
117
+
118
+ reopt_input = adapter.reopt_json_from_feature_report(feature_report, reopt_assumptions_hash)
119
+ if reopt_output_file.nil?
120
+ reopt_output_file = feature_report.directory_name
121
+ end
122
+ reopt_output = api.reopt_request(reopt_input, reopt_output_file)
123
+ return adapter.update_feature_report(feature_report, reopt_output, timeseries_csv_path)
124
+ end
125
+
126
+ ##
127
+ # Updates a ScenarioReport based on an optional set of \REopt Lite optimization assumptions.
128
+ ##
129
+ #
130
+ # [*parameters:*]
131
+ #
132
+ # * +feature_report+ - _URBANopt::Scenario::DefaultReports::ScenarioReport_ - ScenarioReport which will be used in creating and then updated by a \REopt Lite opimization response.
133
+ # * +reopt_assumptions_hash+ - _Hash_ - Optional. A \REopt Lite formatted hash containing default parameters (i.e. utility rate, escalation rate) which will be updated by the ScenarioReport (i.e. location, roof availability)
134
+ # * +reopt_output_file+ - _String_ - Optional. Path to a file at which REpopt Lite responses will be saved.
135
+ # * +timeseries_csv_path+ - _String_ - Optional. Path to a file at which the new timeseries CSV for the ScenarioReport will be saved.
136
+ #
137
+ # [*return:*] _URBANopt::Scenario::DefaultReports::ScenarioReport_ Returns an updated ScenarioReport
138
+ def run_scenario_report(scenario_report, reopt_assumptions_hash = nil, reopt_output_file = nil, timeseries_csv_path = nil)
139
+ if !reopt_assumptions_hash.nil?
140
+ @scenario_reopt_default_assumptions_hash = reopt_assumptions_hash
141
+ end
142
+ if !reopt_output_file.nil?
143
+ @scenario_reopt_default_output_file = reopt_output_file
144
+ end
145
+ if !timeseries_csv_path.nil?
146
+ @scenario_timeseries_default_output_file = timeseries_csv_path
147
+ end
148
+
149
+ api = URBANopt::REopt::REoptLiteAPI.new(@nrel_developer_key, @localhost)
150
+ adapter = URBANopt::REopt::ScenarioReportAdapter.new
151
+
152
+ reopt_input = adapter.reopt_json_from_scenario_report(scenario_report, @scenario_reopt_default_assumptions_hash)
153
+
154
+ reopt_output = api.reopt_request(reopt_input, @scenario_reopt_default_output_file)
155
+
156
+ return adapter.update_scenario_report(scenario_report, reopt_output, @scenario_timeseries_default_output_file)
157
+ end
158
+
159
+ # Updates a set of FeatureReports based on an optional set of \REopt Lite optimization assumptions.
160
+ ##
161
+ #
162
+ # [*parameters:*]
163
+ #
164
+ # * +feature_reports+ - _Array_ - An array of _URBANopt::Scenario::DefaultReports::FeatureReport_ objetcs which will each be used to create (and are subsquently updated by) a \REopt Lite opimization response.
165
+ # * +reopt_assumptions_hashes+ - _Array_ - Optional. An array of \REopt Lite formatted hashes containing default parameters (i.e. utility rate, escalation rate) which will be updated by the ScenarioReport (i.e. location, roof availability). The number and order of the hashes should match the feature_reports array.
166
+ # * +reopt_output_files+ - _Array_ - Optional. A array of paths to files at which REpopt Lite responses will be saved. The number and order of the paths should match the feature_reports array.
167
+ # * +timeseries_csv_path+ - _Array_ - Optional. A array of paths to files at which the new timeseries CSV for the FeatureReports will be saved. The number and order of the paths should match the feature_reports array.
168
+ #
169
+ # [*return:*] _Array_ Returns an array of updated _URBANopt::Scenario::DefaultReports::FeatureReport_ objects
170
+ def run_feature_reports(feature_reports, reopt_assumptions_hashes = [], reopt_output_files = [], timeseries_csv_paths = [])
171
+ if !reopt_assumptions_hashes.empty?
172
+ @feature_reports_reopt_default_assumption_hashes = reopt_assumptions_hashes
173
+ end
174
+
175
+ if !reopt_output_files.empty?
176
+ @feature_reports_reopt_default_output_files = reopt_output_files
177
+ end
178
+
179
+ if !timeseries_csv_paths.empty?
180
+ @feature_reports_timeseries_default_output_files = timeseries_csv_paths
181
+ end
182
+
183
+ if @feature_reports_reopt_default_output_files.empty?
184
+ feature_reports.each do |fr|
185
+ @feature_reports_reopt_default_output_files << File.join(fr.directory_name, "feature_report_#{fr.id}_reopt_run.json")
186
+ end
187
+ end
188
+
189
+ if @feature_reports_timeseries_default_output_files.empty?
190
+ feature_reports.each do |fr|
191
+ @feature_reports_timeseries_default_output_files << File.join(fr.directory_name, "feature_report_#{fr.id}_timeseries.csv")
192
+ end
193
+ end
194
+
195
+ api = URBANopt::REopt::REoptLiteAPI.new(@nrel_developer_key, @localhost)
196
+ feature_adapter = URBANopt::REopt::FeatureReportAdapter.new
197
+ new_feature_reports = []
198
+ feature_reports.each_with_index do |feature_report, idx|
199
+ begin
200
+ reopt_input = feature_adapter.reopt_json_from_feature_report(feature_report, @feature_reports_reopt_default_assumption_hashes[idx])
201
+ if reopt_output_files[idx].nil?
202
+ reopt_output_files[idx] = feature_report.directory_name
203
+ end
204
+ reopt_output = api.reopt_request(reopt_input, @feature_reports_reopt_default_output_files[idx])
205
+ new_feature_report = feature_adapter.update_feature_report(feature_report, reopt_output, @feature_reports_timeseries_default_output_files[idx])
206
+ new_feature_reports.push(new_feature_report)
207
+ rescue StandardError
208
+ @@logger.info("Could not optimize Feature Report #{feature_report.name} #{feature_report.id}")
209
+ end
210
+ end
211
+
212
+ return new_feature_reports
213
+ end
214
+
215
+ # Updates a ScenarioReport based on an optional set of \REopt Lite optimization assumptions.
216
+ ##
217
+ #
218
+ # [*parameters:*]
219
+ #
220
+ # * +scenario_report+ - _Array_ - A _URBANopt::Scenario::DefaultReports::ScenarioReport_ which will each be used to create (and is subsquently updated by) \REopt Lite opimization responses for each of its FeatureReports.
221
+ # * +reopt_assumptions_hashes+ - _Array_ - Optional. An array of \REopt Lite formatted hashes containing default parameters (i.e. utility rate, escalation rate) which will be updated by the ScenarioReport (i.e. location, roof availability). The number and order of the hashes should match the array in ScenarioReport.feature_reports.
222
+ # * +reopt_output_files+ - _Array_ - Optional. An array of paths to files at which REpopt Lite responses will be saved. The number and order of the paths should match the array in ScenarioReport.feature_reports.
223
+ # * +feature_report_timeseries_csv_paths+ - _Array_ - Optional. An array of paths to files at which the new timeseries CSV for the FeatureReports will be saved. The number and order of the paths should match the array in ScenarioReport.feature_reports.
224
+ #
225
+ # [*return:*] _URBANopt::Scenario::DefaultReports::ScenarioReport_ - Returns an updated ScenarioReport
226
+ def run_scenario_report_features(scenario_report, reopt_assumptions_hashes = [], reopt_output_files = [], feature_report_timeseries_csv_paths = [])
227
+ new_feature_reports = run_feature_reports(scenario_report.feature_reports, reopt_assumptions_hashes, reopt_output_files, feature_report_timeseries_csv_paths)
228
+
229
+ new_scenario_report = URBANopt::Scenario::DefaultReports::ScenarioReport.new
230
+ new_scenario_report.id = scenario_report.id
231
+ new_scenario_report.name = scenario_report.name
232
+ new_scenario_report.directory_name = scenario_report.directory_name
233
+
234
+ timeseries_hash = { column_names: scenario_report.timeseries_csv.column_names }
235
+ new_scenario_report.timeseries_csv = URBANopt::Scenario::DefaultReports::TimeseriesCSV.new(timeseries_hash)
236
+
237
+ new_feature_reports.each do |feature_report|
238
+ new_scenario_report.add_feature_report(feature_report)
239
+ end
240
+
241
+ return new_scenario_report
242
+ end
243
+ end
244
+ end
245
+ end