Skip to content

Commit 8490ca4

Browse files
committed
Implement 'balanced'; add a unit test for that
1 parent 917e9cc commit 8490ca4

File tree

3 files changed

+37
-1
lines changed

3 files changed

+37
-1
lines changed

examples/L96m/L96m.jl

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,38 @@ function full(rhs::Array{<:Real,1}, z::Array{<:Real,1}, _s::L96m, t)
8383
return rhs
8484
end
8585

86+
function balanced(rhs::Array{<:Real,1}, x::Array{<:Real,1}, _s::L96m, t)
87+
"""
88+
Compute balanced RHS of the Lorenz '96 multiscale system; i.e. only slow
89+
variables with the linear closure.
90+
Both `rhs` and `x` are vectors of size _s.K.
91+
92+
Input:
93+
- `x` : vector of size K
94+
- `_s` : parameters
95+
- `t` : time (not used here since L96m is autonomous)
96+
97+
Output:
98+
- `rhs` : balanced RHS computed at `x`
99+
100+
"""
101+
102+
K = _s.K
103+
104+
# three boundary cases
105+
rhs[1] = -x[K] * (x[K-1] - x[2]) - (1 - _s.hx*_s.hy) * x[1]
106+
rhs[2] = -x[1] * (x[K] - x[3]) - (1 - _s.hx*_s.hy) * x[2]
107+
rhs[K] = -x[K-1] * (x[K-2] - x[1]) - (1 - _s.hx*_s.hy) * x[K]
108+
109+
# general case
110+
rhs[3:K-1] = -x[2:K-2] .* (x[1:K-3] - x[4:K]) - (1 - _s.hx*_s.hy) * x[3:K-1]
111+
112+
# add forcing
113+
rhs .+= _s.F
114+
115+
return rhs
116+
end
117+
86118
function compute_Yk(_s::L96m, z::Array{<:Real,1})
87119
"""
88120
Reshape a vector of y_{j,k} into a matrix, then sum along one dim and divide

test/L96m/data/balanced.npy

152 Bytes
Binary file not shown.

test/L96m/runtests.jl

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ const data_dir = joinpath(@__DIR__, "data")
1515
const z0 = NPZ.npzread(joinpath(data_dir, "ic.npy"))
1616
const Yk_test = NPZ.npzread(joinpath(data_dir, "Yk.npy"))
1717
const full_test = NPZ.npzread(joinpath(data_dir, "full.npy"))
18+
const balanced_test = NPZ.npzread(joinpath(data_dir, "balanced.npy"))
1819
const dp5_test = NPZ.npzread(joinpath(data_dir, "dp5.npy"))
1920
const tp8_test = NPZ.npzread(joinpath(data_dir, "tsitpap8.npy"))
2021

@@ -23,10 +24,13 @@ const tp8_test = NPZ.npzread(joinpath(data_dir, "tsitpap8.npy"))
2324
################################################################################
2425
Yk_comp = compute_Yk(l96, z0)
2526
full_comp = similar(z0)
26-
full(full_comp, z0, l96, 0)
27+
full(full_comp, z0, l96, 0.0)
28+
balanced_comp = similar(z0[1:l96.K])
29+
balanced(balanced_comp, z0[1:l96.K], l96, 0.0)
2730
@testset "unit testing" begin
2831
@test isapprox(Yk_test, Yk_comp, atol=1e-15)
2932
@test isapprox(full_test, full_comp, atol=1e-15)
33+
@test isapprox(balanced_test, balanced_comp, atol=1e-15)
3034
end
3135
println("")
3236

0 commit comments

Comments
 (0)