directMethod.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) 2013-2021 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 "directMethod.H"
27 #include "directAMI.H"
28 #include "indexedOctree.H"
29 #include "treeDataCell.H"
31 
32 // * * * * * * * * * * * * * * Static Data Members * * * * * * * * * * * * * //
33 
34 namespace Foam
35 {
36  defineTypeNameAndDebug(directMethod, 0);
37  addToRunTimeSelectionTable(meshToMeshMethod, directMethod, components);
38 }
39 
40 // * * * * * * * * * * * * Protected Member Functions * * * * * * * * * * * //
41 
43 (
44  const label srcCelli,
45  const label tgtCelli
46 ) const
47 {
48  return tgt_.pointInCell
49  (
50  src_.cellCentres()[srcCelli],
51  tgtCelli,
53  );
54 }
55 
56 
58 (
59  const labelList& srcCellIDs,
60  const boolList& mapFlag,
61  const label startSeedI,
62  label& srcSeedI,
63  label& tgtSeedI
64 ) const
65 {
66  const cellList& srcCells = src_.cells();
67  const faceList& srcFaces = src_.faces();
68  const pointField& srcPts = src_.points();
69 
70  for (label i = startSeedI; i < srcCellIDs.size(); i++)
71  {
72  label srcI = srcCellIDs[i];
73 
74  if (mapFlag[srcI])
75  {
76  const point srcCtr(srcCells[srcI].centre(srcPts, srcFaces));
77  label tgtI = tgt_.cellTree().findInside(srcCtr);
78 
79  if (tgtI != -1 && intersect(srcI, tgtI))
80  {
81  srcSeedI = srcI;
82  tgtSeedI = tgtI;
83 
84  return true;
85  }
86  }
87  }
88 
89  if (debug)
90  {
91  Pout<< "could not find starting seed" << endl;
92  }
93 
94  return false;
95 }
96 
97 
99 (
100  labelListList& srcToTgtCellAddr,
101  scalarListList& srcToTgtCellWght,
102  labelListList& tgtToSrcCellAddr,
103  scalarListList& tgtToSrcCellWght,
104  const label srcSeedI,
105  const label tgtSeedI,
106  const labelList& srcCellIDs, // not used
107  boolList& mapFlag,
108  label& startSeedI
109 )
110 {
111  // store a list of src cells already mapped
112  labelList srcTgtSeed(src_.nCells(), -1);
113 
114  List<DynamicList<label>> srcToTgt(src_.nCells());
115  List<DynamicList<label>> tgtToSrc(tgt_.nCells());
116 
117  DynamicList<label> srcSeeds(10);
118 
119  const scalarField& srcVc = src_.cellVolumes();
120  const scalarField& tgtVc = tgt_.cellVolumes();
121 
122  label srcCelli = srcSeedI;
123  label tgtCelli = tgtSeedI;
124 
125  do
126  {
127  // store src/tgt cell pair
128  srcToTgt[srcCelli].append(tgtCelli);
129  tgtToSrc[tgtCelli].append(srcCelli);
130 
131  // mark source cell srcSeedI as matched
132  mapFlag[srcCelli] = false;
133 
134  // accumulate intersection volume
135  V_ += srcVc[srcCelli];
136 
137  // find new source seed cell
138  appendToDirectSeeds
139  (
140  mapFlag,
141  srcTgtSeed,
142  srcSeeds,
143  srcCelli,
144  tgtCelli
145  );
146  }
147  while (srcCelli >= 0);
148 
149  // transfer addressing into persistent storage
150  forAll(srcToTgtCellAddr, i)
151  {
152  srcToTgtCellWght[i] = scalarList(srcToTgt[i].size(), srcVc[i]);
153  srcToTgtCellAddr[i].transfer(srcToTgt[i]);
154  }
155 
156  forAll(tgtToSrcCellAddr, i)
157  {
158  tgtToSrcCellWght[i] = scalarList(tgtToSrc[i].size(), tgtVc[i]);
159  tgtToSrcCellAddr[i].transfer(tgtToSrc[i]);
160  }
161 }
162 
163 
165 (
166  boolList& mapFlag,
167  labelList& srcTgtSeed,
168  DynamicList<label>& srcSeeds,
169  label& srcSeedI,
170  label& tgtSeedI
171 ) const
172 {
173  const labelList& srcNbr = src_.cellCells()[srcSeedI];
174  const labelList& tgtNbr = tgt_.cellCells()[tgtSeedI];
175 
176  forAll(srcNbr, i)
177  {
178  label srcI = srcNbr[i];
179 
180  if (mapFlag[srcI] && (srcTgtSeed[srcI] == -1))
181  {
182  // source cell srcI not yet mapped
183 
184  // identfy if target cell exists for source cell srcI
185  bool found = false;
186  forAll(tgtNbr, j)
187  {
188  label tgtI = tgtNbr[j];
189 
190  if (intersect(srcI, tgtI))
191  {
192  // new match - append to lists
193  found = true;
194 
195  srcTgtSeed[srcI] = tgtI;
196  srcSeeds.append(srcI);
197 
198  break;
199  }
200  }
201 
202  if (!found)
203  {
204  // no match available for source cell srcI
205  mapFlag[srcI] = false;
206  }
207  }
208  }
209 
210  if (srcSeeds.size())
211  {
212  srcSeedI = srcSeeds.remove();
213  tgtSeedI = srcTgtSeed[srcSeedI];
214  }
215  else
216  {
217  srcSeedI = -1;
218  tgtSeedI = -1;
219  }
220 }
221 
222 // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
223 
225 (
226  const polyMesh& src,
227  const polyMesh& tgt
228 )
229 :
230  meshToMeshMethod(src, tgt)
231 {}
232 
233 
234 // * * * * * * * * * * * * * * * * Destructor * * * * * * * * * * * * * * * //
235 
237 {}
238 
239 // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
240 
242 {
243  return directAMI::typeName;
244 }
245 
246 
248 (
249  labelListList& srcToTgtAddr,
250  scalarListList& srcToTgtWght,
251  labelListList& tgtToSrcAddr,
252  scalarListList& tgtToSrcWght
253 )
254 {
255  bool ok = initialise
256  (
257  srcToTgtAddr,
258  srcToTgtWght,
259  tgtToSrcAddr,
260  tgtToSrcWght
261  );
262 
263  if (!ok)
264  {
265  return;
266  }
267 
268  // (potentially) participating source mesh cells
269  const labelList srcCellIDs(maskCells());
270 
271  // list to keep track of whether src cell can be mapped
272  boolList mapFlag(src_.nCells(), false);
273  UIndirectList<bool>(mapFlag, srcCellIDs) = true;
274 
275  // find initial point in tgt mesh
276  label srcSeedI = -1;
277  label tgtSeedI = -1;
278  label startSeedI = 0;
279 
280  bool startWalk =
281  findInitialSeeds
282  (
283  srcCellIDs,
284  mapFlag,
285  startSeedI,
286  srcSeedI,
287  tgtSeedI
288  );
289 
290  if (startWalk)
291  {
292  calculateAddressing
293  (
294  srcToTgtAddr,
295  srcToTgtWght,
296  tgtToSrcAddr,
297  tgtToSrcWght,
298  srcSeedI,
299  tgtSeedI,
300  srcCellIDs,
301  mapFlag,
302  startSeedI
303  );
304  }
305  else
306  {
307  // if meshes are collocated, after inflating the source mesh bounding
308  // box tgt mesh cells may be transferred, but may still not overlap
309  // with the source mesh
310  return;
311  }
312 }
313 
314 
315 // ************************************************************************* //
#define forAll(list, i)
Loop across all elements in list.
Definition: UList.H:434
virtual void calculate(labelListList &srcToTgtAddr, scalarListList &srcToTgtWght, labelListList &tgtToTgtAddr, scalarListList &tgtToTgtWght)
Calculate addressing and weights.
Definition: directMethod.C:248
void size(const label)
Override size to be inconsistent with allocated storage.
Definition: ListI.H:164
Ostream & endl(Ostream &os)
Add newline and flush stream.
Definition: Ostream.H:251
directMethod(const polyMesh &src, const polyMesh &tgt)
Construct from source and target meshes.
Definition: directMethod.C:225
Macros for easy insertion into run-time selection tables.
virtual bool intersect(const label srcCelli, const label tgtCelli) const
Return the true if cells intersect.
Definition: directMethod.C:43
virtual bool findInitialSeeds(const labelList &srcCellIDs, const boolList &mapFlag, const label startSeedI, label &srcSeedI, label &tgtSeedI) const
Find indices of overlapping cells in src and tgt meshes - returns.
Definition: directMethod.C:58
A 1D vector of objects of type <T> that resizes itself as necessary to accept the new objects...
Definition: DynamicList.H:56
A class for handling words, derived from string.
Definition: word.H:59
virtual void calculateAddressing(labelListList &srcToTgtCellAddr, scalarListList &srcToTgtCellWght, labelListList &tgtToSrcCellAddr, scalarListList &tgtToSrcCellWght, const label srcSeedI, const label tgtSeedI, const labelList &srcCellIDs, boolList &mapFlag, label &startSeedI)
Calculate the mesh-to-mesh addressing and weights.
Definition: directMethod.C:99
List< scalar > scalarList
A List of scalars.
Definition: scalarList.H:50
DynamicList< T, SizeInc, SizeMult, SizeDiv > & append(const T &)
Append an element at the end of the list.
Definition: DynamicListI.H:296
virtual void appendToDirectSeeds(boolList &mapFlag, labelList &srcTgtSeed, DynamicList< label > &srcSeeds, label &srcSeedI, label &tgtSeedI) const
Append to list of src mesh seed indices.
Definition: directMethod.C:165
addToRunTimeSelectionTable(ensightPart, ensightPartCells, istream)
defineTypeNameAndDebug(combustionModel, 0)
virtual ~directMethod()
Destructor.
Definition: directMethod.C:236
Base class for mesh-to-mesh calculation methods.
T remove()
Remove and return the top element.
Definition: DynamicListI.H:351
virtual const word & AMImethod() const
Return the corresponding AMI method for patch interpolation.
Definition: directMethod.C:241
prefixOSstream Pout(cout, "Pout")
Definition: IOstreams.H:53
A List with indirect addressing.
Definition: fvMatrix.H:106
Mesh consisting of general polyhedral cells.
Definition: polyMesh.H:76
bool found
void transfer(List< T > &)
Transfer the contents of the argument List into this list.
Definition: List.C:342
Namespace for OpenFOAM.