ENH: noiseModels - replaced graph usage by writeFile

Header information now includes, e.g.

    f [Hz] vs P(f) [Pa]
    Lower frequency: 2.500000e+01
    Upper frequency: 5.000000e+03
    Window model: Hanning
    Window number: 2
    Window samples: 512
    Window overlap %: 5.000000e+01
    dBRef       : 2.000000e-05
    Area average: false
    Area sum    : 6.475194e-04
    Number of faces: 473

Note: output files now have .dat extension
This commit is contained in:
Andrew Heather
2022-09-22 14:21:44 +01:00
committed by Mark OLESEN
parent f87f0040b8
commit 7c2311aae6
12 changed files with 307 additions and 164 deletions

View File

@ -6,7 +6,7 @@
\\/ M anipulation |
-------------------------------------------------------------------------------
Copyright (C) 2011-2015 OpenFOAM Foundation
Copyright (C) 2016-2021 OpenCFD Ltd.
Copyright (C) 2016-2022 OpenCFD Ltd.
-------------------------------------------------------------------------------
License
This file is part of OpenFOAM.
@ -127,7 +127,7 @@ int main(int argc, char *argv[])
IOdictionary dict(dictIO);
autoPtr<noiseModel> model(noiseModel::New(dict));
autoPtr<noiseModel> model(noiseModel::New(dict, runTime));
model->calculate();
Info<< nl << "End\n" << endl;

View File

@ -76,6 +76,15 @@ Foam::fileName Foam::functionObjects::writeFile::baseTimeDir() const
}
Foam::fileName Foam::functionObjects::writeFile::filePath
(
const fileName& fName
) const
{
return baseFileDir()/prefix_/fName;
}
Foam::autoPtr<Foam::OFstream> Foam::functionObjects::writeFile::newFile
(
const fileName& fName
@ -85,7 +94,7 @@ Foam::autoPtr<Foam::OFstream> Foam::functionObjects::writeFile::newFile
if (Pstream::master() && writeToFile_)
{
fileName outputDir(baseFileDir()/prefix_/fName.path());
fileName outputDir(filePath(fName).path());
mkDir(outputDir);

View File

@ -137,6 +137,9 @@ protected:
//- Return the base directory for the current time value
fileName baseTimeDir() const;
//- Return the full path for the supplied file name
fileName filePath(const fileName& fName) const;
//- Return autoPtr to a new file using file name
// Note: no check for if the file already exists
virtual autoPtr<OFstream> newFile(const fileName& fName) const;

View File

@ -5,7 +5,7 @@
\\ / A nd | www.openfoam.com
\\/ M anipulation |
-------------------------------------------------------------------------------
Copyright (C) 2015-2021 OpenCFD Ltd.
Copyright (C) 2015-2022 OpenCFD Ltd.
-------------------------------------------------------------------------------
License
This file is part of OpenFOAM.
@ -243,16 +243,58 @@ Foam::fileName Foam::noiseModel::baseFileDir(const label dataseti) const
{
return
(
argList::envGlobalPath()
/ functionObject::outputPrefix
/ "noise"
/ outputPrefix_
outputPrefix_
/ type()
/ ("input" + Foam::name(dataseti))
);
}
void Foam::noiseModel::writeFileHeader
(
Ostream& os,
const string& x,
const string& y,
const List<Tuple2<string, token>>& headerValues
) const
{
writeHeader(os, x + " vs " + y);
writeHeaderValue(os, "Lower frequency", fLower_);
writeHeaderValue(os, "Upper frequency", fUpper_);
writeHeaderValue(os, "Window model", windowModelPtr_->type());
writeHeaderValue(os, "Window number", windowModelPtr_->nWindow());
writeHeaderValue(os, "Window samples", windowModelPtr_->nSamples());
writeHeaderValue(os, "Window overlap %", windowModelPtr_->overlapPercent());
writeHeaderValue(os, "dBRef", dBRef_);
for (const auto& hv : headerValues)
{
writeHeaderValue(os, hv.first(), hv.second());
}
writeCommented(os, x.substr(0, x.find(' ')));
writeTabbed(os, y.substr(0, y.find(' ')));
os << nl;
}
void Foam::noiseModel::writeFreqDataToFile
(
Ostream& os,
const scalarField& f,
const scalarField& fx
) const
{
forAll(f, i)
{
if (f[i] >= fLower_ && f[i] <= fUpper_)
{
os << f[i] << tab << fx[i] << nl;
}
}
}
Foam::tmp<Foam::scalarField> Foam::noiseModel::uniformFrequencies
(
const scalar deltaT,
@ -563,8 +605,15 @@ Foam::scalar Foam::noiseModel::gainD(const scalar f) const
// * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
Foam::noiseModel::noiseModel(const dictionary& dict, const bool readFields)
Foam::noiseModel::noiseModel
(
const dictionary& dict,
const objectRegistry& obr,
const word& name,
const bool readFields
)
:
functionObjects::writeFile(obr, "noise"),
dict_(dict),
rhoRef_(1),
nSamples_(65536),
@ -572,7 +621,6 @@ Foam::noiseModel::noiseModel(const dictionary& dict, const bool readFields)
fUpper_(10000),
startTime_(0),
windowModelPtr_(),
graphFormat_("raw"),
SPLweighting_(weightingType::none),
dBRef_(2e-5),
minPressure_(-0.5*VGREAT),
@ -603,12 +651,16 @@ Foam::noiseModel::noiseModel(const dictionary& dict, const bool readFields)
bool Foam::noiseModel::read(const dictionary& dict)
{
if (!functionObjects::writeFile::read(dict))
{
return false;
}
dict.readIfPresent("rhoRef", rhoRef_);
dict.readIfPresent("N", nSamples_);
dict.readIfPresent("fl", fLower_);
dict.readIfPresent("fu", fUpper_);
dict.readIfPresent("startTime", startTime_);
dict.readIfPresent("graphFormat", graphFormat_);
dict.readIfPresent("minPressure", minPressure_);
dict.readIfPresent("maxPressure", maxPressure_);
dict.readIfPresent("outputPrefix", outputPrefix_);

View File

@ -5,7 +5,7 @@
\\ / A nd | www.openfoam.com
\\/ M anipulation |
-------------------------------------------------------------------------------
Copyright (C) 2015-2021 OpenCFD Ltd.
Copyright (C) 2015-2022 OpenCFD Ltd.
-------------------------------------------------------------------------------
License
This file is part of OpenFOAM.
@ -64,7 +64,6 @@ Description
outputPrefix | Prefix applied to output files| no | ''
SPLweighting | Weighting: dBA, dBB, dBC, DBD | no | none
dBRef | Reference for dB calculation | no | 2e-5
graphFormat | Graph format | no | raw
writePrmsf | Write Prmsf data | no | yes
writeSPL | Write SPL data | no | yes
writePSD | Write PSD data | no | yes
@ -80,13 +79,14 @@ SourceFiles
#ifndef noiseModel_H
#define noiseModel_H
#include "writeFile.H"
#include "dictionary.H"
#include "scalarList.H"
#include "instantList.H"
#include "windowModel.H"
#include "Enum.H"
#include "Tuple2.H"
#include "runTimeSelectionTables.H"
#include "graph.H"
#include <fftw3.h>
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
@ -99,6 +99,8 @@ namespace Foam
\*---------------------------------------------------------------------------*/
class noiseModel
:
public functionObjects::writeFile
{
public:
@ -173,9 +175,6 @@ protected:
//- Window model
autoPtr<windowModel> windowModelPtr_;
//- Graph format
word graphFormat_;
//- Weighting
weightingType SPLweighting_;
@ -252,6 +251,23 @@ protected:
//- Return the base output directory
fileName baseFileDir(const label dataseti) const;
//- Write output file header
void writeFileHeader
(
Ostream& os,
const string& x,
const string& y,
const List<Tuple2<string, token>>& headerValues = {}
) const;
// Write frequency-based data to file
void writeFreqDataToFile
(
Ostream& os,
const scalarField& f,
const scalarField& fx
) const;
//- Create a field of equally spaced frequencies for the current set of
//- data - assumes a constant time step
tmp<scalarField> uniformFrequencies
@ -334,16 +350,27 @@ public:
noiseModel,
dictionary,
(
const dictionary& dict
const dictionary& dict,
const objectRegistry& obr
),
(dict)
(dict, obr)
);
//- Selector
static autoPtr<noiseModel> New(const dictionary& dict);
static autoPtr<noiseModel> New
(
const dictionary& dict,
const objectRegistry& obr
);
//- Constructor
noiseModel(const dictionary& dict, const bool readFields = true);
noiseModel
(
const dictionary& dict,
const objectRegistry& obr,
const word& name,
const bool readFields = true
);
//- Destructor
virtual ~noiseModel() = default;

View File

@ -5,7 +5,7 @@
\\ / A nd | www.openfoam.com
\\/ M anipulation |
-------------------------------------------------------------------------------
Copyright (C) 2016-2021 OpenCFD Ltd.
Copyright (C) 2016-2022 OpenCFD Ltd.
-------------------------------------------------------------------------------
License
This file is part of OpenFOAM.
@ -29,7 +29,11 @@ License
// * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
Foam::autoPtr<Foam::noiseModel> Foam::noiseModel::New(const dictionary& dict)
Foam::autoPtr<Foam::noiseModel> Foam::noiseModel::New
(
const dictionary& dict,
const objectRegistry& obr
)
{
const word modelType(dict.get<word>("noiseModel"));
@ -48,7 +52,10 @@ Foam::autoPtr<Foam::noiseModel> Foam::noiseModel::New(const dictionary& dict)
) << exit(FatalIOError);
}
return autoPtr<noiseModel>(ctorPtr(dict.subDict(modelType + "Coeffs")));
return autoPtr<noiseModel>
(
ctorPtr(dict.subDict(modelType + "Coeffs"), obr)
);
}

View File

@ -5,7 +5,7 @@
\\ / A nd | www.openfoam.com
\\/ M anipulation |
-------------------------------------------------------------------------------
Copyright (C) 2015-2021 OpenCFD Ltd.
Copyright (C) 2015-2022 OpenCFD Ltd.
-------------------------------------------------------------------------------
License
This file is part of OpenFOAM.
@ -113,19 +113,13 @@ void pointNoise::processData
// RMS pressure [Pa]
if (writePrmsf_)
{
graph g
(
"Prms(f)",
"f [Hz]",
"Prms(f) [Pa]",
f,
RMSmeanPf(p)
);
auto filePtr = newFile(outDir/"Prms_f");
auto& os = filePtr();
g.setXRange(fLower_, fUpper_);
Info<< " Writing " << os.name() << endl;
Info<< " Creating graph for " << g.title() << endl;
g.write(outDir, graph::wordify(g.title()), graphFormat_);
writeFileHeader(os, "f [Hz]", "Prms(f) [Pa]");
writeFreqDataToFile(os, f, RMSmeanPf(p));
}
// PSD [Pa^2/Hz]
@ -133,55 +127,42 @@ void pointNoise::processData
if (writePSDf_)
{
graph g
(
"PSD(f)",
"f [Hz]",
"PSD(f) [PaPa_Hz]",
f,
PSDf
);
auto filePtr = newFile(outDir/"PSD_f");
auto& os = filePtr();
g.setXRange(fLower_, fUpper_);
Info<< " Writing " << os.name() << endl;
Info<< " Creating graph for " << g.title() << endl;
g.write(outDir, graph::wordify(g.title()), graphFormat_);
writeFileHeader(os, "f [Hz]", "PSD(f) [PaPa_Hz]");
writeFreqDataToFile(os, f, PSDf);
}
// PSD [dB/Hz]
if (writePSD_)
{
graph g
(
"PSD_dB_Hz(f)",
"f [Hz]",
"PSD(f) [dB_Hz]",
f,
PSD(PSDf)
);
auto filePtr = newFile(outDir/"PSD_dB_Hz_f");
auto& os = filePtr();
g.setXRange(fLower_, fUpper_);
Info<< " Writing " << os.name() << endl;
Info<< " Creating graph for " << g.title() << endl;
g.write(outDir, graph::wordify(g.title()), graphFormat_);
writeFileHeader(os, "f [Hz]", "PSD(f) [dB_Hz]");
writeFreqDataToFile(os, f, PSD(PSDf));
}
// SPL [dB]
if (writeSPL_)
{
graph g
auto filePtr = newFile(outDir/"SPL_dB_f");
auto& os = filePtr();
Info<< " Writing " << os.name() << endl;
writeFileHeader
(
"SPL_dB(f)",
os,
"f [Hz]",
"SPL(f) [" + weightingTypeNames_[SPLweighting_] + "]",
f,
SPL(PSDf*deltaf, f)
"SPL(f) [" + weightingTypeNames_[SPLweighting_] + "]"
);
g.setXRange(fLower_, fUpper_);
Info<< " Creating graph for " << g.title() << endl;
g.write(outDir, graph::wordify(g.title()), graphFormat_);
writeFreqDataToFile(os, f, SPL(PSDf*deltaf, f));
}
if (writeOctaves_)
@ -205,26 +186,38 @@ void pointNoise::processData
// Integrated PSD = P(rms)^2 [Pa^2]
scalarField Prms13f(octaves(PSDf, f, octave13BandIDs));
graph g
auto filePtr = newFile(outDir/"SPL13_dB_fm");
auto& os = filePtr();
Info<< " Writing " << os.name() << endl;
writeFileHeader
(
"SPL13_dB(fm)",
os,
"fm [Hz]",
"SPL(fm) [" + weightingTypeNames_[SPLweighting_] + "]",
"SPL(fm) [" + weightingTypeNames_[SPLweighting_] + "]"
);
writeFreqDataToFile
(
os,
octave13FreqCentre,
SPL(Prms13f, octave13FreqCentre)
);
Info<< " Creating graph for " << g.title() << endl;
g.write(outDir, graph::wordify(g.title()), graphFormat_);
}
}
// * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
pointNoise::pointNoise(const dictionary& dict, const bool readFields)
pointNoise::pointNoise
(
const dictionary& dict,
const objectRegistry& obr,
const word& name,
const bool readFields
)
:
noiseModel(dict, false)
noiseModel(dict, obr, name, false)
{
if (readFields)
{

View File

@ -5,7 +5,7 @@
\\ / A nd | www.openfoam.com
\\/ M anipulation |
-------------------------------------------------------------------------------
Copyright (C) 2015-2016 OpenCFD Ltd.
Copyright (C) 2015-2022 OpenCFD Ltd.
-------------------------------------------------------------------------------
License
This file is part of OpenFOAM.
@ -62,8 +62,6 @@ Description
separator " ";
mergeSeparators yes;
graphFormat raw;
\endverbatim
SourceFiles
@ -129,7 +127,13 @@ public:
TypeName("pointNoise");
//- Constructor
pointNoise(const dictionary& dict, const bool readFields = true);
pointNoise
(
const dictionary& dict,
const objectRegistry& obr,
const word& name = typeName,
const bool readFields = true
);
//- Destructor
virtual ~pointNoise() = default;

View File

@ -29,7 +29,6 @@ License
#include "surfaceReader.H"
#include "surfaceWriter.H"
#include "argList.H"
#include "graph.H"
#include "addToRunTimeSelectionTable.H"
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
@ -441,9 +440,15 @@ scalar surfaceNoise::surfaceAverage
// * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
surfaceNoise::surfaceNoise(const dictionary& dict, const bool readFields)
surfaceNoise::surfaceNoise
(
const dictionary& dict,
const objectRegistry& obr,
const word& name,
const bool readFields
)
:
noiseModel(dict, false),
noiseModel(dict, obr, name, false),
inputFileNames_(),
pName_("p"),
pIndex_(0),
@ -648,7 +653,7 @@ void surfaceNoise::calculate()
const word fNameBase = fName.nameLessExt();
// Output directory for graphs
// Output directory
fileName outDirBase(baseFileDir(filei)/fNameBase);
const scalar deltaf = 1.0/(deltaT_*win.nSamples());
@ -663,8 +668,28 @@ void surfaceNoise::calculate()
<< endl;
}
// Common output information
// Note: hard-coded to read mesh from first time index
scalar surfArea = 0;
label surfSize = 0;
if (Pstream::master())
{
const meshedSurface& surf = readerPtr_->geometry(0);
surfArea = sum(surf.magSf());
surfSize = surf.size();
}
Pstream::broadcast(surfArea);
Pstream::broadcast(surfSize);
List<Tuple2<string, token>> commonInfo =
{
{"Area average", token(word(Switch::name(areaAverage_)))},
{"Area sum", token(surfArea)},
{"Number of faces", token(surfSize)}
};
{
fileName outDir(outDirBase/"fft");
fileName outSurfDir(filePath(outDir));
// Determine frequency range of interest
// Note: frequencies have fixed interval, and are in the range
@ -693,7 +718,7 @@ void surfaceNoise::calculate()
PrmsfAve[i] = writeSurfaceData
(
outDir,
outSurfDir,
fNameBase,
"Prmsf",
freq1[freqI],
@ -704,7 +729,7 @@ void surfaceNoise::calculate()
PSDfAve[i] = writeSurfaceData
(
outDir,
outSurfDir,
fNameBase,
"PSDf",
freq1[freqI],
@ -714,7 +739,7 @@ void surfaceNoise::calculate()
);
writeSurfaceData
(
outDir,
outSurfDir,
fNameBase,
"PSD",
freq1[freqI],
@ -724,7 +749,7 @@ void surfaceNoise::calculate()
);
writeSurfaceData
(
outDir,
outSurfDir,
fNameBase,
"SPL",
freq1[freqI],
@ -737,65 +762,60 @@ void surfaceNoise::calculate()
if (Pstream::master())
{
graph Prmsfg
(
"Average Prms(f)",
"f [Hz]",
"P(f) [Pa]",
fOut,
PrmsfAve
);
Prmsfg.write
(
outDir,
graph::wordify(Prmsfg.title()),
graphFormat_
);
{
auto filePtr = newFile(outDir/"Average_Prms_f");
auto& os = filePtr();
graph PSDfg
(
"Average PSD_f(f)",
"f [Hz]",
"PSD(f) [PaPa_Hz]",
fOut,
PSDfAve
);
PSDfg.write
(
outDir,
graph::wordify(PSDfg.title()),
graphFormat_
);
Info<< " Writing " << os.name() << endl;
graph PSDg
(
"Average PSD_dB_Hz(f)",
"f [Hz]",
"PSD(f) [dB_Hz]",
fOut,
PSD(PSDfAve)
);
PSDg.write
(
outDir,
graph::wordify(PSDg.title()),
graphFormat_
);
writeFileHeader(os, "f [Hz]", "P(f) [Pa]", commonInfo);
writeFreqDataToFile(os, fOut, PrmsfAve);
}
{
auto filePtr = newFile(outDir/"Average_PSD_f_f");
auto& os = filePtr();
graph SPLg
(
"Average SPL_dB(f)",
"f [Hz]",
"SPL(f) [dB]",
fOut,
SPL(PSDfAve*deltaf, fOut)
);
SPLg.write
(
outDir,
graph::wordify(SPLg.title()),
graphFormat_
);
Info<< " Writing " << os.name() << endl;
writeFileHeader
(
os,
"f [Hz]",
"PSD(f) [PaPa_Hz]",
commonInfo
);
writeFreqDataToFile(os, fOut, PSDfAve);
}
{
auto filePtr = newFile(outDir/"Average_PSD_dB_Hz_f");
auto& os = filePtr();
Info<< " Writing " << os.name() << endl;
writeFileHeader
(
os,
"f [Hz]",
"PSD(f) [dB_Hz]",
commonInfo
);
writeFreqDataToFile(os, fOut, PSD(PSDfAve));
}
{
auto filePtr = newFile(outDir/"Average_SPL_dB_f");
auto& os = filePtr();
Info<< " Writing " << os.name() << endl;
writeFileHeader
(
os,
"f [Hz]",
"SPL(f) [dB]",
commonInfo
);
writeFreqDataToFile(os, fOut, SPL(PSDfAve*deltaf, fOut));
}
}
}
@ -803,6 +823,7 @@ void surfaceNoise::calculate()
Info<< "Writing one-third octave surface data" << endl;
{
fileName outDir(outDirBase/"oneThirdOctave");
fileName outSurfDir(filePath(outDir));
scalarField PSDfAve(surfPrms13f.size(), Zero);
scalarField Prms13fAve(surfPrms13f.size(), Zero);
@ -811,7 +832,7 @@ void surfaceNoise::calculate()
{
writeSurfaceData
(
outDir,
outSurfDir,
fNameBase,
"SPL13",
octave13FreqCentre[i],
@ -826,20 +847,24 @@ void surfaceNoise::calculate()
if (Pstream::master())
{
graph SPL13g
auto filePtr = newFile(outDir/"Average_SPL13_dB_fm");
auto& os = filePtr();
Info<< " Writing " << os.name() << endl;
writeFileHeader
(
"Average SPL13_dB(fm)",
os,
"fm [Hz]",
"SPL(fm) [dB]",
commonInfo
);
writeFreqDataToFile
(
os,
octave13FreqCentre,
SPL(Prms13fAve, octave13FreqCentre)
);
SPL13g.write
(
outDir,
graph::wordify(SPL13g.title()),
graphFormat_
);
}
}
}

View File

@ -5,7 +5,7 @@
\\ / A nd | www.openfoam.com
\\/ M anipulation |
-------------------------------------------------------------------------------
Copyright (C) 2015-2021 OpenCFD Ltd.
Copyright (C) 2015-2022 OpenCFD Ltd.
-------------------------------------------------------------------------------
License
This file is part of OpenFOAM.
@ -216,7 +216,13 @@ public:
//- Constructor
explicit surfaceNoise(const dictionary& dict, const bool readFields=true);
surfaceNoise
(
const dictionary& dict,
const objectRegistry& obr,
const word& name = typeName,
const bool readFields = true
);
//- Destructor
virtual ~surfaceNoise() = default;

View File

@ -5,7 +5,7 @@
\\ / A nd | www.openfoam.com
\\/ M anipulation |
-------------------------------------------------------------------------------
Copyright (C) 2015-2020 OpenCFD Ltd.
Copyright (C) 2015-2022 OpenCFD Ltd.
-------------------------------------------------------------------------------
License
This file is part of OpenFOAM.
@ -41,14 +41,10 @@ namespace Foam
Foam::windowModel::windowModel(const dictionary& dict, const label nSamples)
:
scalarField(nSamples),
nOverlapSamples_(0),
overlapPercent_(dict.get<scalar>("overlapPercent")),
nOverlapSamples_(floor(overlapPercent_/scalar(100)*nSamples)),
nWindow_(dict.getOrDefault("nWindow", -1))
{
nOverlapSamples_ = floor
(
dict.get<scalar>("overlapPercent")/scalar(100)*nSamples
);
}
{}
// * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
@ -59,6 +55,18 @@ Foam::label Foam::windowModel::nSamples() const
}
Foam::scalar Foam::windowModel::overlapPercent() const
{
return overlapPercent_;
}
Foam::label Foam::windowModel::nOverlapSamples() const
{
return nOverlapSamples_;
}
Foam::label Foam::windowModel::nWindow() const
{
return nWindow_;

View File

@ -5,7 +5,7 @@
\\ / A nd | www.openfoam.com
\\/ M anipulation |
-------------------------------------------------------------------------------
Copyright (C) 2016-2018 OpenCFD Ltd.
Copyright (C) 2016-2022 OpenCFD Ltd.
-------------------------------------------------------------------------------
License
This file is part of OpenFOAM.
@ -59,6 +59,9 @@ protected:
// Protected Data
//- Overlap percent
scalar overlapPercent_;
//- Number of overlap samples per window
label nOverlapSamples_;
@ -108,6 +111,12 @@ public:
//- Return the number of samples in the window
label nSamples() const;
//- Return the overlap percent
scalar overlapPercent() const;
//- Return number of overlap samples per window
label nOverlapSamples() const;
//- Return the number of windows
label nWindow() const;