From c9cf232a02411dcb43ee1ed126e782c8760b4e0f Mon Sep 17 00:00:00 2001
From: Mark Olesen <Mark.Olesen@Germany>
Date: Tue, 27 Sep 2016 10:08:07 +0200
Subject: [PATCH] ENH: add some convenience methods in ensightFile,
 ensightGeoFile

- Some commonly used write methods that are independent of
  the calling context (ie, 2D/3D data, geometry, fields)

- Provide singleton null() for ensightFile, ensightGeoFile.
  Can be used for MPI slaves that need a file reference for their
  methods, but will never write to it, but it is also reasonable
  to use an autoPtr with rawRef() for that as well.
---
 src/conversion/ensight/part/ensightPart.H     | 14 +---
 src/conversion/ensight/part/ensightPartIO.C   | 76 +------------------
 .../ensight/part/ensightPartTemplates.C       |  8 +-
 src/fileFormats/ensight/file/ensightFile.C    | 73 ++++++++++++++++++
 src/fileFormats/ensight/file/ensightFile.H    | 33 +++++++-
 src/fileFormats/ensight/file/ensightGeoFile.C | 50 ++++++++++--
 src/fileFormats/ensight/file/ensightGeoFile.H | 21 +++++
 .../ensight/name/ensightFileName.H            | 15 ++--
 src/fileFormats/ensight/name/ensightVarName.H | 13 ++--
 9 files changed, 188 insertions(+), 115 deletions(-)

diff --git a/src/conversion/ensight/part/ensightPart.H b/src/conversion/ensight/part/ensightPart.H
index 170b0e763d2..80e31935ef4 100644
--- a/src/conversion/ensight/part/ensightPart.H
+++ b/src/conversion/ensight/part/ensightPart.H
@@ -3,7 +3,7 @@
   \\      /  F ield         | OpenFOAM: The Open Source CFD Toolbox
    \\    /   O peration     |
     \\  /    A nd           | Copyright (C) 2011-2016 OpenFOAM Foundation
-     \\/     M anipulation  |
+     \\/     M anipulation  | Copyright (C) 2016 OpenCFD Ltd.
 -------------------------------------------------------------------------------
 License
     This file is part of OpenFOAM.
@@ -138,18 +138,6 @@ protected:
         //- Check for fully defined fields
         bool isFieldDefined(const List<scalar>&) const;
 
-        //- Write the part header
-        void writeHeader(ensightFile&, bool withDescription=false) const;
-
-        //- Write a scalar field for idList
-        //  A null reference for idList writes the perNode values
-        void writeFieldList
-        (
-            ensightFile& os,
-            const List<scalar>& field,
-            const labelUList& idList
-        ) const;
-
         //- Track points used
         virtual localPoints calcLocalPoints() const
         {
diff --git a/src/conversion/ensight/part/ensightPartIO.C b/src/conversion/ensight/part/ensightPartIO.C
index 4e21d6dd62b..68cc8d41963 100644
--- a/src/conversion/ensight/part/ensightPartIO.C
+++ b/src/conversion/ensight/part/ensightPartIO.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  |
+     \\/     M anipulation  | Copyright (C) 2016 OpenCFD Ltd.
 -------------------------------------------------------------------------------
 License
     This file is part of OpenFOAM.
@@ -30,72 +30,6 @@ Description
 #include "dictionary.H"
 #include "IOstreams.H"
 
-// * * * * * * * * * * * * * Private Member Functions  * * * * * * * * * * * //
-
-void Foam::ensightPart::writeHeader
-(
-    ensightFile& os,
-    bool withDescription
-) const
-{
-    os.write("part");
-    os.newline();
-
-    os.write(number() + 1);   // Ensight starts with 1
-    os.newline();
-
-    if (withDescription)
-    {
-        os.write(name());
-        os.newline();
-    }
-}
-
-
-void Foam::ensightPart::writeFieldList
-(
-    ensightFile& os,
-    const List<scalar>& field,
-    const labelUList& idList
-) const
-{
-    if (notNull(idList))
-    {
-        forAll(idList, i)
-        {
-            if (idList[i] >= field.size() || std::isnan(field[idList[i]]))
-            {
-                os.writeUndef();
-            }
-            else
-            {
-                os.write(field[idList[i]]);
-            }
-
-            os.newline();
-        }
-    }
-    else
-    {
-        // no idList => perNode
-        forAll(field, i)
-        {
-            if (std::isnan(field[i]))
-            {
-                os.writeUndef();
-            }
-            else
-            {
-                os.write(field[i]);
-            }
-
-            os.newline();
-        }
-    }
-}
-
-
-
 // * * * * * * * * * * * * * * * Member Functions  * * * * * * * * * * * * * //
 
 void Foam::ensightPart::reconstruct(Istream& is)
@@ -176,12 +110,8 @@ void Foam::ensightPart::writeGeometry
         const localPoints ptList = calcLocalPoints();
         const labelUList& pointMap = ptList.list;
 
-        writeHeader(os, true);
-
-        // write points
-        os.writeKeyword("coordinates");
-        os.write(ptList.nPoints);
-        os.newline();
+        os.beginPart(number(), name());
+        os.beginCoordinates(ptList.nPoints);
 
         for (direction cmpt=0; cmpt < point::nComponents; ++cmpt)
         {
diff --git a/src/conversion/ensight/part/ensightPartTemplates.C b/src/conversion/ensight/part/ensightPartTemplates.C
index 13186460402..168251435c0 100644
--- a/src/conversion/ensight/part/ensightPartTemplates.C
+++ b/src/conversion/ensight/part/ensightPartTemplates.C
@@ -41,7 +41,7 @@ void Foam::ensightPart::writeField
 {
     if (this->size() && field.size())
     {
-        writeHeader(os);
+        os.beginPart(number());
 
         if (perNode)
         {
@@ -49,7 +49,8 @@ void Foam::ensightPart::writeField
             for (direction d=0; d < pTraits<Type>::nComponents; ++d)
             {
                 label cmpt = ensightPTraits<Type>::componentOrder[d];
-                writeFieldList(os, field.component(cmpt), labelUList::null());
+
+                os.writeList(field.component(cmpt));
             }
         }
         else
@@ -65,7 +66,8 @@ void Foam::ensightPart::writeField
                     for (direction d=0; d < pTraits<Type>::nComponents; ++d)
                     {
                         label cmpt = ensightPTraits<Type>::componentOrder[d];
-                        writeFieldList(os, field.component(cmpt), idList);
+
+                        os.writeList(field.component(cmpt), idList);
                     }
                 }
             }
diff --git a/src/fileFormats/ensight/file/ensightFile.C b/src/fileFormats/ensight/file/ensightFile.C
index 05003b2a002..b106c6ecf4c 100644
--- a/src/fileFormats/ensight/file/ensightFile.C
+++ b/src/fileFormats/ensight/file/ensightFile.C
@@ -25,6 +25,7 @@ License
 
 #include "ensightFile.H"
 #include "error.H"
+#include "UList.H"
 
 #include <cstring>
 #include <sstream>
@@ -307,4 +308,76 @@ Foam::Ostream& Foam::ensightFile::writeBinaryHeader()
 }
 
 
+//
+// Convenience Output Methods
+//
+
+void Foam::ensightFile::beginPart(const label index)
+{
+    write("part");
+    newline();
+    write(index+1); // Ensight starts with 1
+    newline();
+}
+
+
+void Foam::ensightFile::beginParticleCoordinates(const label nparticles)
+{
+    write("particle coordinates");
+    newline();
+    write(nparticles, 8); // unusual width
+    newline();
+}
+
+
+void Foam::ensightFile::writeList
+(
+    const UList<scalar>& field
+)
+{
+    forAll(field, i)
+    {
+        if (std::isnan(field[i]))
+        {
+            writeUndef();
+        }
+        else
+        {
+            write(field[i]);
+        }
+
+        newline();
+    }
+}
+
+
+void Foam::ensightFile::writeList
+(
+    const UList<scalar>& field,
+    const labelUList& idList
+)
+{
+    if (notNull(idList))
+    {
+        forAll(idList, i)
+        {
+            if (idList[i] >= field.size() || std::isnan(field[idList[i]]))
+            {
+                writeUndef();
+            }
+            else
+            {
+                write(field[idList[i]]);
+            }
+
+            newline();
+        }
+    }
+    else
+    {
+        // no idList => perNode
+        writeList(field);
+    }
+}
+
 // ************************************************************************* //
diff --git a/src/fileFormats/ensight/file/ensightFile.H b/src/fileFormats/ensight/file/ensightFile.H
index 03cbdcd7032..78b09904e10 100644
--- a/src/fileFormats/ensight/file/ensightFile.H
+++ b/src/fileFormats/ensight/file/ensightFile.H
@@ -38,6 +38,7 @@ Description
 
 #include "ensightFileName.H"
 #include "ensightVarName.H"
+#include "UList.H"
 
 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
 
@@ -80,9 +81,13 @@ class ensightFile
 
 public:
 
-    // Forward declarations
-    class FileName;
-    class VarName;
+    // Static Member Functions
+
+        //- Return a null ensightFile
+        inline static const ensightFile& null()
+        {
+            return NullObjectRef<ensightFile>();
+        }
 
 
     // Constructors
@@ -172,6 +177,28 @@ public:
 
         //- Add carriage return to ascii stream
         void newline();
+
+
+    // Convenience Output Methods
+
+        //- Begin a part (0-based index).
+        void beginPart(const label index);
+
+        //- Begin a "particle coordinates" block (measured data)
+        void beginParticleCoordinates(const label nparticles);
+
+        //- Write a list of floats as "%12.5e" or as binary
+        //  With carriage return after each value (ascii stream)
+        void writeList(const UList<scalar>& field);
+
+        //- Write an indirect list of scalars as "%12.5e" or as binary
+        //  With carriage return after each value (ascii stream)
+        void writeList
+        (
+            const UList<scalar>& field,
+            const labelUList& idList
+        );
+
 };
 
 
diff --git a/src/fileFormats/ensight/file/ensightGeoFile.C b/src/fileFormats/ensight/file/ensightGeoFile.C
index 0119087e70f..27ce98282f7 100644
--- a/src/fileFormats/ensight/file/ensightGeoFile.C
+++ b/src/fileFormats/ensight/file/ensightGeoFile.C
@@ -35,17 +35,25 @@ License
 
 void Foam::ensightGeoFile::initialize()
 {
+    writeBinaryHeader();
+
+    // Description line 1
+    write("Ensight Geometry File");
+    newline();
+
+    // Description line 2
     #ifdef OPENFOAM_PLUS
-    string desc2("Written by OpenFOAM+ " STRING_QUOTE(OPENFOAM_PLUS));
+    write(string("Written by OpenFOAM+ " STRING_QUOTE(OPENFOAM_PLUS)));
     #else
-    string desc2("Written by OpenFOAM-" + string(Foam::FOAMversion));
+    write(string("Written by OpenFOAM-" + string(Foam::FOAMversion)));
     #endif
 
-    writeBinaryHeader();
-    write("Ensight Geometry File");  newline(); // description line 1
-    write(desc2);                    newline(); // description line 2
-    write("node id assign");         newline();
-    write("element id assign");      newline();
+    newline();
+    write("node id assign");
+    newline();
+
+    write("element id assign");
+    newline();
 }
 
 
@@ -87,10 +95,36 @@ Foam::ensightGeoFile::~ensightGeoFile()
 Foam::Ostream& Foam::ensightGeoFile::writeKeyword(const keyType& key)
 {
     // ensure we get ensightFile::write(const string&)
-    write(static_cast<const string&>(key)); newline();
+    write(static_cast<const string&>(key));
+    newline();
 
     return *this;
 }
 
 
+//
+// Convenience Output Methods
+//
+
+void Foam::ensightGeoFile::beginPart
+(
+    const label index,
+    const string& description
+)
+{
+    beginPart(index);
+    write(description);
+    newline();
+}
+
+
+void Foam::ensightGeoFile::beginCoordinates(const label npoints)
+{
+    write("coordinates");
+    newline();
+    write(npoints);
+    newline();
+}
+
+
 // ************************************************************************* //
diff --git a/src/fileFormats/ensight/file/ensightGeoFile.H b/src/fileFormats/ensight/file/ensightGeoFile.H
index 75cef26cd98..6b60c6e4195 100644
--- a/src/fileFormats/ensight/file/ensightGeoFile.H
+++ b/src/fileFormats/ensight/file/ensightGeoFile.H
@@ -61,6 +61,15 @@ class ensightGeoFile
 
 public:
 
+    // Static Member Functions
+
+        //- Return a null ensightGeoFile
+        inline static const ensightGeoFile& null()
+        {
+            return NullObjectRef<ensightGeoFile>();
+        }
+
+
     // Constructors
 
         //- Construct from pathname.
@@ -89,6 +98,18 @@ public:
 
         //- Write keyword with trailing newline
         virtual Ostream& writeKeyword(const keyType& key);
+
+
+    // Convenience Output Methods
+
+        using ensightFile::beginPart;
+
+        //- Begin a "part" (0-based index), with a description.
+        void beginPart(const label index, const string& description);
+
+        //- Begin a "coordinates" block
+        void beginCoordinates(const label npoints);
+
 };
 
 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
diff --git a/src/fileFormats/ensight/name/ensightFileName.H b/src/fileFormats/ensight/name/ensightFileName.H
index 550a86f68c4..1452730107a 100644
--- a/src/fileFormats/ensight/name/ensightFileName.H
+++ b/src/fileFormats/ensight/name/ensightFileName.H
@@ -28,7 +28,7 @@ Description
     Specification of a valid Ensight file-name.
 
     Spaces must be quoted,
-    no '*' wildcards, not '%' (structured block continuation).
+    no '*' wildcards, no '%' (structured block continuation).
 
     Overall line length within case file is limited to 1024, but this is not
     yet addresssed.
@@ -80,15 +80,16 @@ public:
         //- Is this character valid for an ensight file-name
         inline static bool valid(char);
 
+
     // Member operators
 
-        // Assignment
+    // Assignment (disabled)
 
-            void operator=(const fileName&) = delete;
-            void operator=(const word&) = delete;
-            void operator=(const string&) = delete;
-            void operator=(const std::string&) = delete;
-            void operator=(const char*) = delete;
+        void operator=(const fileName&) = delete;
+        void operator=(const word&) = delete;
+        void operator=(const string&) = delete;
+        void operator=(const std::string&) = delete;
+        void operator=(const char*) = delete;
 
 };
 
diff --git a/src/fileFormats/ensight/name/ensightVarName.H b/src/fileFormats/ensight/name/ensightVarName.H
index fe2c57aefbf..2ffff6ae193 100644
--- a/src/fileFormats/ensight/name/ensightVarName.H
+++ b/src/fileFormats/ensight/name/ensightVarName.H
@@ -63,10 +63,8 @@ class VarName
         //- Strip invalid characters
         inline void stripInvalid();
 
-
 public:
 
-
     // Constructors
 
         //- Construct as copy
@@ -87,12 +85,11 @@ public:
 
     // Member operators
 
-        // Assignment
-
-            void operator=(const word&) = delete;
-            void operator=(const string&) = delete;
-            void operator=(const std::string&) = delete;
-            void operator=(const char*) = delete;
+    // Assignment (disabled)
+        void operator=(const word&) = delete;
+        void operator=(const string&) = delete;
+        void operator=(const std::string&) = delete;
+        void operator=(const char*) = delete;
 
 };
 
-- 
GitLab