26 #ifndef CASADI_DAE_BUILDER_INTERNAL_HPP
27 #define CASADI_DAE_BUILDER_INTERNAL_HPP
29 #include <unordered_map>
31 #include "dae_builder.hpp"
32 #include "shared_object_internal.hpp"
33 #include "casadi_enum.hpp"
44 enum class Type {
FLOAT32,
FLOAT64,
INT8,
UINT8,
INT16,
UINT16,
INT32,
UINT32,
INT64,
UINT64,
69 friend class DaeBuilderInternal;
73 Variable(casadi_int index, casadi_int numel,
const std::string& name,
const MX& v);
166 class CASADI_EXPORT DaeBuilderInternal :
public SharedObjectInternal {
169 friend class FmuFunction;
174 explicit DaeBuilderInternal(
const std::string& name,
const std::string& path,
const Dict& opts);
177 ~DaeBuilderInternal()
override;
180 std::string class_name()
const override {
return "DaeBuilderInternal";}
183 void sanity_check()
const;
194 void lift(
bool lift_shared,
bool lift_calls);
197 void eliminate_quad();
206 void sort_z(
const std::vector<std::string>& z_order);
209 std::vector<size_t>& ind_in(
const std::string& v);
212 const std::vector<size_t>& ind_in(
const std::string& v)
const;
215 void clear_all(
const std::string& v);
218 void set_all(
const std::string& v,
const std::vector<std::string>& name);
221 void prune(
bool prune_p,
bool prune_u);
224 void tearing_variables(std::vector<std::string>* res, std::vector<std::string>* iv,
225 std::vector<std::string>* iv_on_hold)
const;
235 void load_fmi_description(
const std::string& filename);
238 static std::string iso_8601_time();
241 static std::string generate_guid();
244 std::vector<std::string> export_fmu(
const Dict& opts)
const;
247 std::string generate_wrapper(
const std::string& guid,
const CodeGenerator& gen)
const;
250 std::string generate_build_description(
const std::vector<std::string>& cfiles)
const;
253 std::string generate_model_description(
const std::string& guid)
const;
256 XmlNode generate_model_variables()
const;
259 XmlNode generate_model_structure()
const;
262 void update_dependencies()
const;
266 static std::string generate(
const std::vector<size_t>& v);
267 static std::string generate(
const std::vector<double>& v);
271 enum DaeBuilderInternalIn {
286 enum DaeBuilderInternalOut {
297 std::vector<MX> input(DaeBuilderInternalIn ind)
const;
300 std::vector<MX> output(DaeBuilderInternalOut ind)
const;
303 std::vector<MX> input(
const std::vector<DaeBuilderInternalIn>& ind)
const;
306 std::vector<MX> output(
const std::vector<DaeBuilderInternalOut>& ind)
const;
309 void add_lc(
const std::string& name,
const std::vector<std::string>& f_out);
312 Function create(
const std::string& fname,
313 const std::vector<std::string>& name_in,
314 const std::vector<std::string>& name_out,
315 const Dict& opts,
bool sx,
bool lifted_calls)
const;
318 Function fmu_fun(
const std::string& fname,
319 const std::vector<std::string>& name_in,
320 const std::vector<std::string>& name_out,
321 const Dict& opts)
const;
324 Function dependent_fun(
const std::string& fname,
325 const std::vector<std::string>& s_in,
326 const std::vector<std::string>& s_out)
const;
329 Function gather_eq()
const;
332 const MX& var(
const std::string& name)
const;
335 MX der(
const std::string& name)
const;
338 MX der(
const MX& var)
const;
341 std::string type_name()
const {
return "DaeBuilderInternal";}
344 void disp(std::ostream& stream,
bool more)
const override;
347 std::string get_str(
bool more=
false)
const {
348 std::stringstream ss;
354 Variable& new_variable(
const std::string& name, casadi_int numel = 1,
const MX& v = MX());
357 bool has_variable(
const std::string& name)
const;
360 std::vector<std::string> all_variables()
const;
363 size_t n_variables()
const {
return variables_.size();}
366 size_t n_mem()
const;
369 std::vector<double> start_all()
const;
373 Variable& variable(
size_t ind) {
return *variables_.at(ind);}
374 const Variable& variable(
size_t ind)
const {
return *variables_.at(ind);}
379 Variable& variable(
const std::string& name) {
return variable(find(name));}
380 const Variable& variable(
const std::string& name)
const {
return variable(find(name));}
384 const MX& var(
size_t ind)
const;
387 std::vector<MX> var(
const std::vector<size_t>& ind)
const;
390 size_t find(
const std::string& name)
const;
393 std::vector<size_t> find(
const std::vector<std::string>& name)
const;
396 const Function& oracle(
bool sx =
false,
bool elim_w =
false,
bool lifted_calls =
false)
const;
399 Sparsity jac_sparsity(
const std::vector<size_t>& oind,
const std::vector<size_t>& iind)
const;
402 Sparsity hess_sparsity(
const std::vector<size_t>& oind,
const std::vector<size_t>& iind)
const;
408 static std::string qualified_name(
const XmlNode& nn);
415 std::string fmi_version_;
416 std::string model_name_;
418 std::string description_;
420 std::string copyright_;
421 std::string license_;
422 std::string generation_tool_;
423 std::string generation_date_and_time_;
424 std::string variable_naming_convention_;
425 casadi_int number_of_event_indicators_;
428 std::string model_identifier_;
429 bool provides_directional_derivative_;
430 std::vector<std::string> source_files_;
442 std::vector<Variable*> variables_;
445 std::vector<size_t> outputs_, derivatives_, initial_unknowns_;
448 std::unordered_map<std::string, size_t> varind_;
451 std::vector<size_t> t_, p_, u_, x_, z_, q_, c_, d_, w_, y_;
455 std::vector<MX> aux_;
456 std::vector<MX> init_lhs_, init_rhs_;
457 std::vector<MX> when_cond_, when_lhs_, when_rhs_;
463 std::vector<MX> cdef()
const;
468 std::vector<MX> ddef()
const;
473 std::vector<MX> wdef()
const;
478 std::vector<MX> ydef()
const;
483 std::vector<MX> ode()
const;
488 std::vector<MX> alg()
const;
493 std::vector<MX> quad()
const;
497 MX add_t(
const std::string& name);
498 MX add_p(
const std::string& name);
499 MX add_u(
const std::string& name);
500 MX add_x(
const std::string& name);
501 MX add_z(
const std::string& name);
502 MX add_q(
const std::string& name);
503 MX add_c(
const std::string& name,
const MX& new_cdef);
504 MX add_d(
const std::string& name,
const MX& new_ddef);
505 MX add_w(
const std::string& name,
const MX& new_wdef);
506 MX add_y(
const std::string& name,
const MX& new_ydef);
507 void set_ode(
const std::string& name,
const MX& ode_rhs);
508 void set_alg(
const std::string& name,
const MX& alg_rhs);
517 std::vector<Function> fun_;
522 mutable Function oracle_[2][2][2];
525 mutable bool clear_cache_;
528 MX read_expr(
const XmlNode& node);
531 Variable& read_variable(
const XmlNode& node);
534 void import_model_exchange(
const XmlNode& n);
537 void import_model_variables(
const XmlNode& modvars);
540 void import_model_structure(
const XmlNode& n);
543 void clear_cache()
const;
546 Function add_fun(
const std::string& name,
547 const std::vector<std::string>& arg,
548 const std::vector<std::string>& res,
const Dict& opts=
Dict());
551 Function add_fun(
const Function& f);
554 bool has_fun(
const std::string& name)
const;
557 Function fun(
const std::string& name)
const;
564 double attribute(
Attribute a,
const std::string& name)
const;
565 std::vector<double> attribute(
Attribute a,
const std::vector<std::string>& name)
const;
570 void set_attribute(
Attribute a,
const std::string& name,
double val);
571 void set_attribute(
Attribute a,
const std::vector<std::string>& name,
572 const std::vector<double>& val);
577 std::string string_attribute(
Attribute a,
const std::string& name)
const;
578 std::vector<std::string> string_attribute(
Attribute a,
579 const std::vector<std::string>& name)
const;
584 void set_string_attribute(
Attribute a,
const std::string& name,
const std::string& val);
585 void set_string_attribute(
Attribute a,
const std::vector<std::string>& name,
586 const std::vector<std::string>& val);
593 Function f, adj1_f, J, H;
595 std::vector<size_t> v, vdef;
601 std::vector<MX> jac_res;
603 std::vector<MX> adj1_arg;
605 std::vector<MX> adj1_res;
607 std::vector<MX> hess_res;
615 const MX& jac(casadi_int oind, casadi_int iind)
const;
617 const MX& hess(casadi_int iind1, casadi_int iind2)
const;
621 MX jac_vdef_v_from_calls(std::map<MXNode*, CallIO>& call_nodes,
622 const std::vector<casadi_int>& h_offsets)
const;
625 MX hess_v_v_from_calls(std::map<MXNode*, CallIO>& call_nodes,
626 const std::vector<casadi_int>& h_offsets)
const;
629 static void sort_dependent(std::vector<MX>& v, std::vector<MX>& vdef);
634 template<>
struct enum_traits<DaeBuilderInternal::DaeBuilderInternalIn> {
635 static const size_t n_enum = DaeBuilderInternal::DAE_BUILDER_NUM_IN;
637 template<>
struct enum_traits<DaeBuilderInternal::DaeBuilderInternalOut> {
638 static const size_t n_enum = DaeBuilderInternal::DAE_BUILDER_NUM_OUT;
650 CASADI_EXPORT std::string to_string(
TypeFmi2 v);
651 CASADI_EXPORT std::string to_string(
Type v);
652 CASADI_EXPORT std::string to_string(
Causality v);
653 CASADI_EXPORT std::string to_string(
Variability v);
654 CASADI_EXPORT std::string to_string(
Initial v);
655 CASADI_EXPORT std::string to_string(
Attribute v);
657 CASADI_EXPORT std::string to_string(DaeBuilderInternal::DaeBuilderInternalIn v);
658 CASADI_EXPORT std::string to_string(DaeBuilderInternal::DaeBuilderInternalOut v);
A symbolic representation of a differential-algebraic equations model.
std::map< std::string, std::vector< std::string > > AuxOut
Variability
Variability: FMI 2.0 specification, section 2.2.7 or FMI 3.0 specification, section 2....
GenericType::Dict Dict
C++ equivalent of Python's dict or MATLAB's struct.
Initial
Initial: FMI 2.0 specification, section 2.2.7 or FMI 3.0 specification, section 2....
Causality
Causality: FMI 2.0 specification, section 2.2.7 or FMI 3.0 specification, section 2....
Type
Variable type (FMI 3)
TypeFmi2
Variable type (FMI 2)
Holds expressions and meta-data corresponding to a physical quantity evolving in time.
bool dependency
Do other expressions depend on this variable.
void set_attribute(Attribute a, double val)
Set by attribute name.
std::vector< double > value
Numerical value (also for booleans, integers, enums)
void set_string_attribute(Attribute a, const std::string &val)
Set by attribute name (string-valued)
static Initial default_initial(Causality causality, Variability variability)
std::vector< double > start
casadi_int index
Location in variable vector.
std::string stringvalue
String value (if string-valued)
casadi_int numel
Number of elements - product of all dimensions.
unsigned int value_reference
XmlNode export_xml(const DaeBuilderInternal &self) const
std::vector< casadi_int > dependencies
Dependencies.
std::vector< DependenciesKind > dependenciesKind
Dependencies.
double attribute(Attribute a) const
Get by attribute name.
std::vector< casadi_int > dimension
Dimensions.
std::string string_attribute(Attribute a) const
Get by attribute name (string-valued)