ENH: noiseModels - enable models to accept lists of file names

This commit is contained in:
Andrew Heather
2016-11-24 21:31:08 +00:00
parent 7734d7ffd7
commit 00174405c6
8 changed files with 427 additions and 311 deletions

View File

@ -204,7 +204,8 @@ Foam::Function1Types::CSV<Type>::CSV
(
const word& entryName,
const dictionary& dict,
const word& ext
const word& ext,
const fileName& fName
)
:
TableBase<Type>(entryName, dict.subDict(entryName + ext)),
@ -214,7 +215,7 @@ Foam::Function1Types::CSV<Type>::CSV
componentColumns_(coeffs_.lookup("componentColumns")),
separator_(coeffs_.lookupOrDefault<string>("separator", string(","))[0]),
mergeSeparators_(readBool(coeffs_.lookup("mergeSeparators"))),
fName_(coeffs_.lookup("fileName"))
fName_(fName != fileName::null ? fName : coeffs_.lookup("fileName"))
{
if (componentColumns_.size() != pTraits<Type>::nComponents)
{

View File

@ -122,7 +122,8 @@ public:
(
const word& entryName,
const dictionary& dict,
const word& ext = "Coeffs"
const word& ext = "Coeffs",
const fileName& fName = fileName::null
);
//- Copy constructor

View File

@ -94,17 +94,41 @@ Foam::label Foam::noiseModel::findStartTimeIndex
// * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
Foam::noiseModel::noiseModel(const dictionary& dict)
Foam::noiseModel::noiseModel(const dictionary& dict, const bool readFields)
:
dict_(dict),
rhoRef_(dict.lookupOrDefault<scalar>("rhoRef", 1)),
nSamples_(dict.lookupOrDefault<label>("N", 65536)),
fLower_(dict.lookupOrDefault<scalar>("fl", 25)),
fUpper_(dict.lookupOrDefault<scalar>("fu", 10000)),
startTime_(dict.lookupOrDefault<scalar>("startTime", 0)),
windowModelPtr_(windowModel::New(dict, nSamples_)),
graphFormat_(dict.lookupOrDefault<word>("graphFormat", "raw"))
rhoRef_(1),
nSamples_(65536),
fLower_(25),
fUpper_(10000),
startTime_(0),
windowModelPtr_(),
graphFormat_("raw")
{
if (readFields)
{
read(dict);
}
}
// * * * * * * * * * * * * * * * * Destructor * * * * * * * * * * * * * * * //
Foam::noiseModel::~noiseModel()
{}
// * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
bool Foam::noiseModel::read(const dictionary& dict)
{
dict.readIfPresent("rhoRef", rhoRef_);
dict.readIfPresent("N", nSamples_);
dict.readIfPresent("fl", fLower_);
dict.readIfPresent("fu", fUpper_);
dict.readIfPresent("startTime", startTime_);
dict.readIfPresent("graphFormat", graphFormat_);
// Check number of samples - must be a power of 2 for our FFT
bool powerOf2 = ((nSamples_ != 0) && !(nSamples_ & (nSamples_ - 1)));
if (!powerOf2)
@ -139,13 +163,11 @@ Foam::noiseModel::noiseModel(const dictionary& dict)
<< exit(FatalIOError);
}
windowModelPtr_ = windowModel::New(dict, nSamples_);
return true;
}
// * * * * * * * * * * * * * * * * Destructor * * * * * * * * * * * * * * * //
Foam::noiseModel::~noiseModel()
{}
// ************************************************************************* //

View File

@ -155,7 +155,7 @@ public:
static autoPtr<noiseModel> New(const dictionary& dict);
//- Constructor
noiseModel(const dictionary& dict);
noiseModel(const dictionary& dict, const bool readFields = true);
//- Destructor
virtual ~noiseModel();
@ -163,6 +163,9 @@ public:
// Public Member Functions
//- Read from dictionary
virtual bool read(const dictionary& dict);
//- Abstract call to calculate
virtual void calculate() = 0;
};

View File

@ -43,14 +43,12 @@ addToRunTimeSelectionTable(noiseModel, pointNoise, dictionary);
void pointNoise::filterTimeData
(
const Function1Types::CSV<scalar>& pData,
const scalarField& t0,
const scalarField& p0,
scalarField& t,
scalarField& p
)
) const
{
const scalarField t0(pData.x());
const scalarField p0(pData.y());
DynamicList<scalar> tf(t0.size());
DynamicList<scalar> pf(t0.size());
@ -68,23 +66,15 @@ void pointNoise::filterTimeData
}
// * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
void pointNoise::calculate()
void pointNoise::processData(const Function1Types::CSV<scalar>& data)
{
// Point data only handled by master
if (!Pstream::master())
{
return;
}
Info<< "Reading data file " << data.fName() << endl;
Info<< "Reading data file" << endl;
Function1Types::CSV<scalar> pData("pressure", dict_, "Data");
const fileName& fNameBase = data.fName()(true);
// Time and pressure history data
scalarField t, p;
filterTimeData(pData, t, p);
filterTimeData(data.x(), data.y(), t, p);
p *= rhoRef_;
Info<< " read " << t.size() << " values" << nl << endl;
@ -96,7 +86,7 @@ void pointNoise::calculate()
windowModelPtr_->validate(t.size());
const windowModel& win = windowModelPtr_();
const scalar deltaf = 1.0/(deltaT*win.nSamples());
fileName outDir(fileName("postProcessing")/"noise"/typeName);
fileName outDir(fileName("postProcessing")/"noise"/typeName/fNameBase);
// Create the fft
noiseFFT nfft(deltaT, p);
@ -185,12 +175,45 @@ void pointNoise::calculate()
}
// * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
void pointNoise::calculate()
{
// Point data only handled by master
if (!Pstream::master())
{
return;
}
if (inputFileNames_.size())
{
forAll(inputFileNames_, i)
{
const fileName fName = inputFileNames_[i].expand();
Function1Types::CSV<scalar> data("pressure", dict_, "Data", fName);
processData(data);
}
}
else
{
Function1Types::CSV<scalar> data("pressure", dict_, "Data");
processData(data);
}
}
// * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
pointNoise::pointNoise(const dictionary& dict)
pointNoise::pointNoise(const dictionary& dict, const bool readFields)
:
noiseModel(dict)
{}
{
if (readFields)
{
read(dict);
}
}
// * * * * * * * * * * * * * * * * Destructor * * * * * * * * * * * * * * * //
@ -199,6 +222,18 @@ pointNoise::~pointNoise()
{}
bool pointNoise::read(const dictionary& dict)
{
if (noiseModel::read(dict))
{
dict.readIfPresent("inputFiles", inputFileNames_);
return true;
}
return false;
}
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
} // End namespace noiseModels

View File

@ -76,8 +76,8 @@ SeeAlso
\*---------------------------------------------------------------------------*/
#ifndef pointNoise_H
#define pointNoise_H
#ifndef noiseModels_pointNoise_H
#define noiseModels_pointNoise_H
#include "noiseModel.H"
#include "CSV.H"
@ -100,14 +100,24 @@ class pointNoise
protected:
// Protected data
//- Input file names - optional
List<fileName> inputFileNames_;
// Protected Member Functions
void filterTimeData
(
const Function1Types::CSV<scalar>& pData,
const scalarField& t0,
const scalarField& p0,
scalarField& t,
scalarField& p
);
) const;
//- Process the CSV data
void processData(const Function1Types::CSV<scalar>& data);
public:
@ -116,7 +126,7 @@ public:
TypeName("pointNoise");
//- Constructor
pointNoise(const dictionary& dict);
pointNoise(const dictionary& dict, const bool readFields = true);
//- Destructor
virtual ~pointNoise();
@ -124,6 +134,9 @@ public:
// Public Member Functions
//- Read from dictionary
virtual bool read(const dictionary& dict);
//- Calculate
virtual void calculate();

View File

@ -44,12 +44,9 @@ addToRunTimeSelectionTable(noiseModel, surfaceNoise, dictionary);
// * * * * * * * * * * * * * Protected Member Functions * * * * * * * * * * //
void surfaceNoise::initialise(const dictionary& dict)
void surfaceNoise::initialise(const fileName& fName)
{
dict.lookup("inputFile") >> inputFileName_;
inputFileName_.expand();
dict.readIfPresent("fftWriteInterval", fftWriteInterval_);
Info<< "Reading data file " << fName << endl;
label nAvailableTimes = 0;
@ -57,17 +54,15 @@ void surfaceNoise::initialise(const dictionary& dict)
if (Pstream::master())
{
// Create the surface reader
const word readerType(dict.lookup("reader"));
readerPtr_.reset(surfaceReader::New(readerType, inputFileName_).ptr());
readerPtr_ = surfaceReader::New(readerType_, fName);
// Find the index of the pressure data
const word pName(dict.lookupOrDefault<word>("p", "p"));
const List<word> fieldNames(readerPtr_->fieldNames(0));
pIndex_ = findIndex(fieldNames, pName);
pIndex_ = findIndex(fieldNames, pName_);
if (pIndex_ == -1)
{
FatalErrorInFunction
<< "Unable to find pressure field name " << pName
<< "Unable to find pressure field name " << pName_
<< " in list of available fields: " << fieldNames
<< exit(FatalError);
}
@ -76,12 +71,6 @@ void surfaceNoise::initialise(const dictionary& dict)
// - Could be done later, but since this utility can process a lot of
// data we can ensure that the user-input is correct prior to doing
// the heavy lifting
const word writerType(dict.lookup("writer"));
dictionary optDict
(
dict.subOrEmptyDict("writeOptions").subOrEmptyDict(writerType)
);
writerPtr_.reset(surfaceWriter::New(writerType, optDict).ptr());
// Set the time range
const instantList allTimes = readerPtr_->times();
@ -404,18 +393,25 @@ Foam::scalar surfaceNoise::surfaceAverage
// * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
surfaceNoise::surfaceNoise(const dictionary& dict)
surfaceNoise::surfaceNoise(const dictionary& dict, const bool readFields)
:
noiseModel(dict),
inputFileName_("unknown-inputFile"),
noiseModel(dict, false),
inputFileNames_(),
pName_("p"),
pIndex_(0),
times_(),
deltaT_(0),
startTimeIndex_(0),
nFace_(0),
fftWriteInterval_(1)
fftWriteInterval_(1),
readerType_(word::null),
readerPtr_(nullptr),
writerPtr_(nullptr)
{
initialise(dict);
if (readFields)
{
read(dict);
}
}
@ -427,8 +423,39 @@ surfaceNoise::~surfaceNoise()
// * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
bool surfaceNoise::read(const dictionary& dict)
{
if (noiseModel::read(dict))
{
dict.lookup("inputFiles") >> inputFileNames_;
dict.readIfPresent("fftWriteInterval", fftWriteInterval_);
dict.readIfPresent("p", pName_);
dict.lookup("reader") >> readerType_;
word writerType(dict.lookup("writer"));
dictionary optDict
(
dict.subOrEmptyDict("writeOptions").subOrEmptyDict(writerType)
);
writerPtr_ = surfaceWriter::New(writerType, optDict);
return true;
}
return false;
}
void surfaceNoise::calculate()
{
forAll(inputFileNames_, i)
{
fileName fName = inputFileNames_[i];
initialise(fName.expand());
// Container for pressure time history data per face
List<scalarField> pData;
@ -536,8 +563,13 @@ void surfaceNoise::calculate()
}
}
const word& fNameBase = fName.name(true);
// Output directory for graphs
fileName outDir(fileName("postProcessing")/"noise"/typeName);
fileName outDir
(
fileName("postProcessing")/"noise"/typeName/fNameBase
);
const scalar deltaf = 1.0/(deltaT_*win.nSamples());
Info<< "Writing fft surface data" << endl;
@ -550,11 +582,10 @@ void surfaceNoise::calculate()
{
label freqI = i*fftWriteInterval_;
fOut[i] = freq1[freqI];
const word& fName = inputFileName_.name(true);
const word gName = "fft";
PrmsfAve[i] = writeSurfaceData
(
fName,
fNameBase,
gName,
"Prmsf",
freq1[freqI],
@ -564,7 +595,7 @@ void surfaceNoise::calculate()
PSDfAve[i] = writeSurfaceData
(
fName,
fNameBase,
gName,
"PSDf",
freq1[freqI],
@ -573,7 +604,7 @@ void surfaceNoise::calculate()
);
writeSurfaceData
(
fName,
fNameBase,
gName,
"PSD",
freq1[freqI],
@ -582,7 +613,7 @@ void surfaceNoise::calculate()
);
writeSurfaceData
(
fName,
fNameBase,
gName,
"SPL",
freq1[freqI],
@ -640,11 +671,10 @@ void surfaceNoise::calculate()
forAll(surfPSD13f, i)
{
const word& fName = inputFileName_.name(true);
const word gName = "oneThirdOctave";
PSDfAve[i] = writeSurfaceData
(
fName,
fNameBase,
gName,
"PSD13f",
octave13FreqCentre[i],
@ -653,7 +683,7 @@ void surfaceNoise::calculate()
);
writeSurfaceData
(
fName,
fNameBase,
gName,
"PSD13",
octave13FreqCentre[i],
@ -662,7 +692,7 @@ void surfaceNoise::calculate()
);
writeSurfaceData
(
fName,
fNameBase,
gName,
"SPL13",
octave13FreqCentre[i],
@ -670,7 +700,8 @@ void surfaceNoise::calculate()
procFaceOffset
);
Prms13f2Ave[i] = surfaceAverage(surfPrms13f2[i], procFaceOffset);
Prms13f2Ave[i] =
surfaceAverage(surfPrms13f2[i], procFaceOffset);
}
graph PSD13g
@ -693,6 +724,7 @@ void surfaceNoise::calculate()
);
SPL13g.write(outDir, graph::wordify(SPL13g.title()), graphFormat_);
}
}
}

View File

@ -53,7 +53,7 @@ Description
}
// Input file
inputFile "postProcessing/faceSource1/surface/patch/patch.case";
inputFiles ("postProcessing/faceSource1/surface/patch/patch.case");
// Write interval for FFT data, default = 1
fftWriteInterval 100;
@ -83,8 +83,8 @@ SeeAlso
\*---------------------------------------------------------------------------*/
#ifndef surfaceNoise_H
#define surfaceNoise_H
#ifndef noiseModels_surfaceNoise_H
#define noiseModels_surfaceNoise_H
#include "noiseModel.H"
#include "labelList.H"
@ -115,8 +115,11 @@ protected:
// Protected Data
//- Input file name
fileName inputFileName_;
//- Input file names
List<fileName> inputFileNames_;
//- Name of pressure field
word pName_;
//- Index of pressure field in reader field list
label pIndex_;
@ -138,17 +141,20 @@ protected:
// result in a very large number of output files (1 per frequency)
label fftWriteInterval_;
//- Reader type
word readerType_;
//- Pointer to the surface reader
mutable autoPtr<surfaceReader> readerPtr_;
//- Pointer to the surface writer
autoPtr<surfaceWriter> writerPtr_;
mutable autoPtr<surfaceWriter> writerPtr_;
// Protected Member Functions
//- Initialise
void initialise(const dictionary& dict);
void initialise(const fileName& fName);
//- Read surface data
void readSurfaceData
@ -183,7 +189,7 @@ public:
TypeName("surfaceNoise");
//- Constructor
surfaceNoise(const dictionary& dict);
surfaceNoise(const dictionary& dict, const bool readFields = true);
//- Destructor
virtual ~surfaceNoise();
@ -191,6 +197,9 @@ public:
// Public Member Functions
//- Read from dictionary
virtual bool read(const dictionary& dict);
//- Calculate
virtual void calculate();
};