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 -------------------------------------------------------------------------------
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 Class
28  Foam::motionSmootherAlgo
29 
30 Description
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 
56 Note
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 
74 SourceFiles
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"
86 #include "indirectPrimitivePatch.H"
87 #include "className.H"
88 
89 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
90 
91 namespace Foam
92 {
93 
94 class polyMeshGeometry;
95 class faceSet;
96 
97 /*---------------------------------------------------------------------------*\
98  Class motionSmootherAlgo Declaration
99 \*---------------------------------------------------------------------------*/
100 
101 class motionSmootherAlgo
102 {
103  // Private class
104 
105  //- To synchronise displacements. We want max displacement since
106  // this is what is specified on pp and internal mesh will have
107  // zero displacement.
108  class maxMagEqOp
109  {
110  public:
111 
112  void operator()(vector& x, const vector& y) const
113  {
114  for (direction i = 0; i < vector::nComponents; i++)
115  {
116  scalar magX = mag(x[i]);
117  scalar magY = mag(y[i]);
118 
119  if (magX < magY)
120  {
121  x[i] = y[i];
122  }
123  else if (magX == magY)
124  {
125  if (y[i] > x[i])
126  {
127  x[i] = y[i];
128  }
129  }
130  }
131  }
132  };
133 
134 
135  // Private data
136 
137  //- Reference to polyMesh. Non-const since we move mesh.
138  polyMesh& mesh_;
139 
140  //- Reference to pointMesh
141  pointMesh& pMesh_;
142 
143  //- Reference to face subset of all adaptPatchIDs
145 
146  //- Displacement field
147  pointVectorField& displacement_;
148 
149  //- Scale factor for displacement
150  pointScalarField& scale_;
151 
152  //- Starting mesh position
153  pointField& oldPoints_;
154 
155 
156  // Internal data
157 
158  //- Indices of fixedValue patches that we're allowed to modify the
159  // displacement on.
160  const labelList adaptPatchIDs_;
161 
162  //- Smoothing and checking parameters
163  dictionary paramDict_;
164 
165  //- In test/dry-run mode?
166  const bool dryRun_;
167 
168  //- Is mesh point on boundary or not
169  bitSet isInternalPoint_;
170 
171  //- Is edge master (always except if on coupled boundary and on
172  // lower processor)
173  bitSet isMasterEdge_;
174 
175 
176  // Private Member Functions
177 
178  //- Average of connected points.
179  template<class Type>
181  (
183  const scalarField& edgeWeight
184  ) const;
185 
186  //- Average position of connected points.
187  template<class Type>
189  (
191  const scalarField& edgeWeight
192  ) const;
193 
194  //- Check constraints
195  template<class Type>
196  static void checkConstraints
197  (
199  );
200 
201  //- Test synchronisation of generic field (not positions!) on points
202  template<class Type, class CombineOp>
203  void testSyncField
204  (
205  const Field<Type>&,
206  const CombineOp& cop,
207  const Type& zero,
208  const scalar maxMag
209  ) const;
210 
211  //- Test synchronisation of points
212  void testSyncPositions(const pointField&, const scalar maxMag) const;
213 
214  static void checkFld(const pointScalarField&);
215 
216  //- Get points used by given faces
217  labelHashSet getPoints(const labelHashSet& faceLabels) const;
218 
219  //- Calculate per-edge weight
220  tmp<scalarField> calcEdgeWeights(const pointField&) const;
221 
222  //- Explicit smoothing and min on all affected internal points
223  void minSmooth
224  (
225  const scalarField& edgeWeights,
226  const bitSet& isAffectedPoint,
227  const pointScalarField& fld,
228  pointScalarField& newFld
229  ) const;
230 
231  //- Same but only on selected points (usually patch points)
232  void minSmooth
233  (
234  const scalarField& edgeWeights,
235  const bitSet& isAffectedPoint,
236  const labelList& meshPoints,
237  const pointScalarField& fld,
238  pointScalarField& newFld
239  ) const;
240 
241  //- Scale certain (internal) points of a field
242  void scaleField
243  (
244  const labelHashSet& pointLabels,
245  const scalar scale,
247  ) const;
248 
249  //- As above but points have to be in meshPoints as well
250  // (usually to scale patch points)
251  void scaleField
252  (
253  const labelList& meshPoints,
254  const labelHashSet& pointLabels,
255  const scalar scale,
257  ) const;
258 
259  //- Lower on internal points
260  void subtractField
261  (
262  const labelHashSet& pointLabels,
263  const scalar f,
265  ) const;
266 
267  //- As above but points have to be in meshPoints as well
268  // (usually to scale patch points)
269  void subtractField
270  (
271  const labelList& meshPoints,
272  const labelHashSet& pointLabels,
273  const scalar scale,
275  ) const;
276 
277  //- Given a set of faces that cause smoothing and a number of
278  // iterations determine the maximum set of points who are affected
279  // and the accordingly affected faces.
280  void getAffectedFacesAndPoints
281  (
282  const label nPointIter,
283  const faceSet& wrongFaces,
284 
285  labelList& affectedFaces,
286  bitSet& isAffectedPoint
287  ) const;
288 
289  //- No copy construct
290  motionSmootherAlgo(const motionSmootherAlgo&) = delete;
291 
292  //- No copy assignment
293  void operator=(const motionSmootherAlgo&) = delete;
294 
295 
296 public:
297 
298  ClassName("motionSmootherAlgo");
299 
300  // Constructors
301 
302  //- Construct from mesh, patches to work on and smoothing parameters.
304  (
305  polyMesh&,
306  pointMesh&,
307  indirectPrimitivePatch& pp, // 'outside' points
308  pointVectorField& displacement,
309  pointScalarField& scale,
310  pointField& oldPoints,
311  const labelList& adaptPatchIDs, // patches forming 'outside'
312  const dictionary& paramDict,
313  const bool dryRun = false
314  );
315 
316 
317  //- Destructor
319 
320 
321  // Member Functions
322 
323  // Access
324 
325  //- Reference to mesh
326  const polyMesh& mesh() const;
327 
328  //- Reference to pointMesh
329  const pointMesh& pMesh() const;
330 
331  //- Reference to patch
332  const indirectPrimitivePatch& patch() const;
333 
334  //- Patch labels that are being adapted
335  const labelList& adaptPatchIDs() const;
336 
337  const dictionary& paramDict() const;
338 
339  //- Return reference to the point motion displacement field
341  {
342  return displacement_;
343  }
344 
345  //- Return const reference to the point motion displacement field
346  const pointVectorField& pointDisplacement() const
347  {
348  return displacement_;
349  }
350 
351 
352  // Edit
353 
354  //- Take over existing mesh position.
355  void correct();
356 
357  //- Set patch fields on patchIDs to be consistent with
358  // all other boundary conditions
359  static void setDisplacementPatchFields
360  (
361  const labelList& patchIDs,
363  );
364 
365  //- Set patch fields on displacement to be consistent with
366  // internal values.
368 
369  //- Set displacement field from displacement on patch points.
370  // Modify provided displacement to be consistent with actual
371  // boundary conditions on displacement. Note: resets the
372  // displacement to be 0 on coupled patches beforehand
373  // to make sure shared points
374  // partially on pp (on some processors) and partially not
375  // (on other processors) get the value from pp.
376  static void setDisplacement
377  (
378  const labelList& patchIDs,
379  const indirectPrimitivePatch& pp,
380  pointField& patchDisp,
381  pointVectorField& displacement
382  );
383 
384  void setDisplacement(pointField& patchDisp);
385 
386  //- Special correctBoundaryConditions which evaluates fixedValue
387  // patches first so they get overwritten with any constraint
388  // bc's.
390 
391  //- Apply optional point constraint (2d correction)
392  void modifyMotionPoints(pointField& newPoints) const;
393 
394  //- Get the current points (oldPoints+scale*displacement)
395  tmp<pointField> curPoints() const;
396 
397  //- Set the errorReduction (by how much to scale the displacement
398  // at error locations) parameter. Returns the old value.
399  // Set to 0 (so revert to old mesh) grows out one cell layer
400  // from error faces.
401  scalar setErrorReduction(const scalar);
402 
403  //- Move mesh with given scale. Return true if mesh ok or has
404  // less than nAllow errors, false
405  // otherwise and locally update scale. Smoothmesh=false means only
406  // patch points get moved.
407  // Parallel ok (as long as displacement field is consistent
408  // across patches)
409  bool scaleMesh
410  (
411  labelList& checkFaces,
412  const bool smoothMesh = true,
413  const label nAllow = 0
414  );
415 
416  //- Move mesh (with baffles) with given scale.
417  bool scaleMesh
418  (
419  labelList& checkFaces,
420  const List<labelPair>& baffles,
421  const bool smoothMesh = true,
422  const label nAllow = 0
423  );
424 
425  //- Move mesh with externally provided mesh constraints
426  bool scaleMesh
427  (
428  labelList& checkFaces,
429  const List<labelPair>& baffles,
430  const dictionary& paramDict,
431  const dictionary& meshQualityDict,
432  const bool smoothMesh = true,
433  const label nAllow = 0
434  );
435 
436 
437  //- Update for new mesh geometry
438  void movePoints();
439 
440  //- Update for new mesh topology
441  void updateMesh();
442 
443 
444  //- Check mesh with mesh settings in dict. Collects incorrect faces
445  // in set. Returns true if one or more faces in error.
446  // Parallel ok.
447  static bool checkMesh
448  (
449  const bool report,
450  const polyMesh& mesh,
451  const dictionary& dict,
452  labelHashSet& wrongFaces,
453  const bool dryRun = false
454  );
455 
456  //- Check (subset of mesh) with mesh settings in dict.
457  // Collects incorrect faces in set. Returns true if one
458  // or more faces in error. Parallel ok.
459  static bool checkMesh
460  (
461  const bool report,
462  const polyMesh& mesh,
463  const dictionary& dict,
464  const labelList& checkFaces,
465  labelHashSet& wrongFaces,
466  const bool dryRun = false
467  );
468 
469  //- Check (subset of mesh including baffles) with mesh settings
470  // in dict. Collects incorrect faces in set. Returns true if one
471  // or more faces in error. Parallel ok.
472  static bool checkMesh
473  (
474  const bool report,
475  const polyMesh& mesh,
476  const dictionary& dict,
477  const labelList& checkFaces,
478  const List<labelPair>& baffles,
479  labelHashSet& wrongFaces,
480  const bool dryRun = false
481  );
482 
483  //- Check part of mesh with mesh settings in dict.
484  // Collects incorrect faces in set. Returns true if one or
485  // more faces in error. Parallel ok.
486  static bool checkMesh
487  (
488  const bool report,
489  const dictionary& dict,
490  const polyMeshGeometry&,
491  const pointField&,
492  const labelList& checkFaces,
493  labelHashSet& wrongFaces,
494  const bool dryRun = false
495  );
496 
497  //- Check part of mesh including baffles with mesh settings in dict.
498  // Collects incorrect faces in set. Returns true if one or
499  // more faces in error. Parallel ok.
500  static bool checkMesh
501  (
502  const bool report,
503  const dictionary& dict,
504  const polyMeshGeometry&,
505  const pointField&,
506  const labelList& checkFaces,
507  const List<labelPair>& baffles,
508  labelHashSet& wrongFaces,
509  const bool dryRun = false
510  );
511 
512  // Helper functions to manipulate displacement vector.
513 
514  //- Fully explicit smoothing of fields (not positions)
515  // of internal points with varying diffusivity.
516  template<class Type>
517  void smooth
518  (
520  const scalarField& edgeWeight,
522  ) const;
523 
524  //- Wrapper around dictionary::get which does not exit
525  template<class Type>
526  static Type get
527  (
528  const dictionary& dict,
529  const word& keyword,
530  const bool noExit,
531  enum keyType::option matchOpt,
532  const Type& defaultValue = Zero
533  );
534 //
535 // //- Wrapper around dictionary::get which does not exit
536 // template<class Type>
537 // static Type get
538 // (
539 // const dictionary& dict,
540 // const word& keyword,
541 // const bool noExit,
542 // bool recursive,
543 // bool patternMatch
544 // );
545 };
546 
547 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
548 
549 } // End namespace Foam
550 
551 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
552 
553 #ifdef NoRepository
555 #endif
556 
557 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
558 
559 #endif
560 
561 // ************************************************************************* //
Foam::motionSmootherAlgo::setErrorReduction
scalar setErrorReduction(const scalar)
Set the errorReduction (by how much to scale the displacement.
Definition: motionSmootherAlgo.C:698
Foam::motionSmootherAlgo::modifyMotionPoints
void modifyMotionPoints(pointField &newPoints) const
Apply optional point constraint (2d correction)
Definition: motionSmootherAlgo.C:637
Foam::word
A class for handling words, derived from Foam::string.
Definition: word.H:65
Foam::bitSet
A bitSet stores bits (elements with only two states) in packed internal format and supports a variety...
Definition: bitSet.H:63
Foam::motionSmootherAlgo::correctBoundaryConditions
void correctBoundaryConditions(pointVectorField &) const
Special correctBoundaryConditions which evaluates fixedValue.
Definition: motionSmootherAlgo.C:570
Foam::tmp
A class for managing temporary objects.
Definition: PtrList.H:61
Foam::motionSmootherAlgo::ClassName
ClassName("motionSmootherAlgo")
Foam::Zero
static constexpr const zero Zero
Global zero (0)
Definition: zero.H:131
Foam::motionSmootherAlgo::correct
void correct()
Take over existing mesh position.
Definition: motionSmootherAlgo.C:391
Foam::motionSmootherAlgo::updateMesh
void updateMesh()
Update for new mesh topology.
Definition: motionSmootherAlgo.C:1033
Foam::motionSmootherAlgo::patch
const indirectPrimitivePatch & patch() const
Reference to patch.
Definition: motionSmootherAlgo.C:373
Foam::motionSmootherAlgo::adaptPatchIDs
const labelList & adaptPatchIDs() const
Patch labels that are being adapted.
Definition: motionSmootherAlgo.C:379
Foam::motionSmootherAlgo::curPoints
tmp< pointField > curPoints() const
Get the current points (oldPoints+scale*displacement)
Definition: motionSmootherAlgo.C:749
Foam::motionSmootherAlgo::scaleMesh
bool scaleMesh(labelList &checkFaces, const bool smoothMesh=true, const label nAllow=0)
Move mesh with given scale. Return true if mesh ok or has.
Definition: motionSmootherAlgo.C:712
Foam::faceSet
A list of face labels.
Definition: faceSet.H:51
Foam::motionSmootherAlgo::checkMesh
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.
Definition: motionSmootherAlgoCheck.C:462
Foam::motionSmootherAlgo::setDisplacementPatchFields
void setDisplacementPatchFields()
Set patch fields on displacement to be consistent with.
Definition: motionSmootherAlgo.C:461
Foam::HashSet< label, Hash< label > >
bitSet.H
Foam::polyMesh
Mesh consisting of general polyhedral cells.
Definition: polyMesh.H:77
motionSmootherAlgoTemplates.C
Foam::motionSmootherAlgo::~motionSmootherAlgo
~motionSmootherAlgo()
Destructor.
Definition: motionSmootherAlgo.C:355
Foam::Field< vector >
Foam::motionSmootherAlgo
Given a displacement moves the mesh by scaling the displacement back until there are no more mesh err...
Definition: motionSmootherAlgo.H:100
className.H
Macro definitions for declaring ClassName(), NamespaceName(), etc.
fld
gmvFile<< "tracers "<< particles.size()<< nl;for(const passiveParticle &p :particles){ gmvFile<< p.position().x()<< ' ';}gmvFile<< nl;for(const passiveParticle &p :particles){ gmvFile<< p.position().y()<< ' ';}gmvFile<< nl;for(const passiveParticle &p :particles){ gmvFile<< p.position().z()<< ' ';}gmvFile<< nl;for(const word &name :lagrangianScalarNames){ IOField< scalar > fld(IOobject(name, runTime.timeName(), cloud::prefix, mesh, IOobject::MUST_READ, IOobject::NO_WRITE))
Definition: gmvOutputLagrangian.H:23
Foam::motionSmootherAlgo::pointDisplacement
const pointVectorField & pointDisplacement() const
Return const reference to the point motion displacement field.
Definition: motionSmootherAlgo.H:345
indirectPrimitivePatch.H
HashSet.H
dict
dictionary dict
Definition: searchingEngine.H:14
Foam::dictionary
A list of keyword definitions, which are a keyword followed by a number of values (eg,...
Definition: dictionary.H:123
Foam
Namespace for OpenFOAM.
Definition: atmBoundaryLayer.C:33
Foam::pointMesh
Mesh representing a set of points created from polyMesh.
Definition: pointMesh.H:51
Foam::motionSmootherAlgo::paramDict
const dictionary & paramDict() const
Definition: motionSmootherAlgo.C:385
f
labelList f(nPoints)
Foam::Vector< scalar >
Foam::List< label >
Foam::motionSmootherAlgo::mesh
const polyMesh & mesh() const
Reference to mesh.
Definition: motionSmootherAlgo.C:361
Foam::motionSmootherAlgo::pointDisplacement
pointVectorField & pointDisplacement()
Return reference to the point motion displacement field.
Definition: motionSmootherAlgo.H:339
Foam::mag
dimensioned< typename typeOfMag< Type >::type > mag(const dimensioned< Type > &dt)
Foam::motionSmootherAlgo::setDisplacement
static void setDisplacement(const labelList &patchIDs, const indirectPrimitivePatch &pp, pointField &patchDisp, pointVectorField &displacement)
Set displacement field from displacement on patch points.
Definition: motionSmootherAlgo.C:468
Foam::motionSmootherAlgo::smooth
void smooth(const GeometricField< Type, pointPatchField, pointMesh > &fld, const scalarField &edgeWeight, GeometricField< Type, pointPatchField, pointMesh > &newFld) const
Fully explicit smoothing of fields (not positions)
Definition: motionSmootherAlgoTemplates.C:233
Foam::direction
uint8_t direction
Definition: direction.H:52
x
x
Definition: LISASMDCalcMethod2.H:52
Foam::motionSmootherAlgo::movePoints
void movePoints()
Update for new mesh geometry.
Definition: motionSmootherAlgo.C:688
Foam::motionSmootherAlgo::pMesh
const pointMesh & pMesh() const
Reference to pointMesh.
Definition: motionSmootherAlgo.C:367
Foam::polyMeshGeometry
Updateable mesh geometry and checking routines.
Definition: polyMeshGeometry.H:55
Foam::GeometricField
Generic GeometricField class.
Definition: areaFieldsFwd.H:53
Foam::motionSmootherAlgo::get
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.
Definition: motionSmootherAlgoTemplates.C:295
pointLabels
labelList pointLabels(nPoints, -1)
pointFields.H
Foam::VectorSpace< Vector< scalar >, scalar, 3 >::nComponents
static constexpr direction nComponents
Number of components in this vector space.
Definition: VectorSpace.H:101
Foam::keyType::option
option
Enumeration for the data type and search/match modes (bitmask)
Definition: keyType.H:78
y
scalar y
Definition: LISASMDCalcMethod1.H:14
Foam::PrimitivePatch
A list of faces which address into the list of points.
Definition: PrimitivePatch.H:79
Foam::zero
A class representing the concept of 0 (zero) that can be used to avoid manipulating objects known to ...
Definition: zero.H:62