diff --git a/src/problems.jl b/src/problems.jl index a303719..85ab797 100644 --- a/src/problems.jl +++ b/src/problems.jl @@ -252,3 +252,33 @@ end function push!(problem::Problem, element) push!(problem.elements, element) end + +""" Find dofs corresponding to nodes. """ +function find_dofs_by_nodes(problem::Problem, nodes) + dim = get_unknown_field_dimension(problem) + return find_dofs_by_nodes(dim, nodes) +end +function find_dofs_by_nodes(dim::Int, nodes) + dofs = Int64[] + for node in nodes + for j=1:dim + push!(dofs, dim*(node-1)+j) + end + end + return dofs +end + +""" Find nodes corresponding to dofs. """ +function find_nodes_by_dofs(problem::Problem, dofs) + dim = get_unknown_field_dimension(problem) +end +function find_nodes_by_dofs(dim, dofs) + nodes = Int64[] + for dof in dofs + j = Int(ceil(dof/dim)) + j in nodes && continue + push!(nodes, j) + end + return nodes +end + diff --git a/test/test_node_dof_mapping.jl b/test/test_node_dof_mapping.jl new file mode 100644 index 0000000..4b5c06b --- /dev/null +++ b/test/test_node_dof_mapping.jl @@ -0,0 +1,21 @@ +# This file is a part of JuliaFEM. +# License is MIT: see https://github.com/JuliaFEM/JuliaFEM.jl/blob/master/LICENSE.md + +using JuliaFEM.Test + +using JuliaFEM.Core: find_dofs_by_nodes, find_nodes_by_dofs + +@testset "find dofs given a set of nodes" begin + nodes = [1, 3] + dim = 3 + dofs = find_dofs_by_nodes(dim, nodes) + @test dofs == [1, 2, 3, 7, 8, 9] +end + +@testset "find nodes given a set of dofs" begin + dofs = [2, 8, 9] + dim = 3 + nodes = find_nodes_by_dofs(dim, dofs) + @test nodes == [1, 3] +end +