50 #include <FMI2/fmi2_import.h>
53 #include <FMI/fmi_version.h>
54 #include <FMI2/fmi2_types.h>
55 #include <FMI2/fmi2_functions.h>
56 #include <FMI2/fmi2_enums.h>
58 #include <FMI/fmi_util.h>
59 #include "fmi2_import_impl.h"
64 #include <FMI1/fmi1_import.h>
70 #include <FMI/fmi_version.h>
71 #include <FMI1/fmi1_types.h>
72 #include <FMI1/fmi1_functions.h>
73 #include <FMI1/fmi1_enums.h>
75 #include <FMI/fmi_util.h>
76 #include "fmi1_import_impl.h"
78 #define STATUSCHECK(a)\
80 printf("Error in %s, %d\n", __func__, __LINE__); \
81 throw ErrGeneric(MBDYN_EXCEPT_ARGS); \
86 eventInfo->newDiscreteStatesNeeded = fmi2_true;
87 eventInfo->terminateSimulation = fmi2_false;
88 while (eventInfo->newDiscreteStatesNeeded && !eventInfo->terminateSimulation) {
89 fmi2_import_new_discrete_states(fmu, eventInfo);
93 static void stepFinished(fmi2_component_environment_t env, fmi2_status_t status) {
94 silent_cout(
"stepFinished is called with fmiStatus = "<< fmi2_status_to_string(status)<<std::endl);
100 printf(
"Annotation element %s start (tool: %s, parent:%s)\n", elm, parentName,
101 parent?fmi2_import_get_variable_name((fmi2_import_variable_t*)parent):
"model");
103 printf(
"Attribute %s = %s\n", attr[i], attr[i+1]);
106 printf(
"Annotation data:\n");
112 for(i = 0; i < len; i++){
119 printf(
"\nAnnotation element %s end\n", elm);
135 silent_cout(
"Error parsing XML 2.0\n");
138 silent_cout(
"XML Parsed successfully\n");
152 if(
simType != fmi2_import_get_fmu_kind(
fmu)-1){
153 silent_cout(
"This FMU does not support the specified simulation type");
165 std::cout<<
"Unknown Type";
178 return fmi2_import_get_number_of_event_indicators(
fmu);
182 fmi2_import_variable_list_t* vl;
183 vl = fmi2_import_get_variable_list(
fmu, 0);
184 return fmi2_import_get_variable_list_size(vl);
188 jmstatus = fmi2_import_instantiate(
fmu,
"Test ME model instance",fmi2_model_exchange,0,0);
195 fmistatus = fmi2_import_setup_experiment(
fmu, dTol, rTol, time, fmi2_false, 0.0);
203 fmi2_import_enter_continuous_time_mode(
fmu);
207 eventInfo.newDiscreteStatesNeeded = fmi2_false;
208 eventInfo.terminateSimulation = fmi2_false;
209 eventInfo.nominalsOfContinuousStatesChanged = fmi2_false;
210 eventInfo.valuesOfContinuousStatesChanged = fmi2_true;
211 eventInfo.nextEventTimeDefined = fmi2_false;
217 unsigned int capability = 0;
220 fmi2_capabilities_enu_t
id = fmi2_cs_providesDirectionalDerivatives;
221 capability = fmi2_xml_get_capability(
fmu->md,
id);
223 fmi2_capabilities_enu_t
id = fmi2_me_providesDirectionalDerivatives;
224 capability = fmi2_xml_get_capability(
fmu->md,
id);
228 silent_cout(
"This FMU does not support Directional Derivatives\n");
232 silent_cout(
"This FMU supports Directional Derivatives\n");
241 fmi2_import_variable_list_t* derivativeList = fmi2_import_get_derivatives_list(
fmu);
242 size_t derivativeListSize = fmi2_import_get_variable_list_size(derivativeList);
244 fmi2_import_variable_t *var;
245 fmi2_import_real_variable_t *varCont;
246 fmi2_value_reference_t* derList =
new fmi2_value_reference_t[derivativeListSize];
247 fmi2_value_reference_t* stateList =
new fmi2_value_reference_t[derivativeListSize + inputLength];
249 for (
unsigned int i=0; i<derivativeListSize; i++){
250 var = fmi2_import_get_variable(derivativeList, i);
251 derList[i] = fmi2_import_get_variable_vr(var);
252 varCont = fmi2_import_get_real_variable_derivative_of((fmi2_import_real_variable_t*)var);
253 stateList[i] = fmi2_import_get_variable_vr((fmi2_import_variable_t*)varCont);
260 fmi2_real_t
output[numOfContStates + inputLength];
263 fmistatus = fmi2_import_get_directional_derivative(
fmu, stateList, numOfContStates + inputLength, &derList[i], 1, output , seedVector);
266 for (
int j=0; j<numOfContStates + inputLength; j++){
267 jacobian->
PutCoef(i+1, j+1, output[j]);
285 silent_cout(
"This FMU does triggers events. Warning: Event triggers not supported by MBDyn\n");
311 v = fmi2_import_get_variable_by_name(
fmu, s.c_str());
313 silent_cout(s.c_str()<<
" is not defined in XML, hence ");
316 return ((fmi2_import_get_causality(
v))==2);
320 v = fmi2_import_get_variable_by_name(
fmu, s);
321 return static_cast<int>(fmi2_import_get_variable_vr(
v));
327 fmi1_value_reference_t valueReference = {i};
328 fmistatus = fmi2_import_get_real(
fmu, &valueReference, 1, realValue);
337 fmi_import_free_context(
context);
343 fmi2_import_free_instance(
fmu);
344 fmi2_import_destroy_dllfmu(
fmu);
345 fmi2_import_free(
fmu);
351 fmi2_import_free_instance(
fmu);
352 fmi2_import_destroy_dllfmu(
fmu);
353 fmi2_import_free(
fmu);
374 int zeroCrossningEvent = 0;
380 zeroCrossningEvent = 1;
385 if ( zeroCrossningEvent || (
eventInfo.nextEventTimeDefined && currTime ==
eventInfo.nextEventTime)) {
388 fmistatus = fmi2_import_enter_continuous_time_mode(
fmu);
400 fmi2_value_reference_t ref;
401 fmi2_variable_alias_kind_enu_t aliasKind;
402 fmi2_base_type_enu_t baseType;
404 fmi2_import_variable_t*
v = fmi2_import_get_variable_by_name(
fmu, s.c_str());
406 ref = fmi2_import_get_variable_vr(v);
407 aliasKind = fmi2_import_get_variable_alias_kind(v);
408 baseType = fmi2_import_get_variable_base_type(v);
410 if (aliasKind == fmi2_variable_is_not_alias){
411 if(baseType != fmi2_base_type_bool){
415 value = (
bool)!value;
419 if (baseType == fmi2_base_type_real){
422 else if (baseType == fmi2_base_type_int){
423 fmistatus = fmi2_import_set_integer(
fmu, &ref, 1, (
const int*)&value);
425 else if (baseType == fmi2_base_type_bool){
426 fmistatus = fmi2_import_set_boolean(
fmu, &ref, 1, (
const fmi2_boolean_t*)&value);
429 silent_cerr(
"Input type not Supported");
438 std::stringstream resourceLocation;
439 std::string strLocation = location;
441 resourceLocation <<
"file://" << strLocation;
442 fmi2_boolean_t visible = fmi2_false;
443 jmstatus = fmi2_import_instantiate(
fmu,
"Model for CS", fmi2_cosimulation, resourceLocation.str().c_str(), visible);
446 if (tend == std::numeric_limits<double>::max()){
463 fmistatus = fmi2_import_do_step(
fmu, tcur, dt, fmi2_true);
477 fmi1_capi_free_dll(
fmu->capi);
486 fmi1_import_free_model_instance(
fmu);
487 fmi1_import_destroy_dllfmu(
fmu);
488 fmi1_import_free(
fmu);
494 fmi1_import_free_slave_instance(
fmu);
501 fmu = fmi1_import_parse_xml(context, dirPath);
503 silent_cout(
"Error parsing XML\n");
506 silent_cout(
"XML Parsed successfully\n");
514 silent_cout(
"Callback Functions Set.\n");
521 silent_cout(
"Successfully created the DLL loading mechanism.\n");
535 fmi1_boolean_t tolControlled = fmi1_true;
538 dTol = fmi1_import_get_default_experiment_tolerance(
fmu);
539 tolControlled = fmi1_false;
545 jmstatus = fmi1_import_instantiate_model(
fmu,
"Test ME model instance");
555 fmi1_capi_load_dll(
fmu->capi);
561 fmi1_boolean_t loggingOn = fmi1_true;
563 fmi1_boolean_t loggingOn = fmi1_false;
566 fmi1_capi_instantiate_model(
fmu->capi,
"noName", fmi1_import_get_GUID(
fmu), loggingOn);
569 silent_cout(
"FMU Initialized\n");
573 return fmi1_import_get_number_of_event_indicators(
fmu);
577 int zeroCrossningEvent = 0;
583 zeroCrossningEvent = 1;
588 if ( zeroCrossningEvent || (
eventInfo.upcomingTimeEvent && currTime ==
eventInfo.nextEventTime)) {
599 v = fmi1_import_get_variable_by_name(
fmu, s.c_str());
601 silent_cout(s.c_str()<<
" is not defined in XML, hence ");
605 return ((fmi1_import_get_causality(
v))==0);
611 fmi1_value_reference_t ref;
612 fmi1_variable_alias_kind_enu_t aliasKind;
613 fmi1_base_type_enu_t baseType;
615 fmi1_import_variable_t*
v = fmi1_import_get_variable_by_name(
fmu, s.c_str());
617 ref = fmi1_import_get_variable_vr(v);
618 aliasKind = fmi1_import_get_variable_alias_kind(v);
619 baseType = fmi1_import_get_variable_base_type(v);
621 if (aliasKind == fmi1_variable_is_negated_alias){
622 if(baseType != fmi1_base_type_bool){
626 value = (
bool)!value;
630 if (baseType == fmi1_base_type_real){
633 else if (baseType == fmi1_base_type_int){
634 fmistatus = fmi1_import_set_integer(
fmu, &ref, 1, (
const int*)&value);
636 else if (baseType == fmi1_base_type_bool){
637 fmistatus = fmi1_import_set_boolean(
fmu, &ref, 1, (
const fmi1_boolean_t*)&value);
640 silent_cerr(
"Input type not Supported");
647 v = fmi1_import_get_variable_by_name(
fmu, s);
648 return static_cast<int>(fmi1_import_get_variable_vr(
v));
674 fmi1_value_reference_t valueReference = {i};
675 fmistatus = fmi1_import_get_real(
fmu, &valueReference, 1, realValue);
690 fmi1_import_variable_list_t* vl;
691 vl = fmi1_import_get_variable_list(
fmu);
692 return fmi1_import_get_variable_list_size(vl);
707 silent_cout(
"This FMU does triggers events. Warning: Event triggers not supported by MBDyn\n");
712 fmistatus = fmi1_import_do_step(
fmu, tcur, dt, fmi1_true);
734 std::stringstream resourceLocation;
735 std::string strLocation = location;
737 resourceLocation <<
"file://" << strLocation;
738 jmstatus = fmi1_import_instantiate_slave(
fmu,
"Test CS model instance", resourceLocation.str().c_str(),
"", 0.0, fmi1_false, fmi1_false);
741 fmistatus = fmi1_import_initialize_slave(
fmu, 1.0, fmi1_false, 10.0);
fmi1_import_variable_t * v
void EventIndicatorInit(void)
void GetStateDerivatives(double *)
double GetStateFromRefValue(unsigned int i)
static void stepFinished(fmi2_component_environment_t env, fmi2_status_t status)
static void output(const LoadableElem *pEl, OutputHandler &OH)
#define MBDYN_EXCEPT_ARGS
int GetNumOfContinousStates(void)
void SetStates(double *states)
int GetRefValueFromString(const char *s)
fmi2_xml_callbacks_t annotation_callbacks
fmi1_real_t * eventIndicators
void GetStateDerivatives(double *)
int GetNumOfEventIndicators(void)
void CSPropogate(double tcur, double dt)
void SetStates(double *states)
bool SupportsDirectionalDerivatives()
void SetRelativeTol(double dTol)
void GetDirectionalDerivatives(FullMatrixHandler *, int *, int, double *)
fmi1_event_info_t eventInfo
void Initialize(double dTol, double time, double rTol)
void SetTime(double time)
void parseXML(fmi_import_context_t *context, const char *dirPath)
void CSPropogate(double tcur, double dt)
fmi1_real_t * eventIndicatorsPrev
void InitializeAsSlave(const char *location, double tstart, double tend)
fmi2_event_info_t eventInfo
fmi1_boolean_t intermediateResults
void ImportCreateDLL(void)
virtual void PutCoef(integer iRow, integer iCol, const doublereal &dCoef)
void SetValuesByVariable(const std::string, double)
bool CheckInput(const std::string)
void SetValuesByVariable(const std::string, double)
int annotation_end_handle(void *context, const char *elm)
fmi2_real_t * eventIndicators
void parseXML(fmi_import_context_t *context, const char *dirPath)
fmi_import_context_t * context
fmi2_boolean_t callEventUpdate
void SetRelativeTol(double dTol)
double GetStateFromRefValue(unsigned int i)
bool CheckInterrupts(double, double *)
fmi2_real_t relativeTolerance
void EventIndicatorInit(void)
bool SupportsDirectionalDerivatives()
void setCallBackFunction()
void ImportCreateDLL(void)
fmi2_callback_functions_t callBackFunctions
fmi2_boolean_t terminateSimulation
fmi1_callback_functions_t callBackFunctions
void SetTime(double time)
void setCallBackFunction()
int GetNumOfEventIndicators(void)
fmi2_real_t * eventIndicatorsPrev
int GetNumOfContinousStates(void)
int annotation_start_handle(void *context, const char *parentName, void *parent, const char *elm, const char **attr)
bool CheckInput(const std::string)
bool CheckInterrupts(double, double *)
int GetRefValueFromString(const char *s)
fmi1_value_reference_t * vrs
void GetDirectionalDerivatives(FullMatrixHandler *, int *, int, double *)
fmi1_real_t relativeTolerance
void InitializeAsSlave(const char *, double, double)
void do_event_iteration(fmi2_import_t *fmu, fmi2_event_info_t *eventInfo)
int annotation_data_handle(void *context, const char *s, int len)
void Initialize(double dTol, double time, double rTol)
fmi2_import_variable_t * v