refiner_fvMeshTopoChanger.H
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 Class
25  Foam::fvMeshTopoChangers::refiner
26 
27 Description
28  Dynamic mesh refinement/unrefinement based on volScalarField values.
29 
30  Refinement can optionally be specified in a cellZone or in multiple
31  regions, each controlled by a different volScalarField.
32 
33 Usage
34  Example of single field based refinement in all cells:
35  \verbatim
36  topoChanger
37  {
38  type refiner;
39 
40  libs ("libfvMeshTopoChangers.so");
41 
42  // How often to refine
43  refineInterval 1;
44 
45  // Field to be refinement on
46  field alpha.water;
47 
48  // Refine field in between lower..upper
49  lowerRefineLevel 0.001;
50  upperRefineLevel 0.999;
51 
52  // Have slower than 2:1 refinement
53  nBufferLayers 1;
54 
55  // Refine cells only up to maxRefinement levels
56  maxRefinement 2;
57 
58  // Stop refinement if maxCells reached
59  maxCells 200000;
60 
61  // Flux field and corresponding velocity field. Fluxes on changed
62  // faces get recalculated by interpolating the velocity. Use 'none'
63  // on surfaceScalarFields that do not need to be reinterpolated.
64  correctFluxes
65  (
66  (phi none)
67  (nHatf none)
68  (rhoPhi none)
69  (alphaPhi.water none)
70  (ghf none)
71  );
72 
73  // Write the refinement level as a volScalarField
74  dumpLevel true;
75  }
76  \endverbatim
77 
78  Example of single field based refinement in two regions:
79  \verbatim
80  topoChanger
81  {
82  type refiner;
83 
84  // How often to refine
85  refineInterval 1;
86 
87  refinementRegions
88  {
89  region1
90  {
91  cellZone refinementRegion1;
92 
93  // Field to be refinement on
94  field alpha.water;
95 
96  // Refine field in between lower..upper
97  lowerRefineLevel 0.001;
98  upperRefineLevel 0.999;
99 
100  // Refine cells only up to maxRefinement levels
101  maxRefinement 1;
102 
103  // If value < unrefineLevel unrefine
104  unrefineLevel 10;
105  }
106 
107  region2
108  {
109  cellZone refinementRegion2;
110 
111  // Field to be refinement on
112  field alpha.water;
113 
114  // Refine field in between lower..upper
115  lowerRefineLevel 0.001;
116  upperRefineLevel 0.999;
117 
118  // Refine cells only up to maxRefinement levels
119  maxRefinement 2;
120 
121  // If value < unrefineLevel unrefine
122  unrefineLevel 10;
123  }
124  }
125 
126  // If value < unrefineLevel (default=great) unrefine
127  // unrefineLevel 10;
128  // Have slower than 2:1 refinement
129  nBufferLayers 1;
130 
131  // Stop refinement if maxCells reached
132  maxCells 200000;
133 
134  // Flux field and corresponding velocity field. Fluxes on changed
135  // faces get recalculated by interpolating the velocity. Use 'none'
136  // on surfaceScalarFields that do not need to be reinterpolated.
137  correctFluxes
138  (
139  (phi none)
140  (nHatf none)
141  (rhoPhi none)
142  (alphaPhi.water none)
143  (ghf none)
144  );
145 
146  // Write the refinement level as a volScalarField
147  dumpLevel true;
148  }
149  \endverbatim
150 
151 
152 SourceFiles
153  refiner_fvMeshTopoChanger.C
154 
155 \*---------------------------------------------------------------------------*/
156 
157 #ifndef refiner_fvMeshTopoChanger_H
158 #define refiner_fvMeshTopoChanger_H
159 
160 #include "fvMeshTopoChanger.H"
161 #include "hexRef8.H"
162 #include "PackedBoolList.H"
163 #include "Switch.H"
164 
165 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
166 
167 namespace Foam
168 {
169 namespace fvMeshTopoChangers
170 {
171 
172 /*---------------------------------------------------------------------------*\
173  Class fvMeshTopoChangers::refiner Declaration
174 \*---------------------------------------------------------------------------*/
175 
176 class refiner
177 :
178  public fvMeshTopoChanger
179 {
180  // Private Data
181 
182  //- Refinement control dictionary
183  dictionary dict_;
184 
185  label refineInterval_;
186 
187  label maxCells_;
188 
189  label nBufferLayers_;
190 
191  //- Mesh cutting engine
192  hexRef8& meshCutter_;
193 
194  //- Dump cellLevel for postprocessing
195  Switch dumpLevel_;
196 
197  //- Fluxes to map
198  HashTable<word> correctFluxes_;
199 
200  //- Number of refinement/unrefinement steps done so far.
201  label nRefinementIterations_;
202 
203  //- Protected cells (usually since not hexes)
204  PackedBoolList protectedCells_;
205 
206  //- The time index used for updating
207  label timeIndex_;
208 
209 
210  // Private Member Functions
211 
212  //- Count set/unset elements in packedlist.
213  static label count(const PackedBoolList&, const unsigned int);
214 
215  //- Calculate which cells are protected due to their topology not being
216  // suitable for refinement
217  void calcProtectedCells(PackedBoolList& protectedCells) const;
218 
219  //- Calculate cells that are additionally protected, as their
220  // refinement would trigger refinement of protectedCells due to the
221  // 2:1 limit
222  void calcAdditionallyProtectedCells
223  (
224  const PackedBoolList& protectedCells,
225  PackedBoolList& additionallyProtectedCells
226  ) const;
227 
228  //- Read the parameters from dictionary
229  void readDict();
230 
231  //- Refine cells. Update mesh and fields.
232  autoPtr<polyTopoChangeMap> refine(const labelList&);
233 
234  //- Unrefine cells. Gets passed in centre points of cells to combine.
235  autoPtr<polyTopoChangeMap> unrefine(const labelList&);
236 
237  //- Find the U field name corresponding to Uf
238  word Uname(const surfaceVectorField& Uf) const;
239 
240  void refineFluxes
241  (
242  const labelHashSet& masterFaces,
243  const polyTopoChangeMap& map
244  );
245 
246  void unrefineFluxes
247  (
248  const Map<label>& faceToSplitPoint,
249  const polyTopoChangeMap& map
250  );
251 
252  void refineUfs
253  (
254  const labelHashSet& masterFaces,
255  const polyTopoChangeMap& map
256  );
257 
258  void unrefineUfs
259  (
260  const Map<label>& faceToSplitPoint,
261  const polyTopoChangeMap& map
262  );
263 
264 
265  // Selection of cells to un/refine
266 
267  const cellZone& findCellZone
268  (
269  const word& cellZoneName
270  ) const;
271 
272  scalarField cellToPoint(const scalarField& vFld) const;
273 
275  (
276  const scalarField& fld,
277  const scalar minLevel,
278  const scalar maxLevel
279  ) const;
280 
282  (
283  const scalarField& fld,
284  const labelList& cells,
285  const scalar minLevel,
286  const scalar maxLevel
287  ) const;
288 
289  //- Select candidate cells for refinement
290  virtual void selectRefineCandidates
291  (
292  PackedBoolList& candidateCell,
293  const scalar lowerRefineLevel,
294  const scalar upperRefineLevel,
295  const scalar maxRefinement,
296  const scalarField& vFld
297  ) const;
298 
299  //- Select candidate cells for refinement
300  virtual void selectRefineCandidates
301  (
302  PackedBoolList& candidateCell,
303  const scalar lowerRefineLevel,
304  const scalar upperRefineLevel,
305  const scalar maxRefinement,
306  const scalarField& vFld,
307  const labelList& cells
308  ) const;
309 
310  //- Select candidate cells for refinement
311  virtual scalar selectRefineCandidates
312  (
313  PackedBoolList& candidateCell,
314  const dictionary& refineDict
315  ) const;
316 
317  //- Subset candidate cells for refinement
318  virtual labelList selectRefineCells
319  (
320  const label maxCells,
321  const label maxRefinement,
322  const PackedBoolList& candidateCell
323  ) const;
324 
325  //- Select points that can be unrefined.
326  virtual labelList selectUnrefinePoints
327  (
328  const PackedBoolList& markedCell
329  ) const;
330 
331  //- Extend markedCell with cell-face-cell.
332  void extendMarkedCells(PackedBoolList& markedCell) const;
333 
334 
335 public:
336 
337  //- Runtime type information
338  TypeName("refiner");
339 
340 
341  // Constructors
342 
343  //- Construct from fvMesh and dictionary
344  refiner(fvMesh& mesh, const dictionary& dict);
345 
346  //- Disallow default bitwise copy construction
347  refiner(const refiner&) = delete;
348 
349 
350  //- Destructor
351  virtual ~refiner();
352 
353 
354  // Member Functions
355 
356  //- Cells which should not be refined/unrefined
357  const PackedBoolList& protectedCell() const
358  {
359  return protectedCells_;
360  }
361 
362  //- Cells which should not be refined/unrefined
364  {
365  return protectedCells_;
366  }
367 
368  //- Update the mesh for both mesh motion and topology change
369  virtual bool update();
370 
371  //- Update corresponding to the given map
372  virtual void topoChange(const polyTopoChangeMap&);
373 
374  //- Update from another mesh using the given map
375  virtual void mapMesh(const polyMeshMap&);
376 
377  //- Update corresponding to the given distribution map
378  virtual void distribute(const polyDistributionMap&);
379 
380 
381  // Member Operators
382 
383  //- Disallow default bitwise assignment
384  void operator=(const refiner&) = delete;
385 };
386 
387 
388 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
389 
390 } // End namespace fvMeshTopoChangers
391 } // End namespace Foam
392 
393 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
394 
395 #endif
396 
397 // ************************************************************************* //
Generic GeometricField class.
An STL-conforming hash table.
Definition: HashTable.H:127
A bit-packed bool list.
A simple wrapper around bool so that it can be read as a word: true/false, on/off,...
Definition: Switch.H:61
An auto-pointer similar to the STL auto_ptr but with automatic casting to a reference to the type and...
Definition: autoPtr.H:51
A topoSetSource to select points based on usage in cells.
Definition: cellToPoint.H:52
Named list of cell indices representing a sub-set of the mesh.
Definition: cellZone.H:61
A list of keywords followed by any number of values (e.g. words and numbers) or sub-dictionaries.
Definition: dictionary.H:162
Class to handle errors and exceptions in a simple, consistent stream-based manner.
Definition: error.H:71
Abstract base class for fvMesh topology changers.
fvMesh & mesh()
Return the fvMesh.
Dynamic mesh refinement/unrefinement based on volScalarField values.
void operator=(const refiner &)=delete
Disallow default bitwise assignment.
virtual void topoChange(const polyTopoChangeMap &)
Update corresponding to the given map.
TypeName("refiner")
Runtime type information.
virtual void distribute(const polyDistributionMap &)
Update corresponding to the given distribution map.
const PackedBoolList & protectedCell() const
Cells which should not be refined/unrefined.
virtual void mapMesh(const polyMeshMap &)
Update from another mesh using the given map.
virtual bool update()
Update the mesh for both mesh motion and topology change.
refiner(fvMesh &mesh, const dictionary &dict)
Construct from fvMesh and dictionary.
Mesh data needed to do the Finite Volume discretisation.
Definition: fvMesh.H:98
Refinement of (split) hexes using polyTopoChange.
Definition: hexRef8.H:73
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
const cellShapeList & cells
gmvFile<< "tracers "<< particles.size()<< nl;{ pointField positions(particles.size());label particlei=0;forAllConstIter(lagrangian::Cloud< passiveParticle >, particles, iter) { positions[particlei++]=iter().position(mesh);} for(i=0;i< pTraits< point >::nComponents;i++) { forAll(positions, particlei) { gmvFile<< component(positions[particlei], i)<< ' ';} gmvFile<< nl;}}forAll(lagrangianScalarNames, i){ const word &name=lagrangianScalarNames[i];IOField< scalar > fld(IOobject(name, runTime.name(), lagrangian::cloud::prefix, mesh, IOobject::MUST_READ, IOobject::NO_WRITE))
Namespace for OpenFOAM.
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
dictionary dict