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& 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 
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 
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
double start_time() const
Get the start time.
const MX & time() const
Expression for independent variable (usually time)
std::vector< MX > outputs(const std::string &cat) const
Output expressions for a specific category.
DaeBuilder(const std::string &name, const std::string &path="", const Dict &opts=Dict())
Construct a DaeBuilder instance.
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?
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.
void set_start(const std::string &name, const std::vector< double > &val)
Set the start attribute, vector argument.
void set(const std::vector< std::string > &name, const std::vector< std::string > &val)
Set the current value (string)
void set_stop_time(double val)
Set the stop time.
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.
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.
bool has(const std::string &name) const
Check if a particular variable exists.
std::string variability(const std::string &name) const
Get the variability.
std::vector< double > min(const std::vector< std::string > &name) const
Get the lower bound.
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.
Function transition(const std::string &fname, casadi_int index) const
Construct a function describing transition at a specific events.
double tolerance() const
Get the tolerance.
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
MX der(const MX &v)
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 model variables, single variable.
Function oracle(bool sx=false, bool elim_w=false, bool lifted_calls=false) const
Get the (cached) oracle, SX or MX.
Function fun(const std::string &name) const
Get function by name.
std::vector< std::string > c() const
Named constants.
std::string category(const std::string &name) const
Get the variable category.
casadi_int nd() const
Dependent parameters.
void set_attribute(const std::string &a, const std::string &name, double val)
Set an attribute, single variable.
void reorder(const std::string &cat, const std::vector< std::string > &v)
Reorder variables in a category.
MX der(const MX &v) const
std::vector< double > start(const std::string &name) const
Get the start attribute, single variable.
std::vector< std::string > der(const std::vector< std::string > &name) const
Get the time derivative of model variables.
double step_size() const
Get the step size.
void parse_fmi(const std::string &filename)
void gather_fun(casadi_int max_depth=-1)
Collect embedded functions from the expression graph.
const std::string & name() const
Name of instance.
std::string pre(const std::string &name) const
Get the pre-variables of model variables.
std::string causality(const std::string &name) const
Get the causality.
std::vector< double > start(const std::vector< std::string > &name) const
Get the start attribute.
casadi_int nc() const
Named constants.
bool provides_directional_derivatives() const
Does the FMU provide support for analytic derivatives.
MX add(const std::string &name, const Dict &opts=Dict())
Add a new model variable, default variability and causality.
void set_tolerance(double val)
Set the tolerance.
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.
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.
Function create(const std::string &fname, const Dict &opts=Dict()) const
Create a function with standard integrator DAE signature.
std::string type_name() const
Readable name of the class.
Definition: dae_builder.hpp:74
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)
void sort(const std::string &cat)
Sort dependent parameters.
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)
double stop_time() const
Get the stop time.
Function create() const
Create a function with standard integrator DAE signature, default naming.
std::vector< Function > fun() const
Get all functions.
double min(const std::string &name) const
Get the lower bound, single variable.
std::vector< std::string > all() const
Get a list of all variables.
void set_max(const std::vector< std::string > &name, const std::vector< double > &val)
Set the upper bound.
Function add_fun(const Function &f)
Add an already existing function.
Function transition(const std::string &fname) const
Construct a function describing transition at any events.
std::vector< std::string > all(const std::string &cat) const
Get a list of all variables of a particular category.
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.
std::vector< double > max(const std::vector< std::string > &name) const
Get the upper bound.
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.
std::vector< double > nominal(const std::vector< std::string > &name) const
Get the nominal value.
std::vector< MX > alg() const
[DEPRECATED] Replaced with outputs("alg")
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)
[DEPRECATED] Replaced with set_all("y", name)
std::vector< std::string > x() const
Differential states.
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< std::string > y() const
Outputs.
bool has_beq(const std::string &name) const
Does a variable have a binding equation?
std::string display_unit(const std::string &name) const
MX add(const std::string &name, const std::string &causality, const Dict &opts=Dict())
Add a new model variable, default variability.
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.
MX pre(const MX &v) const
Get the pre-expression given variable expression.
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::string pack_fmu(const Dict &files, const Dict &opts=Dict())
Pack files from export_fmu / compile_fmu into a single .fmu archive.
void add(const std::string &name, const std::string &causality, const std::string &variability, const MX &expr, const Dict &opts=Dict())
Add a new model variable, symbolic expression already available.
std::vector< std::string > z() const
Algebraic variables.
void set_attribute(const std::string &a, const std::vector< std::string > &name, const std::vector< double > &val)
Set an attribute.
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.
std::vector< MX > ode() const
[DEPRECATED] Replaced with outputs("ode")
std::vector< std::string > der() const
Model structure: All time derivatives.
bool has_t() const
Is there a time variable?
void set_start_time(double val)
Set the start time.
DaeBuilder()
Default constructor.
void set(const std::string &name, double val)
std::vector< std::string > t_new() const
Independent variable (usually time)
Definition: dae_builder.hpp:98
void eliminate(const std::string &cat)
Eliminate all dependent parameters.
std::vector< std::string > initial_unknowns() const
Model structure: initial unknowns.
std::vector< MX > init_lhs() const
Initial conditions, left-hand-side.
casadi_int value_reference(const std::string &name) const
std::vector< MX > ydef() const
[DEPRECATED] Replaced with outputs("y")
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.
std::vector< std::string > categories(const std::string &name) const
Which categories are possible for a variable?
std::vector< std::string > pre(const std::vector< std::string > &name) const
Get the pre-variables of model variables.
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.
MX var(const std::string &name) const
casadi_int ny() const
Output variables.
void set(const std::vector< std::string > &name, const std::vector< double > &val)
Set the current value.
Dict compile_fmu(const Dict &files, const Dict &opts=Dict())
Compile the sources produced by export_fmu.
std::vector< MX > zero() const
[DEPRECATED] Replaced with outputs("zero")
void set_all(const std::string &v, const std::vector< std::string > &name)
Set all variables within a a category.
std::vector< std::string > outputs() const
[DEPRECATED] Renamed "y"
void load_fmi_description(const std::string &filename)
Import problem description from FMI or XML.
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.
Dict export_fmu(const Dict &opts=Dict())
Export instance into an FMU.
std::vector< MX > ddef() const
Definitions of dependent parameters.
casadi_int nw() const
Dependent variables.
bool symbolic() const
Symbolic DAE instance?
std::vector< MX > init_rhs() const
Initial conditions, right-hand-side.
std::vector< MX > inputs(const std::string &cat) const
Input expressions for a specific category.
void set_step_size(double val)
Set the step size.
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)
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
[DEPRECATED] Replaced with outputs("quad")
MX beq(const std::string &name) const
Get the binding equation for a variable.
std::string type(const std::string &name, casadi_int fmi_version=3) const
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:92
GenericShared implements a reference counting framework similar for efficient and.
General sparsity class.
Definition: sparsity.hpp:106
The casadi namespace.
Definition: archiver.hpp:32
GenericType::Dict Dict
C++ equivalent of Python's dict or MATLAB's struct.