From d5215fbed3ce177570430da7eef4be464e5382f1 Mon Sep 17 00:00:00 2001 From: Petr Krysl Date: Sat, 25 May 2024 18:50:19 -0700 Subject: [PATCH] add mesh validation function --- Project.toml | 2 +- src/FinEtools.jl | 6 +++-- src/MeshModificationModule.jl | 45 ++++++++++++++++++++++++++++++++++- test/test_basics.jl | 22 ++++++++++++++++- 4 files changed, 70 insertions(+), 5 deletions(-) diff --git a/Project.toml b/Project.toml index ce4cf3dc..01b1b2df 100644 --- a/Project.toml +++ b/Project.toml @@ -1,7 +1,7 @@ name = "FinEtools" uuid = "91bb5406-6c9a-523d-811d-0644c4229550" authors = ["Petr Krysl "] -version = "8.0.22" +version = "8.0.23" [deps] DataDrop = "aa547a04-dd37-49ab-8e73-656744f8a8fc" diff --git a/src/FinEtools.jl b/src/FinEtools.jl index 412270fc..2f3bb523 100644 --- a/src/FinEtools.jl +++ b/src/FinEtools.jl @@ -306,7 +306,8 @@ using .MeshModificationModule: distortblock, outer_surface_of_solid, reordermesh, - element_coloring + element_coloring, + validate_mesh # Exported: extraction of boundary, fusing of nodes and merging of meshes, mesh smoothing, node partitioning export meshboundary, fusenodes, @@ -323,7 +324,8 @@ export meshboundary, distortblock, outer_surface_of_solid, reordermesh, - element_coloring + element_coloring, + validate_mesh using .MeshImportModule: import_NASTRAN, import_ABAQUS, import_MESH, import_H5MESH # Exported: mesh import functions diff --git a/src/MeshModificationModule.jl b/src/MeshModificationModule.jl index 985dd7fc..c678e57d 100644 --- a/src/MeshModificationModule.jl +++ b/src/MeshModificationModule.jl @@ -21,7 +21,7 @@ import ..FESetModule: subset import ..FENodeSetModule: FENodeSet import ..BoxModule: boundingbox, inflatebox!, intersectboxes, inbox -import ..MeshSelectionModule: connectednodes, selectelem +import ..MeshSelectionModule: connectednodes, selectelem, findunconnnodes import ..SurfaceNormalModule: SurfaceNormal, updatenormal! using Base.Sort using Base.Order @@ -1357,4 +1357,47 @@ function element_coloring!(element_colors, unique_colors, color_counts, fes, n2e return element_colors, unique_colors, color_counts end +""" + validate_mesh(fens, fes) + +Validate the given mesh by checking if it satisfies certain sanity criteria. + +# Arguments +- `fens`: The finite element nodes of the mesh. +- `fes`: The finite elements of the mesh. + +Validate finite element mesh. + +A finite element mesh given by the node set and the finite element set is +validated by checking the sanity of the numbering: +- the node numbers need to be positive and in serial order +- the fe connectivity needs to refer to valid nodes +- the finite element nodes need to be connected to at least one finite element + +An error is reported as soon as it is detected. + +# Returns +A boolean indicating whether the mesh is valid or not. +""" +function validate_mesh(fens, fes) + totnfens = count(fens) + for i in eachindex(fes) + if (max(fes.conn[i]...) > totnfens) + error("Wrong connectivity (refers to nonexistent node): $(fes.conn[i])") + end + if (min(fes.conn[i]...) < 1) + error("Wrong connectivity (refers to nonexistent node): $(fes.conn[i])") + end + if (length(unique(fes.conn[i])) != length(fes.conn[i])) + error("Wrong connectivity (multiply referenced node): $(fes.conn[i])") + end + end + connected = findunconnnodes(fens, fes) + if (any(connected == 0)) + error("Unconnected nodes present: $(sum(connected==0)) total") + end + return true +end + + end # module diff --git a/test/test_basics.jl b/test/test_basics.jl index b275a41e..2592251f 100644 --- a/test/test_basics.jl +++ b/test/test_basics.jl @@ -1,3 +1,24 @@ +module mval001 +using Test +using FinEtools +function test() + fens = FENodeSet(rand(5, 2)) + fes = FESetT3([1 2 6; 3 4 5]) + @test_throws ErrorException validate_mesh(fens, fes) + fes = FESetT3([1 2 6; -1 4 5]) + @test_throws ErrorException validate_mesh(fens, fes) + fes = FESetT3([1 2 3; 0 4 5]) + @test_throws ErrorException validate_mesh(fens, fes) + fes = FESetT3([1 2 3; 0 4 5]) + @test_throws ErrorException validate_mesh(fens, fes) + fes = FESetT3([1 2 3; 2 4 5]) + @test validate_mesh(fens, fes) + true +end +test() +nothing +end + module mbas113 using FinEtools using Test @@ -3015,4 +3036,3 @@ end test() nothing end -