forceCoeffs.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) 2011-2016 OpenFOAM Foundation
9  Copyright (C) 2019-2022 OpenCFD Ltd.
10 -------------------------------------------------------------------------------
11 License
12  This file is part of OpenFOAM.
13 
14  OpenFOAM is free software: you can redistribute it and/or modify it
15  under the terms of the GNU General Public License as published by
16  the Free Software Foundation, either version 3 of the License, or
17  (at your option) any later version.
18 
19  OpenFOAM is distributed in the hope that it will be useful, but WITHOUT
20  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
21  FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
22  for more details.
23 
24  You should have received a copy of the GNU General Public License
25  along with OpenFOAM. If not, see <http://www.gnu.org/licenses/>.
26 
27 Class
28  Foam::functionObjects::forceCoeffs
29 
30 Group
31  grpForcesFunctionObjects
32 
33 Description
34  Computes force and moment coefficients over a given
35  list of patches, and optionally over given porous zones.
36  The following coefficients can be selected and output:
37  \verbatim
38  Cd | Drag coefficient
39  Cs | Side-force coefficient
40  Cl | Lift coefficient
41  CmRoll | Roll-moment coefficient
42  CmPitch | Pitch-moment coefficient
43  CmYaw | Yaw-moment coefficient
44  \endverbatim
45 
46  The force coefficients can also be optionally output
47  in terms of their front and rear axle constituents:
48  \verbatim
49  Cd{f,r} = 0.5*Cd {+,-} CmRoll
50  Cs{f,r} = 0.5*Cs {+,-} CmYaw
51  Cl{f,r} = 0.5*Cl {+,-} CmPitch
52  \endverbatim
53  where \c f and \c r represent front and rear axles, respectively.
54 
55  Force and moment coefficients are output
56  in their total and constituent components:
57  - total force and moment coefficients
58  - pressure contributions
59  - viscous contributions
60  - porous resistance contributions (optional)
61 
62  Force and moment coefficients can be computed and output in:
63  - the global Cartesian coordinate system (default)
64  - a user-defined Cartesian coordinate system
65 
66  Operands:
67  \table
68  Operand | Type | Location
69  input | - | -
70  output file | dat | postProcessing/<FO>/<time>/<file>s
71  output field | volVectorField | <time>/<outField>s
72  \endtable
73 
74  where \c <file>s:
75  \verbatim
76  coefficient.dat | Integrated coefficients over all patches
77  \endverbatim
78 
79  where \c <outField>s:
80  \verbatim
81  <namePrefix>:forceCoeff | Force coefficient field
82  <namePrefix>:momentCoeff | Moment coefficient field
83  \endverbatim
84 
85 Usage
86  Minimal example by using \c system/controlDict.functions:
87  \verbatim
88  forceCoeffs1
89  {
90  // Mandatory entries
91  type forceCoeffs;
92  libs (forces);
93  patches (<wordRes>); // (wall1 "(wall2|wall3)");
94  magUInf <scalar>;
95  lRef <scalar>;
96  Aref <scalar>;
97 
98  // Optional entries
99  coefficients (<wordHashSet>);
100  directForceDensity <bool>;
101  porosity <bool>;
102  writeFields <bool>;
103  useNamePrefix <bool>;
104 
105  // Conditional mandatory entries
106 
107  // Cartesian coordinate system specification when evaluating
108  // force and moment coefficients, either of the below
109 
110  // Define the centre of rotation
111  // with implicit directions e1=(1 0 0) and e3=(0 0 1)
112  CofR (0 0 0); // Centre of rotation
113 
114  // Define local coordinate system by origin + axes
115  origin (0 0 0);
116  e1 (1 0 0);
117  e3 (0 0 1); // (e1, e2) or (e2, e3) or (e3, e1)
118 
119  // General coordinate system specification (always cartesian)
120  coordinateSystem
121  {
122  origin (0 0 0);
123  rotation
124  {
125  type ...;
126  ...
127  }
128  }
129 
130  // Conditional optional entries
131 
132  // if directForceDensity == true
133  fD <word>;
134 
135  // if directForceDensity == false
136  p <word>;
137  U <word>;
138  rho <word>;
139  rhoInf <scalar>; // redundant for incompressible-flow cases
140  pRef <scalar>;
141 
142  // Inherited entries
143  ...
144  }
145  \endverbatim
146 
147  where the entries mean:
148  \table
149  Property | Description | Type | Reqd | Deflt
150  type | Type name: forceCoeffs | word | yes | -
151  libs | Library name: forces | word | yes | -
152  patches | Names of operand patches | wordRes | yes | -
153  coefficients | Names of operand coefficients | wordHashSet | no | -
154  magUInf | Reference velocity magnitude | scalar | yes | -
155  lRef | Reference length scale for moment calculations <!--
156  --> | scalar | yes | -
157  Aref | Reference area | scalar | yes | -
158  directForceDensity | Flag to directly supply force density <!--
159  --> | bool | no | false
160  porosity | Flag to include porosity contributions | bool | no | false
161  writeFields | Flag to write force and moment fields | bool | no | false
162  useNamePrefix | Flag to include prefix for field names | bool | no | false
163  CofR | Centre of rotation | vector | cndtnl | -
164  origin | Origin of coordinate system | vector | cndtnl | -
165  e3 | e3 coordinate axis | vector | cndtnl | -
166  e1 | e1 coordinate axis | vector | cndtnl | -
167  coordinateSystem | Coordinate system specifier | dictionary | cndtnl | -
168  fD | Name of force density field | word | cndtnl-no | fD
169  p | Name of pressure field | word | cndtnl-no | p
170  U | Name of velocity field | word | cndtnl-no | U
171  rho | Name of density field | word | cndtnl-no | rho
172  rhoInf | Value of reference density | scalar | cndtnl-yes | -
173  pRef | Value of reference pressure | scalar | cndtnl-no | 0
174  \endtable
175 
176  Options for the \c coefficients entry:
177  \verbatim
178  Cd | Drag coefficient
179  Cs | Side-force coefficient
180  Cl | Lift coefficient
181  CmRoll | Roll-moment coefficient
182  CmPitch | Pitch-moment coefficient
183  CmYaw | Yaw-moment coefficient
184  Cd(f) | Front-axle drag coefficient
185  Cs(f) | Front-axle side-force coefficient
186  Cl(f) | Front-axle lift coefficient
187  Cd(r) | Rear-axle drag coefficient
188  Cs(r) | Rear-axle side-force coefficient
189  Cl(r) | Rear-axle lift coefficient
190  \endverbatim
191 
192  The inherited entries are elaborated in:
193  - \link functionObject.H \endlink
194  - \link writeFile.H \endlink
195  - \link coordinateSystem.H \endlink
196  - \link forces.H \endlink
197 
198 Note
199  - \c rhoInf is always redundant for incompressible computations.
200  That is, \c rhoInf is always equal to 1 in incompressible
201  computations no matter which input value is assigned to \c rhoInf.
202  The value of \c rhoInf is only used for compressible computations.
203  - \c writeControl and \c writeInterval entries of function
204  object do control when to output force and moment files and fields.
205  - Input for force/moment coefficient directions
206  require an origin and two orthogonal directions where
207  the remaining orthogonal direction is automatically computed.
208  - The default direction relations are shown below:
209 
210  \table
211  Property | Description | Alias | Direction
212  dragDir | Drag direction | e1 | (1 0 0)
213  sideDir | Side force direction | e2 | (0 1 0)
214  liftDir | Lift direction | e3 | (0 0 1)
215  rollAxis | Roll axis | e1 | (1 0 0)
216  pitchAxis | Pitch axis | e2 | (0 1 0)
217  yawAxis | Yaw axis | e3 | (0 0 1)
218  \endtable
219 
220  - If a \c coordinateSystem entry exists, it is taken in favour of \c CofR.
221 
222 SourceFiles
223  forceCoeffs.C
224 
225 \*---------------------------------------------------------------------------*/
226 
227 #ifndef Foam_functionObjects_forceCoeffs_H
228 #define Foam_functionObjects_forceCoeffs_H
229 
230 #include "forces.H"
231 #include "HashSet.H"
232 #include "Enum.H"
233 
234 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
235 
236 namespace Foam
237 {
238 namespace functionObjects
239 {
240 
241 /*---------------------------------------------------------------------------*\
242  Class forceCoeffs Declaration
243 \*---------------------------------------------------------------------------*/
244 
245 class forceCoeffs
246 :
247  public forces
248 {
249 public:
250 
251  // Container for force and moment components
252  class forceComponents
253  {
254  // Private Data
255 
256  //- Pressure force/moment component
257  vector pressure_;
258 
259  //- Viscous force/moment component
260  vector viscous_;
261 
262  //- Internal force/moment component
263  vector internal_;
264 
265 
266  public:
267 
268  // Constructors
269 
270  //- Default construct
272  :
273  pressure_(Zero),
274  viscous_(Zero),
275  internal_(Zero)
276  {}
277 
278 
279  // Member Functions
280 
281  //- Return the sum of the components
282  const vector total() const noexcept
283  {
284  return pressure_ + viscous_ + internal_;
285  }
286 
287  //- Reset the components to zero
288  void reset() noexcept
289  {
290  pressure_ = Zero;
291  viscous_ = Zero;
292  internal_ = Zero;
293  }
294 
295  //- Reset the components to given values
296  void reset
297  (
298  const vector& pressure,
299  const vector& viscous,
300  const vector& internal
301  ) noexcept
302  {
303  pressure_ = pressure;
304  viscous_ = viscous;
305  internal_ = internal;
306  }
307 
308 
309  // Member Operators
310 
311  //- Return components in a given direction
312  vector operator[](const label cmpt) const
313  {
314  return vector(pressure_[cmpt], viscous_[cmpt], internal_[cmpt]);
315  }
316  };
317 
318 
319  //- Coefficients description
320  struct coeffDesc
321  {
322  enum splitType
323  {
324  stFront,
325  stRear,
326  stNone
327  };
328 
329  string desc_;
330  word name_;
331  label c0_;
332  label c1_;
333  bool active_;
335 
336  // Constructors
337 
338  //- Construct from components
339  coeffDesc
340  (
341  const string& description,
342  const word& name,
343  const label c0,
344  const label c1 = -1
345  )
346  :
347  desc_(description),
348  name_(name),
349  c0_(c0),
350  c1_(c1),
351  active_(false),
353  {}
354 
355  //- Return name with front-name appendix
356  const word frontName() const noexcept
357  {
358  return name_ + "(f)";
359  }
360 
361  //- Return name with rear-name appendix
362  const word rearName() const noexcept
363  {
364  return name_ + "(r)";
365  }
366 
367  //- Print description
368  void print(Ostream& os) const
369  {
370  os << desc_.c_str() << ": " << name_;
371  }
372 
373  //- Return force/moment components based on the specified split type
374  vector value(const forceComponents& f, const forceComponents& m) const
375  {
376  if (c1_ == -1)
377  {
378  return m[c0_];
379  }
380  else
381  {
382  switch (splitType_)
383  {
384  case stFront:
385  {
386  return 0.5*f[c0_] + m[c1_];
387  }
388  case stRear:
389  {
390  return 0.5*f[c0_] - m[c1_];
391  }
392  case stNone:
393  {
394  return f[c0_];
395  }
396  }
397  }
398 
400  << "Cannot determine value"
401  << abort(FatalError);
402 
403  return vector::zero;
404  }
405 
406  //- Return front-axle coefficient description
407  coeffDesc front() const
408  {
409  coeffDesc coeff(*this);
410  coeff.name_ = coeff.frontName();
411  coeff.desc_ = coeff.desc_ + " front";
412  coeff.splitType_ = stFront;
413  return coeff;
414  }
415 
416  //- Return rear-axle coefficient description
417  coeffDesc rear() const
418  {
419  coeffDesc coeff(*this);
420  coeff.name_ = coeff.rearName();
421  coeff.desc_ = coeff.desc_ + " rear";
422  coeff.splitType_ = stRear;
423  return coeff;
424  }
425  };
426 
427 
428 private:
429 
430  // Private Data
431 
432  //- Force components
434 
435  //- Moment components
436  forceComponents Cm_;
437 
438  //- Operand force and moment coefficients
439  HashTable<coeffDesc> coeffs_;
441 
442  // File streams
443 
444  //- File stream for integrated operand coefficients
445  autoPtr<OFstream> coeffFilePtr_;
446 
447 
448  // Reference scales
449 
450  //- Reference velocity magnitude [m/s]
451  scalar magUInf_;
452 
453  //- Reference length scale [m]
454  scalar lRef_;
455 
456  //- Reference area [m^2]
457  scalar Aref_;
458 
459 
460  //- Flag of initialisation (internal)
461  bool initialised_;
462 
463 
464 protected:
465 
466  // Protected Member Functions
468  //- Initialise containers and fields
469  void initialise();
470 
471  //- Return access to the force coefficients field
473 
474  //- Return access to the moment coefficients field
476 
477 
478  //- Reset containers and fields
479  void reset();
481 
482  // Evaluation
483 
484  //- Return the operand coefficients
486 
487  //- Calculate force coefficients
489 
490  //- Calculate moment coefficients
491  void calcMomentCoeffs();
492 
493  //- Return integrated {total, pressure, viscous, porous} components
494  List<scalar> integrateData(const List<Field<scalar>>& coeff) const;
495 
496 
497  // I-O
498 
499  //- Create the integrated-coefficient file
501 
502  //- Write header to the integrated-coefficient file
504  (
505  const word& header,
506  OFstream& os
507  ) const;
508 
509  //- Write integrated coefficients to the integrated-coefficient file
511 
512 
513 public:
514 
515  //- Runtime type information
516  TypeName("forceCoeffs");
517 
518 
519  // Constructors
520 
521  //- Construct from Time and dictionary
523  (
524  const word& name,
525  const Time& runTime,
526  const dictionary& dict,
527  const bool readFields = true
528  );
529 
530  //- No copy construct
531  forceCoeffs(const forceCoeffs&) = delete;
533  //- No copy assignment
534  void operator=(const forceCoeffs&) = delete;
537  //- Destructor
538  virtual ~forceCoeffs() = default;
541  // Member Functions
542 
543  //- Read the dictionary
544  virtual bool read(const dictionary& dict);
545 
546  //- Execute the function object
547  virtual bool execute();
549  //- Write to data files/fields and to streams
550  virtual bool write();
551 };
552 
553 } // End namespace functionObjects
554 
555 
556 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
557 
558 // Output operator
559 inline Ostream& operator<<
560 (
561  Ostream& os,
563 )
564 {
565  coeff.print(os);
566  return os;
567 }
568 
569 
570 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
571 
572 } // End namespace Foam
573 
574 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
575 
576 #endif
577 
578 // ************************************************************************* //
coeffDesc front() const
Return front-axle coefficient description.
Definition: forceCoeffs.H:625
dictionary dict
void calcForceCoeffs()
Calculate force coefficients.
Definition: forceCoeffs.C:152
TypeName("forceCoeffs")
Runtime type information.
virtual bool write()
Write to data files/fields and to streams.
Definition: forceCoeffs.C:433
error FatalError
Error stream (stdout output on all processes), with additional &#39;FOAM FATAL ERROR&#39; header text and sta...
A list of keyword definitions, which are a keyword followed by a number of values (eg...
Definition: dictionary.H:129
#define FatalErrorInFunction
Report an error message using Foam::FatalError.
Definition: error.H:598
coeffDesc(const string &description, const word &name, const label c0, const label c1=-1)
Construct from components.
Definition: forceCoeffs.H:548
const vector total() const noexcept
Return the sum of the components.
Definition: forceCoeffs.H:480
Output to file stream, using an OSstream.
Definition: OFstream.H:49
engineTime & runTime
void writeIntegratedDataFile()
Write integrated coefficients to the integrated-coefficient file.
Definition: forceCoeffs.C:245
volVectorField & momentCoeff()
Return access to the moment coefficients field.
Definition: forceCoeffs.C:86
HashTable< coeffDesc > selectCoeffs() const
Return the operand coefficients.
Definition: forceCoeffs.C:126
const word frontName() const noexcept
Return name with front-name appendix.
Definition: forceCoeffs.H:566
Class to control time during OpenFOAM simulations that is also the top-level objectRegistry.
Definition: Time.H:69
forceCoeffs(const word &name, const Time &runTime, const dictionary &dict, const bool readFields=true)
Construct from Time and dictionary.
Definition: forceCoeffs.C:269
const word & name() const noexcept
Return the name of this functionObject.
vector operator[](const label cmpt) const
Return components in a given direction.
Definition: forceCoeffs.H:516
A class for handling words, derived from Foam::string.
Definition: word.H:63
Vector< scalar > vector
Definition: vector.H:57
void initialise()
Initialise containers and fields.
Definition: forceCoeffs.C:46
A HashTable similar to std::unordered_map.
Definition: HashTable.H:108
errorManip< error > abort(error &err)
Definition: errorManip.H:139
void reset()
Reset containers and fields.
Definition: forceCoeffs.C:115
coeffDesc rear() const
Return rear-axle coefficient description.
Definition: forceCoeffs.H:637
void calcMomentCoeffs()
Calculate moment coefficients.
Definition: forceCoeffs.C:176
An Ostream is an abstract base class for all output systems (streams, files, token lists...
Definition: Ostream.H:56
const direction noexcept
Definition: Scalar.H:258
virtual bool read(const dictionary &dict)
Read the dictionary.
Definition: forceCoeffs.C:296
const word rearName() const noexcept
Return name with rear-name appendix.
Definition: forceCoeffs.H:574
void operator=(const forceCoeffs &)=delete
No copy assignment.
void reset() noexcept
Reset the components to zero.
Definition: forceCoeffs.H:488
Reads fields from the time directories and adds them to the mesh database for further post-processing...
Definition: readFields.H:151
List< scalar > integrateData(const List< Field< scalar >> &coeff) const
Return integrated {total, pressure, viscous, porous} components.
OBJstream os(runTime.globalPath()/outputName)
virtual void print(Ostream &os) const
Print stream description to Ostream.
Definition: IOstream.C:67
volVectorField & forceCoeff()
Return access to the force coefficients field.
Definition: forceCoeffs.C:57
labelList f(nPoints)
void print(Ostream &os) const
Print description.
Definition: forceCoeffs.H:582
Computes force and moment coefficients over a given list of patches, and optionally over given porous...
Definition: forceCoeffs.H:433
void writeIntegratedDataFileHeader(const word &header, OFstream &os) const
Write header to the integrated-coefficient file.
Definition: forceCoeffs.C:211
const dimensionedScalar c1
First radiation constant: default SI units: [W/m2].
Pointer management similar to std::unique_ptr, with some additional methods and type checking...
Definition: HashPtrTable.H:48
virtual ~forceCoeffs()=default
Destructor.
vector value(const forceComponents &f, const forceComponents &m) const
Return force/moment components based on the specified split type.
Definition: forceCoeffs.H:590
void createIntegratedDataFile()
Create the integrated-coefficient file.
Definition: forceCoeffs.C:200
virtual bool execute()
Execute the function object.
Definition: forceCoeffs.C:377
Namespace for OpenFOAM.
static constexpr const zero Zero
Global zero (0)
Definition: zero.H:127