dsutil.h 65 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986
  1. /* dsutil.h */
  2. /*
  3. * Copyright (C) 1995-2001 Dynasim AB.
  4. * All rights reserved.
  5. *
  6. */
  7. /* Utility macros and functions for DSblock.
  8. Author: Hilding Elmqvist, Dynasim AB, 1995-02-21
  9. Version: 1.8, 1996-09-30
  10. 1.9, 1996-10-19: Added support for non-root finder.
  11. 1.10 1996-10-24: DymosimMessageDouble, etc.
  12. 1.11 1997-02-13: Correction of prototypes
  13. 1.12 1997-04-29: Introduced macro cross_
  14. 1.13 1997-05-29: Fixed bugs in function macros, made faster.
  15. 1.14 1997-11-16: Added gearwheel to PackShape
  16. 1.15 1997-11-23: - CombiTable support.
  17. - Longjmp introduced to stop model
  18. evaluation immediately after an error.
  19. - Fixed (minor) bugs in sqr(), setBoolean2/3.
  20. - matError (global variable) used in
  21. matrix I/O routines.
  22. - new switch DYMOSIM, for Dymosim related code.
  23. 1.16 1998-01-20 - Changed TimeEventAccuracy for Sample to 1E-12.
  24. - Added functions for Memory operator.
  25. - Modified pow for Simulink
  26. 1.17 1998-02-05: - solution of compatible singular systems added.
  27. - new signal definition routines
  28. blo3co_ (signal is constant)
  29. blo3sa_, blo3sb_, blo3ta_
  30. (scalar/matrix signal is an alias).
  31. blo3st_, blo3pt_ (data type of signal/parameter).
  32. - signum-macro added
  33. 1.18 1998-03-19: Introduced macro BreakDeclareFunction and
  34. BreakEquationsFunction for handling of large
  35. dsblockb.
  36. 1.19 1998-06-01: Introduced CheckForEvents
  37. 1.20 1998-10-05: Modified for dSPACE realtime
  38. 1.21 1999-01-20: Updated for DYMOLA_DSPACE, added new message routines
  39. 1.22 1999-01-23: Introduced new handling of the Sample function
  40. 1.23 1999-05-11: Removed calls of sprintf() for MATLAB/WATCOM 11.
  41. */
  42. #ifndef DSUTIL_H
  43. #define DSUTIL_H
  44. #include <string.h>
  45. #if defined(DYMOLA_DSPACE)
  46. #include <dsdefs.h>
  47. #else
  48. #include <stdio.h>
  49. #endif
  50. #if defined(RTLAB)
  51. #include <OpalPrint.h>
  52. #endif
  53. #if !defined(DYMOLA_STATIC)
  54. #if defined(__cplusplus)
  55. #define DYMOLA_STATIC extern
  56. #elif defined(RT) && !defined(DYM2DS)
  57. #define DYMOLA_STATIC static
  58. #else
  59. #define DYMOLA_STATIC
  60. #endif
  61. #endif
  62. #if !defined(DSE_STRUCT)
  63. #if defined(DS_EMBEDDED)
  64. #define DSE_STRUCT sts->
  65. #else
  66. #define DSE_STRUCT
  67. #endif
  68. #endif
  69. #include <float.h>
  70. #include <math.h>
  71. #include <stdlib.h>
  72. #include "delay.h"
  73. #include "dymtable.h"
  74. #include "matutil.h"
  75. #include "dymutil.h"
  76. #include "sprwat.h"
  77. /* General handling */
  78. #ifndef beginParallelSection
  79. #define beginParallelSection(layer, section)
  80. #endif
  81. #if defined(LIBDS_DLL) && defined(DYN_MULTINSTANCE)
  82. #define beginJmpParallelSections {struct MyJmpBuf saved_jmp_buf_env;int DYNLocalError=0;struct DymosimSimulator*saved_threadData;saved_jmp_buf_env=DYNGlobal_jmp_buf_env;saved_threadData=dymosimGetThreadData();
  83. #define beginJmpParallelSection dymosimSetThreadData(saved_threadData);DYNPropagateDidToThread(did_);if (setjmp(DYNGlobal_jmp_buf_env.TheBuffer)==0) {
  84. #define endJmpParallelSection } else DYNLocalError=1;
  85. #define endJmpParallelSections DYNGlobal_jmp_buf_env=saved_jmp_buf_env;if (DYNLocalError) {GlobalError_=DYNLocalError;longjmp(DYNGlobal_jmp_buf_env.TheBuffer, 1);}}
  86. #else
  87. #define beginJmpParallelSections {struct MyJmpBuf saved_jmp_buf_env;int DYNLocalError=0;saved_jmp_buf_env=DYNGlobal_jmp_buf_env;
  88. #define beginJmpParallelSection DYNPropagateDidToThread(did_);if (setjmp(DYNGlobal_jmp_buf_env.TheBuffer)==0) {
  89. #define endJmpParallelSection } else DYNLocalError=1;
  90. #define endJmpParallelSections DYNGlobal_jmp_buf_env=saved_jmp_buf_env;if (DYNLocalError) {GlobalError_=DYNLocalError;longjmp(DYNGlobal_jmp_buf_env.TheBuffer, 1);}}
  91. #endif
  92. #ifndef endParallelSection
  93. #define endParallelSection(layer, section)
  94. #endif
  95. #include "ModelicaUtilities.h"
  96. struct DYNInstanceData;
  97. DYMOLA_STATIC void ModelicaVFormatError(const char *string, va_list ap) {
  98. char buf[4000];
  99. #if defined(__WATCOMC__) && defined(DynSimStruct) || defined(NO_FILE) || defined(DYMOLA_DSPACE)
  100. strcpy(buf,string);
  101. #elif defined(_MSC_VER) && _MSC_VER>=1200
  102. _vsnprintf(buf,sizeof(buf)/sizeof(*buf),string,ap);
  103. #else
  104. vsprintf(buf,string,ap);
  105. #endif
  106. ModelicaFormatError("%s",buf);
  107. }
  108. DYMOLA_STATIC void ModelicaVFormatMessage(const char *string, va_list ap) {
  109. char buf[4000];
  110. #if defined(__WATCOMC__) && defined(DynSimStruct) || defined(NO_FILE) || defined(DYMOLA_DSPACE)
  111. strcpy(buf,string);
  112. #elif defined(_MSC_VER) && _MSC_VER>=1200
  113. _vsnprintf(buf,sizeof(buf)/sizeof(*buf),string,ap);
  114. #else
  115. vsprintf(buf,string,ap);
  116. #endif
  117. ModelicaFormatMessage("%s",buf);
  118. }
  119. struct FunctionContext_ {
  120. char*text;
  121. struct FunctionContext_*next;
  122. };
  123. #include <setjmp.h>
  124. struct MyJmpBuf {
  125. jmp_buf TheBuffer;
  126. };
  127. #if defined(DYN_MULTINSTANCE)
  128. #ifndef DYN_MarkObject
  129. #define DYN_MarkObject
  130. typedef double Real;
  131. #if defined(DYM2CCUR)
  132. typedef int Integer;
  133. #else
  134. typedef int Integer;
  135. #endif
  136. typedef const char* String;
  137. typedef Integer SizeType;
  138. struct BufferObject {Real *Realbuffer;Integer *Integerbuffer;SizeType* Sizebuffer;String* Stringbuffer;};
  139. struct MarkObject {struct BufferObject place,mark;};
  140. #endif
  141. struct DYN_ThreadData {
  142. struct FunctionContext_*m_global;
  143. int m_external;
  144. struct FunctionContext_ m_context;
  145. struct MarkObject m_start, m_current;
  146. String m_stringmark;
  147. String m_startstringmark;
  148. String m_endsimplestring;
  149. Real*m_endreal;
  150. Integer*m_endinteger;
  151. SizeType*m_endsize;
  152. String* m_endstring;
  153. struct MyJmpBuf m_jmp_buf_env;
  154. struct DYNInstanceData* did;
  155. };
  156. static struct DYN_ThreadData* DYN_GetThreadData();
  157. DYMOLA_STATIC void DYNPropagateDidToThread(struct DYNInstanceData*did) {
  158. DYN_GetThreadData()->did=did;
  159. }
  160. #else
  161. DYMOLA_STATIC void DYNPropagateDidToThread(struct DYNInstanceData*did) {;}
  162. #endif
  163. #if defined(DYN_MULTINSTANCE)
  164. #define FunctionContextGlobal_ (DYN_GetThreadData()->m_global)
  165. #define FunctionExternalContext_ (DYN_GetThreadData()->m_external)
  166. #define DYNGlobal_jmp_buf_env (DYN_GetThreadData()->m_jmp_buf_env)
  167. #else
  168. DYMOLA_STATIC struct FunctionContext_* FunctionContextGlobal_=0;
  169. DYMOLA_STATIC int FunctionExternalContext_=0;
  170. static struct MyJmpBuf DYNGlobal_jmp_buf_env;
  171. #if (defined(_OPENMP) && !defined(DISABLE_DYMOLA_OPENMP))
  172. #pragma omp threadprivate(FunctionContextGlobal_)
  173. #pragma omp threadprivate(FunctionExternalContext_)
  174. #pragma omp threadprivate(DYNGlobal_jmp_buf_env)
  175. #endif
  176. #endif
  177. #if DymolaNoGuard_
  178. #define PushContext(a) int oldFunctionExternal=FunctionExternalContext_;
  179. #define PopContext() FunctionExternalContext_=oldFunctionExternal;
  180. #define PushModelContext(b,a) (FunctionExternalContext_=0),
  181. #define PopModelContext() (FunctionExternalContext_=0)
  182. #else
  183. #if defined(DYN_MULTINSTANCE)
  184. #define modelContext_ (DYN_GetThreadData()->m_context)
  185. #else
  186. static struct FunctionContext_ modelContext_={0,0};
  187. #if (defined(_OPENMP) && !defined(DISABLE_DYMOLA_OPENMP))
  188. #pragma omp threadprivate(modelContext_)
  189. #endif
  190. #endif
  191. #define PushContext(a) int oldFunctionExternal=FunctionExternalContext_;struct FunctionContext_ alpha_={a};alpha_.next=FunctionContextGlobal_;FunctionContextGlobal_=&alpha_;
  192. #define PopContext() FunctionContextGlobal_=alpha_.next;FunctionExternalContext_=oldFunctionExternal;
  193. #define PushModelContext(b,a) (modelContext_.text=a,FunctionContextGlobal_=&modelContext_,FunctionExternalContext_=0),
  194. #define PopModelContext() (FunctionContextGlobal_=0,FunctionExternalContext_=0)
  195. #endif
  196. static int DYNIncrementFExternal();
  197. #ifdef DYMOSIM
  198. #if defined(LIBDS_DLL) || defined(LIBDS_API)
  199. LIBDS_API void DymosimMessageDymosim(const char* message);
  200. LIBDS_API void DymosimMessageDymosimSev(int severity, const char* message);
  201. #endif
  202. #if !defined(LIBDS_DLL)
  203. LIBDS_API void disstr_(const char* line, int len);
  204. #endif
  205. extern double increment(double incr);
  206. #endif
  207. #ifdef DYM2DS
  208. void d2ds_print0(const char*f, const char* msg);
  209. void d2ds_print1(const char*f, const char* msg, double d);
  210. void d2ds_print8(const char*f, const char*xexpr, const char*op, const char*yexpr, double x, const char*op2, double y, const char*t2, double t);
  211. #endif
  212. #if defined(DYM2CCUR) && defined(OMP_DYMOLA)
  213. extern void simwb_msg(const char*msg);
  214. #endif
  215. DYMOLA_STATIC void fmu_logger(const char* message, int newline, int severity);
  216. DYMOLA_STATIC void DymosimMessage(const char* message) {
  217. #ifdef DYMOSIM
  218. #ifdef LIBDS_DLL
  219. DymosimMessageDymosim(message);
  220. #else
  221. int i = strlen(message);
  222. disstr_(message, i);
  223. #endif
  224. #elif defined(RTLAB)
  225. OpalPrint("%s\n", message);
  226. #elif defined(Matlab6) && !defined(NO_FILE) && defined(ssPrintf)
  227. ssPrintf("%s\n", message);
  228. #elif defined(DYM2DS)
  229. d2ds_print0("%s\n", message);
  230. #elif defined(DYM2CCUR) && defined(OMP_DYMOLA)
  231. simwb_msg(message);
  232. #elif defined(BUILDFMU)
  233. fmu_logger(message, 1, 0);
  234. #else
  235. printf("%s\n", message);
  236. #endif
  237. }
  238. #if !defined(DYMOSIM) && !defined(LIBDS_DLL)
  239. static void DymosimMessageDymosimSev(int severity, const char*message) {
  240. #if defined(BUILDFMU)
  241. fmu_logger(message, 1, severity);
  242. #else
  243. DymosimMessage(message);
  244. #endif
  245. }
  246. #endif
  247. DYMOLA_STATIC void DymosimMessageNoNL(const char*message) {
  248. #ifdef DYMOSIM
  249. LIBDS_API void aprint(int type, const char *str);
  250. aprint(0,(char*)(message));
  251. #elif defined(RTLAB)
  252. OpalPrint("%s", message);
  253. #elif defined(Matlab6) && !defined(NO_FILE) && defined(ssPrintf)
  254. ssPrintf("%s", message);
  255. #elif defined(DYM2DS)
  256. d2ds_print0("%s", message);
  257. #elif defined(DYM2CCUR) && defined(OMP_DYMOLA)
  258. simwb_msg(message);
  259. #elif defined(BUILDFMU)
  260. fmu_logger(message, 0, 0);
  261. #else
  262. printf("%s", message);
  263. #endif
  264. }
  265. #define homotopy(a,b) DYNhomotopy(a,b,did_)
  266. DYMOLA_STATIC double DYNhomotopy(double actual, double simple,struct DYNInstanceData*did_);
  267. DYMOLA_STATIC double DymolaTimerCounterSince(double*d,int i);
  268. DYMOLA_STATIC double MyCPUTime(void);
  269. DYMOLA_STATIC double MyCPUTime(void) {
  270. static int i=0;
  271. static double d;
  272. double res=DymolaTimerCounterSince(&d,i);
  273. i=1;
  274. return res;
  275. }
  276. #if defined(DYMOLA_DSPACE)
  277. #define CurrentClockTime CPUClk
  278. #else
  279. #define CurrentClockTime MyCPUTime()
  280. #endif
  281. DYMOLA_STATIC void DymosimMessageDoubleSev(int severity, const char* message, double d) {
  282. #if !defined(DynSimStruct)
  283. char textline[200];
  284. sprintf(textline, "%.160s %.16G", message, d);
  285. DymosimMessageDymosimSev(severity, textline);
  286. #elif defined(RTLAB)
  287. OpalPrint("%s %.16G\n", message, d);
  288. #elif defined(BUILDFMU)
  289. fmu_logger(message, 1, severity);
  290. #elif defined(Matlab6) && !defined(NO_FILE) && defined(ssPrintf)
  291. ssPrintf("%s %.16G\n", message, d);
  292. #elif defined(DYM2DS)
  293. d2ds_print1("%s %.16G\n", message, d);
  294. #else
  295. printf("%s %.16G\n", message, d);
  296. #endif
  297. }
  298. DYMOLA_STATIC void DymosimMessageDouble(const char* message, double d) {
  299. DymosimMessageDoubleSev(0, message, d);
  300. }
  301. DYMOLA_STATIC void DymosimMessageDoubleVector(const char* matrixname, double * d, int n) {
  302. #if !defined(DynSimStruct)
  303. char text[100];
  304. char textline[2000];
  305. int i;
  306. textline[0] = '\0';
  307. strcat(textline, matrixname);
  308. strcat(textline, " = {");
  309. for (i = 0; i < n; i++) {
  310. if (i > 0)
  311. strcat(textline, ", ");
  312. sprintf(text, "%G", d[i]);
  313. strcat(textline, text);
  314. }
  315. strcat(textline, "}");
  316. DymosimMessage(textline);
  317. #elif defined(RTLAB)
  318. int i;
  319. OpalPrint("%s = {", matrixname);
  320. for (i = 0; i < n; i++) {
  321. if (i > 0)
  322. OpalPrint(", ");
  323. OpalPrint("%G", d[i]);
  324. }
  325. OpalPrint("}\n");
  326. #elif defined(Matlab6) && !defined(NO_FILE) && defined(ssPrintf)
  327. int i;
  328. ssPrintf("%s = {", matrixname);
  329. for (i = 0; i < n; i++) {
  330. if (i > 0)
  331. ssPrintf(", ");
  332. ssPrintf("%G", d[i]);
  333. }
  334. ssPrintf("}\n");
  335. #elif defined(DYM2DS)
  336. int i;
  337. d2ds_print0("%s = {", matrixname);
  338. for (i = 0; i < n; i++) {
  339. if (i > 0)
  340. d2ds_print0("%s", ", ");
  341. d2ds_print1("%s%G", "", d[i]);
  342. }
  343. d2ds_print0("%s\n", "}");
  344. #else
  345. int i;
  346. printf("%s = {", matrixname);
  347. for (i = 0; i < n; i++) {
  348. if (i > 0)
  349. printf(", ");
  350. printf("%G", d[i]);
  351. }
  352. printf("}\n");
  353. #endif
  354. }
  355. #define SetElement(Mat, i, j, nRow, val) \
  356. Mat[(i)-1 + ((j)-1)*(nRow)] = val
  357. #define GetElement(Mat, i, j, nRow) \
  358. Mat[(i)-1 + ((j)-1)*(nRow)]
  359. DYMOLA_STATIC void DymosimMessageDoubleMatrix(const char* matrixname, double * d, int m, int n, int MaxRow) {
  360. /* Writes matrix in Modelica format.
  361. name = {
  362. {1, 2, 3},
  363. {4, 5, 6} };
  364. */
  365. #if !defined(DynSimStruct)
  366. char text[100];
  367. char textline[300];
  368. int i, j;
  369. textline[0] = '\0';
  370. strcat(textline, matrixname);
  371. strcat(textline, " = ");
  372. strcat(textline, "{");
  373. DymosimMessage(textline);
  374. textline[0] = '\0';
  375. DymosimMessage("");
  376. for (i = 1; i <= m; i++) {
  377. strcat(textline, "{");
  378. for (j = 1; j <= n; j++) {
  379. if (j > 1)
  380. strcat(textline, ", ");
  381. if (strlen(textline)>sizeof(textline)-100) {
  382. DymosimMessage(textline);
  383. textline[0] = '\0';
  384. }
  385. sprintf(text, "%G", GetElement(d, i, j, MaxRow));
  386. strcat(textline, text);
  387. }
  388. strcat(textline, "}");
  389. if (i < m)
  390. strcat(textline, ", ");
  391. DymosimMessage(textline);
  392. textline[0] = '\0';
  393. }
  394. DymosimMessage("");
  395. DymosimMessage("};");
  396. #elif defined(RTLAB)
  397. int i, j;
  398. OpalPrint("%s = {\n", matrixname);
  399. for (i = 1; i <= m; i++) {
  400. OpalPrint("{");
  401. for (j = 1; j <= n; j++) {
  402. if (j > 1)
  403. OpalPrint(", ");
  404. OpalPrint("%G", GetElement(d, i, j, MaxRow));
  405. }
  406. OpalPrint("}");
  407. if (i < m)
  408. OpalPrint(", ");
  409. OpalPrint("\n");
  410. }
  411. OpalPrint("};\n");
  412. #elif defined(Matlab6) && !defined(NO_FILE) && defined(ssPrintf)
  413. int i, j;
  414. ssPrintf("%s = {\n", matrixname);
  415. for (i = 1; i <= m; i++) {
  416. ssPrintf("{");
  417. for (j = 1; j <= n; j++) {
  418. if (j > 1)
  419. ssPrintf(", ");
  420. ssPrintf("%G", GetElement(d, i, j, MaxRow));
  421. }
  422. ssPrintf("}");
  423. if (i < m)
  424. ssPrintf(", ");
  425. ssPrintf("\n");
  426. }
  427. ssPrintf("};\n");
  428. #else
  429. int i, j;
  430. printf("%s = {\n", matrixname);
  431. for (i = 1; i <= m; i++) {
  432. printf("{");
  433. for (j = 1; j <= n; j++) {
  434. if (j > 1)
  435. printf(", ");
  436. printf("%G", GetElement(d, i, j, MaxRow));
  437. }
  438. printf("}");
  439. if (i < m)
  440. printf(", ");
  441. printf("\n");
  442. }
  443. printf("};\n");
  444. #endif
  445. }
  446. DYMOLA_STATIC void appendVector(double * mat, int MaxRow, int MaxCol, int *nRow, double * vec, int MaxVec) {
  447. int i;
  448. *nRow += 1;
  449. for (i = 1; i <= MaxCol; i++) {
  450. SetElement(mat, *nRow, i, MaxRow, i<=MaxVec ? GetElement(vec, i, 1, MaxVec) : 0);
  451. }
  452. }
  453. DYMOLA_STATIC void DymosimMessageInt(const char* message, int i) {
  454. #if !defined(DynSimStruct)
  455. char textline[200];
  456. sprintf(textline, "%.160s %i", message, i);
  457. DymosimMessage(textline);
  458. #elif defined(RTLAB)
  459. OpalPrint("%s, %li\n", message, i);
  460. #elif defined(Matlab6) && !defined(NO_FILE) && defined(ssPrintf)
  461. ssPrintf("%s, %li\n", message, i);
  462. #elif defined(DYM2DS)
  463. d2ds_print1("%s \n", message, (double)i);
  464. #else
  465. printf("%s, %li\n", message, i);
  466. #endif
  467. }
  468. DYMOLA_STATIC void DymosimMessageMatrixElement(char* name, int i, int j, double d) {
  469. #if !defined(DynSimStruct)
  470. char textline[200];
  471. sprintf(textline, " %.160s[%i, %i] = %G", name, i, j, d);
  472. DymosimMessage(textline);
  473. #elif defined(RTLAB)
  474. OpalPrint("%s[%li, %li] = %G\n", name, i, j, d);
  475. #elif defined(Matlab6) && !defined(NO_FILE) && defined(ssPrintf)
  476. ssPrintf("%s[%li, %li] = %G\n", name, i, j, d);
  477. #else
  478. printf("%s[%li, %li] = %G\n", name, i, j, d);
  479. #endif
  480. }
  481. static double DymosimGetTime2();
  482. DYMOLA_STATIC int* GlobalErrorPointer(void);
  483. DYMOLA_STATIC double DymosimErrorLev(const char* message, int lev) {
  484. if (!(getBasicIDymosimStruct()->mPrintEvent&(1<<10))) {
  485. DymosimMessageDoubleSev(lev,(lev==1)?"The following was detected at time:" :"The following error was detected at time:", DymosimGetTime2());
  486. DymosimMessageDymosimSev(lev, message);
  487. if (FunctionContextGlobal_) {
  488. DymosimMessageDymosimSev(lev, "The stack of functions is:");
  489. for(;FunctionContextGlobal_;FunctionContextGlobal_=FunctionContextGlobal_->next) {
  490. DymosimMessageDymosimSev(lev, FunctionContextGlobal_->text);
  491. }
  492. }
  493. }
  494. if (lev==1) return 0.0;
  495. *(GlobalErrorPointer()) = 1;
  496. longjmp(DYNGlobal_jmp_buf_env.TheBuffer, 1);
  497. return 0.0;
  498. }
  499. DYMOLA_STATIC double DymosimError(const char* message) {
  500. DymosimErrorLev(message, 2);
  501. return 0;
  502. }
  503. DYMOLA_STATIC void AssertModelicaF(int b,const char*bs,const char*x) {
  504. if (!b) {
  505. DymosimMessageDymosimSev(2, bs);
  506. DymosimError(x); /* should not return. */
  507. }
  508. }
  509. DYMOLA_STATIC void AssertModelicaF4(int b,const char*bs,const char*x,int lev,int*hi) {
  510. if (!b) {
  511. if (lev==1) {
  512. if (hi) {
  513. if (*hi==1) return; /* Skipping */
  514. *hi=1;
  515. }
  516. DymosimMessageDymosimSev(1, bs);
  517. DymosimErrorLev(x, 1);
  518. } else {
  519. DymosimMessageDymosimSev(2, bs);
  520. DymosimError(x); /* should not return. */
  521. }
  522. }
  523. }
  524. DYMOLA_STATIC int DymosimLog(char* message, double v) {
  525. DymosimMessageDouble(message, v);
  526. return 0;
  527. }
  528. static double LogRelation(char *op, char *xexpr, char *yexpr, double x, double y,
  529. Dymola_bool val, double t) {
  530. /* Example: LogRelation(">", #x, #y, x, y, (x) > (y), Time) */
  531. #if !defined(DynSimStruct)
  532. char buf[1000];
  533. char *boolval;
  534. boolval = "false";
  535. if (val)
  536. boolval = "true";
  537. sprintf(buf, " Relation %.400s %s %.400s (%g %s %g) became %s at time = %g",
  538. xexpr, op, yexpr, x, op, y, boolval, t);
  539. DymosimMessage(buf);
  540. #elif defined(RTLAB)
  541. OpalPrint(" Relation %s %s %s (%g %s %g) became %s at time = %g\n",
  542. xexpr, op, yexpr, x, op, y, (val ? "true" : "false"), t);
  543. #elif defined(Matlab6) && !defined(NO_FILE) && defined(ssPrintf)
  544. ssPrintf(" Relation %s %s %s (%g %s %g) became %s at time = %g\n",
  545. xexpr, op, yexpr, x, op, y, (val ? "true" : "false"), t);
  546. #elif defined(DYM2DS)
  547. d2ds_print8(" Relation %s %s %s (%g %s %g) became %s at time = %g\n",
  548. xexpr, op, yexpr, x, op, y, (val ? "true" : "false"), t);
  549. #else
  550. printf(" Relation %s %s %s (%g %s %g) became %s at time = %g\n",
  551. xexpr, op, yexpr, x, op, y, (val ? "true" : "false"), t);
  552. #endif
  553. return 1.0;
  554. }
  555. static double VariableChanged(char *var, double val, double t) {
  556. #if !defined(DynSimStruct)
  557. char buf[1000];
  558. sprintf(buf, " Variable %.400s = %g at time %g", var, val, t);
  559. DymosimMessage(buf);
  560. #elif defined(RTLAB)
  561. OpalPrint(" Variable %s = %g at time %g\n", var, val, t);
  562. #elif defined(Matlab6) && !defined(NO_FILE) && defined(ssPrintf)
  563. ssPrintf(" Variable %s = %g at time %g\n", var, val, t);
  564. #else
  565. printf(" Variable %s = %g at time %g\n", var, val, t);
  566. #endif
  567. return 1.0;
  568. }
  569. static double diverr(const char *xexpr, double x, const char *yexpr, double y) {
  570. #if !defined(DynSimStruct)
  571. char buf[1000];
  572. sprintf(buf, "\nModel error - division by zero: (%.400s) / (%.400s) = (%g) / (%g)\n", xexpr, yexpr, x, y);
  573. DymosimError(buf);
  574. #elif defined(RTLAB)
  575. if (!(getBasicIDymosimStruct()->mPrintEvent&(1<<10))) {
  576. OpalPrint("\nModel error - division by zero: (%s) / (%s) = (%g) / (%g)\n", xexpr, yexpr, x, y);
  577. }
  578. DymosimError("Model error - division by zero");
  579. #elif defined(Matlab6) && !defined(NO_FILE) && defined(ssPrintf)
  580. if (!(getBasicIDymosimStruct()->mPrintEvent&(1<<10))) {
  581. ssPrintf("\nModel error - division by zero: (%s) / (%s) = (%g) / (%g)\n", xexpr, yexpr, x, y);
  582. }
  583. DymosimError("Model error - division by zero");
  584. #else
  585. if (!(getBasicIDymosimStruct()->mPrintEvent&(1<<10))) {
  586. printf("\nModel error - division by zero: (%s) / (%s) = (%g) / (%g)\n", xexpr, yexpr, x, y);
  587. }
  588. DymosimError("Model error - division by zero");
  589. #endif
  590. return 1.0;
  591. }
  592. static
  593. #if defined(_MSC_VER) && _MSC_VER>=1200
  594. __inline
  595. #elif __GNUC__
  596. __inline
  597. #endif
  598. double divmacro(double x,const char*sx,double y,const char*sy) {
  599. return (y!=0.0) ? ( (double) (x) / (double) (y) ) : diverr(sx,(x),sy,(y));
  600. }
  601. #if DymolaNoGuard_
  602. #define divmacro(x,sx,y,sy) (((double)(x))/(y))
  603. #endif
  604. #if DymolaNoGuard_
  605. static
  606. #if defined(_MSC_VER) && _MSC_VER>=1200
  607. __inline
  608. #elif __GNUC__
  609. __inline
  610. #endif
  611. void GuardedError(const char*xexpr, double x,const char*op) {
  612. return;
  613. }
  614. #else
  615. static void GuardedError(const char*xexpr, double x,const char*op) {
  616. #if !defined(DynSimStruct)
  617. char buf[1000];
  618. sprintf(buf, "\nModel error - %s (%.400s) = %s (%g)\n", op,xexpr,op,x);
  619. DymosimError(buf);
  620. #elif defined(RTLAB)
  621. if (!(getBasicIDymosimStruct()->mPrintEvent&(1<<10))) {
  622. OpalPrint("\nModel error - %s (%.400s) = %s (%g)\n", op,xexpr,op,x);
  623. }
  624. DymosimError("Model error - illegal value");
  625. #elif defined(Matlab6) && !defined(NO_FILE) && defined(ssPrintf)
  626. if (!(getBasicIDymosimStruct()->mPrintEvent&(1<<10))) {
  627. ssPrintf("\nModel error - %s (%.400s) = %s (%g)\n", op,xexpr,op,x);
  628. }
  629. DymosimError("Model error - illegal value");
  630. #else
  631. if (!(getBasicIDymosimStruct()->mPrintEvent&(1<<10))) {
  632. printf("\nModel error - %s (%.400s) = %s (%g)\n", op,xexpr,op,x);
  633. }
  634. DymosimError("Model error - illegal value");
  635. #endif
  636. }
  637. #endif
  638. static
  639. #if defined(_MSC_VER) && _MSC_VER>=1200
  640. __inline
  641. #elif __GNUC__
  642. __inline
  643. #endif
  644. double logGuarded(double x,const char*sx) {
  645. if (x<=0) GuardedError(sx, x,"Modelica.Math.log");
  646. return log(x);
  647. }
  648. static
  649. #if defined(_MSC_VER) && _MSC_VER>=1200
  650. __inline
  651. #elif __GNUC__
  652. __inline
  653. #endif
  654. double log10Guarded(double x,const char*sx) {
  655. if (x<=0) GuardedError(sx, x,"Modelica.Math.log10");
  656. return log10(x);
  657. }
  658. static
  659. #if defined(_MSC_VER) && _MSC_VER>=1200
  660. __inline
  661. #elif __GNUC__
  662. __inline
  663. #endif
  664. double asinGuarded(double x,const char*sx) {
  665. if (x<-1 || x>1) GuardedError(sx, x,"Modelica.Math.asin");
  666. return asin(x);
  667. }
  668. static
  669. #if defined(_MSC_VER) && _MSC_VER>=1200
  670. __inline
  671. #elif __GNUC__
  672. __inline
  673. #endif
  674. double acosGuarded(double x,const char*sx) {
  675. if (x<-1 || x>1) GuardedError(sx, x,"Modelica.Math.acos");
  676. return acos(x);
  677. }
  678. /* For old models: */
  679. #define div(x,y) \
  680. ( (y) != 0.0 ? ( (double) (x) / (double) (y) ) : diverr(#x, (x), #y, (y)) )
  681. /* div in Modelica spec */
  682. static double divideDymola(double x,double y) {double quot=x/y;return (quot>=0)?floor(quot):ceil(quot);}
  683. /* rem in Modelica spec */
  684. static double remainderDymola(double x,double y) {return x-divideDymola(x,y)*y;}
  685. static double modulusDymola(double x,double y) {return x-floor(x/y)*y;}
  686. static double powerr(const char *xexpr, double x, const char *yexpr, double y) {
  687. #if !defined(DynSimStruct)
  688. char buf[1000];
  689. sprintf(buf, "\nModel error - power: (%.400s) ^ (%.400s) = (%g) ^ (%g)\n", xexpr, yexpr, x, y);
  690. DymosimError(buf);
  691. #elif defined(RTLAB)
  692. if (!(getBasicIDymosimStruct()->mPrintEvent&(1<<10))) {
  693. OpalPrint("\nModel error - power: (%s) ^ (%s) = (%g) ^ (%g)\n", xexpr, yexpr, x, y);
  694. }
  695. DymosimError("Model error - power");
  696. #elif defined(Matlab6) && !defined(NO_FILE) && defined(ssPrintf)
  697. if (!(getBasicIDymosimStruct()->mPrintEvent&(1<<10))) {
  698. ssPrintf("\nModel error - power: (%s) ^ (%s) = (%g) ^ (%g)\n", xexpr, yexpr, x, y);
  699. }
  700. DymosimError("Model error - power");
  701. #else
  702. if (!(getBasicIDymosimStruct()->mPrintEvent&(1<<10))) {
  703. printf("\nModel error - power: (%s) ^ (%s) = (%g) ^ (%g)\n", xexpr, yexpr, x, y);
  704. }
  705. DymosimError("Model error - power");
  706. #endif
  707. return 1.0;
  708. }
  709. static double powmacro(double x, const char* sx, double y, const char* sy)
  710. {
  711. return (x == 0.0 && y <= 0.0) || (x < 0.0 && floor(y) != y) ?
  712. powerr(sx, x, sy, y) : pow(x, y);
  713. }
  714. static double powmacro2(double x, const char* sx, int y, const char* sy)
  715. {
  716. return (x == 0.0 && y < 0) ?
  717. powerr(sx, x, sy, y) : pow(x, y);
  718. }
  719. #define pow(x,y) powmacro((x), #x, (y), #y)
  720. #undef Dymola_abs
  721. #define Dymola_abs(x) \
  722. fabs(x)
  723. static
  724. #if defined(_MSC_VER) && _MSC_VER>=1200
  725. __inline
  726. #elif __GNUC__
  727. __inline
  728. #endif
  729. double sqr(double x) {
  730. return x*x;
  731. }
  732. static
  733. #if defined(_MSC_VER) && _MSC_VER>=1200
  734. __inline
  735. #elif __GNUC__
  736. __inline
  737. #endif
  738. double sqrtGuarded(double x,const char*sx) {
  739. if (x>=0) return (sqrt)(x);
  740. else {
  741. GuardedError(sx, x, "sqrt");
  742. return 0;
  743. }
  744. }
  745. static
  746. #if defined(_MSC_VER) && _MSC_VER>=1200
  747. __inline
  748. #elif __GNUC__
  749. __inline
  750. #endif
  751. double sqrt__(double x) {
  752. static const double lim=0;
  753. if (x>lim) return (sqrt)(x);
  754. else if (x>0) return x*(sqrt)(lim)/lim;
  755. else return 0;
  756. }
  757. static double sqrtder(double x) {
  758. static const double lim=0;
  759. if (x>lim) return 0.5/(sqrt)(x);
  760. else if (x>0) return (sqrt)(lim)/lim;
  761. else return 0;
  762. }
  763. #define sqrt(x) sqrt__(x)
  764. static
  765. #if defined(_MSC_VER) && _MSC_VER>=1200
  766. __inline
  767. #elif __GNUC__
  768. __inline
  769. #endif
  770. double Dymola_exp(double x) {
  771. static const double lim=690.8;
  772. if (x<lim) return (exp)(x);
  773. return (exp)(lim);
  774. }
  775. #define exp(x) Dymola_exp(x)
  776. /*
  777. (x < log(DBL_MAX/10) ? exp(x) : DBL_MAX/10)
  778. */
  779. static
  780. #if defined(_MSC_VER) && _MSC_VER>=1200
  781. __inline
  782. #elif __GNUC__
  783. __inline
  784. #endif
  785. double Dymola_ln(double x) {
  786. static const double lim=1e-100;
  787. if (x>lim) return (log)(x);
  788. return (log)(1e-00);
  789. }
  790. #define ln(x) Dymola_ln(x)
  791. #undef Dymola_min
  792. #define Dymola_min(x,y) ((x) < (y) ? (x) : (y) )
  793. #undef Dymola_max
  794. #define Dymola_max(x,y) ((x) > (y) ? (x) : (y) )
  795. static
  796. #if defined(_MSC_VER) && _MSC_VER>=1200
  797. __inline
  798. #elif __GNUC__
  799. __inline
  800. #endif
  801. double RealBmin(double x,double y) {return ((x) < (y) ? (x) : (y) );}
  802. static
  803. #if defined(_MSC_VER) && _MSC_VER>=1200
  804. __inline
  805. #elif __GNUC__
  806. __inline
  807. #endif
  808. double RealBmax(double x,double y) {return ((x) > (y) ? (x) : (y) );}
  809. static
  810. #if defined(_MSC_VER) && _MSC_VER>=1200
  811. __inline
  812. #elif __GNUC__
  813. __inline
  814. #endif
  815. int IntegerBmin(int x,int y) {return ((x) < (y) ? (x) : (y) );}
  816. static
  817. #if defined(_MSC_VER) && _MSC_VER>=1200
  818. __inline
  819. #elif __GNUC__
  820. __inline
  821. #endif
  822. int IntegerBmax(int x,int y) {return ((x) > (y) ? (x) : (y) );}
  823. #define IfThenElse(c, x, y) \
  824. (c ? x : y)
  825. #define signum(x) ( ((x)>0.0) ? 1.0 : ((x)<0.0) ? -1.0 : 0.0 )
  826. #define arctan(x) atan(x)
  827. #define arctan2(y,x) atan2(y,x)
  828. #define NoEvaluate(x) (x)
  829. #define NoEvent(x) (x)
  830. #if __GNUC__
  831. static double tanh__(double x) {
  832. return fabs(x)<1e2 ? (tanh)(x) : signum(x);
  833. }
  834. #define tanh(x) tanh__(x)
  835. #endif
  836. /* Pseudo-random number generators for Dymosim.
  837. *
  838. * RandomUniform(time) Uniformly distributed random number,
  839. * range [0, 1].
  840. *
  841. * RandomNormal(time) Normally distrubuted random number with
  842. * mean 0 and variance 1.
  843. *
  844. * RandomSeed(seed) Initializes the random number generator.
  845. *
  846. * Note: the ANSI/ISO Standard C function rand() has alleged bad random
  847. * properties in many implementations. For that reason, we use random()
  848. * when possible.
  849. *
  850. */
  851. #if defined(SUN) || defined(HAS_RANDOM)
  852. #define RandomUniform(time) ( (random() % 1000000) / 999999.0 )
  853. #define RandomSeed(seed) (srandom((unsigned) seed), 0)
  854. #else
  855. #define RandomUniform(time) ( (double) rand() / (double) RAND_MAX )
  856. #define RandomSeed(seed) (srand((unsigned) seed), 0)
  857. #endif
  858. static double RandomNormal(double time)
  859. {
  860. int i;
  861. double r = -6;
  862. for (i = 0; i < 12; i++) r += RandomUniform(time);
  863. return r;
  864. }
  865. static double log_variable(char *xname, double t, double x) {
  866. #if !defined(DynSimStruct)
  867. char buf[500];
  868. sprintf(buf, "%.400s(%g) = %.16g", xname, t, x);
  869. DymosimMessage(buf);
  870. #elif defined(RTLAB)
  871. OpalPrint("%s(%g) = %.16g\n", xname, t, x);
  872. #elif defined(Matlab6) && !defined(NO_FILE) && defined(ssPrintf)
  873. ssPrintf("%s(%g) = %.16g\n", xname, t, x);
  874. #else
  875. printf("%s(%g) = %.16g\n", xname, t, x);
  876. #endif
  877. return x;
  878. }
  879. #define LogVariable(x) \
  880. log_variable(#x, DymosimGetTime2(), x)
  881. DYMOLA_STATIC double PackShape(char * shapename)
  882. {
  883. if (strcmp(shapename, "box") == 0)
  884. return 101.0;
  885. else if (strcmp(shapename, "sphere") == 0)
  886. return 102.0;
  887. else if (strcmp(shapename, "cylinder") == 0)
  888. return 103.0;
  889. else if (strcmp(shapename, "cone") == 0)
  890. return 104.0;
  891. else if (strcmp(shapename, "pipe") == 0)
  892. return 105.0;
  893. else if (strcmp(shapename, "beam") == 0)
  894. return 106.0;
  895. else if (strcmp(shapename, "wirebox") == 0)
  896. return 107.0;
  897. else if (strcmp(shapename, "gearwheel") == 0)
  898. return 108.0;
  899. else if (strcmp(shapename, "vector") == 0)
  900. return 109.0;
  901. else if (strcmp(shapename, "pipecylinder") == 0)
  902. return 110.0;
  903. else if (strcmp(shapename, "spring") == 0)
  904. return 111.0;
  905. else if (strcmp(shapename, "tire")==0 || strcmp(shapename,"tyre")==0)
  906. return 112.0;
  907. else {
  908. return 200.0+atoi(shapename);
  909. }
  910. }
  911. DYMOLA_STATIC double PackMaterial(double d1, double d2, double d3, double d4)
  912. {
  913. int a, b, c, d, ires;
  914. double res;
  915. /* Pack arguments in the range 0.00-1.00 and scale to 0-99. */
  916. a = (int) (d1*100 - 0.5);
  917. if (a < 0) a = 0;
  918. if (a > 99) a = 99;
  919. b = (int) (d2*100 - 0.5);
  920. if (b < 0) b = 0;
  921. if (b > 99) b = 99;
  922. c = (int) (d3*100 - 0.5);
  923. if (c < 0) c = 0;
  924. if (c > 99) c = 99;
  925. d = (int) (d4*10 - 0.5);
  926. if (d < 0) d = 0;
  927. if (d > 9) d = 9;
  928. /* Construct large integer packing arguments */
  929. ires = ((((a * 100) + b) * 100) + c) * 10 + d;
  930. res = ires;
  931. return res;
  932. }
  933. /* Event handling */
  934. LIBDS_API void handleevent3(const char *, int *, double *,
  935. double *, double , Dymola_bool , int , double ,
  936. double , Dymola_bool *, Dymola_bool eventiter, double largeeps, Dymola_bool slow);
  937. LIBDS_API int booleanchanged_(const char *, Dymola_bool *,
  938. Dymola_bool *, int *, Dymola_bool *, int);
  939. #if 1
  940. #define HandleEvent(cross,expr,index) \
  941. QRel_[index-1] = cross; \
  942. if (DYNEvent) { \
  943. handleevent3(expr, &QM_[index], &Qp_[index-1], &Qn_[index-1], QRel_[index-1], \
  944. Init_, PrintEvent, EPS_, *time, &AnyEvent_, Qenable_[index], LargeEPS_, Iter>SlowIter_); \
  945. } \
  946. if (RootFinder_) { \
  947. (DSE_STRUCT QZ_)[2*index-2] = QRel_[index-1] - Qp_[index-1]; \
  948. (DSE_STRUCT QZ_)[2*index-1] = QRel_[index-1] - Qn_[index-1]; \
  949. }
  950. #else
  951. #define HandleEvent(cross,expr,index) \
  952. QRel_[index-1] = cross; \
  953. if (DYNEvent) { \
  954. handleevent_(expr, &QM_[index], &Qp_[index-1], &Qn_[index-1], &QRel_[index-1], \
  955. &Init_, &PrintEvent, &EPS_, time, &AnyEvent_, strlen(expr)); \
  956. } \
  957. if (RootFinder_) { \
  958. QZ_[2*index-2] = QRel_[index-1] - Qp_[index-1]; \
  959. QZ_[2*index-1] = QRel_[index-1] - Qn_[index-1]; \
  960. }
  961. #endif
  962. LIBDS_API void handleevent4(const char*rele,const char*sube,Dymola_bool*ql,double*qp,double*qn,double eps,double qrel,Dymola_bool init,int printevent,
  963. Dymola_bool*anyevent,Dymola_bool ltz,Dymola_bool invres,Dymola_bool eventiter,double largeeps,Dymola_bool slow);
  964. LIBDS_API void handleevent5(const char*rele,const char*sube,Dymola_bool*ql,double*qp,double*qn,double eps,double qrel,Dymola_bool init,int printevent,
  965. Dymola_bool*anyevent,Dymola_bool*anyEvent2,Dymola_bool ltz,Dymola_bool invres,Dymola_bool eventiter,double largeeps,Dymola_bool slow);
  966. #define HandleEvent2A(cross,xexpr,rel,yexpr,index,ltz,invres) \
  967. ((QRel_[index-1] = cross, \
  968. ((DYNEvent) ? \
  969. (handleevent4(xexpr " " rel " " yexpr,"(" xexpr ")-(" yexpr ")",&QL_[index], &Qp_[index-1],&Qn_[index-1],EPS_*Qscaled_[index-1],QRel_[index-1],Init_, PrintEvent,&AnyEvent_,ltz,invres,Qenable_[index],LargeEPS_*Qscaled_[index-1],Iter>SlowIter_),0):0), \
  970. (DSE_STRUCT QZ_)[2*(index)-2] = QRel_[index-1] - Qp_[index-1], \
  971. (DSE_STRUCT QZ_)[2*(index)-1] = (DSE_STRUCT QZ_)[2*(index)-2]),Qenable_[index]=true,QL_[index])
  972. #define HandleEvent2(cross,xexpr,rel,yexpr,index,ltz,invres) \
  973. ((DYNX(QRel_,index-1) = cross, \
  974. ((DYNEvent) ? \
  975. (handleevent4func(did_,xexpr " " rel " " yexpr,"(" xexpr ")-(" yexpr ")",index,PrintEvent,ltz,invres),0):0), \
  976. DYNX((DSE_STRUCT QZ_),2*(index)-2) = DYNX(QRel_,index-1) - DYNX(Qp_,index-1), \
  977. DYNX((DSE_STRUCT QZ_),2*(index)-1) = DYNX((DSE_STRUCT QZ_),2*(index)-2)),DYNX(Qenable_,index)=true,DYNX(QL_,index))
  978. #define HandleEvent2Minor(cross,xexpr,rel,yexpr,index,ltz,invres) \
  979. ((DYNX(QRel_,index-1) = cross, \
  980. ((DYNEvent) ? \
  981. (handleevent4funcMinor(did_,xexpr " " rel " " yexpr,"(" xexpr ")-(" yexpr ")",index,PrintEvent,ltz,invres),0):0), \
  982. DYNX((DSE_STRUCT QZ_),2*(index)-2) = DYNX(QRel_,index-1) - DYNX(Qp_,index-1), \
  983. DYNX((DSE_STRUCT QZ_),2*(index)-1) = DYNX((DSE_STRUCT QZ_),2*(index)-2)),DYNX(Qenable_,index)=true,DYNX(QL_,index))
  984. LIBDS_API Dymola_bool handleevent4S(const char*rele,const char*sube,Dymola_bool*ql,double*qp,double*qn,double eps,double qrel,double*qz,Dymola_bool init,Dymola_bool ev,Dymola_bool i4,int printevent,Dymola_bool*anyevent,Dymola_bool ltz,Dymola_bool invres,Dymola_bool*eventiter,double largeeps,Dymola_bool slow,double t);
  985. #define HandleEvent2S(cross,xexpr,rel,yexpr,index,ltz,invres) \
  986. (DYNX(QRel_,index-1)=(cross),\
  987. (handleevent4Sfunc(did_,xexpr " " rel " " yexpr,"(" xexpr ") - (" yexpr ")",(DSE_STRUCT QZ_),index,*(DSE_STRUCT idemand_)==4,DYNTime,PrintEvent,ltz,invres)))
  988. static double round0(double x) {
  989. return (x>0) ? floor(x) : ceil(x);
  990. }
  991. LIBDS_API void roundEvent4(const char*expr,double (*func)(double),double qrel,double*res,
  992. double*fuzz,double epsil,
  993. int posi,int negi,int reli,
  994. int init,int printevent,int *anyevent,int eventiter,double LargeEPS,int slow);
  995. #define roundEvent(cross,func,expr,index,posi,negi,reli) \
  996. ( DYNX(QRel_,index-1) = cross,\
  997. (DYNEvent? (roundEvent4(expr,func,DYNX(QRel_,index-1),&DYNX(Qp_,index-1),&DYNX(Qn_,index-1),EPS_,\
  998. posi,negi,reli,Init_,PrintEvent,&AnyEvent_,DYNX(Qenable_,index),LargeEPS_,Iter>SlowIter_),0):0),\
  999. DYNX((DSE_STRUCT QZ_),2*(index)-2) = -(DYNX(QRel_,index-1)-DYNX(Qp_,index-1)+DYNX(Qn_,index-1)+((posi)?(reli?(DYNX(Qp_,index-1)<=0):1):0)),\
  1000. DYNX((DSE_STRUCT QZ_),2*(index)-1) = -(DYNX(QRel_,index-1)-DYNX(Qp_,index-1)-DYNX(Qn_,index-1)-((negi)?(reli?(DYNX(Qp_,index-1)>=0):1):0)),\
  1001. DYNX(Qenable_,index)=true,DYNX(Qp_,index-1))
  1002. #define floorEvent(cross,xexpr,index) \
  1003. roundEvent(cross,floor,"floor(" xexpr ")",index+1,0,1,0)
  1004. #define ceilEvent(cross,xexpr,index) \
  1005. roundEvent(cross,ceil,"ceil(" xexpr ")",index+1,1,0,0)
  1006. #define divideDymolaEvent(x,xexpr,y,yexpr,index) \
  1007. roundEvent(((double)(x))/(y),round0,"div(" xexpr "," yexpr ")",index+1,1,1,1)
  1008. #define real2integerEvent(cross,xexpr,index) \
  1009. real2integer(roundEvent(cross,floor,"integer( " xexpr ")",index+1,0,1,0))
  1010. #define remainderDymolaEvent(x,xexpr,y,yexpr,index) \
  1011. ((x)-(y)*divideDymolaEvent(x,xexpr,y,yexpr,index))
  1012. #define modulusDymolaEvent(x,xexpr,y,yexpr,index) \
  1013. ((x)-(y)*roundEvent(((double)(x))/(y),floor,"floor(" xexpr "/" yexpr ")",index+1,0,1,0))
  1014. #define BooleanChanged(name,state,new) \
  1015. {Dymola_bool b1_,b2;\
  1016. b1_ = (Dymola_bool) state; \
  1017. b2_ = (Dymola_bool) new; \
  1018. booleanchanged_(name, &b1_, &b2_, &PrintEvent, &AnyEvent_, strlen(name)); \
  1019. state = new;}
  1020. #define cross_(exprnr) \
  1021. (DYNX(Qenable_,exprnr)=true, DYNX(QL_,exprnr))
  1022. #define when(cond,index) \
  1023. if (Init_) DYNX(QEvaluate_,index) = true;\
  1024. if (cond) { \
  1025. if (DYNX(QEvaluate_,index) && (DYNEvent)) {AnyEvent_=true;DYNX(QEvaluate_,index)=false;
  1026. #define endwhen(index) \
  1027. } \
  1028. } else {\
  1029. if (!DYNX(QEvaluate_,index)) { DYNX(QEvaluate_,index) = true;if (DYNEvent) AnyEvent_=true;}}
  1030. #define whenModelica(cond,index) \
  1031. if (Init_) {Init_=2;\
  1032. DYNX(QEvaluateNew_,index) = DYNX(QEvaluate_,index) = !(cond); \
  1033. Init_=1;\
  1034. }\
  1035. if (!(cond)) { \
  1036. if (!DYNX(QEvaluate_,index)) { DYNX(QEvaluateNew_,index) = true;/*if (DYNEvent) AnyEvent_=true;*/}\
  1037. } else { \
  1038. if (DYNX(QEvaluate_,index) && (DYNEvent)) {AnyEvent_=true;DYNX(QEvaluateNew_,index) = false;\
  1039. if (!haveDoneWhen_) {haveDoneWhen_=1;
  1040. #define whenModelicaOld(cond,index) \
  1041. if (Init_) {\
  1042. DYNX(QEvaluateNew_,index) = DYNX(QEvaluate_,index) = true; \
  1043. }\
  1044. if (!(cond)) { \
  1045. if (!DYNX(QEvaluate_,index)) { DYNX(QEvaluateNew_,index) = true;/*if (DYNEvent) AnyEvent_=true;*/}\
  1046. } else {\
  1047. if (DYNX(QEvaluate_,index) && (DYNEvent)) {AnyEvent_=true;DYNX(QEvaluateNew_,index) = false;\
  1048. if (!haveDoneWhen_) {haveDoneWhen_=1;
  1049. #define endwhenModelica() \
  1050. }\
  1051. } \
  1052. }
  1053. #define whenModelicaS(cond) \
  1054. if (cond && !haveDoneWhen_) {{{haveDoneWhen_=1;
  1055. #define vectorWhen(cond,index,nrIndex) \
  1056. if (Init_) {\
  1057. IntegerArray a_;int i_;\
  1058. Init_=2;a_=cond;Init_=1;\
  1059. for(i_=0;i_<nrIndex;i_++) QEvaluateNew_[index+i_]=QEvaluate_[index+i_]=!a_.data[i_];\
  1060. Release();} \
  1061. if (VectorWhenHandle(cond,IntegerTemporaryDenseOrig(QEvaluate_+index,1,nrIndex),IntegerTemporaryDenseOrig(QEvaluateNew_+index,1,nrIndex),DYNEvent,&AnyEvent_)) {\
  1062. if (!haveDoneWhen_) {haveDoneWhen_=1;
  1063. #define vectorWhenOld(cond,index,nrIndex) \
  1064. if (Init_) {\
  1065. int i_;\
  1066. for(i_=0;i_<nrIndex;i_++) QEvaluateNew_[index+i_]=QEvaluate_[index+i_]=true;\
  1067. } \
  1068. if (VectorWhenHandle(cond,IntegerTemporaryDenseOrig(QEvaluate_+index,1,nrIndex),IntegerTemporaryDenseOrig(QEvaluateNew_+index,1,nrIndex),DYNEvent,&AnyEvent_)) {\
  1069. if (!haveDoneWhen_) {haveDoneWhen_=1;
  1070. #define endVectorWhen() }}
  1071. #define beginwhenBlock {int haveDoneWhen_=0;
  1072. #define endwhenBlock }
  1073. #define UpdateQEvaluate(whenindex)\
  1074. if (DYNEvent&&(final_&&(DymolaOneIteration_==0||DymolaOneIteration_==4) || (!final_&&(DymolaOneIteration_==3||DymolaOneIteration_==4)))) {int i_;for (i_ = 0; i_ < whenindex;i_++) {QEvaluate_[i_]=QEvaluateNew_[i_];}}\
  1075. if (DYNEvent&&!final_) {int i_;for (i_ = 0; i_ < whenindex;i_++) {QEvaluateNew_[i_]=QEvaluate_[i_];}}\
  1076. #define UpdateSampleCounters(sampleindex) \
  1077. if (DYNEvent&&(final_&&(DymolaOneIteration_==0||DymolaOneIteration_==4) || (!final_&&(DymolaOneIteration_==3||DymolaOneIteration_==4)))) {int i_;for (i_=0;i_<sampleindex;i_++) {NextSampleTime_[i_]=NextSampleTimeNew_[i_];NextSampleAct_[i_]=NextSampleActNew_[i_];}}
  1078. #define Initial(v) \
  1079. (Init_)
  1080. #define initialAlsoContinue() ((Init_==1)?(AnyEvent_=1):0)
  1081. #ifdef DymolaOldInitialForContinue_
  1082. #define initial() initialAlsoContinue()
  1083. #else
  1084. #define initial() (((Init_==1) && !continueSimulate_)?(AnyEvent_=1):0)
  1085. #endif
  1086. #define Terminal(v) \
  1087. (*idemand_ == 7)
  1088. #define terminal() (*idemand_ == 7)
  1089. #define SecondDegree 1
  1090. #define currentStepSizeRatio2_ (((struct BasicDDymosimStruct*)(DSE_STRUCT duser_))->mCurrentStepSizeRatio2)
  1091. #define NextTimeEvent (((struct BasicDDymosimStruct*)(DSE_STRUCT duser_))->mNextTimeEvent)
  1092. DYMOLA_STATIC void CheckForEvents(struct DYNInstanceData*did_,double Time, int Init, int Event,
  1093. double QZ_[], int nrel_, double F_[], int nx_,double*duser_,int*iuser_);
  1094. /* Special operators that allows multiple assignments.
  1095. Used for Petri net library. */
  1096. #define SetBoolean2(var1, val1, var2, val2) \
  1097. 0.0; \
  1098. BooleanChanged(#var1, var1, val1); \
  1099. BooleanChanged(#var2, var2, val2); \
  1100. var1 = val1; \
  1101. var2 = val2; \
  1102. if ( Iter < MaxIter) { \
  1103. if (PrintEvent&(1<<1)) \
  1104. DymosimMessage("Iterating to find consistent restart conditions."); \
  1105. PerformIteration_ = true; \
  1106. goto finish; \
  1107. }
  1108. #define SetBoolean3(var1, val1, var2, val2, var3, val3) \
  1109. 0.0; \
  1110. BooleanChanged(#var1, var1, val1); \
  1111. BooleanChanged(#var2, var2, val2); \
  1112. BooleanChanged(#var3, var3, val3); \
  1113. var1 = val1; \
  1114. var2 = val2; \
  1115. var3 = val3; \
  1116. if ( Iter < MaxIter) { \
  1117. if (PrintEvent&(1<<1)) \
  1118. DymosimMessage("Iterating to find consistent restart conditions."); \
  1119. PerformIteration_ = true; \
  1120. goto finish; \
  1121. }
  1122. #define BreakDeclareFunctionDef(nr) \
  1123. DYMOLA_STATIC void declare_##nr(double x0_[], double dp_[], double du_[], int *QiErr); \
  1124. #define BreakDeclareFunction(nr) \
  1125. { BreakDeclareFunctionDef(nr) declare_##nr(x0_, dp_, QiErr); \
  1126. } \
  1127. } \
  1128. \
  1129. DYMOLA_STATIC void declare_##nr(double x0_[], double dp_[], double du_[], int *QiErr) \
  1130. {
  1131. #define BreakDeclareFunctionNew(nr) \
  1132. { declare_##nr(x0_, dp_, du_, QiErr); \
  1133. } \
  1134. } \
  1135. \
  1136. DYMOLA_STATIC void declare_##nr(double x0_[], double dp_[], double du_[], int *QiErr) \
  1137. {
  1138. #define BreakEquationsFunction(nr) \
  1139. { equations_##nr(idemand_, icall_, \
  1140. time, X_, XD_, U_, \
  1141. DP_, IP_, LP_, \
  1142. F_, Y_, W_, QZ_, \
  1143. duser_, iuser_, cuser_, did_, \
  1144. QiErr); \
  1145. } \
  1146. finish: \
  1147. leave: \
  1148. if (GlobalError_ != 0) \
  1149. *QiErr = GlobalError_; \
  1150. } \
  1151. \
  1152. DYMOLA_STATIC void equations_##nr(int *idemand_, int *icall_, \
  1153. double *time, double* X_, double* XD_, double* U_, \
  1154. double* DP_, int IP_[], Dymola_bool LP_[], \
  1155. double* F_, double* Y_, double* W_, double QZ_[], \
  1156. double duser_[], int iuser_[], void*cuser_[], struct DYNInstanceData*did_, \
  1157. int *QiErr) \
  1158. { \
  1159. if (PerformIteration_ || (*QiErr != 0 && *QiErr != -999)) \
  1160. DYN_GOTOFINISH;
  1161. #define BreakSectionFunctionDef(nr) \
  1162. DYMOLA_STATIC void equations_##nr(int *idemand_, int *icall_, \
  1163. double *time, double* X_, double* XD_, double* U_, \
  1164. double* DP_, int IP_[], Dymola_bool LP_[], \
  1165. double* F_, double* Y_, double* W_, double QZ_[], \
  1166. double duser_[], int iuser_[], void*cuser_[], struct DYNInstanceData*did_, \
  1167. int *QiErr);
  1168. #define BreakSectionFunction(nr) \
  1169. }\
  1170. { BreakSectionFunctionDef(nr) \
  1171. equations_##nr(idemand_, icall_, \
  1172. time, X_, XD_, U_, \
  1173. DP_, IP_, LP_, \
  1174. F_, Y_, W_, QZ_, \
  1175. duser_, iuser_, cuser_, did_, \
  1176. QiErr); \
  1177. } \
  1178. finish: \
  1179. leave: \
  1180. if (GlobalError_ != 0) \
  1181. *QiErr = GlobalError_; \
  1182. } \
  1183. \
  1184. DYMOLA_STATIC void equations_##nr(int *idemand_, int *icall_, \
  1185. double *time, double* X_, double* XD_, double* U_, \
  1186. double* DP_, int IP_[], Dymola_bool LP_[], \
  1187. double* F_, double* Y_, double* W_, double QZ_[], \
  1188. double duser_[], int iuser_[], void*cuser_[], struct DYNInstanceData*did_, \
  1189. int *QiErr) \
  1190. { \
  1191. if (PerformIteration_ || (*QiErr != 0 && *QiErr != -999)) \
  1192. DYN_GOTOFINISH;\
  1193. if (sectioncondition) {
  1194. #define BreakSectionFunctionNew(nr) \
  1195. }\
  1196. { \
  1197. equations_##nr(idemand_, icall_, \
  1198. time, X_, XD_, U_, \
  1199. DP_, IP_, LP_, \
  1200. F_, Y_, W_, QZ_, \
  1201. duser_, iuser_, cuser_, did_, \
  1202. QiErr); \
  1203. } \
  1204. finish: \
  1205. leave: \
  1206. if (GlobalError_ != 0) \
  1207. *QiErr = GlobalError_; \
  1208. } \
  1209. \
  1210. DYMOLA_STATIC void equations_##nr(int *idemand_, int *icall_, \
  1211. double *time, double* X_, double* XD_, double* U_, \
  1212. double* DP_, int IP_[], Dymola_bool LP_[], \
  1213. double* F_, double* Y_, double* W_, double QZ_[], \
  1214. double duser_[], int iuser_[], void*cuser_[], struct DYNInstanceData*did_, \
  1215. int *QiErr) \
  1216. { \
  1217. if (PerformIteration_ || (*QiErr != 0 && *QiErr != -999)) \
  1218. DYN_GOTOFINISH;\
  1219. if (sectioncondition) {
  1220. #define BreakSectionFunctionCallNew(nr) \
  1221. } { \
  1222. equations_##nr(idemand_, icall_, \
  1223. time, X_, XD_, U_, \
  1224. DP_, IP_, LP_, \
  1225. F_, Y_, W_, QZ_, \
  1226. duser_, iuser_, cuser_, did_, \
  1227. QiErr); \
  1228. } if (sectioncondition) {
  1229. #define BreakSectionFunctionEnd() \
  1230. } \
  1231. finish: \
  1232. leave: \
  1233. if (GlobalError_ != 0) \
  1234. *QiErr = GlobalError_; \
  1235. }
  1236. #define BreakSectionFunctionStart(nr) \
  1237. DYMOLA_STATIC void equations_##nr(int *idemand_, int *icall_, \
  1238. double *time, double* X_, double* XD_, double* U_, \
  1239. double* DP_, int IP_[], Dymola_bool LP_[], \
  1240. double* F_, double* Y_, double* W_, double QZ_[], \
  1241. double duser_[], int iuser_[], void*cuser_[],struct DYNInstanceData*did_, \
  1242. int *QiErr) \
  1243. { \
  1244. if (PerformIteration_ || (*QiErr != 0 && *QiErr != -999)) \
  1245. DYN_GOTOFINISH;\
  1246. if (sectioncondition) {
  1247. #if (defined(_OPENMP) && !defined(DISABLE_DYMOLA_OPENMP))
  1248. #define DYN_GOTOLEAVE
  1249. #define DYN_GOTOFINISH
  1250. #else
  1251. #define DYN_GOTOLEAVE goto leave
  1252. #define DYN_GOTOFINISH goto finish
  1253. #endif
  1254. #define BreakSectionJacDef(nr) \
  1255. DYMOLA_STATIC int equationsJ_##nr(int *idemand_, int *icall_, \
  1256. double *time, double* X_, double* XD_, double* U_, \
  1257. double* DP_, int IP_[], Dymola_bool LP_[], \
  1258. double* F_, double* Y_, double* W_, double QZ_[], \
  1259. double duser_[], int iuser_[], void*cuser_[],struct DYNInstanceData*did_, \
  1260. int *QiErr,double*Jacobian____internal,int Jacobian____m);
  1261. #define BreakSectionJacCallNew(nr) \
  1262. { switch(equationsJ_##nr(idemand_, icall_, \
  1263. time, X_, XD_, U_, \
  1264. DP_, IP_, LP_, \
  1265. F_, Y_, W_, QZ_, \
  1266. duser_, iuser_, cuser_,did_, \
  1267. QiErr,Jacobian____internal,Jacobian____m)) { case 1:DYN_GOTOFINISH;case 2:DYN_GOTOLEAVE;default:;} \
  1268. }
  1269. #define BreakSectionJacCall(nr) \
  1270. { BreakSectionJacDef(nr) switch(equationsJ_##nr(idemand_, icall_, \
  1271. time, X_, XD_, U_, \
  1272. DP_, IP_, LP_, \
  1273. F_, Y_, W_, QZ_, \
  1274. duser_, iuser_, cuser_,did_, \
  1275. QiErr,Jacobian____internal,Jacobian____m)) { case 1:DYN_GOTOFINISH;case 2:DYN_GOTOLEAVE;default:;} \
  1276. }
  1277. #define BreakSectionJacEnd() \
  1278. return 0;finish: return 1;leave:return 2;}
  1279. #define BreakSectionJacStart(nr) \
  1280. DYMOLA_STATIC int equationsJ_##nr(int *idemand_, int *icall_, \
  1281. double *time, double* X_, double* XD_, double* U_, \
  1282. double* DP_, int IP_[], Dymola_bool LP_[], \
  1283. double* F_, double* Y_, double* W_, double QZ_[], \
  1284. double duser_[], int iuser_[], void*cuser_[],struct DYNInstanceData*did_, \
  1285. int *QiErr,double*Jacobian____internal,int Jacobian____m) {
  1286. #define BreakSectionODEJacStart(nr) \
  1287. DYMOLA_STATIC int equationsJ_##nr(int *idemand_, int *icall_, \
  1288. double *time, double* X_, double* XD_, double* U_, \
  1289. double* DP_, int IP_[], Dymola_bool LP_[], \
  1290. double* F_, double* Y_, double* W_, double QZ_[], \
  1291. double duser_[], int iuser_[], void*cuser_[],struct DYNInstanceData*did_, \
  1292. int *QiErr) {
  1293. #define BreakSectionODEJacDef(nr) \
  1294. DYMOLA_STATIC int equationsJ_##nr(int *idemand_, int *icall_, \
  1295. double *time, double* X_, double* XD_, double* U_, \
  1296. double* DP_, int IP_[], Dymola_bool LP_[], \
  1297. double* F_, double* Y_, double* W_, double QZ_[], \
  1298. double duser_[], int iuser_[], void*cuser_[],struct DYNInstanceData*did_, \
  1299. int *QiErr);
  1300. #define BreakSectionODEJacCall(nr) \
  1301. { BreakSectionODEJacDef(nr) switch(equationsJ_##nr(idemand_, icall_, \
  1302. time, X_, XD_, U_, \
  1303. DP_, IP_, LP_, \
  1304. F_, Y_, W_, QZ_, \
  1305. duser_, iuser_, cuser_,did_, \
  1306. QiErr)) { case 1:DYN_GOTOFINISH;case 2:DYN_GOTOLEAVE;default:;} \
  1307. }
  1308. #define BreakSectionODEJacCallNew(nr) \
  1309. { switch(equationsJ_##nr(idemand_, icall_, \
  1310. time, X_, XD_, U_, \
  1311. DP_, IP_, LP_, \
  1312. F_, Y_, W_, QZ_, \
  1313. duser_, iuser_, cuser_,did_, \
  1314. QiErr)) { case 1:DYN_GOTOFINISH;case 2:DYN_GOTOLEAVE;default:;} \
  1315. }
  1316. #define BreakSectionODEJacEnd() \
  1317. return 0;finish: return 1;leave:return 2;}
  1318. #define BreakSectionDef(nr) \
  1319. DYMOLA_STATIC int equationsJ_##nr(int *idemand_, int *icall_, \
  1320. double *time, double* X_, double* XD_, double* U_, \
  1321. double* DP_, int IP_[], Dymola_bool LP_[], \
  1322. double* F_, double* Y_, double* W_, double QZ_[], \
  1323. double duser_[], int iuser_[], void*cuser_[], struct DYNInstanceData*did_, \
  1324. int *QiErr);
  1325. #define BreakSectionCallNew(nr) \
  1326. { switch(equationsJ_##nr((DSE_STRUCT idemand_), (DSE_STRUCT icall_), \
  1327. time, (DSE_STRUCT X_), (DSE_STRUCT XD_), (DSE_STRUCT U_), \
  1328. (DSE_STRUCT DP_), (DSE_STRUCT IP_), (DSE_STRUCT LP_), \
  1329. (DSE_STRUCT F_), (DSE_STRUCT Y_), (DSE_STRUCT W_), (DSE_STRUCT QZ_), \
  1330. (DSE_STRUCT duser_), (DSE_STRUCT iuser_), (DSE_STRUCT cuser_), (DSE_STRUCT did_), \
  1331. (DSE_STRUCT QiErr))) { case 1:DYN_GOTOFINISH;case 2:DYN_GOTOLEAVE;default:;} \
  1332. }
  1333. #define BreakSectionCall(nr) \
  1334. { BreakSectionDef(nr) \
  1335. switch(equationsJ_##nr(idemand_, icall_, \
  1336. time, X_, XD_, U_, \
  1337. DP_, IP_, LP_, \
  1338. F_, Y_, W_, QZ_, \
  1339. duser_, iuser_, cuser_, did_, \
  1340. QiErr)) { case 1:DYN_GOTOFINISH;case 2:DYN_GOTOLEAVE;default:;} \
  1341. }
  1342. #define BreakSectionEnd() \
  1343. return 0;finish: return 1;leave:return 2;}
  1344. #define BreakSectionStart(nr) \
  1345. DYMOLA_STATIC int equationsJ_##nr(int *idemand_, int *icall_, \
  1346. double *time, double* X_, double* XD_, double* U_, \
  1347. double* DP_, int IP_[], Dymola_bool LP_[], \
  1348. double* F_, double* Y_, double* W_, double QZ_[], \
  1349. double duser_[], int iuser_[], void*cuser_[],struct DYNInstanceData*did_, \
  1350. int *QiErr) {
  1351. #define StartPreBlock if ((Init_||DYNEvent ||DymolaOneIteration_==5) && (final_!=2 || DymolaOneIteration_==2 || DymolaOneIteration_ == 0) ) {
  1352. #define EndPreBlock if (!final_ && (DymolaOneIteration_==3) && (PrintEvent&(1<<1))) DymosimMessage("Iterating to find consistent restart conditions.");}
  1353. #define BreakPreBlockDef(nr) DYMOLA_STATIC void initializeDataPre##nr(double *time, double* X_, double* XD_, double* U_, \
  1354. double* DP_, int IP_[], Dymola_bool LP_[], \
  1355. double* F_, double* Y_, double* W_, double QZ_[], \
  1356. double duser_[], int iuser_[], void*cuser_[],struct DYNInstanceData*did_, \
  1357. int final_,int solverCall);
  1358. #define BreakPreBlockNew(nr) EndPreBlock \
  1359. { initializeDataPre##nr(time,X_,XD_,U_,DP_,IP_,LP_,F_,Y_,W_,QZ_,duser_,iuser_,cuser_,did_,final_, solverCall);}} \
  1360. DYMOLA_STATIC void initializeDataPre##nr(double *time, double* X_, double* XD_, double* U_, \
  1361. double* DP_, int IP_[], Dymola_bool LP_[], \
  1362. double* F_, double* Y_, double* W_, double QZ_[], \
  1363. double duser_[], int iuser_[], void*cuser_[],struct DYNInstanceData*did_, \
  1364. int final_,int solverCall) {StartPreBlock
  1365. #define BreakPreBlock(nr) EndPreBlock \
  1366. { BreakPreBlockDef(nr) \
  1367. initializeDataPre##nr(time,X_,XD_,U_,DP_,IP_,LP_,F_,Y_,W_,QZ_,duser_,iuser_,cuser_,did_,final_, solverCall);}} \
  1368. DYMOLA_STATIC void initializeDataPre##nr(double *time, double* X_, double* XD_, double* U_, \
  1369. double* DP_, int IP_[], Dymola_bool LP_[], \
  1370. double* F_, double* Y_, double* W_, double QZ_[], \
  1371. double duser_[], int iuser_[], void*cuser_[], struct DYNInstanceData*did_,\
  1372. int final_,int solverCall) {StartPreBlock
  1373. #define EndEqBlock }
  1374. #define BreakEquBlockDef(nr) DYMOLA_STATIC void initializeDataEqu##nr(double *time, double* X_, double* XD_, double* U_, \
  1375. double* DP_, int IP_[], Dymola_bool LP_[], \
  1376. double* F_, double* Y_, double* W_, double QZ_[], \
  1377. double duser_[], int iuser_[], void*cuser_[], struct DYNInstanceData*did_,\
  1378. int final_,int solverCall);
  1379. #define BreakEquBlockNew(nr) EndEqBlock \
  1380. { initializeDataEqu##nr(time,X_,XD_,U_,DP_,IP_,LP_,F_,Y_,W_,QZ_,duser_,iuser_,cuser_,did_,final_, solverCall);}} \
  1381. DYMOLA_STATIC void initializeDataEqu##nr(double *time, double* X_, double* XD_, double* U_, \
  1382. double* DP_, int IP_[], Dymola_bool LP_[], \
  1383. double* F_, double* Y_, double* W_, double QZ_[], \
  1384. double duser_[], int iuser_[], void*cuser_[], struct DYNInstanceData*did_,\
  1385. int final_,int solverCall) {StartEqBlock
  1386. #define BreakEquBlock(nr) EndEqBlock \
  1387. { BreakEquBlockDef(nr) \
  1388. initializeDataEqu##nr(time,X_,XD_,U_,DP_,IP_,LP_,F_,Y_,W_,QZ_,duser_,iuser_,cuser_,final_, solverCall);}} \
  1389. DYMOLA_STATIC void initializeDataEqu##nr(double *time, double* X_, double* XD_, double* U_, \
  1390. double* DP_, int IP_[], Dymola_bool LP_[], \
  1391. double* F_, double* Y_, double* W_, double QZ_[], \
  1392. double duser_[], int iuser_[], void*cuser_[],did_, \
  1393. int final_,int solverCall) {StartEqBlock
  1394. #define EndDataBlock }
  1395. #define AssertEquation(expr1, expr2) \
  1396. if(fabs((expr1) - (expr2)) > 1E-6) \
  1397. DymosimMessageDouble("ERROR: " #expr1 " - (" #expr2 ")NOT EQUAL TO ZERO: ", (expr1) - (expr2))
  1398. /* Routines to implement memory block.
  1399. The putMemory function saves its input in a memory area at
  1400. output points. The getMemory function retrieves the value.
  1401. By use of these functions it is possible to break dependency
  1402. loops.
  1403. Note that memory vector needs to have 2 elements.
  1404. Warning: The memory functions should be used with care since
  1405. it is possible to introduce errors that the integration routine
  1406. can not handle.
  1407. */
  1408. DYMOLA_STATIC double getMemory(double memory[], int n1, int n2) {
  1409. if (n1 == 1 && n2 == 1)
  1410. return DymosimError("Memory argument to getMemory must be a vector.");
  1411. return memory[0];
  1412. }
  1413. DYMOLA_STATIC double putMemoryCond(int cond, double memory[], int n1, int n2, double u) {
  1414. if (n1 == 1 && n2 == 1)
  1415. return DymosimError("Memory argument to putMemory must be a vector.");
  1416. if (cond)
  1417. memory[0] = u;
  1418. return 0;
  1419. }
  1420. #define putMemory(memory, n1, n2, u) \
  1421. putMemoryCond(*idemand_ == 3, memory, n1, n2, u)
  1422. #define putMemoryAtEvent(memory, n1, n2, u) \
  1423. putMemoryCond(Event, memory, n1, n2, u)
  1424. DYMOLA_STATIC void registerTimeEventNew(const double atTime,struct DYNInstanceData*did_);
  1425. DYMOLA_STATIC void registerTimeEvent(const double atTime);
  1426. static double findCounter(double Time,double start,double interval) {
  1427. double i=ceil((Time-start)/interval);
  1428. return i+1;
  1429. }
  1430. DYMOLA_STATIC const char* dymosimResources(void);
  1431. DYMOLA_STATIC Dymola_bool sampleFunctionM(struct DYNInstanceData*did_,double Time, double start, double interval, int counter,
  1432. Dymola_bool Init, Dymola_bool Event);
  1433. DYMOLA_STATIC Dymola_bool sampleFunctionM3(struct DYNInstanceData*did_,double Time, double start, double interval, int phase, int maxVal, int counter,
  1434. Dymola_bool Init, Dymola_bool Event);
  1435. /* Special cases for Timed events */
  1436. #define GreaterEqualTime(y, nr) \
  1437. (((Init_||DYNEvent) && ( \
  1438. (DYNTime<y-(4e-14)*(fabs(DYNTime)+(((struct BasicDDymosimStruct*)(DSE_STRUCT duser_))->mOrigTimeError))) ? registerTimeEventNew(y,did_),(DYNX(QTimed_,nr)=0):(DYNX(QTimed_,nr)?0:(AnyDEvent_=DYNX(QTimed_,nr)=1,(DymolaOneIteration_!=0)?AnyEvent_=1:1)))),DYNX(QTimed_,nr))
  1439. /* Deactivate the state event */
  1440. #define GreaterEqualTimeMinor(y, nr) \
  1441. (((Init_||DYNEvent) && ( \
  1442. (DYNTime<y-(4e-14)*(fabs(DYNTime)+(((struct BasicDDymosimStruct*)(DSE_STRUCT duser_))->mOrigTimeError))) ? registerTimeEventNew(y,did_),(DYNX(QTimed_,nr)=0):((((DYNX(QTimed_,nr)==0&&DymolaOneIteration_!=0)?AnyEvent_=true:true),DYNX(QTimed_,nr)=1)))),DYNX(QTimed_,nr))
  1443. /* Deactivate the state event */
  1444. #define LessTime(y, nr) (!GreaterEqualTime(y,nr))
  1445. #define LessTimeMinor(y, nr) (!GreaterEqualTimeMinor(y,nr))
  1446. #define Sample(start, interval, SampleCounter) \
  1447. (DYNEvent ? sampleFunction(did_, DYNTime, start, interval, SampleCounter, Init_, DYNEvent) : 0.0)
  1448. /* (Event ? sampleFunction1(Time, start, interval) : 0.0) */
  1449. /* For Modelica */
  1450. #define sample(start, interval, SampleCounter) \
  1451. (DYNEvent ? sampleFunctionM(did_, DYNTime, start, interval, SampleCounter, Init_, DYNEvent) : 0.0)
  1452. #define sampleNew(start, interval, SampleCounter) \
  1453. (Init_ ? (AnyEvent_=true,0.0) : sample(start, interval, SampleCounter) ? ((DymolaOneIteration_!=0)?(AnyEvent_=true):true) : false)
  1454. #define sampleNew2(start, interval, SampleCounter) \
  1455. (Init_ ? (AnyEvent_=true,0.0) : (sample(start, interval, SampleCounter) ? (AnyEvent_=true) : false))
  1456. #define sampleNew3(start, interval, phase, maxVal, SampleCounter) \
  1457. (Init_ ? (AnyEvent_=true,0.0) : ((DYNEvent ? sampleFunctionM3(did_, DYNTime, start, interval, phase, maxVal, SampleCounter,Init_,DYNEvent):0.0) ? (AnyEvent_=true) : false))
  1458. #define StepSizeCalculations(iv)\
  1459. ResetIfNewSimulation(StepSize=0;previousTime_,-1e30,iv);\
  1460. if (previousTime_<0) {previousTime_=DYNTime;currentStepSize_=0;}\
  1461. else if (DYNTime == previousTime_ || DYNEvent) {currentStepSize_ = 0;previousTime_=DYNTime;}\
  1462. else if (DYNFindEvent) {previousTime_ = DYNTime;} else {currentStepSize_=DYNTime-previousTime_;previousTime_ = DYNTime;}\
  1463. if (DYNFindEvent && StepSize == 0 && DYNTime > LastTime) {\
  1464. StepSize = DYNTime - LastTime;\
  1465. currentStepSize_ = StepSize;\
  1466. }
  1467. /* Special case since the currentStepSize_ is computed inside CheckForEvents */
  1468. /* and only reset to zero here */
  1469. #define whenModelicaS3(iv,prev1,curr1,cond1)\
  1470. ResetIfNewSimulation(prev1,-1e30,iv);\
  1471. whenModelicaS(((cond1) || Init_ || (DYNEvent && prev1==DYNTime))) \
  1472. if (prev1==DYNTime || prev1<-1e29) curr1=0;else curr1=DYNTime-prev1;prev1=DYNTime;currentStepSize_=curr1;
  1473. #define whenModelicaS2(iv,prev1,curr1,cond1,samp)\
  1474. ResetIfNewSimulation(prev1,-1e30,iv);\
  1475. whenModelicaS(((cond1) || Init_ || (DYNEvent && prev1==DYNTime))) \
  1476. if (prev1==DYNTime || prev1<-1e29) curr1=0;else curr1=samp;prev1=DYNTime;currentStepSize_=curr1;
  1477. #define BeginStepSizeCalculations(iv,prev1,curr1,cond1,samp)\
  1478. beginwhenBlock\
  1479. whenModelicaS2(iv,prev1,curr1,cond1,samp)
  1480. #define EndStepSizeCalculations()\
  1481. endwhenModelica()\
  1482. endwhenBlock
  1483. #define ResetIfNewStep(v,val,resetOldTime_,oldResetCnt_) \
  1484. { if ((resetOldTime_!=DYNTime)||(oldResetCnt_!=ResetCounter_)) {v=val;resetOldTime_=DYNTime;oldResetCnt_=ResetCounter_;}}
  1485. #define ResetIfNewSimulation(v,val,oldResetCnt_) \
  1486. { if ((oldResetCnt_!=ResetCounter_)) {v=val;oldResetCnt_=ResetCounter_;}}
  1487. static double CheckContinuousIfCode(int c,const char*ys,double*v,double newValue,double Time,double InitTime) {
  1488. if (c && fabs(*v-newValue)>1e-3*(1+fabs(newValue)+fabs(*v)) && Time!=InitTime) {
  1489. #if defined(DYMOSIM)
  1490. char buf[1000];
  1491. sprintf(buf, "\nModel error - differentiated if-then-else was not continuous:\n %s\n Value jumped from %g to %g.\n", ys, *v, newValue);
  1492. DymosimError(buf);
  1493. #elif defined(Matlab6) && !defined(NO_FILE) && defined(ssPrintf)
  1494. if (!(getBasicIDymosimStruct()->mPrintEvent&(1<<10))) {
  1495. ssPrintf("\nModel error - differentiated if-then-else was not continuous:\n %s\n Value jumped from %g to %g.\n", ys, *v, newValue);
  1496. }
  1497. DymosimError("Model error - differentiated if-then-else was not continuous");
  1498. #else
  1499. if (!(getBasicIDymosimStruct()->mPrintEvent&(1<<10))) {
  1500. printf("\nModel error - differentiated if-then-else was not continuous:\n %s\n Value jumped from %g to %g.\n", ys, *v, newValue);
  1501. }
  1502. DymosimError("Model error - differentiated if-then-else was not continuous");
  1503. #endif
  1504. }
  1505. return *v=newValue;
  1506. }
  1507. #if DymolaNoGuard_
  1508. #define CheckContinuousIf(c,ys,nr) (c)
  1509. #else
  1510. #define CheckContinuousIf(c,ys,nr)\
  1511. (CheckContinuousIfCode(DYNEvent&&Iter>0&&!Init_,ys,QCheckIf_+nr,c,DYNTime,InitTime))
  1512. #endif
  1513. /* Solution of linear systems of equations. */
  1514. LIBDS_API int dymres_(double* a, const int* lda, const int* n, double* b, int* ierr);
  1515. LIBDS_API int dymli1_(int* sysnr, const int *fact, double* a, int* lda, int* n,
  1516. double* b, double *Time, int* Event, int* PrintEvent,
  1517. double* dwork, int* iwork, int* ierr);
  1518. LIBDS_API int dymli2_(int* sysnr, int *fact, double *a, int* lda, int *n,
  1519. double* b, double *Time, int *Event, int* PrintEvent,
  1520. double* dwork, int* iwork, int *factor, int* ierr);
  1521. LIBDS_API int dymli3_(int* sysnr, int *fact, double *a, int* lda, int *n,
  1522. double* b, double *Time, int *Event, int* PrintEvent,
  1523. double* dwork, int* iwork, int *factor, const char*const*varnames,int* ierr);
  1524. LIBDS_API int dymli4_(int* sysnr, int *fact, double *a, int* lda, int *n,
  1525. double* b, double *Time, int *Event, int* PrintEvent,
  1526. double* dwork, int* iwork, int *factor, const char*const*varnames,int* ierr,int*fEvent);
  1527. LIBDS_API int dymli5_(int* sysnr, int *fact, double *a, int* lda, int *n,
  1528. double* b, const double*x0, double *Time, int *Event, int* PrintEvent,
  1529. double* dwork, int* iwork, int *factor, const char*const*varnames,int* ierr,int*fEvent);
  1530. LIBDS_API int dymlin_(int* infrev, int* n, double* sol, double* res, double *jac,
  1531. int* ljac, double* dwork, int *iwork, double *Time,
  1532. int* event, int *printpriority, int *sysnr, int *ierr);
  1533. LIBDS_API int dymnon_(
  1534. int *qinfrev, int *qiopt, int *qnnl,
  1535. double *qsol, double *qres, double *qjac, double *qtol,
  1536. int *qinfo,
  1537. double *qd,
  1538. int *qi, int *printevent, int *qnlnr,
  1539. double *time,
  1540. int *qnlfunc, int *qnljac, int *qnlmax, int *ierr);
  1541. #define DymRes(A, Max, n, b, ierr_) \
  1542. {int i1_,i2_;i1_ = MaxMat; \
  1543. i2_ = n; \
  1544. dymres_(qa_, &i1_, &i2_, qb_, ierr_);}
  1545. #define DymSol(A, Max, n, b, Dwork, Iwork, ierr_) \
  1546. if ( n > MaxMat ) { \
  1547. DymosimMessage("The capacity for solving linear systems of equations, MaxMat, needs to be increased"); \
  1548. goto leave; \
  1549. } \
  1550. {int i1_,i2_,i3;i1_ = MaxMat;i2_ = n;i3_ = 0; \
  1551. dymli1_(&i3_, &i3_, qa_, &i1_, &i2_, qb_, &DYNTime, &Event, &PrintEvent, dwork_, iwork_, ierr_);}
  1552. #define DymLin(QInfRev,QiOpt,QNnl,QSol,QRes,QJac,Qtol,Qinfo,QD,QI, \
  1553. PrintEvent,QNLnr,DYNTime,QNLfunc,QNLjac,QNLmax,ierr_) \
  1554. if (QNnl > MaxMat) { \
  1555. DymosimMessage("The capacity for solving linear systems of equations, MaxMat, needs to be increased"); \
  1556. goto leave; \
  1557. } \
  1558. {int i1_,i2_,i3;i1_ = QMaxNonLin;i2_ = 0;i3_ = Event;\
  1559. dymlin_(&QInfRev,&QNnl,QSol_,QRes_,QJac_,&i1_,dwork_,iwork_,&DYNTime,&i3_,&PrintEvent,&i2_,ierr_);}
  1560. #define QA_(i,j) \
  1561. qa_[(j-1)*MaxMat + i-1]
  1562. #define Qb_(i) \
  1563. qb_[i-1]
  1564. /* Solution of nonlinear systems of equations. */
  1565. #define QRes_(i) \
  1566. QRes_[i-1]
  1567. #define QSol_(i) \
  1568. QSol_[i-1]
  1569. #define QJac_(i,j) \
  1570. QJac_[(j-1)*QNnl + i-1]
  1571. #define DymNon(QInfRev,QiOpt,QNnl,QSol,QRes,QJac,Qtol,Qinfo,QD,QI, \
  1572. PrintEvent,QNLnr,DYNTime,QNLfunc,QNLjac,QNLmax,ierr_) \
  1573. if (QNnl > QMaxNonLin) { \
  1574. DymosimMessage("The capacity for solving nonlinear systems of equations, QMaxNonLin, needs to be increased"); \
  1575. goto leave; \
  1576. } \
  1577. if (QNLnr > QNLmax) { \
  1578. DymosimMessage("The capacity for solving nonlinear systems of equations, QNLmax, needs to be increased"); \
  1579. goto leave; \
  1580. } \
  1581. {int i1_,i2_;double Qtol2=Dymola_min(0.5*EPS_,Qtol*0.01);i1_ = QI_;i2_ = QNLmax;dymnon_(&QInfRev,&QiOpt,&QNnl,QSol_,QRes_,QJac_,&Qtol2,&Qinfo,QD_,&i1_, \
  1582. &PrintEvent,&QNLnr,DYNTime,QNLfunc,QNLjac,&i2_,ierr_);}
  1583. #define DymNeq(QInfRev_,QiOpt_,QSol_,QRes_,QJac_,Qinline_,Qtol_,Qinfo_, \
  1584. QNLnr_,PrintEvent,DYNTime,QiErr) \
  1585. dymNeq(&QInfRev_,&QiOpt_,QSol_,QRes_,QJac_,Qinline_,&Qtol_,&Qinfo_, \
  1586. QNLnr_,PrintEvent,DYNTime,QiErr); \
  1587. if ( Qinfo_ == -1 ) return 0;
  1588. #include <bloutil.h>
  1589. #ifdef _MSC_VER
  1590. #pragma warning ( disable : 4101 4102 )
  1591. #endif
  1592. #if defined(RT)
  1593. LIBDS_API double DymolaTimerCounterSince(double*d,int i) {
  1594. if (i==0) {
  1595. double x=0;
  1596. if (d) *d=x;
  1597. return x;
  1598. } else {
  1599. return 0-*d;
  1600. }
  1601. }
  1602. #else
  1603. #include <time.h>
  1604. static const double invFreq=1.0/CLOCKS_PER_SEC;
  1605. typedef double (*timerCounterCBFunction)(double*,int);
  1606. timerCounterCBFunction DymolaTimerCounterCallback=0;
  1607. #ifndef BUILDFMU
  1608. #ifdef _MSC_VER
  1609. __declspec(dllexport)
  1610. #endif
  1611. void setTimerCounterCB(timerCounterCBFunction f)
  1612. {
  1613. DymolaTimerCounterCallback = f;
  1614. }
  1615. #endif /* BUILDFMU */
  1616. LIBDS_API double myclock(void);
  1617. double DymolaTimerCounterSince(double*d,int i) {
  1618. if (DymolaTimerCounterCallback)
  1619. return DymolaTimerCounterCallback(d,i);
  1620. if (i==0) {
  1621. double x=myclock();
  1622. if (d) *d=x;
  1623. return x;
  1624. } else {
  1625. return invFreq*(myclock()-*d);
  1626. }
  1627. }
  1628. #endif
  1629. #if defined(LIBDS_NODLL_API) || defined(LIBDS_DLL)
  1630. void signalError(void) {
  1631. DymosimError("");
  1632. }
  1633. void (initializeData)(double *time, double* X_, double* XD_, double* U_,
  1634. double* DP_, int IP_[], Dymola_bool LP_[],
  1635. double* F_, double* Y_, double* W_, double QZ_[],
  1636. double duser_[], int iuser_[], void*cuser_[],
  1637. int final_,int Init,int m_Event,int solverCall);
  1638. void (delayBuffersClose)(void);
  1639. DYMOLA_STATIC void SetDymolaJacobianPointers(struct DYNInstanceData*did_, double * QJacobian_,double * QBJacobian_,double * QCJacobian_,double * QDJacobian_,int QJacobianN_,
  1640. int QJacobianNU_,int QJacobianNY_,double * QJacobianSparse_,int * QJacobianSparseR_,int * QJacobianSparseC_,int QJacobianNZ_);
  1641. DYMOLA_STATIC void SetDymolaJacobianPointers2(struct DYNInstanceData*did_, double * QJacobian_,double * QBJacobian_,double * QCJacobian_,double * QDJacobian_,int QJacobianN_,
  1642. int QJacobianNU_,int QJacobianNY_,double * QJacobianSparse_,int * QJacobianSparseR_,int * QJacobianSparseC_,int QJacobianNZ_,int *QJacobianABCDNZ_);
  1643. /* If QJacobianABCDNZ_ is non-null it _receives_ the number of non-zero elements in the sparse ABCD. If additionally QJacobianSparse_ is non-null it activates sparse ABCD-matrix */
  1644. struct DymolaTimes;
  1645. struct DymolaTimes* GetDymolaTimers(struct DYNInstanceData*, int*);
  1646. #if defined(LIBDS_DLL)
  1647. __declspec(dllexport)
  1648. #endif
  1649. const struct DymosimModel* DymosimModelFunctions(void) {
  1650. extern const char*modelName;
  1651. extern const char* usedLibraries[];
  1652. extern const char*dllLibraryPath[];
  1653. extern const char*default_dymosim_license_filename;
  1654. extern int QJacobianCG_[];
  1655. extern int QJacobianGC_[];
  1656. extern double QJacobianCD_[];
  1657. static struct DymosimModel a={
  1658. 12,
  1659. registerTimeEvent,
  1660. signalError,
  1661. dsblock_,
  1662. dsblockb_,
  1663. GetDimensions2,
  1664. initializeData,
  1665. 0,
  1666. delayBuffersClose
  1667. };
  1668. a.modelName=modelName;
  1669. a.usedLibraries=usedLibraries;
  1670. a.dllLibraryPath=dllLibraryPath;
  1671. a.default_dymosim_license_filename=default_dymosim_license_filename;
  1672. a.QJacobianCG_=QJacobianCG_;
  1673. a.QJacobianGC_=QJacobianGC_;
  1674. a.QJacobianCD_=QJacobianCD_;
  1675. a.QJacobian_=0;
  1676. a.QJacobianN_=0;
  1677. a.QJacobianNU_=0;
  1678. a.QJacobianNY_=0;
  1679. a.QJacobianSparse_=0;
  1680. a.QJacobianSparseR_=0;
  1681. a.QJacobianSparseC_=0;
  1682. a.QJacobianNZ_=0;
  1683. a.dymolaTimes_=0;
  1684. a.dymolaTimersLen_=0;
  1685. a.optFunctions=0;
  1686. a.SetDymolaJacobianPointers=SetDymolaJacobianPointers;
  1687. a.GetDymolaTimers=GetDymolaTimers;
  1688. a.SetDymolaJacobianPointers2=SetDymolaJacobianPointers2;
  1689. #ifdef GODESS
  1690. {
  1691. extern struct SimulatorFunctions*GodessSimulatorFunctions();
  1692. a.optFunctions=GodessSimulatorFunctions();
  1693. }
  1694. #endif
  1695. return &a;
  1696. }
  1697. #endif
  1698. #if defined(HACK_SUPPORT_VS2015)
  1699. #if defined(_MSC_VER) && _MSC_VER>=1900
  1700. /*
  1701. below is a stub for missing symbol in VS2015 UCRT used to return stdin/stdout/stderr pointers
  1702. due to changes in UCRT libraries stdin/stdout/stderr compiled with older VC versions can't be linked:
  1703. https://connect.microsoft.com/VisualStudio/feedback/details/1144980
  1704. (converted to C)
  1705. */
  1706. FILE * __cdecl __iob_func(void)
  1707. {
  1708. unsigned char * retaddr = (unsigned char*)_ReturnAddress();
  1709. #ifndef _M_AMD64
  1710. if ( retaddr[0] != 0x83 || retaddr[1] != 0xC0 )
  1711. return stdin;
  1712. switch ( retaddr[2] )
  1713. {
  1714. case 0x20: return (FILE*)((char*)stdout - 0x20);
  1715. case 0x40: return (FILE*)((char*)stderr - 0x40);
  1716. default:return stdin;
  1717. }
  1718. #else
  1719. if ( retaddr[0] != 0x48 || retaddr[1] != 0x83 || retaddr[2] != 0xC0 )
  1720. return stdin;
  1721. switch ( retaddr[3] )
  1722. {
  1723. case 0x30: return (FILE*)((char*)stdout - 0x30);
  1724. case 0x60: return (FILE*)((char*)stderr - 0x60);
  1725. default:return stdin;
  1726. }
  1727. #endif
  1728. }
  1729. #endif
  1730. #endif
  1731. #endif