Skip to content

Commit e26f14c

Browse files
committed
patch
1 parent c121aad commit e26f14c

9 files changed

+196
-145
lines changed

Diff for: src/BEPS/Base/VCmax.jl

+3-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1-
function VCmax(lai, clumping, CosZs, param)
1+
function VCmax(lai::FT, clumping::FT, CosZs::FT, param::AbstractVector{FT})
2+
CosZs <= 0 && return 0.0, 0.0 # 光合仅发生在白天
3+
24
# parameters for Vcmax-Nitrogen calculations
35
G_theta = 0.5 # assuming a spherical leaf angle distribution
46
K = G_theta * clumping / CosZs

Diff for: src/BEPS/evaporation_canopy.jl

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
function evaporation_canopy_jl(T_leaf::Leaf, Ta::Float64, RH::Float64,
22
Gwater::Leaf, lai::Leaf,
3-
perc_water_o::Float64, perc_water_u::Float64, perc_snow_o::Float64, perc_snow_u::Float64)
3+
perc_water_o::Float64, perc_water_u::Float64,
4+
perc_snow_o::Float64, perc_snow_u::Float64)
45

56
# LHw = Leaf() # latent heat from leaves W/m2, caused by evaporation of intercepted rain
67
# LHs = Leaf() # latent heat from leaves W/m2, caused by evaporation of intercepted snow

Diff for: src/BEPS/evaporation_soil.jl

+5-5
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ function evaporation_soil_jl(Tair::FT, Tg::FT, RH::FT, Rn_g::FT, Gheat_g::FT,
88
perc_snow_g::Ref{FT},
99
depth_water, depth_snow,
1010
# depth_water::Ref{FT}, depth_snow::Ref{FT},
11-
mass_water_g::FT, mass_snow_g::Ref{FT},
11+
mass_water_g::FT, mass_snow::CanopyLayer{FT},
1212
ρ_snow::FT, swc_g::FT, porosity_g::FT) where {FT<:Real}
1313

1414
met = meteo_pack_jl(Tg, RH)
@@ -18,7 +18,7 @@ function evaporation_soil_jl(Tair::FT, Tg::FT, RH::FT, Rn_g::FT, Gheat_g::FT,
1818
Gwater_g = 1.0 / (4.0 * exp(8.2 - 4.2 * swc_g / porosity_g))
1919
# kstep = kstep # 360s, 6min
2020

21-
perc_snow_g[] = depth_snow > 0.02 ? 1.0 : mass_snow_g[] / (0.025 * ρ_snow)
21+
perc_snow_g[] = depth_snow > 0.02 ? 1.0 : mass_snow.g / (0.025 * ρ_snow)
2222
perc_snow_g[] = clamp(perc_snow_g[], 0.0, 1.0)
2323

2424
if depth_water > 0.0 && depth_snow == 0.0
@@ -46,11 +46,11 @@ function evaporation_soil_jl(Tair::FT, Tg::FT, RH::FT, Rn_g::FT, Gheat_g::FT,
4646

4747
Esoil_g = max(-0.002 / kstep, Esoil_g)
4848
if Esoil_g > 0.0
49-
Esoil_g = min(Esoil_g, mass_snow_g[] / kstep)
49+
Esoil_g = min(Esoil_g, mass_snow.g / kstep)
5050
end
5151

52-
mass_snow_g[] = max(mass_snow_g[] - Esoil_g * kstep, 0)
53-
depth_snow = mass_snow_g[] > 0.0 ? depth_snow - (Esoil_g / ρ_snow) * kstep : 0.0
52+
mass_snow.g = max(mass_snow.g - Esoil_g * kstep, 0)
53+
depth_snow = mass_snow.g > 0.0 ? depth_snow - (Esoil_g / ρ_snow) * kstep : 0.0
5454

5555
if depth_water > 0.0 || depth_snow > 0.0
5656
Esoil = 0.0

Diff for: src/BEPS/snowpack.jl

+29-26
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,8 @@
88
"""
99
function snowpack_stage1_jl(temp_air::Float64, prcp::Float64,
1010
# ms_o_last::Float64, ms_u_last::Float64, ms_g_last::Float64,
11-
ms_o::Ref{Float64}, ms_u::Ref{Float64}, ms_g::Ref{Float64},
11+
ms::CanopyLayer{Float64},
12+
# ms_o::Ref{Float64}, ms_u::Ref{Float64}, ms_g::Ref{Float64},
1213
lai_o::Float64, lai_u::Float64, clumping::Float64, area_snow_o::Ref{Float64}, area_snow_u::Ref{Float64},
1314
# perc_snow_o::Ref{Float64}, perc_snow_u::Ref{Float64}, perc_snow_g::Ref{Float64},
1415
ρ_snow::Ref{Float64}, d_s, albedo_v_snow::Ref{Float64}, albedo_n_snow::Ref{Float64})
@@ -22,54 +23,54 @@ function snowpack_stage1_jl(temp_air::Float64, prcp::Float64,
2223
albedo_v_Newsnow = 0.94
2324
albedo_n_Newsnow = 0.8
2425

25-
ms_o_last = ms_o[]
26-
ms_u_last = ms_u[]
27-
ms_g_last = ms_g[]
26+
ms_o_last = ms.o
27+
ms_u_last = ms.u
28+
ms_g_last = ms.g
2829

2930
snowrate = temp_air > 0 ? 0 : prcp * ρ_w / ρ_new_snow
3031
snowrate_o = 0.0
3132

3233
if temp_air < 0
3334
snowrate_o = snowrate
34-
ms_o[] = ms_o_last + snowrate_o * kstep * ρ_new_snow * (1 - exp(-lai_o * clumping))
35-
perc_snow_o = ms_o[] / massMax_snow_o
35+
ms.o = ms_o_last + snowrate_o * kstep * ρ_new_snow * (1 - exp(-lai_o * clumping))
36+
perc_snow_o = ms.o / massMax_snow_o
3637
perc_snow_o = clamp(perc_snow_o, 0, 1)
3738
area_snow_o[] = perc_snow_o * areaMax_snow_o
38-
massStep_snow_o = ms_o[] - ms_o_last
39+
massStep_snow_o = ms.o - ms_o_last
3940

4041
snowrate_u = max(0, snowrate_o - (massStep_snow_o) / ρ_new_snow / kstep)
41-
ms_u[] = ms_u_last + snowrate_u * kstep * ρ_new_snow * (1 - exp(-lai_u * clumping))
42-
perc_snow_u = ms_u[] / massMax_snow_u
42+
ms.u = ms_u_last + snowrate_u * kstep * ρ_new_snow * (1 - exp(-lai_u * clumping))
43+
perc_snow_u = ms.u / massMax_snow_u
4344
perc_snow_u = clamp(perc_snow_u, 0, 1)
4445
area_snow_u[] = perc_snow_u * areaMax_snow_u
45-
massStep_snow_u = ms_u[] - ms_u_last
46+
massStep_snow_u = ms.u - ms_u_last
4647

4748
snowrate_g = max(0, snowrate_u - (massStep_snow_u) / ρ_new_snow / kstep)
4849
δ_d_s = snowrate_g * kstep
4950
else
50-
ms_o[] = ms_o_last
51-
perc_snow_o = clamp(ms_o[] / massMax_snow_o, 0, 1)
51+
ms.o = ms_o_last
52+
perc_snow_o = clamp(ms.o / massMax_snow_o, 0, 1)
5253
area_snow_o[] = area_snow_o[]
5354

54-
ms_u[] = ms_u_last
55-
perc_snow_u = ms_u[] / massMax_snow_u
55+
ms.u = ms_u_last
56+
perc_snow_u = ms.u / massMax_snow_u
5657
perc_snow_u = clamp(perc_snow_u, 0, 1)
5758
area_snow_u[] = area_snow_u[]
5859

5960
δ_d_s = 0
6061
end
6162

6263
δ_d_s = max(0, δ_d_s)
63-
ms_g[] = max(0, ms_g_last + δ_d_s * ρ_new_snow)
64+
ms.g = max(0, ms_g_last + δ_d_s * ρ_new_snow)
6465

6566
if δ_d_s > 0
6667
ρ_snow[] = (ρ_snow[] * d_s + ρ_new_snow * δ_d_s) / (d_s + δ_d_s)
6768
else
6869
ρ_snow[] = (ρ_snow[] - 250) * exp(-0.001 * kstep / 3600.0) + 250.0
6970
end
7071

71-
d_s = ms_g[] > 0 ? ms_g[] / ρ_snow[] : 0.0
72-
perc_snow_g = min(ms_g[] / (0.05 * ρ_snow[]), 1.0)
72+
d_s = ms.g > 0 ? ms.g / ρ_snow[] : 0.0
73+
perc_snow_g = min(ms.g / (0.05 * ρ_snow[]), 1.0)
7374

7475
if snowrate_o > 0
7576
albedo_v_snow[] = (albedo_v_snow[] - 0.70) * exp(-0.005 * kstep / 3600) + 0.7
@@ -79,28 +80,30 @@ function snowpack_stage1_jl(temp_air::Float64, prcp::Float64,
7980
albedo_n_snow[] = albedo_n_Newsnow
8081
end
8182

82-
perc_snow_o, perc_snow_u, perc_snow_g, d_s
83+
perc_snow = CanopyLayer(perc_snow_o, perc_snow_u, perc_snow_g)
84+
perc_snow, d_s
8385
end
8486

8587

86-
function snowpack_stage2_jl(evapo_snow_o::Float64, evapo_snow_u::Float64,
87-
ms_o::Ref{Float64}, ms_u::Ref{Float64})
88+
function snowpack_stage2_jl(evapo_snow_o::Float64, evapo_snow_u::Float64, ms::CanopyLayer{Float64})
89+
# ms_o::Ref{Float64}, ms_u::Ref{Float64})
8890

8991
# kstep::Float64 = kstep # length of step
90-
ms_o[] = max(0, ms_o[] - evapo_snow_o * kstep)
91-
ms_u[] = max(0, ms_u[] - evapo_snow_u * kstep)
92+
ms.o = max(0, ms.o - evapo_snow_o * kstep)
93+
ms.u = max(0, ms.u - evapo_snow_u * kstep)
9294
end
9395

9496

9597
function snowpack_stage3_jl(temp_air::Float64, temp_snow::Float64, temp_snow_last::Float64, ρ_snow::Float64,
9698
d_s, d_w,
9799
# d_s::Ref{Float64}, d_w::Ref{Float64},
98-
ms_g::Ref{Float64})
100+
ms::CanopyLayer{Float64})
101+
# ms_g::Ref{Float64})
99102

100103
# kstep = kstep # kstep in model
101104
# it is assumed sublimation happens before the melting and freezing process
102105
d_s_sup = d_s # this snow depth has already considered sublimation
103-
ms_sup = ms_g[]
106+
ms_sup = ms.g
104107

105108
# parameters
106109
cp_ice = 2228.261
@@ -152,8 +155,8 @@ function snowpack_stage3_jl(temp_air::Float64, temp_snow::Float64, temp_snow_las
152155
end
153156

154157
# δ in mass of snow on ground
155-
ms_g[] = ms_g[] - ms_melt + mw_frozen
156-
ms_g[] = max(0, ms_g[])
158+
ms.g = ms.g - ms_melt + mw_frozen
159+
ms.g = max(0, ms.g)
157160

158161
# δ of depth in snow
159162
melt_d_s = ms_melt / ρ_snow

Diff for: src/DataType/CanopyLayer.jl

+34
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
import Base.show
2+
3+
abstract type AbstractLayer{FT} end
4+
5+
"""
6+
x = CanopyLayer{Float64}()
7+
x = CanopyLayer{Ref{Float64}}()
8+
"""
9+
@with_kw_noshow mutable struct CanopyLayer{FT} <: AbstractLayer{FT}
10+
o::FT = FT(0.0) # overlayer
11+
u::FT = FT(0.0) # underlayer
12+
g::FT = FT(0.0) # ground
13+
end
14+
15+
# 注意,如果是Ref,将共用相同的地址
16+
CanopyLayer(o::FT) where {FT} = CanopyLayer{FT}(o, o, o)
17+
18+
CanopyLayer(o::FT, u::FT) where {FT} = CanopyLayer{FT}(; o, u)
19+
20+
21+
function Base.show(io::IO, x::AbstractLayer{FT}) where {FT}
22+
println(io, typeof(x))
23+
names = fieldnames(typeof(x))
24+
for i in 1:nfields(x)
25+
name = names[i]
26+
value = getfield(x, name)
27+
T = typeof(value)
28+
isa(value, Ref) && (value = value[])
29+
println(io, "$name: $T $(value)")
30+
end
31+
end
32+
33+
34+
export CanopyLayer

Diff for: src/DataType/DataType.jl

+1
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ nzero(n) = tuple(zeros(n)...) # n double zero
1313

1414
include("Constant.jl")
1515
include("Leaf.jl")
16+
include("CanopyLayer.jl")
1617
include("Soil.jl")
1718

1819
include("InterTempVars.jl")

Diff for: src/DataType/InterTempVars.jl

+22-22
Original file line numberDiff line numberDiff line change
@@ -43,32 +43,32 @@ InterTempLeafs(x0) = InterTempLeafs(; x0)
4343
export SurfaceMass
4444

4545
@with_kw mutable struct SurfaceMass{FT<:AbstractFloat}
46-
ρ_snow ::FT = 0.0
47-
prcp_g ::FT = 0.0
46+
ρ_snow::FT = 0.0
47+
prcp_g::FT = 0.0
4848

49-
depth_water ::FT = 0.0
50-
depth_snow ::FT = 0.0
49+
depth_water::FT = 0.0
50+
depth_snow::FT = 0.0
5151

52-
perc_snow_o ::FT = 0.0 # Xcs_o
53-
perc_snow_u ::FT = 0.0 # Xcs_u
54-
perc_snow_g ::FT = 0.0 # Xcs_g
52+
perc_snow_o::FT = 0.0 # Xcs_o
53+
perc_snow_u::FT = 0.0 # Xcs_u
54+
perc_snow_g::FT = 0.0 # Xcs_g
5555

56-
perc_water_o ::FT = 0.0 # Xcl_o
57-
perc_water_u ::FT = 0.0 # Xcl_u
56+
perc_water_o::FT = 0.0 # Xcl_o
57+
perc_water_u::FT = 0.0 # Xcl_u
5858

59-
area_snow_o ::FT = 0.0 # Ac_snow_o
60-
area_snow_u ::FT = 0.0 # Ac_snow_u
59+
area_snow_o::FT = 0.0 # Ac_snow_o
60+
area_snow_u::FT = 0.0 # Ac_snow_u
6161

6262
pre_mass_water_o::FT = 0.0 # Wcl_o
6363
pre_mass_water_u::FT = 0.0 # Wcl_u
64-
64+
6565
mass_water_o::FT = 0.0 # Wcl_o
6666
mass_water_u::FT = 0.0 # Wcl_u
6767
mass_water_g::FT = 0.0 # Wcl_g
6868

69-
mass_snow_o ::FT = 0.0 # Wcs_o
70-
mass_snow_u ::FT = 0.0 # Wcs_u
71-
mass_snow_g ::FT = 0.0 # Wcs_g
69+
mass_snow_o::FT = 0.0 # Wcs_o
70+
mass_snow_u::FT = 0.0 # Wcs_u
71+
mass_snow_g::FT = 0.0 # Wcs_g
7272

7373
albedo_v_snow::FT = 0.0
7474
albedo_n_snow::FT = 0.0
@@ -107,13 +107,13 @@ end
107107
# area
108108
Ac_snow_o::Vector{FT} = zeros(MAX_Loop)
109109
Ac_snow_u::Vector{FT} = zeros(MAX_Loop)
110-
110+
111111
rho_snow::Vector{FT} = zeros(MAX_Loop)
112112
r_rain_g::Vector{FT} = zeros(MAX_Loop)
113-
113+
114114
alpha_v_sw::Vector{FT} = zeros(MAX_Loop)
115115
alpha_n_sw::Vector{FT} = zeros(MAX_Loop)
116-
116+
117117
Trans_o::Vector{FT} = zeros(MAX_Loop)
118118
Trans_u::Vector{FT} = zeros(MAX_Loop)
119119
Eil_o::Vector{FT} = zeros(MAX_Loop)
@@ -144,18 +144,18 @@ function init_vars!(x::InterTempVars)
144144
# x.Qhc_o .= 0.
145145
# x.Qhc_u .= 0.
146146
# x.Qhg .= 0.
147-
x.Wcl_o .= 0.
147+
x.Wcl_o .= 0.0
148148
x.Wcl_u .= 0.0
149149

150150
x.Wcs_o .= 0.0
151151
x.Wcs_u .= 0.0
152152
x.Wcs_g .= 0.0
153153

154-
x.Xcl_o .= 0.
155-
x.Xcl_u .= 0.
154+
x.Xcl_o .= 0.0
155+
x.Xcl_u .= 0.0
156156

157157
x.Xcs_o .= 0.0
158-
x.Xcs_u .= 0.
158+
x.Xcs_u .= 0.0
159159
x.Xcs_g .= 0.0
160160

161161
x.rho_snow .= 0.0

Diff for: src/DataType/Leaf.jl

+9-13
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,4 @@
1-
export @reset, reset!
2-
macro reset(x)
3-
quote
4-
reset!($(esc(x)))
5-
end
6-
end
7-
1+
# they also abstract vector but with name
82
@with_kw mutable struct Leaf
93
o_sunlit::Cdouble = 0.0
104
o_shaded::Cdouble = 0.0
@@ -19,7 +13,6 @@ Leaf(o::Cdouble, u::Cdouble) = Leaf(o, u)
1913
Base_ops = ((:Base, :+), (:Base, :-), (:Base, :*), (:Base, :/))
2014

2115
for (m, f) in Base_ops
22-
# _f = Symbol(m, ".:", f)
2316
@eval begin
2417
$m.$f(a::Leaf, b::Leaf) = begin
2518
Leaf(
@@ -33,31 +26,34 @@ for (m, f) in Base_ops
3326
end
3427

3528

29+
export @reset, reset!
30+
macro reset(x)
31+
quote
32+
reset!($(esc(x)))
33+
end
34+
end
35+
3636
reset!(x::Leaf) = init_leaf_dbl(x, 0.0)
3737

3838
function init_leaf_struct(x::Leaf, replacement::Leaf)
3939
x.o_sunlit = replacement.o_sunlit
4040
x.o_shaded = replacement.o_shaded
4141
x.u_sunlit = replacement.u_sunlit
4242
x.u_shaded = replacement.u_shaded
43-
# ccall((:init_leaf_struct, libbeps), Cvoid, (Ptr{Leaf}, Ptr{Leaf}), Ref(x), Ref(replacement))
4443
end
4544

4645
function init_leaf_dbl(x::Leaf, replacement::Float64)
4746
x.o_sunlit = replacement
4847
x.o_shaded = replacement
4948
x.u_sunlit = replacement
5049
x.u_shaded = replacement
51-
nothing
52-
# ccall((:init_leaf_dbl, libbeps), Cvoid, (Ptr{Leaf}, Cdouble), Ref(x), replacement)
5350
end
5451

55-
function init_leaf_dbl2(x, overstory, understory)
52+
function init_leaf_dbl2(x::Leaf, overstory, understory)
5653
x.o_sunlit = overstory
5754
x.o_shaded = overstory
5855
x.u_sunlit = understory
5956
x.u_shaded = understory
60-
# ccall((:init_leaf_dbl2, libbeps), Cvoid, (Ptr{Leaf}, Cdouble, Cdouble), Ref(x), overstory, understory)
6157
end
6258

6359

0 commit comments

Comments
 (0)