Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Propagate component environment to the application and cleanup #18

Merged
merged 3 commits into from
Nov 7, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion export/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,3 @@ if (FMU4CPP_BUILD_TESTS)
enable_testing()
add_subdirectory(tests)
endif ()

10 changes: 6 additions & 4 deletions export/include/fmu4cpp/logger.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,12 @@
#include "fmi2/fmi2FunctionTypes.h"

namespace fmu4cpp {

class logger {

public:
logger(fmi2ComponentEnvironment c, fmi2CallbackFunctions f, std::string instanceName)
: c_(c),
logger(const fmi2CallbackFunctions &f, std::string instanceName)
: c_(f.componentEnvironment),
fmiLogger_(f.logger),
instanceName_(std::move(instanceName)) {}

Expand All @@ -35,10 +36,11 @@ namespace fmu4cpp {
}

private:
bool debugLogging_{false};
std::string instanceName_;
fmi2ComponentEnvironment c_;
fmi2CallbackLogger fmiLogger_;

bool debugLogging_{false};
std::string instanceName_;
std::string msgBuf_;
};

Expand Down
57 changes: 29 additions & 28 deletions export/src/fmu4cpp/fmi2.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,11 @@ namespace {
// A struct that holds all the data for one model instance.
struct Component {

Component(std::unique_ptr<fmu4cpp::fmu_base> slave, fmi2CallbackFunctions callbackFunctions)
Component(std::unique_ptr<fmu4cpp::fmu_base> slave, const fmi2CallbackFunctions &callbackFunctions)
: lastSuccessfulTime{std::numeric_limits<double>::quiet_NaN()},
slave(std::move(slave)),
logger(this->slave.get(), callbackFunctions, this->slave->instanceName()) {
logger(callbackFunctions, this->slave->instanceName()) {

this->slave->__set_logger(&logger);
}

Expand Down Expand Up @@ -81,7 +82,7 @@ fmi2Component fmi2Instantiate(fmi2String instanceName,
const auto guid = slave->guid();
if (guid != fmuGUID) {
std::cerr << "[fmu4cpp] Error. Wrong guid!" << std::endl;
fmu4cpp::logger l(nullptr, *functions, instanceName);
fmu4cpp::logger l(*functions, instanceName);
l.log(fmi2Fatal, "", "Error. Wrong guid!");
return nullptr;
}
Expand All @@ -107,9 +108,9 @@ fmi2Status fmi2SetupExperiment(fmi2Component c,
try {
component->slave->setup_experiment(startTime, stop, tol);
return fmi2OK;
} catch (const fmu4cpp::fatal_error &ex) {
} catch (const fmu4cpp::fatal_error &) {
return fmi2Fatal;
} catch (const std::exception &ex) {
} catch (const std::exception &) {
return fmi2Error;
}
}
Expand All @@ -120,9 +121,9 @@ fmi2Status fmi2EnterInitializationMode(fmi2Component c) {
try {
component->slave->enter_initialisation_mode();
return fmi2OK;
} catch (const fmu4cpp::fatal_error &ex) {
} catch (const fmu4cpp::fatal_error &) {
return fmi2Fatal;
} catch (const std::exception &ex) {
} catch (const std::exception &) {
return fmi2Error;
}
}
Expand All @@ -132,9 +133,9 @@ fmi2Status fmi2ExitInitializationMode(fmi2Component c) {
try {
component->slave->exit_initialisation_mode();
return fmi2OK;
} catch (const fmu4cpp::fatal_error &ex) {
} catch (const fmu4cpp::fatal_error &) {
return fmi2Fatal;
} catch (const std::exception &ex) {
} catch (const std::exception &) {
return fmi2Error;
}
}
Expand All @@ -144,9 +145,9 @@ fmi2Status fmi2Terminate(fmi2Component c) {
try {
component->slave->terminate();
return fmi2OK;
} catch (const fmu4cpp::fatal_error &ex) {
} catch (const fmu4cpp::fatal_error &) {
return fmi2Fatal;
} catch (const std::exception &ex) {
} catch (const std::exception &) {
return fmi2Error;
}
}
Expand Down Expand Up @@ -177,7 +178,7 @@ fmi2Status fmi2DoStep(
}
}

fmi2Status fmi2CancelStep(fmi2Component c) {
fmi2Status fmi2CancelStep(fmi2Component) {
return fmi2Error;
}

Expand All @@ -197,9 +198,9 @@ fmi2Status fmi2GetInteger(
try {
component->slave->get_integer(vr, nvr, value);
return fmi2OK;
} catch (const fmu4cpp::fatal_error &ex) {
} catch (const fmu4cpp::fatal_error &) {
return fmi2Fatal;
} catch (const std::exception &ex) {
} catch (const std::exception &) {
return fmi2Error;
}
}
Expand All @@ -214,9 +215,9 @@ fmi2Status fmi2GetReal(
try {
component->slave->get_real(vr, nvr, value);
return fmi2OK;
} catch (const fmu4cpp::fatal_error &ex) {
} catch (const fmu4cpp::fatal_error &) {
return fmi2Fatal;
} catch (const std::exception &ex) {
} catch (const std::exception &) {
return fmi2Error;
}
}
Expand All @@ -231,9 +232,9 @@ fmi2Status fmi2GetBoolean(
try {
component->slave->get_boolean(vr, nvr, value);
return fmi2OK;
} catch (const fmu4cpp::fatal_error &ex) {
} catch (const fmu4cpp::fatal_error &) {
return fmi2Fatal;
} catch (const std::exception &ex) {
} catch (const std::exception &) {
return fmi2Error;
}
}
Expand All @@ -248,9 +249,9 @@ fmi2Status fmi2GetString(
try {
component->slave->get_string(vr, nvr, value);
return fmi2OK;
} catch (const fmu4cpp::fatal_error &ex) {
} catch (const fmu4cpp::fatal_error &) {
return fmi2Fatal;
} catch (const std::exception &ex) {
} catch (const std::exception &) {
return fmi2Error;
}
}
Expand All @@ -265,9 +266,9 @@ fmi2Status fmi2SetInteger(
try {
component->slave->set_integer(vr, nvr, value);
return fmi2OK;
} catch (const fmu4cpp::fatal_error &ex) {
} catch (const fmu4cpp::fatal_error &) {
return fmi2Fatal;
} catch (const std::exception &ex) {
} catch (const std::exception &) {
return fmi2Error;
}
}
Expand All @@ -282,9 +283,9 @@ fmi2Status fmi2SetReal(
try {
component->slave->set_real(vr, nvr, value);
return fmi2OK;
} catch (const fmu4cpp::fatal_error &ex) {
} catch (const fmu4cpp::fatal_error &) {
return fmi2Fatal;
} catch (const std::exception &ex) {
} catch (const std::exception &) {
return fmi2Error;
}
}
Expand All @@ -299,9 +300,9 @@ fmi2Status fmi2SetBoolean(
try {
component->slave->set_boolean(vr, nvr, value);
return fmi2OK;
} catch (const fmu4cpp::fatal_error &ex) {
} catch (const fmu4cpp::fatal_error &) {
return fmi2Fatal;
} catch (const std::exception &ex) {
} catch (const std::exception &) {
return fmi2Error;
}
}
Expand Down Expand Up @@ -385,7 +386,7 @@ fmi2Status fmi2SetRealInputDerivatives(fmi2Component,
return fmi2Error;
}

fmi2Status fmi2GetRealOutputDerivatives(fmi2Component c,
fmi2Status fmi2GetRealOutputDerivatives(fmi2Component,
const fmi2ValueReference[], size_t,
const fmi2Integer[],
fmi2Real[]) {
Expand All @@ -404,6 +405,7 @@ fmi2Status fmi2GetDirectionalDerivative(fmi2Component,


fmi2Status fmi2GetFMUstate(fmi2Component, fmi2FMUstate *) {

return fmi2Error;
}

Expand Down Expand Up @@ -436,5 +438,4 @@ void fmi2FreeInstance(fmi2Component c) {
const auto component = static_cast<Component *>(c);
delete component;
}

}
42 changes: 21 additions & 21 deletions export/src/fmu4cpp/fmu_base.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,25 +16,26 @@ namespace {
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 &)> &f = [](auto &v) { return true; }) {
const std::function<bool(const fmu4cpp::VariableBase &)> &predicate = [](auto &v) { return true; }) {

std::vector<fmu4cpp::VariableBase> vars;
for (const fmu4cpp::VariableBase &v: v1) {
if (f(v)) {
if (predicate(v)) {
vars.push_back(v);
}
}
for (const fmu4cpp::VariableBase &v: v2) {
if (f(v)) {
if (predicate(v)) {
vars.push_back(v);
}
}
for (const fmu4cpp::VariableBase &v: v3) {
if (f(v)) {
if (predicate(v)) {
vars.push_back(v);
}
}
for (const fmu4cpp::VariableBase &v: v4) {
if (f(v)) {
if (predicate(v)) {
vars.push_back(v);
}
}
Expand Down Expand Up @@ -62,9 +63,8 @@ namespace fmu4cpp {

std::string fmu_base::make_description() const {

const model_info m = get_model_info();
std::stringstream ss;
model_info m = get_model_info();

ss << R"(<?xml version="1.0" encoding="UTF-8"?>)"
<< "\n"
<< R"(<fmiModelDescription fmiVersion="2.0")"
Expand Down Expand Up @@ -93,8 +93,8 @@ namespace fmu4cpp {
ss << "\t<ModelVariables>\n";

for (const auto &v: integers_) {
auto variability = v.variability();
auto initial = v.initial();
const auto variability = v.variability();
const auto initial = v.initial();
ss << "\t\t<ScalarVariable name=\""
<< v.name() << "\" valueReference=\"" << v.value_reference() << "\""
<< " causality=\"" << to_string(v.causality()) << "\"";
Expand All @@ -115,8 +115,8 @@ namespace fmu4cpp {
}

for (const auto &v: reals_) {
auto variability = v.variability();
auto initial = v.initial();
const auto variability = v.variability();
const auto initial = v.initial();
ss << "\t\t<ScalarVariable name=\""
<< v.name() << "\" valueReference=\"" << v.value_reference() << "\""
<< " causality=\"" << to_string(v.causality()) << "\"";
Expand All @@ -131,8 +131,8 @@ namespace fmu4cpp {
if (requires_start(v)) {
ss << " start=\"" << v.get() << "\"";
}
auto min = v.getMin();
auto max = v.getMax();
const auto min = v.getMin();
const auto max = v.getMax();
if (min && max) {
ss << " min=\"" << *min << "\" max=\"" << *max << "\"";
}
Expand All @@ -142,8 +142,8 @@ namespace fmu4cpp {
}

for (const auto &v: booleans_) {
auto variability = v.variability();
auto initial = v.initial();
const auto variability = v.variability();
const auto initial = v.initial();
ss << "\t\t<ScalarVariable name=\""
<< v.name() << "\" valueReference=\"" << v.value_reference() << "\""
<< " causality=\"" << to_string(v.causality()) << "\"";
Expand All @@ -164,8 +164,8 @@ namespace fmu4cpp {
}

for (const auto &v: strings_) {
auto variability = v.variability();
auto initial = v.initial();
const auto variability = v.variability();
const auto initial = v.initial();
ss << "\t\t<ScalarVariable name=\""
<< v.name() << "\" valueReference=\"" << v.value_reference() << "\""
<< " causality=\"" << to_string(v.causality()) << "\"";
Expand Down Expand Up @@ -197,7 +197,7 @@ namespace fmu4cpp {
ss << "\t\t<Outputs>\n";
for (const auto &v: outputs) {
ss << "\t\t\t<Unknown index=\"" << v.index() << "\"";
auto deps = v.getDependencies();
const auto deps = v.getDependencies();
if (!deps.empty()) {
ss << " dependencies=\"";
for (unsigned i = 0; i < deps.size(); i++) {
Expand Down Expand Up @@ -253,8 +253,8 @@ namespace fmu4cpp {
}

[[maybe_unused]] std::string fmu_base::guid() const {
model_info info = get_model_info();
std::vector<std::string> content{
const model_info info = get_model_info();
const std::vector content{
info.author,
info.version,
info.modelIdentifier,
Expand All @@ -266,7 +266,7 @@ namespace fmu4cpp {
ss << str;
}

auto vars = collect(integers_, reals_, booleans_, strings_);
const auto vars = collect(integers_, reals_, booleans_, strings_);
for (const auto &v: vars) {
ss << v.name();
ss << std::to_string(v.index());
Expand Down
Loading