fmiCoSimFunctions_int.c 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923
  1. /* The generic implementation of the FMI co-simulation interface. */
  2. #ifndef FMU_SKIP_CO_SIM
  3. /* need to include first so that correct files are included */
  4. #include "conf.h"
  5. #include "util.h"
  6. #ifndef ONLY_INCLUDE_INLINE_INTEGRATION
  7. #include "integration.h"
  8. #endif
  9. #include "result.h"
  10. #include "fmiFunctions_fwd.h"
  11. #include "adymosim.h"
  12. #include <math.h>
  13. #include <assert.h>
  14. #ifndef DYMOLA_STATIC_IMPORT
  15. #define DYMOLA_STATIC_IMPORT DYMOLA_STATIC
  16. #endif
  17. /* ----------------- macros ----------------- */
  18. #ifdef FMI_2
  19. #define CS_RESULT_SAMPLE(atEvent) \
  20. if (comp->storeResult == FMITrue) { \
  21. result_sample(comp, atEvent); \
  22. if (noSetFMUStatePriorToCurrentPoint == FMITrue) { \
  23. result_flush(comp); \
  24. } \
  25. }
  26. #else
  27. #define CS_RESULT_SAMPLE(atEvent) \
  28. if (comp->storeResult == FMITrue) { \
  29. result_sample(comp, atEvent); \
  30. }
  31. #endif
  32. /* ----------------- local function declarations ----------------- */
  33. static FMIStatus handleEvent(FMIComponent c);
  34. /* ----------------- local variables ----------------- */
  35. /* when compiling as a single complilation unit, this is already defined */
  36. #ifndef FMU_SOURCE_SINGLE_UNIT
  37. extern Component* globalComponent2;
  38. #endif
  39. /* ----------------- function definitions ----------------- */
  40. /* For 2.0 this is replaced by a function common for both ME and Co-sim. */
  41. #ifndef FMI_2
  42. DYMOLA_STATIC const char* fmiGetTypesPlatform_()
  43. {
  44. return fmiPlatform;
  45. }
  46. #endif
  47. /* ---------------------------------------------------------------------- */
  48. #ifdef FMI_2
  49. DYMOLA_STATIC FMIComponent fmiInstantiateSlave_(FMIString instanceName,
  50. FMIString fmuGUID,
  51. FMIString fmuResourceLocation,
  52. const FMICallbackFunctions* functions,
  53. FMIBoolean visible,
  54. FMIBoolean loggingOn)
  55. {
  56. static FMIString label = "fmiInstantiateSlave";
  57. Component* comp = fmiInstantiateModel_(instanceName, fmuGUID, fmuResourceLocation, functions, visible, loggingOn);
  58. /* ignore argument "visible" since we never provide any GUI */
  59. if (comp == NULL) {
  60. return NULL;
  61. }
  62. if (functions->stepFinished != NULL) {
  63. util_logger(comp, comp->instanceName, FMIWarning, "",
  64. "fmiInstantiateSlave: Callback function stepFinished != NULL but asynchronous fmiDoStep is not supported");
  65. }
  66. util_logger(comp, comp->instanceName, FMIOK, "", "%s", label);
  67. return comp;
  68. }
  69. #else
  70. FMIComponent fmiInstantiateSlave_(FMIString instanceName,
  71. FMIString fmuGUID,
  72. FMIString fmuLocation,
  73. FMIString mimeType,
  74. FMIReal timeout,
  75. FMIBoolean visible,
  76. FMIBoolean interactive,
  77. fmiCoSimCallbackFunctions functions,
  78. FMIBoolean loggingOn)
  79. {
  80. static FMIString label = "fmiInstantiateSlave";
  81. Component* comp;
  82. static fmiMECallbackFunctions meFunctions;
  83. meFunctions.logger = functions.logger;
  84. meFunctions.allocateMemory = functions.allocateMemory;
  85. meFunctions.freeMemory = functions.freeMemory;
  86. /* Since we don't support tool coupling, the following arguments are ignored:
  87. fmuLocation, mimeType */
  88. comp = (Component*) fmiInstantiateModel_(instanceName, fmuGUID, meFunctions, loggingOn);
  89. if (comp == NULL) {
  90. return NULL;
  91. }
  92. if (functions.stepFinished != NULL) {
  93. util_logger(comp, comp->instanceName, FMIWarning, "",
  94. "fmiInstantiateSlave: Callback function stepFinished != NULL but asynchronous fmiDoStep is not supported");
  95. }
  96. if (timeout > 0) {
  97. util_logger(comp, comp->instanceName, FMIWarning, "", "fmiInstantiateSlave: timeout != 0 is not supported");
  98. }
  99. /* ignore argument "visible" since we never provide any GUI */
  100. if (interactive) {
  101. util_logger(comp, comp->instanceName, FMIWarning, "", "fmiInstantiateSlave: interactive not supported");
  102. }
  103. util_logger(comp, comp->instanceName, FMIOK, "", "%s", label);
  104. return comp;
  105. }
  106. #endif /* FMI_2 */
  107. /* ---------------------------------------------------------------------- */
  108. DYMOLA_STATIC void fmiFreeSlaveInstance_(FMIComponent c)
  109. {
  110. fmiFreeModelInstance_(c);
  111. }
  112. #ifdef FMI_2
  113. /* ---------------------------------------------------------------------- */
  114. DYMOLA_STATIC FMIStatus fmiEnterSlaveInitializationMode_(FMIComponent c,
  115. FMIReal relativeTolerance,
  116. FMIReal tStart,
  117. FMIBoolean StopTimeDefined,
  118. FMIReal tStop)
  119. {
  120. static FMIString label = "fmiEnterSlaveInitializationMode";
  121. Component* comp = (Component*) c;
  122. FMIStatus status;
  123. util_logger(comp, comp->instanceName, FMIOK, "", "%s...", label);
  124. status = util_initialize_slave(c, relativeTolerance, tStart, StopTimeDefined, tStop);
  125. if (status != FMIOK) {
  126. return status;
  127. }
  128. comp->mStatus = modelInitializationMode;
  129. comp->inlineStepCounter = 0;
  130. util_logger(comp, comp->instanceName, FMIOK, "", "%s completed", label);
  131. return status;
  132. }
  133. /* ---------------------------------------------------------------------- */
  134. FMIStatus fmiExitSlaveInitializationMode_(FMIComponent c)
  135. {
  136. static FMIString label = "fmiExitSlaveInitializationMode";
  137. Component* comp = (Component*) c;
  138. FMIStatus status;
  139. util_logger(comp, comp->instanceName, FMIOK, "", "%s...", label);
  140. status = util_exit_model_initialization_mode(c, label, modelContinousTimeMode);
  141. if (status != FMIOK) {
  142. return status;
  143. }
  144. comp->mStatus = modelContinousTimeMode;
  145. util_logger(comp, comp->instanceName, FMIOK, "", "%s completed", label);
  146. return FMIOK;
  147. }
  148. #else /* FMI_2 */
  149. /* ---------------------------------------------------------------------- */
  150. DYMOLA_STATIC_IMPORT const double cvodeTolerance;
  151. DYMOLA_STATIC FMIStatus fmiInitializeSlave_(FMIComponent c,
  152. FMIReal tStart,
  153. FMIBoolean StopTimeDefined,
  154. FMIReal tStop)
  155. {
  156. static FMIString label = "fmiInitializeSlave";
  157. Component* comp = (Component*) c;
  158. FMIStatus status;
  159. comp->isCoSim=FMITrue;
  160. util_logger(comp, comp->instanceName, FMIOK, "", "%s...", label);
  161. #ifndef ONLY_INCLUDE_INLINE_INTEGRATION
  162. if(!comp->istruct->mInlineIntegration){
  163. util_logger(comp, comp->instanceName, FMIOK, "", "%s cvode tolerance is %g", label, cvodeTolerance);
  164. }
  165. #endif
  166. status = util_initialize_slave(c, cvodeTolerance, tStart, StopTimeDefined, tStop);
  167. if (status != FMIOK) {
  168. return status;
  169. }
  170. comp->mStatus = modelContinousTimeMode;
  171. util_logger(comp, comp->instanceName, FMIOK, "", "%s completed", label);
  172. return status;
  173. }
  174. #endif /* FMI_2 */
  175. /* ---------------------------------------------------------------------- */
  176. extern void delayBuffersClose(void);
  177. extern void delayBuffersCloseNew(struct DYNInstanceData*);
  178. DYMOLA_STATIC FMIStatus fmiTerminateSlave_(FMIComponent c)
  179. {
  180. #ifdef FMI_2
  181. static FMIString label = "fmi2Terminate";
  182. #else
  183. static FMIString label = "fmiTerminateSlave";
  184. #endif
  185. Component* comp = (Component*) c;
  186. FMIStatus status =FMIOK;
  187. FMIStatus status2 = FMIOK;
  188. util_logger(comp, comp->instanceName, FMIOK, "", "%s...", label);
  189. if (comp->mStatus == modelTerminated) {
  190. util_logger(comp, comp->instanceName, FMIWarning, "", "%s: already terminated, ignoring call", label);
  191. return FMIWarning;
  192. }
  193. if(comp->QiErr == 0 && comp->terminationByModel == FMIFalse){
  194. /*Special case for terminal, call dsblock_ directly instead of
  195. using util_refresh_cache to avoid messy logic*/
  196. int terminal = iDemandTerminal;
  197. if (comp->did) {
  198. globalComponent2=comp;
  199. dsblock_tid(&terminal, &comp->icall, &comp->time, comp->states, 0,
  200. comp->inputs, comp->parameters, 0, 0, comp->derivatives,
  201. comp->outputs, comp->auxiliary,
  202. comp->crossingFunctions, comp->duser, comp->iuser,
  203. (void**) comp->sParameters, comp->did, &comp->QiErr, 0);
  204. globalComponent2=0;
  205. } else {
  206. dsblock_(&terminal, &comp->icall, &comp->time, comp->states, 0,
  207. comp->inputs, comp->parameters, 0, 0, comp->derivatives,
  208. comp->outputs, comp->auxiliary,
  209. comp->crossingFunctions, comp->duser, comp->iuser,
  210. (void**) comp->sParameters, &comp->QiErr);
  211. }
  212. if (comp->QiErr>=-995 && comp->QiErr<=-990) comp->QiErr=0; /* Ignore special cases for now */
  213. if(!(comp->QiErr == 0 || comp->QiErr==-999)){
  214. status = FMIError;
  215. util_logger(comp, comp->instanceName, FMIError, "",
  216. "%s: calling terminal section of dsblock_ failed, QiErr = %d",
  217. label,comp->QiErr);
  218. }
  219. }
  220. #ifndef ONLY_INCLUDE_INLINE_INTEGRATION
  221. if(!comp->istruct->mInlineIntegration){
  222. integration_teardown(c);
  223. }
  224. #endif
  225. util_print_dymola_timers(c);
  226. #ifndef FMU_SOURCE_CODE_EXPORT
  227. if (comp->storeResult == FMITrue) {
  228. result_teardown(comp);
  229. }
  230. #endif /* FMU_SOURCE_CODE_EXPORT */
  231. if (comp->did) {
  232. delayBuffersCloseNew(comp->did);
  233. } else {
  234. delayBuffersClose();
  235. }
  236. comp->mStatus = modelTerminated;
  237. util_logger(comp, comp->instanceName, FMIOK, "", "%s completed", label);
  238. return status;
  239. }
  240. extern void declare_(double*, double*, double*, void**,int*);
  241. DYMOLA_STATIC FMIStatus fmiResetSlave_(FMIComponent c)
  242. {
  243. #ifdef FMI_2
  244. static FMIString label = "fmiReset";
  245. #else
  246. static FMIString label = "fmiResetSlave";
  247. #endif
  248. Component* comp = (Component*) c;
  249. FMIStatus status = FMIOK;
  250. int QiErr = 0;
  251. size_t i;
  252. util_logger(comp, comp->instanceName, FMIOK, "", "%s...", label);
  253. switch (comp->mStatus) {
  254. case modelContinousTimeMode:
  255. case modelInitializationMode:
  256. #ifdef FMI_2
  257. case modelEventMode:
  258. case modelEventMode2:
  259. case modelEventModeExit:
  260. #endif
  261. if(comp->isCoSim){
  262. status = fmiTerminateSlave_(c);
  263. #ifdef FMI_2
  264. }else{
  265. status = fmiTerminateModel_(c);
  266. #endif
  267. }
  268. if (status != FMIOK && status != FMIWarning) {
  269. return status;
  270. }
  271. /* fall-through */
  272. case modelTerminated:
  273. /* reset states and parameters */
  274. if(comp->tsParameters == NULL && comp->nSPar>0){
  275. #ifdef FMI_2
  276. comp->tsParameters = (FMIChar**) comp->functions->allocateMemory(comp->nSPar, sizeof(FMIChar*));
  277. #else
  278. comp->tsParameters = (FMIChar**) comp->functions.allocateMemory(comp->nSPar, sizeof(FMIChar*));
  279. #endif
  280. if (comp->tsParameters == NULL) {
  281. util_logger(comp, comp->instanceName, FMIFatal, "", "%s failed: out of Memory", label);
  282. fmiFreeModelInstance_(comp);
  283. }
  284. }
  285. declare_(comp->states, comp->parameters, comp->inputs, (void**) comp->tsParameters, &QiErr);
  286. for(i=0; i < comp->nSPar; ++i){
  287. size_t len;
  288. len=strlen(comp->tsParameters[i]);
  289. if (len>MAX_STRING_SIZE) len=MAX_STRING_SIZE;
  290. memcpy((comp->sParameters)[i], comp->tsParameters[i], len+1);
  291. (comp->sParameters)[i][MAX_STRING_SIZE]='\0';
  292. }
  293. /*Clear aux and outputs to handle cases when dsmodel assumes default 0 start value*/
  294. for(i = 0; i < comp->nAux; ++i){
  295. comp->auxiliary[i] = 0;
  296. }
  297. for(i = 0; i < comp->nOut; ++i){
  298. comp->outputs[i]=0;
  299. }
  300. if (QiErr != 0) {
  301. util_logger(comp, comp->instanceName, FMIError, "",
  302. "%s: declare_ failed, QiErr = %d", label, QiErr);
  303. return util_error(comp);
  304. }
  305. comp->mStatus = modelInstantiated;
  306. comp->recalJacobian = 1;
  307. break;
  308. case modelInstantiated:
  309. util_logger(comp, comp->instanceName, FMIOK, "", "%s: already reset, ignoring call", label);
  310. return FMIOK;
  311. }
  312. util_logger(comp, comp->instanceName, status, "", "%s completed", label);
  313. return status;
  314. }
  315. /* ---------------------------------------------------------------------- */
  316. DYMOLA_STATIC FMIStatus fmiSetRealInputDerivatives_(FMIComponent c,
  317. const FMIValueReference vr[],
  318. size_t nvr,
  319. const FMIInteger order[],
  320. const FMIReal value[])
  321. {
  322. static FMIString label = "fmiSetRealInputDerivatives";
  323. Component* comp = (Component*) c;
  324. #ifdef ONLY_INCLUDE_INLINE_INTEGRATION
  325. util_logger(comp, comp->instanceName, FMIWarning, "", "fmiSetRealInputDerivatives is not suported with inline integration");
  326. return FMIWarning;
  327. #else
  328. size_t nu = comp->nIn;
  329. size_t i;
  330. if(comp->istruct->mInlineIntegration){
  331. util_logger(comp, comp->instanceName, FMIWarning, "", "fmiSetRealInputDerivatives is not suported with inline integration");
  332. return FMIWarning;
  333. }
  334. if( comp->mStatus == modelInstantiated){
  335. util_logger(comp, comp->instanceName, FMIWarning, "",
  336. #ifdef FMI_2
  337. "%s: fmiEnterInitializationMode must be called before calling %s", label, label);
  338. #else
  339. "%s: fmiInitializeSlave must be called before calling %s", label, label);
  340. #endif
  341. return FMIWarning;
  342. }
  343. if (nvr == 0 || nvr > nu) {
  344. util_logger(comp, comp->instanceName, FMIWarning, "",
  345. "%s: invalid nvr = %d (number of inputs = %d), ignoring call", label, nvr, nu);
  346. return FMIWarning;
  347. }
  348. for (i = 0; i < nvr; i++) {
  349. const FMIValueReference r = vr[i];
  350. int ix = FMI_INDEX(r);
  351. if (order[i] == 1) {
  352. switch (FMI_CATEGORY(r)) {
  353. case dsInput:
  354. comp->iData->inputDerivatives[ix] = value[i];
  355. break;
  356. default:
  357. util_logger(comp, comp->instanceName, FMIWarning, "", "%s: variable is not input", label);
  358. return FMIWarning;
  359. }
  360. } else {
  361. util_logger(comp, comp->instanceName, FMIWarning, "",
  362. "%s: derivative order %d is not supported", order[i], label);
  363. return FMIWarning;
  364. }
  365. }
  366. memcpy(comp->iData->inputsT0, comp->inputs, comp->nIn * sizeof(FMIReal));
  367. comp->iData->derivativeTime = comp->time;
  368. comp->iData->useDerivatives = 1;
  369. util_logger(comp, comp->instanceName, FMIOK, "", "%s", label);
  370. return FMIOK;
  371. #endif /* ONLY_INCLUDE_INLINE_INTEGRATION */
  372. }
  373. /* ---------------------------------------------------------------------- */
  374. DYMOLA_STATIC FMIStatus fmiGetRealOutputDerivatives_(FMIComponent c,
  375. const FMIValueReference vr[],
  376. size_t nvr,
  377. const FMIInteger order[],
  378. FMIReal value[])
  379. {
  380. static FMIString label = "fmiGetRealOutputDerivatives";
  381. Component* comp = (Component*) c;
  382. #ifdef ONLY_INCLUDE_INLINE_INTEGRATION
  383. util_logger(comp, comp->instanceName, FMIWarning, "", "fmiGetRealOutputDerivatives is not suported with inline integration");
  384. return FMIWarning;
  385. #else
  386. IntegrationData* iData = comp->iData;
  387. FMIReal dt = comp->time - iData->timePrev;
  388. size_t ny = comp->nOut;
  389. size_t i;
  390. if(comp->istruct->mInlineIntegration){
  391. util_logger(comp, comp->instanceName, FMIWarning, "", "fmiGetRealOutputDerivatives is not suported with inline integration");
  392. return FMIWarning;
  393. }
  394. if( comp->mStatus == modelInstantiated){
  395. util_logger(comp, comp->instanceName, FMIWarning, "",
  396. #ifdef FMI_2
  397. "%s: fmiEnterInitializationMode must be called before calling %s", label, label);
  398. #else
  399. "%s: fmiInitializeSlave must be called before calling %s", label, label);
  400. #endif
  401. return FMIWarning;
  402. }
  403. if (nvr == 0 || nvr > ny) {
  404. util_logger(comp, comp->instanceName, FMIWarning, "",
  405. "%s: invalid nvr = %d (number of outputs = %d)", label, nvr, ny);
  406. return FMIWarning;
  407. }
  408. /* fetch new output values */
  409. if (util_refresh_cache(comp, iDemandOutput, label, NULL)) {
  410. return util_error(comp);
  411. }
  412. for (i = 0; i < nvr; i++) {
  413. const FMIValueReference r = vr[i];
  414. int ix = FMI_INDEX(r);
  415. if (order[i] == 1) {
  416. switch (FMI_CATEGORY(r)) {
  417. case dsOutput:
  418. if (dt <= 0) {
  419. util_logger(comp, comp->instanceName, FMIWarning, "",
  420. "%s: time interval for estimate is %f, returning 0", label, dt);
  421. value[i] = 0;
  422. }
  423. value[i] = (comp->outputs[ix] - iData->outputsPrev[ix]) / dt;
  424. break;
  425. default:
  426. util_logger(comp, comp->instanceName, FMIWarning, "", "%s: variable is not output", label);
  427. return FMIWarning;
  428. break;
  429. }
  430. } else if (order[i] > 1) {
  431. value[i] = 0;
  432. } else {
  433. util_logger(comp, comp->instanceName, FMIWarning, "", "%s: derivative order 0 is not allowed", label);
  434. return FMIWarning;
  435. }
  436. }
  437. util_logger(comp, comp->instanceName, FMIOK, "", "%s", label);
  438. return FMIOK;
  439. #endif /* ONLY_INCLUDE_INLINE_INTEGRATION */
  440. }
  441. /* ---------------------------------------------------------------------- */
  442. DYMOLA_STATIC
  443. #ifdef FMI_2
  444. FMIStatus fmiDoStep_(FMIComponent c, FMIReal currentCommunicationPoint, FMIReal communicationStepSize,
  445. FMIBoolean noSetFMUStatePriorToCurrentPoint)
  446. #else
  447. FMIStatus fmiDoStep_(FMIComponent c, FMIReal currentCommunicationPoint, FMIReal communicationStepSize,
  448. FMIBoolean newStep)
  449. #endif
  450. {
  451. static FMIString label = "fmiDoStep";
  452. Component* comp = (Component*) c;
  453. int QiErr = 0;
  454. FMIStatus status;
  455. FMIReal endStepTime;
  456. FMIBoolean finalTimeEventCalled = FMIFalse;
  457. FMIReal tdev = fabs(comp->time - currentCommunicationPoint);
  458. if(comp->mStatus == modelInstantiated || comp->mStatus== modelInitializationMode){
  459. util_logger(comp, comp->instanceName, FMIWarning, "",
  460. "fmiDoStep: Initialization must be finished before calling fmiDoStep", currentCommunicationPoint, comp->time);
  461. return FMIWarning;
  462. }
  463. /* check if reasonably close to expected time */
  464. if (!comp->istruct->mInlineIntegration ? tdev > 0 : tdev > 1.5*comp->dstruct->mDymolaFixedStepSize) {
  465. if (tdev >= SMALL_TIME_DEV(comp->time)) {
  466. util_logger(comp, comp->instanceName, FMIWarning, "",
  467. "fmiDoStep: currentCommunicationPoint = %.16f, expected %.16f", currentCommunicationPoint, comp->time);
  468. return FMIWarning;
  469. }
  470. /* compensate by adjusting step size, unless event iteration request */
  471. /*no longer since current comuncation point and not internal time is used to calculate end time*/
  472. #if 0
  473. if (communicationStepSize != 0) {
  474. FMIReal dh = currentCommunicationPoint - comp->time;
  475. #ifdef FMU_VERBOSITY
  476. util_logger(comp, comp->instanceName, FMIOK, "",
  477. "t=%f: Adjusting communicationStepSize with %.16e s.\n", comp->time, dh);
  478. #endif
  479. communicationStepSize += dh;
  480. }
  481. #endif
  482. }
  483. if (comp->mStatus == modelTerminated || comp->terminationByModel == FMITrue) {
  484. util_logger(comp, comp->instanceName, FMIWarning, "", "fmiDoStep: model terminated in previous step");
  485. return FMIWarning;
  486. }
  487. if (communicationStepSize == 0) {
  488. return handleEvent(c);
  489. }
  490. #ifndef FMI_2
  491. if (newStep == FMIFalse) {
  492. util_logger(comp, comp->instanceName, FMIWarning, "", "fmiDoStep: newStep = fmiFalse not supported");
  493. return FMIWarning;
  494. }
  495. #endif
  496. /*endStepTime = comp->time + communicationStepSize;*/
  497. endStepTime = currentCommunicationPoint + communicationStepSize;
  498. if (comp->StopTimeDefined) {
  499. /* check if reasonably within stop time */
  500. tdev = endStepTime - comp->tStop;
  501. if (tdev > 0) {
  502. /* allow twice as much since we might have adjusted communicationStepSize already */
  503. if (tdev > 2 * SMALL_TIME_DEV(comp->tStop)) {
  504. util_logger(comp, comp->instanceName, FMIWarning, "",
  505. "fmiDoStep: trying to compute past tStop = %f, to %.16f, at t = %f", "", comp->tStop, endStepTime, comp->time);
  506. return FMIWarning;
  507. }
  508. /* adjust end time */
  509. util_logger(comp, comp->instanceName, FMIOK, "",
  510. "t = %f: Reducing communicationStepSize %.16e s to avoid passing tStop.\n", comp->time, tdev);
  511. endStepTime = comp->tStop;
  512. }
  513. }
  514. /* handle any values changed externally */
  515. if (comp->valWasSet) {
  516. FMIStatus status = handleEvent(c);
  517. comp->valWasSet = 0;
  518. if (status != FMIOK) {
  519. return status;
  520. }
  521. }
  522. /* check if re-computing derivatives necessary */
  523. else if (comp->icall < iDemandDerivative) {
  524. int QiErr = util_refresh_cache(comp, iDemandDerivative, "fmiDoStep", NULL);
  525. if (QiErr != 0) {
  526. return (QiErr == 1) ? FMIDiscard : util_error(comp);
  527. }
  528. }
  529. #ifndef ONLY_INCLUDE_INLINE_INTEGRATION
  530. if(!comp->istruct->mInlineIntegration){
  531. /* integration should have been setup by now */
  532. assert(comp->iData != NULL);
  533. /* Time events can be predicted/handled outside integration steps while state/step events must be
  534. detected within (since we cannot predict their time). Step events are OK to handle after
  535. each step. */
  536. while (comp->time < endStepTime || endStepTime == comp->dstruct->mNextTimeEvent && !finalTimeEventCalled ) {
  537. FMIBoolean callEventUpdate = FMIFalse;
  538. if (comp->time < comp->dstruct->mNextTimeEvent) {
  539. FMIReal nextTime = MIN(endStepTime, comp->dstruct->mNextTimeEvent);
  540. /* integrate */
  541. switch(integration_step(c, nextTime)) {
  542. case integrationOK:
  543. break;
  544. case integrationTerminate:
  545. status = FMIDiscard;
  546. util_logger(comp, comp->instanceName, status, "", "fmiDoStep: simulation terminated by model");
  547. comp->terminationByModel = FMITrue;
  548. return status;
  549. case integrationError:
  550. util_logger(comp, comp->instanceName, FMIError, "", "fmiDoStep: integration_step failed");
  551. return util_error(comp);
  552. }
  553. fmiSetTime_(c, nextTime);
  554. /* compute approximate inputs */
  555. integration_extrapolate_inputs(comp, nextTime);
  556. /* check step events in case they occurred at the very end time of step */
  557. if (comp->istruct->mTriggerStepEvent != 0) {
  558. callEventUpdate = FMITrue;
  559. #ifdef LOG_EVENTS
  560. util_logger(c, comp->instanceName, FMIOK, "", "%.12f: step event", comp->time);
  561. #endif
  562. }
  563. } else {
  564. fmiSetTime_(c, comp->dstruct->mNextTimeEvent);
  565. if(comp->time == endStepTime){
  566. finalTimeEventCalled=FMITrue;
  567. }
  568. callEventUpdate = FMITrue;
  569. #ifdef LOG_EVENTS
  570. util_logger(c, comp->instanceName, fmiOK, "", "%.12f: time event", comp->time);
  571. #endif
  572. }
  573. if (callEventUpdate) {
  574. FMIStatus status;
  575. CS_RESULT_SAMPLE(FMITrue);
  576. status = handleEvent(c);
  577. if (status != FMIOK) {
  578. return status;
  579. }
  580. CS_RESULT_SAMPLE(FMITrue);
  581. }
  582. }
  583. comp->time = endStepTime;
  584. CS_RESULT_SAMPLE(FMITrue);
  585. comp->iData->useDerivatives = 0;
  586. }else{
  587. #endif /* ndef ONLY_INCLUDE_INLINE_INTEGRATION */
  588. double stepTime = 0;
  589. double h = comp->dstruct->mDymolaFixedStepSize;
  590. unsigned long long startStepCount = comp->inlineStepCounter;
  591. while( (stepTime=(++comp->inlineStepCounter)*h) < endStepTime+0.1*h){
  592. status = fmiSetTime_(c, stepTime);
  593. if(status!=FMIOK){
  594. return status;
  595. }
  596. status = handleEvent(c);
  597. if (status != FMIOK) {
  598. return status;
  599. }
  600. }
  601. /*Remove last step counter increase that failed while statement*/
  602. --comp->inlineStepCounter;
  603. if(startStepCount != comp->inlineStepCounter){
  604. CS_RESULT_SAMPLE(FMITrue);
  605. }
  606. #ifndef ONLY_INCLUDE_INLINE_INTEGRATION
  607. }
  608. #endif
  609. util_logger(comp, comp->instanceName, FMIOK, "", "%s", label);
  610. return FMIOK;
  611. }
  612. #ifdef FMI_2
  613. DYMOLA_STATIC FMIStatus fmiHybridDoStep_(FMIComponent c, FMIReal currentCommunicationPoint, FMIReal communicationStepSize,
  614. FMIBoolean noSetFMUStatePriorToCurrentPoint, FMIBoolean *encounteredEvent, FMIReal *endTime)
  615. {
  616. #ifndef ONLY_INCLUDE_INLINE_INTEGRATION
  617. Component* comp = (Component*) c;
  618. int QiErr = 0;
  619. FMIStatus status;
  620. FMIReal endStepTime;
  621. FMIReal tdev = fabs(comp->time - currentCommunicationPoint);
  622. /* check if reasonably close to expected time */
  623. if (tdev > 0) {
  624. if (tdev >= SMALL_TIME_DEV(comp->time)) {
  625. util_logger(comp, comp->instanceName, FMIDiscard, "",
  626. "fmiDoStep: currentCommunicationPoint = %.16f, expected %.16f", currentCommunicationPoint, comp->time);
  627. return FMIDiscard;
  628. }
  629. /* compensate by adjusting step size, unless event iteration request */
  630. if (communicationStepSize != 0) {
  631. FMIReal dh = currentCommunicationPoint - comp->time;
  632. #ifdef FMU_VERBOSITY
  633. util_logger(comp, comp->instanceName, FMIOK, "",
  634. "t=%f: Adjusting communicationStepSize with %.16e s.\n", comp->time, dh);
  635. #endif
  636. communicationStepSize += dh;
  637. }
  638. }
  639. if (comp->mStatus == modelTerminated || comp->terminationByModel == FMITrue) {
  640. util_logger(comp, comp->instanceName, FMIWarning, "", "fmiDoStep: model terminated in previous step");
  641. return FMIWarning;
  642. }
  643. if (communicationStepSize == 0) {
  644. return handleEvent(c);
  645. /*TODO new event handling at 0 step?*/
  646. }
  647. endStepTime = comp->time + communicationStepSize;
  648. if (comp->StopTimeDefined) {
  649. /* check if reasonably within stop time */
  650. tdev = endStepTime - comp->tStop;
  651. if (tdev > 0) {
  652. /* allow twice as much since we might have adjusted communicationStepSize already */
  653. if (tdev > 2 * SMALL_TIME_DEV(comp->tStop)) {
  654. util_logger(comp, comp->instanceName, FMIWarning, "",
  655. "fmiDoStep: trying to compute past tStop = %f, to %.16f, at t = %f", "", comp->tStop, endStepTime, comp->time);
  656. return FMIWarning;
  657. }
  658. /* adjust end time */
  659. util_logger(comp, comp->instanceName, FMIOK, "",
  660. "t = %f: Reducing communicationStepSize %.16e s to avoid passing tStop.\n", comp->time, tdev);
  661. endStepTime = comp->tStop;
  662. }
  663. }
  664. /* handle any values changed externally */
  665. if (comp->valWasSet || comp->eventIterRequired) {
  666. //fmiStatus status = handleEvent(c);
  667. *encounteredEvent=FMITrue;
  668. *endTime = comp->time;
  669. return FMIOK;
  670. }
  671. /* handle any values changed externally */
  672. if (comp->eventIterRequired) {
  673. //fmiStatus status = handleEvent(c);
  674. comp->reinitRequired = 0;
  675. if (integration_reinit(comp) != integrationOK) {
  676. return util_error(comp);
  677. }
  678. }
  679. /* check if re-computing derivatives necessary */
  680. else if (comp->icall < iDemandDerivative) {
  681. int QiErr = util_refresh_cache(comp, iDemandDerivative, "fmiDoStep", NULL);
  682. if (QiErr != 0) {
  683. return (QiErr == 1) ? FMIDiscard : util_error(comp);
  684. }
  685. }
  686. /* integration should have been setup by now */
  687. assert(comp->iData != NULL);
  688. /* Time events can be predicted/handled outside integration steps while state/step events must be
  689. detected within (since we cannot predict their time). Step events are OK to handle after
  690. each step. */
  691. if (comp->time < comp->dstruct->mNextTimeEvent) {
  692. FMIReal nextTime = MIN(endStepTime, comp->dstruct->mNextTimeEvent);
  693. /* integrate */
  694. switch(integration_step_return_at_event(c, nextTime, endTime)) {
  695. case integrationOK:
  696. break;
  697. case integrationTerminate:
  698. status = FMIDiscard;
  699. util_logger(comp, comp->instanceName, status, "", "fmiDoStep: simulation terminated by model");
  700. comp->terminationByModel = FMITrue;
  701. return status;
  702. case integrationError:
  703. util_logger(comp, comp->instanceName, FMIError, "", "fmiDoStep: integration_step failed");
  704. return util_error(comp);
  705. case integrationEvent:
  706. *encounteredEvent=FMITrue;
  707. return FMIOK;
  708. }
  709. fmiSetTime_(c, nextTime);
  710. /* compute approximate inputs */
  711. integration_extrapolate_inputs(comp, nextTime);
  712. /* check step events in case they occurred at the very end time of step */
  713. if (comp->istruct->mTriggerStepEvent != 0) {
  714. comp->reinitRequired =1;
  715. *encounteredEvent=FMITrue;
  716. #ifdef LOG_EVENTS
  717. util_logger(c, comp->instanceName, FMIOK, "", "%.12f: step event", comp->time);
  718. #endif
  719. *endTime = nextTime;
  720. return FMIOK;
  721. }
  722. } else {
  723. fmiSetTime_(c, comp->dstruct->mNextTimeEvent);
  724. *endTime = comp->dstruct->mNextTimeEvent;
  725. *encounteredEvent=FMITrue;
  726. return FMIOK;
  727. #ifdef LOG_EVENTS
  728. util_logger(c, comp->instanceName, FMIOK, "", "%.12f: time event", comp->time);
  729. #endif
  730. }
  731. *encounteredEvent=FMIFalse;
  732. comp->time = endStepTime;
  733. CS_RESULT_SAMPLE(FMITrue);
  734. comp->iData->useDerivatives = 0;
  735. #endif /* ndef ONLY_INCLUDE_INLINE_INTEGRATION */
  736. return FMIOK;
  737. }
  738. #endif /*FMI_2*/
  739. /* ---------------------------------------------------------------------- */
  740. DYMOLA_STATIC FMIStatus fmiCancelStep_(FMIComponent c)
  741. {
  742. Component* comp = (Component*) c;
  743. util_logger(comp, comp->instanceName, FMIDiscard, "", "fmiCancelStep: asynchronous execution of fmiDoStep is not supported");
  744. return FMIDiscard; /* asynchronous execution of fmiDoStep is not supported,
  745. so this method should not be called at all */
  746. }
  747. /* ---------------------------------------------------------------------- */
  748. DYMOLA_STATIC FMIStatus fmiGetStatus_(FMIComponent c, const FMIStatusKind s, FMIStatus* value)
  749. {
  750. Component* comp = (Component*) c;
  751. util_logger(comp, comp->instanceName, FMIDiscard, "",
  752. "fmiGetStatus: not supported since asynchronous execution of fmiDoStep is not supported");
  753. return FMIDiscard;
  754. }
  755. /* ---------------------------------------------------------------------- */
  756. DYMOLA_STATIC FMIStatus fmiGetRealStatus_(FMIComponent c, const FMIStatusKind s, FMIReal* value)
  757. {
  758. Component* comp = (Component*) c;
  759. if (s != FMILastSuccessfulTime) {
  760. util_logger(comp, comp->instanceName, FMIDiscard, "", "fmiGetRealStatus: fmiStatusKind %d unknown", s);
  761. return FMIDiscard;
  762. }
  763. *value = comp->time;
  764. return FMIOK;
  765. }
  766. /* ---------------------------------------------------------------------- */
  767. DYMOLA_STATIC FMIStatus fmiGetIntegerStatus_(FMIComponent c, const FMIStatusKind s, FMIInteger* value)
  768. {
  769. Component* comp = (Component*) c;
  770. util_logger(comp, comp->instanceName, FMIDiscard, "", "fmiGetIntegerStatus: fmiStatusKind %d unknown", s);
  771. return FMIDiscard;
  772. }
  773. /* ---------------------------------------------------------------------- */
  774. DYMOLA_STATIC FMIStatus fmiGetBooleanStatus_(FMIComponent c, const FMIStatusKind s, FMIBoolean* value)
  775. {
  776. Component* comp = (Component*) c;
  777. #ifdef FMI_2
  778. if (s != FMITerminated)
  779. #endif
  780. {
  781. util_logger(comp, comp->instanceName, FMIDiscard, "", "fmiGetBooleanStatus: fmiStatusKind %d unknown", s);
  782. return FMIDiscard;
  783. }
  784. #ifdef FMI_2
  785. *value = comp->terminationByModel;
  786. return FMIOK;
  787. #endif
  788. }
  789. /* ---------------------------------------------------------------------- */
  790. DYMOLA_STATIC FMIStatus fmiGetStringStatus_(FMIComponent c, const FMIStatusKind s, FMIString* value)
  791. {
  792. Component* comp = (Component*) c;
  793. util_logger(comp, comp->instanceName, FMIDiscard, "",
  794. "fmiGetStringStatus: not supported since asynchronous execution of fmiDoStep is not supported");
  795. return FMIDiscard;
  796. }
  797. /* ----------------- local function definitions ----------------- */
  798. static FMIStatus handleEvent(FMIComponent c)
  799. {
  800. Component* comp = (Component*) c;
  801. FMIStatus status;
  802. #ifdef FMI_2
  803. FMIBoolean terminateSimulation;
  804. #else
  805. FMIEventInfo eventInfo;
  806. #endif
  807. status = util_event_update(comp, FMIFalse,
  808. #ifdef FMI_2
  809. &terminateSimulation);
  810. #else
  811. &eventInfo);
  812. #endif
  813. if (status != FMIOK) {
  814. return status;
  815. }
  816. #ifdef FMI_2
  817. if (terminateSimulation) {
  818. #else
  819. if (eventInfo.terminateSimulation) {
  820. #endif
  821. status = FMIDiscard;
  822. util_logger(comp, comp->instanceName, status, "", "fmiDoStep: simulation terminated by model");
  823. comp->terminationByModel = FMITrue;
  824. return status;
  825. }
  826. #ifndef FMI_2
  827. assert(eventInfo.stateValueReferencesChanged == FMIFalse);
  828. #endif
  829. #ifndef ONLY_INCLUDE_INLINE_INTEGRATION
  830. if(!comp->istruct->mInlineIntegration){
  831. if (integration_reinit(c) != integrationOK) {
  832. return util_error(comp);
  833. }
  834. }
  835. #endif
  836. return FMIOK;
  837. }
  838. #endif /* FMU_SKIP_CO_SIM */