From 784461b2fa267c2f783198572062026322fdeefd Mon Sep 17 00:00:00 2001
From: Mark Olesen <Mark.Olesen@Germany>
Date: Mon, 21 Nov 2016 14:28:40 +0100
Subject: [PATCH] ENH: make search of instances in IOobject::typeHeaderOk
 optional (issue #245)

- in specific cases it can be useful to suppress searching the instances.
  For example, if one only wishes to check if a "points" is available at
  the given time instance, without searching backwards through all
  times.
---
 .../dataConversion/foamToEnsight/checkData.H  |  6 ++-
 .../foamToEnsight/checkMeshMoving.H           |  5 +--
 .../foamToEnsightParts/checkMeshMoving.H      |  7 +---
 .../foamToEnsightParts/getTimeIndex.H         |  4 +-
 .../foamToEnsightParts/moveMesh.H             |  7 +++-
 .../dataConversion/foamToGMV/moveMesh.H       | 37 ++++++++-----------
 src/OpenFOAM/db/IOobject/IOobject.H           | 25 +++++++++----
 src/OpenFOAM/db/IOobject/IOobjectTemplates.C  |  8 +++-
 src/OpenFOAM/db/IOobjects/IOMap/IOMapName.C   |  8 ++--
 9 files changed, 57 insertions(+), 50 deletions(-)

diff --git a/applications/utilities/postProcessing/dataConversion/foamToEnsight/checkData.H b/applications/utilities/postProcessing/dataConversion/foamToEnsight/checkData.H
index 50e78e781c8..73a0e174171 100644
--- a/applications/utilities/postProcessing/dataConversion/foamToEnsight/checkData.H
+++ b/applications/utilities/postProcessing/dataConversion/foamToEnsight/checkData.H
@@ -18,8 +18,10 @@ if (!fieldsToUse.found(fieldName))
                 fieldName,
                 timeDirs[n1].name(),
                 mesh,
-                IOobject::NO_READ
-            ).typeHeaderOk<volScalarField>(false)
+                IOobject::NO_READ,
+                IOobject::NO_WRITE,
+                false  // no register
+            ).typeHeaderOk<volScalarField>(false, false)
         );
 
         if (variableGood)
diff --git a/applications/utilities/postProcessing/dataConversion/foamToEnsight/checkMeshMoving.H b/applications/utilities/postProcessing/dataConversion/foamToEnsight/checkMeshMoving.H
index 912f48b28ac..9c0c612ed7f 100644
--- a/applications/utilities/postProcessing/dataConversion/foamToEnsight/checkMeshMoving.H
+++ b/applications/utilities/postProcessing/dataConversion/foamToEnsight/checkMeshMoving.H
@@ -7,8 +7,6 @@ if (timeDirs.size() > 1 && Pstream::master())
     // We already loaded a mesh (usually from constant).
     // See if any other "polyMesh/points" files exist too.
 
-    const fileName& baseDir = mesh.time().path();
-
     Info<< "Search for moving mesh ... " << flush;
     forAll(timeDirs, timeI)
     {
@@ -17,7 +15,6 @@ if (timeDirs.size() > 1 && Pstream::master())
         meshMoving =
         (
             timeName != mesh.pointsInstance()
-         && isDir(baseDir/timeName/polyMesh::meshSubDir)
          && IOobject
             (
                 "points",
@@ -27,7 +24,7 @@ if (timeDirs.size() > 1 && Pstream::master())
                 IOobject::NO_READ,
                 IOobject::NO_WRITE,
                 false  // no register
-            ).typeHeaderOk<pointIOField>(true)
+            ).typeHeaderOk<pointIOField>(true, false)
         );
 
         if (meshMoving)
diff --git a/applications/utilities/postProcessing/dataConversion/foamToEnsightParts/checkMeshMoving.H b/applications/utilities/postProcessing/dataConversion/foamToEnsightParts/checkMeshMoving.H
index 51c7a11e733..8155d920cbc 100644
--- a/applications/utilities/postProcessing/dataConversion/foamToEnsightParts/checkMeshMoving.H
+++ b/applications/utilities/postProcessing/dataConversion/foamToEnsightParts/checkMeshMoving.H
@@ -8,15 +8,12 @@ if (timeDirs.size() > 1 && Pstream::master())
     // We already loaded a mesh (usually from constant).
     // See if any other "polyMesh/points" files exist too.
 
-    const fileName& baseDir = mesh.time().path();
-
     Info<< "Search for moving mesh ... " << flush;
     forAll(timeDirs, timeI)
     {
         meshMoving =
         (
-            isDir(baseDir/timeDirs[timeI].name()/polyMesh::meshSubDir)
-         && IOobject
+            IOobject
             (
                 "points",
                 timeDirs[timeI].name(),
@@ -25,7 +22,7 @@ if (timeDirs.size() > 1 && Pstream::master())
                 IOobject::NO_READ,
                 IOobject::NO_WRITE,
                 false  // no register
-            ).typeHeaderOk<pointIOField>(true)
+            ).typeHeaderOk<pointIOField>(true, false)
         );
 
         if (meshMoving)
diff --git a/applications/utilities/postProcessing/dataConversion/foamToEnsightParts/getTimeIndex.H b/applications/utilities/postProcessing/dataConversion/foamToEnsightParts/getTimeIndex.H
index a55e44e1364..91c718994f1 100644
--- a/applications/utilities/postProcessing/dataConversion/foamToEnsightParts/getTimeIndex.H
+++ b/applications/utilities/postProcessing/dataConversion/foamToEnsightParts/getTimeIndex.H
@@ -21,10 +21,10 @@
             runTime,
             IOobject::READ_IF_PRESENT,
             IOobject::NO_WRITE,
-            false
+            false  // no register
         );
 
-        if (io.typeHeaderOk<IOdictionary>(true))
+        if (io.typeHeaderOk<IOdictionary>(true, false))
         {
             io.readOpt() = IOobject::MUST_READ_IF_MODIFIED;
             IOdictionary timeObject(io);
diff --git a/applications/utilities/postProcessing/dataConversion/foamToEnsightParts/moveMesh.H b/applications/utilities/postProcessing/dataConversion/foamToEnsightParts/moveMesh.H
index f894e2c136a..8e3f53ce966 100644
--- a/applications/utilities/postProcessing/dataConversion/foamToEnsightParts/moveMesh.H
+++ b/applications/utilities/postProcessing/dataConversion/foamToEnsightParts/moveMesh.H
@@ -4,10 +4,13 @@
         "points",
         runTime.timeName(),
         polyMesh::meshSubDir,
-        mesh
+        mesh,
+        IOobject::NO_READ,
+        IOobject::NO_WRITE,
+        false  // no register
     );
 
-    if (io.typeHeaderOk<pointIOField>(true))
+    if (io.typeHeaderOk<pointIOField>(true, false))
     {
         // Read new points
         io.readOpt() = IOobject::MUST_READ;
diff --git a/applications/utilities/postProcessing/dataConversion/foamToGMV/moveMesh.H b/applications/utilities/postProcessing/dataConversion/foamToGMV/moveMesh.H
index c1a1f8dee71..50db68176ce 100644
--- a/applications/utilities/postProcessing/dataConversion/foamToGMV/moveMesh.H
+++ b/applications/utilities/postProcessing/dataConversion/foamToGMV/moveMesh.H
@@ -1,27 +1,20 @@
-IOobject ioPoints
-(
-    "points",
-    runTime.timeName(),
-    mesh.name(),
-    mesh
-);
-
-if (ioPoints.typeHeaderOk<pointIOField>(true))
 {
-    Info<< "new points available" << endl;
-    // Reading new points
-    pointIOField newPoints
+    IOobject io
     (
-        IOobject
-        (
-            "points",
-            runTime.timeName(),
-            mesh.name(),
-            mesh,
-            IOobject::MUST_READ,
-            IOobject::NO_WRITE
-        )
+        "points",
+        runTime.timeName(),
+        mesh.name(),
+        mesh,
+        IOobject::NO_READ,
+        IOobject::NO_WRITE,
+        false  // no register
     );
 
-    mesh.movePoints(newPoints);
+    if (io.typeHeaderOk<pointIOField>(true, false))
+    {
+        Info<< "new points available" << endl;
+        // Read new points
+        io.readOpt() = IOobject::MUST_READ;
+        mesh.movePoints(pointIOField(io));
+    }
 }
diff --git a/src/OpenFOAM/db/IOobject/IOobject.H b/src/OpenFOAM/db/IOobject/IOobject.H
index 50f4f56627f..b08b3bb6617 100644
--- a/src/OpenFOAM/db/IOobject/IOobject.H
+++ b/src/OpenFOAM/db/IOobject/IOobject.H
@@ -425,10 +425,16 @@ public:
             //- Read header
             bool readHeader(Istream&);
 
-            //- Read header (uses typeFilePath to find file) and check header
-            //  info. Optionally checks headerClassName against type
+            //- Read header (uses typeFilePath to find file) and check its info.
+            //  Optionally checks headerClassName against the type-name.
+            //  When search is false, simply use the current instance,
+            //  otherwise search previous instances.
             template<class Type>
-            bool typeHeaderOk(const bool checkType = true);
+            bool typeHeaderOk
+            (
+                const bool checkType = true,
+                const bool search = true
+            );
 
             //- Helper: warn that type does not support re-reading
             template<class Type>
@@ -489,18 +495,23 @@ template<>
 Ostream& operator<<(Ostream& os, const InfoProxy<IOobject>& ip);
 
 
-//- Template function for obtaining global status
+//- Template function for obtaining global vs. local status
 template<class T>
 inline bool typeGlobal()
 {
     return false;
 }
 
-//- Template function for obtaining filePath
+//- Template function for obtaining local or global filePath
 template<class T>
-inline fileName typeFilePath(const IOobject& io)
+inline fileName typeFilePath(const IOobject& io, const bool search=true)
 {
-    return (typeGlobal<T>() ? io.globalFilePath() : io.localFilePath());
+    return
+    (
+        typeGlobal<T>()
+      ? io.globalFilePath(search)
+      : io.localFilePath(search)
+    );
 }
 
 
diff --git a/src/OpenFOAM/db/IOobject/IOobjectTemplates.C b/src/OpenFOAM/db/IOobject/IOobjectTemplates.C
index 1ef06e3415e..f93384bf8cc 100644
--- a/src/OpenFOAM/db/IOobject/IOobjectTemplates.C
+++ b/src/OpenFOAM/db/IOobject/IOobjectTemplates.C
@@ -32,7 +32,11 @@ License
 // * * * * * * * * * * * * * * * Member Functions  * * * * * * * * * * * * * //
 
 template<class Type>
-bool Foam::IOobject::typeHeaderOk(const bool checkType)
+bool Foam::IOobject::typeHeaderOk
+(
+    const bool checkType,
+    const bool search
+)
 {
     bool ok = true;
 
@@ -48,7 +52,7 @@ bool Foam::IOobject::typeHeaderOk(const bool checkType)
     // Determine local status
     if (!masterOnly || Pstream::master())
     {
-        Istream* isPtr = objectStream(typeFilePath<Type>(*this));
+        Istream* isPtr = objectStream(typeFilePath<Type>(*this, search));
 
         // If the stream has failed return
         if (!isPtr)
diff --git a/src/OpenFOAM/db/IOobjects/IOMap/IOMapName.C b/src/OpenFOAM/db/IOobjects/IOMap/IOMapName.C
index 894dd2cfc60..9ee8cb061ae 100644
--- a/src/OpenFOAM/db/IOobjects/IOMap/IOMapName.C
+++ b/src/OpenFOAM/db/IOobjects/IOMap/IOMapName.C
@@ -3,7 +3,7 @@
   \\      /  F ield         | OpenFOAM: The Open Source CFD Toolbox
    \\    /   O peration     |
     \\  /    A nd           | Copyright (C) 2011-2014 OpenFOAM Foundation
-     \\/     M anipulation  |
+     \\/     M anipulation  | Copyright (C) 2016 OpenCFD Ltd.
 -------------------------------------------------------------------------------
 License
     This file is part of OpenFOAM.
@@ -30,11 +30,11 @@ namespace Foam
 {
     defineTemplateTypeNameAndDebug(IOMap<dictionary>, 0);
 
-    //- Template specialisation for obtaining filePath
+    //- Template specialization for global status
     template<>
-    fileName typeFilePath<IOMap<dictionary>>(const IOobject& io)
+    bool typeGlobal<IOMap<dictionary>>()
     {
-        return io.globalFilePath();
+        return true;
     }
 }
 
-- 
GitLab