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  Info<< "Read mesh:" << nl
149  << " cells:" << mesh.globalData().nTotalCells() << nl
150  << " faces:" << mesh.globalData().nTotalFaces() << nl
151  << " points:" << mesh.globalData().nTotalPoints() << nl
152  << " cellLevel :"
153  << " min:" << gMin(meshCutter.cellLevel())
154  << " max:" << gMax(meshCutter.cellLevel()) << nl
155  << " pointLevel :"
156  << " min:" << gMin(meshCutter.pointLevel())
157  << " max:" << gMax(meshCutter.pointLevel()) << nl
158  << endl;
159 
160 
161  // Maintain 2:1 ratio
162  labelList newCellsToRefine
163  (
164  meshCutter.consistentRefinement
165  (
166  cellsToRefine.toc(),
167  !minSet // extend set
168  )
169  );
170 
171  // Mesh changing engine.
172  polyTopoChange meshMod(mesh);
173 
174  // Play refinement commands into mesh changer.
175  meshCutter.setRefinement(newCellsToRefine, meshMod);
176 
177  if (!overwrite)
178  {
179  ++runTime;
180  }
181 
182  // Create mesh, return map from old to new mesh.
183  autoPtr<mapPolyMesh> map = meshMod.changeMesh(mesh, false);
184 
185  // Update fields
186  mesh.updateMesh(map());
187 
188  // Update numbering of cells/vertices.
189  meshCutter.updateMesh(map());
190 
191  // Optionally inflate mesh
192  if (map().hasMotionPoints())
193  {
194  mesh.movePoints(map().preMotionPoints());
195  }
196 
197  Info<< "Refined from " << returnReduce(map().nOldCells(), sumOp<label>())
198  << " to " << mesh.globalData().nTotalCells() << " cells." << nl << endl;
199 
200  if (overwrite)
201  {
202  mesh.setInstance(oldInstance);
203  meshCutter.setInstance(oldInstance);
204  }
205  Info<< "Writing mesh to " << runTime.timeName() << endl;
206 
207  mesh.write();
208  meshCutter.write();
211 
212  Info<< "End\n" << endl;
213 
214  return 0;
215 }
216 
217 
218 // ************************************************************************* //
volFields.H
runTime
engineTime & runTime
Definition: createEngineTime.H:13
meshTools.H
Foam::meshCutter
Cuts (splits) cells.
Definition: meshCutter.H:134
Foam::word
A class for handling words, derived from Foam::string.
Definition: word.H:63
Foam::fvMesh::write
virtual bool write(const bool valid=true) const
Definition: fvMesh.C:1034
Foam::returnReduce
T returnReduce(const T &Value, const BinaryOp &bop, const int tag=Pstream::msgType(), const label comm=UPstream::worldComm)
Definition: PstreamReduceOps.H:88
Foam::ReadFields
wordList ReadFields(const typename GeoMesh::Mesh &mesh, const IOobjectList &objects, PtrList< GeometricField< Type, PatchField, GeoMesh >> &fields, const bool syncPar=true, const bool readOldTime=false)
addOverwriteOption.H
volMesh.H
mapPolyMesh.H
polyTopoChange.H
Foam::MeshObject< polyMesh, UpdateableMeshObject, pointMesh >::New
static const pointMesh & New(const polyMesh &mesh, Args &&... args)
Definition: MeshObject.C:41
Foam::Time::timeName
static word timeName(const scalar t, const int precision=precision_)
Definition: Time.C:773
Foam::polyTopoChange
Direct mesh changes based on v1.3 polyTopoChange syntax.
Definition: polyTopoChange.H:95
Foam::argList::addNote
static void addNote(const string &note)
Definition: argList.C:405
Foam::fvMesh::movePoints
virtual tmp< scalarField > movePoints(const pointField &)
Definition: fvMesh.C:858
Foam::endl
Ostream & endl(Ostream &os)
Definition: Ostream.H:381
surfaceFields.H
Foam::surfaceFields.
Foam::meshCutter::updateMesh
void updateMesh(const mapPolyMesh &)
Definition: meshCutter.C:988
processorMeshes.H
Foam::sumOp
Definition: ops.H:207
Foam::argList::addArgument
static void addArgument(const string &argName, const string &usage="")
Definition: argList.C:294
Foam::polyMesh::pointsInstance
const fileName & pointsInstance() const
Definition: polyMesh.C:839
Foam::argList::noFunctionObjects
static void noFunctionObjects(bool addWithOption=false)
Definition: argList.C:466
Foam::Info
messageStream Info
hexRef8.H
argList.H
addRegionOption.H
Foam::fvMesh::updateMesh
virtual void updateMesh(const mapPolyMesh &mpm)
Definition: fvMesh.C:946
Foam::PtrList
A list of pointers to objects of type <T>, with allocation/deallocation management of the pointers....
Definition: List.H:55
surfaceMesh.H
createNamedMesh.H
Required Variables.
mesh
dynamicFvMesh & mesh
Definition: createDynamicFvMesh.H:6
Foam::globalMeshData::nTotalPoints
label nTotalPoints() const noexcept
Definition: globalMeshData.H:354
fvMesh.H
Foam
Definition: atmBoundaryLayer.C:26
Foam::cellSet
A collection of cell labels.
Definition: cellSet.H:47
Foam::IOobjectList
List of IOobjects with searching and retrieving facilities.
Definition: IOobjectList.H:51
Foam::argList::addBoolOption
static void addBoolOption(const word &optName, const string &usage="", bool advanced=false)
Definition: argList.C:317
Time.H
Foam::autoPtr
Pointer management similar to std::unique_ptr, with some additional methods and type checking.
Definition: HashPtrTable.H:49
setRootCase.H
ReadFields.H
Field reading functions for post-processing utilities.
Foam::nl
constexpr char nl
Definition: Ostream.H:424
Fstream.H
Foam::hexRef8
Refinement of (split) hexes using polyTopoChange.
Definition: hexRef8.H:63
Foam::meshCutter::setRefinement
void setRefinement(const cellCuts &cuts, polyTopoChange &meshMod)
Definition: meshCutter.C:515
Foam::List
A 1D array of objects of type <T>, where the size of the vector is known and used for subscript bound...
Definition: BitOps.H:58
Foam::processorMeshes::removeFiles
static void removeFiles(const polyMesh &mesh)
Definition: processorMeshes.C:267
createTime.H
Foam::globalMeshData::nTotalCells
label nTotalCells() const noexcept
Definition: globalMeshData.H:367
cellSet.H
Foam::gMin
Type gMin(const FieldField< Field, Type > &f)
Definition: FieldFieldFunctions.C:586
Foam::polyMesh::globalData
const globalMeshData & globalData() const
Definition: polyMesh.C:1288
args
Foam::argList args(argc, argv)
Foam::topoSet::removeFiles
static void removeFiles(const polyMesh &)
Definition: topoSet.C:628
pointFields.H
Foam::gMax
Type gMax(const FieldField< Field, Type > &f)
Definition: FieldFieldFunctions.C:585
pointMesh.H
Foam::polyMesh::setInstance
void setInstance(const fileName &instance, const IOobject::writeOption wOpt=IOobject::AUTO_WRITE)
Definition: polyMeshIO.C:29
Foam::globalMeshData::nTotalFaces
label nTotalFaces() const noexcept
Definition: globalMeshData.H:361
Foam::argList::found
bool found(const word &optName) const
Definition: argListI.H:171