matrixop.h 49 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255
  1. #ifndef MATRIXOP_H
  2. #define MATRIXOP_H
  3. /* * Declarations of matrix functions in Dymola
  4. *
  5. * Copyright (C) 1997-2001 Dynasim AB.
  6. * All rights reserved.
  7. *
  8. * Author: Hans Olsson Dynasim AB, 1999
  9. * Version: 1.4, 1999-09-24*/
  10. /* */
  11. #include "matrixop1.h"
  12. #include <math.h>
  13. #if !defined(DSE_STRUCT)
  14. #if defined(DS_EMBEDDED)
  15. #define DSE_STRUCT sts->
  16. #else
  17. #define DSE_STRUCT
  18. #endif
  19. #endif
  20. #ifndef DYNX
  21. #if _MSC_VER>=1700
  22. #define DYNX(s,i) (*(s+i))
  23. #else
  24. #define DYNX(s,i) s[i]
  25. #endif
  26. #endif
  27. #if defined(DS_EMBEDDED)
  28. #include <dsembedded.h>
  29. #include <dse_dymosim.c>
  30. #endif
  31. #if defined(Matlab5) || defined(Matlab51) || defined(SimStruct) || defined(DYM2DS) || defined(FMU_SOURCE_CODE_EXPORT)
  32. #if !defined(DynSimStruct)
  33. #define DynSimStruct 1
  34. /* Needed for dsutil.h in Modelica mode. */
  35. #endif
  36. #endif
  37. #ifndef DYNassert_static
  38. #define DYNassert_static(e) do { enum { DYN_assert_static_=1/(e) }; } while(0)
  39. #endif
  40. #define Modelica
  41. #include <userdefs.h>
  42. #include <stddef.h>
  43. #include <stdarg.h>
  44. #include <math.h>
  45. #include "dsblock.h"
  46. #include "dsutil.h"
  47. #include "ModelicaUtilities.h"
  48. /* Also present in moutil.c*/
  49. #define IF (
  50. #define THEN ) ? (
  51. #define ELSE ) :
  52. #define AND &&
  53. #define OR ||
  54. #define NOT !
  55. #define true 1
  56. #define false 0
  57. #if !defined(NDEBUG) && (defined(DYMOSIM) || defined(BUILDFMU))
  58. #undef Assert
  59. #undef AssertModelica
  60. #define Assert(b,x) ((!(b))?AssertModelicaF(b,#b,x):(void)(0))
  61. #define AssertModelica(b,bs,x) ((!(b))?AssertModelicaF(b,bs,x):(void)(0))
  62. #define AssertModelica3(b,bs,x,l) ((!(b))?AssertModelicaF4(b,bs,x,l,(int*)(0)):(void)(0))
  63. #define AssertModelica4(b,bs,x,l,hi) ((!(b))?AssertModelicaF4(b,bs,x,l,&hi):(void)(hi=0))
  64. #else
  65. #undef Assert
  66. #undef AssertModelica
  67. #define Assert(b,x)
  68. #define AssertModelica(b,bs,x)
  69. #define AssertModelica3(b,bs,x,l)
  70. #define AssertModelica4(b,bs,x,l,hi)
  71. #endif
  72. #if !defined(RT) && !defined(DYN_MULTINSTANCE)
  73. Real Realbuffer[
  74. #ifndef DYNREALBUFFER
  75. 7000000
  76. #else
  77. DYNREALBUFFER
  78. #endif
  79. ];
  80. Integer Integerbuffer[50000];
  81. SizeType Sizebuffer[50000];
  82. String Stringbuffer[10000];
  83. char simplestring[100000];
  84. #endif
  85. #if !defined(RT)
  86. Real RealbufferNon[
  87. #ifndef DYNREALBUFFER
  88. 70000
  89. #else
  90. DYNREALBUFFER/10
  91. #endif
  92. ];
  93. Integer IntegerbufferNon[50000];
  94. SizeType SizebufferNon[50000];
  95. String StringbufferNon[10000];
  96. char simplestringNon[100000];
  97. DYMOLA_STATIC Real* EndRealbufferNon=RealbufferNon+sizeof(RealbufferNon)/sizeof(*RealbufferNon);
  98. DYMOLA_STATIC Integer* EndIntegerbufferNon=IntegerbufferNon+sizeof(IntegerbufferNon)/sizeof(*IntegerbufferNon);
  99. DYMOLA_STATIC SizeType* EndSizebufferNon=SizebufferNon+sizeof(SizebufferNon)/sizeof(*SizebufferNon);
  100. DYMOLA_STATIC String* EndStringbufferNon=StringbufferNon+sizeof(StringbufferNon)/sizeof(*StringbufferNon);
  101. DYMOLA_STATIC char* EndsimplestringNon=simplestringNon+sizeof(simplestringNon)/sizeof(*simplestringNon);
  102. #endif
  103. #if !defined(RT) && !defined(DYN_MULTINSTANCE)
  104. #if (defined(_OPENMP) && !defined(DISABLE_DYMOLA_OPENMP))
  105. DYMOLA_STATIC Real* EndRealbuffer=0;
  106. #pragma omp threadprivate(EndRealbuffer)
  107. DYMOLA_STATIC Integer* EndIntegerbuffer=0;
  108. #pragma omp threadprivate(EndIntegerbuffer)
  109. DYMOLA_STATIC SizeType* EndSizebuffer=0;
  110. #pragma omp threadprivate(EndSizebuffer)
  111. DYMOLA_STATIC String* EndStringbuffer=0;
  112. #pragma omp threadprivate(EndStringbuffer)
  113. DYMOLA_STATIC char* Endsimplestring=0;
  114. #pragma omp threadprivate(Endsimplestring)
  115. DYMOLA_STATIC Real* EndRealbuffer2=Realbuffer+sizeof(Realbuffer)/sizeof(*Realbuffer);
  116. DYMOLA_STATIC Integer* EndIntegerbuffer2=Integerbuffer+sizeof(Integerbuffer)/sizeof(*Integerbuffer);
  117. DYMOLA_STATIC SizeType* EndSizebuffer2=Sizebuffer+sizeof(Sizebuffer)/sizeof(*Sizebuffer);
  118. DYMOLA_STATIC String* EndStringbuffer2=Stringbuffer+sizeof(Stringbuffer)/sizeof(*Stringbuffer);
  119. DYMOLA_STATIC char* Endsimplestring2=simplestring+sizeof(simplestring)/sizeof(*simplestring);
  120. #else
  121. DYMOLA_STATIC Real* EndRealbuffer=Realbuffer+sizeof(Realbuffer)/sizeof(*Realbuffer);
  122. DYMOLA_STATIC Integer* EndIntegerbuffer=Integerbuffer+sizeof(Integerbuffer)/sizeof(*Integerbuffer);
  123. DYMOLA_STATIC SizeType* EndSizebuffer=Sizebuffer+sizeof(Sizebuffer)/sizeof(*Sizebuffer);
  124. DYMOLA_STATIC String* EndStringbuffer=Stringbuffer+sizeof(Stringbuffer)/sizeof(*Stringbuffer);
  125. DYMOLA_STATIC char* Endsimplestring=simplestring+sizeof(simplestring)/sizeof(*simplestring);
  126. #endif
  127. #endif
  128. DYMOLA_STATIC void* RecordElement(const RealArray a,...) {
  129. SizeType index;
  130. va_list ap;
  131. va_start(ap,a);
  132. index=FindIndex(a.ndims,a.dims,ap);
  133. va_end(ap);
  134. return (void*)(a.data+index);
  135. }
  136. DYMOLA_STATIC Integer RecordSize(const RealArray a,SizeType i) {
  137. return RealSize(a,i);
  138. }
  139. DYMOLA_STATIC RealArray RecordLeading(int i,int j,RealArray a) {
  140. return RealLeading(i,j,a);
  141. }
  142. DYMOLA_STATIC IntegerArray RecordSizes(const RealArray a) {
  143. IntegerArray b;
  144. b=RealSizes(a);
  145. Assert(b.ndims>0 && b.dims[0]>0,"Error in record logic");
  146. b.dims[0]-=1;
  147. return b;
  148. }
  149. DYMOLA_STATIC Real *RealTemp(SizeType r);
  150. DYMOLA_STATIC Integer *IntegerTemp(SizeType r);
  151. DYMOLA_STATIC SizeType *SizeTemp(SizeType r);
  152. DYMOLA_STATIC String * StringTemp(SizeType r);
  153. DYMOLA_STATIC Integer RealMatchingSizes(const RealArray a,const RealArray b);
  154. DYMOLA_STATIC Integer IntegerMatchingSizes(const IntegerArray a,const IntegerArray b);
  155. DYMOLA_STATIC Integer StringMatchingSizes(const StringArray a,const StringArray b);
  156. DYMOLA_STATIC RealArray RecordArrayRealSlice(const RealArray a,size_t off) {
  157. RealArray temp;
  158. SizeType i,len;
  159. temp.ndims=a.ndims-1;
  160. temp.dims=SizeTemp(temp.ndims);
  161. for(i=0;i<temp.ndims;++i) temp.dims[i]=a.dims[i];
  162. len=RealNrElements(temp);
  163. temp.data=RealTemp(len);
  164. for(i=0;i<len;++i) temp.data[i]=*((Real*)(((char*)(a.data+i*a.dims[a.ndims-1]))+off));
  165. return temp;
  166. }
  167. DYMOLA_STATIC String Enum2String2(Integer x,Integer leftJustified,Integer minwidth,StringArray names) {
  168. char buf[100];
  169. char*ret;
  170. Assert(minwidth<40,"");
  171. Assert(names.ndims==1 && x>=1 && x<=names.dims[0],"");
  172. sprintf(buf,leftJustified?"%-*s":"%*s",(int)minwidth,names.data[x-1]);
  173. ret=StringAllocate(strlen(buf));
  174. strcpy(ret,buf);
  175. return ret;
  176. }
  177. DYMOLA_STATIC IntegerArray RecordArrayIntegerSlice(const RealArray a,size_t off) {
  178. IntegerArray temp;
  179. SizeType i,len;
  180. temp.ndims=a.ndims-1;
  181. temp.dims=SizeTemp(temp.ndims);
  182. for(i=0;i<temp.ndims;++i) temp.dims[i]=a.dims[i];
  183. len=IntegerNrElements(temp);
  184. temp.data=IntegerTemp(len);
  185. for(i=0;i<len;++i) temp.data[i]=*((Integer*)(((char*)(a.data+i*a.dims[a.ndims-1]))+off));
  186. return temp;
  187. }
  188. DYMOLA_STATIC StringArray RecordArrayStringSlice(const RealArray a,size_t off) {
  189. StringArray temp;
  190. SizeType i,len;
  191. temp.ndims=a.ndims-1;
  192. temp.dims=SizeTemp(temp.ndims);
  193. for(i=0;i<temp.ndims;++i) temp.dims[i]=a.dims[i];
  194. len=StringNrElements(temp);
  195. temp.data=StringTemp(len);
  196. for(i=0;i<len;++i) temp.data[i]=*((String*)(((char*)(a.data+i*a.dims[a.ndims-1]))+off));
  197. return temp;
  198. }
  199. DYMOLA_STATIC RealArray RecordArrayRealArraySlice(const RealArray a,size_t off) {
  200. RealArray temp;
  201. RealArray elem,elemi;
  202. SizeType i,j,lenA,lenTemp,lenElem;
  203. Assert(RealNrElements(a)>0,"Can only make record slices of non-empty arrays");
  204. elem=*(RealArray*)((char*)(a.data+0)+off);
  205. temp.ndims=a.ndims-1+elem.ndims;
  206. temp.dims=SizeTemp(temp.ndims);
  207. lenElem=RealNrElements(elem);
  208. for(i=0;i<temp.ndims;++i) if (i<a.ndims-1) temp.dims[i]=a.dims[i]; else temp.dims[i]=elem.dims[i-(a.ndims-1)];
  209. lenTemp=RealNrElements(temp);
  210. lenA=RealNrElements(a)/a.dims[a.ndims-1];
  211. Assert(lenTemp==lenA*lenElem,"Consistency");
  212. temp.data=RealTemp(lenTemp);
  213. for(i=0;i<lenA;++i) {
  214. elemi=*(RealArray*)((char*)(a.data+i*a.dims[a.ndims-1])+off);
  215. Assert(RealMatchingSizes(elemi,elem),"Can only slice homogenous record arrays");
  216. for(j=0;j<lenElem;++j) temp.data[i*lenElem+j]=elemi.data[j];
  217. }
  218. return temp;
  219. }
  220. DYMOLA_STATIC IntegerArray RecordArrayIntegerArraySlice(const RealArray a,size_t off) {
  221. IntegerArray temp;
  222. IntegerArray elem,elemi;
  223. SizeType i,j,lenA,lenTemp,lenElem;
  224. Assert(RealNrElements(a)>0,"Can only make record slices of non-empty arrays");
  225. elem=*(IntegerArray*)((char*)(a.data+0)+off);
  226. temp.ndims=a.ndims-1+elem.ndims;
  227. temp.dims=SizeTemp(temp.ndims);
  228. lenElem=IntegerNrElements(elem);
  229. for(i=0;i<temp.ndims;++i) if (i<a.ndims-1) temp.dims[i]=a.dims[i]; else temp.dims[i]=elem.dims[i-(a.ndims-1)];
  230. lenTemp=IntegerNrElements(temp);
  231. lenA=RealNrElements(a)/a.dims[a.ndims-1];
  232. Assert(lenTemp==lenA*lenElem,"Consistency");
  233. temp.data=IntegerTemp(lenTemp);
  234. for(i=0;i<lenA;++i) {
  235. elemi=*(IntegerArray*)((char*)(a.data+i*a.dims[a.ndims-1])+off);
  236. Assert(IntegerMatchingSizes(elemi,elem),"Can only slice homogenous record arrays");
  237. for(j=0;j<lenElem;++j) temp.data[i*lenElem+j]=elemi.data[j];
  238. }
  239. return temp;
  240. }
  241. DYMOLA_STATIC StringArray RecordArrayStringArraySlice(const RealArray a,size_t off) {
  242. StringArray temp;
  243. StringArray elem,elemi;
  244. SizeType i,j,lenA,lenTemp,lenElem;
  245. Assert(RealNrElements(a)>0,"Can only make record slices of non-empty arrays");
  246. elem=*(StringArray*)((char*)(a.data+0)+off);
  247. temp.ndims=a.ndims-1+elem.ndims;
  248. temp.dims=SizeTemp(temp.ndims);
  249. lenElem=StringNrElements(elem);
  250. for(i=0;i<temp.ndims;++i) if (i<a.ndims-1) temp.dims[i]=a.dims[i]; else temp.dims[i]=elem.dims[i-(a.ndims-1)];
  251. lenTemp=StringNrElements(temp);
  252. lenA=RealNrElements(a)/a.dims[a.ndims-1];
  253. Assert(lenTemp==lenA*lenElem,"Consistency");
  254. temp.data=StringTemp(lenTemp);
  255. for(i=0;i<lenA;++i) {
  256. elemi=*(StringArray*)((char*)(a.data+i*a.dims[a.ndims-1])+off);
  257. Assert(StringMatchingSizes(elemi,elem),"Can only slice homogenous record arrays");
  258. for(j=0;j<lenElem;++j) temp.data[i*lenElem+j]=elemi.data[j];
  259. }
  260. return temp;
  261. }
  262. static double invmodDymola(double x,double y) {double quot=x/y;return (floor(quot));}
  263. /* static double invmodDymola(double x,double y) {double quot=x/y;return (quot>=0)?floor(quot):ceil(quot);} */
  264. static double abs_inverse(double x,double y, const char*ys) {
  265. Assert(x>=0, StringAdd("The inverse of the function abs is not defined for negative values.\nFor: ",ys));
  266. return (y>=0 ? x :-x);
  267. }
  268. static double powIntExp_inverse(double x, int exponent, double y, const char*ys) {
  269. double xabs;
  270. double yabs;
  271. double invExp;
  272. Assert(exponent!=0, StringAdd("The inverse of the function y^e is not unique for e = 0.\nFor: ", ys));
  273. invExp = 1.0/exponent;
  274. xabs = (x < 0.0 ? -x : x);
  275. if (exponent % 2 == 0) {
  276. /* even exponent */
  277. Assert(x >=0, StringAdd("The inverse of the function y^e is undefined for negative values if e is even.\nFor: ",ys));
  278. yabs = pow(xabs, invExp);
  279. return abs_inverse(yabs, y, ys);
  280. }
  281. yabs = pow(xabs, invExp);
  282. return (x < 0.0 ? -yabs : yabs);
  283. }
  284. static double sin_inverse(double x,double y, const char*ys) {
  285. double pi;
  286. double n;
  287. Assert(x >=-1 && x <=1, StringAdd("The inverse of the function sin is not defined for values greater than one.\nFor: ", ys));
  288. pi = 4*atan(1);
  289. n = invmodDymola((y+pi/2), 2*pi);
  290. return ((y - n*2*pi) > pi/2 ? pi - asin(x) : asin(x)) + n*2*pi;
  291. }
  292. static double cos_inverse(double x,double y, const char*ys) {
  293. double pi;
  294. double n;
  295. Assert(x >=-1 && x <=1, StringAdd("The inverse of the function cos is not defined for values greater than one.\nFor: ", ys));
  296. pi = 4*atan(1);
  297. n = invmodDymola(y, 2*pi);
  298. return (((y - n*2*pi) > pi) ? 2*pi - acos(x) : acos(x)) + n*2*pi;
  299. }
  300. static double tan_inverse(double x,double y, const char*ys) {
  301. double pi;
  302. double n;
  303. pi = 4*atan(1);
  304. n = invmodDymola((y+pi/2), pi);
  305. return atan(x) + n*pi;
  306. }
  307. static double semiLinear(double md,double p,double n) {
  308. if (md>0) return md*p;
  309. else return md*n;
  310. }
  311. DYMOLA_STATIC void terminate(const char*message) {
  312. if (*(GlobalErrorPointer())!=-999) {
  313. DymosimMessage("Simulation successfully terminated");
  314. DymosimMessage((char*)message);
  315. *(GlobalErrorPointer()) = -999;
  316. }
  317. }
  318. DYMOLA_STATIC void resetState(void) {
  319. *(GlobalErrorPointer()) = -998;
  320. }
  321. #define SetRealVectorElement(v,a,i) (DYNX((a).data,(i)-1)=(v))
  322. #define SetIntegerVectorElement(v,a,i) (DYNX((a).data,(i)-1)=(v))
  323. #define SetStringVectorElement(v,a,i) (DYNX((a).data,(i)-1)=(v))
  324. #define RealVectorElement(a,i) (DYNX((a).data,(i)-1))
  325. #define IntegerVectorElement(a,i) (DYNX((a).data,(i)-1))
  326. #define StringVectorElement(a,i) (DYNX((a).data,(i)-1))
  327. #define SetRealMatrixElement(v,a,i,j) (DYNX((a).data,((i)-1)*(a).dims[1]+((j)-1))=(v))
  328. #define SetIntegerMatrixElement(v,a,i,j) (DYNX((a).data,((i)-1)*(a).dims[1]+((j)-1))=(v))
  329. #define SetStringMatrixElement(v,a,i,j) (DYNX((a).data,((i)-1)*(a).dims[1]+((j)-1))=(v))
  330. #define RealMatrixElement(a,i,j) (DYNX((a).data,((i)-1)*(a).dims[1]+((j)-1)))
  331. #define IntegerMatrixElement(a,i,j) (DYNX((a).data,((i)-1)*(a).dims[1]+((j)-1)))
  332. #define StringMatrixElement(a,i,j) (DYNX((a).data,((i)-1)*(a).dims[1]+((j)-1)))
  333. #define SetArray3Element(v,a,i,j,k) (DYNX((a).data,(((i)-1)*(a).dims[1]+((j)-1))*(a).dims[2]+((k)-1))=(v))
  334. #define Array3Element(a,i,j,k) (DYNX((a).data,(((i)-1)*(a).dims[1]+((j)-1))*(a).dims[2]+((k)-1)))
  335. #define DYNSizeSimple(v,i) (v.dims[(i)-1])
  336. /* Routines for moutil.h */
  337. #if !defined(LINALG_PACK)
  338. #define LINALG_PACK
  339. #define DeclareRealMatrix(Mat,r,c) RealArray Mat=RealTemporaryMatrix(r,c);int Mat##__m=r
  340. #define DeclareRealVector(Mat,r) RealArray Mat=RealTemporaryVector(r)
  341. /* Transposed to be compatible with Fortran */
  342. #define DeclareStaticRealMatrix(Mat,r,c) \
  343. static Real Mat##__internal[(r==0 || c==0)? 1 : r*c] ZERO_INITIALIZED;\
  344. static SizeType Mat##__dims[2]={r,c};\
  345. static RealArray Mat={2,Mat##__dims,Mat##__internal};\
  346. static int Mat##__m=r
  347. #define DeclareDidRealMatrix(Mat,r,c,hv) \
  348. Real *Mat##__internal=&(hv);\
  349. static SizeType Mat##__dims[2]={r,c};\
  350. RealArray Mat={2,Mat##__dims,Mat##__internal};\
  351. static int Mat##__m=r
  352. #define MatrixNrRows(Mat) (Mat##__m,Mat.dims[0])
  353. #define MatrixNrColumns(Mat) (Mat##__m,Mat.dims[1])
  354. #define DeclareStaticRealVector(Mat,r) \
  355. static Real Mat##__internal[r] ZERO_INITIALIZED;\
  356. static SizeType Mat##__dims[1]={r};\
  357. static RealArray Mat={1,Mat##__dims,Mat##__internal}
  358. #define DeclareDidRealVector(Mat,r,hv) \
  359. Real *Mat##__internal=&(hv);\
  360. static SizeType Mat##__dims[1]={r};\
  361. RealArray Mat={1,Mat##__dims,Mat##__internal}
  362. #define DeclareDidRealVector1(Mat,r,hv) \
  363. Real *Mat##__internal=&(hv);\
  364. static SizeType Mat##__dims[1]={r};\
  365. RealArray Mat={1,Mat##__dims,0}
  366. #define DeclareDidRealVector2(Mat,r,hv) \
  367. Mat.data=Mat##__internal
  368. #define DeclareDidRealMatrix1(Mat,r,c,hv) \
  369. Real *Mat##__internal=&(hv);\
  370. static SizeType Mat##__dims[2]={r,c};\
  371. RealArray Mat={2,Mat##__dims,0};\
  372. static int Mat##__m=r
  373. #define DeclareDidRealMatrix2(Mat,r,c,hv) \
  374. Mat.data=Mat##__internal
  375. #define SetMatrix(Mat,i,j,val) DYNX(Mat##__internal,(i-1)+(j-1)*Mat##__m)=val
  376. #define SetMatrixLeading(Mat,i,j,ld,val) DYNX(Mat##__internal,(i-1)+(j-1)*ld)=val
  377. #define GetMatrix(Mat,i,j) DYNX(Mat##__internal, (i-1)+(j-1)*Mat##__m)
  378. #define GetVector(Mat,i) DYNX(Mat##__internal,(i-1))
  379. #define SetVector(Mat,i,val) DYNX(Mat##__internal,(i-1))=val
  380. #define SetVectorSlice(Mat,i,val) CopySlice(Mat##__internal+i-1,val)
  381. /* Special case for Factored matrices. We only consider square factored matrices */
  382. #define DeclareStaticFactoredRealMatrix(Mat,n) \
  383. DeclareStaticRealMatrix(Mat,n,n);\
  384. static Real Mat##__DWork[n*n+6*n] ZERO_INITIALIZED; \
  385. static int Mat##__IWork[2*n+1];\
  386. static int Mat##__FactoredV=0;int* Mat##__FactoredP=&(Mat##__FactoredV)
  387. #define DeclareDidFactoredRealMatrix1(Mat,n,hv,nhv,iv,niv) \
  388. DeclareDidRealMatrix1(Mat,n,n,hv);\
  389. Real* Mat##__DWork=(&hv)+(n*n); \
  390. int* Mat##__IWork=&(iv);\
  391. int* Mat##__FactoredP=&(iv)+(2*n+1)
  392. #define DeclareDidFactoredRealMatrix2(Mat,n,hv,nhv,iv,niv) \
  393. DeclareDidRealMatrix2(Mat,n,n,hv);DYNassert_static((n*n+(n*n+6*n))==(nhv) && (2*n+2)==(niv))
  394. #define DeclareFactoredRealMatrix(Mat,n) \
  395. DeclareRealMatrix(Mat,n,n);\
  396. Real *Mat##__DWork=RealTemp(n*n+6*n);\
  397. int *Mat##__IWork=IntegerTemp(2*n+1);\
  398. int Mat##__FactoredV=0;int* Mat##__FactoredP=&(Mat##__FactoredV)
  399. #define NeedFactor(Mat) (*(Mat##__FactoredP)==0)
  400. #define SetNeedFactor(Mat) *(Mat##__FactoredP)=0
  401. #define LinearSystemOfEquations(A, b, x, n) \
  402. DeclareFactoredRealMatrix(A, n); \
  403. DeclareRealVector(b, n); \
  404. DeclareRealVector(x, n); \
  405. int factoredForParametersV_=0;int*factoredForParametersP_=&factoredForParametersV_;\
  406. int factoredForEventsV_=0;int*factoredForEventsP_=&factoredForEventsV_;\
  407. const int NewParametersJac=1;\
  408. A##__Mark = PushMark();\
  409. RealFillAssign(b,0.0); \
  410. SetNeedFactor(A);
  411. #define StaticLinearSystemOfEquations(A,b,x,n) \
  412. DeclareStaticFactoredRealMatrix(A, n); \
  413. DeclareStaticRealVector(b, n); \
  414. DeclareStaticRealVector(x, n); \
  415. static int factoredForParametersV_=0;int*factoredForParametersP_=&factoredForParametersV_;\
  416. static int factoredForEventsV_=0;int*factoredForEventsP_=&factoredForEventsV_;\
  417. const int NewParametersJac=*(factoredForParametersP_) != dymolaParametersNr_;\
  418. RealFillAssign(b,0.0); \
  419. if (Init_) SetNeedFactor(A);
  420. #define DidLinearSystemOfEquations(A,b,x,n,hv,nhv,iv,niv) \
  421. DeclareDidFactoredRealMatrix1(A, n, hv, (n*n*2+6*n), iv, (2*n+2)); \
  422. DeclareDidRealVector1(b, n, (&hv)[(n*n*2+6*n)]); \
  423. DeclareDidRealVector1(x, n, (&hv)[(n*n*2+6*n+n)]); \
  424. int* factoredForParametersP_=&(iv)+(2*n+2);\
  425. int* factoredForEventsP_=&(iv)+(2*n+3);\
  426. int NewParametersJac=*(factoredForParametersP_) != dymolaParametersNr_;\
  427. DYNassert_static( (n*n+(n*n+8*n))==nhv && (2*n+4)==niv);\
  428. DeclareDidFactoredRealMatrix2(A, n, hv, (n*n*2+6*n), iv, (2*n+2)); \
  429. DeclareDidRealVector2(b, n, (&hv)[(n*n*2+6*n)]); \
  430. DeclareDidRealVector2(x, n, (&hv)[(n*n*2+6*n+n)]); \
  431. RealFillAssign(b,0.0); \
  432. if (Init_) SetNeedFactor(A);
  433. #if 0
  434. #define OverdeterminedLinearSystemOfEquations(A,b,x,m,n) \
  435. static Real A##__internal[m*n] ZERO_INITIALIZED;\
  436. static SizeType A##__dims[2]={m,n};\
  437. static RealArray A={2,A##__dims,A##__internal};\
  438. static int A##__m=m;\
  439. static Real A##__DWork[n*n+6*n] ZERO_INITIALIZED; \
  440. static int A##__IWork[2*n+1];\
  441. static int A##__FactoredV=0;int* Mat##__FactoredP=Mat##__FactoredV;\
  442. DeclareStaticRealVector(b, m); \
  443. DeclareStaticRealVector(x, n); \
  444. static int factoredForParametersV_=0;int*factoredForParametersP_=&factoredForParametersV_;\
  445. static int factoredForEventsV_=0;int*factoredForEventsP_=&factoredForEventsV_;\
  446. const int NewParametersJac=*(factoredForParametersP_) != dymolaParametersNr_;\
  447. RealFillAssign(b,0.0); \
  448. if (Init_) SetNeedFactor(A);
  449. #define OverdeterminedDidLinearSystemOfEquations(A,b,x,m,n,hv,nhv,iv,niv) \
  450. Real A##__internal=&(hv);\
  451. static SizeType A##__dims[2]={m,n};\
  452. RealArray A={2,A##__dims,0};\
  453. static int A##__m=m;\
  454. Real* A##__DWork=&(hv)+m*n; \
  455. int* A##__IWork=&(iv);\
  456. int* Mat##__FactoredP=&(iv)+(2*n+1);\
  457. DeclareDidRealVector1(b, m, (&hv)[m*n+n*n+6*n]); \
  458. DeclareDidRealVector1(x, n, (&hv)[m*n+n*n+6*n+m]); \
  459. int*factoredForParametersP_=&(iv)+(2*n+2);\
  460. int*factoredForEventsP_=&(iv)+(2*n+3);\
  461. const int NewParametersJac=*(factoredForParametersP_) != dymolaParametersNr_;
  462. DYNassert_static(m*n+n*n+6*n+m+n==nhv && (2*n+4)==niv);\
  463. RealFillAssign(b,0.0); A.data=A##__internal;DeclareDidRealVector2(b, m, (&hv)[m*n+n*n+6*n]);DeclareDidRealVector2(x, n, (&hv)[m*n+n*n+6*n+m]);\
  464. if (Init_) SetNeedFactor(A);
  465. #endif
  466. static void ReportDummySelection(const char*const varnames_[],const double*Pvar,double t,int m,int n) {
  467. int i,j,first=1;
  468. m=n-m;
  469. for(i=0;i<n;++i) {
  470. int selected=0;
  471. for(j=0;j<m;++j) selected=selected || Pvar[j*n+i];
  472. if (selected) {
  473. char str[1000];
  474. if (first)
  475. sprintf(str,"Selected at %g:\n %s.stateSelect=StateSelect.always",t,varnames_[i]);
  476. else
  477. sprintf(str,", %s.stateSelect=StateSelect.always",varnames_[i]);
  478. DymosimMessage(str);
  479. first=0;
  480. }
  481. }
  482. }
  483. #define DummyLinear(Jac,b,m,n,Pvar,var,stateP) \
  484. {\
  485. LIBDS_API int CheckP(const double*,double*,int,int,int*,const double*);\
  486. LIBDS_API void ComputeP(const double*,double*,int,int,int,int*,double*);\
  487. DeclareStaticRealMatrix(JacCopy_,m,n);\
  488. int i_,j_;\
  489. static int Pivots_[3*n];\
  490. if (Init_ || DYNEvent && CheckP(&GetMatrix(Jac,1,1),&(GetMatrix(JacCopy_,1,1)),m,n,Pivots_,Pvar)) {\
  491. RealArray varP_=var;\
  492. ComputeP(&GetMatrix(Jac,1,1),&(GetMatrix(JacCopy_,1,1)),m,m,n,Pivots_,Pvar);\
  493. {int m1_=n-m,n_=n,uno_=1;double d1_=1.0,d2_=0.0;\
  494. dgemv_("T",&n_,&m1_,&d1_,Pvar,&n_,varP_.data,&uno_,\
  495. &d2_,stateP,&uno_);}\
  496. Release();\
  497. for(i_=m+1;i_<=n;i_++) SetVector(b,i_,(stateP)[i_-(m+1)]);\
  498. if (PrintEvent&(1<<13)) ReportDummySelection(varnames_,Pvar,DYNTime,m,n);\
  499. for(i_=m+1;i_<=n;i_++) for(j_=1;j_<=n;j_++) SetMatrix(Jac,i_,j_,(Pvar)[(i_-(m+1))*n+(j_-1)]);\
  500. AnyREvent_=true;\
  501. } else if (CheckP(&GetMatrix(Jac,1,1),&(GetMatrix(JacCopy_,1,1)),m,n,Pivots_,Pvar)) triggerStepEvent_=1;\
  502. if ((PrintEvent&(1<<14))&&terminal()) ReportDummySelection(varnames_,Pvar,DYNTime,m,n);\
  503. }
  504. #define DummyLinearDid(Jac,b,m,n,Pvar,var,stateP, hv, nhv, iv, niv) \
  505. {\
  506. LIBDS_API int CheckP(const double*,double*,int,int,int*,const double*);\
  507. LIBDS_API void ComputeP(const double*,double*,int,int,int,int*,double*);\
  508. DeclareDidRealMatrix1(JacCopy_,m,n,hv);\
  509. int i_,j_;\
  510. int*Pivots_=&(iv);\
  511. DYNassert_static(m*n==nhv && (3*n)==niv);\
  512. if (Init_ || DYNEvent && CheckP(&GetMatrix(Jac,1,1),&(GetMatrix(JacCopy_,1,1)),m,n,Pivots_,Pvar)) {\
  513. RealArray varP_=var;\
  514. ComputeP(&GetMatrix(Jac,1,1),&(GetMatrix(JacCopy_,1,1)),m,m,n,Pivots_,Pvar);\
  515. {int m1_=n-m,n_=n,uno_=1;double d1_=1.0,d2_=0.0;\
  516. dgemv_("T",&n_,&m1_,&d1_,Pvar,&n_,varP_.data,&uno_,\
  517. &d2_,stateP,&uno_);}\
  518. Release();\
  519. for(i_=m+1;i_<=n;i_++) SetVector(b,i_,(stateP)[i_-(m+1)]);\
  520. if (PrintEvent&(1<<13)) ReportDummySelection(varnames_,Pvar,DYNTime,m,n);\
  521. for(i_=m+1;i_<=n;i_++) for(j_=1;j_<=n;j_++) SetMatrix(Jac,i_,j_,(Pvar)[(i_-(m+1))*n+(j_-1)]);\
  522. AnyREvent_=true;\
  523. } else if (CheckP(&GetMatrix(Jac,1,1),&(GetMatrix(JacCopy_,1,1)),m,n,Pivots_,Pvar)) triggerStepEvent_=1;\
  524. if ((PrintEvent&(1<<14))&&terminal()) ReportDummySelection(varnames_,Pvar,DYNTime,m,n);\
  525. }
  526. #define DummyLinearVar(Jac,b,m1,m2,n,Pvar,var,stateP) \
  527. {\
  528. LIBDS_API void DummyLinearVar1(int,double*,double*,double*,int,int,int,int*,double*);\
  529. LIBDS_API void DummyLinearVar2a(double*,double*,double*,int,int,int,int*,double*,const double*,double*);\
  530. LIBDS_API void DummyLinearVar2b(double*,double*,double*,int,int,int,int*,double*,double*);\
  531. DeclareStaticRealMatrix(Jac2_,n,m2);\
  532. static int Pivots_[1+3*n+m2];\
  533. DummyLinearVar1(DYNEvent,&GetMatrix(Jac,1,1),&GetMatrix(Jac2_,1,1),&GetVector(b,1),m1,m2,n,Pivots_,Pvar);\
  534. if (DYNEvent) {\
  535. RealArray varP_=var;\
  536. DummyLinearVar2a(&GetMatrix(Jac,1,1),&GetMatrix(Jac2_,1,1),&GetVector(b,1),m1,m2,n,Pivots_,Pvar,varP_.data,stateP);\
  537. Release();\
  538. if (PrintEvent&(1<<13)) ReportDummySelection(varnames_,Pvar,DYNTime,m1,n);\
  539. AnyREvent_=true;\
  540. } else DummyLinearVar2b(&GetMatrix(Jac,1,1),&GetMatrix(Jac2_,1,1),&GetVector(b,1),m1,m2,n,Pivots_,Pvar,stateP);\
  541. }
  542. #define DummyLinearVarDid(Jac,b,m1,m2,n,Pvar,var,stateP, hv, nhv, iv, niv) \
  543. {\
  544. LIBDS_API void DummyLinearVar1(int,double*,double*,double*,int,int,int,int*,double*);\
  545. LIBDS_API void DummyLinearVar2a(double*,double*,double*,int,int,int,int*,double*,const double*,double*);\
  546. LIBDS_API void DummyLinearVar2b(double*,double*,double*,int,int,int,int*,double*,double*);\
  547. DeclareDidRealMatrix(Jac2_,n,m2,hv);\
  548. int*Pivots_=&(iv);\
  549. DYNassert_static( (n*m2)==nhv && (1+3*n+m2)==niv );\
  550. DummyLinearVar1(DYNEvent,&GetMatrix(Jac,1,1),&GetMatrix(Jac2_,1,1),&GetVector(b,1),m1,m2,n,Pivots_,Pvar);\
  551. if (DYNEvent) {\
  552. RealArray varP_=var;\
  553. DummyLinearVar2a(&GetMatrix(Jac,1,1),&GetMatrix(Jac2_,1,1),&GetVector(b,1),m1,m2,n,Pivots_,Pvar,varP_.data,stateP);\
  554. Release();\
  555. if (PrintEvent&(1<<13)) ReportDummySelection(varnames_,Pvar,DYNTime,m1,n);\
  556. AnyREvent_=true;\
  557. } else DummyLinearVar2b(&GetMatrix(Jac,1,1),&GetMatrix(Jac2_,1,1),&GetVector(b,1),m1,m2,n,Pivots_,Pvar,stateP);\
  558. }
  559. #define OverdeterminedDummyLinear(Jac,b,l,m,m1,m2,n,Pvar,var,stateP) \
  560. {\
  561. LIBDS_API void CreateJacFullLin(double*,double*,const double*,int,int,int,const int*);\
  562. LIBDS_API void ComputePfull(const double*,double*,int,int,int,int,int,int*,int*,double*,const double*,const double*);\
  563. LIBDS_API int CheckPfull(const double*,double*,int,int,int,int,int,int*,const int*);\
  564. DeclareStaticRealMatrix(JacCopy_,m,n);\
  565. static int IWork_[4*m2+2*m+2*n];\
  566. if (DYNEvent) {\
  567. RealArray varP_=var;\
  568. ComputePfull(&(GetMatrix(Jac,1,1)),&(GetMatrix(JacCopy_,1,1)),l,m,n,m1,m2,IWork_+(m+n),IWork_,Pvar,varP_.data,stateP);\
  569. Release();\
  570. if (PrintEvent&(1<<13)) ReportDummySelection(varnames_,Pvar,DYNTime,m,n);\
  571. AnyREvent_=true;\
  572. } else if (CheckPfull(&(GetMatrix(Jac,1,1)),&(GetMatrix(JacCopy_,1,1)),l,m,n,m1,m2,IWork_+(m+n),IWork_)) triggerStepEvent_=1;\
  573. CreateJacFullLin(&(GetMatrix(Jac,1,1)),&(GetVector(b,1)),stateP,l,m,n,IWork_);\
  574. }
  575. #define OverdeterminedDummyLinearDid(Jac,b,l,m,m1,m2,n,Pvar,var,stateP, hv, nhv, iv, niv) \
  576. {\
  577. LIBDS_API void CreateJacFullLin(double*,double*,const double*,int,int,int,const int*);\
  578. LIBDS_API void ComputePfull(const double*,double*,int,int,int,int,int,int*,int*,double*,const double*,const double*);\
  579. LIBDS_API int CheckPfull(const double*,double*,int,int,int,int,int,int*,const int*);\
  580. DeclareDidRealMatrix(JacCopy_,m,n,hv);\
  581. int*IWork_=&iv;\
  582. DYNassert_static( (m*n)==nhv && (4*m2+2*m+2*n)==niv);\
  583. if (DYNEvent) {\
  584. RealArray varP_=var;\
  585. ComputePfull(&(GetMatrix(Jac,1,1)),&(GetMatrix(JacCopy_,1,1)),l,m,n,m1,m2,IWork_+(m+n),IWork_,Pvar,varP_.data,stateP);\
  586. Release();\
  587. if (PrintEvent&(1<<13)) ReportDummySelection(varnames_,Pvar,DYNTime,m,n);\
  588. AnyREvent_=true;\
  589. } else if (CheckPfull(&(GetMatrix(Jac,1,1)),&(GetMatrix(JacCopy_,1,1)),l,m,n,m1,m2,IWork_+(m+n),IWork_)) triggerStepEvent_=1;\
  590. CreateJacFullLin(&(GetMatrix(Jac,1,1)),&(GetVector(b,1)),stateP,l,m,n,IWork_);\
  591. }
  592. #define OverdeterminedLinear(Jac,b,l,m,m1,m2,n) \
  593. {\
  594. LIBDS_API void CreateJacFullLin(double*,double*,const double*,int,int,int,const int*);\
  595. LIBDS_API void ComputePfull(const double*,double*,int,int,int,int,int,int*,int*,double*,const double*,const double*);\
  596. LIBDS_API int CheckPfull(const double*,double*,int,int,int,int,int,int*,const int*);\
  597. DeclareStaticRealMatrix(JacCopy_,m,n);\
  598. static int IWork_[4*m2+2*m+2*n];\
  599. if (DYNEvent) {\
  600. ComputePfull(&(GetMatrix(Jac,1,1)),&(GetMatrix(JacCopy_,1,1)),l,m,n,m1,m2,IWork_+(m+n),IWork_,0,0,0);\
  601. if (PrintEvent&(1<<13)) ReportDummySelection(varnames_,Pvar,DYNTime,m,n);\
  602. AnyREvent_=true;\
  603. } else if (CheckPfull(&(GetMatrix(Jac,1,1)),&(GetMatrix(JacCopy_,1,1)),l,m,n,m1,m2,IWork_+(m+n),IWork_)) triggerStepEvent_=1;\
  604. CreateJacFullLin(&(GetMatrix(Jac,1,1)),&(GetVector(b,1)),0,l,m,n,IWork_);\
  605. }
  606. #define OverdeterminedLinearDid(Jac,b,l,m,m1,m2,n, hv, nhv, iv, niv) \
  607. {\
  608. LIBDS_API void CreateJacFullLin(double*,double*,const double*,int,int,int,const int*);\
  609. LIBDS_API void ComputePfull(const double*,double*,int,int,int,int,int,int*,int*,double*,const double*,const double*);\
  610. LIBDS_API int CheckPfull(const double*,double*,int,int,int,int,int,int*,const int*);\
  611. DeclareDidRealMatrix(JacCopy_,m,n,hv);\
  612. int*IWork_=&iv;\
  613. DYNassert_static(m*n==nhv && 4*m2+2*m+2*n==niv);\
  614. if (DYNEvent) {\
  615. ComputePfull(&(GetMatrix(Jac,1,1)),&(GetMatrix(JacCopy_,1,1)),l,m,n,m1,m2,IWork_+(m+n),IWork_,0,0,0);\
  616. if (PrintEvent&(1<<13)) ReportDummySelection(varnames_,Pvar,DYNTime,m,n);\
  617. AnyREvent_=true;\
  618. } else if (CheckPfull(&(GetMatrix(Jac,1,1)),&(GetMatrix(JacCopy_,1,1)),l,m,n,m1,m2,IWork_+(m+n),IWork_)) triggerStepEvent_=1;\
  619. CreateJacFullLin(&(GetMatrix(Jac,1,1)),&(GetVector(b,1)),0,l,m,n,IWork_);\
  620. }
  621. LIBDS_API int daxpy_(const int*,double*,double*,const int*,double*,const int*);
  622. LIBDS_API int dgemv_(char*,int*,int*,double*,double*,int*,double*,int*,double*,double*,int*);
  623. #define MatrixZeros(J) RealFillAssign(J,0.0)
  624. #define SolveLinearSystemOfEquations(A, b, x, num) \
  625. {int i1_,i2_,i3_,i4_,Ierror_;\
  626. i1_ = A.dims[0]; \
  627. i2_ = A.dims[1]; \
  628. *(factoredForParametersP_) = dymolaParametersNr_; \
  629. i4_ = ! TryLUFactorization_ | (EquilibMatrix_ ? 2 : 0); \
  630. i3_ = num;\
  631. dymli5_(&i3_, &i4_, &(GetMatrix(A,1,1)), &i1_, &i2_, &(GetVector(b,1)), &(GetVector(x,1)),\
  632. &DYNTime, &DYNEvent, &PrintEvent, A##__DWork, A##__IWork,(A##__FactoredP),varnames_,&Ierror_,0);\
  633. RealAssign(x,b);\
  634. if (Ierror_!=0) {*(DSE_STRUCT QiErr)=Ierror_;DYN_GOTOLEAVE;}\
  635. }
  636. static void dymli3MI_(int* sysnr, int *fact, double *a,
  637. int lda, int n,
  638. double* b, double *Time, int *Event, int* PrintEvent,
  639. double* dwork, int* iwork, int *factor, const char*const*varnames,int* ierr,
  640. double*A0,double*b0,double*AR,double*ARwork,int*ARiwork,
  641. int rrow,int rcol) {
  642. int i,j;
  643. int arFactor=0;
  644. if ((*factor&3)!=0) {
  645. *ierr=0;
  646. for(j=0;j<rcol;++j) {
  647. for(i=0;i<rrow;++i) b0[i]=a[i+j*lda]-A0[i+j*rrow];
  648. for(i=rrow;i<n;++i) b0[i]=0;
  649. dymli3_(sysnr,fact,a,&lda,&n,b0,Time,Event,PrintEvent,dwork,iwork,factor,varnames,ierr);
  650. if (*ierr!=0) {
  651. break;
  652. }
  653. for(i=0;i<rrow && i<rcol;++i) AR[i+j*rcol]=b0[i];
  654. for(i=rrow;i<rcol;++i) AR[i+j*rcol]=0;
  655. AR[j+j*rcol]+=1.0;
  656. }
  657. if (*ierr==0) {
  658. for(i=0;i<n;++i) b0[i]=b[i];
  659. dymli3_(sysnr,fact,a,&lda,&n,b0,Time,Event,PrintEvent,dwork,iwork,factor,varnames,ierr);
  660. }
  661. if (*ierr==0) dymli3_(sysnr,fact,AR,&rcol,&rcol,b0,Time,Event,PrintEvent,ARwork,ARiwork,&arFactor,0,ierr);
  662. if (*ierr==0) for(i=0;i<n;++i) b0[i+n]=b[i];
  663. if (*ierr==0) for(i=0;i<rrow;++i) {double d=0;for(j=0;j<rcol;++j)d+=(a[i+j*lda]-A0[i+j*rrow])*b0[j];b0[i+n]-=d;}
  664. if (*ierr==0) dymli3_(sysnr,fact,a,&lda,&n,b0+n,Time,Event,PrintEvent,dwork,iwork,factor,varnames,ierr);
  665. if (*ierr==0) for(i=0;i<n;++i) b[i]=b0[i+n];
  666. if (*ierr) {
  667. *factor=0;
  668. }
  669. }
  670. if ((*factor&3)==0) {
  671. dymli3_(sysnr,fact,a,&lda,&n,b,Time,Event,PrintEvent,dwork,iwork,factor,varnames,ierr);
  672. for(j=0;j<rcol;++j) for(i=0;i<rrow;++i) A0[i+j*rrow]=a[i+j*lda];
  673. }
  674. }
  675. #define SolveLinearSystemOfEquationsMI(A, b, x, num, rows, rrow, rcol) \
  676. {double d1_,d2_;int i1_,i2_,i3_,i4_,Ierror_;\
  677. static double b0[2*rows];\
  678. static double A0[rrow*rcol];\
  679. DeclareStaticFactoredRealMatrix(Ar,rcol);\
  680. i1_ = DYNX(A.dims,0); \
  681. i2_ = DYNX(A.dims,1); \
  682. *(factoredForParametersP_) = dymolaParametersNr_; \
  683. i4_ = ! TryLUFactorization_ | (EquilibMatrix_ ? 2 : 0); \
  684. d1_ = -1;\
  685. d2_ = 1;\
  686. i3_ = 1;\
  687. if(1)dgemv_("N",&i2_,&i2_,&d1_,&(GetMatrix(A,1,1)),&i1_,&(GetVector(x,1)),&i3_,\
  688. &d2_,&(GetVector(b,1)),&i3_);\
  689. i3_ = num;\
  690. dymli3MI_(&i3_, &i4_, &(GetMatrix(A,1,1)), i1_, i2_, &(GetVector(b,1)), \
  691. &DYNTime, &DYNEvent, &PrintEvent, A##__DWork, A##__IWork,(A##___FactoredP),varnames_,&Ierror_,\
  692. A0,b0,&(GetMatrix(Ar,1,1)),Ar##__DWork,Ar##__IWork, rrow,rcol\
  693. );\
  694. i3_ = 1;\
  695. if(1)daxpy_(&i2_, &d2_, &(GetVector(b,1)), &i3_,&(GetVector(x,1)), &i3_);else RealAssign(x,b);\
  696. if (Ierror_!=0) {*QiErr=Ierror_;DYN_GOTOLEAVE;}\
  697. }
  698. #define DidSolveLinearSystemOfEquationsMI(A, b, x, num, rows, rrow, rcol, hv, nhv, iv, niv) \
  699. {double d1_,d2_;int i1_,i2_,i3_,i4_,Ierror_;\
  700. double*b0=hv;\
  701. double*A0=&(hv)+2*rows;\
  702. DeclareDidFactoredRealMatrix1(Ar,rcol, (&hv)[2*rows+rrow*rcol], nhv-(2*rows+rrow*rcol), iv, 2*rcol+2);\
  703. DeclareDidFactoredRealMatrix2(Ar,rcol, (&hv)[2*rows+rrow*rcol], nhv-(2*rows+rrow*rcol), iv, niv);\
  704. i1_ = DYNX(A.dims,0); \
  705. i2_ = DYNX(A.dims,1); \
  706. *(factoredForParametersP_) = dymolaParametersNr_; \
  707. i4_ = ! TryLUFactorization_ | (EquilibMatrix_ ? 2 : 0); \
  708. d1_ = -1;\
  709. d2_ = 1;\
  710. i3_ = 1;\
  711. if(1)dgemv_("N",&i2_,&i2_,&d1_,&(GetMatrix(A,1,1)),&i1_,&(GetVector(x,1)),&i3_,\
  712. &d2_,&(GetVector(b,1)),&i3_);\
  713. i3_ = num;\
  714. dymli3MI_(&i3_, &i4_, &(GetMatrix(A,1,1)), i1_, i2_, &(GetVector(b,1)), \
  715. &DYNTime, &DYNEvent, &PrintEvent, A##__DWork, A##__IWork,(A##___FactoredP),varnames_,&Ierror_,\
  716. A0,b0,&(GetMatrix(Ar,1,1)),Ar##__DWork,Ar##__IWork, rrow,rcol\
  717. );\
  718. i3_ = 1;\
  719. if(1)daxpy_(&i2_, &d2_, &(GetVector(b,1)), &i3_,&(GetVector(x,1)), &i3_);else RealAssign(x,b);\
  720. if (Ierror_!=0) {*QiErr=Ierror_;DYN_GOTOLEAVE;}\
  721. }
  722. static void SolveScalarError(const char*xs,const char*bs,const char*As,double bval_,double Aval_,int alwaysError) {
  723. char s[1000];
  724. sprintf(s,"Error: %s for %.400s = (%.200s)/(%.200s) = %g/%g",
  725. (alwaysError&1)?"Scalar system is always singular":"Singular inconsistent scalar system",xs,bs,As,bval_,Aval_);
  726. if (alwaysError&2)
  727. DymosimError(s);
  728. else
  729. DymosimMessage(s);
  730. }
  731. static void SolveScalarWarning(const char*xs,double x) {
  732. char s[1000];
  733. sprintf(s,"Singular scalar system. Using minimum norm solution for %.400s = %g\n",xs,x);DymosimMessage(s);
  734. }
  735. #define SolveScalarLinear(A, As, b, bs, x, xs)\
  736. {double Aval_=A;double bval_=b;if (Aval_!=0) x=bval_/Aval_; else if (bval_!=0) {SolveScalarError(xs,bs,As,bval_,Aval_,2);} else if(PrintEvent&(1<<12)) \
  737. { SolveScalarWarning(xs,x);}}
  738. #define SolveScalarLinearMixed(A, As, b, bs, x, xs)\
  739. {double Aval_=A;double bval_=b;if (Aval_!=0) x=bval_/Aval_; else if (bval_!=0) {SolveScalarError(xs,bs,As,bval_,Aval_,0);MixedFailFlag_=1;} else if(PrintEvent&(1<<12)) \
  740. { SolveScalarWarning(xs,x);}}
  741. #define SolveScalarLinearParametric(A, As, b, bs, x, xs)\
  742. {double Aval_=A;double bval_=b;if (Aval_!=0) x=bval_/Aval_; else {SolveScalarError(xs,bs,As,bval_,Aval_,3);}}
  743. #define SolveScalarLinearMixedParametric(A, As, b, bs, x, xs)\
  744. {double Aval_=A;double bval_=b;if (Aval_!=0) x=bval_/Aval_; else {SolveScalarError(xs,bs,As,bval_,Aval_,1);MixedFailFlag_=1;}}
  745. #define SolveLinearSystemOfEquationsMixed(A, b, x, num) \
  746. {int i1_,i2_,i3_,i4_,Ierror_;\
  747. i1_ = DYNX(A.dims,0); \
  748. i2_ = DYNX(A.dims,1); \
  749. *(factoredForParametersP_) = dymolaParametersNr_; \
  750. i4_ = ! TryLUFactorization_ | (EquilibMatrix_ ? 2 : 0); \
  751. i3_ = num;\
  752. dymli5_(&i3_, &i4_, &(GetMatrix(A,1,1)), &i1_, &i2_, &(GetVector(b,1)),&(GetVector(x,1)), \
  753. &DYNTime, &DYNEvent, &PrintEvent, A##__DWork, A##__IWork,(A##__FactoredP),varnames_,&Ierror_,0);\
  754. RealAssign(x,b);\
  755. if (Ierror_!=0) {MixedFailFlag_=Ierror_;Ierror_=0;}\
  756. }
  757. #define SolveLinearSystemOfEquationsMIMixed(A, b, x, num, rows, rrow, rcol) \
  758. {double d1_,d2_;int i1_,i2_,i3_,i4_,Ierror_;\
  759. static double b0[2*rows];\
  760. static double A0[rrow*rcol];\
  761. DeclareStaticFactoredRealMatrix(Ar,rcol);\
  762. i1_ = DYNX(A.dims,0); \
  763. i2_ = DYNX(A.dims,1); \
  764. *(factoredForParametersP_) = dymolaParametersNr_; \
  765. i4_ = ! TryLUFactorization_ | (EquilibMatrix_ ? 2 : 0); \
  766. d1_ = -1;\
  767. d2_ = 1;\
  768. i3_ = 1;\
  769. if(1)dgemv_("N",&i2_,&i2_,&d1_,&(GetMatrix(A,1,1)),&i1_,&(GetVector(x,1)),&i3_,\
  770. &d2_,&(GetVector(b,1)),&i3_);\
  771. i3_ = num;\
  772. dymli3MI_(&i3_, &i4_, &(GetMatrix(A,1,1)), i1_, i2_, &(GetVector(b,1)), \
  773. &DYNTime, &DYNEvent, &PrintEvent, A##__DWork, A##__IWork,(A##__FactoredP),varnames_,&Ierror_,\
  774. A0,b0,&(GetMatrix(Ar,1,1)),Ar##__DWork,Ar##__IWork, rrow,rcol\
  775. );\
  776. i3_ = 1;\
  777. if(1)daxpy_(&i1_, &d2_, &(GetVector(b,1)), &i3_,&(GetVector(x,1)), &i3_);else RealAssign(x,b);\
  778. if (Ierror_!=0) {MixedFailFlag_=Ierror_;Ierror_=0;}\
  779. }
  780. #define DidSolveLinearSystemOfEquationsMIMixed(A, b, x, num, rows, rrow, rcol, hv, nhv, iv, niv) \
  781. {double d1_,d2_;int i1_,i2_,i3_,i4_,Ierror_;\
  782. double*b0=&hv;\
  783. double*A0=&hv+2*rows\
  784. DeclareDidFactoredRealMatrix1(Ar,rcol, (&hv)[2*rows+rrow*rcol], nhv -(2*rows+rrow*rcol), iv, niv);\
  785. DeclareDidFactoredRealMatrix2(Ar,rcol, (&hv)[2*rows+rrow*rcol], nhv -(2*rows+rrow*rcol), iv, niv);\
  786. i1_ = DYNX(A.dims,0); \
  787. i2_ = DYNX(A.dims,1); \
  788. *(factoredForParametersP_) = dymolaParametersNr_; \
  789. i4_ = ! TryLUFactorization_ | (EquilibMatrix_ ? 2 : 0); \
  790. d1_ = -1;\
  791. d2_ = 1;\
  792. i3_ = 1;\
  793. if(1)dgemv_("N",&i2_,&i2_,&d1_,&(GetMatrix(A,1,1)),&i1_,&(GetVector(x,1)),&i3_,\
  794. &d2_,&(GetVector(b,1)),&i3_);\
  795. i3_ = num;\
  796. dymli3MI_(&i3_, &i4_, &(GetMatrix(A,1,1)), i1_, i2_, &(GetVector(b,1)), \
  797. &DYNTime, &DYNEvent, &PrintEvent, A##__DWork, A##__IWork,(A##__FactoredP),varnames_,&Ierror_,\
  798. A0,b0,&(GetMatrix(Ar,1,1)),Ar##__DWork,Ar##__IWork, rrow,rcol\
  799. );\
  800. i3_ = 1;\
  801. if(1)daxpy_(&i1_, &d2_, &(GetVector(b,1)), &i3_,&(GetVector(x,1)), &i3_);else RealAssign(x,b);\
  802. if (Ierror_!=0) {MixedFailFlag_=Ierror_;Ierror_=0;}\
  803. }
  804. #define EndLinearSystemOfEquations(A) PopMark(A##__Mark);Release()
  805. #define EndStaticLinearSystemOfEquations(A)
  806. #endif
  807. #ifdef NDEBUG
  808. #define VerifyIndex(i,m,s) (i)
  809. #else
  810. static unsigned int VerifyIndex(unsigned int i,unsigned int maxVal,const char*s) {
  811. Assert(i<maxVal,s);
  812. return i;
  813. }
  814. #endif
  815. /* Major pop, e.g. at end of integration */
  816. DYMOLA_STATIC void PopAllMarks() {
  817. PopMarkMarks();
  818. PopModelContext();
  819. }
  820. static double spliceFunction2(double,double);
  821. static double spliceFunction2der(double x,double deltax,double dx,double ddeltax);
  822. static double divideDymola(double x,double y);
  823. static double remainderDymola(double x,double y);
  824. static double modulusDymola(double x,double y);
  825. static
  826. #if defined(_MSC_VER) && _MSC_VER>=1200
  827. __inline
  828. #elif __GNUC__
  829. __inline
  830. #endif
  831. double spliceFunctionF(double pos,double neg,double x,double deltax) {
  832. const double d=spliceFunction2(x,deltax);
  833. return d*(d>0 ? pos : 0.0)+(1-d)*(d<1 ? neg : 0.0);
  834. }
  835. DYMOLA_STATIC double spliceFunctionF_der(double pos,double neg,double x,double deltax,double pos_der,double neg_der,double x_der,double deltax_der) {
  836. const double d=spliceFunction2(x,deltax);
  837. return (d*(d>0 ? (pos_der) : 0.0)+(1-d)*(d<1 ? (neg_der) : 0.0)\
  838. + (d>0 && d<1 ? spliceFunction2der((x),(deltax),(x_der),(deltax_der))*((pos)-(neg)): 0.0));
  839. }
  840. #if defined(__GNUC__)
  841. #define MODELICA_NONNULLATTR __attribute__((nonnull))
  842. #if defined(__GNUC_MINOR__) && (__GNUC__ > 3 && __GNUC_MINOR__ > 8)
  843. #define MODELICA_RETURNNONNULLATTR __attribute__((returns_nonnull))
  844. #else
  845. #define MODELICA_RETURNNONNULLATTR
  846. #endif
  847. #elif defined(__ATTR_SAL)
  848. #define MODELICA_NONNULLATTR
  849. #define MODELICA_RETURNNONNULLATTR /* empty since use of _Ret_z_ in matriop.c yileds: error C2059: syntax error : '[' */
  850. #else
  851. #define MODELICA_NONNULLATTR
  852. #define MODELICA_RETURNNONNULLATTR
  853. #endif
  854. #if !defined(__ATTR_SAL) || defined(__MINGW32__)
  855. #define _In_z_
  856. #define _Out_
  857. #endif
  858. static void ModelicaInternal_mkdir(_In_z_ const char* directoryName);
  859. static void ModelicaInternal_rmdir(_In_z_ const char* directoryName);
  860. static int ModelicaInternal_stat(_In_z_ const char* name);
  861. static void ModelicaInternal_rename(_In_z_ const char* oldName, _In_z_ const char* newName) ;
  862. static void ModelicaInternal_removeFile(_In_z_ const char* file);
  863. static void ModelicaInternal_copyFile(_In_z_ const char* oldFile, _In_z_ const char* newFile);
  864. static void ModelicaInternal_readDirectory(_In_z_ const char* directory, int nFiles, _Out_ const char* files[]);
  865. static int ModelicaInternal_getNumberOfFiles(_In_z_ const char* directory);
  866. static const char* ModelicaInternal_fullPathName(_In_z_ const char* name);
  867. static const char* ModelicaInternal_temporaryFileName();
  868. static void ModelicaInternal_print(_In_z_ const char* string, _In_z_ const char* fileName);
  869. static int ModelicaInternal_countLines(_In_z_ const char* fileName);
  870. static void ModelicaInternal_readFile(_In_z_ const char* fileName, _Out_ const char* string[], size_t nLines);
  871. static const char* ModelicaInternal_readLine(_In_z_ const char* fileName, int lineNumber, _Out_ int* endOfFile);
  872. static void ModelicaInternal_chdir(_In_z_ const char* directoryName);
  873. static const char* ModelicaInternal_getcwd(int dummy);
  874. static void ModelicaInternal_getenv(_In_z_ const char* name, int convertToSlash, _Out_ const char**, _Out_ int* exist);
  875. static void ModelicaInternal_setenv(_In_z_ const char* name, _In_z_ const char* value, int convertFromSlash);
  876. static const char* ModelicaStrings_substring(const char* string, int startIndex, int endIndex);
  877. static int ModelicaStrings_length(const char* string);
  878. static int ModelicaStrings_compare(const char* string1, const char* string2, int caseSensitive);
  879. static int ModelicaStrings_skipWhiteSpace(const char* string, int i);
  880. static void ModelicaStrings_scanIdentifier(const char* string, int startIndex, int* nextIndex, const char** identifier);
  881. static void ModelicaStrings_scanInteger(const char* string, int startIndex, int unsignedNumber,
  882. int* nextIndex, int* integerNumber);
  883. static void ModelicaStrings_scanReal(const char* string, int startIndex, int unsignedNumber,
  884. int* nextIndex, double* number);
  885. static void ModelicaStrings_scanString(const char* string, int startIndex,
  886. int* nextIndex, const char** result);
  887. static void ModelicaStreams_closeFile(_In_z_ const char* fileName);
  888. struct ModelicaInternal_readLine_M_struct {
  889. const char* string0_0_0member;
  890. int endOfFile0_0_0member;
  891. };
  892. DYMOLA_STATIC struct ModelicaInternal_readLine_M_struct ModelicaInternal_readLine_M(const char* fileName0_0, int lineNumber0_0) {
  893. {
  894. const char* string0_0;
  895. int endOfFile0_0;
  896. string0_0="";
  897. endOfFile0_0=0;
  898. {
  899. string0_0 = (ModelicaInternal_readLine)(fileName0_0, lineNumber0_0, &endOfFile0_0);
  900. }
  901. {
  902. struct ModelicaInternal_readLine_M_struct out_;
  903. out_.string0_0_0member = string0_0;
  904. out_.endOfFile0_0_0member = endOfFile0_0;
  905. return out_;
  906. }
  907. }}
  908. struct ModelicaInternal_getenv_M_struct {
  909. const char* content0_0_0member;
  910. int exist0_0_0member;
  911. };
  912. DYMOLA_STATIC StringArray ModelicaInternal_readFile_M(const char* fileName0_0) {
  913. size_t s;
  914. StringArray res;
  915. s=ModelicaInternal_countLines(fileName0_0);
  916. res=StringTemporaryVector(s);
  917. ModelicaInternal_readFile(fileName0_0, res.data,s);
  918. return res;
  919. }
  920. DYMOLA_STATIC struct ModelicaInternal_getenv_M_struct ModelicaInternal_getenv_M(const char*
  921. name0_0, int convertToSlash0_0) {
  922. PushContext("ModelicaInternal_getenv")
  923. {
  924. const char* content0_0;
  925. int exist0_0;
  926. content0_0="";
  927. exist0_0=0;
  928. {
  929. (ModelicaInternal_getenv)(name0_0, convertToSlash0_0, &content0_0, &
  930. exist0_0);
  931. }
  932. PopContext()
  933. {
  934. struct ModelicaInternal_getenv_M_struct out_;
  935. out_.content0_0_0member = content0_0;
  936. out_.exist0_0_0member = exist0_0;
  937. return out_;
  938. }
  939. }}
  940. DYMOLA_STATIC StringArray ModelicaInternal_readDirectory_M(const char* directory0_0, int
  941. nNames0_0) {
  942. {
  943. StringArray names0_0;
  944. MarkObject retmark_ = PushMark();
  945. names0_0=StringTemporary( 1, nNames0_0);
  946. retmark_ = PushMark();
  947. StringFillAssign( names0_0, "");
  948. /* Start of real code */
  949. {
  950. (ModelicaInternal_readDirectory)(directory0_0, nNames0_0, names0_0.data);
  951. }
  952. /* Output section */
  953. PopMark(retmark_);
  954. return names0_0;
  955. }}
  956. struct ModelicaStrings_scanInteger_M_struct {
  957. int nextIndex0_0_0member;
  958. int number0_0_0member;
  959. };
  960. DYMOLA_STATIC struct ModelicaStrings_scanInteger_M_struct ModelicaStrings_scanInteger_M
  961. (const char*string0_0, int startTokenIndex0_0, int unsigned0_0) {
  962. struct ModelicaStrings_scanInteger_M_struct out;
  963. ModelicaStrings_scanInteger(string0_0,startTokenIndex0_0, unsigned0_0, &out.nextIndex0_0_0member, &out.number0_0_0member);
  964. return out;
  965. }
  966. struct ModelicaStrings_scanReal_M_struct {
  967. int nextIndex0_0_0member;
  968. double number0_0_0member;
  969. };
  970. DYMOLA_STATIC struct ModelicaStrings_scanReal_M_struct ModelicaStrings_scanReal_M
  971. (const char*string0_0, int startTokenIndex0_0, int unsigned0_0) {
  972. struct ModelicaStrings_scanReal_M_struct out;
  973. ModelicaStrings_scanReal(string0_0,startTokenIndex0_0, unsigned0_0, &out.nextIndex0_0_0member, &out.number0_0_0member);
  974. return out;
  975. }
  976. struct ModelicaStrings_scanString_M_struct {
  977. int nextIndex0_0_0member;
  978. const char* string20_0_0member;
  979. };
  980. DYMOLA_STATIC struct ModelicaStrings_scanString_M_struct ModelicaStrings_scanString_M
  981. (const char*string0_0, int startTokenIndex0_0) {
  982. struct ModelicaStrings_scanString_M_struct out;
  983. ModelicaStrings_scanString(string0_0,startTokenIndex0_0, &out.nextIndex0_0_0member, &out.string20_0_0member);
  984. return out;
  985. }
  986. struct ModelicaStrings_scanIdentifier_M_struct {
  987. int nextIndex0_0_0member;
  988. const char* identifier0_0_0member;
  989. };
  990. DYMOLA_STATIC struct ModelicaStrings_scanIdentifier_M_struct ModelicaStrings_scanIdentifier_M
  991. (const char*string0_0, int startTokenIndex0_0) {
  992. struct ModelicaStrings_scanIdentifier_M_struct out;
  993. ModelicaStrings_scanIdentifier(string0_0,startTokenIndex0_0, &out.nextIndex0_0_0member, &out.identifier0_0_0member);
  994. return out;
  995. }
  996. static void OutputCoverageResult(const char*const classNamesForCoverage_[],
  997. const char*const componentNameStrings_[],
  998. const int componentNameStringMap_[],
  999. const char*const expressionsStringsForCoverage_[],
  1000. const int firstExprIndexForCoverage_[],
  1001. int resultFalse_[], int resultTrue_[],
  1002. int nclasses)
  1003. {
  1004. int i = 0;
  1005. int j = 0;
  1006. #if 0
  1007. DymosimMessage("Result of coverage analysis.");
  1008. DymosimMessage("The following conditions are constant as indicated.");
  1009. for (i = 0; i < nclasses; ++i) {
  1010. Dymola_bool anyfound = false;
  1011. int startindex = firstExprIndexForCoverage_[i];
  1012. int stop = firstExprIndexForCoverage_[i+1];
  1013. for (j = startindex; j < stop; ++j) {
  1014. Dymola_bool isConstant = false;
  1015. Dymola_bool value = false;
  1016. if (resultFalse_[j] != resultTrue_[j]) {
  1017. isConstant = true;
  1018. if (resultFalse_[j] == 1)
  1019. value = false;
  1020. else
  1021. value = true;
  1022. }
  1023. if (isConstant) {
  1024. if (!anyfound) {
  1025. anyfound = true;
  1026. DymosimMessage(classNamesForCoverage_[i]);
  1027. }
  1028. if (!value) {
  1029. DymosimMessage("false ");
  1030. DymosimMessage(expressionsStringsForCoverage_[j]);
  1031. }
  1032. else {
  1033. DymosimMessage("true ");
  1034. DymosimMessage(expressionsStringsForCoverage_[j]);
  1035. }
  1036. }
  1037. }
  1038. }
  1039. #endif
  1040. #if !defined(DYMOLA_DSPACE) && !defined(NO_FILE)
  1041. {
  1042. FILE *f=fopen("Coverage.mo","w");
  1043. if (f) {
  1044. int noutputs = 0;
  1045. fprintf(f,"/* Result of coverage analysis. */\n\n");
  1046. for (i = 0; i < nclasses; ++i) {
  1047. int startindex = firstExprIndexForCoverage_[i];
  1048. int stop = firstExprIndexForCoverage_[i+1];
  1049. for (j = startindex; j < stop; ++j) {
  1050. Dymola_bool isConstant = false;
  1051. Dymola_bool value = false;
  1052. if (resultFalse_[j] != resultTrue_[j]) {
  1053. isConstant = true;
  1054. if (resultFalse_[j] == 1)
  1055. value = false;
  1056. else
  1057. value = true;
  1058. }
  1059. if (isConstant) {
  1060. if (noutputs == 0) {
  1061. fprintf(f,"{");
  1062. } else {
  1063. fprintf(f,",\n");
  1064. }
  1065. ++noutputs;
  1066. fprintf(f,"ModelManagement.Check.Coverage.CoverElement(");
  1067. fprintf(f,"\"%s\"", classNamesForCoverage_[i]);
  1068. fprintf(f,", \"%s\"", componentNameStrings_[componentNameStringMap_[j]]);
  1069. fprintf(f,", \n \"%s\"", expressionsStringsForCoverage_[j]);
  1070. if (!value) fprintf(f,", \"false\"");
  1071. else fprintf(f,", \"true\"");
  1072. fprintf(f,")");
  1073. } else {
  1074. if (noutputs == 0) {
  1075. fprintf(f,"{");
  1076. } else {
  1077. fprintf(f,",\n");
  1078. }
  1079. ++noutputs;
  1080. fprintf(f,"ModelManagement.Check.Coverage.CoverElement(");
  1081. fprintf(f,"\"%s\"", classNamesForCoverage_[i]);
  1082. fprintf(f,", \"%s\"", componentNameStrings_[componentNameStringMap_[j]]);
  1083. fprintf(f,", \n \"%s\"", expressionsStringsForCoverage_[j]);
  1084. fprintf(f,", \"covered\")");
  1085. }
  1086. }
  1087. }
  1088. if (noutputs==0) {
  1089. fprintf(f,"fill(ModelManagement.Check.Coverage.CoverElement(\"\",\"\",\"\",\"\"),0)\n");
  1090. } else {
  1091. fprintf(f,"}\n");
  1092. }
  1093. }
  1094. fclose(f);
  1095. }
  1096. #endif
  1097. }
  1098. /* Buildin functions that depend on runtime values */
  1099. /**
  1100. * Internal builtin function used to convert an integer value to an enumeration value (cf. MCP-0022).
  1101. * The second and third arguments are deduced by the compiler from the enumeration type to convert to given in the Modelica model.
  1102. * Example: if 'type E=enumeration(A, B)' then 'E(i)' is compiled to 'Dymola_Integer_to_enumeration(i, 2, "runtime error message")'.
  1103. */
  1104. DYMOLA_STATIC Integer Dymola_Integer_to_enumeration(const Integer toConvert, const Integer maxEnumValue, const char* const errorMessage)
  1105. {
  1106. Assert(toConvert >= 1 && toConvert <= maxEnumValue, errorMessage);
  1107. return toConvert;
  1108. }
  1109. struct spatialDistribution_M_struct {
  1110. double out00_0_0member;
  1111. double out10_0_0member;
  1112. };
  1113. DYMOLA_STATIC int spatialDistribution_M1(int sz,double x, int positiveVelocity,
  1114. RealArray initialPoints, RealArray initialValues)
  1115. {
  1116. int res,i,n,firstTime=1;
  1117. double u0=0,d1,d2;
  1118. Assert(initialPoints.ndims==1,"Initial Points should be a vector");
  1119. Assert(initialValues.ndims==1,"Initial Values should be a vector");
  1120. Assert(initialPoints.dims[0]==initialValues.dims[0],"Initial Points and Values should have size");
  1121. n=initialValues.dims[0];
  1122. if (n>0) {
  1123. for(i=n-1;i>=0;--i) {
  1124. double xVal,uVal;
  1125. xVal=x+initialPoints.data[i];
  1126. uVal=initialValues.data[i];
  1127. if (firstTime) {
  1128. res=transportIni(sz, xVal, uVal);
  1129. } else {
  1130. if (i==0 && initialPoints.data[i]==0 && n>1 && initialPoints.data[i+1]>1e-13)
  1131. transportFunction(uVal, uVal, xVal+1e-13, 1, res, &d1, &d2);
  1132. transportFunction(uVal, uVal, xVal, 1, res, &d1, &d2);
  1133. }
  1134. firstTime=0;
  1135. }
  1136. } else {
  1137. res=transportIni(sz, x, u0);
  1138. }
  1139. return res;
  1140. }
  1141. DYMOLA_STATIC struct spatialDistribution_M_struct spatialDistribution_M2(int id,double in0, double in1, double x, int positiveVelocity)
  1142. {
  1143. struct spatialDistribution_M_struct res;
  1144. transportFunction(in0, in1, -x, positiveVelocity, id, &(res.out00_0_0member), &(res.out10_0_0member));
  1145. return res;
  1146. }
  1147. #define spatialDistribution_M(in0, in1,x,pv,ip,iv,nr) \
  1148. (delayID[nr]= (delayID[nr]==0) ? 1+spatialDistribution_M1(4000,x,pv,ip,iv): delayID[nr], \
  1149. spatialDistribution_M2(delayID[nr]-1,in0,in1,x,pv))
  1150. #if defined(LIBDS_DLL) || defined(LIBDS_NODLL_API) || (defined(_OPENMP) && !defined(DISABLE_DYMOLA_OPENMP)) || defined(DYN_MULTINSTANCE)
  1151. #ifndef MATRIXOP_IMP
  1152. #define MATRIXOP_IMP
  1153. #include "matrixop.c"
  1154. #endif
  1155. #endif
  1156. #endif