Files
JuliaFEM.jl/src/abaqus_reader.jl
T

128 lines
3.4 KiB
Julia
Raw Normal View History

2015-06-25 21:17:29 +03:00
# This file is a part of JuliaFEM.
# License is MIT: see https://github.com/JuliaFEM/JuliaFEM.jl/blob/master/LICENSE.md
2015-08-22 20:55:24 +03:00
global handlers = Dict()
2015-06-25 21:04:39 +03:00
2015-06-26 18:53:29 +03:00
"""
2015-06-25 20:27:47 +03:00
Register new handler for parser
2015-06-26 18:53:29 +03:00
"""
2015-06-25 20:27:47 +03:00
function add_handler(section, function_name)
handlers[section] = function_name
end
function create_or_get(model, key)
if !(key in keys(model))
model[key] = Dict()
end
return model[key]
end
function parse_header(header_line)
args = map(s -> strip(s), split(header_line, ","))
args[1] = strip(args[1], '*')
2015-08-19 22:51:44 +03:00
d = Dict("section" => args[1], "options" => Dict())
options = d["options"]
2015-06-25 20:27:47 +03:00
for k in args[2:end]
args2 = split(k, "=")
options[args2[1]] = args2[2]
end
return d
end
function parse_node_section(model, header, data)
nodes = create_or_get(model, "nodes")
for line in split(data, "\n")
m = matchall(r"[-0-9.]+", line)
id = parse(Int, m[1])
coords = float(m[2:end])
nodes[id] = coords
end
end
function parse_element_section(model, header, data)
2015-11-25 10:08:24 +02:00
info("Parsing elements")
2015-08-31 21:21:39 +03:00
eldims = Dict(
"C3D10" => 10,
"C3D4" => 4)
2015-06-25 20:27:47 +03:00
eltype = header["options"]["TYPE"]
if !(eltype in keys(eldims))
throw("Element $eltype dimension information missing")
end
eldim = eldims[eltype]
2015-08-31 21:21:39 +03:00
test_match = matchall(r"[0-9]+", "234, 242")
m = matchall(r"[0-9]+", data)
2015-08-22 20:55:24 +03:00
m = map((s) -> parse(Int, s), m)
2015-06-25 20:27:47 +03:00
elements = create_or_get(model, "elements")
m = reshape(m, eldim+1, round(Int, length(m)/(eldim+1)))
nel = size(m)[2]
2015-11-25 10:08:24 +02:00
info("$nel elements found")
2015-06-25 20:27:47 +03:00
for i=1:nel
elements[m[1,i]] = m[2:end,i]
end
if "ELSET" in keys(header["options"])
elsets = create_or_get(model, "elsets")
elset_name = header["options"]["ELSET"]
2015-11-25 10:08:24 +02:00
info("Creating ELSET $elset_name")
2015-06-25 20:27:47 +03:00
elsets[elset_name] = Int64[]
for i=1:nel
push!(elsets[elset_name], m[1,i])
end
end
end
function parse_nodeset_section(model, header, data)
nset_name = header["options"]["NSET"]
2015-11-25 10:08:24 +02:00
info("Creating node set $nset_name")
2015-06-25 20:27:47 +03:00
m = matchall(r"[0-9]+", data)
2015-08-22 20:55:24 +03:00
node_ids = map((s) -> parse(Int, s), m)
2015-06-25 20:27:47 +03:00
nsets = create_or_get(model, "nsets")
nsets[nset_name] = Int64[]
for j in node_ids
push!(nsets[nset_name], j)
end
end
2015-11-25 10:08:24 +02:00
function parse_abaqus(fid::IOStream)
2015-06-25 20:27:47 +03:00
model = Dict()
2015-11-25 10:08:24 +02:00
section = nothing
header = nothing
data = ASCIIString[]
info("Registered handlers: $(keys(handlers))")
2015-06-25 20:27:47 +03:00
function process_section(section)
2015-11-25 10:08:24 +02:00
if section == nothing
2015-06-25 20:27:47 +03:00
return
end
if !(section in keys(handlers))
2015-11-25 10:08:24 +02:00
info("Don't know what to do with data in section $section")
info("Skipping $(length(data)) bytes of unknown data")
2015-06-25 20:27:47 +03:00
return
end
2015-08-31 21:21:39 +03:00
joined = join(data, "")
handlers[section](model, header, strip(joined))
2015-11-25 10:08:24 +02:00
empty!(data)
2015-06-25 20:27:47 +03:00
end
2015-08-31 21:21:39 +03:00
line_idx = 0
2015-06-25 20:27:47 +03:00
for line in eachline(fid)
2015-08-19 22:51:44 +03:00
if startswith(line, "**")
2015-06-25 20:27:47 +03:00
continue
end
2015-08-19 22:51:44 +03:00
if startswith(line, "*")
2015-06-25 20:27:47 +03:00
process_section(section)
header = parse_header(line)
section = header["section"]
continue
end
2015-08-31 21:21:39 +03:00
push!(data, line)
2015-11-25 10:08:24 +02:00
end
2015-06-25 20:27:47 +03:00
process_section(section)
return model
end
# add handlers
add_handler("NODE", parse_node_section)
add_handler("ELEMENT", parse_element_section)
add_handler("NSET", parse_nodeset_section)