From 6e87320af7b188fb9afaf84733f8d29364151ba1 Mon Sep 17 00:00:00 2001
From: Henry <Henry>
Date: Sun, 3 May 2015 16:52:37 +0100
Subject: [PATCH] sampledSets, sampledSurfaces: Provide warning message if
 field cannot be found Generate fatal error if iso-surface field cannot be
 found

---
 .../sampledSets/sampledSetsGrouping.C         | 69 ++++++++++-----
 .../isoSurface/sampledIsoSurface.C            | 83 ++++++++++---------
 .../sampledSurfaces/sampledSurfaces.C         | 24 +++---
 .../sampledSurfaces/sampledSurfacesGrouping.C | 44 ++++++++--
 4 files changed, 144 insertions(+), 76 deletions(-)

diff --git a/src/sampling/sampledSet/sampledSets/sampledSetsGrouping.C b/src/sampling/sampledSet/sampledSets/sampledSetsGrouping.C
index e205d0ef239..c57a604e409 100644
--- a/src/sampling/sampledSet/sampledSets/sampledSetsGrouping.C
+++ b/src/sampling/sampledSet/sampledSets/sampledSetsGrouping.C
@@ -2,7 +2,7 @@
   =========                 |
   \\      /  F ield         | OpenFOAM: The Open Source CFD Toolbox
    \\    /   O peration     |
-    \\  /    A nd           | Copyright (C) 2011 OpenFOAM Foundation
+    \\  /    A nd           | Copyright (C) 2011-2015 OpenFOAM Foundation
      \\/     M anipulation  |
 -------------------------------------------------------------------------------
 License
@@ -83,42 +83,69 @@ Foam::label Foam::sampledSets::classifyFields()
 
     if (loadFromFiles_)
     {
-        // check files for a particular time
+        // Check files for a particular time
         IOobjectList objects(mesh_, mesh_.time().timeName());
         wordList allFields = objects.sortedNames();
 
-        labelList indices = findStrings(fieldSelection_, allFields);
-
-        forAll(indices, fieldI)
+        forAll(fieldSelection_, i)
         {
-            const word& fieldName = allFields[indices[fieldI]];
-
-            nFields += appendFieldGroup
-            (
-                fieldName,
-                objects.find(fieldName)()->headerClassName()
-            );
+            labelList indices = findStrings(fieldSelection_[i], allFields);
+
+            if (indices.size())
+            {
+                forAll(indices, fieldI)
+                {
+                    const word& fieldName = allFields[indices[fieldI]];
+
+                    nFields += appendFieldGroup
+                    (
+                        fieldName,
+                        objects.find(fieldName)()->headerClassName()
+                    );
+                }
+            }
+            else
+            {
+                WarningIn("sampledSets::classifyFields()")
+                    << "Cannot find field file matching "
+                    << fieldSelection_[i] << endl;
+            }
         }
     }
     else
     {
-        // check currently available fields
+        // Check currently available fields
         wordList allFields = mesh_.sortedNames();
         labelList indices = findStrings(fieldSelection_, allFields);
 
-        forAll(indices, fieldI)
+        forAll(fieldSelection_, i)
         {
-            const word& fieldName = allFields[indices[fieldI]];
-
-            nFields += appendFieldGroup
-            (
-                fieldName,
-                mesh_.find(fieldName)()->type()
-            );
+            labelList indices = findStrings(fieldSelection_[i], allFields);
+
+            if (indices.size())
+            {
+                forAll(indices, fieldI)
+                {
+                    const word& fieldName = allFields[indices[fieldI]];
+
+                    nFields += appendFieldGroup
+                    (
+                        fieldName,
+                        mesh_.find(fieldName)()->type()
+                    );
+                }
+            }
+            else
+            {
+                WarningIn("sampledSets::classifyFields()")
+                    << "Cannot find registered field matching "
+                    << fieldSelection_[i] << endl;
+            }
         }
     }
 
     return nFields;
 }
 
+
 // ************************************************************************* //
diff --git a/src/sampling/sampledSurface/isoSurface/sampledIsoSurface.C b/src/sampling/sampledSurface/isoSurface/sampledIsoSurface.C
index dd2a280a516..bb171c2c0e9 100644
--- a/src/sampling/sampledSurface/isoSurface/sampledIsoSurface.C
+++ b/src/sampling/sampledSurface/isoSurface/sampledIsoSurface.C
@@ -2,7 +2,7 @@
   =========                 |
   \\      /  F ield         | OpenFOAM: The Open Source CFD Toolbox
    \\    /   O peration     |
-    \\  /    A nd           | Copyright (C) 2011-2012 OpenFOAM Foundation
+    \\  /    A nd           | Copyright (C) 2011-2015 OpenFOAM Foundation
      \\/     M anipulation  |
 -------------------------------------------------------------------------------
 License
@@ -56,7 +56,7 @@ void Foam::sampledIsoSurface::getIsoFields() const
     {
         if (debug)
         {
-            Info<< "sampledIsoSurface::getIsoField() : lookup volField "
+            Info<< "sampledIsoSurface::getIsoFields() : lookup volField "
                 << isoField_ << endl;
         }
         storedVolFieldPtr_.clear();
@@ -68,7 +68,7 @@ void Foam::sampledIsoSurface::getIsoFields() const
 
         if (debug)
         {
-            Info<< "sampledIsoSurface::getIsoField() : checking "
+            Info<< "sampledIsoSurface::getIsoFields() : checking "
                 << isoField_ << " for same time " << fvm.time().timeName()
                 << endl;
         }
@@ -81,33 +81,44 @@ void Foam::sampledIsoSurface::getIsoFields() const
         {
             if (debug)
             {
-                Info<< "sampledIsoSurface::getIsoField() : reading volField "
+                Info<< "sampledIsoSurface::getIsoFields() : reading volField "
                     << isoField_ << " from time " << fvm.time().timeName()
                     << endl;
             }
 
-            storedVolFieldPtr_.reset
+            IOobject vfHeader
             (
-                new volScalarField
+                isoField_,
+                fvm.time().timeName(),
+                fvm,
+                IOobject::MUST_READ,
+                IOobject::NO_WRITE,
+                false
+            );
+
+            if (vfHeader.headerOk())
+            {
+                storedVolFieldPtr_.reset
                 (
-                    IOobject
+                    new volScalarField
                     (
-                        isoField_,
-                        fvm.time().timeName(),
-                        fvm,
-                        IOobject::MUST_READ,
-                        IOobject::NO_WRITE,
-                        false
-                    ),
-                    fvm
-                )
-            );
-            volFieldPtr_ = storedVolFieldPtr_.operator->();
+                        vfHeader,
+                        fvm
+                    )
+                );
+                volFieldPtr_ = storedVolFieldPtr_.operator->();
+            }
+            else
+            {
+                FatalErrorIn("sampledIsoSurface::getIsoFields()")
+                << "Cannot find isosurface field " << isoField_
+                << " in database or directory " << vfHeader.path()
+                << exit(FatalError);
+            }
         }
     }
 
 
-
     // Get pointField
     // ~~~~~~~~~~~~~~
 
@@ -119,7 +130,7 @@ void Foam::sampledIsoSurface::getIsoFields() const
         {
             if (debug)
             {
-                Info<< "sampledIsoSurface::getIsoField() : lookup pointField "
+                Info<< "sampledIsoSurface::getIsoFields() : lookup pointField "
                     << pointFldName << endl;
             }
             pointFieldPtr_ = &fvm.lookupObject<pointScalarField>(pointFldName);
@@ -130,9 +141,10 @@ void Foam::sampledIsoSurface::getIsoFields() const
 
             if (debug)
             {
-                Info<< "sampledIsoSurface::getIsoField() : checking pointField "
-                    << pointFldName << " for same time "
-                    << fvm.time().timeName() << endl;
+                Info<< "sampledIsoSurface::getIsoFields() : "
+                    << "checking pointField " << pointFldName
+                    << " for same time " << fvm.time().timeName()
+                    << endl;
             }
 
             if
@@ -143,7 +155,7 @@ void Foam::sampledIsoSurface::getIsoFields() const
             {
                 if (debug)
                 {
-                    Info<< "sampledIsoSurface::getIsoField() :"
+                    Info<< "sampledIsoSurface::getIsoFields() :"
                         << " interpolating volField " << volFieldPtr_->name()
                         << " to get pointField " << pointFldName << endl;
                 }
@@ -173,10 +185,10 @@ void Foam::sampledIsoSurface::getIsoFields() const
 
         if (debug)
         {
-            Info<< "sampledIsoSurface::getIsoField() : volField "
+            Info<< "sampledIsoSurface::getIsoFields() : volField "
                 << volFieldPtr_->name() << " min:" << min(*volFieldPtr_).value()
                 << " max:" << max(*volFieldPtr_).value() << endl;
-            Info<< "sampledIsoSurface::getIsoField() : pointField "
+            Info<< "sampledIsoSurface::getIsoFields() : pointField "
                 << pointFieldPtr_->name()
                 << " min:" << gMin(pointFieldPtr_->internalField())
                 << " max:" << gMax(pointFieldPtr_->internalField()) << endl;
@@ -193,7 +205,7 @@ void Foam::sampledIsoSurface::getIsoFields() const
         {
             if (debug)
             {
-                Info<< "sampledIsoSurface::getIsoField() :"
+                Info<< "sampledIsoSurface::getIsoFields() :"
                     << " submesh lookup volField "
                     << isoField_ << endl;
             }
@@ -204,8 +216,8 @@ void Foam::sampledIsoSurface::getIsoFields() const
         {
             if (debug)
             {
-                Info<< "sampledIsoSurface::getIsoField() : subsetting volField "
-                    << isoField_ << endl;
+                Info<< "sampledIsoSurface::getIsoFields() : "
+                    << "subsetting volField " << isoField_ << endl;
             }
             storedVolSubFieldPtr_.reset
             (
@@ -230,7 +242,7 @@ void Foam::sampledIsoSurface::getIsoFields() const
         {
             if (debug)
             {
-                Info<< "sampledIsoSurface::getIsoField() :"
+                Info<< "sampledIsoSurface::getIsoFields() :"
                     << " submesh lookup pointField " << pointFldName << endl;
             }
             storedPointSubFieldPtr_.clear();
@@ -243,7 +255,7 @@ void Foam::sampledIsoSurface::getIsoFields() const
         {
             if (debug)
             {
-                Info<< "sampledIsoSurface::getIsoField() :"
+                Info<< "sampledIsoSurface::getIsoFields() :"
                     << " interpolating submesh volField "
                     << volSubFieldPtr_->name()
                     << " to get submesh pointField " << pointFldName << endl;
@@ -260,7 +272,6 @@ void Foam::sampledIsoSurface::getIsoFields() const
         }
 
 
-
         // If averaging redo the volField. Can only be done now since needs the
         // point field.
         if (average_)
@@ -275,11 +286,11 @@ void Foam::sampledIsoSurface::getIsoFields() const
 
         if (debug)
         {
-            Info<< "sampledIsoSurface::getIsoField() : volSubField "
+            Info<< "sampledIsoSurface::getIsoFields() : volSubField "
                 << volSubFieldPtr_->name()
                 << " min:" << min(*volSubFieldPtr_).value()
                 << " max:" << max(*volSubFieldPtr_).value() << endl;
-            Info<< "sampledIsoSurface::getIsoField() : pointSubField "
+            Info<< "sampledIsoSurface::getIsoFields() : pointSubField "
                 << pointSubFieldPtr_->name()
                 << " min:" << gMin(pointSubFieldPtr_->internalField())
                 << " max:" << gMax(pointSubFieldPtr_->internalField()) << endl;
@@ -292,7 +303,7 @@ bool Foam::sampledIsoSurface::updateGeometry() const
 {
     const fvMesh& fvm = static_cast<const fvMesh&>(mesh());
 
-    // no update needed
+    // No update needed
     if (fvm.time().timeIndex() == prevTimeIndex_)
     {
         return false;
@@ -589,8 +600,6 @@ void Foam::sampledIsoSurface::print(Ostream& os) const
     os  << "sampledIsoSurface: " << name() << " :"
         << "  field   :" << isoField_
         << "  value   :" << isoVal_;
-        //<< "  faces:" << faces().size()       // note: possibly no geom yet
-        //<< "  points:" << points().size();
 }
 
 
diff --git a/src/sampling/sampledSurface/sampledSurfaces/sampledSurfaces.C b/src/sampling/sampledSurface/sampledSurfaces/sampledSurfaces.C
index b33d6b30b75..220a61baee6 100644
--- a/src/sampling/sampledSurface/sampledSurfaces/sampledSurfaces.C
+++ b/src/sampling/sampledSurface/sampledSurfaces/sampledSurfaces.C
@@ -2,7 +2,7 @@
   =========                 |
   \\      /  F ield         | OpenFOAM: The Open Source CFD Toolbox
    \\    /   O peration     |
-    \\  /    A nd           | Copyright (C) 2011-2013 OpenFOAM Foundation
+    \\  /    A nd           | Copyright (C) 2011-2015 OpenFOAM Foundation
      \\/     M anipulation  |
 -------------------------------------------------------------------------------
 License
@@ -35,12 +35,10 @@ License
 
 namespace Foam
 {
-defineTypeNameAndDebug(sampledSurfaces, 0);
+    defineTypeNameAndDebug(sampledSurfaces, 0);
 }
 
-
 bool Foam::sampledSurfaces::verbose_ = false;
-
 Foam::scalar Foam::sampledSurfaces::mergeTol_ = 1e-10;
 
 
@@ -49,7 +47,7 @@ Foam::scalar Foam::sampledSurfaces::mergeTol_ = 1e-10;
 void Foam::sampledSurfaces::writeGeometry() const
 {
     // Write to time directory under outputPath_
-    // skip surface without faces (eg, a failed cut-plane)
+    // Skip surface without faces (eg, a failed cut-plane)
 
     const fileName outputDir = outputPath_/mesh_.time().timeName();
 
@@ -153,7 +151,7 @@ void Foam::sampledSurfaces::write()
 {
     if (size())
     {
-        // finalize surfaces, merge points etc.
+        // Finalize surfaces, merge points etc.
         update();
 
         const label nFields = classifyFields();
@@ -170,7 +168,7 @@ void Foam::sampledSurfaces::write()
             mkDir(outputPath_/mesh_.time().timeName());
         }
 
-        // write geometry first if required,
+        // Write geometry first if required,
         // or when no fields would otherwise be written
         if (nFields == 0 || formatter_->separateGeometry())
         {
@@ -205,8 +203,8 @@ void Foam::sampledSurfaces::read(const dictionary& dict)
         dict.lookup("interpolationScheme") >> interpolationScheme_;
         const word writeType(dict.lookup("surfaceFormat"));
 
-        // define the surface formatter
-        // optionally defined extra controls for the output formats
+        // Define the surface formatter
+        // Optionally defined extra controls for the output formats
         formatter_ = surfaceWriter::New
         (
             writeType,
@@ -225,7 +223,7 @@ void Foam::sampledSurfaces::read(const dictionary& dict)
             mergeList_.setSize(size());
         }
 
-        // ensure all surfaces and merge information are expired
+        // Ensure all surfaces and merge information are expired
         expire();
 
         if (this->size())
@@ -301,7 +299,7 @@ bool Foam::sampledSurfaces::expire()
             justExpired = true;
         }
 
-        // clear merge information
+        // Clear merge information
         if (Pstream::parRun())
         {
             mergeList_[surfI].clear();
@@ -322,7 +320,7 @@ bool Foam::sampledSurfaces::update()
         return updated;
     }
 
-    // serial: quick and easy, no merging required
+    // Serial: quick and easy, no merging required
     if (!Pstream::parRun())
     {
         forAll(*this, surfI)
@@ -336,7 +334,7 @@ bool Foam::sampledSurfaces::update()
         return updated;
     }
 
-    // dimension as fraction of mesh bounding box
+    // Dimension as fraction of mesh bounding box
     scalar mergeDim = mergeTol_ * mesh_.bounds().mag();
 
     if (Pstream::master() && debug)
diff --git a/src/sampling/sampledSurface/sampledSurfaces/sampledSurfacesGrouping.C b/src/sampling/sampledSurface/sampledSurfaces/sampledSurfacesGrouping.C
index 6e67008de34..27b4d63d603 100644
--- a/src/sampling/sampledSurface/sampledSurfaces/sampledSurfacesGrouping.C
+++ b/src/sampling/sampledSurface/sampledSurfaces/sampledSurfacesGrouping.C
@@ -2,7 +2,7 @@
   =========                 |
   \\      /  F ield         | OpenFOAM: The Open Source CFD Toolbox
    \\    /   O peration     |
-    \\  /    A nd           | Copyright (C) 2011 OpenFOAM Foundation
+    \\  /    A nd           | Copyright (C) 2011-2015 OpenFOAM Foundation
      \\/     M anipulation  |
 -------------------------------------------------------------------------------
 License
@@ -32,20 +32,54 @@ License
 
 Foam::label Foam::sampledSurfaces::classifyFields()
 {
-     // check files for a particular time
+    label nFields = 0;
+
     if (loadFromFiles_)
     {
+        // Check files for a particular time
         IOobjectList objects(mesh_, mesh_.time().timeName());
         wordList allFields = objects.sortedNames();
-        labelList indices = findStrings(fieldSelection_, allFields);
-        return indices.size();
+
+        forAll(fieldSelection_, i)
+        {
+            labelList indices = findStrings(fieldSelection_[i], allFields);
+
+            if (indices.size())
+            {
+                nFields += indices.size();
+            }
+            else
+            {
+                WarningIn("sampledSurfaces::classifyFields()")
+                    << "Cannot find field file matching "
+                    << fieldSelection_[i] << endl;
+            }
+        }
     }
     else
     {
+        // Check currently available fields
         wordList allFields = mesh_.sortedNames();
         labelList indices = findStrings(fieldSelection_, allFields);
-        return indices.size();
+
+        forAll(fieldSelection_, i)
+        {
+            labelList indices = findStrings(fieldSelection_[i], allFields);
+
+            if (indices.size())
+            {
+                nFields += indices.size();
+            }
+            else
+            {
+                WarningIn("sampledSurfaces::classifyFields()")
+                    << "Cannot find registered field matching "
+                    << fieldSelection_[i] << endl;
+            }
+        }
     }
+
+    return nFields;
 }
 
 
-- 
GitLab