MRFZone.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 | Copyright (C) 2011-2016 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 "MRFZone.H"
27 #include "fvMesh.H"
28 #include "volFields.H"
29 #include "surfaceFields.H"
30 #include "fvMatrices.H"
31 #include "faceSet.H"
32 #include "geometricOneField.H"
33 #include "syncTools.H"
34 
35 // * * * * * * * * * * * * * * Static Data Members * * * * * * * * * * * * * //
36 
37 namespace Foam
38 {
39  defineTypeNameAndDebug(MRFZone, 0);
40 }
41 
42 
43 // * * * * * * * * * * * * * Private Member Functions * * * * * * * * * * * //
44 
45 void Foam::MRFZone::setMRFFaces()
46 {
47  const polyBoundaryMesh& patches = mesh_.boundaryMesh();
48 
49  // Type per face:
50  // 0:not in zone
51  // 1:moving with frame
52  // 2:other
53  labelList faceType(mesh_.nFaces(), 0);
54 
55  // Determine faces in cell zone
56  // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~
57  // (without constructing cells)
58 
59  const labelList& own = mesh_.faceOwner();
60  const labelList& nei = mesh_.faceNeighbour();
61 
62  // Cells in zone
63  boolList zoneCell(mesh_.nCells(), false);
64 
65  if (cellZoneID_ != -1)
66  {
67  const labelList& cellLabels = mesh_.cellZones()[cellZoneID_];
68  forAll(cellLabels, i)
69  {
70  zoneCell[cellLabels[i]] = true;
71  }
72  }
73 
74 
75  label nZoneFaces = 0;
76 
77  for (label facei = 0; facei < mesh_.nInternalFaces(); facei++)
78  {
79  if (zoneCell[own[facei]] || zoneCell[nei[facei]])
80  {
81  faceType[facei] = 1;
82  nZoneFaces++;
83  }
84  }
85 
86 
87  labelHashSet excludedPatches(excludedPatchLabels_);
88 
89  forAll(patches, patchi)
90  {
91  const polyPatch& pp = patches[patchi];
92 
93  if (pp.coupled() || excludedPatches.found(patchi))
94  {
95  forAll(pp, i)
96  {
97  label facei = pp.start()+i;
98 
99  if (zoneCell[own[facei]])
100  {
101  faceType[facei] = 2;
102  nZoneFaces++;
103  }
104  }
105  }
106  else if (!isA<emptyPolyPatch>(pp))
107  {
108  forAll(pp, i)
109  {
110  label facei = pp.start()+i;
111 
112  if (zoneCell[own[facei]])
113  {
114  faceType[facei] = 1;
115  nZoneFaces++;
116  }
117  }
118  }
119  }
120 
121  // Synchronize the faceType across processor patches
122  syncTools::syncFaceList(mesh_, faceType, maxEqOp<label>());
123 
124  // Now we have for faceType:
125  // 0 : face not in cellZone
126  // 1 : internal face or normal patch face
127  // 2 : coupled patch face or excluded patch face
128 
129  // Sort into lists per patch.
130 
131  internalFaces_.setSize(mesh_.nFaces());
132  label nInternal = 0;
133 
134  for (label facei = 0; facei < mesh_.nInternalFaces(); facei++)
135  {
136  if (faceType[facei] == 1)
137  {
138  internalFaces_[nInternal++] = facei;
139  }
140  }
141  internalFaces_.setSize(nInternal);
142 
143  labelList nIncludedFaces(patches.size(), 0);
144  labelList nExcludedFaces(patches.size(), 0);
145 
146  forAll(patches, patchi)
147  {
148  const polyPatch& pp = patches[patchi];
149 
150  forAll(pp, patchFacei)
151  {
152  label facei = pp.start() + patchFacei;
153 
154  if (faceType[facei] == 1)
155  {
156  nIncludedFaces[patchi]++;
157  }
158  else if (faceType[facei] == 2)
159  {
160  nExcludedFaces[patchi]++;
161  }
162  }
163  }
164 
165  includedFaces_.setSize(patches.size());
166  excludedFaces_.setSize(patches.size());
167  forAll(nIncludedFaces, patchi)
168  {
169  includedFaces_[patchi].setSize(nIncludedFaces[patchi]);
170  excludedFaces_[patchi].setSize(nExcludedFaces[patchi]);
171  }
172  nIncludedFaces = 0;
173  nExcludedFaces = 0;
174 
175  forAll(patches, patchi)
176  {
177  const polyPatch& pp = patches[patchi];
178 
179  forAll(pp, patchFacei)
180  {
181  label facei = pp.start() + patchFacei;
182 
183  if (faceType[facei] == 1)
184  {
185  includedFaces_[patchi][nIncludedFaces[patchi]++] = patchFacei;
186  }
187  else if (faceType[facei] == 2)
188  {
189  excludedFaces_[patchi][nExcludedFaces[patchi]++] = patchFacei;
190  }
191  }
192  }
193 
194 
195  if (debug)
196  {
197  faceSet internalFaces(mesh_, "internalFaces", internalFaces_);
198  Pout<< "Writing " << internalFaces.size()
199  << " internal faces in MRF zone to faceSet "
200  << internalFaces.name() << endl;
201  internalFaces.write();
202 
203  faceSet MRFFaces(mesh_, "includedFaces", 100);
204  forAll(includedFaces_, patchi)
205  {
206  forAll(includedFaces_[patchi], i)
207  {
208  label patchFacei = includedFaces_[patchi][i];
209  MRFFaces.insert(patches[patchi].start()+patchFacei);
210  }
211  }
212  Pout<< "Writing " << MRFFaces.size()
213  << " patch faces in MRF zone to faceSet "
214  << MRFFaces.name() << endl;
215  MRFFaces.write();
216 
217  faceSet excludedFaces(mesh_, "excludedFaces", 100);
218  forAll(excludedFaces_, patchi)
219  {
220  forAll(excludedFaces_[patchi], i)
221  {
222  label patchFacei = excludedFaces_[patchi][i];
223  excludedFaces.insert(patches[patchi].start()+patchFacei);
224  }
225  }
226  Pout<< "Writing " << excludedFaces.size()
227  << " faces in MRF zone with special handling to faceSet "
228  << excludedFaces.name() << endl;
229  excludedFaces.write();
230  }
231 }
232 
233 
234 // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
235 
236 Foam::MRFZone::MRFZone
237 (
238  const word& name,
239  const fvMesh& mesh,
240  const dictionary& dict,
241  const word& cellZoneName
242 )
243 :
244  mesh_(mesh),
245  name_(name),
246  coeffs_(dict),
247  active_(coeffs_.lookupOrDefault("active", true)),
248  cellZoneName_(cellZoneName),
249  cellZoneID_(),
250  excludedPatchNames_
251  (
252  wordReList(coeffs_.lookupOrDefault("nonRotatingPatches", wordReList()))
253  ),
254  origin_(coeffs_.lookup("origin")),
255  axis_(coeffs_.lookup("axis")),
256  omega_(Function1<scalar>::New("omega", coeffs_))
257 {
258  if (cellZoneName_ == word::null)
259  {
260  coeffs_.lookup("cellZone") >> cellZoneName_;
261  }
262 
263  if (!active_)
264  {
265  cellZoneID_ = -1;
266  }
267  else
268  {
269  cellZoneID_ = mesh_.cellZones().findZoneID(cellZoneName_);
270 
271  axis_ = axis_/mag(axis_);
272 
273  const labelHashSet excludedPatchSet
274  (
275  mesh_.boundaryMesh().patchSet(excludedPatchNames_)
276  );
277 
278  excludedPatchLabels_.setSize(excludedPatchSet.size());
279 
280  label i = 0;
281  forAllConstIter(labelHashSet, excludedPatchSet, iter)
282  {
283  excludedPatchLabels_[i++] = iter.key();
284  }
285 
286  bool cellZoneFound = (cellZoneID_ != -1);
287 
288  reduce(cellZoneFound, orOp<bool>());
289 
290  if (!cellZoneFound)
291  {
293  << "cannot find MRF cellZone " << cellZoneName_
294  << exit(FatalError);
295  }
296 
297  setMRFFaces();
298  }
299 }
300 
301 
302 // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
303 
305 {
306  return omega_->value(mesh_.time().timeOutputValue())*axis_;
307 }
308 
309 
311 (
312  const volVectorField& U,
313  volVectorField& ddtU
314 ) const
315 {
316  if (cellZoneID_ == -1)
317  {
318  return;
319  }
320 
321  const labelList& cells = mesh_.cellZones()[cellZoneID_];
322  vectorField& ddtUc = ddtU.primitiveFieldRef();
323  const vectorField& Uc = U;
324 
325  const vector Omega = this->Omega();
326 
327  forAll(cells, i)
328  {
329  label celli = cells[i];
330  ddtUc[celli] += (Omega ^ Uc[celli]);
331  }
332 }
333 
334 
335 void Foam::MRFZone::addCoriolis(fvVectorMatrix& UEqn, const bool rhs) const
336 {
337  if (cellZoneID_ == -1)
338  {
339  return;
340  }
341 
342  const labelList& cells = mesh_.cellZones()[cellZoneID_];
343  const scalarField& V = mesh_.V();
344  vectorField& Usource = UEqn.source();
345  const vectorField& U = UEqn.psi();
346 
347  const vector Omega = this->Omega();
348 
349  if (rhs)
350  {
351  forAll(cells, i)
352  {
353  label celli = cells[i];
354  Usource[celli] += V[celli]*(Omega ^ U[celli]);
355  }
356  }
357  else
358  {
359  forAll(cells, i)
360  {
361  label celli = cells[i];
362  Usource[celli] -= V[celli]*(Omega ^ U[celli]);
363  }
364  }
365 }
366 
367 
369 (
370  const volScalarField& rho,
371  fvVectorMatrix& UEqn,
372  const bool rhs
373 ) const
374 {
375  if (cellZoneID_ == -1)
376  {
377  return;
378  }
379 
380  const labelList& cells = mesh_.cellZones()[cellZoneID_];
381  const scalarField& V = mesh_.V();
382  vectorField& Usource = UEqn.source();
383  const vectorField& U = UEqn.psi();
384 
385  const vector Omega = this->Omega();
386 
387  if (rhs)
388  {
389  forAll(cells, i)
390  {
391  label celli = cells[i];
392  Usource[celli] += V[celli]*rho[celli]*(Omega ^ U[celli]);
393  }
394  }
395  else
396  {
397  forAll(cells, i)
398  {
399  label celli = cells[i];
400  Usource[celli] -= V[celli]*rho[celli]*(Omega ^ U[celli]);
401  }
402  }
403 }
404 
405 
407 {
408  const volVectorField& C = mesh_.C();
409 
410  const vector Omega = this->Omega();
411 
412  const labelList& cells = mesh_.cellZones()[cellZoneID_];
413 
414  forAll(cells, i)
415  {
416  label celli = cells[i];
417  U[celli] -= (Omega ^ (C[celli] - origin_));
418  }
419 
420  // Included patches
421 
422  volVectorField::Boundary& Ubf = U.boundaryFieldRef();
423 
424  forAll(includedFaces_, patchi)
425  {
426  forAll(includedFaces_[patchi], i)
427  {
428  label patchFacei = includedFaces_[patchi][i];
429  Ubf[patchi][patchFacei] = Zero;
430  }
431  }
432 
433  // Excluded patches
434  forAll(excludedFaces_, patchi)
435  {
436  forAll(excludedFaces_[patchi], i)
437  {
438  label patchFacei = excludedFaces_[patchi][i];
439  Ubf[patchi][patchFacei] -=
440  (Omega
441  ^ (C.boundaryField()[patchi][patchFacei] - origin_));
442  }
443  }
444 }
445 
446 
448 {
449  makeRelativeRhoFlux(geometricOneField(), phi);
450 }
451 
452 
454 {
455  makeRelativeRhoFlux(oneFieldField(), phi);
456 }
457 
458 
459 void Foam::MRFZone::makeRelative(Field<scalar>& phi, const label patchi) const
460 {
461  makeRelativeRhoFlux(oneField(), phi, patchi);
462 }
463 
464 
466 (
467  const surfaceScalarField& rho,
468  surfaceScalarField& phi
469 ) const
470 {
471  makeRelativeRhoFlux(rho, phi);
472 }
473 
474 
476 {
477  const volVectorField& C = mesh_.C();
478 
479  const vector Omega = this->Omega();
480 
481  const labelList& cells = mesh_.cellZones()[cellZoneID_];
482 
483  forAll(cells, i)
484  {
485  label celli = cells[i];
486  U[celli] += (Omega ^ (C[celli] - origin_));
487  }
488 
489  // Included patches
490  volVectorField::Boundary& Ubf = U.boundaryFieldRef();
491 
492  forAll(includedFaces_, patchi)
493  {
494  forAll(includedFaces_[patchi], i)
495  {
496  label patchFacei = includedFaces_[patchi][i];
497  Ubf[patchi][patchFacei] =
498  (Omega ^ (C.boundaryField()[patchi][patchFacei] - origin_));
499  }
500  }
501 
502  // Excluded patches
503  forAll(excludedFaces_, patchi)
504  {
505  forAll(excludedFaces_[patchi], i)
506  {
507  label patchFacei = excludedFaces_[patchi][i];
508  Ubf[patchi][patchFacei] +=
509  (Omega ^ (C.boundaryField()[patchi][patchFacei] - origin_));
510  }
511  }
512 }
513 
514 
516 {
517  makeAbsoluteRhoFlux(geometricOneField(), phi);
518 }
519 
520 
522 (
523  const surfaceScalarField& rho,
524  surfaceScalarField& phi
525 ) const
526 {
527  makeAbsoluteRhoFlux(rho, phi);
528 }
529 
530 
532 {
533  const vector Omega = this->Omega();
534 
535  // Included patches
536  volVectorField::Boundary& Ubf = U.boundaryFieldRef();
537 
538  forAll(includedFaces_, patchi)
539  {
540  const vectorField& patchC = mesh_.Cf().boundaryField()[patchi];
541 
542  vectorField pfld(Ubf[patchi]);
543 
544  forAll(includedFaces_[patchi], i)
545  {
546  label patchFacei = includedFaces_[patchi][i];
547 
548  pfld[patchFacei] = (Omega ^ (patchC[patchFacei] - origin_));
549  }
550 
551  Ubf[patchi] == pfld;
552  }
553 }
554 
555 
557 {
558  os << nl;
559  os.write(name_) << nl;
560  os << token::BEGIN_BLOCK << incrIndent << nl;
561  os.writeKeyword("active") << active_ << token::END_STATEMENT << nl;
562  os.writeKeyword("cellZone") << cellZoneName_ << token::END_STATEMENT << nl;
563  os.writeKeyword("origin") << origin_ << token::END_STATEMENT << nl;
564  os.writeKeyword("axis") << axis_ << token::END_STATEMENT << nl;
565  omega_->writeData(os);
566 
567  if (excludedPatchNames_.size())
568  {
569  os.writeKeyword("nonRotatingPatches") << excludedPatchNames_
570  << token::END_STATEMENT << nl;
571  }
572 
573  os << decrIndent << token::END_BLOCK << nl;
574 }
575 
576 
578 {
579  coeffs_ = dict;
580 
581  active_ = coeffs_.lookupOrDefault("active", true);
582  coeffs_.lookup("cellZone") >> cellZoneName_;
583  cellZoneID_ = mesh_.cellZones().findZoneID(cellZoneName_);
584 
585  return true;
586 }
587 
588 
589 // ************************************************************************* //
bool read(const dictionary &dict)
Read MRF dictionary.
Definition: MRFZone.C:577
Foam::surfaceFields.
Top level data entry class for use in dictionaries. Provides a mechanism to specify a variable as a c...
Definition: Function1.H:53
dictionary dict
Graphite solid properties.
Definition: C.H:57
#define forAll(list, i)
Loop across all elements in list.
Definition: UList.H:428
void correctBoundaryVelocity(volVectorField &U) const
Correct the boundary velocity for the rotation of the MRF region.
Definition: MRFZone.C:531
U
Definition: pEqn.H:83
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
errorManipArg< error, int > exit(error &err, const int errNo=1)
Definition: errorManip.H:124
const GeometricField< Type, fvPatchField, volMesh > & psi() const
Definition: fvMatrix.H:281
error FatalError
A list of keyword definitions, which are a keyword followed by any number of values (e...
Definition: dictionary.H:137
#define FatalErrorInFunction
Report an error message using Foam::FatalError.
Definition: error.H:319
Ostream & endl(Ostream &os)
Add newline and flush stream.
Definition: Ostream.H:253
vector Omega() const
Return the current Omega vector.
Definition: MRFZone.C:304
A class representing the concept of a field of oneFields used to avoid unnecessary manipulations for ...
Definition: oneFieldField.H:50
void makeAbsolute(volVectorField &U) const
Make the given relative velocity absolute within the MRF region.
Definition: MRFZone.C:475
Generic field type.
Definition: FieldField.H:51
List< bool > boolList
Bool container classes.
Definition: boolList.H:50
HashSet< label, Hash< label > > labelHashSet
A HashSet with label keys.
Definition: HashSet.H:210
C()
Construct null.
Definition: C.C:41
A class representing the concept of a GeometricField of 1 used to avoid unnecessary manipulations for...
const cellShapeList & cells
const Boundary & boundaryField() const
Return const-reference to the boundary field.
A class for handling words, derived from string.
Definition: word.H:59
virtual const fileName & name() const
Return the name of the stream.
Definition: OSstream.H:88
static const word null
An empty word.
Definition: word.H:77
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:71
List< label > labelList
A List of labels.
Definition: labelList.H:56
static const zero Zero
Definition: zero.H:91
forAllConstIter(PtrDictionary< phaseModel >, mixture.phases(), phase)
Definition: pEqn.H:29
An Ostream is an abstract base class for all output systems (streams, files, token lists...
Definition: Ostream.H:53
prefixOSstream Pout(cout,"Pout")
Definition: IOstreams.H:53
Internal::FieldType & primitiveFieldRef()
Return a reference to the internal field.
static const char nl
Definition: Ostream.H:262
Field< Type > & source()
Definition: fvMatrix.H:291
void addCoriolis(const volVectorField &U, volVectorField &ddtU) const
Add the Coriolis force contribution to the acceleration field.
Definition: MRFZone.C:311
defineTypeNameAndDebug(combustionModel, 0)
Ostream & decrIndent(Ostream &os)
Decrement the indent level.
Definition: Ostream.H:237
Ostream & writeKeyword(const keyType &)
Write the keyword followed by an appropriate indentation.
Definition: Ostream.C:54
void reduce(const List< UPstream::commsStruct > &comms, T &Value, const BinaryOp &bop, const int tag, const label comm)
A class representing the concept of a field of 1 used to avoid unnecessary manipulations for objects ...
Definition: oneField.H:50
static void syncFaceList(const polyMesh &mesh, UList< T > &l, const CombineOp &cop)
Synchronize values on all mesh faces.
Definition: syncTools.H:381
label patchi
void writeData(Ostream &os) const
Write.
Definition: MRFZone.C:556
Boundary & boundaryFieldRef()
Return a reference to the boundary field.
Mesh data needed to do the Finite Volume discretisation.
Definition: fvMesh.H:78
A special matrix type and solver, designed for finite volume solutions of scalar equations.
dimensioned< scalar > mag(const dimensioned< Type > &)
virtual Ostream & write(const token &)=0
Write next token to stream.
List< wordRe > wordReList
A List of wordRe (word or regular expression)
Definition: wordReList.H:50
void makeRelative(volVectorField &U) const
Make the given absolute velocity relative within the MRF region.
Definition: MRFZone.C:406
Ostream & incrIndent(Ostream &os)
Increment the indent level.
Definition: Ostream.H:230
T lookupOrDefault(const word &, const T &, bool recursive=false, bool patternMatch=true) const
Find and return a T,.
Namespace for OpenFOAM.