forked from jhamman/DHSVM
-
Notifications
You must be signed in to change notification settings - Fork 0
/
MainMWM.c
executable file
·793 lines (647 loc) · 26.3 KB
/
MainMWM.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
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
/*
* SUMMARY: MainMWM.c - Mass Wasting Module
* USAGE: MWM
*
* AUTHOR: Colleen O. Doten/Laura Bowling
* ORG: University of Washington, Department of Civil Engineering
* E-MAIL: [email protected]
* ORIG-DATE: Sep-02
* Last Change: Thu Jun 19 09:27:02 2003 by Ed Maurer <[email protected]>
* DESCRIPTION: Main routine to drive MWM - the Mass Wasting Module for DHSVM
* DESCRIP-END.
* FUNCTIONS: main()
* COMMENTS:
*/
/******************************************************************************/
/* INCLUDES */
/******************************************************************************/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
#include "settings.h"
#include "Calendar.h"
#include "getinit.h"
#include "DHSVMerror.h"
#include "data.h"
#include "fileio.h"
#include "functions.h"
#include "constants.h"
#include "DHSVMChannel.h"
#include "slopeaspect.h"
#define BUFSIZE 255
#define empty(s) !(s)
void enqueue(node **head, node **tail, int y, int x);
void dequeue(node **head, node **tail, int *y, int *x);
/******************************************************************************/
/* MAIN */
/******************************************************************************/
void MainMWM(SEDPIX **SedMap, FINEPIX ***FineMap, VEGTABLE *VType,
SEDTABLE *SedType, CHANNEL *ChannelData, char *DumpPath,
SOILPIX **SoilMap, TIMESTRUCT *Time, MAPSIZE *Map,
TOPOPIX **TopoMap, SOILTABLE *SType, VEGPIX **VegMap,
int MaxStreamID, SNOWPIX **SnowMap)
{
int x,y,xx,yy,i,j,ii,jj,k,iter; /* Counters. */
int coursei, coursej;
int nextx, nexty;
int prevx, prevy;
int numfailedpixels;
int numlikelyfailedpixels;
int numfailures;
float avgnumfailures;
float avgpixperfailure;
float failure_threshold = 0.0;
char buffer[32];
char sumoutfile[100]; /* Character array to hold file name. */
int **failure;
float factor_safety;
float LocalSlope;
FILE *fs; /* File pointer. */
int numpixels;
int cells, count, checksink;
int massitertemp; /* if massiter is 0, sets the counter to 1 here */
float TotalVolume;
node *head, *tail;
float SlopeAspect, SedimentToChannel;
float *SegmentSediment; /* The cumulative sediment content over all stochastic
iterations for each channel segment. */
float **SegmentSedimentm; /* The cumulative sediment mass over all stochastic
iterations for each channel segment. */
float *InitialSegmentSediment; /* Placeholder of segment sediment load at
beginning of time step. */
float **InitialSegmentSedimentm; /* Placeholder of segment sediment mass at
beginning of time step. */
float **SedThickness; /* Cumulative sediment depth over all stochastic
iterations for each pixel. */
float **InitialSediment; /* Place holder of pixel sediment load at beginning of
time step. */
float SedToDownslope; /* Sediment wasted from a pixel, awaiting redistribution */
float SedFromUpslope; /* Wasted sediment being redistributed */
float FineMapTableDepth; /* Fine grid water table depth (m) */
float TableDepth; /* Coarse grid water table depth (m) */
float FineMapSatThickness; /* Fine grid saturated thickness (m) */
float **Redistribute, **TopoIndex, **TopoIndexAve;
int firsti, firstj;
head = NULL;
tail = NULL;
/*****************************************************************************
Allocate memory for Soil Moisture Redistribution
****************************************************************************/
if (!(Redistribute = (float **)calloc(Map->NY, sizeof(float *))))
ReportError("MainMWM", 1);
for(i=0; i<Map->NY; i++) {
if (!(Redistribute[i] = (float *)calloc(Map->NX, sizeof(float))))
ReportError("MainMWM", 1);
}
if (!(TopoIndex = (float **)calloc(Map->NY, sizeof(float *))))
ReportError("MainMWM", 1);
for(i=0; i<Map->NY; i++) {
if (!(TopoIndex[i] = (float *)calloc(Map->NX, sizeof(float))))
ReportError("MainMWM", 1);
}
if (!(TopoIndexAve = (float **)calloc(Map->NY, sizeof(float *))))
ReportError("MainMWM", 1);
for(i=0; i<Map->NY; i++) {
if (!(TopoIndexAve[i] = (float *)calloc(Map->NX, sizeof(float))))
ReportError("MainMWM", 1);
}
/* Redistribute soil moisture from coarse grid to fine grid. The is done similarly
to Burton, A. and J.C. Bathurst, 1998, Physically based modelling of shallow
landslide sediment yield as a catchment scale, Environmental Geology,
35 (2-3), 89-99.*/
for (i = 0; i < Map->NY; i++) {
for (j = 0; j < Map->NX; j++) {
/* Check to make sure region is in the basin. */
if (INBASIN(TopoMap[i][j].Mask)) {
/* Step over each fine resolution cell within the model grid cell. */
for(ii=0; ii< Map->DY/Map->DMASS; ii++) { /* Fine resolution counters. */
for(jj=0; jj< Map->DX/Map->DMASS; jj++) {
y = (int) i*Map->DY/Map->DMASS + ii;
x = (int) j*Map->DX/Map->DMASS + jj;
TopoIndex[i][j] += (*FineMap[y][x]).TopoIndex;
}
}
/* TopoIndexAve is the TopoIndex for the coarse grid calculated as the average of the
TopoIndex of the fine grids in the coarse grid. */
TopoIndexAve[i][j] = TopoIndex[i][j]/Map->NumFineIn;
}
}
}
FineMapSatThickness = 0.;
for (i = 0; i < Map->NY; i++) {
for (j = 0; j < Map->NX; j++) {
if (INBASIN(TopoMap[i][j].Mask)) {
TableDepth = SoilMap[i][j].TableDepth;
/* Do not want to distribute ponded water */
if (TableDepth < 0.)
TableDepth = 0.;
for(ii=0; ii< Map->DY/Map->DMASS; ii++) {
for(jj=0; jj< Map->DX/Map->DMASS; jj++) {
y = (int) i*Map->DY/Map->DMASS + ii;
x = (int) j*Map->DX/Map->DMASS + jj;
if (SoilMap[i][j].Depth > SoilMap[i][j].TableDepth){
FineMapTableDepth = TableDepth +
((TopoIndexAve[i][j]-(*FineMap[y][x]).TopoIndex)/
SType[SoilMap[i][j].Soil - 1].KsLatExp);
if (FineMapTableDepth < 0.) {
(*FineMap[y][x]).SatThickness = (*FineMap[y][x]).sediment;
}
else if (FineMapTableDepth > (*FineMap[y][x]).sediment)
(*FineMap[y][x]).SatThickness = 0.;
else
(*FineMap[y][x]).SatThickness = (*FineMap[y][x]).sediment -
FineMapTableDepth;
}
else (*FineMap[y][x]).SatThickness = 0.;
FineMapSatThickness += (*FineMap[y][x]).SatThickness;
if ((ii== Map->DY/Map->DMASS - 1) & (jj== Map->DX/Map->DMASS - 1)){
/* Calculating the difference between the volume of water distributed
(only saturated) and available volume of water (m3)*/
Redistribute[i][j] = (Map->DY * Map->DX *
(SoilMap[i][j].Depth - TableDepth)) -
(FineMapSatThickness*Map->DMASS*Map->DMASS);
FineMapSatThickness = 0.;
}
}
}
}
}
}
/* Redistribute volume difference. Start with cells with too much water. */
for (i = 0; i < Map->NY; i++) {
for (j = 0; j < Map->NX; j++) {
if (INBASIN(TopoMap[i][j].Mask)) {
if (Redistribute[i][j]< -25.){
for (k = 0; k < Map->NumFineIn; k++) {
y = TopoMap[i][j].OrderedTopoIndex[k].y;
x = TopoMap[i][j].OrderedTopoIndex[k].x;
yy = TopoMap[i][j].OrderedTopoIndex[(Map->NumFineIn)-k-1].y;
xx = TopoMap[i][j].OrderedTopoIndex[(Map->NumFineIn)-k-1].x;
/* Convert sat thickness to a volume */
(*FineMap[y][x]).SatThickness *= (Map->DMASS)*(Map->DMASS);
/* Add to volume based on amount to be redistributed */
(*FineMap[y][x]).SatThickness += Redistribute[i][j] *
((*FineMap[yy][xx]).TopoIndex/TopoIndex[i][j]);
/* Convert back to thickness (m)*/
(*FineMap[y][x]).SatThickness /= (Map->DMASS)*(Map->DMASS);
}
}
}
}
}
/* Redistribute volume difference for cells with too little water.*/
for (i = 0; i < Map->NY; i++) {
for (j = 0; j < Map->NX; j++) {
if (INBASIN(TopoMap[i][j].Mask)) {
for(ii=0; ii< Map->DY/Map->DMASS; ii++) {
for(jj=0; jj< Map->DX/Map->DMASS; jj++) {
y = (int) i*Map->DY/Map->DMASS + ii;
x = (int) j*Map->DX/Map->DMASS + jj;
if (Redistribute[i][j] > 25.){
/* Convert sat thickness to a volume */
(*FineMap[y][x]).SatThickness *= (Map->DMASS)*(Map->DMASS);
/* Add to volume based on amount to be redistributed */
(*FineMap[y][x]).SatThickness += Redistribute[i][j] *
((*FineMap[y][x]).TopoIndex/TopoIndex[i][j]);
/* Convert back to thickness (m)*/
(*FineMap[y][x]).SatThickness /= (Map->DMASS)*(Map->DMASS);
}
if ((Redistribute[i][j] > 25.)||(Redistribute[i][j]< -25.)){
if ((*FineMap[y][x]).SatThickness > (*FineMap[y][x]).sediment)
(*FineMap[y][x]).SatThickness = (*FineMap[y][x]).sediment;
else if ((*FineMap[y][x]).SatThickness < 0.)
(*FineMap[y][x]).SatThickness = 0.;
}
}
}
}
}
}
for(i=0; i<Map->NY; i++) {
free(Redistribute[i]);
free(TopoIndex[i]);
free(TopoIndexAve[i]);
}
free(Redistribute);
free(TopoIndex);
free(TopoIndexAve);
/*****************************************************************************
Allocate memory for ensemble calculations
*****************************************************************************/
if (!(failure = (int **)calloc(Map->NYfine, sizeof(int *))))
ReportError("MainMWM", 1);
for(i=0; i<Map->NYfine; i++) {
if (!(failure[i] = (int *)calloc(Map->NXfine, sizeof(int))))
ReportError("MainMWM", 1);
}
if (!(SedThickness = (float **)calloc(Map->NYfine, sizeof(float *))))
ReportError("MainMWM", 1);
for(i=0; i<Map->NYfine; i++) {
if (!(SedThickness[i] = (float *)calloc(Map->NXfine, sizeof(float))))
ReportError("MainMWM", 1);
}
if (!(InitialSediment = (float **)calloc(Map->NYfine, sizeof(float *))))
ReportError("MainMWM", 1);
for(i=0; i<Map->NYfine; i++) {
if (!(InitialSediment[i] = (float *)calloc(Map->NXfine, sizeof(float))))
ReportError("MainMWM", 1);
}
if (!(SegmentSediment = (float *)calloc(MaxStreamID, sizeof(float ))))
ReportError("MainMWM", 1);
if (!(SegmentSedimentm = (float **)calloc(MaxStreamID, sizeof(float *))))
ReportError("MainMWM", 1);
for(i=1; i<MaxStreamID+1; i++) {
if (!(SegmentSedimentm[i] = (float *)calloc(NSEDSIZES, sizeof(float))))
ReportError("MainMWM", 1);
}
if (!(InitialSegmentSediment = (float *)calloc(MaxStreamID, sizeof(float ))))
ReportError("MainMWM", 1);
if (!(InitialSegmentSedimentm = (float **)calloc(MaxStreamID, sizeof(float *))))
ReportError("MainMWM", 1);
for(i=1; i<MaxStreamID+1; i++) {
if (!(InitialSegmentSedimentm[i] = (float *)calloc(NSEDSIZES, sizeof(float))))
ReportError("MainMWM", 1);
}
/* Initialize arrays. */
for (y = 0; y < Map->NY; y++) {
for (x = 0; x < Map->NX; x++) {
if (INBASIN(TopoMap[y][x].Mask)) {
for(ii=0; ii< Map->DY/Map->DMASS; ii++) { /* Fine resolution counters. */
for(jj=0; jj< Map->DX/Map->DMASS; jj++) {
yy = (int) y*Map->DY/Map->DMASS + ii;
xx = (int) x*Map->DX/Map->DMASS + jj;
InitialSediment[yy][xx] = (*FineMap[yy][xx]).sediment;
(*FineMap[yy][xx]).Probability = 0.;
(*FineMap[yy][xx]).MassWasting = 0.;
(*FineMap[yy][xx]).MassDeposition = 0.;
(*FineMap[yy][xx]).SedimentToChannel = 0.;
}
}
}
}
}
initialize_sediment_mass(ChannelData->streams, InitialSegmentSedimentm);
update_sediment_array(ChannelData->streams, InitialSegmentSediment, InitialSegmentSedimentm);
/************************************************************************/
/* Begin iteration for multiple ensembles. */
/************************************************************************/
/* sloppy fix for when MASSITER=0 -- this only needs to be checked once */
if(MASSITER==0) massitertemp=1;
else massitertemp=MASSITER;
numfailures = 0;
for(iter=0; iter < massitertemp; iter++) {
printf("iter=%d\n",iter);
/************************************************************************/
/* Begin factor of safety code. */
/************************************************************************/
for(i=0; i<Map->NY; i++) {
for(j=0; j<Map->NX; j++) {
if (INBASIN(TopoMap[i][j].Mask)) {
for(ii=0; ii<Map->DY/Map->DMASS; ii++) {
for(jj=0; jj<Map->DX/Map->DMASS; jj++) {
y = i*Map->DY/Map->DMASS + ii;
x = j*Map->DX/Map->DMASS + jj;
coursei = i;
coursej = j;
firsti=y;
firstj=x;
/* Don't allow failures that will propagate outside the basin. */
/* Fine mask is optional, so they still may occur. */
if(INBASIN((*FineMap[y][x]).Mask)) {
checksink = 0;
numpixels = 0;
SedToDownslope = 0.0;
SedFromUpslope = 0.0;
SedimentToChannel = 0.0;
/* First check for original failure. */
if((*FineMap[y][x]).SatThickness/SoilMap[i][j].Depth > MTHRESH && failure[y][x] == 0
&& (*FineMap[y][x]).sediment > 0.0) {
LocalSlope = ElevationSlope(Map, TopoMap, FineMap, y, x, &nexty, &nextx, y, x, &SlopeAspect);
if(LocalSlope >= 10.) {
factor_safety = CalcSafetyFactor(LocalSlope, SoilMap[i][j].Soil,
(*FineMap[y][x]).sediment,
VegMap[i][j].Veg, SedType, VType,
(*FineMap[y][x]).SatThickness, SType,
SnowMap[i][j].Swq, SnowMap[i][j].Depth,
iter);
/* check if fine pixel fails */
if (factor_safety < FS_CRITERIA && factor_safety > 0) {
numfailures++;
numpixels = 1;
failure[y][x] = 1;
/* Update sediment depth. All sediment leaves failed fine pixel */
SedToDownslope = (*FineMap[y][x]).sediment;
(*FineMap[y][x]).sediment = 0.0;
// Pass sediment down to next pixel
SedFromUpslope = SedToDownslope;
/* Follow failures down slope; skipped if no original failure. */
while(failure[y][x] == 1 && checksink == 0
&& !channel_grid_has_channel(ChannelData->stream_map, coursej, coursei)
&& INBASIN(TopoMap[coursei][coursej].Mask)) {
/* Update counters. */
prevy = y;
prevx = x;
y = nexty;
x = nextx;
coursei = floor(y*Map->DMASS/Map->DY);
coursej = floor(x*Map->DMASS/Map->DX);
if (!INBASIN(TopoMap[coursei][coursej].Mask)) {
printf("WARNING: attempt to propagate failure to grid cell outside basin: y %d x %d\n",y,x);
printf("Depositing wasted sediment in grid cell y %d x %d\n",prevy,prevx);
(*FineMap[prevy][prevx]).sediment += SedFromUpslope;
SedFromUpslope = SedToDownslope = 0.0;
// Since we're returning SedFromUpslope to the upslope pixel,
// the upslope pixel can't be considered as part of the failure
failure[prevy][prevx] = 0;
}
else {
// Add sediment from upslope to current sediment
(*FineMap[y][x]).sediment += SedFromUpslope;
LocalSlope = ElevationSlope(Map, TopoMap, FineMap, y, x, &nexty,
&nextx, prevy, prevx, &SlopeAspect);
/* Check that not a sink */
if(LocalSlope >= 0.) {
factor_safety = CalcSafetyFactor(LocalSlope, SoilMap[coursei][coursej].Soil,
(*FineMap[y][x]).sediment,
VegMap[coursei][coursej].Veg, SedType, VType,
(*FineMap[y][x]).SatThickness, SType,
SnowMap[coursei][coursej].Swq,
SnowMap[coursei][coursej].Depth, iter);
/* check if fine pixel fails */
if (factor_safety < FS_CRITERIA && factor_safety > 0) {
numpixels += 1;
failure[y][x] = 1;
/* Update sediment depth. All sediment leaves failed fine pixel */
SedToDownslope = (*FineMap[y][x]).sediment;
(*FineMap[y][x]).sediment = 0.0;
// Pass sediment down to next pixel
SedFromUpslope = SedToDownslope;
}
else {
/* Update sediment depth. */
// Remove the sediment we added for the slope calculation
// and instead prepare to distribute this sediment along runout zone
(*FineMap[y][x]).sediment -= SedFromUpslope;
}
} /* end if(LocalSlope >= 0) { */
else {
/* Update sediment depth. */
// Remove the sediment we added for the slope calculation
// and instead prepare to distribute this sediment along runout zone
// (*FineMap[y][x]).sediment -= SedFromUpslope;
/* If it reaches here, a sink exists. A sink can
not fail or run out, so move to the next pixel. */
checksink++;
}
}
} /* End of while loop. */
if (checksink > 0) continue;
/* Failure has stopped, now calculate runout distance and
redistribute sediment. */
// y and x are now the coords of the first pixel of the runout
// (downslope neighbor of final pixel of the failure);
// this is the pixel that caused the last loop to exit,
// whether due to being a sink, not failing,
// being in a coarse pixel containing a stream,
// or being outside the basin
// If current cell is outside the basin,
// stop processing this runout and go to next failure candidate
if (!INBASIN(TopoMap[coursei][coursej].Mask)) {
continue;
}
// TotalVolume = depth (not volume) being redistributed
TotalVolume = SedFromUpslope;
cells = 1;
/* queue begins with initial unfailed pixel. */
enqueue(&head, &tail, y, x);
while(LocalSlope > 4. && !channel_grid_has_channel(ChannelData->stream_map, coursej, coursei)
&& INBASIN(TopoMap[coursei][coursej].Mask)) {
/* Redistribution stops if last pixel was a channel
or was outside the basin. */
/* Update counters. */
prevy = y;
prevx = x;
y = nexty;
x = nextx;
coursei = floor(y*Map->DMASS/Map->DY);
coursej = floor(x*Map->DMASS/Map->DX);
if (INBASIN(TopoMap[coursei][coursej].Mask)) {
LocalSlope = ElevationSlope(Map, TopoMap, FineMap, y, x, &nexty,
&nextx, prevy, prevx,
&SlopeAspect);
enqueue(&head, &tail, y, x);
cells++;
}
else {
printf("WARNING: attempt to propagate runout to grid cell outside the basin: y %d x %d\n",y,x);
printf("Final grid cell of runout will be: y %d x %d\n",prevy,prevx);
}
}
prevy = y;
prevx = x;
for(count=0; count < cells; count++) {
dequeue(&head, &tail, &y, &x);
coursei = floor(y*Map->DMASS/Map->DY);
coursej = floor(x*Map->DMASS/Map->DX);
// If this node has a channel, then this MUST be the end of the queue
if(channel_grid_has_channel(ChannelData->stream_map, coursej, coursei)) {
/* TotalVolume at this point is a depth in m over one fine
map grid cell - convert to m3 */
SedimentToChannel = TotalVolume*(Map->DMASS*Map->DMASS)/(float) cells;
}
else {
/* Redistribute sediment equally among all hillslope cells. */
(*FineMap[y][x]).sediment += TotalVolume/cells;
}
}
if(SedimentToChannel > 0.0) {
if(SlopeAspect < 0.) {
printf("Invalid aspect (%3.1f) in cell y= %d x= %d\n",
SlopeAspect,y,x);
exit(0);
}
else {
// Add Current value of SedimentToChannel to running total for this FineMap cell
// (allowing for more than one debris flow to end at the same channel)
(*FineMap[y][x]).SedimentToChannel += SedimentToChannel;
// Now route SedimentToChannel through stream network
RouteDebrisFlow(&SedimentToChannel, coursei, coursej, SlopeAspect, ChannelData, Map);
}
}
} /* End of this failure/runout event */
}
}
} /* End of fine mask check. */
} /* End of jj loop. */
}
}
}
} /* End of course resolution loop. */
/* Record failures and Reset failure map for new iteration. */
for(i=0; i<Map->NY; i++) {
for(j=0; j<Map->NX; j++) {
if (INBASIN(TopoMap[i][j].Mask)) {
for(ii=0; ii<Map->DY/Map->DMASS; ii++) {
for(jj=0; jj<Map->DX/Map->DMASS; jj++) {
y = i*Map->DY/Map->DMASS + ii;
x = j*Map->DX/Map->DMASS + jj;
(*FineMap[y][x]).Probability += (float) failure[y][x];
/* Record cumulative sediment volume. */
SedThickness[y][x] += (*FineMap[y][x]).sediment;
/* Reset sediment thickness for each iteration; otherwise there is
a decreasing probability of failure for subsequent iterations. */
/* If not in stochastic mode, then allow a history of past failures
and do not reset sediment depth */
if(massitertemp>1) {
(*FineMap[y][x]).sediment = InitialSediment[y][x];
failure[y][x] = 0;
}
}
}
}
}
}
/* printf("Sediment Mass is "); */
/* count_sediment_mass(ChannelData->streams, InitialSegmentSediment); */
/* Record cumulative stream sediment volumes. */
initialize_sediment_array(ChannelData->streams, SegmentSediment,
SegmentSedimentm);
/* Reset channel sediment volume for each iteration. */
update_sediment_array(ChannelData->streams, InitialSegmentSediment, InitialSegmentSedimentm);
update_sediment_mass(ChannelData->streams, SegmentSedimentm,
massitertemp);
} /* End iteration loop */
// Normalize mass wasting vars by number of iterations
numfailedpixels = 0;
numlikelyfailedpixels = 0;
for(i=0; i<Map->NY; i++) {
for(j=0; j<Map->NX; j++) {
if (INBASIN(TopoMap[i][j].Mask)) {
for(ii=0; ii<Map->DY/Map->DMASS; ii++) {
for(jj=0; jj<Map->DX/Map->DMASS; jj++) {
y = i*Map->DY/Map->DMASS + ii;
x = j*Map->DX/Map->DMASS + jj;
(*FineMap[y][x]).Probability /= (float)massitertemp;
(*FineMap[y][x]).sediment = SedThickness[y][x]/(float)massitertemp;
(*FineMap[y][x]).SedimentToChannel /= (float)massitertemp;
if ((*FineMap[y][x]).sediment > InitialSediment[y][x]) {
(*FineMap[y][x]).MassDeposition = ((*FineMap[y][x]).sediment - InitialSediment[y][x])*(Map->DMASS*Map->DMASS);
(*FineMap[y][x]).MassWasting = 0.0;
}
else if ((*FineMap[y][x]).sediment < InitialSediment[y][x]) {
(*FineMap[y][x]).MassDeposition = 0.0;
(*FineMap[y][x]).MassWasting = (InitialSediment[y][x] - (*FineMap[y][x]).sediment)*(Map->DMASS*Map->DMASS);
}
if((*FineMap[y][x]).Probability > 0)
numfailedpixels +=1;
if((*FineMap[y][x]).Probability > failure_threshold)
numlikelyfailedpixels +=1;
(*FineMap[y][x]).DeltaDepth = (*FineMap[y][x]).sediment -
SoilMap[i][j].Depth;
}
}
}
}
}
// Compute average number of failures
avgnumfailures = numfailures/(float)massitertemp;
// Compute average number of pixels per failure
if (numfailures > 0) {
avgpixperfailure = (float)numfailedpixels/(float)numfailures;
}
else {
avgpixperfailure = 0.0;
}
// Average sediment delivery to each stream segment
for(i=1; i<MaxStreamID+1; i++) {
SegmentSediment[i] /= (float)massitertemp;
if(SegmentSediment[i] < 0.0) SegmentSediment[i]=0.0;
}
update_sediment_array(ChannelData->streams, SegmentSediment, SegmentSedimentm);
/* Take new sediment inflow and distribute it by representative diameters*/
/* and convert to mass */
sed_vol_to_distrib_mass(ChannelData->streams, SegmentSediment);
/*************************************************************************/
/* Create failure summary file, in the specified output directory: */
/* failure_summary.txt - for each date that the mwm algorithm is run: */
/* ave. no. of failures (strip of pixels */
/* originating from a failed pixel) */
/* ave. no. of pixles per failure */
/* total number of failed pixels with probability */
/* of failure > failure_threshold */
/*************************************************************************/
sprintf(sumoutfile, "%sfailure_summary.txt", DumpPath);
if((fs=fopen(sumoutfile,"a")) == NULL)
{
printf("Cannot open factor of safety summary output file.\n");
exit(0);
}
SPrintDate(&(Time->Current), buffer);
fprintf(fs, "%-20s %.4f %.4f %7d\n", buffer, avgnumfailures, avgpixperfailure, numlikelyfailedpixels);
printf("%.4f failures; %.4f pixels per failure; %d pixels have failure likelihood > %.2f\n",
avgnumfailures, avgpixperfailure, numlikelyfailedpixels, failure_threshold);
fclose(fs);
for(i=0; i<Map->NYfine; i++) {
free(failure[i]);
free(SedThickness[i]);
free(InitialSediment[i]);
}
for(i=1; i<MaxStreamID+1; i++) {
free(SegmentSedimentm[i]);
}
free(failure);
free(SedThickness);
free(InitialSediment);
free(SegmentSediment);
free(SegmentSedimentm);
free(InitialSegmentSediment);
free(InitialSegmentSedimentm);
}
/*****************************************************************************
End of Main
*****************************************************************************/
void enqueue(node **head, node **tail, int y, int x)
{
node *new;
// Allocate and initialize a new node
if(!(new = (node *) malloc(sizeof(node))))
ReportError("enqueue", 1);
new->x = x;
new->y = y;
new->next = NULL;
if(empty(*head)) {
// If *head is empty, the queue is empty and we're inserting the first node;
// therefore this node is both the header and the tail
*head = new;
*tail = new;
}
else {
// if(!empty(*tail)) {
// fprintf(stderr,"New node is not at end of queue\n");
// exit(0);
// }
// Point the tail node's "next" to the new node
(*tail)->next = new;
// Now this new node is the tail, so point "tail" to this new node
*tail = new;
}
}
void dequeue(node **head, node **tail, int *y, int *x)
{
node *temp;
// if(!empty(*head))
// {
// fprintf(stderr,"Node is not at head of queue\n");
// exit(0);
// }
*y = (*head)->y;
*x = (*head)->x;
// Point temp to the header node so we still have a reference to it
temp = *head;
// Point *head to the next node; this is the new header node
*head = (*head)->next;
if(head == NULL) tail = NULL;
// De-allocate the old header node, now that we're no longer using it
free(temp);
}