libcosim 0.8.2
C++ library for distributed co-simulation
fmu.hpp
Go to the documentation of this file.
1
10#ifndef COSIM_FMI_V2_FMU_HPP
11#define COSIM_FMI_V2_FMU_HPP
12
13#include <cosim/file_cache.hpp>
14#include <cosim/fmi/fmu.hpp>
18#include <cosim/time.hpp>
19
20#include <memory>
21#include <string>
22#include <string_view>
23#include <vector>
24
25
26struct fmi2_import_t;
27
28
29namespace cosim
30{
31namespace fmi
32{
33
34#ifdef _WIN32
35namespace detail
36{
37class additional_path;
38}
39#endif
40
41namespace v2
42{
43
44class slave_instance;
45
46
53class fmu : public fmi::fmu, public std::enable_shared_from_this<fmu>
54{
55private:
56 // Only fmi::importer is allowed to instantiate this class.
57 friend class fmi::importer;
58 fmu(
61
62public:
63 // Disable copy and move
64 fmu(const fmu&) = delete;
65 fmu& operator=(const fmu&) = delete;
66 fmu(fmu&&) = delete;
67 fmu& operator=(fmu&&) = delete;
68
69 ~fmu();
70
71 // fmi::fmu methods
72 fmi::fmi_version fmi_version() const override;
73
75 model_description() const override;
76
78 std::string_view instanceName) override
79 {
80 return std::static_pointer_cast<fmi::slave_instance>(
81 instantiate_v2_slave(instanceName));
82 }
83
85
93 std::string_view instanceName);
94
96 cosim::filesystem::path directory() const;
97
99 fmi2_import_t* fmilib_handle() const;
100
101private:
104
105 fmi2_import_t* handle_;
106 cosim::model_description modelDescription_;
108
109#ifdef _WIN32
110 // Workaround for VIPROMA-67 (FMU DLL search paths on Windows).
111 std::unique_ptr<detail::additional_path> additionalDllSearchPath_;
112#endif
113};
114
115
118{
119private:
120 // Only fmu is allowed to instantiate this class.
122 slave_instance(std::shared_ptr<v2::fmu> fmu, std::string_view instanceName);
123
124public:
125 // Disable copy and move.
126 slave_instance(const slave_instance&) = delete;
127 slave_instance& operator=(const slave_instance&) = delete;
128 slave_instance(slave_instance&&) = delete;
129 slave_instance& operator=(slave_instance&&) = delete;
130
131 ~slave_instance() noexcept;
132
133 // cosim::slave methods
134 void setup(
135 time_point startTime,
136 std::optional<time_point> stopTime,
137 std::optional<double> relativeTolerance) override;
138 void start_simulation() override;
139 void end_simulation() override;
140 step_result do_step(time_point currentT, duration deltaT) override;
141
143 gsl::span<const value_reference> variables,
144 gsl::span<double> values) const override;
146 gsl::span<const value_reference> variables,
147 gsl::span<int> values) const override;
149 gsl::span<const value_reference> variables,
150 gsl::span<bool> values) const override;
152 gsl::span<const value_reference> variables,
153 gsl::span<std::string> values) const override;
154
156 gsl::span<const value_reference> variables,
157 gsl::span<const double> values) override;
159 gsl::span<const value_reference> variables,
160 gsl::span<const int> values) override;
162 gsl::span<const value_reference> variables,
163 gsl::span<const bool> values) override;
165 gsl::span<const value_reference> variables,
166 gsl::span<const std::string> values) override;
167
168 // fmi::slave_instance methods
170 {
171 return std::static_pointer_cast<fmi::fmu>(v2_fmu());
172 }
173
176
178 fmi2_import_t* fmilib_handle() const;
179
180private:
182 fmi2_import_t* handle_;
183
184 bool setupComplete_ = false;
185 bool simStarted_ = false;
186
187 std::string instanceName_;
188};
189
190
191} // namespace v2
192} // namespace fmi
193} // namespace cosim
194#endif // header guard
An interface for classes that represent imported FMUs.
Definition: fmu.hpp:52
Imports and caches FMUs.
Definition: importer.hpp:43
An FMI co-simulation slave instance.
Definition: fmu.hpp:74
A class which represents an imported FMI 2.0 FMU.
Definition: fmu.hpp:54
fmi2_import_t * fmilib_handle() const
Returns the underlying C API handle (for FMI Library)
cosim::filesystem::path directory() const
Returns the path to the directory in which this FMU was unpacked.
std::shared_ptr< v2::slave_instance > instantiate_v2_slave(std::string_view instanceName)
Creates a new co-simulation slave instance.
fmi::fmi_version fmi_version() const override
Which FMI standard version is used in this FMU.
std::shared_ptr< fmi::slave_instance > instantiate_slave(std::string_view instanceName) override
Creates a co-simulation slave instance of this FMU.
Definition: fmu.hpp:77
std::shared_ptr< fmi::importer > importer() const override
The fmi::importer which was used to import this FMU.
std::shared_ptr< const cosim::model_description > model_description() const override
A description of this FMU.
An FMI 2.0 co-simulation slave instance.
Definition: fmu.hpp:118
void get_boolean_variables(gsl::span< const value_reference > variables, gsl::span< bool > values) const override
Retrieves the values of boolean variables.
std::shared_ptr< fmi::fmu > fmu() const override
Returns a reference to the FMU of which this is an instance.
Definition: fmu.hpp:169
fmi2_import_t * fmilib_handle() const
Returns the underlying C API handle (for FMI Library)
void setup(time_point startTime, std::optional< time_point > stopTime, std::optional< double > relativeTolerance) override
Instructs the slave to perform pre-simulation setup and enter initialisation mode.
void set_string_variables(gsl::span< const value_reference > variables, gsl::span< const std::string > values) override
Sets the values of string variables.
void get_real_variables(gsl::span< const value_reference > variables, gsl::span< double > values) const override
Retrieves the values of real variables.
void start_simulation() override
Informs the slave that the initialisation stage ends and the simulation begins.
void get_integer_variables(gsl::span< const value_reference > variables, gsl::span< int > values) const override
Retrieves the values of integer variables.
step_result do_step(time_point currentT, duration deltaT) override
Performs model calculations for the time step which starts at the time point currentT and has a durat...
void get_string_variables(gsl::span< const value_reference > variables, gsl::span< std::string > values) const override
Retrieves the values of string variables.
void set_integer_variables(gsl::span< const value_reference > variables, gsl::span< const int > values) override
Sets the values of integer variables.
std::shared_ptr< v2::fmu > v2_fmu() const
Returns the same object as fmu(), only statically typed as a v2::fmu().
void set_boolean_variables(gsl::span< const value_reference > variables, gsl::span< const bool > values) override
Sets the values of boolean variables.
void end_simulation() override
Informs the slave that the simulation run has ended.
void set_real_variables(gsl::span< const value_reference > variables, gsl::span< const double > values) override
Sets the values of real variables.
Defines a version-independent FMU interface.
fmi_version
Constants that refer to FMI version numbers.
Definition: fmu.hpp:28
FMU import functionality.
Model-descriptive types and constants.
Top-level libcosim namespace.
Definition: algorithm.hpp:27
step_result
Possible outcomes of a subsimulator time step.
Definition: model_description.hpp:205
A description of a model.
Definition: model_description.hpp:180
Time-related functions and types.