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 
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& t() const;
94 
98  std::vector<std::string> x() const;
99 
103  std::vector<MX> ode() const;
104 
108  std::vector<std::string> z() const;
109 
113  std::vector<MX> alg() const;
114 
118  std::vector<std::string> q() const;
119 
123  std::vector<MX> quad() const;
124 
128  std::vector<std::string> y() const;
129 
133  std::vector<MX> ydef() const;
134 
138  std::vector<std::string> u() const;
139 
143  std::vector<std::string> p() const;
144 
148  std::vector<std::string> c() const;
149 
153  std::vector<MX> cdef() const;
154 
158  std::vector<std::string> d() const;
159 
165  std::vector<MX> ddef() const;
166 
170  std::vector<std::string> w() const;
171 
177  std::vector<MX> wdef() const;
178 
182  const std::vector<MX>& aux() const;
183 
187  const std::vector<MX>& init_lhs() const;
188 
192  const std::vector<MX>& init_rhs() const;
193 
197  const std::vector<MX>& when_cond() const;
198 
202  const std::vector<MX>& when_lhs() const;
203 
207  const std::vector<MX>& when_rhs() const;
209 
213  std::vector<std::string> outputs() const;
214 
218  std::vector<std::string> derivatives() const;
219 
223  std::vector<std::string> initial_unknowns() const;
224 
227 
231  bool has_t() const;
232 
236  casadi_int nx() const;
237 
241  casadi_int nz() const;
242 
246  casadi_int nq() const;
247 
251  casadi_int ny() const;
252 
256  casadi_int nu() const;
257 
261  casadi_int np() const;
262 
266  casadi_int nc() const;
267 
271  casadi_int nd() const;
272 
276  casadi_int nw() const;
278 
284  MX add_t(const std::string& name="t");
285 
287  MX add_p(const std::string& name=std::string());
288 
290  MX add_u(const std::string& name=std::string());
291 
293  MX add_x(const std::string& name=std::string());
294 
296  MX add_z(const std::string& name=std::string());
297 
299  MX add_q(const std::string& name=std::string());
300 
302  MX add_c(const std::string& name, const MX& new_cdef);
303 
305  MX add_d(const std::string& name, const MX& new_ddef);
306 
308  MX add_w(const std::string& name, const MX& new_wdef);
309 
311  MX add_y(const std::string& name, const MX& new_ydef);
312 
314  void set_ode(const std::string& name, const MX& ode_rhs);
315 
317  void set_alg(const std::string& name, const MX& alg_rhs);
318 
320  MX add_aux(const std::string& name=std::string(), casadi_int n=1);
321 
323  void add_init(const MX& lhs, const MX& rhs);
324 
326  void add_when(const MX& cond, const MX& lhs, const MX& rhs);
327 
329  void sanity_check() const;
331 
333  void clear_all(const std::string& v);
334 
336  void set_all(const std::string& v, const std::vector<std::string>& name);
337 
340  void register_t(const std::string& name);
341  void register_p(const std::string& name);
342  void register_u(const std::string& name);
343  void register_x(const std::string& name);
344  void register_z(const std::string& name);
345  void register_q(const std::string& name);
346  void register_c(const std::string& name);
347  void register_d(const std::string& name);
348  void register_w(const std::string& name);
349  void register_y(const std::string& name);
351 
352 #ifdef WITH_DEPRECATED_FEATURES
355  void set_u(const std::vector<std::string>& name) { set_all("u", name);}
356  void set_x(const std::vector<std::string>& name) { set_all("x", name);}
357  void set_z(const std::vector<std::string>& name,
358  const std::vector<std::string>& alg = std::vector<std::string>());
359  void set_q(const std::vector<std::string>& name) { set_all("q", name);}
360  void set_y(const std::vector<std::string>& name) { set_all("y", name);}
362 #endif // WITH_DEPRECATED_FEATURES
363 
368 
369 #ifdef WITH_DEPRECATED_FEATURES
371  void clear_in(const std::string& v) { clear_all(v);}
372 #endif // WITH_DEPRECATED_FEATURES
373 
375  void eliminate_w();
376 
378  void lift(bool lift_shared = true, bool lift_calls = true);
379 
382 
384  void sort_d();
385 
387  void sort_w();
388 
390  void sort_z(const std::vector<std::string>& z_order);
391 
393  void prune(bool prune_p = true, bool prune_u = true);
394 
396  void tear();
398 
403 
405  Function add_fun(const std::string& name,
406  const std::vector<std::string>& arg,
407  const std::vector<std::string>& res, const Dict& opts=Dict());
408 
411 
413  Function add_fun(const std::string& name, const Importer& compiler,
414  const Dict& opts=Dict());
415 
417  bool has_fun(const std::string& name) const;
418 
420  Function fun(const std::string& name) const;
421 
423  std::vector<Function> fun() const;
424 
426  void gather_fun(casadi_int max_depth = -1);
428 
433  void parse_fmi(const std::string& filename) {load_fmi_description(filename); }
434 
437 
439  void load_fmi_description(const std::string& filename);
440 
442  std::vector<std::string> export_fmu(const Dict& opts=Dict());
443 
445  void add_lc(const std::string& name, const std::vector<std::string>& f_out);
446 
448  Function create(const std::string& fname,
449  const std::vector<std::string>& name_in,
450  const std::vector<std::string>& name_out, bool sx, bool lifted_calls = false) const;
451 
460  Function create(const std::string& name,
461  const std::vector<std::string>& name_in,
462  const std::vector<std::string>& name_out,
463  const Dict& opts=Dict()) const;
465 
472  Function create(const std::string& name, const Dict& opts=Dict()) const;
473 
475  Function dependent_fun(const std::string& fname,
476  const std::vector<std::string>& s_in,
477  const std::vector<std::string>& s_out) const;
478 
481  MX var(const std::string& name) const;
482  MX operator()(const std::string& name) const {return var(name);}
484 
486  std::vector<std::string> der(const std::vector<std::string>& name) const;
487 
490  MX beq(const std::string& name) const;
491  void set_beq(const std::string& name, const MX& val);
493 
496  casadi_int value_reference(const std::string& name) const;
497  void set_value_reference(const std::string& name, casadi_int val);
499 
502  std::string description(const std::string& name) const;
503  void set_description(const std::string& name, const std::string& val);
505 
508  std::string type(const std::string& name, casadi_int fmi_version = 3) const;
509  void set_type(const std::string& name, const std::string& val);
511 
514  std::string causality(const std::string& name) const;
515  void set_causality(const std::string& name, const std::string& val);
517 
520  std::string variability(const std::string& name) const;
521  void set_variability(const std::string& name, const std::string& val);
523 
526  std::string initial(const std::string& name) const;
527  void set_initial(const std::string& name, const std::string& val);
529 
532  std::string unit(const std::string& name) const;
533  void set_unit(const std::string& name, const std::string& val);
535 
538  std::string display_unit(const std::string& name) const;
539  void set_display_unit(const std::string& name, const std::string& val);
541 
543  casadi_int numel(const std::string& name) const;
544 
546  std::vector<casadi_int> dimension(const std::string& name) const;
547 
548  // The following routines are not needed in MATLAB and would cause ambiguity
549  // Note that a multirow strings can be interpreted as a vector of strings
550 #if !(defined(SWIG) && defined(SWIGMATLAB))
552  std::string der(const std::string& name) const;
553 
555  double attribute(const std::string& a, const std::string& name) const;
556 
558  void set_attribute(const std::string& a, const std::string& name, double val);
559 
561  double min(const std::string& name) const;
562 
564  void set_min(const std::string& name, double val);
565 
567  double max(const std::string& name) const;
568 
570  void set_max(const std::string& name, double val);
571 
573  double nominal(const std::string& name) const;
574 
576  void set_nominal(const std::string& name, double val);
577 
579  double start(const std::string& name) const;
580 
582  void set_start(const std::string& name, double val);
583 
584  // Clear all set values
585  void reset();
586 
587  // Set the current value, single value
588  void set(const std::string& name, double val);
589 
590  // Set the current value, single value (string)
591  void set(const std::string& name, const std::string& val);
592 
594  GenericType get(const std::string& name) const;
595 
596 #endif // !SWIGMATLAB
597 
599  std::vector<double> attribute(const std::string& a, const std::vector<std::string>& name) const;
600 
602  void set_attribute(const std::string& a, const std::vector<std::string>& name,
603  const std::vector<double>& val);
604 
606  std::vector<double> min(const std::vector<std::string>& name) const;
607 
609  void set_min(const std::vector<std::string>& name, const std::vector<double>& val);
610 
612  std::vector<double> max(const std::vector<std::string>& name) const;
613 
615  void set_max(const std::vector<std::string>& name, const std::vector<double>& val);
616 
618  std::vector<double> nominal(const std::vector<std::string>& name) const;
619 
621  void set_nominal(const std::vector<std::string>& name, const std::vector<double>& val);
622 
624  std::vector<double> start(const std::vector<std::string>& name) const;
625 
627  void set_start(const std::vector<std::string>& name, const std::vector<double>& val);
628 
630  void set(const std::vector<std::string>& name, const std::vector<double>& val);
631 
633  void set(const std::vector<std::string>& name, const std::vector<std::string>& val);
634 
636  std::vector<GenericType> get(const std::vector<std::string>& name) const;
637 
639  MX add_variable(const std::string& name, casadi_int n=1);
640 
642  MX add_variable(const std::string& name, const Sparsity& sp);
643 
645  void add_variable(const MX& new_v);
646 
648  size_t add_variable_new(const std::string& name, casadi_int n=1);
649 
651  size_t add_variable_new(const std::string& name, const Sparsity& sp);
652 
654  size_t add_variable_new(const MX& new_v);
655 
657  bool has_variable(const std::string& name) const;
658 
660  std::vector<std::string> all_variables() const;
661 
663  Function oracle(bool sx = false, bool elim_w = false, bool lifted_calls = false) const;
664 
668  Sparsity jac_sparsity(const std::vector<std::string>& onames,
669  const std::vector<std::string>& inames) const;
670 
671 #ifndef SWIG
673  Variable& new_variable(const std::string& name, casadi_int numel = 1);
674 
677  Variable& variable(const std::string& name);
678  const Variable& variable(const std::string& name) const;
680 
683  Variable& variable(size_t ind);
684  const Variable& variable(size_t ind) const;
686 
688  const DaeBuilderInternal* operator->() const;
689 
691  DaeBuilderInternal* operator->();
692 
694  static bool test_cast(const SharedObjectInternal* ptr);
695 
697  const MX& var(size_t ind) const;
698 
700  std::vector<MX> var(const std::vector<size_t>& ind) const;
701 
703  size_t find(const std::string& name) const;
704 
706  std::vector<size_t> find(const std::vector<std::string>& name) const;
707 
711  const std::string& name(size_t ind) const;
712 
716  std::vector<std::string> name(const std::vector<size_t>& ind) const;
717 
718 #endif // SWIG
719 };
720 
721 } // namespace casadi
722 
723 #endif // CASADI_DAE_BUILDER_HPP
A symbolic representation of a differential-algebraic equations model.
Definition: dae_builder.hpp:70
DaeBuilder(const std::string &name, const std::string &path="", const Dict &opts=Dict())
Construct a DaeBuilder instance.
bool has_fun(const std::string &name) const
Does a particular function already exist?
Function create(const std::string &fname, const std::vector< std::string > &name_in, const std::vector< std::string > &name_out, bool sx, bool lifted_calls=false) const
Construct a function object, legacy syntax.
std::vector< std::string > export_fmu(const Dict &opts=Dict())
Export instance into an FMU.
void register_w(const std::string &name)
void set(const std::vector< std::string > &name, const std::vector< std::string > &val)
Set the current value (string)
std::vector< std::string > q() const
Quadrature states.
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)
void sort_z(const std::vector< std::string > &z_order)
Sort algebraic variables.
Function add_fun(const std::string &name, const Importer &compiler, const Dict &opts=Dict())
Add an external function.
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.
MX add_variable(const std::string &name, casadi_int n=1)
Add a new variable: returns corresponding symbolic expression.
std::string variability(const std::string &name) const
void set_alg(const std::string &name, const MX &alg_rhs)
Specificy the residual equation for an algebraic variable.
std::vector< double > min(const std::vector< std::string > &name) const
Get the lower bound.
const std::vector< MX > & when_cond() const
When statement: triggering condition.
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.
double start(const std::string &name) const
Get the start attribute, single variable.
void sort_d()
Sort dependent parameters.
std::string initial(const std::string &name) const
std::vector< double > attribute(const std::string &a, const std::vector< std::string > &name) const
Get an attribute.
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::vector< MX > cdef() const
Definitions of named constants.
void set(const std::string &name, const std::string &val)
std::string unit(const std::string &name) const
void clear_all(const std::string &v)
Clear all variables of a type.
void set_min(const std::vector< std::string > &name, const std::vector< double > &val)
Set the lower bound.
void set_max(const std::string &name, double val)
Set the upper bound, single variable.
std::string der(const std::string &name) const
Get the time derivative of an expression, single variable.
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
Independent variable (usually time)
MX add_t(const std::string &name="t")
void set_ode(const std::string &name, const MX &ode_rhs)
Specify the ordinary differential equation for a state.
Function fun(const std::string &name) const
Get function by name.
std::vector< std::string > c() const
Named constants.
const std::vector< MX > & init_rhs() const
Initial conditions, right-hand-side.
MX add_p(const std::string &name=std::string())
Add a new parameter.
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< std::string > der(const std::vector< std::string > &name) const
Get the time derivative of an expression.
MX add_c(const std::string &name, const MX &new_cdef)
Add a new constant.
void parse_fmi(const std::string &filename)
void gather_fun(casadi_int max_depth=-1)
Collect embedded functions from the expression graph.
MX add_variable(const std::string &name, const Sparsity &sp)
Add a new variable: returns corresponding symbolic expression.
const std::string & name() const
Name of instance.
std::string causality(const std::string &name) const
std::vector< double > start(const std::vector< std::string > &name) const
Get the start attribute.
casadi_int nc() const
Named constants.
void register_c(const std::string &name)
void eliminate_w()
Eliminate all dependent variables.
void set_min(const std::string &name, double val)
Set the lower bound, single variable.
std::vector< MX > wdef() const
Dependent variables and corresponding definitions.
bool has_variable(const std::string &name) const
Check if a particular variable exists.
void tear()
Identify iteration variables and residual equations using naming convention.
std::string type_name() const
Readable name of the class.
Definition: dae_builder.hpp:74
void eliminate_quad()
Eliminate quadrature states and turn them into ODE states.
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.
bool provides_directional_derivative() const
Does the FMU provide support for analytic derivatives.
std::vector< Function > fun() const
Get all functions.
double min(const std::string &name) const
Get the lower bound, single variable.
void add_when(const MX &cond, const MX &lhs, const MX &rhs)
Add a when statement.
void set_max(const std::vector< std::string > &name, const std::vector< double > &val)
Set the upper bound.
void set_x(const std::vector< std::string > &name)
Function add_fun(const Function &f)
Add an already existing function.
void set_display_unit(const std::string &name, const std::string &val)
std::vector< double > max(const std::vector< std::string > &name) const
Get the upper bound.
size_t add_variable_new(const std::string &name, const Sparsity &sp)
Add a new variable: returns corresponding symbolic expression.
MX operator()(const std::string &name) const
Function create(const std::string &name, const std::vector< std::string > &name_in, const std::vector< std::string > &name_out, const Dict &opts=Dict()) const
Construct a function object, names provided.
void register_z(const std::string &name)
std::vector< double > nominal(const std::vector< std::string > &name) const
Get the nominal value.
std::vector< MX > alg() const
Algebraic equations.
void add_lc(const std::string &name, const std::vector< std::string > &f_out)
Add a named linear combination of output expressions.
void set_y(const std::vector< std::string > &name)
MX add_u(const std::string &name=std::string())
Add a new control.
std::vector< std::string > x() const
Differential states.
casadi_int nz() const
Algebraic variables.
std::vector< std::string > y() const
Output variables.
size_t add_variable_new(const MX &new_v)
Add a new variable from symbolic expressions.
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< casadi_int > dimension(const std::string &name) const
Get the dimensions of a variable.
Function create(const std::string &name, const Dict &opts=Dict()) const
Load a function from an FMU DLL, standard IO conforming with simulator.
const std::vector< MX > & aux() const
Auxiliary variables: Used e.g. to define functions.
void set_causality(const std::string &name, const std::string &val)
void prune(bool prune_p=true, bool prune_u=true)
Prune unused controls.
casadi_int nq() const
Quadrature states.
void set_q(const std::vector< std::string > &name)
std::vector< std::string > z() const
Algebraic variables.
MX add_q(const std::string &name=std::string())
Add a new quadrature state.
void set_attribute(const std::string &a, const std::vector< std::string > &name, const std::vector< double > &val)
Set an attribute.
MX add_y(const std::string &name, const MX &new_ydef)
Add a new output.
const std::vector< MX > & when_lhs() const
When statement: left-hand-side.
void set_variability(const std::string &name, const std::string &val)
const std::vector< MX > & when_rhs() const
When statement: right-hand-side.
void set_u(const std::vector< std::string > &name)
std::vector< MX > ode() const
Ordinary differential equations (ODE)
void clear_in(const std::string &v)
[DEPRECATED] Clear input variable: Replaced by clear_all
bool has_t() const
Is there a time variable?
DaeBuilder()
Default constructor.
void set(const std::string &name, double val)
std::vector< std::string > initial_unknowns() const
Model structure: initial unknowns.
void add_init(const MX &lhs, const MX &rhs)
Add an initial equation.
casadi_int value_reference(const std::string &name) const
MX add_z(const std::string &name=std::string())
Add a new algebraic variable.
std::vector< MX > ydef() const
Definitions of output variables.
std::vector< std::string > p() const
Parameters.
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 set_z(const std::vector< std::string > &name, const std::vector< std::string > &alg=std::vector< std::string >())
std::vector< std::string > d() const
Dependent parameters.
void lift(bool lift_shared=true, bool lift_calls=true)
Lift problem formulation by extracting shared subexpressions.
void sort_w()
Sort dependent variables.
MX var(const std::string &name) const
void register_y(const std::string &name)
MX add_x(const std::string &name=std::string())
Add a new differential state.
void register_q(const std::string &name)
size_t add_variable_new(const std::string &name, casadi_int n=1)
Add a new variable: returns corresponding symbolic expression.
casadi_int ny() const
Output variables.
void set(const std::vector< std::string > &name, const std::vector< double > &val)
Set the current value.
MX add_aux(const std::string &name=std::string(), casadi_int n=1)
Add an auxiliary variable.
void set_all(const std::string &v, const std::vector< std::string > &name)
Set all variables of a type.
const std::vector< MX > & init_lhs() const
Initial conditions, left-hand-side.
void register_t(const std::string &name)
std::vector< std::string > outputs() const
Model structure: outputs.
void load_fmi_description(const std::string &filename)
Import problem description from FMI or XML.
void register_x(const std::string &name)
std::vector< GenericType > get(const std::vector< std::string > &name) const
Evaluate the values for a set of variables at the initial time.
casadi_int numel(const std::string &name) const
Get the number of elements of a variable.
GenericType get(const std::string &name) const
Evaluate the values for a set of variables at the initial time, single value.
std::vector< std::string > w() const
Dependent variables.
std::vector< std::string > all_variables() const
Get a list of all variables.
std::vector< MX > ddef() const
Definitions of dependent parameters.
void register_p(const std::string &name)
casadi_int nw() const
Dependent variables.
MX add_w(const std::string &name, const MX &new_wdef)
Add a new dependent variable.
void register_d(const std::string &name)
void add_variable(const MX &new_v)
Add a new variable from symbolic expressions.
std::string description(const std::string &name) const
void set_initial(const std::string &name, const std::string &val)
std::vector< std::string > u() const
Free controls.
void set_nominal(const std::vector< std::string > &name, const std::vector< double > &val)
Set the nominal value.
void set_start(const std::vector< std::string > &name, const std::vector< double > &val)
Set the start attribute.
std::vector< MX > quad() const
Quadrature equations.
MX beq(const std::string &name) const
std::string type(const std::string &name, casadi_int fmi_version=3) const
MX add_d(const std::string &name, const MX &new_ddef)
Add a new dependent parameter.
void set_description(const std::string &name, const std::string &val)
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:84
SharedObject implements a reference counting framework similar for efficient and.
General sparsity class.
Definition: sparsity.hpp:99
The casadi namespace.
GenericType::Dict Dict
C++ equivalent of Python's dict or MATLAB's struct.
Holds expressions and meta-data corresponding to a physical quantity evolving in time.