|
| 1 | + |
| 2 | +#include "fmu4cpp/fmu_base.hpp" |
| 3 | + |
| 4 | +#include <cmath> |
| 5 | + |
| 6 | +class Dahlquist : public fmu4cpp::fmu_base { |
| 7 | + |
| 8 | +public: |
| 9 | + FMU4CPP_CTOR(Dahlquist) { |
| 10 | + |
| 11 | + register_variable(real("x", &x) |
| 12 | + .setCausality(fmu4cpp::causality_t::OUTPUT) |
| 13 | + .setVariability(fmu4cpp::variability_t::CONTINUOUS) |
| 14 | + .setInitial(fmu4cpp::initial_t::EXACT)); |
| 15 | + |
| 16 | + register_variable(real("dx", &dx) |
| 17 | + .setCausality(fmu4cpp::causality_t::LOCAL) |
| 18 | + .setVariability(fmu4cpp::variability_t::CONTINUOUS)); |
| 19 | + |
| 20 | + register_variable(real("k", &k) |
| 21 | + .setCausality(fmu4cpp::causality_t::PARAMETER) |
| 22 | + .setVariability(fmu4cpp::variability_t::FIXED)); |
| 23 | + |
| 24 | + Dahlquist::reset(); |
| 25 | + } |
| 26 | + |
| 27 | + bool do_step(double dt) override { |
| 28 | + |
| 29 | + const double lambda = k; |
| 30 | + // exact Dahlquist step: x_{n+1} = exp(lambda * dt) * x_n |
| 31 | + double x_new = std::exp(lambda * dt) * x; |
| 32 | + // derivative at the new state |
| 33 | + dx = lambda * x_new; |
| 34 | + x = x_new; |
| 35 | + |
| 36 | + return true; |
| 37 | + } |
| 38 | + |
| 39 | + |
| 40 | + void reset() override { |
| 41 | + x = 1.0; |
| 42 | + dx = 0.0; |
| 43 | + k = -1.0; |
| 44 | + } |
| 45 | + |
| 46 | +private: |
| 47 | + double x{}; |
| 48 | + double dx{}; |
| 49 | + double k{}; |
| 50 | +}; |
| 51 | + |
| 52 | +fmu4cpp::model_info fmu4cpp::get_model_info() { |
| 53 | + model_info info; |
| 54 | + info.modelName = "Dahlquist"; |
| 55 | + info.description = "This model implements the Dahlquist test equation"; |
| 56 | + info.defaultExperiment = {0, 10, 0.1}; |
| 57 | + return info; |
| 58 | +} |
| 59 | + |
| 60 | +FMU4CPP_INSTANTIATE(Dahlquist); |
0 commit comments