2
/****************************************************************************
4
* MODULE: simwe library
5
* AUTHOR(S): Helena Mitasova, Jaro Hofierka, Lubos Mitas:
6
* PURPOSE: Hydrologic and sediment transport simulation (SIMWE)
8
* COPYRIGHT: (C) 2002 by the GRASS Development Team
10
* This program is free software under the GNU General Public
11
* License (>=v2). Read the file COPYING that comes with GRASS
14
*****************************************************************************/
16
/* hydro.c (simlib), 20.nov.2002, JH */
21
#include <grass/gis.h>
22
/* #include <grass/site.h> */
23
#include <grass/bitmap.h>
24
#include <grass/linkm.h>
25
#include <grass/glocale.h>
28
#include <grass/waterglobs.h>
30
/* **************************************************** */
31
/* create walker representation of si */
32
/* ******************************************************** */
33
/* .......... iblock loop */
39
/* int icoub, lwout, nmult; */
44
int mgen, mgen2, mgen3;
50
double velx, vely, stxm, stym;
51
double factor, conn, gaux, gauy;
52
double d1, addac, decr;
53
double barea, sarea, walkwe;
54
double gen, gen2, wei2, wei3, wei, weifac;
62
if (maxwa > (MAXW - mx * my)) {
63
mitfac = maxwa / (MAXW - mx * my);
65
maxwa = maxwa / nblock;
68
G_debug(2, " maxwa, nblock %d %d", maxwa, nblock);
70
for (iblock = 1; iblock <= nblock; iblock++) {
75
barea = stepx * stepy;
76
sarea = bresx * bresy;
77
G_debug(2, " barea,sarea,rwalk,sisum: %f %f %f %f", barea, sarea,
80
/* write hh.walkers0 */
82
for (k = 0; k < my; k++) {
83
for (l = 0; l < mx; l++) { /* run thru the whole area */
84
if (zz[k][l] != UNDEF) {
86
x = xp0 + stepx * (double)(l);
87
y = yp0 + stepy * (double)(k);
89
gen = rwalk * si[k][l] / sisum;
91
wei = gen / (double)(mgen + 1);
93
/*if (si[k][l] != 0.) { */
94
/* this stuff later for multiscale */
97
(double)maxwab *si[k][l] / (si0 *
98
(double)(mx2o * my2o));
99
gen2 = gen2 * (barea / sarea);
101
wei2 = gen2 / (double)(mgen2 + 1);
103
(int)((double)mgen2 * wei2 / ((double)mgen * wei));
105
wei3 = gen2 / (double)((mgen + 1) * (mgen2 + 1));
110
fprintf(stderr, "\n zero rainfall excess in cell");
113
/*G_debug(2, " gen,gen2,wei,wei2,mgen3,nmult: %f %f %f %f %d %d",gen,gen2,wei,wei2,mgen3,nmult);
115
for (iw = 1; iw <= mgen + 1; iw++) { /* assign walkers */
119
G_fatal_error(_("nwalk (%d) > maxw (%d)!"), lw, MAXW);
121
w[lw][1] = x + stepx * (ulec() - 0.5);
122
w[lw][2] = y + stepy * (ulec() - 0.5);
126
vavg[lw][1] = v1[k][l];
127
vavg[lw][2] = v2[k][l];
128
if (w[lw][1] >= xmin && w[lw][2] >= ymin &&
129
w[lw][1] <= xmax && w[lw][2] <= ymax) {
149
G_debug(2, " number of written walkers: %d", lwwfin);
150
G_debug(2, " nwalk, maxw %d %d", nwalk, MAXW);
151
G_debug(2, " walkwe (walk weight),frac %f %f", walkwe, frac);
153
stxm = stepx * (double)(mx + 1) - xmin;
154
stym = stepy * (double)(my + 1) - ymin;
156
deldif = sqrt(deltap) * frac; /* diffuse factor */
159
factor = deltap * sisum / (rwalk * (double)nblock);
161
G_debug(2, " deldif,factor %f %e", deldif, factor);
163
/* ********************************************************** */
164
/* main loop over the projection time */
165
/* *********************************************************** */
168
G_debug(2, "main loop over the projection time... ");
170
for (i = 1; i <= miter; i++) { /* iteration loop depending on simulation time and deltap */
171
G_percent(i, miter, 1);
175
nfiterw = i / iterout + 10;
176
nfiterh = i / iterout + 40;
177
G_debug(2, "iblock=%d i=%d miter=%d nwalk=%d nwalka=%d",
178
iblock, i, miter, nwalk, nwalka);
181
if (nwalka == 0 && i > 1)
184
/* ************************************************************ */
185
/* .... propagate one step */
186
/* ************************************************************ */
189
conn = (double)nblock / (double)iblock;
195
for (lw = 1; lw <= nwalk; lw++) {
196
if (w[lw][3] > EPS) { /* check the walker weight */
198
l = (int)((w[lw][1] + stxm) / stepx) - mx - 1;
199
k = (int)((w[lw][2] + stym) / stepy) - my - 1;
201
if (l > mx - 1 || k > my - 1 || k < 0 || l < 0) {
203
G_debug(2, " k,l=%d,%d", k, l);
204
printf(" lw,w=%d %f %f", lw, w[lw][1], w[lw][2]);
205
G_debug(2, " stxym=%f %f", stxm, stym);
206
printf(" step=%f %f", stepx, stepy);
207
G_debug(2, " m=%d %d", my, mx);
208
printf(" nwalka,nwalk=%d %d", nwalka, nwalk);
212
if (zz[k][l] != UNDEF) {
214
if (infil != NULL) { /* infiltration part */
216
if (inf[k][l] - si[k][l] > 0.) {
218
decr = pow(addac * w[lw][3], 3. / 5.); /* decreasing factor in m */
219
if (inf[k][l] > decr) {
220
inf[k][l] -= decr; /* decrease infilt. in cell and eliminate the walker */
224
w[lw][3] -= pow(inf[k][l], 5. / 3.) / addac; /* use just proportional part of the walker weight */
234
gama[k][l] += (addac * w[lw][3]); /* add walker weigh to water depth or conc. */
236
d1 = gama[k][l] * conn;
237
hhc = pow(d1, 3. / 5.);
239
if (hhc > hhmax && wdepth == NULL) { /* increased diffusion if w.depth > hhmax */
240
dif[k][l] = (halpha + 1) * deldif;
251
if (traps != NULL && trap[k][l] != 0.) { /* traps */
253
eff = ulec(); /* random generator */
255
if (eff <= trap[k][l]) {
256
velx = -0.1 * v1[k][l]; /* move it slightly back */
257
vely = -0.1 * v2[k][l];
266
w[lw][1] += (velx + dif[k][l] * gaux); /* move the walker */
267
w[lw][2] += (vely + dif[k][l] * gauy);
269
if (hhc > hhmax && wdepth == NULL) {
270
vavg[lw][1] = hbeta * (vavg[lw][1] + v1[k][l]);
271
vavg[lw][2] = hbeta * (vavg[lw][2] + v2[k][l]);
274
if (w[lw][1] <= xmin || w[lw][2] <= ymin || w[lw][1]
275
>= xmax || w[lw][2] >= ymax) {
276
w[lw][3] = 1e-10; /* eliminate walker if it is out of area */
279
if (wdepth != NULL) {
280
l = (int)((w[lw][1] + stxm) / stepx) - mx - 1;
281
k = (int)((w[lw][2] + stym) / stepy) - my - 1;
282
w[lw][3] *= sigma[k][l];
288
w[lw][3] = 1e-10; /* eliminate walker if it is out of area */
292
/* output walkers commented out */
293
/* write the walkers which reach the box */
295
if (w[lw][1] >= xmin && w[lw][2] >= ymin && w[lw][1]
296
<= xmax && w[lw][2] <= ymax && iflag[lw] == 0) {
300
if (lwout == lw && (i == miter || i == iter1)) {
301
stack[nstack][1] = mixx / conv + w[lw][1] / conv;
302
stack[nstack][2] = miyy / conv + w[lw][2] / conv;
303
stack[nstack][3] = w[lw][3];
313
/* walker is leaving the box */
315
/* if (w[lw][1] <= mixx && w[lw][2] <= bymi && w[lw][1]
316
>= bxma && w[lw][2] >= byma && iflag[lw] == 1) {
320
/* every iterout iteration write out the selected ldemo walkers */
321
/* and water depth and time */
328
/* call output for iteration output */
332
erod(gama); /* divergence of gama field */
334
conn = (double)nblock / (double)iblock;
335
itime = (int)(i * deltap * timec);
336
ii = output_data(itime, conn);
339
G_fatal_error(_("Unable to write raster maps"));
344
/* ascii data site file output for gamma - hydrograph or sediment*/
345
/* cchez incl. sqrt(sinsl) */
351
for (p = 0; p < npoints; p++) {
353
l = (int)((points[p].east - mixx + stxm) / stepx) - mx -
355
k = (int)((points[p].north - miyy + stym) / stepy) - my -
358
if (zz[k][l] != UNDEF) {
360
if (wdepth == NULL) {
361
points[p].z1 = step * gama[k][l] * cchez[k][l];
364
points[p].z1 = gama[k][l] * slope[k][l];
366
G_debug(2, " k,l,z1 %d %d %f", k, l, points[p].z1);
368
fprintf(fw, "%f %f %f\n", points[p].east / conv,
369
points[p].north / conv, points[p].z1);
380
/* if (iwrib != nblock) {
381
icount = icoub / iwrib;
383
if (icoub == (icount * iwrib)) {
386
conn = (double) nblock / (double) iblock;
393
for (k = 0; k < my; k++) {
394
for (l = 0; l < mx; l++) {
395
if (zz[k][l] != UNDEF) {
396
d1 = gama[k][l] * (double)conn;
397
gammas[k][l] += pow(d1, 3. / 5.);
405
/* ........ end of iblock loop */