Skip to content

Commit

Permalink
Increase test coverage.
Browse files Browse the repository at this point in the history
  • Loading branch information
termi-official committed Nov 7, 2023
1 parent 9bc91f4 commit 83ffa96
Showing 1 changed file with 74 additions and 36 deletions.
110 changes: 74 additions & 36 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ function Ferrite.function_value(::MatrixValued, fe_v::Ferrite.Values{dim}, q_poi
return val
end

@testset "gradient fields" begin
@testset "utility operations" begin
# Check scalar problems
for (num_elements_per_dim, geo, ip) [
(4,Triangle, Lagrange{2,RefTetrahedron,1}()),
Expand All @@ -42,7 +42,7 @@ end
(4,Hexahedron, Lagrange{3,RefCube,1}()),
(2,Hexahedron, Lagrange{3,RefCube,2}())
]
@testset failfast=true "scalar($num_elements_per_dim, $geo, $ip)" begin
@testset failfast=true "scalar($num_elements_per_dim, $geo, $ip)" begin
# Get solution
dim = Ferrite.getdim(ip)
grid = generate_grid(geo, ntuple(x->num_elements_per_dim, dim));
Expand All @@ -55,28 +55,46 @@ end
f_ana(x) = sum(0.5 * x.^2)
Ferrite.apply_analytical!(u, dh, :u, f_ana)

@testset "solution fields" begin
plotter = FerriteViz.MakiePlotter(dh,u)
data = FerriteViz.transfer_solution(plotter,u,process=x->x)[:,1]
visible_nodes = .!isnan.(data)# TODO add API
@test isapprox(data[visible_nodes], f_ana.(plotter.physical_coords[visible_nodes]); atol=_test_tolerance(ip))
end

# Compute gradient/flux field
(dh_grad, u_grad) = FerriteViz.interpolate_gradient_field(dh, u, :u)

# Check gradient of solution
qr = QuadratureRule{dim,Ferrite.getrefshape(ip)}(2)
ip_geo = Ferrite.default_interpolation(geo)
ip_grad = Ferrite.getfieldinterpolation(dh_grad, Ferrite.find_field(dh_grad, :gradient))
cellvalues_grad = Ferrite.CellVectorValues(qr, ip_grad, ip_geo)
for cell in CellIterator(dh_grad)
reinit!(cellvalues_grad, cell)
coords = getcoordinates(cell)
uₑ = @views u_grad[celldofs(cell)]
for q_point in 1:getnquadpoints(cellvalues_grad)
x = spatial_coordinate(cellvalues_grad, q_point, coords)
uₐₚₚᵣₒₓ = function_value(cellvalues_grad, q_point, uₑ)
uₐₙₐ = Tensors.gradient(f_ana, x)
@test isapprox(uₐₙₐ, uₐₚₚᵣₒₓ;atol=_test_tolerance(ip))
@testset "gradient fields" begin
(dh_grad, u_grad) = FerriteViz.interpolate_gradient_field(dh, u, :u)

# Check gradient of solution
qr = QuadratureRule{dim,Ferrite.getrefshape(ip)}(2)
ip_geo = Ferrite.default_interpolation(geo)
ip_grad = Ferrite.getfieldinterpolation(dh_grad, Ferrite.find_field(dh_grad, :gradient))
cellvalues_grad = Ferrite.CellVectorValues(qr, ip_grad, ip_geo)
for cell in CellIterator(dh_grad)
reinit!(cellvalues_grad, cell)
coords = getcoordinates(cell)
uₑ = @views u_grad[celldofs(cell)]
for q_point in 1:getnquadpoints(cellvalues_grad)
x = spatial_coordinate(cellvalues_grad, q_point, coords)
uₐₚₚᵣₒₓ = function_value(cellvalues_grad, q_point, uₑ)
uₐₙₐ = Tensors.gradient(f_ana, x)
@test isapprox(uₐₙₐ, uₐₚₚᵣₒₓ;atol=_test_tolerance(ip))
end
end

# Check for correct transfer
plotter_grad = FerriteViz.MakiePlotter(dh_grad,u_grad)
data_grad = FerriteViz.transfer_solution(plotter_grad,u_grad; field_name=:gradient, process=x->x)
visible_nodes_grad = .!isnan.(data_grad)
for i 1:size(data_grad, 1)
!visible_nodes_grad[i] && continue
@test isapprox(Vec{dim}(data_grad[i,:]), Tensors.gradient(f_ana, Vec{dim}(plotter_grad.physical_coords[i])); atol=_test_tolerance(ip))
end
end
end

@testset "vector($num_elements_per_dim, $geo, $ip)" begin
@testset failfast=true "vector($num_elements_per_dim, $geo, $ip)" begin
# Get solution
dim = Ferrite.getdim(ip)
grid = generate_grid(geo, ntuple(x->num_elements_per_dim, dim));
Expand All @@ -89,24 +107,44 @@ end
u = Vector{Float64}(undef, ndofs(dh))
Ferrite.apply_analytical!(u, dh, :u, f_ana)

@testset "solution fields" begin
plotter = FerriteViz.MakiePlotter(dh,u)
data = FerriteViz.transfer_solution(plotter,u,process=x->x)
visible_nodes = .!isnan.(data)# TODO add API
for i 1:size(data, 1)
!visible_nodes[i] && continue
@test isapprox(Vec{dim}(data[i,:]), f_ana(Vec{dim}(plotter.physical_coords[i])); atol=_test_tolerance(ip))
end
end

# Compute gradient/flux field
(dh_grad, u_grad) = FerriteViz.interpolate_gradient_field(dh, u, :u)

# Check gradient of solution
qr = QuadratureRule{dim,Ferrite.getrefshape(ip)}(2)
ip_geo = Ferrite.default_interpolation(geo)
ip_grad = Ferrite.getfieldinterpolation(dh_grad, Ferrite.find_field(dh_grad, :gradient))
cellvalues_grad = Ferrite.CellScalarValues(qr, ip_grad, ip_geo)

for cell in CellIterator(dh_grad)
reinit!(cellvalues_grad, cell)
coords = getcoordinates(cell)
uₑ = @views u_grad[celldofs(cell)]
for q_point in 1:getnquadpoints(cellvalues_grad)
x = spatial_coordinate(cellvalues_grad, q_point, coords)
uₐₚₚᵣₒₓ = function_value(MatrixValued(), cellvalues_grad, q_point, uₑ)
uₐₙₐ = Tensors.gradient(f_ana, x)
@test isapprox(uₐₙₐ, uₐₚₚᵣₒₓ;atol=_test_tolerance(ip))
@testset "gradient fields" begin
(dh_grad, u_grad) = FerriteViz.interpolate_gradient_field(dh, u, :u)

# Check gradient of solution
qr = QuadratureRule{dim,Ferrite.getrefshape(ip)}(2)
ip_geo = Ferrite.default_interpolation(geo)
ip_grad = Ferrite.getfieldinterpolation(dh_grad, Ferrite.find_field(dh_grad, :gradient))
cellvalues_grad = Ferrite.CellScalarValues(qr, ip_grad, ip_geo)
for cell in CellIterator(dh_grad)
reinit!(cellvalues_grad, cell)
coords = getcoordinates(cell)
uₑ = @views u_grad[celldofs(cell)]
for q_point in 1:getnquadpoints(cellvalues_grad)
x = spatial_coordinate(cellvalues_grad, q_point, coords)
uₐₚₚᵣₒₓ = function_value(MatrixValued(), cellvalues_grad, q_point, uₑ)
uₐₙₐ = Tensors.gradient(f_ana, x)
@test isapprox(uₐₙₐ, uₐₚₚᵣₒₓ;atol=_test_tolerance(ip))
end
end

# Check for correct transfer
plotter_grad = FerriteViz.MakiePlotter(dh_grad,u_grad)
data_grad = FerriteViz.transfer_solution(plotter_grad,u_grad; field_name=:gradient, process=x->x)
visible_nodes_grad = .!isnan.(data_grad)
for i 1:size(data_grad, 1)
!visible_nodes_grad[i] && continue
@test isapprox(Tensor{2,dim}(data_grad[i,:]), Tensors.gradient(f_ana, Vec{dim}(plotter_grad.physical_coords[i])); atol=_test_tolerance(ip))
end
end
end
Expand Down

0 comments on commit 83ffa96

Please sign in to comment.