Skip to content

Commit 3ef1c66

Browse files
committed
tweaks and updates
1 parent 09fcf78 commit 3ef1c66

12 files changed

Lines changed: 144 additions & 136 deletions

File tree

export/examples/BouncingBall/bouncing_ball.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ class BouncingBall : public fmu_base {
4242
BouncingBall::reset();
4343
}
4444

45-
bool do_step(double currentTime, double dt) override {
45+
bool do_step(double dt) override {
4646
// Update velocity with gravity
4747
velocity_ += gravity_ * dt;
4848
// Update height with current velocity

export/examples/Resource/resource.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ class Resource : public fmu_base {
2626
Resource::reset();
2727
}
2828

29-
bool do_step(double currentTime, double dt) override {
29+
bool do_step(double dt) override {
3030

3131
log(fmiOK, get_string_variable("content")->get());
3232
return true;

export/examples/SimplePendulum/simple_pendulum.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ class SimplePendulum : public fmu_base {
3636
SimplePendulum::reset();
3737
}
3838

39-
bool do_step(double currentTime, double dt) override {
39+
bool do_step(double dt) override {
4040
angularVelocity_ += (-gravity_ / length_) * std::sin(angle_) * dt - damping_ * angularVelocity_ * dt;
4141
angle_ += angularVelocity_ * dt;
4242
return true;

export/include/fmu4cpp/fmu_base.hpp

Lines changed: 22 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -68,19 +68,27 @@ namespace fmu4cpp {
6868
return std::nullopt;
6969
}
7070

71-
virtual void setup_experiment(double start, std::optional<double> stop, std::optional<double> tolerance);
72-
73-
virtual void enter_initialisation_mode();
71+
void enter_initialisation_mode(double start, std::optional<double> stop, std::optional<double> tolerance) {
72+
enter_initialisation_mode();
73+
}
7474

7575
virtual void exit_initialisation_mode();
7676

7777
bool step(double currentTime, double dt) {
78-
if (do_step(currentTime, dt)) {
78+
79+
constexpr double TIME_TOLERANCE = 1e-9;
80+
if (std::abs(currentTime - time_) > TIME_TOLERANCE) {
81+
throw std::runtime_error("Current time does not match the internal time (within tolerance)");
82+
}
83+
84+
if (do_step(dt)) {
7985
time_ += dt;
86+
87+
return true;
8088
}
81-
}
8289

83-
virtual bool do_step(double currentTime, double dt) = 0;
90+
return false;
91+
}
8492

8593
virtual void terminate();
8694

@@ -172,7 +180,7 @@ namespace fmu4cpp {
172180
logger_ = logger;
173181
}
174182

175-
void log(const fmiStatus s, const std::string &message) {
183+
void log(const fmiStatus s, const std::string &message) const {
176184
if (logger_) {
177185
logger_->log(s, message);
178186
}
@@ -193,10 +201,10 @@ namespace fmu4cpp {
193201
return false;
194202
}
195203

196-
197204
virtual ~fmu_base() = default;
198205

199206
protected:
207+
200208
IntVariable integer(const std::string &name, int *ptr);
201209
IntVariable integer(const std::string &name,
202210
const std::function<int()> &getter,
@@ -222,6 +230,12 @@ namespace fmu4cpp {
222230
void register_variable(BoolVariable v);
223231
void register_variable(StringVariable v);
224232

233+
virtual void enter_initialisation_mode();
234+
virtual bool do_step(double dt) = 0;
235+
236+
[[nodiscard]] double currentTime() const {
237+
return time_;
238+
}
225239

226240
private:
227241
double time_{0};

export/src/fmu4cpp/fmi2.cpp

Lines changed: 39 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -54,32 +54,36 @@ namespace {
5454
class fmi2Logger : public fmu4cpp::logger {
5555

5656
public:
57-
explicit fmi2Logger(const std::string &instanceName, const fmi2CallbackFunctions* f)
57+
explicit fmi2Logger(const std::string &instanceName, const fmi2CallbackFunctions *f)
5858
: logger(instanceName), f_(f) {}
5959

6060
protected:
6161
void debugLog(fmiStatus s, const std::string &message) override {
6262
f_->logger(f_->componentEnvironment, instanceName_.c_str(),
63-
toFmi2StatusFromCommon(s), nullptr, message.c_str());
63+
toFmi2StatusFromCommon(s), nullptr, message.c_str());
6464
}
6565

6666
private:
67-
const fmi2CallbackFunctions* f_;
67+
const fmi2CallbackFunctions *f_;
6868
};
6969

7070
// A struct that holds all the data for one model instance.
71-
struct Component {
71+
struct Fmi2Component {
7272

73-
Component(std::unique_ptr<fmu4cpp::fmu_base> slave, const fmi2CallbackFunctions* callbackFunctions)
73+
Fmi2Component(std::unique_ptr<fmu4cpp::fmu_base> slave, const fmi2CallbackFunctions *callbackFunctions)
7474
: lastSuccessfulTime{std::numeric_limits<double>::quiet_NaN()},
7575
slave(std::move(slave)),
7676
logger(std::make_unique<fmi2Logger>(this->slave->instanceName(), callbackFunctions)) {
7777
this->slave->__set_logger(logger.get());
7878
}
7979

80-
double lastSuccessfulTime;
80+
double lastSuccessfulTime{0};
8181
std::unique_ptr<fmu4cpp::fmu_base> slave;
8282
std::unique_ptr<fmu4cpp::logger> logger;
83+
84+
double start{0};
85+
std::optional<double> stop;
86+
std::optional<double> tolerance;
8387
};
8488

8589
}// namespace
@@ -141,7 +145,7 @@ fmi2Component fmi2Instantiate(fmi2String instanceName,
141145
return nullptr;
142146
}
143147

144-
auto c = std::make_unique<Component>(std::move(slave), functions);
148+
auto c = std::make_unique<Fmi2Component>(std::move(slave), functions);
145149
c->logger->setDebugLogging(loggingOn);
146150
return c.release();
147151
}
@@ -158,24 +162,20 @@ fmi2Status fmi2SetupExperiment(fmi2Component c,
158162
if (stopTimeDefined) stop = stopTime;
159163
if (toleranceDefined) tol = tolerance;
160164

161-
const auto component = static_cast<Component *>(c);
162-
try {
163-
component->slave->setup_experiment(startTime, stop, tol);
164-
return fmi2OK;
165-
} catch (const fmu4cpp::fatal_error &ex) {
166-
component->logger->log(toCommonStatusFromFmi2(fmi2Fatal), ex.what());
167-
return fmi2Fatal;
168-
} catch (const std::exception &ex) {
169-
component->logger->log(toCommonStatusFromFmi2(fmi2Error), ex.what());
170-
return fmi2Error;
171-
}
165+
const auto component = static_cast<Fmi2Component *>(c);
166+
167+
component->start = startTime;
168+
component->stop = stop;
169+
component->tolerance = tol;
170+
171+
return fmi2OK;
172172
}
173173

174174
fmi2Status fmi2EnterInitializationMode(fmi2Component c) {
175-
const auto component = static_cast<Component *>(c);
175+
const auto component = static_cast<Fmi2Component *>(c);
176176

177177
try {
178-
component->slave->enter_initialisation_mode();
178+
component->slave->enter_initialisation_mode(component->start, component->stop, component->tolerance);
179179
return fmi2OK;
180180
} catch (const fmu4cpp::fatal_error &ex) {
181181
component->logger->log(toCommonStatusFromFmi2(fmi2Fatal), ex.what());
@@ -187,7 +187,7 @@ fmi2Status fmi2EnterInitializationMode(fmi2Component c) {
187187
}
188188

189189
fmi2Status fmi2ExitInitializationMode(fmi2Component c) {
190-
const auto component = static_cast<Component *>(c);
190+
const auto component = static_cast<Fmi2Component *>(c);
191191
try {
192192
component->slave->exit_initialisation_mode();
193193
return fmi2OK;
@@ -201,7 +201,7 @@ fmi2Status fmi2ExitInitializationMode(fmi2Component c) {
201201
}
202202

203203
fmi2Status fmi2Terminate(fmi2Component c) {
204-
const auto component = static_cast<Component *>(c);
204+
const auto component = static_cast<Fmi2Component *>(c);
205205
try {
206206
component->slave->terminate();
207207
return fmi2OK;
@@ -220,9 +220,9 @@ fmi2Status fmi2DoStep(
220220
fmi2Real communicationStepSize,
221221
fmi2Boolean /*noSetFMUStatePriorToCurrentPoint*/) {
222222

223-
const auto component = static_cast<Component *>(c);
223+
const auto component = static_cast<Fmi2Component *>(c);
224224
try {
225-
if (component->slave->do_step(currentCommunicationPoint, communicationStepSize)) {
225+
if (component->slave->step(currentCommunicationPoint, communicationStepSize)) {
226226
component->lastSuccessfulTime = currentCommunicationPoint + communicationStepSize;
227227
return fmi2OK;
228228
}
@@ -242,7 +242,7 @@ fmi2Status fmi2CancelStep(fmi2Component) {
242242
}
243243

244244
fmi2Status fmi2Reset(fmi2Component c) {
245-
const auto component = static_cast<Component *>(c);
245+
const auto component = static_cast<Fmi2Component *>(c);
246246
component->slave->reset();
247247
return fmi2OK;
248248
}
@@ -253,7 +253,7 @@ fmi2Status fmi2GetInteger(
253253
size_t nvr,
254254
fmi2Integer value[]) {
255255

256-
const auto component = static_cast<Component *>(c);
256+
const auto component = static_cast<Fmi2Component *>(c);
257257
try {
258258
component->slave->get_integer(vr, nvr, value);
259259
return fmi2OK;
@@ -272,7 +272,7 @@ fmi2Status fmi2GetReal(
272272
size_t nvr,
273273
fmi2Real value[]) {
274274

275-
const auto component = static_cast<Component *>(c);
275+
const auto component = static_cast<Fmi2Component *>(c);
276276
try {
277277
component->slave->get_real(vr, nvr, value);
278278
return fmi2OK;
@@ -291,7 +291,7 @@ fmi2Status fmi2GetBoolean(
291291
size_t nvr,
292292
fmi2Boolean value[]) {
293293

294-
const auto component = static_cast<Component *>(c);
294+
const auto component = static_cast<Fmi2Component *>(c);
295295
try {
296296
component->slave->get_boolean(vr, nvr, value);
297297
return fmi2OK;
@@ -310,7 +310,7 @@ fmi2Status fmi2GetString(
310310
size_t nvr,
311311
fmi2String value[]) {
312312

313-
const auto component = static_cast<Component *>(c);
313+
const auto component = static_cast<Fmi2Component *>(c);
314314
try {
315315
component->slave->get_string(vr, nvr, value);
316316
return fmi2OK;
@@ -329,7 +329,7 @@ fmi2Status fmi2SetInteger(
329329
size_t nvr,
330330
const fmi2Integer value[]) {
331331

332-
const auto component = static_cast<Component *>(c);
332+
const auto component = static_cast<Fmi2Component *>(c);
333333
try {
334334
component->slave->set_integer(vr, nvr, value);
335335
return fmi2OK;
@@ -348,7 +348,7 @@ fmi2Status fmi2SetReal(
348348
size_t nvr,
349349
const fmi2Real value[]) {
350350

351-
const auto component = static_cast<Component *>(c);
351+
const auto component = static_cast<Fmi2Component *>(c);
352352
try {
353353
component->slave->set_real(vr, nvr, value);
354354
return fmi2OK;
@@ -367,7 +367,7 @@ fmi2Status fmi2SetBoolean(
367367
size_t nvr,
368368
const fmi2Boolean value[]) {
369369

370-
const auto component = static_cast<Component *>(c);
370+
const auto component = static_cast<Fmi2Component *>(c);
371371
try {
372372
component->slave->set_boolean(vr, nvr, value);
373373
return fmi2OK;
@@ -386,7 +386,7 @@ fmi2Status fmi2SetString(
386386
size_t nvr,
387387
const fmi2String value[]) {
388388

389-
const auto component = static_cast<Component *>(c);
389+
const auto component = static_cast<Fmi2Component *>(c);
390390
try {
391391
component->slave->set_string(vr, nvr, value);
392392
return fmi2OK;
@@ -412,7 +412,7 @@ fmi2Status fmi2GetRealStatus(
412412
const fmi2StatusKind s,
413413
fmi2Real *value) {
414414

415-
const auto component = static_cast<Component *>(c);
415+
const auto component = static_cast<Fmi2Component *>(c);
416416
if (s == fmi2LastSuccessfulTime) {
417417
*value = component->lastSuccessfulTime;
418418
return fmi2OK;
@@ -449,7 +449,7 @@ fmi2Status fmi2SetDebugLogging(fmi2Component c,
449449
size_t /*nCategories*/,
450450
const fmi2String /*categories*/[]) {
451451

452-
const auto component = static_cast<Component *>(c);
452+
const auto component = static_cast<Fmi2Component *>(c);
453453
component->logger->setDebugLogging(loggingOn);
454454
return fmi2OK;
455455
}
@@ -480,7 +480,7 @@ fmi2Status fmi2GetDirectionalDerivative(fmi2Component,
480480

481481

482482
fmi2Status fmi2GetFMUstate(fmi2Component c, fmi2FMUstate *state) {
483-
const auto component = static_cast<Component *>(c);
483+
const auto component = static_cast<Fmi2Component *>(c);
484484

485485
if (auto s = component->slave->getFMUState()) {
486486
state = &s;
@@ -491,7 +491,7 @@ fmi2Status fmi2GetFMUstate(fmi2Component c, fmi2FMUstate *state) {
491491
}
492492

493493
fmi2Status fmi2SetFMUstate(fmi2Component c, fmi2FMUstate state) {
494-
const auto component = static_cast<Component *>(c);
494+
const auto component = static_cast<Fmi2Component *>(c);
495495

496496
if (component->slave->setFmuState(state)) {
497497
return fmi2OK;
@@ -502,7 +502,7 @@ fmi2Status fmi2SetFMUstate(fmi2Component c, fmi2FMUstate state) {
502502

503503

504504
fmi2Status fmi2FreeFMUstate(fmi2Component c, fmi2FMUstate *state) {
505-
const auto component = static_cast<Component *>(c);
505+
const auto component = static_cast<Fmi2Component *>(c);
506506

507507
if (component->slave->freeFmuState(state)) {
508508
return fmi2OK;
@@ -527,7 +527,7 @@ fmi2Status fmi2DeSerializeFMUstate(fmi2Component, const fmi2Byte[], size_t, fmi2
527527
}
528528

529529
void fmi2FreeInstance(fmi2Component c) {
530-
const auto component = static_cast<Component *>(c);
530+
const auto component = static_cast<Fmi2Component *>(c);
531531
delete component;
532532
}
533533
}

0 commit comments

Comments
 (0)