automatic.C
Go to the documentation of this file.
1 /*---------------------------------------------------------------------------*\
2  ========= |
3  \\ / F ield | OpenFOAM: The Open Source CFD Toolbox
4  \\ / O peration | Website: https://openfoam.org
5  \\ / A nd | Copyright (C) 2012-2019 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 "automatic.H"
28 #include "triSurfaceMesh.H"
29 #include "vtkSurfaceWriter.H"
31 #include "Time.H"
32 
33 // * * * * * * * * * * * * * * Static Data Members * * * * * * * * * * * * * //
34 
35 namespace Foam
36 {
37  defineTypeNameAndDebug(automatic, 0);
38  addToRunTimeSelectionTable(cellSizeCalculationType, automatic, dictionary);
39 }
40 
41 
42 // * * * * * * * * * * * * * Private Member Functions * * * * * * * * * * * //
43 
44 void Foam::automatic::smoothField(triSurfaceScalarField& surf)
45 {
46  label nSmoothingIterations = 10;
47 
48  for (label iter = 0; iter < nSmoothingIterations; ++iter)
49  {
50  const pointField& faceCentres = surface_.faceCentres();
51 
52  forAll(surf, sI)
53  {
54  const labelList& faceFaces = surface_.faceFaces()[sI];
55 
56  const point& fC = faceCentres[sI];
57  const scalar value = surf[sI];
58 
59  scalar newValue = 0;
60  scalar totalDist = 0;
61 
62  label nFaces = 0;
63 
64  forAll(faceFaces, fI)
65  {
66  const label faceLabel = faceFaces[fI];
67  const point& faceCentre = faceCentres[faceLabel];
68 
69  const scalar faceValue = surf[faceLabel];
70  const scalar distance = mag(faceCentre - fC);
71 
72  newValue += faceValue/(distance + small);
73 
74  totalDist += 1.0/(distance + small);
75 
76  if (value < faceValue)
77  {
78  nFaces++;
79  }
80  }
81 
82  // Do not smooth out the peak values
83  if (nFaces == faceFaces.size())
84  {
85  continue;
86  }
87 
88  surf[sI] = newValue/totalDist;
89  }
90  }
91 }
92 
93 
94 // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
95 
97 (
98  const dictionary& cellSizeCalcTypeDict,
99  const triSurfaceMesh& surface,
100  const scalar& defaultCellSize
101 )
102 :
103  cellSizeCalculationType
104  (
105  typeName,
106  cellSizeCalcTypeDict,
107  surface,
108  defaultCellSize
109  ),
110  coeffsDict_(cellSizeCalcTypeDict.optionalSubDict(typeName + "Coeffs")),
111  surfaceName_(surface.searchableSurface::name()),
112  readCurvature_(Switch(coeffsDict_.lookup("curvature"))),
113  curvatureFile_(coeffsDict_.lookup("curvatureFile")),
114  readFeatureProximity_(Switch(coeffsDict_.lookup("featureProximity"))),
115  featureProximityFile_(coeffsDict_.lookup("featureProximityFile")),
116  readInternalCloseness_(Switch(coeffsDict_.lookup("internalCloseness"))),
117  internalClosenessFile_(coeffsDict_.lookup("internalClosenessFile")),
118  internalClosenessCellSizeCoeff_
119  (
120  readScalar(coeffsDict_.lookup("internalClosenessCellSizeCoeff"))
121  ),
122  curvatureCellSizeCoeff_
123  (
124  readScalar(coeffsDict_.lookup("curvatureCellSizeCoeff"))
125  ),
126  maximumCellSize_
127  (
128  readScalar(coeffsDict_.lookup("maximumCellSizeCoeff"))*defaultCellSize
129  )
130 {}
131 
132 
133 // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
134 
136 {
137  Info<< indent
138  << "Calculating cell size on surface: " << surfaceName_ << endl;
139 
140  tmp<triSurfacePointScalarField> tPointCellSize
141  (
143  (
144  IOobject
145  (
146  surfaceName_ + ".cellSize",
147  surface_.searchableSurface::time().constant(),
148  "triSurface",
149  surface_.searchableSurface::time(),
152  ),
153  surface_,
154  dimLength,
155  scalarField(surface_.nPoints(), maximumCellSize_)
156  )
157  );
158 
159  triSurfacePointScalarField& pointCellSize = tPointCellSize.ref();
160 
161  if (readCurvature_)
162  {
163  Info<< indent
164  << "Reading curvature : " << curvatureFile_ << endl;
165 
167  (
168  IOobject
169  (
170  curvatureFile_,
171  surface_.searchableSurface::time().constant(),
172  "triSurface",
173  surface_.searchableSurface::time(),
176  ),
177  surface_,
178  dimLength,
179  true
180  );
181 
182  forAll(pointCellSize, pI)
183  {
184  pointCellSize[pI] =
185  min
186  (
187  1.0
188  /max
189  (
190  (1.0/curvatureCellSizeCoeff_)*mag(curvature[pI]),
191  1.0/maximumCellSize_
192  ),
193  pointCellSize[pI]
194  );
195  }
196  }
197 
198  PrimitivePatchInterpolation
199  <
200  PrimitivePatch<::Foam::List<labelledTri>, pointField>
201  >
202  patchInterpolate(surface_);
203 
204  const Map<label>& meshPointMap = surface_.meshPointMap();
205 
206  if (readInternalCloseness_)
207  {
208  Info<< indent
209  << "Reading internal closeness: " << internalClosenessFile_ << endl;
210 
211  triSurfacePointScalarField internalClosenessPointField
212  (
213  IOobject
214  (
215  internalClosenessFile_,
216  surface_.searchableSurface::time().constant(),
217  "triSurface",
218  surface_.searchableSurface::time(),
221  ),
222  surface_,
223  dimLength,
224  true
225  );
226 
227  forAll(pointCellSize, pI)
228  {
229  pointCellSize[pI] =
230  min
231  (
232  (1.0/internalClosenessCellSizeCoeff_)
233  *internalClosenessPointField[meshPointMap[pI]],
234  pointCellSize[pI]
235  );
236  }
237  }
238 
239  if (readFeatureProximity_)
240  {
241  Info<< indent
242  << "Reading feature proximity : " << featureProximityFile_ << endl;
243 
244  triSurfaceScalarField featureProximity
245  (
246  IOobject
247  (
248  featureProximityFile_,
249  surface_.searchableSurface::time().constant(),
250  "triSurface",
251  surface_.searchableSurface::time(),
254  ),
255  surface_,
256  dimLength,
257  true
258  );
259 
260  scalarField featureProximityPointField
261  (
262  patchInterpolate.faceToPointInterpolate(featureProximity)
263  );
264 
265  forAll(pointCellSize, pI)
266  {
267  pointCellSize[pI] =
268  min
269  (
270  featureProximityPointField[meshPointMap[pI]],
271  pointCellSize[pI]
272  );
273  }
274  }
275 
276  // smoothField(surfaceCellSize);
277 
278  pointCellSize.write();
279 
280  if (debug)
281  {
282  faceList faces(surface_.size());
283 
284  forAll(surface_, fI)
285  {
286  faces[fI] = surface_.triSurface::operator[](fI).triFaceFace();
287  }
288 
289  vtkSurfaceWriter().write
290  (
291  surface_.searchableSurface::time().constant()/"triSurface",
292  surfaceName_.lessExt().name(),
293  surface_.points(),
294  faces,
295  "cellSize",
296  pointCellSize,
297  true,
298  true
299  );
300  }
301 
302  return tPointCellSize;
303 }
304 
305 
306 // ************************************************************************* //
#define forAll(list, i)
Loop across all elements in list.
Definition: UList.H:434
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
const word & name() const
Return name.
Definition: IOobject.H:295
Ostream & indent(Ostream &os)
Indent stream.
Definition: Ostream.H:226
dimensioned< Type > max(const dimensioned< Type > &, const dimensioned< Type > &)
List< face > faceList
Definition: faceListFwd.H:43
Ostream & endl(Ostream &os)
Add newline and flush stream.
Definition: Ostream.H:256
scalar distance(const vector &p1, const vector &p2)
Definition: curveTools.C:12
Macros for easy insertion into run-time selection tables.
vectorField pointField
pointField is a vectorField.
Definition: pointFieldFwd.H:42
stressControl lookup("compactNormalStress") >> compactNormalStress
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 successful.
Definition: doubleScalar.H:68
virtual tmp< triSurfacePointScalarField > load()
Load the cell size field.
addToRunTimeSelectionTable(ensightPart, ensightPartCells, istream)
virtual tmp< pointField > points() const =0
Get the points that define the surface.
defineTypeNameAndDebug(combustionModel, 0)
dimensioned< Type > min(const dimensioned< Type > &, const dimensioned< Type > &)
word name(const complex &)
Return a string representation of a complex.
Definition: complex.C:47
automatic(const dictionary &cellSizeCalcTypeDict, const triSurfaceMesh &surface, const scalar &defaultCellSize)
Construct from components.
vector point
Point is a vector.
Definition: point.H:41
const dimensionSet dimLength(0, 1, 0, 0, 0, 0, 0)
Definition: dimensionSets.H:50
messageStream Info
dimensioned< scalar > mag(const dimensioned< Type > &)
Foam::DimensionedField< scalar, triSurfacePointGeoMesh > triSurfacePointScalarField
virtual label size() const =0
Range of local indices that can be returned.
virtual bool write(const bool write=true) const
Write using setting from DB.
A class for managing temporary objects.
Definition: PtrList.H:53
const searchableSurface & surface_
Reference to the searchableSurface that cellSizeFunction.
Foam::DimensionedField< scalar, triSurfaceGeoMesh > triSurfaceScalarField
Namespace for OpenFOAM.