From 8f16527a512c15159f195d9495974e60b69076ed Mon Sep 17 00:00:00 2001
From: Kutalmis Bercin <kutalmis.bercin@esi-group.com>
Date: Fri, 25 Jun 2021 18:01:28 +0100
Subject: [PATCH] BUG: turbulenceFields: unset duplicate already-registered
 fields

---
 .../field/turbulenceFields/turbulenceFields.C | 29 +++++++++++++++++++
 .../field/turbulenceFields/turbulenceFields.H |  6 ++++
 .../turbulenceFieldsTemplates.C               |  7 -----
 3 files changed, 35 insertions(+), 7 deletions(-)

diff --git a/src/functionObjects/field/turbulenceFields/turbulenceFields.C b/src/functionObjects/field/turbulenceFields/turbulenceFields.C
index 9eb8d494ec1..c445161ddbf 100644
--- a/src/functionObjects/field/turbulenceFields/turbulenceFields.C
+++ b/src/functionObjects/field/turbulenceFields/turbulenceFields.C
@@ -90,6 +90,27 @@ const Foam::word Foam::functionObjects::turbulenceFields::modelName_
 
 // * * * * * * * * * * * * Protected Member Functions  * * * * * * * * * * * //
 
+void Foam::functionObjects::turbulenceFields::initialise()
+{
+    for (const word& f : fieldSet_)
+    {
+        const word scopedName(modelName_ + ':' + f);
+
+        if (obr_.found(scopedName))
+        {
+            WarningInFunction
+                << "Cannot store turbulence field " << scopedName
+                << " since an object with that name already exists"
+                << nl << endl;
+
+            fieldSet_.unset(f);
+        }
+    }
+
+    initialised_ = true;
+}
+
+
 bool Foam::functionObjects::turbulenceFields::compressible()
 {
     if (obr_.foundObject<compressible::turbulenceModel>(modelName_))
@@ -119,6 +140,7 @@ Foam::functionObjects::turbulenceFields::turbulenceFields
 )
 :
     fvMeshFunctionObject(name, runTime, dict),
+    initialised_(false),
     fieldSet_()
 {
     read(dict);
@@ -155,6 +177,8 @@ bool Foam::functionObjects::turbulenceFields::read(const dictionary& dict)
             Info<< "no fields requested to be stored" << nl << endl;
         }
 
+        initialised_ = false;
+
         return true;
     }
 
@@ -164,6 +188,11 @@ bool Foam::functionObjects::turbulenceFields::read(const dictionary& dict)
 
 bool Foam::functionObjects::turbulenceFields::execute()
 {
+    if (!initialised_)
+    {
+        initialise();
+    }
+
     const bool comp = compressible();
 
     if (comp)
diff --git a/src/functionObjects/field/turbulenceFields/turbulenceFields.H b/src/functionObjects/field/turbulenceFields/turbulenceFields.H
index 98cdacbfdd9..f796e1e753e 100644
--- a/src/functionObjects/field/turbulenceFields/turbulenceFields.H
+++ b/src/functionObjects/field/turbulenceFields/turbulenceFields.H
@@ -210,12 +210,18 @@ protected:
 
     // Protected Data
 
+        //- Flag to track initialisation
+        bool initialised_;
+
         //- Fields to load
         wordHashSet fieldSet_;
 
 
     // Protected Member Functions
 
+        //- Unset duplicate fields already registered by other function objects
+        void initialise();
+
         //- Return true if compressible turbulence model is identified
         bool compressible();
 
diff --git a/src/functionObjects/field/turbulenceFields/turbulenceFieldsTemplates.C b/src/functionObjects/field/turbulenceFields/turbulenceFieldsTemplates.C
index 90d0630e1d3..45df782cd4b 100644
--- a/src/functionObjects/field/turbulenceFields/turbulenceFieldsTemplates.C
+++ b/src/functionObjects/field/turbulenceFields/turbulenceFieldsTemplates.C
@@ -47,13 +47,6 @@ void Foam::functionObjects::turbulenceFields::processField
     {
         (*fldPtr) == tvalue();
     }
-    else if (obr_.found(scopedName))
-    {
-        WarningInFunction
-            << "Cannot store turbulence field " << scopedName
-            << " since an object with that name already exists"
-            << nl << endl;
-    }
     else
     {
         obr_.store
-- 
GitLab