Skip to content

Commit

Permalink
Merge pull request #4923 from NREL/4692_Model_load
Browse files Browse the repository at this point in the history
Fix #4692 - Modify Model::load to use the VersionTranslator
  • Loading branch information
jmarrec committed Aug 24, 2023
2 parents 209fe3b + 5ff5fe7 commit 3a24207
Show file tree
Hide file tree
Showing 6 changed files with 61 additions and 18 deletions.
Expand Up @@ -81,6 +81,7 @@ You can also refer to the [OpenStudio SDK Python Binding Version Compatibility M
* [#4932](https://github.com/NREL/OpenStudio/pull/4932) - Support undisturbed ground temperature models on GroundHeatExchangerVertical
* Fix #4930 - Support undisturbed ground temperature models on GroundHeatExchangerVertical
* Update `GroundHeatExchanger:Vertical` to actually use the Ground Temeprature Model field
* [#4923](https://github.com/NREL/OpenStudio/pull/4923) - Fix #4692 - Modify `Model::load` to use the VersionTranslator instead of loading it assuming the version of the loaded OSM is the same as the current SDK version being used.

## Minor changes and bug fixes

Expand Down
2 changes: 2 additions & 0 deletions resources/CMakeLists.txt
Expand Up @@ -175,6 +175,8 @@ set(model_resources_src
model/ASHRAECourthouse.osm
model/A205ExampleChiller.RS0001.a205.cbor
model/4837_SpaceVolume.osm
model/empty361.osm
model/empty361/workflow.osw
)


Expand Down
5 changes: 5 additions & 0 deletions resources/model/empty361.osm
@@ -0,0 +1,5 @@

OS:Version,
{e2190a61-e3f3-4d4b-8c0b-7a9c2342180e}, !- Handle
3.6.1; !- Version Identifier

6 changes: 6 additions & 0 deletions resources/model/empty361/workflow.osw
@@ -0,0 +1,6 @@
{
"created_at" : "20230704T152451Z",
"seed_file" : "../empty361.osm",
"steps" : [],
"updated_at" : "20230704T152548Z"
}
36 changes: 19 additions & 17 deletions src/model/Model.cpp
Expand Up @@ -21,8 +21,11 @@
#include <utilities/idd/IddEnums.hxx>
#include <utilities/idd/OS_Version_FieldEnums.hxx>

#include "../osversion/VersionTranslator.hpp"

#include "../utilities/core/Assert.hpp"
#include "../utilities/core/ContainersMove.hpp"
#include "../utilities/core/Filesystem.hpp"
#include "../utilities/core/PathHelpers.hpp"

#include "../utilities/idd/IddEnums.hpp"
Expand Down Expand Up @@ -1840,27 +1843,26 @@ namespace model {
}

boost::optional<Model> Model::load(const path& osmPath) {
OptionalModel result;
OptionalIdfFile oIdfFile = IdfFile::load(osmPath, IddFileType::OpenStudio);
if (oIdfFile) {
try {
result = Model(*oIdfFile);
} catch (...) {
}
}

if (result) {
// Load the workflow.osw in the model's companion folder
path workflowJSONPath = getCompanionFolder(osmPath) / toPath("workflow.osw");
if (exists(workflowJSONPath)) {
boost::optional<WorkflowJSON> workflowJSON = WorkflowJSON::load(workflowJSONPath);
if (workflowJSON) {
result->setWorkflowJSON(*workflowJSON);
}
if (!openstudio::filesystem::is_regular_file(osmPath)) {
LOG(Warn, "Path is not a valid file: " << osmPath);
return boost::none;
}
openstudio::osversion::VersionTranslator vt;
boost::optional<openstudio::model::Model> model_ = vt.loadModel(osmPath);
if (!model_) {
LOG(Warn, "Failed to load model at " << osmPath);
return boost::none;
}
// Load the workflow.osw in the model's companion folder
const openstudio::path workflowJSONPath = getCompanionFolder(osmPath) / toPath("workflow.osw");
if (exists(workflowJSONPath)) {
if (boost::optional<WorkflowJSON> workflowJSON_ = WorkflowJSON::load(workflowJSONPath)) {
model_->setWorkflowJSON(*workflowJSON_);
}
}

return result;
return model_;
}

boost::optional<Model> Model::load(const path& osmPath, const path& workflowJSONPath) {
Expand Down
29 changes: 28 additions & 1 deletion src/model/test/Model_GTest.cpp
Expand Up @@ -129,16 +129,18 @@
#include "../ExternalInterface.hpp"
#include "../ExternalInterface_Impl.hpp"

#include "../../utilities/sql/SqlFile.hpp"
#include "../../utilities/core/PathHelpers.hpp"
#include "../../utilities/data/TimeSeries.hpp"
#include "../../utilities/idf/IdfFile.hpp"
#include "../../utilities/idf/Workspace.hpp"
#include "../../utilities/idf/WorkspaceObject.hpp"
#include "../../utilities/idf/ValidityReport.hpp"
#include "../../utilities/sql/SqlFile.hpp"

#include "../../osversion/VersionTranslator.hpp"

#include <utilities/idd/IddEnums.hxx>
#include <OpenStudio.hxx>

#include <boost/algorithm/string/case_conv.hpp>

Expand Down Expand Up @@ -1160,3 +1162,28 @@ TEST_F(ModelFixture, UniqueModelObjectCachedGetters) {
EXPECT_TRUE(m.getOptionalUniqueModelObject<ExternalInterface>());
EXPECT_EQ(++i, m.getModelObjects<ModelObject>().size());
}

TEST_F(ModelFixture, Model_load) {

openstudio::path modelPath = resourcesPath() / "model" / toPath("empty361.osm");
ASSERT_TRUE(openstudio::filesystem::exists(modelPath));

const openstudio::path workflowJSONPath = getCompanionFolder(modelPath) / toPath("workflow.osw");
ASSERT_TRUE(openstudio::filesystem::exists(workflowJSONPath));

// Check that the versionObject is indeed translated to the current version
boost::optional<Model> model_ = Model::load(modelPath);
ASSERT_TRUE(model_);
auto versionObject_ = model_->versionObject();
ASSERT_TRUE(versionObject_);
EXPECT_EQ(openStudioVersion(), versionObject_->getString(1).get());

// Check that the workflowJSON in the companion folder is correctly loaded
auto workflowJSON = model_->workflowJSON();
auto p_ = workflowJSON.oswPath();
ASSERT_TRUE(p_);
EXPECT_EQ(workflowJSONPath, *p_);

ASSERT_TRUE(workflowJSON.seedFile());
EXPECT_EQ(workflowJSON.seedFile().get(), openstudio::toPath("../empty361.osm"));
}

0 comments on commit 3a24207

Please sign in to comment.