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::listCombineGather(nearest, mappedPatchBase::nearestEqOp());
90 
91  for (const auto& near : nearest)
92  {
93  if (near.second().second() == Pstream::myProcNo())
94  {
95  addOrDelete(set, near.first().index(), add);
96  }
97  }
98 }
99 
100 
101 // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
102 
104 (
105  const polyMesh& mesh,
106  const pointField& points
107 )
108 :
110  points_(points)
111 {}
112 
113 
115 (
116  const polyMesh& mesh,
118 )
119 :
121  points_(std::move(points))
122 {}
123 
124 
126 (
127  const polyMesh& mesh,
128  const dictionary& dict
129 )
130 :
132  (
133  mesh,
134  dict.get<pointField>("points")
135  )
136 {}
137 
138 
140 (
141  const polyMesh& mesh,
142  Istream& is
143 )
144 :
146  points_(checkIs(is))
147 {}
148 
149 
150 // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
151 
153 (
154  const topoSetSource::setAction action,
155  topoSet& set
156 ) const
157 {
158  if (action == topoSetSource::ADD || action == topoSetSource::NEW)
159  {
160  if (verbose_)
161  {
162  Info<< " Adding cells nearest to " << points_ << endl;
163  }
164 
165  combine(set, true);
166  }
167  else if (action == topoSetSource::SUBTRACT)
168  {
169  if (verbose_)
170  {
171  Info<< " Removing cells nearest to " << points_ << endl;
172  }
173 
174  combine(set, false);
175  }
176 }
177 
178 
179 // ************************************************************************* //
Foam::addToRunTimeSelectionTable
addToRunTimeSelectionTable(decompositionMethod, kahipDecomp, dictionary)
Foam::nearestToCell::nearestToCell
nearestToCell(const polyMesh &mesh, const pointField &points)
Construct from components, copying points.
Definition: nearestToCell.C:104
Foam::topoSetSource::ADD
Add elements to current set.
Definition: topoSetSource.H:103
Foam::BitOps::set
void set(List< bool > &bools, const labelRange &range)
Set the specified range 'on' in a boolList.
Definition: BitOps.C:37
Foam::topoSetSource::addToUsageTable
Class with constructor to add usage string to table.
Definition: topoSetSource.H:129
Foam::endl
Ostream & endl(Ostream &os)
Add newline and flush stream.
Definition: Ostream.H:369
Foam::topoSetSource::setAction
setAction
Enumeration defining the valid actions.
Definition: topoSetSource.H:100
Foam::dictionary::get
T get(const word &keyword, enum keyType::option matchOpt=keyType::REGEX) const
Definition: dictionaryTemplates.C:107
Foam::topoSetSource::NEW
Create a new set and ADD elements to it.
Definition: topoSetSource.H:104
polyMesh.H
Foam::polyMesh
Mesh consisting of general polyhedral cells.
Definition: polyMesh.H:77
forAll
#define forAll(list, i)
Loop across all elements in list.
Definition: stdFoam.H:296
Foam::magSqr
dimensioned< typename typeOfMag< Type >::type > magSqr(const dimensioned< Type > &dt)
Foam::primitiveMesh::findNearestCell
label findNearestCell(const point &location) const
Find the cell with the nearest cell centre to location.
Definition: primitiveMeshFindCell.C:88
Foam::Field< vector >
Foam::nearestToCell
A topoSetCellSource to select cells whose cell centre nearest to given points.
Definition: nearestToCell.H:143
Foam::Istream
An Istream is an abstract base class for all input systems (streams, files, token lists etc)....
Definition: Istream.H:61
Foam::Info
messageStream Info
Information stream (stdout output on master, null elsewhere)
Foam::ListListOps::combine
AccessType combine(const UList< T > &lists, AccessOp aop=accessOp< T >())
Combines sub-lists into a single list.
Definition: ListListOps.C:69
Foam::addNamedToRunTimeSelectionTable
addNamedToRunTimeSelectionTable(topoSetCellSource, badQualityToCell, word, badQuality)
Foam::topoSet
General set of labels of mesh quantity (points, cells, faces).
Definition: topoSet.H:63
Foam::nearestToCell::applyToSet
virtual void applyToSet(const topoSetSource::setAction action, topoSet &set) const
Apply specified action to the topoSet.
Definition: nearestToCell.C:153
dict
dictionary dict
Definition: searchingEngine.H:14
Foam::dictionary
A list of keyword definitions, which are a keyword followed by a number of values (eg,...
Definition: dictionary.H:123
Foam::add
void add(FieldField< Field1, typename typeOfSum< Type1, Type2 >::type > &f, const FieldField< Field1, Type1 > &f1, const FieldField< Field2, Type2 > &f2)
Definition: FieldFieldFunctions.C:939
mesh
dynamicFvMesh & mesh
Definition: createDynamicFvMesh.H:6
addToRunTimeSelectionTable.H
Macros for easy insertion into run-time selection tables.
Foam::topoSetSource::SUBTRACT
Subtract elements from current set.
Definition: topoSetSource.H:105
Foam
Namespace for OpenFOAM.
Definition: atmBoundaryLayer.C:33
Foam::Pstream::listCombineGather
static void listCombineGather(const List< commsStruct > &comms, List< T > &Value, const CombineOp &cop, const int tag, const label comm)
Definition: combineGatherScatter.C:290
Foam::primitiveMesh::cellCentres
const vectorField & cellCentres() const
Definition: primitiveMeshCellCentresAndVols.C:84
Foam::pointIndexHit
PointIndexHit< point > pointIndexHit
A PointIndexHit for 3D points.
Definition: pointIndexHit.H:46
Foam::UPstream::myProcNo
static int myProcNo(const label communicator=worldComm)
Number of this process (starting from masterNo() = 0)
Definition: UPstream.H:463
Foam::topoSetCellSource
The topoSetCellSource is a intermediate class for handling topoSet sources for selecting cells.
Definition: topoSetCellSource.H:54
points
const pointField & points
Definition: gmvOutputHeader.H:1
nearestToCell.H
Foam::topoSetSource::addOrDelete
void addOrDelete(topoSet &set, const label id, const bool add) const
Add or delete id from set. Add when 'add' is true.
Definition: topoSetSource.C:171
Foam::Pstream::listCombineScatter
static void listCombineScatter(const List< commsStruct > &comms, List< T > &Value, const int tag, const label comm)
Scatter data. Reverse of combineGather.
Definition: combineGatherScatter.C:432
Foam::point
vector point
Point is a vector.
Definition: point.H:43
Foam::topoSetSource::mesh_
const polyMesh & mesh_
Reference to the mesh.
Definition: topoSetSource.H:156
Foam::defineTypeNameAndDebug
defineTypeNameAndDebug(combustionModel, 0)
mappedPatchBase.H