improveMeshQuality.C
Go to the documentation of this file.
1 /*---------------------------------------------------------------------------*\
2  ========= |
3  \\ / F ield | cfMesh: A library for mesh generation
4  \\ / O peration |
5  \\ / A nd | Author: Franjo Juretic (franjo.juretic@c-fields.com)
6  \\/ M anipulation | Copyright (C) Creative Fields, Ltd.
7 -------------------------------------------------------------------------------
8 License
9  This file is part of cfMesh.
10 
11  cfMesh is free software; you can redistribute it and/or modify it
12  under the terms of the GNU General Public License as published by the
13  Free Software Foundation; either version 3 of the License, or (at your
14  option) any later version.
15 
16  cfMesh 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 cfMesh. If not, see <http://www.gnu.org/licenses/>.
23 
24 Description
25  Performs point relocations in the mesh (smoothing) in order to
26  improve quality measures. It does not make the mesh invalied.
27 
28 \*---------------------------------------------------------------------------*/
29 
30 #include "argList.H"
31 #include "polyMeshGenModifier.H"
32 #include "meshOptimizer.H"
33 
34 using namespace Foam;
35 
36 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
37 
38 int main(int argc, char *argv[])
39 {
40  argList::validArgs.clear();
41 
42  argList::validOptions.insert("nLoops", "int");
43  argList::validOptions.insert("nIterations", "int");
44  argList::validOptions.insert("nSurfaceIterations", "int");
45  argList::validOptions.insert("qualityThreshold", "scalar");
46  argList::validOptions.insert("constrainedCellsSet", "word");
47 
48 # include "setRootCase.H"
49 # include "createTime.H"
50 
51  //- read the settings
52  label nIterations(50);
53  label nLoops(10);
54  label nSurfaceIterations(2);
55  scalar qualityThreshold(0.1);
56 
57  if( args.options().found("nLoops") )
58  {
59  nLoops = readLabel(IStringStream(args.options()["nLoops"])());
60  }
61  else
62  {
63  Info << "Default number of loops is 10" << endl;
64  }
65 
66  if( args.options().found("nIterations") )
67  {
68  nIterations =
69  readLabel(IStringStream(args.options()["nIterations"])());
70  }
71  else
72  {
73  Info << "Default number of iterations is 50" << endl;
74  }
75 
76  if( args.options().found("nSurfaceIterations") )
77  {
78  nSurfaceIterations =
79  readLabel(IStringStream(args.options()["nSurfaceIterations"])());
80  }
81  else
82  {
83  Info << "Default number of surface iterations is 2" << endl;
84  }
85 
86  if( args.options().found("qualityThreshold") )
87  {
88  qualityThreshold =
89  readScalar(IStringStream(args.options()["qualityThreshold"])());
90  }
91  else
92  {
93  Info << "Using default quality threshold 0.1" << endl;
94  }
95 
96  word constrainedCellSet;
97 
98  if( args.options().found("constrainedCellsSet") )
99  {
100  constrainedCellSet = args.options()["constrainedCellsSet"];
101  }
102  else
103  {
104  Info << "No constraints applied on the smoothing procedure" << endl;
105  }
106 
107  //- load the mesh from disk
108  polyMeshGen pmg(runTime);
109  pmg.read();
110 
111  //- construct the smoother
112  meshOptimizer mOpt(pmg);
113 
114  if( !constrainedCellSet.empty() )
115  {
116  //- lock cells in constrainedCellSet
117  mOpt.lockCellsInSubset(constrainedCellSet);
118 
119  //- find boundary faces which shall be locked
120  labelLongList lockedBndFaces, selectedCells;
121 
122  const label sId = pmg.cellSubsetIndex(constrainedCellSet);
123  pmg.cellsInSubset(sId, selectedCells);
124 
125  boolList activeCell(pmg.cells().size(), false);
126  forAll(selectedCells, i)
127  activeCell[selectedCells[i]] = true;
128  }
129 
130  //- clear geometry information before volume smoothing
131  pmg.clearAddressingData();
132 
133  //- perform optimisation using the laplace smoother and
134  mOpt.optimizeMeshFV
135  (
136  nLoops,
137  nLoops,
138  nIterations,
139  nSurfaceIterations
140  );
141 
142  //- perform optimisation of worst quality faces
143  mOpt.optimizeMeshFVBestQuality(nLoops, qualityThreshold);
144 
145  //- check the mesh again and untangl bad regions if any of them exist
146  mOpt.untangleMeshFV(nLoops, nIterations, nSurfaceIterations);
147 
148  Info << "Writing mesh" << endl;
149  pmg.write();
150 
151  Info << "End\n" << endl;
152  return 0;
153 }
154 
155 // ************************************************************************* //
Foam::argList::validArgs
static SLList< string > validArgs
A list of valid (mandatory) arguments.
Definition: argList.H:143
Foam::meshOptimizer::optimizeMeshFV
void optimizeMeshFV(const label numLaplaceIterations=5, const label maxNumGlobalIterations=10, const label maxNumIterations=50, const label maxNumSurfaceIterations=2)
final optimisation
Definition: optimizeMeshFV.C:557
Foam::meshOptimizer
Definition: meshOptimizer.H:60
Foam::word
A class for handling words, derived from string.
Definition: word.H:59
forAll
#define forAll(list, i)
Loop across all elements in list.
Definition: UList.H:406
main
int main(int argc, char *argv[])
Definition: improveMeshQuality.C:38
polyMeshGenModifier.H
Foam::polyMeshGen
Definition: polyMeshGen.H:46
Foam::endl
Ostream & endl(Ostream &os)
Add newline and flush stream.
Definition: Ostream.H:251
Foam::meshOptimizer::lockCellsInSubset
void lockCellsInSubset(const word &subsetName)
Definition: meshOptimizer.C:199
labelLongList
This is a typedef for LongList<label>
Foam::polyMeshGen::read
void read()
Definition: polyMeshGen.C:121
Foam::argList::options
const Foam::HashTable< string > & options() const
Return options.
Definition: argListI.H:90
Foam::label
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
Foam::Info
messageStream Info
Foam::polyMeshGenCells::cells
const cellListPMG & cells() const
access to cells
Definition: polyMeshGenCellsI.H:39
argList.H
Foam::polyMeshGenCells::clearAddressingData
void clearAddressingData() const
clear addressing data
Definition: polyMeshGenCells.C:346
Foam::argList::validOptions
static HashTable< string > validOptions
A list of valid options.
Definition: argList.H:146
Foam::IStringStream
Input from memory buffer stream.
Definition: IStringStream.H:49
Foam
Namespace for OpenFOAM.
Definition: combustionModel.C:30
setRootCase.H
Foam::meshOptimizer::untangleMeshFV
void untangleMeshFV(const label maxNumGlobalIterations=10, const label maxNumIterations=50, const label maxNumSurfaceIterations=2, const bool relaxedCheck=false)
Definition: optimizeMeshFV.C:57
Foam::readScalar
bool readScalar(const char *buf, doubleScalar &s)
Read whole of buf as a scalar. Return true if succesful.
Definition: doubleScalar.H:63
Foam::List
A 1D array of objects of type <T>, where the size of the vector is known and used for subscript bound...
Definition: HashTable.H:59
Foam::polyMeshGen::write
void write() const
Definition: polyMeshGen.C:126
meshOptimizer.H
createTime.H
Foam::readLabel
label readLabel(Istream &is)
Definition: label.H:64
Foam::polyMeshGenCells::cellsInSubset
void cellsInSubset(const label, ListType &) const
Definition: polyMeshGenCellsI.H:107
args
Foam::argList args(argc, argv)
Foam::meshOptimizer::optimizeMeshFVBestQuality
void optimizeMeshFVBestQuality(const label maxNumIterations=50, const scalar threshold=0.1)
greedy optimisation until the mesh can be improved
Definition: optimizeMeshFV.C:580
Foam::cellListPMG::size
label size() const
return the number of used elements
Definition: cellListPMGI.H:56
Foam::polyMeshGenCells::cellSubsetIndex
label cellSubsetIndex(const word &) const
Definition: polyMeshGenCells.C:402