AMIInterpolation.H
Go to the documentation of this file.
1 /*---------------------------------------------------------------------------*\
2  ========= |
3  \\ / F ield | OpenFOAM: The Open Source CFD Toolbox
4  \\ / O peration | Website: https://openfoam.org
5  \\ / A nd | Copyright (C) 2011-2022 OpenFOAM Foundation
6  \\/ M anipulation |
7 -------------------------------------------------------------------------------
8 License
9  This file is part of OpenFOAM.
10 
11  OpenFOAM is free software: you can redistribute it and/or modify it
12  under the terms of the GNU General Public License as published by
13  the Free Software Foundation, either version 3 of the License, or
14  (at your option) any later version.
15 
16  OpenFOAM 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 OpenFOAM. If not, see <http://www.gnu.org/licenses/>.
23 
24 Class
25  Foam::AMIInterpolation
26 
27 Description
28  Interpolation class dealing with transfer of data between two
29  primitive patches with an arbitrary mesh interface (AMI).
30 
31  Based on the algorithm given in:
32 
33  Conservative interpolation between volume meshes by local Galerkin
34  projection, Farrell PE and Maddison JR, 2011, Comput. Methods Appl.
35  Mech Engrg, Volume 200, Issues 1-4, pp 89-100
36 
37  Interpolation requires that the two patches should have opposite
38  orientations (opposite normals). The 'reverseTarget' flag can be used to
39  reverse the orientation of the target patch.
40 
41 
42 SourceFiles
43  AMIInterpolation.C
44  AMIInterpolationParallelOps.C
45  AMIInterpolationTemplates.C
46 
47 \*---------------------------------------------------------------------------*/
48 
49 #ifndef AMIInterpolation_H
50 #define AMIInterpolation_H
51 
52 #include "className.H"
53 #include "searchableSurface.H"
54 #include "treeBoundBoxList.H"
55 #include "boolList.H"
56 #include "primitivePatch.H"
57 #include "faceAreaIntersect.H"
58 #include "faceAreaWeightAMI.H"
59 #include "globalIndex.H"
60 #include "ops.H"
61 
62 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
63 
64 namespace Foam
65 {
66 
67 /*---------------------------------------------------------------------------*\
68  Class AMIInterpolation Declaration
69 \*---------------------------------------------------------------------------*/
70 
71 class AMIInterpolation
72 {
73  // Private Data
74 
75  //- Interpolation method
76  const word methodName_;
77 
78  //- Flag to indicate that the two patches are co-directional and
79  // that the orientation of the target patch should be reversed
80  const bool reverseTarget_;
81 
82  //- Flag to indicate that the two patches must be matched/an overlap
83  // exists between them
84  const bool requireMatch_;
85 
86  //- Index of processor that holds all of both sides. -1 in all other
87  // cases
88  label singlePatchProc_;
89 
90  //- Threshold weight below which interpolation is deactivated
91  scalar lowWeightCorrection_;
92 
93 
94  // Source patch
95 
96  //- Source face areas
97  scalarField srcMagSf_;
98 
99  //- Addresses of target faces per source face
100  labelListList srcAddress_;
101 
102  //- Weights of target faces per source face
103  scalarListList srcWeights_;
104 
105  //- Sum of weights of target faces per source face
106  scalarField srcWeightsSum_;
107 
108 
109  // Target patch
110 
111  //- Target face areas
112  scalarField tgtMagSf_;
113 
114  //- Addresses of source faces per target face
115  labelListList tgtAddress_;
116 
117  //- Weights of source faces per target face
118  scalarListList tgtWeights_;
119 
120  //- Sum of weights of source faces per target face
121  scalarField tgtWeightsSum_;
122 
123 
124  //- Face triangulation mode
126 
127  //- Source map pointer - parallel running only
128  autoPtr<distributionMap> srcMapPtr_;
129 
130  //- Target map pointer - parallel running only
131  autoPtr<distributionMap> tgtMapPtr_;
132 
133 
134  // Parallel functionality
135 
136  //- Calculate if patches are on multiple processors
137  label calcDistribution
138  (
139  const primitivePatch& srcPatch,
140  const primitivePatch& tgtPatch
141  ) const;
142 
143  label calcOverlappingProcs
144  (
145  const List<treeBoundBoxList>& procBb,
146  const treeBoundBox& bb,
147  boolList& overlaps
148  ) const;
149 
150  void distributePatches
151  (
152  const distributionMap& map,
153  const primitivePatch& pp,
154  const globalIndex& gi,
155  List<faceList>& faces,
157  List<labelList>& tgtFaceIDs
158  ) const;
159 
160  void distributeAndMergePatches
161  (
162  const distributionMap& map,
163  const primitivePatch& tgtPatch,
164  const globalIndex& gi,
165  faceList& tgtFaces,
166  pointField& tgtPoints,
167  labelList& tgtFaceIDs
168  ) const;
169 
170  autoPtr<distributionMap> calcProcMap
171  (
172  const primitivePatch& srcPatch,
173  const primitivePatch& tgtPatch
174  ) const;
175 
176 
177  // Initialisation
178 
179  //- Project points to surface
180  void projectPointsToSurface
181  (
182  const searchableSurface& surf,
183  pointField& pts
184  ) const;
185 
186 
187  // Manipulation
188 
189  //- Sum the weights for each face
190  static void sumWeights
191  (
192  const scalarListList& wght,
193  scalarField& wghtSum
194  );
195 
196  //- As above, but for multiple sets of weights
197  static void sumWeights
198  (
199  const UPtrList<scalarListList>& wghts,
200  scalarField& wghtSum
201  );
202 
203  //- Print out information relating to the weights sum. Values close
204  // to one are ideal. This information acts as a measure of the
205  // quality of the AMI.
206  static void reportSumWeights
207  (
208  const scalarField& patchAreas,
209  const word& patchName,
210  const scalarField& wghtSum,
211  const scalar lowWeightTol
212  );
213 
214  //- Normalise the weights so that they sum to one for each face.
215  // This may stabilise the solution at the expense of accuracy.
216  static void normaliseWeights
217  (
218  scalarListList& wght,
219  const scalarField& wghtSum
220  );
221 
222  //- As above but for multiple sets of weights
223  static void normaliseWeights
224  (
226  const scalarField& wghtSum
227  );
228 
229 
230  // Constructor helpers
231 
232  static void agglomerate
233  (
234  const autoPtr<distributionMap>& targetMap,
235  const scalarField& fineSrcMagSf,
236  const labelListList& fineSrcAddress,
237  const scalarListList& fineSrcWeights,
238 
239  const labelList& sourceRestrictAddressing,
240  const labelList& targetRestrictAddressing,
241 
247  );
248 
249  void constructFromSurface
250  (
251  const primitivePatch& srcPatch,
252  const primitivePatch& tgtPatch,
253  const autoPtr<searchableSurface>& surfPtr,
254  const bool report
255  );
256 
257 public:
258 
259  //- Runtime type information
260  TypeName("cyclicAMI");
261 
262 
263  // Constructors
264 
265  //- Construct from components
267  (
268  const primitivePatch& srcPatch,
269  const primitivePatch& tgtPatch,
271  const bool requireMatch = true,
272  const word& methodName = faceAreaWeightAMI::typeName,
273  const scalar lowWeightCorrection = -1,
274  const bool reverseTarget = false,
275  const bool report = true
276  );
277 
278  //- Construct from components, with projection surface
280  (
281  const primitivePatch& srcPatch,
282  const primitivePatch& tgtPatch,
283  const autoPtr<searchableSurface>& surf,
285  const bool requireMatch = true,
286  const word& methodName = faceAreaWeightAMI::typeName,
287  const scalar lowWeightCorrection = -1,
288  const bool reverseTarget = false,
289  const bool report = true
290  );
291 
292  //- Construct from agglomeration of AMIInterpolation.
293  // Agglomeration passed in as new coarse size and addressing from fine
294  // from coarse
296  (
297  const AMIInterpolation& fineAMI,
298  const labelList& sourceRestrictAddressing,
299  const labelList& neighbourRestrictAddressing,
300  const bool report = false
301  );
302 
303  //- Disallow default bitwise copy construction
304  AMIInterpolation(const AMIInterpolation&) = delete;
305 
306 
307  //- Destructor
308  virtual ~AMIInterpolation();
309 
310 
311  // Member Functions
312 
313  // Access
314 
315  //- Set to -1, or the processor holding all faces (both sides) of
316  // the AMI
317  inline label singlePatchProc() const;
318 
319  //- Threshold weight below which interpolation is deactivated
320  inline scalar lowWeightCorrection() const;
321 
322  //- Return true if employing a 'lowWeightCorrection'
323  inline bool applyLowWeightCorrection() const;
324 
325 
326  // Source patch
327 
328  //- Return const access to source patch face areas
329  inline const scalarField& srcMagSf() const;
330 
331  //- Return const access to source patch addressing
332  inline const labelListList& srcAddress() const;
333 
334  //- Return const access to source patch weights
335  inline const scalarListList& srcWeights() const;
336 
337  //- Return access to source patch weights
338  inline scalarListList& srcWeights();
339 
340  //- Return const access to normalisation factor of source
341  // patch weights (i.e. the sum before normalisation)
342  inline const scalarField& srcWeightsSum() const;
343 
344  //- Return access to normalisation factor of source
345  // patch weights (i.e. the sum before normalisation)
346  inline scalarField& srcWeightsSum();
347 
348  //- Source map pointer - valid only if singlePatchProc = -1
349  // This gets source data into a form to be consumed by
350  // tgtAddress, tgtWeights
351  inline const distributionMap& srcMap() const;
352 
353 
354  // Target patch
355 
356  //- Return const access to target patch face areas
357  inline const scalarField& tgtMagSf() const;
358 
359  //- Return const access to target patch addressing
360  inline const labelListList& tgtAddress() const;
361 
362  //- Return const access to target patch weights
363  inline const scalarListList& tgtWeights() const;
364 
365  //- Return access to target patch weights
366  inline scalarListList& tgtWeights();
367 
368  //- Return const access to normalisation factor of target
369  // patch weights (i.e. the sum before normalisation)
370  inline const scalarField& tgtWeightsSum() const;
371 
372  //- Return access to normalisation factor of target
373  // patch weights (i.e. the sum before normalisation)
374  inline scalarField& tgtWeightsSum();
375 
376  //- Target map pointer - valid only if singlePatchProc=-1.
377  // This gets target data into a form to be consumed by
378  // srcAddress, srcWeights
379  inline const distributionMap& tgtMap() const;
380 
381 
382  // Manipulation
383 
384  //- Update addressing and weights
385  void update
386  (
387  const primitivePatch& srcPatch,
388  const primitivePatch& tgtPatch,
389  const bool report
390  );
391 
392  //- Sum the weights on both sides of an AMI
393  static void sumWeights(AMIInterpolation& AMI);
394 
395  //- As above, but for multiple AMI-s
396  static void sumWeights(PtrList<AMIInterpolation>& AMIs);
397 
398  //- Print out information relating to the weights sum. Values close
399  // to one are ideal. This information acts as a measure of the
400  // quality of the AMI.
401  static void reportSumWeights(AMIInterpolation& AMI);
402 
403  //- Normalise the weights on both sides of an AMI
404  static void normaliseWeights(AMIInterpolation& AMI);
405 
406  //- As above, but for multiple AMI-s
407  static void normaliseWeights(UPtrList<AMIInterpolation>& AMIs);
408 
409 
410  // Evaluation
411 
412  // Low-level
413 
414  //- Interpolate from target to source with supplied op
415  // to combine existing value with remote value and weight
416  template<class Type, class CombineOp>
418  (
419  const UList<Type>& fld,
420  const CombineOp& cop,
421  List<Type>& result,
422  const UList<Type>& defaultValues = UList<Type>::null()
423  ) const;
424 
425  //- Interpolate from source to target with supplied op
426  // to combine existing value with remote value and weight
427  template<class Type, class CombineOp>
429  (
430  const UList<Type>& fld,
431  const CombineOp& cop,
432  List<Type>& result,
433  const UList<Type>& defaultValues = UList<Type>::null()
434  ) const;
435 
436 
437  //- Interpolate from target to source with supplied op
438  template<class Type, class CombineOp>
440  (
441  const Field<Type>& fld,
442  const CombineOp& cop,
443  const UList<Type>& defaultValues = UList<Type>::null()
444  ) const;
445 
446  //- Interpolate from target tmp field to source with supplied op
447  template<class Type, class CombineOp>
449  (
450  const tmp<Field<Type>>& tFld,
451  const CombineOp& cop,
452  const UList<Type>& defaultValues = UList<Type>::null()
453  ) const;
454 
455  //- Interpolate from source to target with supplied op
456  template<class Type, class CombineOp>
458  (
459  const Field<Type>& fld,
460  const CombineOp& cop,
461  const UList<Type>& defaultValues = UList<Type>::null()
462  ) const;
463 
464  //- Interpolate from source tmp field to target with supplied op
465  template<class Type, class CombineOp>
467  (
468  const tmp<Field<Type>>& tFld,
469  const CombineOp& cop,
470  const UList<Type>& defaultValues = UList<Type>::null()
471  ) const;
472 
473  //- Interpolate from target to source
474  template<class Type>
476  (
477  const Field<Type>& fld,
478  const UList<Type>& defaultValues = UList<Type>::null()
479  ) const;
480 
481  //- Interpolate from target tmp field
482  template<class Type>
484  (
485  const tmp<Field<Type>>& tFld,
486  const UList<Type>& defaultValues = UList<Type>::null()
487  ) const;
488 
489  //- Interpolate from source to target
490  template<class Type>
492  (
493  const Field<Type>& fld,
494  const UList<Type>& defaultValues = UList<Type>::null()
495  ) const;
496 
497  //- Interpolate from source tmp field
498  template<class Type>
500  (
501  const tmp<Field<Type>>& tFld,
502  const UList<Type>& defaultValues = UList<Type>::null()
503  ) const;
504 
505 
506  // Point intersections
507 
508  //- Return source patch face index of point on target patch face
510  (
511  const primitivePatch& srcPatch,
512  const primitivePatch& tgtPatch,
513  const vector& n,
514  const label tgtFacei,
515  point& tgtPoint
516  )
517  const;
518 
519  //- Return target patch face index of point on source patch face
521  (
522  const primitivePatch& srcPatch,
523  const primitivePatch& tgtPatch,
524  const vector& n,
525  const label srcFacei,
526  point& srcPoint
527  )
528  const;
529 
530  //- Calculate the patch face magnitudes for the given tri-mode
532  (
533  const primitivePatch& patch,
535  );
536 
537 
538  // Checks
539 
540  //- Write face connectivity as OBJ file
542  (
543  const primitivePatch& srcPatch,
544  const primitivePatch& tgtPatch,
545  const labelListList& srcAddress
546  ) const;
547 
548 
549  // Member Operators
550 
551  //- Disallow default bitwise assignment
552  void operator=(const AMIInterpolation&) = delete;
553 };
554 
555 
556 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
557 
558 } // End namespace Foam
559 
560 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
561 
562 #include "AMIInterpolationI.H"
563 
564 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
565 
566 #ifdef NoRepository
567  #include "AMIInterpolationTemplates.C"
568 #endif
569 
570 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
571 
572 #endif
573 
574 // ************************************************************************* //
void update(const primitivePatch &srcPatch, const primitivePatch &tgtPatch, const bool report)
Update addressing and weights.
label tgtPointFace(const primitivePatch &srcPatch, const primitivePatch &tgtPatch, const vector &n, const label srcFacei, point &srcPoint) const
Return target patch face index of point on source patch face.
label singlePatchProc() const
Set to -1, or the processor holding all faces (both sides) of.
const scalarListList & tgtWeights() const
Return const access to target patch weights.
void writeFaceConnectivity(const primitivePatch &srcPatch, const primitivePatch &tgtPatch, const labelListList &srcAddress) const
Write face connectivity as OBJ file.
label srcPointFace(const primitivePatch &srcPatch, const primitivePatch &tgtPatch, const vector &n, const label tgtFacei, point &tgtPoint) const
Return source patch face index of point on target patch face.
const labelListList & tgtAddress() const
Return const access to target patch addressing.
static tmp< scalarField > patchMagSf(const primitivePatch &patch, const faceAreaIntersect::triangulationMode triMode)
Calculate the patch face magnitudes for the given tri-mode.
Combination-Reduction operation for a parallel run.
Base class of (analytical or triangulated) surface. Encapsulates all the search routines. WIP.
const distributionMap & srcMap() const
Source map pointer - valid only if singlePatchProc = -1.
const scalarField & srcMagSf() const
Return const access to source patch face areas.
A list of faces which address into the list of points.
Calculates a unique integer (label so might not have enough room - 2G max) for processor + local inde...
Definition: globalIndex.H:63
gmvFile<< "tracers "<< particles.size()<< nl;forAllConstIter(Cloud< passiveParticle >, particles, iter){ gmvFile<< iter().position().x()<< ' ';}gmvFile<< nl;forAllConstIter(Cloud< passiveParticle >, particles, iter){ gmvFile<< iter().position().y()<< ' ';}gmvFile<< nl;forAllConstIter(Cloud< passiveParticle >, particles, iter){ gmvFile<< iter().position().z()<< ' ';}gmvFile<< nl;forAll(lagrangianScalarNames, i){ const word &name=lagrangianScalarNames[i];IOField< scalar > fld(IOobject(name, runTime.timeName(), cloud::prefix, mesh, IOobject::MUST_READ, IOobject::NO_WRITE))
const pointField & points
A class for handling words, derived from string.
Definition: word.H:59
const scalarField & tgtWeightsSum() const
Return const access to normalisation factor of target.
const distributionMap & tgtMap() const
Target map pointer - valid only if singlePatchProc=-1.
virtual ~AMIInterpolation()
Destructor.
void interpolateToTarget(const UList< Type > &fld, const CombineOp &cop, List< Type > &result, const UList< Type > &defaultValues=UList< Type >::null()) const
Interpolate from source to target with supplied op.
A templated 1D list of pointers to objects of type <T>, where the size of the array is known and used...
Definition: UPtrList.H:54
const scalarListList & srcWeights() const
Return const access to source patch weights.
void interpolateToSource(const UList< Type > &fld, const CombineOp &cop, List< Type > &result, const UList< Type > &defaultValues=UList< Type >::null()) const
Interpolate from target to source with supplied op.
const scalarField & srcWeightsSum() const
Return const access to normalisation factor of source.
Class containing processor-to-processor mapping information.
A templated 1D list of pointers to objects of type <T>, where the size of the array is known and used...
Definition: List.H:70
AMIInterpolation(const primitivePatch &srcPatch, const primitivePatch &tgtPatch, const faceAreaIntersect::triangulationMode &triMode, const bool requireMatch=true, const word &methodName=faceAreaWeightAMI::typeName, const scalar lowWeightCorrection=-1, const bool reverseTarget=false, const bool report=true)
Construct from components.
Macro definitions for declaring ClassName(), NamespaceName(), etc.
Interpolation class dealing with transfer of data between two primitive patches with an arbitrary mes...
Standard boundBox + extra functionality for use in octree.
Definition: treeBoundBox.H:87
label n
An auto-pointer similar to the STL auto_ptr but with automatic casting to a reference to the type and...
Definition: PtrList.H:52
A class for managing temporary objects.
Definition: PtrList.H:53
scalar lowWeightCorrection() const
Threshold weight below which interpolation is deactivated.
const labelListList & srcAddress() const
Return const access to source patch addressing.
void operator=(const AMIInterpolation &)=delete
Disallow default bitwise assignment.
TypeName("cyclicAMI")
Runtime type information.
const scalarField & tgtMagSf() const
Return const access to target patch face areas.
bool applyLowWeightCorrection() const
Return true if employing a &#39;lowWeightCorrection&#39;.
Namespace for OpenFOAM.