From 79fafda22cee7fd2c4148d9bdd952782232c4faf Mon Sep 17 00:00:00 2001
From: Andrew Heather <>
Date: Thu, 11 Nov 2021 12:07:25 +0000
Subject: [PATCH] ENH: Cloud - added function to read cloud fields from disk to
 obr

---
 src/lagrangian/basic/Cloud/Cloud.H   | 18 ++++++-
 src/lagrangian/basic/Cloud/CloudIO.C | 77 +++++++++++++++++++++++++++-
 2 files changed, 93 insertions(+), 2 deletions(-)

diff --git a/src/lagrangian/basic/Cloud/Cloud.H b/src/lagrangian/basic/Cloud/Cloud.H
index 2fdb8fecf41..06b5d9a9269 100644
--- a/src/lagrangian/basic/Cloud/Cloud.H
+++ b/src/lagrangian/basic/Cloud/Cloud.H
@@ -6,7 +6,7 @@
      \\/     M anipulation  |
 -------------------------------------------------------------------------------
     Copyright (C) 2011-2017 OpenFOAM Foundation
-    Copyright (C) 2017-2020 OpenCFD Ltd.
+    Copyright (C) 2017-2021 OpenCFD Ltd.
 -------------------------------------------------------------------------------
 License
     This file is part of OpenFOAM.
@@ -45,6 +45,7 @@ SourceFiles
 #include "CompactIOField.H"
 #include "polyMesh.H"
 #include "bitSet.H"
+#include "wordRes.H"
 
 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
 
@@ -240,6 +241,21 @@ public:
                 const CompactIOField<Field<DataType>, DataType>& data
             ) const;
 
+            //- Helper function to store a cloud field on its registry
+            template<class Type>
+            bool readStoreFile
+            (
+                const IOobject& io,
+                const IOobject& ioNew
+            ) const;
+
+            //- Read from files into objectRegistry
+            void readFromFiles
+            (
+                objectRegistry& obr,
+                const wordRes& selectFields
+            ) const;
+
 
         // Write
 
diff --git a/src/lagrangian/basic/Cloud/CloudIO.C b/src/lagrangian/basic/Cloud/CloudIO.C
index 858565aff7b..5a34a2891ee 100644
--- a/src/lagrangian/basic/Cloud/CloudIO.C
+++ b/src/lagrangian/basic/Cloud/CloudIO.C
@@ -6,7 +6,7 @@
      \\/     M anipulation  |
 -------------------------------------------------------------------------------
     Copyright (C) 2011-2017, 2020 OpenFOAM Foundation
-    Copyright (C) 2017-2020 OpenCFD Ltd.
+    Copyright (C) 2017-2021 OpenCFD Ltd.
 -------------------------------------------------------------------------------
 License
     This file is part of OpenFOAM.
@@ -30,6 +30,7 @@ License
 #include "Time.H"
 #include "IOPosition.H"
 #include "IOdictionary.H"
+#include "IOobjectList.H"
 
 // * * * * * * * * * * * * * * Static Data Members * * * * * * * * * * * * * //
 
@@ -243,6 +244,80 @@ void Foam::Cloud<ParticleType>::checkFieldFieldIOobject
 }
 
 
+template<class ParticleType>
+template<class Type>
+bool Foam::Cloud<ParticleType>::readStoreFile
+(
+    const IOobject& io,
+    const IOobject& ioNew
+) const
+{
+    if (io.headerClassName() == IOField<Type>::typeName)
+    {
+        IOField<Type> fld(io);
+        auto* fldNewPtr = new IOField<Type>(ioNew, std::move(fld));
+        return fldNewPtr->store();
+    }
+
+    return false;
+}
+
+
+template<class ParticleType>
+void Foam::Cloud<ParticleType>::readFromFiles
+(
+    objectRegistry& obr,
+    const wordRes& selectFields
+) const
+{
+    IOobjectList cloudObjects
+    (
+        *this,
+        time().timeName(),
+        "",
+        IOobject::MUST_READ,
+        IOobject::NO_WRITE,
+        false
+    );
+
+    forAllIters(cloudObjects, iter)
+    {
+        if (selectFields.size() && !selectFields.match(iter()->name()))
+        {
+            continue;
+        }
+
+        IOobject ioNew
+        (
+            iter()->name(),
+            time().timeName(),
+            obr,
+            IOobject::NO_READ,
+            IOobject::NO_WRITE
+        );
+
+        auto& object = *iter();
+
+        const bool stored
+        (
+            readStoreFile<label>(object, ioNew)
+         || readStoreFile<scalar>(object, ioNew)
+         || readStoreFile<vector>(object, ioNew)
+         || readStoreFile<sphericalTensor>(object, ioNew)
+         || readStoreFile<symmTensor>(object, ioNew)
+         || readStoreFile<tensor>(object, ioNew)
+        );
+
+        if (!stored)
+        {
+            DebugInfo
+                << "Unhandled field type " << iter()->headerClassName()
+                << endl;
+        }
+    }
+}
+
+
 template<class ParticleType>
 void Foam::Cloud<ParticleType>::writeFields() const
 {
-- 
GitLab