Skip to content

Commit

Permalink
Fix issue that prevented reseting a Spawn FMU
Browse files Browse the repository at this point in the history
Each generated EnergyPlus FMU is now given a unique id.

ref lbl-srg/modelica-buildings#2956
  • Loading branch information
kbenne committed Apr 11, 2022
1 parent 3caf70d commit a8ff1e5
Show file tree
Hide file tree
Showing 5 changed files with 56 additions and 5 deletions.
5 changes: 5 additions & 0 deletions fmu/modeldescription.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,11 @@ class ModelDescription
}
}

[[nodiscard]] std::string guid() const
{
return fmiModelDescription().attribute("guid").as_string();
}

[[nodiscard]] std::string modelIdentifier() const
{
auto typeNode = fmiModelDescription().child("CoSimulation");
Expand Down
21 changes: 16 additions & 5 deletions lib/fmugenerator.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#include "fmugenerator.hpp"
#include "../submodules/EnergyPlus/src/EnergyPlus/InputProcessing/IdfParser.hh"
#include "../util/fmi_paths.hpp"
#include "../util/unique_id.hpp"
#include "input/input.hpp"
#include "modelDescription.xml.hpp"
#include "ziputil.hpp"
Expand All @@ -10,7 +11,7 @@ using json = nlohmann::json;

namespace spawn {

void createModelDescription(const spawn::Input &input, const spawn_fs::path &savepath);
void createModelDescription(const spawn::Input &input, const spawn_fs::path &savepath, const std::string &id);

void energyplusToFMU(const std::string &jsoninput,
bool nozip,
Expand Down Expand Up @@ -44,13 +45,15 @@ void energyplusToFMU(const std::string &jsoninput,
spawn_fs::create_directories(outputroot);
}

const auto id = util::uniqueId();

const auto modelDescriptionPath = fmuStagingPath / "modelDescription.xml";
const auto fmuResourcesPath = fmuStagingPath / "resources";
const auto fmuspawnPath = fmuResourcesPath / "model.spawn";
const auto fmuidfPath = fmuResourcesPath / input.idfInputPath().filename();
const auto fmuepwPath = fmuResourcesPath / input.epwInputPath().filename();
const auto fmuiddPath = fmuResourcesPath / iddpath.filename();
const auto fmuEPFMIPath = fmuStagingPath / fmi_lib_path(epfmi_basename());
const auto fmuEPFMIPath = fmuStagingPath / fmi_lib_path(id);

spawn_fs::remove_all(fmuPath);
spawn_fs::remove_all(fmuStagingPath);
Expand All @@ -60,12 +63,14 @@ void energyplusToFMU(const std::string &jsoninput,
spawn_fs::create_directories(fmuResourcesPath);
spawn_fs::create_directories(fmuEPFMIPath.parent_path());

std::cout << "Generating fmuEPFMIPath: " << fmuEPFMIPath << std::endl;

spawn_fs::copy_file(epfmupath, fmuEPFMIPath, spawn_fs::copy_options::overwrite_existing);
spawn_fs::copy_file(iddpath, fmuiddPath, spawn_fs::copy_options::overwrite_existing);
spawn_fs::copy_file(input.epwInputPath(), fmuepwPath, spawn_fs::copy_options::overwrite_existing);
spawn_fs::copy_file(input.idfInputPath(), fmuidfPath);

createModelDescription(input, modelDescriptionPath);
createModelDescription(input, modelDescriptionPath, id);

const auto relativeEPWPath = spawn_fs::relative(fmuepwPath, fmuResourcesPath);
input.setEPWInputPath(relativeEPWPath);
Expand All @@ -79,13 +84,19 @@ void energyplusToFMU(const std::string &jsoninput,
}
}

void createModelDescription(const spawn::Input &input, const spawn_fs::path &savepath)
void createModelDescription(const spawn::Input &input, const spawn_fs::path &savepath, const std::string &id)
{
pugi::xml_document doc;
doc.load_string(modelDescriptionXMLText.c_str());

auto xmlvariables = doc.child("fmiModelDescription").child("ModelVariables");
auto fmiModelDescription = doc.child("fmiModelDescription");
auto modelExchange = fmiModelDescription.child("ModelExchange");

fmiModelDescription.attribute("modelName").set_value(id.c_str());
fmiModelDescription.attribute("guid").set_value(id.c_str());
modelExchange.attribute("modelIdentifier").set_value(id.c_str());

auto xmlvariables = fmiModelDescription.child("ModelVariables");
const auto variables = parseVariables(input);

for (const auto &varpair : variables) {
Expand Down
2 changes: 2 additions & 0 deletions util/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ add_library(
dynamiclibrary.hpp
dynamiclibrary_windows.hpp
dynamiclibrary_posix.hpp
unique_id.hpp
unique_id.cpp
${CMAKE_CURRENT_BINARY_DIR}/config.cxx
)

Expand Down
25 changes: 25 additions & 0 deletions util/unique_id.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
#include <random>

namespace spawn::util {

std::string uniqueId()
{
constexpr auto len = 32;
static const std::string_view hex_chars = "0123456789abcdef";

std::mt19937 gen{std::random_device{}()};
std::string uuid;
uuid.reserve(len);

while (uuid.size() < len) {
auto n = gen();
for (auto i = std::mt19937::max(); i & 0x8 && uuid.size() < len; i >>= 4) {
uuid += hex_chars[n & 0xf];
n >>= 4;
}
}

return uuid;
}

} // namespace spawn::util
8 changes: 8 additions & 0 deletions util/unique_id.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
namespace spawn::util {

// This is a cheap approximation of a uuid
// It is good enough for Spawn's purposes, but not a real uuid
// Might be upgraded "some day"
std::string uniqueId();

} // namespace spawn::util

0 comments on commit a8ff1e5

Please sign in to comment.