# ******************************************************************************* # OpenStudio(R), Copyright (c) Alliance for Sustainable Energy, LLC. # See also https://openstudio.net/license # ******************************************************************************* # insert your copyright here require 'openstudio' require 'openstudio/measure/ShowRunnerOutput' require 'minitest/autorun' require_relative '../measure.rb' require 'fileutils' class GEBMetricsReportTest < Minitest::Test def model_in_path_default # return "#{File.dirname(__FILE__)}/SmallOfficeDetailed_90.1-2010_5A.osm" # return "#{File.dirname(__FILE__)}/small_office_simple.osm" # return "#{File.dirname(__FILE__)}/small_office_simple_w_CO2.osm" return "#{File.dirname(__FILE__)}/small_office_simple_w_CO2_daylighting.osm" end def epw_path_default # make sure we have a weather data location epw = File.expand_path("#{File.dirname(__FILE__)}/CZ06RV2.epw") assert(File.exist?(epw.to_s)) return epw.to_s end def run_dir(test_name) # always generate test output in specially named 'output' directory so result files are not made part of the measure return "#{File.dirname(__FILE__)}/output/#{test_name}" end def model_out_path(test_name) return "#{run_dir(test_name)}/example_model.osm" end def sql_path(test_name) return "#{run_dir(test_name)}/run/eplusout.sql" end def workspace_path(test_name) return "#{run_dir(test_name)}/run/in.idf" end def report_path(test_name) return "#{run_dir(test_name)}/report.html" end # method for running the test simulation using OpenStudio 2.x API def setup_test_2(test_name, epw_path) osw_path = File.join(run_dir(test_name), 'in.osw') osw_path = File.absolute_path(osw_path) workflow = OpenStudio::WorkflowJSON.new workflow.setSeedFile(File.absolute_path(model_out_path(test_name))) workflow.setWeatherFile(File.absolute_path(epw_path)) workflow.saveAs(osw_path) cli_path = OpenStudio.getOpenStudioCLI cmd = "\"#{cli_path}\" run -w \"#{osw_path}\"" puts cmd system(cmd) end # create test files if they do not exist when the test first runs def setup_test(test_name, idf_output_requests, model_in_path = model_in_path_default, epw_path = epw_path_default) if !File.exist?(run_dir(test_name)) FileUtils.mkdir_p(run_dir(test_name)) end assert(File.exist?(run_dir(test_name))) if File.exist?(report_path(test_name)) FileUtils.rm(report_path(test_name)) end assert(File.exist?(model_in_path)) if File.exist?(model_out_path(test_name)) FileUtils.rm(model_out_path(test_name)) end # convert output requests to OSM for testing, OS App and PAT will add these to the E+ Idf workspace = OpenStudio::Workspace.new('Draft'.to_StrictnessLevel, 'EnergyPlus'.to_IddFileType) workspace.addObjects(idf_output_requests) rt = OpenStudio::EnergyPlus::ReverseTranslator.new request_model = rt.translateWorkspace(workspace) translator = OpenStudio::OSVersion::VersionTranslator.new model = translator.loadModel(model_in_path) assert(!model.empty?) model = model.get model.addObjects(request_model.objects) model.save(model_out_path(test_name), true) if ENV['OPENSTUDIO_TEST_NO_CACHE_SQLFILE'] if File.exist?(sql_path(test_name)) FileUtils.rm_f(sql_path(test_name)) end end # Run the OSW (only need to run for the first time or when new variable is requested) # setup_test_2(test_name, epw_path) end def load_osm # load the test model translator = OpenStudio::OSVersion::VersionTranslator.new model = translator.loadModel(model_in_path_default) assert(!model.empty?) model.get end def test_geb_metrics_report test_name = 'run_geb_measures' # create an instance of the measure measure = GEBMetricsReport.new # create runner with empty OSW osw = OpenStudio::WorkflowJSON.new runner = OpenStudio::Measure::OSRunner.new(osw) # get arguments model = load_osm arguments = measure.arguments argument_map = OpenStudio::Measure.convertOSArgumentVectorToMap(arguments) args_hash = {} args_hash['baseline_run_output_path'] = File.dirname(__FILE__ ) args_hash['event_date'] = '07-21' args_hash['shed_start'] = '13:00' args_hash['shed_end'] = '15:00' args_hash['take_start'] = '10:00' args_hash['take_end'] = '13:00' # populate argument with specified hash value if specified arguments.each do |arg| temp_arg_var = arg.clone if args_hash.key?(arg.name) assert(temp_arg_var.setValue(args_hash[arg.name])) end argument_map[arg.name] = temp_arg_var end # idf_output_requests = measure.energyPlusOutputRequests(runner, argument_map) idf_output_requests = [] # mimic the process of running this measure in OS App or PAT. Optionally set custom model_in_path and custom epw_path. epw_path = epw_path_default setup_test(test_name, idf_output_requests) assert(File.exist?(model_out_path(test_name))) assert(File.exist?(sql_path(test_name))) assert(File.exist?(epw_path)) puts '~~ ' * 50 puts model_out_path(test_name) puts sql_path(test_name) puts epw_path # set up runner, this will happen automatically when measure is run in PAT or OpenStudio runner.setLastOpenStudioModelPath(OpenStudio::Path.new(model_out_path(test_name))) runner.setLastEnergyPlusWorkspacePath(OpenStudio::Path.new(workspace_path(test_name))) runner.setLastEpwFilePath(epw_path) runner.setLastEnergyPlusSqlFilePath(OpenStudio::Path.new(sql_path(test_name))) # delete the output if it exists if File.exist?(report_path(test_name)) FileUtils.rm(report_path(test_name)) end assert(!File.exist?(report_path(test_name))) # temporarily change directory to the run directory and run the measure start_dir = Dir.pwd begin Dir.chdir(run_dir(test_name)) # run the measure measure.run(runner, argument_map) result = runner.result show_output(result) # puts '+ ' * 40 # puts result assert_equal('Success', result.value.valueName) puts '===> Warnings: ' result.warnings.each do |warning| puts warning.logMessage end assert(result.warnings.empty?) ensure Dir.chdir(start_dir) end # make sure the report file exists assert(File.exist?(report_path(test_name))) end end