nearestToPoint.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 "nearestToPoint.H"
30 #include "polyMesh.H"
32 #include "mappedPatchBase.H"
33 
34 // * * * * * * * * * * * * * * Static Data Members * * * * * * * * * * * * * //
35 
36 namespace Foam
37 {
38  defineTypeNameAndDebug(nearestToPoint, 0);
39  addToRunTimeSelectionTable(topoSetSource, nearestToPoint, word);
40  addToRunTimeSelectionTable(topoSetSource, nearestToPoint, istream);
41  addToRunTimeSelectionTable(topoSetPointSource, nearestToPoint, word);
42  addToRunTimeSelectionTable(topoSetPointSource, nearestToPoint, istream);
44  (
45  topoSetPointSource,
46  nearestToPoint,
47  word,
48  nearest
49  );
51  (
52  topoSetPointSource,
53  nearestToPoint,
54  istream,
55  nearest
56  );
57 }
58 
59 
60 Foam::topoSetSource::addToUsageTable Foam::nearestToPoint::usage_
61 (
62  nearestToPoint::typeName,
63  "\n Usage: nearestToPoint (pt0 .. ptn)\n\n"
64  " Select the nearest point for each of the points pt0 ..ptn\n\n"
65 );
66 
67 
68 // * * * * * * * * * * * * * Private Member Functions * * * * * * * * * * * //
69 
70 void Foam::nearestToPoint::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  // Do linear search since usually just a few points.
76  const pointField& pts = mesh_.points();
77 
78  forAll(points_, pointi)
79  {
80  if (pts.size())
81  {
82  label minPointi = 0;
83  scalar minDistSqr = magSqr(pts[minPointi] - points_[pointi]);
84 
85  for (label i = 1; i < pts.size(); i++)
86  {
87  scalar distSqr = magSqr(pts[i] - points_[pointi]);
88 
89  if (distSqr < minDistSqr)
90  {
91  minDistSqr = distSqr;
92  minPointi = i;
93  }
94  }
95 
96  const point& minPt = pts[minPointi];
97  nearest[pointi].first() = pointIndexHit(true, minPt, minPointi);
98  nearest[pointi].second() = Tuple2<scalar, label>
99  (
100  magSqr(minPt-points_[pointi]),
102  );
103  }
104  }
105 
106 
107  Pstream::listCombineReduce(nearest, mappedPatchBase::nearestEqOp());
108 
109  for (const auto& near : nearest)
110  {
111  if (near.second().second() == Pstream::myProcNo())
112  {
113  addOrDelete(set, near.first().index(), add);
114  }
115  }
116 }
117 
118 
119 // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
120 
122 (
123  const polyMesh& mesh,
124  const pointField& points
125 )
126 :
128  points_(points)
129 {}
130 
131 
133 (
134  const polyMesh& mesh,
136 )
137 :
139  points_(std::move(points))
140 {}
141 
142 
144 (
145  const polyMesh& mesh,
146  const dictionary& dict
147 )
148 :
149  nearestToPoint(mesh, dict.get<pointField>("points"))
150 {}
151 
152 
154 (
155  const polyMesh& mesh,
156  Istream& is
157 )
158 :
160  points_(checkIs(is))
161 {}
162 
163 
164 // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
165 
167 (
168  const topoSetSource::setAction action,
169  topoSet& set
170 ) const
171 {
172  if (action == topoSetSource::ADD || action == topoSetSource::NEW)
173  {
174  if (verbose_)
175  {
176  Info<< " Adding points nearest to " << points_ << endl;
177  }
178 
179  combine(set, true);
180  }
181  else if (action == topoSetSource::SUBTRACT)
182  {
183  if (verbose_)
184  {
185  Info<< " Removing points nearest to " << points_ << endl;
186  }
187 
188  combine(set, false);
189  }
190 }
191 
192 
193 // ************************************************************************* //
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
The topoSetPointSource is a intermediate class for handling topoSet sources for selecting points...
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.
nearestToPoint(const polyMesh &mesh, const pointField &points)
Construct from components, copying points.
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
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
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.
addNamedToRunTimeSelectionTable(topoSetCellSource, badQualityToCell, word, badQuality)
virtual const pointField & points() const
Return raw points.
Definition: polyMesh.C:1078
#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 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)
virtual void applyToSet(const topoSetSource::setAction action, topoSet &set) const
Apply specified action to the topoSet.
General set of labels of mesh quantity (points, cells, faces).
Definition: topoSet.H:59
A topoSetPointSource to select points nearest to given points.
Subtract elements from current set.
vector point
Point is a vector.
Definition: point.H:37
Class with constructor to add usage string to table.
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.
const pointField & pts