List of all members | Public Member Functions | Public Attributes | Friends
casadi::Variable Struct Reference

Holds expressions and meta-data corresponding to a physical quantity evolving in time. More...

#include <dae_builder_internal.hpp>

Detailed Description

Date
2012-2021
Author
Joel Andersson

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

Definition at line 81 of file dae_builder_internal.hpp.

Collaboration diagram for casadi::Variable:
Collaboration graph
[legend]

Public Member Functions

casadi_int size (Attribute a) const
 Total number of elements for a particular attribute. More...
 
XmlNode export_xml (const DaeBuilderInternal &self) const
 
bool is_real () const
 
bool has_start () const
 
bool has_der () const
 
std::vector< Categorycategories () const
 
bool permitted (Category cat) const
 
bool is_set () const
 
bool has_beq () const
 
bool needs_der () const
 
MX get_der (DaeBuilderInternal &self, bool may_allocate=true)
 
MX get_der (const DaeBuilderInternal &self) const
 
void get_attribute (Attribute a, double *val) const
 
void get_attribute (Attribute a, std::vector< double > *val) const
 
void get_attribute (Attribute a, std::string *val) const
 
void set_attribute (Attribute a, double val)
 
void set_attribute (Attribute a, const std::vector< double > &val)
 
void set_attribute (Attribute a, const std::string &val)
 

Public Attributes

casadi_int index
 Location in variable vector. More...
 
std::string name
 Name of the variable. More...
 
std::vector< casadi_int > dimension
 Dimensions. More...
 
casadi_int numel
 Number of elements - product of all dimensions. More...
 
Category category
 CasADi's classification of the variable. More...
 
casadi_int der
 
casadi_int bind
 
bool in_rhs
 
std::vector< double > value
 Numerical value (also for booleans, integers, enums) More...
 
std::string stringvalue
 String value (if string-valued) More...
 
bool dependency
 Do other expressions depend on this variable. More...
 
std::vector< casadi_int > dependencies
 Dependencies. More...
 
std::vector< DependenciesKinddependenciesKind
 Dependencies. More...
 
MX v
 Variable expression (always a vector) More...
 
MX ieq
 Initial equation (to be removed and moved to a separate dependent variable) More...
 
unsigned int value_reference
 
std::string description
 
Type type
 
Causality causality
 
Variability variability
 
std::string unit
 
std::string display_unit
 
Initial initial
 
double min
 
double max
 
double nominal
 
std::vector< double > start
 
casadi_int der_of
 
casadi_int parent
 

Friends

class DaeBuilderInternal
 

Member Function Documentation

◆ categories()

std::vector< Category > casadi::Variable::categories ( ) const

Definition at line 561 of file dae_builder_internal.cpp.

561  {
562  switch (category) {
563  case Category::T:
564  if (in_rhs) {
565  return {Category::T};
566  } else {
567  return {Category::T, Category::NUMEL};
568  }
569  case Category::P: // Fall-through
570  case Category::U: // Fall-through
571  case Category::C:
573  case Category::X: // Fall-through
574  case Category::Q:
575  if (in_rhs) {
576  return {Category::X};
577  } else {
579  }
580  case Category::NUMEL:
582  return {Category::T, Category::NUMEL};
583  } else if (has_der()) {
584  return {Category::Q, Category::NUMEL};
585  } else {
586  return {Category::NUMEL};
587  }
588  case Category::D: return {Category::D};
589  case Category::W: return {Category::W};
591  case Category::Z: return {Category::Z};
592  default: break;
593  }
594  // Error
595  casadi_error("Cannot handle category: " + to_string(category));
596  return {};
597 }
std::string to_string(TypeFmi2 v)
Category category
CasADi's classification of the variable.

References casadi::C, casadi::CALCULATED, category, causality, casadi::D, has_der(), in_rhs, casadi::INDEPENDENT, casadi::NUMEL, casadi::P, casadi::Q, casadi::T, casadi::to_string(), casadi::U, casadi::W, casadi::X, and casadi::Z.

Referenced by permitted().

◆ export_xml()

XmlNode casadi::Variable::export_xml ( const DaeBuilderInternal self) const

Definition at line 470 of file dae_builder_internal.cpp.

470  {
471  // Create new XmlNode
472  XmlNode r;
473  r.name = to_string(type);
474  // Name of variable
475  r.set_attribute("name", name);
476  // Value reference
477  r.set_attribute("valueReference", static_cast<casadi_int>(value_reference));
478  // Description, if any
479  if (!description.empty()) r.set_attribute("description", description);
480  // Causality
482  r.set_attribute("causality", to_string(causality));
483  // Variability (real variables are continuous by default)
485  r.set_attribute("variability", to_string(variability));
486  // Initial property
487  if (initial != Initial::NA && initial != Initial::NUMEL) {
488  r.set_attribute("initial", to_string(initial));
489  }
490  // Minimum attribute
491  if (min != -inf) {
492  if (is_real()) {
493  r.set_attribute("min", min);
494  } else {
495  r.set_attribute("min", static_cast<casadi_int>(min));
496  }
497  }
498  // Maximum attribute
499  if (max != inf) {
500  if (is_real()) {
501  r.set_attribute("max", max);
502  } else {
503  r.set_attribute("max", static_cast<casadi_int>(max));
504  }
505  }
506  // Unit
507  if (!unit.empty()) r.set_attribute("unit", unit);
508  // Display unit
509  if (!display_unit.empty()) r.set_attribute("displayUnit", display_unit);
510  // Nominal value, only for floats
511  if (is_real() && nominal != 1.) r.set_attribute("nominal", nominal);
512  // Start attribute, if any
513  if (has_start()) {
514  if (type == Type::BINARY || type == Type::STRING) {
515  casadi_warning("Start attribute for String, Binary not implemented.");
516  } else {
517  // Convert to string
518  std::stringstream ss;
519  for (size_t i = 0; i < start.size(); ++i) {
520  if (i > 0) ss << " ";
521  if (is_real()) {
522  ss << start.at(i);
523  } else {
524  ss << static_cast<casadi_int>(start.at(i));
525  }
526  }
527  r.set_attribute("start", ss.str());
528  }
529  }
530  // Derivative attribute, if any
531  if (der_of >= 0) {
532  r.set_attribute("derivative",
533  static_cast<casadi_int>(self.variable(der_of).value_reference));
534  }
535  // Return XML representation
536  return r;
537 }
const double inf
infinity
Definition: calculus.hpp:50
std::vector< double > start
std::string name
Name of the variable.

References casadi::BINARY, causality, casadi::CONTINUOUS, der_of, description, display_unit, has_start(), casadi::inf, initial, is_real(), casadi::LOCAL, max, min, casadi::NA, name, casadi::XmlNode::name, nominal, casadi::NUMEL, casadi::XmlNode::set_attribute(), start, casadi::STRING, casadi::to_string(), type, unit, value_reference, and variability.

◆ get_attribute() [1/3]

void casadi::Variable::get_attribute ( Attribute  a,
double *  val 
) const

Get by attribute name

Definition at line 322 of file dae_builder_internal.cpp.

322  {
323  // Only if scalar
324  casadi_assert(numel == 1, "Variable " + name + " is not scalar");
325  // Handle attributes
326  switch (a) {
327  case Attribute::MIN:
328  if (val) *val = min;
329  return;
330  case Attribute::MAX:
331  if (val) *val = max;
332  return;
333  case Attribute::NOMINAL:
334  if (val) *val = nominal;
335  return;
336  case Attribute::START:
337  if (val) *val = start.front();
338  return;
339  case Attribute::VALUE:
340  if (val) *val = value.front();
341  return;
342  default:
343  break;
344  }
345  casadi_error("Cannot handle: " + to_string(a));
346 }
std::vector< double > value
Numerical value (also for booleans, integers, enums)
casadi_int numel
Number of elements - product of all dimensions.

References casadi::MAX, max, casadi::MIN, min, name, casadi::NOMINAL, nominal, numel, casadi::START, start, casadi::to_string(), casadi::VALUE, and value.

Referenced by casadi::DaeBuilderInternal::attribute(), casadi::DaeBuilder::get(), get_attribute(), and casadi::DaeBuilderInternal::string_attribute().

◆ get_attribute() [2/3]

void casadi::Variable::get_attribute ( Attribute  a,
std::string *  val 
) const

Get by attribute name

Definition at line 370 of file dae_builder_internal.cpp.

370  {
371  switch (a) {
373  if (val) *val = stringvalue;
374  return;
375  default:
376  break;
377  }
378  casadi_error("Cannot handle: " + to_string(a));
379 }
std::string stringvalue
String value (if string-valued)

References casadi::STRINGVALUE, stringvalue, and casadi::to_string().

◆ get_attribute() [3/3]

void casadi::Variable::get_attribute ( Attribute  a,
std::vector< double > *  val 
) const

Get by attribute name

Definition at line 348 of file dae_builder_internal.cpp.

348  {
349  // Resize return
350  if (val) val->resize(size(a));
351  // Quick return if scalar
352  if (size(a) == 1) {
353  get_attribute(a, val ? &val->front() : nullptr);
354  return;
355  }
356  // Handle vector attributes
357  switch (a) {
358  case Attribute::START:
359  if (val) std::copy(start.begin(), start.end(), val->begin());
360  return;
361  case Attribute::VALUE:
362  if (val) std::copy(value.begin(), value.end(), val->begin());
363  return;
364  default:
365  break;
366  }
367  casadi_error("Cannot handle: " + to_string(a));
368 }
casadi_int size(Attribute a) const
Total number of elements for a particular attribute.
void get_attribute(Attribute a, double *val) const

References get_attribute(), size(), casadi::START, start, casadi::to_string(), casadi::VALUE, and value.

◆ get_der() [1/2]

MX casadi::Variable::get_der ( const DaeBuilderInternal self) const

Definition at line 606 of file dae_builder_internal.cpp.

606  {
608  // Time derivative of independent variable is 1
609  return 1;
610  } else if (needs_der()) {
611  casadi_assert(der >= 0, "Variable " + name + " has no time derivative");
612  // Should have a derviative expression
613  return self.variable(der).v;
614  } else {
615  // Constant or piecewise constant variable
616  return MX::zeros(v.sparsity());
617  }
618 }
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.
const Sparsity & sparsity() const
Get the sparsity pattern.
Definition: mx.cpp:612
MX v
Variable expression (always a vector)

References causality, der, casadi::INDEPENDENT, name, needs_der(), casadi::MX::sparsity(), v, and casadi::GenericMatrix< MX >::zeros().

◆ get_der() [2/2]

MX casadi::Variable::get_der ( DaeBuilderInternal self,
bool  may_allocate = true 
)

Definition at line 620 of file dae_builder_internal.cpp.

620  {
621  // Create a new derivative variable, if needed
622  if (may_allocate && needs_der() && der < 0) {
623  Variable& der_v = self.new_variable("der(" + name + ")", dimension);
624  self.categorize(der_v.index, Category::Z);
625  der_v.der_of = index;
626  der_v.parent = index;
627  der = der_v.index;
628  // Add to list of derivatives
629  self.der_.push_back(der_v.index);
630  }
631  // Call the const overload
632  return get_der(const_cast<const DaeBuilderInternal&>(self));
633 }
MX get_der(DaeBuilderInternal &self, bool may_allocate=true)
casadi_int index
Location in variable vector.
friend class DaeBuilderInternal
std::vector< casadi_int > dimension
Dimensions.

References der, der_of, dimension, index, name, needs_der(), parent, and casadi::Z.

◆ has_beq()

bool casadi::Variable::has_beq ( ) const
inline

Definition at line 204 of file dae_builder_internal.hpp.

204 {return bind >= 0;}

◆ has_der()

bool casadi::Variable::has_der ( ) const
inline

Definition at line 188 of file dae_builder_internal.hpp.

188  {
189  return der != size_t(-1) || causality == Causality::INDEPENDENT;
190  }

References casadi::INDEPENDENT.

Referenced by categories(), and casadi::DaeBuilderInternal::set_category().

◆ has_start()

bool casadi::Variable::has_start ( ) const

Definition at line 540 of file dae_builder_internal.cpp.

540  {
541  // Rules, according to the FMI 3.0 specification, Section 2.4.7.5.
542  if (initial == Initial::EXACT || initial == Initial::APPROX) return true;
543  if (initial == Initial::CALCULATED || causality == Causality::INDEPENDENT) return false;
544  if (causality == Causality::PARAMETER) return true;
545  if (causality == Causality::INPUT) return true;
546  if (variability == Variability::CONSTANT) return true;
547  return false;
548 }

References casadi::APPROX, casadi::CALCULATED, causality, casadi::CONSTANT, casadi::EXACT, casadi::INDEPENDENT, initial, casadi::INPUT, casadi::PARAMETER, and variability.

Referenced by export_xml().

◆ is_real()

bool casadi::Variable::is_real ( ) const
inline

Definition at line 182 of file dae_builder_internal.hpp.

References casadi::FLOAT32, and casadi::FLOAT64.

Referenced by export_xml().

◆ is_set()

bool casadi::Variable::is_set ( ) const
inline

Definition at line 199 of file dae_builder_internal.hpp.

199  {
200  return !(type==Type::STRING ? stringvalue.empty() : std::isnan(value.front()));
201  }

References casadi::STRING.

Referenced by casadi::Fmu2::init(), and casadi::Fmu3::init().

◆ needs_der()

bool casadi::Variable::needs_der ( ) const

Definition at line 550 of file dae_builder_internal.cpp.

550  {
551  // Only continuous variables can have derivatives
552  if (variability != Variability::CONTINUOUS) return false;
553  // Independent variables have trivial derivatives (1)
554  if (causality == Causality::INDEPENDENT) return false;
555  // Inputs are assumed piecewise constant
556  if (causality == Causality::INPUT) return false;
557  // Other variables may have derivatives
558  return true;
559 }

References causality, casadi::CONTINUOUS, casadi::INDEPENDENT, casadi::INPUT, and variability.

Referenced by get_der().

◆ permitted()

bool casadi::Variable::permitted ( Category  cat) const

Definition at line 599 of file dae_builder_internal.cpp.

599  {
600  for (auto c : categories()) {
601  if (c == cat) return true;
602  }
603  return false;
604 }
std::vector< Category > categories() const

References categories().

◆ set_attribute() [1/3]

void casadi::Variable::set_attribute ( Attribute  a,
const std::string &  val 
)

Set by attribute name

Definition at line 429 of file dae_builder_internal.cpp.

429  {
430  switch (a) {
432  stringvalue = val;
433  return;
434  default:
435  break;
436  }
437 }

References casadi::STRINGVALUE, and stringvalue.

◆ set_attribute() [2/3]

void casadi::Variable::set_attribute ( Attribute  a,
const std::vector< double > &  val 
)

Set by attribute name

Definition at line 405 of file dae_builder_internal.cpp.

405  {
406  // Quick return if scalar
407  if (val.size() == 1) {
408  set_attribute(a, val.front());
409  return;
410  }
411  // If not scalar, size must be number of elements
412  casadi_assert(val.size() == numel, "Wrong size for attribute " + to_string(a));
413  // Handle vector attributes
414  switch (a) {
415  case Attribute::START:
416  std::copy(val.begin(), val.end(), start.begin());
417  return;
418  case Attribute::VALUE:
419  std::copy(val.begin(), val.end(), value.begin());
420  return;
421  default:
422  break;
423  }
424  casadi_error("Cannot handle: " + to_string(a));
425 }
void set_attribute(Attribute a, double val)

References numel, set_attribute(), casadi::START, start, casadi::to_string(), casadi::VALUE, and value.

◆ set_attribute() [3/3]

void casadi::Variable::set_attribute ( Attribute  a,
double  val 
)

Set by attribute name

Definition at line 381 of file dae_builder_internal.cpp.

381  {
382  // Handle attributes
383  switch (a) {
384  case Attribute::MIN:
385  min = val;
386  return;
387  case Attribute::MAX:
388  max = val;
389  return;
390  case Attribute::NOMINAL:
391  nominal = val;
392  return;
393  case Attribute::START:
394  std::fill(start.begin(), start.end(), val);
395  return;
396  case Attribute::VALUE:
397  std::fill(value.begin(), value.end(), val);
398  return;
399  default:
400  break;
401  }
402  casadi_error("Cannot handle: " + to_string(a));
403 }

References casadi::MAX, max, casadi::MIN, min, casadi::NOMINAL, nominal, casadi::START, start, casadi::to_string(), casadi::VALUE, and value.

Referenced by casadi::DaeBuilderInternal::add(), casadi::DaeBuilderInternal::set_attribute(), set_attribute(), and casadi::DaeBuilderInternal::set_string_attribute().

◆ size()

casadi_int casadi::Variable::size ( Attribute  a) const

Definition at line 310 of file dae_builder_internal.cpp.

310  {
311  switch (a) {
312  case Attribute::START: // Fall-through
313  case Attribute::VALUE:
314  // Vector-valued attribute
315  return numel;
316  default:
317  break;
318  }
319  return 1;
320 }

References numel, casadi::START, and casadi::VALUE.

Referenced by get_attribute(), casadi::DaeBuilderInternal::set_attribute(), and casadi::DaeBuilderInternal::size().

Friends And Related Function Documentation

◆ DaeBuilderInternal

friend class DaeBuilderInternal
friend

Definition at line 82 of file dae_builder_internal.hpp.

Member Data Documentation

◆ bind

casadi_int casadi::Variable::bind

◆ category

Category casadi::Variable::category

◆ causality

Causality casadi::Variable::causality

◆ dependencies

std::vector<casadi_int> casadi::Variable::dependencies
mutable

◆ dependenciesKind

std::vector<DependenciesKind> casadi::Variable::dependenciesKind
mutable

◆ dependency

bool casadi::Variable::dependency

◆ der

casadi_int casadi::Variable::der

◆ der_of

casadi_int casadi::Variable::der_of

Type specific attributes common to all types, cf. Table FMI 3.0 specification

Definition at line 127 of file dae_builder_internal.hpp.

Referenced by export_xml(), get_der(), and casadi::DaeBuilderInternal::import_model_variables().

◆ description

std::string casadi::Variable::description

Attributes common to all types of variables, cf. Table 17 in FMI specification

Definition at line 105 of file dae_builder_internal.hpp.

Referenced by casadi::DaeBuilderInternal::add(), and export_xml().

◆ dimension

std::vector<casadi_int> casadi::Variable::dimension

Definition at line 97 of file dae_builder_internal.hpp.

Referenced by get_der().

◆ display_unit

std::string casadi::Variable::display_unit

Type specific attributes common to all types, cf. Table FMI 3.0 specification

Definition at line 118 of file dae_builder_internal.hpp.

Referenced by casadi::DaeBuilderInternal::add(), and export_xml().

◆ ieq

MX casadi::Variable::ieq

◆ in_rhs

bool casadi::Variable::in_rhs

◆ index

casadi_int casadi::Variable::index

◆ initial

Initial casadi::Variable::initial

Type specific attributes common to all types, cf. Table FMI 3.0 specification

Definition at line 119 of file dae_builder_internal.hpp.

Referenced by casadi::DaeBuilderInternal::add(), export_xml(), and has_start().

◆ max

double casadi::Variable::max

Type specific attributes common to all types, cf. Table FMI 3.0 specification

Definition at line 124 of file dae_builder_internal.hpp.

Referenced by casadi::DaeBuilderInternal::add(), export_xml(), get_attribute(), casadi::FmuInternal::init(), casadi::DaeBuilderInternal::load_fmi_description(), and set_attribute().

◆ min

double casadi::Variable::min

Type specific attributes common to all types, cf. Table FMI 3.0 specification

Definition at line 123 of file dae_builder_internal.hpp.

Referenced by casadi::DaeBuilderInternal::add(), export_xml(), get_attribute(), casadi::FmuInternal::init(), casadi::DaeBuilderInternal::load_fmi_description(), and set_attribute().

◆ name

std::string casadi::Variable::name

◆ nominal

double casadi::Variable::nominal

Type specific attributes common to all types, cf. Table FMI 3.0 specification

Definition at line 125 of file dae_builder_internal.hpp.

Referenced by casadi::DaeBuilderInternal::add(), export_xml(), get_attribute(), casadi::FmuInternal::init(), and set_attribute().

◆ numel

casadi_int casadi::Variable::numel

◆ parent

casadi_int casadi::Variable::parent

◆ start

std::vector<double> casadi::Variable::start

Type specific attributes common to all types, cf. Table FMI 3.0 specification

Definition at line 126 of file dae_builder_internal.hpp.

Referenced by casadi::DaeBuilderInternal::add(), export_xml(), get_attribute(), casadi::DaeBuilderInternal::load_fmi_description(), casadi::DaeBuilderInternal::read_identifier(), and set_attribute().

◆ stringvalue

std::string casadi::Variable::stringvalue

◆ type

Type casadi::Variable::type

Attributes common to all types of variables, cf. Table 17 in FMI specification

Definition at line 106 of file dae_builder_internal.hpp.

Referenced by casadi::DaeBuilderInternal::add(), export_xml(), casadi::DaeBuilder::get(), casadi::Fmu2::init(), and casadi::Fmu3::init().

◆ unit

std::string casadi::Variable::unit

Type specific attributes common to all types, cf. Table FMI 3.0 specification

Definition at line 117 of file dae_builder_internal.hpp.

Referenced by casadi::DaeBuilderInternal::add(), and export_xml().

◆ v

MX casadi::Variable::v

◆ value

std::vector<double> casadi::Variable::value

◆ value_reference

unsigned int casadi::Variable::value_reference

Attributes common to all types of variables, cf. Table 17 in FMI specification

Definition at line 104 of file dae_builder_internal.hpp.

Referenced by casadi::DaeBuilderInternal::add(), export_xml(), casadi::DaeBuilderInternal::generate_model_structure(), casadi::FmuInternal::init(), casadi::Fmu2::init(), and casadi::Fmu3::init().

◆ variability

Variability casadi::Variable::variability

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