Skip to content

Commit 6cec749

Browse files
committed
add unit test of Grids
1 parent 5748a8c commit 6cec749

File tree

4 files changed

+196
-30
lines changed

4 files changed

+196
-30
lines changed

Project.toml

+10
Original file line numberDiff line numberDiff line change
@@ -25,3 +25,13 @@ JLD2 = "^0.4"
2525
KernelAbstractions = "^0.7, 0.8"
2626
StructArrays = "^0.6"
2727
julia = "^1.6"
28+
29+
[extras]
30+
BenchmarkTools = "6e4b80f9-dd63-53aa-95a3-0cdb28fa8baf"
31+
Coverage = "a2441757-f6aa-5fb2-8edb-039e3f45d037"
32+
MeshArrays = "cb8c808f-1acf-59a3-9d2b-6e38d009f683"
33+
Test = "8dfed614-e22c-5e08-85e1-65c5234f0b40"
34+
35+
36+
[targets]
37+
test = ["BenchmarkTools", "Coverage", "MeshArrays", "Test"]

test/Project.toml

-14
This file was deleted.

test/grid_test.jl

+164
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,164 @@
1+
using PlanktonIndividuals.Grids
2+
3+
using MeshArrays
4+
5+
function test_rectilinear_grid()
6+
grid = RectilinearGrid(size = (4,6,2), x = (0,12), y = (0,12), z = (0,-8), halo = (2,2,2))
7+
8+
@test grid.Nx == 4
9+
@test grid.Ny == 6
10+
@test grid.Nz == 2
11+
12+
@test grid.Δx == 3.0
13+
@test grid.Δy == 2.0
14+
@test grid.dzC == [4.0,4.0,4.0,4.0,4.0,4.0]
15+
@test grid.dzF == [4.0,4.0,4.0,4.0,4.0,4.0]
16+
17+
@test length(grid.xC) == 4+2*2
18+
@test length(grid.yC) == 6+2*2
19+
@test length(grid.zC) == 2+2*2
20+
@test length(grid.xF) == 4+2*2
21+
@test length(grid.yF) == 6+2*2
22+
@test length(grid.zF) == 2+2*2
23+
24+
return nothing
25+
end
26+
27+
function test_vertically_stretched_rectilinear_grid()
28+
zfs = [0.0, -1.0, -3.0, -5.0, -10.0]
29+
grid = RectilinearGrid(size = (4,6,4), x = (0,12), y = (0,12), z = zfs, halo = (2,2,2))
30+
31+
@test grid.Nx == 4
32+
@test grid.Ny == 6
33+
@test grid.Nz == 4
34+
35+
@test grid.Δx == 3.0
36+
@test grid.Δy == 2.0
37+
@test grid.dzC == [1.0,1.0,1.5,2.0,3.5,5.0,5.0,5.0]
38+
@test grid.dzF == [1.0,1.0,1.0,2.0,2.0,5.0,5.0,5.0]
39+
40+
@test grid.zF == [2.0,1.0,-0.0,-1.0,-3.0,-5.0,-10.0,-15.0]
41+
@test grid.zC == [1.5,0.5,-0.5,-2.0,-4.0,-7.5,-12.5,-17.5]
42+
43+
@test length(grid.xC) == 4+2*2
44+
@test length(grid.yC) == 6+2*2
45+
@test length(grid.zC) == 4+2*2
46+
@test length(grid.xF) == 4+2*2
47+
@test length(grid.yF) == 6+2*2
48+
@test length(grid.zF) == 4+2*2
49+
50+
return nothing
51+
end
52+
53+
function test_rectilinear_areas_volumes()
54+
zfs = [0.0, -1.0, -3.0, -5.0, -10.0]
55+
grid = RectilinearGrid(size = (4,6,4), x = (0,12), y = (0,12), z = zfs, halo = (2,2,2))
56+
@test ΔxC(1,1,1,grid) == grid.Δx
57+
@test ΔyC(1,1,1,grid) == grid.Δy
58+
@test ΔzC(1,1,1,grid) == grid.dzC[1]
59+
@test ΔxF(1,1,1,grid) == grid.Δx
60+
@test ΔyF(1,1,1,grid) == grid.Δy
61+
@test ΔzF(1,1,1,grid) == grid.dzF[1]
62+
@test Ax(1,1,1,grid) == grid.Δy*grid.dzF[1]
63+
@test Ay(1,1,1,grid) == grid.Δx*grid.dzF[1]
64+
@test Az(1,1,1,grid) == grid.Δx*grid.Δy
65+
@test volume(1,1,1,grid) == grid.Δx*grid.Δy*grid.dzF[1]
66+
67+
return nothing
68+
end
69+
70+
function test_lat_lon_grid()
71+
grid = LatLonGrid(size = (360,160,10), lat = (-80,80), lon = (-180,180), z = (0,-20))
72+
@test grid.Nx == 360
73+
@test grid.Ny == 160
74+
@test grid.Nz == 10
75+
76+
@test length(grid.xC) == 360+2*2
77+
@test length(grid.yC) == 160+2*2
78+
@test length(grid.zC) == 10+2*2
79+
@test length(grid.xF) == 360+2*2
80+
@test length(grid.yF) == 160+2*2
81+
@test length(grid.zF) == 10+2*2
82+
end
83+
84+
function test_load_lat_lon_grid()
85+
MeshArrays.GRID_LL360_download();
86+
γ = MeshArrays.GridSpec("PeriodicChannel",MeshArrays.GRID_LL360);
87+
Γ = MeshArrays.GridLoad(γ;option="full");
88+
89+
grid_info=(XC=Γ.XC[1], YC=Γ.YC[1], RC=Γ.RC,
90+
XW=Γ.XW[1], YS=Γ.YS[1], RF=Γ.RF,
91+
DXC=Γ.DXC[1], DYC=Γ.DYC[1], DRC=Γ.DRC,
92+
DXG=Γ.DXG[1], DYG=Γ.DYG[1], DRF=Γ.DRF,
93+
RAW=Γ.RAW[1], RAS=Γ.RAS[1], RAC=Γ.RAC[1],
94+
hFacC=ones(360,160,50), hFacW=ones(360,160,50), hFacS=ones(360,160,50))
95+
96+
mask = ones(360,160,50)
97+
98+
grid = LoadLatLonGrid(;grid_info = grid_info, size = (360,160,50), lat = (-80,80), lon= (-180,180), landmask = mask)
99+
100+
@test grid.Nx == 360
101+
@test grid.Ny == 160
102+
@test grid.Nz == 50
103+
104+
@test length(grid.xC) == 360+2*2
105+
@test length(grid.yC) == 160+2*2
106+
@test length(grid.zC) == 50+2*2
107+
@test length(grid.xF) == 360+2*2
108+
@test length(grid.yF) == 160+2*2
109+
@test length(grid.zF) == 50+2*2
110+
111+
@test minimum(grid.dzF) == 10.0
112+
@test maximum(grid.dzF) == 456.5
113+
114+
return nothing
115+
end
116+
117+
function test_lat_lon_areas_volumes()
118+
grid = LatLonGrid(size = (360,160,10), lat = (-80,80), lon = (-180,180), z = (0,-20))
119+
@test ΔxC(1,1,1,grid) == grid.dxC[1,1]
120+
@test ΔyC(1,1,1,grid) == grid.dyC[1,1]
121+
@test ΔzC(1,1,1,grid) == grid.dzC[1]
122+
@test ΔxF(1,1,1,grid) == grid.dxF[1,1]
123+
@test ΔyF(1,1,1,grid) == grid.dyF[1,1]
124+
@test ΔzF(1,1,1,grid) == grid.dzF[1]
125+
@test Ax(1,1,1,grid) == grid.Ax[1,1,1]
126+
@test Ay(1,1,1,grid) == grid.Ay[1,1,1]
127+
@test Az(1,1,1,grid) == grid.Az[1,1]
128+
@test volume(1,1,1,grid) == grid.Vol[1,1,1]
129+
130+
return nothing
131+
end
132+
133+
@testset "Grids" begin
134+
@testset "Rectilinear Grid" begin
135+
test_rectilinear_grid()
136+
test_vertically_stretched_rectilinear_grid()
137+
test_rectilinear_areas_volumes()
138+
grid = RectilinearGrid(size = (4,6,2), x = (0,12), y = (0,12), z = (0,-8), halo = (2,2,2))
139+
@test try
140+
show(grid); println()
141+
true
142+
catch err
143+
println("error in show(::RectilinearGrid)")
144+
println(sprint(showerror, err))
145+
false
146+
end
147+
@test grid isa RectilinearGrid
148+
end
149+
@testset "Latitude Longitude Grid" begin
150+
test_lat_lon_grid()
151+
test_load_lat_lon_grid()
152+
test_lat_lon_areas_volumes()
153+
grid = LatLonGrid(size = (360,160,10), lat = (-80,80), lon = (-180,180), z = (0,-20))
154+
@test try
155+
show(grid); println()
156+
true
157+
catch err
158+
println("error in show(::LatLonGrid)")
159+
println(sprint(showerror, err))
160+
false
161+
end
162+
@test grid isa LatLonGrid
163+
end
164+
end

test/runtests.jl

+22-16
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,28 @@
11
using Test
22
using PlanktonIndividuals
33

4-
@testset "PlanktonIndividuals 3D tests:" begin
5-
include(dirname(pathof(PlanktonIndividuals))*"/../test/model_test_3D.jl")
6-
@test isapprox(TC,TCt; atol=1e2)
7-
end #@testset "PlanktonIndividuals 3D tests:" begin
4+
# @testset "PlanktonIndividuals 3D tests:" begin
5+
# include(dirname(pathof(PlanktonIndividuals))*"/../test/model_test_3D.jl")
6+
# @test isapprox(TC,TCt; atol=1e2)
7+
# end #@testset "PlanktonIndividuals 3D tests:" begin
88

9-
@testset "PlanktonIndividuals 2D tests:" begin
10-
include(dirname(pathof(PlanktonIndividuals))*"/../test/model_test_2D.jl")
11-
@test isapprox(TP,TPt; atol=1e1)
12-
end #@testset "PlanktonIndividuals 2D tests:" begin
9+
# @testset "PlanktonIndividuals 2D tests:" begin
10+
# include(dirname(pathof(PlanktonIndividuals))*"/../test/model_test_2D.jl")
11+
# @test isapprox(TP,TPt; atol=1e1)
12+
# end #@testset "PlanktonIndividuals 2D tests:" begin
1313

14-
@testset "PlanktonIndividuals 1D tests:" begin
15-
include(dirname(pathof(PlanktonIndividuals))*"/../test/model_test_1D.jl")
16-
@test isapprox(TP,TPt; atol=1e1)
17-
end #@testset "PlanktonIndividuals 1D tests:" begin
14+
# @testset "PlanktonIndividuals 1D tests:" begin
15+
# include(dirname(pathof(PlanktonIndividuals))*"/../test/model_test_1D.jl")
16+
# @test isapprox(TP,TPt; atol=1e1)
17+
# end #@testset "PlanktonIndividuals 1D tests:" begin
1818

19-
@testset "PlanktonIndividuals 0D tests:" begin
20-
include(dirname(pathof(PlanktonIndividuals))*"/../test/model_test_0D.jl")
21-
@test isapprox(TP,TPt; atol=1e1)
22-
end #@testset "PlanktonIndividuals 0D tests:" begin
19+
# @testset "PlanktonIndividuals 0D tests:" begin
20+
# include(dirname(pathof(PlanktonIndividuals))*"/../test/model_test_0D.jl")
21+
# @test isapprox(TP,TPt; atol=1e1)
22+
# end #@testset "PlanktonIndividuals 0D tests:" begin
23+
24+
@testset "PlanktonIndividuals" begin
25+
@testset "Unit tests" begin
26+
include("grid_test.jl")
27+
end
28+
end

0 commit comments

Comments
 (0)