meshToMesh.C
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) 2012-2023 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 \*---------------------------------------------------------------------------*/
25 
26 #include "meshToMesh.H"
27 #include "PatchTools.H"
28 #include "emptyPolyPatch.H"
29 #include "wedgePolyPatch.H"
30 #include "processorPolyPatch.H"
31 
32 // * * * * * * * * * * * * * * Static Data Members * * * * * * * * * * * * * //
33 
34 namespace Foam
35 {
37 }
38 
39 
40 // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
41 
43 (
44  const polyMesh& srcMesh,
45  const polyMesh& tgtMesh,
46  const word& engineType,
47  const HashTable<word>& patchMap
48 )
49 :
50  srcMesh_(srcMesh),
51  tgtMesh_(tgtMesh),
52  cellsInterpolation_(),
53  srcCellsStabilisation_(),
54  tgtCellsStabilisation_(),
55  patchIDs_(),
56  patchInterpolations_(),
57  srcPatchStabilisations_(),
58  tgtPatchStabilisations_()
59 {
60  // If no patch map was supplied, then assume a consistent pair of meshes in
61  // which corresponding patches have the same name
62  if (isNull(patchMap))
63  {
65 
66  forAll(srcMesh_.boundaryMesh(), srcPatchi)
67  {
68  const polyPatch& srcPp = srcMesh_.boundaryMesh()[srcPatchi];
69 
70  // We don't want to map empty or wedge patches, as by definition
71  // these do not hold relevant values. We also don't want to map
72  // processor patches as these are likely to differ between cases.
73  // In general, though, we do want to map constraint patches as they
74  // might have additional mappable properties; e.g., the jump field
75  // of a jump cyclic.
76  if
77  (
78  !isA<emptyPolyPatch>(srcPp)
79  && !isA<wedgePolyPatch>(srcPp)
80  && !isA<processorPolyPatch>(srcPp)
81  )
82  {
83  const label tgtPatchi =
84  tgtMesh_.boundaryMesh().findPatchID(srcPp.name());
85 
86  if (tgtPatchi == -1)
87  {
89  << "Source patch " << srcPp.name()
90  << " not found in target mesh. "
91  << "Available target patches are "
92  << tgtMesh_.boundaryMesh().names()
93  << exit(FatalError);
94  }
95 
96  patchIDs.append(labelPair(srcPatchi, tgtPatchi));
97  }
98  }
99 
100  patchIDs_.transfer(patchIDs);
101  }
102 
103  // If a patch mas was supplied then convert it to pairs of patch indices
104  else
105  {
106  patchIDs_.setSize(patchMap.size());
107  label i = 0;
108  forAllConstIter(HashTable<word>, patchMap, iter)
109  {
110  const word& tgtPatchName = iter.key();
111  const word& srcPatchName = iter();
112 
113  const label srcPatchi =
114  srcMesh_.boundaryMesh().findPatchID(srcPatchName);
115  const label tgtPatchi =
116  tgtMesh_.boundaryMesh().findPatchID(tgtPatchName);
117 
118  if (srcPatchi == -1)
119  {
121  << "Patch " << srcPatchName
122  << " not found in source mesh. "
123  << "Available source patches are "
124  << srcMesh_.boundaryMesh().names()
125  << exit(FatalError);
126  }
127  if (tgtPatchi == -1)
128  {
130  << "Patch " << tgtPatchName
131  << " not found in target mesh. "
132  << "Available target patches are "
133  << tgtMesh_.boundaryMesh().names()
134  << exit(FatalError);
135  }
136 
137  patchIDs_[i ++] = labelPair(srcPatchi, tgtPatchi);
138  }
139  }
140 
141  // Calculate cell addressing and weights
142  Info<< "Creating cellsToCells between source mesh "
143  << srcMesh_.name() << " and target mesh " << tgtMesh_.name()
144  << " using " << engineType << endl << incrIndent;
145 
146  cellsInterpolation_ = cellsToCells::New(engineType);
147  cellsInterpolation_->update(srcMesh_, tgtMesh_);
148 
149  srcCellsStabilisation_.clear();
150  tgtCellsStabilisation_.clear();
151 
152  Info<< decrIndent;
153 
154  // Calculate patch addressing and weights
155  patchInterpolations_.setSize(patchIDs_.size());
156  srcPatchStabilisations_.setSize(patchIDs_.size());
157  tgtPatchStabilisations_.setSize(patchIDs_.size());
158  forAll(patchIDs_, i)
159  {
160  const label srcPatchi = patchIDs_[i].first();
161  const label tgtPatchi = patchIDs_[i].second();
162 
163  const polyPatch& srcPp = srcMesh_.boundaryMesh()[srcPatchi];
164  const polyPatch& tgtPp = tgtMesh_.boundaryMesh()[tgtPatchi];
165 
166  Info<< "Creating patchToPatch between source patch "
167  << srcPp.name() << " and target patch " << tgtPp.name()
168  << " using " << engineType << endl << incrIndent;
169 
170  patchInterpolations_.set
171  (
172  i,
173  patchToPatch::New(engineType, true)
174  );
175 
176  patchInterpolations_[i].update
177  (
178  srcPp,
179  PatchTools::pointNormals(srcMesh_, srcPp),
180  tgtPp
181  );
182 
183  Info<< decrIndent;
184  }
185 }
186 
187 
188 // * * * * * * * * * * * * * * * * Destructor * * * * * * * * * * * * * * * //
189 
191 {}
192 
193 
194 // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
195 
197 {
198  boolList srcPatchIsMapped(srcMesh_.boundaryMesh().size(), false);
199  boolList tgtPatchIsMapped(tgtMesh_.boundaryMesh().size(), false);
200 
201  // Mark anything paired as mapped
202  forAll(patchIDs_, i)
203  {
204  const label srcPatchi = patchIDs_[i].first();
205  const label tgtPatchi = patchIDs_[i].second();
206 
207  srcPatchIsMapped[srcPatchi] = true;
208  tgtPatchIsMapped[tgtPatchi] = true;
209  }
210 
211  // Filter out un-mappable patches
212  forAll(srcMesh_.boundaryMesh(), srcPatchi)
213  {
214  const polyPatch& srcPp = srcMesh_.boundaryMesh()[srcPatchi];
215 
216  if
217  (
218  isA<emptyPolyPatch>(srcPp)
219  || isA<wedgePolyPatch>(srcPp)
220  || isA<processorPolyPatch>(srcPp)
221  )
222  {
223  srcPatchIsMapped[srcPp.index()] = true;
224  }
225  }
226  forAll(tgtMesh_.boundaryMesh(), tgtPatchi)
227  {
228  const polyPatch& tgtPp = tgtMesh_.boundaryMesh()[tgtPatchi];
229 
230  if
231  (
232  isA<emptyPolyPatch>(tgtPp)
233  || isA<wedgePolyPatch>(tgtPp)
234  || isA<processorPolyPatch>(tgtPp)
235  )
236  {
237  tgtPatchIsMapped[tgtPp.index()] = true;
238  }
239  }
240 
241  // Return whether or not everything is mapped
242  return
243  findIndex(srcPatchIsMapped, false) == -1
244  && findIndex(tgtPatchIsMapped, false) == -1;
245 }
246 
247 
249 (
250  const label srcCelli,
251  const point& p
252 ) const
253 {
254  return cellsInterpolation_().srcToTgtPoint(tgtMesh_, srcCelli, p);
255 }
256 
257 
258 // ************************************************************************* //
#define forAll(list, i)
Loop across all elements in list.
Definition: UList.H:434
#define forAllConstIter(Container, container, iter)
Iterate across all elements in the container object of type.
Definition: UList.H:477
A 1D vector of objects of type <T> that resizes itself as necessary to accept the new objects.
Definition: DynamicList.H:78
An STL-conforming hash table.
Definition: HashTable.H:127
label size() const
Return number of elements in table.
Definition: HashTableI.H:65
const word & name() const
Return name.
Definition: IOobject.H:310
static tmp< pointField > pointNormals(const polyMesh &, const PrimitivePatch< FaceList, PointField > &)
Return parallel consistent point normals for patches using mesh points.
static autoPtr< cellsToCells > New(const word &cellsToCellsType)
Select from name.
Definition: cellsToCells.C:163
Class to calculate interpolative addressing and weights between the cells and patches of two overlapp...
Definition: meshToMesh.H:54
virtual ~meshToMesh()
Destructor.
Definition: meshToMesh.C:190
remote srcToTgtPoint(const label srcCelli, const point &p) const
Find the target processor and cell associated with a point in a.
Definition: meshToMesh.C:249
const List< labelPair > & patchIDs() const
Return the list of corresponding source and target patch indices.
Definition: meshToMeshI.H:70
bool consistent() const
Is the interpolation between consistent meshes? I.e., are all.
Definition: meshToMesh.C:196
meshToMesh(const polyMesh &srcMesh, const polyMesh &tgtMesh, const word &engineType, const HashTable< word > &patchMap=NullObjectRef< HashTable< word >>())
Construct from source and target meshes. If a patchMap is supplied,.
Definition: meshToMesh.C:43
label index() const
Return the index of this patch in the boundaryMesh.
const word & name() const
Return name.
static autoPtr< patchToPatch > New(const word &patchToPatchType, const bool reverse)
Select from name.
Definition: patchToPatch.C:786
label findPatchID(const word &patchName) const
Find patch index given a name.
wordList names() const
Return a list of patch names.
Mesh consisting of general polyhedral cells.
Definition: polyMesh.H:80
const polyBoundaryMesh & boundaryMesh() const
Return boundary mesh.
Definition: polyMesh.H:403
A patch is a list of labels that address the faces in the global face list.
Definition: polyPatch.H:70
const polyBoundaryMesh & boundaryMesh() const
Return boundaryMesh reference.
Definition: polyPatch.C:270
Struct for keeping processor, element (cell, face, point) index.
Definition: remote.H:57
A class for handling words, derived from string.
Definition: word.H:62
#define FatalErrorInFunction
Report an error message using Foam::FatalError.
Definition: error.H:306
Namespace for OpenFOAM.
errorManipArg< error, int > exit(error &err, const int errNo=1)
Definition: errorManip.H:124
Pair< label > labelPair
Label pair.
Definition: labelPair.H:48
Ostream & decrIndent(Ostream &os)
Decrement the indent level.
Definition: Ostream.H:235
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
Ostream & endl(Ostream &os)
Add newline and flush stream.
Definition: Ostream.H:251
messageStream Info
Ostream & incrIndent(Ostream &os)
Increment the indent level.
Definition: Ostream.H:228
defineTypeNameAndDebug(combustionModel, 0)
bool isNull(const T &t)
Return true if t is a reference to the nullObject of type T.
Definition: nullObjectI.H:52
label findIndex(const ListType &, typename ListType::const_reference, const label start=0)
Find first occurrence of given element and return index,.
error FatalError
volScalarField & p