directMethod.C
Go to the documentation of this file.
1 /*---------------------------------------------------------------------------*\
2  ========= |
3  \\ / F ield | OpenFOAM: The Open Source CFD Toolbox
4  \\ / O peration |
5  \\ / A nd | Copyright (C) 2013-2015 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 pointField
76  pts(srcCells[srcI].points(srcFaces, srcPts).xfer());
77 
78  forAll(pts, ptI)
79  {
80  const point& pt = pts[ptI];
81  label tgtI = tgt_.cellTree().findInside(pt);
82 
83  if (tgtI != -1 && intersect(srcI, tgtI))
84  {
85  srcSeedI = srcI;
86  tgtSeedI = tgtI;
87 
88  return true;
89  }
90  }
91  }
92  }
93 
94  if (debug)
95  {
96  Pout<< "could not find starting seed" << endl;
97  }
98 
99  return false;
100 }
101 
102 
104 (
105  labelListList& srcToTgtCellAddr,
106  scalarListList& srcToTgtCellWght,
107  labelListList& tgtToSrcCellAddr,
108  scalarListList& tgtToSrcCellWght,
109  const label srcSeedI,
110  const label tgtSeedI,
111  const labelList& srcCellIDs, // not used
112  boolList& mapFlag,
113  label& startSeedI
114 )
115 {
116  // store a list of src cells already mapped
117  labelList srcTgtSeed(src_.nCells(), -1);
118 
119  List<DynamicList<label> > srcToTgt(src_.nCells());
120  List<DynamicList<label> > tgtToSrc(tgt_.nCells());
121 
122  DynamicList<label> srcSeeds(10);
123 
124  const scalarField& srcVc = src_.cellVolumes();
125  const scalarField& tgtVc = tgt_.cellVolumes();
126 
127  label srcCellI = srcSeedI;
128  label tgtCellI = tgtSeedI;
129 
130  do
131  {
132  // store src/tgt cell pair
133  srcToTgt[srcCellI].append(tgtCellI);
134  tgtToSrc[tgtCellI].append(srcCellI);
135 
136  // mark source cell srcSeedI as matched
137  mapFlag[srcCellI] = false;
138 
139  // accumulate intersection volume
140  V_ += srcVc[srcCellI];
141 
142  // find new source seed cell
143  appendToDirectSeeds
144  (
145  mapFlag,
146  srcTgtSeed,
147  srcSeeds,
148  srcCellI,
149  tgtCellI
150  );
151  }
152  while (srcCellI >= 0);
153 
154  // transfer addressing into persistent storage
155  forAll(srcToTgtCellAddr, i)
156  {
157  srcToTgtCellWght[i] = scalarList(srcToTgt[i].size(), srcVc[i]);
158  srcToTgtCellAddr[i].transfer(srcToTgt[i]);
159  }
160 
161  forAll(tgtToSrcCellAddr, i)
162  {
163  tgtToSrcCellWght[i] = scalarList(tgtToSrc[i].size(), tgtVc[i]);
164  tgtToSrcCellAddr[i].transfer(tgtToSrc[i]);
165  }
166 }
167 
168 
170 (
171  boolList& mapFlag,
172  labelList& srcTgtSeed,
173  DynamicList<label>& srcSeeds,
174  label& srcSeedI,
175  label& tgtSeedI
176 ) const
177 {
178  const labelList& srcNbr = src_.cellCells()[srcSeedI];
179  const labelList& tgtNbr = tgt_.cellCells()[tgtSeedI];
180 
181  const vectorField& srcCentre = src_.cellCentres();
182 
183  forAll(srcNbr, i)
184  {
185  label srcI = srcNbr[i];
186 
187  if (mapFlag[srcI] && (srcTgtSeed[srcI] == -1))
188  {
189  // source cell srcI not yet mapped
190 
191  // identfy if target cell exists for source cell srcI
192  bool found = false;
193  forAll(tgtNbr, j)
194  {
195  label tgtI = tgtNbr[j];
196 
197  if
198  (
199  tgt_.pointInCell
200  (
201  srcCentre[srcI],
202  tgtI,
204  )
205  )
206  {
207  // new match - append to lists
208  found = true;
209 
210  srcTgtSeed[srcI] = tgtI;
211  srcSeeds.append(srcI);
212 
213  break;
214  }
215  }
216 
217  if (!found)
218  {
219  // no match available for source cell srcI
220  mapFlag[srcI] = false;
221  }
222  }
223  }
224 
225  if (srcSeeds.size())
226  {
227  srcSeedI = srcSeeds.remove();
228  tgtSeedI = srcTgtSeed[srcSeedI];
229  }
230  else
231  {
232  srcSeedI = -1;
233  tgtSeedI = -1;
234  }
235 }
236 
237 // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
238 
240 (
241  const polyMesh& src,
242  const polyMesh& tgt
243 )
244 :
245  meshToMeshMethod(src, tgt)
246 {}
247 
248 
249 // * * * * * * * * * * * * * * * * Destructor * * * * * * * * * * * * * * * //
250 
252 {}
253 
254 // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
255 
257 (
258  labelListList& srcToTgtAddr,
259  scalarListList& srcToTgtWght,
260  labelListList& tgtToSrcAddr,
261  scalarListList& tgtToSrcWght
262 )
263 {
264  bool ok = initialise
265  (
266  srcToTgtAddr,
267  srcToTgtWght,
268  tgtToSrcAddr,
269  tgtToSrcWght
270  );
271 
272  if (!ok)
273  {
274  return;
275  }
276 
277  // (potentially) participating source mesh cells
278  const labelList srcCellIDs(maskCells());
279 
280  // list to keep track of whether src cell can be mapped
281  boolList mapFlag(src_.nCells(), false);
282  UIndirectList<bool>(mapFlag, srcCellIDs) = true;
283 
284  // find initial point in tgt mesh
285  label srcSeedI = -1;
286  label tgtSeedI = -1;
287  label startSeedI = 0;
288 
289  bool startWalk =
290  findInitialSeeds
291  (
292  srcCellIDs,
293  mapFlag,
294  startSeedI,
295  srcSeedI,
296  tgtSeedI
297  );
298 
299  if (startWalk)
300  {
301  calculateAddressing
302  (
303  srcToTgtAddr,
304  srcToTgtWght,
305  tgtToSrcAddr,
306  tgtToSrcWght,
307  srcSeedI,
308  tgtSeedI,
309  srcCellIDs,
310  mapFlag,
311  startSeedI
312  );
313  }
314  else
315  {
316  // if meshes are collocated, after inflating the source mesh bounding
317  // box tgt mesh cells may be transferred, but may still not overlap
318  // with the source mesh
319  return;
320  }
321 }
322 
323 
324 // ************************************************************************* //
const pointField & points
void transfer(List< T > &)
Transfer the contents of the argument List into this list.
Definition: List.C:390
virtual void calculate(labelListList &srcToTgtAddr, scalarListList &srcToTgtWght, labelListList &tgtToTgtAddr, scalarListList &tgtToTgtWght)
Calculate addressing and weights.
Definition: directMethod.C:257
virtual bool intersect(const label srcCellI, const label tgtCellI) const
Return the true if cells intersect.
Definition: directMethod.C:42
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
void size(const label)
Override size to be inconsistent with allocated storage.
Definition: ListI.H:76
DynamicList< T, SizeInc, SizeMult, SizeDiv > & append(const T &)
Append an element at the end of the list.
Definition: DynamicListI.H:310
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:104
directMethod(const directMethod &)
Disallow default bitwise copy construct.
T remove()
Remove and return the top element.
Definition: DynamicListI.H:368
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
Namespace for OpenFOAM.
Base class for mesh-to-mesh calculation methods.
Ostream & endl(Ostream &os)
Add newline and flush stream.
Definition: Ostream.H:251
A List with indirect addressing.
Definition: fvMatrix.H:106
#define forAll(list, i)
Definition: UList.H:421
virtual ~directMethod()
Destructor.
Definition: directMethod.C:251
Macros for easy insertion into run-time selection tables.
List< scalar > scalarList
A List of scalars.
Definition: scalarList.H:50
bool found
Mesh consisting of general polyhedral cells.
Definition: polyMesh.H:74
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:170
void append(const T &)
Append an element at the end of the list.
Definition: ListI.H:97
addToRunTimeSelectionTable(ensightPart, ensightPartCells, istream)
defineTypeNameAndDebug(combustionModel, 0)
prefixOSstream Pout(cout,"Pout")
Definition: IOstreams.H:53