noiseModel.H
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-2022 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 Class
27  Foam::noiseModel
28 
29 Description
30  Base class for noise models.
31 
32  Data is read from a dictionary, e.g.
33 
34  \verbatim
35  rhoRef 1;
36  N 4096;
37  fl 25;
38  fu 10000;
39  startTime 0;
40 
41  outputPrefix "test1";
42 
43  SPLweighting dBA;
44 
45  // Optional write options dictionary
46  writeOptions
47  {
48  writePrmsf no;
49  writeSPL yes;
50  writePSD yes;
51  writePSDf no;
52  writeOctaves yes;
53  }
54  \endverbatim
55 
56  where
57  \table
58  Property | Description | Required | Default value
59  rhoRef | Reference density | no | 1
60  N | Number of samples in sampling window | no | 65536 (2^16)
61  fl | Lower frequency bounds | no | 25
62  fu | Upper frequency bounds | no | 10000
63  startTime | Start time | no | 0
64  outputPrefix | Prefix applied to output files| no | ''
65  SPLweighting | Weighting: dBA, dBB, dBC, DBD | no | none
66  dBRef | Reference for dB calculation | no | 2e-5
67  writePrmsf | Write Prmsf data | no | yes
68  writeSPL | Write SPL data | no | yes
69  writePSD | Write PSD data | no | yes
70  writePSDf | Write PSDf data | no | yes
71  writeOctaves | Write octaves data | no | yes
72  \endtable
73 
74 SourceFiles
75  noiseModel.C
76 
77 \*---------------------------------------------------------------------------*/
78 
79 #ifndef Foam_noiseModel_H
80 #define Foam_noiseModel_H
81 
82 #include "writeFile.H"
83 #include "dictionary.H"
84 #include "scalarList.H"
85 #include "instantList.H"
86 #include "windowModel.H"
87 #include "Enum.H"
88 #include "Tuple2.H"
89 #include "runTimeSelectionTables.H"
90 #include <fftw3.h>
91 
92 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
93 
94 namespace Foam
95 {
96 
97 /*---------------------------------------------------------------------------*\
98  Class noiseModel Declaration
99 \*---------------------------------------------------------------------------*/
100 
101 class noiseModel
102 :
103  public functionObjects::writeFile
104 {
105 public:
106 
107  enum class weightingType
108  {
109  none,
110  dBA,
111  dBB,
112  dBC,
113  dBD
114  };
115 
116  static const Enum<weightingType> weightingTypeNames_;
117 
118  //- FFTW planner information
119  // Note: storage uses double for use directly with FFTW
120  struct planInfo
121  {
122  bool active;
123  label windowSize;
124  List<double> in;
125  List<double> out;
126  fftw_plan plan;
127  };
128 
129  //- Octave band information
130  struct octaveBandInfo
131  {
132  label octave;
133 
134  // IDs of bin boundaries in pressure data
136 
137  // Centre frequencies for each bin
139  };
140 
141 
142 protected:
143 
144  // Protected Data
145 
146  //- Copy of dictionary used for construction
147  const dictionary dict_;
148 
149  //- Reference density (to convert from kinematic to static pressure)
150  scalar rhoRef_;
151 
152  //- Number of samples in sampling window, default = 2^16
153  label nSamples_;
154 
155  //- Lower frequency limit, default = 25Hz
156  scalar fLower_;
157 
158  //- Upper frequency limit, default = 10kHz
159  scalar fUpper_;
160 
161  //- Start time, default = 0s
162  scalar startTime_;
163 
164  //- Window model
165  autoPtr<windowModel> windowModelPtr_;
167  //- Weighting
169 
170  //- Reference for dB calculation, default = 2e-5
171  scalar dBRef_;
173 
174  // Data validation
175 
176  //- Min pressure value
177  scalar minPressure_;
178 
179  //- Min pressure value
180  scalar maxPressure_;
182 
183  // Write options
184 
185  //- Output file prefix, default = ''
187 
188  //- Write Prmsf; default = yes
189  bool writePrmsf_;
191  //- Write SPL; default = yes
192  bool writeSPL_;
194  //- Write PSD; default = yes
195  bool writePSD_;
196 
197  //- Write PSDf; default = yes
198  bool writePSDf_;
199 
200  //- Write writeOctaves; default = yes
201  bool writeOctaves_;
203 
204  // FFT
206  //- Plan information for FFTW
207  mutable planInfo planInfo_;
209 
210  // Protected Member Functions
211 
212  //- Check and return uniform time step
213  scalar checkUniformTimeStep
214  (
215  const scalarList& times
216  ) const;
217 
218  //- Return true if all pressure data is within min/max bounds
219  bool validateBounds(const scalarList& p) const;
220 
221  //- Find and return start time index
222  FOAM_DEPRECATED_FOR(2022-07, "instant::findStart() static method")
223  static label findStartTimeIndex
224  (
225  const instantList& allTimes,
226  const scalar startTime
227  )
228  {
229  return instant::findStart(allTimes, startTime);
230  }
231 
232  //- Return the base output directory
233  fileName baseFileDir(const label dataseti) const;
235  //- Write output file header
236  void writeFileHeader
237  (
238  Ostream& os,
239  const string& x,
240  const string& y,
241  const UList<Tuple2<string, token>>& headerValues
242  = UList<Tuple2<string, token>>::null()
243  ) const;
245  // Write frequency-based data to file
247  (
248  Ostream& os,
249  const scalarField& f,
250  const scalarField& fx
251  ) const;
252 
253  //- Create a field of equally spaced frequencies for the current set of
254  //- data - assumes a constant time step
256  (
257  const scalar deltaT,
258  const bool check
259  ) const;
260 
261  //- Return a list of the frequency indices wrt f field that correspond
262  //- to the bands limits for a given octave
263  static void setOctaveBands
264  (
265  const scalarField& f,
266  const scalar fLower,
267  const scalar fUpper,
268  const scalar octave,
269  labelList& fBandIDs,
270  scalarField& fCentre
271  );
273  //- Generate octave data
275  (
276  const scalarField& data,
277  const scalarField& f,
278  const labelUList& freqBandIDs
279  ) const;
281  //- Return the fft of the given pressure data
282  tmp<scalarField> Pf(const scalarField& p) const;
283 
284  //- Return the multi-window mean fft of the complete pressure data [Pa]
286 
287  //- Return the multi-window RMS mean fft of the complete pressure
288  //- data [Pa]
289  tmp<scalarField> RMSmeanPf(const scalarField& p) const;
291  //- Return the multi-window Power Spectral Density (PSD) of the complete
292  //- pressure data [Pa^2/Hz]
293  tmp<scalarField> PSDf(const scalarField& p, const scalar deltaT) const;
294 
296  // Weightings
297 
298  //- A weighting function
299  scalar RAf(const scalar f) const;
301  //- A weighting as gain in dB
302  scalar gainA(const scalar f) const;
303 
304  //- B weighting function
305  scalar RBf(const scalar f) const;
306 
307  //- B weighting as gain in dB
308  scalar gainB(const scalar f) const;
309 
310  //- C weighting function
311  scalar RCf(const scalar f) const;
312 
313  //- C weighting as gain in dB
314  scalar gainC(const scalar f) const;
315 
316  //- D weighting function
317  scalar RDf(const scalar f) const;
318 
319  //- D weighting as gain in dB
320  scalar gainD(const scalar f) const;
321 
322 
323 public:
324 
325  //- Runtime type information
326  TypeName("noiseModel");
327 
328  //- Run time selection table
330  (
331  autoPtr,
332  noiseModel,
333  dictionary,
334  (
335  const dictionary& dict,
336  const objectRegistry& obr
337  ),
338  (dict, obr)
339  );
340 
341 
342  // Generated Methods
343 
344  //- No copy construct
345  noiseModel(const noiseModel&) = delete;
346 
347  //- No copy assignment
348  void operator=(const noiseModel&) = delete;
349 
350 
351  // Constructors
352 
353  //- Constructor
354  noiseModel
355  (
356  const dictionary& dict,
357  const objectRegistry& obr,
358  const word& name,
359  const bool readFields = true
360  );
361 
362  //- Selector
363  static autoPtr<noiseModel> New
364  (
365  const dictionary& dict,
366  const objectRegistry& obr
367  );
368 
369 
370  //- Destructor
371  virtual ~noiseModel() = default;
372 
373 
374  // Public Member Functions
375 
376  //- Read from dictionary
377  virtual bool read(const dictionary& dict);
378 
379  //- Abstract call to calculate
380  virtual void calculate() = 0;
381 
382  //- PSD [dB/Hz]
384 
385  //- SPL [dB]
387  (
388  const scalarField& Prms2,
389  const scalar f
390  ) const;
391 
392  //- SPL [dB]
394  (
395  const scalarField& Prms2,
396  const scalarField& f
397  ) const;
398 
399  //- Clean up the FFTW
400  void cleanFFTW();
401 
402  //- Helper function to check weightings
403  void writeWeightings() const;
404 };
405 
406 
407 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
408 
409 } // End namespace Foam
410 
411 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
412 
413 #endif
414 
415 // ************************************************************************* //
static autoPtr< noiseModel > New(const dictionary &dict, const objectRegistry &obr)
Selector.
Definition: noiseModelNew.C:26
void cleanFFTW()
Clean up the FFTW.
Definition: noiseModel.C:850
dictionary dict
tmp< scalarField > meanPf(const scalarField &p) const
Return the multi-window mean fft of the complete pressure data [Pa].
Definition: noiseModel.C:421
scalar gainD(const scalar f) const
D weighting as gain in dB.
Definition: noiseModel.C:586
A class for handling file names.
Definition: fileName.H:71
A list of keyword definitions, which are a keyword followed by a number of values (eg...
Definition: dictionary.H:120
A 2-tuple for storing two objects of dissimilar types. The container is similar in purpose to std::pa...
Definition: stringOps.H:55
autoPtr< windowModel > windowModelPtr_
Window model.
Definition: noiseModel.H:249
bool writeOctaves_
Write writeOctaves; default = yes.
Definition: noiseModel.H:305
scalar gainC(const scalar f) const
C weighting as gain in dB.
Definition: noiseModel.C:562
tmp< scalarField > Pf(const scalarField &p) const
Return the fft of the given pressure data.
Definition: noiseModel.C:378
virtual ~noiseModel()=default
Destructor.
scalar dBRef_
Reference for dB calculation, default = 2e-5.
Definition: noiseModel.H:259
scalar RBf(const scalar f) const
B weighting function.
Definition: noiseModel.C:524
bool validateBounds(const scalarList &p) const
Return true if all pressure data is within min/max bounds.
Definition: noiseModel.C:211
static label findStart(const UList< instant > &times, const scalar timeVal)
Find and return index of given start time (linear search)
Definition: instant.C:37
bool writePrmsf_
Write Prmsf; default = yes.
Definition: noiseModel.H:285
fileName outputPrefix_
Output file prefix, default = &#39;&#39;.
Definition: noiseModel.H:280
label nSamples_
Number of samples in sampling window, default = 2^16.
Definition: noiseModel.H:229
scalar startTime_
Start time, default = 0s.
Definition: noiseModel.H:244
scalar rhoRef_
Reference density (to convert from kinematic to static pressure)
Definition: noiseModel.H:224
class FOAM_DEPRECATED_FOR(2017-05, "Foam::Enum") NamedEnum
Definition: NamedEnum.H:65
scalar fUpper_
Upper frequency limit, default = 10kHz.
Definition: noiseModel.H:239
scalar checkUniformTimeStep(const scalarList &times) const
Check and return uniform time step.
Definition: noiseModel.C:177
scalar y
virtual bool read(const dictionary &dict)
Read from dictionary.
Definition: noiseModel.C:643
word name(const expressions::valueTypeCode typeCode)
A word representation of a valueTypeCode. Empty for INVALID.
Definition: exprTraits.C:52
virtual void calculate()=0
Abstract call to calculate.
tmp< scalarField > octaves(const scalarField &data, const scalarField &f, const labelUList &freqBandIDs) const
Generate octave data.
Definition: noiseModel.C:327
noiseModel(const noiseModel &)=delete
No copy construct.
void writeFileHeader(Ostream &os, const string &x, const string &y, const UList< Tuple2< string, token >> &headerValues=UList< Tuple2< string, token >>::null()) const
Write output file header.
Definition: noiseModel.C:245
A class for handling words, derived from Foam::string.
Definition: word.H:63
Field< scalar > scalarField
Specialisation of Field<T> for scalar.
scalar RDf(const scalar f) const
D weighting function.
Definition: noiseModel.C:573
static const Enum< weightingType > weightingTypeNames_
Definition: noiseModel.H:181
tmp< scalarField > PSDf(const scalarField &p, const scalar deltaT) const
Return the multi-window Power Spectral Density (PSD) of the complete pressure data [Pa^2/Hz]...
Definition: noiseModel.C:461
scalar RAf(const scalar f) const
A weighting function.
Definition: noiseModel.C:496
void writeFreqDataToFile(Ostream &os, const scalarField &f, const scalarField &fx) const
Definition: noiseModel.C:273
A 1D vector of objects of type <T>, where the size of the vector is known and can be used for subscri...
Definition: HashTable.H:99
tmp< Foam::scalarField > PSD(const scalarField &PSDf) const
PSD [dB/Hz].
Definition: noiseModel.C:736
bool writeSPL_
Write SPL; default = yes.
Definition: noiseModel.H:290
An Ostream is an abstract base class for all output systems (streams, files, token lists...
Definition: Ostream.H:55
void operator=(const noiseModel &)=delete
No copy assignment.
FFTW planner information.
Definition: noiseModel.H:188
planInfo planInfo_
Plan information for FFTW.
Definition: noiseModel.H:313
OBJstream os(runTime.globalPath()/outputName)
Database for solution data, solver performance and other reduced data.
Definition: data.H:51
weightingType SPLweighting_
Weighting.
Definition: noiseModel.H:254
static void check(const int retVal, const char *what)
labelList f(nPoints)
scalar gainB(const scalar f) const
B weighting as gain in dB.
Definition: noiseModel.C:540
fileName baseFileDir() const
Return the base directory for output.
Definition: writeFile.C:43
scalar minPressure_
Min pressure value.
Definition: noiseModel.H:267
const dictionary dict_
Copy of dictionary used for construction.
Definition: noiseModel.H:219
static void setOctaveBands(const scalarField &f, const scalar fLower, const scalar fUpper, const scalar octave, labelList &fBandIDs, scalarField &fCentre)
Return a list of the frequency indices wrt f field that correspond to the bands limits for a given oc...
Definition: noiseModel.C:48
bool writePSDf_
Write PSDf; default = yes.
Definition: noiseModel.H:300
scalar gainA(const scalar f) const
A weighting as gain in dB.
Definition: noiseModel.C:513
tmp< scalarField > SPL(const scalarField &Prms2, const scalar f) const
SPL [dB].
Definition: noiseModel.C:745
bool writePSD_
Write PSD; default = yes.
Definition: noiseModel.H:295
Pointer management similar to std::unique_ptr, with some additional methods and type checking...
Definition: HashPtrTable.H:48
void writeWeightings() const
Helper function to check weightings.
Definition: noiseModel.C:861
Macros to ease declaration of run-time selection tables.
void readFields(const typename GeoFieldType::Mesh &mesh, const IOobjectList &objects, const wordHashSet &selectedFields, LIFOStack< regIOobject *> &storedObjects)
Read the selected GeometricFields of the templated type.
List< label > labelList
A List of labels.
Definition: List.H:62
volScalarField & p
A class for managing temporary objects.
Definition: HashPtrTable.H:50
Registry of regIOobjects.
scalar maxPressure_
Min pressure value.
Definition: noiseModel.H:272
declareRunTimeSelectionTable(autoPtr, noiseModel, dictionary,(const dictionary &dict, const objectRegistry &obr),(dict, obr))
Run time selection table.
Foam::label startTime
tmp< scalarField > uniformFrequencies(const scalar deltaT, const bool check) const
Create a field of equally spaced frequencies for the current set of data - assumes a constant time st...
Definition: noiseModel.C:290
static label findStartTimeIndex(const instantList &allTimes, const scalar startTime)
Find and return start time index.
Definition: noiseModel.H:336
tmp< scalarField > RMSmeanPf(const scalarField &p) const
Return the multi-window RMS mean fft of the complete pressure data [Pa].
Definition: noiseModel.C:442
Namespace for OpenFOAM.
scalar fLower_
Lower frequency limit, default = 25Hz.
Definition: noiseModel.H:234
scalar RCf(const scalar f) const
C weighting function.
Definition: noiseModel.C:551
TypeName("noiseModel")
Runtime type information.
Base class for noise models.
Definition: noiseModel.H:166