delay.c 43 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128
  1. /*
  2. * Copyright (C) 1995-2001 Dynasim AB.
  3. * All rights reserved.
  4. *
  5. */
  6. /* delay.c
  7. first implementation:
  8. Dieter Moormann (DLR), till Nov 14, 1995
  9. variable delays introduced:
  10. Dieter Moormann (DLR), Apr 29, 1996
  11. derivatives introduced:
  12. Dieter Moormann (DLR), May 29, 1996
  13. simplifications of code:
  14. Hilding Elmqvist, December 16, 1996
  15. increased maxid from 100 to 500:
  16. Hilding Elmqvist, April 17, 1997
  17. new functions for Delay & Derivatives added
  18. for Dymola version 3.1:
  19. Dieter Moormann (DLR) April, 4, 1998
  20. */
  21. /* DLL-status: Lot of static data used/defined in file => turned into TLS */
  22. #if !defined(NO_FILE)
  23. #include <stdio.h>
  24. #endif
  25. #include <stdlib.h>
  26. #include <string.h>
  27. #include <math.h>
  28. #include "dymutil.h"
  29. #include "delay.h"
  30. #include "dlldata.h"
  31. #if 0
  32. #define MAX_DELAY 2000 /* maximum number of delays */
  33. #define MAX_DERIVATIVE 2000 /* maximum number of true derivatives */
  34. static delayStruct del[MAX_DELAY];
  35. /* all delays are stored in this struct;
  36. each del contains all information of one delay or
  37. drivative */
  38. static delayStruct deri[MAX_DERIVATIVE];
  39. /* all derivatives are stored in this struct;
  40. each del contains all information of one delay or
  41. drivative */
  42. static int lastDelayID = -1; /* whenever <delayinit> is called,
  43. lastDelayID is increased by 1. */
  44. static int lastDerID = -1; /* whenever <derinit> is called,
  45. lastDerID is increased by 1. */
  46. #endif
  47. /****************************************************************************************
  48. the following definitions are required for the Block library of
  49. Dymola version 3.0 or older.
  50. ****************************************************************************************/
  51. #define maxid 1000
  52. #if 0
  53. static int oldid = 0; /* whenever <delayinit> is called,
  54. oldid is increased by 1. It returns the pointer
  55. of the current id */
  56. static int delays = 0; /* counter for delays */
  57. static int init = 1; /* init = 1: if <delayinit> is called, the
  58. initialization is done
  59. init = 0: if <delayinit> is called, the
  60. initialization is not done
  61. The switch <init> is set to 1 at the first call
  62. of delay and at the end of each simulation
  63. <delayclose>
  64. The switch <init> is set to 0 by the function
  65. <delayiniclos>, which is called at the end of
  66. <dsblockS>.*/
  67. #endif
  68. /****************************************************************************************
  69. the above definitions are required for the Block library of
  70. Dymola version 3.0 or older.
  71. ****************************************************************************************/
  72. DYMOLA_STATIC double delayIni(double delaytime, double buffer, double startTime, double u)
  73. {
  74. int buffersize = (int) (buffer + 0.1);
  75. /* buffersize for storage allocation */
  76. char mess[500];
  77. int delayID;
  78. struct DymosimSimulator*dymosimData;
  79. dymosimData= dymosimGetThreadData();
  80. if (dymosimData->lastDelayID >= MAX_DELAY-1) {
  81. sprintf(mess,"The maximum number of delays (= %d) is reached.\n"
  82. "No more delay memory is available\n", (int) MAX_DELAY);
  83. DymosimError(mess);
  84. }
  85. ++ dymosimData->lastDelayID;
  86. delayID = dymosimData->lastDelayID;
  87. dymosimData->del[delayID].nx = buffersize;
  88. dymosimData->del[delayID].timedelay = delaytime;
  89. dymosimData->del[delayID].x = (double *) malloc( sizeof (double) * buffersize * 2);
  90. if (!dymosimData->del[delayID].x) {
  91. sprintf(mess,"Failed to allocate memory for delay.\n You might decrease the number of delays\n "
  92. " or the 'double Buffersize=%d;' in dymola/source/dsblock5.c\n",buffersize);
  93. DymosimError(mess);
  94. }
  95. dymosimData->del[delayID].y = dymosimData->del[delayID].x + buffersize;
  96. dymosimData->del[delayID].x[0] = startTime - fabs(delaytime) - 1.0e6;
  97. /* the first time value of the buffer x[0] is
  98. the starTime minus the absolute value
  99. of the delaytime. To make sure that
  100. x[0] is smaller than x[1] even for delaytimes
  101. of zero, an additional an additional 1.0 is
  102. substracted from x[0]. */
  103. dymosimData->del[delayID].x[1] = startTime;
  104. dymosimData->del[delayID].y[0] = u; /* <u>,here the inital u */
  105. dymosimData->del[delayID].y[1] = u; /* Extrapolation backwards will give constant u. */
  106. dymosimData->del[delayID].first = 0;
  107. dymosimData->del[delayID].add = 1; /* at this position the last element
  108. was placed */
  109. dymosimData->del[delayID].old = 1; /* set initial interpolation position;
  110. at this value <delayget> will start
  111. finding the upper and lower bounds */
  112. return delayID;
  113. }
  114. DYMOLA_STATIC int transportIni(double buffer, double startX, double u) {
  115. int delayID=(int)(delayIni(1,buffer,startX+1,u));
  116. int i;
  117. int initSize=((int )(buffer+0.1))/10;
  118. struct DymosimSimulator*dymosimData;
  119. dymosimData= dymosimGetThreadData();
  120. #if 0
  121. for(i=0;i<(int)(buffer+0.1);++i) dymosimData->del[delayID].y[i]=dymosimData->del[delayID].x[i]=1e300;
  122. #endif
  123. dymosimData->del[delayID].x[0]=startX;
  124. dymosimData->del[delayID].x[1]=startX+1e-9;
  125. for(i=0;i<initSize;++i)
  126. dymosimData->del[delayID].x[2+i]=startX+(i+0.5)/(initSize);
  127. dymosimData->del[delayID].x[initSize+2]=startX+1-1e-9;
  128. dymosimData->del[delayID].x[initSize+3]=startX+1;
  129. for(i=0;i<initSize+4;++i) dymosimData->del[delayID].y[i]=u;
  130. dymosimData->del[delayID].add=initSize+3;
  131. return delayID;
  132. }
  133. static double getXTransport(struct DymosimSimulator*dymosimData,int id,int num) {
  134. #if 0
  135. if (del[id].x[num]>=1e100) {
  136. char str[100];
  137. sprintf(str,"Get %d => %g",num,del[id].x[num]);
  138. DymosimError(str);
  139. }
  140. #endif
  141. if (!dymosimData) return 1e100; /* Error */
  142. return dymosimData->del[id].x[num];
  143. }
  144. DYMOLA_STATIC void transportGet(double T1,double T2,double x,int vs,int id,double*Tf1,double*Tf2) {
  145. double idd=id;
  146. struct DymosimSimulator*dymosimData;
  147. dymosimData= dymosimGetThreadData();
  148. #if 0
  149. static const double neguno=-0.99999999999999;
  150. static const double uno=0.999999999999999;
  151. double xp1=x+1;
  152. if (!vs) {
  153. *Tf1=delayRead(&idd,&uno,&xp1);
  154. *Tf2=T2;
  155. } else {
  156. *Tf1=T1;
  157. *Tf2=delayRead(&idd,&neguno,&x);
  158. }
  159. #else
  160. if (dymosimData->del[id].x==0) {
  161. *Tf1=T1;
  162. *Tf2=T2;
  163. return;
  164. }
  165. if (!vs) {
  166. int old=dymosimData->del[id].old;
  167. while(getXTransport(dymosimData,id,old)<=x && old!=dymosimData->del[id].add) {
  168. int oldnext=old+1;
  169. if (oldnext>=dymosimData->del[id].nx) oldnext=0;
  170. #if 0
  171. {
  172. char str[200];
  173. sprintf(str,"Up %g %g %g",x,dymosimData->del[id].x[old],dymosimData->del[id].x[oldnext]);
  174. DymosimMessage(str);
  175. }
  176. #endif
  177. old=oldnext;
  178. }
  179. for(;;) {
  180. int oldpre=old-1;
  181. if (oldpre<0) oldpre=dymosimData->del[id].nx-1;
  182. if (oldpre==dymosimData->del[id].first) break;
  183. if (dymosimData->del[id].x[oldpre]<x) break;
  184. #if 0
  185. {
  186. char str[200];
  187. sprintf(str,"Down %g %g %g",x,dymosimData->del[id].x[oldpre],dymosimData->del[id].x[old]);
  188. DymosimMessage(str);
  189. }
  190. #endif
  191. old=oldpre;
  192. }
  193. /* Post: dymosimData->del[id].x[oldpre]<=x<dymosimData->del[id].x[old], or at bounds */
  194. dymosimData->del[id].old=old;
  195. *Tf2=T2;
  196. {
  197. int oldpre=(old==0)?(dymosimData->del[id].nx-1):(old-1);
  198. double x0=getXTransport(dymosimData,id,oldpre);
  199. double x1=getXTransport(dymosimData,id,old);
  200. double y0=dymosimData->del[id].y[oldpre];
  201. double y1=dymosimData->del[id].y[old];
  202. #if 0
  203. if (!(x0<=x && x<x1)) {
  204. char str[200];
  205. sprintf(str,"Ooops2: %d %g %g %g %g %g %d %d %d %d",id,x,x0,x1,y0,y1,oldpre,old,dymosimData->del[id].first,dymosimData->del[id].add);
  206. DymosimMessage(str);
  207. }
  208. #endif
  209. if (x<=x0)
  210. *Tf1=y0;
  211. else if (x>=x1)
  212. *Tf1=y1;
  213. else
  214. *Tf1=y0+(y1-y0)*((x)-x0)/(x1-x0);
  215. }
  216. } else {
  217. int old=dymosimData->del[id].old;
  218. while(getXTransport(dymosimData,id,old)<x+1 && old!=dymosimData->del[id].add) {
  219. int oldnext=old+1;
  220. if (oldnext>=dymosimData->del[id].nx) oldnext=0;
  221. old=oldnext;
  222. }
  223. for(;;) {
  224. int oldpre=old-1;
  225. if (oldpre<0) oldpre=dymosimData->del[id].nx-1;
  226. if (oldpre==dymosimData->del[id].first) break;
  227. if (dymosimData->del[id].x[oldpre]<=x+1) break;
  228. old=oldpre;
  229. }
  230. /* Post: dymosimData->del[id].x[oldpre]<x<=dymosimData->del[id].x[old], or at bounds */
  231. *Tf1=T1;
  232. dymosimData->del[id].old=old;
  233. {
  234. int oldpre=(old==0)?(dymosimData->del[id].nx-1):(old-1);
  235. double x0=getXTransport(dymosimData,id,oldpre);
  236. double x1=getXTransport(dymosimData,id,old);
  237. double y0=dymosimData->del[id].y[oldpre];
  238. double y1=dymosimData->del[id].y[old];
  239. #if 0
  240. if (!(x0<=x+1 && x+1<x1)) {
  241. char str[200];
  242. sprintf(str,"Ooops: %d %.17g %.17g %.17g %g %g",id,x,x0,x1,y0,y1);
  243. DymosimMessage(str);
  244. }
  245. #endif
  246. if (x+1<=x0)
  247. *Tf2=y0;
  248. else if (x+1>=x1)
  249. *Tf2=y1;
  250. else
  251. *Tf2=y0+(y1-y0)*((x+1)-x0)/(x1-x0);
  252. }
  253. }
  254. #endif
  255. }
  256. DYMOLA_STATIC void transportPut(double T1,double T2,double x,int vs,int id) {
  257. struct DymosimSimulator*dymosimData;
  258. dymosimData= dymosimGetThreadData();
  259. if (dymosimData->del[id].x==0) return;
  260. if (!vs) {
  261. /* Add T2 at x+1 as last value */
  262. int add=dymosimData->del[id].add;
  263. int preadd=add-1;
  264. if (preadd<0) preadd=dymosimData->del[id].nx-1;
  265. if (getXTransport(dymosimData,id,add)==x+1)
  266. ; /* No need to add it */
  267. else if (getXTransport(dymosimData,id,add)<=x+1 && fabs(getXTransport(dymosimData,id,preadd)-(x+1))>=0.7/dymosimData->del[id].nx) {
  268. ++add;
  269. if (add>=dymosimData->del[id].nx)
  270. add=0;
  271. if (add==dymosimData->del[id].first) {
  272. int newfirst=dymosimData->del[id].first;
  273. newfirst++;
  274. if (newfirst>=dymosimData->del[id].nx) newfirst=0;
  275. if (dymosimData->del[id].old==dymosimData->del[id].first) dymosimData->del[id].old=newfirst;
  276. dymosimData->del[id].first=newfirst;
  277. }
  278. } else {
  279. while(add!=dymosimData->del[id].first && getXTransport(dymosimData,id,preadd)>x+1) {
  280. #if 0
  281. {
  282. char str[200];
  283. sprintf(str,"Removing value2 %g %g %g",x,dymosimData->del[id].x[add],dymosimData->del[id].x[preadd]);
  284. DymosimMessage(str);
  285. del[id].x[add]=1e300;
  286. }
  287. #endif
  288. if (dymosimData->del[id].old==add)
  289. dymosimData->del[id].old=preadd;
  290. add=preadd;
  291. preadd=add-1;
  292. if (preadd<0) preadd=dymosimData->del[id].nx-1;
  293. }
  294. }
  295. dymosimData->del[id].add=add;
  296. dymosimData->del[id].x[add]=x+1;
  297. dymosimData->del[id].y[add]=T2;
  298. } else {
  299. /* Add T1 at x as first value */
  300. int first=dymosimData->del[id].first;
  301. int postfirst=first+1;
  302. if (postfirst>=dymosimData->del[id].nx) postfirst=0;
  303. if (getXTransport(dymosimData,id,first)==x)
  304. ; /* No need to add it */
  305. else if (getXTransport(dymosimData,id,first)>=x && fabs(getXTransport(dymosimData,id,postfirst)-x)>=0.7/dymosimData->del[id].nx) {
  306. --first;
  307. if (first<0)
  308. first=dymosimData->del[id].nx-1;
  309. if (first==dymosimData->del[id].add) {
  310. int newadd=dymosimData->del[id].add;
  311. newadd--;
  312. if (newadd<0) newadd=dymosimData->del[id].nx-1;
  313. if (dymosimData->del[id].old==dymosimData->del[id].add) dymosimData->del[id].old=newadd;
  314. dymosimData->del[id].add=newadd;
  315. }
  316. } else {
  317. while(first!=dymosimData->del[id].add && getXTransport(dymosimData,id,postfirst)<x) {
  318. #if 0
  319. {
  320. char str[200];
  321. sprintf(str,"Removing value %g %g %g",x,dymosimData->del[id].x[first],dymosimData->del[id].x[postfirst]);
  322. DymosimMessage(str);
  323. del[id].x[first]=1e300;
  324. }
  325. #endif
  326. if (dymosimData->del[id].old==first)
  327. dymosimData->del[id].old=postfirst;
  328. first=postfirst;
  329. postfirst=first+1;
  330. if (postfirst>=dymosimData->del[id].nx) postfirst=0;
  331. }
  332. }
  333. dymosimData->del[id].first=first;
  334. dymosimData->del[id].x[first]=x;
  335. dymosimData->del[id].y[first]=T1;
  336. }
  337. }
  338. DYMOLA_STATIC void transportFunction(double T1,double T2,double x,int vs,int id,double*Tf1,double*Tf2) {
  339. #if 0
  340. DymosimMessageDouble("x:",x);
  341. DymosimMessageInt("vs:",vs);
  342. DymosimMessageInt("x add",dymosimData->del[id].add);
  343. DymosimMessageInt("x first",dymosimData->del[id].first);
  344. DymosimMessageDouble("x 0 in table",dymosimData->del[id].x[0]);
  345. DymosimMessageDouble("x 1 in table",dymosimData->del[id].x[1]);
  346. #endif
  347. transportPut(T1,T2,x,vs,id);
  348. transportGet(T1,T2,x,vs,id,Tf1,Tf2);
  349. #if 0
  350. DymosimMessageInt("x add",dymosimData->del[id].add);
  351. DymosimMessageInt("x first",dymosimData->del[id].first);
  352. DymosimMessageDouble("x add in table",dymosimData->del[id].x[dymosimData->del[id].add]);
  353. DymosimMessageDouble("y add in table",dymosimData->del[id].y[dymosimData->del[id].add]);
  354. DymosimMessageDouble("x first in table",dymosimData->del[id].x[dymosimData->del[id].first]);
  355. DymosimMessageDouble("y first in table",dymosimData->del[id].y[dymosimData->del[id].first]);
  356. DymosimMessageDouble("x 0 in table",dymosimData->del[id].x[0]);
  357. DymosimMessageDouble("y 0 in table",dymosimData->del[id].y[0]);
  358. DymosimMessageDouble("x 1 in table",dymosimData->del[id].x[1]);
  359. DymosimMessageDouble("y 1 in table",dymosimData->del[id].y[1]);
  360. #endif
  361. #if 0
  362. DymosimMessageDouble("Tf1:",*Tf1);
  363. DymosimMessageDouble("Tf2:",*Tf2);
  364. #endif
  365. }
  366. DYMOLA_STATIC double derivativeIni(double startTime, double u)
  367. {
  368. int derID;
  369. int buffersize = 5;
  370. char mess[500];
  371. struct DymosimSimulator*dymosimData;
  372. dymosimData= dymosimGetThreadData();
  373. if (dymosimData->lastDerID >= MAX_DERIVATIVE-1) {
  374. sprintf(mess,"The maximum number of derivatives (= %d) is reached.\n"
  375. "No more delay memory is available\n", (int) MAX_DERIVATIVE);
  376. DymosimError(mess);
  377. }
  378. ++ dymosimData->lastDerID;
  379. derID = dymosimData->lastDerID;
  380. dymosimData->deri[derID].nx = buffersize;
  381. dymosimData->deri[derID].timedelay = 0.0;
  382. dymosimData->deri[derID].x = (double *) malloc( sizeof (double) * buffersize * 2);
  383. if (!dymosimData->deri[derID].x) {
  384. sprintf(mess,"Failed to allocate memory for delay.\n You might decrease the number of delays\n "
  385. " or the 'double Buffersize=%d;' in dymola/source/dsblock5.c\n",buffersize);
  386. DymosimError(mess);
  387. }
  388. dymosimData->deri[derID].y = dymosimData->deri[derID].x + buffersize;
  389. dymosimData->deri[derID].x[0] = startTime - 1.0e6;
  390. /* To make sure that x[0] is smaller than x[1]
  391. a constant value of 1.0 is substracted from x[0]. */
  392. dymosimData->deri[derID].x[1] = startTime;
  393. dymosimData->deri[derID].y[0] = u; /* <u>,here the inital u */
  394. dymosimData->deri[derID].y[1] = u; /* Extrapolation backwards will give constant u. */
  395. dymosimData->deri[derID].first = 0;
  396. dymosimData->deri[derID].add = 1; /* at this position the last element
  397. was placed */
  398. dymosimData->deri[derID].old = 1; /* set initial interpolation position;
  399. at this value <delayget> will start
  400. finding the upper and lower bounds */
  401. return derID;
  402. }
  403. DYMOLA_STATIC void delayDerivativeClose()
  404. {
  405. int delayID = 0;
  406. int derID = 0;
  407. struct DymosimSimulator*dymosimData;
  408. dymosimData= dymosimGetThreadData();
  409. while(delayID <= dymosimData->lastDelayID){ /* free storage for all delays */
  410. free(dymosimData->del[delayID].x);
  411. ++delayID;
  412. }
  413. while(derID <= dymosimData->lastDerID){ /* free storage for all derivatives */
  414. free(dymosimData->deri[derID].x);
  415. ++derID;
  416. }
  417. dymosimData->lastDelayID = -1; /* reset counter for delays */
  418. dymosimData->lastDerID = -1; /* reset counter for derivatives */
  419. /* DymosimMessage("delay + derivative storage freed"); */
  420. }
  421. DYMOLA_STATIC double delayFunction(double idd, double delaytime, double maxDelayTime, double t, double u, const char*exprStr)
  422. {
  423. double dummy, y;
  424. if (delaytime>maxDelayTime*1.00001) {
  425. char str[1000];
  426. sprintf(str,"Delay time %g exceeded maximum delay time %g in \"%.400s\"\n",delaytime,maxDelayTime,exprStr?exprStr:"");
  427. DymosimError(str);
  428. }
  429. dummy = delayWrite(&idd,&t,&u); /* call delayWrite before ... */
  430. y = delayRead(&idd,&delaytime,&t); /* ... delayRead */
  431. return (y);
  432. }
  433. static void delayDWrite(int id,double expr,double Time) {
  434. struct DymosimSimulator*dymosimData;
  435. dymosimData= dymosimGetThreadData();
  436. while (Time <= dymosimData->del[id].x[dymosimData->del[id].add]) {
  437. dymosimData->del[id].add = dymosimData->del[id].add - 1;
  438. if (dymosimData->del[id].add < 0)
  439. dymosimData->del[id].add = dymosimData->del[id].nx - 1;
  440. }
  441. if (dymosimData->del[id].y[dymosimData->del[id].add]==expr)
  442. return;
  443. dymosimData->del[id].add++;
  444. if (dymosimData->del[id].add > dymosimData->del[id].nx-1)
  445. dymosimData->del[id].add = 0;
  446. /* Update first position if we are removing the oldest value. */
  447. if (dymosimData->del[id].add == dymosimData->del[id].first) {
  448. dymosimData->del[id].first++;
  449. if (dymosimData->del[id].first>dymosimData->del[id].nx-1)
  450. dymosimData->del[id].first=0;
  451. }
  452. dymosimData->del[id].x[dymosimData->del[id].add]=Time;
  453. dymosimData->del[id].y[dymosimData->del[id].add]=expr;
  454. }
  455. static int delayDnext(int id,double x,double*timeNext) {
  456. struct DymosimSimulator*dymosimData;
  457. dymosimData= dymosimGetThreadData();
  458. if (x >= dymosimData->del[id].x[dymosimData->del[id].add]) {
  459. /* Extrapolation to future. */
  460. /* sprintf(mess,"Delay No. %d: "
  461. "Extrapolation to future occured at time %g \n", id, *t);
  462. DymosimMessage(mess); */
  463. /* Set second point for interpolation to the last point in buffer. */
  464. dymosimData->del[id].old = dymosimData->del[id].add;
  465. return 0;
  466. }
  467. else if (x < dymosimData->del[id].x[dymosimData->del[id].first]) {
  468. /* Extrapolation to past. */
  469. char mess[500];
  470. double buffersize = dymosimData->del[id].nx;
  471. sprintf(mess,"Extrapolation to past occured in Delay-Block No. %d: \n"
  472. "Buffersize(=%g) too small or too many changes for discrete variable\n"
  473. "for chosen simulation settings\n"
  474. "You might increase the double Buffersize=... in dymola/source/dsblock5.c", id, buffersize);
  475. /* DymosimMessage(mess); */
  476. DymosimError(mess);
  477. }
  478. if(x >= dymosimData->del[id].x[dymosimData->del[id].old]) {
  479. /* Search forward from previous time. */
  480. for(;;) {
  481. int newOld=dymosimData->del[id].old+1;
  482. if (newOld>dymosimData->del[id].nx-1) newOld=0;
  483. *timeNext=dymosimData->del[id].x[newOld];
  484. if (x<*timeNext) {
  485. return 1;
  486. }
  487. dymosimData->del[id].old=newOld;
  488. }
  489. }
  490. else {
  491. /* Search backward from previous time. */
  492. for(;;) {
  493. volatile double d;
  494. int newOld=dymosimData->del[id].old-1;
  495. if (newOld<0) newOld=dymosimData->del[id].nx-1;
  496. d=dymosimData->del[id].x[newOld];
  497. dymosimData->del[id].old=newOld;
  498. if (x>=d) {
  499. *timeNext=dymosimData->del[id].x[dymosimData->del[id].old];
  500. return 1;
  501. }
  502. }
  503. }
  504. }
  505. DYMOLA_STATIC void registerTimeEvent(const double atTime);
  506. DYMOLA_STATIC double delayDDFunction(double expr,double dt,double*idd,
  507. int BufferSize,double Time,int Event,int*timed) {
  508. struct DymosimSimulator*dymosimData;
  509. double timeNext;
  510. int delayId;
  511. dymosimData= dymosimGetThreadData();
  512. if (*idd==0) {
  513. *idd=1+delayIni(dt,BufferSize,Time,expr);
  514. delayId=(int)(*idd+0.1)-1;
  515. dymosimData->del[delayId].add=dymosimData->del[delayId].old=0;
  516. } else
  517. delayId=(int)(*idd+0.1)-1;
  518. if (Event) delayDWrite(delayId,expr,Time);
  519. if (Event && delayDnext(delayId,Time-dt,&timeNext)) {
  520. if (Time<timeNext-4e-14*(fabs(Time)+dymosimData->modelData.basicD->mOrigTimeError)) {
  521. registerTimeEvent(timeNext);
  522. *timed=0;
  523. } else {
  524. *timed=1;
  525. }
  526. } else {
  527. *timed=0;
  528. }
  529. return dymosimData->del[delayId].y[dymosimData->del[delayId].old];
  530. }
  531. DYMOLA_STATIC double delayDCFunction(double expr,double dt,double*idd,int BufferSize,double Time,int Event,
  532. double*rel,double*qp,double*qn,int*qen,double*qz2,int*AnyEvent) {
  533. double timeNext;
  534. int delayId;
  535. struct DymosimSimulator*dymosimData;
  536. dymosimData= dymosimGetThreadData();
  537. if (*idd==0) {
  538. *idd=1+delayIni(dt,BufferSize,Time,expr);
  539. delayId=(int)(*idd+0.1)-1;
  540. dymosimData->del[delayId].add=dymosimData->del[delayId].old=0;
  541. } else
  542. delayId=(int)(*idd+0.1)-1;
  543. if (Event) delayDWrite(delayId,expr,Time);
  544. if (Event) {
  545. int oldD=dymosimData->del[delayId].old;
  546. delayDnext(delayId,Time-dt,&timeNext);
  547. if (oldD!=dymosimData->del[delayId].old)
  548. *AnyEvent=1;
  549. }
  550. *rel=Time-dt-dymosimData->del[delayId].x[dymosimData->del[delayId].old];
  551. qz2[0] = -*rel;
  552. if (dymosimData->del[delayId].old==dymosimData->del[delayId].add) {
  553. qz2[1]=1;
  554. } else {
  555. int oldNext=dymosimData->del[delayId].old+1;
  556. if (oldNext>dymosimData->del[delayId].nx-1) oldNext=0;
  557. qz2[1]=-(Time-dt-dymosimData->del[delayId].x[oldNext])+1e-16;
  558. }
  559. return dymosimData->del[delayId].y[dymosimData->del[delayId].old];
  560. }
  561. DYMOLA_STATIC double delayWrite(double *idd, double *t, double *u)
  562. {
  563. int id = (int) (*idd + 0.1);
  564. struct DymosimSimulator*dymosimData;
  565. dymosimData= dymosimGetThreadData();
  566. /* Remove "future" values if any. */
  567. while (*t <= dymosimData->del[id].x[dymosimData->del[id].add]) {
  568. dymosimData->del[id].add = dymosimData->del[id].add - 1;
  569. if (dymosimData->del[id].add < 0)
  570. dymosimData->del[id].add = dymosimData->del[id].nx - 1;
  571. }
  572. /* Update last position. */
  573. dymosimData->del[id].add++;
  574. if (dymosimData->del[id].add > dymosimData->del[id].nx-1)
  575. dymosimData->del[id].add = 0;
  576. /* Update first position if we are removing the oldest value. */
  577. if (dymosimData->del[id].add == dymosimData->del[id].first) {
  578. #if 1
  579. /* Throw away 1-keepnom/keepdenom of first 0.7 of values */
  580. /* Better than simply stopping */
  581. int id2=(dymosimData->del[id].first+(int)(dymosimData->del[id].nx*0.7)) % dymosimData->del[id].nx;
  582. int iold=id2,inew=id2;
  583. const int keepnom=3,keepdenom=4;
  584. int keepcounter=0;
  585. for(;;) {
  586. dymosimData->del[id].x[inew]=dymosimData->del[id].x[iold];
  587. dymosimData->del[id].y[inew]=dymosimData->del[id].y[iold];
  588. if (iold==dymosimData->del[id].first) break;
  589. iold--;
  590. if (iold<0) iold=dymosimData->del[id].nx-1;
  591. keepcounter++;
  592. if (keepcounter>=keepdenom) keepcounter=0;
  593. if (keepcounter<keepnom) {
  594. inew--;
  595. if (inew<0) inew=dymosimData->del[id].nx-1;
  596. }
  597. }
  598. dymosimData->del[id].first=inew;
  599. #else
  600. dymosimData->del[id].first++;
  601. if (dymosimData->del[id].first > dymosimData->del[id].nx-1)
  602. dymosimData->del[id].first = 0;
  603. #endif
  604. }
  605. /* Store values. */
  606. dymosimData->del[id].x[dymosimData->del[id].add] = *t; /* store new t */
  607. dymosimData->del[id].y[dymosimData->del[id].add] = *u; /* store new u */
  608. return (*u);
  609. }
  610. DYMOLA_STATIC double delayRead(double *idd, double *delaytime, double *t)
  611. {
  612. int id = (int) (*idd + 0.1); /* convert id to integer. */
  613. double x, xup, xlow;
  614. double y, yup, ylow;
  615. int oldold;
  616. char mess[500];
  617. double buffersize;
  618. struct DymosimSimulator*dymosimData;
  619. dymosimData= dymosimGetThreadData();
  620. oldold = dymosimData->del[id].old;
  621. dymosimData->del[id].timedelay = *delaytime;
  622. x = *t - *delaytime; /* calculate interpolation x */
  623. if (x > dymosimData->del[id].x[dymosimData->del[id].add]) {
  624. /* Extrapolation to future. */
  625. /* sprintf(mess,"Delay No. %d: "
  626. "Extrapolation to future occured at time %g \n", id, *t);
  627. DymosimMessage(mess); */
  628. /* Set second point for interpolation to the last point in buffer. */
  629. dymosimData->del[id].old = dymosimData->del[id].add;
  630. }
  631. else if (x < dymosimData->del[id].x[dymosimData->del[id].first]) {
  632. /* Extrapolation to past. */
  633. buffersize = dymosimData->del[id].nx;
  634. sprintf(mess,"Extrapolation to past occured in Delay-Block No. %d: \n"
  635. "Buffersize(=%g) too small or DelayTime(=%g sec) too big\n"
  636. "for chosen simulation settings\n"
  637. "You might increase the double Buffersize=... in dymola/source/dsblock5.c", id, buffersize, *delaytime);
  638. /* DymosimMessage(mess); */
  639. DymosimError(mess);
  640. /* Set second point for interpolation to the first+1 point in buffer. */
  641. dymosimData->del[id].old = dymosimData->del[id].first + 1;
  642. if (dymosimData->del[id].old > dymosimData->del[id].nx-1)
  643. dymosimData->del[id].old = 0;
  644. }
  645. else if (x > dymosimData->del[id].x[dymosimData->del[id].old]) {
  646. /* Search forward from previous time. */
  647. while (x > dymosimData->del[id].x[dymosimData->del[id].old]) {
  648. dymosimData->del[id].old++;
  649. if (dymosimData->del[id].old > dymosimData->del[id].nx-1)
  650. dymosimData->del[id].old = 0;
  651. }
  652. }
  653. else {
  654. /* Search backward from previous time. */
  655. while (x <= dymosimData->del[id].x[dymosimData->del[id].old] && dymosimData->del[id].old!=dymosimData->del[id].first) {
  656. dymosimData->del[id].old--;
  657. if (dymosimData->del[id].old < 0)
  658. dymosimData->del[id].old = dymosimData->del[id].nx-1;
  659. }
  660. /* old should be second point for interpolation. */
  661. dymosimData->del[id].old++;
  662. if (dymosimData->del[id].old > dymosimData->del[id].nx-1)
  663. dymosimData->del[id].old = 0;
  664. }
  665. xup = dymosimData->del[id].x[dymosimData->del[id].old]; /* calculate upper bound for interpolation */
  666. if (dymosimData->del[id].old > 0) /* calculate xlow */
  667. xlow = dymosimData->del[id].x[dymosimData->del[id].old-1];
  668. else
  669. xlow = dymosimData->del[id].x[dymosimData->del[id].nx-1];
  670. yup = dymosimData->del[id].y[dymosimData->del[id].old]; /* calculate final yup */
  671. if (dymosimData->del[id].old > 0) /* calculate final ylow */
  672. ylow = dymosimData->del[id].y[dymosimData->del[id].old-1];
  673. else
  674. ylow = dymosimData->del[id].y[dymosimData->del[id].nx-1];
  675. /* The interpolation/extrapolation formula is given by: */
  676. y = ylow + (x - xlow) * (yup - ylow)/(xup - xlow);
  677. return (y);
  678. }
  679. DYMOLA_STATIC double derivativeFunction(double idd, double t, double u)
  680. {
  681. double dummy, y;
  682. dummy = derivWrite(&idd,&t,&u); /* call derivWrite before ... */
  683. y = derivRead(&idd,&t); /* ... derivRead */
  684. return (y);
  685. }
  686. DYMOLA_STATIC double derivWrite(double *idd, double *t, double *u)
  687. {
  688. double epsilon = 1.E-11;
  689. int id = (int) (*idd + 0.2) ;
  690. struct DymosimSimulator*dymosimData;
  691. dymosimData= dymosimGetThreadData();
  692. if (*t > dymosimData->deri[id].x[dymosimData->deri[id].add]+epsilon){ /* new time > old time */
  693. ++dymosimData->deri[id].add;
  694. if (dymosimData->deri[id].add > dymosimData->deri[id].nx-1) {
  695. dymosimData->deri[id].add = 0;
  696. }
  697. dymosimData->deri[id].x[dymosimData->deri[id].add] = *t; /* write new t */
  698. dymosimData->deri[id].y[dymosimData->deri[id].add] = *u; /* write new u */
  699. }
  700. else if (*t < dymosimData->deri[id].x[dymosimData->deri[id].add]-epsilon){ /* new time < old time */
  701. while (*t <= dymosimData->deri[id].x[dymosimData->deri[id].add]-epsilon){ /* find new position */
  702. if (dymosimData->deri[id].add >= 1)
  703. dymosimData->deri[id].add = dymosimData->deri[id].add - 1;
  704. else
  705. dymosimData->deri[id].add = dymosimData->deri[id].nx-1;
  706. }
  707. if (dymosimData->deri[id].add >= dymosimData->deri[id].nx-1)
  708. dymosimData->deri[id].add = 0;
  709. else
  710. ++dymosimData->deri[id].add;
  711. dymosimData->deri[id].x[dymosimData->deri[id].add] = *t; /* write new t */
  712. dymosimData->deri[id].y[dymosimData->deri[id].add] = *u; /* write new u */
  713. }
  714. else /* new time = old time */
  715. ; /* don't write anything */
  716. return (*u);
  717. }
  718. DYMOLA_STATIC double derivRead(double *idd, double *t)
  719. {
  720. int id = (int) (*idd + 0.2);
  721. double x, xup, xlow;
  722. double y, yup, ylow;
  723. struct DymosimSimulator*dymosimData;
  724. dymosimData= dymosimGetThreadData();
  725. x = *t; /* calculate interpolation x */
  726. xup = dymosimData->deri[id].x[dymosimData->deri[id].old]; /* calculate old upper bound as
  727. first guess for interpolation */
  728. while (x > xup){ /* iterate, until x is bigger then xup */
  729. xlow = xup;
  730. ++dymosimData->deri[id].old; /* increase xup */
  731. if (dymosimData->deri[id].old > dymosimData->deri[id].nx-1) dymosimData->deri[id].old = 0;
  732. xup = dymosimData->deri[id].x[dymosimData->deri[id].old];
  733. if (xlow > xup){ /* extrapolation into future */
  734. dymosimData->deri[id].old = dymosimData->deri[id].old - 1;/* decrease xup */
  735. if (dymosimData->deri[id].old < 0) dymosimData->deri[id].old = dymosimData->deri[id].nx-1 ;
  736. xup = x; /* terminate loop */
  737. }
  738. }
  739. if (dymosimData->deri[id].old >= 1) /* calculate xlow */
  740. xlow = dymosimData->deri[id].x[dymosimData->deri[id].old-1];
  741. else
  742. xlow = dymosimData->deri[id].x[dymosimData->deri[id].nx-1];
  743. yup = dymosimData->deri[id].y[dymosimData->deri[id].old]; /* calculate final yup */
  744. if (dymosimData->deri[id].old >= 1) /* calculate final ylow */
  745. ylow = dymosimData->deri[id].y[dymosimData->deri[id].old-1];
  746. else
  747. ylow = dymosimData->deri[id].y[dymosimData->deri[id].nx-1];
  748. /* calculate derivative */
  749. y = (yup - ylow)/(xup - xlow);
  750. return (y);
  751. }
  752. /****************************************************************************************
  753. the following functions are required for the Block library of
  754. Dymola version 3.0 or older, but not for version 3.1
  755. ****************************************************************************************/
  756. DYMOLA_STATIC double delayinit(double delaytime, double buffer, double u)
  757. {
  758. static const double maxfuture = -100.0; /* negative delay means:
  759. "extrapolation into future".
  760. The negative delay is limited to
  761. 100 sconds. This means that at
  762. current time t the value of t+100
  763. would be extrapolated */
  764. int buffersize = (int) (buffer + 0.1);
  765. /* buffersize for storage allocation */
  766. int id;
  767. /* this part is called only once during
  768. the initialisation (init = 1) */
  769. struct DymosimSimulator*dymosimData;
  770. dymosimData= dymosimGetThreadData();
  771. if (dymosimData->delayinit > 0){
  772. id = dymosimData->oldid;
  773. if (delaytime < maxfuture){
  774. DymosimMessage("Extrapolation limit into future exeeded; DelayTime < -100s");
  775. /* *QiErr_ = 1; */
  776. delaytime = maxfuture; /* just to fix the problem here */
  777. }
  778. ++ dymosimData->oldid;
  779. ++ dymosimData->delays;
  780. if (dymosimData->oldid >= maxid){
  781. DymosimMessage("Maximum number of delays (maxid) exeeded");
  782. /* *QiErr_ = 1; */
  783. dymosimData->oldid = 0; /* just to fix the problem here */
  784. }
  785. dymosimData->del[id].nx = buffersize;
  786. dymosimData->del[id].timedelay = delaytime;
  787. dymosimData->del[id].x = (double *) malloc( sizeof (double) * buffersize * 2);
  788. dymosimData->del[id].y = dymosimData->del[id].x + buffersize;
  789. /* the first time value of the buffer is
  790. the negative value of the delaytime.
  791. The (negative) <maxfuture> is added in
  792. order to cope with negative delays as
  793. well (extrapolation into future) */
  794. dymosimData->del[id].x[0] = -1;
  795. dymosimData->del[id].x[1] = 0; /* Assuming StartTime=0 */
  796. dymosimData->del[id].y[0] = u; /* <u>,here the inital u */
  797. dymosimData->del[id].y[1] = u; /* Extrapolation backwards will give constant u. */
  798. dymosimData->del[id].first = 0;
  799. dymosimData->del[id].add = 1; /* at this position the last element
  800. was placed */
  801. dymosimData->del[id].old = 1; /* set initial interpolation position;
  802. at this value <delayget> will start
  803. finding the upper and lower bounds */
  804. }
  805. else {
  806. id = dymosimData->oldid;
  807. dymosimData->del[id].timedelay = delaytime;
  808. dymosimData->oldid++;
  809. if (dymosimData->oldid >= dymosimData->delays)
  810. dymosimData->oldid = 0;
  811. }
  812. return ( (double) id);
  813. }
  814. DYMOLA_STATIC void delayiniclos()
  815. {
  816. struct DymosimSimulator*dymosimData;
  817. dymosimData= dymosimGetThreadData();
  818. dymosimData->delayinit = 0; /* after initialisation */
  819. dymosimData->oldid = 0;
  820. }
  821. DYMOLA_STATIC void delayclose()
  822. {
  823. int id = 0; /* free storage for all delays */
  824. struct DymosimSimulator*dymosimData;
  825. dymosimData= dymosimGetThreadData();
  826. while(id < dymosimData->delays){
  827. free(dymosimData->del[id].x);
  828. ++id;
  829. }
  830. /* set initial conditions for a
  831. new simulation */
  832. dymosimData->oldid = 0;
  833. dymosimData->delays = 0;
  834. dymosimData->delayinit = 1;
  835. }
  836. DYMOLA_STATIC double delayfunc(double delaytime, double t, double u, double buffersize)
  837. {
  838. double idd;
  839. double dummy, y;
  840. idd = delayinit(delaytime,buffersize,u);
  841. dummy = delayput(&idd,&t,&u); /* call delayput before ... */
  842. y = delayget(&idd,&t); /* ... delayget */
  843. return (y);
  844. }
  845. DYMOLA_STATIC double delayput(double *idd, double *t, double *u)
  846. {
  847. int id = (int) (*idd + 0.1);
  848. struct DymosimSimulator*dymosimData;
  849. dymosimData= dymosimGetThreadData();
  850. /* Remove "future" values if any. */
  851. while (*t <= dymosimData->del[id].x[dymosimData->del[id].add]) {
  852. /*
  853. if (*t < dymosimData->del[id].x[dymosimData->del[id].add])
  854. printf("Removing: new time < old time %g < %g \n", *t, dymosimData->del[id].x[dymosimData->del[id].add]);
  855. */
  856. dymosimData->del[id].add = dymosimData->del[id].add - 1;
  857. if (dymosimData->del[id].add < 0)
  858. dymosimData->del[id].add = dymosimData->del[id].nx - 1;
  859. }
  860. /* Update last position. */
  861. dymosimData->del[id].add++;
  862. if (dymosimData->del[id].add > dymosimData->del[id].nx-1)
  863. dymosimData->del[id].add = 0;
  864. /* Update first position if we are removing the oldest value. */
  865. if (dymosimData->del[id].add == dymosimData->del[id].first) {
  866. dymosimData->del[id].first++;
  867. if (dymosimData->del[id].first > dymosimData->del[id].nx-1)
  868. dymosimData->del[id].first = 0;
  869. }
  870. /* Store values. */
  871. dymosimData->del[id].x[dymosimData->del[id].add] = *t; /* store new t */
  872. dymosimData->del[id].y[dymosimData->del[id].add] = *u; /* store new u */
  873. return (*u);
  874. }
  875. DYMOLA_STATIC double delayget(double *idd, double *t)
  876. {
  877. int id = (int) (*idd + 0.1); /* convert id to integer. */
  878. double x, xup, xlow;
  879. double y, yup, ylow;
  880. int oldold;
  881. struct DymosimSimulator*dymosimData;
  882. dymosimData= dymosimGetThreadData();
  883. oldold = dymosimData->del[id].old;
  884. x = *t - dymosimData->del[id].timedelay; /* calculate interpolation x */
  885. if (x > dymosimData->del[id].x[dymosimData->del[id].add]) {
  886. /* Extrapolation to future. */
  887. printf("Warning: Extrapolation to future %g at time %g \n", x, *t);
  888. /* Set second point for interpolation to the last point in buffer. */
  889. dymosimData->del[id].old = dymosimData->del[id].add;
  890. } else if (x < dymosimData->del[id].x[dymosimData->del[id].first]) {
  891. /* Extrapolation to past. */
  892. if (x > 0)
  893. printf("Warning: Extrapolation to past %g at %g \n", x, *t);
  894. /* Set second point for interpolation to the first+1 point in buffer. */
  895. dymosimData->del[id].old = dymosimData->del[id].first + 1;
  896. if (dymosimData->del[id].old > dymosimData->del[id].nx-1)
  897. dymosimData->del[id].old = 0;
  898. } else if (x > dymosimData->del[id].x[dymosimData->del[id].old]) {
  899. /* Search forward from previous time. */
  900. while (x > dymosimData->del[id].x[dymosimData->del[id].old]) {
  901. dymosimData->del[id].old++;
  902. if (dymosimData->del[id].old > dymosimData->del[id].nx-1)
  903. dymosimData->del[id].old = 0;
  904. }
  905. } else {
  906. /* Search backward from previous time. */
  907. while (x <= dymosimData->del[id].x[dymosimData->del[id].old]) {
  908. dymosimData->del[id].old--;
  909. if (dymosimData->del[id].old < 0)
  910. dymosimData->del[id].old = dymosimData->del[id].nx-1;
  911. }
  912. /* old should be second point for interpolation. */
  913. dymosimData->del[id].old++;
  914. if (dymosimData->del[id].old > dymosimData->del[id].nx-1)
  915. dymosimData->del[id].old = 0;
  916. }
  917. /*
  918. if (fabs(dymosimData->del[id].old - oldold) > 10)
  919. printf("Much searching %d %d \n", dymosimData->del[id].old, oldold);
  920. */
  921. xup = dymosimData->del[id].x[dymosimData->del[id].old]; /* calculate upper bound for interpolation */
  922. if (dymosimData->del[id].old > 0) /* calculate xlow */
  923. xlow = dymosimData->del[id].x[dymosimData->del[id].old-1];
  924. else
  925. xlow = dymosimData->del[id].x[dymosimData->del[id].nx-1];
  926. yup = dymosimData->del[id].y[dymosimData->del[id].old]; /* calculate final yup */
  927. if (dymosimData->del[id].old > 0) /* calculate final ylow */
  928. ylow = dymosimData->del[id].y[dymosimData->del[id].old-1];
  929. else
  930. ylow = dymosimData->del[id].y[dymosimData->del[id].nx-1];
  931. /* The interpolation/extrapolation formula is given by: */
  932. y = ylow + (x - xlow) * (yup - ylow)/(xup - xlow);
  933. /*
  934. if (id == 5 && (dymosimData->del[id].old != 1 || x > 0) )
  935. printf("Delay time %g at time %g index %d value %g\n", x, *t, dymosimData->del[id].old, y);
  936. */
  937. if (yup > ylow) {
  938. if (y > yup )
  939. y = yup;
  940. else if (y < ylow)
  941. y = ylow;
  942. } else {
  943. if (y < yup )
  944. y = yup;
  945. else if (y > ylow)
  946. y = ylow;
  947. }
  948. return (y);
  949. }
  950. DYMOLA_STATIC double derivfunc(double t, double u)
  951. {
  952. double dummy, y;
  953. double idd;
  954. idd = delayinit(0.0,5.0,u);
  955. dummy = derivput(&idd,&t,&u); /* call derivput before ... */
  956. y = derivget(&idd,&t); /* ... derivget */
  957. return (y);
  958. }
  959. DYMOLA_STATIC double derivput(double *idd, double *t, double *u)
  960. {
  961. double epsilon = 1.E-11;
  962. int id = (int) (*idd + 0.2) ;
  963. struct DymosimSimulator*dymosimData;
  964. dymosimData= dymosimGetThreadData();
  965. if (*t > dymosimData->del[id].x[dymosimData->del[id].add]+epsilon){ /* new time > old time */
  966. ++dymosimData->del[id].add;
  967. if (dymosimData->del[id].add > dymosimData->del[id].nx-1) {
  968. dymosimData->del[id].add = 0;
  969. }
  970. dymosimData->del[id].x[dymosimData->del[id].add] = *t; /* write new t */
  971. dymosimData->del[id].y[dymosimData->del[id].add] = *u; /* write new u */
  972. }
  973. else if (*t < dymosimData->del[id].x[dymosimData->del[id].add]-epsilon){ /* new time < old time */
  974. while (*t <= dymosimData->del[id].x[dymosimData->del[id].add]-epsilon){ /* find new position */
  975. if (dymosimData->del[id].add >= 1)
  976. dymosimData->del[id].add = dymosimData->del[id].add - 1;
  977. else
  978. dymosimData->del[id].add = dymosimData->del[id].nx-1;
  979. }
  980. if (dymosimData->del[id].add >= dymosimData->del[id].nx-1)
  981. dymosimData->del[id].add = 0;
  982. else
  983. ++dymosimData->del[id].add;
  984. dymosimData->del[id].x[dymosimData->del[id].add] = *t; /* write new t */
  985. dymosimData->del[id].y[dymosimData->del[id].add] = *u; /* write new u */
  986. }
  987. else /* new time = old time */
  988. ; /* don't write anything */
  989. return (*u);
  990. }
  991. DYMOLA_STATIC double derivget(double *idd, double *t)
  992. {
  993. int id = (int) (*idd + 0.2);
  994. double x, xup, xlow;
  995. double y, yup, ylow;
  996. struct DymosimSimulator*dymosimData;
  997. dymosimData= dymosimGetThreadData();
  998. x = *t; /* calculate interpolation x */
  999. xup = dymosimData->del[id].x[dymosimData->del[id].old]; /* calculate old upper bound as
  1000. first guess for interpolation */
  1001. while (x > xup){ /* iterate, until x is bigger then xup */
  1002. xlow = xup;
  1003. ++dymosimData->del[id].old; /* increase xup */
  1004. if (dymosimData->del[id].old > dymosimData->del[id].nx-1) dymosimData->del[id].old = 0;
  1005. xup = dymosimData->del[id].x[dymosimData->del[id].old];
  1006. if (xlow > xup){ /* extrapolation into future */
  1007. dymosimData->del[id].old = dymosimData->del[id].old - 1; /* decrease xup */
  1008. if (dymosimData->del[id].old < 0) dymosimData->del[id].old = dymosimData->del[id].nx-1 ;
  1009. xup = x; /* terminate loop */
  1010. }
  1011. }
  1012. if (dymosimData->del[id].old >= 1) /* calculate xlow */
  1013. xlow = dymosimData->del[id].x[dymosimData->del[id].old-1];
  1014. else
  1015. xlow = dymosimData->del[id].x[dymosimData->del[id].nx-1];
  1016. yup = dymosimData->del[id].y[dymosimData->del[id].old]; /* calculate final yup */
  1017. if (dymosimData->del[id].old >= 1) /* calculate final ylow */
  1018. ylow = dymosimData->del[id].y[dymosimData->del[id].old-1];
  1019. else
  1020. ylow = dymosimData->del[id].y[dymosimData->del[id].nx-1];
  1021. /* calculate derivative */
  1022. y = (yup - ylow)/(xup - xlow);
  1023. return (y);
  1024. }