From 4609aa38e1a110636e122697cec8cde9630d2562 Mon Sep 17 00:00:00 2001
From: mattijs <mattijs>
Date: Mon, 4 Dec 2023 14:38:45 +0000
Subject: [PATCH] BUG: redistributePar: fix global object headerOk. See !605

- mesh/parallel/cavity not yet working since zero-sized mesh
  not written on new processors.
---
 .../redistributePar/redistributePar.C         | 11 +++-
 src/OpenFOAM/db/IOobject/IOobjectTemplates.C  | 64 +++++++++++++------
 2 files changed, 56 insertions(+), 19 deletions(-)

diff --git a/applications/utilities/parallelProcessing/redistributePar/redistributePar.C b/applications/utilities/parallelProcessing/redistributePar/redistributePar.C
index 6f36d2725ed..caef6f96110 100644
--- a/applications/utilities/parallelProcessing/redistributePar/redistributePar.C
+++ b/applications/utilities/parallelProcessing/redistributePar/redistributePar.C
@@ -110,7 +110,8 @@ using namespace Foam;
 
 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
 
-const int debug(::Foam::debug::debugSwitch("redistributePar", 0));
+// Use -verbose -verbose to switch on debug info. TBD.
+int debug(::Foam::debug::debugSwitch("redistributePar", 0));
 #define InfoOrPout (::debug ? Pout : Info())
 
 
@@ -1307,6 +1308,14 @@ int main(int argc, char *argv[])
 
     if (optVerbose)
     {
+        if (optVerbose == 2)
+        {
+            WarningInFunction
+                << "-verbose -verbose switches on full debugging"
+                << nl << endl;
+            ::debug = 1;
+        }
+
         // Report on output
         faMeshDistributor::verbose_ = 1;
         parPointFieldDistributor::verbose_ = 1;
diff --git a/src/OpenFOAM/db/IOobject/IOobjectTemplates.C b/src/OpenFOAM/db/IOobject/IOobjectTemplates.C
index d8218133bf1..f83a5fcba2b 100644
--- a/src/OpenFOAM/db/IOobject/IOobjectTemplates.C
+++ b/src/OpenFOAM/db/IOobject/IOobjectTemplates.C
@@ -59,30 +59,58 @@ bool Foam::IOobject::typeHeaderOk
 
     // Determine local status
     bool ok = false;
-    fileName fName;
 
-    if (!masterOnly || UPstream::master())
-    {
-        fName = typeFilePath<Type>(*this, search);
-        ok = fp.readHeader(*this, fName, Type::typeName);
-    }
-
-    if (ok && checkType && headerClassName_ != Type::typeName)
+    if (masterOnly)
     {
-        ok = false;
-        if (verbose)
+        if (UPstream::master())
         {
-            WarningInFunction
-                << "Unexpected class name \"" << headerClassName_
-                << "\" expected \"" << Type::typeName
-                << "\" when reading " << fName << endl;
+            // Force master-only header reading
+            const bool oldParRun = UPstream::parRun(false);
+            const fileName fName(typeFilePath<Type>(*this, search));
+            ok = fp.readHeader(*this, fName, Type::typeName);
+            UPstream::parRun(oldParRun);
+
+            if (ok && checkType && headerClassName_ != Type::typeName)
+            {
+                ok = false;
+                if (verbose)
+                {
+                    WarningInFunction
+                        << "Unexpected class name \"" << headerClassName_
+                        << "\" expected \"" << Type::typeName
+                        << "\" when reading " << fName << endl;
+                }
+            }
         }
-    }
 
-    // If masterOnly make sure all processors know about it
-    if (masterOnly)
+        // If masterOnly make sure all processors know about the read
+        // information. Note: should ideally be inside fileHandler...
+        Pstream::broadcasts
+        (
+            fp.comm(),
+            ok,
+            headerClassName_,
+            note_
+        );
+    }
+    else
     {
-        Pstream::broadcast(ok);
+        const fileName fName(typeFilePath<Type>(*this, search));
+
+        // All read header
+        ok = fp.readHeader(*this, fName, Type::typeName);
+
+        if (ok && checkType && headerClassName_ != Type::typeName)
+        {
+            ok = false;
+            if (verbose)
+            {
+                WarningInFunction
+                    << "Unexpected class name \"" << headerClassName_
+                    << "\" expected \"" << Type::typeName
+                    << "\" when reading " << fName << endl;
+            }
+        }
     }
 
     return ok;
-- 
GitLab