types.h 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374
  1. /* Internal types for Dymosim FMI implementation. */
  2. #ifndef types_h
  3. #define types_h
  4. #include "conf.h"
  5. #include <stdlib.h>
  6. #define TIME_INFINITY 1e36
  7. #ifdef FMI_2
  8. #include "fmi2TypesPlatform.h"
  9. #include "fmi2FunctionTypes.h"
  10. #else
  11. #include "fmiPlatformTypes_.h"
  12. #include "fmiFunctions_.h"
  13. #endif
  14. #include "FMIversionPrefix.h"
  15. /* for storing of result from within FMU */
  16. #ifdef STORE_RESULT
  17. #include "GenerateResultInNonDymosim.h"
  18. #endif
  19. #ifdef NEW_CODE_GENERATION
  20. #include <dse_types.h>
  21. #endif
  22. #include "dsblock.h"
  23. #ifndef ONLY_INCLUDE_INLINE_INTEGRATION
  24. /* Sundials */
  25. #include <nvector/nvector_serial.h>
  26. #include <sundials/sundials_direct.h>
  27. #endif /* ONLY_INCLUDE_INLINE_INTEGRATION */
  28. /* ----------------- type definitions ----------------- */
  29. #ifndef FMI_2
  30. typedef char fmiChar; /*fmiChar not included in FMI 1, required for String allocation*/
  31. #endif
  32. typedef enum {
  33. iDemandStart, /* Start of integration: Initial equations are solved. */
  34. iDemandOutput, /* Compute outputs */
  35. iDemandDerivative, /* Compute derivatives */
  36. iDemandVariable, /* Compute auxiliary variables */
  37. iDemandCrossingFunction, /* Compute crossing functions */
  38. iDemandEventHandling, /* Event handling */
  39. iDemandTerminal = 7 /* 'terminal()' is true. */
  40. } IDemandLevel;
  41. typedef enum {
  42. modelInstantiated,
  43. modelInitializationMode, /*inside get macro and therfore required to be deifned for fmi1 */
  44. #ifdef FMI_2
  45. modelEventMode,
  46. modelEventMode2, /* event iteration ongoing */
  47. modelEventModeExit, /* event iteration finished */
  48. #endif
  49. modelContinousTimeMode,
  50. modelTerminated
  51. } ModelStatus;
  52. typedef enum {
  53. integrationOK,
  54. integrationError,
  55. integrationTerminate,
  56. integrationEvent
  57. } IntegrationStatus;
  58. typedef struct {
  59. /* Jacobian upper/lower bandwidth */
  60. int jacubw;
  61. int jaclbw;
  62. long steps;
  63. long intSteps;
  64. float psolveTime;
  65. float jacTimesVecTime;
  66. int JacCalls;
  67. int fCalls;
  68. float fTime;
  69. float gTime;
  70. float totTime;
  71. int rejSteps;
  72. int rejIntSteps;
  73. int rejFCalls;
  74. int rejJacCalls;
  75. float rejFTime;
  76. } Statistics;
  77. #ifdef SAVE_STATE_DSMODEL
  78. typedef struct {
  79. Dymola_bool *QL_;
  80. double *QRel_;
  81. double *QM_;
  82. double *Qn_, *Qp_;
  83. double *Qscaled_;
  84. double *QZold_;
  85. Dymola_bool *Qenable_;
  86. Dymola_bool *QTimed_;
  87. double *QCheckIf_;
  88. double *NextSampleTime_;
  89. double *NextSampleTimeNew_;
  90. Dymola_bool *NextSampleAct_;
  91. Dymola_bool *NextSampleActNew_;
  92. Dymola_bool *QEvaluate_;
  93. Dymola_bool *QEvaluateNew_;
  94. double *helpvar;
  95. double *oldQZ_, *oldQZDummy_;
  96. double *oldQZ2_;
  97. double *oldQZ3_;
  98. double *Aux_;
  99. int *QNLfunc;
  100. int *QNLjac;
  101. double *QPre_;
  102. double * *RefPre_;
  103. double *EqRemember1_;
  104. double *EqRemember2_;
  105. } DsmodelData;
  106. #endif /* SAVE_STATE_DSMODEL */
  107. #ifdef SAVE_STATE_SUNDIALS
  108. #include "../src/cvode/cvode_impl.h"
  109. /* internal data necessary to expose due to lack of state saving support in API */
  110. /* taken from CkpntMemRec */
  111. typedef struct {
  112. /* Nordsieck History Array */
  113. N_Vector ck_zn[L_MAX];
  114. /* Step data */
  115. long int ck_nst;
  116. realtype ck_tretlast;
  117. int ck_q;
  118. int ck_qmax;
  119. int ck_qprime;
  120. int ck_qwait;
  121. int ck_L;
  122. realtype ck_gammap;
  123. realtype ck_h;
  124. realtype ck_hprime;
  125. realtype ck_hscale;
  126. realtype ck_eta;
  127. realtype ck_etamax;
  128. realtype ck_tau[L_MAX+1];
  129. realtype ck_tq[NUM_TESTS+1];
  130. realtype ck_l[L_MAX];
  131. /* Saved values */
  132. realtype ck_saved_tq5;
  133. } CVodeCheckPointData;
  134. #endif
  135. typedef struct {
  136. #ifndef ONLY_INCLUDE_INLINE_INTEGRATION
  137. N_Vector y;
  138. void* cvode_mem;
  139. #ifdef SAVE_STATE_SUNDIALS
  140. int cvode_called;
  141. CVodeCheckPointData ck_mem;
  142. #endif /* SAVE_STATE_SUNDIALS */
  143. /* absolute tolerance */
  144. N_Vector abstol;
  145. /* error weight vector */
  146. N_Vector ewt;
  147. /* temporary space */
  148. N_Vector tmp1;
  149. N_Vector tmp2;
  150. DlsMat J;
  151. #endif /* ONLY_INCLUDE_INLINE_INTEGRATION */
  152. /* for input derivatve handling */
  153. FMIReal derivativeTime;
  154. FMIReal* inputDerivatives;
  155. FMIReal* inputsT0;
  156. int useDerivatives;
  157. /* for output derivatve handling */
  158. FMIReal* outputsPrev;
  159. FMIReal timePrev;
  160. /* statistics */
  161. Statistics stat;
  162. } IntegrationData;
  163. /* result storing on file */
  164. typedef struct {
  165. FMIReal time;
  166. FMIReal* parameters;
  167. FMIReal* states;
  168. FMIReal* derivatives;
  169. FMIReal* outputs;
  170. FMIReal* inputs;
  171. FMIReal* auxiliary;
  172. } ResultValues;
  173. typedef struct {
  174. #ifdef STORE_RESULT
  175. struct DeclarePhase phase;
  176. #endif
  177. /* last time for sample, possibly buffered */
  178. FMIReal lastSampleTime;
  179. /* last time for sample on file */
  180. FMIReal lastFileSampleTime;
  181. /* last neglected sample warning applies up to this time */
  182. FMIReal lastWarnTime;
  183. /* buffered values */
  184. ResultValues** buf;
  185. int curBufSize;
  186. int curStartIx;
  187. int curIx;
  188. int nUsed;
  189. /* whether have warned about forced flush */
  190. FMIBoolean flushWarned;
  191. } Result;
  192. #ifdef FMI_2
  193. /* for fmiGetDirectionalDerivative */
  194. typedef struct {
  195. int nJacA;
  196. int nJacB;
  197. int nJacC;
  198. int nJacD;
  199. double * jacA;
  200. double * jacB;
  201. double * jacC;
  202. double * jacD;
  203. size_t nJacV;
  204. size_t nJacZ;
  205. double* jacV;
  206. double* jacVTmp1;
  207. double* jacVTmp2;
  208. double* jacZ;
  209. double* jacZTmp1;
  210. double* jacZTmp2;
  211. } JacobianData;
  212. #endif /* FMI_2 */
  213. typedef struct {
  214. FMIReal tStart;
  215. FMIBoolean StopTimeDefined;
  216. FMIReal tStop;
  217. FMIBoolean relativeToleranceDefined;
  218. FMIReal relativeTolerance;
  219. FMIBoolean isCoSim;
  220. FMIString instanceName;
  221. FMIBoolean loggingOn;
  222. size_t nStates;
  223. size_t nIn;
  224. size_t nOut;
  225. size_t nAux;
  226. size_t nPar;
  227. size_t nSPar;
  228. size_t nCross;
  229. FMIReal* states;
  230. FMIReal* parameters;
  231. FMIReal* derivatives;
  232. FMIReal* inputs;
  233. FMIReal* outputs;
  234. FMIReal* auxiliary;
  235. FMIReal* crossingFunctions;
  236. FMIReal* statesNominal;
  237. FMIChar** sParameters;
  238. FMIChar** tsParameters;
  239. FMIReal* oldStates; /*To check if state values have changed durin event*/
  240. #ifdef SAVE_STATE_DSMODEL
  241. DsmodelData* dsModelData;
  242. #endif
  243. #ifdef NEW_CODE_GENERATION
  244. boolean_t boolVars;
  245. integer8_t int8Vars;
  246. uinteger8_t uint8Vars;
  247. integer16_t int16Vars;
  248. uinteger16_t uint16Vars;
  249. integer32_t int32Vars;
  250. uinteger32_t uint32Vars;
  251. integer64_t int64Vars;
  252. uinteger64_t uint64Vars;
  253. real_t realVars;
  254. #endif /* NEW_CODE_GENERATION */
  255. #ifdef FMI_2
  256. const FMICallbackFunctions* functions;
  257. #else
  258. FMICallbackFunctions functions;
  259. #endif
  260. FMIReal time;
  261. int icall;
  262. /* for buffering of short messages without linebreaks */
  263. char logbuf[256];
  264. char* logbufp;
  265. ModelStatus mStatus;
  266. /* only for FMI 2.0 really, but convenient internally also for 1.0 */
  267. FMIBoolean terminationByModel;
  268. /* for storing of result from within FMU */
  269. FMIBoolean storeResult;
  270. Result* result;
  271. struct BasicDDymosimStruct* dstruct;
  272. struct BasicIDymosimStruct* istruct;
  273. /* to avoid numerous castings */
  274. double* duser;
  275. int* iuser;
  276. /* for libdsdll */
  277. struct DymosimSimulator* sim;
  278. /* for internal integration */
  279. IntegrationData* iData;
  280. int allocDone;
  281. int valWasSet;
  282. int reinitRequired;
  283. int eventIterRequired;
  284. int recalJacobian;
  285. #ifdef FMI_2
  286. /* for fmiGetDirectionalDerivative */
  287. JacobianData jacData;
  288. FMIBoolean firstEventCall;
  289. #endif
  290. int eventIterationOnGoing;
  291. void**handles;
  292. int QiErr;
  293. struct DYNInstanceData* did;
  294. unsigned long long inlineStepCounter;
  295. } Component;
  296. #ifndef DYMOLA_TIMES
  297. #define DYMOLA_TIMES
  298. struct DymolaTimes {
  299. int num;
  300. double maxim;
  301. double minim;
  302. double total;
  303. int numAccum;
  304. double maximAccum;
  305. double minimAccum;
  306. double totalAccum;
  307. const char*name;
  308. };
  309. #endif
  310. #endif /* types_h */