From 945405c32dca5682827356d29fa5557abb3d88d8 Mon Sep 17 00:00:00 2001
From: Andrew Heather <>
Date: Fri, 18 Nov 2022 16:46:25 +0000
Subject: [PATCH] BUG: avoid excessive recalculation of map for moving meshes

---
 .../mappedPolyPatch/mappedPatchBase.C         |  5 ++
 .../mappedPolyPatch/mappedPatchBase.H         | 19 +++++
 .../mappedPolyPatch/mappedPatchBaseI.H        | 83 ++++++++++++++++---
 3 files changed, 97 insertions(+), 10 deletions(-)

diff --git a/src/meshTools/mappedPatches/mappedPolyPatch/mappedPatchBase.C b/src/meshTools/mappedPatches/mappedPolyPatch/mappedPatchBase.C
index e4928adc3ba..15431135d94 100644
--- a/src/meshTools/mappedPatches/mappedPolyPatch/mappedPatchBase.C
+++ b/src/meshTools/mappedPatches/mappedPolyPatch/mappedPatchBase.C
@@ -724,6 +724,8 @@ void Foam::mappedPatchBase::findSamples
 
 void Foam::mappedPatchBase::calcMapping() const
 {
+    DebugInFunction;
+
     static bool hasWarned = false;
     if (mapPtr_)
     {
@@ -1051,6 +1053,9 @@ void Foam::mappedPatchBase::calcMapping() const
             }
         }
     }
+
+    updateMeshTime().setUpToDate();
+    updateSampleMeshTime().setUpToDate();
 }
 
 
diff --git a/src/meshTools/mappedPatches/mappedPolyPatch/mappedPatchBase.H b/src/meshTools/mappedPatches/mappedPolyPatch/mappedPatchBase.H
index fc327fc260c..f2a90f90917 100644
--- a/src/meshTools/mappedPatches/mappedPolyPatch/mappedPatchBase.H
+++ b/src/meshTools/mappedPatches/mappedPolyPatch/mappedPatchBase.H
@@ -96,6 +96,7 @@ SourceFiles
 #include "pointIndexHit.H"
 #include "AMIPatchToPatchInterpolation.H"
 #include "coupleGroupIdentifier.H"
+#include "uniformDimensionedFields.H"
 
 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
 
@@ -288,6 +289,16 @@ protected:
             dictionary surfDict_;
 
 
+        // Mesh update IOobjects
+
+            //- Local mesh update time
+            mutable autoPtr<uniformDimensionedScalarField> updateMeshTimePtr_;
+
+            //- Sample mesh update time
+            mutable autoPtr<uniformDimensionedScalarField>
+                updateSampleMeshTimePtr_;
+
+
     // Protected Member Functions
 
         //- Add a world-world connection
@@ -515,6 +526,14 @@ public:
             //- Cached sampleRegion != mesh.name()
             inline bool sameRegion() const noexcept;
 
+            //- Local mesh update time
+            inline uniformDimensionedScalarField& updateSampleMeshTime() const;
+
+            //- Sample mesh upate time
+            inline uniformDimensionedScalarField& updateMeshTime() const;
+
+            inline 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 f80c3defb18..9f753e9efa5 100644
--- a/src/meshTools/mappedPatches/mappedPolyPatch/mappedPatchBaseI.H
+++ b/src/meshTools/mappedPatches/mappedPolyPatch/mappedPatchBaseI.H
@@ -196,16 +196,78 @@ inline bool Foam::mappedPatchBase::sameRegion() const noexcept
 }
 
 
-inline const Foam::mapDistribute& Foam::mappedPatchBase::map() const
+inline Foam::uniformDimensionedScalarField&
+Foam::mappedPatchBase::updateSampleMeshTime() const
+{
+    if (!updateSampleMeshTimePtr_)
+    {
+        const auto& mesh = sampleMesh();
+
+        updateSampleMeshTimePtr_ =
+            autoPtr<uniformDimensionedScalarField>::New
+            (
+                IOobject
+                (
+                    "updateSampleMeshTime",
+                    mesh.pointsInstance(),
+                    mesh,
+                    IOobject::NO_READ,
+                    IOobject::NO_WRITE,
+                    false
+                )
+            );
+    }
+
+    return updateSampleMeshTimePtr_();
+}
+
+
+inline Foam::uniformDimensionedScalarField&
+Foam::mappedPatchBase::updateMeshTime() const
+{
+    if (!updateMeshTimePtr_)
+    {
+        const auto& mesh = patch_.boundaryMesh().mesh();
+
+        updateMeshTimePtr_ =
+            autoPtr<uniformDimensionedScalarField>::New
+            (
+                IOobject
+                (
+                    "updateMeshTime",
+                    mesh.pointsInstance(),
+                    mesh,
+                    IOobject::NO_READ,
+                    IOobject::NO_WRITE,
+                    false
+                )
+            );
+    }
+
+    return updateMeshTimePtr_();
+}
+
+
+inline bool Foam::mappedPatchBase::upToDate() const
 {
     const polyMesh& thisMesh = patch_.boundaryMesh().mesh();
-    bool topoChange =
-        (sameWorld() && sampleMesh().topoChanging())
-     || thisMesh.topoChanging();
 
-    if (topoChange)
+    return
+        (sameWorld() && sampleMesh().upToDatePoints(updateSampleMeshTime()))
+     && thisMesh.upToDatePoints(updateMeshTime());
+}
+
+
+inline const Foam::mapDistribute& Foam::mappedPatchBase::map() const
+{
+    if (!upToDate())
     {
         mapPtr_.reset(nullptr);
+
+        if (AMIPtr_)
+        {
+            AMIPtr_->upToDate() = false;
+        }
     }
 
     if (!mapPtr_)
@@ -222,12 +284,13 @@ inline const Foam::AMIPatchToPatchInterpolation& Foam::mappedPatchBase::AMI
     bool forceUpdate
 ) const
 {
-    const polyMesh& thisMesh = patch_.boundaryMesh().mesh();
-    bool topoChange =
-        (sameWorld() && sampleMesh().topoChanging())
-     || thisMesh.topoChanging();
+    if (!upToDate())
+    {
+        mapPtr_.reset(nullptr);
+        AMIPtr_->upToDate() = false;
+    }
 
-    if (topoChange || forceUpdate)
+    if (forceUpdate)
     {
         AMIPtr_->upToDate() = false;
     }
-- 
GitLab