Created
March 4, 2020 13:58
-
-
Save KristofferC/5540129158b27d0fef164d6b718ddbbf to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# function meshformatparser(str) | |
# | |
# end | |
# const SECTION_PARSER = Dict{String,Function}( | |
# "MeshFormat" => meshformatparser, | |
# ) | |
module Gmsh | |
using Tensors, JuAFEM | |
function parsenodes(str; version=v"4.1") | |
m = match(r"\$Nodes(.*)\$EndNodes"s, str) | |
lines = split(strip(m.captures[1]), '\n') | |
line = split(popfirst!(lines)) | |
numEntityBlocks, numNodes = parse.(Int, line) | |
nodes = Vector{Node{3,Float64}}(undef, numNodes) | |
visited = falses(numNodes) | |
for nnodeentity in 1:numEntityBlocks | |
line = split(popfirst!(lines)) | |
tagEntity, dimEntity, parametric, numNodes = parse.(Int, line) | |
if version == v"4.1" | |
tag_chunk = Int[] | |
node_chunk = Node{3,Float64}[] | |
for node in 1:numNodes | |
line = split(popfirst!(lines)) | |
tag = parse(Int, line[1]) | |
push!(tag_chunk, tag) | |
end | |
for node in 1:numNodes | |
line = split(popfirst!(lines)) | |
x = parse(Float64, line[1]) | |
y = parse(Float64, line[2]) | |
z = parse(Float64, line[3]) | |
push!(node_chunk, Node{3,Float64}(Vec{3,Float64}((x, y, z)))) | |
end | |
nodes[tag_chunk] .= node_chunk | |
visited[tag_chunk] .= true | |
elseif version == v"4.0" | |
for node in 1:numNodes | |
line = split(popfirst!(lines)) | |
tag = parse(Int, line[1]) | |
x = parse(Float64, line[2]) | |
y = parse(Float64, line[3]) | |
z = parse(Float64, line[4]) | |
nodes[tag] = Node{3,Float64}(Vec{3,Float64}((x, y, z))) | |
visited[tag] = true | |
end | |
end | |
end | |
@assert all(visited) | |
return nodes | |
end | |
function parseelements(str) | |
m = match(r"\$Elements(.*)\$EndElements"s, str) | |
lines = split(strip(m.captures[1]), '\n') | |
line = split(popfirst!(lines)) | |
numEntityBlocks, numElements = parse.(Int, line) | |
elements = Tetrahedron[] | |
for nnodeentity in 1:numEntityBlocks | |
line = split(popfirst!(lines)) | |
tagEntity, dimEntity, typeEle, numElements = parse.(Int, line) | |
for element in 1:numElements | |
line = split(popfirst!(lines)) | |
typeEle === 4 || continue # TODO | |
tag = parse(Int, line[1]) | |
n1 = parse(Int, line[2]) | |
n2 = parse(Int, line[3]) | |
n3 = parse(Int, line[4]) | |
n4 = parse(Int, line[5]) | |
push!(elements, Tetrahedron((n1, n2, n3, n4))) | |
end | |
end | |
return elements | |
end | |
function gmshparser(file) | |
str = read(file, String) | |
# Version | |
m = match(r"\$MeshFormat(.*)\$EndMeshFormat"s, str) | |
version = VersionNumber(first(split(strip(m.captures[1])))) | |
# Nodes | |
nodes = parsenodes(str, version=version) | |
# Elements | |
elements = parseelements(str) | |
return Grid(elements, nodes) | |
end | |
end | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment