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...
 
FmuMemoryalloc_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 Fmu3deserialize (DeserializingStream &s)
 
static std::string dll_suffix ()
 
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 ()
 
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 511 of file fmu3.cpp.

516  : FmuInternal(name, scheme_in, scheme_out, scheme, aux) {
517  instantiate_model_exchange_ = nullptr;
518  free_instance_ = nullptr;
519  reset_ = nullptr;
520  enter_initialization_mode_ = nullptr;
521  exit_initialization_mode_ = nullptr;
522  enter_continuous_time_mode_ = nullptr;
523  set_time_ = nullptr;
524  set_float64_ = nullptr;
525  set_boolean_ = nullptr;
526  get_float64_ = nullptr;
527  get_directional_derivative_ = nullptr;
528  get_adjoint_derivative_ = nullptr;
529  update_discrete_states_ = nullptr;
530 }
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 539 of file fmu3.cpp.

539  : FmuInternal(s) {
540  instantiate_model_exchange_ = nullptr;
541  free_instance_ = nullptr;
542  reset_ = nullptr;
543  enter_initialization_mode_ = nullptr;
544  exit_initialization_mode_ = nullptr;
545  enter_continuous_time_mode_ = nullptr;
546  set_time_ = nullptr;
547  set_float64_ = nullptr;
548  set_boolean_ = nullptr;
549  get_float64_ = nullptr;
550  get_directional_derivative_ = nullptr;
551  get_adjoint_derivative_ = nullptr;
552  update_discrete_states_ = nullptr;
553 
554  s.version("Fmu3", 1);
555  s.unpack("Fmu3::vr_real", vr_real_);
556  s.unpack("Fmu3::vr_integer", vr_integer_);
557  s.unpack("Fmu3::vr_boolean", vr_boolean_);
558  s.unpack("Fmu3::vr_string", vr_string_);
559  s.unpack("Fmu3::init_real", init_real_);
560  s.unpack("Fmu3::init_integer", init_integer_);
561  s.unpack("Fmu3::init_boolean", init_boolean_);
562  s.unpack("Fmu3::init_string", init_string_);
563 
564  s.unpack("Fmu3::vn_aux_real", vn_aux_real_);
565  s.unpack("Fmu3::vn_aux_integer", vn_aux_integer_);
566  s.unpack("Fmu3::vn_aux_boolean", vn_aux_boolean_);
567  s.unpack("Fmu3::vn_aux_string", vn_aux_string_);
568  s.unpack("Fmu3::vr_aux_real", vr_aux_real_);
569  s.unpack("Fmu3::vr_aux_integer", vr_aux_integer_);
570  s.unpack("Fmu3::vr_aux_boolean", vr_aux_boolean_);
571  s.unpack("Fmu3::vr_aux_string", vr_aux_string_);
572 }
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:364
std::vector< double > nominal_in_
Definition: fmu_impl.hpp:354

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:364
std::vector< double > nominal_out_
Definition: fmu_impl.hpp:354

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 736 of file fmu.cpp.

736  {
737  // Options
738  Dict compiler_opts;
739  std::string compiler_plugin = "shell";
740  std::vector<std::string> include_dirs;
741  for (auto&& op : opts) {
742  if (op.first == "compiler") {
743  compiler_plugin = op.second.to_string();
744  } else if (op.first == "compiler_options") {
745  compiler_opts = op.second;
746  } else if (op.first == "include_dirs") {
747  include_dirs = op.second.to_string_vector();
748  } else {
749  casadi_error("No such option: " + op.first);
750  }
751  }
752  // The C sources to compile are the .c entries of the export_fmu map (flat in CWD)
753  std::vector<std::string> csources;
754  for (auto&& kv : files) {
755  const std::string& local = kv.first;
756  if (local.size() > 2 && local.substr(local.size() - 2) == ".c") csources.push_back(local);
757  }
758  // Amalgamation translation unit: a single .c that #includes every generated source,
759  // because the shell Importer compiles exactly one source file. The compile runs in
760  // the working directory, so quoted includes resolve against the loose files there.
761  // This is a build scratch file, NOT FMU content, so it is removed below.
762  std::string amalg = name + "_all.c";
763  {
764  auto f = Filesystem::ofstream_ptr(amalg);
765  for (const std::string& c : csources) *f << "#include \"" << c << "\"\n";
766  }
767  // Compile the amalgamation into a shared library via the Importer plugin.
768  // Pass include dirs through the OS-agnostic 'include_dirs' option (the plugin
769  // prefixes each with the compiler's own include flag: -I on gcc/clang, /I on MSVC).
770  Dict cop = compiler_opts;
771  if (!include_dirs.empty()) cop["include_dirs"] = include_dirs;
772  Importer compiler(amalg, compiler_plugin, cop);
773  // Lift the binary to a stable path before the Importer destructor removes it, then
774  // discard the amalgamation scratch file (compilation is done once the Importer exists)
775  std::string dll = name + dll_suffix();
776  Filesystem::copy_file(compiler.library(), dll);
777  Filesystem::remove(amalg);
778  // Augment the file map with the compiled binary
779  // (FMI-3 layout: binaries/<dll_infix>/<modelIdentifier><suffix>)
780  Dict ret = files;
781  ret[dll] = "binaries/" + Fmu3::dll_infix() + "/" + name + dll_suffix();
782  return ret;
783 }
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 dll_infix()
Definition: fmu3.cpp:61
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(), dll_infix(), casadi::FmuInternal::dll_suffix(), 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.

175  {
176  // Note: i != this because of something something multiple inheritance
177  return str( (casadi_int)(i)) + "/" + static_cast<const Internal*>(this)->class_name();
178  }
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 805 of file fmu.cpp.

805  {
806  // Create description
807  if (more) {
808  // Detailed description
809  std::stringstream ss;
810  ss << vn_in_[id] << " = " << m->ibuf_[id] << " (nominal " << nominal_in_[id]
811  << ", min " << min_in_[id] << ", max " << max_in_[id] << ")";
812  return ss.str();
813  } else {
814  return vn_in_[id];
815  }
816 }
std::vector< double > max_in_
Definition: fmu_impl.hpp:356
std::vector< std::string > vn_in_
Definition: fmu_impl.hpp:357
std::vector< double > min_in_
Definition: fmu_impl.hpp:355

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 533 of file fmu3.cpp.

533  {
534  Fmu3* ret = new Fmu3(s);
535  ret->finalize();
536  return ret;
537 }
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:511

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 1197 of file fmu.cpp.

1197  {
1198  // Helper function: update_discrete_states
1199  EventMemory eventmem;
1200  const size_t max_update_iter = 10;
1201  for (size_t update_iter = 0; update_iter < max_update_iter; ++update_iter) {
1202  if (update_discrete_states(instance, &eventmem)) {
1203  casadi_warning("update_discrete_states");
1204  return 1;
1205  }
1206  // Not implemented
1207  if (eventmem.discrete_states_need_update) {
1210  casadi_warning("Discrete state update not implemented");
1211  }
1212  }
1213  if (eventmem.terminate_simulation) {
1216  casadi_warning("Terminate simulation not implemented");
1217  }
1218  }
1219  if (eventmem.nominals_of_continuous_states_changed) {
1222  casadi_warning("Nominals of continuous states not implemented");
1223  }
1224  }
1225  if (eventmem.values_of_continuous_states_changed) {
1228  casadi_warning("Values of continuous states not implemented");
1229  }
1230  }
1231  if (eventmem.next_event_time_defined) {
1234  casadi_warning("Next event time not implemented");
1235  }
1236  }
1237  // Successful return
1238  if (!eventmem.discrete_states_need_update) {
1239  return 0;
1240  }
1241  }
1242  // Too many iterations
1243  casadi_warning("Discrete state update failed");
1244  return 1;
1245 }
bool warning_fired_values_of_continuous_states_changed_
Definition: fmu_impl.hpp:372
bool warning_fired_next_event_time_defined_
Definition: fmu_impl.hpp:373
bool warning_fired_nominals_of_continuous_states_changed_
Definition: fmu_impl.hpp:371
bool warning_fired_terminate_simulation_
Definition: fmu_impl.hpp:370
bool warning_fired_discrete_states_need_update_
Definition: fmu_impl.hpp:369
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:305
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  {
62  // Architecture
63  std::string arch;
64 #if defined(__arm64__) || defined(__aarch64__)
65  // ARM 64-bit Architecture
66  arch = "aarch64";
67 #elif __arm__
68  if (sizeof(void*) == 4) {
69  // ARM 32-bit Architecture
70  arch = "aarch32";
71  } else {
72  // ARM 64-bit Architecture
73  arch = "aarch64";
74  }
75 #elif defined(__ppc64__) || defined(__powerpc64__)
76  // PowerPC 64-bit Architecture
77  arch = "ppc64";
78 #elif defined(__ppc__) || defined(__powerpc__)
79  // PowerPC 32-bit Architecture
80  arch = "ppc";
81 #else
82  if (sizeof(void*) == 4) {
83  // Intel/AMD x86 32-bit
84  arch = "x86";
85  } else {
86  // Intel/AMD x86 64-bit
87  arch = "x86_64";
88  }
89 #endif
90  // Operating system
91  std::string sys;
92 #if defined(_WIN32)
93  // Microsoft Windows
94  sys = "windows";
95 #elif defined(__APPLE__)
96  // Darwin (macOS, iOS, watchOS, tvOS, audioOS)
97  sys = "darwin";
98 #else
99  // Linux
100  sys = "linux";
101 #endif
102  // Return platform tuple, according to Section 2.5.1.4.1. of the FMI 3 specification
103  return arch + "-" + sys;
104 }

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

◆ 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 295 of file fmu3.cpp.

295  {
296  auto *c = static_cast<fmi3Instance>(instance);
297  fmi3Status status = enter_continuous_time_mode_(c);
298  if (status != fmi3OK) {
299  casadi_warning("fmi3EnterContinuousTimeMode failed");
300  return 1;
301  }
302  return 0;
303 }

References enter_continuous_time_mode_.

◆ enter_initialization_mode()

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

Implements casadi::FmuInternal.

Definition at line 275 of file fmu3.cpp.

275  {
276  auto *c = static_cast<fmi3Instance>(instance);
277  fmi3Status status = enter_initialization_mode_(c, fmutol_ > 0, fmutol_, 0., fmi3True, 1.);
278  if (status != fmi3OK) {
279  casadi_warning("fmi3EnterInitializationMode failed: " + str(status));
280  return 1;
281  }
282  return 0;
283 }

References enter_initialization_mode_, casadi::FmuInternal::fmutol_, and casadi::str().

◆ eval()

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

Definition at line 1361 of file fmu.cpp.

1361  {
1362  // Gather inputs and outputs
1363  gather_io(m);
1364  // Pass inputs to FMU
1365  if (set_all(m, get_ptr(m->v_in_), m->v_in_.size())) return 1;
1366  // Get outputs from FMU
1367  if (get_all(m, get_ptr(m->v_out_), m->v_out_.size())) return 1;
1368  // Collect requested variables
1369  auto it = m->v_out_.begin();
1370  for (size_t id : m->id_out_) {
1371  m->obuf_[id] = *it++;
1372  }
1373  // Successful return
1374  return 0;
1375 }
int get_all(FmuMemory *m, double *values, size_t n_values) const
Definition: fmu.cpp:1349
void gather_io(FmuMemory *m) const
Definition: fmu.cpp:1424
int set_all(FmuMemory *m, const double *values, size_t n_values) const
Definition: fmu.cpp:1321
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 863 of file fmu.cpp.

863  {
864  // Number of inputs and outputs
865  size_t n_known = m->id_in_.size();
866  size_t n_unknown = m->id_out_.size();
867  // Quick return if nothing to be calculated
868  if (n_unknown == 0) return 0;
869  // Quick return if no seeds
870  if (n_known == 0) {
871  std::fill(m->d_out_.begin(), m->d_out_.end(), 0);
872  return 0;
873  }
874  // Evalute (should not be necessary)
875  if (get_real(m->instance, get_ptr(m->vr_out_), n_unknown, get_ptr(m->v_out_), n_unknown, m)) {
876  casadi_warning("FMU evaluation failed");
877  return 1;
878  }
879  // Evaluate directional derivatives
880  if (get_directional_derivative(m->instance,
881  get_ptr(m->vr_out_), n_unknown,
882  get_ptr(m->vr_in_), n_known,
883  get_ptr(m->d_in_), n_known,
884  get_ptr(m->d_out_), n_unknown)) {
885  casadi_warning("FMU directional derivative failed");
886  return 1;
887  }
888  // Collect requested variables
889  auto it = m->d_out_.begin();
890  for (size_t id : m->id_out_) {
891  m->osens_[id] = *it++;
892  }
893  // Successful return
894  return 0;
895 }
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::osens_, 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 834 of file fmu.cpp.

834  {
835  // Gather input and output indices
836  gather_adj(m);
837  // Quick return if nothing to be calculated
838  if (m->id_in_.size() == 0) return 0;
839  // Check if no seeds
840  if (m->id_out_.size() == 0) {
841  // Sensitivities are zero, trivially
842  std::fill(m->d_in_.begin(), m->d_in_.end(), 0);
843  } else {
844  // Evaluate adjoint derivatives
845  if (get_adjoint_derivative(m->instance,
846  get_ptr(m->vr_out_), m->id_out_.size(),
847  get_ptr(m->vr_in_), m->id_in_.size(),
848  get_ptr(m->d_out_), m->id_out_.size(),
849  get_ptr(m->d_in_), m->id_in_.size())) {
850  casadi_warning("FMU adjoint derivative failed");
851  return 1;
852  }
853  }
854  // Collect requested variables
855  auto it = m->d_in_.begin();
856  for (size_t id : m->id_in_) {
857  m->isens_[id] = *it++;
858  }
859  // Successful return
860  return 0;
861 }
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:1467

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::vr_in_, and casadi::FmuMemory::vr_out_.

◆ eval_fd()

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

Definition at line 897 of file fmu.cpp.

897  {
898  // Number of inputs and outputs
899  size_t n_known = m->id_in_.size();
900  size_t n_unknown = m->id_out_.size();
901  // Quick return if nothing to be calculated
902  if (n_unknown == 0) return 0;
903  // Quick return if no seeds
904  if (n_known == 0) {
905  std::fill(m->d_out_.begin(), m->d_out_.end(), 0);
906  return 0;
907  }
908  // Evalute (should not be necessary)
909  if (get_real(m->instance, get_ptr(m->vr_out_), n_unknown, get_ptr(m->v_out_), n_unknown, m)) {
910  casadi_warning("Evaluating FMU failed");
911  return 1;
912  }
913  // Make outputs dimensionless
914  for (size_t k = 0; k < n_unknown; ++k) m->v_out_[k] /= nominal_out_[m->id_out_[k]];
915  // Number of points in FD stencil
916  casadi_int n_points = n_fd_points(m->self.fd_);
917  // Offset for points
918  casadi_int offset = fd_offset(m->self.fd_);
919  // Memory for perturbed outputs
920  m->fd_out_.resize(n_points * n_unknown);
921  // Which inputs are in bounds
922  m->in_bounds_.resize(n_known);
923  // Memory for perturbed inputs
924  m->v_pert_.resize(n_known);
925  // Do any any inputs need flipping?
926  m->flip_.resize(n_known);
927  size_t first_flip = -1;
928  for (size_t i = 0; i < n_known; ++i) {
929  // Try to take step
930  double test = m->v_in_[i] + m->self.step_ * m->d_in_[i];
931  // Check if in bounds
932  size_t id = m->id_in_[i];
933  if (test >= min_in_[id] && test <= max_in_[id]) {
934  // Positive perturbation is fine
935  m->flip_[i] = false;
936  } else {
937  // Try negative direction instead
938  test = m->v_in_[i] - m->self.step_ * m->d_in_[i];
939  casadi_assert(test >= min_in_[id] && test <= max_in_[id],
940  "Cannot perturb " + vn_in_[id] + " at " + str(m->v_in_[i]) + ", min " + str(min_in_[id])
941  + ", max " + str(max_in_[id]) + ", nominal " + str(nominal_in_[id]));
942  m->flip_[i] = true;
943  if (first_flip == size_t(-1)) first_flip = i;
944  }
945  }
946  // If seeds are not independent, we have to flip the sign for all of the seeds or none
947  if (first_flip != size_t(-1) && !independent_seeds) {
948  // Flip the rest of the seeds
949  for (size_t i = 0; i < n_known; ++i) {
950  if (!m->flip_[i]) {
951  // Test negative direction
952  double test = m->v_in_[i] - m->self.step_ * m->d_in_[i];
953  size_t id = m->id_in_[i];
954  casadi_assert(test >= min_in_[id] && test <= max_in_[id],
955  "Cannot perturb both " + vn_in_[id] + " and " + vn_in_[first_flip]);
956  // Flip it too
957  m->flip_[i] = true;
958  }
959  }
960  }
961  // All perturbed outputs
962  const double* yk_all[5] = {nullptr};
963 
964  // Calculate all perturbed outputs
965  for (casadi_int k = 0; k < n_points; ++k) {
966  // Where to save the perturbed outputs
967  double* yk = &m->fd_out_[n_unknown * k];
968  casadi_assert_dev(k < 5);
969  yk_all[k] = yk;
970  // If unperturbed output, quick return
971  if (k == offset) {
972  casadi_copy(get_ptr(m->v_out_), n_unknown, yk);
973  continue;
974  }
975  // Perturbation size
976  double pert = (k - offset) * m->self.step_;
977  // Perturb inputs, if allowed
978  for (size_t i = 0; i < n_known; ++i) {
979  // Try to take step
980  double sign = m->flip_[i] ? -1 : 1;
981  double test = m->v_in_[i] + pert * sign * m->d_in_[i];
982  // Check if in bounds
983  size_t id = m->id_in_[i];
984  m->in_bounds_[i] = test >= min_in_[id] && test <= max_in_[id];
985  // Take step, if allowed
986  m->v_pert_[i] = m->in_bounds_[i] ? test : m->v_in_[i];
987  }
988  // Pass perturbed inputs to FMU
989  if (set_all(m, get_ptr(m->v_pert_), m->v_pert_.size())) return 1;
990  // Evaluate perturbed FMU
991  if (get_all(m, yk, n_unknown)) return 1;
992  // Post-process yk if there was any scaling
993  if (independent_seeds) {
994  for (size_t i = 0; i < n_unknown; ++i) {
995  // Variable id
996  size_t id = m->id_out_[i];
997  // Differentiation with respect to what variable
998  size_t wrt_id = m->wrt_.at(id);
999  // Find the corresponding input variable
1000  size_t wrt_i;
1001  for (wrt_i = 0; wrt_i < n_known; ++wrt_i) {
1002  if (m->id_in_[wrt_i] == wrt_id) break;
1003  }
1004  // Check if in bounds
1005  if (m->in_bounds_.at(wrt_i) && (m->self.fd_flip_ || !m->flip_[i])) {
1006  // Input was in bounds: Keep output, make dimensionless
1007  yk[i] /= nominal_out_[m->id_out_[i]];
1008  } else {
1009  // Input was out of bounds: Discard output
1010  yk[i] = nan;
1011  }
1012  }
1013  }
1014  }
1015  // Restore FMU inputs
1016  if (set_all(m, get_ptr(m->v_in_), m->v_in_.size())) return 1;
1017  // Step size
1018  double h = m->self.step_;
1019 
1020  // Calculate FD approximation
1021  finite_diff(m->self.fd_, yk_all, get_ptr(m->d_out_), h, n_unknown, eps);
1022 
1023  // If seeds are dependent, quick return
1024  if (!independent_seeds) {
1025  // Simpy copy the results to output (no validation)
1026  for (size_t ind = 0; ind < m->id_out_.size(); ++ind) {
1027  m->osens_[m->id_out_[ind]] = m->d_out_[ind];
1028  }
1029  return 0;
1030  }
1031 
1032  // Collect requested variables
1033  for (size_t ind = 0; ind < m->id_out_.size(); ++ind) {
1034  // Variable id
1035  size_t id = m->id_out_[ind];
1036  // With respect to what variable
1037  size_t wrt = m->wrt_[id];
1038  // Find the corresponding input variable
1039  size_t wrt_i;
1040  for (wrt_i = 0; wrt_i < n_known; ++wrt_i) {
1041  if (m->id_in_[wrt_i] == wrt) break;
1042  }
1043  // Nominal value
1044  double n = nominal_out_[id];
1045  // Get the value
1046  double d_fd = m->d_out_[ind] * n;
1047  // Correct sign, if necessary
1048  if (m->flip_[wrt_i]) d_fd = -d_fd;
1049  // Use FD instead of AD or to compare with AD
1050  if (m->self.validate_forward_) {
1051  // Value to compare with
1052  double d_ad = m->osens_[id];
1053  // Nominal value used as seed
1054  d_ad /= nominal_in_[wrt];
1055  d_fd /= nominal_in_[wrt];
1056  // Is it a not a number?
1057  bool d_is_nan = d_ad != d_ad;
1058  // Magnitude of derivatives
1059  double d_max = std::fmax(std::fabs(d_fd), std::fabs(d_ad));
1060  // Check if NaN or error exceeds thresholds
1061  if (d_is_nan || (d_max > n * m->self.abstol_
1062  && std::fabs(d_ad - d_fd) > d_max * m->self.reltol_)) {
1063  // Offset for printing the stencil
1064  double off = m->fd_out_.at(ind + offset * n_unknown);
1065  // Warning or add to file
1066  std::stringstream ss;
1067  if (m->self.validate_ad_file_.empty()) {
1068  // Issue warning
1069  ss << (d_is_nan ? "NaN" : "Inconsistent") << " derivatives of " << vn_out_[id]
1070  << " w.r.t. " << desc_in(m, wrt) << ", got " << d_ad
1071  << " for AD vs. " << d_fd << " for FD[" << to_string(m->self.fd_) << "].";
1072  // Print the stencil:
1073  ss << "\nValues for step size " << h << ": " << (n * off) << " + [";
1074  for (casadi_int k = 0; k < n_points; ++k) {
1075  if (k > 0) ss << ", ";
1076  ss << (n * (m->fd_out_.at(ind + k * n_unknown) - off));
1077  }
1078  ss << "]";
1079  // Issue warning
1080  casadi_warning(ss.str());
1081  } else {
1082  // Output
1083  ss << vn_out_[id] << " ";
1084  // Input
1085  ss << vn_in_[wrt] << " ";
1086  // Value
1087  ss << m->ibuf_[wrt] << " ";
1088  // Noninal
1089  ss << nominal_in_[wrt] << " ";
1090  // Min
1091  ss << min_in_[wrt] << " ";
1092  // Max
1093  ss << max_in_[wrt] << " ";
1094  // AD
1095  ss << d_ad << " ";
1096  // FD
1097  ss << d_fd << " ";
1098  // Step
1099  ss << h << " ";
1100  // Offset
1101  ss << off << " ";
1102  // Stencil
1103  ss << "[";
1104  for (casadi_int k = 0; k < n_points; ++k) {
1105  if (k > 0) ss << ",";
1106  ss << (n * (m->fd_out_.at(ind + k * n_unknown) - off));
1107  }
1108  ss << "]" << std::endl;
1109  // Append to file
1110  auto valfile_ptr = Filesystem::ofstream_ptr(m->self.validate_ad_file_,
1111  std::ios_base::app);
1112  std::ostream& valfile = *valfile_ptr;
1113  valfile << ss.str();
1114  }
1115  }
1116  } else {
1117  // Use instead of AD
1118  m->osens_[id] = d_fd;
1119  }
1120  }
1121  // Successful return
1122  return 0;
1123 }
std::string desc_in(FmuMemory *m, size_t id, bool more=true) const
Definition: fmu.cpp:805
std::vector< std::string > vn_out_
Definition: fmu_impl.hpp:357
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 818 of file fmu.cpp.

818  {
819  // Gather input and output indices
820  gather_fwd(m);
821  // Calculate derivatives using FMU directional derivative support
822  if (m->self.uses_directional_derivatives_) {
823  // Evaluate using AD
824  if (eval_ad(m)) return 1;
825  }
826  // Calculate derivatives using finite differences
827  if (!m->self.uses_directional_derivatives_ || m->self.validate_forward_) {
828  // Evaluate using FD
829  if (eval_fd(m, independent_seeds)) return 1;
830  }
831  return 0;
832 }
void gather_fwd(FmuMemory *m) const
Definition: fmu.cpp:1451
int eval_fd(FmuMemory *m, bool independent_seeds) const
Definition: fmu.cpp:897
int eval_ad(FmuMemory *m) const
Definition: fmu.cpp:863

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 285 of file fmu3.cpp.

285  {
286  auto *c = static_cast<fmi3Instance>(instance);
287  fmi3Status status = exit_initialization_mode_(c);
288  if (status != fmi3OK) {
289  casadi_warning("fmi3ExitInitializationMode failed");
290  return 1;
291  }
292  return 0;
293 }

References exit_initialization_mode_.

◆ finalize()

void casadi::Fmu3::finalize ( )
overridevirtual

Allocate numerical values for initial auxilliary variables

Reimplemented from casadi::FmuInternal.

Definition at line 194 of file fmu3.cpp.

194  {
196  aux_value_.v_real.resize(vn_aux_real_.size());
197  aux_value_.v_integer.resize(vn_aux_integer_.size());
198  aux_value_.v_boolean.resize(vn_aux_boolean_.size());
199  aux_value_.v_string.resize(vn_aux_string_.size());
200 
201  // Recursive call
203 }
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().

◆ free_instance()

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

Implements casadi::FmuInternal.

Definition at line 256 of file fmu3.cpp.

256  {
257  if (free_instance_) {
258  auto *c = static_cast<fmi3Instance>(instance);
259  free_instance_(c);
260  } else {
261  casadi_warning("No free_instance function pointer available");
262  }
263 }

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 1467 of file fmu.cpp.

1467  {
1468  // Gather input and output indices
1469  gather_io(m);
1470  // Number of inputs and outputs
1471  size_t n_known = m->id_in_.size();
1472  // Get/clear seeds
1473  m->d_out_.clear();
1474  for (size_t id : m->id_out_) {
1475  m->d_out_.push_back(m->osens_[id]);
1476  m->osens_[id] = 0;
1477  }
1478  // Allocate result vectors
1479  m->v_in_.resize(n_known);
1480  m->d_in_.resize(n_known);
1481 }

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 1451 of file fmu.cpp.

1451  {
1452  // Gather input and output indices
1453  gather_io(m);
1454  // Number of outputs
1455  size_t n_unknown = m->id_out_.size();
1456  // Get/clear seeds
1457  m->d_in_.clear();
1458  for (size_t id : m->id_in_) {
1459  m->d_in_.push_back(m->isens_[id]);
1460  m->isens_[id] = 0;
1461  }
1462  // Allocate result vectors
1463  m->v_out_.resize(n_unknown);
1464  m->d_out_.resize(n_unknown);
1465 }

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 1424 of file fmu.cpp.

1424  {
1425  // Collect input indices and corresponding value references and values
1426  m->id_in_.clear();
1427  m->vr_in_.clear();
1428  m->v_in_.clear();
1429  for (size_t id = 0; id < m->imarked_.size(); ++id) {
1430  if (m->imarked_[id]) {
1431  m->id_in_.push_back(id);
1432  m->vr_in_.push_back(vr_in_[id]);
1433  m->v_in_.push_back(m->ibuf_[id]);
1434  m->imarked_[id] = false;
1435  }
1436  }
1437  // Collect output indices, corresponding value references
1438  m->id_out_.clear();
1439  m->vr_out_.clear();
1440  m->v_out_.clear();
1441  for (size_t id = 0; id < m->omarked_.size(); ++id) {
1442  if (m->omarked_[id]) {
1443  m->id_out_.push_back(id);
1444  m->vr_out_.push_back(vr_out_[id]);
1445  m->v_out_.push_back(nan);
1446  m->omarked_[id] = false;
1447  }
1448  }
1449 }
std::vector< unsigned int > vr_out_
Definition: fmu_impl.hpp:358
std::vector< unsigned int > vr_in_
Definition: fmu_impl.hpp:358

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 1377 of file fmu.cpp.

1377  {
1378  // Save to return
1379  for (size_t id : ored_[ind]) {
1380  *value++ = m->obuf_.at(id);
1381  }
1382 }

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 1181 of file fmu.cpp.

1181  {
1182  for (casadi_int i = 0; i < nsens; ++i) {
1183  *v++ = m->isens_.at(*id++);
1184  }
1185 }

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 1187 of file fmu.cpp.

1187  {
1188  // Quick return if not needed
1189  if (!v) return;
1190  // Retrieve all sensitivities FIXME(@jaeandersson): should use compatible types
1191  for (size_t id : ired_[ind]) {
1192  casadi_int id2 = id;
1193  get_adj(m, 1, &id2, v++);
1194  }
1195 }
void get_adj(FmuMemory *m, casadi_int nsens, const casadi_int *id, double *v) const
Definition: fmu.cpp:1181

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 367 of file fmu3.cpp.

369  {
370  fmi3Status status = get_adjoint_derivative_(instance, vr_out, n_out, vr_in, n_in,
371  seed, n_seed, sensitivity, n_sensitivity);
372  return status != fmi3OK;
373 }
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 1349 of file fmu.cpp.

1349  {
1350  // Quick return if nothing to get
1351  if (n_values == 0) return 0;
1352  // Retrieve from FMU
1353  if (get_real(m->instance, get_ptr(m->vr_out_), m->vr_out_.size(), values, n_values, m)) {
1354  casadi_warning("Evaluation failed");
1355  return 1;
1356  }
1357  // Successful return
1358  return 0;
1359 }

References casadi::get_ptr(), casadi::FmuInternal::get_real(), casadi::FmuMemory::instance, 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 420 of file fmu3.cpp.

420  {
421  return get_aux_impl(instance, aux_value_);
422 }
int get_aux_impl(void *instance, Value &aux_value) const
Definition: fmu3.cpp:424

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 424 of file fmu3.cpp.

424  {
425  auto *c = static_cast<fmi3Instance>(instance);
426  // Get real auxilliary variables
427  if (!vr_aux_real_.empty()) {
428  fmi3Status status = get_float64_(c, get_ptr(vr_aux_real_), vr_aux_real_.size(),
429  get_ptr(aux_value.v_real), aux_value.v_real.size());
430  if (status != fmi3OK) {
431  casadi_warning("fmi3GetFloat64 failed");
432  return 1;
433  }
434  }
435 
436  // Get integer/enum auxilliary variables
437  if (!vr_aux_integer_.empty()) {
438  fmi3Status status = get_int32_(c, get_ptr(vr_aux_integer_), vr_aux_integer_.size(),
439  get_ptr(aux_value.v_integer), aux_value.v_integer.size());
440  if (status != fmi3OK) {
441  casadi_warning("fmi3GetInt32 failed");
442  return 1;
443  }
444  }
445 
446  // Get boolean auxilliary variables
447  casadi_assert(vr_aux_boolean_.size() == aux_value.v_boolean.size(),
448  "Vector valued variables not supported");
449  for (size_t k = 0; k < vr_aux_boolean_.size(); ++k) {
450  fmi3Boolean value;
451  fmi3Status status = get_boolean_(c, &vr_aux_boolean_[k], 1, &value, 1);
452  if (status != fmi3OK) {
453  casadi_warning("fmi3GetBoolean failed for value reference " + str(vr_aux_boolean_[k]));
454  return 1;
455  }
456  aux_value.v_boolean[k] = value;
457  }
458 
459  // Get string auxilliary variables
460  for (size_t k = 0; k < vr_aux_string_.size(); ++k) {
461  fmi3ValueReference vr = vr_aux_string_[k];
462  fmi3String value = aux_value.v_string.at(k).c_str();
463  fmi3Status status = set_string_(c, &vr, 1, &value, 1);
464  if (status != fmi3OK) {
465  casadi_error("fmi3GetString failed for value reference " + str(vr));
466  }
467  }
468  // Successful return
469  return 0;
470 }
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 325 of file fmu3.cpp.

325  {
326  return 0;
327 }

◆ 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 359 of file fmu3.cpp.

361  {
362  fmi3Status status = get_directional_derivative_(instance, vr_out, n_out, vr_in, n_in,
363  seed, n_seed, sensitivity, n_sensitivity);
364  return status != fmi3OK;
365 }

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 1125 of file fmu.cpp.

1125  {
1126  for (casadi_int i = 0; i < nsens; ++i) {
1127  *v++ = m->osens_.at(*id++);
1128  }
1129 }

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 1131 of file fmu.cpp.

1131  {
1132  // Quick return if not needed
1133  if (!v) return;
1134  // Retrieve all sensitivities FIXME(@jaeandersson): should use compatible types
1135  for (size_t id : ored_[ind]) {
1136  casadi_int id2 = id;
1137  get_fwd(m, 1, &id2, v++);
1138  }
1139 }
void get_fwd(FmuMemory *m, casadi_int nsens, const casadi_int *id, double *v) const
Definition: fmu.cpp:1125

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 352 of file fmu3.cpp.

353  {
354  (void)m; // unused
355  fmi3Status status = get_float64_(instance, vr, n_vr, values, n_values);
356  return status != fmi3OK;
357 }

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 472 of file fmu3.cpp.

473  {
474  Value& v = static_cast<Fmu3Memory*>(m)->aux_value;
475  get_aux_impl(m->instance, v);
476  // Collect auxilliary variables
477  Dict aux;
478  // Real
479  for (size_t k = 0; k < vn_aux_real_.size(); ++k) {
480  aux[vn_aux_real_[k]] = static_cast<double>(v.v_real[k]);
481  }
482  // Integer
483  for (size_t k = 0; k < vn_aux_integer_.size(); ++k) {
484  aux[vn_aux_integer_[k]] = static_cast<casadi_int>(v.v_integer[k]);
485  }
486  // Boolean
487  for (size_t k = 0; k < vn_aux_boolean_.size(); ++k) {
488  aux[vn_aux_boolean_[k]] = static_cast<bool>(v.v_boolean[k]);
489  }
490  // String
491  for (size_t k = 0; k < vn_aux_string_.size(); ++k) {
492  aux[vn_aux_string_[k]] = v.v_string[k];
493  }
494  // Copy to stats
495  (*stats)["aux"] = aux;
496  // Loop over input variables
497  for (size_t k = 0; k < name_in.size(); ++k) {
498  // Only consider regular inputs
499  if (in[k].type == InputType::REG) {
500  // Get the indices
501  const std::vector<size_t>& iind = ired_.at(in[k].ind);
502  // Collect values
503  std::vector<double> v(iind.size());
504  for (size_t i = 0; i < v.size(); ++i) v[i] = value_in_.at(iind[i]);
505  // Save to stats
506  (*stats)[name_in[k]] = v;
507  }
508  }
509 }
std::vector< double > value_in_
Definition: fmu_impl.hpp:361

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.

205  {
206  return static_cast<const Internal*>(this)->count;
207  }

◆ 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:308

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:308

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 106 of file fmu3.cpp.

106  {
107  // Initialize base classes
108  FmuInternal::init(dae);
109 
110  // Collect input and parameter values
111  vr_real_.clear();
112  vr_integer_.clear();
113  vr_boolean_.clear();
114  vr_string_.clear();
115  init_real_.clear();
116  init_integer_.clear();
117  init_boolean_.clear();
118  init_string_.clear();
119  for (size_t i = 0; i < dae->n_variables(); ++i) {
120  const Variable& v = dae->variable(i);
121  casadi_assert(v.numel == 1, "Vector variable support not implemented");
122  // Skip if the wrong type
123  if (v.causality != Causality::PARAMETER && v.causality != Causality::INPUT) continue;
124  // Variable has not been set - keep default value
125  if (!v.is_set()) continue;
126  // Value reference
127  fmi3ValueReference vr = v.value_reference;
128  // Get value
129  switch (to_fmi2(v.type)) {
130  case TypeFmi2::REAL:
131  init_real_.push_back(static_cast<fmi3Float64>(v.value.front()));
132  vr_real_.push_back(vr);
133  break;
134  case TypeFmi2::INTEGER:
135  case TypeFmi2::ENUM:
136  init_integer_.push_back(static_cast<fmi3Int32>(v.value.front()));
137  vr_integer_.push_back(vr);
138  break;
139  case TypeFmi2::BOOLEAN:
140  init_boolean_.push_back(static_cast<fmi3Boolean>(v.value.front()));
141  vr_boolean_.push_back(vr);
142  break;
143  case TypeFmi2::STRING:
144  init_string_.push_back(v.stringvalue);
145  vr_string_.push_back(vr);
146  break;
147  default:
148  casadi_warning("Ignoring " + v.name + ", type: " + to_string(v.type));
149  }
150  }
151 
152  // Collect auxilliary variables
153  vn_aux_real_.clear();
154  vn_aux_integer_.clear();
155  vn_aux_boolean_.clear();
156  vn_aux_string_.clear();
157  vr_aux_real_.clear();
158  vr_aux_integer_.clear();
159  vr_aux_boolean_.clear();
160  vr_aux_string_.clear();
161  for (auto&& s : aux_) {
162  const Variable& v = dae->variable(s);
163  // Convert to expected type
164  fmi3ValueReference vr = v.value_reference;
165  // Sort by type
166  switch (to_fmi2(v.type)) {
167  case TypeFmi2::REAL:
168  // Real
169  vn_aux_real_.push_back(v.name);
170  vr_aux_real_.push_back(vr);
171  break;
172  case TypeFmi2::INTEGER:
173  case TypeFmi2::ENUM:
174  // Integer or enum
175  vn_aux_integer_.push_back(v.name);
176  vr_aux_integer_.push_back(vr);
177  break;
178  case TypeFmi2::BOOLEAN:
179  // Boolean
180  vn_aux_boolean_.push_back(v.name);
181  vr_aux_boolean_.push_back(vr);
182  break;
183  case TypeFmi2::STRING:
184  // String
185  vn_aux_string_.push_back(v.name);
186  vr_aux_string_.push_back(vr);
187  break;
188  default:
189  casadi_warning("Ignoring " + v.name + ", type: " + to_string(v.type));
190  }
191  }
192 }
std::vector< std::string > aux_
Definition: fmu_impl.hpp:312
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:1247

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 243 of file fmu3.cpp.

243  {
244  // Instantiate FMU
245  fmi3String instanceName = instance_name_.c_str();
246  fmi3String instantiationToken = instantiation_token_.c_str();
247  fmi3String resourcePath = resource_loc_.c_str();
248  fmi3Boolean visible = fmi3False;
249  fmi3InstanceEnvironment instanceEnvironment = nullptr;
250  fmi3Instance c = instantiate_model_exchange_(instanceName, instantiationToken, resourcePath,
251  visible, logging_on_, instanceEnvironment, log_message_callback);
252  if (c == nullptr) casadi_error("fmi3Instantiate failed");
253  return c;
254 }
static void log_message_callback(fmi3InstanceEnvironment instanceEnvironment, fmi3Status status, fmi3String category, fmi3String message)
Definition: fmu3.cpp:237
std::string instantiation_token_
Definition: fmu_impl.hpp:324
std::string resource_loc_
Definition: fmu_impl.hpp:315
std::string instance_name_
Definition: fmu_impl.hpp:321

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 381 of file fmu_impl.hpp.

381  {
382  // Load the function
383  signal_t f = li_.get_function(symname);
384  // Ensure that it was found
385  casadi_assert(f != nullptr, "Cannot retrieve '" + symname + "'");
386  // Return function with the right type
387  return reinterpret_cast<T*>(f);
388 }
Importer li_
DLL.
Definition: fmu_impl.hpp:342
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 205 of file fmu3.cpp.

205  {
206  instantiate_model_exchange_ = load_function<fmi3InstantiateModelExchangeTYPE>(
207  "fmi3InstantiateModelExchange");
208  free_instance_ = load_function<fmi3FreeInstanceTYPE>("fmi3FreeInstance");
209  reset_ = load_function<fmi3ResetTYPE>("fmi3Reset");
210  enter_initialization_mode_ = load_function<fmi3EnterInitializationModeTYPE>(
211  "fmi3EnterInitializationMode");
212  exit_initialization_mode_ = load_function<fmi3ExitInitializationModeTYPE>(
213  "fmi3ExitInitializationMode");
214  enter_continuous_time_mode_ = load_function<fmi3EnterContinuousTimeModeTYPE>(
215  "fmi3EnterContinuousTimeMode");
216  set_time_ = load_function<fmi3SetTimeTYPE>("fmi3SetTime");
217  get_float64_ = load_function<fmi3GetFloat64TYPE>("fmi3GetFloat64");
218  set_float64_ = load_function<fmi3SetFloat64TYPE>("fmi3SetFloat64");
219  get_int32_ = load_function<fmi3GetInt32TYPE>("fmi3GetInt32");
220  set_int32_ = load_function<fmi3SetInt32TYPE>("fmi3SetInt32");
221  get_boolean_ = load_function<fmi3GetBooleanTYPE>("fmi3GetBoolean");
222  set_boolean_ = load_function<fmi3SetBooleanTYPE>("fmi3SetBoolean");
223  get_string_ = load_function<fmi3GetStringTYPE>("fmi3GetString");
224  set_string_ = load_function<fmi3SetStringTYPE>("fmi3SetString");
227  load_function<fmi3GetDirectionalDerivativeTYPE>("fmi3GetDirectionalDerivative");
228  }
231  load_function<fmi3GetAdjointDerivativeTYPE>("fmi3GetAdjointDerivative");
232  }
234  load_function<fmi3UpdateDiscreteStatesTYPE>("fmi3UpdateDiscreteStates");
235 }
fmi3GetStringTYPE * get_string_
Definition: fmu3.hpp:108
fmi3SetInt32TYPE * set_int32_
Definition: fmu3.hpp:107
bool provides_adjoint_derivatives_
Definition: fmu_impl.hpp:333
bool provides_directional_derivatives_
Definition: fmu_impl.hpp:333

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 237 of file fmu3.cpp.

238  {
239  // Print message content
240  uout() << "[" << category << "] " << message << std::endl;
241 }
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 785 of file fmu.cpp.

785  {
786  casadi_assert(Archiver::has_plugin("libzip"),
787  "pack_fmu requires libzip. Compile CasADi with WITH_LIBZIP=ON.");
788  // The {local_file -> archive path} map is exactly the set of zip entries: no staging
789  // directory or copies required.
790  std::vector<std::pair<std::string, std::string>> entries;
791  for (auto&& kv : files) {
792  std::string arc = kv.second.to_string();
793  if (arc.substr(0, 2) == "./") arc = arc.substr(2); // zip entries are root-relative
794  entries.push_back({kv.first, arc});
795  }
796  std::string out = path;
797  if (out.size() < 4 || out.substr(out.size() - 4) != ".fmu") out += ".fmu";
798  casadi_assert(Archiver::getPlugin("libzip").exposed.pack_entries(entries, out),
799  "Packing FMU '" + out + "' failed.");
800  // Clean up the loose files
801  for (auto&& e : entries) Filesystem::remove(e.first);
802  return out;
803 }
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 1312 of file fmu.cpp.

1312  {
1313  for (size_t id : ored_[ind]) {
1314  // Mark as requested
1315  m->omarked_.at(id) = true;
1316  // Also log corresponding input index
1317  m->wrt_.at(id) = -1;
1318  }
1319 }

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 1172 of file fmu.cpp.

1172  {
1173  // Request all sensitivities FIXME(@jaeandersson): should use compatible types
1174  casadi_int wrt_id = -1;
1175  for (size_t id : ired_[ind]) {
1176  casadi_int id2 = id;
1177  request_adj(m, 1, &id2, &wrt_id);
1178  }
1179 }
void request_adj(FmuMemory *m, casadi_int nsens, const casadi_int *id, const casadi_int *wrt_id) const
Definition: fmu.cpp:1163

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 1163 of file fmu.cpp.

1164  {
1165  for (casadi_int i = 0; i < nsens; ++i) {
1166  m->imarked_.at(*id) = true;
1167  m->wrt_.at(*id) = wrt_id ? *wrt_id++ : -1;
1168  id++;
1169  }
1170 }

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 1415 of file fmu.cpp.

1415  {
1416  // Request all sensitivities FIXME(@jaeandersson): should use compatible types
1417  casadi_int wrt_id = -1;
1418  for (size_t id : ored_[ind]) {
1419  casadi_int id2 = id;
1420  request_fwd(m, 1, &id2, &wrt_id);
1421  }
1422 }
void request_fwd(FmuMemory *m, casadi_int nsens, const casadi_int *id, const casadi_int *wrt_id) const
Definition: fmu.cpp:1406

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 1406 of file fmu.cpp.

1407  {
1408  for (casadi_int i = 0; i < nsens; ++i) {
1409  m->omarked_.at(*id) = true;
1410  m->wrt_.at(*id) = *wrt_id++;
1411  id++;
1412  }
1413 }

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

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

◆ reset()

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

Definition at line 265 of file fmu3.cpp.

265  {
266  auto *c = static_cast<fmi3Instance>(instance);
267  fmi3Status status = reset_(c);
268  if (status != fmi3OK) {
269  casadi_warning("fmi3Reset failed");
270  return 1;
271  }
272  return 0;
273 }

References reset_.

◆ serialize()

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

Definition at line 1497 of file fmu.cpp.

1497  {
1498  serialize_type(s);
1499  serialize_body(s);
1500 }
virtual void serialize_body(SerializingStream &s) const
Definition: fmu.cpp:1506
virtual void serialize_type(SerializingStream &s) const
Definition: fmu.cpp:1502

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 575 of file fmu3.cpp.

575  {
577 
578  s.version("Fmu3", 1);
579  s.pack("Fmu3::vr_real", vr_real_);
580  s.pack("Fmu3::vr_integer", vr_integer_);
581  s.pack("Fmu3::vr_boolean", vr_boolean_);
582  s.pack("Fmu3::vr_string", vr_string_);
583  s.pack("Fmu3::init_real", init_real_);
584  s.pack("Fmu3::init_integer", init_integer_);
585  s.pack("Fmu3::init_boolean", init_boolean_);
586  s.pack("Fmu3::init_string", init_string_);
587 
588  s.pack("Fmu3::vn_aux_real", vn_aux_real_);
589  s.pack("Fmu3::vn_aux_integer", vn_aux_integer_);
590  s.pack("Fmu3::vn_aux_boolean", vn_aux_boolean_);
591  s.pack("Fmu3::vn_aux_string", vn_aux_string_);
592  s.pack("Fmu3::vr_aux_real", vr_aux_real_);
593  s.pack("Fmu3::vr_aux_integer", vr_aux_integer_);
594  s.pack("Fmu3::vr_aux_boolean", vr_aux_boolean_);
595  s.pack("Fmu3::vr_aux_string", vr_aux_string_);
596 }

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 1502 of file fmu.cpp.

1502  {
1503  s.pack("FmuInternal::type", class_name());
1504 }

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 1291 of file fmu.cpp.

1291  {
1292  if (value) {
1293  // Argument is given
1294  for (size_t id : ired_[ind]) {
1295  if (*value != m->ibuf_.at(id)) {
1296  m->ibuf_.at(id) = *value;
1297  m->imarked_.at(id) = true;
1298  }
1299  value++;
1300  }
1301  } else {
1302  // Argument is null - all zeros
1303  for (size_t id : ired_[ind]) {
1304  if (0 != m->ibuf_.at(id)) {
1305  m->ibuf_.at(id) = 0;
1306  m->imarked_.at(id) = true;
1307  }
1308  }
1309  }
1310 }

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 1141 of file fmu.cpp.

1142  {
1143  for (casadi_int i = 0; i < nseed; ++i) {
1144  if (*v != 0.) {
1145  m->osens_.at(*id) = *v;
1146  m->omarked_.at(*id) = true;
1147  }
1148  id++;
1149  v++;
1150  }
1151 }

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 1153 of file fmu.cpp.

1153  {
1154  // If seeds are zero, no need to add to seed buffers
1155  if (!v) return;
1156  // Pass all seeds FIXME(@jaeandersson): should use compatible types
1157  for (size_t id : ored_[ind]) {
1158  casadi_int id2 = id;
1159  set_adj(m, 1, &id2, v++);
1160  }
1161 }
void set_adj(FmuMemory *m, casadi_int nseed, const casadi_int *id, const double *v) const
Definition: fmu.cpp:1141

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 1321 of file fmu.cpp.

1321  {
1322  // Quick return if nothing to set
1323  if (n_values == 0) return 0;
1324  // Value references
1325  const unsigned int* vr = get_ptr(m->vr_in_);
1326  size_t n_vr = m->vr_in_.size();
1327  // Set time variable, if any
1328  if (has_independent_ && *vr == independent_vr_) {
1329  // Update FMU time
1330  if (set_time(m->instance, *values)) return 1;
1331  // Skip when setting remaining variables
1332  vr++;
1333  n_vr--;
1334  values++;
1335  n_values--;
1336  // Quick return if nothing left to set
1337  if (n_vr == 0) return 0;
1338  }
1339 
1340  // Set remaining variables
1341  if (set_real(m->instance, vr, n_vr, values, n_values)) {
1342  casadi_warning("Setting FMU variables failed");
1343  return 1;
1344  }
1345 
1346  return 0;
1347 }
virtual int set_time(void *instance, double t) const =0
unsigned int independent_vr_
Definition: fmu_impl.hpp:351
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 1384 of file fmu.cpp.

1385  {
1386  for (casadi_int i = 0; i < nseed; ++i) {
1387  if (*v != 0.) {
1388  m->isens_.at(*id) = *v;
1389  m->imarked_.at(*id) = true;
1390  }
1391  id++;
1392  v++;
1393  }
1394 }

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 1396 of file fmu.cpp.

1396  {
1397  // If seeds are zero, no need to add to seed buffers
1398  if (!v) return;
1399  // Pass all seeds FIXME(@jaeandersson): should use compatible types
1400  for (size_t id : ired_[ind]) {
1401  casadi_int id2 = id;
1402  set_fwd(m, 1, &id2, v++);
1403  }
1404 }
void set_fwd(FmuMemory *m, casadi_int nseed, const casadi_int *id, const double *v) const
Definition: fmu.cpp:1384

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 334 of file fmu3.cpp.

335  {
336  // Set time variable, if any
337  if (has_independent_ && n_vr > 0 && *vr == vr_in_[0]) {
338  // Update FMU time
339  fmi3Status status = set_time_(instance, *values);
340  if (status != fmi3OK) return 1;
341  // Skip when setting remaining variables
342  vr++;
343  n_vr--;
344  values++;
345  n_values--;
346  }
347  // Set remaining variables
348  fmi3Status status = set_float64_(instance, vr, n_vr, values, n_values);
349  return status != fmi3OK;
350 }

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 329 of file fmu3.cpp.

329  {
330  fmi3Status status = set_time_(instance, t);
331  return status != fmi3OK;
332 }

References set_time_.

◆ set_values()

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

Implements casadi::FmuInternal.

Definition at line 375 of file fmu3.cpp.

375  {
376  auto *c = static_cast<fmi3Instance>(instance);
377  // Pass real values before initialization
378  casadi_assert(vr_real_.size() == init_real_.size(), "Vector valued variables not supported");
379  for (size_t k = 0; k < vr_real_.size(); ++k) {
380  fmi3Status status = set_float64_(c, &vr_real_[k], 1, &init_real_[k], 1);
381  if (status != fmi3OK) {
382  casadi_warning("fmi3SetFloat64 failed for value reference " + str(vr_real_[k]));
383  return 1;
384  }
385  }
386 
387  // Pass integer values before initialization (also enums)
388  if (!vr_integer_.empty()) {
389  fmi3Status status = set_int32_(c, get_ptr(vr_integer_), vr_integer_.size(),
391  if (status != fmi3OK) {
392  casadi_warning("fmi3SetInt32 failed");
393  return 1;
394  }
395  }
396  // Pass boolean values before initialization
397  if (!vr_boolean_.empty()) {
398  casadi_error("Broken");
399  // nullptr -> get_ptr(init_boolean_)
400  fmi3Status status = set_boolean_(c, get_ptr(vr_boolean_), vr_boolean_.size(),
401  nullptr, init_boolean_.size());
402  if (status != fmi3OK) {
403  casadi_warning("fmi3SetBoolean failed");
404  return 1;
405  }
406  }
407  // Pass string values before initialization
408  for (size_t k = 0; k < vr_string_.size(); ++k) {
409  fmi3ValueReference vr = vr_string_[k];
410  fmi3String value = init_string_[k].c_str();
411  fmi3Status status = set_string_(c, &vr, 1, &value, 1);
412  if (status != fmi3OK) {
413  casadi_error("fmi3SetString failed for value reference " + str(vr));
414  }
415  }
416  // Successful return
417  return 0;
418 }

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(); }

◆ update_discrete_states()

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

Implements casadi::FmuInternal.

Definition at line 305 of file fmu3.cpp.

305  {
306  auto *c = static_cast<fmi3Instance>(instance);
307  // Return arguments in FMI types
308  fmi3Boolean discreteStatesNeedUpdate, terminateSimulation, nominalsOfContinuousStatesChanged,
309  valuesOfContinuousStatesChanged, nextEventTimeDefined;
310  fmi3Float64 nextEventTime;
311  // Call FMU
312  fmi3Status status = update_discrete_states_(c, &discreteStatesNeedUpdate, &terminateSimulation,
313  &nominalsOfContinuousStatesChanged, &valuesOfContinuousStatesChanged, &nextEventTimeDefined,
314  &nextEventTime);
315  // Pass to event iteration memory
316  eventmem->discrete_states_need_update = discreteStatesNeedUpdate;
317  eventmem->terminate_simulation = terminateSimulation;
318  eventmem->nominals_of_continuous_states_changed = nominalsOfContinuousStatesChanged;
319  eventmem->values_of_continuous_states_changed = valuesOfContinuousStatesChanged;
320  eventmem->next_event_time_defined = nextEventTimeDefined;
321  eventmem->next_event_time = nextEventTime;
322  return status != fmi3OK;
323 }

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.

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

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 351 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: