momentumError.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) 2020-2021 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 "momentumError.H"
29 #include "fvcDiv.H"
30 #include "fvcGrad.H"
31 #include "fvcLaplacian.H"
32 #include "turbulenceModel.H"
36 
37 // * * * * * * * * * * * * * * Static Data Members * * * * * * * * * * * * * //
38 
39 namespace Foam
40 {
41 namespace functionObjects
42 {
43  defineTypeNameAndDebug(momentumError, 0);
44  addToRunTimeSelectionTable(functionObject, momentumError, dictionary);
45 }
46 }
47 
48 
49 // * * * * * * * * * * * * Protected Member Functions * * * * * * * * * * * //
50 
53 {
54  typedef compressible::turbulenceModel cmpTurbModel;
55  typedef incompressible::turbulenceModel icoTurbModel;
56 
57  const auto& U = lookupObject<volVectorField>(UName_);
59 
60  {
61  auto* turb = findObject<cmpTurbModel>
62  (
64  );
65 
66  if (turb)
67  {
68  tmp<volScalarField> trho = turb->rho();
69  tmp<volScalarField> tnuEff = turb->nuEff();
70 
71  if (zoneSubSetPtr_)
72  {
73  const fvMeshSubset& subsetter = zoneSubSetPtr_->subsetter();
74 
75  tU = subsetter.interpolate(U, false);
76  trho = subsetter.interpolate(turb->rho(), false);
77  tnuEff = subsetter.interpolate(turb->nuEff()(), false);
78  }
79 
81  (
82  "divDevRhoReff",
83  - fvc::div
84  (
85  (trho()*tnuEff())
86  *dev2(T(fvc::grad(tU()))),
87  "div(((rho*nuEff)*dev2(T(grad(U)))))"
88  )
90  (
91  trho()*tnuEff(),
92  tU(),
93  "laplacian(nuEff,U)"
94  )
95  );
96  }
97  }
98 
99  {
100  const auto* turb = findObject<icoTurbModel>
101  (
103  );
104 
105  if (turb)
106  {
107  tmp<volScalarField> tnuEff = turb->nuEff();
108 
109  if (zoneSubSetPtr_)
110  {
111  const fvMeshSubset& subsetter = zoneSubSetPtr_->subsetter();
112 
113  tU = subsetter.interpolate(U, false);
114  tnuEff = subsetter.interpolate(turb->nuEff()(), false);
115  }
116 
118  (
119  "divDevRhoReff",
120  - fvc::div
121  (
122  tnuEff()*dev2(T(fvc::grad(tU()))),
123  "div((nuEff*dev2(T(grad(U)))))"
124  )
125  - fvc::laplacian(tnuEff(), tU(), "laplacian(nuEff,U)")
126  );
127  }
128  }
129 
131 }
132 
133 
134 // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
135 
137 (
138  const word& name,
139  const Time& runTime,
140  const dictionary& dict
141 )
142 :
144  pName_("p"),
145  UName_("U"),
146  phiName_("phi")
147 {
148  read(dict);
149 
150  const auto& phi = lookupObject<surfaceScalarField>(phiName_);
151 
152  const dimensionSet momDims
153  (
154  phi.dimensions()*dimVelocity/dimVolume
155  );
156 
157 
158  volVectorField* momentPtr = nullptr;
159 
160  word momName(scopedName("momentError"));
161 
162  if (zoneSubSetPtr_)
163  {
164  const fvMesh& subMesh = zoneSubSetPtr_->subsetter().subMesh();
165 
166  // momentErrorMap
167 
168  momentPtr = new volVectorField
169  (
170  IOobject
171  (
172  scopedName("momentErrorMap"),
173  subMesh.time().timeName(),
174  subMesh,
178  ),
179  subMesh,
180  dimensionedVector(momDims)
181  );
182 
183  subMesh.objectRegistry::store(momentPtr);
184 
185  momName = scopedName("momentErrorZone");
186  }
187 
188  momentPtr = new volVectorField
189  (
190  IOobject
191  (
192  momName,
193  time_.timeName(),
194  mesh_,
198  ),
199  mesh_,
200  dimensionedVector(momDims)
201  );
203  mesh_.objectRegistry::store(momentPtr);
204 }
205 
206 
207 // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
208 
210 {
212  {
213  Info<< type() << ' ' << name() << ':' << nl;
214 
215  // Optional field name entries
216  if (dict.readIfPresent<word>("p", pName_))
217  {
218  Info<< " p: " << pName_ << endl;
219  }
220  if (dict.readIfPresent<word>("U", UName_))
221  {
222  Info<< " U: " << UName_ << endl;
223  }
224 
225  if (dict.readIfPresent<word>("phi", phiName_))
226  {
227  Info<< " phi: " << phiName_ << endl;
228  }
229 
230  if (dict.found("cellZones"))
231  {
232  zoneSubSetPtr_.reset(new Detail::zoneSubSet(mesh_, dict));
233  }
234  else
235  {
236  zoneSubSetPtr_.reset(nullptr);
237  }
238 
239  return true;
240  }
241 
242  return false;
243 }
244 
245 
247 {
248  const auto& p = lookupObject<volScalarField>(pName_);
249  const auto& U = lookupObject<volVectorField>(UName_);
250  const auto& phi = lookupObject<surfaceScalarField>(phiName_);
251 
252  if (zoneSubSetPtr_)
253  {
254  const fvMeshSubset& subsetter = zoneSubSetPtr_->subsetter();
255 
256  fvMesh& subMesh = zoneSubSetPtr_->subsetter().subMesh();
257 
258  subMesh.fvSchemes::readOpt(mesh_.fvSchemes::readOpt());
259  subMesh.fvSchemes::read();
260 
261  auto& momentErrMap =
263  (
264  scopedName("momentErrorMap")
265  );
266 
267  tmp<volScalarField> tp = subsetter.interpolate(p, false);
268  tmp<volVectorField> tU = subsetter.interpolate(U, false);
269  tmp<surfaceScalarField> tphi = subsetter.interpolate(phi, false);
270 
271  momentErrMap =
272  (
273  divDevRhoReff()
274  + fvc::div(tphi, tU, "div(phi,U)")
275  + fvc::grad(tp, "grad(p)")
276  );
277  }
278  else
279  {
280  auto& momentErr =
281  lookupObjectRef<volVectorField>(scopedName("momentError"));
282 
283  momentErr = fvc::div(phi, U) + fvc::grad(p) + divDevRhoReff();
284  }
285 }
286 
287 
289 {
290  calcMomentError();
291 
292  return true;
293 }
294 
295 
297 {
298  Log << " functionObjects::" << type() << " " << name();
299 
300  if (!zoneSubSetPtr_)
301  {
302  Log << " writing field: " << scopedName("momentError") << endl;
303 
304  const auto& momentErr =
305  lookupObjectRef<volVectorField>(scopedName("momentError"));
306 
307  momentErr.write();
308  }
309  else
310  {
311  Log << " writing field: " << scopedName("momentErrorMap") << endl;
312 
313  const fvMeshSubset& subsetter = zoneSubSetPtr_->subsetter();
314  const fvMesh& subMesh = subsetter.subMesh();
315 
316  const auto& momentErrMap =
318  (
319  scopedName("momentErrorMap")
320  );
321 
322  tmp<volVectorField> mapMomErr =
323  zoneSubSetPtr_->mapToZone<vector>(momentErrMap);
324 
325  mapMomErr().write();
326  }
327 
328  return true;
329 }
330 
331 
332 // ************************************************************************* //
static tmp< DimensionedField< Type, volMesh > > interpolate(const DimensionedField< Type, volMesh > &, const fvMesh &sMesh, const labelUList &cellMap)
Map volume internal (dimensioned) field.
dictionary dict
tmp< GeometricField< typename outerProduct< vector, Type >::type, fvPatchField, volMesh >> grad(const GeometricField< Type, fvsPatchField, surfaceMesh > &ssf)
Definition: fvcGrad.C:47
defineTypeNameAndDebug(ObukhovLength, 0)
const Type & lookupObject(const word &name, const bool recursive=false) const
Lookup and return const reference to the object of the given Type. Fatal if not found or the wrong ty...
Type & lookupObjectRef(const word &name, const bool recursive=false) const
Lookup and return non-const reference to the object of the given Type. Fatal if not found or the wron...
A list of keyword definitions, which are a keyword followed by a number of values (eg...
Definition: dictionary.H:129
tmp< GeometricField< Type, fvPatchField, volMesh > > div(const GeometricField< Type, fvsPatchField, surfaceMesh > &ssf)
Definition: fvcDiv.C:42
constexpr char nl
The newline &#39;\n&#39; character (0x0a)
Definition: Ostream.H:50
word UName_
Name of velocity field.
compressible::turbulenceModel & turb
dimensioned< vector > dimensionedVector
Dimensioned vector obtained from generic dimensioned type.
engineTime & runTime
Ostream & endl(Ostream &os)
Add newline and flush stream.
Definition: Ostream.H:531
tmp< volScalarField > trho
Ignore writing from objectRegistry::writeObject()
Templated wrapper class to provide compressible turbulence models thermal diffusivity based thermal t...
const Time & time() const
Return the top-level database.
Definition: fvMesh.H:360
GeometricField< vector, fvPatchField, volMesh > volVectorField
Definition: volFieldsFwd.H:82
tmp< GeometricField< Type, fvPatchField, volMesh > > laplacian(const GeometricField< Type, fvPatchField, volMesh > &vf, const word &name)
Definition: fvcLaplacian.C:40
Class to control time during OpenFOAM simulations that is also the top-level objectRegistry.
Definition: Time.H:69
momentumError(const word &name, const Time &runTime, const dictionary &dict)
Construct from Time and dictionary.
Macros for easy insertion into run-time selection tables.
const fvMesh & subMesh() const
Return reference to subset mesh.
Definition: fvMeshSubsetI.H:41
Templated abstract base class for single-phase incompressible turbulence models.
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
const dimensionSet dimVolume(pow3(dimLength))
Definition: dimensionSets.H:58
Dimension set for the base types, which can be used to implement rigorous dimension checking for alge...
Definition: dimensionSet.H:105
word name(const expressions::valueTypeCode typeCode)
A word representation of a valueTypeCode. Empty for expressions::valueTypeCode::INVALID.
Definition: exprTraits.C:127
Calculate the gradient of the given field.
static const word propertiesName
Default name of the turbulence properties dictionary.
A class for handling words, derived from Foam::string.
Definition: word.H:63
Calculate the laplacian of the given field.
static tmp< T > New(Args &&... args)
Construct tmp with forwarding arguments.
Definition: tmp.H:206
virtual bool write(const token &tok)=0
Write token to stream or otherwise handle it.
Calculate the divergence of the given field.
tmp< volVectorField > divDevRhoReff()
Return the effective viscous stress (laminar + turbulent).
Definition: momentumError.C:45
A Vector of values with scalar precision, where scalar is float/double depending on the compilation f...
static word timeName(const scalar t, const int precision=precision_)
Return a time name for the given scalar time value formatted with the given precision.
Definition: Time.C:714
addToRunTimeSelectionTable(functionObject, ObukhovLength, dictionary)
Holds a reference to the original mesh (the baseMesh) and optionally to a subset of that mesh (the su...
Definition: fvMeshSubset.H:75
void T(FieldField< Field, Type > &f1, const FieldField< Field, Type > &f2)
static const GeometricField< vector, fvPatchField, volMesh > & null()
Return a null geometric field.
word phiName_
Name of flux field.
void calcMomentError()
Calculate the momentum error.
virtual bool execute()
Execute.
dimensionedSymmTensor dev2(const dimensionedSymmTensor &dt)
U
Definition: pEqn.H:72
Mesh data needed to do the Finite Volume discretisation.
Definition: fvMesh.H:78
Nothing to be read.
#define Log
Definition: PDRblock.C:28
messageStream Info
Information stream (stdout output on master, null elsewhere)
virtual bool read(const dictionary &dict)
Read optional controls.
Specialization of Foam::functionObject for an Foam::fvMesh, providing a reference to the Foam::fvMesh...
volScalarField & p
A class for managing temporary objects.
Definition: HashPtrTable.H:50
Defines the attributes of an object for which implicit objectRegistry management is supported...
Definition: IOobject.H:172
Request registration (bool: true)
word scopedName(const word &name) const
Return a scoped (prefixed) name.
const fvMesh & mesh_
Reference to the fvMesh.
virtual bool read(const dictionary &)
Read the forces data.
Namespace for OpenFOAM.
autoPtr< Detail::zoneSubSet > zoneSubSetPtr_
Sub-set mesh.
const dimensionSet dimVelocity
const Time & time_
Reference to the time database.