-
-
Notifications
You must be signed in to change notification settings - Fork 344
/
Copy pathhydro.c
419 lines (358 loc) · 16.5 KB
/
hydro.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
/****************************************************************************
*
* MODULE: simwe library
* AUTHOR(S): Helena Mitasova, Jaro Hofierka, Lubos Mitas:
* PURPOSE: Hydrologic and sediment transport simulation (SIMWE)
*
* COPYRIGHT: (C) 2002 by the GRASS Development Team
*
* This program is free software under the GNU General Public
* License (>=v2). Read the file COPYING that comes with GRASS
* for details.
*
*****************************************************************************/
/* hydro.c (simlib), 20.nov.2002, JH */
#include "simlib.h"
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <grass/gis.h>
#include <grass/bitmap.h>
#include <grass/linkm.h>
#include <grass/glocale.h>
#include <grass/waterglobs.h>
#include <grass/simlib.h>
/*
* Soeren 8. Mar 2011 TODO:
* Put all these global variables into several meaningful structures and
* document use and purpose.
*
*/
struct point2D;
struct point3D;
float **zz, **cchez;
double **v1, **v2, **slope;
double **gama, **gammas, **si, **inf, **sigma;
float **dc, **tau, **er, **ct, **trap;
float **dif;
/* int iflag[MAXW]; */
struct point3D *w;
struct point2D *vavg;
/* **************************************************** */
/* create walker representation of si */
/* ******************************************************** */
/* .......... iblock loop */
void main_loop(const Setup *setup, const Geometry *geometry,
const Settings *settings, Simulation *sim,
ObservationPoints *points, const Inputs *inputs,
const Outputs *outputs)
{
int i, l, k;
int iblock;
int iter1;
double conn;
double addac;
int nblock = 1;
double stxm = geometry->stepx * (double)(geometry->mx + 1) - geometry->xmin;
double stym = geometry->stepy * (double)(geometry->my + 1) - geometry->ymin;
double deldif = sqrt(setup->deltap) * settings->frac; /* diffuse factor */
if (sim->maxwa > (MAXW - geometry->mx * geometry->my)) {
nblock = 1 + sim->maxwa / (MAXW - geometry->mx * geometry->my);
sim->maxwa = sim->maxwa / nblock;
}
double factor =
setup->deltap * setup->sisum / (sim->rwalk * (double)nblock);
G_debug(2, " deldif, factor %f %e", deldif, factor);
G_debug(2, " maxwa, nblock %d %d", sim->maxwa, nblock);
G_debug(2, "rwalk,sisum: %f %f", sim->rwalk, setup->sisum);
for (iblock = 1; iblock <= nblock; iblock++) {
int lw = 0;
double walkwe = 0.;
G_message(_("Processing block %d of %d"), iblock, nblock);
/* write hh.walkers0 */
for (k = 0; k < geometry->my; k++) {
for (l = 0; l < geometry->mx; l++) { /* run thru the whole area */
if (zz[k][l] != UNDEF) {
double x = geometry->xp0 + geometry->stepx * (double)(l);
double y = geometry->yp0 + geometry->stepy * (double)(k);
double gen = sim->rwalk * si[k][l] / setup->sisum;
int mgen = (int)gen;
double wei = gen / (double)(mgen + 1);
for (int iw = 1; iw <= mgen + 1;
iw++) { /* assign walkers */
w[lw].x = x + geometry->stepx * (simwe_rand() - 0.5);
w[lw].y = y + geometry->stepy * (simwe_rand() - 0.5);
w[lw].m = wei;
walkwe += w[lw].m;
vavg[lw].x = v1[k][l];
vavg[lw].y = v2[k][l];
lw++;
}
} /* defined area */
}
}
sim->nwalk = lw;
G_debug(2, " nwalk, maxw %d %d", sim->nwalk, MAXW);
G_debug(2, " walkwe (walk weight),frac %f %f", walkwe, settings->frac);
sim->nwalka = 0;
int nwalka = 0;
/* ********************************************************** */
/* main loop over the projection time */
/* *********************************************************** */
G_debug(2, "main loop over the projection time... ");
for (i = 1; i <= setup->miter;
i++) { /* iteration loop depending on simulation time and deltap */
G_percent(i, setup->miter, 1);
iter1 = i / setup->iterout;
iter1 *= setup->iterout;
if (iter1 == i) {
/* nfiterw = i / iterout + 10;
nfiterh = i / iterout + 40; */
G_debug(2, "iblock=%d i=%d miter=%d nwalk=%d nwalka=%d", iblock,
i, setup->miter, sim->nwalk, sim->nwalka);
}
if (sim->nwalka == 0 && i > 1)
goto L_800;
/* ************************************************************ */
/* .... propagate one step */
/* ************************************************************ */
addac = factor;
conn = (double)nblock / (double)iblock;
if (i == 1) {
addac = factor * .5;
}
nwalka = 0;
sim->nstack = 0;
#pragma omp parallel firstprivate(l, lw, k) reduction(+ : nwalka)
{
#if defined(_OPENMP)
int steps = (int)((((double)sim->nwalk) /
((double)omp_get_num_threads())) +
0.5);
int tid = omp_get_thread_num();
int min_loop = tid * steps;
int max_loop = ((tid + 1) * steps) > sim->nwalk
? sim->nwalk
: (tid + 1) * steps;
for (lw = min_loop; lw < max_loop; lw++) {
#else
for (lw = 0; lw < sim->nwalk; lw++) {
#endif
if (w[lw].m > EPS) { /* check the walker weight */
++(nwalka);
l = (int)((w[lw].x + stxm) / geometry->stepx) -
geometry->mx - 1;
k = (int)((w[lw].y + stym) / geometry->stepy) -
geometry->my - 1;
if (l > geometry->mx - 1 || k > geometry->my - 1 ||
k < 0 || l < 0) {
G_debug(2, " k,l=%d,%d", k, l);
printf(" lw,w=%d %f %f", lw, w[lw].y, w[lw].m);
G_debug(2, " stxym=%f %f", stxm, stym);
printf(" step=%f %f", geometry->stepx,
geometry->stepy);
G_debug(2, " m=%d %d", geometry->my,
geometry->mx);
printf(" nwalka,nwalk=%d %d", sim->nwalka,
sim->nwalk);
G_debug(2, " ");
}
if (zz[k][l] != UNDEF) {
if (inf[k][l] != UNDEF) { /* infiltration part */
if (inf[k][l] - si[k][l] > 0.) {
double decr = pow(
addac * w[lw].m,
3. / 5.); /* decreasing factor in m */
if (inf[k][l] > decr) {
inf[k][l] -=
decr; /* decrease infilt. in cell
and eliminate the walker */
w[lw].m = 0.;
}
else {
w[lw].m -=
pow(inf[k][l], 5. / 3.) /
addac; /* use just proportional part
of the walker weight */
inf[k][l] = 0.;
}
}
}
gama[k][l] +=
(addac * w[lw].m); /* add walker weigh to water
depth or conc. */
double d1 = gama[k][l] * conn;
double gaux, gauy;
#if defined(_OPENMP)
gasdev_for_paralel(&gaux, &gauy);
#else
gaux = gasdev();
gauy = gasdev();
#endif
double hhc = pow(d1, 3. / 5.);
double velx, vely;
if (hhc > settings->hhmax &&
inputs->wdepth == NULL) { /* increased diffusion
if w.depth > hhmax */
dif[k][l] = (settings->halpha + 1) * deldif;
velx = vavg[lw].x;
vely = vavg[lw].y;
}
else {
dif[k][l] = deldif;
velx = v1[k][l];
vely = v2[k][l];
}
if (inputs->traps != NULL &&
trap[k][l] != 0.) { /* traps */
float eff = simwe_rand(); /* random generator */
if (eff <= trap[k][l]) {
velx = -0.1 *
v1[k][l]; /* move it slightly back */
vely = -0.1 * v2[k][l];
}
}
w[lw].x +=
(velx + dif[k][l] * gaux); /* move the walker */
w[lw].y += (vely + dif[k][l] * gauy);
if (hhc > settings->hhmax &&
inputs->wdepth == NULL) {
vavg[lw].x =
settings->hbeta * (vavg[lw].x + v1[k][l]);
vavg[lw].y =
settings->hbeta * (vavg[lw].y + v2[k][l]);
}
if (w[lw].x <= geometry->xmin ||
w[lw].y <= geometry->ymin ||
w[lw].x >= geometry->xmax ||
w[lw].y >= geometry->ymax) {
w[lw].m = 1e-10; /* eliminate walker if it is
out of area */
}
else {
if (inputs->wdepth != NULL) {
l = (int)((w[lw].x + stxm) /
geometry->stepx) -
geometry->mx - 1;
k = (int)((w[lw].y + stym) /
geometry->stepy) -
geometry->my - 1;
w[lw].m *= sigma[k][l];
}
} /* else */
} /*DEFined area */
else {
w[lw].m = 1e-10; /* eliminate walker if it is out of
area */
}
}
} /* lw loop */
}
/* Total remaining walkers for this iteration */
sim->nwalka = nwalka;
/* Changes made by Soeren 8. Mar 2011 to replace the site walker
* output implementation */
/* Save all walkers located within the computational region and with
valid z coordinates */
if (outputs->outwalk != NULL && (i == setup->miter || i == iter1)) {
sim->nstack = 0;
for (lw = 0; lw < sim->nwalk; lw++) {
/* Compute the elevation raster map index */
l = (int)((w[lw].x + stxm) / geometry->stepx) -
geometry->mx - 1;
k = (int)((w[lw].y + stym) / geometry->stepy) -
geometry->my - 1;
/* Check for correct elevation raster map index */
if (l < 0 || l >= geometry->mx || k < 0 ||
k >= geometry->my)
continue;
if (w[lw].m > EPS && zz[k][l] != UNDEF) {
/* Save the 3d position of the walker */
sim->stack[sim->nstack].x =
geometry->mixx / geometry->conv +
w[lw].x / geometry->conv;
sim->stack[sim->nstack].y =
geometry->miyy / geometry->conv +
w[lw].y / geometry->conv;
sim->stack[sim->nstack].m = zz[k][l];
sim->nstack++;
}
} /* lw loop */
}
if (i == iter1 && settings->ts) {
/* call output for iteration output */
if (outputs->erdep != NULL)
erod(gama, setup, geometry); /* divergence of gama field */
conn = (double)nblock / (double)iblock;
int itime = (int)(i * setup->deltap * setup->timec);
int ii = output_data(itime, conn, setup, geometry, settings,
sim, inputs, outputs);
if (ii != 1)
G_fatal_error(_("Unable to write raster maps"));
}
/* Write the water depth each time step at an observation point */
if (points->is_open) {
double value = 0.0;
int p;
fprintf(points->output, "%.6d ", i);
/* Write for each point */
for (p = 0; p < points->npoints; p++) {
l = (int)((points->x[p] - geometry->mixx + stxm) /
geometry->stepx) -
geometry->mx - 1;
k = (int)((points->y[p] - geometry->miyy + stym) /
geometry->stepy) -
geometry->my - 1;
if (zz[k][l] != UNDEF) {
if (inputs->wdepth == NULL)
value = geometry->step * gama[k][l] * cchez[k][l];
else
value = gama[k][l] * slope[k][l];
fprintf(points->output, "%2.4f ", value);
}
else {
/* Point is invalid, so a negative value is written */
fprintf(points->output, "%2.4f ", -1.0);
}
}
fprintf(points->output, "\n");
}
} /* miter */
L_800:
/* Soeren 8. Mar 2011: Why is this commented out? */
/* if (iwrib != nblock) {
icount = icoub / iwrib;
if (icoub == (icount * iwrib)) {
++icfl;
nflw = icfl + 50;
conn = (double) nblock / (double) iblock;
}
} */
if (outputs->err != NULL) {
for (k = 0; k < geometry->my; k++) {
for (l = 0; l < geometry->mx; l++) {
if (zz[k][l] != UNDEF) {
double d1 = gama[k][l] * (double)conn;
gammas[k][l] += pow(d1, 3. / 5.);
} /* DEFined area */
}
}
}
if (outputs->erdep != NULL)
erod(gama, setup, geometry);
}
/* ........ end of iblock loop */
/* Write final maps here because we know the last time stamp here */
if (!settings->ts) {
conn = (double)nblock / (double)iblock;
int itime = (int)(i * setup->deltap * setup->timec);
int ii = output_data(itime, conn, setup, geometry, settings, sim,
inputs, outputs);
if (ii != 1)
G_fatal_error(_("Cannot write raster maps"));
}
/* Close the observation logfile */
if (points->is_open)
fclose(points->output);
points->is_open = 0;
}