Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix type error in grid generators and add some test functions for gri… #973

Merged
24 changes: 18 additions & 6 deletions src/Grid/grid_generators.jl
Original file line number Diff line number Diff line change
Expand Up @@ -67,9 +67,14 @@ function generate_grid(::Type{QuadraticLine}, nel::NTuple{1,Int}, left::Vec{1,T}
return Grid(cells, nodes, facetsets=facetsets)
end

function _generate_2d_nodes!(nodes, nx, ny, LL, LR, UR, UL)
function _generate_2d_nodes!(nodes::Vector{Node{2, T}}, nx, ny, LL, LR, UR, UL) where T
for i in 0:ny-1
ratio_bounds = i / (ny-1)

Abdelrahman912 marked this conversation as resolved.
Show resolved Hide resolved
# This float division will be by default Float64,
Abdelrahman912 marked this conversation as resolved.
Show resolved Hide resolved
# so we need to convert it to the same type as element type of LL
# e.g. LL = Vec{2,Float16} -> eltype(LL) = Float16
# ratio_bounds = (i / (ny-1)) # old code
ratio_bounds = convert(T, i) / (ny-1)

x0 = LL[1] * (1 - ratio_bounds) + ratio_bounds * UL[1]
x1 = LR[1] * (1 - ratio_bounds) + ratio_bounds * UR[1]
Expand All @@ -78,7 +83,11 @@ function _generate_2d_nodes!(nodes, nx, ny, LL, LR, UR, UL)
y1 = LR[2] * (1 - ratio_bounds) + ratio_bounds * UR[2]

for j in 0:nx-1
ratio = j / (nx-1)
# This float division will be by default Float64,
# so we need to convert it to the same type as element type of LL
# e.g. LL = Vec{2,Float16} -> eltype(LL) = Float16
# ratio = j / (nx-1) # old code
ratio = convert(T, j) / (nx-1)
x = x0 * (1 - ratio) + ratio * x1
y = y0 * (1 - ratio) + ratio * y1
push!(nodes, Node((x, y)))
Expand Down Expand Up @@ -284,9 +293,12 @@ function generate_grid(::Type{Pyramid}, nel::NTuple{3,Int}, left::Vec{3,T}=Vec{3

#Center node in each "voxel"
for k in 1:nel_z, j in 1:nel_y, i in 1:nel_x
midx = 0.5(coords_x[i+1] + coords_x[i])
midy = 0.5(coords_y[j+1] + coords_y[j])
midz = 0.5(coords_z[k+1] + coords_z[k])
# midx = 0.5(coords_x[i+1] + coords_x[i]) # old code, this will be always Float64 regardless of T
Abdelrahman912 marked this conversation as resolved.
Show resolved Hide resolved
# midy = 0.5(coords_y[j+1] + coords_y[j]) # old code, this will be always Float64 regardless of T
# midz = 0.5(coords_z[k+1] + coords_z[k]) # old code, this will be always Float64 regardless of T
midx = (coords_x[i+1] + coords_x[i]) / 2
midy = (coords_y[j+1] + coords_y[j]) / 2
midz = (coords_z[k+1] + coords_z[k]) / 2
push!(nodes, Node((midx, midy, midz)))
end

Expand Down
1 change: 1 addition & 0 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ include("test_constraints.jl")
include("test_grid_dofhandler_vtk.jl")
include("test_vtk_export.jl")
include("test_abstractgrid.jl")
include("test_grid_generators.jl")
include("test_grid_addboundaryset.jl")
include("test_mixeddofhandler.jl")
include("test_l2_projection.jl")
Expand Down
24 changes: 24 additions & 0 deletions test/test_grid_generators.jl
termi-official marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
# Helper function to test grid generation for a given floating-point type
function test_generate_grid(T::Type)

# Define the cell types to test
cell_types = [Line, QuadraticLine, Quadrilateral,QuadraticQuadrilateral,Triangle,QuadraticTriangle,Hexahedron,Wedge,Pyramid,Tetrahedron,SerendipityQuadraticHexahedron]
Abdelrahman912 marked this conversation as resolved.
Show resolved Hide resolved

# Loop over all cell types and test grid generation
for CT in cell_types
rdim = Ferrite.getrefdim(CT)
nels = ntuple(i -> 2, rdim)
left = - ones(Vec{rdim,T})
right = ones(Vec{rdim,T})
grid = generate_grid(CT, nels, left, right)
@test isa(grid, Grid{rdim, CT, T})
end

end

# Run tests for different floating-point types
@testset "Generate Grid Tests" begin
test_generate_grid(Float64)
test_generate_grid(Float32)
test_generate_grid(Float16)
fredrikekre marked this conversation as resolved.
Show resolved Hide resolved
end