dae_builder.hpp
1 /*
2  * This file is part of CasADi.
3  *
4  * CasADi -- A symbolic framework for dynamic optimization.
5  * Copyright (C) 2010-2023 Joel Andersson, Joris Gillis, Moritz Diehl,
6  * KU Leuven. All rights reserved.
7  * Copyright (C) 2011-2014 Greg Horn
8  *
9  * CasADi is free software; you can redistribute it and/or
10  * modify it under the terms of the GNU Lesser General Public
11  * License as published by the Free Software Foundation; either
12  * version 3 of the License, or (at your option) any later version.
13  *
14  * CasADi is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17  * Lesser General Public License for more details.
18  *
19  * You should have received a copy of the GNU Lesser General Public
20  * License along with CasADi; if not, write to the Free Software
21  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
22  *
23  */
24 
25 
26 #ifndef CASADI_DAE_BUILDER_HPP
27 #define CASADI_DAE_BUILDER_HPP
28 
29 #include "function.hpp"
30 
31 namespace casadi {
32 
33 // Forward declarations
34 class DaeBuilderInternal;
35 
68 class CASADI_EXPORT DaeBuilder
69  : public SharedObject,
70  public SWIG_IF_ELSE(PrintableCommon, Printable<DaeBuilder>) {
71  public:
72 
74  std::string type_name() const {return "DaeBuilder";}
75 
77  DaeBuilder();
78 
80  explicit DaeBuilder(const std::string& name, const std::string& path = "",
81  const Dict& opts = Dict());
82 
86  const std::string& name() const;
87 
90 
93  const MX& time() const;
94 
98  std::vector<std::string> t_new() const {return all("t");}
99 
103  std::vector<std::string> x() const {return all("x");}
104 
108  std::vector<std::string> y() const {return all("y");}
109 
113  std::vector<std::string> z() const {return all("z");}
114 
118  std::vector<std::string> q() const {return all("q");}
119 
123  std::vector<MX> inputs(const std::string& cat) const;
124 
128  std::vector<MX> outputs(const std::string& cat) const;
129 
130 #ifdef WITH_DEPRECATED_FEATURES
132  std::vector<MX> ode() const {return outputs("ode");}
133 
135  std::vector<MX> alg() const {return outputs("alg");}
136 
138  std::vector<MX> quad() const {return outputs("quad");}
139 
141  std::vector<MX> zero() const {return outputs("zero");}
142 
144  std::vector<MX> ydef() const {return outputs("y");}
145 
147  void set_y(const std::vector<std::string>& name) {set_all("y", name);}
148 #endif // WITH_DEPRECATED_FEATURES
149 
153  std::vector<std::string> u() const {return all("u");}
154 
158  std::vector<std::string> p() const {return all("p");}
159 
163  std::vector<std::string> c() const {return all("c");}
164 
168  std::vector<MX> cdef() const;
169 
173  std::vector<std::string> d() const {return all("d");}
174 
180  std::vector<MX> ddef() const;
181 
185  std::vector<std::string> w() const {return all("w");}
186 
192  std::vector<MX> wdef() const;
193 
197  std::vector<MX> init_lhs() const;
198 
202  std::vector<MX> init_rhs() const;
203 
204 #ifdef WITH_DEPRECATED_FEATURES
206  std::vector<std::string> outputs() const {return y();}
207 
209  std::vector<std::string> derivatives() const {return der();}
210 #endif // WITH_DEPRECATED_FEATURES
211 
215  std::vector<std::string> initial_unknowns() const;
216 
219 
223  bool has_t() const;
224 
228  casadi_int nx() const;
229 
233  casadi_int nz() const;
234 
238  casadi_int nq() const;
239 
243  casadi_int nzero() const;
244 
248  casadi_int ny() const;
249 
253  casadi_int nu() const;
254 
258  casadi_int np() const;
259 
263  casadi_int nc() const;
264 
268  casadi_int nd() const;
269 
273  casadi_int nw() const;
275 
280 
282  MX add(const std::string& name,
283  const std::string& causality,
284  const std::string& variability,
285  const Dict& opts=Dict());
286 
288  MX add(const std::string& name,
289  const std::string& causality,
290  const Dict& opts=Dict());
291 
293  MX add(const std::string& name,
294  const Dict& opts=Dict());
295 
297  void add(const std::string& name,
298  const std::string& causality,
299  const std::string& variability,
300  const MX& expr,
301  const Dict& opts=Dict());
302 
304  void eq(const MX& lhs, const MX& rhs, const Dict& opts=Dict());
305 
307  void when(const MX& cond, const std::vector<std::string>& eqs, const Dict& opts=Dict());
308 
310  std::string assign(const std::string& name, const MX& val);
311 
313  std::string reinit(const std::string& name, const MX& val);
314 
316  void set_init(const std::string& name, const MX& init_rhs);
317 
319  void sanity_check() const;
321 
323  void reorder(const std::string& cat, const std::vector<std::string>& v);
324 
326  void set_all(const std::string& v, const std::vector<std::string>& name);
327 
332 
333 
335  void eliminate(const std::string& cat);
336 
338  void sort(const std::string& cat);
339 
341  void lift(bool lift_shared = true, bool lift_calls = true);
342 
344  void prune(bool prune_p = true, bool prune_u = true);
345 
347  void tear();
349 
354 
356  Function add_fun(const std::string& name,
357  const std::vector<std::string>& arg,
358  const std::vector<std::string>& res, const Dict& opts=Dict());
359 
361  Function add_fun(const Function& f);
362 
364  Function add_fun(const std::string& name, const Importer& compiler,
365  const Dict& opts=Dict());
366 
368  bool has_fun(const std::string& name) const;
369 
371  Function fun(const std::string& name) const;
372 
374  std::vector<Function> fun() const;
375 
377  void gather_fun(casadi_int max_depth = -1);
379 
384  void parse_fmi(const std::string& filename) {load_fmi_description(filename); }
385 
387  bool provides_directional_derivatives() const;
388 
390  bool provides_directional_derivative() const {return provides_directional_derivatives();}
391 
393  void load_fmi_description(const std::string& filename);
394 
396  Dict export_fmu(const Dict& opts=Dict());
397 
405  Dict compile_fmu(const Dict& files, const Dict& opts=Dict());
406 
413  std::string pack_fmu(const Dict& files, const Dict& opts=Dict());
414 
416  void add_lc(const std::string& name, const std::vector<std::string>& f_out);
417 
419  Function create(const std::string& fname,
420  const std::vector<std::string>& name_in,
421  const std::vector<std::string>& name_out, bool sx, bool lifted_calls = false) const;
422 
431  Function create(const std::string& name,
432  const std::vector<std::string>& name_in,
433  const std::vector<std::string>& name_out,
434  const Dict& opts=Dict()) const;
436 
443  Function create(const std::string& fname, const Dict& opts=Dict()) const;
444 
448  Function create() const {return create(name() + "_dae");}
449 
451  Function dependent_fun(const std::string& fname,
452  const std::vector<std::string>& s_in,
453  const std::vector<std::string>& s_out) const;
454 
456  Function transition(const std::string& fname, casadi_int index) const;
457 
459  Function transition(const std::string& fname) const;
460 
462  Function transition() const {return transition(name() + "_transition");}
463 
466  MX var(const std::string& name) const;
467  MX operator()(const std::string& name) const {return var(name);}
469 
473  std::vector<std::string> der() const;
474 
476  std::vector<std::string> der(const std::vector<std::string>& name) const;
477 
480  MX der(const MX& v) const;
481  MX der(const MX& v);
483 
485  std::vector<std::string> pre(const std::vector<std::string>& name) const;
486 
488  MX pre(const MX& v) const;
489 
491  bool has_beq(const std::string& name) const;
492 
494  MX beq(const std::string& name) const;
495 
498  casadi_int value_reference(const std::string& name) const;
499  void set_value_reference(const std::string& name, casadi_int val);
501 
504  std::string description(const std::string& name) const;
505  void set_description(const std::string& name, const std::string& val);
507 
510  std::string type(const std::string& name, casadi_int fmi_version = 3) const;
511  void set_type(const std::string& name, const std::string& val);
513 
515  std::string causality(const std::string& name) const;
516 
518  std::vector<std::string> categories(const std::string& name) const;
519 
533  void set_causality(const std::string& name, const std::string& val);
534 
536  std::string variability(const std::string& name) const;
537 
548  void set_variability(const std::string& name, const std::string& val);
549 
551  std::string category(const std::string& name) const;
552 
563  void set_category(const std::string& name, const std::string& val);
564 
567  std::string initial(const std::string& name) const;
568  void set_initial(const std::string& name, const std::string& val);
570 
573  std::string unit(const std::string& name) const;
574  void set_unit(const std::string& name, const std::string& val);
576 
579  std::string display_unit(const std::string& name) const;
580  void set_display_unit(const std::string& name, const std::string& val);
582 
584  casadi_int numel(const std::string& name) const;
585 
587  std::vector<casadi_int> dimension(const std::string& name) const;
588 
590  double start_time() const;
591 
593  void set_start_time(double val);
594 
596  double stop_time() const;
597 
599  void set_stop_time(double val);
600 
602  double tolerance() const;
603 
605  void set_tolerance(double val);
606 
608  double step_size() const;
609 
611  void set_step_size(double val);
612 
613  // The following routines are not needed in MATLAB and would cause ambiguity
614  // Note that a multirow strings can be interpreted as a vector of strings
615 #if !(defined(SWIG) && defined(SWIGMATLAB))
617  std::string der(const std::string& name) const;
618 
620  std::string pre(const std::string& name) const;
621 
623  double attribute(const std::string& a, const std::string& name) const;
624 
626  void set_attribute(const std::string& a, const std::string& name, double val);
627 
629  double min(const std::string& name) const;
630 
632  void set_min(const std::string& name, double val);
633 
635  double max(const std::string& name) const;
636 
638  void set_max(const std::string& name, double val);
639 
641  double nominal(const std::string& name) const;
642 
644  void set_nominal(const std::string& name, double val);
645 
647  std::vector<double> start(const std::string& name) const;
648 
650  void set_start(const std::string& name, double val);
651 
653  void set_start(const std::string& name, const std::vector<double>& val);
654 
655  // Clear all set values
656  void reset();
657 
658  // Set the current value, single value
659  void set(const std::string& name, double val);
660 
661  // Set the current value, single value (string)
662  void set(const std::string& name, const std::string& val);
663 
665  GenericType get(const std::string& name) const;
666 
668  bool symbolic() const;
669 
670 #endif // !SWIGMATLAB
671 
673  std::vector<double> attribute(const std::string& a, const std::vector<std::string>& name) const;
674 
676  void set_attribute(const std::string& a, const std::vector<std::string>& name,
677  const std::vector<double>& val);
678 
680  std::vector<double> min(const std::vector<std::string>& name) const;
681 
683  void set_min(const std::vector<std::string>& name, const std::vector<double>& val);
684 
686  std::vector<double> max(const std::vector<std::string>& name) const;
687 
689  void set_max(const std::vector<std::string>& name, const std::vector<double>& val);
690 
692  std::vector<double> nominal(const std::vector<std::string>& name) const;
693 
695  void set_nominal(const std::vector<std::string>& name, const std::vector<double>& val);
696 
698  std::vector<double> start(const std::vector<std::string>& name) const;
699 
701  void set_start(const std::vector<std::string>& name, const std::vector<double>& val);
702 
704  void set(const std::vector<std::string>& name, const std::vector<double>& val);
705 
707  void set(const std::vector<std::string>& name, const std::vector<std::string>& val);
708 
710  std::vector<GenericType> get(const std::vector<std::string>& name) const;
711 
713  bool has(const std::string& name) const;
714 
716  std::vector<std::string> all() const;
717 
719  std::vector<std::string> all(const std::string& cat) const;
720 
722  Function oracle(bool sx = false, bool elim_w = false, bool lifted_calls = false) const;
723 
727  Sparsity jac_sparsity(const std::vector<std::string>& onames,
728  const std::vector<std::string>& inames) const;
729 
730 #ifndef SWIG
732  const DaeBuilderInternal* operator->() const;
733 
735  DaeBuilderInternal* operator->();
736 
738  static bool test_cast(const SharedObjectInternal* ptr);
739 
740 #endif // SWIG
741 };
742 
743 } // namespace casadi
744 
745 #endif // CASADI_DAE_BUILDER_HPP
A symbolic representation of a differential-algebraic equations model.
Definition: dae_builder.hpp:70
std::vector< std::string > q() const
Quadrature states.
std::vector< std::string > c() const
Named constants.
void parse_fmi(const std::string &filename)
Function transition() const
Construct an event transition function, default naming.
std::string type_name() const
Readable name of the class.
Definition: dae_builder.hpp:74
std::vector< std::string > derivatives() const
[DEPRECATED] Renamed "der"
bool provides_directional_derivative() const
Does the FMU provide support for analytic derivatives (FMI 2 naming)
Function create() const
Create a function with standard integrator DAE signature, default naming.
MX operator()(const std::string &name) const
std::vector< MX > alg() const
[DEPRECATED] Replaced with outputs("alg")
void set_y(const std::vector< std::string > &name)
[DEPRECATED] Replaced with set_all("y", name)
std::vector< std::string > x() const
Differential states.
std::vector< std::string > y() const
Outputs.
std::vector< std::string > z() const
Algebraic variables.
std::vector< MX > ode() const
[DEPRECATED] Replaced with outputs("ode")
std::vector< std::string > t_new() const
Independent variable (usually time)
Definition: dae_builder.hpp:98
std::vector< MX > ydef() const
[DEPRECATED] Replaced with outputs("y")
std::vector< std::string > p() const
Parameters.
std::vector< std::string > d() const
Dependent parameters.
std::vector< MX > zero() const
[DEPRECATED] Replaced with outputs("zero")
std::vector< std::string > outputs() const
[DEPRECATED] Renamed "y"
std::vector< std::string > w() const
Dependent variables.
std::vector< std::string > u() const
Free controls.
std::vector< MX > quad() const
[DEPRECATED] Replaced with outputs("quad")
Function object.
Definition: function.hpp:60
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
GenericShared implements a reference counting framework similar for efficient and.
General sparsity class.
Definition: sparsity.hpp:106
The casadi namespace.
Definition: archiver.cpp:28
std::string description(Category v)
void sort(const std::vector< T > &values, std::vector< T > &sorted_values, std::vector< casadi_int > &indices, bool invert_indices=false)
Sort the data in a vector.
GenericType::Dict Dict
C++ equivalent of Python's dict or MATLAB's struct.
bool all(const std::vector< bool > &v)
Check if all arguments are true.
Definition: casadi_misc.cpp:81
std::vector< casadi_int > path(const std::vector< casadi_int > &map, casadi_int i_start)
std::string filename(const std::string &path)
Definition: ghc.cpp:55