infiltration.c 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  1. #include <math.h>
  2. #include <grass/gis.h>
  3. #include <grass/glocale.h>
  4. #include "global.h"
  5. #define TOLERANCE 0.00001
  6. #define MAX_ITER 20
  7. #define NUM_TERMS 10
  8. #define PONDING_NO 0
  9. #define PONDING_STARTED 1
  10. #define PONDING_YES 2
  11. /* Calculates the infiltrate rate (m/hr) for the given time step. For variable
  12. * names and equation numbers in comments, refer to Beven (1984).
  13. *
  14. * Beven, K. J., 1984. Infiltration into a class of vertically non-uniform
  15. * soils. Hydrological Sciences Journal 29 (4), 425-434.
  16. *
  17. * Beven, K. J., Kirkby, M. J., 1979. A physically based, variable contributing
  18. * area model of basin hydrology. Hydrological Sciences Bulletin 24 (1), 43-69.
  19. *
  20. * Morel-Seytoux, H. J., Khanji, J., 1974. Derivation of an equation of
  21. * infiltration. Water Resources Research 10 (4), 795-800.
  22. */
  23. double calculate_infiltration(int timestep, double R)
  24. {
  25. /* params.K0 Surface hydraulic conductivity (m/h)
  26. * params.psi Wetting front suction (m)
  27. * params.dtheta Water content change across the wetting front
  28. * dtheta = saturated moisture content
  29. * - initial moisture content
  30. * params.m Parameter controlling the decline rate of
  31. * transmissivity (m)
  32. *
  33. * Beven and Kirkby (1979) introduced the scaling
  34. * parameter m.
  35. *
  36. * K(z) = K0 * exp(-f * z)
  37. *
  38. * where K(z) is hydraulic conductivity at depth z,
  39. * z is the soil depth, and
  40. * f is the parameter controlling the decline rate
  41. * of transmissivity (1/m); can be defined by m as
  42. * f = dtheta / m
  43. *
  44. * Now, m = dtheta / f.
  45. *
  46. * R Rainfall intensity (m/h)
  47. * r Infiltration rate (m/h)
  48. * cumI Cumulative infiltration at the start of time step (m)
  49. * I Cumulative infiltration at the end of time step (m)
  50. * dIdt Infiltration rate for the current time step (m/hr)
  51. * C Storage-suction factor (m) (Morel-Seytoux and Khanji,
  52. * 1974); C = psi * dtheta
  53. * IC I + C
  54. * lambda lambda in Eq. (8); Note that this lambda is different
  55. * from params.lambda
  56. * t Current time (hr)
  57. * tp Time to ponding (hr)
  58. * ponding Ponding indicator
  59. * PONDING_NO: no ponding
  60. * PONDING_STARTED: ponding started in this time step
  61. * PONDING_YES: ponding has started in a previous time step
  62. */
  63. static double cumI = 0.0, I = 0.0, lambda = 0.0, tp = 0.0;
  64. static int ponding = PONDING_NO;
  65. double r, C, IC, dIdt, t;
  66. double f1, f2, df, sum;
  67. int fact;
  68. int i, j;
  69. /* reset if there is no rainfall */
  70. if (R <= 0.0) {
  71. cumI = I = lambda = tp = 0.0;
  72. ponding = PONDING_NO;
  73. return 0.0;
  74. }
  75. t = timestep * input.dt;
  76. C = params.psi * params.dtheta;
  77. f1 = 0.0;
  78. /* if ponding hasn't started and cumulative infiltration is greater than 0
  79. */
  80. if (ponding == PONDING_NO && cumI > 0) {
  81. f1 = cumI;
  82. /* infiltration rate: Eq. (6)
  83. * Note that his Ks=K0*exp(f*z) in Eq. (1a) instead of Ks=K0*exp(-f*z)
  84. * used in his TOPMODEL code, TMOD9502.F. Substitute f=-dtheta/m for f
  85. * in Eq. (1a), hence -K0 and exp(f1/m), slightly different from the
  86. * original Eq. (6).
  87. */
  88. r = -params.K0 / params.m * (C + f1) / (1 - exp(f1 / params.m));
  89. /* if infiltration rate is less than rainfall intensity, ponding starts
  90. */
  91. if (r < R) {
  92. I = cumI;
  93. /* ponding time: tp will remain the same until next ponding occurs
  94. */
  95. tp = t - input.dt;
  96. ponding = PONDING_STARTED;
  97. }
  98. }
  99. /* if ponding hasn't started yet */
  100. if (ponding == PONDING_NO) {
  101. /* try full infiltration */
  102. f2 = cumI + R * input.dt;
  103. /* infiltration rate */
  104. r = -params.K0 / params.m * (C + f2) / (1 - exp(f2 / params.m));
  105. /* if potential cumulative infiltration is 0 or infiltration rate is
  106. * greater than rainfall intensity, all rainfall will be infiltrated
  107. */
  108. if (f2 == 0.0 || r > R) {
  109. /* full infiltration */
  110. dIdt = R;
  111. cumI += dIdt * input.dt;
  112. return dIdt;
  113. }
  114. /* infiltration rate is less than rainfall intensity */
  115. /* Newton-Raphson iteration to solve Eq. (6) for I */
  116. /* guess new cumulative infiltration */
  117. I = cumI + r * input.dt;
  118. for (i = 0; i < MAX_ITER; i++) {
  119. /* new infiltration rate */
  120. r = -params.K0 / params.m * (C + I) / (1 - exp(I / params.m));
  121. /* if new infiltration rate is greater than rainfall intensity,
  122. * increase cumulative infiltration
  123. */
  124. if (r > R) {
  125. f1 = I;
  126. I = (I + f2) / 2.0;
  127. df = I - f1;
  128. }
  129. /* otherwise, decrease cumulative infiltration */
  130. else {
  131. f2 = I;
  132. I = (I + f1) / 2.0;
  133. df = I - f2;
  134. }
  135. /* stop if cumulative infiltration converged */
  136. if (fabs(df) <= TOLERANCE)
  137. break;
  138. }
  139. if (i == MAX_ITER)
  140. G_warning(
  141. _("Maximum number of iterations exceeded at time step %d"),
  142. timestep);
  143. /* ponding time: tp will remain the same until next ponding occurs */
  144. tp = t - input.dt + (I - cumI) / R;
  145. /* if ponding time is greater than the current time,
  146. * tp = t - dt + (I - cumI) / R > t
  147. * (I - cumI) / R > dt
  148. * I - cumI > R * dt
  149. * means that additional infiltration (I - cumI) is greater than the
  150. * total rainfall (R * dt), which cannot happen when there is no
  151. * ponding, so infiltrate all rainfall
  152. */
  153. if (tp > t) {
  154. /* full infiltration */
  155. dIdt = R;
  156. cumI += dIdt * input.dt;
  157. return dIdt;
  158. }
  159. /* ponding starts if additional infiltration is less than the total
  160. * rainfall because not all rainfall can be infiltrated in this time
  161. * step
  162. */
  163. ponding = PONDING_STARTED;
  164. }
  165. /* if ponding just started */
  166. if (ponding == PONDING_STARTED) {
  167. /* lambda will remain the same until next ponding occurs */
  168. lambda = 0.0;
  169. fact = 1;
  170. IC = I + C;
  171. for (j = 1; j <= NUM_TERMS; j++) {
  172. fact *= j;
  173. lambda += pow(IC / params.m, (double)j) / (double)(j * fact);
  174. }
  175. /* lambda in Eq. (8) */
  176. lambda = log(IC) - (log(IC) + lambda) / exp(C / params.m);
  177. I += R * (t - tp) / 2.0;
  178. }
  179. /* Newton-Raphson iteration to solve Eq. (8) for I */
  180. for (i = 0; i < MAX_ITER; i++) {
  181. IC = I + C;
  182. sum = 0.0;
  183. fact = 1;
  184. for (j = 1; j <= NUM_TERMS; j++) {
  185. fact *= j;
  186. sum += pow(IC / params.m, (double)j) / (double)(j * fact);
  187. }
  188. /* Eq. (8) - (t - tp) in hr: should converge to 0
  189. * Note that sum is outside 1/exp(C/m) in Eq. (8), but inside in his
  190. * TMOD9502.F. Based on lambda and his code, it looks like a typo in
  191. * Eq. (8).
  192. */
  193. f1 = -(log(IC) - (log(IC) + sum) / exp(C / params.m) - lambda) /
  194. (params.K0 / params.m) - (t - tp);
  195. /* inverse of Eq. (7) in hr/m */
  196. f2 = (exp(I / params.m) - 1.0) / (IC * params.K0 / params.m);
  197. /* -(Eq. (8) - (t-tp)) * Eq. (7): cumulative infiltration in a short
  198. * time period
  199. */
  200. df = -f1 / f2;
  201. I += df;
  202. if (fabs(df) <= TOLERANCE)
  203. break;
  204. }
  205. if (i == MAX_ITER)
  206. G_warning(_("Maximum number of iterations exceeded at time step %d"),
  207. timestep);
  208. /* if new cumulative infiltration is less than the previous cumulative
  209. * infiltration plus the total rainfall, update the current cumulative
  210. * infiltration and guess cumulative infiltration for the next time step
  211. */
  212. if (I < cumI + R * input.dt) {
  213. /* less than full infiltration */
  214. dIdt = (I - cumI) / input.dt;
  215. cumI = I;
  216. /* initial guess for next time step */
  217. I += dIdt * input.dt;
  218. ponding = PONDING_YES;
  219. } else {
  220. /* full infiltration */
  221. dIdt = R;
  222. cumI += dIdt * input.dt;
  223. ponding = PONDING_NO;
  224. }
  225. return dIdt;
  226. }