openstudio-load-flexibility-measures 0.6.0 → 0.6.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -46,6 +46,107 @@ class AddPackagedIceStorageTest < MiniTest::Test
46
46
  # def teardown
47
47
  # end
48
48
 
49
+ # Runs an annual simulation of IDF
50
+ #
51
+ # code based on model_run_simulation_and_log_errors from OpenStudio Standards
52
+ # Maybe update method above to optionally take IDF instead of OSM or add another method
53
+ #
54
+ # @param IDF OpenStudio workspace IDF object
55
+ # @param run_dir [String] file path location for the annual run, defaults to 'Run' in the current directory
56
+ # @return [Bool] returns true if successful, false if not
57
+ def model_run_simulation_and_log_errors(workspace, run_dir = "#{Dir.pwd}/Run")
58
+ # Make the directory if it doesn't exist
59
+ unless Dir.exist?(run_dir)
60
+ FileUtils.mkdir_p(run_dir)
61
+ end
62
+
63
+ # Save workspace IDF
64
+ idf_name = 'in.idf'
65
+ OpenStudio.logFree(OpenStudio::Debug, 'openstudio.model.Model', "Starting simulation here: #{run_dir}.")
66
+ OpenStudio.logFree(OpenStudio::Info, 'openstudio.model.Model', "Running simulation #{run_dir}.")
67
+ idf_path = OpenStudio::Path.new("#{run_dir}/#{idf_name}")
68
+ workspace.save(idf_path, true)
69
+
70
+ # Set up the simulation
71
+ # Find the weather file
72
+ epw_path = OpenStudio::Path.new("#{File.dirname(__FILE__)}/USA_IL_Chicago-OHare.Intl.AP.725300_TMY3.epw")
73
+
74
+ sql_path = nil
75
+
76
+ OpenStudio.logFree(OpenStudio::Debug, 'openstudio.model.Model', 'Running with RunManager.')
77
+
78
+ # Find EnergyPlus
79
+ ep_path = OpenStudio.getEnergyPlusExecutable
80
+
81
+ puts "EnergyPlus is: #{ep_path}"
82
+ job = "energyplus -w #{epw_path.to_s} -d #{run_dir} #{idf_path.to_s}"
83
+ puts job
84
+ system(job)
85
+
86
+ sql_path = OpenStudio::Path.new("#{run_dir}/eplusout.sql")
87
+
88
+ OpenStudio.logFree(OpenStudio::Info, 'openstudio.model.Model', 'Finished run.')
89
+
90
+ # @todo Delete the eplustbl.htm and other files created by the run for cleanliness.
91
+
92
+ if OpenStudio.exists(sql_path)
93
+ sql = OpenStudio::SqlFile.new(sql_path)
94
+ # Check to make sure the sql file is readable,
95
+ # which won't be true if EnergyPlus crashed during simulation.
96
+ unless sql.connectionOpen
97
+ OpenStudio.logFree(OpenStudio::Error, 'openstudio.model.Model', "The run failed, cannot create model. Look at the eplusout.err file in #{File.dirname(sql_path.to_s)} to see the cause.")
98
+ return false
99
+ end
100
+ else
101
+ # If the sql file does not exist, it is likely that EnergyPlus crashed,
102
+ # in which case the useful errors are inside the eplusout.err file.
103
+ err_file_path_string = "#{run_dir}/run/eplusout.err"
104
+ err_file_path = OpenStudio::Path.new(err_file_path_string)
105
+ if OpenStudio.exists(err_file_path)
106
+ if __dir__[0] == ':' # Running from OpenStudio CLI
107
+ errs = EmbeddedScripting.getFileAsString(err_file_path_string)
108
+ else
109
+ errs = File.read(err_file_path_string)
110
+ end
111
+ OpenStudio.logFree(OpenStudio::Error, 'openstudio.model.Model', "The run did not finish because of the following errors: #{errs}")
112
+ return false
113
+ else
114
+ OpenStudio.logFree(OpenStudio::Error, 'openstudio.model.Model', "Results for the run couldn't be found here: #{sql_path}.")
115
+ return false
116
+ end
117
+ end
118
+
119
+ # todo - figure out how to get code below to work so method returns false when simulation fails
120
+
121
+ # # Report severe or fatal errors in the run
122
+ # error_query = "SELECT ErrorMessage
123
+ # FROM Errors
124
+ # WHERE ErrorType in(1,2)"
125
+ # errs = model.sqlFile.get.execAndReturnVectorOfString(error_query)
126
+ # if errs.is_initialized
127
+ # errs = errs.get
128
+ # end
129
+
130
+ # # Check that the run completed successfully
131
+ # end_file_stringpath = "#{run_dir}/run/eplusout.end"
132
+ # end_file_path = OpenStudio::Path.new(end_file_stringpath)
133
+ # if OpenStudio.exists(end_file_path)
134
+ # endstring = File.read(end_file_stringpath)
135
+ # end
136
+
137
+ # if !endstring.include?('EnergyPlus Completed Successfully')
138
+ # OpenStudio.logFree(OpenStudio::Error, 'openstudio.model.Model', "The run did not finish and had following errors: #{errs.join('\n')}")
139
+ # return false
140
+ # end
141
+
142
+ # # Log any severe errors that did not cause simulation to fail
143
+ # unless errs.empty?
144
+ # OpenStudio.logFree(OpenStudio::Warn, 'openstudio.model.Model', "The run completed but had the following severe errors: #{errs.join('\n')}")
145
+ # end
146
+
147
+ return true
148
+ end
149
+
49
150
  def test_good_argument_values
50
151
  # create an instance of the measure
51
152
  measure = AddPackagedIceStorage.new
@@ -82,14 +183,68 @@ class AddPackagedIceStorageTest < MiniTest::Test
82
183
 
83
184
  # run the measure
84
185
  measure.run(workspace, runner, argument_map)
186
+
187
+ # run the annual simulation
188
+ output_file_path = "#{File.dirname(__FILE__)}/output/test_good_argument_values/Run"
189
+ #sim_results = self.model_run_simulation_and_log_errors(workspace, output_file_path)
190
+ #assert(sim_results)
191
+ # todo - need to add code so test fails if simulation fails
192
+
85
193
  result = runner.result
86
194
  assert_equal('Success', result.value.valueName)
87
195
 
88
196
  show_output(result)
89
197
 
90
- # save the workspace to output directory
91
- output_file_path = OpenStudio::Path.new("#{File.dirname(__FILE__)}/output/test_output.idf")
92
- workspace.save(output_file_path, true)
198
+ true
199
+ end
200
+
201
+ def test_single_speed_dx
202
+ # create an instance of the measure
203
+ measure = AddPackagedIceStorage.new
204
+
205
+ # create runner with empty OSW
206
+ osw = OpenStudio::WorkflowJSON.new
207
+ runner = OpenStudio::Measure::OSRunner.new(osw)
208
+
209
+ # load the test model
210
+ translator = OpenStudio::OSVersion::VersionTranslator.new
211
+ path = OpenStudio::Path.new("#{File.dirname(__FILE__)}/single_speed_dx_350.osm")
212
+ model = translator.loadModel(path)
213
+ assert(!model.empty?)
214
+ model = model.get
215
+
216
+ # forward translate OSM file to IDF file
217
+ ft = OpenStudio::EnergyPlus::ForwardTranslator.new
218
+ workspace = ft.translateModel(model)
219
+
220
+ # get arguments
221
+ arguments = measure.arguments(workspace)
222
+ argument_map = OpenStudio::Measure.convertOSArgumentVectorToMap(arguments)
223
+
224
+ # create hash of argument values
225
+ args_hash = {}
226
+ #args_hash['ice_cap'] = '40,50'
227
+
228
+ # populate argument with specified has value if set
229
+ arguments.each do |arg|
230
+ temp_arg_var = arg.clone
231
+ assert(temp_arg_var.setValue(args_hash[arg.name])) if args_hash[arg.name]
232
+ argument_map[arg.name] = temp_arg_var
233
+ end
234
+
235
+ # run the measure
236
+ measure.run(workspace, runner, argument_map)
237
+
238
+ # run the annual simulation
239
+ output_file_path = "#{File.dirname(__FILE__)}/output/single_speed_dx/Run"
240
+ #sim_results = self.model_run_simulation_and_log_errors(workspace, output_file_path)
241
+ #assert(sim_results)
242
+ # todo - need to add code so test fails if simulation fails
243
+
244
+ result = runner.result
245
+ assert_equal('Success', result.value.valueName)
246
+
247
+ show_output(result)
93
248
 
94
249
  true
95
250
  end