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 struct FmuMemory;
40 struct InputStruct;
41 
42 // Forward declaration of internal class
43 class FmuInternal;
44 
46 enum class FmuApi {FMI2, FMI3, NUMEL};
47 
49 CASADI_EXPORT std::string to_string(FmuApi v);
50 
59 class CASADI_EXPORT Fmu
60  : public SharedObject,
61  public SWIG_IF_ELSE(PrintableCommon, Printable<Fmu>) {
62  public:
66  static std::string type_name() {return "Fmu";}
67 
69  Fmu();
70 
72  Fmu(const std::string& name, FmuApi api, const DaeBuilderInternal* dae,
73  const std::vector<std::string>& scheme_in,
74  const std::vector<std::string>& scheme_out,
75  const std::map<std::string, std::vector<size_t>>& scheme,
76  const std::vector<std::string>& aux);
77 
81  const std::string& name() const;
82 
86  const std::string& instance_name() const;
87 
90  FmuInternal* operator->();
91  const FmuInternal* operator->() const;
92  FmuInternal* get() const;
94 
98  size_t n_in() const;
99 
103  size_t n_out() const;
104 
105  // Index lookup for input
106  size_t index_in(const std::string& n) const;
107 
108  // Index lookup for output
109  size_t index_out(const std::string& n) const;
110 
111  // Get all reduced space indices for an input
112  const std::vector<size_t>& ired(size_t ind) const;
113 
114  // Get all reduced space indices for an output
115  const std::vector<size_t>& ored(size_t ind) const;
116 
117  // Get nominal value for reduced-space input
118  double nominal_in(size_t ind) const;
119 
120  // Get nominal value for reduced-space output
121  double nominal_out(size_t ind) const;
122 
123  // Get minimum value for reduced-space input
124  double min_in(size_t ind) const;
125 
126  // Get maximum value for reduced-space input
127  double max_in(size_t ind) const;
128 
129  // Get all nominal values for an input
130  std::vector<double> all_nominal_in(size_t ind) const;
131 
132  // Get all nominal values for an output
133  std::vector<double> all_nominal_out(size_t ind) const;
134 
135  // Description of an input
136  std::string desc_in(FmuMemory* m, size_t id, bool more = true) const;
137 
139  bool provides_directional_derivatives() const;
140 
142  bool provides_adjoint_derivatives() const;
143 
145  bool can_be_instantiated_only_once_per_process() const;
146 
147  // Get Jacobian sparsity for a subset of inputs and outputs
148  Sparsity jac_sparsity(const std::vector<size_t>& osub, const std::vector<size_t>& isub) const;
149 
150  // Get Jacobian sparsity for an output/input pair
151  Sparsity jac_sparsity(size_t oind, size_t iind) const {
152  return jac_sparsity(ored(oind), ired(iind));
153  }
154 
155  // Get Hessian sparsity for a subset of inputs
156  Sparsity hess_sparsity(const std::vector<size_t>& r, const std::vector<size_t>& c) const;
157 
158  // Get Jacobian sparsity for an input/input pair
159  Sparsity hess_sparsity(size_t r, size_t c) const {
160  return hess_sparsity(ired(r), ired(c));
161  }
162 
166  int init_mem(FmuMemory* m) const;
167 
168  // Free FMU instance
169  void free_instance(void* instance) const;
170 
171  // Set value
172  void set(FmuMemory* m, size_t ind, const double* value) const;
173 
174  // Request the calculation of a variable
175  void request(FmuMemory* m, size_t ind) const;
176 
177  // Calculate all requested variables
178  int eval(FmuMemory* m) const;
179 
180  // Get a calculated variable
181  void get(FmuMemory* m, size_t ind, double* value) const;
182 
183  // Set forward seed by variable index
184  void set_fwd(FmuMemory* m, casadi_int nseed, const casadi_int* id, const double* v) const;
185 
186  // Set all forward seeds for a single input
187  void set_fwd(FmuMemory* m, size_t ind, const double* v) const;
188 
189  // Request the calculation of forward sensitivities
190  void request_fwd(FmuMemory* m, casadi_int nsens, const casadi_int* id,
191  const casadi_int* wrt_id) const;
192 
193  // Request the calculation of all forward sensitivities for an output
194  void request_fwd(FmuMemory* m, casadi_int ind) const;
195 
196  // Calculate forward directional derivatives
197  int eval_fwd(FmuMemory* m, bool independent_seeds) const;
198 
199  // Get forward sensitivities
200  void get_fwd(FmuMemory* m, casadi_int nsens, const casadi_int* id, double* v) const;
201 
202  // Get the forward sensitivities for a single output
203  void get_fwd(FmuMemory* m, size_t ind, double* v) const;
204 
205  // Set adjoint seeds
206  void set_adj(FmuMemory* m, casadi_int nseed,
207  const casadi_int* id, const double* v) const;
208 
209  // Set all adjoint seeds for a single output
210  void set_adj(FmuMemory* m, size_t ind, const double* v) const;
211 
212  // Request the calculation of adjoint sensitivities
213  void request_adj(FmuMemory* m, casadi_int nsens, const casadi_int* id,
214  const casadi_int* wrt_id) const;
215 
216  // Request the calculation of all adjoint sensitivities for an input
217  void request_adj(FmuMemory* m, casadi_int ind) const;
218 
219  // Calculate adjoint directional derivatives
220  int eval_adj(FmuMemory* m) const;
221 
222  // Get adjoint sensitivities
223  void get_adj(FmuMemory* m, casadi_int nsens,
224  const casadi_int* id, double* v) const;
225 
226  // Get the adjoint sensitivities for a single input
227  void get_adj(FmuMemory* m, size_t ind, double* v) const;
228 
232  void get_stats(FmuMemory* m, Dict* stats,
233  const std::vector<std::string>& name_in, const InputStruct* in) const;
234 
236 #ifndef SWIG
240  static Fmu create(FmuInternal* node);
241 #endif // SWIG
243 
247  void serialize(SerializingStream &s) const;
248 
252  static Fmu deserialize(DeserializingStream& s);
253 };
254 
255 } // namespace casadi
256 
258 
259 #endif // CASADI_FMU_HPP
Helper class for Serialization.
Interface to binary FMU.
Definition: fmu_impl.hpp:60
Interface to binary FMU.
Definition: fmu.hpp:61
static std::string type_name()
Get type name.
Definition: fmu.hpp:66
Sparsity hess_sparsity(size_t r, size_t c) const
Definition: fmu.hpp:159
Sparsity jac_sparsity(size_t oind, size_t iind) const
Definition: fmu.hpp:151
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:46
GenericType::Dict Dict
C++ equivalent of Python's dict or MATLAB's struct.
std::string to_string(TypeFmi2 v)