20 #include "config_test.h" 27 printf(
"Press 'Enter' to exit\n");
64 fmi2_real_t tstart = 0.0;
67 fmi2_real_t hdef = 0.1;
68 fmi2_real_t tend = 2.0;
70 size_t n_event_indicators;
72 fmi2_real_t states_end_results[] = {0.362000, -3.962000};
73 fmi2_real_t* states_der;
74 fmi2_real_t* event_indicators;
75 fmi2_real_t* event_indicators_prev;
76 fmi2_boolean_t callEventUpdate;
77 fmi2_boolean_t terminateSimulation =
fmi2_false;
78 fmi2_boolean_t toleranceControlled =
fmi2_true;
79 fmi2_real_t relativeTolerance = 0.001;
89 if (
sizeof(states_end_results)/
sizeof(fmi2_real_t) != n_states) {
90 printf(
"Number of states and results have different length n_states = %u n_results = %u\n", (
unsigned)n_states, (
unsigned)
sizeof(states_end_results));
94 states = calloc(n_states,
sizeof(
double));
95 states_der = calloc(n_states,
sizeof(
double));
96 event_indicators = calloc(n_event_indicators,
sizeof(
double));
97 event_indicators_prev = calloc(n_event_indicators,
sizeof(
double));
101 printf(
"fmi2_import_instantiate failed\n");
136 int zero_crossing_event = 0;
141 fmi2_real_t *temp = event_indicators;
142 event_indicators = event_indicators_prev;
143 event_indicators_prev = temp;
148 for (k = 0; k < n_event_indicators; k++) {
149 if ((event_indicators[k] > 0) != (event_indicators_prev[k] > 0)) {
150 zero_crossing_event = 1;
156 if (callEventUpdate || zero_crossing_event ||
173 if(tcur > tend - hcur/1e16) {
180 for (k = 0; k < n_states; k++) {
181 states[k] = states[k] + hcur*states_der[k];
182 if (k == 0) printf(
"Ball height state[%u] = %f\n", (
unsigned)k, states[k]);
189 &terminateSimulation);
193 for (k = 0; k < n_states; k++) {
194 fmi2_real_t res = states[k] - states_end_results[k];
195 res = res > 0 ? res: -res;
197 printf(
"Simulation results is wrong states[%u] %f != %f, |res| = %f\n", (
unsigned)k, states[k], states_end_results[k], res);
209 free(event_indicators);
210 free(event_indicators_prev);
215 int main(
int argc,
char *argv[])
228 printf(
"Usage: %s <fmu_file> <temporary_dir>\n", argv[0]);
236 callbacks.
malloc = malloc;
237 callbacks.
calloc = calloc;
239 callbacks.
free = free;
244 #ifdef FMILIB_GENERATE_BUILD_STAMP 245 printf(
"Library build stamp:\n%s\n", fmilib_get_build_stamp());
254 printf(
"Only version 2.0 is supported by this code\n");
261 printf(
"Error parsing XML, exiting\n");
266 printf(
"Only ME 2.0 is supported by this code\n");
277 printf(
"Could not create the DLL loading mechanism(C-API test).\n");
290 printf(
"Everything seems to be OK since you got this far=)!\n");
FMILIB_EXPORT fmi2_status_t fmi2_import_get_derivatives(fmi2_import_t *fmu, fmi2_real_t derivatives[], size_t nx)
Wrapper for the FMI function fmiGetDerivatives(...)
FMILIB_EXPORT fmi2_status_t fmi2_import_enter_continuous_time_mode(fmi2_import_t *fmu)
Calls the FMI function fmiEnterContinuousTimeMode(...)
jm_calloc_f calloc
Allocate zero initialized memory.
unsigned int num_discrete
Number of discrete variables.
fmi2_boolean_t valuesOfContinuousStatesChanged
FMILIB_EXPORT fmi2_status_t fmi2_import_new_discrete_states(fmi2_import_t *fmu, fmi2_event_info_t *eventInfo)
Calls the FMI function fmiNewDiscreteStates(...)
FMILIB_EXPORT fmi2_status_t fmi2_import_enter_event_mode(fmi2_import_t *fmu)
Calls the FMI function fmiEnterEventMode(...)
FMILIB_EXPORT fmi2_status_t fmi2_import_set_time(fmi2_import_t *fmu, fmi2_real_t time)
Wrapper for the FMI function fmiSetTime(...)
FMILIB_EXPORT void fmi2_import_free_instance(fmi2_import_t *fmu)
Wrapper for the FMI function fmiFreeInstance(...)
int test_simulate_me(fmi2_import_t *fmu)
fmi_version_enu_t
Suported versions of FMI standard.
FMILIB_EXPORT fmi2_status_t fmi2_import_set_continuous_states(fmi2_import_t *fmu, const fmi2_real_t x[], size_t nx)
Wrapper for the FMI function fmiSetContinuousStates(...)
int test_parsed_all_varialbes(fmi2_import_t *fmu)
fmi2_callback_allocate_memory_ft allocateMemory
unsigned int num_continuous
Number of continuous variables.
FMILIB_EXPORT const char * fmi2_import_get_version(fmi2_import_t *fmu)
Wrapper for the FMI function fmiGetVersion()
FMILIB_EXPORT size_t fmi2_import_get_number_of_event_indicators(fmi2_import_t *fmu)
Get the number of event indicators.
FMILIB_EXPORT void fmi2_import_collect_model_counts(fmi2_import_t *fmu, fmi2_import_model_counts_t *counts)
Collect model information by counting the number of variables with specific properties and fillinf in...
FMILIB_EXPORT fmi2_import_t * fmi2_import_parse_xml(fmi_import_context_t *context, const char *dirPath, fmi2_xml_callbacks_t *xml_callbacks)
Create fmi2_import_t structure and parse the FMI 2.0 XML file found in the directory dirName...
fmi2_real_t nextEventTime
FMILIB_EXPORT fmi2_fmu_kind_enu_t fmi2_import_get_fmu_kind(fmi2_import_t *fmu)
Get the type of the FMU (model exchange or co-simulation)
fmi2_boolean_t terminateSimulation
jm_log_level_enu_t log_level
Logging level.
FMILIB_EXPORT fmi2_status_t fmi2_import_set_debug_logging(fmi2_import_t *fmu, fmi2_boolean_t loggingOn, size_t nCategories, fmi2_string_t categories[])
Wrapper for the FMI function fmiSetDebugLogging(...)
fmi2_boolean_t newDiscreteStatesNeeded
FMILIB_EXPORT void fmi2_import_destroy_dllfmu(fmi2_import_t *fmu)
Free a C-API struct. All memory allocated since the struct was created is freed.
FMILIB_EXPORT fmi2_status_t fmi2_import_get_event_indicators(fmi2_import_t *fmu, fmi2_real_t eventIndicators[], size_t ni)
Wrapper for the FMI function fmiGetEventIndicators(...)
FMILIB_EXPORT fmi2_status_t fmi2_import_exit_initialization_mode(fmi2_import_t *fmu)
Calls the FMI function fmiExitInitializationMode(...)
Collection of counters providing model information.
unsigned int num_fixed
Number of fixed.
fmi2_callback_logger_ft logger
Include file to be used in client applications of the FMI Library.
jm_malloc_f malloc
Allocate non-initialized memory.
fmi2_callback_free_memory_ft freeMemory
jm_voidp context
Arbitrary context pointer passed to the logger function.
fmi2_component_environment_t componentEnvironment
FMILIB_EXPORT fmi2_status_t fmi2_import_setup_experiment(fmi2_import_t *fmu, fmi2_boolean_t toleranceDefined, fmi2_real_t tolerance, fmi2_real_t startTime, fmi2_boolean_t stopTimeDefined, fmi2_real_t stopTime)
Calls the FMI function fmiSetupExperiment(...)
FMILIB_EXPORT jm_status_enu_t fmi2_import_create_dllfmu(fmi2_import_t *fmu, fmi2_fmu_kind_enu_t fmuKind, const fmi2_callback_functions_t *callBackFunctions)
Create a C-API struct. The C-API struct is a placeholder for the FMI DLL functions.
FMILIB_EXPORT void jm_default_logger(jm_callbacks *c, jm_string module, jm_log_level_enu_t log_level, jm_string message)
The default logger implementation prints messages to stderr.
The callbacks struct is sent to all the modules in the library.
FMILIB_EXPORT const char * fmi2_status_to_string(fmi2_status_t status)
void do_event_iteration(fmi2_import_t *fmu, fmi2_event_info_t *eventInfo)
fmi2_boolean_t nominalsOfContinuousStatesChanged
FMILIB_EXPORT fmi2_status_t fmi2_import_enter_initialization_mode(fmi2_import_t *fmu)
Calls the FMI function fmiEnterInitializationMode(...)
struct fmi_xml_context_t fmi_import_context_t
FMI version independent library context. Opaque struct returned from fmi_import_allocate_context() ...
FMILIB_EXPORT void fmi2_import_free(fmi2_import_t *fmu)
Release the memory allocated.
FMILIB_EXPORT fmi_import_context_t * fmi_import_allocate_context(jm_callbacks *callbacks)
Create fmi_import_context_t structure.
FMILIB_EXPORT const char * fmi2_import_get_types_platform(fmi2_import_t *fmu)
Wrapper for the FMI function fmiGetTypesPlatform(...)
unsigned int num_independent
Number of independent variables.
fmi2_boolean_t nextEventTimeDefined
FMILIB_EXPORT size_t fmi2_import_get_number_of_continuous_states(fmi2_import_t *fmu)
Get the number of continuous states.
FMILIB_EXPORT fmi2_status_t fmi2_import_completed_integrator_step(fmi2_import_t *fmu, fmi2_boolean_t noSetFMUStatePriorToCurrentPoint, fmi2_boolean_t *enterEventMode, fmi2_boolean_t *terminateSimulation)
Wrapper for the FMI function fmiCompletedIntegratorStep(...)
FMILIB_EXPORT fmi2_status_t fmi2_import_terminate(fmi2_import_t *fmu)
Wrapper for the FMI function fmiTerminate(...)
FMILIB_EXPORT fmi2_status_t fmi2_import_get_continuous_states(fmi2_import_t *fmu, fmi2_real_t states[], size_t nx)
Wrapper for the FMI function fmiGetContinuousStates(...)
FMILIB_EXPORT fmi_version_enu_t fmi_import_get_fmi_version(fmi_import_context_t *c, const char *fileName, const char *dirName)
Unzip an FMU specified by the fileName into directory dirName and parse XML to get FMI standard versi...
FMILIB_EXPORT void fmi_import_free_context(fmi_import_context_t *c)
Free memory allocated for the library context.
jm_realloc_f realloc
Re-allocate memory.
jm_status_enu_t
Return status codes.
FMILIB_EXPORT jm_status_enu_t fmi2_import_instantiate(fmi2_import_t *fmu, fmi2_string_t instanceName, fmi2_type_t fmuType, fmi2_string_t fmuResourceLocation, fmi2_boolean_t visible)
Wrapper for the FMI function fmiInstantiate(...)
int main(int argc, char *argv[])
jm_free_f free
Free-allocated memory.
jm_logger_f logger
Logging callback.
FMILIB_EXPORT void fmi2_log_forwarding(fmi2_component_t c, fmi2_string_t instanceName, fmi2_status_t status, fmi2_string_t category, fmi2_string_t message,...)
An implementation of FMI 2.0 logger that forwards the messages to logger function inside jm_callbacks...
unsigned int num_tunable
Number of tunable.
unsigned int num_constants
Number of constants.
struct fmi2_import_t fmi2_import_t
FMU version 2.0 object.