Skip to content

Commit

Permalink
Support new Ferrite defintiions with facets (#36)
Browse files Browse the repository at this point in the history
* Support facets or faces depending on Ferrite version

* Avoid overwriting const facets

* Don't use deprecated function in tests / readme

* Apply suggestions from code review

Co-authored-by: Maximilian Köhler <[email protected]>

* One more face

---------

Co-authored-by: Maximilian Köhler <[email protected]>
  • Loading branch information
KnutAM and koehlerson authored May 27, 2024
1 parent 0910809 commit f9263ff
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 37 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ domaincellset = cellsets["Domain"]
elements = elements[collect(domaincellset)]

boundarydict = toboundary(facedim)
facesets = tofacesets(boundarydict, elements)
facesets = tofacetsets(boundarydict, elements)
gmsh.finalize()

Grid(elements, nodes, facesets=facesets, cellsets=cellsets)
Expand Down
80 changes: 46 additions & 34 deletions src/FerriteGmsh.jl
Original file line number Diff line number Diff line change
@@ -1,25 +1,30 @@
module FerriteGmsh

using Ferrite
using Ferrite:
Ferrite, Grid, Node, Vec
using Gmsh: Gmsh, gmsh

const gmshtoferritecell = Dict("Line 2" => Line,
"Line 3" => QuadraticLine,
"Triangle 3" => Triangle,
"Triangle 6" => QuadraticTriangle,
"Quadrilateral 4" => Quadrilateral,
"Quadrilateral 9" => QuadraticQuadrilateral,
"Tetrahedron 4" => Tetrahedron,
"Tetrahedron 10" => QuadraticTetrahedron,
"Hexahedron 8" => Hexahedron,
"Hexahedron 20" => Cell{3,20,6})
# Compat for Ferrite before v1.0
const FacetIndex = isdefined(Ferrite, :FacetIndex) ? Ferrite.FacetIndex : Ferrite.FaceIndex
const facets = isdefined(Ferrite, :facets) ? Ferrite.facets : Ferrite.faces

const gmshtoferritecell = Dict("Line 2" => Ferrite.Line,
"Line 3" => Ferrite.QuadraticLine,
"Triangle 3" => Ferrite.Triangle,
"Triangle 6" => Ferrite.QuadraticTriangle,
"Quadrilateral 4" => Ferrite.Quadrilateral,
"Quadrilateral 9" => Ferrite.QuadraticQuadrilateral,
"Tetrahedron 4" => Ferrite.Tetrahedron,
"Tetrahedron 10" => Ferrite.QuadraticTetrahedron,
"Hexahedron 8" => Ferrite.Hexahedron,
"Hexahedron 20" => Ferrite.Cell{3,20,6})

function translate_elements(original_elements)
return original_elements
end

function translate_elements(original_elements::Vector{QuadraticTetrahedron})
ferrite_elements = QuadraticTetrahedron[]
function translate_elements(original_elements::Vector{Ferrite.QuadraticTetrahedron})
ferrite_elements = Ferrite.QuadraticTetrahedron[]
for original_ele in original_elements
push!(ferrite_elements,QuadraticTetrahedron((original_ele.nodes[1],
original_ele.nodes[2],
Expand All @@ -35,10 +40,11 @@ function translate_elements(original_elements::Vector{QuadraticTetrahedron})
return ferrite_elements
end

function translate_elements(original_elements::Vector{Cell{3,20,6}})
ferrite_elements = Cell{3,20,6}[]
function translate_elements(original_elements::Vector{Ferrite.Cell{3,20,6}})
ferrite_elements = Ferrite.Cell{3,20,6}[]
for original_ele in original_elements
push!(ferrite_elements,Cell{3,20,6}((original_ele.nodes[1],
push!(ferrite_elements,Ferrite.Cell{3,20,6}((
original_ele.nodes[1],
original_ele.nodes[2],
original_ele.nodes[3],
original_ele.nodes[4],
Expand Down Expand Up @@ -112,27 +118,27 @@ function toboundary(dim::Int)
return boundarydict
end

function _add_to_facesettuple!(facesettuple::Set{FaceIndex}, boundaryface::Tuple, faces)
for (eleidx, elefaces) in enumerate(faces)
if any(issubset.(elefaces, (boundaryface,)))
localface = findfirst(x -> issubset(x,boundaryface), elefaces)
push!(facesettuple, FaceIndex(eleidx, localface))
function _add_to_facetsettuple!(facetsettuple::Set{FacetIndex}, boundaryfacet::Tuple, element_facets)
for (eleidx, elefacets) in enumerate(element_facets)
if any(issubset.(elefacets, (boundaryfacet,)))
localfacet = findfirst(x -> issubset(x,boundaryfacet), elefacets)
push!(facetsettuple, FacetIndex(eleidx, localfacet))
end
end
return facesettuple
return facetsettuple
end

function tofacesets(boundarydict::Dict{String,Vector}, elements::Vector{<:Ferrite.AbstractCell})
faces = Ferrite.faces.(elements)
facesets = Dict{String,Set{FaceIndex}}()
for (boundaryname, boundaryfaces) in boundarydict
facesettuple = Set{FaceIndex}()
for boundaryface in boundaryfaces
_add_to_facesettuple!(facesettuple, boundaryface, faces)
function tofacetsets(boundarydict::Dict{String,Vector}, elements::Vector{<:Ferrite.AbstractCell})
element_facets = facets.(elements)
facetsets = Dict{String,Set{FacetIndex}}()
for (boundaryname, boundaryfacets) in boundarydict
facetsettuple = Set{FacetIndex}()
for boundaryfacet in boundaryfacets
_add_to_facetsettuple!(facetsettuple, boundaryfacet, element_facets)
end
facesets[boundaryname] = facesettuple
facetsets[boundaryname] = facetsettuple
end
return facesets
return facetsets
end

function tocellsets(dim::Int, global_elementtags::Vector{Int})
Expand Down Expand Up @@ -209,15 +215,21 @@ function togrid(; domain="")
end

boundarydict = toboundary(facedim)
facesets = tofacesets(boundarydict, elements)
facetsets = tofacetsets(boundarydict, elements)
# reset the save_all flag to the default value
if !saveall_flag
gmsh.option.setNumber("Mesh.SaveAll",0)
end
return Grid(elements, nodes, facesets=facesets, cellsets=cellsets)
@static if isdefined(Ferrite, :FacetIndex)
return Grid(elements, nodes, facetsets=facetsets, cellsets=cellsets)
else # Compat for Ferrite before v1.0
return Grid(elements, nodes, facesets=facetsets, cellsets=cellsets)
end
end

export gmsh
export tonodes, toelements, toboundary, tofacesets, tocellsets, togrid
export tonodes, toelements, toboundary, tofacetsets, tocellsets, togrid

@deprecate tofacesets tofacetsets

end
2 changes: 1 addition & 1 deletion test/devtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ gmsh.model.mesh.generate(2)
nodes = tonodes()
elements, gmsh_eleidx = toelements(2)
boundarydict = toboundary(1)
faceset = tofacesets(boundarydict, elements)
faceset = tofacetsets(boundarydict, elements)

grid = Grid(elements,nodes,facesets=faceset)

Expand Down
2 changes: 1 addition & 1 deletion test/test_mixed_mesh.jl
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@
nodes = tonodes()
elements, gmsh_eleidx = toelements(2)
boundarydict = toboundary(1)
facesets = tofacesets(boundarydict,elements)
facesets = tofacetsets(boundarydict,elements)
cellsets = tocellsets(2,gmsh_eleidx)
grid = Grid(elements,nodes,facesets=facesets,cellsets=cellsets)
@test grid.facesets["bottom"] == Set([FaceIndex(15,1),FaceIndex(17,1)])
Expand Down

0 comments on commit f9263ff

Please sign in to comment.