From 44c8237ac71055335eaad93442d3c26ff420b638 Mon Sep 17 00:00:00 2001
From: Jesse Chan <1156048+jlchan@users.noreply.github.com>
Date: Thu, 27 Jun 2024 19:31:21 -0500
Subject: [PATCH] Fix tet face node ordering to be consistent with `rd.Fmask`
 (#172)

* reorder tet face nodes to be consistent with Fmask

* fix periodic BCs in 3D (broken by changing tet face node order)

* remove cruft, preallocate p
---
 src/connectivity_functions.jl | 36 +++++++++++++++++++----------------
 src/ref_elem_utils.jl         |  8 ++++----
 2 files changed, 24 insertions(+), 20 deletions(-)

diff --git a/src/connectivity_functions.jl b/src/connectivity_functions.jl
index da775d35..aa845354 100644
--- a/src/connectivity_functions.jl
+++ b/src/connectivity_functions.jl
@@ -320,16 +320,16 @@ function build_periodic_boundary_maps!(xf, yf, zf,
     yfaces = map(x -> x[1], findall(@. (@. abs(yc - ymax) < NODETOL * LY) | (@. abs(yc - ymin) < NODETOL * LY)))
     zfaces = map(x -> x[1], findall(@. (@. abs(zc - zmax) < NODETOL * LZ) | (@. abs(zc - zmin) < NODETOL * LZ)))
 
-    D = zeros(eltype(xb), size(xb,1), size(xb,1))
-    ids = zeros(Int, size(xb, 1))
+    p = zeros(Int, size(xb, 1))
     if is_periodic_x # find matches in x faces
         for i in xfaces, j in xfaces
-            if i!=j
-                if abs(yc[i] - yc[j]) < NODETOL * LY && abs(zc[i] - zc[j]) < NODETOL * LZ && abs(abs(xc[i] - xc[j]) - LX) < NODETOL * LX
-                    # create distance matrix
-                    @. D = abs(yb[:,i] - yb[:,j]') + abs(zb[:,i] - zb[:,j]')
-                    map!(x->x[1], ids, findall(@. D < NODETOL * LY))
-                    @. mapPB[:,i] = mapMB[ids,j]
+            if i!=j                
+                if abs(yc[i] - yc[j]) < NODETOL * LY && 
+                   abs(zc[i] - zc[j]) < NODETOL * LZ && 
+                   abs(abs(xc[i] - xc[j]) - LX) < NODETOL * LX
+
+                    match_coordinate_vectors!(p, (yb[:,i], zb[:,i]), (yb[:,j], zb[:,j]))
+                    @. mapPB[:,i] = mapMB[p,j]
 
                     FToF[Bfaces[i]] = Bfaces[j]
                 end
@@ -341,10 +341,12 @@ function build_periodic_boundary_maps!(xf, yf, zf,
     if is_periodic_y
         for i in yfaces, j = yfaces
             if i!=j
-                if abs(xc[i] - xc[j]) < NODETOL * LX && abs(zc[i] - zc[j]) < NODETOL * LZ && abs(abs(yc[i] - yc[j]) - LY) < NODETOL * LY
-                    @. D = abs(xb[:,i] - xb[:,j]') + abs(zb[:,i] - zb[:,j]')
-                    map!(x->x[1], ids, findall(@. D < NODETOL * LX))
-                    @. mapPB[:,i] = mapMB[ids,j]
+                if abs(xc[i] - xc[j]) < NODETOL * LX && 
+                   abs(zc[i] - zc[j]) < NODETOL * LZ && 
+                   abs(abs(yc[i] - yc[j]) - LY) < NODETOL * LY
+
+                    match_coordinate_vectors!(p, (xb[:,i], zb[:,i]), (xb[:,j], zb[:,j]))
+                    @. mapPB[:,i] = mapMB[p,j]
 
                     FToF[Bfaces[i]] = Bfaces[j]
                 end
@@ -356,10 +358,12 @@ function build_periodic_boundary_maps!(xf, yf, zf,
     if is_periodic_z
         for i in zfaces, j in zfaces
             if i!=j
-                if abs(xc[i] - xc[j]) < NODETOL * LX && abs(yc[i] - yc[j]) < NODETOL * LY && abs(abs(zc[i] - zc[j]) - LZ) < NODETOL * LZ
-                    @. D = abs(xb[:,i] - xb[:,j]') + abs(yb[:,i] - yb[:,j]')
-                    map!(x->x[1], ids, findall(@. D < NODETOL * LX))
-                    @. mapPB[:,i] = mapMB[ids,j]
+                if abs(xc[i] - xc[j]) < NODETOL * LX && 
+                   abs(yc[i] - yc[j]) < NODETOL * LY && 
+                   abs(abs(zc[i] - zc[j]) - LZ) < NODETOL * LZ
+
+                    match_coordinate_vectors!(p, (xb[:,i], yb[:,i]), (xb[:,j], yb[:,j]))
+                    @. mapPB[:,i] = mapMB[p,j]
 
                     FToF[Bfaces[i]] = Bfaces[j]
                 end
diff --git a/src/ref_elem_utils.jl b/src/ref_elem_utils.jl
index 1fa75801..1049868d 100644
--- a/src/ref_elem_utils.jl
+++ b/src/ref_elem_utils.jl
@@ -30,13 +30,13 @@ end
 function map_face_nodes(::Tet, face_nodes...)
     r, s = face_nodes
     e = ones(size(r))
-    rf = [r; r; -e; r]
-    sf = [-e; s; r; s]
-    tf = [s; -(e + r + s); s; -e]
+    rf = [r; -(e + r + s); -e; r]
+    sf = [-e; r; r; s]
+    tf = [s; s; s; -e]
     return rf, sf, tf
 end
 
-function init_face_data(elem::Tri; quad_rule_face = gauss_quad(0,0,N))
+function init_face_data(elem::Tri; quad_rule_face = gauss_quad(0, 0, N))
     r1D, w1D = quad_rule_face
     e = ones(size(r1D)) 
     z = zeros(size(r1D))