multiDirRefinement.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-2022 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 "multiDirRefinement.H"
27 #include "polyMesh.H"
28 #include "polyTopoChanger.H"
29 #include "Time.H"
30 #include "undoableMeshCutter.H"
31 #include "hexCellLooper.H"
32 #include "geomCellLooper.H"
33 #include "topoSet.H"
34 #include "directions.H"
35 #include "hexRef8.H"
36 #include "polyTopoChangeMap.H"
37 #include "polyTopoChange.H"
38 #include "ListOps.H"
39 #include "cellModeller.H"
40 
41 // * * * * * * * * * * * * * * Static Data Members * * * * * * * * * * * * * //
42 
43 namespace Foam
44 {
46 }
47 
48 
49 // * * * * * * * * * * * * * Private Statc Functions * * * * * * * * * * * * //
50 
51 // Update refCells pattern for split cells. Note refCells is current
52 // list of cells to refine (these should all have been refined)
53 void Foam::multiDirRefinement::addCells
54 (
55  const Map<label>& splitMap,
56  List<refineCell>& refCells
57 )
58 {
59  label newRefI = refCells.size();
60 
61  label oldSize = refCells.size();
62 
63  refCells.setSize(newRefI + splitMap.size());
64 
65  for (label refI = 0; refI < oldSize; refI++)
66  {
67  const refineCell& refCell = refCells[refI];
68 
69  Map<label>::const_iterator iter = splitMap.find(refCell.cellNo());
70 
71  if (iter == splitMap.end())
72  {
74  << "Problem : cannot find added cell for cell "
75  << refCell.cellNo() << abort(FatalError);
76  }
77 
78  refCells[newRefI++] = refineCell(iter(), refCell.direction());
79  }
80 }
81 
82 
83 // Update vectorField for all the new cells. Takes over value of
84 // original cell.
85 void Foam::multiDirRefinement::update
86 (
87  const Map<label>& splitMap,
88  vectorField& field
89 )
90 {
91  field.setSize(field.size() + splitMap.size());
92 
93  forAllConstIter(Map<label>, splitMap, iter)
94  {
95  field[iter()] = field[iter.key()];
96  }
97 }
98 
99 
100 // Add added cells to labelList
101 void Foam::multiDirRefinement::addCells
102 (
103  const Map<label>& splitMap,
104  labelList& labels
105 )
106 {
107  label newCelli = labels.size();
108 
109  labels.setSize(labels.size() + splitMap.size());
110 
111  forAllConstIter(Map<label>, splitMap, iter)
112  {
113  labels[newCelli++] = iter();
114  }
115 }
116 
117 
118 // * * * * * * * * * * * * * Private Member Functions * * * * * * * * * * * //
119 
120 void Foam::multiDirRefinement::addCells
121 (
122  const primitiveMesh& mesh,
123  const Map<label>& splitMap
124 )
125 {
126  // Construct inverse addressing: from new to original cell.
127  labelList origCell(mesh.nCells(), -1);
128 
129  forAll(addedCells_, celli)
130  {
131  const labelList& added = addedCells_[celli];
132 
133  forAll(added, i)
134  {
135  label slave = added[i];
136 
137  if (origCell[slave] == -1)
138  {
139  origCell[slave] = celli;
140  }
141  else if (origCell[slave] != celli)
142  {
144  << "Added cell " << slave << " has two different masters:"
145  << origCell[slave] << " , " << celli
146  << abort(FatalError);
147  }
148  }
149  }
150 
151 
152  forAllConstIter(Map<label>, splitMap, iter)
153  {
154  label masterI = iter.key();
155  label newCelli = iter();
156 
157  while (origCell[masterI] != -1 && origCell[masterI] != masterI)
158  {
159  masterI = origCell[masterI];
160  }
161 
162  if (masterI >= addedCells_.size())
163  {
165  << "Map of added cells contains master cell " << masterI
166  << " which is not a valid cell number" << endl
167  << "This means that the mesh is not consistent with the"
168  << " done refinement" << endl
169  << "newCell:" << newCelli << abort(FatalError);
170  }
171 
172  labelList& added = addedCells_[masterI];
173 
174  if (added.empty())
175  {
176  added.setSize(2);
177  added[0] = masterI;
178  added[1] = newCelli;
179  }
180  else if (findIndex(added, newCelli) == -1)
181  {
182  label sz = added.size();
183  added.setSize(sz + 1);
184  added[sz] = newCelli;
185  }
186  }
187 }
188 
189 
190 Foam::labelList Foam::multiDirRefinement::splitOffHex(const primitiveMesh& mesh)
191 {
192  const cellModel& hex = *(cellModeller::lookup("hex"));
193 
194  const cellShapeList& cellShapes = mesh.cellShapes();
195 
196  // Split cellLabels_ into two lists.
197 
198  labelList nonHexLabels(cellLabels_.size());
199  label nonHexI = 0;
200 
201  labelList hexLabels(cellLabels_.size());
202  label hexI = 0;
203 
204  forAll(cellLabels_, i)
205  {
206  label celli = cellLabels_[i];
207 
208  if (cellShapes[celli].model() == hex)
209  {
210  hexLabels[hexI++] = celli;
211  }
212  else
213  {
214  nonHexLabels[nonHexI++] = celli;
215  }
216  }
217 
218  nonHexLabels.setSize(nonHexI);
219 
220  cellLabels_.transfer(nonHexLabels);
221 
222  hexLabels.setSize(hexI);
223 
224  return hexLabels;
225 }
226 
227 
228 void Foam::multiDirRefinement::refineHex8
229 (
230  polyMesh& mesh,
231  const labelList& hexCells,
232  const bool writeMesh
233 )
234 {
235  if (debug)
236  {
237  Pout<< "multiDirRefinement : Refining hexes " << hexCells.size()
238  << endl;
239  }
240 
241  hexRef8 hexRefiner
242  (
243  mesh,
244  labelList(mesh.nCells(), 0), // cellLevel
245  labelList(mesh.nPoints(), 0), // pointLevel
246  refinementHistory
247  (
248  IOobject
249  (
250  "refinementHistory",
251  mesh.facesInstance(),
253  mesh,
256  false
257  ),
258  List<refinementHistory::splitCell8>(0),
259  labelList(0),
260  false
261  ) // refinement history
262  );
263 
264  polyTopoChange meshMod(mesh);
265 
266  labelList consistentCells
267  (
268  hexRefiner.consistentRefinement
269  (
270  hexCells,
271  true // buffer layer
272  )
273  );
274 
275  // Check that consistentRefinement hasn't added cells
276  {
277  // Create count 1 for original cells
278  Map<label> hexCellSet(2*hexCells.size());
279  forAll(hexCells, i)
280  {
281  hexCellSet.insert(hexCells[i], 1);
282  }
283 
284  // Increment count
285  forAll(consistentCells, i)
286  {
287  const label celli = consistentCells[i];
288 
289  Map<label>::iterator iter = hexCellSet.find(celli);
290 
291  if (iter == hexCellSet.end())
292  {
294  << "Resulting mesh would not satisfy 2:1 ratio"
295  << " when refining cell " << celli << abort(FatalError);
296  }
297  else
298  {
299  iter() = 2;
300  }
301  }
302 
303  // Check if all been visited (should always be since
304  // consistentRefinement set up to extend set.
305  forAllConstIter(Map<label>, hexCellSet, iter)
306  {
307  if (iter() != 2)
308  {
310  << "Resulting mesh would not satisfy 2:1 ratio"
311  << " when refining cell " << iter.key()
312  << abort(FatalError);
313  }
314  }
315  }
316 
317 
318  hexRefiner.setRefinement(consistentCells, meshMod);
319 
320  // Change mesh, no inflation
321  autoPtr<polyTopoChangeMap> mapPtr =
322  meshMod.changeMesh(mesh, false, true);
323  const polyTopoChangeMap& map = mapPtr();
324 
325  if (map.hasMotionPoints())
326  {
327  mesh.movePoints(map.preMotionPoints());
328  }
329 
330  if (writeMesh)
331  {
332  mesh.write();
333  }
334 
335  if (debug)
336  {
337  Pout<< "multiDirRefinement : updated mesh at time "
338  << mesh.time().name() << endl;
339  }
340 
341  hexRefiner.topoChange(map);
342 
343  // Collect all cells originating from same old cell (original + 7 extra)
344 
345  forAll(consistentCells, i)
346  {
347  addedCells_[consistentCells[i]].setSize(8);
348  }
349  labelList nAddedCells(addedCells_.size(), 0);
350 
351  const labelList& cellMap = map.cellMap();
352 
353  forAll(cellMap, celli)
354  {
355  const label oldCelli = cellMap[celli];
356 
357  if (addedCells_[oldCelli].size())
358  {
359  addedCells_[oldCelli][nAddedCells[oldCelli]++] = celli;
360  }
361  }
362 }
363 
364 
365 void Foam::multiDirRefinement::refineAllDirs
366 (
367  polyMesh& mesh,
368  List<vectorField>& cellDirections,
369  const cellLooper& cellWalker,
370  undoableMeshCutter& cutter,
371  const bool writeMesh
372 )
373 {
374  // Iterator
375  refinementIterator refiner(mesh, cutter, cellWalker, writeMesh);
376 
377  forAll(cellDirections, dirI)
378  {
379  if (debug)
380  {
381  Pout<< "multiDirRefinement : Refining " << cellLabels_.size()
382  << " cells in direction " << dirI << endl
383  << endl;
384  }
385 
386  const vectorField& dirField = cellDirections[dirI];
387 
388  // Combine cell to be cut with direction to cut in.
389  // If dirField is only one element use this for all cells.
390 
391  List<refineCell> refCells(cellLabels_.size());
392 
393  if (dirField.size() == 1)
394  {
395  // Uniform directions.
396  if (debug)
397  {
398  Pout<< "multiDirRefinement : Uniform refinement:"
399  << dirField[0] << endl;
400  }
401 
402  forAll(refCells, refI)
403  {
404  label celli = cellLabels_[refI];
405 
406  refCells[refI] = refineCell(celli, dirField[0]);
407  }
408  }
409  else
410  {
411  // Non uniform directions.
412  forAll(refCells, refI)
413  {
414  const label celli = cellLabels_[refI];
415 
416  refCells[refI] = refineCell(celli, dirField[celli]);
417  }
418  }
419 
420  // Do refine mesh (multiple iterations). Remember added cells.
421  Map<label> splitMap = refiner.setRefinement(refCells);
422 
423  // Update overall map of added cells
424  addCells(mesh, splitMap);
425 
426  // Add added cells to list of cells to refine in next iteration
427  addCells(splitMap, cellLabels_);
428 
429  // Update refinement direction for added cells.
430  if (dirField.size() != 1)
431  {
432  forAll(cellDirections, i)
433  {
434  update(splitMap, cellDirections[i]);
435  }
436  }
437 
438  if (debug)
439  {
440  Pout<< "multiDirRefinement : Done refining direction " << dirI
441  << " resulting in " << cellLabels_.size() << " cells" << nl
442  << endl;
443  }
444  }
445 }
446 
447 
448 void Foam::multiDirRefinement::refineFromDict
449 (
450  polyMesh& mesh,
451  List<vectorField>& cellDirections,
452  const dictionary& dict,
453  const bool writeMesh
454 )
455 {
456  // How to walk cell circumference.
457  Switch pureGeomCut(dict.lookup("geometricCut"));
458 
459  autoPtr<cellLooper> cellWalker(nullptr);
460  if (pureGeomCut)
461  {
462  cellWalker.reset(new geomCellLooper(mesh));
463  }
464  else
465  {
466  cellWalker.reset(new hexCellLooper(mesh));
467  }
468 
469  // Construct undoable refinement topology modifier.
470  // Note: undoability switched off.
471  // Might want to reconsider if needs to be possible. But then can always
472  // use other constructor.
473  undoableMeshCutter cutter(mesh, false);
474 
475  refineAllDirs(mesh, cellDirections, cellWalker(), cutter, writeMesh);
476 }
477 
478 
479 
480 // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
481 
482 // Construct from dictionary
484 (
485  polyMesh& mesh,
486  const labelList& cellLabels, // list of cells to refine
487  const dictionary& dict
488 )
489 :
490  cellLabels_(cellLabels),
491  addedCells_(mesh.nCells())
492 {
493  Switch useHex(dict.lookup("useHexTopology"));
494 
495  Switch writeMesh(dict.lookup("writeMesh"));
496 
497  wordList dirNames(dict.lookup("directions"));
498 
499  if (useHex && dirNames.size() == 3)
500  {
501  // Filter out hexes from cellLabels_
502  labelList hexCells(splitOffHex(mesh));
503 
504  refineHex8(mesh, hexCells, writeMesh);
505  }
506 
507  label nRemainingCells = cellLabels_.size();
508 
509  reduce(nRemainingCells, sumOp<label>());
510 
511  if (nRemainingCells > 0)
512  {
513  // Any cells to refine using meshCutter
514 
515  // Determine directions for every cell. Can either be uniform
516  // (size = 1) or non-uniform (one vector per cell)
517  directions cellDirections(mesh, dict);
518 
519  refineFromDict(mesh, cellDirections, dict, writeMesh);
520  }
521 }
522 
523 
524 // Construct from dictionary and directions to refine.
526 (
527  polyMesh& mesh,
528  const labelList& cellLabels, // list of cells to refine
529  const List<vectorField>& cellDirs, // Explicitly provided directions
530  const dictionary& dict
531 )
532 :
533  cellLabels_(cellLabels),
534  addedCells_(mesh.nCells())
535 {
536  Switch useHex(dict.lookup("useHexTopology"));
537 
538  Switch writeMesh(dict.lookup("writeMesh"));
539 
540  wordList dirNames(dict.lookup("directions"));
541 
542  if (useHex && dirNames.size() == 3)
543  {
544  // Filter out hexes from cellLabels_
545  labelList hexCells(splitOffHex(mesh));
546 
547  refineHex8(mesh, hexCells, writeMesh);
548  }
549 
550  label nRemainingCells = cellLabels_.size();
551 
552  reduce(nRemainingCells, sumOp<label>());
553 
554  if (nRemainingCells > 0)
555  {
556  // Any cells to refine using meshCutter
557 
558  // Working copy of cells to refine
559  List<vectorField> cellDirections(cellDirs);
560 
561  refineFromDict(mesh, cellDirections, dict, writeMesh);
562  }
563 }
564 
565 
566 // Construct from components. Implies meshCutter since directions provided.
568 (
569  polyMesh& mesh,
570  undoableMeshCutter& cutter, // actual mesh modifier
571  const cellLooper& cellWalker, // how to cut a single cell with
572  // a plane
573  const labelList& cellLabels, // list of cells to refine
574  const List<vectorField>& cellDirs,
575  const bool writeMesh // write intermediate meshes
576 )
577 :
578  cellLabels_(cellLabels),
579  addedCells_(mesh.nCells())
580 {
581  // Working copy of cells to refine
582  List<vectorField> cellDirections(cellDirs);
583 
584  refineAllDirs(mesh, cellDirections, cellWalker, cutter, writeMesh);
585 }
586 
587 
588 // ************************************************************************* //
Various functions to operate on Lists.
#define forAll(list, i)
Loop across all elements in list.
Definition: UList.H:434
#define forAllConstIter(Container, container, iter)
Iterate across all elements in the container object of type.
Definition: UList.H:477
void size(const label)
Override size to be inconsistent with allocated storage.
Definition: ListI.H:164
void setSize(const label)
Reset size of List.
Definition: List.C:281
HashTable< label, label, Hash< label > >::iterator iterator
Definition: Map.H:56
HashTable< label, label, Hash< label > >::const_iterator const_iterator
Definition: Map.H:59
virtual const fileName & name() const
Return the name of the stream.
Definition: OSstream.H:85
A simple wrapper around bool so that it can be read as a word: true/false, on/off,...
Definition: Switch.H:61
Abstract base class. Concrete implementations know how to cut a cell (i.e. determine a loop around th...
Definition: cellLooper.H:72
static const cellModel * lookup(const word &)
Look up a model by name and return a pointer to the model or nullptr.
Definition: cellModeller.C:100
A list of keyword definitions, which are a keyword followed by any number of values (e....
Definition: dictionary.H:160
ITstream & lookup(const word &, bool recursive=false, bool patternMatch=true) const
Find and return an entry data stream.
Definition: dictionary.C:860
Set of directions for each cell in the mesh. Either uniform and size=1 or one set of directions per c...
Definition: directions.H:67
Does multiple pass refinement to refine cells in multiple directions.
multiDirRefinement(polyMesh &mesh, const labelList &cellLabels, const dictionary &dict)
Construct from dictionary. After construction all refinement will.
Mesh consisting of general polyhedral cells.
Definition: polyMesh.H:80
static word meshSubDir
Return the mesh sub-directory name (usually "polyMesh")
Definition: polyMesh.H:269
The main refinement handler. Gets cellCuts which is structure that describes which cells are to be cu...
#define FatalErrorInFunction
Report an error message using Foam::FatalError.
Definition: error.H:306
const cellShapeList & cellShapes
Namespace for OpenFOAM.
List< cellShape > cellShapeList
List of cellShapes and PtrList of List of cellShape.
Definition: cellShapeList.H:43
List< label > labelList
A List of labels.
Definition: labelList.H:56
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:251
errorManip< error > abort(error &err)
Definition: errorManip.H:131
IOstream & hex(IOstream &io)
Definition: IOstream.H:561
void reduce(const List< UPstream::commsStruct > &comms, T &Value, const BinaryOp &bop, const int tag, const label comm)
defineTypeNameAndDebug(combustionModel, 0)
Field< vector > vectorField
Specialisation of Field<T> for vector.
prefixOSstream Pout(cout, "Pout")
Definition: IOstreams.H:53
label findIndex(const ListType &, typename ListType::const_reference, const label start=0)
Find first occurrence of given element and return index,.
error FatalError
static const char nl
Definition: Ostream.H:260
dictionary dict