Skip to content

Commit

Permalink
Use the numeric type of the input nodes as the output type in grid ge…
Browse files Browse the repository at this point in the history
…nerators (#973)
  • Loading branch information
Abdelrahman912 authored Jun 7, 2024
1 parent 3ae61fa commit 44e43d8
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 6 deletions.
12 changes: 6 additions & 6 deletions src/Grid/grid_generators.jl
Original file line number Diff line number Diff line change
Expand Up @@ -67,9 +67,9 @@ 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)
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 +78,7 @@ 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)
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 +284,9 @@ 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 = (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
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]

# 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)
end

0 comments on commit 44e43d8

Please sign in to comment.