linearDistance.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) 2012-2015 OpenFOAM Foundation
9  Copyright (C) 2018-2022 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 "linearDistance.H"
31 #include "triSurfaceMesh.H"
32 #include "triSurfaceFields.H"
33 #include "volumeType.H"
34 
35 // * * * * * * * * * * * * * * Static Data Members * * * * * * * * * * * * * //
36 
37 namespace Foam
38 {
39  defineTypeNameAndDebug(linearDistance, 0);
40  addToRunTimeSelectionTable(cellSizeFunction, linearDistance, dictionary);
41 }
42 
43 
44 // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
45 
47 (
48  const dictionary& initialPointsDict,
49  const searchableSurface& surface,
50  const scalar& defaultCellSize,
51  const labelList regionIndices
52 )
53 :
54  cellSizeFunction
55  (
56  typeName,
57  initialPointsDict,
58  surface,
59  defaultCellSize,
60  regionIndices
61  ),
62  distanceCellSize_
63  (
64  coeffsDict().get<scalar>("distanceCellSizeCoeff") * defaultCellSize
65  ),
66  distance_
67  (
68  coeffsDict().get<scalar>("distanceCoeff") * defaultCellSize
69  ),
70  distanceSqr_(sqr(distance_))
71 {}
72 
73 
74 // * * * * * * * * * * * * Private Member Functions * * * * * * * * * * * * //
75 
76 Foam::scalar Foam::linearDistance::sizeFunction
77 (
78  const point& pt,
79  scalar d,
80  label index
81 ) const
82 {
83  const scalar interpolatedSize
84  = surfaceCellSizeFunction_().interpolate(pt, index);
85 
86  const scalar gradient
87  = (distanceCellSize_ - interpolatedSize)
88  /distance_;
89 
90  scalar size = gradient*d + interpolatedSize;
91 
92  return size;
93 }
94 
95 
96 // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
97 
99 (
100  const pointIndexHit& hitPt,
101  const vector& n,
102  pointField& shapePts,
103  scalarField& shapeSizes
104 ) const
105 {
106  const Foam::point& pt = hitPt.hitPoint();
107 
108  if (sideMode_ == rmBothsides)
109  {
110  shapePts.resize(2);
111  shapeSizes.resize(2);
112 
113  shapePts[0] = pt - n*distance_;
114  shapePts[1] = pt + n*distance_;
115 
116  shapeSizes[0] = distanceCellSize_;
117  shapeSizes[1] = distanceCellSize_;
118  }
119  else if (sideMode_ == smInside)
120  {
121  shapePts.resize(1);
122  shapeSizes.resize(1);
123 
124  shapePts[0] = pt - n*distance_;
125  shapeSizes[0] = distanceCellSize_;
126  }
127  else if (sideMode_ == smOutside)
128  {
129  shapePts.resize(1);
130  shapeSizes.resize(1);
131 
132  shapePts[0] = pt + n*distance_;
133  shapeSizes[0] = distanceCellSize_;
134  }
135 
136  return false;
137 }
138 
139 
140 bool Foam::linearDistance::cellSize(const point& pt, scalar& size) const
141 {
142  size = 0;
143 
144  List<pointIndexHit> hits;
145 
146  surface_.findNearest
147  (
148  pointField(1, pt),
149  scalarField(1, distanceSqr_),
150  regionIndices_,
151  hits
152  );
153 
154  const pointIndexHit& hitInfo = hits[0];
155 
156  if (hitInfo.hit())
157  {
158  const point& hitPt = hitInfo.point();
159  const label hitIndex = hitInfo.index();
160 
161  const scalar dist = hitPt.dist(pt);
162 
163  if (sideMode_ == rmBothsides)
164  {
165  size = sizeFunction(hitPt, dist, hitIndex);
166 
167  return true;
168  }
169 
170  // If the nearest point is essentially on the surface, do not do a
171  // getVolumeType calculation, as it will be prone to error.
172  if (dist < snapToSurfaceTol_)
173  {
174  size = sizeFunction(hitPt, 0, hitIndex);
175 
176  return true;
177  }
178 
179  pointField ptF(1, pt);
180  List<volumeType> vTL;
181 
182  surface_.getVolumeType(ptF, vTL);
183 
184  bool functionApplied = false;
185 
186  if
187  (
188  sideMode_ == smInside
189  && vTL[0] == volumeType::INSIDE
190  )
191  {
192  size = sizeFunction(hitPt, dist, hitIndex);
193 
194  functionApplied = true;
195  }
196  else if
197  (
198  sideMode_ == smOutside
199  && vTL[0] == volumeType::OUTSIDE
200  )
201  {
202  size = sizeFunction(hitPt, dist, hitIndex);
203 
204  functionApplied = true;
205  }
206 
207  return functionApplied;
208  }
209 
210  return false;
211 }
212 
213 
215 {
216 // labelHashSet surfaceAlreadyHit(surfaceCellSize_.size());
217 
218 // for (const Foam::point& pt : pts)
219 // {
220 // List<pointIndexHit> hits;
221 
222 // surface_.findNearest
223 // (
224 // pointField(1, pt),
225 // scalarField(1, distanceSqr_),
226 // regionIndices_,
227 // hits
228 // );
229 
230 // const label surfHitI = hits[0].index();
231 
232 // if
233 // (
234 // hits[0].hit()
235 // && !surfaceAlreadyHit.found(surfHitI)
236 // )
237 // {
238 // // Halving cell size is arbitrary
239 // surfaceCellSizeFunction_().refineSurfaceSize(surfHitI);
240 
241 // surfaceAlreadyHit.insert(surfHitI);
242 // }
243 // }
244 
245 // return true;
246  return false;
247 }
248 
249 
250 // ************************************************************************* //
List< ReturnType > get(const UPtrList< T > &list, const AccessOp &aop)
List of values generated by applying the access operation to each list item.
virtual bool setCellSize(const pointField &pts)
Adapt local cell size. Return true if anything changed.
Fields for triSurface.
dimensionedSymmTensor sqr(const dimensionedVector &dv)
PointIndexHit< point > pointIndexHit
A PointIndexHit with a 3D point.
Definition: pointIndexHit.H:58
Macros for easy insertion into run-time selection tables.
vectorField pointField
pointField is a vectorField.
Definition: pointFieldFwd.H:38
virtual bool cellSize(const point &pt, scalar &size) const
Modify scalar argument to the cell size specified by function.
Field< scalar > scalarField
Specialisation of Field<T> for scalar.
Vector< scalar > vector
Definition: vector.H:57
A location inside the volume.
Definition: volumeType.H:65
A location outside the volume.
Definition: volumeType.H:66
const wordList surface
Standard surface field types (scalar, vector, tensor, etc)
A Vector of values with scalar precision, where scalar is float/double depending on the compilation f...
defineTypeNameAndDebug(combustionModel, 0)
virtual bool sizeLocations(const pointIndexHit &hitPt, const vector &n, pointField &shapePts, scalarField &shapeSizes) const
addToRunTimeSelectionTable(decompositionMethod, kahipDecomp, dictionary)
vector point
Point is a vector.
Definition: point.H:37
label n
List< label > labelList
A List of labels.
Definition: List.H:62
Namespace for OpenFOAM.
const pointField & pts
linearDistance(const dictionary &initialPointsDict, const searchableSurface &surface, const scalar &defaultCellSize, const labelList regionIndices)
Construct from components.