From e119a853c425146ab7b5d3e15dff9c5697c8cac8 Mon Sep 17 00:00:00 2001
From: mattijs <mattijs>
Date: Thu, 11 Jul 2024 13:49:46 +0100
Subject: [PATCH] BUG: READ_IF_PRESENT: return false if not read. Fixes #3193

Side effects:
- processors will not call readStream
- return false if not headerOk
---
 .../IOobjects/CompactIOField/CompactIOField.C | 64 ++++++++-----------
 src/OpenFOAM/db/IOobjects/IOField/IOField.C   | 30 +++------
 2 files changed, 35 insertions(+), 59 deletions(-)

diff --git a/src/OpenFOAM/db/IOobjects/CompactIOField/CompactIOField.C b/src/OpenFOAM/db/IOobjects/CompactIOField/CompactIOField.C
index 8f5a6c2d978..c0da5cd9bf9 100644
--- a/src/OpenFOAM/db/IOobjects/CompactIOField/CompactIOField.C
+++ b/src/OpenFOAM/db/IOobjects/CompactIOField/CompactIOField.C
@@ -34,50 +34,38 @@ License
 template<class T, class BaseType>
 bool Foam::CompactIOField<T, BaseType>::readIOcontents(bool readOnProc)
 {
-    if (readOpt() == IOobject::MUST_READ)
+    if (isReadRequired() || (isReadOptional() && headerOk()))
     {
-        // Reading
-    }
-    else if (isReadOptional())
-    {
-        if (!headerOk())
-        {
-            readOnProc = false;
-        }
-    }
-    else
-    {
-        return false;
-    }
-
+        // Do reading
+        Istream& is = readStream(word::null, readOnProc);
 
-    // Do reading
-    Istream& is = readStream(word::null, readOnProc);
-
-    if (readOnProc)
-    {
-        if (headerClassName() == IOField<T>::typeName)
+        if (readOnProc)
         {
-            is >> static_cast<Field<T>&>(*this);
-            close();
-        }
-        else if (headerClassName() == typeName)
-        {
-            is >> *this;
-            close();
-        }
-        else
-        {
-            FatalIOErrorInFunction(is)
-                << "Unexpected class name " << headerClassName()
-                << " expected " << typeName
-                << " or " << IOField<T>::typeName << nl
-                << "    while reading object " << name()
-                << exit(FatalIOError);
+            if (headerClassName() == IOField<T>::typeName)
+            {
+                is >> static_cast<Field<T>&>(*this);
+                close();
+            }
+            else if (headerClassName() == typeName)
+            {
+                is >> *this;
+                close();
+            }
+            else
+            {
+                FatalIOErrorInFunction(is)
+                    << "Unexpected class name " << headerClassName()
+                    << " expected " << typeName
+                    << " or " << IOField<T>::typeName << nl
+                    << "    while reading object " << name()
+                    << exit(FatalIOError);
+            }
         }
+
+        return true;
     }
 
-    return true;
+    return false;
 }
 
 
diff --git a/src/OpenFOAM/db/IOobjects/IOField/IOField.C b/src/OpenFOAM/db/IOobjects/IOField/IOField.C
index 89b3d2f8488..e3588f81f0c 100644
--- a/src/OpenFOAM/db/IOobjects/IOField/IOField.C
+++ b/src/OpenFOAM/db/IOobjects/IOField/IOField.C
@@ -33,32 +33,20 @@ License
 template<class Type>
 bool Foam::IOField<Type>::readIOcontents(bool readOnProc)
 {
-    if (isReadRequired())
+    if (isReadRequired() || (isReadOptional() && headerOk()))
     {
-        // Reading
-    }
-    else if (isReadOptional())
-    {
-        if (!headerOk())
+        // Do reading
+        Istream& is = readStream(typeName, readOnProc);
+
+        if (readOnProc)
         {
-            readOnProc = false;
+            is >> *this;
         }
-    }
-    else
-    {
-        return false;
+        close();
+        return true;
     }
 
-
-    // Do reading
-    Istream& is = readStream(typeName, readOnProc);
-
-    if (readOnProc)
-    {
-        is >> *this;
-    }
-    close();
-    return true;
+    return false;
 }
 
 
-- 
GitLab