From 2ad2536f88ebf60657c8eda28c402d4f84726bc4 Mon Sep 17 00:00:00 2001
From: Koen Hillewaert <koen.hillewaert@cenaero.be>
Date: Mon, 20 Jun 2016 13:38:57 +0000
Subject: [PATCH] include closure points for the periodic counterparts in order
 to ensure finding the correct periodic copy, compatible with the periodicity
 at hand (in case of multiple periodicicities)

---
 Geo/GModel.cpp     | 12 +++++++++---
 Mesh/meshGEdge.cpp | 11 +++++++++++
 Mesh/meshGFace.cpp | 10 ++++++++--
 3 files changed, 28 insertions(+), 5 deletions(-)

diff --git a/Geo/GModel.cpp b/Geo/GModel.cpp
index 39072bc672..1fbc053cad 100644
--- a/Geo/GModel.cpp
+++ b/Geo/GModel.cpp
@@ -2017,7 +2017,8 @@ void GModel::alignPeriodicBoundaries()
         MVertex* tgtVtcs[2];
         for (int iVtx=0;iVtx<2;iVtx++) {
           MVertex* tgtVtx = tgtLine->getVertex(iVtx);
-          std::map<MVertex*,MVertex*>& v2v = tgtVtx->onWhat()->correspondingVertices;
+          // std::map<MVertex*,MVertex*>& v2v = tgtVtx->onWhat()->correspondingVertices;
+          std::map<MVertex*,MVertex*>& v2v = tgt->correspondingVertices;
           std::map<MVertex*,MVertex*>::iterator srcIter = v2v.find(tgtVtx);
           if (srcIter == v2v.end()) {
             Msg::Error("Cannot find periodic counterpart of vertex %d"
@@ -2089,8 +2090,13 @@ void GModel::alignPeriodicBoundaries()
         for (int iVtx=0;iVtx<nbVtcs;iVtx++) {
           MVertex* vtx = tgtElmt->getVertex(iVtx);
           GEntity* ge = vtx->onWhat();
-          if (ge->meshMaster() == ge) throw;
-          std::map<MVertex*,MVertex*>& v2v = ge->correspondingVertices;
+          if (ge->meshMaster() == ge) {
+            std::cout << "Point classified on " << ge->dim() << " " << ge->tag() << std::endl;
+            throw;
+          }
+          // std::map<MVertex*,MVertex*>& v2v = ge->correspondingVertices;
+          std::map<MVertex*,MVertex*>& v2v = tgt->correspondingVertices;
+          
           std::map<MVertex*,MVertex*>::iterator vIter = v2v.find(vtx);
           if (vIter==v2v.end()) {
             Msg::Error("Could not find copy of %d in %d",
diff --git a/Mesh/meshGEdge.cpp b/Mesh/meshGEdge.cpp
index 89e8da3718..a95e14da70 100644
--- a/Mesh/meshGEdge.cpp
+++ b/Mesh/meshGEdge.cpp
@@ -314,6 +314,17 @@ void copyMesh(GEdge *from, GEdge *to, int direction)
   Range<double> to_u_bounds = to->parBounds(0);
   double to_u_min = to_u_bounds.low();
 
+  // include begin and end point to avoid conflicts when realigning
+  
+  MVertex* vt0 = to->getBeginVertex()->mesh_vertices[0];
+  MVertex* vt1 = to->getEndVertex()->mesh_vertices[0];
+  
+  MVertex* vs0 = from->getBeginVertex()->mesh_vertices[0];
+  MVertex* vs1 = from->getEndVertex()->mesh_vertices[0];
+  
+  to->correspondingVertices[vt0] = direction > 0 ? vs0 : vs1;
+  to->correspondingVertices[vt1] = direction > 0 ? vs1 : vs0;
+  
   for(unsigned int i = 0; i < from->mesh_vertices.size(); i++){
     int index = (direction < 0) ? (from->mesh_vertices.size() - 1 - i) : i;
     MVertex *v = from->mesh_vertices[index];
diff --git a/Mesh/meshGFace.cpp b/Mesh/meshGFace.cpp
index 53b4b42faf..3d27ba5c8e 100644
--- a/Mesh/meshGFace.cpp
+++ b/Mesh/meshGFace.cpp
@@ -276,7 +276,10 @@ static void copyMesh(GFace *source, GFace *target)
                      target->tag(),source->tag(),gvt->tag());
     }
 
-    vs2vt[gvs->mesh_vertices[0]] = gvt->mesh_vertices[0];
+    MVertex* vs = gvs->mesh_vertices[0];
+    MVertex* vt = gvt->mesh_vertices[0];
+    vs2vt[vs] = vt;
+    target->correspondingVertices[vt] = vs;
   }
 
   // add corresponding edge nodes assuming edges were correctly meshed already
@@ -319,7 +322,10 @@ static void copyMesh(GFace *source, GFace *target)
     int is = orientation == 1 ? 0 : get->mesh_vertices.size()-1;
 
     for (unsigned it=0;it<get->mesh_vertices.size();it++,is+=orientation) {
-      vs2vt[ges->mesh_vertices[is]] = get->mesh_vertices[it];
+      MVertex* vs = ges->mesh_vertices[is];
+      MVertex* vt = get->mesh_vertices[it];
+      vs2vt[vs] = vt; 
+      target->correspondingVertices[vt] = vs;
     }
   }
 
-- 
GitLab