From df5b009708c82130c5e94dbb7b0353750ed5ff34 Mon Sep 17 00:00:00 2001
From: Mark Olesen <Mark.Olesen@esi-group.com>
Date: Wed, 14 Jun 2017 00:53:51 +0200
Subject: [PATCH] ENH: support direct writing of fields from vtk::patchWriter

- eliminates the PtrList requirement (more flexible)
---
 .../vtk/output/foamVtkPatchWriter.C           |  35 ++--
 .../vtk/output/foamVtkPatchWriter.H           |  31 +++-
 .../vtk/output/foamVtkPatchWriterTemplates.C  | 174 ++++++++++--------
 .../vtk/output/foamVtkSurfaceMeshWriter.H     |  15 +-
 .../foamVtkSurfaceMeshWriterTemplates.C       |  52 +++---
 5 files changed, 174 insertions(+), 133 deletions(-)

diff --git a/src/conversion/vtk/output/foamVtkPatchWriter.C b/src/conversion/vtk/output/foamVtkPatchWriter.C
index ebaef3ce86f..fcbe032cfd2 100644
--- a/src/conversion/vtk/output/foamVtkPatchWriter.C
+++ b/src/conversion/vtk/output/foamVtkPatchWriter.C
@@ -61,9 +61,9 @@ void Foam::vtk::patchWriter::writePoints()
 
     format().writeSize(payLoad);
 
-    forAll(patchIDs_, i)
+    for (const label patchId : patchIDs_)
     {
-        const polyPatch& pp = patches[patchIDs_[i]];
+        const polyPatch& pp = patches[patchId];
 
         vtk::writeList(format(), pp.localPoints());
     }
@@ -84,9 +84,9 @@ void Foam::vtk::patchWriter::writePolysLegacy()
 
     // connectivity count without additional storage (done internally)
     uint64_t nConnectivity = 0;
-    forAll(patchIDs_, i)
+    for (const label patchId : patchIDs_)
     {
-        const polyPatch& pp = patches[patchIDs_[i]];
+        const polyPatch& pp = patches[patchId];
 
         forAll(pp, facei)
         {
@@ -101,9 +101,9 @@ void Foam::vtk::patchWriter::writePolysLegacy()
     // [nPts, id1, id2, ..., nPts, id1, id2, ...]
 
     label off = 0;
-    forAll(patchIDs_, i)
+    for (const label patchId : patchIDs_)
     {
-        const polyPatch& pp = patches[patchIDs_[i]];
+        const polyPatch& pp = patches[patchId];
 
         forAll(pp, facei)
         {
@@ -138,14 +138,13 @@ void Foam::vtk::patchWriter::writePolys()
     {
         // payload count
         uint64_t payLoad = 0;
-        forAll(patchIDs_, i)
+        for (const label patchId : patchIDs_)
         {
-            const polyPatch& pp = patches[patchIDs_[i]];
+            const polyPatch& pp = patches[patchId];
 
             forAll(pp, facei)
             {
-                const face& f = pp.localFaces()[facei];
-                payLoad += f.size();
+                payLoad += pp[facei].size();
             }
         }
 
@@ -156,9 +155,9 @@ void Foam::vtk::patchWriter::writePolys()
         format().writeSize(payLoad * sizeof(label));
 
         label off = 0;
-        forAll(patchIDs_, i)
+        for (const label patchId : patchIDs_)
         {
-            const polyPatch& pp = patches[patchIDs_[i]];
+            const polyPatch& pp = patches[patchId];
 
             forAll(pp, facei)
             {
@@ -191,9 +190,9 @@ void Foam::vtk::patchWriter::writePolys()
         format().writeSize(nFaces_ * sizeof(label));
 
         label off = 0;
-        forAll(patchIDs_, i)
+        for (const label patchId : patchIDs_)
         {
-            const polyPatch& pp = patches[patchIDs_[i]];
+            const polyPatch& pp = patches[patchId];
 
             forAll(pp, facei)
             {
@@ -262,9 +261,9 @@ Foam::vtk::patchWriter::patchWriter
 
     // Basic sizes
     nPoints_ = nFaces_ = 0;
-    forAll(patchIDs_, i)
+    for (const label patchId : patchIDs_)
     {
-        const polyPatch& pp = patches[patchIDs_[i]];
+        const polyPatch& pp = patches[patchId];
 
         nPoints_ += pp.nPoints();
         nFaces_  += pp.size();
@@ -384,10 +383,8 @@ void Foam::vtk::patchWriter::writePatchIDs()
 
     format().writeSize(payLoad);
 
-    forAll(patchIDs_, i)
+    for (const label patchId : patchIDs_)
     {
-        const label patchId = patchIDs_[i];
-
         const label sz = mesh_.boundaryMesh()[patchId].size();
 
         for (label facei = 0; facei < sz; ++facei)
diff --git a/src/conversion/vtk/output/foamVtkPatchWriter.H b/src/conversion/vtk/output/foamVtkPatchWriter.H
index 76b21ea436d..627b8478821 100644
--- a/src/conversion/vtk/output/foamVtkPatchWriter.H
+++ b/src/conversion/vtk/output/foamVtkPatchWriter.H
@@ -167,27 +167,38 @@ public:
         void writeFooter();
 
 
-        //- Write volFields
+      // Write fields (individually)
+
+        //- Write volume field
+        template<class Type, template<class> class PatchField>
+        void write(const GeometricField<Type, PatchField, volMesh>& field);
+
+        //- Write point fields
         template<class Type, template<class> class PatchField>
+        void write(const GeometricField<Type, PatchField, pointMesh>& field);
+
+        //- Write point-interpolated volume field
+        template<class Type>
         void write
         (
-            const UPtrList
-            <
-                const GeometricField<Type, PatchField, volMesh>
-            >& flds
+            const PrimitivePatchInterpolation<primitivePatch>& pInterp,
+            const GeometricField<Type, fvPatchField, volMesh>& field
         );
 
-        //- Write pointFields
-        template<class Type, template<class> class PatchField>
+
+      // Write fields (collectively)
+
+        //- Write multiple volume/point fields
+        template<class Type, template<class> class PatchField, class GeoMesh>
         void write
         (
             const UPtrList
             <
-                const GeometricField<Type, PatchField, pointMesh>
-            >&
+                const GeometricField<Type, PatchField, GeoMesh>
+            >& flds
         );
 
-        //- Interpolated volFields
+        //- Write multiple point-interpolated volume fields
         template<class Type>
         void write
         (
diff --git a/src/conversion/vtk/output/foamVtkPatchWriterTemplates.C b/src/conversion/vtk/output/foamVtkPatchWriterTemplates.C
index 58fbadffb61..9c77912499f 100644
--- a/src/conversion/vtk/output/foamVtkPatchWriterTemplates.C
+++ b/src/conversion/vtk/output/foamVtkPatchWriterTemplates.C
@@ -31,48 +31,43 @@ License
 template<class Type, template<class> class PatchField>
 void Foam::vtk::patchWriter::write
 (
-    const UPtrList<const GeometricField<Type, PatchField, volMesh>>& flds
+    const GeometricField<Type, PatchField, volMesh>& field
 )
 {
     const int nCmpt(pTraits<Type>::nComponents);
     const uint64_t payLoad(nFaces_ * nCmpt * sizeof(float));
 
-    forAll(flds, fieldi)
+    if (legacy_)
     {
-        const auto& fld = flds[fieldi];
+        legacy::floatField(os_, field.name(), nCmpt, nFaces_);
+    }
+    else
+    {
+        format().openDataArray<float, nCmpt>(field.name())
+            .closeTag();
+    }
 
-        if (legacy_)
+    format().writeSize(payLoad);
+
+    for (const label patchId : patchIDs_)
+    {
+        const auto& pfld = field.boundaryField()[patchId];
+
+        if (nearCellValue_)
         {
-            legacy::floatField(os_, fld.name(), nCmpt, nFaces_);
+            vtk::writeList(format(), pfld.patchInternalField()());
         }
         else
         {
-            format().openDataArray<float, nCmpt>(fld.name())
-                .closeTag();
-        }
-
-        format().writeSize(payLoad);
-
-        forAll(patchIDs_, i)
-        {
-            const auto& pfld = fld.boundaryField()[patchIDs_[i]];
-
-            if (nearCellValue_)
-            {
-                vtk::writeList(format(), pfld.patchInternalField()());
-            }
-            else
-            {
-                vtk::writeList(format(), pfld);
-            }
+            vtk::writeList(format(), pfld);
         }
+    }
 
-        format().flush();
+    format().flush();
 
-        if (!legacy_)
-        {
-            format().endDataArray();
-        }
+    if (!legacy_)
+    {
+        format().endDataArray();
     }
 }
 
@@ -80,41 +75,36 @@ void Foam::vtk::patchWriter::write
 template<class Type, template<class> class PatchField>
 void Foam::vtk::patchWriter::write
 (
-    const UPtrList<const GeometricField<Type, PatchField, pointMesh>>& flds
+    const GeometricField<Type, PatchField, pointMesh>& field
 )
 {
     const int nCmpt(pTraits<Type>::nComponents);
     const uint64_t payLoad(nPoints_ * nCmpt * sizeof(float));
 
-    forAll(flds, fieldi)
+    if (legacy_)
     {
-        const auto& fld = flds[fieldi];
-
-        if (legacy_)
-        {
-            legacy::floatField(os_, fld.name(), nCmpt, nPoints_);
-        }
-        else
-        {
-            format().openDataArray<float, nCmpt>(fld.name())
-                .closeTag();
-        }
+        legacy::floatField(os_, field.name(), nCmpt, nPoints_);
+    }
+    else
+    {
+        format().openDataArray<float, nCmpt>(field.name())
+            .closeTag();
+    }
 
-        format().writeSize(payLoad);
+    format().writeSize(payLoad);
 
-        forAll(patchIDs_, i)
-        {
-            const auto& pfld = fld.boundaryField()[patchIDs_[i]];
+    for (const label patchId : patchIDs_)
+    {
+        const auto& pfld = field.boundaryField()[patchId];
 
-            vtk::writeList(format(), pfld.patchInternalField()());
-        }
+        vtk::writeList(format(), pfld.patchInternalField()());
+    }
 
-        format().flush();
+    format().flush();
 
-        if (!legacy_)
-        {
-            format().endDataArray();
-        }
+    if (!legacy_)
+    {
+        format().endDataArray();
     }
 }
 
@@ -123,53 +113,75 @@ template<class Type>
 void Foam::vtk::patchWriter::write
 (
     const PrimitivePatchInterpolation<primitivePatch>& pInter,
-    const UPtrList<const GeometricField<Type, fvPatchField, volMesh>>& flds
+    const GeometricField<Type, fvPatchField, volMesh>& field
 )
 {
     const int nCmpt(pTraits<Type>::nComponents);
     const uint64_t payLoad(nPoints_ * nCmpt * sizeof(float));
 
-    forAll(flds, fieldi)
+    if (legacy_)
     {
-        const auto& fld = flds[fieldi];
+        legacy::floatField(os_, field.name(), nCmpt, nPoints_);
+    }
+    else
+    {
+        format().openDataArray<float, nCmpt>(field.name())
+            .closeTag();
+    }
+
+    format().writeSize(payLoad);
 
-        if (legacy_)
+    for (const label patchId : patchIDs_)
+    {
+        const auto& pfld = field.boundaryField()[patchId];
+
+        if (nearCellValue_)
         {
-            legacy::floatField(os_, fld.name(), nCmpt, nPoints_);
+            auto tfield =
+                pInter.faceToPointInterpolate(pfld.patchInternalField()());
+
+            vtk::writeList(format(), tfield());
         }
         else
         {
-            format().openDataArray<float, nCmpt>(fld.name())
-                .closeTag();
-        }
+            auto tfield = pInter.faceToPointInterpolate(pfld);
 
-        format().writeSize(payLoad);
+            vtk::writeList(format(), tfield());
+        }
+    }
 
-        forAll(patchIDs_, i)
-        {
-            const auto& pfld = fld.boundaryField()[patchIDs_[i]];
+    format().flush();
 
-            if (nearCellValue_)
-            {
-                auto tfield =
-                    pInter.faceToPointInterpolate(pfld.patchInternalField()());
+    if (!legacy_)
+    {
+        format().endDataArray();
+    }
+}
 
-                vtk::writeList(format(), tfield());
-            }
-            else
-            {
-                auto tfield = pInter.faceToPointInterpolate(pfld);
 
-                vtk::writeList(format(), tfield());
-            }
-        }
+template<class Type, template<class> class PatchField, class GeoMesh>
+void Foam::vtk::patchWriter::write
+(
+    const UPtrList<const GeometricField<Type, PatchField, GeoMesh>>& flds
+)
+{
+    for (const auto& field : flds)
+    {
+        write(field);
+    }
+}
 
-        format().flush();
 
-        if (!legacy_)
-        {
-            format().endDataArray();
-        }
+template<class Type>
+void Foam::vtk::patchWriter::write
+(
+    const PrimitivePatchInterpolation<primitivePatch>& pInter,
+    const UPtrList<const GeometricField<Type, fvPatchField, volMesh>>& flds
+)
+{
+    for (const auto& field : flds)
+    {
+        write(pInter, field);
     }
 }
 
diff --git a/src/conversion/vtk/output/foamVtkSurfaceMeshWriter.H b/src/conversion/vtk/output/foamVtkSurfaceMeshWriter.H
index 9b174a0dec3..bf5de51327b 100644
--- a/src/conversion/vtk/output/foamVtkSurfaceMeshWriter.H
+++ b/src/conversion/vtk/output/foamVtkSurfaceMeshWriter.H
@@ -135,13 +135,26 @@ public:
         void writeFooter();
 
 
-        //- Get face field
+        //- Get face field (internal face or boundary face)
         template<class Type>
         tmp<Field<Type>> getFaceField
         (
             const GeometricField<Type, fvsPatchField, surfaceMesh>& sfld
         ) const;
 
+
+      // Write fields (individually)
+
+        //- Write surface field
+        template<class Type>
+        void write
+        (
+            const GeometricField<Type, fvsPatchField, surfaceMesh>& field
+        );
+
+
+      // Write fields (collectively)
+
         //- Write surface fields
         template<class Type>
         void write
diff --git a/src/conversion/vtk/output/foamVtkSurfaceMeshWriterTemplates.C b/src/conversion/vtk/output/foamVtkSurfaceMeshWriterTemplates.C
index cdad74b7d0b..1b459b3bd96 100644
--- a/src/conversion/vtk/output/foamVtkSurfaceMeshWriterTemplates.C
+++ b/src/conversion/vtk/output/foamVtkSurfaceMeshWriterTemplates.C
@@ -62,38 +62,46 @@ Foam::vtk::surfaceMeshWriter::getFaceField
 template<class Type>
 void Foam::vtk::surfaceMeshWriter::write
 (
-    const UPtrList
-    <
-        const GeometricField<Type, fvsPatchField, surfaceMesh>
-    >& sflds
+    const GeometricField<Type, fvsPatchField, surfaceMesh>& field
 )
 {
     const int nCmpt(pTraits<Type>::nComponents);
     const uint64_t payLoad(pp_.size() * nCmpt * sizeof(float));
 
-    forAll(sflds, fieldi)
+    if (legacy_)
     {
-        const auto& fld = sflds[fieldi];
+        legacy::floatField(os(), field.name(), nCmpt, pp_.size());
+    }
+    else
+    {
+        format().openDataArray<float, nCmpt>(field.name())
+            .closeTag();
+    }
 
-        if (legacy_)
-        {
-            legacy::floatField(os(), fld.name(), nCmpt, pp_.size());
-        }
-        else
-        {
-            format().openDataArray<float, nCmpt>(fld.name())
-                .closeTag();
-        }
+    format().writeSize(payLoad);
+    vtk::writeList(format(), getFaceField(field)());
+
+    format().flush();
 
-        format().writeSize(payLoad);
-        vtk::writeList(format(), getFaceField(fld)());
+    if (!legacy_)
+    {
+        format().endDataArray();
+    }
+}
 
-        format().flush();
 
-        if (!legacy_)
-        {
-            format().endDataArray();
-        }
+template<class Type>
+void Foam::vtk::surfaceMeshWriter::write
+(
+    const UPtrList
+    <
+        const GeometricField<Type, fvsPatchField, surfaceMesh>
+    >& sflds
+)
+{
+    for (const auto& field : sflds)
+    {
+        write(field);
     }
 }
 
-- 
GitLab