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 83 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 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

◆ export_xml()

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

Definition at line 439 of file dae_builder_internal.cpp.

439  {
440  // Create new XmlNode
441  XmlNode r;
442  r.name = to_string(type);
443  // Name of variable
444  r.set_attribute("name", name);
445  // Value reference
446  r.set_attribute("valueReference", static_cast<casadi_int>(value_reference));
447  // Description, if any
448  if (!description.empty()) r.set_attribute("description", description);
449  // Causality
451  r.set_attribute("causality", to_string(causality));
452  // Variability (real variables are continuous by default)
454  r.set_attribute("variability", to_string(variability));
455  // Initial property
456  if (initial != Initial::NA && initial != Initial::NUMEL) {
457  r.set_attribute("initial", to_string(initial));
458  }
459  // Minimum attribute
460  if (min != -inf) {
461  if (is_real()) {
462  r.set_attribute("min", min);
463  } else {
464  r.set_attribute("min", static_cast<casadi_int>(min));
465  }
466  }
467  // Maximum attribute
468  if (max != inf) {
469  if (is_real()) {
470  r.set_attribute("max", max);
471  } else {
472  r.set_attribute("max", static_cast<casadi_int>(max));
473  }
474  }
475  // Unit
476  if (!unit.empty()) r.set_attribute("unit", unit);
477  // Display unit
478  if (!display_unit.empty()) r.set_attribute("displayUnit", display_unit);
479  // Nominal value, only for floats
480  if (is_real() && nominal != 1.) r.set_attribute("nominal", nominal);
481  // Start attribute, if any
482  if (has_start()) {
483  if (type == Type::BINARY || type == Type::STRING) {
484  casadi_warning("Start attribute for String, Binary not implemented.");
485  } else {
486  // Convert to string
487  std::stringstream ss;
488  for (size_t i = 0; i < start.size(); ++i) {
489  if (i > 0) ss << " ";
490  if (is_real()) {
491  ss << start.at(i);
492  } else {
493  ss << static_cast<casadi_int>(start.at(i));
494  }
495  }
496  r.set_attribute("start", ss.str());
497  }
498  }
499  // Derivative attribute, if any
500  if (der_of >= 0) {
501  r.set_attribute("derivative",
502  static_cast<casadi_int>(self.variable(der_of).value_reference));
503  }
504  // Return XML representation
505  return r;
506 }
const double inf
infinity
Definition: calculus.hpp:50
std::string to_string(TypeFmi2 v)
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 297 of file dae_builder_internal.cpp.

297  {
298  // Only if scalar
299  casadi_assert(numel == 1, "Variable " + name + " is not scalar");
300  // Handle attributes
301  switch (a) {
302  case Attribute::MIN:
303  if (val) *val = min;
304  return;
305  case Attribute::MAX:
306  if (val) *val = max;
307  return;
308  case Attribute::NOMINAL:
309  if (val) *val = nominal;
310  return;
311  case Attribute::START:
312  if (val) *val = start.front();
313  return;
314  case Attribute::VALUE:
315  if (val) *val = value.front();
316  return;
317  default:
318  break;
319  }
320  casadi_error("Cannot handle: " + to_string(a));
321 }
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(), 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 342 of file dae_builder_internal.cpp.

342  {
343  switch (a) {
345  if (val) *val = stringvalue;
346  return;
347  default:
348  break;
349  }
350  casadi_error("Cannot handle: " + to_string(a));
351 }
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 323 of file dae_builder_internal.cpp.

323  {
324  // Resize return
325  if (val) val->resize(size(a));
326  // Quick return if scalar
327  if (size(a) == 1) return get_attribute(a, val ? &val->front() : nullptr);
328  // Handle vector attributes
329  switch (a) {
330  case Attribute::START:
331  if (val) std::copy(start.begin(), start.end(), val->begin());
332  return;
333  case Attribute::VALUE:
334  if (val) std::copy(start.begin(), start.end(), val->begin());
335  return;
336  default:
337  break;
338  }
339  casadi_error("Cannot handle: " + to_string(a));
340 }
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(), and casadi::VALUE.

◆ get_der() [1/2]

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

Definition at line 530 of file dae_builder_internal.cpp.

530  {
532  // Time derivative of independent variable is 1
533  return 1;
534  } else if (needs_der()) {
535  casadi_assert(der >= 0, "Variable " + name + " has no time derivative");
536  // Should have a derviative expression
537  return self.variable(der).v;
538  } else {
539  // Constant or piecewise constant variable
540  return MX::zeros(v.sparsity());
541  }
542 }
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:592
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 544 of file dae_builder_internal.cpp.

544  {
545  // Create a new derivative variable, if needed
546  if (may_allocate && needs_der() && der < 0) {
547  Variable& der_v = self.new_variable("der(" + name + ")", dimension);
548  self.categorize(der_v.index, Category::Z);
549  der_v.der_of = index;
550  der_v.parent = index;
551  der = der_v.index;
552  // Add to list of derivatives
553  self.derivatives_.push_back(der_v.index);
554  }
555  // Call the const overload
556  return get_der(const_cast<const DaeBuilderInternal&>(self));
557 }
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 195 of file dae_builder_internal.hpp.

195 {return bind >= 0;}

◆ has_start()

bool casadi::Variable::has_start ( ) const

Definition at line 509 of file dae_builder_internal.cpp.

509  {
510  // Rules, according to the FMI 3.0 specification, Section 2.4.7.5.
511  if (initial == Initial::EXACT || initial == Initial::APPROX) return true;
512  if (initial == Initial::CALCULATED || causality == Causality::INDEPENDENT) return false;
513  if (causality == Causality::PARAMETER) return true;
514  if (causality == Causality::INPUT) return true;
515  if (variability == Variability::CONSTANT) return true;
516  return false;
517 }

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 184 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 190 of file dae_builder_internal.hpp.

190  {
191  return !(type==Type::STRING ? stringvalue.empty() : std::isnan(value.front()));
192  }

References casadi::STRING.

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

◆ needs_der()

bool casadi::Variable::needs_der ( ) const

Definition at line 519 of file dae_builder_internal.cpp.

519  {
520  // Only continuous variables can have derivatives
521  if (variability != Variability::CONTINUOUS) return false;
522  // Independent variables have trivial derivatives (1)
523  if (causality == Causality::INDEPENDENT) return false;
524  // Inputs are assumed piecewise constant
525  if (causality == Causality::INPUT) return false;
526  // Other variables may have derivatives
527  return true;
528 }

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

Referenced by get_der().

◆ set_attribute() [1/3]

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

Set by attribute name

Definition at line 398 of file dae_builder_internal.cpp.

398  {
399  switch (a) {
401  stringvalue = val;
402  return;
403  default:
404  break;
405  }
406 }

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 377 of file dae_builder_internal.cpp.

377  {
378  // Quick return if scalar
379  if (val.size() == 1) return set_attribute(a, val.front());
380  // If not scalar, size must be number of elements
381  casadi_assert(val.size() == numel, "Wrong size for attribute " + to_string(a));
382  // Handle vector attributes
383  switch (a) {
384  case Attribute::START:
385  std::copy(val.begin(), val.end(), start.begin());
386  return;
387  case Attribute::VALUE:
388  std::copy(val.begin(), val.end(), value.begin());
389  return;
390  default:
391  break;
392  }
393  casadi_error("Cannot handle: " + to_string(a));
394 }
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 353 of file dae_builder_internal.cpp.

353  {
354  // Handle attributes
355  switch (a) {
356  case Attribute::MIN:
357  min = val;
358  return;
359  case Attribute::MAX:
360  max = val;
361  return;
362  case Attribute::NOMINAL:
363  nominal = val;
364  return;
365  case Attribute::START:
366  std::fill(start.begin(), start.end(), val);
367  return;
368  case Attribute::VALUE:
369  std::fill(value.begin(), value.end(), val);
370  return;
371  default:
372  break;
373  }
374  casadi_error("Cannot handle: " + to_string(a));
375 }

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

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

◆ size()

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

Definition at line 285 of file dae_builder_internal.cpp.

285  {
286  switch (a) {
287  case Attribute::START: // Fall-through
288  case Attribute::VALUE:
289  // Vector-valued attribute
290  return numel;
291  default:
292  break;
293  }
294  return 1;
295 }

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 84 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 129 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 107 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 99 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 120 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

Definition at line 140 of file dae_builder_internal.hpp.

Referenced by casadi::DaeBuilderInternal::set_category().

◆ 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 121 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 126 of file dae_builder_internal.hpp.

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

◆ min

double casadi::Variable::min

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().

◆ 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 127 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 128 of file dae_builder_internal.hpp.

Referenced by casadi::DaeBuilderInternal::add(), export_xml(), get_attribute(), 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 108 of file dae_builder_internal.hpp.

Referenced by casadi::DaeBuilderInternal::add(), export_xml(), 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 119 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 106 of file dae_builder_internal.hpp.

Referenced by 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: