Skip to content

Commit

Permalink
Merge branch 'master' into joss
Browse files Browse the repository at this point in the history
  • Loading branch information
markaren committed Dec 3, 2024
2 parents 4fee7fb + 50c0612 commit f01801e
Show file tree
Hide file tree
Showing 15 changed files with 491 additions and 107 deletions.
11 changes: 4 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,28 +30,25 @@ public:

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

register_variable(
real(
"velocity", [this] { return velocity; })
"velocity", &velocity)
.setCausality(causality_t::LOCAL)
.setVariability(variability_t::CONTINUOUS));

register_variable(
real(
"gravity", [this] { return gravity; },
[this](const auto &input) { gravity = input; })
"gravity", &gravity)
.setCausality(causality_t::PARAMETER)
.setVariability(variability_t::FIXED));

register_variable(
real(
"bounceFactor",
[this] { return bounceFactor; },
[this](const auto &input) { bounceFactor = input; })
"bounceFactor", &bounceFactor)
.setCausality(causality_t::PARAMETER)
.setVariability(variability_t::FIXED));

Expand Down
11 changes: 4 additions & 7 deletions export/examples/BouncingBall/bouncing_ball.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,29 +12,26 @@ class BouncingBall : public fmu_base {

register_variable(
real(
"height", [this] { return height_; })
"height", &height_)
.setCausality(causality_t::OUTPUT)
.setVariability(variability_t::CONTINUOUS)
.setInitial(initial_t::EXACT));

register_variable(
real(
"velocity", [this] { return velocity_; })
"velocity", &velocity_)
.setCausality(causality_t::LOCAL)
.setVariability(variability_t::CONTINUOUS));

register_variable(
real(
"gravity", [this] { return gravity_; },
[this](const auto &input) { gravity_ = input; })
"gravity", &gravity_)
.setCausality(causality_t::PARAMETER)
.setVariability(variability_t::FIXED));

register_variable(
real(
"bounceFactor",
[this] { return bounceFactor_; },
[this](const auto &input) { bounceFactor_ = input; })
"bounceFactor", &bounceFactor_)
.setCausality(causality_t::PARAMETER)
.setVariability(variability_t::FIXED));

Expand Down
8 changes: 5 additions & 3 deletions export/examples/Resource/resource.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,11 @@ class Resource : public fmu_base {

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

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

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

Expand All @@ -37,6 +36,9 @@ class Resource : public fmu_base {
// do nothing
}

private:
std::string content_;

};

model_info fmu4cpp::get_model_info() {
Expand Down
18 changes: 5 additions & 13 deletions export/examples/SimplePendulum/simple_pendulum.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,31 +12,23 @@ class SimplePendulum : public fmu_base {
SimplePendulum(const std::string &instanceName, const std::filesystem::path &resources)
: fmu_base(instanceName, resources) {

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

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

Expand Down
8 changes: 6 additions & 2 deletions export/include/fmu4cpp/fmu_base.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -166,18 +166,22 @@ namespace fmu4cpp {
virtual ~fmu_base() = default;

protected:
IntVariable integer(const std::string &name, int *ptr);
IntVariable integer(const std::string &name,
const std::function<int()> &getter,
const std::optional<std::function<void(int)>> &setter = std::nullopt);

RealVariable real(const std::string &name, double *ptr);
RealVariable real(const std::string &name,
const std::function<double()> &getter,
const std::optional<std::function<void(double)>> &setter = std::nullopt);

BoolVariable boolean(const std::string &name, bool *ptr);
BoolVariable boolean(const std::string &name,
const std::function<bool()> &getter,
const std::optional<std::function<void(bool)>> &setter = std::nullopt);
const std::function<bool()> &getter,
const std::optional<std::function<void(bool)>> &setter);

StringVariable string(const std::string &name, std::string *ptr);
StringVariable string(const std::string &name,
const std::function<std::string()> &getter,
const std::optional<std::function<void(std::string)>> &setter = std::nullopt);
Expand Down
48 changes: 42 additions & 6 deletions export/include/fmu4cpp/fmu_variable.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@
#ifndef FMU4CPP_FMU_VARIABLE_HPP
#define FMU4CPP_FMU_VARIABLE_HPP

#include <functional>
#include "variable_access.hpp"

#include <memory>
#include <optional>
#include <stdexcept>
#include <string>
Expand Down Expand Up @@ -37,6 +39,7 @@ namespace fmu4cpp {
std::string to_string(const variability_t &v);
std::string to_string(const initial_t &i);


class VariableBase {

protected:
Expand Down Expand Up @@ -93,19 +96,30 @@ namespace fmu4cpp {
class Variable : public VariableBase {

public:
Variable(
const std::string &name,
unsigned int vr, size_t index, T *ptr)
: VariableBase(name, vr, index), access_(std::make_unique<PtrAccess<T>>(ptr)) {}

Variable(
const std::string &name,
unsigned int vr, size_t index,
std::function<T()> getter,
std::optional<std::function<void(T)>> setter)
: VariableBase(name, vr, index), getter_(std::move(getter)), setter_(std::move(setter)) {}
: VariableBase(name, vr, index),
access_(std::make_unique<LambdaAccess<T>>(std::move(getter), std::move(setter))) {}

[[nodiscard]] T get() const {
return getter_();

return access_->get();
}

void set(T value) {
if (setter_) setter_->operator()(value);
if (causality_ == causality_t::LOCAL || causality_ == causality_t::OUTPUT) {
throw std::logic_error("Cannot set value for variable with causality: " + to_string(causality_));
}

access_->set(value);
}

V &setCausality(causality_t causality) {
Expand All @@ -131,13 +145,17 @@ namespace fmu4cpp {
}

private:
std::function<T()> getter_;
std::optional<std::function<void(T)>> setter_;
std::shared_ptr<VariableAccess<T>> access_;
};

class IntVariable final : public Variable<int, IntVariable> {

public:
IntVariable(
const std::string &name,
unsigned int vr, size_t index, int *ptr)
: Variable(name, vr, index, ptr) {}

IntVariable(
const std::string &name,
unsigned int vr, size_t index,
Expand Down Expand Up @@ -171,6 +189,14 @@ namespace fmu4cpp {
class RealVariable final : public Variable<double, RealVariable> {

public:
RealVariable(
const std::string &name,
unsigned int vr, size_t index, double *ptr)
: Variable(name, vr, index, ptr) {

variability_ = variability_t::CONTINUOUS;
}

RealVariable(
const std::string &name,
unsigned int vr, size_t index,
Expand Down Expand Up @@ -207,6 +233,11 @@ namespace fmu4cpp {
class BoolVariable final : public Variable<bool, BoolVariable> {

public:
BoolVariable(
const std::string &name,
unsigned int vr, size_t index, bool *ptr)
: Variable(name, vr, index, ptr) {}

BoolVariable(
const std::string &name,
unsigned int vr, size_t index,
Expand All @@ -218,6 +249,11 @@ namespace fmu4cpp {
class StringVariable final : public Variable<std::string, StringVariable> {

public:
StringVariable(
const std::string &name,
unsigned int vr, size_t index, std::string *ptr)
: Variable(name, vr, index, ptr) {}

StringVariable(
const std::string &name,
unsigned int vr, size_t index,
Expand Down
62 changes: 62 additions & 0 deletions export/include/fmu4cpp/variable_access.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@

#ifndef FMU4CPP_VARIABLEACCESS_HPP
#define FMU4CPP_VARIABLEACCESS_HPP

#include <functional>
#include <optional>
#include <utility>

namespace fmu4cpp {

template<typename T>
struct VariableAccess {
virtual T get() = 0;
virtual void set(T value) = 0;

virtual ~VariableAccess() = default;
};

template<typename T>
class PtrAccess final : public VariableAccess<T> {

public:
explicit PtrAccess(T *ptr) : ptr_(ptr) {}

T get() override {
return *ptr_;
}

void set(T value) override {
*ptr_ = value;
}

private:
T *ptr_;
};

template<typename T>
class LambdaAccess final : public VariableAccess<T> {

public:
LambdaAccess(std::function<T()> getter, std::optional<std::function<void(T)>> setter)
: getter_(std::move(getter)),
setter_(std::move(setter)) {}

T get() override {
return getter_();
}

void set(T value) override {
if (setter_) {
setter_->operator()(value);
}
}

private:
std::function<T()> getter_;
std::optional<std::function<void(T)>> setter_;
};

}// namespace fmu4cpp

#endif//FMU4CPP_VARIABLEACCESS_HPP
16 changes: 16 additions & 0 deletions export/src/fmu4cpp/fmu_base.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -195,18 +195,34 @@ namespace fmu4cpp {
return ss.str();
}

IntVariable fmu_base::integer(const std::string &name, int *ptr) {
return {name, static_cast<unsigned int>(integers_.size()), numVariables_++, ptr};
}

IntVariable fmu_base::integer(const std::string &name, const std::function<int()> &getter, const std::optional<std::function<void(int)>> &setter) {
return {name, static_cast<unsigned int>(integers_.size()), numVariables_++, getter, setter};
}

RealVariable fmu_base::real(const std::string &name, double *ptr) {
return {name, static_cast<unsigned int>(reals_.size()), numVariables_++, ptr};
}

RealVariable fmu_base::real(const std::string &name, const std::function<double()> &getter, const std::optional<std::function<void(double)>> &setter) {
return {name, static_cast<unsigned int>(reals_.size()), numVariables_++, getter, setter};
}

BoolVariable fmu_base::boolean(const std::string &name, bool *ptr) {
return {name, static_cast<unsigned int>(booleans_.size()), numVariables_++, ptr};
}

BoolVariable fmu_base::boolean(const std::string &name, const std::function<bool()> &getter, const std::optional<std::function<void(bool)>> &setter) {
return {name, static_cast<unsigned int>(booleans_.size()), numVariables_++, getter, setter};
}

StringVariable fmu_base::string(const std::string &name, std::string *ptr) {
return {name, static_cast<unsigned int>(strings_.size()), numVariables_++, ptr};
}

StringVariable fmu_base::string(const std::string &name, const std::function<std::string()> &getter, const std::optional<std::function<void(std::string)>> &setter) {
return {name, static_cast<unsigned int>(strings_.size()), numVariables_++, getter, setter};
}
Expand Down
8 changes: 4 additions & 4 deletions export/src/fmu4cpp/fmu_variable.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@ namespace fmu4cpp {
throw std::logic_error("Invalid causality encountered");
}

std::string to_string(const variability_t &c) {
switch (c) {
std::string to_string(const variability_t &v) {
switch (v) {
case variability_t::CONTINUOUS:
return "continuous";
case variability_t::CONSTANT:
Expand All @@ -37,8 +37,8 @@ namespace fmu4cpp {
throw std::logic_error("Invalid variability encountered");
}

std::string to_string(const initial_t &c) {
switch (c) {
std::string to_string(const initial_t &i) {
switch (i) {
case initial_t::APPROX:
return "approx";
case initial_t::EXACT:
Expand Down
2 changes: 2 additions & 0 deletions export/tests/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -16,5 +16,7 @@ function(make_test modelIdentifier sources)

endfunction()

make_test(array_test array_test.cpp)
make_test(basic_test basic_test.cpp)
make_test(identity_test identity_test.cpp)
make_test(test_resource test_resource.cpp)
Loading

0 comments on commit f01801e

Please sign in to comment.