Skip to content

Commit

Permalink
Fix test coverage back to 100 %
Browse files Browse the repository at this point in the history
  • Loading branch information
ahojukka5 committed Nov 30, 2018
1 parent cfbf94e commit 2186ca5
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 22 deletions.
48 changes: 27 additions & 21 deletions src/read_aster_mesh.jl
Expand Up @@ -6,9 +6,6 @@ function aster_parse_nodes(section; strip_characters=true)
has_started = false
for line in split(section, '\n')
m = collect((string(m.match) for m in eachmatch(r"[\w.-]+", line)))
if (length(m) != 1) && (!has_started)
continue
end
if length(m) == 1
if (m[1] == "COOR_2D") || (m[1] == "COOR_3D")
has_started = true
Expand All @@ -18,6 +15,9 @@ function aster_parse_nodes(section; strip_characters=true)
break
end
end
if !has_started # we have not found COOR_2D or COOR_3D yet.
continue
end
to(T, x) = map(x -> parse(T, x), x)
if length(m) == 4
if strip_characters
Expand Down Expand Up @@ -51,10 +51,10 @@ end

function get_mesh(med::MEDFile, mesh_name::String)
if !haskey(med.data["FAS"], mesh_name)
warn("Mesh $mesh_name not found from med file.")
@warn("Mesh $mesh_name not found from med file.")
meshes = get_mesh_names(med)
all_meshes = join(meshes, ", ")
warn("Available meshes: $all_meshes")
@warn("Available meshes: $all_meshes")
error("Mesh $mesh_name not found.")
end
return med.data["FAS"][mesh_name]
Expand All @@ -80,10 +80,15 @@ function get_node_sets(med::MEDFile, mesh_name::String)::Dict{Int64, Vector{Stri
return node_sets
end

""" Return element sets from med file.
"""
get_element_sets(med, mesh_name)
Return element sets from med file. Return type is a dictionary, where the key is
the element set id number (integer) and value is a vector of strings, containing
human-readable name for element set.
# Notes
Notes
-----
One element set id can have multiple names.
"""
Expand Down Expand Up @@ -146,23 +151,24 @@ function get_connectivity(med::MEDFile, elsets::Dict{Int64, Vector{String}}, mes
return d
end

""" Parse code aster .med file.
"""
aster_read_mesh(filename, mesh_name=nothing)
Parse code aster .med file and return mesh data in a dictionary.
Paramters
---------
fn
file name to parse
mesh_name :: optional
mesh name, if several meshes in one file
Dictionary contains additional dictionaries `nodes`, `node_sets`, `elements`,
`element_sets`, `element_types`, `surface_sets` and `surface_types`.
Returns
-------
Dict containing fields "nodes" and "connectivity".
If mesh file contains several meshes, one must provide the mesh name as
additional argument or expcetion will be thrown.
"""
function aster_read_mesh(fn, mesh_name=nothing)
med = MEDFile(fn)
mesh_names = get_mesh_names(med::MEDFile)
function aster_read_mesh(filename::String, mesh_name=nothing)
aster_read_mesh_(MEDFile(filename), mesh_name)
end

function aster_read_mesh_(med::MEDFile, mesh_name=nothing)
mesh_names = get_mesh_names(med)
all_meshes = join(mesh_names, ", ")
if mesh_name == nothing
length(mesh_names) == 1 || error("several meshes found from med, pick one: $all_meshes")
Expand Down
17 changes: 17 additions & 0 deletions test/test_read_aster_mesh.jl
Expand Up @@ -29,6 +29,23 @@ datadir = first(splitext(basename(@__FILE__)))
@test length(nodes) == 8
end

@testset "read mesh med file (mesh not found)" begin
med = AsterReader.MEDFile(joinpath(datadir, "quad4.med"))
@test_throws ErrorException AsterReader.get_mesh(med, "notfound")
end

@testset "read element sets (element sets not found)" begin
med = AsterReader.MEDFile(joinpath(datadir, "quad4.med"))
delete!(med.data["FAS"]["BLOCK"], "ELEME")
@test isempty(AsterReader.get_element_sets(med, "BLOCK"))
end

@testset "read med file with several mesh files" begin
med = AsterReader.MEDFile(joinpath(datadir, "quad4.med"))
med.data["FAS"]["new_mesh"] = med.data["FAS"]["BLOCK"]
@test_throws ErrorException AsterReader.aster_read_mesh_(med)
end

@testset "read mesh med file" begin
meshfile = joinpath(datadir, "quad4.med")
mesh = aster_read_mesh(meshfile)
Expand Down
8 changes: 7 additions & 1 deletion test/test_read_aster_results.jl
Expand Up @@ -9,7 +9,13 @@ datadir = first(splitext(basename(@__FILE__)))
@testset "read nodal field from result rmed file" begin
rmedfile = joinpath(datadir, "rings.rmed")
rmed = RMEDFile(rmedfile)
temp = aster_read_data(rmed, "TEMP")
temp = aster_read_data(rmed, "TEMP"; info_fields=true)
@test isapprox(temp[15], 1.0)
@test isapprox(temp[95], 2.0)
end

@testset "try to read gauss point field from result rmed file" begin
rmedfile = joinpath(datadir, "rings.rmed")
rmed = RMEDFile(rmedfile)
@test_throws Exception aster_read_data(rmed, "FLUX_ELGA")
end

0 comments on commit 2186ca5

Please sign in to comment.