Skip to content

Commit

Permalink
Run runic
Browse files Browse the repository at this point in the history
  • Loading branch information
KnutAM committed Jul 23, 2024
1 parent fd138ca commit 0c723c7
Show file tree
Hide file tree
Showing 11 changed files with 88 additions and 86 deletions.
4 changes: 2 additions & 2 deletions docs/generate.jl
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ mkpath(GENERATEDDIR)
supplementary_fileextensions = [".inp", ".svg", ".png", ".jpg", ".gif"]
for example in readdir(EXAMPLEDIR)
if any(endswith.(example, supplementary_fileextensions))
cp(joinpath(EXAMPLEDIR, example), joinpath(GENERATEDDIR, example); force=true)
cp(joinpath(EXAMPLEDIR, example), joinpath(GENERATEDDIR, example); force = true)
end
end

Expand All @@ -21,7 +21,7 @@ for example in readdir(EXAMPLEDIR)

# remove "hidden" lines which are not shown in the markdown
line_ending_symbol = occursin(code, "\r\n") ? "\r\n" : "\n"
code_clean = join(filter(x->!endswith(x,"#hide"),split(code, r"\n|\r\n")), line_ending_symbol)
code_clean = join(filter(x -> !endswith(x, "#hide"), split(code, r"\n|\r\n")), line_ending_symbol)

mdpost(str) = replace(str, "@__CODE__" => code_clean)
Literate.markdown(input, GENERATEDDIR, postprocess = mdpost)
Expand Down
33 changes: 18 additions & 15 deletions docs/make.jl
Original file line number Diff line number Diff line change
Expand Up @@ -4,29 +4,32 @@ using Documenter
const is_ci = get(ENV, "CI", "false") == "true"

include("generate.jl")
GENERATEDEXAMPLES = [joinpath("examples", f) for f in (
"compact_tension.md",
"user_element.md")]
GENERATEDEXAMPLES = [
joinpath("examples", f) for f in (
"compact_tension.md",
"user_element.md",
)
]

DocMeta.setdocmeta!(FerriteMeshParser, :DocTestSetup, :(using FerriteMeshParser); recursive=true)
DocMeta.setdocmeta!(FerriteMeshParser, :DocTestSetup, :(using FerriteMeshParser); recursive = true)

makedocs(;
authors="Knut Andreas Meyer and contributors",
repo="https://github.com/Ferrite-FEM/FerriteMeshParser.jl/blob/{commit}{path}#{line}",
sitename="FerriteMeshParser.jl",
format=Documenter.HTML(;
prettyurls=is_ci,
canonical="https://Ferrite-FEM.github.io/FerriteMeshParser.jl",
assets=String[],
authors = "Knut Andreas Meyer and contributors",
repo = "https://github.com/Ferrite-FEM/FerriteMeshParser.jl/blob/{commit}{path}#{line}",
sitename = "FerriteMeshParser.jl",
format = Documenter.HTML(;
prettyurls = is_ci,
canonical = "https://Ferrite-FEM.github.io/FerriteMeshParser.jl",
assets = String[],
),
pages=[
pages = [
"Home" => "index.md",
"Examples" => GENERATEDEXAMPLES,
],
)

deploydocs(;
repo="github.com/Ferrite-FEM/FerriteMeshParser.jl",
devbranch="main",
push_preview=true,
repo = "github.com/Ferrite-FEM/FerriteMeshParser.jl",
devbranch = "main",
push_preview = true,
)
3 changes: 1 addition & 2 deletions docs/src/literate/compact_tension.jl
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ println([(key, length(set)) for (key, set) in Ferrite.getfacesets(grid)])
# Clearly, the facetset `"CrackZone"` doesn't make much sense, but unless the mesh is
# very large it doesn't hurt. The facetsets can be created manually from each nodeset
# by using the `create_facetset` function:
facetset = create_facetset(grid, getnodeset(grid,"Hole"));
facetset = create_facetset(grid, getnodeset(grid, "Hole"));
# This can, if desired, be merged into the grid by
# ```julia
# addfaceset!(grid, "HoleManual", facetset)
Expand All @@ -52,4 +52,3 @@ facetset = create_facetset(grid, getnodeset(grid,"Hole"));
#md # ```julia
#md # @__CODE__
#md # ```

7 changes: 3 additions & 4 deletions docs/src/literate/user_element.jl
Original file line number Diff line number Diff line change
Expand Up @@ -7,22 +7,22 @@
#
# For this element, it can be defined as a specific `Ferrite.Cell` type
using Ferrite, FerriteMeshParser
LinearWedge = Ferrite.Cell{3,6,5}
LinearWedge = Ferrite.Cell{3, 6, 5}

# For this element to work with Ferrite, one must define a new reference shape
# e.g. Wedge and the appropriate interpolations for this shape. In doing so,
# one also chooses the node order. Following the standard Ferrite conventions,
# the node order should be the same as in Abaqus as shown above. To change this,
# it is possible to overload the function `FerriteMeshParser.create_cell` as follows

create_cell(::Type{LinearWedge}, node_numbers, ::FerriteMeshParser.AbaqusMeshFormat) = LinearWedge(ntuple(j->node_numbers[j], length(node_numbers)))
create_cell(::Type{LinearWedge}, node_numbers, ::FerriteMeshParser.AbaqusMeshFormat) = LinearWedge(ntuple(j -> node_numbers[j], length(node_numbers)))

# This setup allows changing the node order for your specific element.
# It is also possible to use another type which is not a variant of `Ferrite.Cell`, but
# rather a subtype of `Ferrite.AbstractCell`. After these modifications, one can import
# the mesh by specifying that the Abaqus code `C3D6` should be interpreted as a `LinearWedge`:

grid = get_ferrite_grid("wedge_element.inp"; user_elements=Dict("C3D6"=>LinearWedge));
grid = get_ferrite_grid("wedge_element.inp"; user_elements = Dict("C3D6" => LinearWedge));

# Giving the following grid
println(typeof(grid))
Expand All @@ -37,4 +37,3 @@ println(unique(typeof.(getcells(grid)))) # The different cell types in the gr
#md # ```julia
#md # @__CODE__
#md # ```

18 changes: 9 additions & 9 deletions src/FerriteMeshParser.jl
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
module FerriteMeshParser
using Ferrite:
using Ferrite:
Ferrite, Grid, Node, Vec,
getcells, getnodes, getcoordinates, getncells

Expand Down Expand Up @@ -27,22 +27,22 @@ end
Base.showerror(io::IO, e::InvalidFileContent) = println(io, e.msg)

@static if !isdefined(Ferrite, :SerendipityQuadraticHexahedron)
const SerendipityQuadraticHexahedron = Ferrite.Cell{3,20,6}
const SerendipityQuadraticQuadrilateral = Ferrite.Cell{2,8,4}
const SerendipityQuadraticHexahedron = Ferrite.Cell{3, 20, 6}
const SerendipityQuadraticQuadrilateral = Ferrite.Cell{2, 8, 4}
else
const SerendipityQuadraticHexahedron = Ferrite.SerendipityQuadraticHexahedron
const SerendipityQuadraticQuadrilateral = Ferrite.SerendipityQuadraticQuadrilateral
end

const FacetsDefined = isdefined(Ferrite, :FacetIndex) # Ferrite after v1.0 (Ferrite#914)

const FacetIndex = FacetsDefined ? Ferrite.FacetIndex : Ferrite.FaceIndex
const facets = FacetsDefined ? Ferrite.facets : Ferrite.faces
const FacetIndex = FacetsDefined ? Ferrite.FacetIndex : Ferrite.FaceIndex
const facets = FacetsDefined ? Ferrite.facets : Ferrite.faces
const addfacetset! = FacetsDefined ? Ferrite.addfacetset! : Ferrite.addfaceset!

include("rawmesh.jl")
include("elements.jl")
include("reading_utils.jl")
include("reading_utils.jl")
include("abaqusreader.jl")
include("gridcreator.jl")

Expand All @@ -64,7 +64,7 @@ Optional arguments:
* `generate_facetsets`: Should facesets be automatically generated from all nodesets?
"""
function get_ferrite_grid(filename; meshformat=AutomaticMeshFormat(), user_elements::Dict{String, DataType}=Dict{String, DataType}(), generate_facetsets::Bool=true, generate_facesets=nothing)
function get_ferrite_grid(filename; meshformat = AutomaticMeshFormat(), user_elements::Dict{String, DataType} = Dict{String, DataType}(), generate_facetsets::Bool = true, generate_facesets = nothing)
generate_facesets !== nothing && error("The keyword generate_facesets is deprecated, use generate_facetsets instead")
detected_format = detect_mesh_format(filename, meshformat)
mesh = read_mesh(filename, detected_format)
Expand All @@ -88,11 +88,11 @@ Otherwise the search is over all cells.
This function is normally only required when calling `get_ferrite_grid` with `generate_facetsets=false`.
The created `facetset` can be added to the grid as `addfacetset!(grid, "facetsetkey", facetset)`
"""
function create_facetset(grid::Ferrite.AbstractGrid, nodeset::AbstractSet{Int}, cellset=1:getncells(grid))
function create_facetset(grid::Ferrite.AbstractGrid, nodeset::AbstractSet{Int}, cellset = 1:getncells(grid))
facetset = sizehint!(Set{FacetIndex}(), length(nodeset))
for (cellid, cell) in enumerate(getcells(grid))
cellid cellset || continue
if any(n-> n nodeset, cell.nodes)
if any(n -> n nodeset, cell.nodes)
for (facetid, facet) in enumerate(facets(cell))
if all(n -> n nodeset, facet)
push!(facetset, FacetIndex(cellid, facetid))
Expand Down
30 changes: 15 additions & 15 deletions src/abaqusreader.jl
Original file line number Diff line number Diff line change
Expand Up @@ -7,20 +7,20 @@ function join_multiline_elementdata(element_data::Vector{<:AbstractString})
nrows = length(element_data)
while i < nrows
i += 1
length(element_data[i])==0 && continue
length(element_data[i]) == 0 && continue
i_fixed += 1
fixed_element_data[i_fixed] = element_data[i]
while endswith(fixed_element_data[i_fixed], ',') && i < nrows
i += 1
fixed_element_data[i_fixed] = fixed_element_data[i_fixed]*element_data[i]
fixed_element_data[i_fixed] = fixed_element_data[i_fixed] * element_data[i]
end
end
return fixed_element_data[1:i_fixed]
end

function read_abaqus_nodes!(f, node_numbers::Vector{Int}, coord_vec::Vector{Float64})
local coords
node_data = readlinesuntil(f; stopsign='*')
node_data = readlinesuntil(f; stopsign = '*')
for nodeline in node_data
node = split(nodeline, ',', keepempty = false)
length(node) == 0 && continue
Expand All @@ -31,15 +31,15 @@ function read_abaqus_nodes!(f, node_numbers::Vector{Int}, coord_vec::Vector{Floa
return length(coords)
end

function read_abaqus_elements!(f, topology_vectors, element_number_vectors, element_type::AbstractString, element_set="", element_sets=nothing)
function read_abaqus_elements!(f, topology_vectors, element_number_vectors, element_type::AbstractString, element_set = "", element_sets = nothing)
if !haskey(topology_vectors, element_type)
topology_vectors[element_type] = Int[]
element_number_vectors[element_type] = Int[]
end
topology_vec = topology_vectors[element_type]
element_numbers = element_number_vectors[element_type]
element_numbers_new = Int[]
element_data_raw = readlinesuntil(f; stopsign='*')
element_data_raw = readlinesuntil(f; stopsign = '*')
element_data = join_multiline_elementdata(element_data_raw)
for elementline in element_data
element = split(elementline, ',', keepempty = false)
Expand All @@ -62,7 +62,7 @@ function read_abaqus_set!(f, sets, setname::AbstractString)
indices = collect(start:step:stop)
setname = split(setname, [','])[1]
else
data = readlinesuntil(f; stopsign='*')
data = readlinesuntil(f; stopsign = '*')
indices = Int[]
for line in data
indices_str = split(line, ',', keepempty = false)
Expand Down Expand Up @@ -102,10 +102,10 @@ function read_mesh(filename, ::AbaqusMeshFormat)
elseif startswith(lowercase(header), "*element")
if ((m = match(r"\*Element, type=(.*), ELSET=(.*)"i, header)) !== nothing)
DEBUG_PARSE && println("Reading elements with elset")
read_abaqus_elements!(f, topology_vectors, element_number_vectors, m.captures[1], m.captures[2], elementsets)
read_abaqus_elements!(f, topology_vectors, element_number_vectors, m.captures[1], m.captures[2], elementsets)
elseif ((m = match(r"\*Element, type=(.*)"i, header)) !== nothing)
DEBUG_PARSE && println("Reading elements without elset")
read_abaqus_elements!(f, topology_vectors, element_number_vectors, m.captures[1])
read_abaqus_elements!(f, topology_vectors, element_number_vectors, m.captures[1])
end
elseif ((m = match(r"\*Elset, elset=(.*)"i, header)) !== nothing)
DEBUG_PARSE && println("Reading elementset")
Expand All @@ -119,10 +119,10 @@ function read_mesh(filename, ::AbaqusMeshFormat)
elseif startswith(lowercase(header), "*instance")
DEBUG_PARSE && println("Increment instance counter")
instance_counter += 1
discardlinesuntil(f, stopsign='*') # Instances contain translations, or start with *Node if independent mesh
discardlinesuntil(f, stopsign = '*') # Instances contain translations, or start with *Node if independent mesh
elseif isabaquskeyword(header) # Ignore unused keywords
DEBUG_PARSE && println("Discarding keyword content")
discardlinesuntil(f, stopsign='*')
discardlinesuntil(f, stopsign = '*')
else
eof(f) && break # discardlinesuntil will stop at eof, and last line read again and incorrectly considered a "header"
throw(InvalidFileContent("Unknown header, \"$header\", in file \"$filename\". Could also indicate an incomplete file"))
Expand All @@ -136,15 +136,15 @@ function read_mesh(filename, ::AbaqusMeshFormat)
throw(InvalidFileContent(msg))
end
end

elements = Dict{String, RawElements}()
for element_type in keys(topology_vectors)
topology_vec = topology_vectors[element_type]
element_numbers = element_number_vectors[element_type]
n_elements = length(element_numbers)
topology_matrix = reshape(topology_vec, length(topology_vec) ÷ n_elements, n_elements)
elements[element_type] = RawElements(numbers=element_numbers, topology=topology_matrix)
elements[element_type] = RawElements(numbers = element_numbers, topology = topology_matrix)
end
nodes = RawNodes(numbers=node_numbers, coordinates=reshape(coord_vec, dim, length(coord_vec) ÷ dim))
return RawMesh(elements=elements, nodes=nodes, nodesets=nodesets, elementsets=elementsets)
end
nodes = RawNodes(numbers = node_numbers, coordinates = reshape(coord_vec, dim, length(coord_vec) ÷ dim))
return RawMesh(elements = elements, nodes = nodes, nodesets = nodesets, elementsets = elementsets)
end
8 changes: 4 additions & 4 deletions src/elements.jl
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
function get_element_type_dict(::AbaqusMeshFormat)

quad = (Ferrite.Quadrilateral, ("CPE4", "CPS4", "CPE4R", "CPS4R"))
quad2 = (SerendipityQuadraticQuadrilateral, ("CPS8", "CPS8R", "CPE8", "CPE8R"))
tria = (Ferrite.Triangle, ("CPE3", "CPS3"))
tria2 = (Ferrite.QuadraticTriangle, ("CPE6", "CPS6", "CPE6M", "CPS6M"))
tetra = (Ferrite.Tetrahedron, ("C3D4",))
tetra2 = (Ferrite.QuadraticTetrahedron, ("C3D10",))
hexa = (Ferrite.Hexahedron, ("C3D8","C3D8R"))
hexa2 = (SerendipityQuadraticHexahedron, ("C3D20","C3D20R"))
hexa = (Ferrite.Hexahedron, ("C3D8", "C3D8R"))
hexa2 = (SerendipityQuadraticHexahedron, ("C3D20", "C3D20R"))

dict = Dict{String, DataType}()
for types in (quad, tria, quad2, tria2, tetra, tetra2, hexa, hexa2)
Expand All @@ -17,4 +17,4 @@ function get_element_type_dict(::AbaqusMeshFormat)
end

# Default creator for Ferrite cell types from Abaqus elements
create_cell(::Type{CellType}, node_numbers, ::AbaqusMeshFormat) where{CellType<:Ferrite.AbstractCell} = CellType(ntuple(j->node_numbers[j], length(node_numbers)))
create_cell(::Type{CellType}, node_numbers, ::AbaqusMeshFormat) where {CellType <: Ferrite.AbstractCell} = CellType(ntuple(j -> node_numbers[j], length(node_numbers)))
8 changes: 4 additions & 4 deletions src/gridcreator.jl
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,14 @@ function create_grid(mesh::RawMesh, format, user_elements)
nodes = create_nodes(getnodes(mesh), Val(dim))
cellsets = create_cellsets(getelementsdicts(mesh), getelementsets(mesh))
nodesets = create_nodesets(Ferrite.getnodesets(mesh))
return Grid(cells, nodes; cellsets=cellsets, nodesets=nodesets)
return Grid(cells, nodes; cellsets = cellsets, nodesets = nodesets)
end

function create_nodes(rawnodes::RawNodes, ::Val{dim}) where{dim}
function create_nodes(rawnodes::RawNodes, ::Val{dim}) where {dim}
num = getnumnodes(rawnodes)
nodes=Array{Node{dim, Float64}}(undef, num)
nodes = Array{Node{dim, Float64}}(undef, num)
for (index, node_number) in enumerate(getnumbers(rawnodes))
x=Vec{dim}(getcoordinate(rawnodes, index))
x = Vec{dim}(getcoordinate(rawnodes, index))
nodes[node_number] = Node(x)
end
return nodes
Expand Down
12 changes: 6 additions & 6 deletions src/rawmesh.jl
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ struct RawElements
numbers::Vector{Int}
topology::Matrix{Int}
end
RawElements(;numbers, topology) = RawElements(numbers, topology)
RawElements(; numbers, topology) = RawElements(numbers, topology)

getnumbers(elements::RawElements) = elements.numbers
gettopology(elements::RawElements) = elements.topology
Expand All @@ -17,14 +17,14 @@ struct RawNodes
numbers::Vector{Int}
coordinates::Matrix{Float64}
end
RawNodes(;numbers, coordinates) = RawNodes(numbers, coordinates)
RawNodes(; numbers, coordinates) = RawNodes(numbers, coordinates)

getnumbers(nodes::RawNodes) = nodes.numbers
Ferrite.getcoordinates(nodes::RawNodes) = nodes.coordinates

getnumnodes(nodes::RawNodes) = length(getnumbers(nodes))
getdim(nodes::RawNodes) = size(getcoordinates(nodes),1)
getcoordinate(nodes::RawNodes, number) = getcoordinates(nodes)[:,number]
getdim(nodes::RawNodes) = size(getcoordinates(nodes), 1)
getcoordinate(nodes::RawNodes, number) = getcoordinates(nodes)[:, number]

# Complete mesh
struct RawMesh
Expand All @@ -33,7 +33,7 @@ struct RawMesh
elementsets::Dict{String, Vector{Int}}
nodesets::Dict{String, Vector{Int}}
end
RawMesh(;elements,nodes,elementsets,nodesets) = RawMesh(elements,nodes,elementsets,nodesets)
RawMesh(; elements, nodes, elementsets, nodesets) = RawMesh(elements, nodes, elementsets, nodesets)

Ferrite.getnodes(mesh::RawMesh) = mesh.nodes
getelementsdicts(mesh::RawMesh) = mesh.elementsdicts
Expand All @@ -60,4 +60,4 @@ function checkmesh(mesh::RawMesh)
end
minelnum == 1 || throw(InvalidFileContent("Element numbering must start with 1"))
maxelnum == getnumelements(mesh) || throw(InvalidFileContent("No element numbers may be skipped"))
end
end
6 changes: 3 additions & 3 deletions src/reading_utils.jl
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,9 @@ end
# stopsign encountered. Buffer at beginning of stopsign
function readlinesuntil(f; stopsign)
# Split lines for both Windows and Linux line endings
data = strip.(split(readuntil(f, stopsign; keep=false), r"\r\n|\n"))
data = strip.(split(readuntil(f, stopsign; keep = false), r"\r\n|\n"))
# Set buffer to the beginning of the stopsign
seek(f, position(f) - length(stopsign))
seek(f, position(f) - length(stopsign))
return data
end

Expand All @@ -34,4 +34,4 @@ function discardlinesuntil(io; stopsign)
l = readline(io)
end
reset(io)
end
end
Loading

0 comments on commit 0c723c7

Please sign in to comment.