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