dae_builder.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 "dae_builder_internal.hpp"
27 
28 #include <cctype>
29 #include <ctime>
30 #include <map>
31 #include <set>
32 #include <sstream>
33 #include <string>
34 #include <algorithm>
35 
36 #include "casadi_misc.hpp"
37 #include "exception.hpp"
38 #include "code_generator.hpp"
39 #include "calculus.hpp"
40 #include "xml_file.hpp"
41 #include "external.hpp"
42 #include "integrator.hpp"
43 
44 namespace casadi {
45 
46 // Throw informative error message
47 #define THROW_ERROR(FNAME, WHAT) \
48 throw CasadiException("Error in DaeBuilder::" FNAME " for '" + this->name() \
49  + "' at " + CASADI_WHERE + ":\n" + std::string(WHAT));
50 
52 }
53 
54 DaeBuilder::DaeBuilder(const std::string& name, const std::string& path, const Dict& opts) {
55  own(new DaeBuilderInternal(name, path, opts));
56  if (!path.empty()) load_fmi_description((*this)->resource_.path() + "/modelDescription.xml");
57 }
58 
59 const std::string& DaeBuilder::name() const {
60  return (*this)->name_;
61 }
62 
63 const MX& DaeBuilder::time() const {
64  try {
65  return (*this)->time();
66  } catch (std::exception& e) {
67  THROW_ERROR("time", e.what());
68  static const MX t;
69  return t; // never reached
70  }
71 }
72 
73 std::vector<std::string> DaeBuilder::y() const {
74  try {
75  return (*this)->name((*this)->outputs_);
76  } catch (std::exception& e) {
77  THROW_ERROR("y", e.what());
78  return {}; // never reached
79  }
80 }
81 
82 std::vector<MX> DaeBuilder::ode() const {
83  try {
84  return (*this)->output(OutputCategory::ODE);
85  } catch (std::exception& e) {
86  THROW_ERROR("ode", e.what());
87  return {}; // never reached
88  }
89 }
90 
91 std::vector<MX> DaeBuilder::alg() const {
92  try {
93  return (*this)->output(OutputCategory::ALG);
94  } catch (std::exception& e) {
95  THROW_ERROR("alg", e.what());
96  return {}; // never reached
97  }
98 }
99 
100 std::vector<MX> DaeBuilder::quad() const {
101  try {
102  return (*this)->output(OutputCategory::QUAD);
103  } catch (std::exception& e) {
104  THROW_ERROR("quad", e.what());
105  return {}; // never reached
106  }
107 }
108 
109 std::vector<MX> DaeBuilder::zero() const {
110  try {
111  return (*this)->output(OutputCategory::ZERO);
112  } catch (std::exception& e) {
113  THROW_ERROR("zero", e.what());
114  }
115 }
116 
117 std::vector<MX> DaeBuilder::ydef() const {
118  try {
119  return (*this)->output(OutputCategory::Y);
120  } catch (std::exception& e) {
121  THROW_ERROR("ydef", e.what());
122  return {}; // never reached
123  }
124 }
125 
126 void DaeBuilder::set_y(const std::vector<std::string>& name) {
127  try {
128  // Update causality of any existing outputs not in name
129  std::set<std::string> name_set(name.begin(), name.end());
130  casadi_assert(name_set.size() == name.size(), "Duplicate names");
131  for (auto&& n : y()) {
132  auto it = name_set.find(n);
133  if (it == name_set.end()) {
134  // Not an output anymore, make local
135  set_causality(n, "local");
136  } else {
137  // Mark as added
138  name_set.erase(it);
139  }
140  }
141  // Update causality of new outputs
142  for (auto&& n : name) {
143  // Check if not already added
144  auto it = name_set.find(n);
145  if (it != name_set.end()) {
146  // Make output causality
147  set_causality(n, "output");
148  // Mark as added
149  name_set.erase(it);
150  }
151  }
152  // Consistency checks
153  casadi_assert_dev(name_set.empty());
154  casadi_assert_dev((*this)->outputs_.size() == name.size());
155  // Update output ordering
156  (*this)->outputs_ = (*this)->find(name);
157  } catch (std::exception& e) {
158  THROW_ERROR("set_rate", e.what());
159  }
160 }
161 
162 std::vector<std::string> DaeBuilder::rate() const {
163  try {
164  return (*this)->name((*this)->rate_);
165  } catch (std::exception& e) {
166  THROW_ERROR("rate", e.what());
167  return {}; // never reached
168  }
169 }
170 
171 void DaeBuilder::set_rate(const std::vector<std::string>& name) {
172  try {
173  casadi_assert(name.size() <= 1, "At most one rate variable");
174  (*this)->rate_ = (*this)->find(name);
175  } catch (std::exception& e) {
176  THROW_ERROR("set_rate", e.what());
177  }
178 }
179 
180 std::vector<MX> DaeBuilder::cdef() const {
181  try {
182  return (*this)->cdef();
183  } catch (std::exception& e) {
184  THROW_ERROR("cdef", e.what());
185  return {}; // never reached
186  }
187 }
188 
189 std::vector<MX> DaeBuilder::ddef() const {
190  try {
191  return (*this)->output(OutputCategory::DDEF);
192  } catch (std::exception& e) {
193  THROW_ERROR("ddef", e.what());
194  return {}; // never reached
195  }
196 }
197 
198 std::vector<MX> DaeBuilder::wdef() const {
199  try {
200  return (*this)->output(OutputCategory::WDEF);
201  } catch (std::exception& e) {
202  THROW_ERROR("wdef", e.what());
203  return {}; // never reached
204  }
205 }
206 
207 std::vector<MX> DaeBuilder::init_lhs() const {
208  return (*this)->init_lhs();
209 }
210 
211 std::vector<MX> DaeBuilder::init_rhs() const {
212  return (*this)->init_rhs();
213 }
214 
215 std::vector<std::string> DaeBuilder::outputs() const {
216  try {
217  return (*this)->name((*this)->outputs_);
218  } catch (std::exception& e) {
219  THROW_ERROR("outputs", e.what());
220  return {}; // never reached
221  }
222 }
223 
224 std::vector<std::string> DaeBuilder::derivatives() const {
225  try {
226  return (*this)->name((*this)->derivatives_);
227  } catch (std::exception& e) {
228  THROW_ERROR("derivatives", e.what());
229  return {}; // never reached
230  }
231 }
232 
233 std::vector<std::string> DaeBuilder::initial_unknowns() const {
234  try {
235  return (*this)->name((*this)->initial_unknowns_);
236  } catch (std::exception& e) {
237  THROW_ERROR("initial_unknowns", e.what());
238  return {}; // never reached
239  }
240 }
241 
242 bool DaeBuilder::has_t() const {
243  try {
244  return (*this)->has_t();
245  } catch (std::exception& e) {
246  THROW_ERROR("has_t", e.what());
247  return false; // never reached
248  }
249 }
250 
251 bool DaeBuilder::has_rate() const {
252  try {
253  return !(*this)->rate_.empty();
254  } catch (std::exception& e) {
255  THROW_ERROR("has_rate", e.what());
256  return false; // never reached
257  }
258 }
259 
260 casadi_int DaeBuilder::nx() const {
261  return (*this)->size(Category::X);
262 }
263 
264 casadi_int DaeBuilder::nz() const {
265  return (*this)->size(Category::Z);
266 }
267 
268 casadi_int DaeBuilder::nq() const {
269  return (*this)->size(Category::Q);
270 }
271 
272 casadi_int DaeBuilder::nzero() const {
273  return (*this)->event_indicators_.size();
274 }
275 
276 casadi_int DaeBuilder::ny() const {
277  return (*this)->outputs_.size();
278 }
279 
280 casadi_int DaeBuilder::nu() const {
281  return (*this)->size(Category::U);
282 }
283 
284 casadi_int DaeBuilder::np() const {
285  return (*this)->size(Category::P);
286 }
287 
288 casadi_int DaeBuilder::nc() const {
289  return (*this)->size(Category::C);
290 }
291 
292 casadi_int DaeBuilder::nd() const {
293  return (*this)->size(Category::D);
294 }
295 
296 casadi_int DaeBuilder::nw() const {
297  return (*this)->size(Category::W);
298 }
299 
300 void DaeBuilder::load_fmi_description(const std::string& filename) {
301  try {
302  (*this)->load_fmi_description(filename);
303  } catch (std::exception& e) {
304  THROW_ERROR("load_fmi_description", e.what());
305  }
306 }
307 
309  try {
310  casadi_assert(!(*this)->symbolic_, "Functionality only applies to imported standard FMUs");
311  return (*this)->provides_directional_derivatives_;
312  } catch (std::exception& e) {
313  THROW_ERROR("provides_directional_derivatives", e.what());
314  return false;
315  }
316 }
317 
318 std::vector<std::string> DaeBuilder::export_fmu(const Dict& opts) {
319  try {
320  return (*this)->export_fmu(opts);
321  } catch (std::exception& e) {
322  THROW_ERROR("export_fmu", e.what());
323  }
324 }
325 
326 void DaeBuilder::prune(bool prune_p, bool prune_u) {
327  try {
328  (*this)->prune(prune_p, prune_u);
329  } catch (std::exception& e) {
330  THROW_ERROR("prune", e.what());
331  }
332 }
333 
335  try {
336  (*this)->tear();
337  } catch (std::exception& e) {
338  THROW_ERROR("tear", e.what());
339  }
340 }
341 
342 bool DaeBuilder::has(const std::string& name) const {
343  try {
344  return (*this)->has(name);
345  } catch (std::exception& e) {
346  THROW_ERROR("has", e.what());
347  return false; // never reached
348  }
349 }
350 
351 std::vector<std::string> DaeBuilder::all() const {
352  try {
353  return (*this)->all();
354  } catch (std::exception& e) {
355  THROW_ERROR("all", e.what());
356  return {}; // never reached
357  }
358 }
359 
360 std::vector<std::string> DaeBuilder::all(const std::string& cat) const {
361  try {
362  return (*this)->all(to_enum<Category>(cat));
363  } catch (std::exception& e) {
364  THROW_ERROR("all", e.what());
365  return {}; // never reached
366  }
367 }
368 
369 #ifdef WITH_DEPRECATED_FEATURES
370 Variable& DaeBuilder::new_variable(const std::string& name, casadi_int numel) {
371  try {
372  return (*this)->new_variable(name, {numel});
373  } catch (std::exception& e) {
374  THROW_ERROR("new_variable", e.what());
375  }
376 }
377 
378 Variable& DaeBuilder::variable(const std::string& name) {
379  try {
380  return (*this)->variable(name);
381  } catch (std::exception& e) {
382  THROW_ERROR("variable", e.what());
383  }
384 }
385 
386 const Variable& DaeBuilder::variable(const std::string& name) const {
387  try {
388  return (*this)->variable(name);
389  } catch (std::exception& e) {
390  THROW_ERROR("variable", e.what());
391  }
392 }
393 
395  try {
396  return (*this)->variable(ind);
397  } catch (std::exception& e) {
398  THROW_ERROR("variable", e.what());
399  }
400 }
401 
402 const Variable& DaeBuilder::variable(size_t ind) const {
403  try {
404  return (*this)->variable(ind);
405  } catch (std::exception& e) {
406  THROW_ERROR("variable", e.what());
407  }
408 }
409 
410 size_t DaeBuilder::find(const std::string& name) const {
411  try {
412  return (*this)->find(name);
413  } catch (std::exception& e) {
414  THROW_ERROR("find", e.what());
415  return -1; // never reached
416  }
417 }
418 
419 std::vector<size_t> DaeBuilder::find(const std::vector<std::string>& name) const {
420  try {
421  return (*this)->find(name);
422  } catch (std::exception& e) {
423  THROW_ERROR("find", e.what());
424  return {}; // never reached
425  }
426 }
427 
428 const std::string& DaeBuilder::name(size_t ind) const {
429  try {
430  return (*this)->name(ind);
431  } catch (std::exception& e) {
432  THROW_ERROR("name", e.what());
433  static std::string dummy;
434  return dummy; // never reached
435  }
436 }
437 
438 std::vector<std::string> DaeBuilder::name(const std::vector<size_t>& ind) const {
439  try {
440  return (*this)->name(ind);
441  } catch (std::exception& e) {
442  THROW_ERROR("name", e.what());
443  return {}; // never reached
444  }
445 }
446 const MX& DaeBuilder::var(size_t ind) const {
447  try {
448  return (*this)->var(ind);
449  } catch (std::exception& e) {
450  THROW_ERROR("var", e.what());
451  static MX dummy;
452  return dummy; // never reached
453  }
454 }
455 
456 std::vector<MX> DaeBuilder::var(const std::vector<size_t>& ind) const {
457  try {
458  return (*this)->var(ind);
459  } catch (std::exception& e) {
460  THROW_ERROR("var", e.what());
461  return {}; // never reached
462  }
463 }
464 
465 MX DaeBuilder::add_variable(const std::string& name, casadi_int n) {
466  return add_variable(name, Sparsity::dense(n));
467 }
468 
469 MX DaeBuilder::add_variable(const std::string& name, const Sparsity& sp) {
470  Variable& v = new_variable(name);
471  v.v = MX::sym(name, sp);
472  return v.v;
473 }
474 
475 void DaeBuilder::add_variable(const MX& new_v) {
476  Variable& v = new_variable(new_v.name());
477  v.v = new_v;
478 }
479 
480 size_t DaeBuilder::add_variable_new(const std::string& name, casadi_int n) {
482 }
483 
484 size_t DaeBuilder::add_variable_new(const std::string& name, const Sparsity& sp) {
485  Variable& v = new_variable(name);
486  v.v = MX::sym(name, sp);
487  return v.index;
488 }
489 
490 size_t DaeBuilder::add_variable_new(const MX& new_v) {
491  Variable& v = new_variable(new_v.name());
492  v.v = new_v;
493  return v.index;
494 }
495 
496 void DaeBuilder::register_t(const std::string& name) {
497  // Save to class
498  casadi_assert(!has_t(), "'t' already defined");
499  (*this)->indices(Category::T).push_back(find(name));
500 }
501 
502 void DaeBuilder::register_p(const std::string& name) {
503  (*this)->indices(Category::P).push_back(find(name));
504 }
505 
506 void DaeBuilder::register_u(const std::string& name) {
507  (*this)->indices(Category::U).push_back(find(name));
508 }
509 
510 void DaeBuilder::register_x(const std::string& name) {
511  (*this)->indices(Category::X).push_back(find(name));
512 }
513 
514 void DaeBuilder::register_z(const std::string& name) {
515  (*this)->indices(Category::Z).push_back(find(name));
516 }
517 
518 void DaeBuilder::register_q(const std::string& name) {
519  (*this)->indices(Category::Q).push_back(find(name));
520 }
521 
522 void DaeBuilder::register_c(const std::string& name) {
523  (*this)->indices(Category::C).push_back(find(name));
524 }
525 
526 void DaeBuilder::register_d(const std::string& name) {
527  (*this)->indices(Category::D).push_back(find(name));
528 }
529 
530 void DaeBuilder::register_w(const std::string& name) {
531  (*this)->indices(Category::W).push_back(find(name));
532 }
533 
534 void DaeBuilder::register_y(const std::string& name) {
535  (*this)->outputs_.push_back(find(name));
536 }
537 
538 void DaeBuilder::register_e(const std::string& name) {
539  (*this)->event_indicators_.push_back(find(name));
540 }
541 
543  try {
544  return eliminate("d");
545  } catch (std::exception& e) {
546  THROW_ERROR("eliminate_d", e.what());
547  }
548 }
549 
551  try {
552  return eliminate("w");
553  } catch (std::exception& e) {
554  THROW_ERROR("eliminate_w", e.what());
555  }
556 }
557 
559  try {
560  return eliminate("q");
561  } catch (std::exception& e) {
562  THROW_ERROR("eliminate_quad", e.what());
563  }
564 }
565 
567  try {
568  return sort("d");
569  } catch (std::exception& e) {
570  THROW_ERROR("sort_d", e.what());
571  }
572 }
573 
575  try {
576  return sort("w");
577  } catch (std::exception& e) {
578  THROW_ERROR("sort_w", e.what());
579  }
580 }
581 
582 void DaeBuilder::sort_z(const std::vector<std::string>& z_order) {
583  try {
584  return reorder("z", z_order);
585  } catch (std::exception& e) {
586  THROW_ERROR("sort_z", e.what());
587  }
588 }
589 
590 void DaeBuilder::clear_all(const std::string& v) {
591  try {
592  (*this)->clear_cache_ = true; // Clear cache after this
593  (*this)->indices(to_enum<Category>(v)).clear();
594  } catch (std::exception& e) {
595  THROW_ERROR("clear_all", e.what());
596  }
597 }
598 
599 void DaeBuilder::set_all(const std::string& v, const std::vector<std::string>& name) {
600  try {
601  (*this)->clear_cache_ = true; // Clear cache after this
602  const std::vector<size_t>& new_ind = (*this)->find(name);
603  if (v == "y") {
604  (*this)->outputs_ = new_ind;
605  } else {
606  (*this)->indices(to_enum<Category>(v)) = new_ind;
607  }
608  } catch (std::exception& e) {
609  THROW_ERROR("set_all", e.what());
610  }
611 }
612 
613 #endif // WITH_DEPRECATED_FEATURES
614 
615 void DaeBuilder::reorder(const std::string& cat, const std::vector<std::string>& v) {
616  try {
617  auto vind = (*this)->find(v);
618  if (cat == "y") {
619  // Reorder outputs
620  (*this)->reorder("y", (*this)->outputs_, vind);
621  } else {
622  // Reorder inputs
623  (*this)->reorder(to_enum<Category>(cat), vind);
624  }
625  } catch (std::exception& e) {
626  THROW_ERROR("reorder", e.what());
627  }
628 }
629 
630 MX DaeBuilder::add(const std::string& name, const std::string& causality,
631  const std::string& variability, const Dict& opts) {
632  try {
633  return (*this)->add(name, to_enum<Causality>(causality),
634  to_enum<Variability>(variability), opts).v;
635  } catch (std::exception& e) {
636  THROW_ERROR("add", e.what());
637  return MX();
638  }
639 }
640 
641 MX DaeBuilder::add(const std::string& name, const std::string& causality, const Dict& opts) {
642  try {
643  return (*this)->add(name, to_enum<Causality>(causality), opts).v;
644  } catch (std::exception& e) {
645  THROW_ERROR("add", e.what());
646  return MX();
647  }
648 }
649 
650 MX DaeBuilder::add(const std::string& name, const Dict& opts) {
651  try {
652  return (*this)->add(name, opts).v;
653  } catch (std::exception& e) {
654  THROW_ERROR("add", e.what());
655  return MX();
656  }
657 }
658 
659 void DaeBuilder::add(const std::string& name, const std::string& causality,
660  const std::string& variability, const MX& expr, const Dict& opts) {
661  try {
662  // Ensure pure symbolic expression
663  casadi_assert(expr.is_symbolic(), "Expression must be symbolic");
664  // Make sure name matches expression
665  casadi_assert(name == expr.name(), "Name must match expression");
666  // Add variable
667  (*this)->add(name, to_enum<Causality>(causality), to_enum<Variability>(variability),
668  expr, opts);
669  } catch (std::exception& e) {
670  THROW_ERROR("add", e.what());
671  }
672 }
673 
674 #ifdef WITH_DEPRECATED_FEATURES
675 MX DaeBuilder::add_t(const std::string& name) {
676  return add(name, "independent");
677 }
678 
679 MX DaeBuilder::add_p(const std::string& name) {
680  casadi_assert(!name.empty(), "Variable name is required");
681  return add(name, "parameter", "tunable");
682 }
683 
684 MX DaeBuilder::add_u(const std::string& name) {
685  casadi_assert(!name.empty(), "Variable name is required");
686  return add(name, "input");
687 }
688 
689 MX DaeBuilder::add_x(const std::string& name) {
690  casadi_assert(!name.empty(), "Variable name is required");
691  return add(name);
692 }
693 
694 MX DaeBuilder::add_z(const std::string& name) {
695  casadi_assert(!name.empty(), "Variable name is required");
696  return add(name);
697 }
698 
699 MX DaeBuilder::add_q(const std::string& name) {
700  casadi_assert(!name.empty(), "Variable name is required");
701  return add(name);
702 }
703 
704 MX DaeBuilder::add_c(const std::string& name, const MX& new_cdef) {
705  MX v = add(name, "local", "constant");
706  set_beq(name, new_cdef);
707  return v;
708 }
709 
710 MX DaeBuilder::add_d(const std::string& name, const MX& new_ddef) {
711  MX v = add(name, "calculatedParameter", "fixed");
712  set_beq(name, new_ddef);
713  return v;
714 }
715 
716 MX DaeBuilder::add_w(const std::string& name, const MX& new_wdef) {
717  MX v = add(name);
718  eq(v, new_wdef);
719  return v;
720 }
721 
722 MX DaeBuilder::add_y(const std::string& name, const MX& new_ydef) {
723  MX v = add(name, "output");
724  eq(v, new_ydef);
725  return v;
726 }
727 
728 void DaeBuilder::set_beq(const std::string& name, const MX& val) {
729  try {
730  eq(var(name), val);
731  } catch (std::exception& e) {
732  THROW_ERROR("set_beq", e.what());
733  }
734 }
735 
736 #endif // WITH_DEPRECATED_FEATURES
737 
738 void DaeBuilder::eq(const MX& lhs, const MX& rhs, const Dict& opts) {
739  try {
740  (*this)->eq(lhs, rhs, opts);
741  } catch (std::exception& e) {
742  THROW_ERROR("eq", e.what());
743  }
744 }
745 
746 void DaeBuilder::when(const MX& cond, const std::vector<std::string>& eqs, const Dict& opts) {
747  try {
748  (*this)->when(cond, eqs, opts);
749  } catch (std::exception& e) {
750  THROW_ERROR("when", e.what());
751  }
752 }
753 
754 std::string DaeBuilder::assign(const std::string& name, const MX& val) {
755  try {
756  return (*this)->assign(name, val).name;
757  } catch (std::exception& e) {
758  THROW_ERROR("assign", e.what());
759  return std::string(); // never reached
760  }
761 }
762 
763 std::string DaeBuilder::reinit(const std::string& name, const MX& val) {
764  try {
765  return (*this)->reinit(name, val).name;
766  } catch (std::exception& e) {
767  THROW_ERROR("reinit", e.what());
768  return std::string(); // never reached
769  }
770 }
771 
772 void DaeBuilder::set_init(const std::string& name, const MX& init_rhs) {
773  try {
774  (*this)->set_init(name, init_rhs);
775  } catch (std::exception& e) {
776  THROW_ERROR("set_init", e.what());
777  }
778 }
779 
781  try {
782  (*this)->sanity_check();
783  } catch (std::exception& e) {
784  THROW_ERROR("sanity_check", e.what());
785  }
786 }
787 
788 MX DaeBuilder::var(const std::string& name) const {
789  try {
790  return (*this)->variable(name).v;
791  } catch (std::exception& e) {
792  THROW_ERROR("var", e.what());
793  return MX(); // never reached
794  }
795 }
796 
797 std::string DaeBuilder::der(const std::string& name) const {
798  try {
799  // Get variable index
800  size_t ind = (*this)->find(name);
801  // Differentiate
802  ind = (*this)->variable(ind).der;
803  casadi_assert(ind != size_t(-1), "No derivative expression for " + name);
804  // Return name
805  return (*this)->variable(ind).name;
806  } catch (std::exception& e) {
807  THROW_ERROR("der", e.what());
808  return std::string(); // never reached
809  }
810 }
811 
812 std::string DaeBuilder::pre(const std::string& name) const {
813  try {
814  // Not implemented
815  static bool warned = false;
816  if (!warned) {
817  casadi_warning("DaeBuilder::pre has not been implemented: Returning identity mapping");
818  warned = true;
819  }
820  return name;
821  } catch (std::exception& e) {
822  THROW_ERROR("pre", e.what());
823  return std::string(); // never reached
824  }
825 }
826 
827 MX DaeBuilder::pre(const MX& v) const {
828  try {
829  // Not implemented
830  static bool warned = false;
831  if (!warned) {
832  casadi_warning("DaeBuilder::pre has not been implemented: Returning identity mapping");
833  warned = true;
834  }
835  return v;
836  } catch (std::exception& e) {
837  THROW_ERROR("pre", e.what());
838  return MX(); // never reached
839  }
840 }
841 
842 std::vector<std::string> DaeBuilder::der(const std::vector<std::string>& name) const {
843  try {
844  std::vector<std::string> r(name.size());
845  for (size_t i = 0; i < r.size(); ++i) r[i] = der(name[i]);
846  return r;
847  } catch (std::exception& e) {
848  THROW_ERROR("der", e.what());
849  return {}; // never reached
850  }
851 }
852 
853 std::vector<std::string> DaeBuilder::pre(const std::vector<std::string>& name) const {
854  try {
855  std::vector<std::string> r(name.size());
856  for (size_t i = 0; i < r.size(); ++i) r[i] = pre(name[i]);
857  return r;
858  } catch (std::exception& e) {
859  THROW_ERROR("pre", e.what());
860  return {}; // never reached
861  }
862 }
863 
864 bool DaeBuilder::has_beq(const std::string& name) const {
865  try {
866  return !(*this)->variable(name).has_beq();
867  } catch (std::exception& e) {
868  THROW_ERROR("has_beq", e.what());
869  return false; // never reached
870  }
871 }
872 
873 MX DaeBuilder::beq(const std::string& name) const {
874  try {
875  const Variable& v = (*this)->variable(name);
876  return (*this)->variable(v.bind).v;
877  } catch (std::exception& e) {
878  THROW_ERROR("beq", e.what());
879  return MX(); // never reached
880  }
881 }
882 
883 void DaeBuilder::eliminate(const std::string& cat) {
884  try {
885  return (*this)->eliminate(to_enum<Category>(cat));
886  } catch (std::exception& e) {
887  THROW_ERROR("eliminate", e.what());
888  }
889 }
890 
891 void DaeBuilder::sort(const std::string& cat) {
892  try {
893  return (*this)->sort(to_enum<Category>(cat));
894  } catch (std::exception& e) {
895  THROW_ERROR("sort", e.what());
896  }
897 }
898 
899 void DaeBuilder::lift(bool lift_shared, bool lift_calls) {
900  try {
901  (*this)->lift(lift_shared, lift_calls);
902  } catch (std::exception& e) {
903  THROW_ERROR("lift", e.what());
904  }
905 }
906 
907 casadi_int DaeBuilder::value_reference(const std::string& name) const {
908  return (*this)->variable(name).value_reference;
909 }
910 
911 void DaeBuilder::set_value_reference(const std::string& name, casadi_int val) {
912  (*this)->variable(name).value_reference = val;
913 }
914 
915 std::string DaeBuilder::description(const std::string& name) const {
916  return (*this)->variable(name).description;
917 }
918 
919 void DaeBuilder::set_description(const std::string& name, const std::string& val) {
920  (*this)->variable(name).description = val;
921 }
922 
923 std::string DaeBuilder::type(const std::string& name, casadi_int fmi_version) const {
924  // Check version
925  casadi_assert(fmi_version == 2 || fmi_version == 3, "Only FMI version 2 or 3 supported");
926  // Handle FMI 2
927  if (fmi_version == 2) {
928  return to_string(to_fmi2((*this)->variable(name).type));
929  }
930  // Assume FMI 3
931  return to_string((*this)->variable(name).type);
932 }
933 
934 void DaeBuilder::set_type(const std::string& name, const std::string& val) {
935  // Fallback to FMI 2, if necessary
936  if (has_enum<TypeFmi2>(val) && !has_enum<Type>(val)) {
937  (*this)->variable(name).type = from_fmi2(to_enum<TypeFmi2>(val));
938  }
939  // Assume FMI 3
940  (*this)->variable(name).type = to_enum<Type>(val);
941 }
942 
943 std::string DaeBuilder::causality(const std::string& name) const {
944  try {
945  return to_string((*this)->causality((*this)->find(name)));
946  } catch (std::exception& e) {
947  THROW_ERROR("causality", e.what());
948  return std::string(); // never reached
949  }
950 }
951 
952 void DaeBuilder::set_causality(const std::string& name, const std::string& val) {
953  try {
954  (*this)->set_causality((*this)->find(name), to_enum<Causality>(val));
955  } catch (std::exception& e) {
956  THROW_ERROR("set_causality", e.what());
957  }
958 }
959 
960 std::string DaeBuilder::variability(const std::string& name) const {
961  try {
962  return to_string((*this)->variability((*this)->find(name)));
963  } catch (std::exception& e) {
964  THROW_ERROR("variability", e.what());
965  return std::string(); // never reached
966  }
967 }
968 
969 void DaeBuilder::set_variability(const std::string& name, const std::string& val) {
970  try {
971  (*this)->set_variability((*this)->find(name), to_enum<Variability>(val));
972  } catch (std::exception& e) {
973  THROW_ERROR("set_variability", e.what());
974  }
975 }
976 
977 std::string DaeBuilder::category(const std::string& name) const {
978  try {
979  return to_string((*this)->category((*this)->find(name)));
980  } catch (std::exception& e) {
981  THROW_ERROR("category", e.what());
982  return std::string(); // never reached
983  }
984 }
985 
986 void DaeBuilder::set_category(const std::string& name, const std::string& val) {
987  try {
988  (*this)->set_category((*this)->find(name), to_enum<Category>(val));
989  } catch (std::exception& e) {
990  THROW_ERROR("set_category", e.what());
991  }
992 }
993 
994 std::string DaeBuilder::initial(const std::string& name) const {
995  return to_string((*this)->variable(name).initial);
996 }
997 
998 void DaeBuilder::set_initial(const std::string& name, const std::string& val) {
999  (*this)->variable(name).initial = to_enum<Initial>(val);
1000 }
1001 
1002 std::string DaeBuilder::unit(const std::string& name) const {
1003  return (*this)->variable(name).unit;
1004 }
1005 
1006 void DaeBuilder::set_unit(const std::string& name, const std::string& val) {
1007  (*this)->variable(name).unit = val;
1008 }
1009 
1010 std::string DaeBuilder::display_unit(const std::string& name) const {
1011  return (*this)->variable(name).display_unit;
1012 }
1013 
1014 void DaeBuilder::set_display_unit(const std::string& name, const std::string& val) {
1015  (*this)->variable(name).display_unit = val;
1016 }
1017 
1018 casadi_int DaeBuilder::numel(const std::string& name) const {
1019  return (*this)->variable(name).numel;
1020 }
1021 
1022 std::vector<casadi_int> DaeBuilder::dimension(const std::string& name) const {
1023  return (*this)->variable(name).dimension;
1024 }
1025 
1026 double DaeBuilder::start_time() const {
1027  try {
1028  return (*this)->start_time_;
1029  } catch (std::exception& e) {
1030  THROW_ERROR("start_time", e.what());
1031  return nan;
1032  }
1033 }
1034 
1035 void DaeBuilder::set_start_time(double val) {
1036  try {
1037  (*this)->start_time_ = val;
1038  } catch (std::exception& e) {
1039  THROW_ERROR("set_start_time", e.what());
1040  }
1041 }
1042 
1043 double DaeBuilder::stop_time() const {
1044  try {
1045  return (*this)->stop_time_;
1046  } catch (std::exception& e) {
1047  THROW_ERROR("stop_time", e.what());
1048  return nan;
1049  }
1050 }
1051 
1052 void DaeBuilder::set_stop_time(double val) {
1053  try {
1054  (*this)->stop_time_ = val;
1055  } catch (std::exception& e) {
1056  THROW_ERROR("set_stop_time", e.what());
1057  }
1058 }
1059 
1060 double DaeBuilder::tolerance() const {
1061  try {
1062  return (*this)->tolerance_;
1063  } catch (std::exception& e) {
1064  THROW_ERROR("tolerance", e.what());
1065  return nan;
1066  }
1067 }
1068 
1069 void DaeBuilder::set_tolerance(double val) {
1070  try {
1071  (*this)->tolerance_ = val;
1072  } catch (std::exception& e) {
1073  THROW_ERROR("set_tolerance", e.what());
1074  }
1075 }
1076 
1077 double DaeBuilder::step_size() const {
1078  try {
1079  return (*this)->step_size_;
1080  } catch (std::exception& e) {
1081  THROW_ERROR("step_size", e.what());
1082  return nan;
1083  }
1084 }
1085 
1086 void DaeBuilder::set_step_size(double val) {
1087  try {
1088  (*this)->step_size_ = val;
1089  } catch (std::exception& e) {
1090  THROW_ERROR("set_step_size", e.what());
1091  }
1092 }
1093 
1094 void DaeBuilder::add_lc(const std::string& name,
1095  const std::vector<std::string>& f_out) {
1096  try {
1097  (*this)->add_lc(name, f_out);
1098  } catch (std::exception& e) {
1099  THROW_ERROR("add_lc", e.what());
1100  }
1101 }
1102 
1103 Function DaeBuilder::create(const std::string& fname,
1104  const std::vector<std::string>& name_in,
1105  const std::vector<std::string>& name_out, bool sx, bool lifted_calls) const {
1106  try {
1107  return (*this)->create(fname, name_in, name_out, Dict(), sx, lifted_calls);
1108  } catch (std::exception& e) {
1109  THROW_ERROR("create", e.what());
1110  return Function(); // never reached
1111  }
1112 }
1113 
1114 Function DaeBuilder::create(const std::string& fname,
1115  const std::vector<std::string>& name_in,
1116  const std::vector<std::string>& name_out, const Dict& opts) const {
1117  try {
1118  return (*this)->create(fname, name_in, name_out, opts, false, false);
1119  } catch (std::exception& e) {
1120  THROW_ERROR("create", e.what());
1121  return Function(); // never reached
1122  }
1123 }
1124 
1125 Function DaeBuilder::create(const std::string& name, const Dict& opts) const {
1126  try {
1127  return (*this)->create(name, dyn_in(), dyn_out(), opts, false, false);
1128  } catch (std::exception& e) {
1129  THROW_ERROR("create", e.what());
1130  return Function(); // never reached
1131  }
1132 }
1133 
1135  try {
1136  return (*this)->add_fun(f);
1137  } catch (std::exception& e) {
1138  THROW_ERROR("add_fun", e.what());
1139  return Function(); // never reached
1140  }
1141 }
1142 
1143 Function DaeBuilder::add_fun(const std::string& name, const std::vector<std::string>& arg,
1144  const std::vector<std::string>& res, const Dict& opts) {
1145  try {
1146  return (*this)->add_fun(name, arg, res, opts);
1147  } catch (std::exception& e) {
1148  THROW_ERROR("add_fun", e.what());
1149  return Function(); // never reached
1150  }
1151 }
1152 
1153 Function DaeBuilder::add_fun(const std::string& name, const Importer& compiler, const Dict& opts) {
1154  casadi_assert(!has_fun(name), "Function '" + name + "' already exists");
1155  return add_fun(external(name, compiler, opts));
1156 }
1157 
1158 bool DaeBuilder::has_fun(const std::string& name) const {
1159  try {
1160  return (*this)->has_fun(name);
1161  } catch (std::exception& e) {
1162  THROW_ERROR("has_fun", e.what());
1163  return false; // never reached
1164  }
1165 }
1166 
1167 Function DaeBuilder::fun(const std::string& name) const {
1168  try {
1169  return (*this)->fun(name);
1170  } catch (std::exception& e) {
1171  THROW_ERROR("fun", e.what());
1172  return Function(); // never reached
1173  }
1174 }
1175 
1176 void DaeBuilder::gather_fun(casadi_int max_depth) {
1177  try {
1178  // Get a function corresponding to all equations (no inputs)
1179  Function all_eq = (*this)->gather_eq();
1180  // Gather all functions
1181  std::vector<Function> allfun = all_eq.find_functions(max_depth);
1182  // Add to list of functions
1183  for (const Function& f : allfun) {
1184  if (has_fun(f.name())) {
1185  // Skip functions with duplicate names
1186  casadi_warning("Duplicate function: '" + f.name() + "', ignored");
1187  } else {
1188  // Add to list of functions
1189  add_fun(f);
1190  }
1191  }
1192  } catch (std::exception& e) {
1193  THROW_ERROR("gather_fun", e.what());
1194  }
1195 }
1196 
1197 std::vector<Function> DaeBuilder::fun() const {
1198  return (*this)->fun_;
1199 }
1200 
1201 Function DaeBuilder::oracle(bool sx, bool elim_w, bool lifted_calls) const {
1202  try {
1203  return (*this)->oracle(sx, elim_w, lifted_calls);
1204  } catch (std::exception& e) {
1205  THROW_ERROR("oracle", e.what());
1206  return Function(); // never reached
1207  }
1208 }
1209 
1210 Function DaeBuilder::dependent_fun(const std::string& fname,
1211  const std::vector<std::string>& s_in,
1212  const std::vector<std::string>& s_out) const {
1213  try {
1214  return (*this)->dependent_fun(fname, s_in, s_out);
1215  } catch (std::exception& e) {
1216  THROW_ERROR("dependent_fun", e.what());
1217  return Function(); // never reached
1218  }
1219 }
1220 
1221 Function DaeBuilder::transition(const std::string& fname, casadi_int index) const {
1222  try {
1223  return (*this)->transition(fname, index);
1224  } catch (std::exception& e) {
1225  THROW_ERROR("transition", e.what());
1226  return Function(); // never reached
1227  }
1228 }
1229 
1230 Function DaeBuilder::transition(const std::string& fname) const {
1231  try {
1232  return (*this)->transition(fname);
1233  } catch (std::exception& e) {
1234  THROW_ERROR("transition", e.what());
1235  return Function(); // never reached
1236  }
1237 }
1238 
1240  return dynamic_cast<const DaeBuilderInternal*>(ptr) != nullptr;
1241 }
1242 
1244  return static_cast<DaeBuilderInternal*>(SharedObject::operator->());
1245 }
1246 
1248  return static_cast<const DaeBuilderInternal*>(SharedObject::operator->());
1249 }
1250 
1251 MX DaeBuilder::der(const MX& v) const {
1252  try {
1253  return (*this)->der(v);
1254  } catch (std::exception& e) {
1255  THROW_ERROR("der", e.what());
1256  return MX(); // never reached
1257  }
1258 }
1259 
1260 MX DaeBuilder::der(const MX& v) {
1261  try {
1262  return (*this)->der(v);
1263  } catch (std::exception& e) {
1264  THROW_ERROR("der", e.what());
1265  return MX(); // never reached
1266  }
1267 }
1268 
1269 double DaeBuilder::attribute(const std::string& a, const std::string& name) const {
1270  try {
1271  return (*this)->attribute(to_enum<Attribute>(a), name);
1272  } catch (std::exception& e) {
1273  THROW_ERROR("attribute", e.what());
1274  return 0; // never reached
1275  }
1276 }
1277 
1278 std::vector<double> DaeBuilder::attribute(const std::string& a,
1279  const std::vector<std::string>& name) const {
1280  try {
1281  return (*this)->attribute(to_enum<Attribute>(a), name);
1282  } catch (std::exception& e) {
1283  THROW_ERROR("attribute", e.what());
1284  return {}; // never reached
1285  }
1286 }
1287 
1288 void DaeBuilder::set_attribute(const std::string& a, const std::string& name, double val) {
1289  try {
1290  (*this)->set_attribute(to_enum<Attribute>(a), name, val);
1291  } catch (std::exception& e) {
1292  THROW_ERROR("set_attribute", e.what());
1293  }
1294 }
1295 
1296 void DaeBuilder::set_attribute(const std::string& a, const std::vector<std::string>& name,
1297  const std::vector<double>& val) {
1298  try {
1299  (*this)->set_attribute(to_enum<Attribute>(a), name, val);
1300  } catch (std::exception& e) {
1301  THROW_ERROR("set_attribute", e.what());
1302  }
1303 }
1304 
1305 double DaeBuilder::min(const std::string& name) const {
1306  try {
1307  return (*this)->variable(name).min;
1308  } catch (std::exception& e) {
1309  THROW_ERROR("min", e.what());
1310  return 0; // never reached
1311  }
1312 }
1313 
1314 std::vector<double> DaeBuilder::min(const std::vector<std::string>& name) const {
1315  try {
1316  return (*this)->attribute(Attribute::MIN, name);
1317  } catch (std::exception& e) {
1318  THROW_ERROR("min", e.what());
1319  return {}; // never reached
1320  }
1321 }
1322 
1323 void DaeBuilder::set_min(const std::string& name, double val) {
1324  try {
1325  (*this)->variable(name).min = val;
1326  } catch (std::exception& e) {
1327  THROW_ERROR("set_min", e.what());
1328  }
1329 }
1330 
1331 void DaeBuilder::set_min(const std::vector<std::string>& name, const std::vector<double>& val) {
1332  try {
1333  (*this)->set_attribute(Attribute::MIN, name, val);
1334  } catch (std::exception& e) {
1335  THROW_ERROR("set_min", e.what());
1336  }
1337 }
1338 
1339 double DaeBuilder::max(const std::string& name) const {
1340  try {
1341  return (*this)->variable(name).max;
1342  } catch (std::exception& e) {
1343  THROW_ERROR("max", e.what());
1344  return 0; // never reached
1345  }
1346 }
1347 
1348 std::vector<double> DaeBuilder::max(const std::vector<std::string>& name) const {
1349  try {
1350  return (*this)->attribute(Attribute::MAX, name);
1351  } catch (std::exception& e) {
1352  THROW_ERROR("max", e.what());
1353  return {}; // never reached
1354  }
1355 }
1356 
1357 void DaeBuilder::set_max(const std::string& name, double val) {
1358  try {
1359  (*this)->variable(name).max = val;
1360  } catch (std::exception& e) {
1361  THROW_ERROR("set_max", e.what());
1362  }
1363 }
1364 
1365 void DaeBuilder::set_max(const std::vector<std::string>& name, const std::vector<double>& val) {
1366  try {
1367  (*this)->set_attribute(Attribute::MAX, name, val);
1368  } catch (std::exception& e) {
1369  THROW_ERROR("set_max", e.what());
1370  }
1371 }
1372 
1373 double DaeBuilder::nominal(const std::string& name) const {
1374  try {
1375  return (*this)->variable(name).nominal;
1376  } catch (std::exception& e) {
1377  THROW_ERROR("nominal", e.what());
1378  return 0; // never reached
1379  }
1380 }
1381 
1382 std::vector<double> DaeBuilder::nominal(const std::vector<std::string>& name) const {
1383  try {
1384  return (*this)->attribute(Attribute::NOMINAL, name);
1385  } catch (std::exception& e) {
1386  THROW_ERROR("nominal", e.what());
1387  return {}; // never reached
1388  }
1389 }
1390 
1391 void DaeBuilder::set_nominal(const std::string& name, double val) {
1392  try {
1393  (*this)->variable(name).nominal = val;
1394  } catch (std::exception& e) {
1395  THROW_ERROR("set_nominal", e.what());
1396  }
1397 }
1398 
1399 void DaeBuilder::set_nominal(const std::vector<std::string>& name, const std::vector<double>& val) {
1400  try {
1401  (*this)->set_attribute(Attribute::NOMINAL, name, val);
1402  } catch (std::exception& e) {
1403  THROW_ERROR("set_mininal", e.what());
1404  }
1405 }
1406 
1407 std::vector<double> DaeBuilder::start(const std::string& name) const {
1408  try {
1409  return (*this)->attribute(Attribute::START, std::vector<std::string>{name});
1410  } catch (std::exception& e) {
1411  THROW_ERROR("start", e.what());
1412  return {}; // never reached
1413  }
1414 }
1415 
1416 std::vector<double> DaeBuilder::start(const std::vector<std::string>& name) const {
1417  try {
1418  return (*this)->attribute(Attribute::START, name);
1419  } catch (std::exception& e) {
1420  THROW_ERROR("start", e.what());
1421  return {}; // never reached
1422  }
1423 }
1424 
1425 void DaeBuilder::set_start(const std::string& name, double val) {
1426  try {
1427  (*this)->set_attribute(Attribute::START, name, val);
1428  } catch (std::exception& e) {
1429  THROW_ERROR("set_start", e.what());
1430  }
1431 }
1432 
1433 void DaeBuilder::set_start(const std::string& name, const std::vector<double>& val) {
1434  try {
1435  (*this)->set_attribute(Attribute::START, std::vector<std::string>{name}, val);
1436  } catch (std::exception& e) {
1437  THROW_ERROR("set_start", e.what());
1438  }
1439 }
1440 
1441 void DaeBuilder::set_start(const std::vector<std::string>& name, const std::vector<double>& val) {
1442  try {
1443  (*this)->set_attribute(Attribute::START, name, val);
1444  } catch (std::exception& e) {
1445  THROW_ERROR("set_start", e.what());
1446  }
1447 }
1448 
1450  try {
1451  (*this)->reset();
1452  } catch (std::exception& e) {
1453  THROW_ERROR("reset", e.what());
1454  }
1455 }
1456 
1457 void DaeBuilder::set(const std::string& name, double val) {
1458  try {
1459  (*this)->set_attribute(Attribute::VALUE, name, val);
1460  } catch (std::exception& e) {
1461  THROW_ERROR("set", e.what());
1462  }
1463 }
1464 
1465 void DaeBuilder::set(const std::string& name, const std::string& val) {
1466  try {
1467  (*this)->set_string_attribute(Attribute::STRINGVALUE, name, val);
1468  } catch (std::exception& e) {
1469  THROW_ERROR("set", e.what());
1470  }
1471 }
1472 
1473 void DaeBuilder::set(const std::vector<std::string>& name, const std::vector<double>& val) {
1474  try {
1475  (*this)->set_attribute(Attribute::VALUE, name, val);
1476  } catch (std::exception& e) {
1477  THROW_ERROR("set", e.what());
1478  }
1479 }
1480 
1481 void DaeBuilder::set(const std::vector<std::string>& name,
1482  const std::vector<std::string>& val) {
1483  try {
1484  (*this)->set_string_attribute(Attribute::STRINGVALUE, name, val);
1485  } catch (std::exception& e) {
1486  THROW_ERROR("set", e.what());
1487  }
1488 }
1489 
1490 GenericType DaeBuilder::get(const std::string& name) const {
1491  return get(std::vector<std::string>{name}).front();
1492 }
1493 
1494 std::vector<GenericType> DaeBuilder::get(const std::vector<std::string>& name) const {
1495  try {
1496  casadi_assert(!(*this)->symbolic_,
1497  "Functionality not implemented for symbolic representation");
1498  // Create a temporary FmuFunction instance
1499  Function f = create(this->name() + "_get", {}, {}, Dict{{"aux", name}});
1500  // Get the stats
1501  Dict stats = f.stats().at("aux");
1502  // Return in the same order as inputs
1503  std::vector<GenericType> ret;
1504  ret.reserve(name.size());
1505  for (const std::string& n : name) ret.push_back(stats.at(n));
1506  return ret;
1507  } catch (std::exception& e) {
1508  THROW_ERROR("get", e.what());
1509  return {};
1510  }
1511 }
1512 
1513 Sparsity DaeBuilder::jac_sparsity(const std::vector<std::string>& onames,
1514  const std::vector<std::string>& inames) const {
1515  try {
1516  return (*this)->jac_sparsity((*this)->find(onames), (*this)->find(inames));
1517  } catch (std::exception& e) {
1518  THROW_ERROR("jac_sparsity", e.what());
1519  return Sparsity(); // never reached
1520  }
1521 }
1522 
1523 } // namespace casadi
double start_time() const
Get the start time.
bool has_rate() const
Is there a rate output?
void when(const MX &cond, const std::vector< std::string > &eqs, const Dict &opts=Dict())
Add when equations.
bool has_fun(const std::string &name) const
Does a particular function already exist?
void register_w(const std::string &name)
std::vector< Function > fun() const
Get all functions.
void set_stop_time(double val)
Set the stop time.
casadi_int nu() const
Free controls.
double nominal(const std::string &name) const
Get the nominal value, single variable.
void set_beq(const std::string &name, const MX &val)
[DEPRECATED] Replaced by eq
void sort_z(const std::vector< std::string > &z_order)
[DEPRECATED] Use reorder("z", new_order)
void sanity_check() const
Check if dimensions match.
Function add_fun(const std::string &name, const std::vector< std::string > &arg, const std::vector< std::string > &res, const Dict &opts=Dict())
Add a function from loaded expressions.
bool has(const std::string &name) const
Check if a particular variable exists.
const std::string & name() const
Name of instance.
Definition: dae_builder.cpp:59
MX add_variable(const std::string &name, casadi_int n=1)
[DEPRECATED] Use add
std::string variability(const std::string &name) const
Get the variability.
Variable & variable(const std::string &name)
void set_value_reference(const std::string &name, casadi_int val)
casadi_int nx() const
Differential states.
void set_start(const std::string &name, double val)
Set the start attribute, single variable.
void sort_d()
[DEPRECATED] Use sort("d")
static bool test_cast(const SharedObjectInternal *ptr)
Check if a particular cast is allowed.
double tolerance() const
Get the tolerance.
std::string initial(const std::string &name) const
std::vector< casadi_int > dimension(const std::string &name) const
Get the dimensions of a variable.
Function dependent_fun(const std::string &fname, const std::vector< std::string > &s_in, const std::vector< std::string > &s_out) const
Construct a function for evaluating dependent parameters.
double max(const std::string &name) const
Get the upper bound, single variable.
std::string unit(const std::string &name) const
void clear_all(const std::string &v)
[DEPRECATED] Use set_variability, set_causality or set_category to change variable category
std::vector< std::string > all() const
Get a list of all variables.
void set_max(const std::string &name, double val)
Set the upper bound, single variable.
std::vector< MX > quad() const
Quadrature equations.
Function oracle(bool sx=false, bool elim_w=false, bool lifted_calls=false) const
Get the (cached) oracle, SX or MX.
const MX & t() const
[DEPRECATED] Renamed "time"
MX add_t(const std::string &name="t")
[DEPRECATED] Replaced by add
std::string category(const std::string &name) const
Get the variable category.
void register_e(const std::string &name)
MX add_p(const std::string &name=std::string())
[DEPRECATED] Replaced by add
casadi_int nd() const
Dependent parameters.
void set_attribute(const std::string &a, const std::string &name, double val)
Set an attribute, single variable.
std::vector< MX > init_lhs() const
Initial conditions, left-hand-side.
void reorder(const std::string &cat, const std::vector< std::string > &v)
Reorder variables in a category.
double step_size() const
Get the step size.
MX add_c(const std::string &name, const MX &new_cdef)
[DEPRECATED] Replaced by add and eq
void gather_fun(casadi_int max_depth=-1)
Collect embedded functions from the expression graph.
const MX & time() const
Expression for independent variable (usually time)
Definition: dae_builder.cpp:63
std::string causality(const std::string &name) const
Get the causality.
casadi_int nc() const
Named constants.
bool provides_directional_derivatives() const
Does the FMU provide support for analytic derivatives.
void set_tolerance(double val)
Set the tolerance.
void register_c(const std::string &name)
void eliminate_w()
[DEPRECATED] Use eliminate("w")
void set_min(const std::string &name, double val)
Set the lower bound, single variable.
std::vector< std::string > rate() const
Get all rate variables.
std::string assign(const std::string &name, const MX &val)
Assignment inside a when-equation or if-else equation.
void tear()
Identify iteration variables and residual equations using naming convention.
Function transition() const
Construct an event transition function, default naming.
std::vector< double > start(const std::string &name) const
Get the start attribute, single variable.
void eliminate_quad()
[DEPRECATED] Use eliminate("q")
void set_type(const std::string &name, const std::string &val)
double attribute(const std::string &a, const std::string &name) const
Get an attribute, single variable.
void set_unit(const std::string &name, const std::string &val)
std::vector< std::string > derivatives() const
Model structure: derivatives.
std::vector< std::string > e() const
[DEPRECATED] Use all("zero") *‍/
void sort(const std::string &cat)
Sort dependent parameters.
double stop_time() const
Get the stop time.
Function create() const
Create a function with standard integrator DAE signature, default naming.
double min(const std::string &name) const
Get the lower bound, single variable.
void set_display_unit(const std::string &name, const std::string &val)
std::string reinit(const std::string &name, const MX &val)
Reinitialize a state inside when-equations.
void register_z(const std::string &name)
void set_init(const std::string &name, const MX &init_rhs)
Specify the initial equation for a variable.
void add_lc(const std::string &name, const std::vector< std::string > &f_out)
Add a named linear combination of output expressions.
void eq(const MX &lhs, const MX &rhs, const Dict &opts=Dict())
Add a simple equation.
void set_y(const std::vector< std::string > &name)
Set all output variables.
MX add_u(const std::string &name=std::string())
[DEPRECATED] Replaced by add
void set_category(const std::string &name, const std::string &val)
Set the variable category, if permitted.
casadi_int nz() const
Algebraic variables.
std::vector< MX > zero() const
Zero-crossing functions.
bool has_beq(const std::string &name) const
Does a variable have a binding equation?
void set_rate(const std::vector< std::string > &name)
Set rate variables.
std::string display_unit(const std::string &name) const
void register_u(const std::string &name)
void set_nominal(const std::string &name, double val)
Set the nominal value, single variable.
std::vector< std::string > der(const std::vector< std::string > &name) const
Get the time derivative of model variables.
void set_causality(const std::string &name, const std::string &val)
Set the causality, if permitted.
void prune(bool prune_p=true, bool prune_u=true)
Prune unused controls.
casadi_int nq() const
Quadrature states.
std::vector< MX > ydef() const
Definitions of output variables.
std::vector< std::string > pre(const std::vector< std::string > &name) const
Get the pre-variables of model variables.
std::vector< MX > alg() const
Algebraic equations.
Definition: dae_builder.cpp:91
MX add_q(const std::string &name=std::string())
[DEPRECATED] Replaced by add
MX add_y(const std::string &name, const MX &new_ydef)
[DEPRECATED] Replaced by add and eq
std::vector< MX > init_rhs() const
Initial conditions, right-hand-side.
void set_variability(const std::string &name, const std::string &val)
Set the variability, if permitted.
MX add(const std::string &name, const std::string &causality, const std::string &variability, const Dict &opts=Dict())
Add a new model variable.
bool has_t() const
Is there a time variable?
void set_start_time(double val)
Set the start time.
DaeBuilder()
Default constructor.
Definition: dae_builder.cpp:51
void set(const std::string &name, double val)
void eliminate(const std::string &cat)
Eliminate all dependent parameters.
casadi_int value_reference(const std::string &name) const
MX add_z(const std::string &name=std::string())
[DEPRECATED] Replaced by add
std::vector< std::string > export_fmu(const Dict &opts=Dict())
Export instance into an FMU.
Sparsity jac_sparsity(const std::vector< std::string > &onames, const std::vector< std::string > &inames) const
Get Jacobian sparsity.
casadi_int np() const
Parameters.
void lift(bool lift_shared=true, bool lift_calls=true)
Lift problem formulation by extracting shared subexpressions.
std::vector< MX > ode() const
Ordinary differential equations (ODE)
Definition: dae_builder.cpp:82
void sort_w()
[DEPRECATED] Use sort("w")
MX var(const std::string &name) const
void register_y(const std::string &name)
MX add_x(const std::string &name=std::string())
[DEPRECATED] Replaced by add
void register_q(const std::string &name)
size_t add_variable_new(const std::string &name, casadi_int n=1)
[DEPRECATED] Use add
casadi_int ny() const
Output variables.
const DaeBuilderInternal * operator->() const
Access a member function or object.
std::vector< MX > cdef() const
Definitions of named constants.
void set_all(const std::string &v, const std::vector< std::string > &name)
[DEPRECATED] Use set_variability, set_causality, set_category and/or reorder
void register_t(const std::string &name)
void load_fmi_description(const std::string &filename)
Import problem description from FMI or XML.
void register_x(const std::string &name)
casadi_int numel(const std::string &name) const
Get the number of elements of a variable.
void register_p(const std::string &name)
casadi_int nw() const
Dependent variables.
MX add_w(const std::string &name, const MX &new_wdef)
[DEPRECATED] Replaced by add and eq
void set_step_size(double val)
Set the step size.
std::vector< MX > ddef() const
Definitions of dependent parameters.
std::vector< std::string > outputs() const
Model structure: outputs.
std::vector< std::string > initial_unknowns() const
Model structure: initial unknowns.
void register_d(const std::string &name)
Variable & new_variable(const std::string &name, casadi_int numel=1)
[DEPRECATED] Use add
casadi_int nzero() const
Zero-crossing functions.
std::string description(const std::string &name) const
void set_initial(const std::string &name, const std::string &val)
size_t find(const std::string &name) const
void eliminate_d()
[DEPRECATED] Use eliminate("d")
MX beq(const std::string &name) const
Get the binding equation for a variable.
std::vector< std::string > y() const
Outputs *‍/.
Definition: dae_builder.cpp:73
std::string type(const std::string &name, casadi_int fmi_version=3) const
MX add_d(const std::string &name, const MX &new_ddef)
[DEPRECATED] Replaced by add and eq
std::vector< MX > wdef() const
Dependent variables and corresponding definitions.
void set_description(const std::string &name, const std::string &val)
virtual const Function & oracle() const
Get oracle.
Function object.
Definition: function.hpp:60
std::vector< Function > find_functions(casadi_int max_depth=-1) const
Get all functions embedded in the expression graphs.
Definition: function.cpp:1878
Dict stats(int mem=0) const
Get all statistics obtained at the end of the last evaluate call.
Definition: function.cpp:928
static MX sym(const std::string &name, casadi_int nrow=1, casadi_int ncol=1)
Create an nrow-by-ncol symbolic primitive.
SharedObjectInternal * get() const
Get a const pointer to the node.
SharedObjectInternal * operator->() const
Access a member function or object.
Generic data type, can hold different types such as bool, casadi_int, std::string etc.
Importer.
Definition: importer.hpp:86
MX - Matrix expression.
Definition: mx.hpp:92
std::string name() const
Get the name.
Definition: mx.cpp:762
bool is_symbolic() const
Check if symbolic.
Definition: mx.cpp:766
General sparsity class.
Definition: sparsity.hpp:106
static Sparsity dense(casadi_int nrow, casadi_int ncol=1)
Create a dense rectangular sparsity pattern *.
Definition: sparsity.cpp:1012
std::vector< std::string > dyn_out()
Get output scheme of a DAE function.
Definition: integrator.cpp:236
std::vector< std::string > dyn_in()
Get input scheme of a DAE function.
Definition: integrator.cpp:232
The casadi namespace.
Definition: archiver.cpp:28
Type from_fmi2(TypeFmi2 v)
TypeFmi2 to_fmi2(Type v)
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
std::vector< casadi_int > path(const std::vector< casadi_int > &map, casadi_int i_start)
Function external(const std::string &name, const Importer &li, const Dict &opts)
Load a just-in-time compiled external function.
Definition: external.cpp:42
std::string filename(const std::string &path)
Definition: ghc.cpp:55
Holds expressions and meta-data corresponding to a physical quantity evolving in time.
casadi_int index
Location in variable vector.
MX v
Variable expression (always a vector)