From dcea6a44d8ab35d913a85a2ffae35662a71322d4 Mon Sep 17 00:00:00 2001 From: Kyle Benne Date: Tue, 31 Oct 2023 10:56:51 -0500 Subject: [PATCH 1/3] Improve weather file handling in OS Workflow Now we make sure to look in the Model for the final weather file defined after all Measures have run. We also still iniitialize the weather file from the OSW if one is defined. close #5009 --- src/workflow/OSWorkflow.cpp | 47 ++++++++++++++++++++++++++++++ src/workflow/OSWorkflow.hpp | 2 ++ src/workflow/RunInitialization.cpp | 36 +---------------------- src/workflow/RunTranslator.cpp | 2 ++ 4 files changed, 52 insertions(+), 35 deletions(-) diff --git a/src/workflow/OSWorkflow.cpp b/src/workflow/OSWorkflow.cpp index d98b7ec098e..6a7cc4b7b97 100644 --- a/src/workflow/OSWorkflow.cpp +++ b/src/workflow/OSWorkflow.cpp @@ -99,6 +99,53 @@ OSWorkflow::OSWorkflow(const WorkflowRunOptions& t_workflowRunOptions, ScriptEng } } +void OSWorkflow::initializeWeatherFileFromOSW() { + LOG(Debug, "Initialize the weather file from osw"); + auto epwPath_ = workflowJSON.weatherFile(); + + if (epwPath_) { + LOG(Debug, "Search for weather file defind by osw " << epwPath_.get()); + auto epwFullPath_ = workflowJSON.findFile(epwPath_.get()); + if (!epwFullPath_) { + auto epwFullPath_ = workflowJSON.findFile(epwPath_->filename()); + } + if (!epwFullPath_) { + throw std::runtime_error(fmt::format("Weather file {} specified but cannot be found", epwPath_->string())); + } + + epwPath = epwFullPath_.get(); + + if (auto epwFile_ = openstudio::EpwFile::load(epwPath)) { + model::WeatherFile::setWeatherFile(model, epwFile_.get()); + } else { + LOG(Warn, "Could not load weather file from " << epwPath_.get()); + } + } else { + LOG(Debug, "No weather file defined by osw"); + } +} + +void OSWorkflow::applyWeatherFileFromModel() { + LOG(Debug, "Apply the final weather file"); + if (auto epwFile_ = model.weatherFile()) { + if (auto epwPath_ = epwFile_->path()) { + LOG(Debug, "Search for weather file " << epwPath_.get()); + auto epwFullPath_ = workflowJSON.findFile(epwPath_.get()); + if (!epwFullPath_) { + auto epwFullPath_ = workflowJSON.findFile(epwPath_->filename()); + } + if (!epwFullPath_) { + throw std::runtime_error(fmt::format("Weather file {} specified but cannot be found", epwPath_->string())); + } + + epwPath = epwFullPath_.get(); + return; + } + } + + LOG(Warn, "Weather file is not defined"); +} + void OSWorkflow::applyArguments(measure::OSArgumentMap& argumentMap, const std::string& argumentName, const openstudio::Variant& argumentValue) { LOG(Info, "Setting argument value '" << argumentName << "' to '" << argumentValue << "'"); diff --git a/src/workflow/OSWorkflow.hpp b/src/workflow/OSWorkflow.hpp index 02dbf2af8c7..73f3ad1c693 100644 --- a/src/workflow/OSWorkflow.hpp +++ b/src/workflow/OSWorkflow.hpp @@ -116,6 +116,8 @@ class OSWorkflow //@} + void initializeWeatherFileFromOSW(); + void applyWeatherFileFromModel(); void applyMeasures(MeasureType measureType, bool energyplus_output_requests = false); static void applyArguments(measure::OSArgumentMap& argumentMap, const std::string& argumentName, const openstudio::Variant& argumentValue); void saveOSMToRootDirIfDebug(); diff --git a/src/workflow/RunInitialization.cpp b/src/workflow/RunInitialization.cpp index c6d6d9a419e..a35ba499a64 100644 --- a/src/workflow/RunInitialization.cpp +++ b/src/workflow/RunInitialization.cpp @@ -83,41 +83,7 @@ void OSWorkflow::runInitialization() { openstudio::model::initializeModelObjects(model); } - LOG(Debug, "Getting the initial weather file"); - // Initialize the weather file - auto epwPath_ = workflowJSON.weatherFile(); - bool alreadySetInModel = false; - if (!epwPath_) { - LOG(Debug, "No weather file specified in OSW, looking in model"); - if (auto epwFile_ = model.weatherFile()) { - epwPath_ = epwFile_->path(); - alreadySetInModel = true; - } - } - - if (epwPath_) { - LOG(Debug, "Search for weather file " << epwPath_.get()); - auto epwFullPath_ = workflowJSON.findFile(epwPath_.get()); - if (!epwFullPath_) { - auto epwFullPath_ = workflowJSON.findFile(epwPath_->filename()); - } - if (!epwFullPath_) { - throw std::runtime_error(fmt::format("Weather file {} specified but cannot be found", epwPath_->string())); - } - - epwPath = epwFullPath_.get(); - - if (!alreadySetInModel) { - if (auto epwFile_ = openstudio::EpwFile::load(epwPath)) { - model::WeatherFile::setWeatherFile(model, epwFile_.get()); - } else { - LOG(Warn, "Could not load weather file from " << epwPath_.get()); - } - } - - } else { - LOG(Debug, "No valid weather file defined in either the osm or osw"); - } + initializeWeatherFileFromOSW(); // Set a clone of the WorkflowJSON for the model, so that it finds the filePaths (such as generated_files we added above) model.setWorkflowJSON(workflowJSON.clone()); diff --git a/src/workflow/RunTranslator.cpp b/src/workflow/RunTranslator.cpp index f5850dd3ced..91484dbb84c 100644 --- a/src/workflow/RunTranslator.cpp +++ b/src/workflow/RunTranslator.cpp @@ -38,6 +38,8 @@ void OSWorkflow::runTranslator() { auto runDir = workflowJSON.absoluteRunDir(); OS_ASSERT(openstudio::filesystem::is_directory(runDir)); + applyWeatherFileFromModel(); + // Copy in the weather file if (!epwPath.empty()) { openstudio::filesystem::copy_file(epwPath, runDir / "in.epw", openstudio::filesystem::copy_options::overwrite_existing); From c5219a55c5f126bd1e8d6e760fc5b3ddd96a9418 Mon Sep 17 00:00:00 2001 From: Kyle Benne Date: Tue, 31 Oct 2023 13:49:00 -0500 Subject: [PATCH 2/3] Update Runner::lastEPWFile before every Measure. ref #5009 --- src/workflow/ApplyMeasure.cpp | 5 ++--- src/workflow/OSWorkflow.cpp | 11 +++++++---- src/workflow/OSWorkflow.hpp | 2 +- src/workflow/RunTranslator.cpp | 2 +- 4 files changed, 11 insertions(+), 9 deletions(-) diff --git a/src/workflow/ApplyMeasure.cpp b/src/workflow/ApplyMeasure.cpp index 54c56415650..d15886bdf19 100644 --- a/src/workflow/ApplyMeasure.cpp +++ b/src/workflow/ApplyMeasure.cpp @@ -60,9 +60,8 @@ void OSWorkflow::applyMeasures(MeasureType measureType, bool energyplus_output_r if (!sqlPath.empty()) { runner.setLastEnergyPlusSqlFilePath(sqlPath); } - if (!epwPath.empty()) { - runner.setLastEpwFilePath(epwPath); - } + + updateLastWeatherFileFromModel(); const auto measureDirName = step.measureDirName(); if (openstudio::filesystem::path(measureDirName).is_absolute()) { diff --git a/src/workflow/OSWorkflow.cpp b/src/workflow/OSWorkflow.cpp index 6a7cc4b7b97..5fbfebde22b 100644 --- a/src/workflow/OSWorkflow.cpp +++ b/src/workflow/OSWorkflow.cpp @@ -117,16 +117,17 @@ void OSWorkflow::initializeWeatherFileFromOSW() { if (auto epwFile_ = openstudio::EpwFile::load(epwPath)) { model::WeatherFile::setWeatherFile(model, epwFile_.get()); + runner.setLastEpwFilePath(epwPath); } else { LOG(Warn, "Could not load weather file from " << epwPath_.get()); } } else { - LOG(Debug, "No weather file defined by osw"); + LOG(Debug, "Weather file is not defined by the osw"); } } -void OSWorkflow::applyWeatherFileFromModel() { - LOG(Debug, "Apply the final weather file"); +void OSWorkflow::updateLastWeatherFileFromModel() { + LOG(Debug, "Find model's weather file and update LastEpwFilePath"); if (auto epwFile_ = model.weatherFile()) { if (auto epwPath_ = epwFile_->path()) { LOG(Debug, "Search for weather file " << epwPath_.get()); @@ -139,11 +140,13 @@ void OSWorkflow::applyWeatherFileFromModel() { } epwPath = epwFullPath_.get(); + runner.setLastEpwFilePath(epwPath); + return; } } - LOG(Warn, "Weather file is not defined"); + LOG(Debug, "Weather file is not defined by the model"); } void OSWorkflow::applyArguments(measure::OSArgumentMap& argumentMap, const std::string& argumentName, const openstudio::Variant& argumentValue) { diff --git a/src/workflow/OSWorkflow.hpp b/src/workflow/OSWorkflow.hpp index 73f3ad1c693..e7b608f479f 100644 --- a/src/workflow/OSWorkflow.hpp +++ b/src/workflow/OSWorkflow.hpp @@ -117,7 +117,7 @@ class OSWorkflow //@} void initializeWeatherFileFromOSW(); - void applyWeatherFileFromModel(); + void updateLastWeatherFileFromModel(); void applyMeasures(MeasureType measureType, bool energyplus_output_requests = false); static void applyArguments(measure::OSArgumentMap& argumentMap, const std::string& argumentName, const openstudio::Variant& argumentValue); void saveOSMToRootDirIfDebug(); diff --git a/src/workflow/RunTranslator.cpp b/src/workflow/RunTranslator.cpp index 91484dbb84c..9fd9b97036a 100644 --- a/src/workflow/RunTranslator.cpp +++ b/src/workflow/RunTranslator.cpp @@ -38,7 +38,7 @@ void OSWorkflow::runTranslator() { auto runDir = workflowJSON.absoluteRunDir(); OS_ASSERT(openstudio::filesystem::is_directory(runDir)); - applyWeatherFileFromModel(); + updateLastWeatherFileFromModel(); // Copy in the weather file if (!epwPath.empty()) { From 100167e1887a072f30dca3f74d85a26469fe3d44 Mon Sep 17 00:00:00 2001 From: Kyle Benne Date: Wed, 1 Nov 2023 10:36:52 -0500 Subject: [PATCH 3/3] Adjust timing for updating the lastEPWFile ref #5009 --- src/workflow/ApplyMeasure.cpp | 9 +-------- src/workflow/RunTranslator.cpp | 2 -- 2 files changed, 1 insertion(+), 10 deletions(-) diff --git a/src/workflow/ApplyMeasure.cpp b/src/workflow/ApplyMeasure.cpp index d15886bdf19..c3a07f09ccb 100644 --- a/src/workflow/ApplyMeasure.cpp +++ b/src/workflow/ApplyMeasure.cpp @@ -212,14 +212,7 @@ void OSWorkflow::applyMeasures(MeasureType measureType, bool energyplus_output_r } if (measureType == MeasureType::ModelMeasure) { - if (auto weatherFile_ = model.weatherFile()) { - if (auto p_ = weatherFile_->path()) { - // Probably a workflowJSON.findFile() call... - // m_epwPath_ = p_; - } else { - LOG(Warn, "Weather file object found in model but no path is given"); - } - } + updateLastWeatherFileFromModel(); } if (m_add_timings && m_detailed_timings) { diff --git a/src/workflow/RunTranslator.cpp b/src/workflow/RunTranslator.cpp index 9fd9b97036a..f5850dd3ced 100644 --- a/src/workflow/RunTranslator.cpp +++ b/src/workflow/RunTranslator.cpp @@ -38,8 +38,6 @@ void OSWorkflow::runTranslator() { auto runDir = workflowJSON.absoluteRunDir(); OS_ASSERT(openstudio::filesystem::is_directory(runDir)); - updateLastWeatherFileFromModel(); - // Copy in the weather file if (!epwPath.empty()) { openstudio::filesystem::copy_file(epwPath, runDir / "in.epw", openstudio::filesystem::copy_options::overwrite_existing);