|
| 1 | + |
| 2 | +#include "catch2/matchers/catch_matchers_vector.hpp" |
| 3 | +#include "fmi3/fmi3Functions.h" |
| 4 | + |
| 5 | + |
| 6 | +#include <catch2/catch_approx.hpp> |
| 7 | +#include <catch2/catch_test_macros.hpp> |
| 8 | +#include <cstdarg> |
| 9 | +#include <iostream> |
| 10 | + |
| 11 | +#include <fmu4cpp/fmu_base.hpp> |
| 12 | + |
| 13 | +class Model : public fmu4cpp::fmu_base { |
| 14 | + |
| 15 | +public: |
| 16 | + explicit Model(const fmu4cpp::fmu_data &data) : fmu_base(data) { |
| 17 | + |
| 18 | + register_variable(integer("integerIn", &integer_) |
| 19 | + .setCausality(fmu4cpp::causality_t::INPUT) |
| 20 | + .setVariability(fmu4cpp::variability_t::DISCRETE)); |
| 21 | + |
| 22 | + register_variable(real("realIn", &real_) |
| 23 | + .setCausality(fmu4cpp::causality_t::INPUT) |
| 24 | + .setVariability(fmu4cpp::variability_t::DISCRETE)); |
| 25 | + |
| 26 | + register_variable(boolean("booleanIn", &boolean_) |
| 27 | + .setCausality(fmu4cpp::causality_t::INPUT) |
| 28 | + .setVariability(fmu4cpp::variability_t::DISCRETE)); |
| 29 | + |
| 30 | + register_variable(string("stringIn", &string_) |
| 31 | + .setCausality(fmu4cpp::causality_t::INPUT) |
| 32 | + .setVariability(fmu4cpp::variability_t::DISCRETE)); |
| 33 | + |
| 34 | + |
| 35 | + register_variable(integer("integerOut", &integer_) |
| 36 | + .setCausality(fmu4cpp::causality_t::OUTPUT) |
| 37 | + .setVariability(fmu4cpp::variability_t::DISCRETE) |
| 38 | + .setInitial(fmu4cpp::initial_t::CALCULATED) |
| 39 | + .setDependencies({"integerIn"})); |
| 40 | + |
| 41 | + register_variable(real("realOut", &real_) |
| 42 | + .setCausality(fmu4cpp::causality_t::OUTPUT) |
| 43 | + .setVariability(fmu4cpp::variability_t::DISCRETE) |
| 44 | + .setInitial(fmu4cpp::initial_t::CALCULATED) |
| 45 | + .setDependencies({"realIn"})); |
| 46 | + |
| 47 | + register_variable(boolean("booleanOut", &boolean_) |
| 48 | + .setCausality(fmu4cpp::causality_t::OUTPUT) |
| 49 | + .setVariability(fmu4cpp::variability_t::DISCRETE) |
| 50 | + .setInitial(fmu4cpp::initial_t::CALCULATED) |
| 51 | + .setDependencies({"booleanIn"})); |
| 52 | + |
| 53 | + register_variable(string("stringOut", [this] { return string_; }) |
| 54 | + .setCausality(fmu4cpp::causality_t::OUTPUT) |
| 55 | + .setVariability(fmu4cpp::variability_t::DISCRETE) |
| 56 | + .setInitial(fmu4cpp::initial_t::CALCULATED) |
| 57 | + .setDependencies({"stringIn"})); |
| 58 | + |
| 59 | + Model::reset(); |
| 60 | + } |
| 61 | + |
| 62 | + bool do_step(double dt) override { |
| 63 | + return true; |
| 64 | + } |
| 65 | + |
| 66 | + void reset() override { |
| 67 | + integer_ = 0; |
| 68 | + real_ = 0; |
| 69 | + boolean_ = false; |
| 70 | + string_ = "empty"; |
| 71 | + } |
| 72 | + |
| 73 | +private: |
| 74 | + int integer_; |
| 75 | + double real_; |
| 76 | + bool boolean_; |
| 77 | + std::string string_; |
| 78 | +}; |
| 79 | + |
| 80 | +fmu4cpp::model_info fmu4cpp::get_model_info() { |
| 81 | + model_info info; |
| 82 | + info.modelName = "Identity"; |
| 83 | + info.description = "A simple feed-trough model"; |
| 84 | + info.modelIdentifier = FMU4CPP_MODEL_IDENTIFIER; |
| 85 | + return info; |
| 86 | +} |
| 87 | + |
| 88 | +FMU4CPP_INSTANTIATE(Model); |
| 89 | + |
| 90 | + |
| 91 | +int readInt(fmi3Instance c, fmi3ValueReference ref) { |
| 92 | + fmi3Int32 value; |
| 93 | + REQUIRE(fmi3GetInt32(c, &ref, 1, &value, 1) == fmi3OK); |
| 94 | + |
| 95 | + return value; |
| 96 | +} |
| 97 | + |
| 98 | +void setInt(fmi3Instance c, fmi3ValueReference ref, int value) { |
| 99 | + REQUIRE(fmi3SetInt32(c, &ref, 1, &value, 1) == fmi3OK); |
| 100 | +} |
| 101 | + |
| 102 | +double readReal(fmi3Instance c, fmi3ValueReference ref) { |
| 103 | + fmi3Float64 value; |
| 104 | + REQUIRE(fmi3GetFloat64(c, &ref, 1, &value, 0) == fmi3OK); |
| 105 | + |
| 106 | + return value; |
| 107 | +} |
| 108 | + |
| 109 | +void setReal(fmi3Instance c, fmi3ValueReference ref, double value) { |
| 110 | + REQUIRE(fmi3SetFloat64(c, &ref, 1, &value, 0) == fmi3OK); |
| 111 | +} |
| 112 | + |
| 113 | +bool readBool(fmi3Instance c, fmi3ValueReference ref) { |
| 114 | + fmi3Boolean value; |
| 115 | + REQUIRE(fmi3GetBoolean(c, &ref, 1, &value, 0) == fmi3OK); |
| 116 | + |
| 117 | + return value; |
| 118 | +} |
| 119 | + |
| 120 | +void setBool(fmi3Instance c, fmi3ValueReference ref, bool value) { |
| 121 | + fmi3Boolean value_ = value; |
| 122 | + REQUIRE(fmi3SetBoolean(c, &ref, 1, &value_, 0) == fmi3OK); |
| 123 | +} |
| 124 | + |
| 125 | + |
| 126 | +std::string readString(fmi3Instance c, fmi3ValueReference ref) { |
| 127 | + fmi3String value; |
| 128 | + REQUIRE(fmi3GetString(c, &ref, 1, &value, 0) == fmi3OK); |
| 129 | + |
| 130 | + return value; |
| 131 | +} |
| 132 | + |
| 133 | +void setString(fmi3Instance c, fmi3ValueReference ref, const std::string &value) { |
| 134 | + fmi3String value_ = value.c_str(); |
| 135 | + REQUIRE(fmi3SetString(c, &ref, 1, &value_, 0) == fmi3OK); |
| 136 | +} |
| 137 | + |
| 138 | +void setOutputFail(fmi3Instance c) { |
| 139 | + fmi3ValueReference ref = 999;// out of bounds |
| 140 | + fmi3Int32 i = 0; |
| 141 | + fmi3String s = ""; |
| 142 | + fmi3Float64 r = 0; |
| 143 | + fmi3Boolean b = fmi3False; |
| 144 | + |
| 145 | + REQUIRE(fmi3SetInt32(c, &ref, 1, &i, 0) == fmi3Error); |
| 146 | + REQUIRE(fmi3SetFloat64(c, &ref, 1, &r, 0) == fmi3Error); |
| 147 | + REQUIRE(fmi3SetString(c, &ref, 1, &s, 0) == fmi3Error); |
| 148 | + REQUIRE(fmi3SetBoolean(c, &ref, 1, &b, 0) == fmi3Error); |
| 149 | +} |
| 150 | + |
| 151 | +void fmilogger(fmi3InstanceEnvironment, fmi3Status status, fmi3String /*category*/, fmi3String message) { |
| 152 | + |
| 153 | + std::cout << status << ": " << message << std::endl; |
| 154 | +} |
| 155 | + |
| 156 | +TEST_CASE("test_identity") { |
| 157 | + |
| 158 | + Model model({}); |
| 159 | + const auto guid = model.guid(); |
| 160 | + |
| 161 | + const auto realIn = model.get_real_variable("realIn"); |
| 162 | + const auto stringIn = model.get_string_variable("stringIn"); |
| 163 | + const auto integerIn = model.get_int_variable("integerIn"); |
| 164 | + const auto booleanIn = model.get_bool_variable("booleanIn"); |
| 165 | + |
| 166 | + const auto realOut = model.get_real_variable("realOut"); |
| 167 | + const auto stringOut = model.get_string_variable("stringOut"); |
| 168 | + const auto integerOut = model.get_int_variable("integerOut"); |
| 169 | + const auto booleanOut = model.get_bool_variable("booleanOut"); |
| 170 | + |
| 171 | + const auto c = fmi3InstantiateCoSimulation("identity", guid.c_str(), "", false, true, false, false, nullptr, 0, nullptr, fmilogger, nullptr); |
| 172 | + REQUIRE(c); |
| 173 | + |
| 174 | + REQUIRE(fmi3EnterInitializationMode(c, false, 0, 0, false, 0) == fmi3OK); |
| 175 | + REQUIRE(fmi3ExitInitializationMode(c) == fmi3OK); |
| 176 | + |
| 177 | + REQUIRE(readReal(c, realOut->value_reference()) == Catch::Approx(0)); |
| 178 | + REQUIRE(readString(c, stringOut->value_reference()) == "empty"); |
| 179 | + REQUIRE(readInt(c, integerOut->value_reference()) == 0); |
| 180 | + REQUIRE(readBool(c, booleanOut->value_reference()) == false); |
| 181 | + |
| 182 | + double t{0}; |
| 183 | + const double dt{0.1}; |
| 184 | + |
| 185 | + bool b{false}; |
| 186 | + int counter{0}; |
| 187 | + while (t < 1) { |
| 188 | + |
| 189 | + setInt(c, integerIn->value_reference(), counter); |
| 190 | + setReal(c, realIn->value_reference(), t); |
| 191 | + setString(c, stringIn->value_reference(), std::to_string(t)); |
| 192 | + setBool(c, booleanIn->value_reference(), b); |
| 193 | + |
| 194 | + bool eventhandlingNeeded; |
| 195 | + bool terminateSimulation; |
| 196 | + bool earlyReturn; |
| 197 | + double lastSucessfulTime; |
| 198 | + |
| 199 | + REQUIRE(fmi3DoStep(c, t, dt, true, &eventhandlingNeeded, &terminateSimulation, &earlyReturn, &lastSucessfulTime) == fmi3OK); |
| 200 | + |
| 201 | + REQUIRE(readReal(c, realOut->value_reference()) == Catch::Approx(t)); |
| 202 | + REQUIRE(readString(c, stringOut->value_reference()) == std::to_string(t)); |
| 203 | + REQUIRE(readInt(c, integerOut->value_reference()) == counter); |
| 204 | + REQUIRE(readBool(c, booleanOut->value_reference()) == b); |
| 205 | + |
| 206 | + t += dt; |
| 207 | + counter++; |
| 208 | + b = !b; |
| 209 | + } |
| 210 | + |
| 211 | + setOutputFail(c); |
| 212 | + |
| 213 | + REQUIRE(fmi3Terminate(c) == fmi3OK); |
| 214 | + |
| 215 | + fmi3FreeInstance(c); |
| 216 | +} |
0 commit comments