From c4aebc3c8ea9a7e33f66edd4947314946ea63a7b Mon Sep 17 00:00:00 2001
From: Mark Olesen <Mark.Olesen@esi-group.com>
Date: Fri, 25 Jan 2019 12:04:23 +0100
Subject: [PATCH] ENH: avoid double write triggering in writeObjects
 functionObject

- in the previous version, a specification such as (U "U.*")
  would have selected the field name twice for writing
---
 .../utilities/writeObjects/writeObjects.C     | 42 ++++++++++++-------
 1 file changed, 27 insertions(+), 15 deletions(-)

diff --git a/src/functionObjects/utilities/writeObjects/writeObjects.C b/src/functionObjects/utilities/writeObjects/writeObjects.C
index 236fa10f988..709ac80fc69 100644
--- a/src/functionObjects/utilities/writeObjects/writeObjects.C
+++ b/src/functionObjects/utilities/writeObjects/writeObjects.C
@@ -3,7 +3,7 @@
   \\      /  F ield         | OpenFOAM: The Open Source CFD Toolbox
    \\    /   O peration     |
     \\  /    A nd           | Copyright (C) 2011-2016 OpenFOAM Foundation
-     \\/     M anipulation  | Copyright (C) 2016-2018 OpenCFD Ltd.
+     \\/     M anipulation  | Copyright (C) 2016-2019 OpenCFD Ltd.
 -------------------------------------------------------------------------------
 License
     This file is part of OpenFOAM.
@@ -26,6 +26,7 @@ License
 #include "writeObjects.H"
 #include "Time.H"
 #include "polyMesh.H"
+#include "ListOps.H"
 #include "addToRunTimeSelectionTable.H"
 
 // * * * * * * * * * * * * * * Static Data Members * * * * * * * * * * * * * //
@@ -127,25 +128,33 @@ bool Foam::functionObjects::writeObjects::write()
         obr_.time().writeTimeDict();
     }
 
-    DynamicList<word> allNames(obr_.toc().size());
-    for (const wordRe& objName : objectNames_)
-    {
-        wordList names(obr_.names<regIOobject>(objName));
+    // Get selection
+    const wordList selectedNames(obr_.sortedNames<regIOobject>(objectNames_));
 
-        if (names.size())
-        {
-            allNames.append(std::move(names));
-        }
-        else
+    // Warning if anything was missed
+    bitSet missed(objectNames_.size());
+
+    label index = 0;
+    for (const wordRe& select : objectNames_)
+    {
+        if (!ListOps::found(selectedNames, select))
         {
-            WarningInFunction
-                << "Object " << objName << " not found in "
-                << "database. Available objects:" << nl << obr_.sortedToc()
-                << endl;
+            missed.set(index);
         }
+        ++index;
     }
 
-    for (const word& objName : allNames)
+    if (missed.any())
+    {
+        WarningInFunction
+            << "No corresponding selection for "
+            << flatOutput(subset(missed, objectNames_)) << nl
+            << "Available objects in database:"
+            << nl << obr_.sortedToc()
+            << endl;
+    }
+
+    for (const word& objName : selectedNames)
     {
         regIOobject& obj = obr_.lookupObjectRef<regIOobject>(objName);
 
@@ -181,6 +190,9 @@ bool Foam::functionObjects::writeObjects::write()
                     << ". Valid writeOption types are "
                     << writeOptionNames_
                     << exit(FatalError);
+
+                continue;
+                break;
             }
         }
 
-- 
GitLab