From 6a0b35bdc8d02acf2ede5662bb574c8394e9aaf4 Mon Sep 17 00:00:00 2001
From: Andrew Heather <a.heather@opencfd.co.uk>
Date: Thu, 1 Jun 2017 17:27:30 +0100
Subject: [PATCH] ENH: noiseModels - updated parallel usage (no longer needs
 proc dirs) and apply input data validation

---
 .../utilities/postProcessing/noise/noise.C    |  2 +
 src/OpenFOAM/global/argList/argList.C         | 44 +++++++++++++++----
 src/OpenFOAM/global/argList/argList.H         |  6 +++
 .../noise/noiseModels/noiseModel/noiseModel.C | 28 ++++++++++++
 .../noise/noiseModels/noiseModel/noiseModel.H | 14 +++++-
 .../noise/noiseModels/pointNoise/pointNoise.C |  8 ++++
 6 files changed, 92 insertions(+), 10 deletions(-)

diff --git a/applications/utilities/postProcessing/noise/noise.C b/applications/utilities/postProcessing/noise/noise.C
index c872b1557ce..089efeefac8 100644
--- a/applications/utilities/postProcessing/noise/noise.C
+++ b/applications/utilities/postProcessing/noise/noise.C
@@ -102,6 +102,8 @@ using namespace Foam;
 
 int main(int argc, char *argv[])
 {
+    argList::noCheckProcessorDirectories();
+
     #include "addDictOption.H"
     #include "setRootCase.H"
     #include "createTime.H"
diff --git a/src/OpenFOAM/global/argList/argList.C b/src/OpenFOAM/global/argList/argList.C
index fad74c2dd0a..a869e791f35 100644
--- a/src/OpenFOAM/global/argList/argList.C
+++ b/src/OpenFOAM/global/argList/argList.C
@@ -43,6 +43,7 @@ License
 // * * * * * * * * * * * * * * Static Data Members * * * * * * * * * * * * * //
 
 bool Foam::argList::bannerEnabled_ = true;
+bool Foam::argList::checkProcessorDirectories_ = true;
 Foam::SLList<Foam::string>    Foam::argList::validArgs;
 Foam::HashTable<Foam::string> Foam::argList::validOptions;
 Foam::HashTable<Foam::string> Foam::argList::validParOptions;
@@ -194,6 +195,12 @@ void Foam::argList::noParallel()
 }
 
 
+void Foam::argList::noCheckProcessorDirectories()
+{
+    checkProcessorDirectories_ = false;
+}
+
+
 void Foam::argList::printOptionUsage
 (
     const label location,
@@ -748,7 +755,7 @@ void Foam::argList::parse
             // - normal running : nProcs = dictNProcs = nProcDirs
             // - decomposition to more  processors : nProcs = dictNProcs
             // - decomposition to fewer processors : nProcs = nProcDirs
-            if (dictNProcs > Pstream::nProcs())
+            if (checkProcessorDirectories_ && dictNProcs > Pstream::nProcs())
             {
                 FatalError
                     << source
@@ -803,7 +810,11 @@ void Foam::argList::parse
             {
                 // Possibly going to fewer processors.
                 // Check if all procDirs are there.
-                if (dictNProcs < Pstream::nProcs())
+                if
+                (
+                    checkProcessorDirectories_
+                 && dictNProcs < Pstream::nProcs()
+                )
                 {
                     label nProcDirs = 0;
                     while
@@ -1318,15 +1329,30 @@ bool Foam::argList::checkRootCase() const
         return false;
     }
 
-    if (Pstream::master() && !isDir(path()))
+    if (Pstream::parRun())
     {
-        // Allow slaves on non-existing processor directories, created later
-        FatalError
-            << executable_
-            << ": cannot open case directory " << path()
-            << endl;
+        if (Pstream::master() && (checkProcessorDirectories_ && !isDir(path())))
+        {
+            // Allow slaves on non-existing processor directories created later
+            FatalError
+                << executable_
+                << ": cannot open case directory " << path()
+                << endl;
 
-        return false;
+            return false;
+        }
+    }
+    else
+    {
+        if (!isDir(path()))
+        {
+            FatalError
+                << executable_
+                << ": cannot open case directory " << path()
+                << endl;
+
+            return false;
+        }
     }
 
     return true;
diff --git a/src/OpenFOAM/global/argList/argList.H b/src/OpenFOAM/global/argList/argList.H
index 09c6777261c..569125c7da9 100644
--- a/src/OpenFOAM/global/argList/argList.H
+++ b/src/OpenFOAM/global/argList/argList.H
@@ -119,6 +119,9 @@ class argList
         //- Track enabled/disabled banner state
         static bool bannerEnabled_;
 
+        //- Track enabled/disabled checking of processor directories state
+        static bool checkProcessorDirectories_;
+
         //- Switch on/off parallel mode. Has to be first to be constructed
         //  so destructor is done last.
         ParRunControl parRunControl_;
@@ -387,6 +390,9 @@ public:
             //- Remove the parallel options
             static void noParallel();
 
+            //- Remove checking of processor directories
+            static void noCheckProcessorDirectories();
+
             //- Return true if the post-processing option is specified
             static bool postProcess(int argc, char *argv[]);
 
diff --git a/src/randomProcesses/noise/noiseModels/noiseModel/noiseModel.C b/src/randomProcesses/noise/noiseModels/noiseModel/noiseModel.C
index b98f00c8bda..84ba84ca4be 100644
--- a/src/randomProcesses/noise/noiseModels/noiseModel/noiseModel.C
+++ b/src/randomProcesses/noise/noiseModels/noiseModel/noiseModel.C
@@ -92,6 +92,28 @@ Foam::scalar Foam::noiseModel::checkUniformTimeStep
 }
 
 
+bool Foam::noiseModel::validateBounds(const scalarList& p) const
+{
+    forAll(p, i)
+    {
+        if ((p[i] < minPressure_) || (p[i] > maxPressure_))
+        {
+            WarningInFunction
+                << "Pressure data at position " << i
+                << " is outside of permitted bounds:" << nl
+                << "    pressure: " << p[i] << nl
+                << "    minimum pressure: " << minPressure_ << nl
+                << "    maximum pressure: " << maxPressure_ << nl
+                << endl;
+
+            return false;
+        }
+    }
+
+    return true;
+}
+
+
 Foam::label Foam::noiseModel::findStartTimeIndex
 (
     const instantList& allTimes,
@@ -141,6 +163,8 @@ Foam::noiseModel::noiseModel(const dictionary& dict, const bool readFields)
     startTime_(0),
     windowModelPtr_(),
     graphFormat_("raw"),
+    minPressure_(-0.5*VGREAT),
+    maxPressure_(0.5*VGREAT),
     outputPrefix_(),
     writePrmsf_(true),
     writeSPL_(true),
@@ -178,6 +202,8 @@ bool Foam::noiseModel::read(const dictionary& dict)
     }
     dict.readIfPresent("startTime", startTime_);
     dict.readIfPresent("graphFormat", graphFormat_);
+    dict.readIfPresent("minPressure", minPressure_);
+    dict.readIfPresent("maxPressure", maxPressure_);
     dict.readIfPresent("outputPrefix", outputPrefix_);
 
     // Check number of samples  - must be a power of 2 for our FFT
@@ -225,6 +251,8 @@ bool Foam::noiseModel::read(const dictionary& dict)
 
     windowModelPtr_ = windowModel::New(dict, nSamples_);
 
+    Info<< nl << endl;
+
     return true;
 }
 
diff --git a/src/randomProcesses/noise/noiseModels/noiseModel/noiseModel.H b/src/randomProcesses/noise/noiseModels/noiseModel/noiseModel.H
index 7b625096cea..b74e053dd00 100644
--- a/src/randomProcesses/noise/noiseModels/noiseModel/noiseModel.H
+++ b/src/randomProcesses/noise/noiseModels/noiseModel/noiseModel.H
@@ -126,7 +126,7 @@ protected:
         //- Upper frequency limit, default = 10kHz
         scalar fUpper_;
 
-        //- Flag to indicate that custom frequenct bounds are being used
+        //- Flag to indicate that custom frequency bounds are being used
         bool customBounds_;
 
         //- Start time, default = 0s
@@ -139,6 +139,15 @@ protected:
         word graphFormat_;
 
 
+        // Data validation
+
+            //- Min pressure value
+            scalar minPressure_;
+
+            //- Min pressure value
+            scalar maxPressure_;
+
+
         // Write options
 
             //- Output  file prefix, default = ''
@@ -176,6 +185,9 @@ protected:
             const scalarList& times
         ) const;
 
+        //- Return true if all pressure data is within min/max bounds
+        bool validateBounds(const scalarList& p) const;
+
         //- Find and return start time index
         label findStartTimeIndex
         (
diff --git a/src/randomProcesses/noise/noiseModels/pointNoise/pointNoise.C b/src/randomProcesses/noise/noiseModels/pointNoise/pointNoise.C
index 083bcdf31bf..3449b3295c5 100644
--- a/src/randomProcesses/noise/noiseModels/pointNoise/pointNoise.C
+++ b/src/randomProcesses/noise/noiseModels/pointNoise/pointNoise.C
@@ -84,8 +84,16 @@ void pointNoise::processData
     Info<< "    read " << t.size() << " values" << nl << endl;
 
     Info<< "Creating noise FFT" << endl;
+
     const scalar deltaT = checkUniformTimeStep(t);
 
+    if (!validateBounds(p))
+    {
+        Info<< "No noise data generated" << endl;
+        return;
+    }
+
+
     // Determine the windowing
     windowModelPtr_->validate(t.size());
     const windowModel& win = windowModelPtr_();
-- 
GitLab