Skip to content

Commit

Permalink
support resources (#19)
Browse files Browse the repository at this point in the history
  • Loading branch information
markaren authored Nov 14, 2024
1 parent 5715fd1 commit 6188ea7
Show file tree
Hide file tree
Showing 14 changed files with 99 additions and 21 deletions.
27 changes: 21 additions & 6 deletions cmake/generate_fmu.cmake
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@

function(generateFMU modelIdentifier)
function(generateFMU modelIdentifier resourceFolder)

target_sources(${modelIdentifier} PRIVATE "$<TARGET_OBJECTS:fmu4cpp>")
target_include_directories("${modelIdentifier}" PRIVATE "${PROJECT_SOURCE_DIR}/export/include")
Expand All @@ -26,10 +26,25 @@ function(generateFMU modelIdentifier)
WORKING_DIRECTORY "${CMAKE_BINARY_DIR}"
COMMAND descriptionGenerator ${modelIdentifier} "${outputDir}/$<TARGET_FILE_NAME:${modelIdentifier}>")

if (resourceFolder STREQUAL "")
add_custom_command(TARGET ${modelIdentifier} POST_BUILD
WORKING_DIRECTORY "${CMAKE_BINARY_DIR}/${modelIdentifier}"
COMMAND ${CMAKE_COMMAND} -E tar "c" "${modelIdentifier}.fmu" --format=zip
"${CMAKE_BINARY_DIR}/${modelIdentifier}/binaries"
"${CMAKE_BINARY_DIR}/${modelIdentifier}/modelDescription.xml")

else ()
message("[generateFMU] Using resourceFolder=${resourceFolder} for ${modelIdentifier}")

file(COPY "${resourceFolder}/" DESTINATION "${CMAKE_BINARY_DIR}/${modelIdentifier}/resources")

add_custom_command(TARGET ${modelIdentifier} POST_BUILD
WORKING_DIRECTORY "${CMAKE_BINARY_DIR}/${modelIdentifier}"
COMMAND ${CMAKE_COMMAND} -E tar "c" "${modelIdentifier}.fmu" --format=zip
"resources"
"${CMAKE_BINARY_DIR}/${modelIdentifier}/binaries"
"${CMAKE_BINARY_DIR}/${modelIdentifier}/modelDescription.xml")
endif ()


add_custom_command(TARGET ${modelIdentifier} POST_BUILD
WORKING_DIRECTORY "${CMAKE_BINARY_DIR}/${modelIdentifier}"
COMMAND ${CMAKE_COMMAND} -E tar "c" "${modelIdentifier}.fmu" --format=zip
"${CMAKE_BINARY_DIR}/${modelIdentifier}/binaries"
"${CMAKE_BINARY_DIR}/${modelIdentifier}/modelDescription.xml")
endfunction()
2 changes: 1 addition & 1 deletion export/examples/BouncingBall/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,4 @@ add_library(bouncing_ball SHARED
"bouncing_ball.cpp"
)

generateFMU("bouncing_ball")
generateFMU("bouncing_ball" "")
4 changes: 2 additions & 2 deletions export/examples/BouncingBall/bouncing_ball.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ using namespace fmu4cpp;
class BouncingBall : public fmu_base {

public:
BouncingBall(const std::string &instanceName, const std::string &resources)
BouncingBall(const std::string &instanceName, const std::filesystem::path &resources)
: fmu_base(instanceName, resources) {

register_variable(
Expand Down Expand Up @@ -79,6 +79,6 @@ model_info fmu4cpp::get_model_info() {
return info;
}

std::unique_ptr<fmu_base> fmu4cpp::createInstance(const std::string &instanceName, const std::string &fmuResourceLocation) {
std::unique_ptr<fmu_base> fmu4cpp::createInstance(const std::string &instanceName, const std::filesystem::path &fmuResourceLocation) {
return std::make_unique<BouncingBall>(instanceName, fmuResourceLocation);
}
1 change: 1 addition & 0 deletions export/examples/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@

add_subdirectory(BouncingBall)
add_subdirectory(SimplePendulum)
add_subdirectory(Resource)
6 changes: 6 additions & 0 deletions export/examples/Resource/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@

add_library(resource SHARED
"resource.cpp"
)

generateFMU("resource" "${CMAKE_CURRENT_SOURCE_DIR}/resources")
52 changes: 52 additions & 0 deletions export/examples/Resource/resource.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@


#include <fmu4cpp/fmu_base.hpp>

#include <fstream>

using namespace fmu4cpp;


class Resource : public fmu_base {

public:
Resource(const std::string &instanceName, const std::filesystem::path &resources)
: fmu_base(instanceName, resources) {

std::ifstream ifs(resources / "file.txt");

std::string line;
std::getline(ifs, line);

register_variable(
string(
"content", [line] { return line; })
.setVariability(variability_t::CONSTANT)
.setCausality(causality_t::OUTPUT));

Resource::reset();
}

bool do_step(double currentTime, double dt) override {

debugLog(fmi2OK, get_string_variable("content")->get());
return true;
}

void reset() override {
// do nothing
}

};

model_info fmu4cpp::get_model_info() {
model_info info;
info.modelName = "Resource";
info.description = "A model with resources";
info.modelIdentifier = FMU4CPP_MODEL_IDENTIFIER;
return info;
}

std::unique_ptr<fmu_base> fmu4cpp::createInstance(const std::string &instanceName, const std::filesystem::path &fmuResourceLocation) {
return std::make_unique<Resource>(instanceName, fmuResourceLocation);
}
1 change: 1 addition & 0 deletions export/examples/Resource/resources/file.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Hello World!
2 changes: 1 addition & 1 deletion export/examples/SimplePendulum/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,4 @@ add_library(simple_pendulum SHARED
"simple_pendulum.cpp"
)

generateFMU(simple_pendulum)
generateFMU(simple_pendulum "")
4 changes: 2 additions & 2 deletions export/examples/SimplePendulum/simple_pendulum.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ using namespace fmu4cpp;

class SimplePendulum : public fmu_base {
public:
SimplePendulum(const std::string &instanceName, const std::string &resources)
SimplePendulum(const std::string &instanceName, const std::filesystem::path &resources)
: fmu_base(instanceName, resources) {

register_variable(real("angle",
Expand Down Expand Up @@ -74,6 +74,6 @@ model_info fmu4cpp::get_model_info() {
return info;
}

std::unique_ptr<fmu_base> fmu4cpp::createInstance(const std::string &instanceName, const std::string &fmuResourceLocation) {
std::unique_ptr<fmu_base> fmu4cpp::createInstance(const std::string &instanceName, const std::filesystem::path &fmuResourceLocation) {
return std::make_unique<SimplePendulum>(instanceName, fmuResourceLocation);
}
10 changes: 6 additions & 4 deletions export/include/fmu4cpp/fmu_base.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,14 @@
#include "logger.hpp"
#include "model_info.hpp"

#include <filesystem>

namespace fmu4cpp {

class fmu_base {

public:
fmu_base(std::string instance_name, std::string resourceLocation)
fmu_base(std::string instance_name, std::filesystem::path resourceLocation)
: instanceName_(std::move(instance_name)), resourceLocation_(std::move(resourceLocation)) {}

fmu_base(const fmu_base &) = delete;
Expand All @@ -29,7 +31,7 @@ namespace fmu4cpp {
return instanceName_;
}

[[nodiscard]] std::string resourceLocation() const {
[[nodiscard]] const std::filesystem::path& resourceLocation() const {
return resourceLocation_;
}

Expand Down Expand Up @@ -181,7 +183,7 @@ namespace fmu4cpp {
size_t numVariables_{1};

std::string instanceName_;
std::string resourceLocation_;
std::filesystem::path resourceLocation_;

std::vector<IntVariable> integers_;
std::vector<RealVariable> reals_;
Expand All @@ -193,7 +195,7 @@ namespace fmu4cpp {

model_info get_model_info();

std::unique_ptr<fmu_base> createInstance(const std::string &instanceName, const std::string &fmuResourceLocation);
std::unique_ptr<fmu_base> createInstance(const std::string &instanceName, const std::filesystem::path &fmuResourceLocation);

}// namespace fmu4cpp

Expand Down
4 changes: 2 additions & 2 deletions export/tests/basic_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
class Model : public fmu4cpp::fmu_base {

public:
Model(const std::string &instanceName, const std::string &resources)
Model(const std::string &instanceName, const std::filesystem::path &resources)
: fmu_base(instanceName, resources) {

register_variable(real("myReal", [this] { return real_; })
Expand Down Expand Up @@ -50,7 +50,7 @@ fmu4cpp::model_info fmu4cpp::get_model_info() {
return m;
}

std::unique_ptr<fmu4cpp::fmu_base> fmu4cpp::createInstance(const std::string &instanceName, const std::string &fmuResourceLocation) {
std::unique_ptr<fmu4cpp::fmu_base> fmu4cpp::createInstance(const std::string &instanceName, const std::filesystem::path &fmuResourceLocation) {
return std::make_unique<Model>(instanceName, fmuResourceLocation);
}

Expand Down
1 change: 1 addition & 0 deletions resources/readme.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
place fmu4cpp resources in this folder
2 changes: 1 addition & 1 deletion src/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ add_library(${modelIdentifier} SHARED
)

# Generate FMU
generateFMU("${modelIdentifier}")
generateFMU("${modelIdentifier}" "${PROJECT_SOURCE_DIR}/resources")

set(OUTPUT_FILE "${CMAKE_BINARY_DIR}/model.txt")
file(WRITE ${OUTPUT_FILE} "${modelIdentifier}")
4 changes: 2 additions & 2 deletions src/model.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ using namespace fmu4cpp;
class Model : public fmu_base {

public:
Model(const std::string &instanceName, const std::string &resources)
Model(const std::string &instanceName, const std::filesystem::path &resources)
: fmu_base(instanceName, resources) {

register_variable(integer(
Expand Down Expand Up @@ -96,6 +96,6 @@ model_info fmu4cpp::get_model_info() {
}

std::unique_ptr<fmu_base> fmu4cpp::createInstance(const std::string &instanceName,
const std::string &fmuResourceLocation) {
const std::filesystem::path &fmuResourceLocation) {
return std::make_unique<Model>(instanceName, fmuResourceLocation);
}

0 comments on commit 6188ea7

Please sign in to comment.