Skip to content

Commit

Permalink
Merge pull request #21 from DJ4Earth/rewrite_compare_archive_tests
Browse files Browse the repository at this point in the history
change the structure of the tests
  • Loading branch information
enigne authored Jul 5, 2024
2 parents 6004072 + 26aa438 commit 74991a1
Show file tree
Hide file tree
Showing 9 changed files with 63 additions and 51 deletions.
Binary file added test/Data/SquareSheetConstrained.arch
Binary file not shown.
42 changes: 2 additions & 40 deletions test/runtests.jl
Original file line number Diff line number Diff line change
@@ -1,42 +1,7 @@
using DJUICE
using Test

function searchdir(path,key)
filter(x->occursin(key,x), readdir(path))
end

function compareArchive(tf::String, procedure::Symbol)
# run test
@inline include(tf)
id = match(r"test(\d+).jl", tf).captures[1]
archive_name = "Archive"*string(id)
archive_path = issmdir()*"/test/Archives/"*archive_name*".arch"
if procedure===:update
# update Archive
else
# check Archive
if isfile(archive_path)
for k=1:length(field_names)
# Get field and tolerance
field=field_values[k];
fieldname=field_names[k];
tolerance=field_tolerances[k];

# Compare to archive
if !isnan(tolerance)
# Our output is in the correct order (n,1) or (1,1), so we do not need to transpose again
archive = archread(archive_path, archive_name*"_field"*string(k))
error_diff = (maximum(abs.(archive-field))/(maximum(abs.(archive))+eps(Float64)))

@test isnan(error_diff) == false
@test error_diff < tolerance skip = isnan(tolerance)
end
end
else
@warn "$archive_name does not exist! Skip the comparison of the results"
end
end
end
include("utils.jl")

@time @testset "DJUICE" begin
@time @testset "Model Struct Tests" begin include("modelstructtests.jl") end
Expand All @@ -46,10 +11,7 @@ end
for tf in testsolutions
@time @testset "Model Solution Tests: $tf" begin
# check the results vs. saved archive
compareArchive(tf, :test)
include(tf)
end
end

# GPU test
#@time include("testGPU.jl")
end
2 changes: 2 additions & 0 deletions test/test101.jl
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using DJUICE
include("utils.jl")

md = model()
md = triangle(md,issmdir()*"/test/Exp/Square.exp",50000.)
Expand Down Expand Up @@ -50,3 +51,4 @@ field_tolerances=[4e-13,4e-13,4e-13]
field_values= [(md.results["StressbalanceSolution"]["Vx"]),
(md.results["StressbalanceSolution"]["Vy"]),
(md.results["StressbalanceSolution"]["Vel"]) ]
compareArchive(@__FILE__, field_names, field_tolerances, field_values, :test)
2 changes: 2 additions & 0 deletions test/test201.jl
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using DJUICE
include("utils.jl")

md = model()
md = triangle(md,issmdir()*"/test/Exp/Square.exp", 150000.)
Expand Down Expand Up @@ -55,3 +56,4 @@ field_tolerances=[2e-10,2e-10,2e-10]
field_values= [(md.results["StressbalanceSolution"]["Vx"]),
(md.results["StressbalanceSolution"]["Vy"]),
(md.results["StressbalanceSolution"]["Vel"]) ]
compareArchive(@__FILE__, field_names, field_tolerances, field_values, :test)
8 changes: 5 additions & 3 deletions test/test208.jl
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using DJUICE
include("utils.jl")

md = model()
md = triangle(md,issmdir()*"/test/Exp/Square.exp", 150000.)
Expand Down Expand Up @@ -65,10 +66,11 @@ field_tolerances=[2e-10,2e-10,2e-10,NaN,2e-10,2e-10,2e-10,NaN,NaN]
field_values= [(md.results["TransientSolution"][1]["Vx"]),
(md.results["TransientSolution"][1]["Vy"]),
(md.results["TransientSolution"][1]["Vel"]),
(NaN),
([NaN]),
(md.results["TransientSolution"][1]["Base"]),
(md.results["TransientSolution"][1]["Surface"]),
(md.results["TransientSolution"][1]["Thickness"]),
(NaN),
(NaN),
([NaN]),
([NaN]),
]
compareArchive(@__FILE__, field_names, field_tolerances, field_values, :test)
18 changes: 10 additions & 8 deletions test/test301.jl
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using DJUICE
include("utils.jl")

md = model()
md = triangle(md,issmdir()*"/test/Exp/Square.exp",150000.)
Expand All @@ -17,13 +18,13 @@ md.geometry.surface = md.geometry.base+md.geometry.thickness
md.geometry.bed = md.geometry.base

#Initial velocity
#x = archread(issmdir()*"/test/Data/SquareShelfConstrained.arch","x")
#y = archread(issmdir()*"/test/Data/SquareShelfConstrained.arch","y")
#vx = archread(issmdir()*"/test/Data/SquareShelfConstrained.arch","vx")
#vy = archread(issmdir()*"/test/Data/SquareShelfConstrained.arch","vy")
#index = archread(issmdir()*"/test/Data/SquareShelfConstrained.arch","index")
md.initialization.vx=zeros(md.mesh.numberofvertices)#InterpFromMeshToMesh2d(index,x,y,vx,md.mesh.x,md.mesh.y)
md.initialization.vy=zeros(md.mesh.numberofvertices)#InterpFromMeshToMesh2d(index,x,y,vy,md.mesh.x,md.mesh.y)
x = archread(issmdir()*"/test/Data/SquareSheetConstrained.arch","x")
y = archread(issmdir()*"/test/Data/SquareSheetConstrained.arch","y")
vx = archread(issmdir()*"/test/Data/SquareSheetConstrained.arch","vx")
vy = archread(issmdir()*"/test/Data/SquareSheetConstrained.arch","vy")
index = Int.(archread(issmdir()*"/test/Data/SquareSheetConstrained.arch","index"))
md.initialization.vx=InterpFromMeshToMesh2d(index,x,y,vx,md.mesh.x,md.mesh.y,0.0)
md.initialization.vy=InterpFromMeshToMesh2d(index,x,y,vy,md.mesh.x,md.mesh.y,0.0)

md.materials.rheology_B=1.815730284801701e+08*ones(md.mesh.numberofvertices)
md.materials.rheology_n=3*ones(md.mesh.numberofelements)
Expand All @@ -45,7 +46,8 @@ md.stressbalance.spcvy[pos] .= 0.0
md=solve(md,:Stressbalance)

field_names =["Vx","Vy","Vel"]
field_tolerances=[NaN,NaN,NaN]
field_tolerances=[2e-10,1e-10,1e-10]
field_values= [(md.results["StressbalanceSolution"]["Vx"]),
(md.results["StressbalanceSolution"]["Vy"]),
(md.results["StressbalanceSolution"]["Vel"]) ]
compareArchive(@__FILE__, field_names, field_tolerances, field_values, :test)
2 changes: 2 additions & 0 deletions test/test501.jl
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using DJUICE
include("utils.jl")

md = model()
md = triangle(md,issmdir()*"/test/Exp/Pig.exp",20000.)
Expand Down Expand Up @@ -54,3 +55,4 @@ field_tolerances=[NaN,NaN,NaN]
field_values= [(md.results["StressbalanceSolution"]["Vx"]),
(md.results["StressbalanceSolution"]["Vy"]),
(md.results["StressbalanceSolution"]["Vel"]) ]
compareArchive(@__FILE__, field_names, field_tolerances, field_values, :test)
3 changes: 3 additions & 0 deletions test/test807.jl
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using DJUICE
include("utils.jl")

md = model()
md = triangle(md,issmdir()*"/test/Exp/Square.exp",200000.)
Expand Down Expand Up @@ -72,3 +73,5 @@ md.levelset.spclevelset=NaN*ones(md.mesh.numberofvertices)
md.levelset.spclevelset[pos] = md.mask.ice_levelset[pos]

md=solve(md,:Transient)
compareArchive(@__FILE__, field_names, field_tolerances, field_values, :test)

37 changes: 37 additions & 0 deletions test/utils.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
using DJUICE
using Test

function searchdir(path,key)
filter(x->occursin(key,x), readdir(path))
end

function compareArchive(tf::String, field_names::Vector{String}, field_tolerances::Vector{Float64}, field_values::Vector{Vector{Float64}}, procedure::Symbol)
# make the test
id = match(r"test(\d+).jl", tf).captures[1]
archive_name = "Archive"*string(id)
archive_path = issmdir()*"/test/Archives/"*archive_name*".arch"
if procedure===:update
# TODO: update Archive
else
# check Archive
if isfile(archive_path)
@time @testset " Compare with $archive_name" begin
for k=1:length(field_names)
# Compare to archive
if !isnan(field_tolerances[k])
@time @testset " $(field_names[k]): " begin
# Our output is in the correct order (n,1) or (1,1), so we do not need to transpose again
archive = archread(archive_path, archive_name*"_field"*string(k))
error_diff = (maximum(abs.(archive-field_values[k]))/(maximum(abs.(archive))+eps(Float64)))

@test isnan(error_diff) == false
@test error_diff < field_tolerances[k] skip = isnan(field_tolerances[k])
end
end
end
end
else
@warn "$archive_name does not exist! Skip the comparison of the results"
end
end
end

0 comments on commit 74991a1

Please sign in to comment.