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-2021 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  coeffsDict_.lookup<scalar>("internalClosenessCellSizeCoeff")
121  ),
122  curvatureCellSizeCoeff_
123  (
124  coeffsDict_.lookup<scalar>("curvatureCellSizeCoeff")
125  ),
126  maximumCellSize_
127  (
128  coeffsDict_.lookup<scalar>("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(),
149  (
150  surface_.searchableSurface::time()
151  ),
152  surface_.searchableSurface::time(),
155  ),
156  surface_,
157  dimLength,
158  scalarField(surface_.nPoints(), maximumCellSize_)
159  )
160  );
161 
162  triSurfacePointScalarField& pointCellSize = tPointCellSize.ref();
163 
164  if (readCurvature_)
165  {
166  Info<< indent
167  << "Reading curvature : " << curvatureFile_ << endl;
168 
170  (
171  IOobject
172  (
173  curvatureFile_,
174  surface_.searchableSurface::time().constant(),
176  (
177  surface_.searchableSurface::time()
178  ),
179  surface_.searchableSurface::time(),
182  ),
183  surface_,
184  dimLength,
185  true
186  );
187 
188  forAll(pointCellSize, pI)
189  {
190  pointCellSize[pI] =
191  min
192  (
193  1.0
194  /max
195  (
196  (1.0/curvatureCellSizeCoeff_)*mag(curvature[pI]),
197  1.0/maximumCellSize_
198  ),
199  pointCellSize[pI]
200  );
201  }
202  }
203 
204  PrimitivePatchInterpolation
205  <
206  PrimitivePatch<::Foam::List<labelledTri>, pointField>
207  >
208  patchInterpolate(surface_);
209 
210  const Map<label>& meshPointMap = surface_.meshPointMap();
211 
212  if (readInternalCloseness_)
213  {
214  Info<< indent
215  << "Reading internal closeness: " << internalClosenessFile_ << endl;
216 
217  triSurfacePointScalarField internalClosenessPointField
218  (
219  IOobject
220  (
221  internalClosenessFile_,
222  surface_.searchableSurface::time().constant(),
224  (
225  surface_.searchableSurface::time()
226  ),
227  surface_.searchableSurface::time(),
230  ),
231  surface_,
232  dimLength,
233  true
234  );
235 
236  forAll(pointCellSize, pI)
237  {
238  pointCellSize[pI] =
239  min
240  (
241  (1.0/internalClosenessCellSizeCoeff_)
242  *internalClosenessPointField[meshPointMap[pI]],
243  pointCellSize[pI]
244  );
245  }
246  }
247 
248  if (readFeatureProximity_)
249  {
250  Info<< indent
251  << "Reading feature proximity : " << featureProximityFile_ << endl;
252 
253  triSurfaceScalarField featureProximity
254  (
255  IOobject
256  (
257  featureProximityFile_,
258  surface_.searchableSurface::time().constant(),
260  (
261  surface_.searchableSurface::time()
262  ),
263  surface_.searchableSurface::time(),
266  ),
267  surface_,
268  dimLength,
269  true
270  );
271 
272  scalarField featureProximityPointField
273  (
274  patchInterpolate.faceToPointInterpolate(featureProximity)
275  );
276 
277  forAll(pointCellSize, pI)
278  {
279  pointCellSize[pI] =
280  min
281  (
282  featureProximityPointField[meshPointMap[pI]],
283  pointCellSize[pI]
284  );
285  }
286  }
287 
288  // smoothField(surfaceCellSize);
289 
290  pointCellSize.write();
291 
292  if (debug)
293  {
294  faceList faces(surface_.size());
295 
296  forAll(surface_, fI)
297  {
298  faces[fI] = surface_.triSurface::operator[](fI).triFaceFace();
299  }
300 
301  vtkSurfaceWriter
302  (
303  surface_.searchableSurface::time().writeFormat()
304  ).write
305  (
306  surface_.searchableSurface::time().constant()/
308  (
309  surface_.searchableSurface::time()
310  ),
311  surfaceName_.lessExt().name(),
312  surface_.points(),
313  faces,
314  "cellSize",
315  pointCellSize,
316  true
317  );
318  }
319 
320  return tPointCellSize;
321 }
322 
323 
324 // ************************************************************************* //
void write(Ostream &, const bool subDict=true) const
Write dictionary, normally with sub-dictionary formatting.
Definition: dictionaryIO.C:207
#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
Ostream & indent(Ostream &os)
Indent stream.
Definition: Ostream.H:221
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:251
scalar distance(const vector &p1, const vector &p2)
Definition: curveTools.C:12
Macros for easy insertion into run-time selection tables.
const dimensionSet dimLength
vectorField pointField
pointField is a vectorField.
Definition: pointFieldFwd.H:42
stressControl lookup("compactNormalStress") >> compactNormalStress
static const word & geometryDir()
Return the geometry directory name.
Field< scalar > scalarField
Specialisation of Field<T> for scalar.
List< label > labelList
A List of labels.
Definition: labelList.H:56
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
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.
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.