pointNoise.C
Go to the documentation of this file.
1 /*---------------------------------------------------------------------------*\
2  ========= |
3  \\ / F ield | OpenFOAM: The Open Source CFD Toolbox
4  \\ / O peration |
5  \\ / A nd | www.openfoam.com
6  \\/ M anipulation |
7 -------------------------------------------------------------------------------
8  Copyright (C) 2015-2020 OpenCFD Ltd.
9 -------------------------------------------------------------------------------
10 License
11  This file is part of OpenFOAM.
12 
13  OpenFOAM is free software: you can redistribute it and/or modify it
14  under the terms of the GNU General Public License as published by
15  the Free Software Foundation, either version 3 of the License, or
16  (at your option) any later version.
17 
18  OpenFOAM is distributed in the hope that it will be useful, but WITHOUT
19  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
20  FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
21  for more details.
22 
23  You should have received a copy of the GNU General Public License
24  along with OpenFOAM. If not, see <http://www.gnu.org/licenses/>.
25 
26 \*---------------------------------------------------------------------------*/
27 
28 #include "pointNoise.H"
29 #include "argList.H"
31 
32 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
33 
34 namespace Foam
35 {
36 namespace noiseModels
37 {
38 
39 // * * * * * * * * * * * * * * Static Data Members * * * * * * * * * * * * * //
40 
43 
44 // * * * * * * * * * * * * * Protected Member Functions * * * * * * * * * * //
45 
47 (
48  const scalarField& t0,
49  const scalarField& p0,
50  scalarField& t,
51  scalarField& p
52 ) const
53 {
54  DynamicList<scalar> tf(t0.size());
55  DynamicList<scalar> pf(t0.size());
56 
57  forAll(t0, timeI)
58  {
59  if (t0[timeI] >= startTime_)
60  {
61  tf.append(t0[timeI]);
62  pf.append(p0[timeI]);
63  }
64  }
65 
66  t.transfer(tf);
67  p.transfer(pf);
68 }
69 
70 
72 (
73  const label dataseti,
75 )
76 {
77  Info<< "Reading data file " << data.fName() << endl;
78 
79  const word fNameBase(data.fName().nameLessExt());
80 
81  // Time and pressure history data
82  scalarField t, p;
83  filterTimeData(data.x(), data.y(), t, p);
84 
85  // Apply conversions
86  p *= rhoRef_;
87  p -= average(p);
88 
89  Info<< " read " << t.size() << " values" << nl << endl;
90 
91  Info<< "Creating noise FFT" << endl;
92 
93  const scalar deltaT = checkUniformTimeStep(t);
94 
95  if (!validateBounds(p))
96  {
97  Info<< "No noise data generated" << endl;
98  return;
99  }
100 
101 
102  // Determine the windowing
103  windowModelPtr_->validate(t.size());
104  const windowModel& win = windowModelPtr_();
105  const scalar deltaf = 1.0/(deltaT*win.nSamples());
106  const fileName outDir(baseFileDir(dataseti)/fNameBase);
107 
108 
109  // Narrow band data
110  // ----------------
111 
112  scalarField f(uniformFrequencies(deltaT));
113 
114  // RMS pressure [Pa]
115  if (writePrmsf_)
116  {
117  graph g
118  (
119  "Prms(f)",
120  "f [Hz]",
121  "Prms(f) [Pa]",
122  f,
123  RMSmeanPf(p)
124  );
125 
126  g.setXRange(fLower_, fUpper_);
127 
128  Info<< " Creating graph for " << g.title() << endl;
129  g.write(outDir, graph::wordify(g.title()), graphFormat_);
130  }
131 
132  // PSD [Pa^2/Hz]
133  const scalarField PSDf(this->PSDf(p, deltaT));
134 
135  if (writePSDf_)
136  {
137  graph g
138  (
139  "PSD(f)",
140  "f [Hz]",
141  "PSD(f) [PaPa_Hz]",
142  f,
143  PSDf
144  );
145 
146  g.setXRange(fLower_, fUpper_);
147 
148  Info<< " Creating graph for " << g.title() << endl;
149  g.write(outDir, graph::wordify(g.title()), graphFormat_);
150  }
151 
152  // PSD [dB/Hz]
153  if (writePSD_)
154  {
155  graph g
156  (
157  "PSD_dB_Hz(f)",
158  "f [Hz]",
159  "PSD(f) [dB_Hz]",
160  f,
161  PSD(PSDf)
162  );
163 
164  g.setXRange(fLower_, fUpper_);
165 
166  Info<< " Creating graph for " << g.title() << endl;
167  g.write(outDir, graph::wordify(g.title()), graphFormat_);
168  }
169 
170  // SPL [dB]
171  if (writeSPL_)
172  {
173  graph g
174  (
175  "SPL_dB(f)",
176  "f [Hz]",
177  "SPL(f) [" + weightingTypeNames_[SPLweighting_] + "]",
178  f,
179  SPL(PSDf*deltaf, f)
180  );
181 
182  g.setXRange(fLower_, fUpper_);
183 
184  Info<< " Creating graph for " << g.title() << endl;
185  g.write(outDir, graph::wordify(g.title()), graphFormat_);
186  }
187 
188  if (writeOctaves_)
189  {
190  labelList octave13BandIDs;
191  scalarField octave13FreqCentre;
192  setOctaveBands
193  (
194  f,
195  fLower_,
196  fUpper_,
197  3,
198  octave13BandIDs,
199  octave13FreqCentre
200  );
201 
202 
203  // 1/3 octave data
204  // ---------------
205 
206  // Integrated PSD = P(rms)^2 [Pa^2]
207  scalarField Prms13f(octaves(PSDf, f, octave13BandIDs));
208 
209  graph g
210  (
211  "SPL13_dB(fm)",
212  "fm [Hz]",
213  "SPL(fm) [" + weightingTypeNames_[SPLweighting_] + "]",
214  octave13FreqCentre,
215  SPL(Prms13f, octave13FreqCentre)
216  );
217 
218  Info<< " Creating graph for " << g.title() << endl;
219  g.write(outDir, graph::wordify(g.title()), graphFormat_);
220  }
221 }
222 
223 
224 // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
225 
227 :
228  noiseModel(dict, false)
229 {
230  if (readFields)
231  {
232  read(dict);
233  }
234 }
235 
236 
237 // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
238 
240 {
241  // Point data only handled by master
242  if (!Pstream::master())
243  {
244  return;
245  }
246 
247  forAll(inputFileNames_, filei)
248  {
249  fileName fName = inputFileNames_[filei];
250  fName.expand();
251 
252  if (!fName.isAbsolute())
253  {
254  fName = argList::envGlobalPath()/fName;
255  }
256 
257  Function1Types::CSV<scalar> data("pressure", dict_, fName);
258  processData(filei, data);
259  }
260 }
261 
262 
264 {
265  if (noiseModel::read(dict))
266  {
267  if (!dict.readIfPresent("files", inputFileNames_))
268  {
270 
271  // Note: lookup uses same keyword as used by the CSV constructor
272  dict.readEntry("file", inputFileNames_.first());
273  }
274 
275  return true;
276  }
277 
278  return false;
279 }
280 
281 
282 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
283 
284 } // End namespace noiseModels
285 } // End namespace Foam
286 
287 // ************************************************************************* //
p
volScalarField & p
Definition: createFieldRefs.H:8
Foam::noiseModel::read
virtual bool read(const dictionary &dict)
Read from dictionary.
Definition: noiseModel.C:538
Foam::graph
Class to create, store and output qgraph files.
Definition: graph.H:61
Foam::noiseModels::pointNoise::pointNoise
pointNoise(const dictionary &dict, const bool readFields=true)
Constructor.
Definition: pointNoise.C:226
Foam::word
A class for handling words, derived from Foam::string.
Definition: word.H:62
Foam::fileName
A class for handling file names.
Definition: fileName.H:69
Foam::noiseModel
Base class for noise models.
Definition: noiseModel.H:169
Foam::argList::envGlobalPath
static fileName envGlobalPath()
Global case (directory) from environment variable.
Definition: argList.C:537
Foam::graph::wordify
static word wordify(const string &sname)
Helper function to convert string name into appropriate word.
Definition: graph.C:48
Foam::DynamicList< scalar >
Foam::Function1Types::CSV
Templated CSV function.
Definition: CSV.H:75
Foam::noiseModels::pointNoise::inputFileNames_
List< fileName > inputFileNames_
Input file names - optional.
Definition: pointNoise.H:104
Foam::UPstream::master
static bool master(const label communicator=worldComm)
Am I the master process.
Definition: UPstream.H:458
Foam::noiseModels::defineTypeNameAndDebug
defineTypeNameAndDebug(pointNoise, 0)
Foam::endl
Ostream & endl(Ostream &os)
Add newline and flush stream.
Definition: Ostream.H:350
pointNoise.H
forAll
#define forAll(list, i)
Loop across all elements in list.
Definition: stdFoam.H:296
Foam::noiseModel::dict_
const dictionary dict_
Copy of dictionary used for construction.
Definition: noiseModel.H:224
Foam::Field< scalar >
Foam::Info
messageStream Info
Information stream (uses stdout - output is on the master only)
argList.H
Foam::noiseModels::pointNoise::read
virtual bool read(const dictionary &dict)
Read from dictionary.
Definition: pointNoise.C:263
Foam::List::resize
void resize(const label newSize)
Adjust allocated size of list.
Definition: ListI.H:139
dict
dictionary dict
Definition: searchingEngine.H:14
Foam::dictionary
A list of keyword definitions, which are a keyword followed by a number of values (eg,...
Definition: dictionary.H:121
addToRunTimeSelectionTable.H
Macros for easy insertion into run-time selection tables.
g
const uniformDimensionedVectorField & g
Definition: createFluidFields.H:24
Foam
Namespace for OpenFOAM.
Definition: atmBoundaryLayer.C:33
Foam::readFields
void readFields(const typename GeoFieldType::Mesh &mesh, const IOobjectList &objects, const wordHashSet &selectedFields, LIFOStack< regIOobject * > &storedObjects)
Read the selected GeometricFields of the templated type.
Definition: ReadFieldsTemplates.C:312
Foam::nl
constexpr char nl
Definition: Ostream.H:385
Foam::noiseModels::pointNoise
Perform noise analysis on point-based pressure data.
Definition: pointNoise.H:94
f
labelList f(nPoints)
Foam::List< label >
Foam::string::expand
string & expand(const bool allowEmpty=false)
Definition: string.C:186
Foam::windowModel::nSamples
label nSamples() const
Return the number of samples in the window.
Definition: windowModel.C:56
Foam::noiseModels::pointNoise::calculate
virtual void calculate()
Calculate.
Definition: pointNoise.C:239
p0
const volScalarField & p0
Definition: EEqn.H:36
Foam::noiseModels::pointNoise::processData
void processData(const label dataseti, const Function1Types::CSV< scalar > &data)
Process the CSV data.
Definition: pointNoise.C:72
Foam::windowModel
Base class for windowing models.
Definition: windowModel.H:52
Foam::noiseModels::pointNoise::filterTimeData
void filterTimeData(const scalarField &t0, const scalarField &p0, scalarField &t, scalarField &p) const
Definition: pointNoise.C:47
Foam::noiseModels::addToRunTimeSelectionTable
addToRunTimeSelectionTable(noiseModel, pointNoise, dictionary)
Foam::data
Database for solution data, solver performance and other reduced data.
Definition: data.H:55
Foam::fileName::isAbsolute
static bool isAbsolute(const std::string &str)
Return true if string starts with a '/'.
Definition: fileNameI.H:136
Foam::average
dimensioned< Type > average(const DimensionedField< Type, GeoMesh > &df)
Definition: DimensionedFieldFunctions.C:328