-
Notifications
You must be signed in to change notification settings - Fork 5
/
hd2sed.c
673 lines (621 loc) · 23.6 KB
/
hd2sed.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
/*
*
* ENVIRONMENTAL MODELLING SUITE (EMS)
*
* File: model/hd/sediments/hd2sed.c
*
* Description:
* Routine to handle data flow from hydromodule to sediment
*
* Copyright:
* Copyright (c) 2018. Commonwealth Scientific and Industrial
* Research Organisation (CSIRO). ABN 41 687 119 230. All rights
* reserved. See the license file for disclaimer and full
* use/redistribution conditions.
*
* $Id: hd2sed.c 6247 2019-07-17 01:09:37Z mar644 $
*
*/
#ifdef __cplusplus
#include "stdafx.h"
extern "C" {
#endif
#include "sediments.h"
#if defined(HAVE_SEDIMENT_MODULE)
extern void update_porosity_wc(sediment_t *sediment, sed_column_t *sm);
extern void update_porosity_sed(sediment_t *sediment, sed_column_t *sm);
static void hd2sed_internal(sediment_t *sediment, sed_column_t *sm, int c);
void reef_scale_depth(sediment_t *sediment, sed_column_t *sm);
double sinterface_getmodeltime(void* hmodel) ;
double sinterface_getcellarea(void* hmodel, int c);
double sinterface_getwind1(void* hmodel, int c) ;
double sinterface_getwind2(void* hmodel, int c) ;
double sinterface_getwindspeed(void* hmodel, int c);
double sinterface_gethripples(void* hmodel, int c) ;
double sinterface_getlripples(void* hmodel, int c) ;
double sinterface_getwave_ub(void* hmodel, int c) ;
double sinterface_getwave_period(void* hmodel, int c);
double sinterface_getwave_dir(void* hmodel, int c) ;
int sinterface_gettopk_wc(void* hmodel, int c) ;
int sinterface_getbotk_wc(void* hmodel, int c) ;
double sinterface_getbotz_wc(void* hmodel, int c);
double sinterface_gettopz_wc(void* hmodel, int c) ;
int sinterface_getcelli(void* hmodel, int c);
int sinterface_getcellj(void* hmodel, int c);
void sinterface_getdz_wc(void* hmodel, int c, double *dz_wc) ;
void sinterface_getgridz_sed(void* hmodel, int c,double *gridz_sed );
void sinterface_getu1_wc(void* hmodel, int c, double *u1_wc);
void sinterface_getu2_wc(void* hmodel, int c, double *u2_wc);
void sinterface_getkz_wc(void* hmodel, int c, double *Kz_wc) ;
void si_gettracer_wc(void* hmodel, int c,
int n, int m, double ***ptr_wc);
void si_gettracer_sed(void* hmodel, int c,
int n, int m, double ***ptr_sed);
void diagnostics(sediment_t *sediment, sed_column_t *sm, const char *comment);
void mass_balance(sediment_t *sediment, sed_column_t *sm);
double sinterface_getvalueofBtracer(void* hmodel, int n, int c);
double sinterface_get_srf_flux(void* hmodel, char *name, int c);
/* Fill sed_column_t structure */
void hd2sed(sediment_t *sediment, sed_column_t *sm, int c)
{
int kone, k, n,m;
void *hmodel = sediment->hmodel;
sed_params_t *param = sediment->msparam;
int col_index;
FILE *flog;
double a, rab1,rab2,ship_prob;
char etext[MAXSTRLEN];
/*UR-CHANGED , u_scale; */
/* update column index */
col_index=sm->col_number-1;
sm->hd_col_index = c;
sm->sed_start=1;
/* Increment time step (2019) */
if(col_index == 0)
param->nstep += 1;
/* Copy internal spatial sediment variables */
hd2sed_internal(sediment, sm, col_index);
sm->i=sinterface_getcelli(hmodel, c);
sm->j=sinterface_getcellj(hmodel, c);
sm->area = sinterface_getcellarea(hmodel, c);
/* sm->z0_skin= const specified during initialisation ; */
sm->wind1= sinterface_getwind1( hmodel, c);
sm->wind2= sinterface_getwind2( hmodel, c) ;
sm->windspeed= sinterface_getwindspeed( hmodel, c) ;
sm->wave_ub = sinterface_getwave_ub( hmodel, c) ;
sm->wave_period = sinterface_getwave_period( hmodel, c) ;
sm->wave_dir = sinterface_getwave_dir( hmodel, c) ;
sm->css_dep = param->css_dep;
for(n=0; n < param->ntr; n++) {
sm->erdepflux_ac[n] = 0.;
sm->erdepflux[n] = 0.;
}
/* wc */
sm->topk_wc =sinterface_gettopk_wc( hmodel, c) ;
sm->botk_wc =sinterface_getbotk_wc( hmodel, c) ;
sm->botz_wc =sinterface_getbotz_wc( hmodel, c);
sm->topz_wc = sinterface_gettopz_wc( hmodel, c);
sinterface_getdz_wc(hmodel, c, sm->dz_wc);
sinterface_getu1_wc( hmodel, c, sm->u1_wc) ;
sinterface_getu2_wc( hmodel, c, sm->u2_wc) ;
sinterface_getkz_wc(hmodel, c, sm->Kz_wc);
sm->depth_wc = -sm->botz_wc + sm->topz_wc;
/* get wc tracers */
for(n=0; n < param->ntr; n++) {
sed_tracer_t *tr = &sediment->mstracers[n];
si_gettracer_wc(hmodel, c, tr->n, tr->n_hd_wc, sm->ptr_wc) ;
sm->tr_srf_flux[n] = sinterface_get_srf_flux(hmodel, tr->name, c);
}
for(n=0; n < param->ntr; n++) {
sed_tracer_t *tr = &sediment->mstracers[n];
sm->tr_srf_flux[n] /= tr->u_scale;
for(k=sm->botk_wc;k<=sm->topk_wc;k++) {
sm->tr_wc[n][k] = *sm->ptr_wc[n][k]/tr->u_scale;
// NM: June 2017: tmp fix to get rid of small negative concentrations
// produced in EMS
if(sm->tr_wc[n][k] < 0) sm->tr_wc[n][k] = 0.;
}
}
/* Calculate sm->gridz_wc as it differs from window->gridz.
(for example water surface and top of the window->gridz
interface does not necessarily coincide, while in
mecosed the grid starts from the surface) */
if (sm->topk_wc > sm->botk_wc) {
sm->gridz_wc[sm->topk_wc+1] = sm->topz_wc ;
for(k=sm->topk_wc;k>=sm->botk_wc;k--)
sm->gridz_wc[k] = sm->gridz_wc[k+1] - sm->dz_wc[k];
for(k=sm->topk_wc;k>=sm->botk_wc;k--)
sm->gridvel_wc[k] = 0.;
}
else
{ /* If there is one layer in water column split it in two layers.
Note that only mecosed uses these two layers and
when sediment processing is complete these layers are again merged
into one layer and transferred to hydromodule */
if (sm->topk_wc == sm->botk_wc) {
kone=sm->topk_wc;
sm->botk_wc = 0;
sm->topk_wc = 1;
sm->gridz_wc[2] = sm->topz_wc ;
sm->gridz_wc[0] = sm->botz_wc;
sm->gridz_wc[1] = 0.5*(sm->gridz_wc[0]+sm->gridz_wc[2]);
for(k=0;k<2;k++) {
sm->dz_wc[k] = sm->gridz_wc[k+1] - sm->gridz_wc[k] ;
sm->u1_wc[k] = sm->u1_wc[kone];
sm->u2_wc[k] = sm->u2_wc[kone];
sm->Kz_wc[k] = sm->Kz_wc[kone];
sm->gridvel_wc[k] = 0.;
for(n=0; n < param->ntr; n++)
sm->tr_wc[n][k] =sm->tr_wc[n][kone];
}
}
}
for(k=sm->botk_wc;k<=sm->topk_wc;k++){
sm->dzold_wc[k] = sm->dz_wc[k];
sm->cellz_wc[k] = 0.5*(sm->gridz_wc[k]+ sm->gridz_wc[k+1]);
}
/* sedibed */
sm->botk_sed = 0;
sm->topk_sed = param->sednz - 2;
sinterface_getgridz_sed(hmodel, c, sm->gridz_sed);
for(k=0;k<param->sednz;k++) {
sm->dissol_kz[k] = (param->bi_dissol_kz_spv ? param->bi_dissol_kz_spv[col_index] : param->bi_dissol_kz);
sm->partic_kz[k] = (param->bt_partic_kz_spv ? param->bt_partic_kz_spv[col_index] : param->bt_partic_kz);
sm->css[k] = param->css * (param->css_scale_spv ? param->css_scale_spv[col_index] : param->css_scale);
sm->gridvel_sed[k] = 0.;
}
sm->dissol_kz_i = (param->bi_dissol_kz_i_spv ? param->bi_dissol_kz_i_spv[col_index] : param->bi_dissol_kz_i);
sm->partic_kz_i = (param->bt_partic_kz_i_spv ? param->bt_partic_kz_i_spv[col_index] : param->bt_partic_kz_i);
/* Recalculate */
sm->botz_sed = sm->gridz_sed[sm->botk_sed];
sm->topz_sed = sm->gridz_sed[sm->topk_sed+1];
sm->depth_sedi = - sm->botz_sed + sm->topz_sed;
for(k=0;k<param->sednz-1;k++) {
sm->dz_sed[k] = sm->gridz_sed[k+1] - sm->gridz_sed[k];
sm->dzold_sed[k] = sm->dz_sed[k];
sm->cellz_sed[k] = 0.5*(sm->gridz_sed[k]+ sm->gridz_sed[k+1]);
}
/*get sedibed tracers */
for(n=0; n < param->ntr; n++) {
sed_tracer_t *tr = &sediment->mstracers[n];
si_gettracer_sed(hmodel, c, tr->n, tr->n_hd_sed, sm->ptr_sed) ;
}
for(n=0; n < param->ntr; n++) {
sed_tracer_t *tr = &sediment->mstracers[n];
for(k=sm->botk_sed;k<=sm->topk_sed;k++) {
sm->tr_sed[n][k] = *sm->ptr_sed[n][k]/tr->u_scale;
//NMY: August 2017 tmp fix
if (sm->tr_sed[n][k] < 0) sm->tr_sed[n][k] = 0.;
}
}
update_porosity_wc(sediment, sm);
update_porosity_sed(sediment, sm);
//css varying with sediment depth
if (param->cssmode == 4) {
// FR 06-2015: Note we are no longer setting param->css here
double css = param->css_er_val[sm->topk_sed];
for(k=sm->topk_sed;k>=sm->botk_sed;k--)
if(sm->gridz_sed[sm->topk_sed+1] < param->css_er_depth[k])
css=param->css_er_val[k];
for(k=0;k<param->sednz;k++)
sm->css[k] = css * (param->css_scale_spv ? param->css_scale_spv[col_index] : param->css_scale);
}
/* subregions to handle spatially varying parameters */
if(param->nprmS)
for (m=0;m<param->nprmS;m++){
k=param->prmindexS[m];
n=param->trindexS[m];
*param->prmpointS[k][col_index] = sinterface_getvalueofBtracer(hmodel, n, c);
}
// FR 06-2015: Moved from sed_init:alloc_sed_spatial
{
sed_spatial_t *spatial = sediment->spatial;
double physriph = param->physriph;
double physripl = param->physripl;
if (param->physriph_spv != NULL)
physriph = param->physriph_spv[col_index];
if (param->physripl_spv != NULL)
physripl = param->physripl_spv[col_index];
spatial->hripples[col_index] = max(physriph, param->bioriph);
spatial->lripples[col_index] = max(physripl, param->bioripl);
}
// hardsubstrate
sm->hardsub_scale = 1.0;
if(param->hardsub_numb > -1) {
n = param->hardsub_numb;
sm->hardsub_scale = 0.01 * fabs(100.0 - sinterface_getvalueofBtracer(hmodel, n, c) );
}
// ships
if(param->ship_N) {
ship_prob = ( param->ship_T * param->ship_N ) / (24 * param->ship_C);
for(n=0;n<param->ship_C;n++) {
if(param->ship_i[n]==sm->i && param->ship_j[n]==sm->j) {
rab1= (1.*rand())/(RAND_MAX+1.);
if(rab1 < ship_prob) {
for(k=sm->botk_wc;k<=sm->topk_wc+1;k++)
sm->Kz_wc[k] += param->ship_Kz;
}
}
}
}
/* Print section */
if (param->verbose_sed == 2) {
mass_balance(sediment, sm);
if (sm->col_number==1 && param->nstep<2)
diagnostics(sediment,sm,"Diagnostics print out for column 1 and step 1; verbose_sed=2");
}
if (param->verbose_sed == 1) {
if (sm->col_number==1 && param->nstep<2) {
diagnostics(sediment,sm,"Diagnostics print out for column 1 and step 1; verbose_sed=1");
}
}
/* some checks */
if (sm->topk_wc < sm->botk_wc) {
/* MH 07/2012: included instability handling */
i_set_error(hmodel, sm->col_number, LFATAL, "sed:hd2sed: Error topk_wc < botk_wc \n");
/*sedtag(LFATAL,"sed:hd2sed:hd2sed", "Error topk_wc < botk_wc \n");*/
diagnostics(sediment,sm,"if(sm->topk_wc < sm->botk_wc)");
/*exit(1);*/
/* END MH */
}
if (sm->topz_wc < sm->botz_wc) {
/* MH 07/2012: included instability handling */
i_set_error(hmodel, sm->col_number, LFATAL, "sed:hd2sed: topz_wc < botz_wc \n");
/*sedtag(LFATAL,"sed:hd2sed:hd2sed", " topz_wc < botz_wc \n");*/
diagnostics(sediment,sm, "if (sm->topz_wc < sm->botz_wc)");
/*exit(1);*/
/* END MH */
}
if (fabs(sm->gridz_wc[sm->topk_wc+1] - sm->topz_wc) > 0.00001) {
/* MH 07/2012: included instability handling */
i_set_error(hmodel, sm->col_number, LFATAL, "sed:hd2sed: topz_wc =/= gridz_wc(topk_wc+1) \n");
/*sedtag(LFATAL,"sed:hd2sed:hd2sed", " topz_wc =/= gridz_wc(topk_wc+1) \n");*/
diagnostics(sediment,sm,"if (fabs(sm->gridz_wc[sm->topk_wc+1] - sm->topz_wc) > 0.00001)");
/*exit(1);*/
/* END MH */
}
if (fabs(sm->gridz_wc[sm->botk_wc] - sm->botz_wc)> 0.00001) {
/* MH 07/2012: included instability handling */
i_set_error(hmodel, sm->col_number, LFATAL, "sed:hd2sed: Error botz_wc =/= gridz_wc(botz_wc) \n");
/*sedtag(LFATAL,"sed:hd2sed:hd2sed", "Error botz_wc =/= gridz_wc(botz_wc) \n");*/
diagnostics(sediment,sm," if (fabs(sm->gridz_wc[sm->botk_wc] - sm->botz_wc)> 0.00001)");
/*exit(1);*/
/* END MH */
}
a=0.;
for(k=sm->botk_wc;k<=sm->topk_wc;k++)
a=a+sm->dz_wc[k];
if (fabs(a - sm->depth_wc) > 0.00001) {
sedtag(LWARN,"sed:hd2sed:hd2sed", "Sum(dz[k]) =/= sm->depth_wc col_numb= %d nstep=%d, c-index=%d, i=%d, j=%d \n",
sm->col_number, param->nstep, c, sm->i,sm->j);
/* MH 07/2012: included instability handling */
sprintf(etext, "sed:hd2sed: Sum(dz[k]) = %f, sm->depth_wc=%f \n", a, sm->depth_wc);
i_set_error(hmodel, sm->col_number, LFATAL, etext);
/*sedtag(LFATAL,"sed:hd2sed:hd2sed", "Sum(dz[k]) = %f, sm->depth_wc=%f \n",
a, sm->depth_wc);*/
diagnostics(sediment,sm,"Sum(dz[k]) =/= sm->depth_wc");
/*exit(1);*/
/* END MH */
}
for(k=sm->botk_wc;k<=sm->topk_wc;k++)
{
if ( sm->dz_wc[k] < 0.0) {
/* MH 07/2012: included instability handling */
sprintf(etext, "sed:hd2sed: dz_wc < 0, k=%d \n", k);
i_set_error(hmodel, sm->col_number, LFATAL, etext);
/*sedtag(LFATAL,"sed:hd2sed:hd2sed", " dz_wc < 0, k=%d \n", k);*/
diagnostics(sediment,sm," if ( sm->dz_wc[k] < 0.0)");
/*exit(1);*/
/* END MH */
}
}
for(k=sm->botk_sed;k<=sm->topk_sed;k++) {
if(sm->dz_sed[k] <= 0) {
/*fprintf(stderr, "dz_sed<=0"); */
/* MH 07/2012: included instability handling */
i_set_error(hmodel, sm->col_number, LFATAL, "sed:hd2sed: dz_sed<=0");
diagnostics(sediment,sm," if(sm->dz_sed[k] <= 0)");
/*exit(1);*/
/* END MH */
}
if(sm->por_sed[k] < 0) {
/* MH 07/2012: included instability handling */
i_set_error(hmodel, sm->col_number, LFATAL, "sed:hd2sed: por_sed<0");
/*fprintf(stderr, "por_sed<0"); */
diagnostics(sediment,sm," if(sm->por_sed[k] < 0)");
/*exit(1);*/
/* END MH */
}
if(sm->por_sed[k] >1) {
/* MH 07/2012: included instability handling */
i_set_error(hmodel, sm->col_number, LFATAL, "sed:hd2sed: por_sed>1");
/*fprintf(stderr, "por_sed>1"); */
diagnostics(sediment,sm," if(sm->por_sed[k] >1)");
/*exit(1);*/
/* END MH */
}
if(sm->partic_kz[k] <= 0) {
/* MH 07/2012: included instability handling */
i_set_error(hmodel, sm->col_number, LFATAL, "sed:hd2sed: kz_sed<=0");
/*fprintf(stderr, "kz_sed<=0"); */
diagnostics(sediment,sm," if(sm->partic_kz[k] <= 0) ");
/*exit(1);*/
/* END MH */
}
}
for(n=0; n < param->ntr; n++) {
sed_tracer_t *tr = &sediment->mstracers[n];
for(k=sm->botk_wc;k<=sm->topk_wc;k++) {
// if (!tr->prmspatial)
if (tr->diagn < 1 && sm->tr_wc[n][k] < 0.) {
sedtag(LWARN,"sed:hd2sed:hd2sed",
" Negative tracer input in wc n=%d \n", n);
/* MH 07/2012: included instability handling */
sprintf(etext, "sed:hd2sed: k=%d tiopk=%d botk=%d topz=%f botz=%f col_numb=%d nstep=%d \n",
k, sm->topk_wc, sm->botk_wc, sm->topz_wc, sm->botz_wc, sm->col_number, param->nstep);
i_set_error(hmodel, sm->col_number, LFATAL, etext);
diagnostics(sediment,sm," if (tr->diagn < 1 && sm->tr_wc[n][k] < 0.)");
/* END MH */
}
if (tr->diagn < 1 && finite(sm->tr_wc[n][k])==0 ) {
sedtag(LWARN,"sed:hd2sed:hd2sed",
" NaN or INF tracer input into mecosed wc-layer n=%d \n", n);
/* MH 07/2012: included instability handling */
sprintf(etext, "sed:hd2sed: k=%d tiopk=%d botk=%d topz=%f botz=%f col_numb=%d nstep=%d \n",
k, sm->topk_wc, sm->botk_wc, sm->topz_wc, sm->botz_wc, sm->col_number, param->nstep);
i_set_error(hmodel, sm->col_number, LFATAL, etext);
diagnostics(sediment,sm,"if (tr->diagn < 1 && finite(sm->tr_wc[n][k])==0 ) ");
/* END MH */
}
} /* end for k=sm->botk_wc */
for(k=sm->botk_sed;k<=sm->topk_sed;k++) {
// if (!tr->prmspatial)
if (tr->diagn < 1 && sm->tr_sed[n][k] < 0.) {
sedtag(LWARN,"sed:hd2sed:hd2sed",
"Negative tracer input in sed n=%d \n", n);
/* MH 07/2012: included instability handling */
sprintf(etext, "sed:hd2sed: k=%d tiopk=%d botk=%d topz=%f botz=%f col_numb= %d nstep=%d\n",
k, sm->topk_sed, sm->botk_sed, sm->topz_sed, sm->botz_sed, sm->col_number, param->nstep);
i_set_error(hmodel, sm->col_number, LFATAL, etext);
diagnostics(sediment,sm," if (tr->diagn < 1 && sm->tr_sed[n][k] < 0.) ");
/* END MH */
}
if (tr->diagn < 1 && finite(sm->tr_sed[n][k])==0 ) {
sedtag(LWARN,"sed:hd2sed:hd2sed",
"NaN or INF tracer input into mecosed: sed-layer n=%d \n", n);
/* MH 07/2012: included instability handling */
sprintf(etext, "sed:hd2sed: k=%d tiopk=%d botk=%d topz=%f botz=%f col_numb= %d nstep=%d\n",
k, sm->topk_sed, sm->botk_sed, sm->topz_sed, sm->botz_sed, sm->col_number, param->nstep);
i_set_error(hmodel, sm->col_number, LFATAL, etext);
diagnostics(sediment,sm, "if (tr->diagn < 1 && finite(sm->tr_sed[n][k])==0 ) ");
/* END MH */
}
} /* end for k=sm->botk_sed */
}
}
/***************************************************************/
// spatial->cbnm1[n][c] holds concentration of a tracer n in the
// top sediment layer integrated over a number of previous time steps
// to get rid of hf oscillations when eroding sediments
// Feb 2015 - hripples amd lripples are redefined in bbl.c !
static void hd2sed_internal(sediment_t *sediment, sed_column_t *sm, int c)
{
int n;
sed_params_t *param = sediment->msparam;
sed_spatial_t *spatial = sediment->spatial;
sm->theta=spatial->theta[c];
sm->hripples = spatial->hripples[c];
sm->lripples = spatial->lripples[c];
for (n = 0; n < param->ntr; n++) {
sm->cbnm1[n] = spatial->cbnm1[n][c];
sm->cbnm2[n] = spatial->cbnm2[n][c];
sm->cbnm3[n] = spatial->cbnm3[n][c];
sm->cbnm4[n] = spatial->cbnm4[n][c];
}
}
/***************************************************************/
void diagnostics(sediment_t *sediment, sed_column_t *sm, const char *comment)
{
int kone, k, n;
sed_params_t *param = sediment->msparam;
int col_index;
FILE *flog;
double a;
/* Print section */
#ifdef HAVE_OMP
#pragma omp critical
{
#endif
if (param->verbose_sed) {
flog = fopen("sedlog.txt","a");
fprintf(flog, " \n hd2sed \n \n");
fprintf(flog, " %s \n",comment);
fprintf(flog, "sm->i=%d, sm->j=%d, sm->hd_col_index=%d \n"
,sm->i,sm->j,sm->hd_col_index);
fprintf(flog, "sm->hd_col_index=%d, sm->col_number=%d, param->nstep=%d \n"
,sm->hd_col_index,sm->col_number,param->nstep);
fprintf(flog, "sm->botk_wc=%d, sm->topk_wc=%d \n"
,sm->botk_wc,sm->topk_wc);
fprintf(flog, "sm->botz_wc=%f, sm->topz_wc=%f \n \n"
,sm->botz_wc,sm->topz_wc);
for(k=sm->botk_wc;k<=sm->topk_wc+1;k++)
fprintf(flog, "k=%d, sm->gridz_wc=%f, sm->Kz=%f \n",
k, sm->gridz_wc[k], sm->Kz_wc[k]);
fprintf(flog, "\n");
for(k=sm->botk_wc;k<=sm->topk_wc;k++)
fprintf(flog, "k=%d, sm->dz_wc=%f, sm->cellz_wc=%f \n",
k, sm->dz_wc[k], sm->cellz_wc[k]);
fprintf(flog, "\n");
for(k=sm->botk_wc;k<=sm->topk_wc;k++)
fprintf(flog, "k=%d, sm->u1_wc=%f, sm->u2_wc=%f, \n",
k ,sm->u1_wc[k],sm->u2_wc[k]);
fprintf(flog, "\n");
for(n=0; n < param->ntr; n++) {
for(k=sm->botk_wc;k<=sm->topk_wc;k++) {
fprintf(flog, "n=%d, k=%d, sm->tr_wc[n][k] = %f \n",
n, k, sm->tr_wc[n][k]);
}
fprintf(flog, "\n");
}
fprintf(flog, " \n ");
fprintf(flog, "sm->botk_sed=%d, sm->topk_sed=%d \n"
,sm->botk_sed,sm->topk_sed);
fprintf(flog, "sm->botz_sed=%f, sm->topz_sed=%f \n"
,sm->botz_sed,sm->topz_sed);
fprintf(flog, "\n");
for(k=sm->botk_sed;k<=sm->topk_sed+1;k++)
fprintf(flog, "k=%d, sm->gridz_sed=%f, sm->dissol_kz=%e, sm->partic_kz=%e\n", k, sm->gridz_sed[k], sm->dissol_kz[k], sm->partic_kz[k]);
fprintf(flog, "\n");
for(k=sm->botk_sed;k<=sm->topk_sed;k++)
fprintf(flog, "k=%d, sm->dz_sed=%f, sm->cellz_sed=%f, sm->por_sed=%f \n", k, sm->dz_sed[k], sm->cellz_sed[k] ,sm->por_sed[k]);
fprintf(flog, "\n");
for(n=0; n < param->ntr; n++) {
for(k=sm->botk_sed;k<=sm->topk_sed;k++) {
fprintf(flog, "n=%d, k=%d, sm->tr_sed[n][k] = %f \n",
n, k, sm->tr_sed[n][k]);
}
fprintf(flog, "\n");
}
fprintf(flog, "\n");
fprintf(flog, "sm->depth_wc=%f, sm->depth_sedi=%f \n"
,sm->depth_wc,sm->depth_sedi);
fclose(flog);
}
#ifdef HAVE_OMP
}
#endif
} //end diagnostics
/*****************/
void mass_balance(sediment_t *sediment, sed_column_t *sm)
{
int k, n;
sed_params_t *param = sediment->msparam;
FILE *flog;
/* Print section */
// ********* 2010 sediment mass balance check before and after
// every sediment cycle
#ifdef HAVE_OMP
#pragma omp critical
{
#endif
// initialise
if(sm->col_number == 1 && sm->sed_start == 1) {
for(n=0; n < param->ntr; n++) {
sed_tracer_t *tr = &sediment->mstracers[n];
tr->mass_start_wc = 0;
tr->mass_start_sed = 0;
tr->mass_end_wc = 0;
tr->mass_end_sed = 0;
}
}
// mass at the beiginning of the sed cycle
if(sm->sed_start == 1) {
for(n=0; n < param->ntr; n++) {
sed_tracer_t *tr = &sediment->mstracers[n];
for(k=sm->botk_wc;k<=sm->topk_wc;k++) {
if(tr->dissol)
tr->mass_start_wc += sm->tr_wc[n][k]*sm->dz_wc[k]*sm->por_wc[k]*sm->area/1e6;
else
tr->mass_start_wc += sm->tr_wc[n][k]*sm->dz_wc[k]*sm->area/1e6;
}
for(k=sm->botk_sed;k<=sm->topk_sed;k++) {
if(tr->dissol)
tr->mass_start_sed += sm->tr_sed[n][k]*sm->dz_sed[k]*sm->por_sed[k]*sm->area/1e6;
else
tr->mass_start_sed += sm->tr_sed[n][k]*sm->dz_sed[k]*sm->area/1e6;
}
}
}
// mass after the sed cycle
if(sm->sed_start == 0) {
for(n=0; n < param->ntr; n++) {
sed_tracer_t *tr = &sediment->mstracers[n];
for(k=sm->botk_wc;k<=sm->topk_wc;k++) {
if(tr->dissol)
tr->mass_end_wc += sm->tr_wc[n][k]*sm->dz_wc[k]*sm->por_wc[k]*sm->area/1e6;
else
tr->mass_end_wc += sm->tr_wc[n][k]*sm->dz_wc[k]*sm->area/1e6;
}
for(k=sm->botk_sed;k<=sm->topk_sed;k++) {
if(tr->dissol)
tr->mass_end_sed += sm->tr_sed[n][k]*sm->dz_sed[k]*sm->por_sed[k]*sm->area/1e6;
else
tr->mass_end_sed += sm->tr_sed[n][k]*sm->dz_sed[k]*sm->area/1e6;
}
}
}
// print the mass balance
if(sm->col_number == sediment->msparam->ncol && sm->sed_start == 0) {
flog = fopen("sed_mass_start.txt","a");
fprintf(flog,"%f ", param->t);
for(n=0; n < param->ntr; n++) {
sed_tracer_t *tr = &sediment->mstracers[n];
fprintf(flog, "%f ", tr->mass_start_wc + tr->mass_start_sed);
}
fprintf(flog, "\n");
fclose(flog);
/**/
flog = fopen("sed_mass_end.txt","a");
fprintf(flog,"%f ", param->t);
for(n=0; n < param->ntr; n++) {
sed_tracer_t *tr = &sediment->mstracers[n];
fprintf(flog, "%f ", tr->mass_end_wc + tr->mass_end_sed);
}
fprintf(flog, "\n");
fclose(flog);
/**/
flog = fopen("sed_mass_diff.txt","a");
fprintf(flog,"%f ", param->t);
for(n=0; n < param->ntr; n++) {
sed_tracer_t *tr = &sediment->mstracers[n];
fprintf(flog, "%e ", tr->mass_end_wc + tr->mass_end_sed - tr->mass_start_wc - tr->mass_start_sed);
}
fprintf(flog, "\n");
fclose(flog);
}
//*************
#ifdef HAVE_OMP
}
#endif
} //end mass_balance
/************************/
// scales css and benthic diff coefficients with depth
// invoked via REEF_SCALE_DEPTH key
void reef_scale_depth(sediment_t *sediment, sed_column_t *sm)
{
sed_params_t *param = sediment->msparam;
int k;
double rab1,rab2, mindep,maxdep;
mindep=20.;
maxdep=param->reef_scale_depth;
if(maxdep < 1e-11)
return;
/* eReefs scaling with water depth */
if (sm->depth_wc < mindep) {
rab1=1;
rab2=1;
}
else
if (sm->depth_wc>=mindep && sm->depth_wc<maxdep ) {
rab1=1-(sm->depth_wc - mindep)/(maxdep-mindep);
rab2=1+3*(sm->depth_wc - mindep)/(maxdep-mindep);
}
else {
rab1=0.;
rab2=4.;
}
for(k=0;k<param->sednz;k++) {
// 2019 May
// sm->dissol_kz[k] = sm->dissol_kz[k]*rab1+1e-25;
// sm->partic_kz[k] = sm->partic_kz[k]*rab1+1e-25;
sm->css[k] = sm->css[k]*rab2;
}
sm->dissol_kz_i = sm->dissol_kz_i*(rab1+0.1);
sm->partic_kz_i = sm->partic_kz_i*(rab1+0.1);
// sm->hripples = param->physriph * (rab1+0.1);
}
/* end eReef scaling */
#endif
#ifdef __cplusplus
}
#endif