-
Notifications
You must be signed in to change notification settings - Fork 1
/
FerriteMeshParser.jl
99 lines (84 loc) · 3.26 KB
/
FerriteMeshParser.jl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
module FerriteMeshParser
using Ferrite
# Convenience when debugging
const DEBUG_PARSE = false
# Mesh formats
struct AutomaticMeshFormat end
struct AbaqusMeshFormat end
# Exceptions
struct UndetectableMeshFormatError <: Exception
filename::String
end
Base.showerror(io::IO, e::UndetectableMeshFormatError) = println(io, "Couldn't automatically detect mesh format in $(e.filename)")
struct UnsupportedElementType <: Exception
elementtype::String
end
Base.showerror(io::IO, e::UnsupportedElementType) = println(io, "The element type \"$(e.elementtype)\" is not supported or given in user_elements")
struct InvalidFileContent <: Exception
msg::String
end
Base.showerror(io::IO, e::InvalidFileContent) = println(io, e.msg)
include("rawmesh.jl")
include("elements.jl")
include("reading_utils.jl")
include("abaqusreader.jl")
include("gridcreator.jl")
"""
function get_ferrite_grid(
filename;
meshformat=AutomaticMeshFormat(),
user_elements=Dict{String,DataType}(),
generate_facesets=true
)
Create a `Ferrite.Grid` by reading in the file specified by `filename`.
Optional arguments:
* `meshformat`: Which format the mesh
is given in, normally automatically detected by the file extension
* `user_elements`: Used to add extra elements not supported,
might require a separate cell constructor.
* `generate_facesets`: Should facesets be automatically generated from all nodesets?
"""
function get_ferrite_grid(filename; meshformat=AutomaticMeshFormat(), user_elements::Dict{String,DataType}=Dict{String,DataType}(), generate_facesets::Bool=true)
detected_format = detect_mesh_format(filename, meshformat)
mesh = read_mesh(filename, detected_format)
checkmesh(mesh)
grid = create_grid(mesh, detected_format, user_elements)
generate_facesets && generate_facesets!(grid)
return grid
end
"""
create_faceset(
grid::Ferrite.AbstractGrid,
nodeset::Set{Int},
cellset::Union{UnitRange{Int},Set{Int}}=1:getncells(grid)
)
Find the faces in the grid for which all nodes are in `nodeset`. Return them as a `Set{FaceIndex}`.
A `cellset` can be given to only look only for faces amongst those cells to speed up the computation.
Otherwise the search is over all cells.
This function is normally only required when calling `get_ferrite_grid` with `generate_facesets=false`.
The created `faceset` can be added to the grid as `addfaceset!(grid, "facesetkey", faceset)`
"""
function create_faceset(grid::Ferrite.AbstractGrid, nodeset::AbstractSet{Int}, cellset=1:getncells(grid))
faceset = sizehint!(Set{FaceIndex}(), length(nodeset))
for (cellid, cell) in enumerate(getcells(grid))
cellid ∈ cellset || continue
if any(n-> n ∈ nodeset, cell.nodes)
for (faceid, face) in enumerate(Ferrite.faces(cell))
if all(n -> n ∈ nodeset, face)
push!(faceset, FaceIndex(cellid, faceid))
end
end
end
end
return faceset
end
detect_mesh_format(_, meshformat) = meshformat
function detect_mesh_format(filename, ::AutomaticMeshFormat)
if endswith(filename, ".inp")
return AbaqusMeshFormat()
else
throw(UndetectableMeshFormatError(filename))
end
end
export get_ferrite_grid, create_faceset
end