refineHexMesh.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) 2011-2014 OpenFOAM Foundation
9  Copyright (C) 2016 OpenCFD Ltd.
10 -------------------------------------------------------------------------------
11 License
12  This file is part of OpenFOAM.
13 
14  OpenFOAM is free software: you can redistribute it and/or modify it
15  under the terms of the GNU General Public License as published by
16  the Free Software Foundation, either version 3 of the License, or
17  (at your option) any later version.
18 
19  OpenFOAM is distributed in the hope that it will be useful, but WITHOUT
20  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
21  FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
22  for more details.
23 
24  You should have received a copy of the GNU General Public License
25  along with OpenFOAM. If not, see <http://www.gnu.org/licenses/>.
26 
27 Application
28  refineHexMesh
29 
30 Group
31  grpMeshAdvancedUtilities
32 
33 Description
34  Refine a hex mesh by 2x2x2 cell splitting for the specified cellSet.
35 
36 \*---------------------------------------------------------------------------*/
37 
38 #include "fvMesh.H"
39 #include "pointMesh.H"
40 #include "argList.H"
41 #include "Time.H"
42 #include "hexRef8.H"
43 #include "cellSet.H"
44 #include "Fstream.H"
45 #include "meshTools.H"
46 #include "polyTopoChange.H"
47 #include "mapPolyMesh.H"
48 #include "volMesh.H"
49 #include "surfaceMesh.H"
50 #include "volFields.H"
51 #include "surfaceFields.H"
52 #include "pointFields.H"
53 #include "ReadFields.H"
54 #include "processorMeshes.H"
55 
56 using namespace Foam;
57 
58 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
59 
60 int main(int argc, char *argv[])
61 {
63  (
64  "Refine a hex mesh by 2x2x2 cell splitting for the specified cellSet"
65  );
66  #include "addOverwriteOption.H"
67  #include "addRegionOption.H"
68  argList::addArgument("cellSet");
70  (
71  "minSet",
72  "Remove cells from input cellSet to keep to 2:1 ratio"
73  " (default is to extend set)"
74  );
75 
76  argList::noFunctionObjects(); // Never use function objects
77 
78  #include "setRootCase.H"
79  #include "createTime.H"
80  #include "createNamedMesh.H"
81 
82  const word oldInstance = mesh.pointsInstance();
83 
84  word cellSetName(args[1]);
85  const bool overwrite = args.found("overwrite");
86 
87  const bool minSet = args.found("minSet");
88 
89  Info<< "Reading cells to refine from cellSet " << cellSetName
90  << nl << endl;
91 
92  cellSet cellsToRefine(mesh, cellSetName);
93 
94  Info<< "Read " << returnReduce(cellsToRefine.size(), sumOp<label>())
95  << " cells to refine from cellSet " << cellSetName << nl
96  << endl;
97 
98 
99  // Read objects in time directory
100  IOobjectList objects(mesh, runTime.timeName());
101 
102  // Read vol fields.
103 
105  ReadFields(mesh, objects, vsFlds);
106 
108  ReadFields(mesh, objects, vvFlds);
109 
111  ReadFields(mesh, objects, vstFlds);
112 
113  PtrList<volSymmTensorField> vsymtFlds;
114  ReadFields(mesh, objects, vsymtFlds);
115 
117  ReadFields(mesh, objects, vtFlds);
118 
119  // Read surface fields.
120 
122  ReadFields(mesh, objects, ssFlds);
123 
125  ReadFields(mesh, objects, svFlds);
126 
128  ReadFields(mesh, objects, sstFlds);
129 
131  ReadFields(mesh, objects, ssymtFlds);
132 
134  ReadFields(mesh, objects, stFlds);
135 
136  // Read point fields
138  ReadFields(pointMesh::New(mesh), objects, psFlds);
139 
141  ReadFields(pointMesh::New(mesh), objects, pvFlds);
142 
143 
144  // Construct refiner without unrefinement. Read existing point/cell level.
146 
147  // Some stats
148  {
149  auto cellLimits = gMinMax(meshCutter.cellLevel());
150  auto pointLimits = gMinMax(meshCutter.pointLevel());
151 
152  Info<< "Read mesh:" << nl
153  << " cells:" << mesh.globalData().nTotalCells() << nl
154  << " faces:" << mesh.globalData().nTotalFaces() << nl
155  << " points:" << mesh.globalData().nTotalPoints() << nl
156  << " cellLevel :"
157  << " min:" << cellLimits.min()
158  << " max:" << cellLimits.max() << nl
159  << " pointLevel :"
160  << " min:" << pointLimits.min()
161  << " max:" << pointLimits.max() << nl
162  << endl;
163  }
164 
165 
166  // Maintain 2:1 ratio
167  labelList newCellsToRefine
168  (
169  meshCutter.consistentRefinement
170  (
171  cellsToRefine.toc(),
172  !minSet // extend set
173  )
174  );
175 
176  // Mesh changing engine.
177  polyTopoChange meshMod(mesh);
178 
179  // Play refinement commands into mesh changer.
180  meshCutter.setRefinement(newCellsToRefine, meshMod);
181 
182  if (!overwrite)
183  {
184  ++runTime;
185  }
186 
187  // Create mesh, return map from old to new mesh.
188  autoPtr<mapPolyMesh> map = meshMod.changeMesh(mesh, false);
189 
190  // Update fields
191  mesh.updateMesh(map());
192 
193  // Update numbering of cells/vertices.
194  meshCutter.updateMesh(map());
195 
196  // Optionally inflate mesh
197  if (map().hasMotionPoints())
198  {
199  mesh.movePoints(map().preMotionPoints());
200  }
201 
202  Info<< "Refined from " << returnReduce(map().nOldCells(), sumOp<label>())
203  << " to " << mesh.globalData().nTotalCells() << " cells." << nl << endl;
204 
205  if (overwrite)
206  {
207  mesh.setInstance(oldInstance);
208  meshCutter.setInstance(oldInstance);
209  }
210  Info<< "Writing mesh to " << runTime.timeName() << endl;
211 
212  mesh.write();
213  meshCutter.write();
216 
217  Info<< "End\n" << endl;
218 
219  return 0;
220 }
221 
222 
223 // ************************************************************************* //
Foam::surfaceFields.
static void noFunctionObjects(bool addWithOption=false)
Remove &#39;-noFunctionObjects&#39; option and ignore any occurrences.
Definition: argList.C:562
static void addNote(const string &note)
Add extra notes for the usage information.
Definition: argList.C:477
wordList ReadFields(const typename GeoMesh::Mesh &mesh, const IOobjectList &objects, PtrList< GeometricField< Type, PatchField, GeoMesh >> &fields, const bool syncPar=true, const bool readOldTime=false)
Read Geometric fields of templated type.
List of IOobjects with searching and retrieving facilities. Implemented as a HashTable, so the various sorted methods should be used if traversing in parallel.
Definition: IOobjectList.H:55
static FOAM_NO_DANGLING_REFERENCE const pointMesh & New(const polyMesh &mesh, Args &&... args)
Get existing or create MeshObject registered with typeName.
constexpr char nl
The newline &#39;\n&#39; character (0x0a)
Definition: Ostream.H:50
engineTime & runTime
Ostream & endl(Ostream &os)
Add newline and flush stream.
Definition: Ostream.H:518
virtual void movePoints(const pointField &)
Move points, returns volumes swept by faces in motion.
Definition: fvMesh.C:884
Required Classes.
static void addBoolOption(const word &optName, const string &usage="", bool advanced=false)
Add a bool option to validOptions with usage information.
Definition: argList.C:389
Field reading functions for post-processing utilities.
label nTotalPoints() const noexcept
Total global number of mesh points. Not compensated for duplicate points!
void updateMesh(const mapPolyMesh &)
Force recalculation of locally stored data on topological change.
Definition: meshCutter.C:988
static void removeFiles(const polyMesh &mesh)
Helper: remove all procAddressing files from mesh instance.
Required Classes.
static void removeFiles(const polyMesh &)
Helper: remove all sets files from mesh instance.
Definition: topoSet.C:693
const fileName & pointsInstance() const
Return the current instance directory for points.
Definition: polyMesh.C:838
virtual void updateMesh(const mapPolyMesh &mpm)
Update mesh corresponding to the given map.
Definition: fvMesh.C:960
dynamicFvMesh & mesh
Refinement of (split) hexes using polyTopoChange.
Definition: hexRef8.H:62
A class for handling words, derived from Foam::string.
Definition: word.H:63
Cuts (splits) cells.
Definition: meshCutter.H:134
MinMax< Type > gMinMax(const FieldField< Field, Type > &f)
T returnReduce(const T &value, BinaryOp bop, const int tag=UPstream::msgType(), const int communicator=UPstream::worldComm)
Perform reduction on a copy, using specified binary operation.
const globalMeshData & globalData() const
Return parallel info (demand-driven)
Definition: polyMesh.C:1296
void setRefinement(const cellCuts &cuts, polyTopoChange &meshMod)
Do actual cutting with cut description. Inserts mesh changes.
Definition: meshCutter.C:515
virtual bool write(const bool writeOnProc=true) const
Write mesh using IO settings from time.
Definition: fvMesh.C:1068
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:713
label nTotalCells() const noexcept
Total global number of mesh cells.
void setInstance(const fileName &instance, const IOobjectOption::writeOption wOpt=IOobject::AUTO_WRITE)
Set the instance for mesh files.
Definition: polyMeshIO.C:29
A list of pointers to objects of type <T>, with allocation/deallocation management of the pointers...
Definition: PtrList.H:56
A collection of cell labels.
Definition: cellSet.H:47
Direct mesh changes based on v1.3 polyTopoChange syntax.
static void addArgument(const string &argName, const string &usage="")
Append a (mandatory) argument to validArgs.
Definition: argList.C:366
messageStream Info
Information stream (stdout output on master, null elsewhere)
label nTotalFaces() const noexcept
Total global number of mesh faces. Not compensated for duplicate faces!
Pointer management similar to std::unique_ptr, with some additional methods and type checking...
Definition: HashPtrTable.H:48
Foam::argList args(argc, argv)
bool found(const word &optName) const
Return true if the named option is found.
Definition: argListI.H:171
Namespace for OpenFOAM.