fmu2.hpp
1 /*
2  * This file is part of CasADi.
3  *
4  * CasADi -- A symbolic framework for dynamic optimization.
5  * Copyright (C) 2010-2023 Joel Andersson, Joris Gillis, Moritz Diehl,
6  * KU Leuven. All rights reserved.
7  * Copyright (C) 2011-2014 Greg Horn
8  *
9  * CasADi is free software; you can redistribute it and/or
10  * modify it under the terms of the GNU Lesser General Public
11  * License as published by the Free Software Foundation; either
12  * version 3 of the License, or (at your option) any later version.
13  *
14  * CasADi is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17  * Lesser General Public License for more details.
18  *
19  * You should have received a copy of the GNU Lesser General Public
20  * License along with CasADi; if not, write to the Free Software
21  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
22  *
23  */
24 
25 #ifndef CASADI_FMU2_HPP
26 #define CASADI_FMU2_HPP
27 
28 #ifdef WITH_FMI2
29 
30 #include "fmu_impl.hpp"
31 
32 #include <fmi2Functions.h>
33 
35 
36 namespace casadi {
37 
44 class CASADI_EXPORT Fmu2 : public FmuInternal {
45  public:
46  // Constructor
47  Fmu2(const std::string& name,
48  const std::vector<std::string>& scheme_in, const std::vector<std::string>& scheme_out,
49  const std::map<std::string, std::vector<size_t>>& scheme, const std::vector<std::string>& aux);
50 
52  ~Fmu2() override;
53 
57  std::string class_name() const override { return "Fmu2";}
58 
59  // Initialize
60  void init(const DaeBuilderInternal* dae) override;
61 
62  // Finalize
63  void finalize() override;
64 
65  // Path to the FMU resource directory
66  std::string resource_loc_;
67 
68  // Tolerance
69  double fmutol_;
70 
71  // Instance name
72  std::string instance_name_;
73 
74  // GUID
75  std::string guid_;
76 
77  // Logging?
78  bool logging_on_;
79 
80  // Variables used for initialization, by type
81  std::vector<fmi2ValueReference> vr_real_, vr_integer_, vr_boolean_, vr_string_;
82  std::vector<fmi2Real> init_real_;
83  std::vector<fmi2Integer> init_integer_;
84  std::vector<fmi2Boolean> init_boolean_;
85  std::vector<std::string> init_string_;
86 
87  // Auxilliary variables, by type
88  std::vector<std::string> vn_aux_real_, vn_aux_integer_, vn_aux_boolean_, vn_aux_string_;
89  std::vector<fmi2ValueReference> vr_aux_real_, vr_aux_integer_, vr_aux_boolean_, vr_aux_string_;
90 
91  // Does the FMU declare analytic derivatives support?
92  bool declared_ad_;
93 
94  // Following members set in finalize
95 
96  // FMU C API function prototypes. Cf. FMI specification 2.0.2
97  fmi2InstantiateTYPE* instantiate_;
98  fmi2FreeInstanceTYPE* free_instance_;
99  fmi2ResetTYPE* reset_;
100  fmi2SetupExperimentTYPE* setup_experiment_;
101  fmi2EnterInitializationModeTYPE* enter_initialization_mode_;
102  fmi2ExitInitializationModeTYPE* exit_initialization_mode_;
103  fmi2EnterContinuousTimeModeTYPE* enter_continuous_time_mode_;
104  fmi2GetRealTYPE* get_real_;
105  fmi2SetRealTYPE* set_real_;
106  fmi2GetBooleanTYPE* get_boolean_;
107  fmi2SetBooleanTYPE* set_boolean_;
108  fmi2GetIntegerTYPE* get_integer_;
109  fmi2SetIntegerTYPE* set_integer_;
110  fmi2GetStringTYPE* get_string_;
111  fmi2SetStringTYPE* set_string_;
112  fmi2GetDirectionalDerivativeTYPE* get_directional_derivative_;
113 
114  // Callback functions
115  fmi2CallbackFunctions functions_;
116 
117  // Collection of variable values, all types
118  struct Value {
119  std::vector<fmi2Real> v_real;
120  std::vector<fmi2Integer> v_integer;
121  std::vector<fmi2Boolean> v_boolean;
122  std::vector<std::string> v_string;
123  };
124 
125  Value aux_value_;
126 
127  // Does the FMU support analytic derivatives?
128  bool has_ad() const override { return get_directional_derivative_ != nullptr; }
129 
130  // New memory object
131  fmi2Component instantiate() const;
132 
133  // Free FMU instance
134  void free_instance(void* c) const override;
135 
136  // Reset solver
137  int reset(fmi2Component c);
138 
139  // Setup experiment
140  void setup_experiment(fmi2Component c) const;
141 
142  // Enter initialization mode
143  int enter_initialization_mode(fmi2Component c) const;
144 
145  // Exit initialization mode
146  int exit_initialization_mode(fmi2Component c) const;
147 
148  // Copy values set in DaeBuilder to FMU
149  int set_values(fmi2Component c) const;
150 
151  // Retrieve input variable values from FMU
152  int get_in(fmi2Component c, std::vector<fmi2Real>* v) const;
153 
154  // Retrieve auxilliary variables from FMU
155  int get_aux(fmi2Component c, Value* v) const;
156 
160  void get_stats(FmuMemory* m, Dict* stats,
161  const std::vector<std::string>& name_in, const InputStruct* in) const override;
162 
166  int init_mem(FmuMemory* m) const override;
167 
168  // Calculate all requested variables
169  int eval(FmuMemory* m) const override;
170 
171  // Calculate directional derivatives using AD
172  int eval_ad(FmuMemory* m) const override;
173 
174  // Calculate directional derivatives using FD
175  int eval_fd(FmuMemory* m, bool independent_seeds) const override;
176 
177  // Name of system, per the FMI specification
178  static std::string system_infix();
179 
180  // DLL suffix, per the FMI specification
181  static std::string dll_suffix();
182 
183  // Process message
184  static void logger(fmi2ComponentEnvironment componentEnvironment,
185  fmi2String instanceName,
186  fmi2Status status,
187  fmi2String category,
188  fmi2String message, ...);
189 
190  void serialize_body(SerializingStream& s) const override;
191 
192  static Fmu2* deserialize(DeserializingStream& s);
193 
194  protected:
195  explicit Fmu2(DeserializingStream& s);
196 };
197 
198 } // namespace casadi
199 
201 
202 #endif // WITH_FMI2
203 
204 #endif // CASADI_FMU2_HPP
The casadi namespace.
GenericType::Dict Dict
C++ equivalent of Python's dict or MATLAB's struct.