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 | Copyright (C) 2012-2015 OpenFOAM Foundation
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 "linearDistance.H"
28 #include "triSurfaceMesh.H"
29 #include "triSurfaceFields.H"
30 #include "volumeType.H"
31 
32 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
33 
34 namespace Foam
35 {
36 
37 // * * * * * * * * * * * * * * Static Data Members * * * * * * * * * * * * * //
38 
39 defineTypeNameAndDebug(linearDistance, 0);
40 addToRunTimeSelectionTable(cellSizeFunction, linearDistance, dictionary);
41 
42 
43 // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
44 
46 (
47  const dictionary& initialPointsDict,
48  const searchableSurface& surface,
49  const scalar& defaultCellSize,
50  const labelList regionIndices
51 )
52 :
53  cellSizeFunction
54  (
55  typeName,
56  initialPointsDict,
57  surface,
58  defaultCellSize,
59  regionIndices
60  ),
61  distanceCellSize_
62  (
63  readScalar(coeffsDict().lookup("distanceCellSizeCoeff"))
64  *defaultCellSize
65  ),
66  distance_
67  (
68  readScalar(coeffsDict().lookup("distanceCoeff"))*defaultCellSize
69  ),
70  distanceSqr_(sqr(distance_))
71 {}
72 
73 
74 // * * * * * * * * * * * * Private Member Functions * * * * * * * * * * * * //
75 
76 scalar 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  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  shapeSizes[0] = distanceCellSize_;
115 
116  shapePts[1] = pt + n*distance_;
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 linearDistance::cellSize(const point& pt, scalar& size) const
141 {
142  size = 0;
143 
144  List<pointIndexHit> hits;
145 
147  (
148  pointField(1, pt),
149  scalarField(1, distanceSqr_),
151  hits
152  );
153 
154  const pointIndexHit& hitInfo = hits[0];
155 
156  if (hitInfo.hit())
157  {
158  const point& hitPt = hitInfo.hitPoint();
159  const label hitIndex = hitInfo.index();
160 
161  const scalar dist = mag(pt - hitPt);
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  (
189  && vTL[0] == volumeType::INSIDE
190  )
191  {
192  size = sizeFunction(hitPt, dist, hitIndex);
193 
194  functionApplied = true;
195  }
196  else if
197  (
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 // forAll(pts, ptI)
219 // {
220 // const Foam::point& pt = pts[ptI];
221 
222 // List<pointIndexHit> hits;
223 
224 // surface_.findNearest
225 // (
226 // pointField(1, pt),
227 // scalarField(1, distanceSqr_),
228 // regionIndices_,
229 // hits
230 // );
231 
232 // const label surfHitI = hits[0].index();
233 
234 // if
235 // (
236 // hits[0].hit()
237 // && !surfaceAlreadyHit.found(surfHitI)
238 // )
239 // {
240 // // Halving cell size is arbitrary
241 // surfaceCellSizeFunction_().refineSurfaceSize(surfHitI);
242 
243 // surfaceAlreadyHit.insert(surfHitI);
244 // }
245 // }
246 
247 // return true;
248  return false;
249 }
250 
251 
252 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
253 
254 } // End namespace Foam
255 
256 // ************************************************************************* //
virtual bool setCellSize(const pointField &pts)
Adapt local cell size. Return true if anything changed.
virtual bool cellSize(const point &pt, scalar &size) const
Modify scalar argument to the cell size specified by function.
intWM_LABEL_SIZE_t label
A label is an int32_t or int64_t as specified by the pre-processor macro WM_LABEL_SIZE.
Definition: label.H:59
autoPtr< surfaceCellSizeFunction > surfaceCellSizeFunction_
Fields for triSurface.
dimensionedSymmTensor sqr(const dimensionedVector &dv)
PointIndexHit< point > pointIndexHit
Definition: pointIndexHit.H:42
Vector< scalar > vector
A scalar version of the templated Vector.
Definition: vector.H:49
label size() const
Return number of elements in list.
Definition: DLListBaseI.H:77
Macros for easy insertion into run-time selection tables.
const labelList regionIndices_
Index of the region of the surface that this cell size function.
vectorField pointField
pointField is a vectorField.
Definition: pointFieldFwd.H:42
stressControl lookup("compactNormalStress") >> compactNormalStress
virtual void findNearest(const pointField &sample, const scalarField &nearestDistSqr, List< pointIndexHit > &) const =0
Field< scalar > scalarField
Specialisation of Field<T> for scalar.
List< label > labelList
A List of labels.
Definition: labelList.H:56
bool readScalar(const char *buf, doubleScalar &s)
Read whole of buf as a scalar. Return true if succesful.
Definition: doubleScalar.H:63
addToRunTimeSelectionTable(ensightPart, ensightPartCells, istream)
defineTypeNameAndDebug(combustionModel, 0)
vector point
Point is a vector.
Definition: point.H:41
virtual bool sizeLocations(const pointIndexHit &hitPt, const vector &n, pointField &shapePts, scalarField &shapeSizes) const
virtual void getVolumeType(const pointField &, List< volumeType > &) const =0
Determine type (inside/outside) for point. unknown if.
static scalar snapToSurfaceTol_
Point closeness tolerance to a surface where the function "snaps" to.
dimensioned< scalar > mag(const dimensioned< Type > &)
const searchableSurface & surface_
Reference to the searchableSurface that cellSizeFunction.
sideMode sideMode_
Mode of size specification, i.e. inside, outside or bothSides.
Namespace for OpenFOAM.
linearDistance(const dictionary &initialPointsDict, const searchableSurface &surface, const scalar &defaultCellSize, const labelList regionIndices)
Construct from components.