cellsToCells.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-2025 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 "cellsToCells.H"
27 #include "globalIndex.H"
28 #include "PatchTools.H"
29 #include "patchToPatchTools.H"
30 #include "emptyPolyPatch.H"
31 #include "wedgePolyPatch.H"
32 #include "processorPolyPatch.H"
33 #include "Time.H"
34 #include "pointInCell.H"
35 
36 // * * * * * * * * * * * * * * Static Data Members * * * * * * * * * * * * * //
37 
38 namespace Foam
39 {
42 }
43 
44 
45 // * * * * * * * * * * * * Protected Member Functions * * * * * * * * * * * //
46 
48 (
49  const polyMesh& srcMesh,
50  const polyMesh& tgtMesh
51 )
52 {
53  srcLocalTgtCells_.setSize(srcMesh.nCells());
54  srcWeights_.setSize(srcMesh.nCells());
55  forAll(srcLocalTgtCells_, srcCelli)
56  {
57  srcLocalTgtCells_[srcCelli].clear();
58  srcWeights_[srcCelli].clear();
59  }
60 
61  tgtLocalSrcCells_.setSize(tgtMesh.nCells());
62  tgtWeights_.setSize(tgtMesh.nCells());
63  forAll(tgtLocalSrcCells_, tgtCelli)
64  {
65  tgtLocalSrcCells_[tgtCelli].clear();
66  tgtWeights_[tgtCelli].clear();
67  }
68 }
69 
70 
72 (
73  const polyMesh& srcMesh,
74  const polyMesh& tgtMesh
75 ) const
76 {
78  (
79  max(srcMesh.bounds().min(), tgtMesh.bounds().min()),
80  min(srcMesh.bounds().max(), tgtMesh.bounds().max())
81  );
82 
83  meshBb.inflate(0.01);
84 
85  const cellList& srcCells = srcMesh.cells();
86  const faceList& srcFaces = srcMesh.faces();
87  const pointField& srcPts = srcMesh.points();
88 
89  DynamicList<label> resultDyn(srcMesh.nCells());
90  forAll(srcCells, srcCelli)
91  {
92  const boundBox cellBb
93  (
94  srcCells[srcCelli].points(srcFaces, srcPts),
95  false
96  );
97 
98  if (meshBb.overlaps(cellBb))
99  {
100  resultDyn.append(srcCelli);
101  }
102  }
103 
104  labelList result;
105  result.transfer(resultDyn);
106  return result;
107 }
108 
109 
111 (
112  const label celli,
113  const polyMesh& mesh,
114  const DynamicList<label>& visitedCells,
115  DynamicList<label>& nbrCells
116 ) const
117 {
118  // Get all cell-cells
119  const labelList& allNbrCells = mesh.cellCells()[celli];
120 
121  // Filter out cells already visited
122  forAll(allNbrCells, i)
123  {
124  const label nbrCelli = allNbrCells[i];
125 
126  if
127  (
128  findIndex(visitedCells, nbrCelli) == -1
129  && findIndex(nbrCells, nbrCelli) == -1
130  )
131  {
132  nbrCells.append(nbrCelli);
133  }
134  }
135 }
136 
137 
138 // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
139 
141 :
142  singleProcess_(-1),
143  srcLocalTgtCells_(),
144  tgtLocalSrcCells_(),
145  srcWeights_(),
146  tgtWeights_(),
147  srcMapPtr_(nullptr),
148  tgtMapPtr_(nullptr),
149  localSrcProcCellsPtr_(nullptr),
150  localTgtProcCellsPtr_(nullptr),
151  localTgtMeshPtr_(nullptr)
152 {}
153 
154 
155 // * * * * * * * * * * * * * * * * Destructor * * * * * * * * * * * * * * * //
156 
158 {}
159 
160 
161 // * * * * * * * * * * * * * * * * Selector * * * * * * * * * * * * * * * * //
162 
164 (
165  const word& cellsToCellsType
166 )
167 {
168  wordConstructorTable::iterator cstrIter =
169  wordConstructorTablePtr_->find(cellsToCellsType);
170 
171  if (cstrIter == wordConstructorTablePtr_->end())
172  {
174  << "Unknown " << typeName << " type "
175  << cellsToCellsType << endl << endl
176  << "Valid " << typeName << " types are : " << endl
177  << wordConstructorTablePtr_->sortedToc()
178  << exit(FatalError);
179  }
180 
181  return cstrIter()();
182 }
183 
184 
185 // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
186 
188 {
189  PackedBoolList result(srcLocalTgtCells_.size());
190  forAll(srcLocalTgtCells_, srcCelli)
191  {
192  result[srcCelli] = !srcLocalTgtCells_[srcCelli].empty();
193  }
194  return result;
195 }
196 
197 
199 {
200  PackedBoolList result(tgtLocalSrcCells_.size());
201  forAll(tgtLocalSrcCells_, tgtCelli)
202  {
203  result[tgtCelli] = !tgtLocalSrcCells_[tgtCelli].empty();
204  }
205  return result;
206 }
207 
208 
210 (
211  const polyMesh& tgtMesh,
212  const label srcCelli,
213  const point& p
214 ) const
215 {
216  forAll(srcLocalTgtCells_[srcCelli], i)
217  {
218  const label tgtCelli = srcLocalTgtCells_[srcCelli][i];
219 
220  const polyMesh& localTgtMesh =
221  singleProcess_ == -1 ? localTgtMeshPtr_() : tgtMesh;
222 
223  if (pointInCell(p, localTgtMesh, tgtCelli))
224  {
225  return
226  singleProcess_ == -1
227  ? localTgtProcCellsPtr_()[tgtCelli]
228  : remote(Pstream::myProcNo(), tgtCelli);
229  }
230  }
231 
232  return remote();
233 }
234 
235 
237 (
238  const polyMesh& srcMesh,
239  const polyMesh& tgtMesh
240 )
241 {
242  cpuTime time;
243 
244  // Determine numbers of faces on both sides, report, and quit if either
245  // side is empty
246  const label srcTotalSize = returnReduce(srcMesh.nCells(), sumOp<label>());
247  const label tgtTotalSize = returnReduce(tgtMesh.nCells(), sumOp<label>());
248  if (srcTotalSize == 0 || tgtTotalSize == 0)
249  {
250  return 0;
251  }
252 
253  Info<< indent << typeName << ": Calculating couplings between "
254  << srcTotalSize << " source cells and " << tgtTotalSize
255  << " target cells" << incrIndent << endl;
256 
257  singleProcess_ =
259  (
260  srcMesh.nCells(),
261  tgtMesh.nCells()
262  );
263 
264  scalar V = 0;
265 
266  if (isSingleProcess())
267  {
268  // Do the intersection
269  V = calculate(srcMesh, tgtMesh);
270 
271  // Normalise the weights
272  normalise(srcMesh, srcLocalTgtCells_, srcWeights_);
273  normalise(tgtMesh, tgtLocalSrcCells_, tgtWeights_);
274  }
275  else
276  {
277  // Create the target map of overlapping cells. This map gets remote
278  // parts of the target mesh so that everything needed to compute an
279  // intersection is available locally to the source. Use it to create a
280  // source-local target mesh.
281  tgtMapPtr_ =
283  (
284  tgtMeshSendCells(srcMesh, tgtMesh)
285  );
286  localTgtProcCellsPtr_.reset
287  (
288  new List<remote>
289  (
290  distributeMesh
291  (
292  tgtMapPtr_(),
293  tgtMesh,
294  localTgtMeshPtr_
295  )
296  )
297  );
298  const polyMesh& localTgtMesh = localTgtMeshPtr_();
299 
300  if (debug > 1)
301  {
302  Pout<< "Writing local target mesh: "
303  << localTgtMesh.name() << endl;
304  localTgtMesh.write();
305  }
306 
307  // Do the intersection
308  V = calculate(srcMesh, localTgtMesh);
309 
310  // Trim the local target mesh
311  trimLocalTgt();
312 
313  if (debug > 1)
314  {
315  Pout<< "Writing trimmed local target mesh: "
316  << localTgtMesh.name() << endl;
317  localTgtMesh.write();
318  }
319 
320  // Construct the source map
321  srcMapPtr_ =
323  (
325  (
326  tgtLocalSrcCells_,
327  localTgtProcCellsPtr_()
328  )
329  );
330  localSrcProcCellsPtr_.reset
331  (
332  new List<remote>
333  (
335  )
336  );
337 
338  // Collect the addressing on the target
340  (
341  tgtMesh.nCells(),
342  tgtMapPtr_(),
343  localSrcProcCellsPtr_(),
344  tgtLocalSrcCells_
345  );
346 
347  // Collect the weights on the target
349  (
350  tgtMesh.nCells(),
351  tgtMapPtr_(),
352  tgtWeights_
353  );
354 
355  // Normalise the weights
356  normalise(srcMesh, srcLocalTgtCells_, srcWeights_);
357  normalise(tgtMesh, tgtLocalSrcCells_, tgtWeights_);
358 
359  // Collect volume intersection contributions
360  reduce(V, sumOp<scalar>());
361  }
362 
363  label nCouples = 0;
364  forAll(srcLocalTgtCells_, srcCelli)
365  {
366  nCouples += srcLocalTgtCells_[srcCelli].size();
367  }
368  forAll(tgtLocalSrcCells_, tgtCelli)
369  {
370  nCouples += tgtLocalSrcCells_[tgtCelli].size();
371  }
372  reduce(nCouples, sumOp<label>());
373 
374  if (nCouples != 0)
375  {
376  Info<< indent << "Overlapping volume = " << V << endl
377  << indent << nCouples << " couplings calculated in "
378  << time.cpuTimeIncrement() << 's' << endl;
379  }
380  else
381  {
382  Info<< indent << "No couplings found" << endl;
383  }
384 
385  Info<< decrIndent;
386 
387  return V;
388 }
389 
390 
391 // ************************************************************************* //
#define forAll(list, i)
Loop across all elements in list.
Definition: UList.H:449
DynamicList< T, SizeInc, SizeMult, SizeDiv > & append(const T &)
Append an element at the end of the list.
Definition: DynamicListI.H:296
const word & name() const
Return name.
Definition: IOobject.H:307
void transfer(List< T > &)
Transfer the contents of the argument List into this list.
Definition: List.C:342
void clear()
Clear the list, i.e. set size to zero.
Definition: ListI.H:125
void setSize(const label)
Reset size of List.
Definition: List.C:281
A bit-packed bool list.
bool empty() const
Return true if the list is empty (ie, size() is zero).
Definition: PackedListI.H:702
static int myProcNo(const label communicator=0)
Number of this process (starting from masterNo() = 0)
Definition: UPstream.H:429
A bounding box defined in terms of the points at its extremities.
Definition: boundBox.H:60
const point & min() const
Minimum point defining the bounding box.
Definition: boundBoxI.H:88
const point & max() const
Maximum point defining the bounding box.
Definition: boundBoxI.H:94
Class to calculate interpolative addressing and weights between the cells of two overlapping meshes.
Definition: cellsToCells.H:56
cellsToCells()
Construct null.
Definition: cellsToCells.C:140
labelListList tgtLocalSrcCells_
For each target cell, the coupled local source cells.
Definition: cellsToCells.H:68
virtual ~cellsToCells()
Destructor.
Definition: cellsToCells.C:157
labelList maskCells(const polyMesh &srcMesh, const polyMesh &tgtMesh) const
Return src cell IDs for the overlap region.
Definition: cellsToCells.C:72
scalar update(const polyMesh &srcMesh, const polyMesh &tgtMesh)
Update addressing and weights for the given meshes. Returns the.
Definition: cellsToCells.C:237
void appendNbrCells(const label tgtCelli, const polyMesh &mesh, const DynamicList< label > &visitedTgtCells, DynamicList< label > &nbrTgtCellIDs) const
Append target cell neighbour cells to cellIDs list.
Definition: cellsToCells.C:111
remote srcToTgtPoint(const polyMesh &tgtMesh, const label srcCelli, const point &p) const
Find the target processor and cell associated with a point in a.
Definition: cellsToCells.C:210
PackedBoolList srcCoupled() const
Return a list indicating which source cells are coupled.
Definition: cellsToCells.C:187
static autoPtr< cellsToCells > New(const word &cellsToCellsType)
Select from name.
Definition: cellsToCells.C:164
scalarListList tgtWeights_
For each target cell, the coupled source weights.
Definition: cellsToCells.H:74
labelListList srcLocalTgtCells_
For each source cell, the coupled local target cells.
Definition: cellsToCells.H:65
scalarListList srcWeights_
For each source cell, the coupled target weights.
Definition: cellsToCells.H:71
PackedBoolList tgtCoupled() const
Return a list indicating which target cells are coupled.
Definition: cellsToCells.C:198
void initialise(const polyMesh &srcMesh, const polyMesh &tgtMesh)
Initialise the addressing and weights.
Definition: cellsToCells.C:48
Starts timing CPU usage and return elapsed time from start.
Definition: cpuTime.H:55
Mesh consisting of general polyhedral cells.
Definition: polyMesh.H:78
virtual const faceList & faces() const
Return raw faces.
Definition: polyMesh.C:1308
virtual const pointField & points() const
Return raw points.
Definition: polyMesh.C:1295
const boundBox & bounds() const
Return mesh bounding box.
Definition: polyMesh.H:399
const labelListList & cellCells() const
label nCells() const
const cellList & cells() const
virtual bool write(const bool write=true) const
Write using setting from DB.
Struct for keeping processor, element (cell, face, point) index.
Definition: remote.H:57
Template function which returns the un-mangled name of a given type. Useful for types which do not ha...
A class for handling words, derived from string.
Definition: word.H:63
Foam::fvMesh mesh(Foam::IOobject(regionName, runTime.name(), runTime, Foam::IOobject::MUST_READ), false)
#define FatalErrorInFunction
Report an error message using Foam::FatalError.
Definition: error.H:334
const pointField & points
const dimensionSet time
label calculate(const fvMesh &mesh, const labelHashSet &patchIDs, const scalar minFaceFraction, GeometricField< scalar, GeoMesh > &distance)
Calculate distance data from patches.
label singleProcess(const label srcSize, const label tgtSize)
Determine whether this intersection is confined to a single processor or.
autoPtr< distributionMap > constructDistributionMap(const labelListList &procSendIndices)
Turn a list of send-to-processor indices into a distribution map.
List< remote > distributeAddressing(const distributionMap &map)
Construct local addressing from the given distribution map. The result is a.
void rDistributeTgtAddressing(const label tgtSize, const distributionMap &tgtMap, const List< remote > &localSrcProcFaces, labelListList &tgtLocalSrcFaces)
Reverse distribute a set of target addressing.
static void rDistributeListList(const label size, const distributionMap &map, List< List< Type >> &data)
Reverse distribute a list-list given the map.
labelListList procSendIndices(const labelListList &tgtLocalSrcFaces, const List< remote > &localTgtProcFaces)
Given a local intersection addressing, determine what elements need sending.
Namespace for OpenFOAM.
errorManipArg< error, int > exit(error &err, const int errNo=1)
Definition: errorManip.H:124
Ostream & decrIndent(Ostream &os)
Decrement the indent level.
Definition: Ostream.H:272
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:288
messageStream Info
Ostream & incrIndent(Ostream &os)
Increment the indent level.
Definition: Ostream.H:265
void reduce(const List< UPstream::commsStruct > &comms, T &Value, const BinaryOp &bop, const int tag, const label comm)
defineRunTimeSelectionTable(fvConstraint, dictionary)
T returnReduce(const T &Value, const BinaryOp &bop, const int tag=Pstream::msgType(), const label comm=UPstream::worldComm)
dimensioned< Type > min(const DimensionedField< Type, GeoMesh, PrimitiveField > &df)
prefixOSstream Pout(cout, "Pout")
Definition: IOstreams.H:53
label findIndex(const ListType &, typename ListType::const_reference, const label start=0)
Find first occurrence of given element and return index,.
error FatalError
quaternion normalise(const quaternion &q)
Return the normalised (unit) quaternion of the given quaternion.
Definition: quaternionI.H:603
bool pointInCell(const point &p, const polyMesh &mesh, const label celli, const pointInCellShapes=pointInCellShapes::tets)
Test if a point is in a given cell.
Definition: pointInCell.C:155
defineTypeNameAndDebug(atmosphericBoundaryLayer, 0)
Ostream & indent(Ostream &os)
Indent stream.
Definition: Ostream.H:243
dimensioned< Type > max(const DimensionedField< Type, GeoMesh, PrimitiveField > &df)
Function for determining if a point is within a cell of a polyMesh.
List< treeBoundBox > meshBb(1, treeBoundBox(boundBox(coarseMesh.points(), false)).extend(1e-3))
volScalarField & p