FreeFOAM The Cross-Platform CFD Toolkit
regionToCell.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) 1991-2010 OpenCFD Ltd.
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 "regionToCell.H"
27 #include <OpenFOAM/polyMesh.H>
28 #include <meshTools/regionSplit.H>
30 #include <meshTools/cellSet.H>
31 #include <OpenFOAM/syncTools.H>
32 
34 
35 // * * * * * * * * * * * * * * Static Data Members * * * * * * * * * * * * * //
36 
37 namespace Foam
38 {
39 
40 defineTypeNameAndDebug(regionToCell, 0);
41 
42 addToRunTimeSelectionTable(topoSetSource, regionToCell, word);
43 
44 addToRunTimeSelectionTable(topoSetSource, regionToCell, istream);
45 
46 }
47 
48 
49 Foam::topoSetSource::addToUsageTable Foam::regionToCell::usage_
50 (
51  regionToCell::typeName,
52  "\n Usage: regionToCell subCellSet (x y z)\n\n"
53  " Select all cells in the connected region containing point.\n"
54  " If started inside the subCellSet keeps to it;\n"
55  " if started outside stays outside.\n"
56 );
57 
58 
59 // * * * * * * * * * * * * * Private Member Functions * * * * * * * * * * * //
60 
61 void Foam::regionToCell::combine(topoSet& set, const bool add) const
62 {
63  label cellI = mesh_.findCell(insidePoint_);
64 
65  // Load the subset of cells
66  boolList blockedFace(mesh_.nFaces(), false);
67  {
68  Info<< "Loading subset " << setName_ << " to delimit search region."
69  << endl;
70  cellSet subSet(mesh_, setName_);
71 
72  boolList inSubset(mesh_.nCells(), false);
73  forAllConstIter(cellSet, subSet, iter)
74  {
75  inSubset[iter.key()] = true;
76  }
77 
78  if (cellI != -1 && inSubset[cellI])
79  {
80  Pout<< "Point " << insidePoint_ << " is inside cellSet "
81  << setName_ << endl
82  << "Collecting all cells connected to " << cellI
83  << " and inside cellSet " << setName_ << endl;
84  }
85  else
86  {
87  Pout<< "Point " << insidePoint_ << " is outside cellSet "
88  << setName_ << endl
89  << "Collecting all cells connected to " << cellI
90  << " and outside cellSet " << setName_ << endl;
91  }
92 
93  // Get coupled cell status
94  label nInt = mesh_.nInternalFaces();
95  boolList neiSet(mesh_.nFaces()-nInt, false);
96  for (label faceI = nInt; faceI < mesh_.nFaces(); faceI++)
97  {
98  neiSet[faceI-nInt] = inSubset[mesh_.faceOwner()[faceI]];
99  }
100  syncTools::swapBoundaryFaceList(mesh_, neiSet, false);
101 
102  // Find faces inbetween subSet and non-subset.
103  for (label faceI = 0; faceI < nInt; faceI++)
104  {
105  bool ownInSet = inSubset[mesh_.faceOwner()[faceI]];
106  bool neiInSet = inSubset[mesh_.faceNeighbour()[faceI]];
107  blockedFace[faceI] = (ownInSet != neiInSet);
108  }
109  for (label faceI = nInt; faceI < mesh_.nFaces(); faceI++)
110  {
111  bool ownInSet = inSubset[mesh_.faceOwner()[faceI]];
112  bool neiInSet = neiSet[faceI-nInt];
113  blockedFace[faceI] = (ownInSet != neiInSet);
114  }
115  }
116 
117  // Find connected regions without crossing boundary of the cellset.
118  regionSplit regions(mesh_, blockedFace);
119 
120  // Get the region containing the insidePoint
121  label regionI = -1;
122 
123  if (cellI != -1)
124  {
125  // On processor that has found cell.
126  regionI = regions[cellI];
127  }
128 
129  reduce(regionI, maxOp<label>());
130 
131  if (regionI == -1)
132  {
133  WarningIn
134  (
135  "regionToCell::combine(topoSet&, const bool) const"
136  ) << "Point " << insidePoint_
137  << " is not inside the mesh." << nl
138  << "Bounding box of the mesh:" << mesh_.globalData().bb()
139  << endl;
140  return;
141  }
142 
143 
144  // Pick up the cells of the region
145  const labelList regionCells(findIndices(regions, regionI));
146 
147  forAll(regionCells, i)
148  {
149  addOrDelete(set, regionCells[i], add);
150  }
151 }
152 
153 
154 // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
155 
156 // Construct from components
158 (
159  const polyMesh& mesh,
160  const word& setName,
161  const point& insidePoint
162 )
163 :
164  topoSetSource(mesh),
165  setName_(setName),
166  insidePoint_(insidePoint)
167 {}
168 
169 
170 // Construct from dictionary
172 (
173  const polyMesh& mesh,
174  const dictionary& dict
175 )
176 :
177  topoSetSource(mesh),
178  setName_(dict.lookup("set")),
179  insidePoint_(dict.lookup("insidePoint"))
180 {}
181 
182 
183 // Construct from Istream
185 (
186  const polyMesh& mesh,
187  Istream& is
188 )
189 :
190  topoSetSource(mesh),
191  setName_(checkIs(is)),
192  insidePoint_(checkIs(is))
193 {}
194 
195 
196 // * * * * * * * * * * * * * * * * Destructor * * * * * * * * * * * * * * * //
197 
199 {}
200 
201 
202 // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
203 
205 (
206  const topoSetSource::setAction action,
207  topoSet& set
208 ) const
209 {
210  if ((action == topoSetSource::NEW) || (action == topoSetSource::ADD))
211  {
212  Info<< " Adding all cells of connected region containing point "
213  << insidePoint_ << " ..." << endl;
214 
215  combine(set, true);
216  }
217  else if (action == topoSetSource::DELETE)
218  {
219  Info<< " Removing all cells of connected region containing point "
220  << insidePoint_ << " ..." << endl;
221 
222  combine(set, false);
223  }
224 }
225 
226 
227 // ************************ vim: set sw=4 sts=4 et: ************************ //