-
Notifications
You must be signed in to change notification settings - Fork 32
Expand file tree
/
Copy pathsoil.jl
More file actions
1440 lines (1331 loc) · 49.6 KB
/
Copy pathsoil.jl
File metadata and controls
1440 lines (1331 loc) · 49.6 KB
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
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
abstract type AbstractSoilModel end
"Struct for storing SBM soil model variables"
@with_kw struct SbmSoilVariables{N}
n::Int
# Calculated soil water pressure head h3 of the root water uptake reduction function (Feddes) [cm]
h3::Vector{Float64} = fill(MISSING_VALUE, n)
# Unsaturated store capacity [mm]
ustorecapacity::Vector{Float64}
# Amount of water in the unsaturated store, per layer [mm]
ustorelayerdepth::Vector{SVector{N, Float64}}
# Thickness of unsaturated zone, per layer [mm]
ustorelayerthickness::Vector{SVector{N, Float64}}
# Saturated store [mm]
satwaterdepth::Vector{Float64}
# Drainable water store [mm]
drainable_waterdepth::Vector{Float64}
# Pseudo-water table depth [mm] (top of the saturated zone)
zi::Vector{Float64}
# Number of unsaturated soil layers
n_unsatlayers::Vector{Int}
# Transpiration [mm Δt⁻¹]
transpiration::Vector{Float64} = fill(MISSING_VALUE, n)
# Actual evaporation from unsaturated store [mm Δt⁻¹]
ae_ustore::Vector{Float64} = fill(MISSING_VALUE, n)
# Soil evaporation from unsaturated and saturated store [mm Δt⁻¹]
soilevap::Vector{Float64} = fill(MISSING_VALUE, n)
# Soil evaporation from saturated store [mm Δt⁻¹]
soilevapsat::Vector{Float64} = fill(MISSING_VALUE, n)
# Actual capillary rise [mm Δt⁻¹]
actcapflux::Vector{Float64} = fill(MISSING_VALUE, n)
# Actual transpiration from saturated store [mm Δt⁻¹]
actevapsat::Vector{Float64} = fill(MISSING_VALUE, n)
# Total actual evapotranspiration [mm Δt⁻¹]
actevap::Vector{Float64} = fill(MISSING_VALUE, n)
# Actual infiltration into the unsaturated zone [mm Δt⁻¹]
actinfilt::Vector{Float64} = fill(MISSING_VALUE, n)
# Actual infiltration non-compacted fraction [mm Δt⁻¹]
actinfiltsoil::Vector{Float64} = fill(MISSING_VALUE, n)
# Actual infiltration compacted fraction [mm Δt⁻¹]
actinfiltpath::Vector{Float64} = fill(MISSING_VALUE, n)
# Actual infiltration (compacted and the non-compacted areas) [mm Δt⁻¹]
infiltsoilpath::Vector{Float64} = fill(MISSING_VALUE, n)
# Infiltration excess water [mm Δt⁻¹]
infiltexcess::Vector{Float64} = fill(MISSING_VALUE, n)
# Infiltration from surface water [mm Δt⁻¹]
infilt_surfacewater::Vector{Float64} = fill(0.0, n)
# Potential infiltration originating from surface water [mm Δt⁻¹]
potential_infiltration_surfacewater::Vector{Float64} = fill(0.0, n)
# Total water available for infiltration [mm Δt⁻¹]
potential_infiltration::Vector{Float64} = fill(0.0, n)
# Water that cannot infiltrate due to saturated soil (saturation excess) [mm Δt⁻¹]
excesswater::Vector{Float64} = fill(MISSING_VALUE, n)
# Water exfiltrating during saturation excess conditions [mm Δt⁻¹]
exfiltsatwater::Vector{Float64} = fill(MISSING_VALUE, n)
# Excess water for non-compacted fraction [mm Δt⁻¹]
excesswatersoil::Vector{Float64} = fill(MISSING_VALUE, n)
# Excess water for compacted fraction [mm Δt⁻¹]
excesswaterpath::Vector{Float64} = fill(MISSING_VALUE, n)
# Total surface runoff from infiltration and saturation excess (excluding actual open water evaporation) [mm Δt⁻¹]
runoff::Vector{Float64} = fill(MISSING_VALUE, n)
# Net surface runoff (surface runoff - actual open water evaporation) [mm Δt⁻¹]
net_runoff::Vector{Float64} = fill(MISSING_VALUE, n)
# Volumetric water content [-] per soil layer (including theta_r and saturated zone)
vwc::Vector{SVector{N, Float64}}
# Volumetric water content [%] per soil layer (including theta_r and saturated zone)
vwc_perc::Vector{SVector{N, Float64}}
# Root water storage [mm] in unsaturated and saturated zone (excluding theta_r)
rootstore::Vector{Float64} = fill(MISSING_VALUE, n)
# Volumetric water content [-] in root zone (including theta_r and saturated zone)
vwc_root::Vector{Float64} = fill(MISSING_VALUE, n)
# Volumetric water content [%] in root zone (including theta_r and saturated zone)
vwc_percroot::Vector{Float64} = fill(MISSING_VALUE, n)
# Amount of available water in the unsaturated zone [mm]
ustoredepth::Vector{Float64} = zeros(n)
# Downward flux from unsaturated to saturated zone [mm Δt⁻¹]
transfer::Vector{Float64} = fill(MISSING_VALUE, n)
# Net recharge to saturated store [mm Δt⁻¹]
recharge::Vector{Float64} = fill(MISSING_VALUE, n)
# Actual leakage from saturated store [mm Δt⁻¹]
actleakage::Vector{Float64} = fill(MISSING_VALUE, n)
# Total water storage (excluding floodplain volume and reservoirs) [mm]
total_storage::Vector{Float64} = zeros(n)
# Total soil water storage [mm]
total_soilwater_storage::Vector{Float64}
# Top soil temperature [ᵒC]
tsoil::Vector{Float64} = fill(10.0, n)
# Soil infiltration reduction factor (when soil is frozen) [-]
f_infiltration_reduction::Vector{Float64} = ones(n)
end
"Struct for storing SBM soil model parameters"
@with_kw struct SbmSoilParameters{N, M, Kv}
# Maximum number of soil layers [-]
maxlayers::Int
# Number of soil layers [-]
nlayers::Vector{Int}
# Saturated water content (porosity) [-]
theta_s::Vector{Float64}
# Residual water content [-]
theta_r::Vector{Float64}
# Field capacity water content [-]
theta_fc::Vector{Float64}
# Soilwater capacity [mm]
soilwatercapacity::Vector{Float64}
# Multiplication factor [-] applied to kv_z (vertical flow)
kvfrac::Vector{SVector{N, Float64}}
# Air entry pressure [cm] of soil (Brooks-Corey)
hb::Vector{Float64}
# Soil thickness [mm]
soilthickness::Vector{Float64}
# Thickness of soil layers [mm]
act_thickl::Vector{SVector{N, Float64}}
# Cumulative sum of soil layers [mm], starting at soil surface (0)
sumlayers::Vector{SVector{M, Float64}}
# Infiltration capacity of the compacted areas [mm Δt⁻¹]
infiltcappath::Vector{Float64}
# Soil infiltration capacity [mm Δt⁻¹]
infiltcapsoil::Vector{Float64}
# Maximum leakage [mm Δt⁻¹] from saturated zone
maxleakage::Vector{Float64}
# Parameter [mm] controlling capillary rise
cap_hmax::Vector{Float64}
# Coefficient [-] controlling capillary rise
cap_n::Vector{Float64}
# Brooks-Corey power coefficient [-] for each soil layer
c::Vector{SVector{N, Float64}}
# Soil temperature smooth factor [-]
w_soil::Vector{Float64}
# Controls soil infiltration reduction factor when soil is frozen [-]
cf_soil::Vector{Float64}
# Fraction of compacted area [-]
pathfrac::Vector{Float64}
# Controls how roots are linked to water table [-]
rootdistpar::Vector{Float64}
# Fraction of the root length density in each soil layer [-]
rootfraction::Vector{SVector{N, Float64}}
# Soil water pressure head h1 of the root water uptake reduction function (Feddes) [cm]
h1::Vector{Float64}
# Soil water pressure head h2 of the root water uptake reduction function (Feddes) [cm]
h2::Vector{Float64}
# Soil water pressure head h3_high of the root water uptake reduction function (Feddes) [cm]
h3_high::Vector{Float64}
# Soil water pressure head h3_low of the root water uptake reduction function (Feddes) [cm]
h3_low::Vector{Float64}
# Soil water pressure head h4 of the root water uptake reduction function (Feddes) [cm]
h4::Vector{Float64}
# Root water uptake reduction at soil water pressure head h1 (0.0 or 1.0) [-]
alpha_h1::Vector{Float64}
# Soil fraction [-]
soil_fraction::Vector{Float64}
# Vertical hydraulic conductivity profile type
kv_profile::Kv
# Vegetation parameter set
vegetation_parameter_set::VegetationParameters
end
"Initialize SBM soil model variables"
function SbmSoilVariables(n::Int, parameters::SbmSoilParameters)
(;
soilthickness,
maxlayers,
act_thickl,
sumlayers,
soilwatercapacity,
theta_s,
theta_r,
theta_fc,
) = parameters
satwaterdepth = 0.85 .* soilwatercapacity # cold state value for satwaterdepth
ustoredepth = zeros(n)
zi = @. max(0.0, soilthickness - satwaterdepth / (theta_s - theta_r))
drainable_waterdepth =
@. (soilthickness - zi) * lower_bound_drainable_porosity(theta_s, theta_fc)
ustorelayerthickness = set_layerthickness.(zi, sumlayers, act_thickl)
n_unsatlayers = number_of_active_layers.(ustorelayerthickness)
vwc = fill(MISSING_VALUE, maxlayers, n)
vwc_perc = fill(MISSING_VALUE, maxlayers, n)
total_soilwater_storage = satwaterdepth .+ ustoredepth
vars = SbmSoilVariables(;
n,
ustorelayerdepth = zero(act_thickl),
ustorecapacity = soilwatercapacity .- satwaterdepth,
ustorelayerthickness,
satwaterdepth,
drainable_waterdepth,
zi,
n_unsatlayers,
vwc = svectorscopy(vwc, Val{maxlayers}()),
vwc_perc = svectorscopy(vwc_perc, Val{maxlayers}()),
total_soilwater_storage,
)
return vars
end
"Struct for storing SBM soil model boundary conditions"
@with_kw struct SbmSoilBC
n::Int
# Water flux at the soil surface [mm Δt⁻¹]
water_flux_surface::Vector{Float64} = fill(MISSING_VALUE, n)
# Potential transpiration rate [mm Δt⁻¹]
potential_transpiration::Vector{Float64} = fill(MISSING_VALUE, n)
# Potential soil evaporation rate [mm Δt⁻¹]
potential_soilevaporation::Vector{Float64} = fill(MISSING_VALUE, n)
end
"Exponential depth profile of vertical hydraulic conductivity at the soil surface"
struct KvExponential
# Vertical hydraulic conductivity [mm Δt⁻¹] at soil surface
kv_0::Vector{Float64}
# A scaling parameter [mm⁻¹] (controls exponential decline of kv_0)
f::Vector{Float64}
end
"Exponential constant depth profile of vertical hydraulic conductivity"
struct KvExponentialConstant
exponential::KvExponential
# Depth [mm] from soil surface for which exponential decline of kv_0 is valid
z_exp::Vector{Float64}
end
"Layered depth profile of vertical hydraulic conductivity"
struct KvLayered{N}
# Vertical hydraulic conductivity [mm Δt⁻¹] per soil layer
kv::Vector{SVector{N, Float64}}
end
"Layered exponential depth profile of vertical hydraulic conductivity"
struct KvLayeredExponential{N}
# A scaling parameter [mm⁻¹] (controls exponential decline of kv_0)
f::Vector{Float64}
# Vertical hydraulic conductivity [mm Δt⁻¹] per soil layer
kv::Vector{SVector{N, Float64}}
# Number of soil layers [-] with vertical hydraulic conductivity value `kv`
nlayers_kv::Vector{Int}
# Depth [mm] from soil surface for which layered profile is valid
z_layered::Vector{Float64}
end
"Initialize SBM soil model hydraulic conductivity depth profile"
function sbm_kv_profiles(
dataset::NCDataset,
config::Config,
indices::Vector{CartesianIndex{2}},
kv_0::Vector{Float64},
f::Vector{Float64},
maxlayers::Int,
nlayers::Vector{Int},
sumlayers::Vector,
dt::Second,
)
kv_profile_type = config.model.saturated_hydraulic_conductivity_profile
n = length(indices)
if kv_profile_type == VerticalConductivityProfile.exponential
kv_profile = KvExponential(kv_0, f)
elseif kv_profile_type == VerticalConductivityProfile.exponential_constant
z_exp = ncread(
dataset,
config,
"soil_exponential_vertical_saturated_hydraulic_conductivity_profile_below_surface__depth";
optional = false,
sel = indices,
type = Float64,
)
exp_profile = KvExponential(kv_0, f)
kv_profile = KvExponentialConstant(exp_profile, z_exp)
elseif kv_profile_type == VerticalConductivityProfile.layered ||
kv_profile_type == VerticalConductivityProfile.layered_exponential
kv =
ncread(
dataset,
config,
"soil_layer_water__vertical_saturated_hydraulic_conductivity";
optional = false,
sel = indices,
type = Float64,
dimname = :layer,
) .* (dt / BASETIMESTEP)
if size(kv, 1) != maxlayers
parname = param(
config.input.static,
"soil_layer_water__vertical_saturated_hydraulic_conductivity",
)
size1 = size(kv, 1)
error("$parname needs a layer dimension of size $maxlayers, but is $size1")
end
if kv_profile_type == VerticalConductivityProfile.layered
kv_profile = KvLayered(svectorscopy(kv, Val{maxlayers}()))
else
z_layered = ncread(
dataset,
config,
"soil_layered_vertical_saturated_hydraulic_conductivity_profile_below_surface__depth";
optional = false,
sel = indices,
type = Float64,
)
nlayers_kv = fill(0, n)
for i in eachindex(nlayers_kv)
layers = @view sumlayers[i][2:nlayers[i]]
_, k = findmin(abs.(z_layered[i] .- layers))
nlayers_kv[i] = k
z_layered[i] = layers[k]
end
kv_profile = KvLayeredExponential(
f,
svectorscopy(kv, Val{maxlayers}()),
nlayers_kv,
z_layered,
)
end
end
return kv_profile
end
"Initialize SBM soil model parameters"
function SbmSoilParameters(
dataset::NCDataset,
config::Config,
vegetation_parameter_set::VegetationParameters,
indices::Vector{CartesianIndex{2}},
dt::Second,
)
config_soil_layer_thickness = config.model.soil_layer__thickness
soil_layer_thickness =
SVector(Tuple(push!(Float64.(config_soil_layer_thickness), MISSING_VALUE)))
cum_depth_layers = pushfirst(cumsum(soil_layer_thickness), 0.0)
maxlayers = length(soil_layer_thickness) # max number of soil layers
@info "Using `$(maxlayers - 1)` soil layers with the following thickness: `$config_soil_layer_thickness`"
w_soil =
ncread(
dataset,
config,
"soil_surface_temperature__weight_coefficient";
sel = indices,
defaults = 0.1125,
type = Float64,
) .* (dt / BASETIMESTEP)
cf_soil = ncread(
dataset,
config,
"soil_surface_water__infiltration_reduction_parameter";
sel = indices,
defaults = 0.038,
type = Float64,
)
# soil parameters
theta_s = ncread(
dataset,
config,
"soil_water__saturated_volume_fraction";
optional = false,
sel = indices,
type = Float64,
)
theta_r = ncread(
dataset,
config,
"soil_water__residual_volume_fraction";
optional = false,
sel = indices,
type = Float64,
)
kv_0 =
ncread(
dataset,
config,
"soil_surface_water__vertical_saturated_hydraulic_conductivity";
optional = false,
sel = indices,
type = Float64,
) .* (dt / BASETIMESTEP)
f = ncread(
dataset,
config,
"soil_water__vertical_saturated_hydraulic_conductivity_scale_parameter";
optional = false,
sel = indices,
type = Float64,
)
hb = ncread(
dataset,
config,
"soil_water__air_entry_pressure_head";
sel = indices,
defaults = -10.0,
type = Float64,
)
h1 = ncread(
dataset,
config,
"vegetation_root__feddes_critical_pressure_head_h1";
sel = indices,
defaults = 0.0,
type = Float64,
)
h2 = ncread(
dataset,
config,
"vegetation_root__feddes_critical_pressure_head_h2";
sel = indices,
defaults = -100.0,
type = Float64,
)
h3_high = ncread(
dataset,
config,
"vegetation_root__feddes_critical_pressure_head_h3_high";
sel = indices,
defaults = -400.0,
type = Float64,
)
h3_low = ncread(
dataset,
config,
"vegetation_root__feddes_critical_pressure_head_h3_low";
sel = indices,
defaults = -1000.0,
type = Float64,
)
h4 = ncread(
dataset,
config,
"vegetation_root__feddes_critical_pressure_head_h4";
sel = indices,
defaults = -16000.0,
type = Float64,
)
alpha_h1 = ncread(
dataset,
config,
"vegetation_root__feddes_critical_pressure_head_h1_reduction_coefficient";
sel = indices,
defaults = 1.0,
type = Float64,
)
soilthickness = ncread(
dataset,
config,
"soil__thickness";
optional = false,
sel = indices,
type = Float64,
)
infiltcappath =
ncread(
dataset,
config,
"compacted_soil_surface_water__infiltration_capacity";
sel = indices,
defaults = 10.0,
type = Float64,
) .* (dt / BASETIMESTEP)
maxleakage =
ncread(
dataset,
config,
"soil_water_saturated_zone_bottom__max_leakage_volume_flux";
sel = indices,
defaults = 0.0,
type = Float64,
) .* (dt / BASETIMESTEP)
c = ncread(
dataset,
config,
"soil_layer_water__brooks_corey_exponent";
optional = false,
sel = indices,
type = Float64,
dimname = :layer,
)
if size(c, 1) != maxlayers
parname = param(config.input.static, "soil_layer_water__brooks_corey_exponent")
size1 = size(c, 1)
error("$parname needs a layer dimension of size $maxlayers, but is $size1")
end
c = svectorscopy(c, Val{maxlayers}())
kvfrac = ncread(
dataset,
config,
"soil_layer_water__vertical_saturated_hydraulic_conductivity_factor";
sel = indices,
defaults = 1.0,
type = Float64,
dimname = :layer,
)
if size(kvfrac, 1) != maxlayers
parname = param(
config.input.static,
"soil_layer_water__vertical_saturated_hydraulic_conductivity_factor",
)
size1 = size(kvfrac, 1)
error("$parname needs a layer dimension of size $maxlayers, but is $size1")
end
# soil infiltration capacity based on kv_0 and kvfrac upper soil layer
infiltcapsoil = kv_0 .* @view kvfrac[1, :]
# fraction compacted area
pathfrac = ncread(
dataset,
config,
"compacted_soil__area_fraction";
optional = false,
sel = indices,
type = Float64,
)
# vegetation parameters
rootdistpar = ncread(
dataset,
config,
"soil_wet_root__sigmoid_function_shape_parameter";
sel = indices,
defaults = -500.0,
type = Float64,
)
cap_hmax = ncread(
dataset,
config,
"soil_water_saturated_zone_top__capillary_rise_max_water_table_depth";
sel = indices,
defaults = 2000.0,
type = Float64,
)
cap_n = ncread(
dataset,
config,
"soil_water_saturated_zone_top__capillary_rise_averianov_exponent";
sel = indices,
defaults = 2.0,
type = Float64,
)
act_thickl =
set_layerthickness.(soilthickness, (cum_depth_layers,), (soil_layer_thickness,))
sumlayers = @. pushfirst(cumsum(act_thickl), 0.0)
nlayers = number_of_active_layers.(act_thickl)
if haskey(config.input.static, "soil_water__field_capacity_volume_fraction")
theta_fc = ncread(
dataset,
config,
"soil_water__field_capacity_volume_fraction";
optional = false,
sel = indices,
type = Float64,
)
else
theta_fc = field_capacity.(act_thickl, nlayers, theta_s, theta_r, c, hb)
end
# optional root fraction
rootfraction_name = "soil_root__length_density_fraction"
if haskey(config.input.static, rootfraction_name)
rootfraction = ncread(
dataset,
config,
rootfraction_name;
optional = false,
sel = indices,
type = Float64,
dimname = :layer,
)
else
n = length(indices)
(; rootingdepth) = vegetation_parameter_set
# default root fraction
rootfraction = zeros(maxlayers, n)
for i in 1:n
if rootingdepth[i] > 0.0
for k in 1:maxlayers
if (rootingdepth[i] - sumlayers[i][k]) >= act_thickl[i][k]
rootfraction[k, i] = act_thickl[i][k] / rootingdepth[i]
else
rootfraction[k, i] =
max(rootingdepth[i] - sumlayers[i][k], 0.0) / rootingdepth[i]
end
end
end
end
end
kv_profile = sbm_kv_profiles(
dataset,
config,
indices,
kv_0,
f,
maxlayers,
nlayers,
sumlayers,
dt,
)
soilwatercapacity = @. soilthickness * (theta_s - theta_r)
n = length(indices)
sbm_params = SbmSoilParameters(;
maxlayers,
nlayers,
soilwatercapacity,
theta_s,
theta_r,
theta_fc,
kvfrac = svectorscopy(kvfrac, Val{maxlayers}()),
hb,
h1,
h2,
h3_high,
h3_low,
h4,
alpha_h1,
soilthickness,
act_thickl,
sumlayers,
infiltcappath,
infiltcapsoil,
maxleakage,
pathfrac,
rootdistpar,
rootfraction = svectorscopy(rootfraction, Val{maxlayers}()),
cap_hmax,
cap_n,
c,
w_soil,
cf_soil,
soil_fraction = fill(MISSING_VALUE, n),
kv_profile,
vegetation_parameter_set,
)
return sbm_params
end
"SBM soil model"
@with_kw struct SbmSoilModel{N, M, Kv} <: AbstractSoilModel
n::Int
boundary_conditions::SbmSoilBC = SbmSoilBC(; n)
parameters::SbmSoilParameters{N, M, Kv}
variables::SbmSoilVariables{N}
end
"Initialize SBM soil model"
function SbmSoilModel(
dataset::NCDataset,
config::Config,
vegetation_parameter_set::VegetationParameters,
indices::Vector{CartesianIndex{2}},
dt::Second,
)
n = length(indices)
parameters = SbmSoilParameters(dataset, config, vegetation_parameter_set, indices, dt)
variables = SbmSoilVariables(n, parameters)
soil_model = SbmSoilModel(; n, parameters, variables)
return soil_model
end
"Return soil fraction"
function soil_fraction!(
soil_model::AbstractSoilModel,
glacier_model::AbstractGlacierModel,
parameters::LandParameters,
)
(; canopygapfraction) = soil_model.parameters.vegetation_parameter_set
(; soil_fraction) = soil_model.parameters
(; water_fraction, river_fraction) = parameters
glacier_fraction = get_glacier_fraction(glacier_model)
@. soil_fraction =
max(canopygapfraction - water_fraction - river_fraction - glacier_fraction, 0.0)
return nothing
end
"Update boundary conditions of the SBM soil model for a single timestep"
function update_bc_soil_model!(
soil_model::SbmSoilModel,
atmospheric_forcing::AtmosphericForcing,
external_models::NamedTuple,
)
(; interception, runoff, demand, allocation) = external_models
(; potential_transpiration, water_flux_surface, potential_soilevaporation) =
soil_model.boundary_conditions
potential_transpiration .= get_potential_transpiration(interception)
@. potential_soilevaporation =
soil_model.parameters.soil_fraction * atmospheric_forcing.potential_evaporation
evaporation!(demand.paddy, potential_soilevaporation)
potential_soilevaporation .= potential_soilevaporation .- get_evaporation(demand.paddy)
water_flux_surface .=
max.(
runoff.boundary_conditions.water_flux_surface .+
get_irrigation_allocated(allocation) .- runoff.variables.runoff_river .-
runoff.variables.runoff_land .+ get_water_depth(demand.paddy),
0.0,
)
return nothing
end
"Update soil temperature of the SBM soil model for a single timestep"
function soil_temperature!(
soil_model::SbmSoilModel,
::AbstractSnowModel,
temperature::Vector{Float64},
)
v = soil_model.variables
p = soil_model.parameters
@. v.tsoil = soil_temperature(v.tsoil, p.w_soil, temperature)
return nothing
end
soil_temperature!(::SbmSoilModel, ::NoSnowModel, ::Vector{Float64}) = nothing
"Update total available water in the unsaturated zone of the SBM soil model for a single timestep"
function ustoredepth!(soil_model::SbmSoilModel)
v = soil_model.variables
p = soil_model.parameters
for i in eachindex(v.ustorelayerdepth)
v.ustoredepth[i] = sum(@view v.ustorelayerdepth[i][1:p.nlayers[i]])
end
return nothing
end
"Update the infiltration reduction factor of the SBM soil model for a single timestep"
function infiltration_reduction_factor!(
soil_model::SbmSoilModel;
modelsnow = false,
soil_infiltration_reduction = false,
)
v = soil_model.variables
p = soil_model.parameters
n = length(v.tsoil)
threaded_foreach(1:n; basesize = 1000) do i
v.f_infiltration_reduction[i] = infiltration_reduction_factor(
v.tsoil[i],
p.cf_soil[i];
modelsnow,
soil_infiltration_reduction,
)
end
return nothing
end
function update_available_for_infiltration!(
model::SbmSoilModel,
domain::Domain,
runoff::AbstractRunoffModel,
do_surface_water_infiltration::Bool,
)
v = model.variables
(; water_flux_surface) = model.boundary_conditions
(; waterdepth_land) = runoff.boundary_conditions
(; river_fraction) = domain.land.parameters
n = length(v.potential_infiltration)
threaded_foreach(1:n; basesize = 1000) do i
v.potential_infiltration_surfacewater[i] = 0.0
if do_surface_water_infiltration
v.potential_infiltration_surfacewater[i] =
waterdepth_land[i] * (1.0 - river_fraction[i]) * 0.95
water_flux_surface[i] += v.potential_infiltration_surfacewater[i]
end
v.potential_infiltration[i] = water_flux_surface[i]
end
return nothing
end
function correct_infiltration!(model::SbmSoilModel)
v = model.variables
(; water_flux_surface) = model.boundary_conditions
n = length(v.actinfilt)
threaded_foreach(1:n; basesize = 1000) do i
v.infilt_surfacewater[i],
v.actinfilt[i],
v.infiltexcess[i],
v.excesswater[i],
water_flux_surface[i] = correct_infiltration(
v.potential_infiltration[i],
v.potential_infiltration_surfacewater[i],
water_flux_surface[i],
v.actinfilt[i],
v.infiltexcess[i],
)
end
end
"""
infiltration!(soil_model::SbmSoilMsoil
Update the infiltration rate `infiltsoilpath` and infiltration excess water rate
`infiltexcess` of the SBM soil model for a single timestep.
"""
function infiltration!(soil_model::SbmSoilModel)
v = soil_model.variables
p = soil_model.parameters
(; water_flux_surface) = soil_model.boundary_conditions
n = length(v.infiltsoilpath)
threaded_foreach(1:n; basesize = 1000) do i
v.infiltsoilpath[i], v.infiltexcess[i] = infiltration(
water_flux_surface[i],
p.pathfrac[i],
p.infiltcapsoil[i],
p.infiltcappath[i],
v.ustorecapacity[i],
v.f_infiltration_reduction[i],
)
end
return nothing
end
"""
unsaturated_zone_flow!(soil_model::SbmSoilModel)
Update unsaturated storage `ustorelayerdepth` and the `transfer` of water from the unsaturated
to the saturated store of the SBM soil model for a single timestep, based on the Brooks-Corey
approach.
"""
function unsaturated_zone_flow!(soil_model::SbmSoilModel)
v = soil_model.variables
p = soil_model.parameters
n = length(v.transfer)
threaded_foreach(1:n; basesize = 250) do i
if v.n_unsatlayers[i] > 0
# Brooks-Corey approach
z = cumsum(v.ustorelayerthickness[i])
flow_rate = 0.0
for m in 1:v.n_unsatlayers[i]
l_sat = v.ustorelayerthickness[i][m] * (p.theta_s[i] - p.theta_r[i])
kv_z = hydraulic_conductivity_at_depth(p.kv_profile, p.kvfrac, z[m], i, m)
ustorelayerdepth = if m == 1
v.ustorelayerdepth[i][m] + v.infiltsoilpath[i]
else
v.ustorelayerdepth[i][m] + flow_rate
end
ustorelayerdepth, flow_rate =
unsatzone_flow_layer(ustorelayerdepth, kv_z, l_sat, p.c[i][m])
v.ustorelayerdepth[i] = setindex(v.ustorelayerdepth[i], ustorelayerdepth, m)
end
v.transfer[i] = flow_rate
else
v.transfer[i] = 0.0
end
end
return nothing
end
"""
soil_evaporation!(soil_model::SbmSoilModel)
Update soil evaporation from the saturated store `soilevapsat` and the total soil
evaporation from the unsaturated and saturated store `soilevap` of the SBM soil model for a
single timestep. Also unsaturated storage `ustorelayerdepth` and the saturated store
`satwaterdepth` are updated.
"""
function soil_evaporation!(soil_model::SbmSoilModel)
(; potential_soilevaporation) = soil_model.boundary_conditions
v = soil_model.variables
p = soil_model.parameters
n = length(potential_soilevaporation)
threaded_foreach(1:n; basesize = 1000) do i
potsoilevap = potential_soilevaporation[i]
# First calculate the evaporation of unsaturated storage into the
# atmosphere from the upper layer.
soilevapunsat = soil_evaporation_unsatured_store(
potsoilevap,
v.ustorelayerdepth[i][1],
v.ustorelayerthickness[i][1],
v.n_unsatlayers[i],
v.zi[i],
p.theta_s[i] - p.theta_r[i],
)
# Ensure that the unsaturated evaporation rate does not exceed the
# available unsaturated moisture
soilevapunsat = min(soilevapunsat, v.ustorelayerdepth[i][1])
# Update the additional atmospheric demand
potsoilevap -= soilevapunsat
v.ustorelayerdepth[i] =
setindex(v.ustorelayerdepth[i], v.ustorelayerdepth[i][1] - soilevapunsat, 1)
theta_drainable = lower_bound_drainable_porosity(p.theta_s[i], p.theta_fc[i])
soilevapsat = soil_evaporation_satured_store(
potsoilevap,
v.n_unsatlayers[i],
p.act_thickl[i][1],
v.zi[i],
theta_drainable,
)
v.soilevapsat[i] = soilevapsat
v.soilevap[i] = soilevapunsat + soilevapsat
v.drainable_waterdepth[i] = v.drainable_waterdepth[i] - soilevapsat
end
return nothing
end
"""
transpiration!(soil_model::SbmSoilModel, dt)
Update total `transpiration`, transpiration from the unsaturated store `ae_ustore` and
saturated store `actevapsat` of the SBM soil model for a single timestep. Also unsaturated
storage `ustorelayerdepth` and the saturated store `satwaterdepth` are updated.
"""
function transpiration!(soil_model::SbmSoilModel, dt::Float64)
(; potential_transpiration) = soil_model.boundary_conditions
v = soil_model.variables
p = soil_model.parameters
rootingdepth = get_rootingdepth(soil_model)
n = length(rootingdepth)
threaded_foreach(1:n; basesize = 250) do i
v.h3[i] = feddes_h3(p.h3_high[i], p.h3_low[i], potential_transpiration[i], dt)
# compute sum of root fraction in unsaturated soil layers and adapt root fraction
# lowest unsaturated soil layer if water table depth intersects the unsaturated root
# zone
sum_rootfraction_unsat = 0.0
rootfraction_unsat_lowest = 0.0
for k in 1:v.n_unsatlayers[i]
# the root fraction is valid for the root length in a soil layer, if zi decreases
# the root length the root fraction needs to be adapted
if k == v.n_unsatlayers[i] && v.zi[i] < rootingdepth[i]
rootlength = min(p.act_thickl[i][k], rootingdepth[i] - p.sumlayers[i][k])
rootfraction_unsat =
p.rootfraction[i][k] * (v.ustorelayerthickness[i][k] / rootlength)
sum_rootfraction_unsat += rootfraction_unsat
else
rootfraction_unsat = p.rootfraction[i][k]
sum_rootfraction_unsat += rootfraction_unsat
end
# rootfraction lowest unsaturated layer
rootfraction_unsat_lowest = rootfraction_unsat
end
actevapustore = 0.0
for k in 1:v.n_unsatlayers[i]
# scale rootfraction soil layer unsaturated zone based on sum of rootfraction in
# unsaturated zone
if k < v.n_unsatlayers[i]
rootfraction_unsat = p.rootfraction[i][k]
else
rootfraction_unsat = rootfraction_unsat_lowest
end
rootfraction_unsat_scaled =
rootingdepth[i] > 0.0 ?
max((1.0 / sum_rootfraction_unsat), 1.0) * rootfraction_unsat : 0.0
vwc = max(
v.ustorelayerdepth[i][k] / v.ustorelayerthickness[i][k],
Float64(0.0000001),
)
head = head_brooks_corey(vwc, p.theta_s[i], p.theta_r[i], p.c[i][k], p.hb[i])
alpha = rwu_reduction_feddes(
head,
p.h1[i],
p.h2[i],
v.h3[i],
p.h4[i],
p.alpha_h1[i],
)
availcap = min(
1.0,
max(
0.0,
(rootingdepth[i] - p.sumlayers[i][k]) / v.ustorelayerthickness[i][k],
),
)
maxextr = v.ustorelayerdepth[i][k] * availcap
actevapustore_layer =
min(alpha * rootfraction_unsat_scaled * potential_transpiration[i], maxextr)
ustorelayerdepth = v.ustorelayerdepth[i][k] - actevapustore_layer
actevapustore += actevapustore_layer
v.ustorelayerdepth[i] = setindex(v.ustorelayerdepth[i], ustorelayerdepth, k)
end
# transpiration from saturated store
wetroots = scurve(v.zi[i], rootingdepth[i], Float64(1.0), p.rootdistpar[i])
alpha = rwu_reduction_feddes(
Float64(0.0),
p.h1[i],
p.h2[i],
v.h3[i],
p.h4[i],
p.alpha_h1[i],
)
restpottrans = potential_transpiration[i] - actevapustore
actevapsat = min(restpottrans * wetroots * alpha, v.drainable_waterdepth[i])