From d9f3dd00ffd7af5512334965cc4a5fc8e8d8a515 Mon Sep 17 00:00:00 2001
From: Mark Olesen <Mark.Olesen@esi-group.com>
Date: Thu, 24 Aug 2023 14:35:02 +0200
Subject: [PATCH] update

---
 .../surfaceFieldValue/surfaceFieldValue.C     | 48 ++++++++++++++-----
 1 file changed, 36 insertions(+), 12 deletions(-)

diff --git a/src/functionObjects/field/fieldValues/surfaceFieldValue/surfaceFieldValue.C b/src/functionObjects/field/fieldValues/surfaceFieldValue/surfaceFieldValue.C
index adf426faa1c..bd19654cd0f 100644
--- a/src/functionObjects/field/fieldValues/surfaceFieldValue/surfaceFieldValue.C
+++ b/src/functionObjects/field/fieldValues/surfaceFieldValue/surfaceFieldValue.C
@@ -216,6 +216,8 @@ void Foam::functionObjects::fieldValues::surfaceFieldValue::setFaceZoneFaces()
         refPtr<OSstream> os;
         bool fatal = false;
 
+        ++nWarnings_;  // Always increment (even if ignore etc)
+
         switch (emptySurfaceError_)
         {
             case error::handlerTypes::IGNORE:
@@ -225,7 +227,7 @@ void Foam::functionObjects::fieldValues::surfaceFieldValue::setFaceZoneFaces()
 
             case error::handlerTypes::WARN:
             {
-                if (++nWarnings_ <= maxWarnings)
+                if (nWarnings_ <= maxWarnings)
                 {
                     os.ref(WarningInFunction);
                 }
@@ -370,10 +372,12 @@ void Foam::functionObjects::fieldValues::surfaceFieldValue::setPatchFaces()
 
     if (!nFaces_)
     {
+        // Raise warning or error
         refPtr<OSstream> os;
         bool fatal = false;
 
-        // Raise warning or error
+        ++nWarnings_;  // Always increment (even if ignore etc)
+
         switch (emptySurfaceError_)
         {
             case error::handlerTypes::IGNORE:
@@ -383,7 +387,7 @@ void Foam::functionObjects::fieldValues::surfaceFieldValue::setPatchFaces()
 
             case error::handlerTypes::WARN:
             {
-                if (++nWarnings_ <= maxWarnings)
+                if (nWarnings_ <= maxWarnings)
                 {
                     os.ref(WarningInFunction);
                 }
@@ -716,6 +720,8 @@ bool Foam::functionObjects::fieldValues::surfaceFieldValue::update()
         refPtr<OSstream> os;
         bool fatal = false;
 
+        ++nWarnings_;  // Always increment (even if ignore etc)
+
         switch (emptySurfaceError_)
         {
             case error::handlerTypes::IGNORE:
@@ -725,7 +731,7 @@ bool Foam::functionObjects::fieldValues::surfaceFieldValue::update()
 
             case error::handlerTypes::WARN:
             {
-                if (++nWarnings_ <= maxWarnings)
+                if (nWarnings_ <= maxWarnings)
                 {
                     os.ref(WarningInFunction);
                 }
@@ -765,7 +771,11 @@ bool Foam::functionObjects::fieldValues::surfaceFieldValue::update()
         << "    total area    = " << totalArea_ << nl
         << endl;
 
-    writeFileHeader(file());
+    // Emit file header on success or change of state
+    if (nWarnings_ <= 1)
+    {
+        writeFileHeader(file());
+    }
 
     return true;
 }
@@ -1307,12 +1317,6 @@ bool Foam::functionObjects::fieldValues::surfaceFieldValue::write()
 
     update();
 
-    if (!nFaces_)
-    {
-        // Encountered an error
-        return true;
-    }
-
     if (operation_ != opNone)
     {
         writeCurrentTime(file());
@@ -1320,7 +1324,15 @@ bool Foam::functionObjects::fieldValues::surfaceFieldValue::write()
 
     if (writeArea_)
     {
-        totalArea_ = totalArea();
+        // Update the area
+        if (!nFaces_)
+        {
+            totalArea_ = 0;
+        }
+        else
+        {
+            totalArea_ = totalArea();
+        }
         Log << "    total area = " << totalArea_ << endl;
 
         if (operation_ != opNone && Pstream::master())
@@ -1329,6 +1341,18 @@ bool Foam::functionObjects::fieldValues::surfaceFieldValue::write()
         }
     }
 
+    if (!nFaces_)
+    {
+        // Early exit on error
+        if (operation_ != opNone)
+        {
+            file() << endl;
+            Log << endl;
+        }
+        return true;
+    }
+
+
     // Many operations use the Sf field
     vectorField Sf;
     if (usesSf())
-- 
GitLab