util.h 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. /* Utility header file for Dymosim FMI implementation. */
  2. #ifndef util_h
  3. #define util_h
  4. /* need to include first so that correct files are included */
  5. #include "conf.h"
  6. #include "types.h"
  7. #include <float.h>
  8. /* ----------------- macros ----------------- */
  9. /* Standard value reference */
  10. /* idemand: 31-28, category: 27-24, index: 23-0 */
  11. #define FMI_IDEMAND(valueRef) ((valueRef) >> 28)
  12. #define FMI_CATEGORY(valueRef) (((valueRef) & 0xf000000) >> 24)
  13. #define FMI_INDEX(valueRef) ((valueRef) & 0xffffff)
  14. /* Value reference for embedded systems */
  15. /* idemand: 31-28, restricted: 27, FMI type size: 26-23, index: 22-0 */
  16. // TODO: define macros
  17. #define MIN(A, B) ((A) <= (B) ? (A) : (B))
  18. #define SMALL_TIME_DEV(curtime) (4 * DBL_EPSILON * (1 + fabs(curtime)))
  19. #define LOG(c, status, msg) util_logger(c, c->instanceName, status, "", msg)
  20. #define HANDLE_STATUS_RETURN(status) return (status == FMIError) ? util_error(comp) : status;
  21. #define MAX_STRING_SIZE 500
  22. /*TODO support larger string sizes and allocate more memory if needed*/
  23. /* ------------------ function declarations ----------------- */
  24. /* logger wrapper for handling off enabled/disabled logging */
  25. DYMOLA_STATIC void util_logger(Component* comp, FMIString instanceName, FMIStatus status,
  26. FMIString category, FMIString message, ...);
  27. /* buffered variant used when line breaks should be omitted */
  28. DYMOLA_STATIC void util_buflogger(Component* comp, FMIString instanceName, FMIStatus status,
  29. FMIString category, FMIString message, ...);
  30. /* cannot use strdup since direct use of malloc not allowed */
  31. DYMOLA_STATIC FMIString util_strdup(const FMICallbackFunctions *functions, FMIString s);
  32. /* locally modified variant from Sundials to store in buffer instead of printing */
  33. DYMOLA_STATIC int util_check_flag(void *flagvalue, char *funcname, int opt, Component* comp);
  34. /* refresh variable values using dsblock_ */
  35. DYMOLA_STATIC int util_refresh_cache(Component* comp, int idemand, const char* label, FMIBoolean* iterationConverged);
  36. /* handle termination due to an error */
  37. DYMOLA_STATIC FMIStatus util_error(Component* comp);
  38. /* Initialize model, partly or completely, depending on argument "complete". */
  39. DYMOLA_STATIC FMIStatus util_initialize_model(FMIComponent c, FMIBoolean toleranceControlled, FMIReal relativeTolerance, FMIBoolean complete);
  40. /* Perform event iteration. */
  41. DYMOLA_STATIC FMIStatus util_event_update(FMIComponent c, FMIBoolean intermediateResults,
  42. #ifdef FMI_2
  43. /* needs another argument since not in eventInfo for FMI 2*/
  44. FMIBoolean* terminateSolution
  45. #else
  46. FMIEventInfo* eventInfo
  47. #endif
  48. );
  49. /* Initialize slave, partly or completely, depending on FMI version */
  50. DYMOLA_STATIC FMIStatus util_initialize_slave(FMIComponent c, FMIReal relativeTolerance, FMIReal tStart, FMIBoolean StopTimeDefined, FMIReal tStop);
  51. DYMOLA_STATIC void util_print_dymola_timers(FMIComponent c);
  52. #ifdef FMI_2
  53. /* Exit initialization mode. */
  54. DYMOLA_STATIC FMIStatus util_exit_model_initialization_mode(FMIComponent c, const char* label, ModelStatus nextStatus);
  55. /* --------------------------------------------------------------------------------------------------------
  56. API functions defined in earlier version of fmi 2.0 still in use internally
  57. ----------------------------------------------------------------------------------------------------------*/
  58. /* Creation and destruction of model instances and setting debug status */
  59. DYMOLA_STATIC FMIComponent fmiInstantiateModel_(FMIString, FMIString, FMIString, const FMICallbackFunctions*, FMIBoolean, FMIBoolean);
  60. DYMOLA_STATIC void fmiFreeModelInstance_(FMIComponent);
  61. /* Evaluation of the model equations */
  62. DYMOLA_STATIC FMIStatus fmiEnterModelInitializationMode_(FMIComponent, FMIBoolean, FMIReal);
  63. DYMOLA_STATIC FMIStatus fmiExitModelInitializationMode_(FMIComponent);
  64. DYMOLA_STATIC FMIStatus fmiTerminateModel_(FMIComponent);
  65. /* Creation and destruction of slave instances */
  66. DYMOLA_STATIC FMIComponent fmiInstantiateSlave_(FMIString, FMIString, FMIString, const FMICallbackFunctions*, FMIBoolean, FMIBoolean);
  67. DYMOLA_STATIC void fmiFreeSlaveInstance_(FMIComponent);
  68. /* Simulating the slave */
  69. DYMOLA_STATIC FMIStatus fmiEnterSlaveInitializationMode_(FMIComponent, FMIReal, FMIReal, FMIBoolean, FMIReal);
  70. DYMOLA_STATIC FMIStatus fmiExitSlaveInitializationMode_(FMIComponent);
  71. DYMOLA_STATIC FMIStatus fmiTerminateSlave_(FMIComponent);
  72. DYMOLA_STATIC FMIStatus fmiResetSlave_(FMIComponent);
  73. #endif
  74. #endif /* util_h */