From 3d0cb79be3d6d9bb476e20cf8d90009e7a98cd42 Mon Sep 17 00:00:00 2001
From: mattijs <mattijs>
Date: Thu, 18 Jan 2024 16:11:32 +0000
Subject: [PATCH] ENH: mapped: keep coupling info. Fixes #3090

---
 .../mappedPolyPatch/mappedPatchBase.C         | 84 ++++++++++++++++++-
 .../mappedPolyPatch/mappedPatchBase.H         | 26 +++++-
 .../mappedPolyPatch/mappedPatchBaseI.H        | 19 -----
 .../mappedPolyPatch/mappedPolyPatch.C         | 15 ++--
 .../mappedPolyPatch/mappedWallPolyPatch.C     | 14 ++--
 5 files changed, 122 insertions(+), 36 deletions(-)

diff --git a/src/meshTools/mappedPatches/mappedPolyPatch/mappedPatchBase.C b/src/meshTools/mappedPatches/mappedPolyPatch/mappedPatchBase.C
index e1b457edd05..3f281664211 100644
--- a/src/meshTools/mappedPatches/mappedPolyPatch/mappedPatchBase.C
+++ b/src/meshTools/mappedPatches/mappedPolyPatch/mappedPatchBase.C
@@ -6,7 +6,7 @@
      \\/     M anipulation  |
 -------------------------------------------------------------------------------
     Copyright (C) 2011-2016 OpenFOAM Foundation
-    Copyright (C) 2015-2023 OpenCFD Ltd.
+    Copyright (C) 2015-2024 OpenCFD Ltd.
 -------------------------------------------------------------------------------
 License
     This file is part of OpenFOAM.
@@ -86,6 +86,24 @@ Foam::mappedPatchBase::offsetModeNames_
 
 // * * * * * * * * * * * * * Private Member Functions  * * * * * * * * * * * //
 
+void Foam::mappedPatchBase::calcGeometry(PstreamBuffers& pBufs)
+{}
+
+
+void Foam::mappedPatchBase::movePoints
+(
+    PstreamBuffers& pBufs,
+    const pointField& p
+)
+{}
+
+
+void Foam::mappedPatchBase::updateMesh(PstreamBuffers& pBufs)
+{
+    clearOut();
+}
+
+
 Foam::autoPtr<Foam::fileName> Foam::mappedPatchBase::readDatabase
 (
     const dictionary& dict
@@ -1569,8 +1587,9 @@ Foam::mappedPatchBase::~mappedPatchBase()
 void Foam::mappedPatchBase::clearOut()
 {
     mapPtr_.reset(nullptr);
-    surfPtr_.reset(nullptr);
     AMIPtr_->upToDate(false);
+
+    //Note: no need to clear out surface since not mesh related
 }
 
 
@@ -1674,6 +1693,67 @@ const Foam::polyPatch& Foam::mappedPatchBase::samplePolyPatch() const
 }
 
 
+bool Foam::mappedPatchBase::upToDate() const
+{
+    const polyMesh& thisMesh = patch_.boundaryMesh().mesh();
+
+    bool thisUpToDate = thisMesh.upToDatePoints(updateMeshTime());
+    bool sampleUpToDate =
+    (
+        sameWorld()
+      ? sampleMesh().upToDatePoints(updateSampleMeshTime())
+      : true
+    );
+
+    if (!thisUpToDate && thisMesh.moving())
+    {
+        // Moving (but not topoChanging mesh) : do more accurate check:
+        // compare actual patch point position
+
+        thisUpToDate = true;
+        for (const label pointi : patch_.meshPoints())
+        {
+            const point& oldPt = thisMesh.oldPoints()[pointi];
+            const point& thisPt = thisMesh.points()[pointi];
+            if (mag(oldPt-thisPt) > SMALL)
+            {
+                thisUpToDate = false;
+                break;
+            }
+        }
+        Pstream::reduceAnd(thisUpToDate);
+
+        if (thisUpToDate)
+        {
+            updateMeshTime().setUpToDate();
+        }
+    }
+
+    if (!sampleUpToDate && sampleMesh().moving())
+    {
+        sampleUpToDate = true;
+        for (const label pointi : samplePolyPatch().meshPoints())
+        {
+            const point& oldPt = sampleMesh().oldPoints()[pointi];
+            const point& samplePt = sampleMesh().points()[pointi];
+            if (mag(oldPt-samplePt) > SMALL)
+            {
+                sampleUpToDate = false;
+                break;
+            }
+        }
+        Pstream::reduceAnd(sampleUpToDate);
+
+        if (sampleUpToDate)
+        {
+            updateSampleMeshTime().setUpToDate();
+        }
+    }
+
+    return (thisUpToDate && sampleUpToDate);
+}
+
+
 Foam::tmp<Foam::pointField> Foam::mappedPatchBase::samplePoints
 (
     const pointField& fc
diff --git a/src/meshTools/mappedPatches/mappedPolyPatch/mappedPatchBase.H b/src/meshTools/mappedPatches/mappedPolyPatch/mappedPatchBase.H
index f2a90f90917..de3715d2634 100644
--- a/src/meshTools/mappedPatches/mappedPolyPatch/mappedPatchBase.H
+++ b/src/meshTools/mappedPatches/mappedPolyPatch/mappedPatchBase.H
@@ -301,6 +301,30 @@ protected:
 
     // Protected Member Functions
 
+        // polyPatch callbacks
+
+            //- Initialise the calculation of the patch geometry
+            virtual void initGeometry(PstreamBuffers&)
+            {}
+
+            //- Calculate the patch geometry
+            virtual void calcGeometry(PstreamBuffers&);
+
+            //- Initialise the patches for moving points
+            virtual void initMovePoints(PstreamBuffers&, const pointField&)
+            {}
+
+            //- Correct patches after moving points
+            virtual void movePoints(PstreamBuffers&, const pointField&);
+
+            //- Initialise the update of the patch topology
+            virtual void initUpdateMesh(PstreamBuffers&)
+            {}
+
+            //- Update of the patch topology
+            virtual void updateMesh(PstreamBuffers&);
+
+
         //- Add a world-world connection
         bool addWorldConnection();
 
@@ -532,7 +556,7 @@ public:
             //- Sample mesh upate time
             inline uniformDimensionedScalarField& updateMeshTime() const;
 
-            inline bool upToDate() const;
+            bool upToDate() const;
 
             //- Return reference to the parallel distribution map
             inline const mapDistribute& map() const;
diff --git a/src/meshTools/mappedPatches/mappedPolyPatch/mappedPatchBaseI.H b/src/meshTools/mappedPatches/mappedPolyPatch/mappedPatchBaseI.H
index b2ebe51e9ff..5c267312207 100644
--- a/src/meshTools/mappedPatches/mappedPolyPatch/mappedPatchBaseI.H
+++ b/src/meshTools/mappedPatches/mappedPolyPatch/mappedPatchBaseI.H
@@ -252,25 +252,6 @@ Foam::mappedPatchBase::updateMeshTime() const
 }
 
 
-inline bool Foam::mappedPatchBase::upToDate() const
-{
-    const polyMesh& thisMesh = patch_.boundaryMesh().mesh();
-
-    if (sameWorld())
-    {
-        return
-            sampleMesh().upToDatePoints(updateSampleMeshTime())
-         && thisMesh.upToDatePoints(updateMeshTime());
-    }
-    else
-    {
-        // If not the same world we do not know what the other side is doing
-        // so only check our local side
-        return thisMesh.upToDatePoints(updateMeshTime());
-    }
-}
-
-
 inline const Foam::mapDistribute& Foam::mappedPatchBase::map() const
 {
     if (!upToDate())
diff --git a/src/meshTools/mappedPatches/mappedPolyPatch/mappedPolyPatch.C b/src/meshTools/mappedPatches/mappedPolyPatch/mappedPolyPatch.C
index 76d7f2bb827..79ccb0a79bd 100644
--- a/src/meshTools/mappedPatches/mappedPolyPatch/mappedPolyPatch.C
+++ b/src/meshTools/mappedPatches/mappedPolyPatch/mappedPolyPatch.C
@@ -6,7 +6,7 @@
      \\/     M anipulation  |
 -------------------------------------------------------------------------------
     Copyright (C) 2011-2013 OpenFOAM Foundation
-    Copyright (C) 2021-2023 OpenCFD Ltd.
+    Copyright (C) 2021-2024 OpenCFD Ltd.
 -------------------------------------------------------------------------------
 License
     This file is part of OpenFOAM.
@@ -169,9 +169,7 @@ Foam::mappedPolyPatch::mappedPolyPatch
 // * * * * * * * * * * * * * * * * Destructor  * * * * * * * * * * * * * * * //
 
 Foam::mappedPolyPatch::~mappedPolyPatch()
-{
-    mappedPatchBase::clearOut();
-}
+{}
 
 
 // * * * * * * * * * * * * * * * Member Functions  * * * * * * * * * * * * * //
@@ -179,13 +177,14 @@ Foam::mappedPolyPatch::~mappedPolyPatch()
 void Foam::mappedPolyPatch::initGeometry(PstreamBuffers& pBufs)
 {
     polyPatch::initGeometry(pBufs);
+    mappedPatchBase::initGeometry(pBufs);
 }
 
 
 void Foam::mappedPolyPatch::calcGeometry(PstreamBuffers& pBufs)
 {
     polyPatch::calcGeometry(pBufs);
-    mappedPatchBase::clearOut();
+    mappedPatchBase::calcGeometry(pBufs);
 }
 
 
@@ -196,6 +195,7 @@ void Foam::mappedPolyPatch::initMovePoints
 )
 {
     polyPatch::initMovePoints(pBufs, p);
+    mappedPatchBase::initMovePoints(pBufs, p);
 }
 
 
@@ -206,20 +206,21 @@ void Foam::mappedPolyPatch::movePoints
 )
 {
     polyPatch::movePoints(pBufs, p);
-    mappedPatchBase::clearOut();
+    mappedPatchBase::movePoints(pBufs, p);
 }
 
 
 void Foam::mappedPolyPatch::initUpdateMesh(PstreamBuffers& pBufs)
 {
     polyPatch::initUpdateMesh(pBufs);
+    mappedPatchBase::initUpdateMesh(pBufs);
 }
 
 
 void Foam::mappedPolyPatch::updateMesh(PstreamBuffers& pBufs)
 {
     polyPatch::updateMesh(pBufs);
-    mappedPatchBase::clearOut();
+    mappedPatchBase::updateMesh(pBufs);
 }
 
 
diff --git a/src/meshTools/mappedPatches/mappedPolyPatch/mappedWallPolyPatch.C b/src/meshTools/mappedPatches/mappedPolyPatch/mappedWallPolyPatch.C
index 759e07aaa13..223bfc0aeec 100644
--- a/src/meshTools/mappedPatches/mappedPolyPatch/mappedWallPolyPatch.C
+++ b/src/meshTools/mappedPatches/mappedPolyPatch/mappedWallPolyPatch.C
@@ -6,7 +6,7 @@
      \\/     M anipulation  |
 -------------------------------------------------------------------------------
     Copyright (C) 2011-2013 OpenFOAM Foundation
-    Copyright (C) 2021-2023 OpenCFD Ltd.
+    Copyright (C) 2021-2024 OpenCFD Ltd.
 -------------------------------------------------------------------------------
 License
     This file is part of OpenFOAM.
@@ -175,9 +175,7 @@ Foam::mappedWallPolyPatch::mappedWallPolyPatch
 // * * * * * * * * * * * * * * * * Destructor  * * * * * * * * * * * * * * * //
 
 Foam::mappedWallPolyPatch::~mappedWallPolyPatch()
-{
-    mappedPatchBase::clearOut();
-}
+{}
 
 
 // * * * * * * * * * * * * * * * Member Functions  * * * * * * * * * * * * * //
@@ -191,7 +189,7 @@ void Foam::mappedWallPolyPatch::initGeometry(PstreamBuffers& pBufs)
 void Foam::mappedWallPolyPatch::calcGeometry(PstreamBuffers& pBufs)
 {
     wallPolyPatch::calcGeometry(pBufs);
-    mappedPatchBase::clearOut();
+    mappedPatchBase::calcGeometry(pBufs);
 }
 
 
@@ -202,6 +200,7 @@ void Foam::mappedWallPolyPatch::initMovePoints
 )
 {
     wallPolyPatch::initMovePoints(pBufs, p);
+    mappedPatchBase::initMovePoints(pBufs, p);
 }
 
 
@@ -212,20 +211,21 @@ void Foam::mappedWallPolyPatch::movePoints
 )
 {
     wallPolyPatch::movePoints(pBufs, p);
-    mappedPatchBase::clearOut();
+    mappedPatchBase::movePoints(pBufs, p);
 }
 
 
 void Foam::mappedWallPolyPatch::initUpdateMesh(PstreamBuffers& pBufs)
 {
     wallPolyPatch::initUpdateMesh(pBufs);
+    mappedPatchBase::initUpdateMesh(pBufs);
 }
 
 
 void Foam::mappedWallPolyPatch::updateMesh(PstreamBuffers& pBufs)
 {
     wallPolyPatch::updateMesh(pBufs);
-    mappedPatchBase::clearOut();
+    mappedPatchBase::updateMesh(pBufs);
 }
 
 
-- 
GitLab