Skip to content

Commit

Permalink
全线竣工
Browse files Browse the repository at this point in the history
  • Loading branch information
kongdd committed Jan 16, 2024
1 parent 164392c commit 10be37d
Show file tree
Hide file tree
Showing 9 changed files with 43 additions and 26 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/CI.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ jobs:
- '1'
# - 'nightly'
os:
# - ubuntu-latest
- ubuntu-latest
# - macOS-latest
- windows-latest
arch:
Expand Down
21 changes: 15 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,16 +9,30 @@ Boreal Ecosystem Productivity Simulator in Julia

> Dongdong Kong
BEPS.jl is alive in Julia now. All functions have been ported to Julia, and the
performance is about 2.5 times faster than C version.

> - Julia: 0.286327 seconds (822.38 k allocations: 22.998 MiB, 0.85% gc time)
> - C : 0.787059 seconds (629.95 k allocations: 13.915 MiB)
> [!CAUTION]
> `BEPS.clang` only works under Windows.
## Install

* For developers
```bash
git clone https://github.com/CUG-hydro/BEPS.jl
cd BEPS.jl/deps
git clone https://github.com/CUG-hydro/BEPS.c
```

* For users
```bash
# In Julia
] add https://github.com/CUG-hydro/BEPS.jl
```

## Usage

```julia
Expand All @@ -33,16 +47,11 @@ par = (lon=120.5, lat=30.5, landcover=25, clumping=0.85,
@time df_jl, df_ET_jl = besp_main(d, lai, par; version="julia");
```

> Figure1: The bias of Julia version compared with C.
> Figure1: The bias of Julia version compared with C, `bias = (Julia - C)/ C`.
![Figure1: bias of julia version compared with c](./images/Figure1_bias_of_julia-version.png)

The bias of `SH` is the largest due to double number accuracy, about 1.48%, which is acceptable.

> The Julia version is about 2X times faster than C.
- Julia: 0.327014 seconds (1.09 M allocations: 47.056 MiB, 2.32% gc time)
- C : 0.831637 seconds (629.96 k allocations: 13.916 MiB)

See [examples/example_01.qmd](examples/example_01.qmd) for details.

## TODO
Expand Down
1 change: 1 addition & 0 deletions examples/example_01.qmd
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ write_fig("images/Figure1_bias_of_julia-version.png", 15, 8; show=false)
# @time df_jl, df_ET_jl = besp_main(d, lai, par; version="julia");
# @profview df_jl, df_ET_jl = besp_main(d, lai, par; version="julia");
@profview_allocs df_jl, df_ET_jl = besp_main(d, lai, par; version="julia");
@profview df_jl, df_ET_jl = besp_main(d, lai, par; version="julia");
```


Expand Down
7 changes: 4 additions & 3 deletions src/BEPS.jl
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,16 @@ import Parameters: @with_kw, @with_kw_noshow
using Printf
using Reexport
@reexport using Serialization: deserialize, serialize
@reexport using DelimitedFiles: readdlm

export clang
export besp_main
export init_soil!

path_proj(f...) = normpath(joinpath(@__DIR__, "..", f...))
libbeps = path_proj("deps/libbeps.dll")

# import Statistics: mean, std
include("IO.jl")
include("DataFrames.jl")
include("Ipaper.jl")
# include("c2julia.jl")

Expand All @@ -26,7 +26,8 @@ include("BEPS_helper.jl")
include("BEPS/BEPS.jl")

include("clang/BEPS_c.jl")
@reexport using BEPS.clang;
@reexport import BEPS.clang;
import BEPS.clang: inter_prg_c, photosynthesis_c

include("beps_inter_prg.jl")
include("beps_main.jl")
Expand Down
20 changes: 10 additions & 10 deletions src/BEPS/soil_thermal_regime.jl
Original file line number Diff line number Diff line change
Expand Up @@ -83,9 +83,9 @@ end

# Function to compute soil water stress factor
function soil_water_factor_v2(p::Soil)
ft = zeros(Float64, p.n_layer)
fpsisr = zeros(Float64, p.n_layer)
dtt = zeros(Float64, p.n_layer)
# ft = zeros(Float64, p.n_layer)
# fpsisr = zeros(Float64, p.n_layer)
# dtt = zeros(Float64, p.n_layer)

t1 = -0.02
t2 = 2.0
Expand All @@ -99,29 +99,29 @@ function soil_water_factor_v2(p::Soil)

for i in 1:p.n_layer
# psi_sr in m H2O! This is the old version. LHE.
fpsisr[i] = p.psim[i] > p.psi_min ? 1.0 / (1 + ((p.psim[i] - p.psi_min) / p.psi_min)^p.alpha) : 1.0
p.fpsisr[i] = p.psim[i] > p.psi_min ? 1.0 / (1 + ((p.psim[i] - p.psi_min) / p.psi_min)^p.alpha) : 1.0

ft[i] = p.temp_soil_p[i] > 0.0 ? 1.0 - exp(t1 * p.temp_soil_p[i]^t2) : 0
p.ft[i] = p.temp_soil_p[i] > 0.0 ? 1.0 - exp(t1 * p.temp_soil_p[i]^t2) : 0

fpsisr[i] *= ft[i]
p.fpsisr[i] *= p.ft[i]
end

for i in 1:p.n_layer
dtt[i] = FW_VERSION == 1 ? p.f_root[i] * fpsisr[i] : p.f_root[i]
p.dtt[i] = FW_VERSION == 1 ? p.f_root[i] * p.fpsisr[i] : p.f_root[i]
end
dtt_sum = sum(dtt)
dtt_sum = sum(p.dtt)

if dtt_sum < 0.000001
p.f_soilwater = 0.1
else
fpsisr_sum = 0
for i in 1:p.n_layer
p.dt[i] = dtt[i] / dtt_sum
p.dt[i] = p.dtt[i] / dtt_sum

if isnan(p.dt[i])
println(p.dt[1])
end
fpsisr_sum += fpsisr[i] * p.dt[i]
fpsisr_sum += p.fpsisr[i] * p.dt[i]
end
p.f_soilwater = max(0.1, fpsisr_sum)
end
Expand Down
3 changes: 0 additions & 3 deletions src/IO.jl → src/DataFrames.jl
Original file line number Diff line number Diff line change
@@ -1,7 +1,4 @@

import DataFrames: DataFrame
using DelimitedFiles: readdlm


function Base.sum(df::DataFrame)
vals = [sum(df[!, c]) for c in names(df)]
Expand Down
9 changes: 9 additions & 0 deletions src/DataType/Soil.jl
Original file line number Diff line number Diff line change
Expand Up @@ -44,4 +44,13 @@ T = Vector{Float64}
lambda::T = zeros(10)
Ett::T = zeros(10)
G::T = zeros(10)

# temporary variables in soil_water_factor_v2
ft::T = zeros(10)
dtt::T = zeros(10)
fpsisr::T = zeros(10)
end

# ft = zeros(Float64, p.n_layer)
# fpsisr = zeros(Float64, p.n_layer)
# dtt = zeros(Float64, p.n_layer)
4 changes: 2 additions & 2 deletions src/clang/BEPS_c.jl
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ using BEPS
import BEPS: libbeps
# const libbeps = "deps/libbeps.dll"

include("../DataType/Constant.jl")
# include("../DataType/Constant.jl")
include("SOIL_c.jl")
include("module.jl")
# include("beps_inter_prg.jl")
Expand Down Expand Up @@ -164,7 +164,7 @@ end
# end

export inter_prg_c,
Vcmax_Jmax,
# Vcmax_Jmax,
# readparam,
# readcoef,
# lai2, s_coszs,
Expand Down
2 changes: 1 addition & 1 deletion test/test-soil.jl
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ using Test
import BEPS: kstep

function is_soil_equal(p_jl, p_c; tol=1e-7, verbose=false)
names = fieldnames(typeof(p_jl))
names = fieldnames(typeof(p_c))
for name in names
x_jl = getfield(p_jl, name)
x_c = getfield(p_c, name)
Expand Down

0 comments on commit 10be37d

Please sign in to comment.