Skip to content

Commit

Permalink
update
Browse files Browse the repository at this point in the history
  • Loading branch information
markaren committed Nov 8, 2024
1 parent 960379e commit 5d932f9
Show file tree
Hide file tree
Showing 4 changed files with 65 additions and 39 deletions.
49 changes: 25 additions & 24 deletions export/examples/BouncingBall/bouncing_ball.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,62 +12,63 @@ class BouncingBall : public fmu_base {

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

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

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

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


BouncingBall::reset();
}

bool do_step(double currentTime, double dt) override {
// Update velocity with gravity
velocity += gravity * dt;
velocity_ += gravity_ * dt;
// Update height with current velocity
height += velocity * dt;
height_ += velocity_ * dt;

// Check for bounce
if (height <= 0.0f) {
height = 0.0f; // Reset height to ground level
velocity = -velocity * bounceFactor;// Reverse velocity and apply bounce factor
if (height_ <= 0.0f) {
height_ = 0.0f; // Reset height to ground level
velocity_ = -velocity_ * bounceFactor_;// Reverse velocity and apply bounce factor
}

return true;
}

void reset() override {
height = 10;
velocity = 0;
gravity = -9.81f;
bounceFactor = 0.6f;
height_ = 10;
velocity_ = 0;
gravity_ = -9.81f;
bounceFactor_ = 0.6f;
}

private:
double height; // Current height of the ball
double velocity; // Current velocity of the ball
double gravity; // Acceleration due to gravity
double bounceFactor;// Factor to reduce velocity on bounce
double height_; // Current height of the ball
double velocity_; // Current velocity of the ball
double gravity_; // Acceleration due to gravity
double bounceFactor_;// Factor to reduce velocity on bounce
};

model_info fmu4cpp::get_model_info() {
Expand Down
5 changes: 4 additions & 1 deletion export/include/fmu4cpp/fmu_variable.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,10 @@ namespace fmu4cpp {
unsigned int vr, size_t index,
const std::function<double()> &getter,
const std::optional<std::function<void(double)>> &setter)
: Variable(name, vr, index, getter, setter) {}
: Variable(name, vr, index, getter, setter) {

variability_ = variability_t::CONTINUOUS;
}

[[nodiscard]] std::optional<double> getMin() const {
return min_;
Expand Down
34 changes: 24 additions & 10 deletions export/src/fmu4cpp/fmu_base.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,14 @@

namespace {

std::vector<const fmu4cpp::VariableBase*> collect(
std::vector<const fmu4cpp::VariableBase *> collect(
const std::vector<fmu4cpp::IntVariable> &v1,
const std::vector<fmu4cpp::RealVariable> &v2,
const std::vector<fmu4cpp::BoolVariable> &v3,
const std::vector<fmu4cpp::StringVariable> &v4,
const std::function<bool(const fmu4cpp::VariableBase &)> &predicate = [](auto &v) { return true; }) {

std::vector<const fmu4cpp::VariableBase*> vars;
std::vector<const fmu4cpp::VariableBase *> vars;
for (const fmu4cpp::VariableBase &v: v1) {
if (predicate(v)) {
vars.push_back(&v);
Expand Down Expand Up @@ -96,7 +96,7 @@ namespace fmu4cpp {
ss << "\t<ModelVariables>\n";

auto allVars = collect(integers_, reals_, booleans_, strings_);
std::sort(allVars.begin(), allVars.end(), [](const VariableBase* v1, const VariableBase* v2) {
std::sort(allVars.begin(), allVars.end(), [](const VariableBase *v1, const VariableBase *v2) {
return v1->index() < v2->index();
});

Expand All @@ -115,12 +115,12 @@ namespace fmu4cpp {
ss << " initial=\"" << to_string(*initial) << "\"";
}
ss << ">\n";
if (auto i = dynamic_cast<const IntVariable*>(v)) {
if (auto i = dynamic_cast<const IntVariable *>(v)) {
ss << "\t\t\t<Integer";
if (requires_start(*v)) {
ss << " start=\"" << i->get() << "\"";
}
} else if (auto r = dynamic_cast<const RealVariable*>(v)) {
} else if (auto r = dynamic_cast<const RealVariable *>(v)) {
ss << "\t\t\t<Real";
if (requires_start(*v)) {
ss << " start=\"" << r->get() << "\"";
Expand All @@ -130,12 +130,12 @@ namespace fmu4cpp {
if (min && max) {
ss << " min=\"" << *min << "\" max=\"" << *max << "\"";
}
} else if (auto s = dynamic_cast<const StringVariable*>(v)) {
} else if (auto s = dynamic_cast<const StringVariable *>(v)) {
ss << "\t\t\t<String";
if (requires_start(*v)) {
ss << " start=\"" << s->get() << "\"";
}
} else if (auto b = dynamic_cast<const BoolVariable*>(v)) {
} else if (auto b = dynamic_cast<const BoolVariable *>(v)) {
ss << "\t\t\t<Boolean";
if (requires_start(*v)) {
ss << " start=\"" << b->get() << "\"";
Expand All @@ -150,13 +150,13 @@ namespace fmu4cpp {

ss << "\t<ModelStructure>\n";

std::vector<const VariableBase*> outputs = collect(integers_, reals_, booleans_, strings_, [](auto &v) {
std::vector<const VariableBase *> unknowns = collect(integers_, reals_, booleans_, strings_, [](auto &v) {
return v.causality() == causality_t::OUTPUT;
});

if (!outputs.empty()) {
if (!unknowns.empty()) {
ss << "\t\t<Outputs>\n";
for (const auto &v: outputs) {
for (const auto &v: unknowns) {
ss << "\t\t\t<Unknown index=\"" << v->index() << "\"";
const auto deps = v->getDependencies();
if (!deps.empty()) {
Expand All @@ -174,6 +174,20 @@ namespace fmu4cpp {
ss << "\t\t</Outputs>\n";
}


std::vector<const VariableBase *> initialUnknowns = collect(integers_, reals_, booleans_, strings_, [](auto &v) {
return (v.causality() == causality_t::OUTPUT && v.initial() == initial_t::APPROX || v.initial() == initial_t::CALCULATED) || v.causality() == causality_t::CALCULATED_PARAMETER;
});
if (!initialUnknowns.empty()) {
ss << "\t\t<InitialUnknowns>\n";
for (const auto &v: initialUnknowns) {
ss << "\t\t\t<Unknown index=\"" << v->index() << "\"";
ss << "/>\n";
}
ss << "\t\t</InitialUnknowns>\n";
}


ss << "\t</ModelStructure>\n";

ss << "</fmiModelDescription>";
Expand Down
16 changes: 12 additions & 4 deletions src/model.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,46 +18,54 @@ class Model : public fmu_base {
integer(
"integerIn", [this] { return integer_; }, [this](int value) { integer_ = value; })
.setCausality(causality_t::INPUT)
.setVariability(variability_t::DISCRETE));
.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));
.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));
.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));
.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

0 comments on commit 5d932f9

Please sign in to comment.