nearestToCell.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 | www.openfoam.com
6  \\/ M anipulation |
7 -------------------------------------------------------------------------------
8  Copyright (C) 2011-2016 OpenFOAM Foundation
9  Copyright (C) 2017-2018 OpenCFD Ltd.
10 -------------------------------------------------------------------------------
11 License
12  This file is part of OpenFOAM.
13 
14  OpenFOAM is free software: you can redistribute it and/or modify it
15  under the terms of the GNU General Public License as published by
16  the Free Software Foundation, either version 3 of the License, or
17  (at your option) any later version.
18 
19  OpenFOAM is distributed in the hope that it will be useful, but WITHOUT
20  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
21  FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
22  for more details.
23 
24  You should have received a copy of the GNU General Public License
25  along with OpenFOAM. If not, see <http://www.gnu.org/licenses/>.
26 
27 \*---------------------------------------------------------------------------*/
28 
29 #include "nearestToCell.H"
30 #include "polyMesh.H"
32 #include "mappedPatchBase.H"
33 
34 // * * * * * * * * * * * * * * Static Data Members * * * * * * * * * * * * * //
35 
36 namespace Foam
37 {
38  defineTypeNameAndDebug(nearestToCell, 0);
39  addToRunTimeSelectionTable(topoSetSource, nearestToCell, word);
40  addToRunTimeSelectionTable(topoSetSource, nearestToCell, istream);
41  addToRunTimeSelectionTable(topoSetCellSource, nearestToCell, word);
42  addToRunTimeSelectionTable(topoSetCellSource, nearestToCell, istream);
44  (
45  topoSetCellSource,
46  nearestToCell,
47  word,
48  nearest
49  );
51  (
52  topoSetCellSource,
53  nearestToCell,
54  istream,
55  nearest
56  );
57 }
58 
59 
60 Foam::topoSetSource::addToUsageTable Foam::nearestToCell::usage_
61 (
62  nearestToCell::typeName,
63  "\n Usage: nearestToCell (pt0 .. ptn)\n\n"
64  " Select the nearest cell for each of the points pt0 ..ptn\n\n"
65 );
66 
67 
68 // * * * * * * * * * * * * * Private Member Functions * * * * * * * * * * * //
69 
70 void Foam::nearestToCell::combine(topoSet& set, const bool add) const
71 {
72  // All the info for nearest. Construct to miss
73  List<mappedPatchBase::nearInfo> nearest(points_.size());
74 
75  forAll(points_, pointi)
76  {
77  const label celli = mesh_.findNearestCell(points_[pointi]);
78  const point& cc = mesh_.cellCentres()[celli];
79 
80  nearest[pointi].first() = pointIndexHit(true, cc, celli);
81  nearest[pointi].second() = Tuple2<scalar, label>
82  (
83  magSqr(cc-points_[pointi]),
85  );
86  }
87 
88  Pstream::listCombineReduce(nearest, mappedPatchBase::nearestEqOp());
89 
90  for (const auto& near : nearest)
91  {
92  if (near.second().second() == Pstream::myProcNo())
93  {
94  addOrDelete(set, near.first().index(), add);
95  }
96  }
97 }
98 
99 
100 // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
101 
103 (
104  const polyMesh& mesh,
105  const pointField& points
106 )
107 :
109  points_(points)
110 {}
111 
112 
114 (
115  const polyMesh& mesh,
117 )
118 :
120  points_(std::move(points))
121 {}
122 
123 
125 (
126  const polyMesh& mesh,
127  const dictionary& dict
128 )
129 :
131  (
133  dict.get<pointField>("points")
134  )
135 {}
136 
137 
139 (
140  const polyMesh& mesh,
141  Istream& is
142 )
143 :
145  points_(checkIs(is))
146 {}
147 
148 
149 // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
150 
152 (
153  const topoSetSource::setAction action,
154  topoSet& set
155 ) const
156 {
157  if (action == topoSetSource::ADD || action == topoSetSource::NEW)
158  {
159  if (verbose_)
160  {
161  Info<< " Adding cells nearest to " << points_ << endl;
162  }
163 
164  combine(set, true);
165  }
166  else if (action == topoSetSource::SUBTRACT)
167  {
168  if (verbose_)
169  {
170  Info<< " Removing cells nearest to " << points_ << endl;
171  }
172 
173  combine(set, false);
174  }
175 }
176 
177 
178 // ************************************************************************* //
List< ReturnType > get(const UPtrList< T > &list, const AccessOp &aop)
List of values generated by applying the access operation to each list item.
dictionary dict
void size(const label n)
Older name for setAddressableSize.
Definition: UList.H:116
virtual void applyToSet(const topoSetSource::setAction action, topoSet &set) const
Apply specified action to the topoSet.
A list of keyword definitions, which are a keyword followed by a number of values (eg...
Definition: dictionary.H:129
Create a new set and ADD elements to it.
Add elements to current set.
An Istream is an abstract base class for all input systems (streams, files, token lists etc)...
Definition: Istream.H:57
T & first()
Access first element of the list, position [0].
Definition: UList.H:853
Ostream & endl(Ostream &os)
Add newline and flush stream.
Definition: Ostream.H:531
PointIndexHit< point > pointIndexHit
A PointIndexHit with a 3D point.
Definition: pointIndexHit.H:58
static int myProcNo(const label communicator=worldComm)
Rank of this process in the communicator (starting from masterNo()). Can be negative if the process i...
Definition: UPstream.H:1074
The topoSetCellSource is a intermediate class for handling topoSet sources for selecting cells...
void addOrDelete(topoSet &set, const label id, const bool add) const
Add or delete id from set. Add when &#39;add&#39; is true.
Macros for easy insertion into run-time selection tables.
nearestToCell(const polyMesh &mesh, const pointField &points)
Construct from components, copying points.
Definition: nearestToCell.C:96
addNamedToRunTimeSelectionTable(topoSetCellSource, badQualityToCell, word, badQuality)
#define forAll(list, i)
Loop across all elements in list.
Definition: stdFoam.H:421
AccessType combine(const UList< T > &lists, AccessOp aop=accessOp< T >())
Combines sub-lists into a single list.
Definition: ListListOps.C:62
vectorField pointField
pointField is a vectorField.
Definition: pointFieldFwd.H:38
dynamicFvMesh & mesh
const pointField & points
setAction
Enumeration defining various actions.
const vectorField & cellCentres() const
A topoSetCellSource to select cells whose cell centre nearest to given points.
const polyMesh & mesh_
Reference to the mesh.
void add(FieldField< Field1, typename typeOfSum< Type1, Type2 >::type > &f, const FieldField< Field1, Type1 > &f1, const FieldField< Field2, Type2 > &f2)
defineTypeNameAndDebug(combustionModel, 0)
General set of labels of mesh quantity (points, cells, faces).
Definition: topoSet.H:59
Subtract elements from current set.
vector point
Point is a vector.
Definition: point.H:37
Class with constructor to add usage string to table.
label findNearestCell(const point &location) const
Find the cell with the nearest cell centre to location.
messageStream Info
Information stream (stdout output on master, null elsewhere)
Mesh consisting of general polyhedral cells.
Definition: polyMesh.H:74
dimensioned< typename typeOfMag< Type >::type > magSqr(const dimensioned< Type > &dt)
Namespace for OpenFOAM.
addToRunTimeSelectionTable(functionObject, pointHistory, dictionary)
static void listCombineReduce(List< T > &values, const CombineOp &cop, const int tag=UPstream::msgType(), const label comm=UPstream::worldComm)
After completion all processors have the same data.