fmiCommonFunctions_int.c 48 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272
  1. /* The generic implementation of the FMI interface common for both ME and Co-simulation. */
  2. /* need to include first so that correct files are included */
  3. #include "conf.h"
  4. #include "util.h"
  5. #ifndef ONLY_INCLUDE_INLINE_INTEGRATION
  6. #include "integration.h"
  7. #endif /* ONLY_INCLUDE_INLINE_INTEGRATION */
  8. #include "fmiFunctions_fwd.h"
  9. #include "adymosim.h"
  10. //#include <string.h>
  11. #include <assert.h>
  12. #include <math.h>
  13. /* ----------------- macros ----------------- */
  14. #define ALLOC_AND_CHECK(dest, n, size) \
  15. dest = allocateMemory(n, size); \
  16. if (dest == NULL) { \
  17. goto mem_fail; \
  18. } else { \
  19. freeList[freeIndex++] = dest; \
  20. }
  21. #define COPY_VARIABLES(source, target) \
  22. memcpy(target->states, source->states, source->nStates * sizeof(FMIReal)); \
  23. memcpy(target->derivatives, source->derivatives, source->nStates * sizeof(FMIReal)); \
  24. memcpy(target->parameters, source->parameters, source->nPar * sizeof(FMIReal)); \
  25. memcpy(target->inputs, source->inputs, source->nIn * sizeof(FMIReal)); \
  26. memcpy(target->outputs, source->outputs, source->nOut * sizeof(FMIReal)); \
  27. memcpy(target->auxiliary, source->auxiliary, source->nAux * sizeof(FMIReal)); \
  28. memcpy(target->crossingFunctions, source->crossingFunctions, source->nCross * sizeof(FMIReal)); \
  29. memcpy(target->statesNominal, source->statesNominal, source->nStates * sizeof(FMIReal)); \
  30. { \
  31. size_t i = 0; \
  32. for(i = 0; i< target->nSPar; ++i){ \
  33. memcpy(target->sParameters[i], source->sParameters[i], (MAX_STRING_SIZE+1) * sizeof(FMIChar));\
  34. target->sParameters[i][MAX_STRING_SIZE]='\0'; \
  35. } \
  36. }
  37. /* ----------------- local function declarations ----------------- */
  38. /* find the largest iDemand value in a given array of value references */
  39. static int getLargestIdemand(const FMIValueReference vr[], size_t nvr);
  40. /* copy model variables, including the state nominals, from source to target component */
  41. static void copyVariables(Component* source, Component* target);
  42. /* ----------------- local variables ----------------- */
  43. /* when compiling as a single complilation unit, this is already defined */
  44. #ifndef FMU_SOURCE_SINGLE_UNIT
  45. /* only supports single instances */
  46. Component* globalComponent = NULL;
  47. Component* globalComponent2 = NULL;
  48. #endif
  49. /* Global logger function used by DymosimMessage routines */
  50. DYMOLA_STATIC void fmu_logger(const char* message, int newline, int severity)
  51. {
  52. char hashHandeldMessage[4096];
  53. FMIStatus stat=FMIError;
  54. int i = 0;
  55. int j = 0;
  56. Component* comp;
  57. switch (severity){
  58. case 0: stat = FMIOK; break;
  59. case 1: stat = FMIWarning; break;
  60. case 2: stat = FMIError; break;
  61. }
  62. for(; j<sizeof(hashHandeldMessage)-1;++i,++j){
  63. if(message[i]=='\0'){
  64. hashHandeldMessage[j]='\0';
  65. break;
  66. }
  67. if(message[i]=='#'){
  68. hashHandeldMessage[j++]='#';
  69. }
  70. hashHandeldMessage[j]=message[i];
  71. }
  72. hashHandeldMessage[sizeof(hashHandeldMessage)-1]='\0';
  73. comp = globalComponent;
  74. if (comp == NULL) comp=globalComponent2;
  75. if (comp != NULL) {
  76. if (newline) {
  77. util_logger(comp, comp->instanceName, stat, "", "%s", hashHandeldMessage);
  78. } else {
  79. util_buflogger(comp, comp->instanceName, stat, "", "%s", hashHandeldMessage);
  80. }
  81. }
  82. }
  83. /* ----------------- external variables ----------------- */
  84. #ifndef DYMOLA_STATIC_IMPORT
  85. #define DYMOLA_STATIC_IMPORT DYMOLA_STATIC
  86. #endif
  87. #ifdef SAVE_STATE_DSMODEL
  88. extern int QJacobianCG_[];
  89. extern int QJacobianGC_[];
  90. extern Dymola_bool QL_[];
  91. extern double QRel_[];
  92. extern double QM_[];
  93. extern double Qn_[], Qp_[];
  94. extern double Qscaled_[];
  95. extern double QZold_[];
  96. extern Dymola_bool Qenable_[];
  97. extern Dymola_bool QTimed_[];
  98. extern double QCheckIf_[];
  99. extern double NextSampleTime_[];
  100. extern double NextSampleTimeNew_[];
  101. extern Dymola_bool NextSampleAct_[];
  102. extern Dymola_bool NextSampleActNew_[];
  103. extern Dymola_bool QEvaluate_[];
  104. extern Dymola_bool QEvaluateNew_[];
  105. extern double helpvar[];
  106. extern double oldQZ_[], oldQZDummy_[];
  107. //#if SecondDegree
  108. extern double oldQZ2_[];
  109. extern double oldQZ3_[];
  110. //#endif
  111. extern double Aux_[];
  112. extern int QNLfunc[];
  113. extern int QNLjac[];
  114. extern double QPre_[];
  115. extern double * RefPre_[];
  116. extern double EqRemember1_[];
  117. extern double EqRemember2_[];
  118. extern double EqRemember1Time_;
  119. extern double EqRemember2Time_;
  120. extern int MaxPreNr_;
  121. extern int QNLmax;
  122. #endif /* SAVE_STATE_DSMODEL */
  123. /* ----------------- unexposed function definitions ----------------- */
  124. /* Enable storing of result from within FMU */
  125. DYMOLA_STATIC void __enableResultStoring(FMIComponent c) {
  126. Component* comp = (Component*) c;
  127. comp->storeResult = FMITrue;
  128. }
  129. /* ----------------- function definitions ----------------- */
  130. /* For 1.0 it belongs to Co-sim interface only */
  131. #ifdef FMI_2
  132. DYMOLA_STATIC const char* fmiGetTypesPlatform_()
  133. {
  134. return fmi2TypesPlatform;
  135. }
  136. #endif
  137. /* ---------------------------------------------------------------------- */
  138. DYMOLA_STATIC const char* fmiGetVersion_()
  139. {
  140. return fmiVersion;
  141. }
  142. /* ---------------------------------------------------------------------- */
  143. DYMOLA_STATIC FMIStatus fmiSetDebugLogging_(FMIComponent c, FMIBoolean loggingOn
  144. #ifdef FMI_2
  145. , size_t nCategories, const FMIString categories[]
  146. #endif
  147. )
  148. {
  149. Component* comp = (Component*) c;
  150. comp->loggingOn = loggingOn;
  151. #ifdef FMI_2
  152. /* TODO: handle categories */
  153. #endif
  154. return FMIOK;
  155. }
  156. #ifdef FMI_2
  157. static FMIString initFuncName = "fmiEnterInitializationMode";
  158. #else
  159. static FMIString initFuncName = "fmiInitializeModel";
  160. #endif
  161. /* Common get macro */
  162. #define FMI_GET(type, label, vr_label, m, vr, nvr, value, idemand) { \
  163. Component* comp = (Component*) c; \
  164. size_t i; \
  165. \
  166. if(comp->mStatus == modelInstantiated ){ \
  167. util_logger(comp, comp->instanceName, FMIWarning, "", \
  168. "fmiGet"#label": Not allowed before call of %s", initFuncName); \
  169. return FMIWarning; \
  170. } \
  171. \
  172. if (comp->icall < idemand ||comp->icall == iDemandStart && comp->mStatus == \
  173. modelInitializationMode && comp->valWasSet) { \
  174. /* refresh cache */ \
  175. int QiErr = 0; \
  176. /* TODO: how to handle dsblock for NEW_CODE_GENERATION? */ \
  177. QiErr = util_refresh_cache(comp, idemand, NULL, NULL); \
  178. if (QiErr != 0) { \
  179. FMIStatus status = \
  180. (QiErr == 1 && strcmp(#label, "Real") == 0) ? FMIDiscard : FMIError; \
  181. util_logger(comp, comp->instanceName, status, "", \
  182. "fmiGet"#label": dsblock_ failed, QiErr = %d", QiErr); \
  183. HANDLE_STATUS_RETURN(status); \
  184. } \
  185. } \
  186. \
  187. for (i = 0; i < nvr; i++) { \
  188. const FMIValueReference r = vr[i]; \
  189. int ix = FMI_INDEX(r); \
  190. \
  191. switch (FMI_CATEGORY(r)) { \
  192. case dsOutput: \
  193. value[i] = (type)comp->outputs[ix]; \
  194. break; \
  195. case dsAux: \
  196. value[i] = (type)comp->auxiliary[ix]; \
  197. break; \
  198. case dsState: \
  199. value[i] = (type)comp->states[ix]; \
  200. break; \
  201. case dsDerivative: \
  202. value[i] = (type)comp->derivatives[ix]; \
  203. break; \
  204. case dsParameter: \
  205. value[i] = (type)comp->parameters[ix]; \
  206. break; \
  207. case dsInput: \
  208. value[i] = (type)comp->inputs[ix]; \
  209. break; \
  210. default: \
  211. util_logger(comp, comp->instanceName, FMIWarning, "", \
  212. "fmiGet"#label": cannot get #"#vr_label"%u#", r); \
  213. return FMIWarning; \
  214. } \
  215. util_logger(comp, comp->instanceName, FMIOK, "", \
  216. "fmiGet"#label": #"#vr_label"%u# = %g", r, (double) value[i]); \
  217. } \
  218. } return FMIOK
  219. /* ---------------------------------------------------------------------- */
  220. DYMOLA_STATIC FMIStatus fmiGetReal_(FMIComponent c, const FMIValueReference vr[], size_t nvr, FMIReal value[])
  221. {
  222. int idemand = getLargestIdemand(vr, nvr);
  223. #ifdef NEW_CODE_GENERATION
  224. return 0;
  225. #else
  226. FMI_GET(FMIReal, Real, r, c, vr, nvr, value, idemand);
  227. #endif
  228. }
  229. /* ---------------------------------------------------------------------- */
  230. DYMOLA_STATIC FMIStatus fmiGetInteger_(FMIComponent c, const FMIValueReference vr[], size_t nvr, FMIInteger value[])
  231. {
  232. int idemand = getLargestIdemand(vr, nvr);
  233. FMI_GET(FMIInteger, Integer, i, c, vr, nvr, value, idemand);
  234. }
  235. /* ---------------------------------------------------------------------- */
  236. DYMOLA_STATIC FMIStatus fmiGetBoolean_(FMIComponent c, const FMIValueReference vr[], size_t nvr, FMIBoolean value[])
  237. {
  238. int idemand = getLargestIdemand(vr, nvr);
  239. FMI_GET(FMIBoolean, Boolean, b, c, vr, nvr, value, idemand);
  240. }
  241. /* ---------------------------------------------------------------------- */
  242. DYMOLA_STATIC FMIStatus fmiGetString_(FMIComponent c, const FMIValueReference vr[], size_t nvr, FMIString value[])
  243. {
  244. Component* comp = (Component*) c;
  245. int idemand = getLargestIdemand(vr, nvr);
  246. size_t i;
  247. if(comp->mStatus == modelInstantiated ){
  248. util_logger(comp, comp->instanceName, FMIWarning, "",
  249. "fmiGetString: Not allowed before call of %s", initFuncName);
  250. return FMIWarning;
  251. }
  252. if (comp->icall < idemand || comp->mStatus == modelInitializationMode && comp->valWasSet) {
  253. int QiErr = 0;
  254. QiErr = util_refresh_cache(comp, idemand, NULL, NULL);
  255. if (QiErr != 0) {
  256. util_logger(comp, comp->instanceName, FMIError, "",
  257. "fmiGetString: dsblock_ failed, QiErr = %d", QiErr);
  258. HANDLE_STATUS_RETURN(FMIError);
  259. }
  260. }
  261. for ( i = 0; i < nvr; i++) {
  262. const FMIValueReference r = vr[i];
  263. int ix = FMI_INDEX(r);
  264. switch (FMI_CATEGORY(r)) {
  265. case dsStringParameter:
  266. value[i] = comp->sParameters[ix];
  267. break;
  268. default:
  269. util_logger(comp, comp->instanceName, FMIWarning, "",
  270. "fmiGetString: cannot get #s%u#", r);
  271. return FMIWarning;
  272. }
  273. util_logger(comp, comp->instanceName, FMIOK, "",
  274. "fmiGetString: #s%u# = %s", r, (FMIString) value[i]);
  275. }
  276. return FMIOK;
  277. }
  278. #ifdef ONLY_INCLUDE_INLINE_INTEGRATION
  279. #define integration_sync_extrapolation_input(a, b)
  280. #endif /* ONLY_INCLUDE_INLINE_INTEGRATION */
  281. /* ---------------------------------------------------------------------- */
  282. /* Common set macro */
  283. #define FMI_SET(label, vr_label, m, vr, nvr, value) \
  284. Component* comp = (Component*) c; \
  285. size_t i; \
  286. FMIBoolean allowed = FMITrue; \
  287. FMIBoolean modified = FMIFalse; \
  288. \
  289. for (i = 0; i < nvr; i++) { \
  290. const FMIValueReference r = vr[i]; \
  291. int ix = FMI_INDEX(r); \
  292. \
  293. switch (FMI_CATEGORY(r)) { \
  294. case dsInput: \
  295. if (comp->inputs[ix] != value[i]) { \
  296. comp->inputs[ix] = value[i]; \
  297. modified = FMITrue; \
  298. if (comp->isCoSim) { \
  299. integration_sync_extrapolation_input(comp, ix); \
  300. } \
  301. } \
  302. break; \
  303. case dsState: \
  304. /* allow this to help our own master out, although not really legal */ \
  305. /* if (comp->isCoSim && comp->mStatus != modelInstantiated) { */\
  306. /* allowed = fmiFalse; */\
  307. /* } else { */\
  308. /* allow since equivalent with setContinuousStates */ \
  309. if (comp->states[ix] != value[i]) { \
  310. comp->states[ix] = value[i]; \
  311. modified = FMITrue; \
  312. } \
  313. /* } */ \
  314. break; \
  315. case dsAux: \
  316. /*Allow if inline since states have become aux at that time*/ \
  317. if (comp->mStatus == modelInstantiated || \
  318. comp->mStatus == modelInitializationMode || \
  319. comp->istruct->mInlineIntegration ) { \
  320. if (comp->auxiliary[ix] != value[i]) { \
  321. comp->auxiliary[ix] = value[i]; \
  322. modified = FMITrue; \
  323. } \
  324. }else { \
  325. allowed = FMIFalse; \
  326. } \
  327. break; \
  328. case dsParameter: \
  329. /* TODO: check extra bit in value reference whether tunable */ \
  330. /*if (comp->mStatus == modelInstantiated || "tunable") {*/ \
  331. if (comp->parameters[ix] != value[i]) { \
  332. comp->parameters[ix] = value[i]; \
  333. modified = FMITrue; \
  334. if (comp->istruct) comp->istruct->mParametersNr++; \
  335. } \
  336. /*} else {*/ \
  337. /* allowed = fmiFalse;*/ \
  338. /*}*/ \
  339. break; \
  340. case dsDerivative: \
  341. if (comp->mStatus == modelInstantiated) { \
  342. if (comp->derivatives[ix] != value[i]) { \
  343. comp->derivatives[ix] = value[i]; \
  344. modified = FMITrue; \
  345. } \
  346. } else { \
  347. allowed = FMIFalse; \
  348. } \
  349. break; \
  350. case dsOutput: \
  351. if (comp->mStatus == modelInstantiated) { \
  352. if (comp->outputs[ix] != value[i]) { \
  353. comp->outputs[ix] = value[i]; \
  354. modified = FMITrue; \
  355. } \
  356. } else { \
  357. allowed = FMIFalse; \
  358. } \
  359. break; \
  360. default: \
  361. util_logger(comp, comp->instanceName, FMIWarning, "", \
  362. "fmiSet"#label": cannot set #"#vr_label"%u# ", r); \
  363. return FMIWarning; \
  364. } \
  365. \
  366. if (allowed == FMIFalse) { \
  367. util_logger(comp, comp->instanceName, FMIWarning, "", \
  368. "fmiSet"#label": may not change #"#vr_label"%u# at this " \
  369. "stage, setting of variable ignored", r); \
  370. return FMIWarning; \
  371. } \
  372. util_logger(comp, comp->instanceName, FMIOK, "", \
  373. "fmiSet"#label": #"#vr_label"%u# = %g", r, (double) value[i]); \
  374. } \
  375. \
  376. if (modified == FMITrue) { \
  377. comp->icall = iDemandStart; \
  378. comp->valWasSet = 1; \
  379. comp->recalJacobian = 1; \
  380. } \
  381. return FMIOK
  382. /* ---------------------------------------------------------------------- */
  383. DYMOLA_STATIC FMIStatus fmiSetReal_(FMIComponent c, const FMIValueReference vr[], size_t nvr, const FMIReal value[])
  384. {
  385. FMI_SET(Real, r, c, vr, nvr, value);
  386. }
  387. /* ---------------------------------------------------------------------- */
  388. DYMOLA_STATIC FMIStatus fmiSetInteger_(FMIComponent c, const FMIValueReference vr[], size_t nvr, const FMIInteger value[])
  389. {
  390. FMI_SET(Integer, i, c, vr, nvr, value);
  391. }
  392. /* ---------------------------------------------------------------------- */
  393. DYMOLA_STATIC FMIStatus fmiSetBoolean_(FMIComponent c, const FMIValueReference vr[], size_t nvr, const FMIBoolean value[])
  394. {
  395. FMI_SET(Boolean, b, c, vr, nvr, value);
  396. }
  397. /* ---------------------------------------------------------------------- */
  398. DYMOLA_STATIC FMIStatus fmiSetString_(FMIComponent c, const FMIValueReference vr[], size_t nvr, const FMIString value[])
  399. {
  400. Component* comp = (Component*) c;
  401. size_t i;
  402. FMIBoolean allowed = FMITrue;
  403. FMIBoolean modified = FMIFalse;
  404. for (i = 0; i < nvr; i++) {
  405. size_t len;
  406. const FMIValueReference r = vr[i];
  407. int ix = FMI_INDEX(r);
  408. switch (FMI_CATEGORY(r)) {
  409. case dsStringParameter:
  410. len=strlen(value[i]);
  411. if (len>MAX_STRING_SIZE) len=MAX_STRING_SIZE;
  412. memcpy((comp->sParameters)[ix], value[i], len+1);
  413. comp->sParameters[ix][MAX_STRING_SIZE]='\0';
  414. if (comp->istruct) comp->istruct->mParametersNr++;
  415. break;
  416. default:
  417. util_logger(comp, comp->instanceName, FMIWarning, "",
  418. "fmiSetString: cannot set #s%u# ", r);
  419. return FMIWarning;
  420. }
  421. if (allowed == FMIFalse) {
  422. util_logger(comp, comp->instanceName, FMIWarning, "",
  423. "fmiSetString: may not change #s%u# at this stage", r);
  424. return FMIWarning;
  425. }
  426. util_logger(comp, comp->instanceName, FMIOK, "",
  427. "fmiSetString: #s%u# = %s", r, (FMIString) value[i]);
  428. }
  429. if (modified == FMITrue) {
  430. comp->icall = iDemandStart;
  431. comp->valWasSet = 1;
  432. comp->recalJacobian = 1;
  433. }
  434. return FMIOK;
  435. }
  436. #ifdef FMI_2
  437. DYMOLA_STATIC_IMPORT size_t dyn_allowMultipleInstances;
  438. DYMOLA_STATIC void DYNInitializeDid(struct DYNInstanceData*did_);
  439. /* ---------------------------------------------------------------------- */
  440. DYMOLA_STATIC FMIStatus fmiGetFMUstate_(FMIComponent c, FMIFMUstate* FMUstate)
  441. {
  442. static FMIString label = "fmiGetFMUstate";
  443. Component* source = (Component*) c;
  444. Component* target;
  445. FMIStatus status;
  446. /* first handle part common for both Model exchange and co-simulation */
  447. void* freeList[128];
  448. /*TODO suppot for manny Strings*/
  449. int freeIndex = 0;
  450. FMICallbackAllocateMemory allocateMemory = source->functions->allocateMemory;
  451. util_logger(source, source->instanceName, FMIOK, "", label);
  452. if (*FMUstate == NULL) {
  453. ALLOC_AND_CHECK(*FMUstate, 1, sizeof(Component));
  454. target = (Component*) *FMUstate;
  455. target->iData = NULL;
  456. target->allocDone = 0;
  457. } else {
  458. target = (Component*) *FMUstate;
  459. }
  460. /* get state vectors, derivatives, etc */
  461. {
  462. if (!target->allocDone) {
  463. size_t i =0;
  464. target->nStates = source->nStates;
  465. target->nPar = source->nPar;
  466. target->nIn = source->nIn;
  467. target->nOut = source->nOut;
  468. target->nAux = source->nAux;
  469. target->nCross = source->nCross;
  470. target->nSPar = source->nSPar;
  471. ALLOC_AND_CHECK(target->states, source->nStates + 1, sizeof(FMIReal));
  472. ALLOC_AND_CHECK(target->derivatives, source->nStates + 1, sizeof(FMIReal));
  473. ALLOC_AND_CHECK(target->parameters, source->nPar + 1, sizeof(FMIReal));
  474. ALLOC_AND_CHECK(target->inputs, source->nIn + 1, sizeof(FMIReal));
  475. ALLOC_AND_CHECK(target->outputs, source->nOut + 1, sizeof(FMIReal));
  476. ALLOC_AND_CHECK(target->auxiliary, source->nAux + 1, sizeof(FMIReal));
  477. ALLOC_AND_CHECK(target->crossingFunctions, source->nCross + 1, sizeof(FMIReal));
  478. ALLOC_AND_CHECK(target->statesNominal, source->nStates + 1, sizeof(FMIReal));
  479. ALLOC_AND_CHECK(target->sParameters, source->nSPar + 1, sizeof(FMIChar*));
  480. for(i = 0; i<target->nSPar; ++i){
  481. ALLOC_AND_CHECK(target->sParameters[i], MAX_STRING_SIZE+1, sizeof(FMIChar));
  482. }
  483. target->sParameters[target->nSPar]=NULL;
  484. }
  485. COPY_VARIABLES(source, target);
  486. }
  487. target->time = source->time;
  488. target->icall = source->icall;
  489. target->mStatus = source->mStatus;
  490. if (!target->allocDone) {
  491. ALLOC_AND_CHECK(target->dstruct, 1, sizeof(struct BasicDDymosimStruct));
  492. ALLOC_AND_CHECK(target->istruct, 1, sizeof(struct BasicIDymosimStruct));
  493. }
  494. memcpy(target->dstruct, source->dstruct, sizeof(struct BasicDDymosimStruct));
  495. memcpy(target->istruct, source->istruct, sizeof(struct BasicIDymosimStruct));
  496. if(!target->allocDone){
  497. if(dyn_allowMultipleInstances){
  498. ALLOC_AND_CHECK(target->did, 1, dyn_allowMultipleInstances);
  499. DYNInitializeDid(target->did);
  500. }else{
  501. target->did=NULL;
  502. }
  503. }
  504. if(target->did && dyn_allowMultipleInstances && source->did){
  505. memcpy(target->did, source->did, dyn_allowMultipleInstances);
  506. }
  507. #ifdef SAVE_STATE_DSMODEL
  508. {
  509. int nrel_, ntim_, ncheckif_, nsamp_, nwhen_, nglobalhelp_, maxaux, qnlmax_, sizepre_, sizeeq_;
  510. DsmodelData* dsData;
  511. GetDimensions3(&nrel_, &ntim_, &ncheckif_, &nsamp_, &nwhen_,
  512. &nglobalhelp_, &maxaux, &qnlmax_, &sizepre_, &sizeeq_);
  513. if (!target->allocDone) {
  514. ALLOC_AND_CHECK(dsData, 1, sizeof(DsmodelData));
  515. ALLOC_AND_CHECK(dsData->QL_, nrel_ + 1, sizeof(Dymola_bool));
  516. ALLOC_AND_CHECK(dsData->QRel_, nrel_ + 1, sizeof(double));
  517. ALLOC_AND_CHECK(dsData->QM_, nrel_ + 1, sizeof(double));
  518. ALLOC_AND_CHECK(dsData->Qn_, nrel_ + 1, sizeof(double));
  519. ALLOC_AND_CHECK(dsData->Qp_, nrel_ + 1, sizeof(double));
  520. ALLOC_AND_CHECK(dsData->Qscaled_, nrel_ + 1, sizeof(double));
  521. ALLOC_AND_CHECK(dsData->QZold_, 2 * nrel_ + 1, sizeof(double));
  522. ALLOC_AND_CHECK(dsData->Qenable_, nrel_ + 1, sizeof(Dymola_bool));
  523. ALLOC_AND_CHECK(dsData->QTimed_, ntim_ + 1, sizeof(Dymola_bool));
  524. ALLOC_AND_CHECK(dsData->QCheckIf_, ncheckif_ + 1, sizeof(double));
  525. ALLOC_AND_CHECK(dsData->NextSampleTime_, nsamp_ + 1, sizeof(double));
  526. ALLOC_AND_CHECK(dsData->NextSampleTimeNew_, nsamp_ + 1, sizeof(double));
  527. ALLOC_AND_CHECK(dsData->NextSampleAct_, nsamp_ + 1, sizeof(Dymola_bool));
  528. ALLOC_AND_CHECK(dsData->NextSampleActNew_, nsamp_ + 1, sizeof(Dymola_bool));
  529. ALLOC_AND_CHECK(dsData->QEvaluate_, nwhen_ + 1, sizeof(Dymola_bool));
  530. ALLOC_AND_CHECK(dsData->QEvaluateNew_, nwhen_ + 1, sizeof(Dymola_bool));
  531. ALLOC_AND_CHECK(dsData->helpvar, nglobalhelp_ + 1, sizeof(double));
  532. ALLOC_AND_CHECK(dsData->oldQZ_, 2 * nrel_ + 1, sizeof(double));
  533. ALLOC_AND_CHECK(dsData->oldQZDummy_, 2 * nrel_ + 1, sizeof(double));
  534. ALLOC_AND_CHECK(dsData->oldQZ2_, 2 * nrel_ + 1, sizeof(double));
  535. ALLOC_AND_CHECK(dsData->oldQZ3_, 2 * nrel_ + 1, sizeof(double));
  536. ALLOC_AND_CHECK(dsData->Aux_, maxaux+10000, sizeof(double));
  537. ALLOC_AND_CHECK(dsData->QNLfunc, qnlmax_ ? qnlmax_ : 1, sizeof(int));
  538. ALLOC_AND_CHECK(dsData->QNLjac, qnlmax_ ? qnlmax_ : 1, sizeof(int));
  539. ALLOC_AND_CHECK(dsData->QPre_, sizepre_ ? sizepre_ : 1, sizeof(double));
  540. ALLOC_AND_CHECK(dsData->RefPre_, sizepre_ ? sizepre_ : 1, sizeof(double));
  541. ALLOC_AND_CHECK(dsData->EqRemember1_, sizeeq_ ? sizeeq_ : 1, sizeof(double));
  542. ALLOC_AND_CHECK(dsData->EqRemember2_, sizeeq_ ? sizeeq_ : 1, sizeof(double));
  543. target->dsModelData = dsData;
  544. } else {
  545. dsData = target->dsModelData;
  546. }
  547. memcpy(dsData->QL_, QL_, nrel_ * sizeof(Dymola_bool));
  548. memcpy(dsData->QRel_, QRel_, nrel_ * sizeof(double));
  549. memcpy(dsData->QM_, QM_, nrel_ * sizeof(double));
  550. memcpy(dsData->Qn_, Qn_, nrel_ * sizeof(double));
  551. memcpy(dsData->Qp_, Qp_, nrel_ * sizeof(double));
  552. memcpy(dsData->Qscaled_, Qscaled_, nrel_ * sizeof(double));
  553. memcpy(dsData->QZold_, QZold_, 2 * nrel_ * sizeof(double));
  554. memcpy(dsData->Qenable_, Qenable_, nrel_ * sizeof(Dymola_bool));
  555. memcpy(dsData->QTimed_, QTimed_, ntim_ * sizeof(Dymola_bool));
  556. memcpy(dsData->QCheckIf_, QCheckIf_, ncheckif_ * sizeof(double));
  557. memcpy(dsData->NextSampleTime_, NextSampleTime_, nsamp_ * sizeof(double));
  558. memcpy(dsData->NextSampleTimeNew_, NextSampleTimeNew_, nsamp_ * sizeof(double));
  559. memcpy(dsData->NextSampleAct_, NextSampleAct_, nsamp_ * sizeof(Dymola_bool));
  560. memcpy(dsData->NextSampleActNew_, NextSampleActNew_, nsamp_ * sizeof(Dymola_bool));
  561. memcpy(dsData->QEvaluate_, QEvaluate_, nwhen_ * sizeof(Dymola_bool));
  562. memcpy(dsData->QEvaluateNew_, QEvaluateNew_, nwhen_ * sizeof(Dymola_bool));
  563. memcpy(dsData->helpvar, helpvar, nglobalhelp_ * sizeof(double));
  564. memcpy(dsData->oldQZ_, oldQZ_, 2 * nrel_ * sizeof(double));
  565. memcpy(dsData->oldQZDummy_, oldQZDummy_, 2 * nrel_ * sizeof(double));
  566. memcpy(dsData->oldQZ2_, oldQZ2_, 2 * nrel_ * sizeof(double));
  567. memcpy(dsData->oldQZ3_, oldQZ3_, 2 * nrel_ * sizeof(double));
  568. memcpy(dsData->Aux_, Aux_, (maxaux + 10000) * sizeof(double));
  569. memcpy(dsData->QNLfunc, QNLfunc, qnlmax_ * sizeof(int));
  570. memcpy(dsData->QNLjac, QNLjac, qnlmax_ * sizeof(int));
  571. memcpy(dsData->QPre_, QPre_, sizepre_ * sizeof(double));
  572. memcpy(dsData->RefPre_, RefPre_, sizepre_ * sizeof(double*));
  573. memcpy(dsData->EqRemember1_, EqRemember1_, sizeeq_ * sizeof(double));
  574. memcpy(dsData->EqRemember2_, EqRemember2_, sizeeq_ * sizeof(double));
  575. }
  576. #endif /* SAVE_STATE_DSMODEL */
  577. /* then handle co-simulation specific part */
  578. #ifndef ONLY_INCLUDE_INLINE_INTEGRATION
  579. if (source->isCoSim && !source->istruct->mInlineIntegration) {
  580. status = integration_get_state(source, target);
  581. if (status == FMIOK) {
  582. target->allocDone = 1;
  583. return FMIOK;
  584. } else {
  585. util_logger(c, source->instanceName, FMIError, "", "fmiGetFMUstate failed");
  586. return util_error(source);
  587. }
  588. }
  589. #endif
  590. target->allocDone = 1;
  591. util_logger(source, source->instanceName, FMIOK, "", "%s", label);
  592. return FMIOK;
  593. mem_fail:
  594. {
  595. int i;
  596. util_logger(source, source->instanceName, FMIError, "", "memory allocation failed");
  597. for (i = freeIndex - 1; i >= 0; i--) {
  598. source->functions->freeMemory(freeList[i]);
  599. }
  600. *FMUstate = NULL;
  601. return util_error(c);
  602. }
  603. }
  604. /* ---------------------------------------------------------------------- */
  605. DYMOLA_STATIC FMIStatus fmiSetFMUstate_(FMIComponent c, FMIFMUstate FMUstate)
  606. {
  607. static FMIString label = "fmiSetFMUstate";
  608. Component* target = (Component*) c;
  609. Component* source;
  610. /* first handle part common for both Model exchange and co-simulation */
  611. if (FMUstate == NULL) {
  612. util_logger(target, target->instanceName, FMIWarning, "", "fmiSetFMUstate: FMUstate == NULL");
  613. return FMIWarning;
  614. }
  615. source = (Component*) FMUstate;
  616. assert(source->allocDone);
  617. /* set state vectors, derivatives, etc */
  618. {
  619. COPY_VARIABLES(source, target);
  620. }
  621. if (source->time > target->time) {
  622. util_logger(target, target->instanceName, FMIWarning, "",
  623. "%s: Time in FMU state later than current. The behaviour for this is not well tested.",label);
  624. }
  625. target->time = source->time;
  626. target->icall = source->icall;
  627. target->mStatus = source->mStatus;
  628. memcpy(target->dstruct, source->dstruct, sizeof(struct BasicDDymosimStruct));
  629. memcpy(target->istruct, source->istruct, sizeof(struct BasicIDymosimStruct));
  630. if(target->did && source->did && dyn_allowMultipleInstances){
  631. memcpy(target->did, source->did, dyn_allowMultipleInstances);
  632. }
  633. #ifdef SAVE_STATE_DSMODEL
  634. {
  635. int nrel_, ntim_, ncheckif_, nsamp_, nwhen_, nglobalhelp_, maxaux, qnlmax_, sizepre_, sizeeq_;
  636. DsmodelData* dsData = source->dsModelData;
  637. GetDimensions3(&nrel_, &ntim_, &ncheckif_, &nsamp_, &nwhen_,
  638. &nglobalhelp_, &maxaux, &qnlmax_, &sizepre_, &sizeeq_);
  639. memcpy(QL_, dsData->QL_, nrel_ * sizeof(Dymola_bool));
  640. memcpy(QRel_, dsData->QRel_, nrel_ * sizeof(double));
  641. memcpy(QM_, dsData->QM_, nrel_ * sizeof(double));
  642. memcpy(Qn_, dsData->Qn_, nrel_ * sizeof(double));
  643. memcpy(Qp_, dsData->Qp_, nrel_ * sizeof(double));
  644. memcpy(Qscaled_, dsData->Qscaled_, nrel_ * sizeof(double));
  645. memcpy(QZold_, dsData->QZold_, 2 * nrel_ * sizeof(double));
  646. memcpy(Qenable_, dsData->Qenable_, nrel_ * sizeof(Dymola_bool));
  647. memcpy(QTimed_, dsData->QTimed_, ntim_ * sizeof(Dymola_bool));
  648. memcpy(QCheckIf_, dsData->QCheckIf_, ncheckif_ * sizeof(double));
  649. memcpy(NextSampleTime_, dsData->NextSampleTime_, nsamp_ * sizeof(double));
  650. memcpy(NextSampleTimeNew_, dsData->NextSampleTimeNew_, nsamp_ * sizeof(double));
  651. memcpy(NextSampleAct_, dsData->NextSampleAct_, nsamp_ * sizeof(Dymola_bool));
  652. memcpy(NextSampleActNew_, dsData->NextSampleActNew_, nsamp_ * sizeof(Dymola_bool));
  653. memcpy(QEvaluate_, dsData->QEvaluate_, nwhen_ * sizeof(Dymola_bool));
  654. memcpy(QEvaluateNew_, dsData->QEvaluateNew_, nwhen_ * sizeof(Dymola_bool));
  655. memcpy(helpvar, dsData->helpvar, nglobalhelp_ * sizeof(double));
  656. memcpy(oldQZ_, dsData->oldQZ_, 2 * nrel_ * sizeof(double));
  657. memcpy(oldQZDummy_, dsData->oldQZDummy_, 2 * nrel_ * sizeof(double));
  658. memcpy(oldQZ2_, dsData->oldQZ2_, 2 * nrel_ * sizeof(double));
  659. memcpy(oldQZ3_, dsData->oldQZ3_, 2 * nrel_ * sizeof(double));
  660. memcpy(Aux_, dsData->Aux_, (maxaux + 10000) * sizeof(double));
  661. memcpy(QNLfunc, dsData->QNLfunc, qnlmax_ * sizeof(int));
  662. memcpy(QNLjac, dsData->QNLjac, qnlmax_ * sizeof(int));
  663. memcpy(QPre_, dsData->QPre_, sizepre_ * sizeof(double));
  664. memcpy(RefPre_, dsData->RefPre_, sizepre_ * sizeof(double*));
  665. memcpy(EqRemember1_, dsData->EqRemember1_, sizeeq_ * sizeof(double));
  666. memcpy(EqRemember2_, dsData->EqRemember2_, sizeeq_ * sizeof(double));
  667. }
  668. #endif /* SAVE_STATE_DSMODEL */
  669. /* then handle co-simulation specific part */
  670. #ifndef ONLY_INCLUDE_INLINE_INTEGRATION
  671. if (source->isCoSim && !source->istruct->mInlineIntegration) {
  672. return integration_set_state(source, target);
  673. }
  674. #endif /* ONLY_INCLUDE_INLINE_INTEGRATION */
  675. #if 0
  676. status = util_event_update(target, FMIFalse, &terminateSimulation);
  677. if (status != FMIOK) {
  678. util_logger(target, target->instanceName, status, "", "%s: failed with event iteration", label);
  679. return status;
  680. }
  681. if (terminateSimulation) {
  682. util_logger(target, target->instanceName, status, "", "%s: simulation terminated by model", label);
  683. target->terminationByModel = FMITrue;
  684. return status;
  685. }
  686. #endif
  687. #ifndef ONLY_INCLUDE_INLINE_INTEGRATION
  688. if(target->isCoSim && !target->istruct->mInlineIntegration){
  689. if (integration_reinit(target) != integrationOK) {
  690. return util_error(target);
  691. }
  692. }
  693. #endif
  694. util_logger(target, target->instanceName, FMIOK, "", "%s", label);
  695. return FMIOK;
  696. }
  697. /* ---------------------------------------------------------------------- */
  698. DYMOLA_STATIC FMIStatus fmiFreeFMUstate_(FMIComponent c, FMIFMUstate* FMUstate)
  699. {
  700. static FMIString label = "fmiFreeFMUstate";
  701. Component* comp = (Component*) c;
  702. Component* target;
  703. FMICallbackFreeMemory freeMemory;
  704. int i;
  705. if (FMUstate == NULL || *FMUstate == NULL) {
  706. return FMIOK;
  707. }
  708. target = (Component*) *FMUstate;
  709. /* first handle part common for both Model exchange and co-simulation */
  710. freeMemory = comp->functions->freeMemory;
  711. freeMemory(target->states);
  712. freeMemory(target->derivatives);
  713. freeMemory(target->parameters);
  714. freeMemory(target->inputs);
  715. freeMemory(target->outputs);
  716. freeMemory(target->auxiliary);
  717. freeMemory(target->crossingFunctions);
  718. freeMemory(target->statesNominal);
  719. for(i = (int) comp->nSPar-1; i >=0; --i){
  720. freeMemory( (target->sParameters)[i]);
  721. }
  722. freeMemory(target->sParameters);
  723. freeMemory(target->dstruct);
  724. freeMemory(target->istruct);
  725. freeMemory(target->did);
  726. #ifdef SAVE_STATE_DSMODEL
  727. if (target->dsModelData != NULL) {
  728. DsmodelData* dsData = target->dsModelData;
  729. freeMemory(dsData->QL_);
  730. freeMemory(dsData->QRel_);
  731. freeMemory(dsData->QM_);
  732. freeMemory(dsData->Qn_);
  733. freeMemory(dsData->Qp_);
  734. freeMemory(dsData->Qscaled_);
  735. freeMemory(dsData->QZold_);
  736. freeMemory(dsData->Qenable_);
  737. freeMemory(dsData->QTimed_);
  738. freeMemory(dsData->QCheckIf_);
  739. freeMemory(dsData->NextSampleTime_);
  740. freeMemory(dsData->NextSampleTimeNew_);
  741. freeMemory(dsData->NextSampleAct_);
  742. freeMemory(dsData->NextSampleActNew_);
  743. freeMemory(dsData->QEvaluate_);
  744. freeMemory(dsData->QEvaluateNew_);
  745. freeMemory(dsData->helpvar);
  746. freeMemory(dsData->oldQZ_);
  747. freeMemory(dsData->oldQZDummy_);
  748. freeMemory(dsData->oldQZ2_);
  749. freeMemory(dsData->oldQZ3_);
  750. freeMemory(dsData->Aux_);
  751. freeMemory(dsData->QNLfunc);
  752. freeMemory(dsData->QNLjac);
  753. freeMemory(dsData->QPre_);
  754. freeMemory(dsData->RefPre_);
  755. freeMemory(dsData->EqRemember1_);
  756. freeMemory(dsData->EqRemember2_);
  757. freeMemory(dsData);
  758. }
  759. #endif /* SAVE_STATE_DSMODEL */
  760. /* then handle co-simulation specific part */
  761. #ifndef ONLY_INCLUDE_INLINE_INTEGRATION
  762. if (comp->isCoSim && !comp->istruct->mInlineIntegration) {
  763. integration_free_state(c, target);
  764. }
  765. #endif /* ONLY_INCLUDE_INLINE_INTEGRATION */
  766. freeMemory(*FMUstate);
  767. *FMUstate = NULL;
  768. util_logger(comp, comp->instanceName, FMIOK, "", "%s", label);
  769. return FMIOK;
  770. }
  771. /* ---------------------------------------------------------------------- */
  772. DYMOLA_STATIC FMIStatus fmiSerializedFMUstateSize_(FMIComponent c, FMIFMUstate FMUstate,
  773. size_t *size)
  774. {
  775. Component* comp = (Component*) c;
  776. util_logger(comp, comp->instanceName, FMIWarning, "", "fmiSerializedFMUstateSize: not supported");
  777. return FMIWarning;
  778. }
  779. /* ---------------------------------------------------------------------- */
  780. DYMOLA_STATIC FMIStatus fmiSerializeFMUstate_(FMIComponent c, FMIFMUstate FMUstate,
  781. FMIByte* serializedState, size_t size)
  782. {
  783. Component* comp = (Component*) c;
  784. util_logger(comp, comp->instanceName, FMIWarning, "", "fmiSerializeFMUstate: not supported");
  785. return FMIWarning;
  786. }
  787. /* ---------------------------------------------------------------------- */
  788. DYMOLA_STATIC FMIStatus fmiDeSerializeFMUstate_(FMIComponent c,
  789. const FMIByte serializedState[],
  790. size_t size, FMIFMUstate* FMUstate)
  791. {
  792. Component* comp = (Component*) c;
  793. util_logger(comp, comp->instanceName, FMIWarning, "", "fmiDeSerializeFMUstate: not supported");
  794. return FMIWarning;
  795. }
  796. /* ---------------------------------------------------------------------- */
  797. DYMOLA_STATIC void SetDymolaJacobianPointers(struct DYNInstanceData*did_, double * QJacobian_,double * QBJacobian_,double * QCJacobian_,double * QDJacobian_,int QJacobianN_,
  798. int QJacobianNU_,int QJacobianNY_,double * QJacobianSparse_,int * QJacobianSparseR_,int * QJacobianSparseC_,int QJacobianNZ_);
  799. DYMOLA_STATIC FMIStatus fmiGetDirectionalDerivative_(FMIComponent c,
  800. const FMIValueReference z_ref[],
  801. size_t nz,
  802. const FMIValueReference v_ref[],
  803. size_t nv,
  804. const FMIReal dv[],
  805. FMIReal dz[])
  806. {
  807. static FMIString label = "fmiGetDirectionalDerivative";
  808. size_t i;
  809. size_t j;
  810. Component* comp = (Component*) c;
  811. FMIStatus status = FMIOK;
  812. FMICallbackAllocateMemory allocateMemory = comp->functions->allocateMemory;
  813. FMICallbackFreeMemory freeMemory = comp->functions->freeMemory;
  814. JacobianData* jacData = &comp->jacData;
  815. extern int QJacobianDefined_;
  816. util_logger(comp, comp->instanceName, FMIOK, "", "%s", label);
  817. LOG(comp, FMIOK, label);
  818. for (i = 0; i < nz; i++) {
  819. dz[i] = 0;
  820. }
  821. /* analytic Jacobian available ? */
  822. if (QJacobianDefined_ && (comp->mStatus != modelInstantiated ||
  823. comp->mStatus != modelInitializationMode) ) {
  824. /*Analytical Jacobians are only valid after intialization*/
  825. int nx, nx2, nu, ny, nw, np, nrel, ncons, dae;
  826. int nA, nB, nC, nD;
  827. GetDimensions(&nx, &nx2, &nu, &ny, &nw, &np, &nrel, &ncons, &dae);
  828. nA = nx * nx;
  829. nB = nx * nu;
  830. nC = ny * nx;
  831. nD = ny * nu;
  832. /* avoid re-allocation on each call */
  833. if (jacData->nJacA < nA || jacData->jacA == NULL) {
  834. freeMemory(jacData->jacA);
  835. jacData->jacA = (double*) allocateMemory(nA+1, sizeof(double));
  836. }
  837. if (jacData->nJacB < nB || jacData->jacB == NULL) {
  838. freeMemory(jacData->jacB);
  839. jacData->jacB = (double*) allocateMemory(nB+1, sizeof(double));
  840. }
  841. if (jacData->nJacC < nC || jacData->jacC == NULL) {
  842. freeMemory(jacData->jacC);
  843. jacData->jacC = (double*) allocateMemory(nC+1, sizeof(double));
  844. }
  845. if (jacData->nJacD < nD || jacData->jacD == NULL) {
  846. freeMemory(jacData->jacD);
  847. jacData->jacD = (double*) allocateMemory(nD+1, sizeof(double));
  848. }
  849. if (jacData->jacA == NULL || jacData->jacB == NULL || jacData->jacC== NULL || jacData->jacD == NULL) {
  850. util_logger(comp, comp->instanceName, FMIError, "", "%s: memory allocation failed", label);
  851. status = FMIError;
  852. goto done1;
  853. }
  854. jacData->nJacA = nA;
  855. jacData->nJacB = nB;
  856. jacData->nJacC = nC;
  857. jacData->nJacD = nD;
  858. if(comp->icall < iDemandDerivative || comp->recalJacobian){
  859. SetDymolaJacobianPointers(comp->did, jacData->jacA, jacData->jacB, jacData->jacC, jacData->jacD, nx, nu, ny, 0, 0,0,0);
  860. if (util_refresh_cache(comp, iDemandDerivative, label, NULL)) {
  861. status = FMIError;
  862. goto done1;
  863. }
  864. comp->recalJacobian = 0;
  865. }
  866. for (j = 0; j < nv; j++) {
  867. /* skip for seed == 0 */
  868. if (dv[j] != 0) {
  869. FMIBoolean vIsState = FMITrue;
  870. int v_ix = FMI_INDEX(v_ref[j]);
  871. /* determine type for v reference*/
  872. dsCategory v_cat = FMI_CATEGORY(v_ref[j]);
  873. switch(v_cat) {
  874. case dsState:
  875. assert(v_ix < nx);
  876. break;
  877. case dsInput:
  878. assert(v_ix < nu);
  879. vIsState = FMIFalse;
  880. break;
  881. default:
  882. util_logger(comp, comp->instanceName, FMIError, "",
  883. "%s: v_ref[%d] refers neither to state nor input", label, i);
  884. status = FMIError;
  885. goto done1;
  886. }
  887. /* Compute column j */
  888. for (i = 0; i < nz; i++) {
  889. /* figure out which of A, B, C and D matrix to fetch from */
  890. dsCategory z_cat = FMI_CATEGORY(z_ref[i]);
  891. int z_ix = FMI_INDEX(z_ref[i]);
  892. switch (z_cat) {
  893. case dsDerivative:
  894. assert(z_ix < nx);
  895. if (vIsState == FMITrue) {
  896. dz[i] += dv[j] * jacData->jacA[z_ix + v_ix * nx];
  897. } else {
  898. dz[i] += dv[j] * jacData->jacB[z_ix + v_ix * nx];
  899. }
  900. break;
  901. case dsOutput:
  902. assert(z_ix < ny);
  903. if (vIsState == FMITrue) {
  904. dz[i] += dv[j] * jacData->jacC[z_ix + v_ix * ny];
  905. } else {
  906. dz[i] += dv[j] * jacData->jacD[z_ix + v_ix * ny];
  907. }
  908. break;
  909. default:
  910. util_logger(comp, comp->instanceName, FMIError, "",
  911. "%s: z_ref[%d] refers neither to state derivative nor output", label, i);
  912. status = FMIError;
  913. goto done1;
  914. }
  915. }
  916. }
  917. }
  918. done1:
  919. SetDymolaJacobianPointers(comp->did, 0, 0, 0, 0, 0, 0, 0, 0, 0,0,0);
  920. HANDLE_STATUS_RETURN(status);
  921. }
  922. /* analytic Jacobian not available */
  923. {
  924. double *jacV;
  925. double *jacVTmp1;
  926. double *jacVTmp2;
  927. double *jacZ;
  928. double *jacZTmp1;
  929. double *jacZTmp2;
  930. double h;
  931. FMIBoolean success1;
  932. FMIBoolean success2;
  933. comp->istruct->mInJacobian=1;
  934. /* avoid re-allocation on each call */
  935. if (jacData->nJacV < nv) {
  936. freeMemory(jacData->jacV);
  937. freeMemory(jacData->jacVTmp1);
  938. freeMemory(jacData->jacVTmp2);
  939. jacData->jacV = (double*) allocateMemory(nv, sizeof(double));
  940. jacData->jacVTmp1 = (double*) allocateMemory(nv, sizeof(double));
  941. jacData->jacVTmp2 = (double*) allocateMemory(nv, sizeof(double));
  942. }
  943. if (jacData->nJacZ < nz) {
  944. freeMemory(jacData->jacZ);
  945. freeMemory(jacData->jacZTmp1);
  946. freeMemory(jacData->jacZTmp2);
  947. jacData->jacZ = (double*) allocateMemory(nz, sizeof(double));
  948. jacData->jacZTmp1 = (double*) allocateMemory(nz, sizeof(double));
  949. jacData->jacZTmp2 = (double*) allocateMemory(nz, sizeof(double));
  950. }
  951. if (jacData->jacV == NULL || jacData->jacVTmp1 == NULL || jacData->jacVTmp2 == NULL ||
  952. jacData->jacZ == NULL || jacData->jacZTmp1 == NULL || jacData->jacZTmp2 == NULL) {
  953. util_logger(comp, comp->instanceName, FMIError, "", "%s: memory allocation failed", label);
  954. status = FMIError;
  955. goto done2;
  956. }
  957. jacData->nJacV = nv;
  958. jacData->nJacZ = nz;
  959. /* save current v_values for later restoring */
  960. if ((status = fmiGetReal_(c, v_ref, nv, jacData->jacV)) != FMIOK ||
  961. (status = fmiGetReal_(c, z_ref, nz, jacData->jacZ)) != FMIOK) {
  962. goto done2;
  963. }
  964. jacV = jacData->jacV;
  965. jacVTmp1 = jacData->jacVTmp1;
  966. jacVTmp2 = jacData->jacVTmp2;
  967. jacZ = jacData->jacZ;
  968. jacZTmp1 = jacData->jacZTmp1;
  969. jacZTmp2 = jacData->jacZTmp2;
  970. h=1e6;
  971. for(i=0; i < nv; ++i ){
  972. if( dv[i] != 0 ){
  973. double temp = ( fabs(jacV[i]) + 1.0 ) * 1.0e-5 / fabs(dv[i]);
  974. if( temp < h )
  975. h=temp;
  976. }
  977. }
  978. /* perturb each state/continuous input */
  979. for (i = 0; i < nv; i++) {
  980. /* skip for seed == 0 */
  981. if (dv[i] != 0) {
  982. jacVTmp1[i] = jacV[i] + h*dv[i];
  983. jacVTmp2[i] = jacV[i] - h*dv[i];
  984. }else{
  985. jacVTmp1[i]=jacVTmp2[i]=jacV[i];
  986. }
  987. }
  988. if ((status = fmiSetReal_(c, v_ref, nv, jacVTmp1)) != FMIOK ) {
  989. goto done2;
  990. }
  991. status = fmiGetReal_(c, z_ref, nz, jacZTmp1);
  992. success1 = status == FMIOK;
  993. if ((status = fmiSetReal_(c, v_ref, nv, jacVTmp2)) != FMIOK ) {
  994. goto done2;
  995. }
  996. status = fmiGetReal_(c, z_ref, nz, jacZTmp2);
  997. success2 = status == FMIOK;
  998. if(success1 && success2){
  999. /*Central Difference*/
  1000. for (i = 0; i < nz; i++) {
  1001. dz[i] = (FMIReal) (jacZTmp1[i] - jacZTmp2[i]) / (2.0 * h);
  1002. }
  1003. }else if(success1){
  1004. /*Forwad Difference*/
  1005. for (i = 0; i < nz; i++) {
  1006. dz[i] = (FMIReal) (jacZTmp1[i] - jacZ[i]) / h;
  1007. }
  1008. }else if(success2){
  1009. /*Backward Difference*/
  1010. for (i = 0; i < nz; i++) {
  1011. dz[i] = (FMIReal) ( jacZ[i]- jacZTmp2[i]) / h;
  1012. }
  1013. }else{
  1014. goto done2;
  1015. }
  1016. /* Restore */
  1017. if ((status = fmiSetReal_(c, v_ref, 1, jacV)) != FMIOK) {
  1018. goto done2;
  1019. }
  1020. done2:
  1021. comp->istruct->mInJacobian=0;
  1022. HANDLE_STATUS_RETURN(status);
  1023. }
  1024. }
  1025. /* Wrapper functions for merging CS and ME functions with fmi 2 RC1 */
  1026. DYMOLA_STATIC FMIComponent fmiInstantiate_(FMIString instanceName, FMIType fmuType, FMIString fmuGUID, FMIString fmuResourceLocation, const FMICallbackFunctions* functions, FMIBoolean visible, FMIBoolean loggingOn){
  1027. Component* comp;
  1028. if(fmuType==FMICoSimulation){
  1029. comp = (Component*) fmiInstantiateSlave_(instanceName, fmuGUID, fmuResourceLocation, functions, visible, loggingOn);
  1030. if(comp)
  1031. comp->isCoSim = FMITrue;
  1032. }else if(fmuType==FMIModelExchange){
  1033. comp = (Component*) fmiInstantiateModel_(instanceName, fmuGUID, fmuResourceLocation, functions, visible, loggingOn);
  1034. if(comp)
  1035. comp->isCoSim = FMIFalse;
  1036. }else{
  1037. return NULL;
  1038. }
  1039. return comp;
  1040. }
  1041. DYMOLA_STATIC void fmiFreeInstance_(FMIComponent c)
  1042. {
  1043. fmiFreeModelInstance_(c);
  1044. }
  1045. DYMOLA_STATIC FMIStatus fmiSetupExperiment_(FMIComponent c,
  1046. FMIBoolean relativeToleranceDefined,
  1047. FMIReal relativeTolerance,
  1048. FMIReal tStart,
  1049. FMIBoolean tStopDefined,
  1050. FMIReal tStop)
  1051. {
  1052. /* TODO fix warning of unsuported features for CS/ME now instead of at initialization*/
  1053. static FMIString label = "fmi2SetupExperiment";
  1054. Component* comp = (Component*) c;
  1055. comp->tStart = tStart;
  1056. comp->StopTimeDefined = tStopDefined;
  1057. comp->tStop = tStop;
  1058. if(relativeToleranceDefined && !comp->isCoSim ){
  1059. util_logger(comp, comp->instanceName, FMIWarning, "", "%s: tolerance control not supported for fmuType fmi2ModelExchange, setting toleranceDefined to fmi2False",label);
  1060. comp->relativeToleranceDefined = FMIFalse;
  1061. comp->relativeTolerance =0;
  1062. }else{
  1063. comp->relativeToleranceDefined = relativeToleranceDefined;
  1064. comp->relativeTolerance = relativeTolerance;
  1065. }
  1066. comp->time = tStart;
  1067. comp->icall = iDemandStart;
  1068. util_logger(comp, comp->instanceName, FMIOK, "", "%s: startTime is set to %g",label, tStart);
  1069. return FMIOK;
  1070. }
  1071. DYMOLA_STATIC FMIStatus fmiEnterInitializationMode_(FMIComponent c)
  1072. {
  1073. Component* comp = (Component*) c;
  1074. if(comp->isCoSim){
  1075. return fmiEnterSlaveInitializationMode_(comp, comp->relativeTolerance, comp->tStart, comp->StopTimeDefined, comp->tStop);
  1076. }else if(!comp->isCoSim){
  1077. return fmiEnterModelInitializationMode_(comp, FMIFalse, 0);
  1078. }
  1079. return util_error(comp);
  1080. }
  1081. DYMOLA_STATIC FMIStatus fmiExitInitializationMode_(FMIComponent c)
  1082. {
  1083. Component* comp = (Component*) c;
  1084. if(comp->isCoSim){
  1085. return fmiExitSlaveInitializationMode_(comp);
  1086. }else if(!comp->isCoSim){
  1087. return fmiExitModelInitializationMode_(comp);
  1088. }
  1089. return util_error(comp);
  1090. }
  1091. DYMOLA_STATIC FMIStatus fmiTerminate_(FMIComponent c)
  1092. {
  1093. Component* comp = (Component*) c;
  1094. if(comp->isCoSim){
  1095. return fmiTerminateSlave_(comp);
  1096. }else if(!comp->isCoSim){
  1097. return fmiTerminateModel_(comp);
  1098. }
  1099. return util_error(comp);
  1100. }
  1101. DYMOLA_STATIC FMIStatus fmiReset_(FMIComponent c)
  1102. {
  1103. return fmiResetSlave_(c);
  1104. }
  1105. #endif /* FMI_2 */
  1106. /* ----------------- local function definitions ----------------- */
  1107. static int getLargestIdemand(const FMIValueReference vr[], size_t nvr)
  1108. {
  1109. int idemand = iDemandStart;
  1110. size_t i;
  1111. /* find biggest idemand among requested values */
  1112. for (i = 0; i < nvr; i++) {
  1113. const FMIValueReference r = vr[i];
  1114. int id = FMI_IDEMAND(r);
  1115. if (id > idemand) {
  1116. idemand = id;
  1117. }
  1118. }
  1119. return idemand;
  1120. }