age.C
Go to the documentation of this file.
1 /*---------------------------------------------------------------------------*\
2  ========= |
3  \\ / F ield | OpenFOAM: The Open Source CFD Toolbox
4  \\ / O peration | Website: https://openfoam.org
5  \\ / A nd | Copyright (C) 2018-2021 OpenFOAM Foundation
6  \\/ M anipulation |
7 -------------------------------------------------------------------------------
8 License
9  This file is part of OpenFOAM.
10 
11  OpenFOAM is free software: you can redistribute it and/or modify it
12  under the terms of the GNU General Public License as published by
13  the Free Software Foundation, either version 3 of the License, or
14  (at your option) any later version.
15 
16  OpenFOAM is distributed in the hope that it will be useful, but WITHOUT
17  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
18  FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
19  for more details.
20 
21  You should have received a copy of the GNU General Public License
22  along with OpenFOAM. If not, see <http://www.gnu.org/licenses/>.
23 
24 \*---------------------------------------------------------------------------*/
25 
26 #include "age.H"
27 #include "fvmDiv.H"
28 #include "fvmLaplacian.H"
29 #include "fvModels.H"
30 #include "fvConstraints.H"
31 #include "momentumTransportModel.H"
33 #include "wallFvPatch.H"
36 
37 // * * * * * * * * * * * * * * Static Data Members * * * * * * * * * * * * * //
38 
39 namespace Foam
40 {
41 namespace functionObjects
42 {
43  defineTypeNameAndDebug(age, 0);
44  addToRunTimeSelectionTable(functionObject, age, dictionary);
45 }
46 }
47 
48 
49 // * * * * * * * * * * * * * Private Member Functions * * * * * * * * * * * //
50 
51 Foam::wordList Foam::functionObjects::age::patchTypes() const
52 {
53  wordList result
54  (
55  mesh_.boundary().size(),
57  );
58 
59  forAll(mesh_.boundary(), patchi)
60  {
61  if (isA<wallFvPatch>(mesh_.boundary()[patchi]))
62  {
64  }
65  }
66 
67  return result;
68 }
69 
70 
71 bool Foam::functionObjects::age::converged
72 (
73  const int nCorr,
74  const scalar initialResidual
75 ) const
76 {
77  if (initialResidual < tolerance_)
78  {
79  Info<< "Field " << typeName
80  << " converged in " << nCorr << " correctors\n" << endl;
81 
82  return true;
83  }
84  else
85  {
86  return false;
87  }
88 }
89 
90 
91 // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
92 
94 (
95  const word& name,
96  const Time& runTime,
97  const dictionary& dict
98 )
99 :
100  fvMeshFunctionObject(name, runTime, dict)
101 {
102  read(dict);
103 }
104 
105 
106 // * * * * * * * * * * * * * * * * Destructor * * * * * * * * * * * * * * * //
107 
109 {}
110 
111 
112 // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
113 
115 {
116  phiName_ = dict.lookupOrDefault<word>("phi", "phi");
117  rhoName_ = dict.lookupOrDefault<word>("rho", "rho");
118  nCorr_ = dict.lookupOrDefault<int>("nCorr", 5);
119  schemesField_ = dict.lookupOrDefault<word>("schemesField", typeName);
120  diffusion_ = dict.lookupOrDefault<Switch>("diffusion", false);
121  tolerance_ = dict.lookupOrDefault<scalar>("tolerance", 1e-5);
122 
123  return true;
124 }
125 
126 
128 {
130  (
131  new volScalarField
132  (
133  IOobject
134  (
135  typeName,
136  mesh_.time().timeName(),
137  mesh_,
140  false
141  ),
142  mesh_,
144  patchTypes()
145  )
146  );
147  volScalarField& age = tage.ref();
148 
149  const word divScheme("div(phi," + schemesField_ + ")");
150 
151  // Set under-relaxation coeff
152  scalar relaxCoeff = 0.0;
153  if (mesh_.relaxEquation(schemesField_))
154  {
155  relaxCoeff = mesh_.equationRelaxationFactor(schemesField_);
156  }
157 
160  (
162  );
163 
164  // This only works because the null constructed inletValue for an
165  // inletOutletFvPatchField is zero. If we needed any other value we would
166  // have to loop over the inletOutlet patches and explicitly set the
167  // inletValues. We would need to change the interface of inletOutlet in
168  // order to do this.
169 
170  const surfaceScalarField& phi =
171  mesh_.lookupObject<surfaceScalarField>(phiName_);
172 
173  if (phi.dimensions() == dimMass/dimTime)
174  {
175  const volScalarField& rho =
176  mesh_.lookupObject<volScalarField>(rhoName_);
177 
178  tmp<volScalarField> tmuEff;
179  word laplacianScheme;
180 
181  if (diffusion_)
182  {
183  tmuEff =
184  mesh_.lookupObject<momentumTransportModel>
185  (
186  momentumTransportModel::typeName
187  ).muEff();
188 
189  laplacianScheme =
190  "laplacian(" + tmuEff().name() + ',' + schemesField_ + ")";
191  }
192 
193  for (int i=0; i<=nCorr_; i++)
194  {
195  fvScalarMatrix ageEqn
196  (
197  fvm::div(phi, age, divScheme) == rho + fvModels.source(rho, age)
198  );
199 
200  if (diffusion_)
201  {
202  ageEqn -= fvm::laplacian(tmuEff(), age, laplacianScheme);
203  }
204 
205  ageEqn.relax(relaxCoeff);
206 
207  fvConstraints.constrain(ageEqn);
208 
209  if (converged(i, ageEqn.solve(schemesField_).initialResidual()))
210  {
211  break;
212  };
213 
214  fvConstraints.constrain(age);
215  }
216  }
217  else
218  {
219  tmp<volScalarField> tnuEff;
220  word laplacianScheme;
221 
222  if (diffusion_)
223  {
224  tnuEff =
225  mesh_.lookupObject<momentumTransportModel>
226  (
227  momentumTransportModel::typeName
228  ).nuEff();
229 
230  laplacianScheme =
231  "laplacian(" + tnuEff().name() + ',' + schemesField_ + ")";
232  }
233 
234  for (int i=0; i<=nCorr_; i++)
235  {
236  fvScalarMatrix ageEqn
237  (
238  fvm::div(phi, age, divScheme)
239  == dimensionedScalar(1) + fvModels.source(age)
240  );
241 
242  if (diffusion_)
243  {
244  ageEqn -= fvm::laplacian(tnuEff(), age, laplacianScheme);
245  }
246 
247  ageEqn.relax(relaxCoeff);
248 
249  fvConstraints.constrain(ageEqn);
250 
251  if (converged(i, ageEqn.solve(schemesField_).initialResidual()))
252  {
253  break;
254  }
255 
256  fvConstraints.constrain(age);
257  }
258  }
259 
260  Info<< "Min/max age:" << min(age).value()
261  << ' ' << max(age).value() << endl;
262 
263  return store(tage);
264 }
265 
266 
268 {
269  return writeObject(typeName);
270 }
271 
272 
273 // ************************************************************************* //
virtual bool execute()
Execute.
Definition: age.C:127
virtual bool read(const dictionary &)
Read the data.
Definition: age.C:114
#define forAll(list, i)
Loop across all elements in list.
Definition: UList.H:434
A list of keyword definitions, which are a keyword followed by any number of values (e...
Definition: dictionary.H:156
dimensioned< Type > max(const dimensioned< Type > &, const dimensioned< Type > &)
static const char *const typeName
Definition: Field.H:105
T & ref() const
Return non-const reference or generate a fatal error.
Definition: tmpI.H:181
age(const word &name, const Time &runTime, const dictionary &dict)
Construct from Time and dictionary.
Definition: age.C:94
addToRunTimeSelectionTable(functionObject, Qdot, dictionary)
wordList patchTypes(nPatches)
Ostream & endl(Ostream &os)
Add newline and flush stream.
Definition: Ostream.H:251
A simple wrapper around bool so that it can be read as a word: true/false, on/off, yes/no, y/n, t/f, or none/any.
Definition: Switch.H:60
Calculate the matrix for the laplacian of the field.
Class to control time during OpenFOAM simulations that is also the top-level objectRegistry.
Definition: Time.H:68
Macros for easy insertion into run-time selection tables.
const dimensionSet dimTime
const dimensionSet & dimensions() const
Return dimensions.
bool read(const char *, int32_t &)
Definition: int32IO.C:85
Foam::fvConstraints & fvConstraints
A class for handling words, derived from string.
Definition: word.H:59
Finite volume models.
Definition: fvModels.H:60
phi
Definition: correctPhi.H:3
A special matrix type and solver, designed for finite volume solutions of scalar equations. Face addressing is used to make all matrix assembly and solution loops vectorise.
Definition: fvPatchField.H:72
tmp< fvMatrix< Type > > div(const surfaceScalarField &flux, const GeometricField< Type, fvPatchField, volMesh > &vf, const word &name)
Definition: fvmDiv.C:46
static autoPtr< dictionary > New(Istream &)
Construct top-level dictionary on freestore from Istream.
Definition: dictionaryIO.C:96
dimensioned< Type > min(const dimensioned< Type > &, const dimensioned< Type > &)
const dimensionSet dimMass
defineTypeNameAndDebug(Qdot, 0)
Calculates and writes out the time taken for a particle to travel from an inlet to the location...
Definition: age.H:129
bool constrain(fvMatrix< Type > &eqn) const
Apply constraints to an equation.
Abstract base class for turbulence models (RAS, LES and laminar).
Foam::fvModels & fvModels
Calculate the matrix for the divergence of the given field and flux.
List< word > wordList
A List of words.
Definition: fileName.H:54
label patchi
virtual ~age()
Destructor.
Definition: age.C:108
T lookupOrDefault(const word &, const T &, bool recursive=false, bool patternMatch=true) const
Find and return a T,.
Finite volume constraints.
Definition: fvConstraints.H:57
dimensioned< scalar > dimensionedScalar
Dimensioned scalar obtained from generic dimensioned type.
tmp< fvMatrix< Type > > laplacian(const GeometricField< Type, fvPatchField, volMesh > &vf, const word &name)
Definition: fvmLaplacian.C:46
messageStream Info
virtual bool write()
Write.
Definition: age.C:267
const doubleScalar e
Elementary charge.
Definition: doubleScalar.H:105
Specialisation of Foam::functionObject for an Foam::fvMesh, providing a reference to the Foam::fvMesh...
A class for managing temporary objects.
Definition: PtrList.H:53
IOobject defines the attributes of an object for which implicit objectRegistry management is supporte...
Definition: IOobject.H:92
Namespace for OpenFOAM.