From 143f1599baa58adb636fed63cc3a36fa010292a4 Mon Sep 17 00:00:00 2001 From: Jukka Aho Date: Wed, 12 Dec 2018 11:11:28 +0200 Subject: [PATCH] Increase test coverage (#15) --- src/read_aster_mesh.jl | 48 ++++++++++++++++++--------------- test/test_read_aster_mesh.jl | 17 ++++++++++++ test/test_read_aster_results.jl | 8 +++++- 3 files changed, 51 insertions(+), 22 deletions(-) diff --git a/src/read_aster_mesh.jl b/src/read_aster_mesh.jl index 1b5b9ce..189586c 100644 --- a/src/read_aster_mesh.jl +++ b/src/read_aster_mesh.jl @@ -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 @@ -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 @@ -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] @@ -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. """ @@ -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") diff --git a/test/test_read_aster_mesh.jl b/test/test_read_aster_mesh.jl index ac7c2a0..cb542ea 100644 --- a/test/test_read_aster_mesh.jl +++ b/test/test_read_aster_mesh.jl @@ -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) diff --git a/test/test_read_aster_results.jl b/test/test_read_aster_results.jl index 39986f7..1958346 100644 --- a/test/test_read_aster_results.jl +++ b/test/test_read_aster_results.jl @@ -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