meanVelocityForce.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) 2011-2026 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 "meanVelocityForce.H"
27 #include "volFields.H"
28 #include "fvMatrices.H"
29 #include "timeIOdictionary.H"
30 #include "IFstream.H"
32 
33 // * * * * * * * * * * * * * Static Member Functions * * * * * * * * * * * * //
34 
35 namespace Foam
36 {
37 namespace fv
38 {
40 
42  (
46  );
47 }
48 }
49 
50 
51 // * * * * * * * * * * * * * Private Member Functions * * * * * * * * * * * //
52 
53 void Foam::fv::meanVelocityForce::readCoeffs(const dictionary& dict)
54 {
55  UName_ = dict.lookupOrDefault<word>("U", "U");
56  Ubar_ = dict.lookup<vector>("Ubar");
57  relaxation_ = dict.lookupOrDefault<scalar>("relaxation", 1);
58 }
59 
60 
61 void Foam::fv::meanVelocityForce::writeProps
62 (
63  const scalar gradP
64 ) const
65 {
66  // Only write on output time
67  if (mesh().time().writeTime())
68  {
69  timeIOdictionary propsDict
70  (
71  IOobject
72  (
73  name() + "Properties",
74  mesh().time().name(),
75  "uniform",
76  mesh(),
79  )
80  );
81  propsDict.add("gradient", gradP);
82  propsDict.regIOobject::write();
83  }
84 }
85 
86 
87 // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
88 
90 (
91  const word& name,
92  const word& modelType,
93  const fvMesh& mesh,
94  const dictionary& dict
95 )
96 :
97  fvConstraint(name, modelType, mesh, dict),
98  zone_(mesh, coeffs(dict)),
99  UName_(word::null),
100  Ubar_(vector::uniform(NaN)),
101  relaxation_(NaN),
102  gradP0_(0),
103  dGradP_(0),
104  rAPtr_(nullptr)
105 {
106  readCoeffs(coeffs(dict));
107 
108  // Read the initial pressure gradient from file if it exists
109  IFstream propsFile
110  (
111  mesh.time().timePath()/"uniform"/(this->name() + "Properties")
112  );
113  if (propsFile.good())
114  {
115  Info<< indent << "Reading pressure gradient from file" << endl;
117  propsDict.lookup("gradient") >> gradP0_;
118  }
119 
120  Info<< indent << "Initial pressure gradient = " << gradP0_ << endl;
121 }
122 
123 
124 // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
125 
127 {
128  return wordList(1, UName_);
129 }
130 
131 
132 Foam::scalar Foam::fv::meanVelocityForce::magUbarAve
133 (
134  const volVectorField& U
135 ) const
136 {
137  const labelList& cells = zone_.zone();
138  const scalarField& cv = mesh().V();
139 
140  scalar magUbarAve = 0;
141  forAll(cells, i)
142  {
143  const label celli = cells[i];
144  magUbarAve += (normalised(Ubar_) & U[celli])*cv[celli];
145  }
146  reduce(magUbarAve, sumOp<scalar>());
147  magUbarAve /= zone_.V();
148 
149  return magUbarAve;
150 }
151 
152 
154 (
155  fvMatrix<vector>& eqn,
156  const word& fieldName
157 ) const
158 {
159  zone_.regenerate();
160 
162  (
163  IOobject
164  (
165  name() + fieldName + "Sup",
166  mesh().time().name(),
167  mesh(),
170  ),
171  mesh(),
173  );
174 
175  const scalar gradP = gradP0_ + dGradP_;
176 
177  UIndirectList<vector>(Su, zone_.zone()) = normalised(Ubar_)*gradP;
178 
179  eqn -= Su;
180 
181  if (rAPtr_.empty())
182  {
183  rAPtr_.reset
184  (
185  new volScalarField
186  (
187  IOobject
188  (
189  name() + ":rA",
190  mesh().time().name(),
191  mesh(),
194  false
195  ),
196  1/eqn.A()
197  )
198  );
199  }
200  else
201  {
202  rAPtr_() = 1/eqn.A();
203  }
204 
205  gradP0_ += dGradP_;
206  dGradP_ = 0;
207 
208  return true;
209 }
210 
211 
213 {
214  const scalarField& rAU = rAPtr_();
215 
216  const labelList& cells = zone_.zone();
217  const scalarField& cv = mesh().V();
218 
219  // Average rAU over the cell set
220  scalar rAUave = 0;
221  forAll(cells, i)
222  {
223  const label celli = cells[i];
224  rAUave += rAU[celli]*cv[celli];
225  }
226  reduce(rAUave, sumOp<scalar>());
227  rAUave /= zone_.V();
228 
229  const scalar magUbarAve = this->magUbarAve(U);
230 
231  // Calculate the pressure gradient increment needed to adjust the average
232  // flow-rate to the desired value
233  dGradP_ = relaxation_*(mag(Ubar_) - magUbarAve)/rAUave;
234 
235  // Apply correction to velocity field
236  forAll(cells, i)
237  {
238  label celli = cells[i];
239  U[celli] += normalised(Ubar_)*rAU[celli]*dGradP_;
240  }
241 
242  const scalar gradP = gradP0_ + dGradP_;
243 
244  Info<< indent
245  << "Pressure gradient source: uncorrected Ubar = " << magUbarAve
246  << ", pressure gradient = " << gradP << endl;
247 
248  writeProps(gradP);
249 
250  return true;
251 }
252 
253 
255 {
256  zone_.movePoints();
257  return true;
258 }
259 
260 
262 {
263  zone_.topoChange(map);
264 }
265 
266 
268 {
269  zone_.mapMesh(map);
270 }
271 
272 
274 {
275  zone_.distribute(map);
276 }
277 
278 
280 {
282  {
283  zone_.read(coeffs(dict));
284  return true;
285  }
286  else
287  {
288  return false;
289  }
290 }
291 
292 
293 // ************************************************************************* //
#define forAll(list, i)
Loop across all elements in list.
Definition: UList.H:449
Macros for easy insertion into run-time selection tables.
Field with dimensions and associated with geometry type GeoMesh which is used to size the field and a...
Generic GeometricField class.
Input from file stream.
Definition: IFstream.H:85
IOobject defines the attributes of an object for which implicit objectRegistry management is supporte...
Definition: IOobject.H:99
bool good() const
Return true if next operation might succeed.
Definition: Istream.H:101
fileName timePath() const
Return current time path.
Definition: Time.H:286
A List with indirect addressing.
Definition: UIndirectList.H:61
A list of keywords followed by any number of values (e.g. words and numbers) or sub-dictionaries.
Definition: dictionary.H:162
static const dictionary null
Null dictionary.
Definition: dictionary.H:261
Finite volume options abstract base class.
Definition: fvConstraint.H:57
virtual bool read(const dictionary &dict)
Read source dictionary.
Definition: fvConstraint.C:163
const dictionary & coeffs(const dictionary &) const
Return the coefficients sub-dictionary.
Definition: fvConstraintI.H:41
const fvMesh & mesh() const
Return const access to the mesh database.
Definition: fvConstraintI.H:34
A special matrix type and solver, designed for finite volume solutions of scalar equations....
Definition: fvMatrix.H:118
tmp< volScalarField > A() const
Return the central coefficient.
Definition: fvMatrix.C:807
const dimensionSet & dimensions() const
Definition: fvMatrix.H:302
Mesh data needed to do the Finite Volume discretisation.
Definition: fvMesh.H:98
const Time & time() const
Return the top-level database.
Definition: fvMesh.H:433
const DimensionedField< scalar, fvMesh > & V() const
Return cell volumes.
Calculates and applies the force necessary to maintain the specified mean velocity.
virtual bool movePoints()
Update for mesh motion.
virtual bool constrain(fvMatrix< vector > &eqn, const word &fieldName) const
Add the momentum source and set the 1/A coefficient.
virtual void topoChange(const polyTopoChangeMap &)
Update topology using the given map.
virtual void distribute(const polyDistributionMap &)
Redistribute or update using the given distribution map.
virtual bool read(const dictionary &dict)
Read source dictionary.
meanVelocityForce(const word &sourceName, const word &modelType, const fvMesh &mesh, const dictionary &dict)
Construct from explicit source name and mesh.
virtual void mapMesh(const polyMeshMap &)
Update from another mesh using the given map.
virtual wordList constrainedFields() const
Return the list of fields constrained by the fvConstraint.
Class containing mesh-to-mesh mapping information after a mesh distribution where we send parts of me...
Class containing mesh-to-mesh mapping information.
Definition: polyMeshMap.H:51
Class containing mesh-to-mesh mapping information after a change in polyMesh topology.
A class for handling words, derived from string.
Definition: word.H:63
Foam::fvMesh mesh(Foam::IOobject(regionName, runTime.name(), runTime, Foam::IOobject::MUST_READ), false)
A special matrix type and solver, designed for finite volume solutions of scalar equations.
const cellShapeList & cells
volScalarField rAU(1.0/UEqn.A())
U
Definition: pEqn.H:72
const dimensionSet time
addToRunTimeSelectionTable(fvConstraint, bound, dictionary)
defineTypeNameAndDebug(bound, 0)
tmp< VolField< Type > > Su(const VolField< Type > &su, const VolField< Type > &vf)
Definition: fvcSup.C:44
Namespace for OpenFOAM.
static const zero Zero
Definition: zero.H:97
List< word > wordList
A List of words.
Definition: fileName.H:54
intWM_LABEL_SIZE_t label
A label is an int32_t or int64_t as specified by the pre-processor macro WM_LABEL_SIZE.
Definition: label.H:59
Ostream & endl(Ostream &os)
Add newline and flush stream.
Definition: Ostream.H:288
messageStream Info
const dimensionSet & dimVolume
Definition: dimensions.C:150
Vector< scalar > vector
A scalar version of the templated Vector.
Definition: vector.H:49
void reduce(const List< UPstream::commsStruct > &comms, T &Value, const BinaryOp &bop, const int tag, const label comm)
dimensionSet normalised(const dimensionSet &)
Definition: dimensionSet.C:509
dimensioned< vector > dimensionedVector
Dimensioned vector obtained from generic dimensioned type.
word name(const LagrangianState state)
Return a string representation of a Lagrangian state enumeration.
tmp< DimensionedField< scalar, GeoMesh, Field > > mag(const DimensionedField< Type, GeoMesh, PrimitiveField > &df)
Ostream & indent(Ostream &os)
Indent stream.
Definition: Ostream.H:243
labelList fv(nPoints)
dictionary dict
dimensionedVector gradP("gradP", dimensionSet(0, 1, -2, 0, 0), Zero)
IOdictionary propsDict(systemDict("particleTracksDict", args, runTime))