limitTurbulenceViscosity.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) 2023 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 
29 #include "fvMesh.H"
30 #include "transportModel.H"
31 #include "fluidThermo.H"
32 #include "turbulenceModel.H"
33 #include "processorFvPatch.H"
35 
36 // * * * * * * * * * * * * * * Static Data Members * * * * * * * * * * * * * //
37 
38 namespace Foam
39 {
40 namespace fv
41 {
42  defineTypeNameAndDebug(limitTurbulenceViscosity, 0);
44 }
45 }
46 
47 
48 // * * * * * * * * * * * * Protected Member Functions * * * * * * * * * * * //
49 
51 {
52  writeHeaderValue(os, "Nut", nutName_);
53  writeHeaderValue(os, "c", c_);
54  writeCommented(os, "Time");
55  writeTabbed(os, "nLimitedCells_[count]");
56  writeTabbed(os, "nLimitedCells_[%]");
57 
58  os << endl;
59 
60  writtenHeader_ = true;
61 }
62 
63 
65 {
66  const auto* turbPtr =
68  if (turbPtr)
69  {
70  return turbPtr->nu();
71  }
72 
73  const auto* thermoPtr =
74  mesh_.cfindObject<fluidThermo>(fluidThermo::dictName);
75  if (thermoPtr)
76  {
77  return thermoPtr->nu();
78  }
79 
80  const auto* laminarPtr =
81  mesh_.cfindObject<transportModel>("transportProperties");
82  if (laminarPtr)
83  {
84  return laminarPtr->nu();
85  }
86 
87  const auto* dictPtr = mesh_.cfindObject<dictionary>("transportProperties");
88  if (dictPtr)
89  {
90  return
92  (
93  IOobject
94  (
95  "nu",
96  mesh_.time().timeName(),
97  mesh_,
99  ),
100  mesh_,
101  dimensionedScalar("nu", dimViscosity, *dictPtr)
102  );
103  }
104 
106  << "No valid model for laminar viscosity"
107  << exit(FatalError);
108 
109  return nullptr;
110 }
111 
112 
113 // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
114 
116 (
117  const word& name,
118  const word& modelType,
119  const dictionary& dict,
120  const fvMesh& mesh
121 )
122 :
123  fv::cellSetOption(name, modelType, dict, mesh),
124  writeFile(mesh, name, typeName, dict, false),
125  nutName_("nut"),
126  c_(1e5)
127 {
128  if (isActive())
129  {
131  }
132 }
133 
134 
135 // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
136 
138 {
140  {
141  return false;
142  }
143 
144  coeffs_.readIfPresent("nut", nutName_);
145  coeffs_.readIfPresent("c", c_);
146 
147  fieldNames_.resize(1, nutName_);
148 
150 
151  if (canResetFile())
152  {
153  resetFile(typeName);
154  }
155 
156  if (canWriteHeader())
157  {
158  writeFileHeader(file());
159  }
160 
161 
162  return true;
163 }
164 
165 
167 {
168  const auto& tnu = this->nu();
169  const auto& nu = tnu();
170 
171  const label nTotCells(returnReduce(cells_.size(), sumOp<label>()));
172 
173  label nAboveMax = 0;
174  for (const label celli : cells_)
175  {
176  const scalar nutLim = c_*nu[celli];
177 
178  if (nut[celli] > nutLim)
179  {
180  nut[celli] = nutLim;
181  ++nAboveMax;
182  }
183  }
184 
185  reduce(nAboveMax, sumOp<label>());
186 
187  // Percent, max 2 decimal places
188  const auto percent = [](scalar num, label denom) -> scalar
189  {
190  return (denom ? 1e-2*round(1e4*num/denom) : 0);
191  };
192 
193  const scalar nAboveMaxPercent = percent(nAboveMax, nTotCells);
194 
195  Info<< type() << ' ' << name_ << " limited " << nAboveMax << " ("
196  << nAboveMaxPercent << "%) of cells" << endl;
197 
198  if (canWriteToFile())
199  {
200  file()
201  << mesh_.time().timeOutputValue() << token::TAB
202  << nAboveMax << token::TAB
203  << nAboveMaxPercent
204  << endl;
205  }
206 
207 
208  // Handle boundaries in the case of 'all'
210  {
211  const volScalarField::Boundary& nubf = nu.boundaryField();
212  volScalarField::Boundary& nutbf = nut.boundaryFieldRef();
213 
214  for (auto& nutp : nutbf)
215  {
216  // if (!nutp.fixesValue())
217  {
218  const scalarField& nup = nubf[nutp.patch().index()];
219 
220  forAll(nutp, facei)
221  {
222  scalar nutLim = c_*nup[facei];
223  if (nutp[facei] > nutLim)
224  {
225  nutp[facei] = nutLim;
226  }
227  }
228  }
229  }
230  }
231 
232 
233  if (nAboveMax)
234  {
235  // We've changed internal values so give boundary conditions
236  // opportunity to correct
237 
238  // Note: calling nut.correctBoundaryConditions() will re-evaluate wall
239  // functions and potentially (re-)introduce out-of-bounds values
240  //nut.correctBoundaryConditions();
241  if (UPstream::parRun())
242  {
243  nut.boundaryFieldRef().evaluateCoupled<processorFvPatch>();
244  }
245  }
246 }
247 
248 
249 // ************************************************************************* //
word dictName() const
The local dictionary name (final part of scoped name)
Definition: dictionaryI.H:53
dictionary dict
errorManipArg< error, int > exit(error &err, const int errNo=1)
Definition: errorManip.H:125
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
defineTypeNameAndDebug(atmAmbientTurbSource, 0)
#define FatalErrorInFunction
Report an error message using Foam::FatalError.
Definition: error.H:598
Tab [isspace].
Definition: token.H:129
virtual tmp< volScalarField > nu() const =0
Return the laminar viscosity.
const dimensionSet dimViscosity
Ostream & endl(Ostream &os)
Add newline and flush stream.
Definition: Ostream.H:531
static bool & parRun() noexcept
Test if this a parallel run.
Definition: UPstream.H:1049
virtual bool read(const dictionary &dict)
Read source dictionary.
GeometricBoundaryField< scalar, fvPatchField, volMesh > Boundary
Type of boundary fields.
bool writtenHeader_
Flag to identify whether the header has been written.
Definition: writeFile.H:157
T returnReduce(const T &value, const BinaryOp &bop, const int tag=UPstream::msgType(), const label comm=UPstream::worldComm)
Perform reduction on a copy, using specified binary operation.
Abstract base class for turbulence models (RAS, LES and laminar).
Macros for easy insertion into run-time selection tables.
#define forAll(list, i)
Loop across all elements in list.
Definition: stdFoam.H:421
fileName::Type type(const fileName &name, const bool followLink=true)
Return the file type: DIRECTORY or FILE, normally following symbolic links.
Definition: POSIX.C:799
virtual void writeCommented(Ostream &os, const string &str) const
Write a commented string to stream.
Definition: writeFile.C:313
const dimensionedScalar e
Elementary charge.
Definition: createFields.H:11
dynamicFvMesh & mesh
word name(const expressions::valueTypeCode typeCode)
A word representation of a valueTypeCode. Empty for expressions::valueTypeCode::INVALID.
Definition: exprTraits.C:127
virtual bool isActive()
Is the source active?
static const word propertiesName
Default name of the turbulence properties dictionary.
A class for handling words, derived from Foam::string.
Definition: word.H:63
labelList fv(nPoints)
Field< scalar > scalarField
Specialisation of Field<T> for scalar.
Fundamental fluid thermodynamic properties.
Definition: fluidThermo.H:48
void writeFileHeader(Ostream &os)
Write file header information.
limitTurbulenceViscosity(const word &name, const word &modelType, const dictionary &dict, const fvMesh &mesh)
Construct from components.
static tmp< T > New(Args &&... args)
Construct tmp with forwarding arguments.
Definition: tmp.H:206
addToRunTimeSelectionTable(option, atmAmbientTurbSource, dictionary)
void writeHeaderValue(Ostream &os, const string &property, const Type &value) const
Write a (commented) header property and value pair.
An Ostream is an abstract base class for all output systems (streams, files, token lists...
Definition: Ostream.H:56
void read(Istream &, label &val, const dictionary &)
In-place read with dictionary lookup.
OBJstream os(runTime.globalPath()/outputName)
scalar c_
Limiting coefficient [].
tmp< volScalarField > nu() const
Return laminar viscosity.
dimensioned< scalar > dimensionedScalar
Dimensioned scalar obtained from generic dimensioned type.
Mesh data needed to do the Finite Volume discretisation.
Definition: fvMesh.H:78
void resetApplied()
Resize/reset applied flag list for all fieldNames_ entries.
Definition: fvOption.C:41
Nothing to be read.
virtual tmp< volScalarField > nu() const
Kinematic viscosity of mixture [m^2/s].
Definition: fluidThermo.C:96
void reduce(const List< UPstream::commsStruct > &comms, T &value, const BinaryOp &bop, const int tag, const label comm)
Reduce inplace (cf. MPI Allreduce) using specified communication schedule.
messageStream Info
Information stream (stdout output on master, null elsewhere)
Corrects turbulence viscosity field (e.g. nut) within a specified region by applying a maximum limit...
Intermediate abstract class for handling cell-set options for the derived fvOptions.
word nutName_
Name of turbulence viscosity field.
A class for managing temporary objects.
Definition: HashPtrTable.H:50
virtual bool read(const dictionary &dict)
Read dictionary.
bool useSubMesh() const noexcept
True if sub-selection should be used.
volScalarField & nu
virtual void correct(volScalarField &nut)
Correct the energy field.
scalar nut
Namespace for OpenFOAM.
Base abstract class for handling finite volume options (i.e. fvOption).
Definition: fvOption.H:123
virtual void writeTabbed(Ostream &os, const string &str) const
Write a tabbed string to stream.
Definition: writeFile.C:329