fmu.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 
26 #ifndef CASADI_FMU_HPP
27 #define CASADI_FMU_HPP
28 
29 #include "shared_object.hpp"
30 #include "printable.hpp"
31 #include "sparsity.hpp"
32 
34 
35 namespace casadi {
36 
37 // Forward declarations
38 class DaeBuilderInternal;
39 class FmuFunction;
40 struct FmuMemory;
41 struct InputStruct;
42 
43 // Forward declaration of internal class
44 class FmuInternal;
45 
47 enum class FmuApi {FMI2, FMI3, NUMEL};
48 
50 CASADI_EXPORT std::string to_string(FmuApi v);
51 
60 class CASADI_EXPORT Fmu
61  : public SharedObject,
62  public SWIG_IF_ELSE(PrintableCommon, Printable<Fmu>) {
63  public:
67  static std::string type_name() {return "Fmu";}
68 
70  Fmu();
71 
73  Fmu(const std::string& name, FmuApi api, const DaeBuilderInternal* dae,
74  const std::vector<std::string>& scheme_in,
75  const std::vector<std::string>& scheme_out,
76  const std::map<std::string, std::vector<size_t>>& scheme,
77  const std::vector<std::string>& aux);
78 
82  const std::string& name() const;
83 
87  const std::string& instance_name() const;
88 
91  FmuInternal* operator->();
92  const FmuInternal* operator->() const;
93  FmuInternal* get() const;
95 
99  size_t n_in() const;
100 
104  size_t n_out() const;
105 
106  // Index lookup for input
107  size_t index_in(const std::string& n) const;
108 
109  // Index lookup for output
110  size_t index_out(const std::string& n) const;
111 
112  // Get all reduced space indices for an input
113  const std::vector<size_t>& ired(size_t ind) const;
114 
115  // Get all reduced space indices for an output
116  const std::vector<size_t>& ored(size_t ind) const;
117 
118  // Get nominal value for reduced-space input
119  double nominal_in(size_t ind) const;
120 
121  // Get nominal value for reduced-space output
122  double nominal_out(size_t ind) const;
123 
124  // Get minimum value for reduced-space input
125  double min_in(size_t ind) const;
126 
127  // Get maximum value for reduced-space input
128  double max_in(size_t ind) const;
129 
130  // Get all nominal values for an input
131  std::vector<double> all_nominal_in(size_t ind) const;
132 
133  // Get all nominal values for an output
134  std::vector<double> all_nominal_out(size_t ind) const;
135 
136  // Description of an input
137  std::string desc_in(FmuMemory* m, size_t id, bool more = true) const;
138 
140  bool provides_directional_derivatives() const;
141 
143  bool provides_adjoint_derivatives() const;
144 
146  bool can_be_instantiated_only_once_per_process() const;
147 
148  // Get Jacobian sparsity for a subset of inputs and outputs
149  Sparsity jac_sparsity(const std::vector<size_t>& osub, const std::vector<size_t>& isub) const;
150 
151  // Get Jacobian sparsity for an output/input pair
152  Sparsity jac_sparsity(size_t oind, size_t iind) const {
153  return jac_sparsity(ored(oind), ired(iind));
154  }
155 
156  // Get Hessian sparsity for a subset of inputs
157  Sparsity hess_sparsity(const std::vector<size_t>& r, const std::vector<size_t>& c) const;
158 
159  // Get Jacobian sparsity for an input/input pair
160  Sparsity hess_sparsity(size_t r, size_t c) const {
161  return hess_sparsity(ired(r), ired(c));
162  }
163 
167  FmuMemory* alloc_mem(const FmuFunction& f) const;
168 
172  int init_mem(FmuMemory* m) const;
173 
177  void free_mem(void *mem) const;
178 
179  // Free FMU instance
180  void free_instance(void* instance) const;
181 
182  // Set value
183  void set(FmuMemory* m, size_t ind, const double* value) const;
184 
185  // Request the calculation of a variable
186  void request(FmuMemory* m, size_t ind) const;
187 
188  // Calculate all requested variables
189  int eval(FmuMemory* m) const;
190 
191  // Get a calculated variable
192  void get(FmuMemory* m, size_t ind, double* value) const;
193 
194  // Set forward seed by variable index
195  void set_fwd(FmuMemory* m, casadi_int nseed, const casadi_int* id, const double* v) const;
196 
197  // Set all forward seeds for a single input
198  void set_fwd(FmuMemory* m, size_t ind, const double* v) const;
199 
200  // Request the calculation of forward sensitivities
201  void request_fwd(FmuMemory* m, casadi_int nsens, const casadi_int* id,
202  const casadi_int* wrt_id) const;
203 
204  // Request the calculation of all forward sensitivities for an output
205  void request_fwd(FmuMemory* m, casadi_int ind) const;
206 
207  // Calculate forward directional derivatives
208  int eval_fwd(FmuMemory* m, bool independent_seeds) const;
209 
210  // Get forward sensitivities
211  void get_fwd(FmuMemory* m, casadi_int nsens, const casadi_int* id, double* v) const;
212 
213  // Get the forward sensitivities for a single output
214  void get_fwd(FmuMemory* m, size_t ind, double* v) const;
215 
216  // Set adjoint seeds
217  void set_adj(FmuMemory* m, casadi_int nseed,
218  const casadi_int* id, const double* v) const;
219 
220  // Set all adjoint seeds for a single output
221  void set_adj(FmuMemory* m, size_t ind, const double* v) const;
222 
223  // Request the calculation of adjoint sensitivities
224  void request_adj(FmuMemory* m, casadi_int nsens, const casadi_int* id,
225  const casadi_int* wrt_id) const;
226 
227  // Request the calculation of all adjoint sensitivities for an input
228  void request_adj(FmuMemory* m, casadi_int ind) const;
229 
230  // Calculate adjoint directional derivatives
231  int eval_adj(FmuMemory* m) const;
232 
233  // Get adjoint sensitivities
234  void get_adj(FmuMemory* m, casadi_int nsens,
235  const casadi_int* id, double* v) const;
236 
237  // Get the adjoint sensitivities for a single input
238  void get_adj(FmuMemory* m, size_t ind, double* v) const;
239 
243  void get_stats(FmuMemory* m, Dict* stats,
244  const std::vector<std::string>& name_in, const InputStruct* in) const;
245 
247 #ifndef SWIG
251  static Fmu create(FmuInternal* node);
252 #endif // SWIG
254 
258  void serialize(SerializingStream &s) const;
259 
263  static Fmu deserialize(DeserializingStream& s);
264 };
265 
266 } // namespace casadi
267 
269 
270 #endif // CASADI_FMU_HPP
Helper class for Serialization.
Interface to binary FMU.
Definition: fmu_impl.hpp:61
Interface to binary FMU.
Definition: fmu.hpp:62
static std::string type_name()
Get type name.
Definition: fmu.hpp:67
Sparsity hess_sparsity(size_t r, size_t c) const
Definition: fmu.hpp:160
Sparsity jac_sparsity(size_t oind, size_t iind) const
Definition: fmu.hpp:152
Helper class for Serialization.
GenericShared implements a reference counting framework similar for efficient and.
General sparsity class.
Definition: sparsity.hpp:106
The casadi namespace.
Definition: archiver.cpp:28
FmuApi
Which C API.
Definition: fmu.hpp:47
GenericType::Dict Dict
C++ equivalent of Python's dict or MATLAB's struct.
std::string to_string(TypeFmi2 v)