Skip to content

Commit 1474718

Browse files
committed
apply changes from #37
and some refactoring
1 parent f9aa073 commit 1474718

12 files changed

Lines changed: 87 additions & 89 deletions

File tree

README.md

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -26,14 +26,15 @@ using namespace fmu4cpp;
2626
class BouncingBall : public fmu_base {
2727

2828
public:
29-
BouncingBall(const std::string &instanceName, const std::string &resources)
30-
: fmu_base(instanceName, resources) {
29+
BouncingBall(const fmu_data& data)
30+
: fmu_base(data) {
3131

3232
register_variable(
3333
real(
3434
"height", &height)
3535
.setCausality(causality_t::OUTPUT)
36-
.setVariability(variability_t::CONTINUOUS));
36+
.setVariability(variability_t::CONTINUOUS))
37+
.setInitial(initial_t::EXACT));
3738

3839
register_variable(
3940
real(
@@ -80,10 +81,10 @@ public:
8081
}
8182

8283
private:
83-
double height; // Current height of the ball
84-
double velocity; // Current velocity of the ball
85-
double gravity; // Acceleration due to gravity
86-
double bounceFactor;// Factor to reduce velocity on bounce
84+
double height{}; // Current height of the ball
85+
double velocity{}; // Current velocity of the ball
86+
double gravity{}; // Acceleration due to gravity
87+
double bounceFactor{};// Factor to reduce velocity on bounce
8788
};
8889

8990
model_info fmu4cpp::get_model_info() {

export/examples/BouncingBall/bouncing_ball.cpp

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,7 @@ using namespace fmu4cpp;
1010
class BouncingBall : public fmu_base {
1111

1212
public:
13-
BouncingBall(const std::string &instanceName, const std::filesystem::path &resources)
14-
: fmu_base(instanceName, resources) {
13+
explicit BouncingBall(const fmu_data &data) : fmu_base(data) {
1514

1615
register_variable(
1716
real(
@@ -71,10 +70,10 @@ class BouncingBall : public fmu_base {
7170
}
7271

7372
private:
74-
double height_; // Current height of the ball
75-
double velocity_; // Current velocity of the ball
76-
double gravity_; // Acceleration due to gravity
77-
double bounceFactor_; // Factor to reduce velocity on bounce
73+
double height_{}; // Current height of the ball
74+
double velocity_{}; // Current velocity of the ball
75+
double gravity_{}; // Acceleration due to gravity
76+
double bounceFactor_{};// Factor to reduce velocity on bounce
7877
};
7978

8079
model_info fmu4cpp::get_model_info() {

export/examples/Resource/resource.cpp

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,9 @@ using namespace fmu4cpp;
1010
class Resource : public fmu_base {
1111

1212
public:
13-
Resource(const std::string &instanceName, const std::filesystem::path &resources)
14-
: fmu_base(instanceName, resources) {
13+
explicit Resource(const fmu_data &data) : fmu_base(data) {
1514

16-
std::ifstream ifs(resources / "file.txt");
15+
std::ifstream ifs(resourceLocation() / "file.txt");
1716

1817
std::getline(ifs, content_);
1918

@@ -38,7 +37,6 @@ class Resource : public fmu_base {
3837

3938
private:
4039
std::string content_;
41-
4240
};
4341

4442
model_info fmu4cpp::get_model_info() {

export/examples/SimplePendulum/simple_pendulum.cpp

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,9 @@ using namespace fmu4cpp;
88
constexpr double pi = 3.14159265358979323846;
99

1010
class SimplePendulum : public fmu_base {
11+
1112
public:
12-
SimplePendulum(const std::string &instanceName, const std::filesystem::path &resources)
13-
: fmu_base(instanceName, resources) {
13+
explicit SimplePendulum(const fmu_data &data) : fmu_base(data) {
1414

1515
register_variable(real("angle", &angle_)
1616
.setCausality(causality_t::OUTPUT)
@@ -51,11 +51,11 @@ class SimplePendulum : public fmu_base {
5151
}
5252

5353
private:
54-
double angle_; // Current angle of the pendulum
55-
double angularVelocity_;// Current angular velocity
56-
double gravity_; // Acceleration due to gravity
57-
double length_; // Length of the pendulum
58-
double damping_; // Damping factor
54+
double angle_{}; // Current angle of the pendulum
55+
double angularVelocity_{};// Current angular velocity
56+
double gravity_{}; // Acceleration due to gravity
57+
double length_{}; // Length of the pendulum
58+
double damping_{}; // Damping factor
5959
};
6060

6161

export/include/fmu4cpp/fmu_base.hpp

Lines changed: 20 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -16,19 +16,24 @@
1616

1717
#include <filesystem>
1818

19-
#define FMU4CPP_INSTANTIATE(MODELCLASS) \
20-
std::unique_ptr<fmu4cpp::fmu_base> fmu4cpp::createInstance(const std::string &instanceName, \
21-
const std::filesystem::path &fmuResourceLocation) { \
22-
return std::make_unique<MODELCLASS>(instanceName, fmuResourceLocation); \
19+
#define FMU4CPP_INSTANTIATE(MODELCLASS) \
20+
std::unique_ptr<fmu4cpp::fmu_base> fmu4cpp::createInstance(const fmu4cpp::fmu_data &data) { \
21+
return std::make_unique<MODELCLASS>(data); \
2322
}
2423

2524
namespace fmu4cpp {
2625

26+
struct fmu_data {
27+
logger *logger{nullptr};
28+
std::string instance_name{};
29+
std::filesystem::path resourceLocation{};
30+
};
31+
2732
class fmu_base {
2833

2934
public:
30-
fmu_base(std::string instance_name, std::filesystem::path resourceLocation)
31-
: instanceName_(std::move(instance_name)), resourceLocation_(std::move(resourceLocation)) {
35+
explicit fmu_base(fmu_data data)
36+
: data_(std::move(data)) {
3237

3338
register_variable(real("time", &time_)
3439
.setCausality(causality_t::INDEPENDENT)
@@ -39,11 +44,11 @@ namespace fmu4cpp {
3944
fmu_base(const fmu_base &&) = delete;
4045

4146
[[nodiscard]] std::string instanceName() const {
42-
return instanceName_;
47+
return data_.instance_name;
4348
}
4449

4550
[[nodiscard]] const std::filesystem::path &resourceLocation() const {
46-
return resourceLocation_;
51+
return data_.resourceLocation;
4752
}
4853

4954
[[nodiscard]] std::optional<IntVariable> get_int_variable(const std::string &name) const {
@@ -200,13 +205,9 @@ namespace fmu4cpp {
200205

201206
[[nodiscard]] std::string make_description_v3() const;
202207

203-
void __set_logger(logger *logger) {
204-
logger_ = logger;
205-
}
206-
207208
void log(const fmiStatus s, const std::string &message) const {
208-
if (logger_) {
209-
logger_->log(s, message);
209+
if (data_.logger) {
210+
data_.logger->log(s, message);
210211
}
211212
}
212213

@@ -265,15 +266,13 @@ namespace fmu4cpp {
265266
}
266267

267268
private:
268-
double time_{0};
269-
std::optional<double> stop_;
270-
std::optional<double> tolerance_;
269+
fmu_data data_;
271270

272-
logger *logger_ = nullptr;
271+
double time_{0};
273272
size_t numVariables_{0};
274273

275-
std::string instanceName_;
276-
std::filesystem::path resourceLocation_;
274+
std::optional<double> stop_;
275+
std::optional<double> tolerance_;
277276

278277
std::vector<IntVariable> integers_;
279278
std::unordered_map<unsigned int, size_t> vrToIntegerIndices_;
@@ -291,7 +290,7 @@ namespace fmu4cpp {
291290

292291
model_info get_model_info();
293292

294-
std::unique_ptr<fmu_base> createInstance(const std::string &instanceName, const std::filesystem::path &fmuResourceLocation);
293+
std::unique_ptr<fmu_base> createInstance(const fmu_data &data);
295294

296295
}// namespace fmu4cpp
297296

export/src/fmu4cpp/fmi2.cpp

Lines changed: 20 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -51,16 +51,15 @@ namespace {
5151
// A struct that holds all the data for one model instance.
5252
struct Fmi2Component {
5353

54-
Fmi2Component(std::unique_ptr<fmu4cpp::fmu_base> slave, const fmi2CallbackFunctions *callbackFunctions)
54+
Fmi2Component(std::unique_ptr<fmu4cpp::fmu_base> slave, std::unique_ptr<fmi2Logger> logger)
5555
: lastSuccessfulTime{std::numeric_limits<double>::quiet_NaN()},
5656
slave(std::move(slave)),
57-
logger(std::make_unique<fmi2Logger>(this->slave->instanceName(), callbackFunctions)) {
58-
this->slave->__set_logger(logger.get());
59-
}
57+
logger(std::move(logger)) {}
6058

6159
double lastSuccessfulTime{0};
60+
6261
std::unique_ptr<fmu4cpp::fmu_base> slave;
63-
std::unique_ptr<fmu4cpp::logger> logger;
62+
std::unique_ptr<fmi2Logger> logger;
6463

6564
double start{0};
6665
std::optional<double> stop;
@@ -80,7 +79,7 @@ const char *fmi2GetVersion(void) {
8079
}
8180

8281
FMI2_Export void write_description(const char *location) {
83-
const auto instance = fmu4cpp::createInstance("", "");
82+
const auto instance = fmu4cpp::createInstance({});
8483
const auto xml = instance->make_description_v2();
8584
std::ofstream of(location);
8685
of << xml;
@@ -117,18 +116,27 @@ fmi2Component fmi2Instantiate(fmi2String instanceName,
117116
resources.replace(0, 6 - magic, "");
118117
}
119118

120-
auto slave = fmu4cpp::createInstance(instanceName, resources);
119+
auto logger = std::make_unique<fmi2Logger>(instanceName, functions);
120+
121+
auto slave = fmu4cpp::createInstance({logger.get(), instanceName, resources});
121122
const auto guid = slave->guid();
122123
if (guid != fmuGUID) {
123124
std::cerr << "[fmu4cpp] Error. Wrong guid!" << std::endl;
124-
fmi2Logger l(instanceName, functions);
125-
l.log(fmiFatal, "Error. Wrong guid!");
125+
logger->log(fmiFatal, "Error. Wrong guid!");
126126
return nullptr;
127127
}
128128

129-
auto c = std::make_unique<Fmi2Component>(std::move(slave), functions);
130-
c->logger->setDebugLogging(loggingOn);
131-
return c.release();
129+
try {
130+
auto c = std::make_unique<Fmi2Component>(std::move(slave), std::move(logger));
131+
c->logger->setDebugLogging(loggingOn);
132+
133+
return c.release();
134+
} catch (const std::exception &e) {
135+
136+
logger->log(fmiFatal, "Unable to instantiate model! " + std::string(e.what()));
137+
138+
return nullptr;
139+
}
132140
}
133141

134142
fmi2Status fmi2SetupExperiment(fmi2Component c,

export/src/fmu4cpp/fmi3.cpp

Lines changed: 10 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -57,17 +57,14 @@ namespace {
5757
Invalid = 1 << 4
5858
};
5959

60-
Fmi3Component(std::unique_ptr<fmu4cpp::fmu_base> slave, fmi3InstanceEnvironment env, fmi3LogMessageCallback logCallback)
60+
Fmi3Component(std::unique_ptr<fmu4cpp::fmu_base> slave, std::unique_ptr<fmi3Logger> logger)
6161
: state(State::Instantiated),
6262
slave(std::move(slave)),
63-
logger(std::make_unique<fmi3Logger>(env, logCallback, this->slave->instanceName())) {
64-
65-
this->slave->__set_logger(logger.get());
66-
}
63+
logger(std::move(logger)) { }
6764

6865
State state;
6966
std::unique_ptr<fmu4cpp::fmu_base> slave;
70-
std::unique_ptr<fmu4cpp::logger> logger;
67+
std::unique_ptr<fmi3Logger> logger;
7168
};
7269

7370
#define FMU_TYPE(type) fmi3##type
@@ -105,7 +102,7 @@ const char *fmi3GetVersion(void) {
105102
}
106103

107104
FMI3_Export void write_description(const char *location) {
108-
const auto instance = fmu4cpp::createInstance("", "");
105+
const auto instance = fmu4cpp::createInstance({});
109106
const auto xml = instance->make_description_v3();
110107
std::ofstream of(location);
111108
of << xml;
@@ -175,24 +172,24 @@ fmi3Instance fmi3InstantiateCoSimulation(
175172
resources.replace(0, 6 - magic, "");
176173
}
177174

178-
auto slave = fmu4cpp::createInstance(instanceName, resources);
175+
auto logger = std::make_unique<fmi3Logger>(instanceEnvironment, logMessage, instanceName);
176+
177+
auto slave = fmu4cpp::createInstance({logger.get(), instanceName, resources});
179178
const auto guid = slave->guid();
180179
if (guid != instantiationToken) {
181180
std::cerr << "[fmu4cpp] Error. Wrong guid!" << std::endl;
182-
fmi3Logger l(instanceEnvironment, logMessage, instanceName);
183-
l.log(fmiFatal, "Error. Wrong guid!");
181+
logger->log(fmiFatal, "Error. Wrong guid!");
184182
return nullptr;
185183
}
186184

187185
try {
188-
auto c = std::make_unique<Fmi3Component>(std::move(slave), instanceEnvironment, logMessage);
186+
auto c = std::make_unique<Fmi3Component>(std::move(slave), std::move(logger));
189187
c->logger->setDebugLogging(loggingOn);
190188

191189
return c.release();
192190
} catch (const std::exception &e) {
193191

194-
fmi3Logger l(instanceEnvironment, logMessage, instanceName);
195-
l.log(fmiFatal, "Unable to instantiate model! " + std::string(e.what()));
192+
logger->log(fmiFatal, "Unable to instantiate model! " + std::string(e.what()));
196193

197194
return nullptr;
198195
}

export/tests/array_test.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,8 @@
1313
class Model : public fmu4cpp::fmu_base {
1414

1515
public:
16-
Model(const std::string &instanceName, const std::filesystem::path &resources)
17-
: fmu_base(instanceName, resources), reals_(4) {
16+
explicit Model(const fmu4cpp::fmu_data &data)
17+
: fmu_base(data), reals_(4) {
1818

1919
for (int i = 0; i < reals_.size(); i++) {
2020
register_variable(
@@ -58,7 +58,7 @@ void fmilogger(fmi2Component, fmi2String instanceName, fmi2Status status, fmi2St
5858

5959
TEST_CASE("test_array") {
6060

61-
Model model("", "");
61+
Model model({});
6262
const auto guid = model.guid();
6363

6464
std::cout << model.make_description_v2() << std::endl;

export/tests/basic_test.cpp

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,7 @@
77
class Model : public fmu4cpp::fmu_base {
88

99
public:
10-
Model(const std::string &instanceName, const std::filesystem::path &resources)
11-
: fmu_base(instanceName, resources) {
10+
explicit Model(const fmu4cpp::fmu_data &data) : fmu_base(data) {
1211

1312
register_variable(real("myReal", &real_)
1413
.setCausality(fmu4cpp::causality_t::OUTPUT));
@@ -55,7 +54,7 @@ FMU4CPP_INSTANTIATE(Model);
5554

5655
TEST_CASE("basic_test") {
5756

58-
const auto instance = fmu4cpp::createInstance("", "");
57+
const auto instance = fmu4cpp::createInstance({});
5958

6059
double t = 0;
6160
const double dt = 0.1;

export/tests/identity_test.cpp

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,7 @@
1212
class Model : public fmu4cpp::fmu_base {
1313

1414
public:
15-
Model(const std::string &instanceName, const std::filesystem::path &resources)
16-
: fmu_base(instanceName, resources) {
15+
explicit Model(const fmu4cpp::fmu_data &data) : fmu_base(data) {
1716

1817
register_variable(integer("integerIn", &integer_)
1918
.setCausality(fmu4cpp::causality_t::INPUT)
@@ -159,7 +158,7 @@ void fmilogger(fmi2Component, fmi2String instanceName, fmi2Status status, fmi2St
159158

160159
TEST_CASE("test_identity") {
161160

162-
Model model("", "");
161+
Model model({});
163162
const auto guid = model.guid();
164163

165164
const auto realIn = model.get_real_variable("realIn");

0 commit comments

Comments
 (0)