Loading...
Searching...
No Matches
motionSmootherAlgo.H
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-2016 OpenFOAM Foundation
9 Copyright (C) 2015-2018 OpenCFD Ltd.
10-------------------------------------------------------------------------------
11License
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
27Class
28 Foam::motionSmootherAlgo
29
30Description
31 Given a displacement moves the mesh by scaling the displacement back
32 until there are no more mesh errors.
33
34 Holds displacement field (read upon construction since need boundary
35 conditions) and scaling factor and optional patch number on which to
36 scale back displacement.
37
38 E.g.
39 \verbatim
40 // Construct iterative mesh mover.
41 motionSmoother meshMover(mesh, labelList(1, patchi));
42
43 // Set desired displacement:
44 meshMover.displacement() = ..
45
46 for (label iter = 0; iter < maxIter; iter++)
47 {
48 if (meshMover.scaleMesh(true))
49 {
50 Info<< "Successfully moved mesh" << endl;
51 return true;
52 }
53 }
54 \endverbatim
55
56Note
57 - Shared points (parallel): a processor can have points which are part of
58 pp on another processor but have no pp itself (i.e. it has points
59 and/or edges but no faces of pp). Hence we have to be careful when e.g.
60 synchronising displacements that the value from the processor which has
61 faces of pp get priority. This is currently handled in setDisplacement
62 by resetting the internal displacement to zero on coupled points
63 that are coupled to patch points before doing anything
64 else. The combine operator used will give preference to non-zero
65 values.
66
67 - Various routines take baffles. These are sets of boundary faces that
68 are treated as a single internal face. This is a hack used to apply
69 movement to internal faces.
70
71 - Mesh constraints are looked up from the supplied dictionary. (uses
72 recursive lookup)
73
74SourceFiles
75 motionSmootherAlgo.C
76 motionSmootherAlgoTemplates.C
77
78\*---------------------------------------------------------------------------*/
79
80#ifndef motionSmootherAlgo_H
81#define motionSmootherAlgo_H
82
83#include "pointFields.H"
84#include "HashSet.H"
85#include "bitSet.H"
87#include "className.H"
88
89// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
90
91namespace Foam
92{
93
95class faceSet;
97/*---------------------------------------------------------------------------*\
98 Class motionSmootherAlgo Declaration
99\*---------------------------------------------------------------------------*/
100
101class motionSmootherAlgo
102{
103 // Private data
104
105 //- Reference to polyMesh. Non-const since we move mesh.
106 polyMesh& mesh_;
107
108 //- Reference to pointMesh
109 pointMesh& pMesh_;
110
111 //- Reference to face subset of all adaptPatchIDs
113
114 //- Displacement field
115 pointVectorField& displacement_;
116
117 //- Scale factor for displacement
118 pointScalarField& scale_;
119
120 //- Starting mesh position
121 pointField& oldPoints_;
122
123
124 // Internal data
125
126 //- Indices of fixedValue patches that we're allowed to modify the
127 // displacement on.
128 const labelList adaptPatchIDs_;
129
130 //- Smoothing and checking parameters
131 dictionary paramDict_;
132
133 //- In test/dry-run mode?
134 const bool dryRun_;
135
136 //- Is mesh point on boundary or not
137 bitSet isInternalPoint_;
138
139 //- Is edge master (always except if on coupled boundary and on
140 // lower processor)
141 bitSet isMasterEdge_;
142
143
144 // Private Member Functions
145
146 //- Average of connected points.
147 template<class Type>
149 (
151 const scalarField& edgeWeight
152 ) const;
153
154 //- Average position of connected points.
155 template<class Type>
157 (
159 const scalarField& edgeWeight
160 ) const;
161
162 //- Check constraints
163 template<class Type>
164 static void checkConstraints
165 (
167 );
168
169 //- Test synchronisation of generic field (not positions!) on points
170 template<class Type, class CombineOp>
171 void testSyncField
172 (
173 const Field<Type>&,
174 const CombineOp& cop,
175 const Type& zero,
176 const scalar maxMag
177 ) const;
178
179 //- Test synchronisation of points
180 void testSyncPositions(const pointField&, const scalar maxMag) const;
181
182 static void checkFld(const pointScalarField&);
183
184 //- Get points used by given faces
185 labelHashSet getPoints(const labelHashSet& faceLabels) const;
186
187 //- Calculate per-edge weight
188 tmp<scalarField> calcEdgeWeights(const pointField&) const;
189
190 //- Explicit smoothing and min on all affected internal points
191 void minSmooth
192 (
193 const scalarField& edgeWeights,
194 const bitSet& isAffectedPoint,
195 const pointScalarField& fld,
196 pointScalarField& newFld
197 ) const;
198
199 //- Same but only on selected points (usually patch points)
200 void minSmooth
201 (
202 const scalarField& edgeWeights,
203 const bitSet& isAffectedPoint,
204 const labelList& meshPoints,
205 const pointScalarField& fld,
206 pointScalarField& newFld
207 ) const;
208
209 //- Scale certain (internal) points of a field
210 void scaleField
211 (
213 const scalar scale,
215 ) const;
216
217 //- As above but points have to be in meshPoints as well
218 // (usually to scale patch points)
219 void scaleField
220 (
221 const labelList& meshPoints,
223 const scalar scale,
225 ) const;
226
227 //- Lower on internal points
228 void subtractField
229 (
231 const scalar f,
233 ) const;
234
235 //- As above but points have to be in meshPoints as well
236 // (usually to scale patch points)
237 void subtractField
238 (
239 const labelList& meshPoints,
241 const scalar scale,
243 ) const;
244
245 //- Given a set of faces that cause smoothing and a number of
246 // iterations determine the maximum set of points who are affected
247 // and the accordingly affected faces.
248 void getAffectedFacesAndPoints
249 (
250 const label nPointIter,
251 const faceSet& wrongFaces,
252
253 labelList& affectedFaces,
254 bitSet& isAffectedPoint
255 ) const;
256
257 //- No copy construct
258 motionSmootherAlgo(const motionSmootherAlgo&) = delete;
259
260 //- No copy assignment
261 void operator=(const motionSmootherAlgo&) = delete;
262
263
264public:
265
266 ClassName("motionSmootherAlgo");
267
268 // Constructors
269
270 //- Construct from mesh, patches to work on and smoothing parameters.
271 motionSmootherAlgo
272 (
273 polyMesh&,
274 pointMesh&,
275 indirectPrimitivePatch& pp, // 'outside' points
276 pointVectorField& displacement,
277 pointScalarField& scale,
278 pointField& oldPoints,
279 const labelList& adaptPatchIDs, // patches forming 'outside'
280 const dictionary& paramDict,
281 const bool dryRun = false
282 );
283
284
285 //- Destructor
287
288
289 // Member Functions
290
291 // Access
292
293 //- Reference to mesh
294 const polyMesh& mesh() const;
295
296 //- Reference to pointMesh
297 const pointMesh& pMesh() const;
298
299 //- Reference to patch
300 const indirectPrimitivePatch& patch() const;
301
302 //- Patch labels that are being adapted
303 const labelList& adaptPatchIDs() const;
304
305 const dictionary& paramDict() const;
306
307 //- Return reference to the point motion displacement field
309 {
310 return displacement_;
311 }
312
313 //- Return const reference to the point motion displacement field
315 {
316 return displacement_;
317 }
318
319
320 // Edit
321
322 //- Take over existing mesh position.
323 void correct();
324
325 //- Set patch fields on patchIDs to be consistent with
326 // all other boundary conditions
328 (
329 const labelList& patchIDs,
331 );
332
333 //- Set patch fields on displacement to be consistent with
334 // internal values.
336
337 //- Set displacement field from displacement on patch points.
338 // Modify provided displacement to be consistent with actual
339 // boundary conditions on displacement. Note: resets the
340 // displacement to be 0 on coupled patches beforehand
341 // to make sure shared points
342 // partially on pp (on some processors) and partially not
343 // (on other processors) get the value from pp.
344 static void setDisplacement
345 (
346 const labelList& patchIDs,
348 pointField& patchDisp,
349 pointVectorField& displacement
350 );
351
352 void setDisplacement(pointField& patchDisp);
353
354 //- Special correctBoundaryConditions which evaluates fixedValue
355 // patches first so they get overwritten with any constraint
356 // bc's.
358
359 //- Apply optional point constraint (2d correction)
360 void modifyMotionPoints(pointField& newPoints) const;
361
362 //- Get the current points (oldPoints+scale*displacement)
364
365 //- Set the errorReduction (by how much to scale the displacement
366 // at error locations) parameter. Returns the old value.
367 // Set to 0 (so revert to old mesh) grows out one cell layer
368 // from error faces.
369 scalar setErrorReduction(const scalar);
370
371 //- Move mesh with given scale. Return true if mesh ok or has
372 // less than nAllow errors, false
373 // otherwise and locally update scale. Smoothmesh=false means only
374 // patch points get moved.
375 // Parallel ok (as long as displacement field is consistent
376 // across patches)
377 bool scaleMesh
378 (
379 labelList& checkFaces,
380 const bool smoothMesh = true,
381 const label nAllow = 0
382 );
383
384 //- Move mesh (with baffles) with given scale.
385 bool scaleMesh
386 (
387 labelList& checkFaces,
388 const List<labelPair>& baffles,
389 const bool smoothMesh = true,
390 const label nAllow = 0
391 );
392
393 //- Move mesh with externally provided mesh constraints
394 bool scaleMesh
395 (
396 labelList& checkFaces,
397 const List<labelPair>& baffles,
398 const dictionary& paramDict,
399 const dictionary& meshQualityDict,
400 const bool smoothMesh = true,
401 const label nAllow = 0
402 );
403
404
405 //- Update for new mesh geometry
406 void movePoints();
407
408 //- Update for new mesh topology
409 void updateMesh();
410
411
412 //- Check mesh with mesh settings in dict. Collects incorrect faces
413 // in set. Returns true if one or more faces in error.
414 // Parallel ok.
415 static bool checkMesh
416 (
417 const bool report,
418 const polyMesh& mesh,
419 const dictionary& dict,
420 labelHashSet& wrongFaces,
421 const bool dryRun = false
422 );
423
424 //- Check (subset of mesh) with mesh settings in dict.
425 // Collects incorrect faces in set. Returns true if one
426 // or more faces in error. Parallel ok.
427 static bool checkMesh
428 (
429 const bool report,
430 const polyMesh& mesh,
431 const dictionary& dict,
432 const labelList& checkFaces,
433 labelHashSet& wrongFaces,
434 const bool dryRun = false
435 );
436
437 //- Check (subset of mesh including baffles) with mesh settings
438 // in dict. Collects incorrect faces in set. Returns true if one
439 // or more faces in error. Parallel ok.
440 static bool checkMesh
441 (
442 const bool report,
443 const polyMesh& mesh,
444 const dictionary& dict,
445 const labelList& checkFaces,
446 const List<labelPair>& baffles,
447 labelHashSet& wrongFaces,
448 const bool dryRun = false
449 );
450
451 //- Check part of mesh with mesh settings in dict.
452 // Collects incorrect faces in set. Returns true if one or
453 // more faces in error. Parallel ok.
454 static bool checkMesh
455 (
456 const bool report,
457 const dictionary& dict,
458 const polyMeshGeometry&,
459 const pointField&,
460 const labelList& checkFaces,
461 labelHashSet& wrongFaces,
462 const bool dryRun = false
463 );
464
465 //- Check part of mesh including baffles with mesh settings in dict.
466 // Collects incorrect faces in set. Returns true if one or
467 // more faces in error. Parallel ok.
468 static bool checkMesh
469 (
470 const bool report,
471 const dictionary& dict,
472 const polyMeshGeometry&,
473 const pointField&,
474 const labelList& checkFaces,
475 const List<labelPair>& baffles,
476 labelHashSet& wrongFaces,
477 const bool dryRun = false
478 );
479
480 // Helper functions to manipulate displacement vector.
481
482 //- Fully explicit smoothing of fields (not positions)
483 // of internal points with varying diffusivity.
484 template<class Type>
485 void smooth
486 (
488 const scalarField& edgeWeight,
490 ) const;
491
492 //- Wrapper around dictionary::get which does not exit
493 template<class Type>
494 static Type get
495 (
496 const dictionary& dict,
497 const word& keyword,
498 const bool noExit,
499 enum keyType::option matchOpt,
500 const Type& defaultValue = Zero
501 );
502//
503// //- Wrapper around dictionary::get which does not exit
504// template<class Type>
505// static Type get
506// (
507// const dictionary& dict,
508// const word& keyword,
509// const bool noExit,
510// bool recursive,
511// bool patternMatch
512// );
513};
514
515// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
516
517} // End namespace Foam
518
519// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
520
521#ifdef NoRepository
523#endif
524
525// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
526
527#endif
528
529// ************************************************************************* //
Info<< nl;Info<< "Write faMesh in vtk format:"<< nl;{ vtk::uindirectPatchWriter writer(aMesh.patch(), fileName(aMesh.time().globalPath()/vtkBaseFileName));writer.writeGeometry();globalIndex procAddr(aMesh.nFaces());labelList cellIDs;if(UPstream::master()) { cellIDs.resize(procAddr.totalSize());for(const labelRange &range :procAddr.ranges()) { auto slice=cellIDs.slice(range);slice=identity(range);} } writer.beginCellData(4);writer.writeProcIDs();writer.write("cellID", cellIDs);writer.write("area", aMesh.S().field());writer.write("normal", aMesh.faceAreaNormals());writer.beginPointData(1);writer.write("normal", aMesh.pointAreaNormals());Info<< " "<< writer.output().name()<< nl;}{ vtk::lineWriter writer(aMesh.points(), aMesh.edges(), fileName(aMesh.time().globalPath()/(vtkBaseFileName+"-edges")));writer.writeGeometry();writer.beginCellData(4);writer.writeProcIDs();{ Field< scalar > fld(faMeshTools::flattenEdgeField(aMesh.magLe(), true))
labelList faceLabels(nFaceLabels)
uindirectPrimitivePatch pp(UIndirectList< face >(mesh.faces(), faceLabels), mesh.points())
labelList patchIDs
Generic templated field type that is much like a Foam::List except that it is expected to hold numeri...
Definition Field.H:172
Generic GeometricField class.
A 1D array of objects of type <T>, where the size of the vector is known and used for subscript bound...
Definition List.H:72
A bitSet stores bits (elements with only two states) in packed internal format and supports a variety...
Definition bitSet.H:61
A list of keyword definitions, which are a keyword followed by a number of values (eg,...
Definition dictionary.H:133
A list of face labels.
Definition faceSet.H:50
option
Enumeration for the data type and search/match modes (bitmask).
Definition keyType.H:80
void setDisplacementPatchFields()
Set patch fields on displacement to be consistent with.
tmp< pointField > curPoints() const
Get the current points (oldPoints+scale*displacement).
scalar setErrorReduction(const scalar)
Set the errorReduction (by how much to scale the displacement.
const labelList & adaptPatchIDs() const
Patch labels that are being adapted.
ClassName("motionSmootherAlgo")
static void setDisplacement(const labelList &patchIDs, const indirectPrimitivePatch &pp, pointField &patchDisp, pointVectorField &displacement)
Set displacement field from displacement on patch points.
static Type get(const dictionary &dict, const word &keyword, const bool noExit, enum keyType::option matchOpt, const Type &defaultValue=Zero)
Wrapper around dictionary::get which does not exit.
void correct()
Take over existing mesh position.
const polyMesh & mesh() const
Reference to mesh.
void movePoints()
Update for new mesh geometry.
const indirectPrimitivePatch & patch() const
Reference to patch.
void smooth(const GeometricField< Type, pointPatchField, pointMesh > &fld, const scalarField &edgeWeight, GeometricField< Type, pointPatchField, pointMesh > &newFld) const
Fully explicit smoothing of fields (not positions).
const pointMesh & pMesh() const
Reference to pointMesh.
const pointVectorField & pointDisplacement() const
Return const reference to the point motion displacement field.
static bool checkMesh(const bool report, const polyMesh &mesh, const dictionary &dict, labelHashSet &wrongFaces, const bool dryRun=false)
Check mesh with mesh settings in dict. Collects incorrect faces.
pointVectorField & pointDisplacement()
Return reference to the point motion displacement field.
const dictionary & paramDict() const
bool scaleMesh(labelList &checkFaces, const bool smoothMesh=true, const label nAllow=0)
Move mesh with given scale. Return true if mesh ok or has.
void updateMesh()
Update for new mesh topology.
void modifyMotionPoints(pointField &newPoints) const
Apply optional point constraint (2d correction).
Mesh representing a set of points created from polyMesh.
Definition pointMesh.H:49
Updateable mesh geometry and checking routines.
Mesh consisting of general polyhedral cells.
Definition polyMesh.H:79
A class for managing temporary objects.
Definition tmp.H:75
A class for handling words, derived from Foam::string.
Definition word.H:66
A class representing the concept of 0 (zero) that can be used to avoid manipulating objects known to ...
Definition zero.H:58
Macro definitions for declaring ClassName(), NamespaceName(), etc.
#define ClassName(TypeNameString)
Add typeName information from argument TypeNameString to a class.
Definition className.H:74
dynamicFvMesh & mesh
Namespace for OpenFOAM.
GeometricField< scalar, pointPatchField, pointMesh > pointScalarField
List< label > labelList
A List of labels.
Definition List.H:62
HashSet< label, Hash< label > > labelHashSet
A HashSet of labels, uses label hasher.
Definition HashSet.H:85
GeometricField< vector, pointPatchField, pointMesh > pointVectorField
Field< scalar > scalarField
Specialisation of Field<T> for scalar.
PrimitivePatch< IndirectList< face >, const pointField & > indirectPrimitivePatch
A PrimitivePatch with an IndirectList for the faces, const reference for the point field.
static constexpr const zero Zero
Global zero (0).
Definition zero.H:127
vectorField pointField
pointField is a vectorField.
labelList f(nPoints)
labelList pointLabels(nPoints, -1)
dictionary dict
cellMask correctBoundaryConditions()