delay.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298
  1. #ifndef delay_h
  2. #define delay_h
  3. /*
  4. * Copyright (C) 1995-2006 Dynasim AB.
  5. * All rights reserved.
  6. *
  7. */
  8. /* first implementation:
  9. Dieter Moormann (DLR), till Nov 14, 1995 */
  10. /* modfied for use with derivatives also:
  11. Dieter Moormann (DLR), till May 24, 1996 */
  12. /* improvements made for Dymola Version 3.1:
  13. Dieter Moormann (DLR), April 2, 1998 */
  14. /* improvements made for Dymola Version 3.1b:
  15. Dag Bruck, Dynasim AB, October 5, 1998 */
  16. /* These functions are to be used together with the Dymola model
  17. classes <FixDelay>, <VarDelay>, and <Derivative>.
  18. The time <Time> and the input <u> are written into a circular buffer.
  19. Delay:
  20. At start time the <Time> is set to the negative value of the
  21. delaytime, which is specified by the parameter <DelayTime>, <u> is
  22. given its initial value.
  23. After the start of the integration, only pairs of <Time> and <u>
  24. are stored in the buffer, when the time has increased from the
  25. last value. If the integrator has move backwards, the old (now
  26. invalid) values will be overwritten by the new ones. This guarantees,
  27. that the time is allways increasing ( = that the history of the
  28. buffer is always correct).
  29. If the end of the buffer is reached, the new pair of <Time> and <u>
  30. is stored at the first postion of the buffer (circular buffer).
  31. The output <y> is interpolated from the buffer at the <Time> reduced
  32. by the <DelayTime> by linear interpolation.
  33. DelayedTime = Time - DelayTime
  34. At that time <DelayedTime>, the interpolation is performed:
  35. ( u(t_up) - ul(t_low) )
  36. y = u(t_low) + (DelayedTime - t_low) * ------------------------
  37. ( t_up - t_low )
  38. with: t_low < DelayedTime <= t_up
  39. If the <DelayedTime> is smaller than the lowest time in the buffer,
  40. the value is extrapolated from the two lowest values of the buffer.
  41. If the <DelayedTime> is bigger than the highest time in the buffer,
  42. the value is extrapolated from the two highest values of the buffer.
  43. This can happen, when the delaytime is negative ("extrapolation into
  44. future")
  45. The buffersize is fixed to the value specified by the parameter
  46. <Buffersize> in the Dymola model class <Delay>.
  47. In the current version the Delay Block is realized as SISO system.
  48. Derivative:
  49. ( u(t_up) - ul(t_low) )
  50. y = ------------------------
  51. ( t_up - t_low )
  52. In the current version the Derivative block is realized as SISO system.
  53. */
  54. #include "libdssetup.h"
  55. #ifndef DELAYSTRUCT_H
  56. #define DELAYSTRUCT_H
  57. typedef struct { /* struct for storing the delay/derivative information */
  58. double *x; /* column of time values */
  59. double *y; /* column of values, which belong to time values of
  60. same row */
  61. double timedelay; /* value of timedelay (used for delays only) */
  62. int nx; /* buffersize of the system = number of columns */
  63. int first; /* column of first value in the buffer */
  64. int old; /* column of last interpolation step */
  65. int add; /* column of last value added to the buffer */
  66. } delayStruct;
  67. #endif
  68. DYMOLA_STATIC LIBDS_API_AFTER double delayIni(double delaytime, double buffer, double startTime, double u);
  69. /* Function is called from the Dymola generated code for each <Delay>
  70. during initialisation. It allocates the storage for each delay of the model
  71. and sets its inital conditions and returns the identifyer.
  72. inputs: <delaytime> (Delaytime) ;double
  73. <buffer> (Buffersize) ;double
  74. <startTime> (StartTime) ;double
  75. <u> (Input) ;double
  76. outputs: RETURN (Identifyer) ;double
  77. */
  78. DYMOLA_STATIC LIBDS_API_AFTER double delayFunction(double idd, double delaytime, double maxDelayTime, double t, double u,const char*str);
  79. /* Function is called from the Dymola generated code for each <Delay>.
  80. It puts the current value of Time <t> and Input <u> in the buffer
  81. of the Delay with the identifyer <idd>. It returns the interpolated
  82. value of the delayed time.
  83. inputs: <idd> (Identity) ;double
  84. <delaytime> (Delaytime) ;double
  85. <t> (Time) ;double
  86. <u> (Input) ;double
  87. outputs: RETURN (Derivative) ;double
  88. This function calls first: <delayWrite>
  89. and then: <delayRead>
  90. */
  91. DYMOLA_STATIC LIBDS_API_AFTER double delayWrite (double *idd, double *t, double *u);
  92. /* Function is called from <delayfunc>.
  93. It puts the current value of Time <t> and Input <u> in the buffer
  94. of the Delay with the identifyer <idd>. The function returns the
  95. unchanged input <u>.
  96. inputs: <idd> (Identifyer) ;double
  97. <t> (Time) ;double
  98. <u> (Input) ;double
  99. outputs: RETURN (Input) ;double
  100. */
  101. DYMOLA_STATIC LIBDS_API_AFTER double delayRead(double *idd, double *delaytime, double *t);
  102. /* Function is called from <delayfunc>.
  103. It returns the interpolated value of the Delay with the
  104. identifyer <idd> of the delayed time.
  105. inputs: <idd> (Identifyer) ;double
  106. <t> (Time) ;double
  107. outputs: RETURN (Interpolated Output) ;double
  108. */
  109. DYMOLA_STATIC LIBDS_API_AFTER double derivativeIni(double startTime, double u);
  110. /* Function is called from the Dymola generated code for each <Derivative>
  111. during initialisation. It allocates the storage for each derivative of the model
  112. and sets its inital conditions and returns the identifyer.
  113. inputs: <startTime> (StartTime) ;double
  114. <u> (Input) ;double
  115. outputs: RETURN (Identifyer) ;double
  116. */
  117. DYMOLA_STATIC LIBDS_API_AFTER double derivativeFunction(double idd, double t, double u);
  118. /* Function is called from the Dymola generated code for each <Derivative>.
  119. It puts the current value of Time <t> and Input <u> in the buffer
  120. of the Derivative with the identifyer <idd>. It returns the derivative.
  121. inputs: <idd> (Identifyer) ;double
  122. <t> (Time) ;double
  123. <u> (Input) ;double
  124. outputs: RETURN (Derivative) ;double
  125. This function calls first: <derivWrite>
  126. and then: <derivPut>
  127. */
  128. DYMOLA_STATIC LIBDS_API_AFTER double derivWrite(double *idd, double *t, double *u);
  129. /* Function is called from <derivativeFunction>.
  130. It puts the current value of Time <t> and Input <u> in the buffer
  131. of the Derivative with the identifyer <idd>. The function returns the
  132. unchanged input <u>.
  133. inputs: <idd> (Identifyer) ;double
  134. <t> (Time) ;double
  135. <u> (Input) ;double
  136. outputs: RETURN (Input) ;double
  137. */
  138. DYMOLA_STATIC LIBDS_API_AFTER double derivRead(double *idd, double *t);
  139. /* Function is called from <derivativeFunction>.
  140. It returns the derivative value of the Derivative with the
  141. identifyer <idd>.
  142. inputs: <idd> (Identifyer) ;double
  143. <t> (Time) ;double
  144. outputs: RETURN (Derivative) ;double
  145. */
  146. DYMOLA_STATIC LIBDS_API_AFTER void delayDerivativeClose(void);
  147. /* Function is called at the end of the simulation to free allocated
  148. storage of delays and derivatives and to reset the counters.
  149. no inputs, no outputs */
  150. /****************************************************************************************
  151. the following definitions are required for the Block library of
  152. Dymola version 3.0 or older, but not for Dymola 3.1
  153. ****************************************************************************************/
  154. DYMOLA_STATIC LIBDS_API_AFTER double delayinit(double delaytime, double buffer, double u);
  155. /* Function is called from the Dymola generated code for each <Delay>.
  156. Called from <dsblockS> (during initialisation) it allocates the
  157. storage for each delay of the model and sets its inital conditions
  158. and returns the identifyer.
  159. Called during simulation it just returns the identifyer.
  160. inputs: <delaytime> (Delaytime) ;double
  161. <buffer> (Buffersize) ;double
  162. <u> (Input) ;double
  163. outputs: RETURN (Identifyer) ;double
  164. */
  165. DYMOLA_STATIC LIBDS_API_AFTER void delayiniclos(void);
  166. /* Function is called to end the initialization procedure.
  167. no inputs, no outputs */
  168. DYMOLA_STATIC LIBDS_API_AFTER void delayclose(void);
  169. /* Function is called at the end of the simulation to free allocated
  170. storage.
  171. no inputs, no outputs */
  172. DYMOLA_STATIC LIBDS_API_AFTER double delayfunc(double delaytime, double t, double u, double buffersize);
  173. /* Function is called from the Dymola generated code for each <Delay>.
  174. It puts the current value of Time <t> and Input <u> in the buffer
  175. of the Delay. It returns the interpolated value of the delayed time.
  176. inputs: <delaytime> (Delytime) ;double
  177. <t> (Time) ;double
  178. <u> (Input) ;double
  179. <buffersize> (Buffersize) ;double
  180. outputs: RETURN (Interpolated Output) ;double
  181. This function calls first: <delayput>
  182. and then: <delayget>
  183. */
  184. DYMOLA_STATIC LIBDS_API_AFTER double delayput(double *idd, double *t, double *u);
  185. /* Function is called from <delayfunc>.
  186. It puts the current value of Time <t> and Input <u> in the buffer
  187. of the Delay with the identifyer <idd>. The function returns the
  188. unchanged input <u>.
  189. inputs: <idd> (Identifyer) ;double
  190. <t> (Time) ;double
  191. <u> (Input) ;double
  192. outputs: RETURN (Input) ;double
  193. */
  194. DYMOLA_STATIC LIBDS_API_AFTER double delayget(double *idd, double *t);
  195. /* Function is called from <delayfunc>.
  196. It returns the interpolated value of the Delay with the
  197. identifyer <idd> of the delayed time.
  198. inputs: <idd> (Identifyer) ;double
  199. <t> (Time) ;double
  200. outputs: RETURN (Interpolated Output) ;double
  201. */
  202. DYMOLA_STATIC LIBDS_API_AFTER double derivfunc(double t, double u);
  203. /* Function is called from the Dymola generated code for each <Delay>.
  204. It puts the current value of Time <t> and Input <u> in the buffer
  205. of the Delay with the identifyer <idd>. It returns the interpolated
  206. value of the delayed time.
  207. inputs: <t> (Time) ;double
  208. <u> (Input) ;double
  209. outputs: RETURN (Derivative) ;double
  210. This function calls first: <delayput>
  211. and then: <derivget>
  212. */
  213. DYMOLA_STATIC LIBDS_API_AFTER double derivput(double *idd, double *t, double *u);
  214. /* Function is called from <delayfunc>.
  215. It puts the current value of Time <t> and Input <u> in the buffer
  216. of the Derivative with the identifyer <idd>. The function returns the
  217. unchanged input <u>.
  218. inputs: <idd> (Identifyer) ;double
  219. <t> (Time) ;double
  220. <u> (Input) ;double
  221. outputs: RETURN (Input) ;double
  222. */
  223. DYMOLA_STATIC LIBDS_API_AFTER double derivget(double *idd, double *t);
  224. /* Function is called from <delayfunc>.
  225. It returns the derivative value of the Derivative with the
  226. identifyer <idd>.
  227. inputs: <idd> (Identifyer) ;double
  228. <t> (Time) ;double
  229. outputs: RETURN (Derivative) ;double
  230. */
  231. DYMOLA_STATIC LIBDS_API_AFTER double delayDDFunction(double expr,double dt,double*idd,int BufferSize,double Time,int Event,int*timed);
  232. DYMOLA_STATIC LIBDS_API_AFTER double delayDCFunction(double expr,double dt,double*idd,int BufferSize,double Time,int Event,
  233. double*rel,double*qp,double*qn,int*qen,double*qz2,int*AnyEvent);
  234. DYMOLA_STATIC LIBDS_API_AFTER int transportIni(double buffer, double startX, double u);
  235. DYMOLA_STATIC LIBDS_API_AFTER void transportGet(double T1,double T2,double x,int vs,int id,double*Tf1,double*Tf2);
  236. DYMOLA_STATIC LIBDS_API_AFTER void transportPut(double T1,double T2,double x,int vs,int id);
  237. DYMOLA_STATIC LIBDS_API_AFTER void transportFunction(double T1,double T2,double x,int vs,int id,double*Tf1,double*Tf2);
  238. #endif