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

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

#include <fmu3.hpp>

Detailed Description

Author
Joel Andersson
Date
2024

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

Definition at line 42 of file fmu3.hpp.

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

Classes

struct  Value
 

Public Types

using weak_ref_type = WeakRefInternal
 

Public Member Functions

 Fmu3 (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)
 
 ~Fmu3 () 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 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 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
 
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 log_message_callback (fmi3InstanceEnvironment instanceEnvironment, fmi3Status status, fmi3String category, fmi3String message)
 
static Fmu3 * 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< fmi3ValueReference > vr_real_
 
std::vector< fmi3ValueReference > vr_integer_
 
std::vector< fmi3ValueReference > vr_boolean_
 
std::vector< fmi3ValueReference > vr_string_
 
std::vector< fmi3Float64 > init_real_
 
std::vector< fmi3Int32 > init_integer_
 
std::vector< fmi3Boolean > 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< fmi3ValueReference > vr_aux_real_
 
std::vector< fmi3ValueReference > vr_aux_integer_
 
std::vector< fmi3ValueReference > vr_aux_boolean_
 
std::vector< fmi3ValueReference > vr_aux_string_
 
fmi3InstantiateModelExchangeTYPE * instantiate_model_exchange_
 
fmi3FreeInstanceTYPE * free_instance_
 
fmi3ResetTYPE * reset_
 
fmi3EnterInitializationModeTYPE * enter_initialization_mode_
 
fmi3ExitInitializationModeTYPE * exit_initialization_mode_
 
fmi3EnterContinuousTimeModeTYPE * enter_continuous_time_mode_
 
fmi3SetTimeTYPE * set_time_
 
fmi3GetFloat64TYPE * get_float64_
 
fmi3SetFloat64TYPE * set_float64_
 
fmi3GetBooleanTYPE * get_boolean_
 
fmi3SetBooleanTYPE * set_boolean_
 
fmi3GetInt32TYPE * get_int32_
 
fmi3SetInt32TYPE * set_int32_
 
fmi3GetStringTYPE * get_string_
 
fmi3SetStringTYPE * set_string_
 
fmi3GetDirectionalDerivativeTYPE * get_directional_derivative_
 
fmi3GetAdjointDerivativeTYPE * get_adjoint_derivative_
 
fmi3UpdateDiscreteStatesTYPE * update_discrete_states_
 
Value aux_value_
 

Protected Member Functions

 Fmu3 (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

◆ Fmu3() [1/2]

casadi::Fmu3::Fmu3 ( 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 470 of file fmu3.cpp.

475  : FmuInternal(name, scheme_in, scheme_out, scheme, aux) {
476  instantiate_model_exchange_ = nullptr;
477  free_instance_ = nullptr;
478  reset_ = nullptr;
479  enter_initialization_mode_ = nullptr;
480  exit_initialization_mode_ = nullptr;
481  enter_continuous_time_mode_ = nullptr;
482  set_time_ = nullptr;
483  set_float64_ = nullptr;
484  set_boolean_ = nullptr;
485  get_float64_ = nullptr;
486  get_directional_derivative_ = nullptr;
487  get_adjoint_derivative_ = nullptr;
488  update_discrete_states_ = nullptr;
489 }
fmi3InstantiateModelExchangeTYPE * instantiate_model_exchange_
Definition: fmu3.hpp:95
fmi3ExitInitializationModeTYPE * exit_initialization_mode_
Definition: fmu3.hpp:99
fmi3SetFloat64TYPE * set_float64_
Definition: fmu3.hpp:103
fmi3EnterContinuousTimeModeTYPE * enter_continuous_time_mode_
Definition: fmu3.hpp:100
fmi3SetTimeTYPE * set_time_
Definition: fmu3.hpp:101
fmi3GetFloat64TYPE * get_float64_
Definition: fmu3.hpp:102
fmi3SetBooleanTYPE * set_boolean_
Definition: fmu3.hpp:105
fmi3GetDirectionalDerivativeTYPE * get_directional_derivative_
Definition: fmu3.hpp:110
fmi3ResetTYPE * reset_
Definition: fmu3.hpp:97
fmi3GetAdjointDerivativeTYPE * get_adjoint_derivative_
Definition: fmu3.hpp:111
fmi3UpdateDiscreteStatesTYPE * update_discrete_states_
Definition: fmu3.hpp:112
fmi3FreeInstanceTYPE * free_instance_
Definition: fmu3.hpp:96
fmi3EnterInitializationModeTYPE * enter_initialization_mode_
Definition: fmu3.hpp:98
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_adjoint_derivative_, get_directional_derivative_, get_float64_, instantiate_model_exchange_, reset_, set_boolean_, set_float64_, set_time_, and update_discrete_states_.

Referenced by deserialize().

◆ ~Fmu3()

casadi::Fmu3::~Fmu3 ( )
override

Definition at line 58 of file fmu3.cpp.

58  {
59 }

◆ Fmu3() [2/2]

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

Definition at line 498 of file fmu3.cpp.

498  : FmuInternal(s) {
499  instantiate_model_exchange_ = nullptr;
500  free_instance_ = nullptr;
501  reset_ = nullptr;
502  enter_initialization_mode_ = nullptr;
503  exit_initialization_mode_ = nullptr;
504  enter_continuous_time_mode_ = nullptr;
505  set_time_ = nullptr;
506  set_float64_ = nullptr;
507  set_boolean_ = nullptr;
508  get_float64_ = nullptr;
509  get_directional_derivative_ = nullptr;
510  get_adjoint_derivative_ = nullptr;
511  update_discrete_states_ = nullptr;
512 
513  s.version("Fmu3", 1);
514  s.unpack("Fmu3::vr_real", vr_real_);
515  s.unpack("Fmu3::vr_integer", vr_integer_);
516  s.unpack("Fmu3::vr_boolean", vr_boolean_);
517  s.unpack("Fmu3::vr_string", vr_string_);
518  s.unpack("Fmu3::init_real", init_real_);
519  s.unpack("Fmu3::init_integer", init_integer_);
520  s.unpack("Fmu3::init_boolean", init_boolean_);
521  s.unpack("Fmu3::init_string", init_string_);
522 
523  s.unpack("Fmu3::vn_aux_real", vn_aux_real_);
524  s.unpack("Fmu3::vn_aux_integer", vn_aux_integer_);
525  s.unpack("Fmu3::vn_aux_boolean", vn_aux_boolean_);
526  s.unpack("Fmu3::vn_aux_string", vn_aux_string_);
527  s.unpack("Fmu3::vr_aux_real", vr_aux_real_);
528  s.unpack("Fmu3::vr_aux_integer", vr_aux_integer_);
529  s.unpack("Fmu3::vr_aux_boolean", vr_aux_boolean_);
530  s.unpack("Fmu3::vr_aux_string", vr_aux_string_);
531 }
std::vector< fmi3ValueReference > vr_string_
Definition: fmu3.hpp:82
std::vector< fmi3ValueReference > vr_aux_real_
Definition: fmu3.hpp:90
std::vector< std::string > vn_aux_boolean_
Definition: fmu3.hpp:89
std::vector< fmi3ValueReference > vr_boolean_
Definition: fmu3.hpp:82
std::vector< fmi3ValueReference > vr_aux_string_
Definition: fmu3.hpp:90
std::vector< fmi3Boolean > init_boolean_
Definition: fmu3.hpp:85
std::vector< fmi3Int32 > init_integer_
Definition: fmu3.hpp:84
std::vector< std::string > vn_aux_real_
Definition: fmu3.hpp:89
std::vector< std::string > init_string_
Definition: fmu3.hpp:86
std::vector< fmi3ValueReference > vr_integer_
Definition: fmu3.hpp:82
std::vector< std::string > vn_aux_integer_
Definition: fmu3.hpp:89
std::vector< fmi3ValueReference > vr_real_
Definition: fmu3.hpp:82
std::vector< fmi3ValueReference > vr_aux_boolean_
Definition: fmu3.hpp:90
std::vector< fmi3ValueReference > vr_aux_integer_
Definition: fmu3.hpp:90
std::vector< fmi3Float64 > init_real_
Definition: fmu3.hpp:83
std::vector< std::string > vn_aux_string_
Definition: fmu3.hpp:89

References enter_continuous_time_mode_, enter_initialization_mode_, exit_initialization_mode_, free_instance_, get_adjoint_derivative_, get_directional_derivative_, get_float64_, init_boolean_, init_integer_, init_real_, init_string_, instantiate_model_exchange_, reset_, set_boolean_, set_float64_, set_time_, casadi::DeserializingStream::unpack(), update_discrete_states_, 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::Fmu3::alloc_mem ( const FmuFunction &  f) const
overridevirtual

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

Implements casadi::FmuInternal.

Definition at line 50 of file fmu3.cpp.

50  {
51  return new Fmu3Memory(f);
52 }

◆ class_name()

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

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

Implements casadi::SharedObjectInternal.

Definition at line 55 of file fmu3.hpp.

55 { return "Fmu3";}

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

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

Definition at line 492 of file fmu3.cpp.

492  {
493  Fmu3* ret = new Fmu3(s);
494  ret->finalize();
495  return ret;
496 }
Fmu3(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: fmu3.cpp:470

References finalize(), and Fmu3().

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::Fmu3::dll_infix ( )
static

Definition at line 61 of file fmu3.cpp.

61  {
63 }

References casadi::FmuInternal::fmi3_dll_infix().

◆ 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::Fmu3::enter_continuous_time_mode ( void *  instance) const
overridevirtual

Implements casadi::FmuInternal.

Definition at line 254 of file fmu3.cpp.

254  {
255  auto *c = static_cast<fmi3Instance>(instance);
256  fmi3Status status = enter_continuous_time_mode_(c);
257  if (status != fmi3OK) {
258  casadi_warning("fmi3EnterContinuousTimeMode failed");
259  return 1;
260  }
261  return 0;
262 }

References enter_continuous_time_mode_.

◆ enter_initialization_mode()

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

Implements casadi::FmuInternal.

Definition at line 234 of file fmu3.cpp.

234  {
235  auto *c = static_cast<fmi3Instance>(instance);
236  fmi3Status status = enter_initialization_mode_(c, fmutol_ > 0, fmutol_, 0., fmi3True, 1.);
237  if (status != fmi3OK) {
238  casadi_warning("fmi3EnterInitializationMode failed: " + str(status));
239  return 1;
240  }
241  return 0;
242 }

References enter_initialization_mode_, casadi::FmuInternal::fmutol_, 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::Fmu3::exit_initialization_mode ( void *  instance) const
overridevirtual

Implements casadi::FmuInternal.

Definition at line 244 of file fmu3.cpp.

244  {
245  auto *c = static_cast<fmi3Instance>(instance);
246  fmi3Status status = exit_initialization_mode_(c);
247  if (status != fmi3OK) {
248  casadi_warning("fmi3ExitInitializationMode failed");
249  return 1;
250  }
251  return 0;
252 }

References exit_initialization_mode_.

◆ finalize()

void casadi::Fmu3::finalize ( )
overridevirtual

Allocate numerical values for initial auxilliary variables

Reimplemented from casadi::FmuInternal.

Definition at line 153 of file fmu3.cpp.

153  {
155  aux_value_.v_real.resize(vn_aux_real_.size());
156  aux_value_.v_integer.resize(vn_aux_integer_.size());
157  aux_value_.v_boolean.resize(vn_aux_boolean_.size());
158  aux_value_.v_string.resize(vn_aux_string_.size());
159 
160  // Recursive call
162 }
Value aux_value_
Definition: fmu3.hpp:122
virtual void finalize()
Definition: fmu.cpp:593
std::vector< fmi3Float64 > v_real
Definition: fmu3.hpp:116
std::vector< std::string > v_string
Definition: fmu3.hpp:119
std::vector< fmi3Boolean > v_boolean
Definition: fmu3.hpp:118
std::vector< fmi3Int32 > v_integer
Definition: fmu3.hpp:117

References aux_value_, casadi::FmuInternal::finalize(), casadi::Fmu3::Value::v_boolean, casadi::Fmu3::Value::v_integer, casadi::Fmu3::Value::v_real, casadi::Fmu3::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 dll_infix().

◆ free_instance()

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

Implements casadi::FmuInternal.

Definition at line 215 of file fmu3.cpp.

215  {
216  if (free_instance_) {
217  auto *c = static_cast<fmi3Instance>(instance);
218  free_instance_(c);
219  } else {
220  casadi_warning("No free_instance function pointer available");
221  }
222 }

References free_instance_.

◆ free_mem()

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

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

Implements casadi::FmuInternal.

Definition at line 54 of file fmu3.cpp.

54  {
55  delete static_cast<Fmu3Memory*>(mem);
56 }

◆ 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::Fmu3::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
overridevirtual

Reimplemented from casadi::FmuInternal.

Definition at line 326 of file fmu3.cpp.

328  {
329  fmi3Status status = get_adjoint_derivative_(instance, vr_out, n_out, vr_in, n_in,
330  seed, n_seed, sensitivity, n_sensitivity);
331  return status != fmi3OK;
332 }
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_adjoint_derivative_, casadi::FmuInternal::n_in(), and casadi::FmuInternal::n_out().

◆ 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::Fmu3::get_aux ( void *  instance)
overridevirtual

Implements casadi::FmuInternal.

Definition at line 379 of file fmu3.cpp.

379  {
380  return get_aux_impl(instance, aux_value_);
381 }
int get_aux_impl(void *instance, Value &aux_value) const
Definition: fmu3.cpp:383

References aux_value_, and get_aux_impl().

◆ get_aux_impl()

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

Definition at line 383 of file fmu3.cpp.

383  {
384  auto *c = static_cast<fmi3Instance>(instance);
385  // Get real auxilliary variables
386  if (!vr_aux_real_.empty()) {
387  fmi3Status status = get_float64_(c, get_ptr(vr_aux_real_), vr_aux_real_.size(),
388  get_ptr(aux_value.v_real), aux_value.v_real.size());
389  if (status != fmi3OK) {
390  casadi_warning("fmi3GetFloat64 failed");
391  return 1;
392  }
393  }
394 
395  // Get integer/enum auxilliary variables
396  if (!vr_aux_integer_.empty()) {
397  fmi3Status status = get_int32_(c, get_ptr(vr_aux_integer_), vr_aux_integer_.size(),
398  get_ptr(aux_value.v_integer), aux_value.v_integer.size());
399  if (status != fmi3OK) {
400  casadi_warning("fmi3GetInt32 failed");
401  return 1;
402  }
403  }
404 
405  // Get boolean auxilliary variables
406  casadi_assert(vr_aux_boolean_.size() == aux_value.v_boolean.size(),
407  "Vector valued variables not supported");
408  for (size_t k = 0; k < vr_aux_boolean_.size(); ++k) {
409  fmi3Boolean value;
410  fmi3Status status = get_boolean_(c, &vr_aux_boolean_[k], 1, &value, 1);
411  if (status != fmi3OK) {
412  casadi_warning("fmi3GetBoolean failed for value reference " + str(vr_aux_boolean_[k]));
413  return 1;
414  }
415  aux_value.v_boolean[k] = value;
416  }
417 
418  // Get string auxilliary variables
419  for (size_t k = 0; k < vr_aux_string_.size(); ++k) {
420  fmi3ValueReference vr = vr_aux_string_[k];
421  fmi3String value = aux_value.v_string.at(k).c_str();
422  fmi3Status status = set_string_(c, &vr, 1, &value, 1);
423  if (status != fmi3OK) {
424  casadi_error("fmi3GetString failed for value reference " + str(vr));
425  }
426  }
427  // Successful return
428  return 0;
429 }
fmi3GetInt32TYPE * get_int32_
Definition: fmu3.hpp:106
fmi3SetStringTYPE * set_string_
Definition: fmu3.hpp:109
fmi3GetBooleanTYPE * get_boolean_
Definition: fmu3.hpp:104

References get_boolean_, get_float64_, get_int32_, casadi::get_ptr(), set_string_, casadi::str(), casadi::Fmu3::Value::v_boolean, casadi::Fmu3::Value::v_integer, casadi::Fmu3::Value::v_real, casadi::Fmu3::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::Fmu3::get_derivatives ( void *  instance,
double *  derivatives,
size_t  nx 
) const
overridevirtual

Implements casadi::FmuInternal.

Definition at line 284 of file fmu3.cpp.

284  {
285  return 0;
286 }

◆ get_directional_derivative()

int casadi::Fmu3::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 318 of file fmu3.cpp.

320  {
321  fmi3Status status = get_directional_derivative_(instance, vr_out, n_out, vr_in, n_in,
322  seed, n_seed, sensitivity, n_sensitivity);
323  return status != fmi3OK;
324 }

References get_directional_derivative_, 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::Fmu3::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 311 of file fmu3.cpp.

312  {
313  (void)m; // unused
314  fmi3Status status = get_float64_(instance, vr, n_vr, values, n_values);
315  return status != fmi3OK;
316 }

References get_float64_.

◆ get_stats()

void casadi::Fmu3::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_2bc

Implements casadi::FmuInternal.

Definition at line 431 of file fmu3.cpp.

432  {
433  Value& v = static_cast<Fmu3Memory*>(m)->aux_value;
434  get_aux_impl(m->instance, v);
435  // Collect auxilliary variables
436  Dict aux;
437  // Real
438  for (size_t k = 0; k < vn_aux_real_.size(); ++k) {
439  aux[vn_aux_real_[k]] = static_cast<double>(v.v_real[k]);
440  }
441  // Integer
442  for (size_t k = 0; k < vn_aux_integer_.size(); ++k) {
443  aux[vn_aux_integer_[k]] = static_cast<casadi_int>(v.v_integer[k]);
444  }
445  // Boolean
446  for (size_t k = 0; k < vn_aux_boolean_.size(); ++k) {
447  aux[vn_aux_boolean_[k]] = static_cast<bool>(v.v_boolean[k]);
448  }
449  // String
450  for (size_t k = 0; k < vn_aux_string_.size(); ++k) {
451  aux[vn_aux_string_[k]] = v.v_string[k];
452  }
453  // Copy to stats
454  (*stats)["aux"] = aux;
455  // Loop over input variables
456  for (size_t k = 0; k < name_in.size(); ++k) {
457  // Only consider regular inputs
458  if (in[k].type == InputType::REG) {
459  // Get the indices
460  const std::vector<size_t>& iind = ired_.at(in[k].ind);
461  // Collect values
462  std::vector<double> v(iind.size());
463  for (size_t i = 0; i < v.size(); ++i) v[i] = value_in_.at(iind[i]);
464  // Save to stats
465  (*stats)[name_in[k]] = v;
466  }
467  }
468 }
std::vector< double > value_in_
Definition: fmu_impl.hpp:366

References get_aux_impl(), casadi::FmuMemory::instance, casadi::FmuInternal::ired_, casadi::REG, casadi::Fmu3::Value::v_boolean, casadi::Fmu3::Value::v_integer, casadi::Fmu3::Value::v_real, casadi::Fmu3::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::Fmu3::init ( const DaeBuilderInternal *  dae)
overridevirtual

Reimplemented from casadi::FmuInternal.

Definition at line 65 of file fmu3.cpp.

65  {
66  // Initialize base classes
67  FmuInternal::init(dae);
68 
69  // Collect input and parameter values
70  vr_real_.clear();
71  vr_integer_.clear();
72  vr_boolean_.clear();
73  vr_string_.clear();
74  init_real_.clear();
75  init_integer_.clear();
76  init_boolean_.clear();
77  init_string_.clear();
78  for (size_t i = 0; i < dae->n_variables(); ++i) {
79  const Variable& v = dae->variable(i);
80  casadi_assert(v.numel == 1, "Vector variable support not implemented");
81  // Skip if the wrong type
82  if (v.causality != Causality::PARAMETER && v.causality != Causality::INPUT) continue;
83  // Variable has not been set - keep default value
84  if (!v.is_set()) continue;
85  // Value reference
86  fmi3ValueReference vr = v.value_reference;
87  // Get value
88  switch (to_fmi2(v.type)) {
89  case TypeFmi2::REAL:
90  init_real_.push_back(static_cast<fmi3Float64>(v.value.front()));
91  vr_real_.push_back(vr);
92  break;
93  case TypeFmi2::INTEGER:
94  case TypeFmi2::ENUM:
95  init_integer_.push_back(static_cast<fmi3Int32>(v.value.front()));
96  vr_integer_.push_back(vr);
97  break;
98  case TypeFmi2::BOOLEAN:
99  init_boolean_.push_back(static_cast<fmi3Boolean>(v.value.front()));
100  vr_boolean_.push_back(vr);
101  break;
102  case TypeFmi2::STRING:
103  init_string_.push_back(v.stringvalue);
104  vr_string_.push_back(vr);
105  break;
106  default:
107  casadi_warning("Ignoring " + v.name + ", type: " + to_string(v.type));
108  }
109  }
110 
111  // Collect auxilliary variables
112  vn_aux_real_.clear();
113  vn_aux_integer_.clear();
114  vn_aux_boolean_.clear();
115  vn_aux_string_.clear();
116  vr_aux_real_.clear();
117  vr_aux_integer_.clear();
118  vr_aux_boolean_.clear();
119  vr_aux_string_.clear();
120  for (auto&& s : aux_) {
121  const Variable& v = dae->variable(s);
122  // Convert to expected type
123  fmi3ValueReference vr = v.value_reference;
124  // Sort by type
125  switch (to_fmi2(v.type)) {
126  case TypeFmi2::REAL:
127  // Real
128  vn_aux_real_.push_back(v.name);
129  vr_aux_real_.push_back(vr);
130  break;
131  case TypeFmi2::INTEGER:
132  case TypeFmi2::ENUM:
133  // Integer or enum
134  vn_aux_integer_.push_back(v.name);
135  vr_aux_integer_.push_back(vr);
136  break;
137  case TypeFmi2::BOOLEAN:
138  // Boolean
139  vn_aux_boolean_.push_back(v.name);
140  vr_aux_boolean_.push_back(vr);
141  break;
142  case TypeFmi2::STRING:
143  // String
144  vn_aux_string_.push_back(v.name);
145  vr_aux_string_.push_back(vr);
146  break;
147  default:
148  casadi_warning("Ignoring " + v.name + ", type: " + to_string(v.type));
149  }
150  }
151 }
std::vector< std::string > aux_
Definition: fmu_impl.hpp:317
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::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::Fmu3::init_mem ( FmuMemory *  m) const
overridevirtual

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

Allocate numerical values for initial auxilliary variables

Reimplemented from casadi::FmuInternal.

Definition at line 39 of file fmu3.cpp.

39  {
40  if (FmuInternal::init_mem(mem)) return 1;
41  auto *m = static_cast<Fmu3Memory*>(mem);
43  m->aux_value.v_real.resize(vn_aux_real_.size());
44  m->aux_value.v_integer.resize(vn_aux_integer_.size());
45  m->aux_value.v_boolean.resize(vn_aux_boolean_.size());
46  m->aux_value.v_string.resize(vn_aux_string_.size());
47  return 0;
48 }
virtual int init_mem(FmuMemory *m) const
Initalize memory block.
Definition: fmu.cpp:1298

References casadi::FmuInternal::init_mem(), 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::Fmu3::instantiate ( ) const
overridevirtual

Implements casadi::FmuInternal.

Definition at line 202 of file fmu3.cpp.

202  {
203  // Instantiate FMU
204  fmi3String instanceName = instance_name_.c_str();
205  fmi3String instantiationToken = instantiation_token_.c_str();
206  fmi3String resourcePath = resource_loc_.c_str();
207  fmi3Boolean visible = fmi3False;
208  fmi3InstanceEnvironment instanceEnvironment = nullptr;
209  fmi3Instance c = instantiate_model_exchange_(instanceName, instantiationToken, resourcePath,
210  visible, logging_on_, instanceEnvironment, log_message_callback);
211  if (c == nullptr) casadi_error("fmi3Instantiate failed");
212  return c;
213 }
static void log_message_callback(fmi3InstanceEnvironment instanceEnvironment, fmi3Status status, fmi3String category, fmi3String message)
Definition: fmu3.cpp:196
std::string instantiation_token_
Definition: fmu_impl.hpp:329
std::string resource_loc_
Definition: fmu_impl.hpp:320
std::string instance_name_
Definition: fmu_impl.hpp:326

References casadi::FmuInternal::instance_name_, instantiate_model_exchange_, casadi::FmuInternal::instantiation_token_, log_message_callback(), casadi::FmuInternal::logging_on_, and casadi::FmuInternal::resource_loc_.

◆ 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::Fmu3::load_functions ( )
overridevirtual

Implements casadi::FmuInternal.

Definition at line 164 of file fmu3.cpp.

164  {
165  instantiate_model_exchange_ = load_function<fmi3InstantiateModelExchangeTYPE>(
166  "fmi3InstantiateModelExchange");
167  free_instance_ = load_function<fmi3FreeInstanceTYPE>("fmi3FreeInstance");
168  reset_ = load_function<fmi3ResetTYPE>("fmi3Reset");
169  enter_initialization_mode_ = load_function<fmi3EnterInitializationModeTYPE>(
170  "fmi3EnterInitializationMode");
171  exit_initialization_mode_ = load_function<fmi3ExitInitializationModeTYPE>(
172  "fmi3ExitInitializationMode");
173  enter_continuous_time_mode_ = load_function<fmi3EnterContinuousTimeModeTYPE>(
174  "fmi3EnterContinuousTimeMode");
175  set_time_ = load_function<fmi3SetTimeTYPE>("fmi3SetTime");
176  get_float64_ = load_function<fmi3GetFloat64TYPE>("fmi3GetFloat64");
177  set_float64_ = load_function<fmi3SetFloat64TYPE>("fmi3SetFloat64");
178  get_int32_ = load_function<fmi3GetInt32TYPE>("fmi3GetInt32");
179  set_int32_ = load_function<fmi3SetInt32TYPE>("fmi3SetInt32");
180  get_boolean_ = load_function<fmi3GetBooleanTYPE>("fmi3GetBoolean");
181  set_boolean_ = load_function<fmi3SetBooleanTYPE>("fmi3SetBoolean");
182  get_string_ = load_function<fmi3GetStringTYPE>("fmi3GetString");
183  set_string_ = load_function<fmi3SetStringTYPE>("fmi3SetString");
186  load_function<fmi3GetDirectionalDerivativeTYPE>("fmi3GetDirectionalDerivative");
187  }
190  load_function<fmi3GetAdjointDerivativeTYPE>("fmi3GetAdjointDerivative");
191  }
193  load_function<fmi3UpdateDiscreteStatesTYPE>("fmi3UpdateDiscreteStates");
194 }
fmi3GetStringTYPE * get_string_
Definition: fmu3.hpp:108
fmi3SetInt32TYPE * set_int32_
Definition: fmu3.hpp:107
bool provides_adjoint_derivatives_
Definition: fmu_impl.hpp:338
bool provides_directional_derivatives_
Definition: fmu_impl.hpp:338

References enter_continuous_time_mode_, enter_initialization_mode_, exit_initialization_mode_, free_instance_, get_adjoint_derivative_, get_boolean_, get_directional_derivative_, get_float64_, get_int32_, get_string_, instantiate_model_exchange_, casadi::FmuInternal::provides_adjoint_derivatives_, casadi::FmuInternal::provides_directional_derivatives_, reset_, set_boolean_, set_float64_, set_int32_, set_string_, set_time_, and update_discrete_states_.

◆ log_message_callback()

void casadi::Fmu3::log_message_callback ( fmi3InstanceEnvironment  instanceEnvironment,
fmi3Status  status,
fmi3String  category,
fmi3String  message 
)
static

Definition at line 196 of file fmu3.cpp.

197  {
198  // Print message content
199  uout() << "[" << category << "] " << message << std::endl;
200 }
std::ostream & uout()

References casadi::uout().

Referenced by instantiate().

◆ 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::Fmu3::reset ( void *  instance)

Definition at line 224 of file fmu3.cpp.

224  {
225  auto *c = static_cast<fmi3Instance>(instance);
226  fmi3Status status = reset_(c);
227  if (status != fmi3OK) {
228  casadi_warning("fmi3Reset failed");
229  return 1;
230  }
231  return 0;
232 }

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::Fmu3::serialize_body ( SerializingStream &  s) const
overridevirtual

Reimplemented from casadi::FmuInternal.

Definition at line 534 of file fmu3.cpp.

534  {
536 
537  s.version("Fmu3", 1);
538  s.pack("Fmu3::vr_real", vr_real_);
539  s.pack("Fmu3::vr_integer", vr_integer_);
540  s.pack("Fmu3::vr_boolean", vr_boolean_);
541  s.pack("Fmu3::vr_string", vr_string_);
542  s.pack("Fmu3::init_real", init_real_);
543  s.pack("Fmu3::init_integer", init_integer_);
544  s.pack("Fmu3::init_boolean", init_boolean_);
545  s.pack("Fmu3::init_string", init_string_);
546 
547  s.pack("Fmu3::vn_aux_real", vn_aux_real_);
548  s.pack("Fmu3::vn_aux_integer", vn_aux_integer_);
549  s.pack("Fmu3::vn_aux_boolean", vn_aux_boolean_);
550  s.pack("Fmu3::vn_aux_string", vn_aux_string_);
551  s.pack("Fmu3::vr_aux_real", vr_aux_real_);
552  s.pack("Fmu3::vr_aux_integer", vr_aux_integer_);
553  s.pack("Fmu3::vr_aux_boolean", vr_aux_boolean_);
554  s.pack("Fmu3::vr_aux_string", vr_aux_string_);
555 }

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::Fmu3::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 293 of file fmu3.cpp.

294  {
295  // Set time variable, if any
296  if (has_independent_ && n_vr > 0 && *vr == vr_in_[0]) {
297  // Update FMU time
298  fmi3Status status = set_time_(instance, *values);
299  if (status != fmi3OK) return 1;
300  // Skip when setting remaining variables
301  vr++;
302  n_vr--;
303  values++;
304  n_values--;
305  }
306  // Set remaining variables
307  fmi3Status status = set_float64_(instance, vr, n_vr, values, n_values);
308  return status != fmi3OK;
309 }

References casadi::FmuInternal::has_independent_, set_float64_, set_time_, and casadi::FmuInternal::vr_in_.

◆ set_time()

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

Implements casadi::FmuInternal.

Definition at line 288 of file fmu3.cpp.

288  {
289  fmi3Status status = set_time_(instance, t);
290  return status != fmi3OK;
291 }

References set_time_.

◆ set_values()

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

Implements casadi::FmuInternal.

Definition at line 334 of file fmu3.cpp.

334  {
335  auto *c = static_cast<fmi3Instance>(instance);
336  // Pass real values before initialization
337  casadi_assert(vr_real_.size() == init_real_.size(), "Vector valued variables not supported");
338  for (size_t k = 0; k < vr_real_.size(); ++k) {
339  fmi3Status status = set_float64_(c, &vr_real_[k], 1, &init_real_[k], 1);
340  if (status != fmi3OK) {
341  casadi_warning("fmi3SetFloat64 failed for value reference " + str(vr_real_[k]));
342  return 1;
343  }
344  }
345 
346  // Pass integer values before initialization (also enums)
347  if (!vr_integer_.empty()) {
348  fmi3Status status = set_int32_(c, get_ptr(vr_integer_), vr_integer_.size(),
350  if (status != fmi3OK) {
351  casadi_warning("fmi3SetInt32 failed");
352  return 1;
353  }
354  }
355  // Pass boolean values before initialization
356  if (!vr_boolean_.empty()) {
357  casadi_error("Broken");
358  // nullptr -> get_ptr(init_boolean_)
359  fmi3Status status = set_boolean_(c, get_ptr(vr_boolean_), vr_boolean_.size(),
360  nullptr, init_boolean_.size());
361  if (status != fmi3OK) {
362  casadi_warning("fmi3SetBoolean failed");
363  return 1;
364  }
365  }
366  // Pass string values before initialization
367  for (size_t k = 0; k < vr_string_.size(); ++k) {
368  fmi3ValueReference vr = vr_string_[k];
369  fmi3String value = init_string_[k].c_str();
370  fmi3Status status = set_string_(c, &vr, 1, &value, 1);
371  if (status != fmi3OK) {
372  casadi_error("fmi3SetString failed for value reference " + str(vr));
373  }
374  }
375  // Successful return
376  return 0;
377 }

References casadi::get_ptr(), init_boolean_, init_integer_, init_real_, init_string_, set_boolean_, set_float64_, set_int32_, 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::Fmu3::system_infix ( ) const
inlineoverridevirtual

Implements casadi::FmuInternal.

Definition at line 126 of file fmu3.hpp.

126 { return dll_infix(); }
static std::string dll_infix()
Definition: fmu3.cpp:61

◆ update_discrete_states()

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

Implements casadi::FmuInternal.

Definition at line 264 of file fmu3.cpp.

264  {
265  auto *c = static_cast<fmi3Instance>(instance);
266  // Return arguments in FMI types
267  fmi3Boolean discreteStatesNeedUpdate, terminateSimulation, nominalsOfContinuousStatesChanged,
268  valuesOfContinuousStatesChanged, nextEventTimeDefined;
269  fmi3Float64 nextEventTime;
270  // Call FMU
271  fmi3Status status = update_discrete_states_(c, &discreteStatesNeedUpdate, &terminateSimulation,
272  &nominalsOfContinuousStatesChanged, &valuesOfContinuousStatesChanged, &nextEventTimeDefined,
273  &nextEventTime);
274  // Pass to event iteration memory
275  eventmem->discrete_states_need_update = discreteStatesNeedUpdate;
276  eventmem->terminate_simulation = terminateSimulation;
277  eventmem->nominals_of_continuous_states_changed = nominalsOfContinuousStatesChanged;
278  eventmem->values_of_continuous_states_changed = valuesOfContinuousStatesChanged;
279  eventmem->next_event_time_defined = nextEventTimeDefined;
280  eventmem->next_event_time = nextEventTime;
281  return status != fmi3OK;
282 }

References casadi::EventMemory::discrete_states_need_update, casadi::EventMemory::next_event_time, casadi::EventMemory::next_event_time_defined, casadi::EventMemory::nominals_of_continuous_states_changed, casadi::EventMemory::terminate_simulation, update_discrete_states_, 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::Fmu3::aux_value_

Definition at line 122 of file fmu3.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_

fmi3EnterContinuousTimeModeTYPE* casadi::Fmu3::enter_continuous_time_mode_

Definition at line 100 of file fmu3.hpp.

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

◆ enter_initialization_mode_

fmi3EnterInitializationModeTYPE* casadi::Fmu3::enter_initialization_mode_

Definition at line 98 of file fmu3.hpp.

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

◆ exit_initialization_mode_

fmi3ExitInitializationModeTYPE* casadi::Fmu3::exit_initialization_mode_

Definition at line 99 of file fmu3.hpp.

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

◆ fmutol_

double casadi::FmuInternal::fmutol_
protectedinherited

◆ free_instance_

fmi3FreeInstanceTYPE* casadi::Fmu3::free_instance_

Definition at line 96 of file fmu3.hpp.

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

◆ get_adjoint_derivative_

fmi3GetAdjointDerivativeTYPE* casadi::Fmu3::get_adjoint_derivative_

Definition at line 111 of file fmu3.hpp.

Referenced by Fmu3(), get_adjoint_derivative(), and load_functions().

◆ get_boolean_

fmi3GetBooleanTYPE* casadi::Fmu3::get_boolean_

Definition at line 104 of file fmu3.hpp.

Referenced by get_aux_impl(), and load_functions().

◆ get_directional_derivative_

fmi3GetDirectionalDerivativeTYPE* casadi::Fmu3::get_directional_derivative_

Definition at line 110 of file fmu3.hpp.

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

◆ get_float64_

fmi3GetFloat64TYPE* casadi::Fmu3::get_float64_

Definition at line 102 of file fmu3.hpp.

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

◆ get_int32_

fmi3GetInt32TYPE* casadi::Fmu3::get_int32_

Definition at line 106 of file fmu3.hpp.

Referenced by get_aux_impl(), and load_functions().

◆ get_string_

fmi3GetStringTYPE* casadi::Fmu3::get_string_

Definition at line 108 of file fmu3.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<fmi3Boolean> casadi::Fmu3::init_boolean_

Definition at line 85 of file fmu3.hpp.

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

◆ init_integer_

std::vector<fmi3Int32> casadi::Fmu3::init_integer_

Definition at line 84 of file fmu3.hpp.

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

◆ init_real_

std::vector<fmi3Float64> casadi::Fmu3::init_real_

Definition at line 83 of file fmu3.hpp.

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

◆ init_string_

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

Definition at line 86 of file fmu3.hpp.

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

◆ instance_name_

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

◆ instantiate_model_exchange_

fmi3InstantiateModelExchangeTYPE* casadi::Fmu3::instantiate_model_exchange_

Definition at line 95 of file fmu3.hpp.

Referenced by Fmu3(), 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

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

fmi3ResetTYPE* casadi::Fmu3::reset_

Definition at line 97 of file fmu3.hpp.

Referenced by Fmu3(), 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_

fmi3SetBooleanTYPE* casadi::Fmu3::set_boolean_

Definition at line 105 of file fmu3.hpp.

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

◆ set_float64_

fmi3SetFloat64TYPE* casadi::Fmu3::set_float64_

Definition at line 103 of file fmu3.hpp.

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

◆ set_int32_

fmi3SetInt32TYPE* casadi::Fmu3::set_int32_

Definition at line 107 of file fmu3.hpp.

Referenced by load_functions(), and set_values().

◆ set_string_

fmi3SetStringTYPE* casadi::Fmu3::set_string_

Definition at line 109 of file fmu3.hpp.

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

◆ set_time_

fmi3SetTimeTYPE* casadi::Fmu3::set_time_

Definition at line 101 of file fmu3.hpp.

Referenced by Fmu3(), load_functions(), set_real(), and set_time().

◆ start_time_

double casadi::FmuInternal::start_time_
protectedinherited

◆ update_discrete_states_

fmi3UpdateDiscreteStatesTYPE* casadi::Fmu3::update_discrete_states_

Definition at line 112 of file fmu3.hpp.

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

◆ value_in_

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

◆ vn_aux_boolean_

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

Definition at line 89 of file fmu3.hpp.

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

◆ vn_aux_integer_

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

Definition at line 89 of file fmu3.hpp.

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

◆ vn_aux_real_

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

Definition at line 89 of file fmu3.hpp.

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

◆ vn_aux_string_

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

Definition at line 89 of file fmu3.hpp.

Referenced by finalize(), Fmu3(), 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<fmi3ValueReference> casadi::Fmu3::vr_aux_boolean_

Definition at line 90 of file fmu3.hpp.

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

◆ vr_aux_integer_

std::vector<fmi3ValueReference> casadi::Fmu3::vr_aux_integer_

Definition at line 90 of file fmu3.hpp.

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

◆ vr_aux_real_

std::vector<fmi3ValueReference> casadi::Fmu3::vr_aux_real_

Definition at line 90 of file fmu3.hpp.

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

◆ vr_aux_string_

std::vector<fmi3ValueReference> casadi::Fmu3::vr_aux_string_

Definition at line 90 of file fmu3.hpp.

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

◆ vr_boolean_

std::vector<fmi3ValueReference> casadi::Fmu3::vr_boolean_

Definition at line 82 of file fmu3.hpp.

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

◆ vr_in_

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

◆ vr_integer_

std::vector<fmi3ValueReference> casadi::Fmu3::vr_integer_

Definition at line 82 of file fmu3.hpp.

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

◆ vr_out_

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

◆ vr_real_

std::vector<fmi3ValueReference> casadi::Fmu3::vr_real_

Definition at line 82 of file fmu3.hpp.

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

◆ vr_string_

std::vector<fmi3ValueReference> casadi::Fmu3::vr_string_

Definition at line 82 of file fmu3.hpp.

Referenced by Fmu3(), 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: