From 0f64cabfffcbfeafd6a2d2497bd2b6efc1b1c94f Mon Sep 17 00:00:00 2001 From: Jukka Aho Date: Sat, 20 Jun 2015 16:00:32 +0300 Subject: [PATCH] added functions to add and remove nodes from model --- src/JuliaFEM.jl | 45 +++++++++++++++++++++++++++++++++++++++++++++ test/runtests.jl | 17 +++++++++++++++++ 2 files changed, 62 insertions(+) diff --git a/src/JuliaFEM.jl b/src/JuliaFEM.jl index 13907d2..5b0179d 100644 --- a/src/JuliaFEM.jl +++ b/src/JuliaFEM.jl @@ -71,5 +71,50 @@ function get_field(field_type, field_name; create_if_doesnt_exist=false) end +function add_nodes(model, nodes) + """Add new nodes to model. + + Parameters + ---------- + nodes : Dict() + id => coords + + Returns + ------- + model + + Notes + ----- + Create new field "coords" to model if not found + """ + + println("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 + + +function get_nodes(model, node_ids) + """Return subset of nodes from model. + + Parameters + ---------- + node_ids : array + list of node ids to return + + Returns + ------- + Dict() + id => coords + """ + subset = Dict() + for node_id in node_ids + subset[node_id] = model.nodes["coords"][node_id] + end + return subset +end + end # module diff --git a/test/runtests.jl b/test/runtests.jl index cecd4bf..5c6a190 100644 --- a/test/runtests.jl +++ b/test/runtests.jl @@ -1,6 +1,7 @@ using JuliaFEM using Base.Test + function test_create_model() m = new_model() fn = fieldnames(m) @@ -11,12 +12,14 @@ function test_create_model() @assert :element_gauss_points in fn end + function test_add_field() m = new_model() field = new_field(m.elements, "color") @assert "color" in keys(m.elements) end + function test_get_field() m = new_model() field = new_field(m.elements, "color") @@ -25,6 +28,7 @@ function test_get_field() @assert field2[1] == "red" end + function test_get_field_if_it_doesnt_exist() m = new_model() # FIXME: how to test exception? @@ -36,6 +40,19 @@ function test_get_field_if_it_doesnt_exist() end +function test_add_and_get_nodes() + m = new_model() + nodes = Dict(1 => [0.0, 0.0, 0.0], + 2 => [1.0, 0.0, 0.0], + 3 => [0.0, 1.0, 0.0], + 4 => [0.0, 0.0, 1.0]) + add_nodes(m, nodes) + subset = get_nodes(m, [2, 3]) + @assert length(subset) == 2 + @assert subset[2] == [1.0, 0.0, 0.0] + @assert subset[3] == [0.0, 1.0, 0.0] +end + # write your own tests here # @test 1 == JuliaFEM.test() # @test_approx_eq 1.0 1.0