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, 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 
85  FmuInternal* operator->();
86  const FmuInternal* operator->() const;
87  FmuInternal* get() const;
89 
93  size_t n_in() const;
94 
98  size_t n_out() const;
99 
100  // Index lookup for input
101  size_t index_in(const std::string& n) const;
102 
103  // Index lookup for output
104  size_t index_out(const std::string& n) const;
105 
106  // Get all reduced space indices for an input
107  const std::vector<size_t>& ired(size_t ind) const;
108 
109  // Get all reduced space indices for an output
110  const std::vector<size_t>& ored(size_t ind) const;
111 
112  // Get nominal value for reduced-space input
113  double nominal_in(size_t ind) const;
114 
115  // Get nominal value for reduced-space output
116  double nominal_out(size_t ind) const;
117 
118  // Get minimum value for reduced-space input
119  double min_in(size_t ind) const;
120 
121  // Get maximum value for reduced-space input
122  double max_in(size_t ind) const;
123 
124  // Get all nominal values for an input
125  std::vector<double> all_nominal_in(size_t ind) const;
126 
127  // Get all nominal values for an output
128  std::vector<double> all_nominal_out(size_t ind) const;
129 
130  // Description of an input
131  std::string desc_in(FmuMemory* m, size_t id, bool more = true) const;
132 
134  bool has_ad() const;
135 
136  // Get Jacobian sparsity for a subset of inputs and outputs
137  Sparsity jac_sparsity(const std::vector<size_t>& osub, const std::vector<size_t>& isub) const;
138 
139  // Get Jacobian sparsity for an output/input pair
140  Sparsity jac_sparsity(size_t oind, size_t iind) const {
141  return jac_sparsity(ored(oind), ired(iind));
142  }
143 
144  // Get Hessian sparsity for a subset of inputs
145  Sparsity hess_sparsity(const std::vector<size_t>& r, const std::vector<size_t>& c) const;
146 
147  // Get Jacobian sparsity for an input/input pair
148  Sparsity hess_sparsity(size_t r, size_t c) const {
149  return hess_sparsity(ired(r), ired(c));
150  }
151 
155  int init_mem(FmuMemory* m) const;
156 
157  // Free FMU instance
158  void free_instance(void* c) const;
159 
160  // Set value
161  void set(FmuMemory* m, size_t ind, const double* value) const;
162 
163  // Request the calculation of a variable
164  void request(FmuMemory* m, size_t ind) const;
165 
166  // Calculate all requested variables
167  int eval(FmuMemory* m) const;
168 
169  // Get a calculated variable
170  void get(FmuMemory* m, size_t ind, double* value) const;
171 
172  // Set seed
173  void set_seed(FmuMemory* m, casadi_int nseed, const casadi_int* id, const double* v) const;
174 
175  // Request the calculation of a sensitivity
176  void request_sens(FmuMemory* m, casadi_int nsens, const casadi_int* id,
177  const casadi_int* wrt_id) const;
178 
179  // Calculate directional derivatives
180  int eval_derivative(FmuMemory* m, bool independent_seeds) const;
181 
182  // Get calculated derivatives
183  void get_sens(FmuMemory* m, casadi_int nsens, const casadi_int* id, double* v) const;
184 
185  // Set all forward seeds for a single input
186  void set_fwd(FmuMemory* m, size_t ind, const double* v) const;
187 
188  // Request the calculation of all forward sensitivities for an output
189  void request_fwd(FmuMemory* m, casadi_int ind) const;
190 
191  // Get the forward sensitivities for a single output
192  void get_fwd(FmuMemory* m, size_t ind, double* v) const;
193 
197  void get_stats(FmuMemory* m, Dict* stats,
198  const std::vector<std::string>& name_in, const InputStruct* in) const;
199 
201 #ifndef SWIG
205  static Fmu create(FmuInternal* node);
206 #endif // SWIG
208 
212  void serialize(SerializingStream &s) const;
213 
217  static Fmu deserialize(DeserializingStream& s);
218 };
219 
220 } // namespace casadi
221 
223 
224 #endif // CASADI_FMU_HPP
The casadi namespace.
GenericType::Dict Dict
C++ equivalent of Python's dict or MATLAB's struct.