List of all members | Classes | Public Types | Public Member Functions | Protected Member Functions | Static Protected Member Functions | Protected Attributes | Friends
casadi::DaeBuilderInternal Class Reference

#include <dae_builder_internal.hpp>

Detailed Description

Internal class for DaeBuilder, see comments on the public class.

Definition at line 209 of file dae_builder_internal.hpp.

Inheritance diagram for casadi::DaeBuilderInternal:
Inheritance graph
[legend]
Collaboration diagram for casadi::DaeBuilderInternal:
Collaboration graph
[legend]

Classes

struct  CallIO
 Helper class, represents inputs and outputs for a function call node. More...
 

Public Types

using weak_ref_type = WeakRefInternal
 

Public Member Functions

 DaeBuilderInternal (const std::string &name, const std::string &path, const Dict &opts)
 Constructor. More...
 
 ~DaeBuilderInternal () override
 Destructor. More...
 
std::string class_name () const override
 Readable name of the internal class. More...
 
void sanity_check () const
 Check if dimensions match. More...
 
std::vector< MXinput (Category ind) const
 
std::vector< MXoutput (OutputCategory ind) const
 
std::vector< MXinput (const std::vector< Category > &ind) const
 
std::vector< MXoutput (const std::vector< OutputCategory > &ind) const
 
void add_lc (const std::string &name, const std::vector< std::string > &f_out)
 Add a named linear combination of output expressions. More...
 
Function create (const std::string &fname, const std::vector< std::string > &name_in, const std::vector< std::string > &name_out, const Dict &opts, bool sx, bool lifted_calls) const
 Construct a function object. More...
 
Function fmu_fun (const std::string &fname, const std::vector< std::string > &name_in, const std::vector< std::string > &name_out, const Dict &opts) const
 Construct function from an FMU DLL. More...
 
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. More...
 
Function transition (const std::string &fname, casadi_int index, bool dummy_index_input=false) const
 Construct a function describing transition at a specific event. More...
 
Function transition (const std::string &fname) const
 Construct a function describing transition at all events. More...
 
Function gather_eq () const
 Function corresponding to all equations. More...
 
const MXvar (const std::string &name) const
 Get variable expression by name. More...
 
MX get_der (size_t ind) const
 Get a derivative expression by variable index (const, never create) More...
 
MX get_der (size_t ind, bool may_allocate=true)
 Get a derivative expression by variable index (non-const, may create) More...
 
MX der (const MX &var) const
 Get a derivative expression by non-differentiated expression (const, never create) More...
 
MX der (const MX &var, bool may_allocate=true)
 Get a derivative expression by non-differentiated expression (non-const, may create) More...
 
std::string unique_name (const std::string &prefix, bool allow_no_prefix=true) const
 Find a unique name, with a specific prefix. More...
 
std::string type_name () const
 Readable name of the class. More...
 
void disp (std::ostream &stream, bool more) const override
 Print description. More...
 
std::string get_str (bool more=false) const
 Get string representation. More...
 
Variablenew_variable (const std::string &name, const std::vector< casadi_int > &dimension={1}, const MX &expr=MX())
 Create a new variable. More...
 
bool has (const std::string &name) const
 Check if a particular variable exists. More...
 
std::vector< std::string > all () const
 Get a list of all variables. More...
 
std::vector< std::string > all (Category cat) const
 Get a list of all variables of a particular category. More...
 
size_t n_variables () const
 Length of variables array. More...
 
size_t n_mem () const
 Length of memory for all variables. More...
 
std::vector< double > start_all () const
 Start values for all variables. More...
 
const MXvar (size_t ind) const
 Get variable expression by index. More...
 
const MXvar (Category cat, size_t ind) const
 Get variable expression by category and index. More...
 
std::vector< MXvar (const std::vector< size_t > &ind) const
 Get variable expressions by index. More...
 
std::vector< MXvar (Category cat) const
 Get variable expressions by category. More...
 
size_t find (const std::string &name) const
 Get index of variable, given name. More...
 
size_t find (const MX &v) const
 Get index of variable, given expression. More...
 
std::vector< size_t > find (const std::vector< std::string > &name) const
 Get indices of variable, given multiple names. More...
 
std::vector< size_t > find (const std::vector< MX > &v) const
 Get indices of variable, given multiple expressions. More...
 
const std::string & name (size_t ind) const
 Get variable name by index. More...
 
std::vector< std::string > name (const std::vector< size_t > &ind) const
 Get variable names by indices. More...
 
const Functionoracle (bool sx=false, bool elim_w=false, bool lifted_calls=false) const
 Get the (cached) oracle, SX or MX. More...
 
Sparsity jac_sparsity (const std::vector< size_t > &oind, const std::vector< size_t > &iind) const
 Get Jacobian sparsity. More...
 
Sparsity hess_sparsity (const std::vector< size_t > &oind, const std::vector< size_t > &iind) const
 Get what is known of the Hessian sparsity. More...
 
casadi_int getCount () const
 Get the reference count. More...
 
std::string debug_repr (const SharedObjectInternal *) const
 
GenericWeakRef< SharedObject, SharedObjectInternal > * weak ()
 Get a weak reference to the object. More...
 
Manipulation

Reformulate the dynamic optimization problem.

void eliminate (Category cat)
 Eliminate all variables of a category. More...
 
void sort (Category cat)
 Sort all variables of a category. More...
 
void lift (bool lift_shared, bool lift_calls)
 Lift problem formulation by extracting shared subexpressions. More...
 
void sort_z (const std::vector< std::string > &z_order)
 Sort algebraic variables. More...
 
std::vector< size_t > & indices (Category cat)
 Classified variable indices (mutable) More...
 
const std::vector< size_t > & indices (Category cat) const
 Classified variable indices (immutable) More...
 
size_t size (Category cat) const
 Number of indices with a particular category. More...
 
void reorder (Category cat, const std::vector< size_t > &v)
 Reorder variables in a category. More...
 
void reorder (const std::string &n, std::vector< size_t > &ind, const std::vector< size_t > &v) const
 Reorder any index vector. More...
 
void prune (bool prune_p, bool prune_u)
 Prune unused controls. More...
 
void tearing_variables (std::vector< std::string > *res, std::vector< std::string > *iv, std::vector< std::string > *iv_on_hold) const
 Identify free variables and residual equations. More...
 
void tear ()
 Identify free variables and residual equations. More...
 
Variablevariable (size_t ind)
 
const Variablevariable (size_t ind) const
 
Variablevariable (Category cat, size_t ind)
 
const Variablevariable (Category cat, size_t ind) const
 
Variablevariable (const std::string &name)
 
const Variablevariable (const std::string &name) const
 
Variablevariable (const MX &v)
 
const Variablevariable (const MX &v) const
 

Protected Member Functions

bool has_t () const
 Is there a time variable? More...
 
const MXtime () const
 
std::vector< MXcdef () const
 Definitions of dependent constants. More...
 
std::vector< MXquad () const
 Quadrature right hand sides. More...
 
std::vector< MXinit_lhs () const
 Initial conditions, left-hand-side. More...
 
std::vector< MXinit_rhs () const
 Initial conditions, right-hand-side. More...
 
Variableadd (const std::string &name, Causality causality, Variability variability, const Dict &opts)
 Add a new variable. More...
 
Variableadd (const std::string &name, Causality causality, Variability variability, const MX &expr, const Dict &opts)
 Add a new variable, expression provided. More...
 
Variableadd (const std::string &name, Causality causality, const Dict &opts)
 Add a new variable, default variability. More...
 
Variableadd (const std::string &name, const Dict &opts)
 Add a new variable, default variability and causality. More...
 
void categorize (size_t ind, Category cat)
 Set or change the category for a variable. More...
 
void insert (std::vector< size_t > &v, size_t ind) const
 Insert into list of variables, keeping it ordered. More...
 
void remove (std::vector< size_t > &v, size_t ind) const
 Remove from list of variables. More...
 
Causality causality (size_t ind) const
 Get causality. More...
 
void set_causality (size_t ind, Causality causality)
 Set causality. More...
 
Variability variability (size_t ind) const
 Get variability. More...
 
void set_variability (size_t ind, Variability variability)
 Set variability. More...
 
Category category (size_t ind) const
 Get category. More...
 
void set_category (size_t ind, Category cat)
 Set category. More...
 
void eq (const MX &lhs, const MX &rhs, const Dict &opts)
 Add a simple equation. More...
 
void when (const MX &cond, const std::vector< std::string > &eqs, const Dict &opts)
 Add when equations. More...
 
Variableassign (const std::string &name, const MX &val)
 Assignment inside when-equations or if-else equations. More...
 
Variablereinit (const std::string &name, const MX &val)
 Reinitialize a state inside when-equations. More...
 
void set_init (const std::string &name, const MX &init_rhs)
 Set a initial equation. More...
 
MX read_expr (const XmlNode &node)
 Read an equation. More...
 
MX read_identifier (const XmlNode &node)
 Read an identifier expression. More...
 
Variableread_variable (const XmlNode &node, Attribute *att=0)
 Read a variable. More...
 
void import_default_experiment (const XmlNode &n)
 
std::vector< casadi_int > read_dependencies (const XmlNode &n)
 
std::vector< DependenciesKindread_dependencies_kind (const XmlNode &n, size_t ndep)
 
void import_model_exchange (const XmlNode &n)
 
void import_model_variables (const XmlNode &modvars)
 
void import_model_structure (const XmlNode &n)
 
void import_binding_equations (const XmlNode &eqs)
 
void import_dynamic_equations (const XmlNode &eqs)
 
void import_initial_equations (const XmlNode &eqs)
 
void clear_cache () const
 Problem structure has changed: Clear cache. More...
 
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. More...
 
Function add_fun (const Function &f)
 Add an already existing function. More...
 
bool has_fun (const std::string &name) const
 Does a particular function already exist? More...
 
Function fun (const std::string &name) const
 Get function by name. More...
 
void reset ()
 
casadi_int size (Attribute a, const std::vector< std::string > &name) const
 Total number of elements for a particular attribute. More...
 
MX jac_vdef_v_from_calls (std::map< MXNode *, CallIO > &call_nodes, const std::vector< casadi_int > &h_offsets) const
 Calculate contribution to jac_vdef_v from lifted calls. More...
 
MX hess_v_v_from_calls (std::map< MXNode *, CallIO > &call_nodes, const std::vector< casadi_int > &h_offsets) const
 Calculate contribution to hess_?_v_v from lifted calls. More...
 
void initSingleton ()
 
void destroySingleton ()
 
shared_from_this ()
 Get a shared object from the current internal object. More...
 
const B shared_from_this () const
 Get a shared object from the current internal object. More...
 
double attribute (Attribute a, const std::string &name) const
 
std::vector< double > attribute (Attribute a, const std::vector< std::string > &name) const
 
void set_attribute (Attribute a, const std::string &name, double val)
 
void set_attribute (Attribute a, const std::vector< std::string > &name, const std::vector< double > &val)
 
std::string string_attribute (Attribute a, const std::string &name) const
 
std::vector< std::string > string_attribute (Attribute a, const std::vector< std::string > &name) const
 
void set_string_attribute (Attribute a, const std::string &name, const std::string &val)
 
void set_string_attribute (Attribute a, const std::vector< std::string > &name, const std::vector< std::string > &val)
 

Static Protected Member Functions

static std::string qualified_name (const XmlNode &nn, Attribute *att=0)
 Get the qualified name. More...
 
static Variability default_variability (Causality causality, Type type)
 Default variability attribute, per the FMI specification. More...
 
static Initial default_initial (Causality causality, Variability variability)
 
static void sort_dependent (std::vector< MX > &v, std::vector< MX > &vdef)
 

Protected Attributes

bool debug_
 
double fmutol_
 
bool ignore_time_
 
std::string fmi_version_
 
std::string model_name_
 
std::string instantiation_token_
 
std::string description_
 
std::string author_
 
std::string copyright_
 
std::string license_
 
std::string generation_tool_
 
std::string generation_date_and_time_
 
std::string variable_naming_convention_
 
casadi_int number_of_event_indicators_
 
double start_time_
 
double stop_time_
 
double tolerance_
 
double step_size_
 
std::string model_identifier_
 
bool provides_directional_derivatives_
 
bool provides_adjoint_derivatives_
 
bool can_be_instantiated_only_once_per_process_
 
std::vector< std::string > source_files_
 
std::string name_
 Name of instance. More...
 
Resource resource_
 
bool symbolic_
 
bool detect_quad_
 
casadi_int fmi_major_
 
std::vector< Variable * > variables_
 All variables. More...
 
std::vector< size_t > outputs_
 
std::vector< size_t > derivatives_
 
std::vector< size_t > initial_unknowns_
 
std::vector< size_t > event_indicators_
 
std::vector< size_t > residuals_
 
std::unordered_map< std::string, size_t > varind_
 Find of variable by name. More...
 
std::unordered_map< unsigned int, size_t > vrmap_
 Find of variable by value reference. More...
 
std::vector< std::vector< size_t > > indices_
 Ordered variables. More...
 
std::vector< size_t > init_
 
std::vector< std::pair< size_t, std::vector< size_t > > > when_
 
Function::AuxOut lc_
 Linear combinations of output expressions. More...
 
std::vector< Functionfun_
 Functions. More...
 
Function oracle_ [2][2][2]
 Function oracles (cached) More...
 
bool clear_cache_
 Should the cache be cleared? More...
 

Friends

class DaeBuilder
 
class FmuInternal
 
class FmuFunction
 
class Variable
 

Import and export

void load_fmi_description (const std::string &filename)
 
std::vector< std::string > export_fmu (const Dict &opts) const
 Export instance into an FMU (experimental) More...
 
std::string generate_wrapper (const std::string &guid, const CodeGenerator &gen) const
 Generate FMU wrapper file (fmi3Functions.c) More...
 
std::string generate_build_description (const std::vector< std::string > &cfiles) const
 Generate buildDescription.xml. More...
 
std::string generate_model_description (const std::string &guid) const
 Generate modelDescription.xml. More...
 
XmlNode generate_model_variables () const
 Generate FMU ModelVariables. More...
 
XmlNode generate_model_structure () const
 Generate FMU ModelStructure. More...
 
void update_dependencies () const
 Update model variable dependencies. More...
 
static std::string iso_8601_time ()
 Get current date and time in the ISO 8601 format. More...
 
static std::string generate_guid ()
 
static std::string generate (const std::vector< size_t > &v)
 
static std::string generate (const std::vector< double > &v)
 

Member Typedef Documentation

◆ weak_ref_type

Definition at line 152 of file shared_object.hpp.

Constructor & Destructor Documentation

◆ DaeBuilderInternal()

casadi::DaeBuilderInternal::DaeBuilderInternal ( const std::string &  name,
const std::string &  path,
const Dict opts 
)
explicit

Definition at line 565 of file dae_builder_internal.cpp.

566  : name_(name), resource_(path) {
567  clear_cache_ = false;
572  symbolic_ = true;
573  detect_quad_ = false;
574  start_time_ = nan;
575  stop_time_ = nan;
576  tolerance_ = nan;
577  step_size_ = nan;
578  // Default options
579  debug_ = false;
580  fmutol_ = 0;
581  ignore_time_ = false;
582  std::string resource_serialize = "link";
583  // Read options
584  for (auto&& op : opts) {
585  if (op.first=="debug") {
586  debug_ = op.second;
587  } else if (op.first=="fmutol") {
588  fmutol_ = op.second;
589  } else if (op.first=="ignore_time") {
590  ignore_time_ = op.second;
591  } else if (op.first=="detect_quad") {
592  detect_quad_ = op.second;
593  } else if (op.first=="resource_serialize_mode") {
594  resource_.change_option("serialize_mode", op.second);
595  } else {
596  casadi_error("No such option: " + op.first);
597  }
598  }
600 }
bool clear_cache_
Should the cache be cleared?
std::vector< std::vector< size_t > > indices_
Ordered variables.
const std::string & name(size_t ind) const
Get variable name by index.
std::string name_
Name of instance.
void change_option(const std::string &option_name, const GenericType &option_value)
Change option after object creation for debugging.
Definition: resource.cpp:74
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)
static const size_t n_enum
Definition: casadi_enum.hpp:42

References can_be_instantiated_only_once_per_process_, casadi::Resource::change_option(), clear_cache_, debug_, detect_quad_, fmutol_, ignore_time_, indices_, casadi::nan, number_of_event_indicators_, provides_adjoint_derivatives_, provides_directional_derivatives_, resource_, start_time_, step_size_, stop_time_, symbolic_, and tolerance_.

◆ ~DaeBuilderInternal()

casadi::DaeBuilderInternal::~DaeBuilderInternal ( )
override

Definition at line 559 of file dae_builder_internal.cpp.

559  {
560  for (Variable* v : variables_) {
561  if (v) delete v;
562  }
563 }
std::vector< Variable * > variables_
All variables.

References variables_.

Member Function Documentation

◆ add() [1/4]

Variable & casadi::DaeBuilderInternal::add ( const std::string &  name,
Causality  causality,
const Dict opts 
)
protected

Definition at line 2866 of file dae_builder_internal.cpp.

2866  {
2867  // Get type
2868  Type type = Type::FLOAT64;
2869  if (opts.find("type") != opts.end()) {
2870  type = to_enum<Type>(opts.at("type").to_string());
2871  }
2872 
2873  // Default variability per FMI 3.0.2, section 2.4.7.4
2874  return add(name, causality, default_variability(causality, type), opts);
2875 }
Causality causality(size_t ind) const
Get causality.
static Variability default_variability(Causality causality, Type type)
Default variability attribute, per the FMI specification.
Variable & add(const std::string &name, Causality causality, Variability variability, const Dict &opts)
Add a new variable.
Type
Variable type (FMI 3)

References add(), causality(), default_variability(), casadi::FLOAT64, and name().

◆ add() [2/4]

Variable & casadi::DaeBuilderInternal::add ( const std::string &  name,
Causality  causality,
Variability  variability,
const Dict opts 
)
protected

Definition at line 2735 of file dae_builder_internal.cpp.

2736  {
2737  // No expression provided
2738  return add(name, causality, variability, MX(), opts);
2739 }
Variability variability(size_t ind) const
Get variability.

References causality(), name(), and variability().

Referenced by add(), assign(), eq(), import_model_variables(), reinit(), and when().

◆ add() [3/4]

Variable & casadi::DaeBuilderInternal::add ( const std::string &  name,
Causality  causality,
Variability  variability,
const MX expr,
const Dict opts 
)
protected

Definition at line 2741 of file dae_builder_internal.cpp.

2742  {
2743  // Default options
2744  std::string description, type, initial, unit, display_unit;
2745  std::vector<casadi_int> dimension = {1};
2746  double min = -casadi::inf, max = casadi::inf, nominal = 1;
2747  std::vector<double> start;
2748  // Read options
2749  for (auto&& op : opts) {
2750  if (op.first=="dimension") {
2751  dimension = op.second.to_int_vector();
2752  } else if (op.first=="description") {
2753  description = op.second.to_string();
2754  } else if (op.first=="unit") {
2755  unit = op.second.to_string();
2756  } else if (op.first=="display_unit") {
2757  display_unit = op.second.to_string();
2758  } else if (op.first=="min") {
2759  min = op.second.to_double();
2760  } else if (op.first=="max") {
2761  max = op.second.to_double();
2762  } else if (op.first=="nominal") {
2763  nominal = op.second.to_double();
2764  } else if (op.first=="start") {
2765  if (op.second.can_cast_to(OT_DOUBLE)) {
2766  start.resize(1, op.second.to_double());
2767  } else {
2768  start = op.second.to_double_vector();
2769  }
2770  } else if (op.first=="type") {
2771  type = op.second.to_string();
2772  } else if (op.first=="initial") {
2773  initial = op.second.to_string();
2774  } else {
2775  casadi_error("No such option: " + op.first);
2776  }
2777  }
2778  // Create a new variable
2779  Variable& v = new_variable(name, dimension, expr);
2780  v.description = description;
2781  if (!type.empty()) v.type = to_enum<Type>(type);
2782  v.causality = causality;
2783  v.variability = variability;
2784  if (!start.empty()) v.start = start;
2785  if (!initial.empty()) v.initial = to_enum<Initial>(initial);
2786  if (!unit.empty()) v.unit = unit;
2787  if (!display_unit.empty()) v.display_unit = display_unit;
2788  if (min != -casadi::inf) v.min = min;
2789  if (max != casadi::inf) v.max = max;
2790  v.nominal = nominal;
2791  // Handle different categories
2792  switch (causality) {
2793  case Causality::PARAMETER:
2794  // Parameter
2796  categorize(v.index, Category::P);
2797  } else if (variability == Variability::FIXED) {
2798  categorize(v.index, Category::C);
2799  } else {
2800  casadi_error("'parameter' causality requires 'fixed' or 'tunable' variability");
2801  }
2802  break;
2805  "'calculatedParameter' causality requires 'fixed' or 'tunable' variability");
2806  categorize(v.index, Category::D);
2807  break;
2808  case Causality::INPUT:
2809  // Control
2810  casadi_assert(variability == Variability::CONTINUOUS
2812  "'input' causality requires 'continuous' or 'discrete' variability");
2813  categorize(v.index, Category::U);
2814  break;
2815  case Causality::OUTPUT:
2816  // Type determined by providing equation, unless discrete variability
2818  // Constant output
2819  categorize(v.index, Category::C);
2820  } else if (variability == Variability::DISCRETE) {
2821  // Discrete variables are considered states with zero derivatives
2823  } else if (variability == Variability::CONTINUOUS) {
2824  // Continuous variables are considered algebraic variables until given a defining equation
2825  categorize(v.index, Category::Z);
2826  } else {
2827  casadi_error("'output' causality requires 'constant', 'continuous' or "
2828  "'discrete' variability");
2829  }
2830  break;
2831  case Causality::LOCAL:
2832  // Type determined by providing equation, unless discrete variability
2834  // Constant local
2835  categorize(v.index, Category::C);
2836  } else if (variability == Variability::DISCRETE) {
2837  // Discrete variables are considered states with zero derivatives
2839  } else if (variability == Variability::CONTINUOUS) {
2840  // Continuous variables are considered algebraic variables until given a defining equation
2841  categorize(v.index, Category::Z);
2843  // Fixed or tunable local
2844  categorize(v.index, Category::D);
2845  } else {
2846  casadi_error("'output' causality requires 'constant', 'fixed', 'tunable', 'discrete' or "
2847  "'continuous' variability");
2848  }
2849  break;
2851  // Independent variable
2852  casadi_assert(!has_t(), "'t' already defined");
2853  casadi_assert(variability == Variability::CONTINUOUS,
2854  "Independent variable must be continuous");
2855  categorize(v.index, Category::T);
2856  break;
2857  default:
2858  casadi_error("Unknown causality: " + to_string(causality));
2859  }
2860  // If an output, add to list of outputs
2861  if (causality == Causality::OUTPUT) outputs_.push_back(v.index);
2862  // Return variable reference
2863  return v;
2864 }
bool has_t() const
Is there a time variable?
Variable & new_variable(const std::string &name, const std::vector< casadi_int > &dimension={1}, const MX &expr=MX())
Create a new variable.
void categorize(size_t ind, Category cat)
Set or change the category for a variable.
std::string description(Category v)
const double inf
infinity
Definition: calculus.hpp:50
std::string to_string(TypeFmi2 v)

References casadi::C, casadi::CALCULATED_PARAMETER, categorize(), casadi::Variable::causality, causality(), casadi::CONSTANT, casadi::CONTINUOUS, casadi::D, casadi::description(), casadi::Variable::description, detect_quad_, casadi::DISCRETE, casadi::Variable::display_unit, casadi::FIXED, has_t(), casadi::INDEPENDENT, casadi::Variable::index, casadi::inf, casadi::Variable::initial, casadi::INPUT, casadi::LOCAL, casadi::Variable::max, casadi::Variable::min, name(), new_variable(), casadi::Variable::nominal, casadi::OT_DOUBLE, casadi::OUTPUT, outputs_, casadi::P, casadi::PARAMETER, casadi::Q, casadi::Variable::start, casadi::T, casadi::to_string(), casadi::TUNABLE, casadi::Variable::type, casadi::U, casadi::Variable::unit, casadi::Variable::variability, variability(), casadi::X, and casadi::Z.

◆ add() [4/4]

Variable& casadi::DaeBuilderInternal::add ( const std::string &  name,
const Dict opts 
)
inlineprotected

Definition at line 595 of file dae_builder_internal.hpp.

595  {
596  // Per FMI 3.0.2 specification, section 2.4.7.4: Default causality is LOCAL
597  return add(name, Causality::LOCAL, opts);
598  }

References casadi::LOCAL.

◆ add_fun() [1/2]

Function casadi::DaeBuilderInternal::add_fun ( const Function f)
protected

Definition at line 3950 of file dae_builder_internal.cpp.

3950  {
3951  casadi_assert(!has_fun(f.name()), "Function '" + f.name() + "' already exists");
3952  fun_.push_back(f);
3953  return f;
3954 }
bool has_fun(const std::string &name) const
Does a particular function already exist?
std::vector< Function > fun_
Functions.

References fun_, has_fun(), and casadi::Function::name().

◆ add_fun() [2/2]

Function casadi::DaeBuilderInternal::add_fun ( const std::string &  name,
const std::vector< std::string > &  arg,
const std::vector< std::string > &  res,
const Dict opts = Dict() 
)
protected

Definition at line 3956 of file dae_builder_internal.cpp.

3958  {
3959  casadi_assert(!has_fun(name), "Function '" + name + "' already exists");
3960 
3961  // Dependent variable definitions
3962  std::vector<MX> wdef = output(OutputCategory::WDEF);
3963  // Get inputs
3964  std::vector<MX> arg_ex, res_ex;
3965  for (auto&& s : arg) arg_ex.push_back(var(s));
3966  for (auto&& s : res) {
3967  // Find the binding expression FIXME(@jaeandersson)
3968  casadi_int v_ind;
3969  for (v_ind = 0; v_ind < size(Category::W); ++v_ind) {
3970  if (s == variable(indices(Category::W).at(v_ind)).name) {
3971  res_ex.push_back(wdef.at(v_ind));
3972  break;
3973  }
3974  }
3975  casadi_assert(v_ind < size(Category::W), "Cannot find dependent '" + s + "'");
3976  }
3977  Function ret(name, arg_ex, res_ex, arg, res, opts);
3978  return add_fun(ret);
3979 }
std::vector< size_t > & indices(Category cat)
Classified variable indices (mutable)
std::vector< MX > output(OutputCategory ind) const
Variable & variable(size_t ind)
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.
size_t size(Category cat) const
Number of indices with a particular category.
const MX & var(const std::string &name) const
Get variable expression by name.
std::string name
Name of the variable.

References has_fun(), indices(), casadi::Variable::name, name(), output(), size(), var(), variable(), casadi::W, and casadi::WDEF.

◆ add_lc()

void casadi::DaeBuilderInternal::add_lc ( const std::string &  name,
const std::vector< std::string > &  f_out 
)

Definition at line 1887 of file dae_builder_internal.cpp.

1887  {
1888  // Make sure object valid
1889  sanity_check();
1890 
1891  // Make sure name is valid
1892  casadi_assert(!name.empty(), "DaeBuilderInternal::add_lc: \"name\" is empty");
1893  for (std::string::const_iterator i=name.begin(); i!=name.end(); ++i) {
1894  casadi_assert(isalnum(*i),
1895  "DaeBuilderInternal::add_lc: \"name\" must be alphanumeric");
1896  }
1897 
1898  // Consistency checks
1899  casadi_assert(!f_out.empty(), "DaeBuilderInternal::add_lc: Linear combination is empty");
1900  std::vector<bool> in_use(enum_traits<OutputCategory>::n_enum, false);
1901  for (casadi_int i=0; i < f_out.size(); ++i) {
1902  auto oind = static_cast<size_t>(to_enum<OutputCategory>(f_out[i]));
1903  casadi_assert(!in_use[oind], "DaeBuilderInternal::add_lc: Duplicate expression " + f_out[i]);
1904  in_use[oind] = true;
1905  }
1906 
1907  std::vector<std::string>& ret1 = lc_[name];
1908  if (!ret1.empty()) casadi_warning("DaeBuilderInternal::add_lc: Overwriting " << name);
1909  ret1 = f_out;
1910 }
void sanity_check() const
Check if dimensions match.
Function::AuxOut lc_
Linear combinations of output expressions.

References lc_, name(), and sanity_check().

◆ all() [1/2]

std::vector< std::string > casadi::DaeBuilderInternal::all ( ) const

Definition at line 1593 of file dae_builder_internal.cpp.

1593  {
1594  std::vector<std::string> r;
1595  r.reserve(n_variables());
1596  for (const Variable* v : variables_) r.push_back(v->name);
1597  return r;
1598 }
size_t n_variables() const
Length of variables array.

References n_variables(), and variables_.

◆ all() [2/2]

std::vector< std::string > casadi::DaeBuilderInternal::all ( Category  cat) const

Definition at line 1600 of file dae_builder_internal.cpp.

1600  {
1601  return name(indices(cat));
1602 }

References indices(), and name().

◆ assign()

Variable & casadi::DaeBuilderInternal::assign ( const std::string &  name,
const MX val 
)
protected

Definition at line 3214 of file dae_builder_internal.cpp.

3214  {
3215  // Create a unique name for the reinit variable
3216  std::string assign_name = unique_name("__assign__" + name + "__");
3217  // Add a new dependent variable defined by val
3219  val, Dict());
3220  // Classify as assign variable
3221  categorize(v.index, Category::CALCULATED);
3222  v.parent = variable(name).index;
3223  // Return the variable name
3224  return v;
3225 }
std::string unique_name(const std::string &prefix, bool allow_no_prefix=true) const
Find a unique name, with a specific prefix.
GenericType::Dict Dict
C++ equivalent of Python's dict or MATLAB's struct.
casadi_int index
Location in variable vector.

References add(), casadi::CALCULATED, categorize(), casadi::CONTINUOUS, casadi::Variable::index, casadi::LOCAL, name(), casadi::Variable::parent, unique_name(), and variable().

Referenced by import_binding_equations(), import_dynamic_equations(), lift(), and read_expr().

◆ attribute() [1/2]

double casadi::DaeBuilderInternal::attribute ( Attribute  a,
const std::string &  name 
) const
protected

Get by attribute name

Definition at line 4003 of file dae_builder_internal.cpp.

4003  {
4004  double ret;
4005  variable(name).get_attribute(a, &ret);
4006  return ret;
4007 }
void get_attribute(Attribute a, double *val) const

References casadi::Variable::get_attribute(), name(), and variable().

◆ attribute() [2/2]

std::vector< double > casadi::DaeBuilderInternal::attribute ( Attribute  a,
const std::vector< std::string > &  name 
) const
protected

Get by attribute name

Definition at line 4009 of file dae_builder_internal.cpp.

4010  {
4011  // Allocate return
4012  std::vector<double> r;
4013  r.reserve(size(a, name));
4014  // Get contribution from each variable
4015  std::vector<double> r1;
4016  for (auto& n : name) {
4017  variable(n).get_attribute(a, &r1);
4018  r.insert(r.end(), r1.begin(), r1.end());
4019  }
4020  return r;
4021 }

References casadi::Variable::get_attribute(), name(), size(), and variable().

◆ categorize()

void casadi::DaeBuilderInternal::categorize ( size_t  ind,
Category  cat 
)
protected

Definition at line 2877 of file dae_builder_internal.cpp.

2877  {
2878  // Get variable reference
2879  Variable& v = variable(ind);
2880  // If same category, quick return
2881  if (v.category == cat) return;
2882  // Remove from current category, if any
2883  if (v.category != Category::NUMEL) {
2884  remove(indices(v.category), ind);
2885  v.category = Category::NUMEL;
2886  }
2887  // Add to new category, if any
2888  if (cat != Category::NUMEL) {
2889  std::vector<size_t>& indices = this->indices(cat);
2890  if (is_acyclic(cat)) {
2891  indices.push_back(ind);
2892  } else {
2893  insert(indices, ind);
2894  }
2895  v.category = cat;
2896  }
2897 }
void insert(std::vector< size_t > &v, size_t ind) const
Insert into list of variables, keeping it ordered.
void remove(std::vector< size_t > &v, size_t ind) const
Remove from list of variables.
bool is_acyclic(Category cat)

References casadi::Variable::category, indices(), insert(), casadi::is_acyclic(), casadi::NUMEL, remove(), and variable().

Referenced by add(), assign(), eliminate(), import_dynamic_equations(), import_model_structure(), import_model_variables(), reinit(), set_category(), set_variability(), and when().

◆ category()

Category casadi::DaeBuilderInternal::category ( size_t  ind) const
protected

Definition at line 3014 of file dae_builder_internal.cpp.

3014  {
3015  return variable(ind).category;
3016 }
Category category
CasADi's classification of the variable.

References casadi::Variable::category, and variable().

◆ causality()

Causality casadi::DaeBuilderInternal::causality ( size_t  ind) const
protected

Definition at line 2921 of file dae_builder_internal.cpp.

2921  {
2922  return variable(ind).causality;
2923 }

References casadi::Variable::causality, and variable().

Referenced by add(), default_initial(), default_variability(), import_model_variables(), and set_causality().

◆ cdef()

std::vector< MX > casadi::DaeBuilderInternal::cdef ( ) const
protected

Extra doc: https://github.com/casadi/casadi/wiki/L_2c7

Definition at line 2667 of file dae_builder_internal.cpp.

2667  {
2668  std::vector<MX> ret;
2669  ret.reserve(size(Category::C));
2670  for (size_t c : indices(Category::C)) ret.push_back(variable(variable(c).bind).v);
2671  return ret;
2672 }

References casadi::C, indices(), size(), and variable().

◆ class_name()

std::string casadi::DaeBuilderInternal::class_name ( ) const
inlineoverridevirtual

Implements casadi::SharedObjectInternal.

Definition at line 224 of file dae_builder_internal.hpp.

224 {return "DaeBuilderInternal";}

◆ clear_cache()

void casadi::DaeBuilderInternal::clear_cache ( ) const
protected

Definition at line 2222 of file dae_builder_internal.cpp.

2222  {
2223  for (bool sx : {false, true}) {
2224  for (bool elim_w : {false, true}) {
2225  for (bool lifted_calls : {false, true}) {
2226  Function& fref = oracle_[sx][elim_w][lifted_calls];
2227  if (!fref.is_null()) fref = Function();
2228  }
2229  }
2230  }
2231  clear_cache_ = false;
2232 }
Function oracle_[2][2][2]
Function oracles (cached)

References clear_cache_, casadi::GenericShared< Shared, Internal >::is_null(), and oracle_.

Referenced by oracle().

◆ create()

Function casadi::DaeBuilderInternal::create ( const std::string &  fname,
const std::vector< std::string > &  name_in,
const std::vector< std::string > &  name_out,
const Dict opts,
bool  sx,
bool  lifted_calls 
) const

Definition at line 1912 of file dae_builder_internal.cpp.

1914  {
1915  // Are there any '_' in the names?
1916  bool with_underscore = false;
1917  for (auto s_io : {&s_in, &s_out}) {
1918  for (const std::string& s : *s_io) {
1919  with_underscore = with_underscore || std::count(s.begin(), s.end(), '_');
1920  }
1921  }
1922  // Model equations in DLL
1923  if (!symbolic_) {
1924  // Cannot lift calls in an FMU
1925  casadi_assert(!lifted_calls, "Lifting requires a symbolic representation");
1926  // Cannot convert to SX
1927  casadi_assert(!sx, "SX expansion requires a symbolic representation");
1928  // Redirect to FmuFunction creation
1929  return fmu_fun(fname, s_in, s_out, opts);
1930  }
1931  // Replace '_' with ':', if needed
1932  if (with_underscore) {
1933  std::vector<std::string> s_in_mod(s_in), s_out_mod(s_out);
1934  for (auto s_io : {&s_in_mod, &s_out_mod}) {
1935  for (std::string& s : *s_io) std::replace(s.begin(), s.end(), '_', ':');
1936  }
1937  // Recursive call
1938  return create(fname, s_in_mod, s_out_mod, opts, sx, lifted_calls);
1939  }
1940  // Check if dependent variables are given and needed
1941  bool elim_w = false;
1942  if (size(Category::W) > 0) {
1943  // Dependent variables exists, eliminate unless v is given
1944  elim_w = true;
1945  for (const std::string& s : s_in) {
1946  if (s == "w") {
1947  // Dependent variables are given
1948  elim_w = false;
1949  break;
1950  }
1951  }
1952  }
1953  // Are lifted calls really needed?
1954  if (lifted_calls) {
1955  // Consistency check
1956  casadi_assert(!elim_w, "Lifted calls cannot be used if dependent variables are eliminated");
1957  // Only lift calls if really needed
1958  lifted_calls = false;
1959  for (const MX& vdef_comp : output(OutputCategory::WDEF)) {
1960  if (vdef_comp.is_output()) {
1961  // There are indeed function calls present
1962  lifted_calls = true;
1963  break;
1964  }
1965  }
1966  }
1967  // Call factory without lifted calls
1968  std::string fname_nocalls = lifted_calls ? fname + "_nocalls" : fname;
1969  Function ret = oracle(sx, elim_w, lifted_calls).factory(fname_nocalls, s_in, s_out, lc_);
1970  // If no lifted calls, done
1971  if (!lifted_calls) return ret;
1972  // MX expressions for ret without lifted calls
1973  std::vector<MX> ret_in = ret.mx_in();
1974  std::vector<MX> ret_out = ret(ret_in);
1975  // Offsets in v
1976  std::vector<casadi_int> h_offsets = offset(var(indices(Category::W)));
1977  // Split "w", "lam_wdef" into components
1978  std::vector<MX> v_in, lam_vdef_in;
1979  for (size_t i = 0; i < s_in.size(); ++i) {
1980  if (ret.name_in(i) == "w") {
1981  v_in = vertsplit(ret_in[i], h_offsets);
1982  } else if (ret.name_in(i) == "lam_wdef") {
1983  lam_vdef_in = vertsplit(ret_in[i], h_offsets);
1984  }
1985  }
1986  // Map dependent variables into index in vector
1987  std::map<MXNode*, size_t> v_map;
1988  for (size_t i = 0; i < size(Category::W); ++i) {
1989  v_map[var(Category::W, i).get()] = i;
1990  }
1991  // Definitions of w
1992  std::vector<MX> wdef = output(OutputCategory::WDEF);
1993  // Collect all the call nodes
1994  std::map<MXNode*, CallIO> call_nodes;
1995  for (size_t vdefind = 0; vdefind < wdef.size(); ++vdefind) {
1996  // Current element handled
1997  const MX& vdefref = wdef.at(vdefind);
1998  // Handle function call nodes
1999  if (vdefref.is_output()) {
2000  // Get function call node
2001  MX c = vdefref.dep(0);
2002  // Find the corresponding call node in the map
2003  auto call_it = call_nodes.find(c.get());
2004  // If first time this call node is encountered
2005  if (call_it == call_nodes.end()) {
2006  // Create new CallIO struct
2007  CallIO cio;
2008  // Save function instance
2009  cio.f = c.which_function();
2010  // Expressions for function call inputs
2011  cio.v.resize(c.n_dep(), -1);
2012  cio.arg.resize(cio.v.size());
2013  for (casadi_int i = 0; i < cio.v.size(); ++i) {
2014  if (c.dep(i).is_constant()) {
2015  cio.arg.at(i) = c.dep(i);
2016  } else {
2017  size_t v_ind = v_map.at(c.dep(i).get());
2018  cio.v.at(i) = v_ind;
2019  cio.arg.at(i) = v_in.at(v_ind);
2020  }
2021  }
2022  // Allocate memory for function call outputs
2023  cio.vdef.resize(c.n_out(), -1);
2024  cio.res.resize(cio.vdef.size());
2025  // Allocate memory for adjoint seeds, if any
2026  if (!lam_vdef_in.empty()) cio.adj1_arg.resize(c.n_out());
2027  // Save to map and update iterator
2028  call_it = call_nodes.insert(std::make_pair(c.get(), cio)).first;
2029  }
2030  // Which output of the function are we calculating?
2031  casadi_int oind = vdefref.which_output();
2032  // Save output expression to structure
2033  call_it->second.vdef.at(oind) = vdefind;
2034  call_it->second.res.at(oind) = v_in.at(vdefind);
2035  // Save adjoint seed to structure, if any
2036  if (!lam_vdef_in.empty()) call_it->second.adj1_arg.at(oind) = lam_vdef_in.at(vdefind);
2037  }
2038  }
2039  // Additional term in jac_vdef_v
2040  for (size_t i = 0; i < ret_out.size(); ++i) {
2041  if (ret.name_out(i) == "jac_wdef_w") {
2042  ret_out.at(i) += jac_vdef_v_from_calls(call_nodes, h_offsets);
2043  }
2044  }
2045  // Additional term in hess_?_v_v where ? is any linear combination containing vdef
2046  MX extra_hess_v_v; // same for all linear combinations, if multiple
2047  for (auto&& e : lc_) {
2048  // Find out of vdef is part of the linear combination
2049  bool has_vdef = false;
2050  for (const std::string& r : e.second) {
2051  if (r == "wdef") {
2052  has_vdef = true;
2053  break;
2054  }
2055  }
2056  // Skip if linear combination does not depend on vdef
2057  if (!has_vdef) continue;
2058  // Search for matching function outputs
2059  for (size_t i = 0; i < ret_out.size(); ++i) {
2060  if (ret.name_out(i) == "hess_" + e.first + "_w_w") {
2061  // Calculate contribution to hess_?_v_v
2062  if (extra_hess_v_v.is_empty())
2063  extra_hess_v_v = hess_v_v_from_calls(call_nodes, h_offsets);
2064  // Add contribution to output
2065  ret_out.at(i) += extra_hess_v_v;
2066  }
2067  }
2068  }
2069  // Assemble modified return function and return
2070  ret = Function(fname, ret_in, ret_out, ret.name_in(), ret.name_out());
2071  return ret;
2072 }
Function create(const std::string &fname, const std::vector< std::string > &name_in, const std::vector< std::string > &name_out, const Dict &opts, bool sx, bool lifted_calls) const
Construct a function object.
const Function & oracle(bool sx=false, bool elim_w=false, bool lifted_calls=false) const
Get the (cached) oracle, SX or MX.
MX hess_v_v_from_calls(std::map< MXNode *, CallIO > &call_nodes, const std::vector< casadi_int > &h_offsets) const
Calculate contribution to hess_?_v_v from lifted calls.
Function fmu_fun(const std::string &fname, const std::vector< std::string > &name_in, const std::vector< std::string > &name_out, const Dict &opts) const
Construct function from an FMU DLL.
MX jac_vdef_v_from_calls(std::map< MXNode *, CallIO > &call_nodes, const std::vector< casadi_int > &h_offsets) const
Calculate contribution to jac_vdef_v from lifted calls.
const MX mx_in(casadi_int ind) const
Get symbolic primitives equivalent to the input expressions.
Definition: function.cpp:1584
Function factory(const std::string &name, const std::vector< std::string > &s_in, const std::vector< std::string > &s_out, const AuxOut &aux=AuxOut(), const Dict &opts=Dict()) const
Definition: function.cpp:1812
MXNode * get() const
Get a const pointer to the node.
Definition: mx.cpp:544

References casadi::DaeBuilderInternal::CallIO::adj1_arg, casadi::DaeBuilderInternal::CallIO::arg, casadi::MX::dep(), casadi::DaeBuilderInternal::CallIO::f, casadi::Function::factory(), casadi::MX::find(), fmu_fun(), casadi::MX::get(), hess_v_v_from_calls(), indices(), casadi::MX::is_constant(), casadi::GenericMatrix< MatType >::is_empty(), casadi::MX::is_output(), jac_vdef_v_from_calls(), lc_, casadi::Function::mx_in(), casadi::MX::n_dep(), casadi::MX::n_out(), casadi::Function::name_in(), casadi::Function::name_out(), oracle(), output(), casadi::DaeBuilderInternal::CallIO::res, size(), symbolic_, casadi::DaeBuilderInternal::CallIO::v, var(), casadi::DaeBuilderInternal::CallIO::vdef, casadi::W, casadi::WDEF, casadi::MX::which_function(), and casadi::MX::which_output().

◆ debug_repr()

std::string casadi::GenericSharedInternal< SharedObject , SharedObjectInternal >::debug_repr ( const Internal *  i) const
inherited

Definition at line 62 of file generic_shared_internal.hpp.

162  {
163  // Note: i != this because of something something multiple inheritance
164  return str( (casadi_int)(i)) + "/" + static_cast<const Internal*>(this)->class_name();
165  }
std::string str(const T &v)
String representation, any type.

◆ default_initial()

Initial casadi::DaeBuilderInternal::default_initial ( Causality  causality,
Variability  variability 
)
staticprotected

Definition at line 2708 of file dae_builder_internal.cpp.

2708  {
2709  // According to table in FMI 2.0.2 specification, section 2.2.7
2710  switch (variability) {
2711  case Variability::CONSTANT:
2713  return Initial::EXACT;
2714  break;
2715  case Variability::FIXED:
2716  // Fall-through
2717  case Variability::TUNABLE:
2719  return Initial::EXACT;
2721  return Initial::CALCULATED;
2722  break;
2723  case Variability::DISCRETE:
2724  // Fall-through
2727  return Initial::CALCULATED;
2728  break;
2729  default: break;
2730  }
2731  // Initial value not available
2732  return Initial::NA;
2733 }

References casadi::CALCULATED, casadi::CALCULATED_PARAMETER, causality(), casadi::CONSTANT, casadi::CONTINUOUS, casadi::DISCRETE, casadi::EXACT, casadi::FIXED, casadi::LOCAL, casadi::NA, casadi::OUTPUT, casadi::PARAMETER, casadi::TUNABLE, and variability().

Referenced by import_model_variables().

◆ default_variability()

Variability casadi::DaeBuilderInternal::default_variability ( Causality  causality,
Type  type 
)
staticprotected

Definition at line 2692 of file dae_builder_internal.cpp.

2692  {
2693  // Default variability per FMI 3.0.2, section 2.4.7.4
2694  // "The default for variables of causality parameter, structural parameter or
2695  // calculated parameter is fixed."
2697  return Variability::FIXED;
2698  }
2699  // "The default for variables of type Float32 and Float64 and causality other
2700  // than parameter, structuralParameter or calculatedParameter is continuous"
2701  if (type == Type::FLOAT32 || type == Type::FLOAT64) {
2702  return Variability::CONTINUOUS;
2703  } else {
2704  return Variability::DISCRETE;
2705  }
2706 }

References casadi::CALCULATED_PARAMETER, causality(), casadi::CONTINUOUS, casadi::DISCRETE, casadi::FIXED, casadi::FLOAT32, casadi::FLOAT64, and casadi::PARAMETER.

Referenced by add(), and import_model_variables().

◆ dependent_fun()

Function casadi::DaeBuilderInternal::dependent_fun ( const std::string &  fname,
const std::vector< std::string > &  s_in,
const std::vector< std::string > &  s_out 
) const

Definition at line 2434 of file dae_builder_internal.cpp.

2436  {
2437  // Are we calculating d and/or w
2438  bool calc_d = false, calc_w = false;
2439  // Convert outputs to enums
2440  std::vector<Category> v_out;
2441  v_out.reserve(v_out.size());
2442  for (const std::string& s : s_out) {
2443  Category e = to_enum<Category>(s);
2444  if (e == Category::D) {
2445  calc_d = true;
2446  } else if (e == Category::W) {
2447  calc_w = true;
2448  } else {
2449  casadi_error("Can only calculate d and/or w");
2450  }
2451  v_out.push_back(e);
2452  }
2453  // Consistency check
2454  casadi_assert(calc_d || calc_w, "Nothing to calculate");
2455  // Convert inputs to enums
2456  std::vector<Category> v_in;
2457  v_in.reserve(v_in.size());
2458  for (const std::string& s : s_in) {
2459  Category e = to_enum<Category>(s);
2460  if (calc_d && e == Category::D) casadi_error("'d' cannot be both input and output");
2461  if (calc_w && e == Category::W) casadi_error("'w' cannot be both input and output");
2462  v_in.push_back(e);
2463  }
2464  // Collect input expressions
2465  std::vector<MX> f_in;
2466  f_in.reserve(s_in.size());
2467  for (Category v : v_in) f_in.push_back(vertcat(input(v)));
2468  // Collect output expressions
2469  std::vector<MX> f_out;
2470  f_out.reserve(s_out.size());
2471  for (Category v : v_out) f_out.push_back(vertcat(input(v)));
2472  // Variables to be substituted
2473  std::vector<MX> dw, dwdef;
2474  if (calc_d) {
2475  std::vector<MX> d = var(indices(Category::D));
2476  dw.insert(dw.end(), d.begin(), d.end());
2477  std::vector<MX> ddef = output(OutputCategory::DDEF);
2478  dwdef.insert(dwdef.end(), ddef.begin(), ddef.end());
2479  }
2480  if (calc_w) {
2481  std::vector<MX> w = var(indices(Category::W));
2482  dw.insert(dw.end(), w.begin(), w.end());
2483  std::vector<MX> wdef = output(OutputCategory::WDEF);
2484  dwdef.insert(dwdef.end(), wdef.begin(), wdef.end());
2485  }
2486  // Perform elimination
2487  substitute_inplace(dw, dwdef, f_out);
2488  // Assemble return function
2489  return Function(fname, f_in, f_out, s_in, s_out);
2490 }
std::vector< MX > input(Category ind) const

References casadi::D, casadi::DDEF, indices(), input(), output(), var(), casadi::W, and casadi::WDEF.

◆ der() [1/2]

MX casadi::DaeBuilderInternal::der ( const MX var) const

Definition at line 1682 of file dae_builder_internal.cpp.

1682  {
1683  return const_cast<DaeBuilderInternal*>(this)->der(var, false);
1684 }
MX der(const MX &var) const
Get a derivative expression by non-differentiated expression (const, never create)
DaeBuilderInternal(const std::string &name, const std::string &path, const Dict &opts)
Constructor.

References var().

Referenced by der(), disp(), generate_model_structure(), generate_wrapper(), import_dynamic_equations(), lift(), and update_dependencies().

◆ der() [2/2]

MX casadi::DaeBuilderInternal::der ( const MX var,
bool  may_allocate = true 
)

Definition at line 1686 of file dae_builder_internal.cpp.

1686  {
1687  // Must be a vector
1688  casadi_assert(var.is_column(), "Input expression must be a vector");
1689  // Quick return if symbolic variable
1690  if (var.is_symbolic()) return get_der(find(var), may_allocate);
1691  // If a vertical concatenation
1692  if (var.is_valid_input()) {
1693  // Differentiate each primitive
1694  auto var_split = var.primitives();
1695  for (MX& s : var_split) s = der(s, may_allocate);
1696  // Return the concatenation
1697  return var.join_primitives(var_split);
1698  }
1699  // Handle general case: Get dependent symbolic primitives
1700  std::vector<MX> dep = symvar(var);
1701  // Get derivatives of dependent symbolic primitives
1702  std::vector<MX> dep_der;
1703  for (size_t ind : find(dep)) dep_der.push_back(get_der(ind, may_allocate));
1704  // Forward directional derivative to get time derivative:
1705  // dot(var) = d_var/d_dep * dot(dep)
1706  std::vector<std::vector<MX>> r = {dep_der};
1707  r = forward(std::vector<MX>{var}, dep, r);
1708  casadi_assert_dev(r.size() == 1);
1709  return vertcat(r.at(0));
1710 }
MX get_der(size_t ind) const
Get a derivative expression by variable index (const, never create)
size_t find(const std::string &name) const
Get index of variable, given name.
bool is_column() const
Check if the matrix is a column vector (i.e. size2()==1)
bool is_valid_input() const
Check if matrix can be used to define function inputs.
Definition: mx.cpp:925
std::vector< MX > primitives() const
Get primitives.
Definition: mx.cpp:933
MX join_primitives(const std::vector< MX > &v) const
Join an expression along symbolic primitives.
Definition: mx.cpp:965
bool is_symbolic() const
Check if symbolic.
Definition: mx.cpp:766

References der(), find(), get_der(), casadi::GenericMatrix< MatType >::is_column(), casadi::MX::is_symbolic(), casadi::MX::is_valid_input(), casadi::MX::join_primitives(), casadi::MX::primitives(), and var().

◆ destroySingleton()

void casadi::GenericSharedInternal< SharedObject , SharedObjectInternal >::destroySingleton ( )
inlineprotectedinherited

Called in the destructor of singletons

Definition at line 77 of file generic_shared_internal.hpp.

77  {
78  static_cast<Internal*>(this)->count--;
79  }

◆ disp()

void casadi::DaeBuilderInternal::disp ( std::ostream &  stream,
bool  more 
) const
overridevirtual

Implements casadi::SharedObjectInternal.

Definition at line 1226 of file dae_builder_internal.cpp.

1226  {
1227  // Assert correctness
1228  if (more) sanity_check();
1229 
1230  // Print dimensions
1231  stream << "nx = " << size(Category::X) << ", "
1232  << "nz = " << size(Category::Z) << ", "
1233  << "nq = " << size(Category::Q) << ", "
1234  << "ny = " << outputs_.size() << ", "
1235  << "np = " << size(Category::P) << ", "
1236  << "nc = " << size(Category::C) << ", "
1237  << "nd = " << size(Category::D) << ", "
1238  << "nw = " << size(Category::W) << ", "
1239  << "nu = " << size(Category::U);
1240 
1241  // Quick return?
1242  if (!more) return;
1243  stream << std::endl;
1244 
1245  // Print the functions
1246  if (!fun_.empty()) {
1247  stream << "Functions:" << std::endl;
1248  for (const Function& f : fun_) {
1249  stream << " " << f << std::endl;
1250  }
1251  }
1252 
1253  // Print the variables, including outputs
1254  stream << "Model variables:" << std::endl;
1257  if (size(cat) > 0) {
1258  stream << " " << to_string(cat) << " = " << var(indices(cat)) << std::endl;
1259  }
1260  }
1261 
1262  // All variables that can have dependent variables
1263  for (Category cat : {Category::C, Category::D, Category::W}) {
1264  if (size(cat) > 0) {
1265  stream << "List of " << description(cat) << "s (" << to_string(cat) << "):" << std::endl;
1266  for (size_t c : indices(cat)) {
1267  const Variable& v = variable(c);
1268  stream << " " << v.name;
1269  if (v.bind >= 0) stream << " := " << variable(v.bind).v;
1270  stream << std::endl;
1271  }
1272  }
1273  }
1274 
1275  // Print derivatives
1276  for (Category cat : {Category::X, Category::Q}) {
1277  if (size(cat) > 0) {
1278  stream << "Time derivatives of " << description(cat) << "s (" << to_string(cat) << "):"
1279  << std::endl;
1280  for (size_t k : indices(cat)) {
1281  const Variable& v = variable(k);
1282  stream << " " << v.name << ": " << der(v.v) << std::endl;
1283  }
1284  }
1285  }
1286 
1287  // Outputs
1288  if (!outputs_.empty()) {
1289  stream << "Outputs (y):" << std::endl;
1290  for (size_t k : outputs_) {
1291  const Variable& v = variable(k);
1292  stream << " " << v.name << ": " << v.v << std::endl;
1293  }
1294  }
1295 
1296  if (!residuals_.empty()) {
1297  stream << "Algebraic equations:" << std::endl;
1298  for (size_t k : residuals_) {
1299  stream << " 0 == " << variable(k).v << std::endl;
1300  }
1301  }
1302 
1303  if (!init_.empty()) {
1304  stream << "Initial equations:" << std::endl;
1305  for (size_t k : init_) {
1306  const Variable& v = variable(k);
1307  stream << " " << v.name;
1308  if (!v.ieq.is_empty()) stream << " := " << v.ieq;
1309  stream << std::endl;
1310  }
1311  }
1312 
1313  if (!when_.empty()) {
1314  stream << "When equations:" << std::endl;
1315  for (auto weq : when_) {
1316  stream << " when " << variable(weq.first).v << " < 0 : " << std::endl;
1317  for (size_t eq : weq.second) {
1318  auto v = variable(eq).parent;
1319  stream << " " << variable(v).name << " := " << variable(eq).v << std::endl;
1320  }
1321  }
1322  }
1323 }
std::vector< std::pair< size_t, std::vector< size_t > > > when_
std::vector< size_t > residuals_
void eq(const MX &lhs, const MX &rhs, const Dict &opts)
Add a simple equation.
MX v
Variable expression (always a vector)

References casadi::Variable::bind, casadi::C, casadi::D, der(), casadi::description(), eq(), fun_, casadi::Variable::ieq, indices(), init_, casadi::GenericMatrix< MatType >::is_empty(), casadi::Variable::name, outputs_, casadi::P, casadi::Variable::parent, casadi::Q, residuals_, sanity_check(), size(), casadi::T, casadi::to_string(), casadi::U, casadi::Variable::v, var(), variable(), casadi::W, when_, casadi::X, and casadi::Z.

Referenced by import_binding_equations(), import_dynamic_equations(), and import_initial_equations().

◆ eliminate()

void casadi::DaeBuilderInternal::eliminate ( Category  cat)

Definition at line 1723 of file dae_builder_internal.cpp.

1723  {
1724  // Eliminate quadratures
1725  if (cat == Category::Q) {
1726  for (size_t q : indices(cat)) set_category(q, Category::X);
1727  return;
1728  }
1729 
1730  // Assume dependent variable (c, d, w)
1731  casadi_assert(is_acyclic(cat), "Elimination not supported for category " + to_string(cat));
1732 
1733  // Quick return if no dependent variables
1734  if (size(cat) == 0) return;
1735  // Clear cache after this
1736  clear_cache_ = true;
1737  // Ensure variables are sorted
1738  sort(cat);
1739  // Expressions where the variables are also being used
1740  std::vector<MX> ex;
1741  for (const Variable* v : variables_) {
1742  if (!v->v.is_constant()) ex.push_back(v->v);
1743  }
1744  // Perform elimination
1745  std::vector<size_t> ind = indices(cat);
1746  std::vector<MX> v = var(ind);
1747  std::vector<MX> vdef = output(dependent_definition(cat));
1748  substitute_inplace(v, vdef, ex);
1749  // Replace binding equations
1750  auto it = ex.begin();
1751  for (Variable* v : variables_) {
1752  if (!v->v.is_constant()) v->v = *it++;
1753  }
1754  // Consistency check
1755  casadi_assert_dev(it == ex.end());
1756  // Reclassify as calculated variables
1757  for (size_t k : ind) {
1759  }
1760 }
void sort(Category cat)
Sort all variables of a category.
void set_category(size_t ind, Category cat)
Set category.
OutputCategory dependent_definition(Category cat)

References casadi::CALCULATED, categorize(), clear_cache_, casadi::dependent_definition(), indices(), casadi::is_acyclic(), output(), casadi::Q, set_category(), size(), sort(), casadi::to_string(), var(), variable(), variables_, and casadi::X.

◆ eq()

void casadi::DaeBuilderInternal::eq ( const MX lhs,
const MX rhs,
const Dict opts 
)
protected

Definition at line 3058 of file dae_builder_internal.cpp.

3058  {
3059  // Read options
3060  for (auto&& op : opts) {
3061  casadi_error("No such option: " + op.first);
3062  }
3063  // Make sure vectors
3064  casadi_assert(lhs.is_column(), "Left-hand-side must be a column vector");
3065  casadi_assert(rhs.is_column(), "Right-hand-side must be a column vector");
3066  // Make sure dense
3067  if (!lhs.is_dense()) return eq(densify(lhs), rhs, opts);
3068  if (!rhs.is_dense()) return eq(lhs, densify(rhs), opts);
3069  // Make sure dimensions agree
3070  if (lhs.size1() != rhs.size1()) {
3071  // Handle mismatching dimnensions by recursion
3072  if (lhs.size1() == 1 && rhs.size1() > 1) {
3073  return eq(repmat(lhs, rhs.size1()), rhs, opts);
3074  } else if (lhs.size1() > 1 && rhs.size1() == 1) {
3075  return eq(lhs, repmat(rhs, lhs.size1()), opts);
3076  } else {
3077  casadi_error("Mismatched dimensions: " + str(lhs.size1()) + " vs " + str(rhs.size1()));
3078  }
3079  }
3080  // Make sure right-hand-side only depends on known model variables
3081  std::vector<size_t> rhs_vars = find(symvar(rhs));
3082  // If right-hand-side contains a time derivative that hasn't been defined yet,
3083  // add a new algebraic variable der_x and the ODE "der(x) = der_x"
3084  // This ensures that the DAE stays in semi-explicit form
3085  for (size_t rhs : rhs_vars) {
3086  Variable& v = variable(rhs);
3087  if (v.category == Category::Z && v.parent >= 0) {
3088  // Find the corresponding state variable
3089  Variable& x = variable(v.parent);
3090  // The "parent" attribute is used in some other cases, like assignments
3091  casadi_assert(x.der == v.index, "Cannot handle right-hand-side variable: " + v.name);
3092  // Create a new algebraic variable der_{name}, to distinguish from der({name})
3093  Variable& der_x = add(unique_name("der_" + x.name, true),
3095  {{"dimension", {x.dimension}}});
3096  // Add the trivial ODE
3097  eq(x.get_der(*this), der_x.v, Dict());
3098  }
3099  }
3100  // Try to honor a == b as an explicit equation
3101  if (lhs.is_valid_input()) {
3102  // Explicit equation
3103  if (lhs.is_symbolic()) {
3104  // Regular symbolic: Find the variable
3105  Variable& v = variable(lhs);
3106  // Set the binding equation
3107  if (v.has_beq()) {
3108  // Treat as implicit equation via recursion
3109  return eq(MX::zeros(lhs.sparsity()), lhs - rhs, opts);
3110  } else {
3111  // Set the binding equation
3112  Variable& beq = assign(v.name, rhs);
3113  v.bind = beq.index;
3114  }
3115  // (Re)classify variables
3116  if (v.parent >= 0) {
3117  // Derivative variable is being set - find the corresponding state variable
3118  Variable& x = variable(v.parent);
3119  // The "parent" attribute is used in some other cases, like assignments
3120  casadi_assert(x.der == v.index, "Cannot handle left-hand-side: " + str(lhs));
3121  // Reclassify as a differential state and derivative as a dependent variable
3122  if (x.category == Category::Z) {
3123  // Not previously used: Reclassify as differential state
3124  categorize(x.index, detect_quad_ && !x.in_rhs ? Category::Q : Category::X);
3125  categorize(v.index, Category::W);
3126  } else if (x.category == Category::W) {
3127  // Already given a defining equation: Create a new dependent variable and use this
3128  // for the previous definition
3129  Variable& def_x = add(unique_name("def_" + x.name, true),
3131  {{"dimension", {x.dimension}}});
3132  categorize(def_x.index, Category::W);
3133  def_x.bind = x.bind;
3134  // We can now reclassify x as a differential state
3135  x.bind = -1;
3136  categorize(x.index, Category::X);
3137  categorize(v.index, Category::W);
3138  // Add a new implicit equation: 0 == x - def_x
3140  x.v - def_x.v, {{"dimension", x.dimension}});
3141  categorize(alg.index, Category::CALCULATED);
3142  residuals_.push_back(alg.index);
3143  } else {
3144  casadi_error("Unexpected category for " + x.name + ": " + to_string(x.category));
3145  }
3146  } else if (v.category == Category::Z) {
3147  // Reclassify as dependent variable
3148  categorize(v.index, Category::W);
3149  } else {
3150  casadi_error("Cannot handle left-hand-side: " + str(lhs) + " of category '"
3151  + to_string(v.category) + "'");
3152  }
3153  } else {
3154  // Concatenation: Split into primitives
3155  auto lhs_split = lhs.primitives();
3156  std::vector<MX> rhs_split = lhs.split_primitives(rhs);
3157  // Call recursively
3158  for (size_t k = 0; k < lhs_split.size(); ++k) {
3159  eq(lhs_split.at(k), rhs_split.at(k), opts);
3160  }
3161  return;
3162  }
3163  } else {
3164  // Implicit equation: Create residual variable
3166  lhs - rhs, {{"dimension", std::vector<casadi_int>{lhs.size1()}}});
3167  categorize(alg.index, Category::CALCULATED);
3168  residuals_.push_back(alg.index);
3169  }
3170  // Do not allow any quadrature states in the right-hand-sides
3171  for (size_t rhs : rhs_vars) {
3172  Variable& v = variable(rhs);
3173  if (!v.in_rhs) {
3174  v.in_rhs = true;
3175  if (v.category == Category::Q) categorize(v.index, Category::X);
3176  }
3177  }
3178 }
Variable & assign(const std::string &name, const MX &val)
Assignment inside when-equations or if-else equations.
static MX zeros(casadi_int nrow=1, casadi_int ncol=1)
Create a dense matrix or a matrix with specified sparsity with all entries zero.

References add(), casadi::Variable::category, casadi::CONTINUOUS, casadi::Variable::der, find(), casadi::Variable::index, casadi::GenericMatrix< MatType >::is_column(), casadi::GenericMatrix< MatType >::is_dense(), casadi::LOCAL, casadi::Variable::name, casadi::Variable::parent, casadi::GenericMatrix< MatType >::size1(), casadi::str(), unique_name(), variable(), and casadi::Z.

Referenced by disp(), import_binding_equations(), import_dynamic_equations(), import_initial_equations(), transition(), and when().

◆ export_fmu()

std::vector< std::string > casadi::DaeBuilderInternal::export_fmu ( const Dict opts) const

Definition at line 893 of file dae_builder_internal.cpp.

893  {
894  // Default options
895  bool no_warning = false;
896  for (auto&& op : opts) {
897  if (op.first == "no_warning") {
898  no_warning = op.second;
899  }
900  }
901  // Feature incomplete
902  if (!no_warning) casadi_warning("FMU generation is experimental and incomplete")
903  // Return object
904  std::vector<std::string> ret;
905  // GUID
906  std::string guid = generate_guid();
907  // Generate model function
908  std::string dae_filename = name_;
909  Function dae = shared_from_this<DaeBuilder>().create(dae_filename,
910  {"t", "x", "p", "u"}, {"ode", "y", "zero"});
911  // Event transition function, if needed
912  Function tfun;
913  if (!event_indicators_.empty()) tfun = transition("transition_" + name_);
914  // Generate C code for model equations
915  Dict codegen_opts;
916  codegen_opts["with_header"] = true;
917  CodeGenerator gen(dae_filename, codegen_opts);
918  gen.add(dae);
919  gen.add(dae.forward(1));
920  gen.add(dae.reverse(1));
921  if (!tfun.is_null()) gen.add(tfun);
922  ret.push_back(gen.generate());
923  ret.push_back(dae_filename + ".h");
924  // Make sure dependencies are up-to-date
926  // Generate FMU wrapper file
927  std::string wrapper_filename = generate_wrapper(guid, gen);
928  ret.push_back(wrapper_filename);
929  // Generate build description
930  ret.push_back(generate_build_description(ret));
931  // Generate modelDescription file
932  ret.push_back(generate_model_description(guid));
933  // Return list of files
934  return ret;
935 }
Function transition(const std::string &fname, casadi_int index, bool dummy_index_input=false) const
Construct a function describing transition at a specific event.
void update_dependencies() const
Update model variable dependencies.
std::string generate_wrapper(const std::string &guid, const CodeGenerator &gen) const
Generate FMU wrapper file (fmi3Functions.c)
std::vector< size_t > event_indicators_
std::string generate_build_description(const std::vector< std::string > &cfiles) const
Generate buildDescription.xml.
std::string generate_model_description(const std::string &guid) const
Generate modelDescription.xml.
B shared_from_this()
Get a shared object from the current internal object.

References casadi::CodeGenerator::add(), casadi::Function::create(), event_indicators_, casadi::Function::forward(), casadi::CodeGenerator::generate(), generate_build_description(), generate_guid(), generate_model_description(), generate_wrapper(), casadi::GenericShared< Shared, Internal >::is_null(), name_, casadi::Function::reverse(), transition(), and update_dependencies().

◆ find() [1/4]

size_t casadi::DaeBuilderInternal::find ( const MX v) const

Definition at line 3916 of file dae_builder_internal.cpp.

3916  {
3917  // Make sure it is a valid input
3918  casadi_assert(v.is_symbolic(), "Variable must be symbolic");
3919  // Find the prospective variable
3920  size_t ind = find(v.name());
3921  // Make sure that the expression (not just the name) is correct
3922  casadi_assert(is_equal(v, variables_.at(ind)->v),
3923  "Variable \"" + v.name() + "\" has mismatching symbolic expression");
3924  // Return index
3925  return ind;
3926 }
bool is_equal(double x, double y, casadi_int depth=0)
Definition: calculus.hpp:281

References find(), casadi::is_equal(), casadi::MX::is_symbolic(), casadi::MX::name(), and variables_.

◆ find() [2/4]

size_t casadi::DaeBuilderInternal::find ( const std::string &  name) const

Definition at line 3910 of file dae_builder_internal.cpp.

3910  {
3911  auto it = varind_.find(name);
3912  casadi_assert(it != varind_.end(), "No such variable: \"" + name + "\".");
3913  return it->second;
3914 }
std::unordered_map< std::string, size_t > varind_
Find of variable by name.

References name(), and varind_.

Referenced by der(), eq(), find(), fmu_fun(), sort(), sort_z(), and tear().

◆ find() [3/4]

std::vector< size_t > casadi::DaeBuilderInternal::find ( const std::vector< MX > &  v) const

Definition at line 3934 of file dae_builder_internal.cpp.

3934  {
3935  std::vector<size_t> r(v.size());
3936  for (size_t i = 0; i < r.size(); ++i) r[i] = find(v[i]);
3937  return r;
3938 }

References find().

◆ find() [4/4]

std::vector< size_t > casadi::DaeBuilderInternal::find ( const std::vector< std::string > &  name) const

Definition at line 3928 of file dae_builder_internal.cpp.

3928  {
3929  std::vector<size_t> r(name.size());
3930  for (size_t i = 0; i < r.size(); ++i) r[i] = find(name[i]);
3931  return r;
3932 }

References find(), and name().

◆ fmu_fun()

Function casadi::DaeBuilderInternal::fmu_fun ( const std::string &  fname,
const std::vector< std::string > &  name_in,
const std::vector< std::string > &  name_out,
const Dict opts 
) const

Definition at line 2562 of file dae_builder_internal.cpp.

2565  {
2566  // Iterator for options lookup
2567  Dict::const_iterator it;
2568  // Scheme inputs
2569  std::vector<std::string> scheme_in;
2570  bool has_in = false;
2571  if ((it = opts.find("scheme_in")) != opts.end()) {
2572  try {
2573  scheme_in = it->second;
2574  } catch (std::exception& e) {
2575  casadi_error(std::string("Cannot read 'scheme_in': ") + e.what());
2576  }
2577  has_in = true;
2578  }
2579  // Scheme outputs
2580  std::vector<std::string> scheme_out;
2581  bool has_out = false;
2582  if ((it = opts.find("scheme_out")) != opts.end()) {
2583  try {
2584  scheme_out = it->second;
2585  has_out = true;
2586  } catch (std::exception& e) {
2587  casadi_error(std::string("Cannot read 'scheme_out': ") + e.what());
2588  }
2589  }
2590  // If scheme_in and/or scheme_out not provided, identify from name_in, name_out
2591  if (!has_in || !has_out) {
2592  FmuFunction::identify_io(has_in ? 0 : &scheme_in, has_out ? 0 : &scheme_out, name_in, name_out);
2593  }
2594  // IO scheme
2595  std::map<std::string, std::vector<size_t>> scheme;
2596  if ((it = opts.find("scheme")) != opts.end()) {
2597  try {
2598  // Argument is a Dict
2599  Dict scheme_dict = it->second;
2600  // Convert indices
2601  for (auto&& e : scheme_dict) {
2602  std::vector<std::string> v = e.second;
2603  scheme[e.first] = find(v);
2604  }
2605  } catch (std::exception& e) {
2606  casadi_error(std::string("Cannot read 'scheme': ") + e.what());
2607  }
2608  } else {
2609  // Initialize all scheme entries
2610  for (auto&& s : dyn_in()) scheme[s] = std::vector<size_t>();
2611  for (auto&& s : dyn_out()) scheme[s] = std::vector<size_t>();
2612  // Default IO scheme
2613  scheme["t"] = indices(Category::T);
2614  scheme["x"] = indices(Category::X);
2615  scheme["u"] = indices(Category::U);
2616  scheme["z"] = indices(Category::Z);
2617  scheme["p"] = indices(Category::P);
2618  scheme["ode"] = indices(Category::X);
2619  for (size_t& i : scheme["ode"]) i = variable(i).der;
2620  scheme["alg"] = indices(Category::Z);
2621  casadi_assert(size(Category::Z) == 0, "Not implemented)");
2622  scheme["y"] = outputs_;
2623  }
2624  // Auxilliary variables, if any
2625  std::vector<std::string> aux;
2626  if ((it = opts.find("aux")) != opts.end()) {
2627  try {
2628  aux = it->second;
2629  } catch (std::exception& e) {
2630  casadi_error(std::string("Cannot read 'aux': ") + e.what());
2631  }
2632  }
2633  // New FMU instance (to be shared between derivative functions)
2634  Fmu fmu(name, fmi_major_ >= 3 ? FmuApi::FMI3 : FmuApi::FMI2, this,
2635  scheme_in, scheme_out, scheme, aux);
2636 
2637  // Crete new function
2638  return Function::create(new FmuFunction(name, fmu, name_in, name_out), opts);
2639 }
static void identify_io(std::vector< std::string > *scheme_in, std::vector< std::string > *scheme_out, const std::vector< std::string > &name_in, const std::vector< std::string > &name_out)
static Function create(FunctionInternal *node)
Create from node.
Definition: function.cpp:336
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

References casadi::Function::create(), casadi::Variable::der, casadi::dyn_in(), casadi::dyn_out(), find(), casadi::FMI2, casadi::FMI3, fmi_major_, FmuFunction, casadi::FmuFunction::identify_io(), indices(), name(), outputs_, casadi::P, size(), casadi::T, casadi::U, variable(), casadi::X, and casadi::Z.

Referenced by create().

◆ fun()

Function casadi::DaeBuilderInternal::fun ( const std::string &  name) const
protected

Definition at line 3988 of file dae_builder_internal.cpp.

3988  {
3989  casadi_assert(has_fun(name), "No such function: '" + name + "'");
3990  for (const Function& f : fun_) {
3991  if (f.name()==name) return f;
3992  }
3993  return Function();
3994 }

References fun_, has_fun(), and name().

◆ gather_eq()

Function casadi::DaeBuilderInternal::gather_eq ( ) const

Definition at line 2641 of file dae_builder_internal.cpp.

2641  {
2642  // Output expressions
2643  std::vector<MX> f_out;
2644  // Names of outputs
2645  std::vector<std::string> f_out_name;
2646  // Get all expressions
2647  for (OutputCategory cat : output_categories()) {
2648  std::vector<MX> v = output(cat);
2649  if (!v.empty()) {
2650  f_out.push_back(vertcat(v));
2651  f_out_name.push_back(to_string(cat));
2652  }
2653  }
2654  // Construct function
2655  return Function("all_eq", {}, f_out, {}, f_out_name, {{"allow_free", true}});
2656 }
std::vector< OutputCategory > output_categories()

References output(), casadi::output_categories(), and casadi::to_string().

◆ generate() [1/2]

std::string casadi::DaeBuilderInternal::generate ( const std::vector< double > &  v)
static

Import existing problem from FMI/XML

Definition at line 952 of file dae_builder_internal.cpp.

952  {
953  std::stringstream ss;
954  ss << "{";
955  bool first = true;
956  for (double e : v) {
957  // Separator
958  if (!first) ss << ", ";
959  first = false;
960  // Print element
961  ss << std::scientific << std::setprecision(std::numeric_limits<double>::digits10 + 1) << e;
962  }
963  ss << "}";
964  return ss.str();
965 }

◆ generate() [2/2]

std::string casadi::DaeBuilderInternal::generate ( const std::vector< size_t > &  v)
static

Helper function: generate constants

Definition at line 937 of file dae_builder_internal.cpp.

937  {
938  std::stringstream ss;
939  ss << "{";
940  bool first = true;
941  for (double e : v) {
942  // Separator
943  if (!first) ss << ", ";
944  first = false;
945  // Print element
946  ss << e;
947  }
948  ss << "}";
949  return ss.str();
950 }

Referenced by generate_wrapper().

◆ generate_build_description()

std::string casadi::DaeBuilderInternal::generate_build_description ( const std::vector< std::string > &  cfiles) const

Definition at line 703 of file dae_builder_internal.cpp.

704  {
705  // Default arguments
706  int fmi_major = 3;
707  int fmi_minor = 0;
708  std::string model_name = name_;
709  // Construct XML file
710  XmlNode r;
711  // Preamble
712  r.name = "fmiBuildDescription";
713  r.set_attribute("fmiVersion", std::to_string(fmi_major) + "." + std::to_string(fmi_minor));
714  // Set of source files
715  XmlNode source_file_set;
716  source_file_set.name = "SourceFileSet";
717  for (auto&& f : cfiles) {
718  XmlNode source_file;
719  source_file.name = "SourceFile";
720  source_file.set_attribute("name", f);
721  source_file_set.children.push_back(source_file);
722  }
723  // Build configurations
724  XmlNode bc;
725  bc.name = "BuildConfiguration";
726  bc.set_attribute("modelIdentifier", model_name);
727  bc.children.push_back(source_file_set);
728  r.children.push_back(bc);
729  // XML file name
730  std::string xml_filename = "buildDescription.xml";
731  // Construct ModelDescription
732  XmlNode build_description;
733  build_description.children.push_back(r);
734  // Export to file
735  XmlFile xml_file("tinyxml");
736  xml_file.dump(xml_filename, build_description);
737  return xml_filename;
738 }

References casadi::XmlNode::children, casadi::XmlFile::dump(), casadi::XmlNode::name, name_, and casadi::XmlNode::set_attribute().

Referenced by export_fmu().

◆ generate_guid()

std::string casadi::DaeBuilderInternal::generate_guid ( )
static

Import existing problem from FMI/XML

Definition at line 4170 of file dae_builder_internal.cpp.

4170  {
4171  // Initialize random seed
4172  static bool initialized = false;
4173  if (!initialized) {
4174  srand(::time(nullptr)); // NOLINT(runtime/threadsafe_fn)
4175  initialized = true;
4176  }
4177  // Possible characters
4178  const char h[] = "0123456789abcdef";
4179  // Length of GUID
4180  const size_t len = 32;
4181  // Generate random hex string
4182  std::vector<char> buf(len);
4183  for (size_t i = 0; i < len; ++i)
4184  buf[i] = h[rand() % 16]; // NOLINT(runtime/threadsafe_fn)
4185  return std::string(&buf.front(), len);
4186 }

References time().

Referenced by export_fmu().

◆ generate_model_description()

std::string casadi::DaeBuilderInternal::generate_model_description ( const std::string &  guid) const

Definition at line 740 of file dae_builder_internal.cpp.

740  {
741  // Default arguments
742  int fmi_major = 3;
743  int fmi_minor = 0;
744  std::string model_name = name_;
745  std::string description; // none
746  std::string author; // none
747  std::string version; // none
748  std::string copyright; // none
749  std::string license; // none
750  // Construct XML file
751  XmlNode r;
752  // Preamble
753  r.name = "fmiModelDescription";
754  r.set_attribute("fmiVersion", std::to_string(fmi_major) + "." + std::to_string(fmi_minor));
755  r.set_attribute("modelName", model_name);
756  r.set_attribute(fmi_major >= 3 ? "instantiationToken" : "guid", guid);
757  if (!description.empty()) r.set_attribute("description", description);
758  if (!author.empty()) r.set_attribute("author", author);
759  if (!version.empty()) r.set_attribute("version", version);
760  if (!copyright.empty()) r.set_attribute("copyright", copyright);
761  if (!license.empty()) r.set_attribute("license", license);
762  r.set_attribute("generationTool", "CasADi");
763  r.set_attribute("generationDateAndTime", iso_8601_time());
764  r.set_attribute("variableNamingConvention", "structured"); // flat better?
765  if (fmi_major < 3) r.set_attribute("numberOfEventIndicators", "0");
766  // Model exchange marker
767  XmlNode me;
768  me.name = "ModelExchange";
769  me.set_attribute("modelIdentifier", model_name); // sanitize name?
770  r.children.push_back(me);
771  // Model variables
772  r.children.push_back(generate_model_variables());
773  // Model structure
774  r.children.push_back(generate_model_structure());
775  // XML file name
776  std::string xml_filename = "modelDescription.xml";
777  // Construct ModelDescription
778  XmlNode model_description;
779  model_description.children.push_back(r);
780  // Export to file
781  XmlFile xml_file("tinyxml");
782  xml_file.dump(xml_filename, model_description);
783  return xml_filename;
784 }
XmlNode generate_model_variables() const
Generate FMU ModelVariables.
XmlNode generate_model_structure() const
Generate FMU ModelStructure.
static std::string iso_8601_time()
Get current date and time in the ISO 8601 format.

References casadi::XmlNode::children, casadi::description(), casadi::XmlFile::dump(), generate_model_structure(), generate_model_variables(), iso_8601_time(), casadi::XmlNode::name, name_, and casadi::XmlNode::set_attribute().

Referenced by export_fmu().

◆ generate_model_structure()

XmlNode casadi::DaeBuilderInternal::generate_model_structure ( ) const

Definition at line 796 of file dae_builder_internal.cpp.

796  {
797  XmlNode r;
798  r.name = "ModelStructure";
799  // Add outputs
800  for (size_t i : outputs_) {
801  const Variable& y = variable(i);
802  XmlNode c;
803  c.name = "Output";
804  c.set_attribute("valueReference", static_cast<casadi_int>(y.value_reference));
805  c.set_attribute("dependencies", y.dependencies);
806  r.children.push_back(c);
807  }
808  // Add state derivatives
809  for (size_t i : indices(Category::X)) {
810  const Variable& xdot = variable(variable(i).der);
811  XmlNode c;
812  c.name = "ContinuousStateDerivative";
813  c.set_attribute("valueReference", static_cast<casadi_int>(xdot.value_reference));
814  c.set_attribute("dependencies", xdot.dependencies);
815  r.children.push_back(c);
816  }
817  // Add initial unknowns: Outputs
818  for (size_t i : outputs_) {
819  const Variable& y = variable(i);
820  XmlNode c;
821  c.name = "InitialUnknown";
822  c.set_attribute("valueReference", static_cast<casadi_int>(y.value_reference));
823  c.set_attribute("dependencies", y.dependencies);
824  r.children.push_back(c);
825  }
826  // Add initial unknowns: State derivative
827  for (size_t i : indices(Category::X)) {
828  const Variable& xdot = variable(variable(i).der);
829  XmlNode c;
830  c.name = "InitialUnknown";
831  c.set_attribute("valueReference", static_cast<casadi_int>(xdot.value_reference));
832  c.set_attribute("dependencies", xdot.dependencies);
833  r.children.push_back(c);
834  }
835  // Add event indicators
836  for (size_t i : event_indicators_) {
837  const Variable& zero = variable(i);
838  XmlNode c;
839  c.name = "EventIndicator";
840  c.set_attribute("valueReference", static_cast<casadi_int>(zero.value_reference));
841  c.set_attribute("dependencies", zero.dependencies);
842  r.children.push_back(c);
843  }
844  return r;
845 }

References casadi::XmlNode::children, casadi::Variable::dependencies, der(), event_indicators_, indices(), casadi::XmlNode::name, outputs_, casadi::XmlNode::set_attribute(), casadi::Variable::value_reference, variable(), and casadi::X.

Referenced by generate_model_description().

◆ generate_model_variables()

XmlNode casadi::DaeBuilderInternal::generate_model_variables ( ) const

Definition at line 787 of file dae_builder_internal.cpp.

787  {
788  XmlNode r;
789  r.name = "ModelVariables";
790  for (auto&& v : variables_) {
791  r.children.push_back(v->export_xml(*this));
792  }
793  return r;
794 }

References casadi::XmlNode::children, casadi::XmlNode::name, and variables_.

Referenced by generate_model_description().

◆ generate_wrapper()

std::string casadi::DaeBuilderInternal::generate_wrapper ( const std::string &  guid,
const CodeGenerator gen 
) const

Definition at line 975 of file dae_builder_internal.cpp.

976  {
977  // Create file
978  std::string wrapper_filename = name_ + "_wrap.c";
979  std::ofstream f;
980  CodeGenerator::file_open(f, wrapper_filename, false);
981 
982  // Add includes
983  f << "#include <fmi3Functions.h>\n"
984  << "#include \"" << name_ << ".h\"\n"
985  << "\n";
986 
987  // Total number of variables
988  f << "#define N_VAR " << n_variables() << "\n";
989 
990  // Memory size
991  f << "#define SZ_MEM " << n_mem() << "\n";
992 
993  // Work vectors sizes
994  size_t sz_arg, sz_res, sz_iw, sz_w;
995  gen.sz_work(sz_arg, sz_res, sz_iw, sz_w);
996  f << "#define SZ_ARG " << sz_arg << "\n"
997  << "#define SZ_RES " << sz_res << "\n"
998  << "#define SZ_IW " << sz_iw << "\n"
999  << "#define SZ_W " << sz_w << "\n";
1000 
1001  // Memory offsets
1002  f << "const size_t var_offset[N_VAR + 1] = {0";
1003  size_t mem_ind = 0;
1004  for (const Variable* v : variables_) {
1005  mem_ind += v->numel;
1006  f << ", " << mem_ind;
1007  }
1008  f << "};\n\n";
1009 
1010  // Start attributes
1011  f << "casadi_real start[SZ_MEM] = " << generate(start_all()) << ";\n\n";
1012 
1013  // States
1014  f << "#define N_X " << size(Category::X) << "\n"
1015  << "fmi3ValueReference x_vr[N_X] = " << generate(indices(Category::X)) << ";\n"
1016  << "\n";
1017 
1018  // Controls
1019  f << "#define N_U " << size(Category::U) << "\n"
1020  << "fmi3ValueReference u_vr[N_U] = " << generate(indices(Category::U)) << ";\n"
1021  << "\n";
1022 
1023  // Parameters
1024  f << "#define N_P " << size(Category::P) << "\n"
1025  << "fmi3ValueReference p_vr[N_P] = " << generate(indices(Category::P)) << ";\n"
1026  << "\n";
1027 
1028  // State derivatives
1029  std::vector<size_t> xdot;
1030  for (size_t v : indices(Category::X)) xdot.push_back(variable(v).der);
1031  f << "fmi3ValueReference xdot_vr[N_X] = " << generate(xdot) << ";\n"
1032  << "\n";
1033 
1034  // Outputs
1035  f << "#define N_Y " << outputs_.size() << "\n"
1036  << "fmi3ValueReference y_vr[N_Y] = " << generate(outputs_) << ";\n"
1037  << "\n";
1038 
1039  // Event indicators
1040  f << "#define N_ZERO " << event_indicators_.size() << "\n"
1041  << "fmi3ValueReference zero_vr[N_ZERO] = " << generate(event_indicators_) << ";\n"
1042  << "\n";
1043 
1044  // Memory structure
1046 
1047  // Finalize file
1048  CodeGenerator::file_close(f, false);
1049  return wrapper_filename;
1050 }
static std::string fmu_helpers(const std::string &modelname)
FMU helper functions.
static void file_close(std::ofstream &f, bool cpp)
Print file header.
static void file_open(std::ofstream &f, const std::string &name, bool cpp)
Print file header.
size_t n_mem() const
Length of memory for all variables.
static std::string generate(const std::vector< size_t > &v)
std::vector< double > start_all() const
Start values for all variables.

References der(), event_indicators_, casadi::CodeGenerator::file_close(), casadi::CodeGenerator::file_open(), casadi::CodeGenerator::fmu_helpers(), generate(), indices(), n_mem(), n_variables(), name_, outputs_, casadi::P, size(), start_all(), casadi::CodeGenerator::sz_work(), casadi::U, variable(), variables_, and casadi::X.

Referenced by export_fmu().

◆ get_der() [1/2]

MX casadi::DaeBuilderInternal::get_der ( size_t  ind) const
inline

Definition at line 358 of file dae_builder_internal.hpp.

358 {return variable(ind).get_der(*this);}
MX get_der(DaeBuilderInternal &self, bool may_allocate=true)

Referenced by der().

◆ get_der() [2/2]

MX casadi::DaeBuilderInternal::get_der ( size_t  ind,
bool  may_allocate = true 
)
inline

Definition at line 361 of file dae_builder_internal.hpp.

361  {
362  return variable(ind).get_der(*this, may_allocate);
363  }

◆ get_str()

std::string casadi::DaeBuilderInternal::get_str ( bool  more = false) const
inline

Definition at line 381 of file dae_builder_internal.hpp.

381  {
382  std::stringstream ss;
383  disp(ss, more);
384  return ss.str();
385  }
void disp(std::ostream &stream, bool more) const override
Print description.

◆ getCount()

Definition at line 60 of file generic_shared_internal.hpp.

186  {
187  return static_cast<const Internal*>(this)->count;
188  }

◆ has()

bool casadi::DaeBuilderInternal::has ( const std::string &  name) const

Definition at line 1589 of file dae_builder_internal.cpp.

1589  {
1590  return varind_.find(name) != varind_.end();
1591 }

References name(), and varind_.

Referenced by new_variable(), tearing_variables(), and unique_name().

◆ has_fun()

bool casadi::DaeBuilderInternal::has_fun ( const std::string &  name) const
protected

Definition at line 3981 of file dae_builder_internal.cpp.

3981  {
3982  for (const Function& f : fun_) {
3983  if (f.name()==name) return true;
3984  }
3985  return false;
3986 }

References fun_, and name().

Referenced by add_fun(), and fun().

◆ has_t()

bool casadi::DaeBuilderInternal::has_t ( ) const
protected

Extra doc: https://github.com/casadi/casadi/wiki/L_2bx

Definition at line 2663 of file dae_builder_internal.cpp.

2663  {
2664  return size(Category::T) > 0;
2665 }

References size(), and casadi::T.

Referenced by add(), and time().

◆ hess_sparsity()

Sparsity casadi::DaeBuilderInternal::hess_sparsity ( const std::vector< size_t > &  oind,
const std::vector< size_t > &  iind 
) const

Definition at line 4108 of file dae_builder_internal.cpp.

4109  {
4110  // Mark inputs
4111  std::vector<casadi_int> lookup(n_variables(), -1);
4112  for (size_t i = 0; i < iind.size(); ++i) lookup.at(iind[i]) = i;
4113  // Which variables enter as a nonlinear dependency in any variable in oind
4114  std::vector<bool> nonlin(iind.size(), false);
4115  // List of nonlinearly entering variables for the specific output
4116  std::vector<casadi_int> nonlin_list;
4117  // Rows and columns of the Hessian
4118  std::vector<casadi_int> row, col;
4119  // Loop over output variables
4120  for (casadi_int j = 0; j < oind.size(); ++j) {
4121  const Variable& v = variable(oind[j]);
4122  // Loop over dependencies
4123  for (size_t k = 0; k < v.dependencies.size(); ++k) {
4124  if (v.dependenciesKind.empty() || v.dependenciesKind.at(k) == DependenciesKind::DEPENDENT) {
4125  casadi_int i = lookup.at(v.dependencies[k]);
4126  if (i >= 0 && !nonlin.at(i)) {
4127  // Add to list
4128  nonlin_list.push_back(i);
4129  nonlin.at(i) = true;
4130  }
4131  }
4132  }
4133  // Add all combinations to sparsity pattern
4134  for (casadi_int k1 : nonlin_list) {
4135  for (casadi_int k2 : nonlin_list) {
4136  row.push_back(k1);
4137  col.push_back(k2);
4138  }
4139  }
4140  // If row/col vectors grow too large, remove duplicates
4141  if (col.size() > 2 * iind.size() * iind.size()) {
4142  Sparsity r = Sparsity::triplet(iind.size(), iind.size(), row, col);
4143  row = r.get_row();
4144  col = r.get_col();
4145  }
4146  // Reset nonlin, nonlin_list for next iteration
4147  for (casadi_int k : nonlin_list) nonlin[k] = false;
4148  nonlin_list.clear();
4149  }
4150  // Create sparsity pattern
4151  return Sparsity::triplet(iind.size(), iind.size(), row, col);
4152 }
static Sparsity triplet(casadi_int nrow, casadi_int ncol, const std::vector< casadi_int > &row, const std::vector< casadi_int > &col, std::vector< casadi_int > &mapping, bool invert_mapping)
Create a sparsity pattern given the nonzeros in sparse triplet form *.
Definition: sparsity.cpp:1127

References casadi::Variable::dependencies, casadi::Variable::dependenciesKind, casadi::DEPENDENT, casadi::Sparsity::get_col(), casadi::Sparsity::get_row(), n_variables(), casadi::Sparsity::triplet(), and variable().

Referenced by casadi::FmuInternal::init().

◆ hess_v_v_from_calls()

MX casadi::DaeBuilderInternal::hess_v_v_from_calls ( std::map< MXNode *, CallIO > &  call_nodes,
const std::vector< casadi_int > &  h_offsets 
) const
protected

Definition at line 2145 of file dae_builder_internal.cpp.

2146  {
2147  // Calculate all Hessian expressions
2148  for (auto&& call_ref : call_nodes) call_ref.second.calc_hess();
2149  // Row offsets in hess_v_v
2150  casadi_int voffset_begin = 0, voffset_end = 0, voffset_last = 0;
2151  // Vertical and horizontal slices of hess_v_v
2152  std::vector<MX> vblocks, hblocks;
2153  // All blocks for a block row
2154  std::map<size_t, MX> hess_brow;
2155  // Loop over block rows
2156  for (size_t vind1 = 0; vind1 < size(Category::W); ++vind1) {
2157  // Current element handled
2158  const MX& vref = var(Category::W, vind1);
2159  // Update vertical offset
2160  voffset_begin = voffset_end;
2161  voffset_end += vref.numel();
2162  // Collect all blocks for this block row
2163  hess_brow.clear();
2164  for (auto&& call_ref : call_nodes) {
2165  // Locate the specific index
2166  for (size_t iind1 = 0; iind1 < call_ref.second.v.size(); ++iind1) {
2167  if (call_ref.second.v.at(iind1) == vind1) {
2168  // Add contribution to block row
2169  for (size_t iind2 = 0; iind2 < call_ref.second.v.size(); ++iind2) {
2170  // Corresponding index in v
2171  size_t vind2 = call_ref.second.v[iind2];
2172  if (vind2 == size_t(-1)) continue;
2173  // Hessian contribution
2174  MX H_contr = call_ref.second.hess(iind1, iind2);
2175  // Insert new block or add to existing one
2176  auto it = hess_brow.find(vind2);
2177  if (it != hess_brow.end()) {
2178  it->second += H_contr;
2179  } else {
2180  hess_brow[vind2] = H_contr;
2181  }
2182  }
2183  // An index can only appear once
2184  break;
2185  }
2186  }
2187  }
2188  // If no blocks, skip row
2189  if (hess_brow.empty()) continue;
2190  // Add empty rows to vblocks, if any
2191  if (voffset_last != voffset_begin) {
2192  vblocks.push_back(MX(voffset_begin - voffset_last, h_offsets.back()));
2193  }
2194  // Collect horizontal blocks
2195  hblocks.clear();
2196  casadi_int hoffset = 0;
2197  for (auto e : hess_brow) {
2198  // Add empty block before Jacobian block, if needed
2199  if (hoffset < h_offsets.at(e.first))
2200  hblocks.push_back(MX(vref.numel(), h_offsets.at(e.first) - hoffset));
2201  // Add Jacobian block
2202  hblocks.push_back(e.second);
2203  // Update offsets
2204  hoffset = h_offsets.at(e.first + 1);
2205  }
2206  // Add trailing empty block, if needed
2207  if (hoffset < h_offsets.back())
2208  hblocks.push_back(MX(vref.numel(), h_offsets.back() - hoffset));
2209  // Add new block row to vblocks
2210  vblocks.push_back(horzcat(hblocks));
2211  // Keep track of the offset handled in jac_brow
2212  voffset_last = voffset_end;
2213  }
2214  // Add empty trailing row to vblocks, if any
2215  if (voffset_last != voffset_end) {
2216  vblocks.push_back(MX(voffset_end - voffset_last, h_offsets.back()));
2217  }
2218  // Return additional term in jac_vdef_v
2219  return vertcat(vblocks);
2220 }

References casadi::MX::find(), casadi::GenericMatrix< MatType >::numel(), size(), var(), and casadi::W.

Referenced by create().

◆ import_binding_equations()

void casadi::DaeBuilderInternal::import_binding_equations ( const XmlNode eqs)
protected

Definition at line 3707 of file dae_builder_internal.cpp.

3707  {
3708  if (debug_) {
3709  uout() << "== Structure before importing binding equations ==" << std::endl;
3710  disp(uout(), true);
3711  }
3712 
3713  // Loop over binding equations
3714  for (casadi_int i = 0; i < eqs.size(); ++i) {
3715  const XmlNode& eq = eqs[i];
3716  std::string eq_name = "beq_" + str(i);
3717  // Try to read the node
3718  try {
3719  // Get the variable and binding expression
3720  Variable& var = read_variable(eq[0]);
3721  if (eq[1].size() == 1) {
3722  // Set the binding equation
3723  var.bind = assign(var.name, read_expr(eq[1][0])).index;
3724  } else {
3725  // OpenModelica 1.17 occationally generates integer values without type specifier (bug?)
3726  casadi_assert(eq[1].size() == 0, "Not implemented");
3727  casadi_int val;
3728  eq[1].get(&val);
3729  casadi_warning(var.name + " has binding equation without type specifier: " + str(val));
3730  // Set the binding equation
3731  var.bind = assign(var.name, val).index;
3732  }
3733  // Add to list of dependent parameters
3734  indices(Category::D).push_back(var.index);
3735  } catch (std::exception& e) {
3736  casadi_error("Failed to read " + eq_name + ":" + str(e.what()));
3737  }
3738  }
3739 }
Variable & read_variable(const XmlNode &node, Attribute *att=0)
Read a variable.
MX read_expr(const XmlNode &node)
Read an equation.
std::string name() const
Get the name.
Definition: mx.cpp:762
std::ostream & uout()

References assign(), casadi::D, debug_, disp(), eq(), casadi::Variable::index, indices(), casadi::MX::name(), read_expr(), read_variable(), casadi::XmlNode::size(), size(), casadi::str(), casadi::uout(), and var().

Referenced by load_fmi_description().

◆ import_default_experiment()

void casadi::DaeBuilderInternal::import_default_experiment ( const XmlNode n)
protected

Definition at line 3275 of file dae_builder_internal.cpp.

3275  {
3276  start_time_ = n.attribute<double>("startTime", nan);
3277  stop_time_ = n.attribute<double>("stopTime", nan);
3278  tolerance_ = n.attribute<double>("tolerance", nan);
3279  step_size_ = n.attribute<double>("stepSize", nan);
3280 }

References casadi::XmlNode::attribute(), casadi::nan, start_time_, step_size_, stop_time_, and tolerance_.

Referenced by load_fmi_description().

◆ import_dynamic_equations()

void casadi::DaeBuilderInternal::import_dynamic_equations ( const XmlNode eqs)
protected

Definition at line 3741 of file dae_builder_internal.cpp.

3741  {
3742  if (debug_) {
3743  uout() << "== Structure before importing dynamic equations ==" << std::endl;
3744  disp(uout(), true);
3745  }
3746  // Add discrete states to x_
3747  // Note: Make generic, should also apply to regular FMUs
3748  for (Variable* v : variables_) {
3749  if (v->variability == Variability::DISCRETE) {
3750  categorize(v->index, Category::X);
3751  }
3752  }
3753  // Add equations
3754  for (casadi_int i = 0; i < eqs.size(); ++i) {
3755  const XmlNode& eq = eqs[i];
3756  std::string eq_name = "dyneq_" + str(i);
3757  // Try to read the node
3758  try {
3759  if (eq.name == "equ:When") { // When equation
3760  // Nodes for condition, equations
3761  const XmlNode& n_cond = eq["equ:Condition"];
3762  const XmlNode& n_equ = eq["equ:Equation"];
3763  // Consistency checks - only working for very simple expressions
3764  casadi_assert(n_cond.size() == 1, "Only one condition in when equation supported");
3765  casadi_assert(n_equ.size() == 1, "Only one equation in when equation supported");
3766  // Get expression for condition
3767  std::string cond_name = n_cond[0][0].attribute<std::string>("name");
3768  std::string when_prefix = "$whenCondition";
3769  cond_name = cond_name.substr(when_prefix.size());
3770  casadi_int ind = std::stoi(cond_name) - 1;
3771  // Left-hand-side and right-hand-side
3772  MX lhs, rhs;
3773  // Handle different types of equations
3774  if (n_equ[0].name == "exp:Sub") {
3775  // Assume equation is an assignment
3776  lhs = read_identifier(n_equ[0][0]);
3777  rhs = read_expr(n_equ[0][1]);
3778  when_.at(ind).second.push_back(assign(lhs.name(), rhs).index);
3779  } else if (n_equ[0].name == "exp:Reinit") {
3780  // Reinitialization
3781  lhs = read_identifier(n_equ[0][0]);
3782  rhs = read_expr(n_equ[0][1]);
3783  when_.at(ind).second.push_back(reinit(lhs.name(), rhs).index);
3784  } else {
3785  // Not implemented
3786  casadi_error(n_equ[0].name + " in when equation not supported");
3787  }
3788  } else if (eq.name == "equ:Equation") { // Residual equation
3789  // Consistency checks
3790  casadi_assert_dev(eq.size() == 1 && eq[0].name == "exp:Sub");
3791  // Skip if empty
3792  if (eq[0].size() == 0) {
3793  casadi_warning(eq_name + " is empty, ignored.");
3794  continue;
3795  }
3796  // Get the left-hand-sides and right-hand-sides
3797  const XmlNode& lhs = eq[0][0];
3798  const XmlNode& rhs = eq[0][1];
3799  // Right-hand-side is the binding equation
3800  MX beq = read_expr(rhs);
3801  // Left-hand-side is a variable or derivative
3802  std::string when_prefix = "$whenCondition";
3803  if (lhs.name == "exp:Der") {
3804  // Differential equation
3805  Variable& v = read_variable(lhs[0]);
3806  this->eq(der(v.v), beq, Dict());
3807  } else if (lhs.size() > 0
3808  && lhs[0].attribute<std::string>("name").rfind(when_prefix, 0) == 0) {
3809  // Get the index
3810  std::string cond_name = lhs[0].attribute<std::string>("name");
3811  if (debug_) {
3812  uout() << "Reading event indicator: " << cond_name << " := " << beq << std::endl;
3813  }
3814  cond_name = cond_name.substr(when_prefix.size());
3815  casadi_int ind = std::stoi(cond_name) - 1;
3816  // Ensure consequitive for now
3817  casadi_assert(ind == when_.size(), "Non-consequitive when conditions");
3818  // Create a when equation with no equations
3819  when(beq, {}, Dict());
3820  } else {
3821  // Regular equation
3822  Variable& v = read_variable(lhs);
3823  if (debug_) uout() << "Reading equation: " << v.name << " == " << beq << std::endl;
3824  this->eq(v.v, beq, Dict());
3825  }
3826  } else {
3827  casadi_error("Unknown dynamic equation type, got:" + eq.name);
3828  }
3829  } catch (std::exception& e) {
3830  casadi_error("Failed to read " + eq_name + ":" + str(e.what()));
3831  }
3832  }
3833 }
void when(const MX &cond, const std::vector< std::string > &eqs, const Dict &opts)
Add when equations.
MX read_identifier(const XmlNode &node)
Read an identifier expression.
Variable & reinit(const std::string &name, const MX &val)
Reinitialize a state inside when-equations.

References assign(), casadi::XmlNode::attribute(), categorize(), debug_, der(), casadi::DISCRETE, disp(), eq(), casadi::Variable::index, casadi::Variable::name, casadi::MX::name(), name(), casadi::XmlNode::name, read_expr(), read_identifier(), read_variable(), reinit(), casadi::XmlNode::size(), size(), casadi::str(), casadi::uout(), casadi::Variable::v, variables_, when(), when_, and casadi::X.

Referenced by load_fmi_description().

◆ import_initial_equations()

void casadi::DaeBuilderInternal::import_initial_equations ( const XmlNode eqs)
protected

Definition at line 3835 of file dae_builder_internal.cpp.

3835  {
3836  if (debug_) {
3837  uout() << "== Structure before importing initial equations ==" << std::endl;
3838  disp(uout(), true);
3839  }
3840 
3841  // Hack: OpenModelica XML may contain duplicate expressions, ignore these
3842  std::set<std::string> already_added;
3843  // Add equations
3844  for (casadi_int i = 0; i < eqs.size(); ++i) {
3845  const XmlNode& eq = eqs[i];
3846  // Try to read the node
3847  std::string eq_name = "initeq_" + str(i);
3848  try {
3849  if (eq.name == "equ:Equation") { // Residual equation
3850  // Consistency checks
3851  casadi_assert_dev(eq.size() == 1 && eq[0].name == "exp:Sub");
3852  // Ensure not empty
3853  if (eq[0].size() == 0) {
3854  casadi_warning(eq_name + " is empty, ignored.");
3855  continue;
3856  }
3857  // Get the left-hand-sides and right-hand-sides
3858  const XmlNode& lhs = eq[0][0];
3859  const XmlNode& rhs = eq[0][1];
3860 
3861  // Ignore initalizations of when equation indicators
3862  if (lhs.size() > 0
3863  && lhs[0].attribute<std::string>("name").rfind("$whenCondition", 0) == 0) {
3864  continue;
3865  }
3866 
3867  // Hack: Ignore expressions that just set a $PRE variable
3868  if (lhs.size() > 0 && lhs[0].attribute<std::string>("name") == "$PRE") {
3869  casadi_warning(eq_name + " defines a pre-variable, ignored");
3870  continue;
3871  }
3872  // Right-hand-side is the binding equation
3873  MX beq = read_expr(rhs);
3874  // Left-hand-side is a variable
3875  Variable& v = read_variable(lhs);
3876  // Hack: ignore initial equations for tunable parameters
3877  if (v.variability == Variability::TUNABLE) {
3878  casadi_warning(eq_name + " defines a tunable parameter, ignored")
3879  continue;
3880  }
3881  // Hack: avoid duplicate equations
3882  std::string eq_str = str(v.v) + " == " + str(beq);
3883  auto it = already_added.insert(eq_str);
3884  if (!it.second) {
3885  casadi_warning(eq_name + " duplicate of previous equation " + eq_str + ", ignored")
3886  continue;
3887  }
3888  // Set initial condition
3889  set_init(v.name, beq);
3890  } else {
3891  casadi_error("Unknown initial equation type, got:" + eq.name);
3892  }
3893  } catch (std::exception& e) {
3894  casadi_error("Failed to read " + eq_name + ":" + str(e.what()));
3895  }
3896  }
3897 }
void set_init(const std::string &name, const MX &init_rhs)
Set a initial equation.

References casadi::XmlNode::attribute(), debug_, disp(), eq(), casadi::Variable::name, read_expr(), read_variable(), set_init(), casadi::XmlNode::size(), size(), casadi::str(), casadi::TUNABLE, casadi::uout(), casadi::Variable::v, and casadi::Variable::variability.

Referenced by load_fmi_description().

◆ import_model_exchange()

void casadi::DaeBuilderInternal::import_model_exchange ( const XmlNode n)
protected

Definition at line 3282 of file dae_builder_internal.cpp.

3282  {
3283  // Read attributes
3284  provides_directional_derivatives_ = n.attribute<bool>(
3285  fmi_major_ >= 3 ? "providesDirectionalDerivatives" : "providesDirectionalDerivative", false);
3287  = n.attribute<bool>("providesAdjointDerivatives", false);
3288  model_identifier_ = n.attribute<std::string>("modelIdentifier");
3290  n.attribute<bool>("canBeInstantiatedOnlyOncePerProcess", false);
3291  // Get list of source files
3292  if (n.has_child("SourceFiles")) {
3293  for (const XmlNode& sf : n["SourceFiles"].children) {
3294  source_files_.push_back(sf.attribute<std::string>("name"));
3295  }
3296  }
3297 }
std::vector< std::string > source_files_

References casadi::XmlNode::attribute(), can_be_instantiated_only_once_per_process_, fmi_major_, casadi::XmlNode::has_child(), model_identifier_, provides_adjoint_derivatives_, provides_directional_derivatives_, and source_files_.

Referenced by load_fmi_description().

◆ import_model_structure()

void casadi::DaeBuilderInternal::import_model_structure ( const XmlNode n)
protected

Definition at line 3518 of file dae_builder_internal.cpp.

3518  {
3519  // Do not use the automatic selection of outputs based on output causality
3520  outputs_.clear();
3521 
3522  // Algebraic variables are handled internally in the FMU
3523  for (size_t i = 0; i < n_variables(); ++i) {
3524  Variable& v = variable(i);
3525  if (v.category == Category::Z) {
3526  // Mark as dependent variable, no need for an algebraic equation anymore
3527  categorize(v.index, Category::W);
3528  }
3529  }
3530 
3531  // Read structure
3532  if (fmi_major_ >= 3) {
3533  // Loop over ModelStructure elements
3534  for (casadi_int i = 0; i < n.size(); ++i) {
3535  const XmlNode& e = n[i];
3536  // Get a reference to the variable
3537  if (e.name == "Output") {
3538  // Get index
3539  outputs_.push_back(vrmap_.at(e.attribute<size_t>("valueReference")));
3540  // Corresponding variable
3541  Variable& v = variable(outputs_.back());
3542  // Get dependencies
3543  v.dependencies = read_dependencies(e);
3544  v.dependenciesKind = read_dependencies_kind(e, v.dependencies.size());
3545  // Mark interdependencies
3546  for (casadi_int d : v.dependencies) variable(d).dependency = true;
3547  } else if (e.name == "ContinuousStateDerivative") {
3548  // Get index
3549  derivatives_.push_back(vrmap_.at(e.attribute<size_t>("valueReference")));
3550  // Corresponding variable
3551  Variable& v = variable(derivatives_.back());
3552  // Add to list of states and derivative to list of dependent variables
3553  casadi_assert(v.parent >= 0, "Error processing derivative info for " + v.name);
3554  categorize(v.index, Category::W);
3555  categorize(v.parent, Category::X);
3556  // Map der field to derivative variable
3557  variable(v.parent).der = derivatives_.back();
3558  // Get dependencies
3559  v.dependencies = read_dependencies(e);
3560  v.dependenciesKind = read_dependencies_kind(e, v.dependencies.size());
3561  // Mark interdependencies
3562  for (casadi_int d : v.dependencies) variable(d).dependency = true;
3563  } else if (e.name == "ClockedState") {
3564  // Clocked state
3565  casadi_message("ClockedState not implemented, ignoring");
3566  } else if (e.name == "InitialUnknown") {
3567  // Get index
3568  initial_unknowns_.push_back(vrmap_.at(e.attribute<size_t>("valueReference")));
3569  // Get dependencies
3570  for (casadi_int d : read_dependencies(e)) variable(d).dependency = true;
3571  } else if (e.name == "EventIndicator") {
3572  // Event indicator
3573  event_indicators_.push_back(vrmap_.at(e.attribute<size_t>("valueReference")));
3575  } else {
3576  // Unknown
3577  casadi_error("Unknown ModelStructure element: " + e.name);
3578  }
3579  }
3580  } else {
3581  // Derivatives
3582  if (n.has_child("Derivatives")) {
3583  for (auto& e : n["Derivatives"].children) {
3584  // Get index
3585  derivatives_.push_back(e.attribute<casadi_int>("index", 0) - 1);
3586  // Corresponding variable
3587  Variable& v = variable(derivatives_.back());
3588  // Add to list of states and derivative to list of dependent variables
3589  casadi_assert(v.parent >= 0, "Error processing derivative info for " + v.name);
3590  categorize(v.index, Category::W);
3591  categorize(v.parent, Category::X);
3592  // Map der field to derivative variable
3593  variable(v.parent).der = derivatives_.back();
3594  }
3595  }
3596 
3597  // What if dependencies attributed is missing from Outputs,Derivatives?
3598  // Depends on x_ having been populated
3599  std::vector<casadi_int> default_dependencies;
3600  default_dependencies.insert(default_dependencies.begin(),
3601  indices(Category::T).begin(), indices(Category::T).end());
3602  default_dependencies.insert(default_dependencies.begin(),
3603  indices(Category::X).begin(), indices(Category::X).end());
3604  default_dependencies.insert(default_dependencies.begin(),
3605  indices(Category::U).begin(), indices(Category::U).end());
3606  std::sort(default_dependencies.begin(), default_dependencies.end());
3607 
3608  // FMI2 standard does not allow parameters to be included in dependencies attribute
3609  // (Table in section 2.2.8)
3610  // FMI3 standard does (Table in section 2.4.8)
3611  //
3612  // A uniform treatment is unconditionally add dense dependencies to all parameters
3613  // when loading FMI2
3614 
3615  std::vector<casadi_int> additional_dependencies;
3616  additional_dependencies.insert(additional_dependencies.begin(),
3617  indices(Category::P).begin(), indices(Category::P).end());
3618  std::vector<DependenciesKind> additional_dependencies_kind(size(Category::P),
3620 
3621  // Derivatives
3622  if (n.has_child("Derivatives")) {
3623  // Separate pass for dependencies
3624  for (auto& e : n["Derivatives"].children) {
3625  casadi_int index = e.attribute<casadi_int>("index", 0)-1;
3626 
3627  // Corresponding variable
3628  Variable& v = variable(index);
3629 
3630  // Get dependencies
3631  if (e.has_attribute("dependencies")) {
3632  v.dependencies = read_dependencies(e);
3633  } else {
3634  v.dependencies = default_dependencies;
3635  }
3636  v.dependenciesKind = read_dependencies_kind(e, v.dependencies.size());
3637 
3638  v.dependencies.insert(v.dependencies.end(),
3639  additional_dependencies.begin(), additional_dependencies.end());
3640  v.dependenciesKind.insert(v.dependenciesKind.end(),
3641  additional_dependencies_kind.begin(), additional_dependencies_kind.end());
3642 
3643  // Mark interdependencies
3644  for (casadi_int d : v.dependencies) variable(d).dependency = true;
3645  }
3646  }
3647  // Outputs
3648  if (n.has_child("Outputs")) {
3649  for (auto& e : n["Outputs"].children) {
3650  // Get index
3651  outputs_.push_back(e.attribute<casadi_int>("index", 0) - 1);
3652  // Corresponding variable
3653  Variable& v = variable(outputs_.back());
3654  // Get dependencies
3655  if (e.has_attribute("dependencies")) {
3656  v.dependencies = read_dependencies(e);
3657  } else {
3658  v.dependencies = default_dependencies;
3659  }
3660  v.dependenciesKind = read_dependencies_kind(e, v.dependencies.size());
3661 
3662  v.dependencies.insert(v.dependencies.end(),
3663  additional_dependencies.begin(), additional_dependencies.end());
3664  v.dependenciesKind.insert(v.dependenciesKind.end(),
3665  additional_dependencies_kind.begin(), additional_dependencies_kind.end());
3666 
3667  // Mark interdependencies
3668  for (casadi_int d : v.dependencies) variable(d).dependency = true;
3669  }
3670  }
3671  // What if dependencies is missing from InitialUnknowns?
3672  // Depends on x_ having been populated
3673  default_dependencies.clear();
3674  default_dependencies.insert(default_dependencies.begin(),
3675  indices(Category::T).begin(), indices(Category::T).end());
3676  for (const Variable* v : variables_) {
3677  if (v->initial == Initial::EXACT) default_dependencies.push_back(v->index);
3678  }
3679  default_dependencies.insert(default_dependencies.begin(),
3680  indices(Category::U).begin(), indices(Category::U).end());
3681  std::sort(default_dependencies.begin(), default_dependencies.end());
3682 
3683  // Initial unknowns
3684  if (n.has_child("InitialUnknowns")) {
3685  for (auto& e : n["InitialUnknowns"].children) {
3686  // Get index
3687  initial_unknowns_.push_back(e.attribute<casadi_int>("index", 0) - 1);
3688 
3689  std::vector<casadi_int> dependencies;
3690  // Get dependencies
3691  if (e.has_attribute("dependencies")) {
3692  dependencies = read_dependencies(e);
3693  } else {
3694  dependencies = default_dependencies;
3695  }
3696 
3697  dependencies.insert(dependencies.end(),
3698  additional_dependencies.begin(), additional_dependencies.end());
3699 
3700  // Get dependencies
3701  for (casadi_int d : dependencies) variable(d).dependency = true;
3702  }
3703  }
3704  }
3705 }
std::vector< size_t > derivatives_
std::vector< size_t > initial_unknowns_
std::unordered_map< unsigned int, size_t > vrmap_
Find of variable by value reference.
std::vector< DependenciesKind > read_dependencies_kind(const XmlNode &n, size_t ndep)
std::vector< casadi_int > read_dependencies(const XmlNode &n)
bool dependency
Do other expressions depend on this variable.

References casadi::XmlNode::attribute(), categorize(), casadi::Variable::category, casadi::Variable::dependencies, casadi::Variable::dependenciesKind, casadi::Variable::dependency, casadi::DEPENDENT, casadi::Variable::der, derivatives_, event_indicators_, casadi::EXACT, fmi_major_, casadi::XmlNode::has_child(), casadi::Variable::index, indices(), initial_unknowns_, n_variables(), casadi::Variable::name, casadi::XmlNode::name, number_of_event_indicators_, outputs_, casadi::P, casadi::Variable::parent, read_dependencies(), read_dependencies_kind(), casadi::XmlNode::size(), size(), casadi::T, casadi::U, variable(), variables_, vrmap_, casadi::W, casadi::X, and casadi::Z.

Referenced by load_fmi_description().

◆ import_model_variables()

void casadi::DaeBuilderInternal::import_model_variables ( const XmlNode modvars)
protected

Definition at line 3299 of file dae_builder_internal.cpp.

3299  {
3300  // Mapping from derivative variables to corresponding state variables, FMUX only
3301  std::vector<std::pair<std::string, std::string>> fmi1_der;
3302 
3303  // Add variables
3304  for (casadi_int i = 0; i < modvars.size(); ++i) {
3305  // Get a reference to the variable
3306  const XmlNode& vnode = modvars[i];
3307 
3308  // Name of variable
3309  std::string name = vnode.attribute<std::string>("name");
3310 
3311  // Handle variable categories (FMUX)
3312  if (fmi_major_ == 1 && vnode.has_child("VariableCategory")) {
3313  std::string variable_category = vnode["VariableCategory"].text;
3314  if (variable_category == "derivative") {
3315  // Create a new derivative variable
3316  std::string x_name = vnode["QualifiedName"][0].attribute<std::string>("name");
3317  fmi1_der.push_back(std::make_pair(x_name, name));
3318  }
3319  }
3320 
3321  // When conditions are reformulated into continuous zero-crossing functions
3322  if (fmi_major_ == 1 && name.rfind("$whenCondition", 0) == 0) continue;
3323 
3324  // Ignore duplicate variables
3325  if (varind_.find(name) != varind_.end()) {
3326  casadi_warning("Duplicate variable '" + name + "' ignored");
3327  continue;
3328  }
3329 
3330  // Type specific properties
3331  Dict opts;
3332  Type type = Type::NUMEL;
3333  casadi_int derivative = -1;
3334  if (fmi_major_ >= 3) {
3335  // FMI 3.0: Type information in the same node
3336  type = to_enum<Type>(vnode.name);
3337  switch (type) {
3338  case Type::FLOAT32: // fall-through
3339  case Type::FLOAT64:
3340  // Floating point valued variables
3341  opts["unit"] = vnode.attribute<std::string>("unit", "");
3342  opts["display_unit"] = vnode.attribute<std::string>("displayUnit", "");
3343  opts["min"] = vnode.attribute<double>("min", -inf);
3344  opts["max"] = vnode.attribute<double>("max", inf);
3345  opts["nominal"] = vnode.attribute<double>("nominal", 1.);
3346  opts["start"] = vnode.attribute<double>("start", 0.);
3347  derivative = vnode.attribute<casadi_int>("derivative", -1);
3348  break;
3349  case Type::INT8: // fall-through
3350  case Type::UINT8: // fall-through
3351  case Type::INT16: // fall-through
3352  case Type::UINT16: // fall-through
3353  case Type::INT32: // fall-through
3354  case Type::UINT32: // fall-through
3355  case Type::INT64: // fall-through
3356  case Type::UINT64: // fall-through
3357  // Integer valued variables
3358  opts["min"] = vnode.attribute<double>("min", -inf);
3359  opts["max"] = vnode.attribute<double>("max", inf);
3360  break;
3361  default:
3362  break;
3363  }
3364  } else {
3365  // FMI 1.0 / 2.0: Type information in a separate node
3366  if (vnode.has_child("Real")) {
3367  type = Type::FLOAT64;
3368  const XmlNode& props = vnode["Real"];
3369  opts["unit"] = props.attribute<std::string>("unit", "");
3370  opts["display_unit"] = props.attribute<std::string>("displayUnit", "");
3371  opts["min"] = props.attribute<double>("min", -inf);
3372  opts["max"] = props.attribute<double>("max", inf);
3373  opts["nominal"] = props.attribute<double>("nominal", 1.);
3374  opts["start"] = props.attribute<double>("start", 0.);
3375  derivative = props.attribute<casadi_int>("derivative", -1);
3376  } else if (vnode.has_child("Integer")) {
3377  type = Type::INT32;
3378  const XmlNode& props = vnode["Integer"];
3379  opts["min"] = props.attribute<double>("min", -inf);
3380  opts["max"] = props.attribute<double>("max", inf);
3381  } else if (vnode.has_child("Boolean")) {
3382  type = Type::BOOLEAN;
3383  } else if (vnode.has_child("String")) {
3384  type = Type::STRING;
3385  } else if (vnode.has_child("Enumeration")) {
3386  type = Type::ENUMERATION;
3387  } else {
3388  casadi_warning("Unknown type for " + name);
3389  }
3390  }
3391 
3392  // Description
3393  std::string description = vnode.attribute<std::string>("description", "");
3394 
3395  // Causality (FMI 1.0 -> FMI 2.0+)
3396  std::string causality_str = vnode.attribute<std::string>("causality", "local");
3397  if (fmi_major_ == 1 && causality_str == "internal") causality_str = "local";
3398  Causality causality = to_enum<Causality>(causality_str);
3399 
3400  // Variability (FMI 1.0 -> FMI 2.0+)
3401  std::string variability_str = vnode.attribute<std::string>("variability",
3403  if (fmi_major_ == 1 && variability_str == "parameter") variability_str = "fixed";
3404  Variability variability = to_enum<Variability>(variability_str);
3405 
3406  // Initial property
3408  std::string initial_str = vnode.attribute<std::string>("initial", "");
3409  if (!initial_str.empty()) {
3410  // Consistency check
3412  "The combination causality = '" + to_string(causality) + "', "
3413  "initial = '" + initial_str + "' is not allowed per the FMI specification.");
3414  initial = to_enum<Initial>(initial_str);
3415  }
3416 
3417  // If an input has a description that starts with "PARAMETER:",
3418  // treat it as a tunable parameter
3419  if (causality == Causality::INPUT && description.rfind("PARAMETER:", 0) == 0) {
3420  // Make tunable parameter
3423  }
3424 
3425  // Create the new variable
3426  opts["type"] = to_string(type);
3427  opts["initial"] = to_string(initial);
3428  opts["description"] = description;
3429  Variable& var = add(name, causality, variability, opts);
3430  if (debug_) uout() << "Added variable: " << var.name << std::endl;
3431 
3432  // Ignore time variable?
3434  categorize(var.index, Category::NUMEL);
3435  }
3436 
3437  // Do not permit discrete variables in x, for now
3439  categorize(var.index, Category::NUMEL);
3440  }
3441 
3442  // Assume all variables in the right-hand-sides for now
3443  // Prevents changing X to Q
3444  var.in_rhs = true;
3445  var.value_reference = static_cast<unsigned int>(vnode.attribute<casadi_int>("valueReference"));
3446  vrmap_[var.value_reference] = var.index;
3447  var.der_of = derivative;
3448  }
3449 
3450  // Set "parent" property using "derivative" attribute
3451  for (size_t i = 0; i < n_variables(); ++i) {
3452  Variable& v = variable(i);
3453  if (v.der_of >= 0) {
3454  if (fmi_major_ >= 3) {
3455  // Value reference is given: Find corresponding variable
3456  v.parent = vrmap_.at(static_cast<unsigned int>(v.der_of));
3457  } else if (fmi_major_ > 1) {
3458  // Variable given with index-1, make index 0
3459  v.parent = v.der_of - 1;
3460  }
3461  // TODO(@jaeandersson): Remove this redefinition of der_of
3462  v.der_of = v.parent;
3463  }
3464  }
3465 
3466  // Map derivative variables to corresponding state variables, FMUX only
3467  for (auto& p : fmi1_der) {
3468  // Add to list of derivatives
3469  Variable& v = variable(p.first);
3470  Variable& der_v = variable(p.second);
3471  categorize(der_v.index, Category::Z);
3472  der_v.der_of = der_v.parent = v.index;
3473  v.der = der_v.index;
3474  derivatives_.push_back(der_v.index);
3475  }
3476 }
static Initial default_initial(Causality causality, Variability variability)
Variability
Variability: FMI 2.0 specification, section 2.2.7 or FMI 3.0 specification, section 2....
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....

References add(), casadi::XmlNode::attribute(), casadi::BOOLEAN, categorize(), causality(), debug_, default_initial(), default_variability(), casadi::Variable::der, casadi::Variable::der_of, derivatives_, casadi::description(), casadi::DISCRETE, casadi::ENUMERATION, casadi::FLOAT32, casadi::FLOAT64, fmi_major_, casadi::XmlNode::has_child(), ignore_time_, casadi::INDEPENDENT, casadi::Variable::index, casadi::inf, casadi::INPUT, casadi::INT16, casadi::INT32, casadi::INT64, casadi::INT8, n_variables(), casadi::MX::name(), name(), casadi::XmlNode::name, casadi::NUMEL, casadi::PARAMETER, casadi::Variable::parent, casadi::XmlNode::size(), casadi::STRING, casadi::XmlNode::text, casadi::to_string(), casadi::TUNABLE, casadi::UINT16, casadi::UINT32, casadi::UINT64, casadi::UINT8, casadi::uout(), var(), variability(), variable(), varind_, vrmap_, and casadi::Z.

Referenced by load_fmi_description().

◆ indices() [1/2]

std::vector< size_t > & casadi::DaeBuilderInternal::indices ( Category  cat)

◆ indices() [2/2]

const std::vector< size_t > & casadi::DaeBuilderInternal::indices ( Category  cat) const

Definition at line 1358 of file dae_builder_internal.cpp.

1358  {
1359  return const_cast<DaeBuilderInternal*>(this)->indices(cat);
1360 }

References indices().

◆ init_lhs()

std::vector< MX > casadi::DaeBuilderInternal::init_lhs ( ) const
protected

Extra doc: https://github.com/casadi/casadi/wiki/L_2a7

Definition at line 2674 of file dae_builder_internal.cpp.

2674  {
2675  std::vector<MX> ret;
2676  ret.reserve(init_.size());
2677  for (size_t ind : init_) {
2678  ret.push_back(variable(ind).v);
2679  }
2680  return ret;
2681 }

References init_, and variable().

◆ init_rhs()

std::vector< MX > casadi::DaeBuilderInternal::init_rhs ( ) const
protected

Extra doc: https://github.com/casadi/casadi/wiki/L_2a8

Definition at line 2683 of file dae_builder_internal.cpp.

2683  {
2684  std::vector<MX> ret;
2685  ret.reserve(init_.size());
2686  for (size_t ind : init_) {
2687  ret.push_back(variable(ind).ieq);
2688  }
2689  return ret;
2690 }

References init_, and variable().

Referenced by set_init().

◆ initSingleton()

void casadi::GenericSharedInternal< SharedObject , SharedObjectInternal >::initSingleton ( )
inlineprotectedinherited

Called in the constructor of singletons to avoid that the counter reaches zero

Definition at line 71 of file generic_shared_internal.hpp.

71  {
72  casadi_assert_dev(static_cast<Internal*>(this)->count==0);
73  static_cast<Internal*>(this)->count++;
74  }

◆ input() [1/2]

std::vector< MX > casadi::DaeBuilderInternal::input ( Category  ind) const

Definition at line 1818 of file dae_builder_internal.cpp.

1818  {
1819  // Operation only permitted for input categories
1820  casadi_assert(is_input_category(ind),
1821  to_string(ind) + " is not an input category");
1822  // Get the expressions
1823  return var(indices(ind));
1824 }
bool is_input_category(Category cat)

References indices(), casadi::is_input_category(), casadi::to_string(), and var().

Referenced by dependent_fun(), input(), oracle(), and prune().

◆ input() [2/2]

std::vector< MX > casadi::DaeBuilderInternal::input ( const std::vector< Category > &  ind) const

Definition at line 1826 of file dae_builder_internal.cpp.

1826  {
1827  std::vector<MX> ret(ind.size());
1828  for (casadi_int i=0; i<ind.size(); ++i) {
1829  ret[i] = vertcat(input(ind[i]));
1830  }
1831  return ret;
1832 }

References input().

◆ insert()

void casadi::DaeBuilderInternal::insert ( std::vector< size_t > &  v,
size_t  ind 
) const
protected

Definition at line 2899 of file dae_builder_internal.cpp.

2899  {
2900  // Keep list ordered: Insert at location corresponding to model variable index
2901  size_t loc = v.size();
2902  for (size_t i = 0; i < v.size(); ++i) {
2903  if (variable(v[i]).index >= ind) {
2904  loc = i;
2905  break;
2906  }
2907  }
2908  v.insert(v.begin() + loc, ind);
2909 }

References variable().

Referenced by categorize(), and set_causality().

◆ iso_8601_time()

std::string casadi::DaeBuilderInternal::iso_8601_time ( )
static

Definition at line 4154 of file dae_builder_internal.cpp.

4154  {
4155  // Get current time
4156  auto now = std::chrono::system_clock::now();
4157  std::time_t tt = std::chrono::system_clock::to_time_t(now);
4158  auto local_tm = *std::localtime(&tt); // NOLINT(runtime/threadsafe_fn)
4159  // Convert to ISO 8601 (YYYY-MM-DDThh:mm:ssZ) format and return
4160  std::stringstream ss;
4161  ss << local_tm.tm_year + 1900 << '-'; // YYYY-
4162  ss << std::setfill('0') << std::setw(2) << local_tm.tm_mon + 1 << '-'; // MM-
4163  ss << std::setfill('0') << std::setw(2) << local_tm.tm_mday << 'T'; // DDT
4164  ss << std::setfill('0') << std::setw(2) << local_tm.tm_hour << ':'; // hh:
4165  ss << std::setfill('0') << std::setw(2) << local_tm.tm_min << ':'; // mm:
4166  ss << std::setfill('0') << std::setw(2) << local_tm.tm_sec << 'Z'; // ssZ
4167  return ss.str();
4168 }

Referenced by generate_model_description().

◆ jac_sparsity()

Sparsity casadi::DaeBuilderInternal::jac_sparsity ( const std::vector< size_t > &  oind,
const std::vector< size_t > &  iind 
) const

Definition at line 4086 of file dae_builder_internal.cpp.

4087  {
4088  // Mark inputs
4089  std::vector<casadi_int> lookup(n_variables(), -1);
4090  for (size_t i = 0; i < iind.size(); ++i)
4091  lookup.at(iind[i]) = i;
4092  // Sparsity pattern for the Jacobian block
4093  std::vector<casadi_int> row, col;
4094  // Loop over output nonzeros
4095  for (casadi_int j = 0; j < oind.size(); ++j) {
4096  for (casadi_int d : variable(oind[j]).dependencies) {
4097  casadi_int i = lookup.at(d);
4098  if (i >= 0) {
4099  row.push_back(j); // Note: May not be sorted in ascending order
4100  col.push_back(i);
4101  }
4102  }
4103  }
4104  // Assemble sparsity in triplet format
4105  return Sparsity::triplet(oind.size(), iind.size(), row, col);
4106 }

References n_variables(), casadi::Sparsity::triplet(), and variable().

Referenced by casadi::FmuInternal::init().

◆ jac_vdef_v_from_calls()

MX casadi::DaeBuilderInternal::jac_vdef_v_from_calls ( std::map< MXNode *, CallIO > &  call_nodes,
const std::vector< casadi_int > &  h_offsets 
) const
protected

Definition at line 2074 of file dae_builder_internal.cpp.

2075  {
2076  // Calculate all Jacobian expressions
2077  for (auto call_it = call_nodes.begin(); call_it != call_nodes.end(); ++call_it) {
2078  call_it->second.calc_jac();
2079  }
2080  // Row offsets in jac_vdef_v
2081  casadi_int voffset_begin = 0, voffset_end = 0, voffset_last = 0;
2082  // Vertical and horizontal slices of jac_vdef_v
2083  std::vector<MX> vblocks, hblocks;
2084  // All blocks for this block row
2085  std::map<size_t, MX> jac_brow;
2086  // Definitions of w
2087  std::vector<MX> wdef = output(OutputCategory::WDEF);
2088  // Collect all Jacobian blocks
2089  for (size_t vdefind = 0; vdefind < wdef.size(); ++vdefind) {
2090  // Current element handled
2091  const MX& vdefref = wdef.at(vdefind);
2092  // Update vertical offset
2093  voffset_begin = voffset_end;
2094  voffset_end += vdefref.numel();
2095  // Handle function call nodes
2096  if (vdefref.is_output()) {
2097  // Which output of the function are we calculating?
2098  casadi_int oind = vdefref.which_output();
2099  // Get function call node
2100  MX c = vdefref.dep(0);
2101  // Find data about inputs and outputs
2102  auto call_it = call_nodes.find(c.get());
2103  casadi_assert_dev(call_it != call_nodes.end());
2104  // Collect all blocks for this block row
2105  jac_brow.clear();
2106  for (casadi_int iind = 0; iind < call_it->second.arg.size(); ++iind) {
2107  size_t vind = call_it->second.v.at(iind);
2108  if (vind != size_t(-1)) {
2109  jac_brow[vind] = call_it->second.jac(oind, iind);
2110  }
2111  }
2112  // Add empty rows to vblocks, if any
2113  if (voffset_last != voffset_begin) {
2114  vblocks.push_back(MX(voffset_begin - voffset_last, h_offsets.back()));
2115  }
2116  // Collect horizontal blocks
2117  hblocks.clear();
2118  casadi_int hoffset = 0;
2119  for (auto e : jac_brow) {
2120  // Add empty block before Jacobian block, if needed
2121  if (hoffset < h_offsets.at(e.first))
2122  hblocks.push_back(MX(vdefref.numel(), h_offsets.at(e.first) - hoffset));
2123  // Add Jacobian block
2124  hblocks.push_back(e.second);
2125  // Update offsets
2126  hoffset = h_offsets.at(e.first + 1);
2127  }
2128  // Add trailing empty block, if needed
2129  if (hoffset < h_offsets.back())
2130  hblocks.push_back(MX(vdefref.numel(), h_offsets.back() - hoffset));
2131  // Add new block row to vblocks
2132  vblocks.push_back(horzcat(hblocks));
2133  // Keep track of the offset handled in jac_brow
2134  voffset_last = voffset_end;
2135  }
2136  }
2137  // Add empty trailing row to vblocks, if any
2138  if (voffset_last != voffset_end) {
2139  vblocks.push_back(MX(voffset_end - voffset_last, h_offsets.back()));
2140  }
2141  // Return additional term in jac_vdef_v
2142  return vertcat(vblocks);
2143 }

References casadi::MX::dep(), casadi::MX::find(), casadi::MX::get(), casadi::MX::is_output(), casadi::GenericMatrix< MatType >::numel(), output(), casadi::WDEF, and casadi::MX::which_output().

Referenced by create().

◆ lift()

void casadi::DaeBuilderInternal::lift ( bool  lift_shared,
bool  lift_calls 
)

Definition at line 1762 of file dae_builder_internal.cpp.

1762  {
1763  // Not tested if w is non-empty before
1764  if (size(Category::W) > 0) casadi_warning("'w' already has entries");
1765  // Expressions where the variables are also being used
1766  std::vector<MX> ex;
1767  for (size_t v : indices(Category::X)) ex.push_back(variable(variable(variable(v).der).bind).v);
1768  for (size_t v : indices(Category::Q)) ex.push_back(variable(variable(variable(v).der).bind).v);
1769  for (size_t v : residuals_) ex.push_back(variable(v).v);
1770  // Lift expressions
1771  std::vector<MX> new_w, new_wdef;
1772  Dict opts{{"lift_shared", lift_shared}, {"lift_calls", lift_calls},
1773  {"prefix", "w_"}, {"suffix", ""}, {"offset", static_cast<casadi_int>(size(Category::W))}};
1774  extract(ex, new_w, new_wdef, opts);
1775  // Register as dependent variables
1776  for (size_t i = 0; i < new_w.size(); ++i) {
1777  Variable& v = new_variable(new_w.at(i).name());
1778  v.v = new_w.at(i);
1779  Variable& v_beq = assign(v.name, new_wdef.at(i));
1780  v.bind = v_beq.index;
1781  indices(Category::W).push_back(v.index);
1782  }
1783  // Get expressions
1784  auto it = ex.begin();
1785  for (size_t v : indices(Category::X)) variable(variable(variable(v).der).bind).v = *it++;
1786  for (size_t v : indices(Category::Q)) variable(variable(variable(v).der).bind).v = *it++;
1787  for (size_t v : residuals_) variable(v).v = *it++;
1788  // Consistency check
1789  casadi_assert_dev(it == ex.end());
1790 }

References assign(), casadi::Variable::bind, der(), casadi::Variable::index, indices(), casadi::Variable::name, new_variable(), casadi::Q, residuals_, size(), casadi::Variable::v, variable(), casadi::W, and casadi::X.

◆ load_fmi_description()

void casadi::DaeBuilderInternal::load_fmi_description ( const std::string &  filename)

Import existing problem from FMI/XML

Definition at line 602 of file dae_builder_internal.cpp.

602  {
603  // Check if file exists
604  std::ifstream test(filename);
605  if (!test.good()) {
606  if (Filesystem::is_enabled()) {
607  casadi_error("Could not open file '" + filename + "'.");
608  } else {
609  casadi_error("Could not open file '" + filename + "'. "
610  "Note that, since CasADi was compiled without WITH_GHC_FILESYSTEM=ON, "
611  "passing fmu files to DaeBuilder is unsupported. "
612  "You could manually unzip the FMU file and "
613  "pass the path to the unzipped directory instead.");
614  }
615  }
616 
617  // Ensure no variables already
618  casadi_assert(n_variables() == 0, "Instance already has variables");
619 
620  // Parse XML file
621  XmlFile xml_file("tinyxml");
622  XmlNode fmi_desc = xml_file.parse(filename)[0]; // One child; fmiModelDescription
623 
624  // Read FMU version
625  fmi_version_ = fmi_desc.attribute<std::string>("fmiVersion", "");
626  if (fmi_version_.rfind("1.", 0) == 0) {
627  // FMI 1.0: Only for symbolic FMUs
628  fmi_major_ = 1;
629  } else if (fmi_version_.rfind("2.", 0) == 0) {
630  // FMI 2.0
631  fmi_major_ = 2;
632  } else if (fmi_version_.rfind("3.", 0) == 0) {
633  // FMI 3
634  fmi_major_ = 3;
635  } else {
636  casadi_error("Unknown FMI version: " + fmi_version_);
637  }
638 
639  // Read attributes
640  model_name_ = fmi_desc.attribute<std::string>("modelName", "");
641  instantiation_token_ = fmi_desc.attribute<std::string>(
642  fmi_major_ >= 3 ? "instantiationToken" : "guid");
643  description_ = fmi_desc.attribute<std::string>("description", "");
644  author_ = fmi_desc.attribute<std::string>("author", "");
645  copyright_ = fmi_desc.attribute<std::string>("copyright", "");
646  license_ = fmi_desc.attribute<std::string>("license", "");
647  generation_tool_ = fmi_desc.attribute<std::string>("generationTool", "");
648  generation_date_and_time_ = fmi_desc.attribute<std::string>("generationDateAndTime", "");
649  variable_naming_convention_ = fmi_desc.attribute<std::string>("variableNamingConvention", "");
650  if (fmi_major_ >= 3) {
651  number_of_event_indicators_ = 0; // In FMI 3: Obtain from binary
652  } else {
653  number_of_event_indicators_ = fmi_desc.attribute<casadi_int>("numberOfEventIndicators", 0);
654  }
655 
656  // Process DefaultExperiment
657  if (fmi_desc.has_child("DefaultExperiment")) {
658  import_default_experiment(fmi_desc["DefaultExperiment"]);
659  }
660 
661  // Process ModelExchange
662  bool has_model_exchange = false;
663  if (fmi_desc.has_child("ModelExchange")) {
664  has_model_exchange = true;
665  import_model_exchange(fmi_desc["ModelExchange"]);
666  }
667 
668  // Process ModelVariables
669  casadi_assert(fmi_desc.has_child("ModelVariables"), "Missing 'ModelVariables'");
670  import_model_variables(fmi_desc["ModelVariables"]);
671 
672  // Process model structure
673  if (fmi_desc.has_child("ModelStructure")) {
674  import_model_structure(fmi_desc["ModelStructure"]);
675  }
676 
677  // Is a symbolic representation available?
678  symbolic_ = false; // use DLL by default
679 
680  // Add symbolic binding equations
681  if (fmi_desc.has_child("equ:BindingEquations")) {
682  symbolic_ = true;
683  import_binding_equations(fmi_desc["equ:BindingEquations"]);
684  }
685 
686  // Add symbolic initial equations
687  if (fmi_desc.has_child("equ:InitialEquations")) {
688  symbolic_ = true;
689  import_initial_equations(fmi_desc["equ:InitialEquations"]);
690  }
691 
692  // Add symbolic dynamic equations
693  if (fmi_desc.has_child("equ:DynamicEquations")) {
694  symbolic_ = true;
695  import_dynamic_equations(fmi_desc["equ:DynamicEquations"]);
696  }
697 
698  // Ensure model equations are available, binary or symbolic
699  casadi_assert(symbolic_ || has_model_exchange,
700  "FMU must be of ModelExchange type or be symbolic (FMUX)");
701 }
void import_model_structure(const XmlNode &n)
void import_dynamic_equations(const XmlNode &eqs)
void import_model_variables(const XmlNode &modvars)
void import_model_exchange(const XmlNode &n)
void import_initial_equations(const XmlNode &eqs)
void import_default_experiment(const XmlNode &n)
void import_binding_equations(const XmlNode &eqs)
static bool is_enabled()
Definition: filesystem.cpp:83
std::string filename(const std::string &path)
Definition: ghc.cpp:55

References casadi::XmlNode::attribute(), author_, copyright_, description_, casadi::filename(), fmi_major_, fmi_version_, generation_date_and_time_, generation_tool_, casadi::XmlNode::has_child(), import_binding_equations(), import_default_experiment(), import_dynamic_equations(), import_initial_equations(), import_model_exchange(), import_model_structure(), import_model_variables(), instantiation_token_, casadi::Filesystem::is_enabled(), license_, model_name_, n_variables(), number_of_event_indicators_, casadi::XmlFile::parse(), symbolic_, and variable_naming_convention_.

◆ n_mem()

size_t casadi::DaeBuilderInternal::n_mem ( ) const

Definition at line 1604 of file dae_builder_internal.cpp.

1604  {
1605  size_t n = 0;
1606  for (const Variable* v : variables_) n += v->numel;
1607  return n;
1608 }

References variables_.

Referenced by generate_wrapper().

◆ n_variables()

size_t casadi::DaeBuilderInternal::n_variables ( ) const
inline

◆ name() [1/2]

std::vector< std::string > casadi::DaeBuilderInternal::name ( const std::vector< size_t > &  ind) const

Extra doc: https://github.com/casadi/casadi/wiki/L_2bw

Definition at line 3944 of file dae_builder_internal.cpp.

3944  {
3945  std::vector<std::string> r(ind.size());
3946  for (size_t i = 0; i < r.size(); ++i) r[i] = name(ind[i]);
3947  return r;
3948 }

References name().

◆ name() [2/2]

const std::string & casadi::DaeBuilderInternal::name ( size_t  ind) const

◆ new_variable()

Variable & casadi::DaeBuilderInternal::new_variable ( const std::string &  name,
const std::vector< casadi_int > &  dimension = {1},
const MX expr = MX() 
)

Definition at line 1610 of file dae_builder_internal.cpp.

1611  {
1612  // Name check
1613  casadi_assert(!name.empty(), "Name is empty string");
1614  // Try to find the component
1615  casadi_assert(!has(name), "Variable \"" + name + "\" already exists.");
1616  // Index of the variable
1617  size_t ind = n_variables();
1618  // Add to the map of all variables
1619  varind_[name] = ind;
1620  variables_.push_back(new Variable(ind, name, dimension, expr));
1621  // Clear cache
1622  clear_cache_ = true;
1623  // Return reference to new variable
1624  return *variables_.back();
1625 }
bool has(const std::string &name) const
Check if a particular variable exists.

References clear_cache_, has(), n_variables(), name(), Variable, variables_, and varind_.

Referenced by add(), lift(), and read_expr().

◆ oracle()

const Function & casadi::DaeBuilderInternal::oracle ( bool  sx = false,
bool  elim_w = false,
bool  lifted_calls = false 
) const

Definition at line 2234 of file dae_builder_internal.cpp.

2234  {
2235  casadi_assert(symbolic_, "DaeBuilder oracle only available if symbolic representation");
2236 
2237  // Clear cache now, if necessary
2238  if (clear_cache_) clear_cache();
2239  // Create an MX oracle, if needed
2240  if (oracle_[false][elim_w][lifted_calls].is_null()) {
2241  // Oracle function inputs and outputs
2242  std::vector<MX> f_in, f_out, v;
2243  std::vector<std::string> f_in_name, f_out_name;
2244  // Index for wdef
2245  casadi_int wdef_ind = -1;
2246  // Options consistency check
2247  casadi_assert(!(elim_w && lifted_calls), "Incompatible options");
2248  // Do we need to substitute out v
2249  bool subst_v = false;
2250  // Collect all DAE input variables
2251  for (Category cat : input_categories()) {
2252  v = input(cat);
2253  if (elim_w && cat == Category::W) {
2254  if (!v.empty()) subst_v = true;
2255  } else {
2256  if (v.empty()) {
2257  f_in.push_back(MX(0, 1));
2258  } else {
2259  f_in.push_back(vertcat(v));
2260  }
2261  f_in_name.push_back(to_string(cat));
2262  }
2263  }
2264 
2265  // Collect all DAE output variables
2266  for (OutputCategory cat : output_categories()) {
2267  f_out_name.push_back(to_string(cat));
2268  v = output(cat);
2269  if (v.empty()) {
2270  f_out.push_back(MX(0, 1));
2271  } else {
2272  if (cat == OutputCategory::WDEF) wdef_ind = f_out.size();
2273  f_out.push_back(vertcat(v));
2274  }
2275  }
2276  // Eliminate v from inputs
2277  if (subst_v) {
2278  // Dependent variable definitions
2279  std::vector<MX> wdef = output(OutputCategory::WDEF);
2280  // Perform in-place substitution
2281  substitute_inplace(var(Category::W), wdef, f_out, false);
2282  } else if (lifted_calls && wdef_ind >= 0) {
2283  // Dependent variable definitions
2284  std::vector<MX> wdef = output(OutputCategory::WDEF);
2285  // Remove references to call nodes
2286  for (MX& wdefref : wdef) {
2287  if (wdefref.is_output()) wdefref = MX::zeros(wdefref.sparsity());
2288  }
2289  // Save to oracle outputs
2290  f_out.at(wdef_ind) = vertcat(wdef);
2291  }
2292  // Create oracle
2293  oracle_[false][elim_w][lifted_calls]
2294  = Function("mx_oracle", f_in, f_out, f_in_name, f_out_name);
2295  }
2296  // Return MX oracle, if requested
2297  if (!sx) return oracle_[false][elim_w][lifted_calls];
2298  // Create SX oracle, if needed
2299  Function& sx_oracle = oracle_[true][elim_w][lifted_calls];
2300  if (sx_oracle.is_null()) sx_oracle = oracle_[false][elim_w][lifted_calls].expand("sx_oracle");
2301  // Return SX oracle reference
2302  return sx_oracle;
2303 }
void clear_cache() const
Problem structure has changed: Clear cache.
Function expand() const
Expand a function to SX.
Definition: function.cpp:308
std::vector< Category > input_categories()

References clear_cache(), clear_cache_, casadi::Function::expand(), input(), casadi::input_categories(), casadi::GenericShared< Shared, Internal >::is_null(), oracle_, output(), casadi::output_categories(), symbolic_, casadi::to_string(), var(), casadi::W, casadi::WDEF, and casadi::GenericMatrix< MX >::zeros().

Referenced by create(), transition(), and update_dependencies().

◆ output() [1/2]

std::vector< MX > casadi::DaeBuilderInternal::output ( const std::vector< OutputCategory > &  ind) const

Definition at line 1879 of file dae_builder_internal.cpp.

1879  {
1880  std::vector<MX> ret(ind.size());
1881  for (casadi_int i=0; i<ind.size(); ++i) {
1882  ret[i] = vertcat(output(ind[i]));
1883  }
1884  return ret;
1885 }

References output().

◆ output() [2/2]

std::vector< MX > casadi::DaeBuilderInternal::output ( OutputCategory  ind) const

Definition at line 1834 of file dae_builder_internal.cpp.

1834  {
1835  // If defined by index set
1836  switch (ind) {
1837  case OutputCategory::Y:
1838  return var(outputs_);
1839  case OutputCategory::ZERO:
1840  return var(event_indicators_);
1841  case OutputCategory::ALG:
1842  return var(residuals_);
1843  default: break;
1844  }
1845  // Otherwise: Defined by corresponding input category
1846  Category cat = input_category(ind);
1847  // Return object
1848  std::vector<MX> ret;
1849  ret.reserve(size(cat));
1850  // Handle different categories
1851  switch (ind) {
1852  case OutputCategory::ODE: // fall-through
1853  case OutputCategory::QUAD:
1854  // Differential state
1855  for (size_t v : indices(cat)) {
1856  const Variable& x = variable(v);
1857  if (x.der >= 0) {
1858  // Derivative variable
1859  ret.push_back(variable(variable(x.der).bind).v);
1860  } else if (x.variability == Variability::DISCRETE) {
1861  // Discrete variable - derivative is zero
1862  ret.push_back(MX::zeros(x.v.sparsity()));
1863  } else {
1864  // Missing ODE?
1865  casadi_error("Missing derivative for " + str(x.name));
1866  }
1867  }
1868  break;
1869  case OutputCategory::DDEF: // fall-through
1870  case OutputCategory::WDEF:
1871  // Defined by binding expression
1872  for (size_t d : indices(cat)) ret.push_back(variable(variable(d).bind).v);
1873  break;
1874  default: break;
1875  }
1876  return ret;
1877 }
Category input_category(OutputCategory cat)

References casadi::ALG, casadi::Variable::bind, casadi::DDEF, casadi::Variable::der, casadi::DISCRETE, event_indicators_, indices(), casadi::input_category(), casadi::Variable::name, casadi::ODE, outputs_, casadi::QUAD, residuals_, size(), casadi::MX::sparsity(), casadi::str(), casadi::Variable::v, var(), casadi::Variable::variability, variable(), casadi::WDEF, casadi::Y, casadi::ZERO, and casadi::GenericMatrix< MX >::zeros().

Referenced by add_fun(), create(), dependent_fun(), eliminate(), gather_eq(), jac_vdef_v_from_calls(), oracle(), output(), prune(), sort(), and transition().

◆ prune()

void casadi::DaeBuilderInternal::prune ( bool  prune_p,
bool  prune_u 
)

Definition at line 1381 of file dae_builder_internal.cpp.

1381  {
1382  // Function inputs and outputs
1383  std::vector<MX> f_in, f_out, v;
1384  std::vector<std::string> f_in_name, f_out_name;
1385  // Collect all DAE input variables with at least one entry, skip u
1386  for (Category cat : input_categories()) {
1387  if (prune_p && cat == Category::P) continue;
1388  if (prune_u && cat == Category::U) continue;
1389  v = input(cat);
1390  if (!v.empty()) {
1391  f_in.push_back(vertcat(v));
1392  f_in_name.push_back(to_string(cat));
1393  }
1394  }
1395  // Collect all DAE output variables with at least one entry
1396  for (OutputCategory cat : output_categories()) {
1397  v = output(cat);
1398  if (!v.empty()) {
1399  f_out.push_back(vertcat(v));
1400  f_out_name.push_back(to_string(cat));
1401  }
1402  }
1403  // Create a function
1404  Function f("prune_fcn", f_in, f_out, f_in_name, f_out_name);
1405  // Mark which variables are free
1406  std::vector<bool> free_variables(n_variables(), false);
1407  for (const std::string& s : f.get_free()) {
1408  auto it = varind_.find(s);
1409  casadi_assert(it != varind_.end(), "No such variable: \"" + s + "\".");
1410  free_variables.at(it->second) = true;
1411  }
1412  // Prune p
1413  if (prune_p) {
1414  size_t np = 0;
1415  for (size_t i = 0; i < size(Category::P); ++i) {
1416  if (!free_variables.at(indices(Category::P).at(i))) {
1417  indices(Category::P).at(np++) = indices(Category::P).at(i);
1418  }
1419  }
1420  indices(Category::P).resize(np);
1421  }
1422  // Prune u
1423  if (prune_u) {
1424  size_t nu = 0;
1425  std::vector<size_t>& u = indices(Category::U);
1426  for (size_t i = 0; i < u.size(); ++i) {
1427  if (!free_variables.at(u.at(i))) u.at(nu++) = u.at(i);
1428  }
1429  u.resize(nu);
1430  }
1431 }

References casadi::Function::get_free(), indices(), input(), casadi::input_categories(), n_variables(), output(), casadi::output_categories(), casadi::P, size(), casadi::to_string(), casadi::U, and varind_.

◆ quad()

std::vector<MX> casadi::DaeBuilderInternal::quad ( ) const
protected

◆ qualified_name()

std::string casadi::DaeBuilderInternal::qualified_name ( const XmlNode nn,
Attribute att = 0 
)
staticprotected

Definition at line 1635 of file dae_builder_internal.cpp.

1635  {
1636  // std::stringstream to assemble name
1637  std::stringstream qn;
1638  bool first_part = true;
1639  if (att) *att = Attribute::VALUE; // value attribute by default
1640 
1641  // Loop over name parts
1642  for (casadi_int i=0; i<nn.size(); ++i) {
1643  // Get the name part
1644  std::string np = nn[i].attribute<std::string>("name");
1645 
1646  // Check if an attribute
1647  if (np == "$START") {
1648  if (att) {
1649  *att = Attribute::START;
1650  } else {
1651  casadi_error("Ignoring attribute " + np);
1652  }
1653  continue;
1654  } else if (np == "$PRE") {
1655  casadi_warning("$PRE attribute has not been implemented, ignoring");
1656  continue;
1657  }
1658 
1659  // Add the name part to the variable name
1660  if (!first_part) qn << ".";
1661  qn << np;
1662 
1663  // Get the index, if any
1664  if (nn[i].size()>0) {
1665  casadi_int ind;
1666  nn[i]["exp:ArraySubscripts"]["exp:IndexExpression"]["exp:IntegerLiteral"].get(&ind);
1667  qn << "[" << ind << "]";
1668  }
1669 
1670  // Dot prefix if a additional parts
1671  first_part = false;
1672  }
1673 
1674  // Return the name
1675  return qn.str();
1676 }

References casadi::XmlNode::attribute(), casadi::XmlNode::get(), casadi::XmlNode::size(), size(), casadi::START, and casadi::VALUE.

Referenced by read_expr(), and read_variable().

◆ read_dependencies()

std::vector< casadi_int > casadi::DaeBuilderInternal::read_dependencies ( const XmlNode n)
protected

Definition at line 3478 of file dae_builder_internal.cpp.

3478  {
3479  // Default behaviour should be no known structure
3480  casadi_assert(n.has_attribute("dependencies"),
3481  "Default 'dependencies' not implemented");
3482  // Read list of dependencies
3483  std::vector<casadi_int> r = n.attribute<std::vector<casadi_int>>("dependencies", {});
3484  // Get corresponding variable index
3485  for (casadi_int& e : r) {
3486  if (fmi_major_ >= 3) {
3487  // Value reference is given
3488  e = vrmap_.at(static_cast<unsigned int>(e));
3489  } else {
3490  // Index-1 is given
3491  e--;
3492  }
3493  }
3494  // Return list of dependencies
3495  return r;
3496 }

References casadi::XmlNode::attribute(), fmi_major_, casadi::XmlNode::has_attribute(), and vrmap_.

Referenced by import_model_structure().

◆ read_dependencies_kind()

std::vector< DependenciesKind > casadi::DaeBuilderInternal::read_dependencies_kind ( const XmlNode n,
size_t  ndep 
)
protected

Definition at line 3498 of file dae_builder_internal.cpp.

3499  {
3500  // Quick return if node not provided
3501  if (!n.has_attribute("dependenciesKind")) {
3502  // No structure known, assume general dependency
3503  return std::vector<DependenciesKind>(ndep, DependenciesKind::DEPENDENT);
3504  } else {
3505  // Read list of strings
3506  auto dk_str = n.attribute<std::vector<std::string>>("dependenciesKind");
3507  // Make sure expected length
3508  casadi_assert(dk_str.size() == ndep, "Mismatching 'dependenciesKind'");
3509  // Convert to enums
3510  std::vector<DependenciesKind> r(ndep);
3511  for (size_t i = 0; i < ndep; ++i) {
3512  r[i] = to_enum<DependenciesKind>(dk_str[i]);
3513  }
3514  return r;
3515  }
3516 }

References casadi::XmlNode::attribute(), casadi::DEPENDENT, and casadi::XmlNode::has_attribute().

Referenced by import_model_structure().

◆ read_expr()

MX casadi::DaeBuilderInternal::read_expr ( const XmlNode node)
protected

Definition at line 1077 of file dae_builder_internal.cpp.

1077  {
1078  try {
1079  const std::string& fullname = node.name;
1080 
1081  if (fullname == "fun:If") {
1082  // Expression for condition, if and else
1083  const XmlNode& cond = node["fun:Condition"];
1084  const XmlNode& then_stmt = node["fun:Statements"];
1085  const XmlNode& else_stmt = node["fun:Else"];
1086  // Only scalar expression implemented
1087  casadi_assert(cond.size() == 1, "Only one condition in if expression supported");
1088  casadi_assert(then_stmt.size() == 1, "Only one then statement in if expression supported");
1089  casadi_assert(else_stmt.size() == 1, "Only one else statement in if expression supported");
1090  return if_else(read_expr(cond[0]), read_expr(then_stmt[0]), read_expr(else_stmt[0]));
1091  }
1092 
1093  if (fullname.find("exp:")== std::string::npos) {
1094  casadi_error("DaeBuilderInternal::read_expr: unknown - expression is supposed to "
1095  "start with 'exp:' , got " + fullname);
1096  }
1097 
1098  // Chop the 'exp:'
1099  std::string name = fullname.substr(4);
1100 
1101  // The switch below is alphabetical, and can be thus made more efficient,
1102  // for example by using a switch statement of the first three letters,
1103  // if it would ever become a bottleneck
1104  if (name=="Add") {
1105  return read_expr(node[0]) + read_expr(node[1]);
1106  } else if (name=="Acos") {
1107  return acos(read_expr(node[0]));
1108  } else if (name=="Asin") {
1109  return asin(read_expr(node[0]));
1110  } else if (name=="Atan") {
1111  return atan(read_expr(node[0]));
1112  } else if (name=="Cos") {
1113  return cos(read_expr(node[0]));
1114  } else if (name=="Der") {
1115  return variable(read_variable(node[0]).der_of).v;
1116  } else if (name=="Div") {
1117  return read_expr(node[0]) / read_expr(node[1]);
1118  } else if (name=="Exp") {
1119  return exp(read_expr(node[0]));
1120  } else if (name=="Identifier") {
1121  return read_identifier(node);
1122  } else if (name=="IntegerLiteral" || name=="BooleanLiteral") {
1123  casadi_int val;
1124  node.get(&val);
1125  return val;
1126  } else if (name=="Instant") {
1127  double val;
1128  node.get(&val);
1129  return val;
1130  } else if (name=="Log") {
1131  return log(read_expr(node[0]));
1132  } else if (name=="Not") { // Logical not
1133  return !read_expr(node[0]);
1134  } else if (name=="LogLeq") { // Logical less than equal
1135  return read_expr(node[0]) <= read_expr(node[1]);
1136  } else if (name=="LogGeq") { // Logical greater than equal
1137  return read_expr(node[0]) >= read_expr(node[1]);
1138  } else if (name=="LogLt") { // Logical less than
1139  return read_expr(node[0]) < read_expr(node[1]);
1140  } else if (name=="LogGt") { // Logical greater than
1141  return read_expr(node[0]) > read_expr(node[1]);
1142  } else if (name=="Max") {
1143  return fmax(read_expr(node[0]), read_expr(node[1]));
1144  } else if (name=="Min") {
1145  return fmin(read_expr(node[0]), read_expr(node[1]));
1146  } else if (name=="Mul") { // Multiplication
1147  return read_expr(node[0]) * read_expr(node[1]);
1148  } else if (name=="Neg") {
1149  return -read_expr(node[0]);
1150  } else if (name=="NoEvent") {
1151  // NOTE: This is a workaround, we assume that whenever NoEvent occurs,
1152  // what is meant is a switch
1153  casadi_int n = node.size();
1154 
1155  // Default-expression
1156  MX ex = read_expr(node[n-1]);
1157 
1158  // Evaluate ifs
1159  for (casadi_int i=n-3; i>=0; i -= 2) {
1160  ex = if_else(read_expr(node[i]), read_expr(node[i+1]), ex);
1161  }
1162 
1163  return ex;
1164  } else if (name=="Pow") {
1165  return pow(read_expr(node[0]), read_expr(node[1]));
1166  } else if (name=="Pre") {
1167  casadi_warning("Ignoring pre attribute");
1168  return read_expr(node[0]);
1169  } else if (name=="RealLiteral") {
1170  double val;
1171  node.get(&val);
1172  return val;
1173  } else if (name=="Sin") {
1174  return sin(read_expr(node[0]));
1175  } else if (name=="Sqrt") {
1176  return sqrt(read_expr(node[0]));
1177  } else if (name=="StringLiteral") {
1178  casadi_error(node.text);
1179  } else if (name=="Sub") {
1180  return read_expr(node[0]) - read_expr(node[1]);
1181  } else if (name=="Tan") {
1182  return tan(read_expr(node[0]));
1183  } else if (name=="Time") {
1184  return var(indices(Category::T).at(0));
1185  } else if (name=="TimedVariable") {
1186  return read_variable(node[0]).v;
1187  } else if (name=="FunctionCall") {
1188  // Get the name of the function
1189  std::string fname = qualified_name(node["exp:Name"]);
1190  casadi_warning("Function call to '" + fname + "' incomplete");
1191  // Collect the arguments
1192  const XmlNode& args = node["exp:Arguments"];
1193  std::vector<MX> farg(args.size());
1194  for (casadi_int i = 0; i < args.size(); ++i) {
1195  // Lift input arguments
1196  Variable& v = new_variable("w_" + str(size(Category::W)));
1197  // Add to list of variables
1198  indices(Category::W).push_back(v.index);
1199  // Set binding expression
1200  Variable& v_beq = assign(v.name, read_expr(args[i]));
1201  v.bind = v_beq.index;
1202  // Add to list of function arguments
1203  farg[i] = v.v;
1204  }
1205  // Return argument (scalar for now)
1206  Variable& r = new_variable("w_" + str(size(Category::W)));
1207  // Add to list of variables
1208  indices(Category::W).push_back(r.index);
1209  // Return output variable
1210  return r.v;
1211  } else if (name=="Array") {
1212  // Array of arguments
1213  std::vector<MX> v(node.size());
1214  for (casadi_int i = 0; i < v.size(); ++i) v[i] = read_expr(node[i]);
1215  return vertcat(v);
1216  }
1217 
1218  // throw error if reached this point
1219  casadi_error("Unknown node: " + name);
1220  } catch (std::exception& e) {
1221  THROW_ERROR_NODE("read_expr", node, e.what());
1222  return {};
1223  }
1224 }
static std::string qualified_name(const XmlNode &nn, Attribute *att=0)
Get the qualified name.
double if_else(double x, double y, double z)
Definition: calculus.hpp:290

References assign(), casadi::Variable::bind, casadi::XmlNode::get(), casadi::if_else(), casadi::Variable::index, indices(), casadi::Variable::name, name(), casadi::XmlNode::name, new_variable(), qualified_name(), read_identifier(), read_variable(), casadi::XmlNode::size(), size(), casadi::str(), casadi::T, casadi::XmlNode::text, casadi::Variable::v, var(), variable(), and casadi::W.

Referenced by import_binding_equations(), import_dynamic_equations(), and import_initial_equations().

◆ read_identifier()

MX casadi::DaeBuilderInternal::read_identifier ( const XmlNode node)
protected

Definition at line 1064 of file dae_builder_internal.cpp.

1064  {
1065  Attribute att; // attribute
1066  Variable& v = read_variable(node, &att);
1067  if (att == Attribute::VALUE) {
1068  return v.v;
1069  } else if (att == Attribute::START) {
1070  return v.start;
1071  } else {
1072  casadi_error("Cannot read attribute " + to_string(att));
1073  return MX();
1074  }
1075 }

References read_variable(), casadi::START, casadi::Variable::start, casadi::to_string(), casadi::Variable::v, and casadi::VALUE.

Referenced by import_dynamic_equations(), and read_expr().

◆ read_variable()

Variable & casadi::DaeBuilderInternal::read_variable ( const XmlNode node,
Attribute att = 0 
)
protected

Definition at line 1052 of file dae_builder_internal.cpp.

1052  {
1053  try {
1054  // Qualified name
1055  std::string qn = qualified_name(node, att);
1056 
1057  return variable(qn);
1058  } catch (std::exception& e) {
1059  THROW_ERROR_NODE("read_variable", node, e.what());
1060  //return {};
1061  }
1062 }

References qualified_name(), and variable().

Referenced by import_binding_equations(), import_dynamic_equations(), import_initial_equations(), read_expr(), and read_identifier().

◆ reinit()

Variable & casadi::DaeBuilderInternal::reinit ( const std::string &  name,
const MX val 
)
protected

Definition at line 3227 of file dae_builder_internal.cpp.

3227  {
3228  // Create a unique name for the reinit variable
3229  std::string reinit_name = unique_name("__reinit__" + name + "__");
3230  // Add a new dependent variable defined by val
3231  Variable& v = add(reinit_name, Causality::LOCAL, Variability::CONTINUOUS, val, Dict());
3232  // Classify as a defined variable
3233  categorize(v.index, Category::CALCULATED);
3234  v.parent = variable(name).index;
3235  // Return the variable name
3236  return v;
3237 }

References add(), casadi::CALCULATED, categorize(), casadi::CONTINUOUS, casadi::Variable::index, casadi::LOCAL, name(), casadi::Variable::parent, unique_name(), and variable().

Referenced by import_dynamic_equations().

◆ remove()

void casadi::DaeBuilderInternal::remove ( std::vector< size_t > &  v,
size_t  ind 
) const
protected

Definition at line 2911 of file dae_builder_internal.cpp.

2911  {
2912  for (auto it = v.begin(); it != v.end(); ++it) {
2913  if (*it == ind) {
2914  v.erase(it);
2915  return;
2916  }
2917  }
2918  casadi_error("Variable not found");
2919 }

Referenced by categorize(), and set_causality().

◆ reorder() [1/2]

void casadi::DaeBuilderInternal::reorder ( Category  cat,
const std::vector< size_t > &  v 
)

Definition at line 1362 of file dae_builder_internal.cpp.

1362  {
1363  return reorder(to_string(cat), indices(cat), v);
1364 }
void reorder(Category cat, const std::vector< size_t > &v)
Reorder variables in a category.

References indices(), and casadi::to_string().

◆ reorder() [2/2]

void casadi::DaeBuilderInternal::reorder ( const std::string &  n,
std::vector< size_t > &  ind,
const std::vector< size_t > &  v 
) const

Definition at line 1366 of file dae_builder_internal.cpp.

1367  {
1368  // Check if the sizes match
1369  casadi_assert(ind.size() == v.size(), "Cannot reorder " + n + ": "
1370  + str(v.size()) + " elements provided for " + str(ind.size()) + " components.");
1371  // Mark elements to be set
1372  std::vector<bool> set(n_variables(), false);
1373  for (size_t i : v) set.at(i) = true;
1374  // Make sure all elements are present
1375  for (size_t i : ind) casadi_assert(set.at(i), "Cannot reorder " + n + ": "
1376  + variable(i).name + " is missing.");
1377  // Set the new order
1378  std::copy(v.begin(), v.end(), ind.begin());
1379 }

References n_variables(), name(), casadi::str(), and variable().

◆ reset()

void casadi::DaeBuilderInternal::reset ( )
protected

Definition at line 3996 of file dae_builder_internal.cpp.

3996  {
3997  for (Variable* v : variables_) {
3998  std::fill(v->value.begin(), v->value.end(), nan);
3999  v->stringvalue = std::string();
4000  }
4001 }

References casadi::nan, and variables_.

◆ sanity_check()

void casadi::DaeBuilderInternal::sanity_check ( ) const

Definition at line 1627 of file dae_builder_internal.cpp.

1627  {
1628  // Time
1629  if (size(Category::T) > 0) {
1630  casadi_assert(size(Category::T) == 1, "At most one time variable allowed");
1631  casadi_assert(variable(Category::T, 0).v.is_scalar(), "Non-scalar time t");
1632  }
1633 }

References size(), casadi::T, and variable().

Referenced by add_lc(), and disp().

◆ set_attribute() [1/2]

void casadi::DaeBuilderInternal::set_attribute ( Attribute  a,
const std::string &  name,
double  val 
)
protected

Set by attribute name

Definition at line 4023 of file dae_builder_internal.cpp.

4023  {
4024  variable(name).set_attribute(a, val);
4025 }
void set_attribute(Attribute a, double val)

References name(), casadi::Variable::set_attribute(), and variable().

◆ set_attribute() [2/2]

void casadi::DaeBuilderInternal::set_attribute ( Attribute  a,
const std::vector< std::string > &  name,
const std::vector< double > &  val 
)
protected

Set by attribute name

Definition at line 4027 of file dae_builder_internal.cpp.

4028  {
4029  if (name.size() == val.size()) {
4030  // One scalar value per variable
4031  for (size_t k = 0; k < name.size(); ++k) variable(name[k]).set_attribute(a, val[k]);
4032  } else if (val.size() == size(a, name)) {
4033  // One vector slice per variable
4034  auto val_it = val.begin();
4035  for (size_t k = 0; k < name.size(); ++k) {
4036  Variable& v = variable(name[k]);
4037  auto val_next = val_it + v.size(a);
4038  v.set_attribute(a, std::vector<double>(val_it, val_next));
4039  val_it = val_next;
4040  }
4041  } else {
4042  casadi_error("Cannot set attribute " + to_string(a) + ": Argument is of length " +
4043  str(val.size()) + ", expected number of elements (" + str(size(a, name))
4044  + ") or number of variables (" + str(name.size()) + ")");
4045  }
4046 }

References name(), casadi::Variable::set_attribute(), casadi::Variable::size(), size(), casadi::str(), casadi::to_string(), and variable().

◆ set_category()

void casadi::DaeBuilderInternal::set_category ( size_t  ind,
Category  cat 
)
protected

Definition at line 3018 of file dae_builder_internal.cpp.

3018  {
3019  // Get variable reference
3020  Variable& v = variable(ind);
3021  // Quick return if same category
3022  if (v.category == cat) return;
3023  // Update category: See comment for public interface
3024  switch (cat) {
3025  case Category::U:
3026  if (v.category == Category::P || v.category == Category::C) {
3027  return set_variability(v.index, Variability::CONTINUOUS);
3028  }
3029  break;
3030  case Category::P:
3031  if (v.category == Category::U || v.category == Category::C) {
3032  return set_variability(v.index, Variability::TUNABLE);
3033  }
3034  break;
3035  case Category::C:
3036  if (v.category == Category::U || v.category == Category::P) {
3037  return set_variability(v.index, Variability::FIXED);
3038  }
3039  break;
3040  case Category::X:
3041  if (v.category == Category::Q && !v.in_rhs) {
3042  return categorize(v.index, Category::X);
3043  }
3044  break;
3045  case Category::Q:
3046  if (v.category == Category::X) {
3047  return categorize(v.index, Category::Q);
3048  }
3049  break;
3050  default:
3051  break;
3052  }
3053  // Failure if reached this point
3054  casadi_error("Cannot change category of " + v.name + " from '"
3055  + to_string(v.category) + "' to '" + to_string(cat) + "'");
3056 }
void set_variability(size_t ind, Variability variability)
Set variability.

References casadi::C, categorize(), casadi::Variable::category, casadi::CONTINUOUS, casadi::FIXED, casadi::Variable::in_rhs, casadi::Variable::index, casadi::Variable::name, casadi::P, casadi::Q, set_variability(), casadi::to_string(), casadi::TUNABLE, casadi::U, variable(), and casadi::X.

Referenced by eliminate().

◆ set_causality()

void casadi::DaeBuilderInternal::set_causality ( size_t  ind,
Causality  causality 
)
protected

Definition at line 2925 of file dae_builder_internal.cpp.

2925  {
2926  // Get variable reference
2927  Variable& v = variable(ind);
2928  // Quick return if same causality
2929  if (v.causality == causality) return;
2930  // Handle permitted changes
2931  if (v.causality == Causality::LOCAL && causality == Causality::OUTPUT) {
2932  // Add to list of outputs
2933  insert(outputs_, v.index);
2934  } else if (v.causality == Causality::OUTPUT && causality == Causality::LOCAL) {
2935  // Remove from list of outputs
2936  remove(outputs_, v.index);
2937  } else {
2938  // Not possible
2939  casadi_error("Cannot change causality of " + v.name + " which is of category '"
2940  + to_string(v.category) + "'");
2941  }
2942  // Success: Update causality
2943  v.causality = causality;
2944  // The oracle would need to be regenerated after changes to the categorization
2945  clear_cache_ = true;
2946 }

References casadi::Variable::category, casadi::Variable::causality, causality(), clear_cache_, casadi::Variable::index, insert(), casadi::LOCAL, casadi::Variable::name, casadi::OUTPUT, outputs_, remove(), casadi::to_string(), and variable().

◆ set_init()

void casadi::DaeBuilderInternal::set_init ( const std::string &  name,
const MX init_rhs 
)
protected

Definition at line 3239 of file dae_builder_internal.cpp.

3239  {
3240  // Find the algebraic variable
3241  Variable& v = variable(name);
3242  // If variable already has an initial binding equation, remove it
3243  if (!v.ieq.is_empty()) {
3244  // Remove from list of initial equations
3245  auto old_loc = std::find(init_.begin(), init_.end(), v.index);
3246  if (old_loc == init_.end()) casadi_error("Corrupted list of initial equations");
3247  init_.erase(old_loc);
3248  v.ieq = MX();
3249  }
3250  // If right-hand-side is empty, just erase
3251  if (init_rhs.is_empty()) return;
3252 
3253  // Make sure not already in list of initial equations
3254  if (std::find(init_.begin(), init_.end(), v.index) != init_.end()) {
3255  casadi_error("Initial equation for " + name + " has already been set");
3256  }
3257  // Add to list of initial equations
3258  init_.push_back(v.index);
3259  v.ieq = init_rhs;
3260 }
std::vector< MX > init_rhs() const
Initial conditions, right-hand-side.

References casadi::Variable::ieq, casadi::Variable::index, init_, init_rhs(), casadi::GenericMatrix< MatType >::is_empty(), name(), and variable().

Referenced by import_initial_equations().

◆ set_string_attribute() [1/2]

void casadi::DaeBuilderInternal::set_string_attribute ( Attribute  a,
const std::string &  name,
const std::string &  val 
)
protected

Set by attribute name (string-valued)

Definition at line 4069 of file dae_builder_internal.cpp.

4070  {
4071  variable(name).set_attribute(a, val);
4072 }

References name(), casadi::Variable::set_attribute(), and variable().

◆ set_string_attribute() [2/2]

void casadi::DaeBuilderInternal::set_string_attribute ( Attribute  a,
const std::vector< std::string > &  name,
const std::vector< std::string > &  val 
)
protected

Set by attribute name (string-valued)

Definition at line 4074 of file dae_builder_internal.cpp.

4075  {
4076  casadi_assert(name.size() == val.size(), "Dimension mismatch");
4077  for (size_t k = 0; k < name.size(); ++k) variable(name[k]).set_attribute(a, val[k]);
4078 }

References name(), casadi::Variable::set_attribute(), and variable().

◆ set_variability()

void casadi::DaeBuilderInternal::set_variability ( size_t  ind,
Variability  variability 
)
protected

Definition at line 2952 of file dae_builder_internal.cpp.

2952  {
2953  // Get variable reference
2954  Variable& v = variable(ind);
2955  // Quick return if same variability
2956  if (v.variability == variability) return;
2957  // Update category: See comment for public interface
2958  switch (v.category) {
2959  case Category::U:
2961  // Make fixed parameter
2962  categorize(v.index, Category::C);
2963  v.causality = Causality::PARAMETER;
2964  } else if (variability == Variability::TUNABLE) {
2965  // Make tunable parameter
2966  categorize(v.index, Category::P);
2967  v.causality = Causality::PARAMETER;
2968  } else {
2969  // Not possible
2970  casadi_error("The variability of " + v.name + ", which is of category 'u', can only be "
2971  "changed to 'fixed' (for no category) or 'tunable' (for category 'p')");
2972  }
2973  break;
2974  case Category::P:
2976  // Make input
2977  categorize(v.index, Category::U);
2978  v.causality = Causality::INPUT;
2979  } else if (variability == Variability::FIXED) {
2980  // Make fixed parameter
2981  categorize(v.index, Category::C);
2982  v.causality = Causality::PARAMETER;
2983  } else {
2984  // Not possible
2985  casadi_error("The variability of " + v.name + ", which is of category 'p', can only be "
2986  "changed to 'continuous' (for category 'u') or 'fixed' (for no category)");
2987  }
2988  break;
2989  case Category::C:
2991  // Make input
2992  categorize(v.index, Category::U);
2993  v.causality = Causality::INPUT;
2994  } else if (variability == Variability::TUNABLE) {
2995  // Make tunable parameter
2996  categorize(v.index, Category::P);
2997  v.causality = Causality::PARAMETER;
2998  } else {
2999  // Not possible
3000  casadi_error("The variability of " + v.name + ", which is of type 'c', can only be "
3001  "changed to 'continuous' (for category 'u') or 'tunable' (for category 'p')");
3002  }
3003  break;
3004  default:
3005  casadi_error("Cannot change variability of " + v.name + ", which is of category '"
3006  + to_string(v.category) + "'");
3007  }
3008  // Success: Update variability
3009  v.variability = variability;
3010  // The oracle would need to be regenerated after changes to the categorization
3011  clear_cache_ = true;
3012 }

References casadi::C, categorize(), casadi::Variable::category, casadi::Variable::causality, clear_cache_, casadi::CONTINUOUS, casadi::FIXED, casadi::Variable::index, casadi::INPUT, casadi::Variable::name, casadi::P, casadi::PARAMETER, casadi::to_string(), casadi::TUNABLE, casadi::U, casadi::Variable::variability, variability(), and variable().

Referenced by set_category().

◆ shared_from_this() [1/2]

B casadi::GenericSharedInternal< SharedObject , SharedObjectInternal >::shared_from_this ( )
inlineprotectedinherited

Definition at line 83 of file generic_shared_internal.hpp.

83  {
84  casadi_assert_dev(B::test_cast(static_cast<Internal*>(this)));
85  B ret;
86  ret.own(static_cast<Internal*>(this));
87  return ret;
88  }

◆ shared_from_this() [2/2]

const B casadi::GenericSharedInternal< SharedObject , SharedObjectInternal >::shared_from_this ( ) const
inlineprotectedinherited

Definition at line 92 of file generic_shared_internal.hpp.

92  {
93  casadi_assert_dev(B::test_cast(static_cast<const Internal*>(this)));
94  B ret;
95  ret.own(const_cast<Internal*>(static_cast<const Internal*>(this)));
96  return ret;
97  }

◆ size() [1/2]

casadi_int casadi::DaeBuilderInternal::size ( Attribute  a,
const std::vector< std::string > &  name 
) const
protected

Definition at line 4080 of file dae_builder_internal.cpp.

4080  {
4081  casadi_int r = 0;
4082  for (auto& n : name) r += variable(n).size(a);
4083  return r;
4084 }
casadi_int size(Attribute a) const
Total number of elements for a particular attribute.

References name(), casadi::Variable::size(), and variable().

◆ size() [2/2]

size_t casadi::DaeBuilderInternal::size ( Category  cat) const
inline

◆ sort()

void casadi::DaeBuilderInternal::sort ( Category  cat)

Definition at line 1325 of file dae_builder_internal.cpp.

1325  {
1326  casadi_assert(is_acyclic(cat), "Sorting not supported for category " + to_string(cat));
1327  // Find new order based on interdependencies
1328  std::vector<MX> v = var(indices(cat)), vdef = output(dependent_definition(cat));
1329  sort_dependent(v, vdef);
1330  // New order
1331  std::vector<size_t> new_order;
1332  for (const MX& e : v) new_order.push_back(find(e.name()));
1333  std::copy(new_order.begin(), new_order.end(), indices(cat).begin());
1334 }
static void sort_dependent(std::vector< MX > &v, std::vector< MX > &vdef)

References casadi::dependent_definition(), find(), indices(), casadi::is_acyclic(), output(), sort_dependent(), casadi::to_string(), and var().

Referenced by eliminate().

◆ sort_dependent()

void casadi::DaeBuilderInternal::sort_dependent ( std::vector< MX > &  v,
std::vector< MX > &  vdef 
)
staticprotected

Definition at line 2380 of file dae_builder_internal.cpp.

2380  {
2381  // Form function to evaluate dependent variables
2382  Function vfcn("vfcn", {vertcat(v)}, {vertcat(vdef)}, {"v"}, {"vdef"},
2383  Dict{{"allow_free", true}});
2384  // Is any variable vector-valued?
2385  bool any_vector_valued = false;
2386  for (const MX& v_i : v) {
2387  casadi_assert(!v_i.is_empty(), "Cannot have zero-dimension dependent variables");
2388  if (!v_i.is_scalar()) {
2389  any_vector_valued = true;
2390  break;
2391  }
2392  }
2393  // If vector-valued variables exists, collapse them
2394  if (any_vector_valued) {
2395  // New v corresponding to one scalar input per v argument
2396  std::vector<MX> vfcn_in(v), vfcn_arg(v);
2397  for (size_t i = 0; i < v.size(); ++i) {
2398  if (!v.at(i).is_scalar()) {
2399  vfcn_in.at(i) = MX::sym(v.at(i).name());
2400  vfcn_arg.at(i) = repmat(vfcn_in.at(i), v.at(i).size1());
2401  }
2402  }
2403  // Wrap vfcn
2404  std::vector<MX> vfcn_out = vfcn(vertcat(vfcn_arg));
2405  vfcn_out = vertsplit(vfcn_out.at(0), offset(v));
2406  // Collapse vector-valued outputs
2407  for (size_t i = 0; i < v.size(); ++i) {
2408  if (!v.at(i).is_scalar()) {
2409  vfcn_out.at(i) = dot(vfcn_out.at(i), vfcn_out.at(i));
2410  }
2411  }
2412  // Recreate vfcn with smaller dimensions
2413  vfcn = Function(vfcn.name(), {vertcat(vfcn_in)}, {vertcat(vfcn_out)},
2414  vfcn.name_in(), vfcn.name_out(), {{"allow_free", true}});
2415  }
2416  // Calculate sparsity pattern of dvdef/dv
2417  Sparsity Jv = vfcn.jac_sparsity(0, 0);
2418  // Add diagonal (equation is v-vdef = 0)
2419  Jv = Jv + Sparsity::diag(Jv.size1());
2420  // If lower triangular, nothing to do
2421  if (Jv.is_triu()) return;
2422  // Perform a Dulmage-Mendelsohn decomposition
2423  std::vector<casadi_int> rowperm, colperm, rowblock, colblock, coarse_rowblock, coarse_colblock;
2424  (void)Jv.btf(rowperm, colperm, rowblock, colblock, coarse_rowblock, coarse_colblock);
2425  // Reorder the variables
2426  std::vector<MX> tmp(v.size());
2427  for (size_t k = 0; k < v.size(); ++k) tmp[k] = v.at(colperm.at(k));
2428  std::copy(tmp.begin(), tmp.end(), v.begin());
2429  // Reorder the equations
2430  for (size_t k = 0; k < v.size(); ++k) tmp[k] = vdef.at(rowperm.at(k));
2431  std::copy(tmp.begin(), tmp.end(), vdef.begin());
2432 }
static MX sym(const std::string &name, casadi_int nrow=1, casadi_int ncol=1)
Create an nrow-by-ncol symbolic primitive.
static Sparsity diag(casadi_int nrow)
Create diagonal sparsity pattern *.
Definition: sparsity.hpp:190
T dot(const std::vector< T > &a, const std::vector< T > &b)

References casadi::dot(), and casadi::GenericMatrix< MX >::sym().

Referenced by sort().

◆ sort_z()

void casadi::DaeBuilderInternal::sort_z ( const std::vector< std::string > &  z_order)

Definition at line 1336 of file dae_builder_internal.cpp.

1336  {
1337  // Make sure lengths agree
1338  casadi_assert(z_order.size() == size(Category::Z), "Dimension mismatch");
1339  // Mark existing components in z
1340  std::vector<bool> old_z(n_variables(), false);
1341  for (size_t i : indices(Category::Z)) old_z.at(i) = true;
1342  // New vector of z
1343  std::vector<size_t> new_z;
1344  new_z.reserve(z_order.size());
1345  for (const std::string& s : z_order) {
1346  size_t i = find(s);
1347  casadi_assert(old_z.at(i), "Variable \"" + s + "\" is not an algebraic variable.");
1348  new_z.push_back(i);
1349  }
1350  // Success: Update z
1351  std::copy(new_z.begin(), new_z.end(), indices(Category::Z).begin());
1352 }

References find(), indices(), n_variables(), size(), and casadi::Z.

◆ start_all()

std::vector< double > casadi::DaeBuilderInternal::start_all ( ) const

Definition at line 967 of file dae_builder_internal.cpp.

967  {
968  std::vector<double> r;
969  for (const Variable* v : variables_) {
970  for (double s : v->start) r.push_back(s);
971  }
972  return r;
973 }

References variables_.

Referenced by generate_wrapper().

◆ string_attribute() [1/2]

std::string casadi::DaeBuilderInternal::string_attribute ( Attribute  a,
const std::string &  name 
) const
protected

Get by attribute name (string-valued)

Definition at line 4048 of file dae_builder_internal.cpp.

4049  {
4050  std::string r;
4051  variable(name).get_attribute(a, &r);
4052  return r;
4053 }

References casadi::Variable::get_attribute(), name(), and variable().

◆ string_attribute() [2/2]

std::vector< std::string > casadi::DaeBuilderInternal::string_attribute ( Attribute  a,
const std::vector< std::string > &  name 
) const
protected

Get by attribute name (string-valued)

Definition at line 4055 of file dae_builder_internal.cpp.

4056  {
4057  // Allocate return
4058  std::vector<std::string> r;
4059  r.reserve(size(a, name));
4060  // Get contribution from each variable
4061  std::string r1;
4062  for (auto& n : name) {
4063  variable(n).get_attribute(a, &r1);
4064  r.push_back(r1);
4065  }
4066  return r;
4067 }

References casadi::Variable::get_attribute(), name(), size(), and variable().

◆ tear()

void casadi::DaeBuilderInternal::tear ( )

Definition at line 1433 of file dae_builder_internal.cpp.

1433  {
1434  // Prefix
1435  const std::string res_prefix = "res__";
1436  // Get residual variables, iteration variables
1437  std::vector<std::string> res, iv, iv_on_hold;
1438  tearing_variables(&res, &iv, &iv_on_hold);
1439  // All iteration variables
1440  std::set<std::string> iv_set;
1441  for (auto& e : iv) iv_set.insert(e);
1442  for (auto& e : iv_on_hold) iv_set.insert(e);
1443  // Remove any (held or not held) iteration variables, equations from z and alg
1444  size_t sz = 0;
1445  for (size_t k = 0; k < size(Category::Z); ++k) {
1446  if (!iv_set.count(variable(Category::Z, k).name)) {
1447  // Non-iteration variable: Keep
1448  indices(Category::Z).at(k) = indices(Category::Z).at(sz);
1449  sz++;
1450  }
1451  }
1452  indices(Category::Z).resize(sz);
1453  // Remove any (held or not held) iteration variables, equations from u
1454  sz = 0;
1455  for (size_t k = 0; k < size(Category::U); ++k) {
1456  if (!iv_set.count(variable(Category::U, k).name)) {
1457  // Non-iteration variable: Keep
1458  indices(Category::U).at(k) = indices(Category::U).at(sz++);
1459  }
1460  }
1461  indices(Category::U).resize(sz);
1462  // Add algebraic variables
1463  for (auto& e : iv) indices(Category::Z).push_back(find(e));
1464  // Add output variables
1465  for (auto& e : iv_on_hold) indices(Category::U).push_back(find(e));
1466 }
void tearing_variables(std::vector< std::string > *res, std::vector< std::string > *iv, std::vector< std::string > *iv_on_hold) const
Identify free variables and residual equations.

References find(), indices(), name(), size(), tearing_variables(), casadi::U, variable(), and casadi::Z.

◆ tearing_variables()

void casadi::DaeBuilderInternal::tearing_variables ( std::vector< std::string > *  res,
std::vector< std::string > *  iv,
std::vector< std::string > *  iv_on_hold 
) const

Definition at line 1468 of file dae_builder_internal.cpp.

1469  {
1470  // Clear output
1471  if (res) res->clear();
1472  if (iv) iv->clear();
1473  if (iv_on_hold) iv_on_hold->clear();
1474  // Prefix
1475  const std::string res_prefix = "res__";
1476  // Collect hold indices
1477  std::vector<MX> r_hold, iv_hold;
1478  // Any hold variable?
1479  bool any_hold = false;
1480  // Collect residual variables, iteration variables, expression for hold indices, if any
1481  for (const Variable* v : variables_) {
1482  // Residual variables are specified with a "res__" prefix
1483  if (v->name.rfind(res_prefix, 0) == 0) {
1484  // Process iteration variable name, names of hold markers
1485  std::string iv_name, res_hold_name, iv_hold_name;
1486  // Iteration variable, hold markers are contained in the remainder of the name
1487  try {
1488  size_t pos = res_prefix.size();
1489  // Find the next "__", if any
1490  size_t end = v->name.find("__", pos);
1491  if (end == std::string::npos) end = v->name.size();
1492  // Look up iteration variable
1493  iv_name = v->name.substr(pos, end - pos);
1494  // Ensure that the variable exists
1495  casadi_assert(has(iv_name), "No such variable: " + iv_name);
1496  // Get hold indices, if any
1497  if (end != v->name.size()) {
1498  // Find next "__", read hold index for residual variable
1499  pos = end + 2;
1500  end = v->name.find("__", pos);
1501  if (end == std::string::npos) end = v->name.size();
1502  res_hold_name = v->name.substr(pos, end - pos);
1503  // Ensure that the variable exists
1504  casadi_assert(has(res_hold_name), "No such variable: " + res_hold_name);
1505  // The remainder of the name contains iv_hold_name
1506  if (end != v->name.size()) {
1507  iv_hold_name = v->name.substr(end + 2);
1508  casadi_assert(has(iv_hold_name), "No such variable: " + iv_hold_name);
1509  }
1510  }
1511  } catch (std::exception& e) {
1512  // Generate warning
1513  casadi_warning("Cannot process residual variable: " + v->name + ":" +
1514  std::string(e.what()));
1515  continue;
1516  }
1517  // Add residual variable, corresponding hold variable
1518  if (res_hold_name.empty()) {
1519  r_hold.push_back(false);
1520  } else {
1521  any_hold = true;
1522  r_hold.push_back(variable(res_hold_name).v);
1523  casadi_assert(r_hold.back().is_scalar(), "Non-scalar hold variable for " + res_hold_name);
1524  }
1525  if (res) res->push_back(v->name);
1526  // Add iteration variable, corresponding hold variable
1527  if (iv_hold_name.empty()) {
1528  iv_hold.push_back(false);
1529  } else {
1530  any_hold = true;
1531  iv_hold.push_back(variable(iv_hold_name).v);
1532  casadi_assert(iv_hold.back().is_scalar(), "Non-scalar hold variable for " + iv_hold_name);
1533  }
1534  if (iv) iv->push_back(iv_name);
1535  }
1536  }
1537  // Evaluate hold variables, if needed
1538  if (any_hold) {
1539  try {
1540  // Code below needs to be refactored
1541  casadi_error("not implemented");
1542 #if 0
1543  // Get start attributes for p
1544  Function startfun_p = attribute_fun("startfun_p", {}, {"start_p"});
1545  if (startfun_p.has_free()) {
1546  casadi_error("startfun has free variables: " + str(startfun_p.get_free()));
1547  }
1548  DM p0 = startfun_p(std::vector<DM>{}).at(0);
1549  // Create function to evaluate the hold attributes
1550  Function holdfun("holdfun", {vertcat(var(p_))},
1551  {vertcat(r_hold), vertcat(iv_hold)}, {"p"}, {"r_hold", "iv_hold"});
1552  if (holdfun.has_free()) {
1553  casadi_error("holdfun has free variables: " + str(holdfun.get_free()));
1554  }
1555  // Evaluate holdfun to get hold attributes
1556  std::vector<DM> hold0 = holdfun(std::vector<DM>{p0});
1557  std::vector<double> r_hold0 = hold0.at(0).nonzeros();
1558  std::vector<double> iv_hold0 = hold0.at(1).nonzeros();
1559  casadi_assert_dev(r_hold0.size() == res->size());
1560  casadi_assert_dev(iv_hold0.size() == iv->size());
1561  // Remove hold variables from residual variables
1562  size_t sz = 0;
1563  if (res) {
1564  for (size_t k = 0; k < res->size(); ++k) {
1565  if (!static_cast<bool>(r_hold0.at(k))) {
1566  res->at(sz++) = res->at(k);
1567  }
1568  }
1569  res->resize(sz);
1570  }
1571  // Remove hold variables from iteration variables
1572  sz = 0;
1573  for (size_t k = 0; k < iv->size(); ++k) {
1574  if (!static_cast<bool>(iv_hold0.at(k))) {
1575  if (iv) iv->at(sz++) = iv->at(k);
1576  } else {
1577  if (iv_on_hold) iv_on_hold->push_back(iv->at(k));
1578  }
1579  }
1580  if (iv) iv->resize(sz);
1581 #endif
1582  } catch (std::exception& e) {
1583  // Warning instead of error
1584  casadi_warning("Failed to evaluate hold variables: " + std::string(e.what()));
1585  }
1586  }
1587 }
Matrix< double > DM
Definition: dm_fwd.hpp:33

References casadi::Function::get_free(), has(), casadi::Function::has_free(), casadi::str(), var(), variable(), and variables_.

Referenced by tear().

◆ time()

const MX & casadi::DaeBuilderInternal::time ( ) const
protected

Definition at line 2658 of file dae_builder_internal.cpp.

2658  {
2659  casadi_assert(has_t(), "No explicit time variable");
2660  return var(indices(Category::T).at(0));
2661 }

References has_t(), indices(), casadi::T, and var().

Referenced by generate_guid().

◆ transition() [1/2]

Function casadi::DaeBuilderInternal::transition ( const std::string &  fname) const

Definition at line 2540 of file dae_builder_internal.cpp.

2540  {
2541  // If no events, return null
2542  if (when_.empty()) return Function();
2543 
2544  // If just a single event, create an event function with a dummy index input
2545  if (when_.size() == 1) return transition(fname, 0, true);
2546 
2547  // Create separate transition functions for each event
2548  std::vector<Function> f_all;
2549  for (casadi_int i = 0; i < when_.size(); ++i) {
2550  f_all.push_back(transition(fname + "_" + str(i), i));
2551  }
2552 
2553  // Make the last function the default value in the switch
2554  Function f_def = f_all.back();
2555  f_all.pop_back();
2556 
2557  // Create a switch function
2558  return Function::conditional(fname, f_all, f_def);
2559 }
static Function conditional(const std::string &name, const std::vector< Function > &f, const Function &f_def, const Dict &opts=Dict())
Constuct a switch function.
Definition: function.cpp:765

References casadi::Function::conditional(), casadi::str(), transition(), and when_.

◆ transition() [2/2]

Function casadi::DaeBuilderInternal::transition ( const std::string &  fname,
casadi_int  index,
bool  dummy_index_input = false 
) const

Definition at line 2492 of file dae_builder_internal.cpp.

2493  {
2494 
2495  // Make sure that the index is valid
2496  casadi_assert(index >= 0 && index < when_.size(), "Illegal event index");
2497 
2498  // Get input expressions for the oracle
2499  std::vector<MX> oracle_in = oracle().mx_in();
2500 
2501  // Input expressions for the event functions, without the index
2502  std::vector<MX> ret_in(DYN_NUM_IN);
2503  ret_in[DYN_T] = oracle_in.at(static_cast<size_t>(Category::T));
2504  ret_in[DYN_X] = oracle_in.at(static_cast<size_t>(Category::X));
2505  ret_in[DYN_Z] = oracle_in.at(static_cast<size_t>(Category::Z));
2506  ret_in[DYN_P] = oracle_in.at(static_cast<size_t>(Category::P));
2507  ret_in[DYN_U] = oracle_in.at(static_cast<size_t>(Category::U));
2508 
2509  // When equation left-hand sides and right-hand sides
2510  std::vector<MX> when_lhs, when_rhs;
2511  for (size_t eq : when_.at(index).second) {
2512  auto v = variable(eq).parent;
2513  when_lhs.push_back(variable(v).v);
2514  when_rhs.push_back(variable(eq).v);
2515  }
2516 
2517  // Expressions for x and z after event
2518  std::vector<MX> ret_out = {ret_in[DYN_X], ret_in[DYN_Z]};
2519  ret_out = MX::substitute(ret_out, when_lhs, when_rhs);
2520 
2521  // Remove dependent variables, if any
2522  if (size(Category::W) > 0) {
2523  // Dependent variable definitions
2524  std::vector<MX> wdef = output(OutputCategory::WDEF);
2525  // Perform in-place substitution
2526  substitute_inplace(var(Category::W), wdef, ret_out, false);
2527  }
2528 
2529  // Check if a dummy index input needes to be included
2530  if (dummy_index_input) {
2531  // Create a function with the transition input signature
2532  ret_in.insert(ret_in.begin(), MX());
2533  return Function(fname, ret_in, ret_out, event_in(), event_out());
2534  } else {
2535  // Create a function with the DAE function input signature
2536  return Function(fname, ret_in, ret_out, dyn_in(), event_out());
2537  }
2538 }
static MX substitute(const MX &ex, const MX &v, const MX &vdef)
Definition: mx.cpp:1424
std::vector< std::string > event_in()
Get input scheme of an event transition function.
Definition: integrator.cpp:256
std::vector< std::string > event_out()
Get output scheme of an event transition functions.
Definition: integrator.cpp:260
@ DYN_NUM_IN
Definition: integrator.hpp:196

References casadi::dyn_in(), casadi::DYN_NUM_IN, casadi::DYN_P, casadi::DYN_T, casadi::DYN_U, casadi::DYN_X, casadi::DYN_Z, eq(), casadi::event_in(), casadi::event_out(), casadi::Function::mx_in(), oracle(), output(), casadi::P, casadi::Variable::parent, size(), casadi::MX::substitute(), casadi::T, casadi::U, var(), variable(), casadi::W, casadi::WDEF, when_, casadi::X, and casadi::Z.

Referenced by export_fmu(), and transition().

◆ type_name()

std::string casadi::DaeBuilderInternal::type_name ( ) const
inline

Definition at line 375 of file dae_builder_internal.hpp.

375 {return "DaeBuilderInternal";}

◆ unique_name()

std::string casadi::DaeBuilderInternal::unique_name ( const std::string &  prefix,
bool  allow_no_prefix = true 
) const

Definition at line 1712 of file dae_builder_internal.cpp.

1713  {
1714  // Check if the variable exists without any prefix
1715  if (allow_no_prefix && !has(prefix)) return prefix;
1716  // Find the first available index
1717  size_t i = 0;
1718  while (has(prefix + str(i))) i++;
1719  // Return the unique name
1720  return prefix + str(i);
1721 }

References has(), and casadi::str().

Referenced by assign(), eq(), reinit(), and when().

◆ update_dependencies()

void casadi::DaeBuilderInternal::update_dependencies ( ) const

Definition at line 847 of file dae_builder_internal.cpp.

847  {
848  // Get oracle function
849  const Function& oracle = this->oracle();
850  // Dependendencies of the ODE right-hand-side
851  Sparsity dode_dxT = oracle.jac_sparsity(oracle.index_out("ode"), oracle.index_in("x")).T();
852  Sparsity dode_duT = oracle.jac_sparsity(oracle.index_out("ode"), oracle.index_in("u")).T();
853  for (casadi_int i = 0; i < size(Category::X); ++i) {
854  // Get output variable
855  const Variable& xdot = variable(variable(Category::X, i).der);
856  // Clear dependencies
857  xdot.dependencies.clear();
858  // Dependencies on states
859  for (casadi_int k = dode_dxT.colind(i); k < dode_dxT.colind(i + 1); ++k) {
860  casadi_int j = dode_dxT.row(k);
861  xdot.dependencies.push_back(variable(Category::X, j).value_reference);
862  }
863  // Dependencies on controls
864  for (casadi_int k = dode_duT.colind(i); k < dode_duT.colind(i + 1); ++k) {
865  casadi_int j = dode_duT.row(k);
866  xdot.dependencies.push_back(variable(Category::U, j).value_reference);
867  }
868  }
869  // Dependendencies of the outputs and event indicators
870  for (std::string catname : {"y", "zero"}) {
871  const std::vector<size_t>& oind = catname == "y" ? outputs_ : event_indicators_;
872  Sparsity dy_dxT = oracle.jac_sparsity(oracle.index_out(catname), oracle.index_in("x")).T();
873  Sparsity dy_duT = oracle.jac_sparsity(oracle.index_out(catname), oracle.index_in("u")).T();
874  for (casadi_int i = 0; i < oind.size(); ++i) {
875  // Get output variable
876  const Variable& y = variable(oind.at(i));
877  // Clear dependencies
878  y.dependencies.clear();
879  // Dependencies on states
880  for (casadi_int k = dy_dxT.colind(i); k < dy_dxT.colind(i + 1); ++k) {
881  casadi_int j = dy_dxT.row(k);
882  y.dependencies.push_back(variable(Category::X, j).value_reference);
883  }
884  // Dependencies on controls
885  for (casadi_int k = dy_duT.colind(i); k < dy_duT.colind(i + 1); ++k) {
886  casadi_int j = dy_duT.row(k);
887  y.dependencies.push_back(variable(Category::U, j).value_reference);
888  }
889  }
890  }
891 }
casadi_int index_in(const std::string &name) const
Find the index for a string describing a particular entry of an input scheme.
Definition: function.cpp:969
casadi_int index_out(const std::string &name) const
Find the index for a string describing a particular entry of an output scheme.
Definition: function.cpp:977
const std::vector< Sparsity > & jac_sparsity(bool compact=false) const
Get, if necessary generate, the sparsity of all Jacobian blocks.
Definition: function.cpp:940

References casadi::Sparsity::colind(), casadi::Variable::dependencies, der(), event_indicators_, casadi::Function::index_in(), casadi::Function::index_out(), casadi::Function::jac_sparsity(), oracle(), outputs_, casadi::Sparsity::row(), size(), casadi::U, variable(), and casadi::X.

Referenced by export_fmu().

◆ var() [1/5]

std::vector<MX> casadi::DaeBuilderInternal::var ( Category  cat) const
inline

Definition at line 446 of file dae_builder_internal.hpp.

446 {return var(indices(cat));}

References var().

Referenced by var().

◆ var() [2/5]

const MX& casadi::DaeBuilderInternal::var ( Category  cat,
size_t  ind 
) const
inline

Definition at line 440 of file dae_builder_internal.hpp.

440 {return var(indices(cat).at(ind));}

References var().

Referenced by var().

◆ var() [3/5]

const MX & casadi::DaeBuilderInternal::var ( const std::string &  name) const

◆ var() [4/5]

std::vector< MX > casadi::DaeBuilderInternal::var ( const std::vector< size_t > &  ind) const

Definition at line 3903 of file dae_builder_internal.cpp.

3903  {
3904  std::vector<MX> ret;
3905  ret.reserve(ind.size());
3906  for (size_t i : ind) ret.push_back(var(i));
3907  return ret;
3908 }

References var().

◆ var() [5/5]

const MX & casadi::DaeBuilderInternal::var ( size_t  ind) const

Definition at line 3899 of file dae_builder_internal.cpp.

3899  {
3900  return variable(ind).v;
3901 }

References casadi::Variable::v, and variable().

◆ variability()

Variability casadi::DaeBuilderInternal::variability ( size_t  ind) const
protected

Definition at line 2948 of file dae_builder_internal.cpp.

2948  {
2949  return variable(ind).variability;
2950 }

References casadi::Variable::variability, and variable().

Referenced by add(), default_initial(), import_model_variables(), and set_variability().

◆ variable() [1/8]

Variable& casadi::DaeBuilderInternal::variable ( Category  cat,
size_t  ind 
)
inline

Access a variable by Category and index

Definition at line 418 of file dae_builder_internal.hpp.

418 {return variable(indices(cat).at(ind));}

References variable().

Referenced by variable().

◆ variable() [2/8]

const Variable& casadi::DaeBuilderInternal::variable ( Category  cat,
size_t  ind 
) const
inline

Access a variable by Category and index

Definition at line 419 of file dae_builder_internal.hpp.

419  {
420  return variable(indices(cat).at(ind));
421  }

◆ variable() [3/8]

Variable& casadi::DaeBuilderInternal::variable ( const MX v)
inline

Access a variable by expression

Definition at line 432 of file dae_builder_internal.hpp.

432 {return variable(find(v));}

References casadi::find(), and variable().

Referenced by variable().

◆ variable() [4/8]

const Variable& casadi::DaeBuilderInternal::variable ( const MX v) const
inline

Access a variable by expression

Definition at line 433 of file dae_builder_internal.hpp.

433 {return variable(find(v));}

References casadi::find(), and variable().

Referenced by variable().

◆ variable() [5/8]

Variable& casadi::DaeBuilderInternal::variable ( const std::string &  name)
inline

Access a variable by name

Definition at line 426 of file dae_builder_internal.hpp.

426 {return variable(find(name));}

References casadi::find(), and variable().

Referenced by variable().

◆ variable() [6/8]

const Variable& casadi::DaeBuilderInternal::variable ( const std::string &  name) const
inline

Access a variable by name

Definition at line 427 of file dae_builder_internal.hpp.

427 {return variable(find(name));}

References casadi::find(), and variable().

Referenced by variable().

◆ variable() [7/8]

Variable& casadi::DaeBuilderInternal::variable ( size_t  ind)
inline

◆ variable() [8/8]

const Variable& casadi::DaeBuilderInternal::variable ( size_t  ind) const
inline

Access a variable by index

Definition at line 413 of file dae_builder_internal.hpp.

413 {return *variables_.at(ind);}

◆ weak()

Extra doc: https://github.com/casadi/casadi/wiki/L_1ai

Definition at line 67 of file generic_shared_internal.hpp.

191  {
192  if (weak_ref_==nullptr) {
193  weak_ref_ = new GenericWeakRef<Shared, Internal>(static_cast<Internal*>(this));
194  }
195  return weak_ref_;
196  }

◆ when()

void casadi::DaeBuilderInternal::when ( const MX cond,
const std::vector< std::string > &  eqs,
const Dict opts 
)
protected

Definition at line 3180 of file dae_builder_internal.cpp.

3181  {
3182  // Read options
3183  for (auto&& op : opts) {
3184  casadi_error("No such option: " + op.first);
3185  }
3186  // Convert condition into a smooth zero crossing condition
3187  MX zero;
3188  if (cond.is_op(OP_LT)) {
3189  zero = cond.dep(0) - cond.dep(1); // Reformulate a < b to a - b < 0
3190  } else if (cond.is_op(OP_LE)) {
3191  casadi_error("Only strict inequality in zero-crossing conditions permitted, got: "
3192  + str(cond));
3193  } else {
3194  casadi_error("Cannot parse zero-crossing condition: " + str(cond));
3195  }
3196  // Create a new dependent variable for the event indicator
3198  zero, Dict());
3199  event_indicators_.push_back(e.index);
3200  categorize(e.index, Category::CALCULATED);
3201  // Convert to legacy format, pending refactoring
3202  std::vector<MX> all_lhs, all_rhs;
3203  std::vector<size_t> all_eqs;
3204  for (auto&& eq : eqs) {
3205  Variable& ee = variable(eq);
3206  casadi_assert_dev(ee.category == Category::CALCULATED);
3207  all_lhs.push_back(var(ee.parent));
3208  all_rhs.push_back(ee.v);
3209  all_eqs.push_back(ee.index);
3210  }
3211  when_.push_back(std::make_pair(e.index, all_eqs));
3212 }
@ OP_LT
Definition: calculus.hpp:70
@ OP_LE
Definition: calculus.hpp:70

References add(), casadi::CALCULATED, categorize(), casadi::Variable::category, casadi::CONTINUOUS, casadi::MX::dep(), eq(), event_indicators_, casadi::Variable::index, casadi::MX::is_op(), casadi::LOCAL, casadi::OP_LE, casadi::OP_LT, casadi::Variable::parent, casadi::str(), unique_name(), casadi::Variable::v, var(), variable(), and when_.

Referenced by import_dynamic_equations().

Friends And Related Function Documentation

◆ DaeBuilder

friend class DaeBuilder
friend

Definition at line 210 of file dae_builder_internal.hpp.

◆ FmuFunction

friend class FmuFunction
friend

Definition at line 212 of file dae_builder_internal.hpp.

Referenced by fmu_fun().

◆ FmuInternal

friend class FmuInternal
friend

Definition at line 211 of file dae_builder_internal.hpp.

◆ Variable

friend class Variable
friend

Definition at line 213 of file dae_builder_internal.hpp.

Referenced by new_variable().

Member Data Documentation

◆ author_

std::string casadi::DaeBuilderInternal::author_
protected

Definition at line 495 of file dae_builder_internal.hpp.

Referenced by load_fmi_description().

◆ can_be_instantiated_only_once_per_process_

bool casadi::DaeBuilderInternal::can_be_instantiated_only_once_per_process_
protected

◆ clear_cache_

bool casadi::DaeBuilderInternal::clear_cache_
mutableprotected

◆ copyright_

std::string casadi::DaeBuilderInternal::copyright_
protected

Definition at line 496 of file dae_builder_internal.hpp.

Referenced by load_fmi_description().

◆ debug_

bool casadi::DaeBuilderInternal::debug_
protected

◆ derivatives_

std::vector<size_t> casadi::DaeBuilderInternal::derivatives_
protected

Definition at line 532 of file dae_builder_internal.hpp.

Referenced by import_model_structure(), and import_model_variables().

◆ description_

std::string casadi::DaeBuilderInternal::description_
protected

Definition at line 494 of file dae_builder_internal.hpp.

Referenced by load_fmi_description().

◆ detect_quad_

bool casadi::DaeBuilderInternal::detect_quad_
protected

Definition at line 523 of file dae_builder_internal.hpp.

Referenced by add(), and DaeBuilderInternal().

◆ event_indicators_

std::vector<size_t> casadi::DaeBuilderInternal::event_indicators_
protected

◆ fmi_major_

casadi_int casadi::DaeBuilderInternal::fmi_major_
protected

◆ fmi_version_

std::string casadi::DaeBuilderInternal::fmi_version_
protected

Definition at line 491 of file dae_builder_internal.hpp.

Referenced by load_fmi_description().

◆ fmutol_

double casadi::DaeBuilderInternal::fmutol_
protected

Definition at line 487 of file dae_builder_internal.hpp.

Referenced by DaeBuilderInternal(), and casadi::FmuInternal::init().

◆ fun_

std::vector<Function> casadi::DaeBuilderInternal::fun_
protected

Extra doc: https://github.com/casadi/casadi/wiki/L_11

Definition at line 648 of file dae_builder_internal.hpp.

Referenced by add_fun(), disp(), fun(), and has_fun().

◆ generation_date_and_time_

std::string casadi::DaeBuilderInternal::generation_date_and_time_
protected

Definition at line 499 of file dae_builder_internal.hpp.

Referenced by load_fmi_description().

◆ generation_tool_

std::string casadi::DaeBuilderInternal::generation_tool_
protected

Definition at line 498 of file dae_builder_internal.hpp.

Referenced by load_fmi_description().

◆ ignore_time_

bool casadi::DaeBuilderInternal::ignore_time_
protected

Definition at line 488 of file dae_builder_internal.hpp.

Referenced by DaeBuilderInternal(), and import_model_variables().

◆ indices_

std::vector<std::vector<size_t> > casadi::DaeBuilderInternal::indices_
protected

Definition at line 541 of file dae_builder_internal.hpp.

Referenced by DaeBuilderInternal(), and indices().

◆ init_

std::vector<size_t> casadi::DaeBuilderInternal::init_
protected

Definition at line 544 of file dae_builder_internal.hpp.

Referenced by disp(), init_lhs(), init_rhs(), and set_init().

◆ initial_unknowns_

std::vector<size_t> casadi::DaeBuilderInternal::initial_unknowns_
protected

Definition at line 532 of file dae_builder_internal.hpp.

Referenced by import_model_structure().

◆ instantiation_token_

std::string casadi::DaeBuilderInternal::instantiation_token_
protected

Definition at line 493 of file dae_builder_internal.hpp.

Referenced by casadi::FmuInternal::init(), and load_fmi_description().

◆ lc_

Function::AuxOut casadi::DaeBuilderInternal::lc_
protected

Definition at line 643 of file dae_builder_internal.hpp.

Referenced by add_lc(), and create().

◆ license_

std::string casadi::DaeBuilderInternal::license_
protected

Definition at line 497 of file dae_builder_internal.hpp.

Referenced by load_fmi_description().

◆ model_identifier_

std::string casadi::DaeBuilderInternal::model_identifier_
protected

Definition at line 507 of file dae_builder_internal.hpp.

Referenced by import_model_exchange(), and casadi::FmuInternal::init().

◆ model_name_

std::string casadi::DaeBuilderInternal::model_name_
protected

Definition at line 492 of file dae_builder_internal.hpp.

Referenced by load_fmi_description().

◆ name_

std::string casadi::DaeBuilderInternal::name_
protected

◆ number_of_event_indicators_

casadi_int casadi::DaeBuilderInternal::number_of_event_indicators_
protected

◆ oracle_

Function casadi::DaeBuilderInternal::oracle_[2][2][2]
mutableprotected

Extra doc: https://github.com/casadi/casadi/wiki/L_12

Definition at line 653 of file dae_builder_internal.hpp.

Referenced by clear_cache(), and oracle().

◆ outputs_

std::vector<size_t> casadi::DaeBuilderInternal::outputs_
protected

◆ provides_adjoint_derivatives_

bool casadi::DaeBuilderInternal::provides_adjoint_derivatives_
protected

◆ provides_directional_derivatives_

bool casadi::DaeBuilderInternal::provides_directional_derivatives_
protected

◆ residuals_

std::vector<size_t> casadi::DaeBuilderInternal::residuals_
protected

Definition at line 532 of file dae_builder_internal.hpp.

Referenced by disp(), lift(), and output().

◆ resource_

Resource casadi::DaeBuilderInternal::resource_
protected

Definition at line 517 of file dae_builder_internal.hpp.

Referenced by DaeBuilderInternal(), and casadi::FmuInternal::init().

◆ source_files_

std::vector<std::string> casadi::DaeBuilderInternal::source_files_
protected

Definition at line 511 of file dae_builder_internal.hpp.

Referenced by import_model_exchange().

◆ start_time_

double casadi::DaeBuilderInternal::start_time_
protected

Definition at line 504 of file dae_builder_internal.hpp.

Referenced by DaeBuilderInternal(), and import_default_experiment().

◆ step_size_

double casadi::DaeBuilderInternal::step_size_
protected

Definition at line 504 of file dae_builder_internal.hpp.

Referenced by DaeBuilderInternal(), and import_default_experiment().

◆ stop_time_

double casadi::DaeBuilderInternal::stop_time_
protected

Definition at line 504 of file dae_builder_internal.hpp.

Referenced by DaeBuilderInternal(), and import_default_experiment().

◆ symbolic_

bool casadi::DaeBuilderInternal::symbolic_
protected

Definition at line 520 of file dae_builder_internal.hpp.

Referenced by create(), DaeBuilderInternal(), load_fmi_description(), and oracle().

◆ tolerance_

double casadi::DaeBuilderInternal::tolerance_
protected

Definition at line 504 of file dae_builder_internal.hpp.

Referenced by DaeBuilderInternal(), and import_default_experiment().

◆ variable_naming_convention_

std::string casadi::DaeBuilderInternal::variable_naming_convention_
protected

Definition at line 500 of file dae_builder_internal.hpp.

Referenced by load_fmi_description().

◆ variables_

std::vector<Variable*> casadi::DaeBuilderInternal::variables_
protected

◆ varind_

std::unordered_map<std::string, size_t> casadi::DaeBuilderInternal::varind_
protected

Definition at line 535 of file dae_builder_internal.hpp.

Referenced by find(), has(), import_model_variables(), new_variable(), and prune().

◆ vrmap_

std::unordered_map<unsigned int, size_t> casadi::DaeBuilderInternal::vrmap_
protected

◆ when_

std::vector<std::pair<size_t, std::vector<size_t> > > casadi::DaeBuilderInternal::when_
protected

Definition at line 547 of file dae_builder_internal.hpp.

Referenced by disp(), import_dynamic_equations(), transition(), and when().


The documentation for this class was generated from the following files: