From b9f308aee334e9774b11d8d2f02b1364b681a09b Mon Sep 17 00:00:00 2001 From: Jukka Aho Date: Mon, 22 Jun 2015 23:19:10 +0300 Subject: [PATCH] testing test --- src/JuliaFEM.jl | 74 ------------------------- test/runtests.jl | 1 + test/test_model.jl | 135 +++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 136 insertions(+), 74 deletions(-) create mode 100644 test/test_model.jl diff --git a/src/JuliaFEM.jl b/src/JuliaFEM.jl index c2e6d42..dbc00d8 100644 --- a/src/JuliaFEM.jl +++ b/src/JuliaFEM.jl @@ -38,30 +38,6 @@ end -@doc """ -Create new field to model. - -Parameters ----------- -field_type : Dict() - Target topology (model.model, model.nodes, model.elements, - model.element_nodes, model.element_gauss -field_name : str - Field name - -Returns -------- -Dict - New field - -""" -> -function new_field(field_type, field_name) - d = Dict() - setindex!(field_type, d, field_name) - return d -end - - @doc """Get field from model. @@ -96,56 +72,6 @@ end - -@doc """Add new nodes to model. - -Parameters ----------- -nodes : Dict() - id => coords - -Returns -------- -model - -Notes ------ -Create new field "coords" to model if not found -""" -> -function add_nodes(model, nodes) - #pri("Adding ", length(nodes), " nodes to model") - field = get_field(model.nodes, "coords"; create_if_doesnt_exist=true) - for (node_id, coords) in nodes - field[node_id] = coords - end -end - - - - -@doc """Return subset of nodes from model. - -Parameters ----------- -node_ids : array - list of node ids to return - -Returns -------- -Dict() - id => coords -""" -> -function get_nodes(model, node_ids) - subset = Dict() - for node_id in node_ids - subset[node_id] = model.nodes["coords"][node_id] - end - return subset -end - - - - @doc """Add new elements to model. Parameters ---------- diff --git a/test/runtests.jl b/test/runtests.jl index b939a30..2adb570 100644 --- a/test/runtests.jl +++ b/test/runtests.jl @@ -135,6 +135,7 @@ test_get_element() include("test_xdmf.jl") include("solver_tests/test_elasticity_solver.jl") +include("test_model.jl") # write your own tests here # @test 1 == JuliaFEM.test() diff --git a/test/test_model.jl b/test/test_model.jl new file mode 100644 index 0000000..99422a9 --- /dev/null +++ b/test/test_model.jl @@ -0,0 +1,135 @@ +using FactCheck +#using JuliaFEM +using Logging +using HDF5 +@Logging.configure(level=DEBUG) + +@doc """ +Create new field to model. + +Parameters +---------- +field_type : Dict() + Target topology (model.model, model.nodes, model.elements, + model.element_nodes, model.element_gauss +field_name : str + Field name + +Returns +------- +Dict + New field + +""" -> +function new_field!(model, field_type, field_name; partition=1, time=0, increment=0) + h5write("$model.$partition.h5", "$time/$increment/$field_type/$field_name", Float64[]) +end + + +@doc """Add new nodes to model. + +Parameters +---------- +model : str + Path to model file +nodes : Dict() + id => coords +partition : int, optional + Partition number, defaults to 1 +time : float, optional + Time step, defaults to 0 +increment : float, optional + Increment number, defaults to 0 + +Returns +------- +None + +Notes +----- +Create new field "coords" to model if not found +""" -> +function add_nodes!(model, nodes; partition=1, time=0, increment=0) + coords = Float64[] + node_ids = Int64[] + for (nid, ncoords) in nodes + @debug("adding nid: ", nid, " with coords: ", ncoords) + @assert length(ncoords) == 3 + for x in ncoords + push!(coords, x) + end + push!(node_ids, nid) + end + h5write("$model.$partition.h5", "$time/$increment/nodes/coords", coords) + h5write("$model.$partition.h5", "$time/$increment/nodes/node_ids", node_ids) +end + + + +@doc """Return subset of nodes from model. + +Parameters +---------- +node_ids : array, optional + List of node ids to return. If not given, return all nodes. +partition : int, optional + Partition number, defaults to 1 +time : float, optional + Time step, defaults to 0 +increment : float, optional + Increment number, defaults to 0 + +Returns +------- +Dict() + id => coords +""" -> +function get_nodes(model, node_ids=[]; partition=1, time=0, increment=0) + subset = Dict() + all_node_coords = h5read("$model.1.h5", "$time/$increment/nodes/coords") + all_node_ids = h5read("$model.1.h5", "$time/$increment/nodes/node_ids") + @debug("all node coords: ", all_node_coords) + @debug("all node ids: ", all_node_ids) + if length(node_ids) == 0 + node_ids = all_node_ids + end + dim = 3 + for node_id in node_ids + @debug("fetching node ", node_id) + idx = findfirst(all_node_ids, node_id) + @debug("found node coords from idx ", idx) + subset[node_id] = all_node_coords[dim*(idx-1)+1:dim*(idx-1)+dim] + end + return subset +end + + + +facts("create new field to model") do + model = tempname() + @debug("model file name", model) + new_field!(model, "nodes", "coords") + data = h5read("$model.1.h5", "0/0/nodes/coords") + @fact length(data) => 0 +end + +facts("add nodes to model") do + model = tempname() + nodes = Dict(1 => [1.0, 2.0, 3.0]) + add_nodes!(model, nodes) + @fact h5read("$model.1.h5", "0/0/nodes/coords") => [1.0, 2.0, 3.0] + @fact h5read("$model.1.h5", "0/0/nodes/node_ids") => [1] +end + +facts("get nodes from model") do + model = tempname() + nodes = Dict(1 => [1.0, 2.0, 3.0], 2 => [2.0, 3.0, 4.0]) + add_nodes!(model, nodes) + subset = get_nodes(model, [1]) + @fact subset[1] => [1.0, 2.0, 3.0] + subset = get_nodes(model, [2]) + @fact subset[2] => [2.0, 3.0, 4.0] + subset = get_nodes(model, [1,2]) + @fact subset[1] => [1.0, 2.0, 3.0] + @fact subset[2] => [2.0, 3.0, 4.0] +end \ No newline at end of file