Skip to content

Commit

Permalink
add pendulum example and some other changes
Browse files Browse the repository at this point in the history
  • Loading branch information
markaren committed Nov 8, 2024
1 parent 3cfb82e commit 2722cb2
Show file tree
Hide file tree
Showing 9 changed files with 144 additions and 63 deletions.
6 changes: 4 additions & 2 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,12 @@ project(fmu4cpp-template VERSION ${projectVersion})

option(FMU4CPP_BUILD_TESTS "Build tests" OFF)

set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD 20)
set(CMAKE_WINDOWS_EXPORT_ALL_SYMBOLS ON)

set(modelIdentifier identity)
set(modelIdentifier identity) # <-- CHANGE ME

include("cmake/generate_fmu.cmake")

if (MSVC)
# link statically against the the Visual C runtime
Expand Down
5 changes: 5 additions & 0 deletions cmake/generate_fmu.cmake
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@

function(generateFMU modelIdentifier)

target_sources(${modelIdentifier} PRIVATE "$<TARGET_OBJECTS:fmu4cpp>")
target_include_directories("${modelIdentifier}" PRIVATE "${PROJECT_SOURCE_DIR}/export/include")
target_compile_definitions("${modelIdentifier}" PRIVATE FMU4CPP_MODEL_IDENTIFIER="${modelIdentifier}")


set(outputDir "$<1:${CMAKE_BINARY_DIR}/${modelIdentifier}/binaries/${TARGET_PLATFORM}>")

if (WIN32)
Expand Down
2 changes: 1 addition & 1 deletion export/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
add_subdirectory(src)
add_subdirectory(examples)

add_executable(descriptionGenerator descriptionGenerator.cpp)
add_executable(descriptionGenerator "descriptionGenerator.cpp")
if (UNIX)
target_link_libraries(descriptionGenerator PRIVATE dl)
endif()
Expand Down
5 changes: 1 addition & 4 deletions export/examples/BouncingBall/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@

add_library(bouncing_ball SHARED
bouncing_ball.cpp
"$<TARGET_OBJECTS:fmu4cpp>"
"bouncing_ball.cpp"
)
target_include_directories(bouncing_ball PRIVATE "../../include")
target_compile_definitions(bouncing_ball PRIVATE FMU4CPP_MODEL_IDENTIFIER="bouncing_ball")

generateFMU("bouncing_ball")
3 changes: 1 addition & 2 deletions export/examples/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@

include("${PROJECT_SOURCE_DIR}/cmake/generate_fmu.cmake")

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

add_library(simple_pendulum SHARED
"simple_pendulum.cpp"
)

generateFMU(simple_pendulum)
78 changes: 78 additions & 0 deletions export/examples/SimplePendulum/simple_pendulum.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@

#include <fmu4cpp/fmu_base.hpp>

#include <numbers>

using namespace fmu4cpp;


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

register_variable(real("angle",
[this] { return angle_; })
.setCausality(causality_t::OUTPUT)
.setVariability(variability_t::CONTINUOUS));

register_variable(real("angularVelocity",
[this] { return angularVelocity_; })
.setCausality(causality_t::LOCAL)
.setVariability(variability_t::CONTINUOUS));
register_variable(real(
"gravity",
[this] { return gravity_; },
[this](double input) { gravity_ = input; })
.setCausality(causality_t::PARAMETER)
.setVariability(variability_t::FIXED));
register_variable(real(
"length",
[this] { return length_; },
[this](double input) { length_ = input; })
.setCausality(causality_t::PARAMETER)
.setVariability(variability_t::FIXED));
register_variable(real(
"damping",
[this] { return damping_; },
[this](double input) { damping_ = input; })
.setCausality(causality_t::PARAMETER)
.setVariability(variability_t::FIXED));

SimplePendulum::reset();
}

bool do_step(double currentTime, double dt) override {
angularVelocity_ += (-gravity_ / length_) * sin(angle_) * dt - damping_ * angularVelocity_ * dt;
angle_ += angularVelocity_ * dt;
return true;
}

void reset() override {
angle_ = std::numbers::pi / 4;// 45 degrees
angularVelocity_ = 0;
gravity_ = -9.81;
length_ = 1.0;
damping_ = 0.1;
}

private:
double angle_; // Current angle of the pendulum
double angularVelocity_;// Current angular velocity
double gravity_; // Acceleration due to gravity
double length_; // Length of the pendulum
double damping_; // Damping factor
};


model_info fmu4cpp::get_model_info() {
model_info info;
info.modelName = "SimplePendulum";
info.description = "A simple pendulum model";
info.modelIdentifier = FMU4CPP_MODEL_IDENTIFIER;
return info;
}

std::unique_ptr<fmu_base> fmu4cpp::createInstance(const std::string &instanceName, const std::string &fmuResourceLocation) {
return std::make_unique<SimplePendulum>(instanceName, fmuResourceLocation);
}
10 changes: 3 additions & 7 deletions src/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,15 +1,11 @@
include("${PROJECT_SOURCE_DIR}/cmake/generate_fmu.cmake")


add_library(${modelIdentifier} SHARED
model.cpp
"$<TARGET_OBJECTS:fmu4cpp>")
target_include_directories(${modelIdentifier} PRIVATE ${PROJECT_SOURCE_DIR}/export/include)
target_compile_definitions(${modelIdentifier} PRIVATE FMU4CPP_MODEL_IDENTIFIER="${modelIdentifier}")
"model.cpp"
)

# Generate FMU
generateFMU("${modelIdentifier}")

set(OUTPUT_FILE "${CMAKE_BINARY_DIR}/model.txt")
file(WRITE ${OUTPUT_FILE} "${modelIdentifier}")

#generateFMU("model" "${modelIdentifier}")
92 changes: 45 additions & 47 deletions src/model.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,59 +14,56 @@ class Model : public fmu_base {
Model(const std::string &instanceName, const std::string &resources)
: fmu_base(instanceName, resources) {

register_variable(
integer(
"integerIn", [this] { return integer_; }, [this](int value) { integer_ = value; })
.setCausality(causality_t::INPUT)
.setVariability(variability_t::DISCRETE)
.setInitial(initial_t::EXACT));
register_variable(integer(
"integerIn", [this] { return integer_; },
[this](int value) { integer_ = value; })
.setCausality(causality_t::INPUT)
.setVariability(variability_t::DISCRETE)
.setInitial(initial_t::EXACT));
register_variable(
real(
"realIn", [this] { return real_; }, [this](double value) { real_ = value; })
.setCausality(causality_t::INPUT)
.setVariability(variability_t::DISCRETE)
.setInitial(initial_t::EXACT));
register_variable(
boolean(
"booleanIn", [this] { return boolean_; }, [this](bool value) { boolean_ = value; })
.setCausality(causality_t::INPUT)
.setVariability(variability_t::DISCRETE)
.setInitial(initial_t::EXACT));
register_variable(
string(
"stringIn", [this] { return string_; }, [this](std::string value) { string_ = std::move(value); })
.setCausality(causality_t::INPUT)
.setVariability(variability_t::DISCRETE)
.setInitial(initial_t::EXACT));

register_variable(
integer(
"integerOut", [this] { return integer_; })
.setCausality(causality_t::OUTPUT)
.setVariability(variability_t::DISCRETE)
.setInitial(initial_t::CALCULATED)
.setDependencies({get_int_variable("integerIn")->index()}));
register_variable(
real(
"realOut", [this] { return real_; })
.setCausality(causality_t::OUTPUT)
.setVariability(variability_t::DISCRETE)
.setInitial(initial_t::CALCULATED)
.setDependencies({get_real_variable("realIn")->index()}));
register_variable(
boolean(
"booleanOut", [this] { return boolean_; })
.setCausality(causality_t::OUTPUT)
.setVariability(variability_t::DISCRETE)
.setInitial(initial_t::CALCULATED)
.setDependencies({get_bool_variable("booleanIn")->index()}));
register_variable(
string(
"stringOut", [this] { return string_; })
.setCausality(causality_t::OUTPUT)
.setVariability(variability_t::DISCRETE)
.setInitial(initial_t::CALCULATED)
.setDependencies({get_string_variable("stringIn")->index()}));
register_variable(boolean(
"booleanIn", [this] { return boolean_; },
[this](bool value) { boolean_ = value; })
.setCausality(causality_t::INPUT)
.setVariability(variability_t::DISCRETE)
.setInitial(initial_t::EXACT));

register_variable(string(
"stringIn", [this] { return string_; },
[this](std::string value) { string_ = std::move(value); })
.setCausality(causality_t::INPUT)
.setVariability(variability_t::DISCRETE)
.setInitial(initial_t::EXACT));

register_variable(integer("integerOut", [this] { return integer_; })
.setCausality(causality_t::OUTPUT)
.setVariability(variability_t::DISCRETE)
.setInitial(initial_t::CALCULATED)
.setDependencies({get_int_variable("integerIn")->index()}));

register_variable(real("realOut", [this] { return real_; })
.setCausality(causality_t::OUTPUT)
.setVariability(variability_t::DISCRETE)
.setInitial(initial_t::CALCULATED)
.setDependencies({get_real_variable("realIn")->index()}));

register_variable(boolean("booleanOut", [this] { return boolean_; })
.setCausality(causality_t::OUTPUT)
.setVariability(variability_t::DISCRETE)
.setInitial(initial_t::CALCULATED)
.setDependencies({get_bool_variable("booleanIn")->index()}));

register_variable(string("stringOut", [this] { return string_; })
.setCausality(causality_t::OUTPUT)
.setVariability(variability_t::DISCRETE)
.setInitial(initial_t::CALCULATED)
.setDependencies({get_string_variable("stringIn")->index()}));

Model::reset();
}
Expand Down Expand Up @@ -98,6 +95,7 @@ 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::string &fmuResourceLocation) {
return std::make_unique<Model>(instanceName, fmuResourceLocation);
}

0 comments on commit 2722cb2

Please sign in to comment.