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

Interface to a binary FMU, adhering to FMI version 2.0. More...

#include <fmu2.hpp>

Detailed Description

Author
Joel Andersson
Date
2023

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

Definition at line 42 of file fmu2.hpp.

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

Classes

struct  Value
 

Public Types

using weak_ref_type = WeakRefInternal
 

Public Member Functions

 Fmu2 (const std::string &name, const std::vector< std::string > &scheme_in, const std::vector< std::string > &scheme_out, const std::map< std::string, std::vector< size_t >> &scheme, const std::vector< std::string > &aux)
 
 ~Fmu2 () override
 Destructor. More...
 
std::string class_name () const override
 Get type name. More...
 
void init (const DaeBuilderInternal *dae) override
 
int init_mem (FmuMemory *m) const override
 Initalize memory block. More...
 
FmuMemory * alloc_mem (const FmuFunction &f) const override
 Create memory block. More...
 
void free_mem (void *mem) const override
 Free memory block. More...
 
void finalize () override
 
void load_functions () override
 
std::string system_infix () const override
 
void * instantiate () const override
 
void free_instance (void *instance) const override
 
int reset (void *instance)
 
int enter_initialization_mode (void *instance) const override
 
int exit_initialization_mode (void *instance) const override
 
int enter_continuous_time_mode (void *instance) const override
 
int update_discrete_states (void *instance, EventMemory *eventmem) const override
 
int get_derivatives (void *instance, double *derivatives, size_t nx) const override
 
int set_time (void *instance, double t) const override
 
int set_real (void *instance, const unsigned int *vr, size_t n_vr, const double *values, size_t n_values) const override
 
int get_real (void *instance, const unsigned int *vr, size_t n_vr, double *values, size_t n_values, FmuMemory *m=nullptr) const override
 
int get_directional_derivative (void *instance, const unsigned int *vr_out, size_t n_out, const unsigned int *vr_in, size_t n_in, const double *seed, size_t n_seed, double *sensitivity, size_t n_sensitivity) const override
 
int set_values (void *instance) const override
 
int get_aux (void *instance) override
 
int get_aux_impl (void *instance, Value &aux_value) const
 
void get_stats (FmuMemory *m, Dict *stats, const std::vector< std::string > &name_in, const InputStruct *in) const override
 Get stats. More...
 
void serialize_body (SerializingStream &s) const override
 
virtual int get_adjoint_derivative (void *instance, const unsigned int *vr_out, size_t n_out, const unsigned int *vr_in, size_t n_in, const double *seed, size_t n_seed, double *sensitivity, size_t n_sensitivity) const
 
void disp (std::ostream &stream, bool more) const override
 Print. More...
 
size_t n_in () const
 Get the number of scheme inputs. More...
 
size_t n_out () const
 Get the number of scheme outputs. More...
 
size_t index_in (const std::string &n) const
 
size_t index_out (const std::string &n) const
 
Sparsity jac_sparsity (const std::vector< size_t > &osub, const std::vector< size_t > &isub) const
 
Sparsity hess_sparsity (const std::vector< size_t > &r, const std::vector< size_t > &c) const
 
std::string desc_in (FmuMemory *m, size_t id, bool more=true) const
 
template<typename T >
T * load_function (const std::string &symname)
 
int discrete_states_iter (void *instance) const
 
void set (FmuMemory *m, size_t ind, const double *value) const
 
void request (FmuMemory *m, size_t ind) const
 
int set_all (FmuMemory *m, const double *values, size_t n_values) const
 
int get_all (FmuMemory *m, double *values, size_t n_values) const
 
int eval (FmuMemory *m) const
 
void get (FmuMemory *m, size_t id, double *value) const
 
void set_fwd (FmuMemory *m, casadi_int nseed, const casadi_int *id, const double *v) const
 
void set_fwd (FmuMemory *m, size_t ind, const double *v) const
 
void request_fwd (FmuMemory *m, casadi_int nsens, const casadi_int *id, const casadi_int *wrt_id) const
 
void request_fwd (FmuMemory *m, casadi_int ind) const
 
int eval_fwd (FmuMemory *m, bool independent_seeds) const
 
int eval_ad (FmuMemory *m) const
 
int eval_fd (FmuMemory *m, bool independent_seeds) const
 
void get_fwd (FmuMemory *m, casadi_int nsens, const casadi_int *id, double *v) const
 
void get_fwd (FmuMemory *m, size_t ind, double *v) const
 
void set_adj (FmuMemory *m, casadi_int nseed, const casadi_int *id, const double *v) const
 
void set_adj (FmuMemory *m, size_t ind, const double *v) const
 
void request_adj (FmuMemory *m, casadi_int nsens, const casadi_int *id, const casadi_int *wrt_id) const
 
void request_adj (FmuMemory *m, casadi_int ind) const
 
int eval_adj (FmuMemory *m) const
 
void get_adj (FmuMemory *m, casadi_int nsens, const casadi_int *id, double *v) const
 
void get_adj (FmuMemory *m, size_t ind, double *v) const
 
void gather_fwd (FmuMemory *m) const
 
void gather_adj (FmuMemory *m) const
 
void gather_io (FmuMemory *m) const
 
void serialize (SerializingStream &s) const
 
virtual void serialize_type (SerializingStream &s) const
 
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...
 
std::vector< double > all_nominal_in (size_t i) const
 Retreive nominal values. More...
 
std::vector< double > all_nominal_out (size_t i) const
 Retreive nominal values. More...
 

Static Public Member Functions

static std::string dll_infix ()
 
static void logger (fmi2ComponentEnvironment componentEnvironment, fmi2String instanceName, fmi2Status status, fmi2String category, fmi2String message,...)
 
static Fmu2 * deserialize (DeserializingStream &s)
 
static std::string dll_suffix ()
 
static std::string fmi3_dll_infix ()
 
static Dict compile_fmu (const std::string &name, const Dict &files, const Dict &opts)
 
static std::string pack_fmu (const Dict &files, const std::string &path)
 

Public Attributes

std::vector< fmi2ValueReference > vr_real_
 
std::vector< fmi2ValueReference > vr_integer_
 
std::vector< fmi2ValueReference > vr_boolean_
 
std::vector< fmi2ValueReference > vr_string_
 
std::vector< fmi2Real > init_real_
 
std::vector< fmi2Integer > init_integer_
 
std::vector< fmi2Boolean > init_boolean_
 
std::vector< std::string > init_string_
 
std::vector< std::string > vn_aux_real_
 
std::vector< std::string > vn_aux_integer_
 
std::vector< std::string > vn_aux_boolean_
 
std::vector< std::string > vn_aux_string_
 
std::vector< fmi2ValueReference > vr_aux_real_
 
std::vector< fmi2ValueReference > vr_aux_integer_
 
std::vector< fmi2ValueReference > vr_aux_boolean_
 
std::vector< fmi2ValueReference > vr_aux_string_
 
fmi2InstantiateTYPE * instantiate_
 
fmi2FreeInstanceTYPE * free_instance_
 
fmi2ResetTYPE * reset_
 
fmi2SetupExperimentTYPE * setup_experiment_
 
fmi2EnterInitializationModeTYPE * enter_initialization_mode_
 
fmi2ExitInitializationModeTYPE * exit_initialization_mode_
 
fmi2EnterContinuousTimeModeTYPE * enter_continuous_time_mode_
 
fmi2GetDerivativesTYPE * get_derivatives_
 
fmi2SetTimeTYPE * set_time_
 
fmi2GetRealTYPE * get_real_
 
fmi2SetRealTYPE * set_real_
 
fmi2GetBooleanTYPE * get_boolean_
 
fmi2SetBooleanTYPE * set_boolean_
 
fmi2GetIntegerTYPE * get_integer_
 
fmi2SetIntegerTYPE * set_integer_
 
fmi2GetStringTYPE * get_string_
 
fmi2SetStringTYPE * set_string_
 
fmi2GetDirectionalDerivativeTYPE * get_directional_derivative_
 
fmi2NewDiscreteStatesTYPE * new_discrete_states_
 
fmi2CallbackFunctions functions_
 
Value aux_value_
 

Protected Member Functions

 Fmu2 (DeserializingStream &s)
 
void initSingleton ()
 
void destroySingleton ()
 
B 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...
 

Protected Attributes

Resource resource_
 
std::string name_
 Instance name. More...
 
std::vector< std::string > scheme_in_
 
std::vector< std::string > scheme_out_
 
std::map< std::string, std::vector< size_t > > scheme_
 
std::vector< std::string > aux_
 
std::string resource_loc_
 
double fmutol_
 
std::string instance_name_
 
std::string instantiation_token_
 
bool logging_on_
 
casadi_int number_of_event_indicators_
 
bool provides_directional_derivatives_
 
bool provides_adjoint_derivatives_
 
bool can_be_instantiated_only_once_per_process_
 
double start_time_
 
Importer li_
 DLL. More...
 
std::vector< size_t > iind_
 
std::vector< size_t > iind_map_
 
std::vector< size_t > oind_
 
std::vector< size_t > oind_map_
 
bool has_independent_
 
unsigned int independent_vr_
 
std::vector< double > nominal_in_
 
std::vector< double > nominal_out_
 
std::vector< double > min_in_
 
std::vector< double > min_out_
 
std::vector< double > max_in_
 
std::vector< double > max_out_
 
std::vector< std::string > vn_in_
 
std::vector< std::string > vn_out_
 
std::vector< unsigned int > vr_in_
 
std::vector< unsigned int > vr_out_
 
std::vector< double > value_in_
 
std::vector< std::vector< size_t > > ired_
 
std::vector< std::vector< size_t > > ored_
 
Sparsity jac_sp_
 
Sparsity hess_sp_
 
bool warning_fired_discrete_states_need_update_
 
bool warning_fired_terminate_simulation_
 
bool warning_fired_nominals_of_continuous_states_changed_
 
bool warning_fired_values_of_continuous_states_changed_
 
bool warning_fired_next_event_time_defined_
 
size_t nx_
 
bool do_evaluation_dance_
 

Member Typedef Documentation

◆ weak_ref_type

Definition at line 152 of file shared_object.hpp.

Constructor & Destructor Documentation

◆ Fmu2() [1/2]

casadi::Fmu2::Fmu2 ( const std::string &  name,
const std::vector< std::string > &  scheme_in,
const std::vector< std::string > &  scheme_out,
const std::map< std::string, std::vector< size_t >> &  scheme,
const std::vector< std::string > &  aux 
)

Definition at line 532 of file fmu2.cpp.

537  : FmuInternal(name, scheme_in, scheme_out, scheme, aux) {
538  instantiate_ = nullptr;
539  free_instance_ = nullptr;
540  reset_ = nullptr;
541  setup_experiment_ = nullptr;
542  enter_initialization_mode_ = nullptr;
543  exit_initialization_mode_ = nullptr;
544  enter_continuous_time_mode_ = nullptr;
545  set_time_ = nullptr;
546  set_real_ = nullptr;
547  set_boolean_ = nullptr;
548  get_real_ = nullptr;
549  get_directional_derivative_ = nullptr;
550  new_discrete_states_ = nullptr;
551 }
fmi2ExitInitializationModeTYPE * exit_initialization_mode_
Definition: fmu2.hpp:100
fmi2SetupExperimentTYPE * setup_experiment_
Definition: fmu2.hpp:98
fmi2EnterInitializationModeTYPE * enter_initialization_mode_
Definition: fmu2.hpp:99
fmi2InstantiateTYPE * instantiate_
Definition: fmu2.hpp:95
fmi2SetTimeTYPE * set_time_
Definition: fmu2.hpp:103
fmi2ResetTYPE * reset_
Definition: fmu2.hpp:97
fmi2SetBooleanTYPE * set_boolean_
Definition: fmu2.hpp:107
fmi2SetRealTYPE * set_real_
Definition: fmu2.hpp:105
fmi2GetDirectionalDerivativeTYPE * get_directional_derivative_
Definition: fmu2.hpp:112
fmi2FreeInstanceTYPE * free_instance_
Definition: fmu2.hpp:96
fmi2EnterContinuousTimeModeTYPE * enter_continuous_time_mode_
Definition: fmu2.hpp:101
fmi2NewDiscreteStatesTYPE * new_discrete_states_
Definition: fmu2.hpp:113
fmi2GetRealTYPE * get_real_
Definition: fmu2.hpp:104
FmuInternal(const std::string &name, const std::vector< std::string > &scheme_in, const std::vector< std::string > &scheme_out, const std::map< std::string, std::vector< size_t >> &scheme, const std::vector< std::string > &aux)
Definition: fmu.cpp:442

References enter_continuous_time_mode_, enter_initialization_mode_, exit_initialization_mode_, free_instance_, get_directional_derivative_, get_real_, instantiate_, new_discrete_states_, reset_, set_boolean_, set_real_, set_time_, and setup_experiment_.

Referenced by deserialize().

◆ ~Fmu2()

casadi::Fmu2::~Fmu2 ( )
override

Definition at line 62 of file fmu2.cpp.

62  {
63 }

◆ Fmu2() [2/2]

casadi::Fmu2::Fmu2 ( DeserializingStream &  s)
explicitprotected

Definition at line 559 of file fmu2.cpp.

559  : FmuInternal(s) {
560  instantiate_ = nullptr;
561  free_instance_ = nullptr;
562  reset_ = nullptr;
563  setup_experiment_ = nullptr;
564  enter_initialization_mode_ = nullptr;
565  exit_initialization_mode_ = nullptr;
566  enter_continuous_time_mode_ = nullptr;
567  set_real_ = nullptr;
568  set_boolean_ = nullptr;
569  get_real_ = nullptr;
570  get_directional_derivative_ = nullptr;
571  new_discrete_states_ = nullptr;
572 
573  s.version("Fmu2", 2);
574  s.unpack("Fmu2::vr_real", vr_real_);
575  s.unpack("Fmu2::vr_integer", vr_integer_);
576  s.unpack("Fmu2::vr_boolean", vr_boolean_);
577  s.unpack("Fmu2::vr_string", vr_string_);
578  s.unpack("Fmu2::init_real", init_real_);
579  s.unpack("Fmu2::init_integer", init_integer_);
580  s.unpack("Fmu2::init_boolean", init_boolean_);
581  s.unpack("Fmu2::init_string", init_string_);
582 
583  s.unpack("Fmu2::vn_aux_real", vn_aux_real_);
584  s.unpack("Fmu2::vn_aux_integer", vn_aux_integer_);
585  s.unpack("Fmu2::vn_aux_boolean", vn_aux_boolean_);
586  s.unpack("Fmu2::vn_aux_string", vn_aux_string_);
587  s.unpack("Fmu2::vr_aux_real", vr_aux_real_);
588  s.unpack("Fmu2::vr_aux_integer", vr_aux_integer_);
589  s.unpack("Fmu2::vr_aux_boolean", vr_aux_boolean_);
590  s.unpack("Fmu2::vr_aux_string", vr_aux_string_);
591 }
std::vector< fmi2ValueReference > vr_string_
Definition: fmu2.hpp:82
std::vector< fmi2Real > init_real_
Definition: fmu2.hpp:83
std::vector< fmi2ValueReference > vr_boolean_
Definition: fmu2.hpp:82
std::vector< fmi2Boolean > init_boolean_
Definition: fmu2.hpp:85
std::vector< std::string > vn_aux_integer_
Definition: fmu2.hpp:89
std::vector< std::string > vn_aux_string_
Definition: fmu2.hpp:89
std::vector< fmi2Integer > init_integer_
Definition: fmu2.hpp:84
std::vector< std::string > vn_aux_real_
Definition: fmu2.hpp:89
std::vector< fmi2ValueReference > vr_integer_
Definition: fmu2.hpp:82
std::vector< std::string > vn_aux_boolean_
Definition: fmu2.hpp:89
std::vector< fmi2ValueReference > vr_aux_string_
Definition: fmu2.hpp:90
std::vector< fmi2ValueReference > vr_aux_integer_
Definition: fmu2.hpp:90
std::vector< fmi2ValueReference > vr_real_
Definition: fmu2.hpp:82
std::vector< fmi2ValueReference > vr_aux_real_
Definition: fmu2.hpp:90
std::vector< std::string > init_string_
Definition: fmu2.hpp:86
std::vector< fmi2ValueReference > vr_aux_boolean_
Definition: fmu2.hpp:90

References enter_continuous_time_mode_, enter_initialization_mode_, exit_initialization_mode_, free_instance_, get_directional_derivative_, get_real_, init_boolean_, init_integer_, init_real_, init_string_, instantiate_, new_discrete_states_, reset_, set_boolean_, set_real_, setup_experiment_, casadi::DeserializingStream::unpack(), casadi::DeserializingStream::version(), vn_aux_boolean_, vn_aux_integer_, vn_aux_real_, vn_aux_string_, vr_aux_boolean_, vr_aux_integer_, vr_aux_real_, vr_aux_string_, vr_boolean_, vr_integer_, vr_real_, and vr_string_.

Member Function Documentation

◆ all_nominal_in()

std::vector< double > casadi::FmuInternal::all_nominal_in ( size_t  i) const
inherited

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

Definition at line 707 of file fmu.cpp.

707  {
708  auto&& ind = ired_.at(i);
709  std::vector<double> n;
710  n.reserve(ind.size());
711  for (size_t k : ind) n.push_back(nominal_in_.at(k));
712  return n;
713 }
std::vector< std::vector< size_t > > ired_
Definition: fmu_impl.hpp:369
std::vector< double > nominal_in_
Definition: fmu_impl.hpp:359

References casadi::FmuInternal::ired_, and casadi::FmuInternal::nominal_in_.

◆ all_nominal_out()

std::vector< double > casadi::FmuInternal::all_nominal_out ( size_t  i) const
inherited

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

Definition at line 715 of file fmu.cpp.

715  {
716  auto&& ind = ored_.at(i);
717  std::vector<double> n;
718  n.reserve(ind.size());
719  for (size_t k : ind) n.push_back(nominal_out_.at(k));
720  return n;
721 }
std::vector< std::vector< size_t > > ored_
Definition: fmu_impl.hpp:369
std::vector< double > nominal_out_
Definition: fmu_impl.hpp:359

References casadi::FmuInternal::nominal_out_, and casadi::FmuInternal::ored_.

◆ alloc_mem()

FmuMemory * casadi::Fmu2::alloc_mem ( const FmuFunction &  f) const
overridevirtual

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

Implements casadi::FmuInternal.

Definition at line 54 of file fmu2.cpp.

54  {
55  return new Fmu2Memory(f);
56 }

◆ class_name()

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

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

Implements casadi::SharedObjectInternal.

Definition at line 55 of file fmu2.hpp.

55 { return "Fmu2";}

◆ compile_fmu()

Dict casadi::FmuInternal::compile_fmu ( const std::string &  name,
const Dict &  files,
const Dict &  opts 
)
staticinherited

Definition at line 781 of file fmu.cpp.

781  {
782  // Options
783  Dict compiler_opts;
784  std::string compiler_plugin = "shell";
785  std::vector<std::string> include_dirs;
786  for (auto&& op : opts) {
787  if (op.first == "compiler") {
788  compiler_plugin = op.second.to_string();
789  } else if (op.first == "compiler_options") {
790  compiler_opts = op.second;
791  } else if (op.first == "include_dirs") {
792  include_dirs = op.second.to_string_vector();
793  } else {
794  casadi_error("No such option: " + op.first);
795  }
796  }
797  // The C sources to compile are the .c entries of the export_fmu map (flat in CWD)
798  std::vector<std::string> csources;
799  for (auto&& kv : files) {
800  const std::string& local = kv.first;
801  if (local.size() > 2 && local.substr(local.size() - 2) == ".c") csources.push_back(local);
802  }
803  // Amalgamation translation unit: a single .c that #includes every generated source,
804  // because the shell Importer compiles exactly one source file. The compile runs in
805  // the working directory, so quoted includes resolve against the loose files there.
806  // This is a build scratch file, NOT FMU content, so it is removed below.
807  std::string amalg = name + "_all.c";
808  {
809  auto f = Filesystem::ofstream_ptr(amalg);
810  for (const std::string& c : csources) *f << "#include \"" << c << "\"\n";
811  }
812  // Compile the amalgamation into a shared library via the Importer plugin.
813  // Pass include dirs through the OS-agnostic 'include_dirs' option (the plugin
814  // prefixes each with the compiler's own include flag: -I on gcc/clang, /I on MSVC).
815  Dict cop = compiler_opts;
816  if (!include_dirs.empty()) cop["include_dirs"] = include_dirs;
817  Importer compiler(amalg, compiler_plugin, cop);
818  // Lift the binary to a stable path before the Importer destructor removes it, then
819  // discard the amalgamation scratch file (compilation is done once the Importer exists)
820  std::string dll = name + dll_suffix();
821  Filesystem::copy_file(compiler.library(), dll);
822  Filesystem::remove(amalg);
823  // Augment the file map with the compiled binary
824  // (FMI-3 layout: binaries/<dll_infix>/<modelIdentifier><suffix>)
825  Dict ret = files;
826  ret[dll] = "binaries/" + fmi3_dll_infix() + "/" + name + dll_suffix();
827  return ret;
828 }
static void copy_file(const std::string &src, const std::string &dest)
Definition: filesystem.cpp:144
static std::unique_ptr< std::ostream > ofstream_ptr(const std::string &path, std::ios_base::openmode mode=std::ios_base::out)
Definition: filesystem.cpp:115
static bool remove(const std::string &path)
Definition: filesystem.cpp:43
static std::string fmi3_dll_infix()
Definition: fmu.cpp:736
static std::string dll_suffix()
Definition: fmu.cpp:723
GenericType::Dict Dict
C++ equivalent of Python's dict or MATLAB's struct.

References casadi::Filesystem::copy_file(), casadi::FmuInternal::dll_suffix(), casadi::FmuInternal::fmi3_dll_infix(), casadi::Importer::library(), casadi::Filesystem::ofstream_ptr(), and casadi::Filesystem::remove().

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

◆ 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.

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

◆ desc_in()

std::string casadi::FmuInternal::desc_in ( FmuMemory *  m,
size_t  id,
bool  more = true 
) const
inherited

Definition at line 850 of file fmu.cpp.

850  {
851  // Create description
852  if (more) {
853  // Detailed description
854  std::stringstream ss;
855  ss << vn_in_[id] << " = " << m->ibuf_[id] << " (nominal " << nominal_in_[id]
856  << ", min " << min_in_[id] << ", max " << max_in_[id] << ")";
857  return ss.str();
858  } else {
859  return vn_in_[id];
860  }
861 }
std::vector< double > max_in_
Definition: fmu_impl.hpp:361
std::vector< std::string > vn_in_
Definition: fmu_impl.hpp:362
std::vector< double > min_in_
Definition: fmu_impl.hpp:360

References casadi::FmuMemory::ibuf_, casadi::FmuInternal::max_in_, casadi::FmuInternal::min_in_, casadi::FmuInternal::nominal_in_, and casadi::FmuInternal::vn_in_.

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

◆ deserialize()

Fmu2 * casadi::Fmu2::deserialize ( DeserializingStream &  s)
static

Definition at line 553 of file fmu2.cpp.

553  {
554  Fmu2* ret = new Fmu2(s);
555  ret->finalize();
556  return ret;
557 }
Fmu2(const std::string &name, const std::vector< std::string > &scheme_in, const std::vector< std::string > &scheme_out, const std::map< std::string, std::vector< size_t >> &scheme, const std::vector< std::string > &aux)
Definition: fmu2.cpp:532

References finalize(), and Fmu2().

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

◆ 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  }

◆ discrete_states_iter()

int casadi::FmuInternal::discrete_states_iter ( void *  instance) const
inherited

Definition at line 1248 of file fmu.cpp.

1248  {
1249  // Helper function: update_discrete_states
1250  EventMemory eventmem;
1251  const size_t max_update_iter = 10;
1252  for (size_t update_iter = 0; update_iter < max_update_iter; ++update_iter) {
1253  if (update_discrete_states(instance, &eventmem)) {
1254  casadi_warning("update_discrete_states");
1255  return 1;
1256  }
1257  // Not implemented
1258  if (eventmem.discrete_states_need_update) {
1261  casadi_warning("Discrete state update not implemented");
1262  }
1263  }
1264  if (eventmem.terminate_simulation) {
1267  casadi_warning("Terminate simulation not implemented");
1268  }
1269  }
1270  if (eventmem.nominals_of_continuous_states_changed) {
1273  casadi_warning("Nominals of continuous states not implemented");
1274  }
1275  }
1276  if (eventmem.values_of_continuous_states_changed) {
1279  casadi_warning("Values of continuous states not implemented");
1280  }
1281  }
1282  if (eventmem.next_event_time_defined) {
1285  casadi_warning("Next event time not implemented");
1286  }
1287  }
1288  // Successful return
1289  if (!eventmem.discrete_states_need_update) {
1290  return 0;
1291  }
1292  }
1293  // Too many iterations
1294  casadi_warning("Discrete state update failed");
1295  return 1;
1296 }
bool warning_fired_values_of_continuous_states_changed_
Definition: fmu_impl.hpp:377
bool warning_fired_next_event_time_defined_
Definition: fmu_impl.hpp:378
bool warning_fired_nominals_of_continuous_states_changed_
Definition: fmu_impl.hpp:376
bool warning_fired_terminate_simulation_
Definition: fmu_impl.hpp:375
bool warning_fired_discrete_states_need_update_
Definition: fmu_impl.hpp:374
virtual int update_discrete_states(void *instance, EventMemory *eventmem) const =0

References casadi::EventMemory::discrete_states_need_update, casadi::EventMemory::next_event_time_defined, casadi::EventMemory::nominals_of_continuous_states_changed, casadi::EventMemory::terminate_simulation, casadi::FmuInternal::update_discrete_states(), casadi::EventMemory::values_of_continuous_states_changed, casadi::FmuInternal::warning_fired_discrete_states_need_update_, casadi::FmuInternal::warning_fired_next_event_time_defined_, casadi::FmuInternal::warning_fired_nominals_of_continuous_states_changed_, casadi::FmuInternal::warning_fired_terminate_simulation_, and casadi::FmuInternal::warning_fired_values_of_continuous_states_changed_.

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

◆ disp()

void casadi::FmuInternal::disp ( std::ostream &  stream,
bool  more 
) const
overridevirtualinherited

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

Implements casadi::SharedObjectInternal.

Definition at line 651 of file fmu.cpp.

651  {
652  (void)more; // unused
653  stream << name_ << " " << class_name();
654 }
std::string name_
Instance name.
Definition: fmu_impl.hpp:310
virtual std::string class_name() const =0
Readable name of the internal class.

References casadi::SharedObjectInternal::class_name(), and casadi::FmuInternal::name_.

◆ dll_infix()

std::string casadi::Fmu2::dll_infix ( )
static

Definition at line 65 of file fmu2.cpp.

65  {
66 #if defined(_WIN32)
67  // Windows system
68 #ifdef _WIN64
69  return "win64";
70 #else
71  return "win32";
72 #endif
73 #elif defined(__APPLE__)
74  // OSX
75  return sizeof(void*) == 4 ? "darwin32" : "darwin64";
76 #else
77  // Linux
78  return sizeof(void*) == 4 ? "linux32" : "linux64";
79 #endif
80 }

◆ dll_suffix()

std::string casadi::FmuInternal::dll_suffix ( )
staticinherited

Definition at line 723 of file fmu.cpp.

723  {
724 #if defined(_WIN32)
725  // Windows system
726  return ".dll";
727 #elif defined(__APPLE__)
728  // OSX
729  return ".dylib";
730 #else
731  // Linux
732  return ".so";
733 #endif
734 }

Referenced by casadi::FmuInternal::compile_fmu(), and casadi::FmuInternal::finalize().

◆ enter_continuous_time_mode()

int casadi::Fmu2::enter_continuous_time_mode ( void *  instance) const
overridevirtual

Implements casadi::FmuInternal.

Definition at line 308 of file fmu2.cpp.

308  {
309  auto *c = static_cast<fmi2Component>(instance);
310  fmi2Status status = enter_continuous_time_mode_(c);
311  if (status != fmi2OK) {
312  casadi_warning("fmi2EnterContinuousTimeMode failed");
313  return 1;
314  }
315  return 0;
316 }

References enter_continuous_time_mode_.

◆ enter_initialization_mode()

int casadi::Fmu2::enter_initialization_mode ( void *  instance) const
overridevirtual

Implements casadi::FmuInternal.

Definition at line 288 of file fmu2.cpp.

288  {
289  auto *c = static_cast<fmi2Component>(instance);
290  fmi2Status status = enter_initialization_mode_(c);
291  if (status != fmi2OK) {
292  casadi_warning("fmi2EnterInitializationMode failed: " + str(status));
293  return 1;
294  }
295  return 0;
296 }

References enter_initialization_mode_, and casadi::str().

◆ eval()

int casadi::FmuInternal::eval ( FmuMemory *  m) const
inherited

Definition at line 1414 of file fmu.cpp.

1414  {
1415  // Gather inputs and outputs
1416  gather_io(m);
1417  // Pass inputs to FMU
1418  if (set_all(m, get_ptr(m->v_in_), m->v_in_.size())) return 1;
1419  // Get outputs from FMU
1420  if (get_all(m, get_ptr(m->v_out_), m->v_out_.size())) return 1;
1421  // Collect requested variables
1422  auto it = m->v_out_.begin();
1423  for (size_t id : m->id_out_) {
1424  m->obuf_[id] = *it++;
1425  }
1426  // Successful return
1427  return 0;
1428 }
int get_all(FmuMemory *m, double *values, size_t n_values) const
Definition: fmu.cpp:1400
void gather_io(FmuMemory *m) const
Definition: fmu.cpp:1477
int set_all(FmuMemory *m, const double *values, size_t n_values) const
Definition: fmu.cpp:1372
T * get_ptr(std::vector< T > &v)
Get a pointer to the data contained in the vector.

References casadi::FmuInternal::gather_io(), casadi::FmuInternal::get_all(), casadi::get_ptr(), casadi::FmuMemory::id_out_, casadi::FmuMemory::obuf_, casadi::FmuInternal::set_all(), casadi::FmuMemory::v_in_, and casadi::FmuMemory::v_out_.

◆ eval_ad()

int casadi::FmuInternal::eval_ad ( FmuMemory *  m) const
inherited

Definition at line 912 of file fmu.cpp.

912  {
913  // Number of inputs and outputs
914  size_t n_known = m->id_in_.size();
915  size_t n_unknown = m->id_out_.size();
916  // Quick return if nothing to be calculated
917  if (n_unknown == 0) return 0;
918  // Quick return if no seeds
919  if (n_known == 0) {
920  std::fill(m->d_out_.begin(), m->d_out_.end(), 0);
921  return 0;
922  }
923  // Evalute (should not be necessary)
924  if (get_real(m->instance, get_ptr(m->vr_out_), n_unknown, get_ptr(m->v_out_), n_unknown, m)) {
925  casadi_warning("FMU evaluation failed");
926  return 1;
927  }
928  // Evaluate directional derivatives
929  m->n_get_directional++;
930  auto start = std::chrono::high_resolution_clock::now();
931  int flag = get_directional_derivative(m->instance,
932  get_ptr(m->vr_out_), n_unknown, get_ptr(m->vr_in_), n_known,
933  get_ptr(m->d_in_), n_known, get_ptr(m->d_out_), n_unknown);
934  auto stop = std::chrono::high_resolution_clock::now();
935  m->t_get_directional += std::chrono::duration_cast<std::chrono::microseconds>(
936  stop - start).count() / 1e6;
937  // Collect requested variables
938  if (!flag) {
939  auto it = m->d_out_.begin();
940  for (size_t id : m->id_out_) {
941  m->osens_[id] = *it++;
942  }
943  }
944  // Return result of the evaluation
945  return flag;
946 }
virtual int get_directional_derivative(void *instance, const unsigned int *vr_out, size_t n_out, const unsigned int *vr_in, size_t n_in, const double *seed, size_t n_seed, double *sensitivity, size_t n_sensitivity) const =0
virtual int get_real(void *instance, const unsigned int *vr, size_t n_vr, double *values, size_t n_values, FmuMemory *m=nullptr) const =0

References casadi::FmuMemory::d_in_, casadi::FmuMemory::d_out_, casadi::FmuInternal::get_directional_derivative(), casadi::get_ptr(), casadi::FmuInternal::get_real(), casadi::FmuMemory::id_in_, casadi::FmuMemory::id_out_, casadi::FmuMemory::instance, casadi::FmuMemory::n_get_directional, casadi::FmuMemory::osens_, casadi::FmuMemory::t_get_directional, casadi::FmuMemory::v_out_, casadi::FmuMemory::vr_in_, and casadi::FmuMemory::vr_out_.

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

◆ eval_adj()

int casadi::FmuInternal::eval_adj ( FmuMemory *  m) const
inherited

Definition at line 879 of file fmu.cpp.

879  {
880  // Gather input and output indices
881  gather_adj(m);
882  // Quick return if nothing to be calculated
883  if (m->id_in_.size() == 0) return 0;
884  // Check if no seeds
885  int flag;
886  if (m->id_out_.size() == 0) {
887  // Sensitivities are zero, trivially
888  std::fill(m->d_in_.begin(), m->d_in_.end(), 0);
889  flag = 0;
890  } else {
891  // Evaluate adjoint derivatives
892  m->n_get_adjoint++;
893  auto start = std::chrono::high_resolution_clock::now();
894  flag = get_adjoint_derivative(m->instance,
895  get_ptr(m->vr_out_), m->id_out_.size(), get_ptr(m->vr_in_), m->id_in_.size(),
896  get_ptr(m->d_out_), m->id_out_.size(), get_ptr(m->d_in_), m->id_in_.size());
897  auto stop = std::chrono::high_resolution_clock::now();
898  m->t_get_adjoint += std::chrono::duration_cast<std::chrono::microseconds>(
899  stop - start).count() / 1e6;
900  }
901  // Collect requested variables
902  if (!flag) {
903  auto it = m->d_in_.begin();
904  for (size_t id : m->id_in_) {
905  m->isens_[id] = *it++;
906  }
907  }
908  // Return results of the evaluation
909  return flag;
910 }
virtual int get_adjoint_derivative(void *instance, const unsigned int *vr_out, size_t n_out, const unsigned int *vr_in, size_t n_in, const double *seed, size_t n_seed, double *sensitivity, size_t n_sensitivity) const
Definition: fmu.cpp:586
void gather_adj(FmuMemory *m) const
Definition: fmu.cpp:1520

References casadi::FmuMemory::d_in_, casadi::FmuMemory::d_out_, casadi::FmuInternal::gather_adj(), casadi::FmuInternal::get_adjoint_derivative(), casadi::get_ptr(), casadi::FmuMemory::id_in_, casadi::FmuMemory::id_out_, casadi::FmuMemory::instance, casadi::FmuMemory::isens_, casadi::FmuMemory::n_get_adjoint, casadi::FmuMemory::t_get_adjoint, casadi::FmuMemory::vr_in_, and casadi::FmuMemory::vr_out_.

◆ eval_fd()

int casadi::FmuInternal::eval_fd ( FmuMemory *  m,
bool  independent_seeds 
) const
inherited

Definition at line 948 of file fmu.cpp.

948  {
949  // Number of inputs and outputs
950  size_t n_known = m->id_in_.size();
951  size_t n_unknown = m->id_out_.size();
952  // Quick return if nothing to be calculated
953  if (n_unknown == 0) return 0;
954  // Quick return if no seeds
955  if (n_known == 0) {
956  std::fill(m->d_out_.begin(), m->d_out_.end(), 0);
957  return 0;
958  }
959  // Evalute (should not be necessary)
960  if (get_real(m->instance, get_ptr(m->vr_out_), n_unknown, get_ptr(m->v_out_), n_unknown, m)) {
961  casadi_warning("Evaluating FMU failed");
962  return 1;
963  }
964  // Make outputs dimensionless
965  for (size_t k = 0; k < n_unknown; ++k) m->v_out_[k] /= nominal_out_[m->id_out_[k]];
966  // Number of points in FD stencil
967  casadi_int n_points = n_fd_points(m->self.fd_);
968  // Offset for points
969  casadi_int offset = fd_offset(m->self.fd_);
970  // Memory for perturbed outputs
971  m->fd_out_.resize(n_points * n_unknown);
972  // Which inputs are in bounds
973  m->in_bounds_.resize(n_known);
974  // Memory for perturbed inputs
975  m->v_pert_.resize(n_known);
976  // Do any any inputs need flipping?
977  m->flip_.resize(n_known);
978  size_t first_flip = -1;
979  for (size_t i = 0; i < n_known; ++i) {
980  // Try to take step
981  double test = m->v_in_[i] + m->self.step_ * m->d_in_[i];
982  // Check if in bounds
983  size_t id = m->id_in_[i];
984  if (test >= min_in_[id] && test <= max_in_[id]) {
985  // Positive perturbation is fine
986  m->flip_[i] = false;
987  } else {
988  // Try negative direction instead
989  test = m->v_in_[i] - m->self.step_ * m->d_in_[i];
990  casadi_assert(test >= min_in_[id] && test <= max_in_[id],
991  "Cannot perturb " + vn_in_[id] + " at " + str(m->v_in_[i]) + ", min " + str(min_in_[id])
992  + ", max " + str(max_in_[id]) + ", nominal " + str(nominal_in_[id]));
993  m->flip_[i] = true;
994  if (first_flip == size_t(-1)) first_flip = i;
995  }
996  }
997  // If seeds are not independent, we have to flip the sign for all of the seeds or none
998  if (first_flip != size_t(-1) && !independent_seeds) {
999  // Flip the rest of the seeds
1000  for (size_t i = 0; i < n_known; ++i) {
1001  if (!m->flip_[i]) {
1002  // Test negative direction
1003  double test = m->v_in_[i] - m->self.step_ * m->d_in_[i];
1004  size_t id = m->id_in_[i];
1005  casadi_assert(test >= min_in_[id] && test <= max_in_[id],
1006  "Cannot perturb both " + vn_in_[id] + " and " + vn_in_[first_flip]);
1007  // Flip it too
1008  m->flip_[i] = true;
1009  }
1010  }
1011  }
1012  // All perturbed outputs
1013  const double* yk_all[5] = {nullptr};
1014 
1015  // Calculate all perturbed outputs
1016  for (casadi_int k = 0; k < n_points; ++k) {
1017  // Where to save the perturbed outputs
1018  double* yk = &m->fd_out_[n_unknown * k];
1019  casadi_assert_dev(k < 5);
1020  yk_all[k] = yk;
1021  // If unperturbed output, quick return
1022  if (k == offset) {
1023  casadi_copy(get_ptr(m->v_out_), n_unknown, yk);
1024  continue;
1025  }
1026  // Perturbation size
1027  double pert = (k - offset) * m->self.step_;
1028  // Perturb inputs, if allowed
1029  for (size_t i = 0; i < n_known; ++i) {
1030  // Try to take step
1031  double sign = m->flip_[i] ? -1 : 1;
1032  double test = m->v_in_[i] + pert * sign * m->d_in_[i];
1033  // Check if in bounds
1034  size_t id = m->id_in_[i];
1035  m->in_bounds_[i] = test >= min_in_[id] && test <= max_in_[id];
1036  // Take step, if allowed
1037  m->v_pert_[i] = m->in_bounds_[i] ? test : m->v_in_[i];
1038  }
1039  // Pass perturbed inputs to FMU
1040  if (set_all(m, get_ptr(m->v_pert_), m->v_pert_.size())) return 1;
1041  // Evaluate perturbed FMU
1042  if (get_all(m, yk, n_unknown)) return 1;
1043  // Post-process yk if there was any scaling
1044  if (independent_seeds) {
1045  for (size_t i = 0; i < n_unknown; ++i) {
1046  // Variable id
1047  size_t id = m->id_out_[i];
1048  // Differentiation with respect to what variable
1049  size_t wrt_id = m->wrt_.at(id);
1050  // Find the corresponding input variable
1051  size_t wrt_i;
1052  for (wrt_i = 0; wrt_i < n_known; ++wrt_i) {
1053  if (m->id_in_[wrt_i] == wrt_id) break;
1054  }
1055  // Check if in bounds
1056  if (m->in_bounds_.at(wrt_i) && (m->self.fd_flip_ || !m->flip_[wrt_i])) {
1057  // Input was in bounds: Keep output, make dimensionless
1058  yk[i] /= nominal_out_[m->id_out_[i]];
1059  } else {
1060  // Input was out of bounds: Discard output
1061  yk[i] = nan;
1062  }
1063  }
1064  }
1065  }
1066  // Restore FMU inputs
1067  if (set_all(m, get_ptr(m->v_in_), m->v_in_.size())) return 1;
1068  // Step size
1069  double h = m->self.step_;
1070 
1071  // Calculate FD approximation
1072  finite_diff(m->self.fd_, yk_all, get_ptr(m->d_out_), h, n_unknown, eps);
1073 
1074  // If seeds are dependent, quick return
1075  if (!independent_seeds) {
1076  // Simpy copy the results to output (no validation)
1077  for (size_t ind = 0; ind < m->id_out_.size(); ++ind) {
1078  m->osens_[m->id_out_[ind]] = m->d_out_[ind];
1079  }
1080  return 0;
1081  }
1082 
1083  // Collect requested variables
1084  for (size_t ind = 0; ind < m->id_out_.size(); ++ind) {
1085  // Variable id
1086  size_t id = m->id_out_[ind];
1087  // With respect to what variable
1088  size_t wrt = m->wrt_[id];
1089  // Find the corresponding input variable
1090  size_t wrt_i;
1091  for (wrt_i = 0; wrt_i < n_known; ++wrt_i) {
1092  if (m->id_in_[wrt_i] == wrt) break;
1093  }
1094  // Nominal value
1095  double n = nominal_out_[id];
1096  // Get the value
1097  double d_fd = m->d_out_[ind] * n;
1098  // Correct sign, if necessary
1099  if (m->flip_[wrt_i]) d_fd = -d_fd;
1100  // Use FD instead of AD or to compare with AD
1101  if (m->self.validate_forward_) {
1102  // Value to compare with
1103  double d_ad = m->osens_[id];
1104  // Nominal value used as seed
1105  d_ad /= nominal_in_[wrt];
1106  d_fd /= nominal_in_[wrt];
1107  // Is it a not a number?
1108  bool d_is_nan = d_ad != d_ad;
1109  // Magnitude of derivatives
1110  double d_max = std::fmax(std::fabs(d_fd), std::fabs(d_ad));
1111  // Check if NaN or error exceeds thresholds
1112  if (d_is_nan || (d_max > n * m->self.abstol_
1113  && std::fabs(d_ad - d_fd) > d_max * m->self.reltol_)) {
1114  // Offset for printing the stencil
1115  double off = m->fd_out_.at(ind + offset * n_unknown);
1116  // Warning or add to file
1117  std::stringstream ss;
1118  if (m->self.validate_ad_file_.empty()) {
1119  // Issue warning
1120  ss << (d_is_nan ? "NaN" : "Inconsistent") << " derivatives of " << vn_out_[id]
1121  << " w.r.t. " << desc_in(m, wrt) << ", got " << d_ad
1122  << " for AD vs. " << d_fd << " for FD[" << to_string(m->self.fd_) << "].";
1123  // Print the stencil:
1124  ss << "\nValues for step size " << h << ": " << (n * off) << " + [";
1125  for (casadi_int k = 0; k < n_points; ++k) {
1126  if (k > 0) ss << ", ";
1127  ss << (n * (m->fd_out_.at(ind + k * n_unknown) - off));
1128  }
1129  ss << "]";
1130  // Issue warning
1131  casadi_warning(ss.str());
1132  } else {
1133  // Output
1134  ss << vn_out_[id] << " ";
1135  // Input
1136  ss << vn_in_[wrt] << " ";
1137  // Value
1138  ss << m->ibuf_[wrt] << " ";
1139  // Noninal
1140  ss << nominal_in_[wrt] << " ";
1141  // Min
1142  ss << min_in_[wrt] << " ";
1143  // Max
1144  ss << max_in_[wrt] << " ";
1145  // AD
1146  ss << d_ad << " ";
1147  // FD
1148  ss << d_fd << " ";
1149  // Step
1150  ss << h << " ";
1151  // Offset
1152  ss << off << " ";
1153  // Stencil
1154  ss << "[";
1155  for (casadi_int k = 0; k < n_points; ++k) {
1156  if (k > 0) ss << ",";
1157  ss << (n * (m->fd_out_.at(ind + k * n_unknown) - off));
1158  }
1159  ss << "]" << std::endl;
1160  // Append to file
1161  auto valfile_ptr = Filesystem::ofstream_ptr(m->self.validate_ad_file_,
1162  std::ios_base::app);
1163  std::ostream& valfile = *valfile_ptr;
1164  valfile << ss.str();
1165  }
1166  }
1167  } else {
1168  // Use instead of AD
1169  m->osens_[id] = d_fd;
1170  }
1171  }
1172  // Successful return
1173  return 0;
1174 }
std::string desc_in(FmuMemory *m, size_t id, bool more=true) const
Definition: fmu.cpp:850
std::vector< std::string > vn_out_
Definition: fmu_impl.hpp:362
const double eps
Machine epsilon.
Definition: calculus.hpp:56
casadi_int n_fd_points(FdMode v)
Length of FD stencil, including unperturbed input.
casadi_int fd_offset(FdMode v)
Offset for FD stencil, i.e. index of unperturbed input.
double sign(double x)
Sign function, note that sign(nan) == nan.
Definition: calculus.hpp:270
void casadi_copy(const T1 *x, casadi_int n, T1 *y)
COPY: y <-x.
std::string to_string(TypeFmi2 v)
const double nan
Not a number.
Definition: calculus.hpp:53
CASADI_EXPORT void finite_diff(FdMode v, const T1 **yk, T1 *J, T1 h, casadi_int n_y, T1 smoothing)
Calculate FD estimate.

References casadi::FmuFunction::abstol_, casadi::casadi_copy(), casadi::FmuMemory::d_in_, casadi::FmuMemory::d_out_, casadi::FmuInternal::desc_in(), casadi::eps, casadi::FmuFunction::fd_, casadi::FmuFunction::fd_flip_, casadi::fd_offset(), casadi::FmuMemory::fd_out_, casadi::finite_diff(), casadi::FmuMemory::flip_, casadi::FmuInternal::get_all(), casadi::get_ptr(), casadi::FmuInternal::get_real(), casadi::FmuMemory::ibuf_, casadi::FmuMemory::id_in_, casadi::FmuMemory::id_out_, casadi::FmuMemory::in_bounds_, casadi::FmuMemory::instance, casadi::FmuInternal::max_in_, casadi::FmuInternal::min_in_, casadi::n_fd_points(), casadi::nan, casadi::FmuInternal::nominal_in_, casadi::FmuInternal::nominal_out_, casadi::Filesystem::ofstream_ptr(), casadi::FmuMemory::osens_, casadi::FmuFunction::reltol_, casadi::FmuMemory::self, casadi::FmuInternal::set_all(), casadi::sign(), casadi::FmuFunction::step_, casadi::str(), casadi::to_string(), casadi::FmuMemory::v_in_, casadi::FmuMemory::v_out_, casadi::FmuMemory::v_pert_, casadi::FmuFunction::validate_ad_file_, casadi::FmuFunction::validate_forward_, casadi::FmuInternal::vn_in_, casadi::FmuInternal::vn_out_, casadi::FmuMemory::vr_out_, and casadi::FmuMemory::wrt_.

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

◆ eval_fwd()

int casadi::FmuInternal::eval_fwd ( FmuMemory *  m,
bool  independent_seeds 
) const
inherited

Definition at line 863 of file fmu.cpp.

863  {
864  // Gather input and output indices
865  gather_fwd(m);
866  // Calculate derivatives using FMU directional derivative support
867  if (m->self.uses_directional_derivatives_) {
868  // Evaluate using AD
869  if (eval_ad(m)) return 1;
870  }
871  // Calculate derivatives using finite differences
872  if (!m->self.uses_directional_derivatives_ || m->self.validate_forward_) {
873  // Evaluate using FD
874  if (eval_fd(m, independent_seeds)) return 1;
875  }
876  return 0;
877 }
void gather_fwd(FmuMemory *m) const
Definition: fmu.cpp:1504
int eval_fd(FmuMemory *m, bool independent_seeds) const
Definition: fmu.cpp:948
int eval_ad(FmuMemory *m) const
Definition: fmu.cpp:912

References casadi::FmuInternal::eval_ad(), casadi::FmuInternal::eval_fd(), casadi::FmuInternal::gather_fwd(), casadi::FmuMemory::self, casadi::FmuFunction::uses_directional_derivatives_, and casadi::FmuFunction::validate_forward_.

◆ exit_initialization_mode()

int casadi::Fmu2::exit_initialization_mode ( void *  instance) const
overridevirtual

Implements casadi::FmuInternal.

Definition at line 298 of file fmu2.cpp.

298  {
299  auto *c = static_cast<fmi2Component>(instance);
300  fmi2Status status = exit_initialization_mode_(c);
301  if (status != fmi2OK) {
302  casadi_warning("fmi2ExitInitializationMode failed");
303  return 1;
304  }
305  return 0;
306 }

References exit_initialization_mode_.

◆ finalize()

void casadi::Fmu2::finalize ( )
overridevirtual

Allocate numerical values for initial auxilliary variables

Reimplemented from casadi::FmuInternal.

Definition at line 173 of file fmu2.cpp.

173  {
175  aux_value_.v_real.resize(vn_aux_real_.size());
176  aux_value_.v_integer.resize(vn_aux_integer_.size());
177  aux_value_.v_boolean.resize(vn_aux_boolean_.size());
178  aux_value_.v_string.resize(vn_aux_string_.size());
179 
180  // Recursive call
182 }
Value aux_value_
Definition: fmu2.hpp:126
virtual void finalize()
Definition: fmu.cpp:593
std::vector< fmi2Boolean > v_boolean
Definition: fmu2.hpp:122
std::vector< fmi2Real > v_real
Definition: fmu2.hpp:120
std::vector< std::string > v_string
Definition: fmu2.hpp:123
std::vector< fmi2Integer > v_integer
Definition: fmu2.hpp:121

References aux_value_, casadi::FmuInternal::finalize(), casadi::Fmu2::Value::v_boolean, casadi::Fmu2::Value::v_integer, casadi::Fmu2::Value::v_real, casadi::Fmu2::Value::v_string, vn_aux_boolean_, vn_aux_integer_, vn_aux_real_, and vn_aux_string_.

Referenced by deserialize().

◆ fmi3_dll_infix()

std::string casadi::FmuInternal::fmi3_dll_infix ( )
staticinherited

Definition at line 736 of file fmu.cpp.

736  {
737  // Architecture
738  std::string arch;
739 #if defined(__arm64__) || defined(__aarch64__)
740  // ARM 64-bit Architecture
741  arch = "aarch64";
742 #elif __arm__
743  if (sizeof(void*) == 4) {
744  // ARM 32-bit Architecture
745  arch = "aarch32";
746  } else {
747  // ARM 64-bit Architecture
748  arch = "aarch64";
749  }
750 #elif defined(__ppc64__) || defined(__powerpc64__)
751  // PowerPC 64-bit Architecture
752  arch = "ppc64";
753 #elif defined(__ppc__) || defined(__powerpc__)
754  // PowerPC 32-bit Architecture
755  arch = "ppc";
756 #else
757  if (sizeof(void*) == 4) {
758  // Intel/AMD x86 32-bit
759  arch = "x86";
760  } else {
761  // Intel/AMD x86 64-bit
762  arch = "x86_64";
763  }
764 #endif
765  // Operating system
766  std::string sys;
767 #if defined(_WIN32)
768  // Microsoft Windows
769  sys = "windows";
770 #elif defined(__APPLE__)
771  // Darwin (macOS, iOS, watchOS, tvOS, audioOS)
772  sys = "darwin";
773 #else
774  // Linux
775  sys = "linux";
776 #endif
777  // Return platform tuple, according to Section 2.5.1.4.1. of the FMI 3 specification
778  return arch + "-" + sys;
779 }

Referenced by casadi::FmuInternal::compile_fmu(), and casadi::Fmu3::dll_infix().

◆ free_instance()

void casadi::Fmu2::free_instance ( void *  instance) const
overridevirtual

Implements casadi::FmuInternal.

Definition at line 269 of file fmu2.cpp.

269  {
270  if (free_instance_) {
271  auto *c = static_cast<fmi2Component>(instance);
272  free_instance_(c);
273  } else {
274  casadi_warning("No free_instance function pointer available");
275  }
276 }

References free_instance_.

◆ free_mem()

void casadi::Fmu2::free_mem ( void *  mem) const
overridevirtual

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

Implements casadi::FmuInternal.

Definition at line 58 of file fmu2.cpp.

58  {
59  delete static_cast<Fmu2Memory*>(mem);
60 }

◆ gather_adj()

void casadi::FmuInternal::gather_adj ( FmuMemory *  m) const
inherited

Definition at line 1520 of file fmu.cpp.

1520  {
1521  // Gather input and output indices
1522  gather_io(m);
1523  // Number of inputs and outputs
1524  size_t n_known = m->id_in_.size();
1525  // Get/clear seeds
1526  m->d_out_.clear();
1527  for (size_t id : m->id_out_) {
1528  m->d_out_.push_back(m->osens_[id]);
1529  m->osens_[id] = 0;
1530  }
1531  // Allocate result vectors
1532  m->v_in_.resize(n_known);
1533  m->d_in_.resize(n_known);
1534 }

References casadi::FmuMemory::d_in_, casadi::FmuMemory::d_out_, casadi::FmuInternal::gather_io(), casadi::FmuMemory::id_in_, casadi::FmuMemory::id_out_, casadi::FmuMemory::osens_, and casadi::FmuMemory::v_in_.

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

◆ gather_fwd()

void casadi::FmuInternal::gather_fwd ( FmuMemory *  m) const
inherited

Definition at line 1504 of file fmu.cpp.

1504  {
1505  // Gather input and output indices
1506  gather_io(m);
1507  // Number of outputs
1508  size_t n_unknown = m->id_out_.size();
1509  // Get/clear seeds
1510  m->d_in_.clear();
1511  for (size_t id : m->id_in_) {
1512  m->d_in_.push_back(m->isens_[id]);
1513  m->isens_[id] = 0;
1514  }
1515  // Allocate result vectors
1516  m->v_out_.resize(n_unknown);
1517  m->d_out_.resize(n_unknown);
1518 }

References casadi::FmuMemory::d_in_, casadi::FmuMemory::d_out_, casadi::FmuInternal::gather_io(), casadi::FmuMemory::id_in_, casadi::FmuMemory::id_out_, casadi::FmuMemory::isens_, and casadi::FmuMemory::v_out_.

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

◆ gather_io()

void casadi::FmuInternal::gather_io ( FmuMemory *  m) const
inherited

Definition at line 1477 of file fmu.cpp.

1477  {
1478  // Collect input indices and corresponding value references and values
1479  m->id_in_.clear();
1480  m->vr_in_.clear();
1481  m->v_in_.clear();
1482  for (size_t id = 0; id < m->imarked_.size(); ++id) {
1483  if (m->imarked_[id]) {
1484  m->id_in_.push_back(id);
1485  m->vr_in_.push_back(vr_in_[id]);
1486  m->v_in_.push_back(m->ibuf_[id]);
1487  m->imarked_[id] = false;
1488  }
1489  }
1490  // Collect output indices, corresponding value references
1491  m->id_out_.clear();
1492  m->vr_out_.clear();
1493  m->v_out_.clear();
1494  for (size_t id = 0; id < m->omarked_.size(); ++id) {
1495  if (m->omarked_[id]) {
1496  m->id_out_.push_back(id);
1497  m->vr_out_.push_back(vr_out_[id]);
1498  m->v_out_.push_back(nan);
1499  m->omarked_[id] = false;
1500  }
1501  }
1502 }
std::vector< unsigned int > vr_out_
Definition: fmu_impl.hpp:363
std::vector< unsigned int > vr_in_
Definition: fmu_impl.hpp:363

References casadi::FmuMemory::ibuf_, casadi::FmuMemory::id_in_, casadi::FmuMemory::id_out_, casadi::FmuMemory::imarked_, casadi::nan, casadi::FmuMemory::omarked_, casadi::FmuMemory::v_in_, casadi::FmuMemory::v_out_, casadi::FmuMemory::vr_in_, casadi::FmuInternal::vr_in_, casadi::FmuMemory::vr_out_, and casadi::FmuInternal::vr_out_.

Referenced by casadi::FmuInternal::eval(), casadi::FmuInternal::gather_adj(), and casadi::FmuInternal::gather_fwd().

◆ get()

void casadi::FmuInternal::get ( FmuMemory *  m,
size_t  id,
double *  value 
) const
inherited

Definition at line 1430 of file fmu.cpp.

1430  {
1431  // Save to return
1432  for (size_t id : ored_[ind]) {
1433  *value++ = m->obuf_.at(id);
1434  }
1435 }

References casadi::FmuMemory::obuf_, and casadi::FmuInternal::ored_.

Referenced by casadi::Fmu::get().

◆ get_adj() [1/2]

void casadi::FmuInternal::get_adj ( FmuMemory *  m,
casadi_int  nsens,
const casadi_int *  id,
double *  v 
) const
inherited

Definition at line 1232 of file fmu.cpp.

1232  {
1233  for (casadi_int i = 0; i < nsens; ++i) {
1234  *v++ = m->isens_.at(*id++);
1235  }
1236 }

References casadi::FmuMemory::isens_.

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

◆ get_adj() [2/2]

void casadi::FmuInternal::get_adj ( FmuMemory *  m,
size_t  ind,
double *  v 
) const
inherited

Definition at line 1238 of file fmu.cpp.

1238  {
1239  // Quick return if not needed
1240  if (!v) return;
1241  // Retrieve all sensitivities FIXME(@jaeandersson): should use compatible types
1242  for (size_t id : ired_[ind]) {
1243  casadi_int id2 = id;
1244  get_adj(m, 1, &id2, v++);
1245  }
1246 }
void get_adj(FmuMemory *m, casadi_int nsens, const casadi_int *id, double *v) const
Definition: fmu.cpp:1232

References casadi::FmuInternal::get_adj(), and casadi::FmuInternal::ired_.

◆ get_adjoint_derivative()

int casadi::FmuInternal::get_adjoint_derivative ( void *  instance,
const unsigned int *  vr_out,
size_t  n_out,
const unsigned int *  vr_in,
size_t  n_in,
const double *  seed,
size_t  n_seed,
double *  sensitivity,
size_t  n_sensitivity 
) const
virtualinherited

Reimplemented in casadi::Fmu3.

Definition at line 586 of file fmu.cpp.

588  {
589  casadi_error("Adjoint derivatives not supported for " + class_name());
590  return 1;
591 }

References casadi::SharedObjectInternal::class_name().

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

◆ get_all()

int casadi::FmuInternal::get_all ( FmuMemory *  m,
double *  values,
size_t  n_values 
) const
inherited

Definition at line 1400 of file fmu.cpp.

1400  {
1401  // Quick return if nothing to get
1402  if (n_values == 0) return 0;
1403  // Retrieve from FMU
1404  m->n_get_all++;
1405  auto start = std::chrono::high_resolution_clock::now();
1406  int flag = get_real(m->instance, get_ptr(m->vr_out_), m->vr_out_.size(), values, n_values, m);
1407  auto stop = std::chrono::high_resolution_clock::now();
1408  m->t_get_all += std::chrono::duration_cast<std::chrono::microseconds>(
1409  stop - start).count() / 1e6;
1410  // Return result of the evaluation
1411  return flag;
1412 }

References casadi::get_ptr(), casadi::FmuInternal::get_real(), casadi::FmuMemory::instance, casadi::FmuMemory::n_get_all, casadi::FmuMemory::t_get_all, and casadi::FmuMemory::vr_out_.

Referenced by casadi::FmuInternal::eval(), and casadi::FmuInternal::eval_fd().

◆ get_aux()

int casadi::Fmu2::get_aux ( void *  instance)
overridevirtual

Implements casadi::FmuInternal.

Definition at line 489 of file fmu2.cpp.

489  {
490  return get_aux_impl(instance, aux_value_);
491 }
int get_aux_impl(void *instance, Value &aux_value) const
Definition: fmu2.cpp:447

References aux_value_, and get_aux_impl().

◆ get_aux_impl()

int casadi::Fmu2::get_aux_impl ( void *  instance,
Value &  aux_value 
) const

Definition at line 447 of file fmu2.cpp.

447  {
448  auto *c = static_cast<fmi2Component>(instance);
449  // Get real auxilliary variables
450  if (!vr_aux_real_.empty()) {
451  fmi2Status status = get_real_(c, get_ptr(vr_aux_real_), vr_aux_real_.size(),
452  get_ptr(aux_value.v_real));
453  if (status != fmi2OK) {
454  casadi_warning("fmi2GetReal failed");
455  return 1;
456  }
457  }
458  // Get integer/enum auxilliary variables
459  if (!vr_aux_integer_.empty()) {
460  fmi2Status status = get_integer_(c, get_ptr(vr_aux_integer_), vr_aux_integer_.size(),
461  get_ptr(aux_value.v_integer));
462  if (status != fmi2OK) {
463  casadi_warning("fmi2GetInteger failed");
464  return 1;
465  }
466  }
467  // Get boolean auxilliary variables
468  if (!vr_aux_boolean_.empty()) {
469  fmi2Status status = get_boolean_(c, get_ptr(vr_aux_boolean_), vr_aux_boolean_.size(),
470  get_ptr(aux_value.v_boolean));
471  if (status != fmi2OK) {
472  casadi_warning("fmi2GetBoolean failed");
473  return 1;
474  }
475  }
476  // Get string auxilliary variables
477  for (size_t k = 0; k < vr_aux_string_.size(); ++k) {
478  fmi2ValueReference vr = vr_aux_string_[k];
479  fmi2String value = aux_value.v_string.at(k).c_str();
480  fmi2Status status = set_string_(c, &vr, 1, &value);
481  if (status != fmi2OK) {
482  casadi_error("fmi2GetString failed for value reference " + str(vr));
483  }
484  }
485  // Successful return
486  return 0;
487 }
fmi2GetIntegerTYPE * get_integer_
Definition: fmu2.hpp:108
fmi2GetBooleanTYPE * get_boolean_
Definition: fmu2.hpp:106
fmi2SetStringTYPE * set_string_
Definition: fmu2.hpp:111

References get_boolean_, get_integer_, casadi::get_ptr(), get_real_, set_string_, casadi::str(), casadi::Fmu2::Value::v_boolean, casadi::Fmu2::Value::v_integer, casadi::Fmu2::Value::v_real, casadi::Fmu2::Value::v_string, vr_aux_boolean_, vr_aux_integer_, vr_aux_real_, and vr_aux_string_.

Referenced by get_aux(), and get_stats().

◆ get_derivatives()

int casadi::Fmu2::get_derivatives ( void *  instance,
double *  derivatives,
size_t  nx 
) const
overridevirtual

Implements casadi::FmuInternal.

Definition at line 318 of file fmu2.cpp.

318  {
319  auto *c = static_cast<fmi2Component>(instance);
320  fmi2Status status = get_derivatives_(c, derivatives, nx);
321  if (status != fmi2OK) {
322  casadi_warning("fmi2GetDerivatives failed");
323  return 1;
324  }
325  return 0;
326 }
fmi2GetDerivativesTYPE * get_derivatives_
Definition: fmu2.hpp:102

References get_derivatives_.

◆ get_directional_derivative()

int casadi::Fmu2::get_directional_derivative ( void *  instance,
const unsigned int *  vr_out,
size_t  n_out,
const unsigned int *  vr_in,
size_t  n_in,
const double *  seed,
size_t  n_seed,
double *  sensitivity,
size_t  n_sensitivity 
) const
overridevirtual

Implements casadi::FmuInternal.

Definition at line 380 of file fmu2.cpp.

382  {
383  // Consistency checks
384  casadi_assert(n_in == n_seed, "Vector-valued variables not supported in FMI 2");
385  casadi_assert(n_out == n_sensitivity, "Vector-valued variables not supported in FMI 2");
386  // Quick return if zero dimension
387  if (n_in == 0 || n_out == 0) return 0;
388  // No directional derivatives w.r.t. automatically added independent variable
389  if (has_independent_ && n_in > 0 && *vr_in == static_cast<unsigned int>(-1)) {
390  n_in--;
391  vr_in++;
392  n_seed--;
393  seed++;
394  // Quick return if now zero dimension
395  if (n_in == 0) {
396  std::fill(sensitivity, sensitivity + n_sensitivity, 0.0);
397  return 0;
398  }
399  }
400  // Retrieve from FMU and return
401  fmi2Status status = get_directional_derivative_(instance, vr_out, n_out, vr_in, n_in,
402  seed, sensitivity);
403  return status != fmi2OK;
404 }
size_t n_in() const
Get the number of scheme inputs.
Definition: fmu_impl.hpp:130
size_t n_out() const
Get the number of scheme outputs.
Definition: fmu_impl.hpp:135

References get_directional_derivative_, casadi::FmuInternal::has_independent_, casadi::FmuInternal::n_in(), and casadi::FmuInternal::n_out().

◆ get_fwd() [1/2]

void casadi::FmuInternal::get_fwd ( FmuMemory *  m,
casadi_int  nsens,
const casadi_int *  id,
double *  v 
) const
inherited

Definition at line 1176 of file fmu.cpp.

1176  {
1177  for (casadi_int i = 0; i < nsens; ++i) {
1178  *v++ = m->osens_.at(*id++);
1179  }
1180 }

References casadi::FmuMemory::osens_.

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

◆ get_fwd() [2/2]

void casadi::FmuInternal::get_fwd ( FmuMemory *  m,
size_t  ind,
double *  v 
) const
inherited

Definition at line 1182 of file fmu.cpp.

1182  {
1183  // Quick return if not needed
1184  if (!v) return;
1185  // Retrieve all sensitivities FIXME(@jaeandersson): should use compatible types
1186  for (size_t id : ored_[ind]) {
1187  casadi_int id2 = id;
1188  get_fwd(m, 1, &id2, v++);
1189  }
1190 }
void get_fwd(FmuMemory *m, casadi_int nsens, const casadi_int *id, double *v) const
Definition: fmu.cpp:1176

References casadi::FmuInternal::get_fwd(), and casadi::FmuInternal::ored_.

◆ get_real()

int casadi::Fmu2::get_real ( void *  instance,
const unsigned int *  vr,
size_t  n_vr,
double *  values,
size_t  n_values,
FmuMemory *  m = nullptr 
) const
overridevirtual

Implements casadi::FmuInternal.

Definition at line 363 of file fmu2.cpp.

364  {
365  casadi_assert(n_vr == n_values, "Vector-valued variables not supported in FMI 2");
366  if (do_evaluation_dance_ && mem) {
367  auto *m = static_cast<Fmu2Memory*>(mem);
368  // Dummy call to trigger rtOneStep
369  fmi2Status status = get_real_(instance, nullptr, 0, nullptr);
370  if (status != fmi2OK) return 1;
371  // Dummy call to trigger computation of derivatives, possibly read later via get_real
372  status = get_derivatives_(instance, get_ptr(m->derivate_dump), nx_);
373  if (status != fmi2OK) return 1;
374  }
375 
376  fmi2Status status = get_real_(instance, vr, n_vr, values);
377  return status != fmi2OK;
378 }

References casadi::FmuInternal::do_evaluation_dance_, get_derivatives_, casadi::get_ptr(), get_real_, and casadi::FmuInternal::nx_.

◆ get_stats()

void casadi::Fmu2::get_stats ( FmuMemory *  m,
Dict *  stats,
const std::vector< std::string > &  name_in,
const InputStruct *  in 
) const
overridevirtual

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

Implements casadi::FmuInternal.

Definition at line 493 of file fmu2.cpp.

494  {
495  Value& v = static_cast<Fmu2Memory*>(m)->aux_value;
496  get_aux_impl(m->instance, v);
497  // Collect auxilliary variables
498  Dict aux;
499  // Real
500  for (size_t k = 0; k < vn_aux_real_.size(); ++k) {
501  aux[vn_aux_real_[k]] = static_cast<double>(v.v_real[k]);
502  }
503  // Integer
504  for (size_t k = 0; k < vn_aux_integer_.size(); ++k) {
505  aux[vn_aux_integer_[k]] = static_cast<casadi_int>(v.v_integer[k]);
506  }
507  // Boolean
508  for (size_t k = 0; k < vn_aux_boolean_.size(); ++k) {
509  aux[vn_aux_boolean_[k]] = static_cast<bool>(v.v_boolean[k]);
510  }
511  // String
512  for (size_t k = 0; k < vn_aux_string_.size(); ++k) {
513  aux[vn_aux_string_[k]] = v.v_string[k];
514  }
515  // Copy to stats
516  (*stats)["aux"] = aux;
517  // Loop over input variables
518  for (size_t k = 0; k < name_in.size(); ++k) {
519  // Only consider regular inputs
520  if (in[k].type == InputType::REG) {
521  // Get the indices
522  const std::vector<size_t>& iind = ired_.at(in[k].ind);
523  // Collect values
524  std::vector<double> v(iind.size());
525  for (size_t i = 0; i < v.size(); ++i) v[i] = value_in_.at(iind[i]);
526  // Save to stats
527  (*stats)[name_in[k]] = v;
528  }
529  }
530 }
std::vector< double > value_in_
Definition: fmu_impl.hpp:366

References get_aux_impl(), casadi::FmuMemory::instance, casadi::FmuInternal::ired_, casadi::REG, casadi::Fmu2::Value::v_boolean, casadi::Fmu2::Value::v_integer, casadi::Fmu2::Value::v_real, casadi::Fmu2::Value::v_string, casadi::FmuInternal::value_in_, vn_aux_boolean_, vn_aux_integer_, vn_aux_real_, and vn_aux_string_.

◆ getCount()

Definition at line 60 of file generic_shared_internal.hpp.

210  {
211  return static_cast<const Internal*>(this)->count;
212  }

◆ hess_sparsity()

Sparsity casadi::FmuInternal::hess_sparsity ( const std::vector< size_t > &  r,
const std::vector< size_t > &  c 
) const
inherited

Definition at line 696 of file fmu.cpp.

697  {
698  // Convert to casadi_int type
699  std::vector<casadi_int> r1(r.begin(), r.end());
700  std::vector<casadi_int> c1(c.begin(), c.end());
701  // Index mapping (not used)
702  std::vector<casadi_int> mapping;
703  // Get selection
704  return hess_sp_.sub(r1, c1, mapping);
705 }
Sparsity sub(const std::vector< casadi_int > &rr, const std::vector< casadi_int > &cc, std::vector< casadi_int > &mapping, bool ind1=false) const
Get a submatrix.
Definition: sparsity.cpp:334

References casadi::FmuInternal::hess_sp_, and casadi::Sparsity::sub().

◆ index_in()

size_t casadi::FmuInternal::index_in ( const std::string &  n) const
inherited

Definition at line 665 of file fmu.cpp.

665  {
666  // Linear search for the input
667  for (size_t i = 0; i < scheme_in_.size(); ++i) {
668  if (scheme_in_[i] == n) return i;
669  }
670  // Not found
671  casadi_error("No such input: " + n);
672  return -1;
673 }
std::vector< std::string > scheme_in_
Definition: fmu_impl.hpp:313

References casadi::FmuInternal::scheme_in_.

Referenced by casadi::InputStruct::parse(), and casadi::OutputStruct::parse().

◆ index_out()

size_t casadi::FmuInternal::index_out ( const std::string &  n) const
inherited

Definition at line 675 of file fmu.cpp.

675  {
676  // Linear search for the input
677  for (size_t i = 0; i < scheme_out_.size(); ++i) {
678  if (scheme_out_[i] == n) return i;
679  }
680  // Not found
681  casadi_error("No such output: " + n);
682  return -1;
683 }
std::vector< std::string > scheme_out_
Definition: fmu_impl.hpp:313

References casadi::FmuInternal::scheme_out_.

Referenced by casadi::InputStruct::parse(), and casadi::OutputStruct::parse().

◆ init()

void casadi::Fmu2::init ( const DaeBuilderInternal *  dae)
overridevirtual

Reimplemented from casadi::FmuInternal.

Definition at line 82 of file fmu2.cpp.

82  {
83  // Initialize base classes
84  FmuInternal::init(dae);
85 
86  // Add prefix to resource location, cf. FMI specification 2.0.5, section 2.1.5
87  resource_loc_ = "file://" + resource_loc_;
88 
89  // Collect input and parameter values
90  vr_real_.clear();
91  vr_integer_.clear();
92  vr_boolean_.clear();
93  vr_string_.clear();
94  init_real_.clear();
95  init_integer_.clear();
96  init_boolean_.clear();
97  init_string_.clear();
98  for (size_t i = 0; i < dae->n_variables(); ++i) {
99  const Variable& v = dae->variable(i);
100  casadi_assert(v.numel == 1, "Vector variable support not implemented");
101  // Skip if the wrong type
102  if (v.causality != Causality::PARAMETER && v.causality != Causality::INPUT) continue;
103  // Variable has not been set - keep default value
104  if (!v.is_set()) continue;
105  // Value reference
106  fmi2ValueReference vr = v.value_reference;
107  // Get value
108  switch (to_fmi2(v.type)) {
109  case TypeFmi2::REAL:
110  init_real_.push_back(static_cast<fmi2Real>(v.value.front()));
111  vr_real_.push_back(vr);
112  break;
113  case TypeFmi2::INTEGER:
114  case TypeFmi2::ENUM:
115  init_integer_.push_back(static_cast<fmi2Integer>(v.value.front()));
116  vr_integer_.push_back(vr);
117  break;
118  case TypeFmi2::BOOLEAN:
119  init_boolean_.push_back(static_cast<fmi2Boolean>(v.value.front()));
120  vr_boolean_.push_back(vr);
121  break;
122  case TypeFmi2::STRING:
123  init_string_.push_back(v.stringvalue);
124  vr_string_.push_back(vr);
125  break;
126  default:
127  casadi_warning("Ignoring " + v.name + ", type: " + to_string(v.type));
128  }
129  }
130 
131  // Collect auxilliary variables
132  vn_aux_real_.clear();
133  vn_aux_integer_.clear();
134  vn_aux_boolean_.clear();
135  vn_aux_string_.clear();
136  vr_aux_real_.clear();
137  vr_aux_integer_.clear();
138  vr_aux_boolean_.clear();
139  vr_aux_string_.clear();
140  for (auto&& s : aux_) {
141  const Variable& v = dae->variable(s);
142  // Convert to expected type
143  fmi2ValueReference vr = v.value_reference;
144  // Sort by type
145  switch (to_fmi2(v.type)) {
146  case TypeFmi2::REAL:
147  // Real
148  vn_aux_real_.push_back(v.name);
149  vr_aux_real_.push_back(vr);
150  break;
151  case TypeFmi2::INTEGER:
152  case TypeFmi2::ENUM:
153  // Integer or enum
154  vn_aux_integer_.push_back(v.name);
155  vr_aux_integer_.push_back(vr);
156  break;
157  case TypeFmi2::BOOLEAN:
158  // Boolean
159  vn_aux_boolean_.push_back(v.name);
160  vr_aux_boolean_.push_back(vr);
161  break;
162  case TypeFmi2::STRING:
163  // String
164  vn_aux_string_.push_back(v.name);
165  vr_aux_string_.push_back(vr);
166  break;
167  default:
168  casadi_warning("Ignoring " + v.name + ", type: " + to_string(v.type));
169  }
170  }
171 }
std::vector< std::string > aux_
Definition: fmu_impl.hpp:317
std::string resource_loc_
Definition: fmu_impl.hpp:320
virtual void init(const DaeBuilderInternal *dae)
Definition: fmu.cpp:453
struct VariableStruct Variable
TypeFmi2 to_fmi2(Type v)

References casadi::FmuInternal::aux_, casadi::BOOLEAN, casadi::Variable::causality, casadi::ENUM, casadi::FmuInternal::init(), init_boolean_, init_integer_, init_real_, init_string_, casadi::INPUT, casadi::INTEGER, casadi::Variable::is_set(), casadi::DaeBuilderInternal::n_variables(), casadi::Variable::name, casadi::Variable::numel, casadi::PARAMETER, casadi::REAL, casadi::FmuInternal::resource_loc_, casadi::STRING, casadi::Variable::stringvalue, casadi::to_fmi2(), casadi::to_string(), casadi::Variable::type, casadi::Variable::value, casadi::Variable::value_reference, casadi::DaeBuilderInternal::variable(), vn_aux_boolean_, vn_aux_integer_, vn_aux_real_, vn_aux_string_, vr_aux_boolean_, vr_aux_integer_, vr_aux_real_, vr_aux_string_, vr_boolean_, vr_integer_, vr_real_, and vr_string_.

◆ init_mem()

int casadi::Fmu2::init_mem ( FmuMemory *  m) const
overridevirtual

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

Allocate numerical values for initial auxilliary variables

Reimplemented from casadi::FmuInternal.

Definition at line 41 of file fmu2.cpp.

41  {
42  if (FmuInternal::init_mem(mem)) return 1;
43  auto *m = static_cast<Fmu2Memory*>(mem);
45  m->aux_value.v_real.resize(vn_aux_real_.size());
46  m->aux_value.v_integer.resize(vn_aux_integer_.size());
47  m->aux_value.v_boolean.resize(vn_aux_boolean_.size());
48  m->aux_value.v_string.resize(vn_aux_string_.size());
49  // Allocate scratch space for evaluation dance
50  if (do_evaluation_dance_) m->derivate_dump.resize(nx_);
51  return 0;
52 }
virtual int init_mem(FmuMemory *m) const
Initalize memory block.
Definition: fmu.cpp:1298

References casadi::FmuInternal::do_evaluation_dance_, casadi::FmuInternal::init_mem(), casadi::FmuInternal::nx_, vn_aux_boolean_, vn_aux_integer_, vn_aux_real_, and vn_aux_string_.

◆ 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  }

◆ instantiate()

void * casadi::Fmu2::instantiate ( ) const
overridevirtual

Implements casadi::FmuInternal.

Definition at line 251 of file fmu2.cpp.

251  {
252  // Instantiate FMU
253  fmi2String instanceName = instance_name_.c_str();
254  fmi2Type fmuType = fmi2ModelExchange;
255  fmi2String fmuGUID = instantiation_token_.c_str();
256  fmi2String fmuResourceLocation = resource_loc_.c_str();
257  fmi2Boolean visible = fmi2False;
258  fmi2Component c = instantiate_(instanceName, fmuType, fmuGUID, fmuResourceLocation,
259  &functions_, visible, logging_on_);
260  if (c == nullptr) casadi_error("fmi2Instantiate failed");
261 
262  // Call fmi2SetupExperiment
263  fmi2Status status = setup_experiment_(c, fmutol_ > 0, fmutol_, 0., fmi2True, 1.);
264  casadi_assert(status == fmi2OK, "fmi2SetupExperiment failed");
265 
266  return c;
267 }
fmi2CallbackFunctions functions_
Definition: fmu2.hpp:116
std::string instantiation_token_
Definition: fmu_impl.hpp:329
std::string instance_name_
Definition: fmu_impl.hpp:326

References casadi::FmuInternal::fmutol_, functions_, casadi::FmuInternal::instance_name_, instantiate_, casadi::FmuInternal::instantiation_token_, casadi::FmuInternal::logging_on_, casadi::FmuInternal::resource_loc_, and setup_experiment_.

◆ jac_sparsity()

Sparsity casadi::FmuInternal::jac_sparsity ( const std::vector< size_t > &  osub,
const std::vector< size_t > &  isub 
) const
inherited

Definition at line 685 of file fmu.cpp.

686  {
687  // Convert to casadi_int type
688  std::vector<casadi_int> osub1(osub.begin(), osub.end());
689  std::vector<casadi_int> isub1(isub.begin(), isub.end());
690  // Index mapping (not used)
691  std::vector<casadi_int> mapping;
692  // Get selection
693  return jac_sp_.sub(osub1, isub1, mapping);
694 }

References casadi::FmuInternal::jac_sp_, and casadi::Sparsity::sub().

◆ load_function()

template<typename T >
T * casadi::FmuInternal::load_function ( const std::string &  symname)
inherited

Definition at line 386 of file fmu_impl.hpp.

386  {
387  // Load the function
388  signal_t f = li_.get_function(symname);
389  // Ensure that it was found
390  casadi_assert(f != nullptr, "Cannot retrieve '" + symname + "'");
391  // Return function with the right type
392  return reinterpret_cast<T*>(f);
393 }
Importer li_
DLL.
Definition: fmu_impl.hpp:347
signal_t get_function(const std::string &symname)
Get a function pointer for numerical evaluation.
Definition: importer.cpp:84
void(* signal_t)(void)
Function pointer types for the C API.

References casadi::Importer::get_function(), casadi::FmuInternal::li_, and casadi::T.

◆ load_functions()

void casadi::Fmu2::load_functions ( )
overridevirtual

Implements casadi::FmuInternal.

Definition at line 184 of file fmu2.cpp.

184  {
185  instantiate_ = load_function<fmi2InstantiateTYPE>("fmi2Instantiate");
186  free_instance_ = load_function<fmi2FreeInstanceTYPE>("fmi2FreeInstance");
187  reset_ = load_function<fmi2ResetTYPE>("fmi2Reset");
188  setup_experiment_ = load_function<fmi2SetupExperimentTYPE>("fmi2SetupExperiment");
189  enter_initialization_mode_ = load_function<fmi2EnterInitializationModeTYPE>(
190  "fmi2EnterInitializationMode");
191  exit_initialization_mode_ = load_function<fmi2ExitInitializationModeTYPE>(
192  "fmi2ExitInitializationMode");
193  enter_continuous_time_mode_ = load_function<fmi2EnterContinuousTimeModeTYPE>(
194  "fmi2EnterContinuousTimeMode");
195  get_derivatives_ = load_function<fmi2GetDerivativesTYPE>("fmi2GetDerivatives");
196  set_time_ = load_function<fmi2SetTimeTYPE>("fmi2SetTime");
197  get_real_ = load_function<fmi2GetRealTYPE>("fmi2GetReal");
198  set_real_ = load_function<fmi2SetRealTYPE>("fmi2SetReal");
199  get_integer_ = load_function<fmi2GetIntegerTYPE>("fmi2GetInteger");
200  set_integer_ = load_function<fmi2SetIntegerTYPE>("fmi2SetInteger");
201  get_boolean_ = load_function<fmi2GetBooleanTYPE>("fmi2GetBoolean");
202  set_boolean_ = load_function<fmi2SetBooleanTYPE>("fmi2SetBoolean");
203  get_string_ = load_function<fmi2GetStringTYPE>("fmi2GetString");
204  set_string_ = load_function<fmi2SetStringTYPE>("fmi2SetString");
207  load_function<fmi2GetDirectionalDerivativeTYPE>("fmi2GetDirectionalDerivative");
208  }
209  new_discrete_states_ = load_function<fmi2NewDiscreteStatesTYPE>("fmi2NewDiscreteStates");
210 
211  // Callback functions
212  functions_.logger = logger;
213  functions_.allocateMemory = calloc;
214  functions_.freeMemory = free;
215  functions_.stepFinished = nullptr;
216  functions_.componentEnvironment = nullptr;
217 }
fmi2SetIntegerTYPE * set_integer_
Definition: fmu2.hpp:109
static void logger(fmi2ComponentEnvironment componentEnvironment, fmi2String instanceName, fmi2Status status, fmi2String category, fmi2String message,...)
Definition: fmu2.cpp:219
fmi2GetStringTYPE * get_string_
Definition: fmu2.hpp:110
bool provides_directional_derivatives_
Definition: fmu_impl.hpp:338

References enter_continuous_time_mode_, enter_initialization_mode_, exit_initialization_mode_, free_instance_, functions_, get_boolean_, get_derivatives_, get_directional_derivative_, get_integer_, get_real_, get_string_, instantiate_, logger(), new_discrete_states_, casadi::FmuInternal::provides_directional_derivatives_, reset_, set_boolean_, set_integer_, set_real_, set_string_, set_time_, and setup_experiment_.

◆ logger()

void casadi::Fmu2::logger ( fmi2ComponentEnvironment  componentEnvironment,
fmi2String  instanceName,
fmi2Status  status,
fmi2String  category,
fmi2String  message,
  ... 
)
static

Definition at line 219 of file fmu2.cpp.

223  {
224  // Variable number of arguments
225  va_list args;
226  va_start(args, message);
227  // Static & dynamic buffers
228  char buf[256];
229  size_t buf_sz = sizeof(buf);
230  char* buf_dyn = nullptr;
231  // Try to print with a small buffer
232  int n = vsnprintf(buf, buf_sz, message, args);
233  // Need a larger buffer?
234  if (n > buf_sz) {
235  buf_sz = n + 1;
236  buf_dyn = new char[buf_sz];
237  n = vsnprintf(buf_dyn, buf_sz, message, args);
238  }
239  // Print buffer content
240  if (n >= 0) {
241  uout() << "[" << instanceName << ":" << category << "] "
242  << (buf_dyn ? buf_dyn : buf) << std::endl;
243  }
244  // Cleanup
245  delete[] buf_dyn;
246  va_end(args);
247  // Throw error if failure
248  casadi_assert(n>=0, "Print failure while processing '" + std::string(message) + "'");
249 }
std::ostream & uout()

References casadi::uout().

Referenced by load_functions().

◆ n_in()

size_t casadi::FmuInternal::n_in ( ) const
inlineinherited

◆ n_out()

size_t casadi::FmuInternal::n_out ( ) const
inlineinherited

◆ pack_fmu()

std::string casadi::FmuInternal::pack_fmu ( const Dict &  files,
const std::string &  path 
)
staticinherited

Definition at line 830 of file fmu.cpp.

830  {
831  casadi_assert(Archiver::has_plugin("libzip"),
832  "pack_fmu requires libzip. Compile CasADi with WITH_LIBZIP=ON.");
833  // The {local_file -> archive path} map is exactly the set of zip entries: no staging
834  // directory or copies required.
835  std::vector<std::pair<std::string, std::string>> entries;
836  for (auto&& kv : files) {
837  std::string arc = kv.second.to_string();
838  if (arc.substr(0, 2) == "./") arc = arc.substr(2); // zip entries are root-relative
839  entries.push_back({kv.first, arc});
840  }
841  std::string out = path;
842  if (out.size() < 4 || out.substr(out.size() - 4) != ".fmu") out += ".fmu";
843  casadi_assert(Archiver::getPlugin("libzip").exposed.pack_entries(entries, out),
844  "Packing FMU '" + out + "' failed.");
845  // Clean up the loose files
846  for (auto&& e : entries) Filesystem::remove(e.first);
847  return out;
848 }
static bool has_plugin(const std::string &pname, bool verbose=false)
Check if a plugin is available or can be loaded.
static Plugin & getPlugin(const std::string &pname)
Load and get the creator function.
std::vector< casadi_int > path(const std::vector< casadi_int > &map, casadi_int i_start)

References casadi::PluginInterface< Archiver >::getPlugin(), casadi::PluginInterface< Archiver >::has_plugin(), casadi::path(), and casadi::Filesystem::remove().

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

◆ request()

void casadi::FmuInternal::request ( FmuMemory *  m,
size_t  ind 
) const
inherited

Definition at line 1363 of file fmu.cpp.

1363  {
1364  for (size_t id : ored_[ind]) {
1365  // Mark as requested
1366  m->omarked_.at(id) = true;
1367  // Also log corresponding input index
1368  m->wrt_.at(id) = -1;
1369  }
1370 }

References casadi::FmuMemory::omarked_, casadi::FmuInternal::ored_, and casadi::FmuMemory::wrt_.

◆ request_adj() [1/2]

void casadi::FmuInternal::request_adj ( FmuMemory *  m,
casadi_int  ind 
) const
inherited

Definition at line 1223 of file fmu.cpp.

1223  {
1224  // Request all sensitivities FIXME(@jaeandersson): should use compatible types
1225  casadi_int wrt_id = -1;
1226  for (size_t id : ired_[ind]) {
1227  casadi_int id2 = id;
1228  request_adj(m, 1, &id2, &wrt_id);
1229  }
1230 }
void request_adj(FmuMemory *m, casadi_int nsens, const casadi_int *id, const casadi_int *wrt_id) const
Definition: fmu.cpp:1214

References casadi::FmuInternal::ired_, and casadi::FmuInternal::request_adj().

◆ request_adj() [2/2]

void casadi::FmuInternal::request_adj ( FmuMemory *  m,
casadi_int  nsens,
const casadi_int *  id,
const casadi_int *  wrt_id 
) const
inherited

Definition at line 1214 of file fmu.cpp.

1215  {
1216  for (casadi_int i = 0; i < nsens; ++i) {
1217  m->imarked_.at(*id) = true;
1218  m->wrt_.at(*id) = wrt_id ? *wrt_id++ : -1;
1219  id++;
1220  }
1221 }

References casadi::FmuMemory::imarked_, and casadi::FmuMemory::wrt_.

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

◆ request_fwd() [1/2]

void casadi::FmuInternal::request_fwd ( FmuMemory *  m,
casadi_int  ind 
) const
inherited

Definition at line 1468 of file fmu.cpp.

1468  {
1469  // Request all sensitivities FIXME(@jaeandersson): should use compatible types
1470  casadi_int wrt_id = -1;
1471  for (size_t id : ored_[ind]) {
1472  casadi_int id2 = id;
1473  request_fwd(m, 1, &id2, &wrt_id);
1474  }
1475 }
void request_fwd(FmuMemory *m, casadi_int nsens, const casadi_int *id, const casadi_int *wrt_id) const
Definition: fmu.cpp:1459

References casadi::FmuInternal::ored_, and casadi::FmuInternal::request_fwd().

◆ request_fwd() [2/2]

void casadi::FmuInternal::request_fwd ( FmuMemory *  m,
casadi_int  nsens,
const casadi_int *  id,
const casadi_int *  wrt_id 
) const
inherited

Definition at line 1459 of file fmu.cpp.

1460  {
1461  for (casadi_int i = 0; i < nsens; ++i) {
1462  m->omarked_.at(*id) = true;
1463  m->wrt_.at(*id) = *wrt_id++;
1464  id++;
1465  }
1466 }

References casadi::FmuMemory::omarked_, and casadi::FmuMemory::wrt_.

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

◆ reset()

int casadi::Fmu2::reset ( void *  instance)

Definition at line 278 of file fmu2.cpp.

278  {
279  auto *c = static_cast<fmi2Component>(instance);
280  fmi2Status status = reset_(c);
281  if (status != fmi2OK) {
282  casadi_warning("fmi2Reset failed");
283  return 1;
284  }
285  return 0;
286 }

References reset_.

◆ serialize()

void casadi::FmuInternal::serialize ( SerializingStream &  s) const
inherited

Definition at line 1550 of file fmu.cpp.

1550  {
1551  serialize_type(s);
1552  serialize_body(s);
1553 }
virtual void serialize_body(SerializingStream &s) const
Definition: fmu.cpp:1559
virtual void serialize_type(SerializingStream &s) const
Definition: fmu.cpp:1555

References casadi::FmuInternal::serialize_body(), and casadi::FmuInternal::serialize_type().

◆ serialize_body()

void casadi::Fmu2::serialize_body ( SerializingStream &  s) const
overridevirtual

Reimplemented from casadi::FmuInternal.

Definition at line 594 of file fmu2.cpp.

594  {
596 
597  s.version("Fmu2", 2);
598  s.pack("Fmu2::vr_real", vr_real_);
599  s.pack("Fmu2::vr_integer", vr_integer_);
600  s.pack("Fmu2::vr_boolean", vr_boolean_);
601  s.pack("Fmu2::vr_string", vr_string_);
602  s.pack("Fmu2::init_real", init_real_);
603  s.pack("Fmu2::init_integer", init_integer_);
604  s.pack("Fmu2::init_boolean", init_boolean_);
605  s.pack("Fmu2::init_string", init_string_);
606 
607  s.pack("Fmu2::vn_aux_real", vn_aux_real_);
608  s.pack("Fmu2::vn_aux_integer", vn_aux_integer_);
609  s.pack("Fmu2::vn_aux_boolean", vn_aux_boolean_);
610  s.pack("Fmu2::vn_aux_string", vn_aux_string_);
611  s.pack("Fmu2::vr_aux_real", vr_aux_real_);
612  s.pack("Fmu2::vr_aux_integer", vr_aux_integer_);
613  s.pack("Fmu2::vr_aux_boolean", vr_aux_boolean_);
614  s.pack("Fmu2::vr_aux_string", vr_aux_string_);
615 }

References init_boolean_, init_integer_, init_real_, init_string_, casadi::SerializingStream::pack(), casadi::FmuInternal::serialize_body(), casadi::SerializingStream::version(), vn_aux_boolean_, vn_aux_integer_, vn_aux_real_, vn_aux_string_, vr_aux_boolean_, vr_aux_integer_, vr_aux_real_, vr_aux_string_, vr_boolean_, vr_integer_, vr_real_, and vr_string_.

◆ serialize_type()

void casadi::FmuInternal::serialize_type ( SerializingStream &  s) const
virtualinherited

Definition at line 1555 of file fmu.cpp.

1555  {
1556  s.pack("FmuInternal::type", class_name());
1557 }

References casadi::SharedObjectInternal::class_name(), and casadi::SerializingStream::pack().

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

◆ set()

void casadi::FmuInternal::set ( FmuMemory *  m,
size_t  ind,
const double *  value 
) const
inherited

Definition at line 1342 of file fmu.cpp.

1342  {
1343  if (value) {
1344  // Argument is given
1345  for (size_t id : ired_[ind]) {
1346  if (*value != m->ibuf_.at(id)) {
1347  m->ibuf_.at(id) = *value;
1348  m->imarked_.at(id) = true;
1349  }
1350  value++;
1351  }
1352  } else {
1353  // Argument is null - all zeros
1354  for (size_t id : ired_[ind]) {
1355  if (0 != m->ibuf_.at(id)) {
1356  m->ibuf_.at(id) = 0;
1357  m->imarked_.at(id) = true;
1358  }
1359  }
1360  }
1361 }

References casadi::FmuMemory::ibuf_, casadi::FmuMemory::imarked_, and casadi::FmuInternal::ired_.

◆ set_adj() [1/2]

void casadi::FmuInternal::set_adj ( FmuMemory *  m,
casadi_int  nseed,
const casadi_int *  id,
const double *  v 
) const
inherited

Definition at line 1192 of file fmu.cpp.

1193  {
1194  for (casadi_int i = 0; i < nseed; ++i) {
1195  if (*v != 0.) {
1196  m->osens_.at(*id) = *v;
1197  m->omarked_.at(*id) = true;
1198  }
1199  id++;
1200  v++;
1201  }
1202 }

References casadi::FmuMemory::omarked_, and casadi::FmuMemory::osens_.

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

◆ set_adj() [2/2]

void casadi::FmuInternal::set_adj ( FmuMemory *  m,
size_t  ind,
const double *  v 
) const
inherited

Definition at line 1204 of file fmu.cpp.

1204  {
1205  // If seeds are zero, no need to add to seed buffers
1206  if (!v) return;
1207  // Pass all seeds FIXME(@jaeandersson): should use compatible types
1208  for (size_t id : ored_[ind]) {
1209  casadi_int id2 = id;
1210  set_adj(m, 1, &id2, v++);
1211  }
1212 }
void set_adj(FmuMemory *m, casadi_int nseed, const casadi_int *id, const double *v) const
Definition: fmu.cpp:1192

References casadi::FmuInternal::ored_, and casadi::FmuInternal::set_adj().

◆ set_all()

int casadi::FmuInternal::set_all ( FmuMemory *  m,
const double *  values,
size_t  n_values 
) const
inherited

Definition at line 1372 of file fmu.cpp.

1372  {
1373  // Quick return if nothing to set
1374  if (n_values == 0) return 0;
1375  // Value references
1376  const unsigned int* vr = get_ptr(m->vr_in_);
1377  size_t n_vr = m->vr_in_.size();
1378  // Set time variable, if any
1379  if (has_independent_ && *vr == independent_vr_) {
1380  // Update FMU time
1381  if (set_time(m->instance, *values)) return 1;
1382  // Skip when setting remaining variables
1383  vr++;
1384  n_vr--;
1385  values++;
1386  n_values--;
1387  // Quick return if nothing left to set
1388  if (n_vr == 0) return 0;
1389  }
1390 
1391  // Set remaining variables
1392  if (set_real(m->instance, vr, n_vr, values, n_values)) {
1393  casadi_warning("Setting FMU variables failed");
1394  return 1;
1395  }
1396 
1397  return 0;
1398 }
virtual int set_time(void *instance, double t) const =0
unsigned int independent_vr_
Definition: fmu_impl.hpp:356
virtual int set_real(void *instance, const unsigned int *vr, size_t n_vr, const double *values, size_t n_values) const =0

References casadi::get_ptr(), casadi::FmuInternal::has_independent_, casadi::FmuInternal::independent_vr_, casadi::FmuMemory::instance, casadi::FmuInternal::set_real(), casadi::FmuInternal::set_time(), and casadi::FmuMemory::vr_in_.

Referenced by casadi::FmuInternal::eval(), and casadi::FmuInternal::eval_fd().

◆ set_fwd() [1/2]

void casadi::FmuInternal::set_fwd ( FmuMemory *  m,
casadi_int  nseed,
const casadi_int *  id,
const double *  v 
) const
inherited

Definition at line 1437 of file fmu.cpp.

1438  {
1439  for (casadi_int i = 0; i < nseed; ++i) {
1440  if (*v != 0.) {
1441  m->isens_.at(*id) = *v;
1442  m->imarked_.at(*id) = true;
1443  }
1444  id++;
1445  v++;
1446  }
1447 }

References casadi::FmuMemory::imarked_, and casadi::FmuMemory::isens_.

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

◆ set_fwd() [2/2]

void casadi::FmuInternal::set_fwd ( FmuMemory *  m,
size_t  ind,
const double *  v 
) const
inherited

Definition at line 1449 of file fmu.cpp.

1449  {
1450  // If seeds are zero, no need to add to seed buffers
1451  if (!v) return;
1452  // Pass all seeds FIXME(@jaeandersson): should use compatible types
1453  for (size_t id : ired_[ind]) {
1454  casadi_int id2 = id;
1455  set_fwd(m, 1, &id2, v++);
1456  }
1457 }
void set_fwd(FmuMemory *m, casadi_int nseed, const casadi_int *id, const double *v) const
Definition: fmu.cpp:1437

References casadi::FmuInternal::ired_, and casadi::FmuInternal::set_fwd().

◆ set_real()

int casadi::Fmu2::set_real ( void *  instance,
const unsigned int *  vr,
size_t  n_vr,
const double *  values,
size_t  n_values 
) const
overridevirtual

Implements casadi::FmuInternal.

Definition at line 355 of file fmu2.cpp.

356  {
357  casadi_assert(n_vr == n_values, "Vector-valued variables not supported in FMI 2");
358 
359  fmi2Status status = set_real_(instance, vr, n_vr, values);
360  return status != fmi2OK;
361 }

References set_real_.

◆ set_time()

int casadi::Fmu2::set_time ( void *  instance,
double  t 
) const
overridevirtual

Implements casadi::FmuInternal.

Definition at line 350 of file fmu2.cpp.

350  {
351  fmi2Status status = set_time_(instance, t);
352  return status != fmi2OK;
353 }

References set_time_.

◆ set_values()

int casadi::Fmu2::set_values ( void *  instance) const
overridevirtual

Implements casadi::FmuInternal.

Definition at line 406 of file fmu2.cpp.

406  {
407  auto *c = static_cast<fmi2Component>(instance);
408  // Pass real values before initialization
409  if (!vr_real_.empty()) {
410  fmi2Status status = set_real_(c, get_ptr(vr_real_), vr_real_.size(), get_ptr(init_real_));
411  if (status != fmi2OK) {
412  casadi_warning("fmi2SetReal failed");
413  return 1;
414  }
415  }
416  // Pass integer values before initialization (also enums)
417  if (!vr_integer_.empty()) {
418  fmi2Status status = set_integer_(c, get_ptr(vr_integer_), vr_integer_.size(),
420  if (status != fmi2OK) {
421  casadi_warning("fmi2SetInteger failed");
422  return 1;
423  }
424  }
425  // Pass boolean values before initialization
426  if (!vr_boolean_.empty()) {
427  fmi2Status status = set_boolean_(c, get_ptr(vr_boolean_), vr_boolean_.size(),
429  if (status != fmi2OK) {
430  casadi_warning("fmi2SetBoolean failed");
431  return 1;
432  }
433  }
434  // Pass string values before initialization
435  for (size_t k = 0; k < vr_string_.size(); ++k) {
436  fmi2ValueReference vr = vr_string_[k];
437  fmi2String value = init_string_[k].c_str();
438  fmi2Status status = set_string_(c, &vr, 1, &value);
439  if (status != fmi2OK) {
440  casadi_error("fmi2SetString failed for value reference " + str(vr));
441  }
442  }
443  // Successful return
444  return 0;
445 }

References casadi::get_ptr(), init_boolean_, init_integer_, init_real_, init_string_, set_boolean_, set_integer_, set_real_, set_string_, casadi::str(), vr_boolean_, vr_integer_, vr_real_, and vr_string_.

◆ 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  }

◆ system_infix()

std::string casadi::Fmu2::system_infix ( ) const
inlineoverridevirtual

Implements casadi::FmuInternal.

Definition at line 130 of file fmu2.hpp.

130 { return dll_infix(); }
static std::string dll_infix()
Definition: fmu2.cpp:65

◆ update_discrete_states()

int casadi::Fmu2::update_discrete_states ( void *  instance,
EventMemory *  eventmem 
) const
overridevirtual

Implements casadi::FmuInternal.

Definition at line 328 of file fmu2.cpp.

328  {
329  auto *c = static_cast<fmi2Component>(instance);
330  // Return arguments in FMI types
331  fmi2EventInfo eventInfo;
332  eventInfo.newDiscreteStatesNeeded = fmi2False;
333  eventInfo.terminateSimulation = fmi2False;
334  eventInfo.nominalsOfContinuousStatesChanged = fmi2False;
335  eventInfo.valuesOfContinuousStatesChanged = fmi2False;
336  eventInfo.nextEventTimeDefined = fmi2False;
337  eventInfo.nextEventTime = 0.0;
338  // Call FMU
339  fmi2Status status = new_discrete_states_(c, &eventInfo);
340  // Pass to event iteration memory
341  eventmem->discrete_states_need_update = eventInfo.newDiscreteStatesNeeded;
342  eventmem->terminate_simulation = eventInfo.terminateSimulation;
343  eventmem->nominals_of_continuous_states_changed = eventInfo.nominalsOfContinuousStatesChanged;
344  eventmem->values_of_continuous_states_changed = eventInfo.valuesOfContinuousStatesChanged;
345  eventmem->next_event_time_defined = eventInfo.nextEventTimeDefined;
346  eventmem->next_event_time = eventInfo.nextEventTime;
347  return status != fmi2OK;
348 }

References casadi::EventMemory::discrete_states_need_update, new_discrete_states_, casadi::EventMemory::next_event_time, casadi::EventMemory::next_event_time_defined, casadi::EventMemory::nominals_of_continuous_states_changed, casadi::EventMemory::terminate_simulation, and casadi::EventMemory::values_of_continuous_states_changed.

◆ weak()

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

Definition at line 67 of file generic_shared_internal.hpp.

215  {
216 #ifdef CASADI_WITH_THREADSAFE_SYMBOLICS
217  auto* w = weak_ref_.load(std::memory_order_acquire);
218  if (!w) {
219  auto* nw = new GenericWeakRef<Shared, Internal>(static_cast<Internal*>(this));
220  GenericWeakRef<Shared, Internal>* expected = nullptr;
221  if (weak_ref_.compare_exchange_strong(
222  expected, nw, std::memory_order_release, std::memory_order_acquire)) {
223  w = nw;
224  } else {
225  delete nw; // lost the race; another thread published first
226  w = expected;
227  }
228  }
229  return w;
230 #else
231  if (weak_ref_==nullptr) {
232  weak_ref_ = new GenericWeakRef<Shared, Internal>(static_cast<Internal*>(this));
233  }
234  return weak_ref_;
235 #endif // CASADI_WITH_THREADSAFE_SYMBOLICS
236  }

Member Data Documentation

◆ aux_

std::vector<std::string> casadi::FmuInternal::aux_
protectedinherited

◆ aux_value_

Value casadi::Fmu2::aux_value_

Definition at line 126 of file fmu2.hpp.

Referenced by finalize(), and get_aux().

◆ can_be_instantiated_only_once_per_process_

bool casadi::FmuInternal::can_be_instantiated_only_once_per_process_
protectedinherited

◆ do_evaluation_dance_

bool casadi::FmuInternal::do_evaluation_dance_
protectedinherited

◆ enter_continuous_time_mode_

fmi2EnterContinuousTimeModeTYPE* casadi::Fmu2::enter_continuous_time_mode_

Definition at line 101 of file fmu2.hpp.

Referenced by enter_continuous_time_mode(), Fmu2(), and load_functions().

◆ enter_initialization_mode_

fmi2EnterInitializationModeTYPE* casadi::Fmu2::enter_initialization_mode_

Definition at line 99 of file fmu2.hpp.

Referenced by enter_initialization_mode(), Fmu2(), and load_functions().

◆ exit_initialization_mode_

fmi2ExitInitializationModeTYPE* casadi::Fmu2::exit_initialization_mode_

Definition at line 100 of file fmu2.hpp.

Referenced by exit_initialization_mode(), Fmu2(), and load_functions().

◆ fmutol_

double casadi::FmuInternal::fmutol_
protectedinherited

◆ free_instance_

fmi2FreeInstanceTYPE* casadi::Fmu2::free_instance_

Definition at line 96 of file fmu2.hpp.

Referenced by Fmu2(), free_instance(), and load_functions().

◆ functions_

fmi2CallbackFunctions casadi::Fmu2::functions_

Definition at line 116 of file fmu2.hpp.

Referenced by instantiate(), and load_functions().

◆ get_boolean_

fmi2GetBooleanTYPE* casadi::Fmu2::get_boolean_

Definition at line 106 of file fmu2.hpp.

Referenced by get_aux_impl(), and load_functions().

◆ get_derivatives_

fmi2GetDerivativesTYPE* casadi::Fmu2::get_derivatives_

Definition at line 102 of file fmu2.hpp.

Referenced by get_derivatives(), get_real(), and load_functions().

◆ get_directional_derivative_

fmi2GetDirectionalDerivativeTYPE* casadi::Fmu2::get_directional_derivative_

Definition at line 112 of file fmu2.hpp.

Referenced by Fmu2(), get_directional_derivative(), and load_functions().

◆ get_integer_

fmi2GetIntegerTYPE* casadi::Fmu2::get_integer_

Definition at line 108 of file fmu2.hpp.

Referenced by get_aux_impl(), and load_functions().

◆ get_real_

fmi2GetRealTYPE* casadi::Fmu2::get_real_

Definition at line 104 of file fmu2.hpp.

Referenced by Fmu2(), get_aux_impl(), get_real(), and load_functions().

◆ get_string_

fmi2GetStringTYPE* casadi::Fmu2::get_string_

Definition at line 110 of file fmu2.hpp.

Referenced by load_functions().

◆ has_independent_

bool casadi::FmuInternal::has_independent_
protectedinherited

◆ hess_sp_

Sparsity casadi::FmuInternal::hess_sp_
protectedinherited

◆ iind_

std::vector<size_t> casadi::FmuInternal::iind_
protectedinherited

◆ iind_map_

std::vector<size_t> casadi::FmuInternal::iind_map_
protectedinherited

◆ independent_vr_

unsigned int casadi::FmuInternal::independent_vr_
protectedinherited

Definition at line 356 of file fmu_impl.hpp.

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

◆ init_boolean_

std::vector<fmi2Boolean> casadi::Fmu2::init_boolean_

Definition at line 85 of file fmu2.hpp.

Referenced by Fmu2(), init(), serialize_body(), and set_values().

◆ init_integer_

std::vector<fmi2Integer> casadi::Fmu2::init_integer_

Definition at line 84 of file fmu2.hpp.

Referenced by Fmu2(), init(), serialize_body(), and set_values().

◆ init_real_

std::vector<fmi2Real> casadi::Fmu2::init_real_

Definition at line 83 of file fmu2.hpp.

Referenced by Fmu2(), init(), serialize_body(), and set_values().

◆ init_string_

std::vector<std::string> casadi::Fmu2::init_string_

Definition at line 86 of file fmu2.hpp.

Referenced by Fmu2(), init(), serialize_body(), and set_values().

◆ instance_name_

std::string casadi::FmuInternal::instance_name_
protectedinherited

◆ instantiate_

fmi2InstantiateTYPE* casadi::Fmu2::instantiate_

Definition at line 95 of file fmu2.hpp.

Referenced by Fmu2(), instantiate(), and load_functions().

◆ instantiation_token_

std::string casadi::FmuInternal::instantiation_token_
protectedinherited

◆ ired_

std::vector<std::vector<size_t> > casadi::FmuInternal::ired_
protectedinherited

◆ jac_sp_

Sparsity casadi::FmuInternal::jac_sp_
protectedinherited

◆ li_

Importer casadi::FmuInternal::li_
protectedinherited

◆ logging_on_

bool casadi::FmuInternal::logging_on_
protectedinherited

◆ max_in_

std::vector<double> casadi::FmuInternal::max_in_
protectedinherited

◆ max_out_

std::vector<double> casadi::FmuInternal::max_out_
protectedinherited

◆ min_in_

std::vector<double> casadi::FmuInternal::min_in_
protectedinherited

◆ min_out_

std::vector<double> casadi::FmuInternal::min_out_
protectedinherited

◆ name_

std::string casadi::FmuInternal::name_
protectedinherited

◆ new_discrete_states_

fmi2NewDiscreteStatesTYPE* casadi::Fmu2::new_discrete_states_

Definition at line 113 of file fmu2.hpp.

Referenced by Fmu2(), load_functions(), and update_discrete_states().

◆ nominal_in_

std::vector<double> casadi::FmuInternal::nominal_in_
protectedinherited

◆ nominal_out_

std::vector<double> casadi::FmuInternal::nominal_out_
protectedinherited

◆ number_of_event_indicators_

casadi_int casadi::FmuInternal::number_of_event_indicators_
protectedinherited

◆ nx_

size_t casadi::FmuInternal::nx_
protectedinherited

◆ oind_

std::vector<size_t> casadi::FmuInternal::oind_
protectedinherited

◆ oind_map_

std::vector<size_t> casadi::FmuInternal::oind_map_
protectedinherited

◆ ored_

std::vector<std::vector<size_t> > casadi::FmuInternal::ored_
protectedinherited

◆ provides_adjoint_derivatives_

bool casadi::FmuInternal::provides_adjoint_derivatives_
protectedinherited

◆ provides_directional_derivatives_

bool casadi::FmuInternal::provides_directional_derivatives_
protectedinherited

◆ reset_

fmi2ResetTYPE* casadi::Fmu2::reset_

Definition at line 97 of file fmu2.hpp.

Referenced by Fmu2(), load_functions(), and reset().

◆ resource_

Resource casadi::FmuInternal::resource_
protectedinherited

◆ resource_loc_

std::string casadi::FmuInternal::resource_loc_
protectedinherited

◆ scheme_

std::map<std::string, std::vector<size_t> > casadi::FmuInternal::scheme_
protectedinherited

◆ scheme_in_

std::vector<std::string> casadi::FmuInternal::scheme_in_
protectedinherited

◆ scheme_out_

std::vector<std::string> casadi::FmuInternal::scheme_out_
protectedinherited

◆ set_boolean_

fmi2SetBooleanTYPE* casadi::Fmu2::set_boolean_

Definition at line 107 of file fmu2.hpp.

Referenced by Fmu2(), load_functions(), and set_values().

◆ set_integer_

fmi2SetIntegerTYPE* casadi::Fmu2::set_integer_

Definition at line 109 of file fmu2.hpp.

Referenced by load_functions(), and set_values().

◆ set_real_

fmi2SetRealTYPE* casadi::Fmu2::set_real_

Definition at line 105 of file fmu2.hpp.

Referenced by Fmu2(), load_functions(), set_real(), and set_values().

◆ set_string_

fmi2SetStringTYPE* casadi::Fmu2::set_string_

Definition at line 111 of file fmu2.hpp.

Referenced by get_aux_impl(), load_functions(), and set_values().

◆ set_time_

fmi2SetTimeTYPE* casadi::Fmu2::set_time_

Definition at line 103 of file fmu2.hpp.

Referenced by Fmu2(), load_functions(), and set_time().

◆ setup_experiment_

fmi2SetupExperimentTYPE* casadi::Fmu2::setup_experiment_

Definition at line 98 of file fmu2.hpp.

Referenced by Fmu2(), instantiate(), and load_functions().

◆ start_time_

double casadi::FmuInternal::start_time_
protectedinherited

◆ value_in_

std::vector<double> casadi::FmuInternal::value_in_
protectedinherited

◆ vn_aux_boolean_

std::vector<std::string> casadi::Fmu2::vn_aux_boolean_

Definition at line 89 of file fmu2.hpp.

Referenced by finalize(), Fmu2(), get_stats(), init(), init_mem(), and serialize_body().

◆ vn_aux_integer_

std::vector<std::string> casadi::Fmu2::vn_aux_integer_

Definition at line 89 of file fmu2.hpp.

Referenced by finalize(), Fmu2(), get_stats(), init(), init_mem(), and serialize_body().

◆ vn_aux_real_

std::vector<std::string> casadi::Fmu2::vn_aux_real_

Definition at line 89 of file fmu2.hpp.

Referenced by finalize(), Fmu2(), get_stats(), init(), init_mem(), and serialize_body().

◆ vn_aux_string_

std::vector<std::string> casadi::Fmu2::vn_aux_string_

Definition at line 89 of file fmu2.hpp.

Referenced by finalize(), Fmu2(), get_stats(), init(), init_mem(), and serialize_body().

◆ vn_in_

std::vector<std::string> casadi::FmuInternal::vn_in_
protectedinherited

◆ vn_out_

std::vector<std::string> casadi::FmuInternal::vn_out_
protectedinherited

◆ vr_aux_boolean_

std::vector<fmi2ValueReference> casadi::Fmu2::vr_aux_boolean_

Definition at line 90 of file fmu2.hpp.

Referenced by Fmu2(), get_aux_impl(), init(), and serialize_body().

◆ vr_aux_integer_

std::vector<fmi2ValueReference> casadi::Fmu2::vr_aux_integer_

Definition at line 90 of file fmu2.hpp.

Referenced by Fmu2(), get_aux_impl(), init(), and serialize_body().

◆ vr_aux_real_

std::vector<fmi2ValueReference> casadi::Fmu2::vr_aux_real_

Definition at line 90 of file fmu2.hpp.

Referenced by Fmu2(), get_aux_impl(), init(), and serialize_body().

◆ vr_aux_string_

std::vector<fmi2ValueReference> casadi::Fmu2::vr_aux_string_

Definition at line 90 of file fmu2.hpp.

Referenced by Fmu2(), get_aux_impl(), init(), and serialize_body().

◆ vr_boolean_

std::vector<fmi2ValueReference> casadi::Fmu2::vr_boolean_

Definition at line 82 of file fmu2.hpp.

Referenced by Fmu2(), init(), serialize_body(), and set_values().

◆ vr_in_

std::vector<unsigned int> casadi::FmuInternal::vr_in_
protectedinherited

◆ vr_integer_

std::vector<fmi2ValueReference> casadi::Fmu2::vr_integer_

Definition at line 82 of file fmu2.hpp.

Referenced by Fmu2(), init(), serialize_body(), and set_values().

◆ vr_out_

std::vector<unsigned int> casadi::FmuInternal::vr_out_
protectedinherited

◆ vr_real_

std::vector<fmi2ValueReference> casadi::Fmu2::vr_real_

Definition at line 82 of file fmu2.hpp.

Referenced by Fmu2(), init(), serialize_body(), and set_values().

◆ vr_string_

std::vector<fmi2ValueReference> casadi::Fmu2::vr_string_

Definition at line 82 of file fmu2.hpp.

Referenced by Fmu2(), init(), serialize_body(), and set_values().

◆ warning_fired_discrete_states_need_update_

bool casadi::FmuInternal::warning_fired_discrete_states_need_update_
mutableprotectedinherited

◆ warning_fired_next_event_time_defined_

bool casadi::FmuInternal::warning_fired_next_event_time_defined_
mutableprotectedinherited

◆ warning_fired_nominals_of_continuous_states_changed_

bool casadi::FmuInternal::warning_fired_nominals_of_continuous_states_changed_
mutableprotectedinherited

◆ warning_fired_terminate_simulation_

bool casadi::FmuInternal::warning_fired_terminate_simulation_
mutableprotectedinherited

◆ warning_fired_values_of_continuous_states_changed_

bool casadi::FmuInternal::warning_fired_values_of_continuous_states_changed_
mutableprotectedinherited

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