I am working on implementing periodic boundary conditions in a finite element problem and I want to pair the nodes on boundary A with nodes on boundary B given a vector trans
that lines boundary A up with boundary B. The nodes in boundary A are given in a list g1
; in B they are g2
. The node coordinates are looked up in mesh%nodes(:,nodenum)
.
I have decided to do this by creating a distance matrix for each node, which I realise is not the most efficient way, and to be honest I don't expect to save significant time by optimising this algorithm. The question is more academic.
I know that Fortran stores in column-major order, on the other hand, the array will be symmetric and when the array is completed I want to take column slices of it to find the nearest node. So the question is how should one populate this?
Here is my naive attempt.
subroutine autopair_nodes_in_groups(mesh, g1, g2, pairs, trans)
type(meshdata) :: mesh
integer(kind=sp) :: i,j
integer(kind=sp),dimension(:) :: g1,g2
integer(kind=sp),dimension(:,:) :: pairs
real(kind=dp) :: trans(3) !xyz translate
real(kind=dp) :: dist_mat(size(g1),size(g2))
real(kind=dp) :: p1(3), p2(3)
dist_mat = -1.0_wp
! make a distance matrix
do j=1,size(g2)
p2 = mesh%nodes(1:3,g2(j))-trans
do i=1,j
p1 = mesh%nodes(1:3,g1(i))
dist_mat(i,j) = norm2(p1-p2) !equivalent to norm2(n1pos+trans-n2pos)
if (i.ne.j) dist_mat(j,i) = dist_mat(i,j) !fill symmetry
end do
end do
! Remainder of routine to find nearest nodes
end subroutine autopair_nodes_in_groups
The problem as far as I can tell is that this is efficient in terms of memory access until one symmetrises the array.