fmu.cpp
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 #include "fmu_impl.hpp"
27 #include "fmu_function.hpp"
28 #include "dae_builder_internal.hpp"
29 #include "filesystem_impl.hpp"
30 
31 #ifdef WITH_FMI2
32 #include "fmu2.hpp"
33 #endif // WITH_FMI2
34 
35 #ifdef WITH_FMI3
36 #include "fmu3.hpp"
37 #endif // WITH_FMI3
38 
39 namespace casadi {
40 
41 // Throw informative error message
42 #define THROW_ERROR(FNAME, WHAT) \
43 throw CasadiException("Error in Fmu::" FNAME " for '" + this->name() + "' "\
44  "[" + this->class_name() + "] at " + CASADI_WHERE + ":\n"\
45  + std::string(WHAT));
46 
47 
49 }
50 
51 Fmu::Fmu(const std::string& name, FmuApi api, const DaeBuilderInternal* dae,
52  const std::vector<std::string>& scheme_in,
53  const std::vector<std::string>& scheme_out,
54  const std::map<std::string, std::vector<size_t>>& scheme,
55  const std::vector<std::string>& aux) {
56  if (api == FmuApi::FMI2) {
57 #ifdef WITH_FMI2
58  // Create
59  own(new Fmu2(name, scheme_in, scheme_out, scheme, aux));
60 #else // WITH_FMI2
61  // No compilation support
62  casadi_error("CasADi was not compiled with WITH_FMI2=ON.");
63 #endif // WITH_FMI2
64  } else if (api == FmuApi::FMI3) {
65 #ifdef WITH_FMI3
66  // Create
67  own(new Fmu3(name, scheme_in, scheme_out, scheme, aux));
68 #else // WITH_FMI3
69  // No compilation support
70  casadi_error("CasADi was not compiled with WITH_FMI3=ON.");
71 #endif // WITH_FMI3
72  } else {
73  // Not supported
74  casadi_error("Unsupported FMU API: " + to_string(api));
75  }
76  // Initialize
77  try {
78  (*this)->init(dae);
79  (*this)->finalize();
80  } catch(std::exception& e) {
81  THROW_ERROR("init", e.what());
82  }
83 }
84 
86  return static_cast<FmuInternal*>(SharedObject::operator->());
87 }
88 
89 const FmuInternal* Fmu::operator->() const {
90  return static_cast<const FmuInternal*>(SharedObject::operator->());
91 }
92 
94  return static_cast<FmuInternal*>(SharedObject::get());
95 }
96 
97 const std::string& Fmu::name() const {
98  if (is_null()) {
99  static std::string null = "null";
100  return null;
101  } else {
102  return (*this)->name_;
103  }
104 }
105 
106 const std::string& Fmu::instance_name() const {
107  if (is_null()) {
108  static std::string null = "null";
109  return null;
110  } else {
111  return (*this)->instance_name_;
112  }
113 }
114 
115 size_t Fmu::n_in() const {
116  try {
117  return (*this)->n_in();
118  } catch(std::exception& e) {
119  THROW_ERROR("n_in", e.what());
120  }
121 }
122 
123 size_t Fmu::n_out() const {
124  try {
125  return (*this)->n_out();
126  } catch(std::exception& e) {
127  THROW_ERROR("n_out", e.what());
128  }
129 }
130 
131 size_t Fmu::index_in(const std::string& n) const {
132  try {
133  return (*this)->index_in(n);
134  } catch(std::exception& e) {
135  THROW_ERROR("index_in", e.what());
136  }
137 }
138 
139 size_t Fmu::index_out(const std::string& n) const {
140  try {
141  return (*this)->index_out(n);
142  } catch(std::exception& e) {
143  THROW_ERROR("index_out", e.what());
144  }
145 }
146 
147 const std::vector<size_t>& Fmu::ired(size_t ind) const {
148  try {
149  return (*this)->ired_.at(ind);
150  } catch(std::exception& e) {
151  THROW_ERROR("ired", e.what());
152  }
153 }
154 
155 const std::vector<size_t>& Fmu::ored(size_t ind) const {
156  try {
157  return (*this)->ored_.at(ind);
158  } catch(std::exception& e) {
159  THROW_ERROR("ored", e.what());
160  }
161 }
162 
163 double Fmu::nominal_in(size_t ind) const {
164  try {
165  return (*this)->nominal_in_.at(ind);
166  } catch(std::exception& e) {
167  THROW_ERROR("nominal_in", e.what());
168  }
169 }
170 
171 double Fmu::nominal_out(size_t ind) const {
172  try {
173  return (*this)->nominal_out_.at(ind);
174  } catch(std::exception& e) {
175  THROW_ERROR("nominal_out", e.what());
176  }
177 }
178 
179 double Fmu::min_in(size_t ind) const {
180  try {
181  return (*this)->min_in_.at(ind);
182  } catch(std::exception& e) {
183  THROW_ERROR("min_in", e.what());
184  }
185 }
186 
187 double Fmu::max_in(size_t ind) const {
188  try {
189  return (*this)->max_in_.at(ind);
190  } catch(std::exception& e) {
191  THROW_ERROR("max_in", e.what());
192  }
193 }
194 
195 std::vector<double> Fmu::all_nominal_in(size_t ind) const {
196  try {
197  return (*this)->all_nominal_in(ind);
198  } catch(std::exception& e) {
199  THROW_ERROR("all_nominal_in", e.what());
200  }
201 }
202 
203 std::vector<double> Fmu::all_nominal_out(size_t ind) const {
204  try {
205  return (*this)->all_nominal_out(ind);
206  } catch(std::exception& e) {
207  THROW_ERROR("all_nominal_out", e.what());
208  }
209 }
210 
211 std::string Fmu::desc_in(FmuMemory* m, size_t id, bool more) const {
212  try {
213  return (*this)->desc_in(m, id, more);
214  } catch(std::exception& e) {
215  THROW_ERROR("desc_in", e.what());
216  }
217 }
218 
220  try {
221  return (*this)->provides_directional_derivatives_;
222  } catch(std::exception& e) {
223  THROW_ERROR("provides_directional_derivatives", e.what());
224  }
225 }
226 
228  try {
229  return (*this)->provides_adjoint_derivatives_;
230  } catch(std::exception& e) {
231  THROW_ERROR("provides_adjoint_derivatives", e.what());
232  }
233 }
234 
236  try {
237  return (*this)->can_be_instantiated_only_once_per_process_;
238  } catch(std::exception& e) {
239  THROW_ERROR("can_be_instantiated_only_once_per_process", e.what());
240  }
241 }
242 
243 Sparsity Fmu::jac_sparsity(const std::vector<size_t>& osub,
244  const std::vector<size_t>& isub) const {
245  try {
246  return (*this)->jac_sparsity(osub, isub);
247  } catch(std::exception& e) {
248  THROW_ERROR("jac_sparsity", e.what());
249  }
250 }
251 
252 Sparsity Fmu::hess_sparsity(const std::vector<size_t>& r, const std::vector<size_t>& c) const {
253  try {
254  return (*this)->hess_sparsity(r, c);
255  } catch(std::exception& e) {
256  THROW_ERROR("hess_sparsity", e.what());
257  }
258 }
259 
260 int Fmu::init_mem(FmuMemory* m) const {
261  try {
262  return (*this)->init_mem(m);
263  } catch(std::exception& e) {
264  THROW_ERROR("init_mem", e.what());
265  return 1;
266  }
267 }
268 
269 void Fmu::free_instance(void* instance) const {
270  try {
271  return (*this)->free_instance(instance);
272  } catch(std::exception& e) {
273  THROW_ERROR("free_instance", e.what());
274  }
275 }
276 
277 void Fmu::set(FmuMemory* m, size_t ind, const double* value) const {
278  try {
279  return (*this)->set(m, ind, value);
280  } catch(std::exception& e) {
281  THROW_ERROR("set", e.what());
282  }
283 }
284 
285 void Fmu::request(FmuMemory* m, size_t ind) const {
286  try {
287  return (*this)->request(m, ind);
288  } catch(std::exception& e) {
289  THROW_ERROR("request", e.what());
290  }
291 }
292 
293 int Fmu::eval(FmuMemory* m) const {
294  try {
295  return (*this)->eval(m);
296  } catch(std::exception& e) {
297  THROW_ERROR("eval", e.what());
298  }
299 }
300 
301 void Fmu::get(FmuMemory* m, size_t id, double* value) const {
302  try {
303  return (*this)->get(m, id, value);
304  } catch(std::exception& e) {
305  THROW_ERROR("get", e.what());
306  }
307 }
308 
309 void Fmu::set_fwd(FmuMemory* m, casadi_int nseed, const casadi_int* id, const double* v) const {
310  try {
311  return (*this)->set_fwd(m, nseed, id, v);
312  } catch(std::exception& e) {
313  THROW_ERROR("set_fwd", e.what());
314  }
315 }
316 
317 void Fmu::set_fwd(FmuMemory* m, size_t ind, const double* v) const {
318  try {
319  return (*this)->set_fwd(m, ind, v);
320  } catch(std::exception& e) {
321  THROW_ERROR("set_fwd", e.what());
322  }
323 }
324 
325 void Fmu::request_fwd(FmuMemory* m, casadi_int nsens, const casadi_int* id,
326  const casadi_int* wrt_id) const {
327  try {
328  return (*this)->request_fwd(m, nsens, id, wrt_id);
329  } catch(std::exception& e) {
330  THROW_ERROR("request_fwd", e.what());
331  }
332 }
333 
334 void Fmu::request_fwd(FmuMemory* m, casadi_int ind) const {
335  try {
336  return (*this)->request_fwd(m, ind);
337  } catch(std::exception& e) {
338  THROW_ERROR("request_fwd", e.what());
339  }
340 }
341 
342 int Fmu::eval_fwd(FmuMemory* m, bool independent_seeds) const {
343  try {
344  return (*this)->eval_fwd(m, independent_seeds);
345  } catch(std::exception& e) {
346  THROW_ERROR("eval_fwd", e.what());
347  }
348 }
349 
350 void Fmu::get_fwd(FmuMemory* m, casadi_int nsens, const casadi_int* id, double* v) const {
351  try {
352  return (*this)->get_fwd(m, nsens, id, v);
353  } catch(std::exception& e) {
354  THROW_ERROR("get_fwd", e.what());
355  }
356 }
357 
358 void Fmu::get_fwd(FmuMemory* m, size_t ind, double* v) const {
359  try {
360  return (*this)->get_fwd(m, ind, v);
361  } catch(std::exception& e) {
362  THROW_ERROR("get_fwd", e.what());
363  }
364 }
365 
366 void Fmu::set_adj(FmuMemory* m, casadi_int nseed, const casadi_int* id, const double* v) const {
367  try {
368  return (*this)->set_adj(m, nseed, id, v);
369  } catch(std::exception& e) {
370  THROW_ERROR("set_adj", e.what());
371  }
372 }
373 
374 void Fmu::set_adj(FmuMemory* m, size_t ind, const double* v) const {
375  try {
376  return (*this)->set_adj(m, ind, v);
377  } catch(std::exception& e) {
378  THROW_ERROR("set_adj", e.what());
379  }
380 }
381 
382 void Fmu::request_adj(FmuMemory* m, casadi_int nsens, const casadi_int* id,
383  const casadi_int* wrt_id) const {
384  try {
385  return (*this)->request_adj(m, nsens, id, wrt_id);
386  } catch(std::exception& e) {
387  THROW_ERROR("request_adj", e.what());
388  }
389 }
390 
391 void Fmu::request_adj(FmuMemory* m, casadi_int ind) const {
392  try {
393  return (*this)->request_adj(m, ind);
394  } catch(std::exception& e) {
395  THROW_ERROR("request_adj", e.what());
396  }
397 }
398 
399 int Fmu::eval_adj(FmuMemory* m) const {
400  try {
401  return (*this)->eval_adj(m);
402  } catch(std::exception& e) {
403  THROW_ERROR("eval_adj", e.what());
404  }
405 }
406 
407 void Fmu::get_adj(FmuMemory* m, casadi_int nsens, const casadi_int* id, double* v) const {
408  try {
409  return (*this)->get_adj(m, nsens, id, v);
410  } catch(std::exception& e) {
411  THROW_ERROR("get_adj", e.what());
412  }
413 }
414 
415 void Fmu::get_adj(FmuMemory* m, size_t ind, double* v) const {
416  try {
417  return (*this)->get_adj(m, ind, v);
418  } catch(std::exception& e) {
419  THROW_ERROR("get_adj", e.what());
420  }
421 }
422 
423 void Fmu::get_stats(FmuMemory* m, Dict* stats,
424  const std::vector<std::string>& name_in, const InputStruct* in) const {
425  try {
426  return (*this)->get_stats(m, stats, name_in, in);
427  } catch(std::exception& e) {
428  THROW_ERROR("get_stats", e.what());
429  }
430 }
431 
432 FmuInternal::FmuInternal(const std::string& name,
433  const std::vector<std::string>& scheme_in,
434  const std::vector<std::string>& scheme_out,
435  const std::map<std::string, std::vector<size_t>>& scheme,
436  const std::vector<std::string>& aux)
437  : name_(name), scheme_in_(scheme_in), scheme_out_(scheme_out), scheme_(scheme), aux_(aux) {
438 }
439 
441 }
442 
444  // Copy info from DaeBuilder
445  resource_ = dae->resource_;
446  fmutol_ = dae->fmutol_;
449  logging_on_ = dae->debug_;
454 
455  // Mark input indices
456  size_t numel = 0;
457  std::vector<bool> lookup(dae->n_variables(), false);
458  for (auto&& n : scheme_in_) {
459  for (size_t i : scheme_.at(n)) {
460  casadi_assert(!lookup.at(i), "Duplicate variable: " + dae->variable(i).name);
461  lookup.at(i) = true;
462  numel++;
463  }
464  }
465  // Input mappings
466  iind_.reserve(numel);
467  iind_map_.reserve(lookup.size());
468  for (size_t k = 0; k < lookup.size(); ++k) {
469  if (lookup[k]) {
470  iind_map_.push_back(iind_.size());
471  iind_.push_back(k);
472  } else {
473  iind_map_.push_back(-1);
474  }
475  }
476  // Mark output indices
477  numel = 0;
478  std::fill(lookup.begin(), lookup.end(), false);
479  for (auto&& n : scheme_out_) {
480  for (size_t i : scheme_.at(n)) {
481  casadi_assert(!lookup.at(i), "Duplicate variable: " + dae->variable(i).name);
482  lookup.at(i) = true;
483  numel++;
484  }
485  }
486  // Construct mappings
487  oind_.reserve(numel);
488  oind_map_.reserve(lookup.size());
489  for (size_t k = 0; k < lookup.size(); ++k) {
490  if (lookup[k]) {
491  oind_map_.push_back(oind_.size());
492  oind_.push_back(k);
493  } else {
494  oind_map_.push_back(-1);
495  }
496  }
497  // Inputs
498  ired_.resize(scheme_in_.size());
499  for (size_t i = 0; i < ired_.size(); ++i) {
500  auto&& s = scheme_.at(scheme_in_[i]);
501  ired_[i].resize(s.size());
502  for (size_t k = 0; k < s.size(); ++k) {
503  ired_[i][k] = iind_map_.at(s[k]);
504  }
505  }
506  // Outputs
507  ored_.resize(scheme_out_.size());
508  for (size_t i = 0; i < ored_.size(); ++i) {
509  auto&& s = scheme_.at(scheme_out_[i]);
510  ored_[i].resize(s.size());
511  for (size_t k = 0; k < s.size(); ++k) {
512  ored_[i][k] = oind_map_.at(s[k]);
513  }
514  }
515 
516  // Is there an independent variable?
517  has_independent_ = false;
518 
519  // Collect meta information for inputs
520  nominal_in_.reserve(iind_.size());
521  min_in_.reserve(iind_.size());
522  max_in_.reserve(iind_.size());
523  vn_in_.reserve(iind_.size());
524  vr_in_.reserve(iind_.size());
525  for (size_t i : iind_) {
526  const Variable& v = dae->variable(i);
527  nominal_in_.push_back(v.nominal);
528  min_in_.push_back(v.min);
529  max_in_.push_back(v.max);
530  vn_in_.push_back(v.name);
531  vr_in_.push_back(v.value_reference);
533  if (i != 0) casadi_error("Independent variable must be first input of FMU");
534  has_independent_ = true;
535  }
536  }
537  // Collect meta information for outputs
538  nominal_out_.reserve(oind_.size());
539  min_out_.reserve(oind_.size());
540  max_out_.reserve(oind_.size());
541  vn_out_.reserve(oind_.size());
542  vr_out_.reserve(oind_.size());
543  for (size_t i : oind_) {
544  const Variable& v = dae->variable(i);
545  nominal_out_.push_back(v.nominal);
546  min_out_.push_back(v.min);
547  max_out_.push_back(v.max);
548  vn_out_.push_back(v.name);
549  vr_out_.push_back(v.value_reference);
550  }
551 
552  // Numerical values for inputs
553  value_in_.resize(iind_.size());
554 
555  // Get Jacobian sparsity information
556  jac_sp_ = dae->jac_sparsity(oind_, iind_);
557 
558  // Get Hessian sparsity information
560 }
561 
562 int FmuInternal::get_adjoint_derivative(void* instance, const unsigned int* vr_out, size_t n_out,
563  const unsigned int* vr_in, size_t n_in, const double* seed, size_t n_seed,
564  double* sensitivity, size_t n_sensitivity) const {
565  casadi_error("Adjoint derivatives not supported for " + class_name());
566  return 1;
567 }
568 
570  // Load DLL
571  std::string instance_name_no_dot = instance_name_;
572  std::replace(instance_name_no_dot.begin(), instance_name_no_dot.end(), '.', '_');
573  std::string dll_path = resource_.path() + "/binaries/" + system_infix()
574  + "/" + instance_name_no_dot + dll_suffix();
575  li_ = Importer(dll_path, "dll");
576 
577  // Get FMI C functions
578  load_functions();
579 
580  // Path to resource directory
581  if (Filesystem::is_enabled()) {
582  resource_loc_ = "file://" + Filesystem::absolute(resource_.path()) + "/resources";
583  } else {
584  resource_loc_ = "file://" + resource_.path();
585  }
586 
587  // Create a temporary instance
588  void* c = instantiate();
589  // Set all values
590  if (set_values(c)) {
591  casadi_error("FmuInternal::set_values failed");
592  }
593  // Initialization mode begins
594  if (enter_initialization_mode(c)) {
595  casadi_error("FmuInternal::enter_initialization_mode failed");
596  }
597  // Get input values
598  if (get_real(c, get_ptr(vr_in_), vr_in_.size(), get_ptr(value_in_), value_in_.size())) {
599  casadi_error("FmuInternal::get_in failed");
600  }
601  // Get auxilliary variables
602  if (get_aux(c)) {
603  casadi_error("FmuInternal::get_aux failed");
604  }
605  // Free memory
606  free_instance(c);
607 }
608 
609 void FmuInternal::disp(std::ostream& stream, bool more) const {
610  (void)more; // unused
611  stream << name_ << " " << class_name();
612 }
613 
614 std::string to_string(FmuApi v) {
615  switch (v) {
616  case FmuApi::FMI2: return "fmi2";
617  case FmuApi::FMI3: return "fmi3";
618  default: break;
619  }
620  return "";
621 }
622 
623 size_t FmuInternal::index_in(const std::string& n) const {
624  // Linear search for the input
625  for (size_t i = 0; i < scheme_in_.size(); ++i) {
626  if (scheme_in_[i] == n) return i;
627  }
628  // Not found
629  casadi_error("No such input: " + n);
630  return -1;
631 }
632 
633 size_t FmuInternal::index_out(const std::string& n) const {
634  // Linear search for the input
635  for (size_t i = 0; i < scheme_out_.size(); ++i) {
636  if (scheme_out_[i] == n) return i;
637  }
638  // Not found
639  casadi_error("No such output: " + n);
640  return -1;
641 }
642 
643 Sparsity FmuInternal::jac_sparsity(const std::vector<size_t>& osub,
644  const std::vector<size_t>& isub) const {
645  // Convert to casadi_int type
646  std::vector<casadi_int> osub1(osub.begin(), osub.end());
647  std::vector<casadi_int> isub1(isub.begin(), isub.end());
648  // Index mapping (not used)
649  std::vector<casadi_int> mapping;
650  // Get selection
651  return jac_sp_.sub(osub1, isub1, mapping);
652 }
653 
654 Sparsity FmuInternal::hess_sparsity(const std::vector<size_t>& r,
655  const std::vector<size_t>& c) const {
656  // Convert to casadi_int type
657  std::vector<casadi_int> r1(r.begin(), r.end());
658  std::vector<casadi_int> c1(c.begin(), c.end());
659  // Index mapping (not used)
660  std::vector<casadi_int> mapping;
661  // Get selection
662  return hess_sp_.sub(r1, c1, mapping);
663 }
664 
665 std::vector<double> FmuInternal::all_nominal_in(size_t i) const {
666  auto&& ind = ired_.at(i);
667  std::vector<double> n;
668  n.reserve(ind.size());
669  for (size_t k : ind) n.push_back(nominal_in_.at(k));
670  return n;
671 }
672 
673 std::vector<double> FmuInternal::all_nominal_out(size_t i) const {
674  auto&& ind = ored_.at(i);
675  std::vector<double> n;
676  n.reserve(ind.size());
677  for (size_t k : ind) n.push_back(nominal_out_.at(k));
678  return n;
679 }
680 
681 std::string FmuInternal::dll_suffix() {
682 #if defined(_WIN32)
683  // Windows system
684  return ".dll";
685 #elif defined(__APPLE__)
686  // OSX
687  return ".dylib";
688 #else
689  // Linux
690  return ".so";
691 #endif
692 }
693 
694 std::string FmuInternal::desc_in(FmuMemory* m, size_t id, bool more) const {
695  // Create description
696  if (more) {
697  // Detailed description
698  std::stringstream ss;
699  ss << vn_in_[id] << " = " << m->ibuf_[id] << " (nominal " << nominal_in_[id]
700  << ", min " << min_in_[id] << ", max " << max_in_[id] << ")";
701  return ss.str();
702  } else {
703  return vn_in_[id];
704  }
705 }
706 
707 int FmuInternal::eval_fwd(FmuMemory* m, bool independent_seeds) const {
708  // Gather input and output indices
709  gather_fwd(m);
710  // Calculate derivatives using FMU directional derivative support
712  // Evaluate using AD
713  if (eval_ad(m)) return 1;
714  }
715  // Calculate derivatives using finite differences
717  // Evaluate using FD
718  if (eval_fd(m, independent_seeds)) return 1;
719  }
720  return 0;
721 }
722 
724  // Gather input and output indices
725  gather_adj(m);
726  // Quick return if nothing to be calculated
727  if (m->id_in_.size() == 0) return 0;
728  // Evaluate adjoint derivatives
730  get_ptr(m->vr_out_), m->id_out_.size(),
731  get_ptr(m->vr_in_), m->id_in_.size(),
732  get_ptr(m->d_out_), m->id_out_.size(),
733  get_ptr(m->d_in_), m->id_in_.size())) {
734  casadi_warning("FMU adjoint derivative failed");
735  return 1;
736  }
737  // Collect requested variables
738  auto it = m->d_in_.begin();
739  for (size_t id : m->id_in_) {
740  m->isens_[id] = *it++;
741  }
742  // Successful return
743  return 0;
744 }
745 
747  // Number of inputs and outputs
748  size_t n_known = m->id_in_.size();
749  size_t n_unknown = m->id_out_.size();
750  // Quick return if nothing to be calculated
751  if (n_unknown == 0) return 0;
752  // Evalute (should not be necessary)
753  if (get_real(m->instance, get_ptr(m->vr_out_), n_unknown, get_ptr(m->v_out_), n_unknown)) {
754  casadi_warning("FMU evaluation failed");
755  return 1;
756  }
757  // Evaluate directional derivatives
759  get_ptr(m->vr_out_), n_unknown,
760  get_ptr(m->vr_in_), n_known,
761  get_ptr(m->d_in_), n_known,
762  get_ptr(m->d_out_), n_unknown)) {
763  casadi_warning("FMU directional derivative failed");
764  return 1;
765  }
766  // Collect requested variables
767  auto it = m->d_out_.begin();
768  for (size_t id : m->id_out_) {
769  m->osens_[id] = *it++;
770  }
771  // Successful return
772  return 0;
773 }
774 
775 int FmuInternal::eval_fd(FmuMemory* m, bool independent_seeds) const {
776  // Number of inputs and outputs
777  size_t n_known = m->id_in_.size();
778  size_t n_unknown = m->id_out_.size();
779  // Quick return if nothing to be calculated
780  if (n_unknown == 0) return 0;
781  // Evalute (should not be necessary)
782  if (get_real(m->instance, get_ptr(m->vr_out_), n_unknown, get_ptr(m->v_out_), n_unknown)) {
783  casadi_warning("Evaluating FMU failed");
784  return 1;
785  }
786  // Make outputs dimensionless
787  for (size_t k = 0; k < n_unknown; ++k) m->v_out_[k] /= nominal_out_[m->id_out_[k]];
788  // Number of points in FD stencil
789  casadi_int n_points = n_fd_points(m->self.fd_);
790  // Offset for points
791  casadi_int offset = fd_offset(m->self.fd_);
792  // Memory for perturbed outputs
793  m->fd_out_.resize(n_points * n_unknown);
794  // Which inputs are in bounds
795  m->in_bounds_.resize(n_known);
796  // Memory for perturbed inputs
797  m->v_pert_.resize(n_known);
798  // Do any any inputs need flipping?
799  m->flip_.resize(n_known);
800  size_t first_flip = -1;
801  for (size_t i = 0; i < n_known; ++i) {
802  // Try to take step
803  double test = m->v_in_[i] + m->self.step_ * m->d_in_[i];
804  // Check if in bounds
805  size_t id = m->id_in_[i];
806  if (test >= min_in_[id] && test <= max_in_[id]) {
807  // Positive perturbation is fine
808  m->flip_[i] = false;
809  } else {
810  // Try negative direction instead
811  test = m->v_in_[i] - m->self.step_ * m->d_in_[i];
812  casadi_assert(test >= min_in_[id] && test <= max_in_[id],
813  "Cannot perturb " + vn_in_[id] + " at " + str(m->v_in_[i]) + ", min " + str(min_in_[id])
814  + ", max " + str(max_in_[id]) + ", nominal " + str(nominal_in_[id]));
815  m->flip_[i] = true;
816  if (first_flip == size_t(-1)) first_flip = i;
817  }
818  }
819  // If seeds are not independent, we have to flip the sign for all of the seeds or none
820  if (first_flip != size_t(-1) && !independent_seeds) {
821  // Flip the rest of the seeds
822  for (size_t i = 0; i < n_known; ++i) {
823  if (!m->flip_[i]) {
824  // Test negative direction
825  double test = m->v_in_[i] - m->self.step_ * m->d_in_[i];
826  size_t id = m->id_in_[i];
827  casadi_assert(test >= min_in_[id] && test <= max_in_[id],
828  "Cannot perturb both " + vn_in_[id] + " and " + vn_in_[first_flip]);
829  // Flip it too
830  m->flip_[i] = true;
831  }
832  }
833  }
834  // All perturbed outputs
835  const double* yk_all[5] = {0};
836 
837  // Calculate all perturbed outputs
838  for (casadi_int k = 0; k < n_points; ++k) {
839  // Where to save the perturbed outputs
840  double* yk = &m->fd_out_[n_unknown * k];
841  casadi_assert_dev(k < 5);
842  yk_all[k] = yk;
843  // If unperturbed output, quick return
844  if (k == offset) {
845  casadi_copy(get_ptr(m->v_out_), n_unknown, yk);
846  continue;
847  }
848  // Perturbation size
849  double pert = (k - offset) * m->self.step_;
850  // Perturb inputs, if allowed
851  for (size_t i = 0; i < n_known; ++i) {
852  // Try to take step
853  double sign = m->flip_[i] ? -1 : 1;
854  double test = m->v_in_[i] + pert * sign * m->d_in_[i];
855  // Check if in bounds
856  size_t id = m->id_in_[i];
857  m->in_bounds_[i] = test >= min_in_[id] && test <= max_in_[id];
858  // Take step, if allowed
859  m->v_pert_[i] = m->in_bounds_[i] ? test : m->v_in_[i];
860  }
861  // Pass perturbed inputs to FMU
862  if (set_real(m->instance, get_ptr(m->vr_in_), n_known, get_ptr(m->v_pert_), n_known)) {
863  casadi_warning("Setting FMU variables failed");
864  return 1;
865  }
866  // Evaluate perturbed FMU
867  if (get_real(m->instance, get_ptr(m->vr_out_), n_unknown, yk, n_unknown)) {
868  casadi_warning("Evaluation failed");
869  return 1;
870  }
871  // Post-process yk if there was any scaling
872  if (independent_seeds) {
873  for (size_t i = 0; i < n_unknown; ++i) {
874  // Variable id
875  size_t id = m->id_out_[i];
876  // Differentiation with respect to what variable
877  size_t wrt_id = m->wrt_.at(id);
878  // Find the corresponding input variable
879  size_t wrt_i;
880  for (wrt_i = 0; wrt_i < n_known; ++wrt_i) {
881  if (m->id_in_[wrt_i] == wrt_id) break;
882  }
883  // Check if in bounds
884  if (m->in_bounds_.at(wrt_i)) {
885  // Input was in bounds: Keep output, make dimensionless
886  yk[i] /= nominal_out_[m->id_out_[i]];
887  } else {
888  // Input was out of bounds: Discard output
889  yk[i] = nan;
890  }
891  }
892  }
893  }
894  // Restore FMU inputs
895  if (set_real(m->instance, get_ptr(m->vr_in_), n_known, get_ptr(m->v_in_), n_known)) {
896  casadi_warning("Setting FMU variables failed");
897  return 1;
898  }
899  // Step size
900  double h = m->self.step_;
901 
902  // Calculate FD approximation
903  finite_diff(m->self.fd_, yk_all, get_ptr(m->d_out_), h, n_unknown, eps);
904 
905  // If seeds are dependent, quick return
906  if (!independent_seeds) {
907  // Simpy copy the results to output (no validation)
908  for (size_t ind = 0; ind < m->id_out_.size(); ++ind) {
909  m->osens_[m->id_out_[ind]] = m->d_out_[ind];
910  }
911  return 0;
912  }
913 
914  // Collect requested variables
915  for (size_t ind = 0; ind < m->id_out_.size(); ++ind) {
916  // Variable id
917  size_t id = m->id_out_[ind];
918  // With respect to what variable
919  size_t wrt = m->wrt_[id];
920  // Find the corresponding input variable
921  size_t wrt_i;
922  for (wrt_i = 0; wrt_i < n_known; ++wrt_i) {
923  if (m->id_in_[wrt_i] == wrt) break;
924  }
925  // Nominal value
926  double n = nominal_out_[id];
927  // Get the value
928  double d_fd = m->d_out_[ind] * n;
929  // Correct sign, if necessary
930  if (m->flip_[wrt_i]) d_fd = -d_fd;
931  // Use FD instead of AD or to compare with AD
932  if (m->self.validate_forward_) {
933  // Value to compare with
934  double d_ad = m->osens_[id];
935  // Nominal value used as seed
936  d_ad /= nominal_in_[wrt];
937  d_fd /= nominal_in_[wrt];
938  // Is it a not a number?
939  bool d_is_nan = d_ad != d_ad;
940  // Magnitude of derivatives
941  double d_max = std::fmax(std::fabs(d_fd), std::fabs(d_ad));
942  // Check if NaN or error exceeds thresholds
943  if (d_is_nan || (d_max > n * m->self.abstol_
944  && std::fabs(d_ad - d_fd) > d_max * m->self.reltol_)) {
945  // Offset for printing the stencil
946  double off = m->fd_out_.at(ind + offset * n_unknown);
947  // Warning or add to file
948  std::stringstream ss;
949  if (m->self.validate_ad_file_.empty()) {
950  // Issue warning
951  ss << (d_is_nan ? "NaN" : "Inconsistent") << " derivatives of " << vn_out_[id]
952  << " w.r.t. " << desc_in(m, wrt) << ", got " << d_ad
953  << " for AD vs. " << d_fd << " for FD[" << to_string(m->self.fd_) << "].";
954  // Print the stencil:
955  ss << "\nValues for step size " << h << ": " << (n * off) << " + [";
956  for (casadi_int k = 0; k < n_points; ++k) {
957  if (k > 0) ss << ", ";
958  ss << (n * (m->fd_out_.at(ind + k * n_unknown) - off));
959  }
960  ss << "]";
961  // Issue warning
962  casadi_warning(ss.str());
963  } else {
964  // Output
965  ss << vn_out_[id] << " ";
966  // Input
967  ss << vn_in_[wrt] << " ";
968  // Value
969  ss << m->ibuf_[wrt] << " ";
970  // Noninal
971  ss << nominal_in_[wrt] << " ";
972  // Min
973  ss << min_in_[wrt] << " ";
974  // Max
975  ss << max_in_[wrt] << " ";
976  // AD
977  ss << d_ad << " ";
978  // FD
979  ss << d_fd << " ";
980  // Step
981  ss << h << " ";
982  // Offset
983  ss << off << " ";
984  // Stencil
985  ss << "[";
986  for (casadi_int k = 0; k < n_points; ++k) {
987  if (k > 0) ss << ",";
988  ss << (n * (m->fd_out_.at(ind + k * n_unknown) - off));
989  }
990  ss << "]" << std::endl;
991  // Append to file
992  std::ofstream valfile;
993  valfile.open(m->self.validate_ad_file_, std::ios_base::app);
994  valfile << ss.str();
995  }
996  }
997  } else {
998  // Use instead of AD
999  m->osens_[id] = d_fd;
1000  }
1001  }
1002  // Successful return
1003  return 0;
1004 }
1005 
1006 void FmuInternal::get_fwd(FmuMemory* m, casadi_int nsens, const casadi_int* id, double* v) const {
1007  for (casadi_int i = 0; i < nsens; ++i) {
1008  *v++ = m->osens_.at(*id++);
1009  }
1010 }
1011 
1012 void FmuInternal::get_fwd(FmuMemory* m, size_t ind, double* v) const {
1013  // Quick return if not needed
1014  if (!v) return;
1015  // Retrieve all sensitivities FIXME(@jaeandersson): should use compatible types
1016  for (size_t id : ored_[ind]) {
1017  casadi_int id2 = id;
1018  get_fwd(m, 1, &id2, v++);
1019  }
1020 }
1021 
1022 void FmuInternal::set_adj(FmuMemory* m, casadi_int nseed,
1023  const casadi_int* id, const double* v) const {
1024  for (casadi_int i = 0; i < nseed; ++i) {
1025  m->osens_.at(*id) = *v++;
1026  m->omarked_.at(*id) = true;
1027  id++;
1028  }
1029 }
1030 
1031 void FmuInternal::set_adj(FmuMemory* m, size_t ind, const double* v) const {
1032  // If seeds are zero, no need to add to seed buffers
1033  if (!v) return;
1034  // Pass all seeds FIXME(@jaeandersson): should use compatible types
1035  for (size_t id : ored_[ind]) {
1036  casadi_int id2 = id;
1037  set_adj(m, 1, &id2, v++);
1038  }
1039 }
1040 
1041 void FmuInternal::request_adj(FmuMemory* m, casadi_int nsens, const casadi_int* id,
1042  const casadi_int* wrt_id) const {
1043  for (casadi_int i = 0; i < nsens; ++i) {
1044  m->imarked_.at(*id) = true;
1045  m->wrt_.at(*id) = *wrt_id++;
1046  id++;
1047  }
1048 }
1049 
1050 void FmuInternal::request_adj(FmuMemory* m, casadi_int ind) const {
1051  // Request all sensitivities FIXME(@jaeandersson): should use compatible types
1052  casadi_int wrt_id = -1;
1053  for (size_t id : ired_[ind]) {
1054  casadi_int id2 = id;
1055  request_adj(m, 1, &id2, &wrt_id);
1056  }
1057 }
1058 
1059 void FmuInternal::get_adj(FmuMemory* m, casadi_int nsens, const casadi_int* id, double* v) const {
1060  for (casadi_int i = 0; i < nsens; ++i) {
1061  *v++ = m->isens_.at(*id++);
1062  }
1063 }
1064 
1065 void FmuInternal::get_adj(FmuMemory* m, size_t ind, double* v) const {
1066  // Quick return if not needed
1067  if (!v) return;
1068  // Retrieve all sensitivities FIXME(@jaeandersson): should use compatible types
1069  for (size_t id : ired_[ind]) {
1070  casadi_int id2 = id;
1071  get_adj(m, 1, &id2, v++);
1072  }
1073 }
1074 
1075 int FmuInternal::discrete_states_iter(void* instance) const {
1076  // Helper function: update_discrete_states
1077  EventMemory eventmem;
1078  const size_t max_update_iter = 10;
1079  for (size_t update_iter = 0; update_iter < max_update_iter; ++update_iter) {
1080  if (update_discrete_states(instance, &eventmem)) {
1081  casadi_warning("update_discrete_states");
1082  return 1;
1083  }
1084  // Not implemented
1085  if (eventmem.discrete_states_need_update)
1086  casadi_warning("Discrete state update not implemented");
1087  if (eventmem.terminate_simulation)
1088  casadi_warning("Terminate solution not implemented");
1090  casadi_warning("Update of nominals of states not implemented");
1092  casadi_warning("Update of values of continuous states not implemented");
1093  if (eventmem.next_event_time_defined)
1094  casadi_warning("Ignoring next time defined: " + std::to_string(eventmem.next_event_time));
1095  // Successful return
1096  if (!eventmem.discrete_states_need_update) {
1097  return 0;
1098  }
1099  }
1100  // Too many iterations
1101  casadi_warning("Discrete state update failed");
1102  return 1;
1103 }
1104 
1106  // Ensure not already instantiated
1107  casadi_assert(m->instance == 0, "Already instantiated");
1108  // Create instance
1109  m->instance = instantiate();
1110  // Set all values
1111  if (set_values(m->instance)) {
1112  casadi_warning("FmuInternal::set_values failed");
1113  return 1;
1114  }
1115  // Initialization mode begins
1116  if (enter_initialization_mode(m->instance)) return 1;
1117  // Initialization mode ends
1118  if (exit_initialization_mode(m->instance)) return 1;
1119  // Initial event iteration
1120  if (discrete_states_iter(m->instance)) return 1;
1121  // Continuous-time mode
1122  if (enter_continuous_time_mode(m->instance)) return 1;
1123  // Allocate/reset input buffer
1124  m->ibuf_.resize(iind_.size());
1125  std::fill(m->ibuf_.begin(), m->ibuf_.end(), casadi::nan);
1126  // Allocate/reset output buffer
1127  m->obuf_.resize(oind_.size());
1128  std::fill(m->obuf_.begin(), m->obuf_.end(), casadi::nan);
1129  // Maximum input or output
1130  size_t max_io = std::max(iind_.size(), oind_.size());
1131  // Allocate/reset seeds
1132  m->isens_.resize(max_io);
1133  std::fill(m->isens_.begin(), m->isens_.end(), 0);
1134  // Allocate/reset sensitivities
1135  m->osens_.resize(max_io);
1136  std::fill(m->osens_.begin(), m->osens_.end(), 0);
1137  // Allocate/reset changed
1138  m->imarked_.resize(max_io);
1139  std::fill(m->imarked_.begin(), m->imarked_.end(), false);
1140  // Allocate/reset requested
1141  m->omarked_.resize(max_io);
1142  std::fill(m->omarked_.begin(), m->omarked_.end(), false);
1143  // Also allocate memory for corresponding Jacobian entry (for debugging)
1144  m->wrt_.resize(max_io);
1145  // Successful return
1146  return 0;
1147 }
1148 
1149 void FmuInternal::set(FmuMemory* m, size_t ind, const double* value) const {
1150  if (value) {
1151  // Argument is given
1152  for (size_t id : ired_[ind]) {
1153  if (*value != m->ibuf_.at(id)) {
1154  m->ibuf_.at(id) = *value;
1155  m->imarked_.at(id) = true;
1156  }
1157  value++;
1158  }
1159  } else {
1160  // Argument is null - all zeros
1161  for (size_t id : ired_[ind]) {
1162  if (0 != m->ibuf_.at(id)) {
1163  m->ibuf_.at(id) = 0;
1164  m->imarked_.at(id) = true;
1165  }
1166  }
1167  }
1168 }
1169 
1170 void FmuInternal::request(FmuMemory* m, size_t ind) const {
1171  for (size_t id : ored_[ind]) {
1172  // Mark as requested
1173  m->omarked_.at(id) = true;
1174  // Also log corresponding input index
1175  m->wrt_.at(id) = -1;
1176  }
1177 }
1178 
1180  // Gather inputs and outputs
1181  gather_io(m);
1182  // Number of inputs and outputs
1183  size_t n_set = m->id_in_.size();
1184  size_t n_out = m->id_out_.size();
1185  // Set all variables
1186  if (set_real(m->instance, get_ptr(m->vr_in_), n_set, get_ptr(m->v_in_), n_set)) {
1187  casadi_warning("Setting FMU variables failed");
1188  return 1;
1189  }
1190  // Quick return if nothing requested
1191  if (n_out == 0) return 0;
1192  // Calculate all variables
1193  m->v_out_.resize(n_out);
1194  if (get_real(m->instance, get_ptr(m->vr_out_), n_out, get_ptr(m->v_out_), n_out)) {
1195  casadi_warning("Evaluation failed");
1196  return 1;
1197  }
1198  // Collect requested variables
1199  auto it = m->v_out_.begin();
1200  for (size_t id : m->id_out_) {
1201  m->obuf_[id] = *it++;
1202  }
1203  // Successful return
1204  return 0;
1205 }
1206 
1207 void FmuInternal::get(FmuMemory* m, size_t ind, double* value) const {
1208  // Save to return
1209  for (size_t id : ored_[ind]) {
1210  *value++ = m->obuf_.at(id);
1211  }
1212 }
1213 
1214 void FmuInternal::set_fwd(FmuMemory* m, casadi_int nseed,
1215  const casadi_int* id, const double* v) const {
1216  for (casadi_int i = 0; i < nseed; ++i) {
1217  m->isens_.at(*id) = *v++;
1218  m->imarked_.at(*id) = true;
1219  id++;
1220  }
1221 }
1222 
1223 void FmuInternal::set_fwd(FmuMemory* m, size_t ind, const double* v) const {
1224  // If seeds are zero, no need to add to seed buffers
1225  if (!v) return;
1226  // Pass all seeds FIXME(@jaeandersson): should use compatible types
1227  for (size_t id : ired_[ind]) {
1228  casadi_int id2 = id;
1229  set_fwd(m, 1, &id2, v++);
1230  }
1231 }
1232 
1233 void FmuInternal::request_fwd(FmuMemory* m, casadi_int nsens, const casadi_int* id,
1234  const casadi_int* wrt_id) const {
1235  for (casadi_int i = 0; i < nsens; ++i) {
1236  m->omarked_.at(*id) = true;
1237  m->wrt_.at(*id) = *wrt_id++;
1238  id++;
1239  }
1240 }
1241 
1242 void FmuInternal::request_fwd(FmuMemory* m, casadi_int ind) const {
1243  // Request all sensitivities FIXME(@jaeandersson): should use compatible types
1244  casadi_int wrt_id = -1;
1245  for (size_t id : ored_[ind]) {
1246  casadi_int id2 = id;
1247  request_fwd(m, 1, &id2, &wrt_id);
1248  }
1249 }
1250 
1252  // Collect input indices and corresponding value references and values
1253  m->id_in_.clear();
1254  m->vr_in_.clear();
1255  m->v_in_.clear();
1256  for (size_t id = 0; id < m->imarked_.size(); ++id) {
1257  if (m->imarked_[id]) {
1258  m->id_in_.push_back(id);
1259  m->vr_in_.push_back(vr_in_[id]);
1260  m->v_in_.push_back(m->ibuf_[id]);
1261  m->imarked_[id] = false;
1262  }
1263  }
1264  // Collect output indices, corresponding value references
1265  m->id_out_.clear();
1266  m->vr_out_.clear();
1267  for (size_t id = 0; id < m->omarked_.size(); ++id) {
1268  if (m->omarked_[id]) {
1269  m->id_out_.push_back(id);
1270  m->vr_out_.push_back(vr_out_[id]);
1271  m->omarked_[id] = false;
1272  }
1273  }
1274 }
1275 
1277  // Gather input and output indices
1278  gather_io(m);
1279  // Number of inputs and outputs
1280  size_t n_known = m->id_in_.size();
1281  size_t n_unknown = m->id_out_.size();
1282  // Get/clear seeds
1283  m->d_in_.clear();
1284  for (size_t id : m->id_in_) {
1285  m->d_in_.push_back(m->isens_[id]);
1286  m->isens_[id] = 0;
1287  }
1288  // Ensure at least one seed
1289  casadi_assert(n_known != 0, "No seeds");
1290  // Allocate result vectors
1291  m->v_out_.resize(n_unknown);
1292  m->d_out_.resize(n_unknown);
1293 }
1294 
1296  // Gather input and output indices
1297  gather_io(m);
1298  // Number of inputs and outputs
1299  size_t n_known = m->id_in_.size();
1300  size_t n_unknown = m->id_out_.size();
1301  // Get/clear seeds
1302  m->d_out_.clear();
1303  for (size_t id : m->id_out_) {
1304  m->d_out_.push_back(m->osens_[id]);
1305  m->osens_[id] = 0;
1306  }
1307  // Ensure at least one seed
1308  casadi_assert(n_unknown != 0, "No seeds");
1309  // Allocate result vectors
1310  m->v_in_.resize(n_known);
1311  m->d_in_.resize(n_known);
1312 }
1313 
1315  return (*this)->serialize(s);
1316 }
1317 
1320 }
1321 
1323  Fmu ret;
1324  ret.own(node);
1325  return ret;
1326 }
1327 
1329  serialize_type(s);
1330  serialize_body(s);
1331 }
1332 
1334  s.pack("FmuInternal::type", class_name());
1335 }
1336 
1338  s.version("FmuInternal", 3);
1339  s.pack("FmuInternal::name", name_);
1340  s.pack("FmuInternal::scheme_in", scheme_in_);
1341  s.pack("FmuInternal::scheme_out", scheme_out_);
1342  s.pack("FmuInternal::scheme", scheme_);
1343  s.pack("FmuInternal::aux", aux_);
1344  s.pack("FmuInternal::iind", iind_);
1345  s.pack("FmuInternal::iind_map", iind_map_);
1346  s.pack("FmuInternal::oind", oind_);
1347  s.pack("FmuInternal::oind_map", oind_map_);
1348  s.pack("FmuInternal::nominal_in", nominal_in_);
1349  s.pack("FmuInternal::nominal_out", nominal_out_);
1350  s.pack("FmuInternal::min_in", min_in_);
1351  s.pack("FmuInternal::min_out", min_out_);
1352  s.pack("FmuInternal::max_in", max_in_);
1353  s.pack("FmuInternal::max_out", max_out_);
1354  s.pack("FmuInternal::vn_in", vn_in_);
1355  s.pack("FmuInternal::vn_out", vn_out_);
1356  s.pack("FmuInternal::vr_in", vr_in_);
1357  s.pack("FmuInternal::vr_out", vr_out_);
1358 
1359  s.pack("FmuInternal::value_in", value_in_);
1360  s.pack("FmuInternal::ired", ired_);
1361  s.pack("FmuInternal::ored", ored_);
1362  s.pack("FmuInternal::jac_sp", jac_sp_);
1363  s.pack("FmuInternal::hess_sp", hess_sp_);
1364 
1365  s.pack("FmuInternal::resource", resource_);
1366  s.pack("FmuInternal::fmutol", fmutol_);
1367  s.pack("FmuInternal::instance_name", instance_name_);
1368  s.pack("FmuInternal::instantiation_token", instantiation_token_);
1369  s.pack("FmuInternal::logging_on", logging_on_);
1370  s.pack("FmuInternal::number_of_event_indicators", number_of_event_indicators_);
1371  s.pack("FmuInternal::provides_directional_derivatives", provides_directional_derivatives_);
1372  s.pack("FmuInternal::provides_adjoint_derivatives", provides_adjoint_derivatives_);
1373  s.pack("FmuInternal::can_be_instantiated_only_once_per_process",
1375 }
1376 
1378  s.version("FmuInternal", 3);
1379  s.unpack("FmuInternal::name", name_);
1380  s.unpack("FmuInternal::scheme_in", scheme_in_);
1381  s.unpack("FmuInternal::scheme_out", scheme_out_);
1382  s.unpack("FmuInternal::scheme", scheme_);
1383  s.unpack("FmuInternal::aux", aux_);
1384  s.unpack("FmuInternal::iind", iind_);
1385  s.unpack("FmuInternal::iind_map", iind_map_);
1386  s.unpack("FmuInternal::oind", oind_);
1387  s.unpack("FmuInternal::oind_map", oind_map_);
1388  s.unpack("FmuInternal::nominal_in", nominal_in_);
1389  s.unpack("FmuInternal::nominal_out", nominal_out_);
1390  s.unpack("FmuInternal::min_in", min_in_);
1391  s.unpack("FmuInternal::min_out", min_out_);
1392  s.unpack("FmuInternal::max_in", max_in_);
1393  s.unpack("FmuInternal::max_out", max_out_);
1394  s.unpack("FmuInternal::vn_in", vn_in_);
1395  s.unpack("FmuInternal::vn_out", vn_out_);
1396  s.unpack("FmuInternal::vr_in", vr_in_);
1397  s.unpack("FmuInternal::vr_out", vr_out_);
1398 
1399  s.unpack("FmuInternal::value_in", value_in_);
1400  s.unpack("FmuInternal::ired", ired_);
1401  s.unpack("FmuInternal::ored", ored_);
1402  s.unpack("FmuInternal::jac_sp", jac_sp_);
1403  s.unpack("FmuInternal::hess_sp", hess_sp_);
1404 
1405  s.unpack("FmuInternal::resource", resource_);
1406  s.unpack("FmuInternal::fmutol", fmutol_);
1407  s.unpack("FmuInternal::instance_name", instance_name_);
1408  s.unpack("FmuInternal::instantiation_token", instantiation_token_);
1409  s.unpack("FmuInternal::logging_on", logging_on_);
1410  s.unpack("FmuInternal::number_of_event_indicators", number_of_event_indicators_);
1411  s.unpack("FmuInternal::provides_directional_derivatives", provides_directional_derivatives_);
1412  s.unpack("FmuInternal::provides_adjoint_derivatives", provides_adjoint_derivatives_);
1413  s.unpack("FmuInternal::can_be_instantiated_only_once_per_process",
1415 }
1416 
1418  std::string class_name;
1419  s.unpack("FmuInternal::type", class_name);
1420  if (class_name=="Fmu2") {
1421 #ifdef WITH_FMI2
1422  return Fmu2::deserialize(s);
1423 #else
1424  casadi_error("CasADi was not compiled with WITH_FMI2=ON.");
1425 #endif // WITH_FMI2
1426  } else if (class_name=="Fmu3") {
1427 #ifdef WITH_FMI3
1428  casadi_error("Not implemented");
1429 #else
1430  casadi_error("CasADi was not compiled with WITH_FMI2=ON.");
1431 #endif // WITH_FMI3
1432  } else {
1433  casadi_error("Cannot deserialize type '" + class_name + "'");
1434  }
1435 }
1436 
1437 } // namespace casadi
Sparsity jac_sparsity(const std::vector< size_t > &oind, const std::vector< size_t > &iind) const
Get Jacobian sparsity.
size_t n_variables() const
Length of variables array.
Variable & variable(size_t ind)
Sparsity hess_sparsity(const std::vector< size_t > &oind, const std::vector< size_t > &iind) const
Get what is known of the Hessian sparsity.
Helper class for Serialization.
void unpack(Sparsity &e)
Reconstruct an object from the input stream.
void version(const std::string &name, int v)
static std::string absolute(const std::string &path)
Definition: filesystem.cpp:78
static bool is_enabled()
Definition: filesystem.cpp:83
Interface to a binary FMU, adhering to FMI version 2.0.
Definition: fmu2.hpp:42
static Fmu2 * deserialize(DeserializingStream &s)
Definition: fmu2.cpp:468
Interface to a binary FMU, adhering to FMI version 2.0.
Definition: fmu3.hpp:42
std::string validate_ad_file_
Interface to binary FMU.
Definition: fmu_impl.hpp:60
std::vector< std::string > scheme_out_
Definition: fmu_impl.hpp:277
std::string instantiation_token_
Definition: fmu_impl.hpp:293
std::vector< double > max_in_
Definition: fmu_impl.hpp:319
std::string name_
Instance name.
Definition: fmu_impl.hpp:274
virtual int get_directional_derivative(void *instance, const unsigned int *vr_out, size_t n_out, const unsigned int *vr_in, size_t n_in, const double *seed, size_t n_seed, double *sensitivity, size_t n_sensitivity) const =0
bool provides_adjoint_derivatives_
Definition: fmu_impl.hpp:302
std::vector< double > all_nominal_in(size_t i) const
Retreive nominal values.
Definition: fmu.cpp:665
std::vector< unsigned int > vr_out_
Definition: fmu_impl.hpp:321
std::string desc_in(FmuMemory *m, size_t id, bool more=true) const
Definition: fmu.cpp:694
virtual void free_instance(void *c) const =0
std::vector< std::string > vn_out_
Definition: fmu_impl.hpp:320
void set(FmuMemory *m, size_t ind, const double *value) const
Definition: fmu.cpp:1149
std::vector< size_t > iind_
Definition: fmu_impl.hpp:311
void serialize(SerializingStream &s) const
Definition: fmu.cpp:1328
void finalize()
Definition: fmu.cpp:569
void disp(std::ostream &stream, bool more) const override
Print.
Definition: fmu.cpp:609
FmuInternal(const std::string &name, const std::vector< std::string > &scheme_in, const std::vector< std::string > &scheme_out, const std::map< std::string, std::vector< size_t >> &scheme, const std::vector< std::string > &aux)
Definition: fmu.cpp:432
std::vector< std::vector< size_t > > ired_
Definition: fmu_impl.hpp:327
std::vector< std::string > aux_
Definition: fmu_impl.hpp:281
std::vector< std::vector< size_t > > ored_
Definition: fmu_impl.hpp:327
std::map< std::string, std::vector< size_t > > scheme_
Definition: fmu_impl.hpp:278
int discrete_states_iter(void *instance) const
Definition: fmu.cpp:1075
virtual void serialize_body(SerializingStream &s) const
Definition: fmu.cpp:1337
virtual int get_aux(void *instance)=0
virtual int get_real(void *instance, const unsigned int *vr, size_t n_vr, double *values, size_t n_values) const =0
void get_adj(FmuMemory *m, casadi_int nsens, const casadi_int *id, double *v) const
Definition: fmu.cpp:1059
void gather_fwd(FmuMemory *m) const
Definition: fmu.cpp:1276
std::vector< double > nominal_out_
Definition: fmu_impl.hpp:317
void request(FmuMemory *m, size_t ind) const
Definition: fmu.cpp:1170
void gather_io(FmuMemory *m) const
Definition: fmu.cpp:1251
int eval_fd(FmuMemory *m, bool independent_seeds) const
Definition: fmu.cpp:775
std::vector< std::string > vn_in_
Definition: fmu_impl.hpp:320
std::vector< size_t > oind_
Definition: fmu_impl.hpp:311
virtual void load_functions()=0
~FmuInternal() override
Destructor.
Definition: fmu.cpp:440
virtual int update_discrete_states(void *instance, EventMemory *eventmem) const =0
static FmuInternal * deserialize(DeserializingStream &s)
Definition: fmu.cpp:1417
void set_fwd(FmuMemory *m, casadi_int nseed, const casadi_int *id, const double *v) const
Definition: fmu.cpp:1214
virtual void serialize_type(SerializingStream &s) const
Definition: fmu.cpp:1333
void request_adj(FmuMemory *m, casadi_int nsens, const casadi_int *id, const casadi_int *wrt_id) const
Definition: fmu.cpp:1041
std::vector< unsigned int > vr_in_
Definition: fmu_impl.hpp:321
virtual void * instantiate() const =0
virtual int exit_initialization_mode(void *instance) const =0
virtual int get_adjoint_derivative(void *instance, const unsigned int *vr_out, size_t n_out, const unsigned int *vr_in, size_t n_in, const double *seed, size_t n_seed, double *sensitivity, size_t n_sensitivity) const
Definition: fmu.cpp:562
virtual int set_values(void *instance) const =0
std::vector< double > min_out_
Definition: fmu_impl.hpp:318
bool can_be_instantiated_only_once_per_process_
Definition: fmu_impl.hpp:305
int eval_ad(FmuMemory *m) const
Definition: fmu.cpp:746
std::vector< double > nominal_in_
Definition: fmu_impl.hpp:317
std::vector< double > max_out_
Definition: fmu_impl.hpp:319
size_t index_out(const std::string &n) const
Definition: fmu.cpp:633
std::string resource_loc_
Definition: fmu_impl.hpp:284
std::vector< double > min_in_
Definition: fmu_impl.hpp:318
static std::string dll_suffix()
Definition: fmu.cpp:681
void set_adj(FmuMemory *m, casadi_int nseed, const casadi_int *id, const double *v) const
Definition: fmu.cpp:1022
std::vector< double > value_in_
Definition: fmu_impl.hpp:324
void get(FmuMemory *m, size_t id, double *value) const
Definition: fmu.cpp:1207
virtual int set_real(void *instance, const unsigned int *vr, size_t n_vr, const double *values, size_t n_values) const =0
casadi_int number_of_event_indicators_
Definition: fmu_impl.hpp:299
std::vector< double > all_nominal_out(size_t i) const
Retreive nominal values.
Definition: fmu.cpp:673
Importer li_
DLL.
Definition: fmu_impl.hpp:308
int eval_fwd(FmuMemory *m, bool independent_seeds) const
Definition: fmu.cpp:707
virtual void init(const DaeBuilderInternal *dae)
Definition: fmu.cpp:443
size_t n_out() const
Get the number of scheme outputs.
Definition: fmu_impl.hpp:129
void get_fwd(FmuMemory *m, casadi_int nsens, const casadi_int *id, double *v) const
Definition: fmu.cpp:1006
void gather_adj(FmuMemory *m) const
Definition: fmu.cpp:1295
int init_mem(FmuMemory *m) const
Initalize memory block.
Definition: fmu.cpp:1105
virtual int enter_continuous_time_mode(void *instance) const =0
size_t index_in(const std::string &n) const
Definition: fmu.cpp:623
void request_fwd(FmuMemory *m, casadi_int nsens, const casadi_int *id, const casadi_int *wrt_id) const
Definition: fmu.cpp:1233
virtual int enter_initialization_mode(void *instance) const =0
Sparsity hess_sparsity(const std::vector< size_t > &r, const std::vector< size_t > &c) const
Definition: fmu.cpp:654
std::vector< size_t > oind_map_
Definition: fmu_impl.hpp:311
Sparsity jac_sparsity(const std::vector< size_t > &osub, const std::vector< size_t > &isub) const
Definition: fmu.cpp:643
std::vector< std::string > scheme_in_
Definition: fmu_impl.hpp:277
std::string instance_name_
Definition: fmu_impl.hpp:290
bool provides_directional_derivatives_
Definition: fmu_impl.hpp:302
virtual std::string system_infix() const =0
int eval_adj(FmuMemory *m) const
Definition: fmu.cpp:723
int eval(FmuMemory *m) const
Definition: fmu.cpp:1179
std::vector< size_t > iind_map_
Definition: fmu_impl.hpp:311
Interface to binary FMU.
Definition: fmu.hpp:61
void set(FmuMemory *m, size_t ind, const double *value) const
Definition: fmu.cpp:277
void get_fwd(FmuMemory *m, casadi_int nsens, const casadi_int *id, double *v) const
Definition: fmu.cpp:350
const std::vector< size_t > & ored(size_t ind) const
Definition: fmu.cpp:155
static Fmu deserialize(DeserializingStream &s)
Deserialize with type disambiguation.
Definition: fmu.cpp:1318
size_t index_out(const std::string &n) const
Definition: fmu.cpp:139
int eval_adj(FmuMemory *m) const
Definition: fmu.cpp:399
Fmu()
Default constructor.
Definition: fmu.cpp:48
double nominal_out(size_t ind) const
Definition: fmu.cpp:171
void get_stats(FmuMemory *m, Dict *stats, const std::vector< std::string > &name_in, const InputStruct *in) const
Get stats.
Definition: fmu.cpp:423
Sparsity hess_sparsity(const std::vector< size_t > &r, const std::vector< size_t > &c) const
Definition: fmu.cpp:252
bool can_be_instantiated_only_once_per_process() const
Does the FMU declare restrictions on instantiation?
Definition: fmu.cpp:235
std::vector< double > all_nominal_out(size_t ind) const
Definition: fmu.cpp:203
bool provides_adjoint_derivatives() const
Does the FMU provide support for adjoint directional derivatives.
Definition: fmu.cpp:227
Sparsity jac_sparsity(const std::vector< size_t > &osub, const std::vector< size_t > &isub) const
Definition: fmu.cpp:243
int eval_fwd(FmuMemory *m, bool independent_seeds) const
Definition: fmu.cpp:342
double nominal_in(size_t ind) const
Definition: fmu.cpp:163
size_t index_in(const std::string &n) const
Definition: fmu.cpp:131
void get_adj(FmuMemory *m, casadi_int nsens, const casadi_int *id, double *v) const
Definition: fmu.cpp:407
double max_in(size_t ind) const
Definition: fmu.cpp:187
const std::string & name() const
Name of the instance.
Definition: fmu.cpp:97
void set_fwd(FmuMemory *m, casadi_int nseed, const casadi_int *id, const double *v) const
Definition: fmu.cpp:309
static Fmu create(FmuInternal *node)
Create from node.
Definition: fmu.cpp:1322
size_t n_out() const
Get the number of scheme outputs.
Definition: fmu.cpp:123
const std::string & instance_name() const
Name of the FMU.
Definition: fmu.cpp:106
std::vector< double > all_nominal_in(size_t ind) const
Definition: fmu.cpp:195
double min_in(size_t ind) const
Definition: fmu.cpp:179
FmuInternal * get() const
Definition: fmu.cpp:93
FmuInternal * operator->()
Definition: fmu.cpp:85
void set_adj(FmuMemory *m, casadi_int nseed, const casadi_int *id, const double *v) const
Definition: fmu.cpp:366
int init_mem(FmuMemory *m) const
Initalize memory block.
Definition: fmu.cpp:260
bool provides_directional_derivatives() const
Does the FMU provide support for forward directional derivatives.
Definition: fmu.cpp:219
int eval(FmuMemory *m) const
Definition: fmu.cpp:293
const std::vector< size_t > & ired(size_t ind) const
Definition: fmu.cpp:147
void request_adj(FmuMemory *m, casadi_int nsens, const casadi_int *id, const casadi_int *wrt_id) const
Definition: fmu.cpp:382
size_t n_in() const
Get the number of scheme inputs.
Definition: fmu.cpp:115
void free_instance(void *instance) const
Definition: fmu.cpp:269
void request(FmuMemory *m, size_t ind) const
Definition: fmu.cpp:285
void request_fwd(FmuMemory *m, casadi_int nsens, const casadi_int *id, const casadi_int *wrt_id) const
Definition: fmu.cpp:325
std::string desc_in(FmuMemory *m, size_t id, bool more=true) const
Definition: fmu.cpp:211
void serialize(SerializingStream &s) const
Serialize an object.
Definition: fmu.cpp:1314
SharedObjectInternal * get() const
Get a const pointer to the node.
SharedObjectInternal * operator->() const
Access a member function or object.
Importer.
Definition: importer.hpp:86
const std::string & path() const
Get path for a consumer.
Definition: resource.cpp:70
Helper class for Serialization.
void version(const std::string &name, int v)
void pack(const Sparsity &e)
Serializes an object to the output stream.
virtual std::string class_name() const =0
Readable name of the internal class.
General sparsity class.
Definition: sparsity.hpp:106
Sparsity sub(const std::vector< casadi_int > &rr, const std::vector< casadi_int > &cc, std::vector< casadi_int > &mapping, bool ind1=false) const
Get a submatrix.
Definition: sparsity.cpp:334
The casadi namespace.
Definition: archiver.cpp:28
const double eps
Machine epsilon.
Definition: calculus.hpp:56
FmuApi
Which C API.
Definition: fmu.hpp:46
casadi_int n_fd_points(FdMode v)
Length of FD stencil, including unperturbed input.
casadi_int fd_offset(FdMode v)
Offset for FD stencil, i.e. index of unperturbed input.
double sign(double x)
Sign function, note that sign(nan) == nan.
Definition: calculus.hpp:264
void casadi_copy(const T1 *x, casadi_int n, T1 *y)
COPY: y <-x.
std::string str(const T &v)
String representation, any type.
GenericType::Dict Dict
C++ equivalent of Python's dict or MATLAB's struct.
std::string to_string(TypeFmi2 v)
const double nan
Not a number.
Definition: calculus.hpp:53
T * get_ptr(std::vector< T > &v)
Get a pointer to the data contained in the vector.
CASADI_EXPORT void finite_diff(FdMode v, const T1 **yk, T1 *J, T1 h, casadi_int n_y, T1 smoothing)
Calculate FD estimate.
bool discrete_states_need_update
Definition: fmu_impl.hpp:39
bool next_event_time_defined
Definition: fmu_impl.hpp:43
double next_event_time
Definition: fmu_impl.hpp:44
bool nominals_of_continuous_states_changed
Definition: fmu_impl.hpp:41
bool values_of_continuous_states_changed
Definition: fmu_impl.hpp:42
std::vector< double > d_in_
std::vector< size_t > id_in_
std::vector< double > obuf_
std::vector< double > isens_
std::vector< bool > in_bounds_
const FmuFunction & self
std::vector< double > osens_
std::vector< size_t > wrt_
std::vector< double > d_out_
std::vector< unsigned int > vr_in_
std::vector< double > v_out_
std::vector< bool > flip_
std::vector< double > v_pert_
std::vector< size_t > id_out_
std::vector< double > fd_out_
std::vector< bool > omarked_
std::vector< double > v_in_
std::vector< double > ibuf_
std::vector< bool > imarked_
std::vector< unsigned int > vr_out_
Holds expressions and meta-data corresponding to a physical quantity evolving in time.
std::string name
Name of the variable.