distanceSurface.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) 2016-2019 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 "distanceSurface.H"
30 #include "dictionary.H"
31 #include "volFields.H"
32 #include "volPointInterpolation.H"
34 #include "fvMesh.H"
35 
36 // * * * * * * * * * * * * * * Static Data Members * * * * * * * * * * * * * //
37 
38 namespace Foam
39 {
40  defineTypeNameAndDebug(distanceSurface, 0);
41 }
42 
43 
44 // * * * * * * * * * * * * * * * Local Functions * * * * * * * * * * * * * * //
45 
46 namespace Foam
47 {
49  {
50  // Previously 'cell' (bool), but now 'isoAlgorithm' (enum)
51 
52  // Default (bool) for 1906 and earlier
53  bool useCell = true;
54 
55  // Default (enum) after 1906
57 
58  if
59  (
60  !isoSurfaceBase::algorithmNames.readIfPresent
61  (
62  "isoAlgorithm", dict, algo
63  )
64  // When above fails, use 'compat' to also get upgrade messages
66  (
67  "isoAlgorithm", {{"cell", 1906}}, useCell
68  )
69  )
70  {
71  return
72  (
73  useCell
76  );
77  }
78 
79  return algo;
80  }
81 
82 } // End namespace Foam
83 
84 
85 // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
86 
88 (
89  const word& defaultSurfaceName,
90  const polyMesh& mesh,
91  const dictionary& dict
92 )
93 :
94  mesh_(mesh),
95  surfPtr_
96  (
98  (
99  dict.get<word>("surfaceType"),
100  IOobject
101  (
102  dict.getOrDefault("surfaceName", defaultSurfaceName),
103  mesh.time().constant(), // directory
104  "triSurface", // instance
105  mesh.time(), // registry
106  IOobject::MUST_READ,
107  IOobject::NO_WRITE
108  ),
109  dict
110  )
111  ),
112  distance_(dict.get<scalar>("distance")),
113  signed_
114  (
115  distance_ < 0 || equal(distance_, Zero) || dict.get<bool>("signed")
116  ),
117  isoAlgo_(getIsoAlgorithm(dict)),
118  filter_
119  (
120  isoSurfaceBase::getFilterType
121  (
122  dict,
123  isoSurfaceBase::filterType::DIAGCELL
124  )
125  ),
126  bounds_(dict.getOrDefault("bounds", boundBox::invertedBox)),
127  isoSurfPtr_(nullptr),
128  isoSurfCellPtr_(nullptr),
129  isoSurfTopoPtr_(nullptr)
130 {}
131 
132 
134 (
135  const polyMesh& mesh,
136  const bool interpolate,
137  const word& surfaceType,
138  const word& surfaceName,
139  const scalar distance,
140  const bool signedDistance,
142  const isoSurfaceBase::filterType filter,
143  const boundBox& bounds
144 )
145 :
146  mesh_(mesh),
147  surfPtr_
148  (
150  (
151  surfaceType,
152  IOobject
153  (
154  surfaceName, // name
155  mesh.time().constant(), // directory
156  "triSurface", // instance
157  mesh.time(), // registry
158  IOobject::MUST_READ,
159  IOobject::NO_WRITE
160  ),
161  dictionary()
162  )
163  ),
164  distance_(distance),
165  signed_
166  (
167  signedDistance || distance_ < 0 || equal(distance_, Zero)
168  ),
169  isoAlgo_(algo),
170  filter_(filter),
171  bounds_(bounds),
172  isoSurfPtr_(nullptr),
173  isoSurfCellPtr_(nullptr),
174  isoSurfTopoPtr_(nullptr)
175 {}
176 
177 
178 // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
179 
181 {
182  if (debug)
183  {
184  Pout<< "distanceSurface::createGeometry updating geometry." << endl;
185  }
186 
187  // Clear any stored topologies
188  isoSurfPtr_.clear();
189  isoSurfCellPtr_.clear();
190  isoSurfTopoPtr_.clear();
191 
192  const fvMesh& fvm = static_cast<const fvMesh&>(mesh_);
193 
194  // Distance to cell centres
195  // ~~~~~~~~~~~~~~~~~~~~~~~~
196 
197  cellDistancePtr_.reset
198  (
199  new volScalarField
200  (
201  IOobject
202  (
203  "distanceSurface.cellDistance",
204  fvm.time().timeName(),
205  fvm.time(),
208  false
209  ),
210  fvm,
212  )
213  );
214  volScalarField& cellDistance = *cellDistancePtr_;
215 
216  // For distance = 0 (and isoSurfaceCell) we apply additional filtering
217  // to limit the extent of open edges.
218 
219  const bool isZeroDist = equal(distance_, Zero);
220  const bool filterCells =
221  (
222  isZeroDist
223  && isoAlgo_ != isoSurfaceBase::ALGO_POINT
224  );
225 
226  bitSet ignoreCells;
227  if (filterCells)
228  {
229  ignoreCells.resize(fvm.nCells());
230  }
231 
232  // Internal field
233  {
234  const pointField& cc = fvm.C();
235  scalarField& fld = cellDistance.primitiveFieldRef();
236 
237  List<pointIndexHit> nearest;
238  surfPtr_().findNearest
239  (
240  cc,
241  scalarField(cc.size(), GREAT),
242  nearest
243  );
244 
245  if (signed_ || isZeroDist)
246  {
247  vectorField norms;
248  surfPtr_().getNormal(nearest, norms);
249 
250  boundBox cellBb;
251 
252  forAll(norms, i)
253  {
254  const point diff(cc[i] - nearest[i].hitPoint());
255 
256  fld[i] =
257  (
258  isZeroDist // Use normal distance
259  ? (diff & norms[i])
260  : Foam::sign(diff & norms[i]) * Foam::mag(diff)
261  );
262 
263  if (filterCells)
264  {
265  cellBb.clear();
266  cellBb.add(fvm.points(), fvm.cellPoints(i));
267 
268  // Expand slightly to catch corners
269  cellBb.inflate(0.1);
270 
271  if (!cellBb.contains(nearest[i].hitPoint()))
272  {
273  ignoreCells.set(i);
274  }
275  }
276  }
277  }
278  else
279  {
280  forAll(nearest, i)
281  {
282  fld[i] = Foam::mag(cc[i] - nearest[i].hitPoint());
283 
284  // No filtering for unsigned or distance != 0.
285  }
286  }
287  }
288 
289  // Patch fields
290  {
291  volScalarField::Boundary& cellDistanceBf =
292  cellDistance.boundaryFieldRef();
293 
294  forAll(fvm.C().boundaryField(), patchi)
295  {
296  const pointField& cc = fvm.C().boundaryField()[patchi];
297  fvPatchScalarField& fld = cellDistanceBf[patchi];
298 
299  List<pointIndexHit> nearest;
300  surfPtr_().findNearest
301  (
302  cc,
303  scalarField(cc.size(), GREAT),
304  nearest
305  );
306 
307  if (signed_)
308  {
309  vectorField norms;
310  surfPtr_().getNormal(nearest, norms);
311 
312  forAll(norms, i)
313  {
314  const point diff(cc[i] - nearest[i].hitPoint());
315 
316  fld[i] =
317  (
318  isZeroDist // Use normal distance
319  ? (diff & norms[i])
320  : Foam::sign(diff & norms[i]) * Foam::mag(diff)
321  );
322  }
323  }
324  else
325  {
326  forAll(nearest, i)
327  {
328  fld[i] = Foam::mag(cc[i] - nearest[i].hitPoint());
329  }
330  }
331  }
332  }
333 
334 
335  // On processor patches the mesh.C() will already be the cell centre
336  // on the opposite side so no need to swap cellDistance.
337 
338 
339  // Distance to points
340  pointDistance_.setSize(fvm.nPoints());
341  {
342  const pointField& pts = fvm.points();
343 
344  List<pointIndexHit> nearest;
345  surfPtr_().findNearest
346  (
347  pts,
348  scalarField(pts.size(), GREAT),
349  nearest
350  );
351 
352  if (signed_)
353  {
354  vectorField norms;
355  surfPtr_().getNormal(nearest, norms);
356 
357  forAll(norms, i)
358  {
359  const point diff(pts[i] - nearest[i].hitPoint());
360 
361  pointDistance_[i] =
362  (
363  isZeroDist // Use normal distance
364  ? (diff & norms[i])
365  : Foam::sign(diff & norms[i]) * Foam::mag(diff)
366  );
367  }
368  }
369  else
370  {
371  forAll(nearest, i)
372  {
373  pointDistance_[i] = Foam::mag(pts[i] - nearest[i].hitPoint());
374  }
375  }
376  }
377 
378 
379  if (debug)
380  {
381  Pout<< "Writing cell distance:" << cellDistance.objectPath() << endl;
382  cellDistance.write();
383  pointScalarField pDist
384  (
385  IOobject
386  (
387  "distanceSurface.pointDistance",
388  fvm.time().timeName(),
389  fvm.time(),
392  false
393  ),
394  pointMesh::New(fvm),
396  );
397  pDist.primitiveFieldRef() = pointDistance_;
398 
399  Pout<< "Writing point distance:" << pDist.objectPath() << endl;
400  pDist.write();
401  }
402 
403  // Don't need ignoreCells if there is nothing to ignore.
404  if (!ignoreCells.any())
405  {
406  ignoreCells.clear();
407  }
408 
409 
410  // Direct from cell field and point field.
411  if (isoAlgo_ == isoSurfaceBase::ALGO_CELL)
412  {
413  isoSurfCellPtr_.reset
414  (
415  new isoSurfaceCell
416  (
417  fvm,
418  cellDistance,
419  pointDistance_,
420  distance_,
421  filter_,
422  bounds_,
423  1e-6, // mergeTol
424  ignoreCells
425  )
426  );
427  }
428  else if (isoAlgo_ == isoSurfaceBase::ALGO_TOPO)
429  {
430  isoSurfTopoPtr_.reset
431  (
432  new isoSurfaceTopo
433  (
434  fvm,
435  cellDistance,
436  pointDistance_,
437  distance_,
438  filter_,
439  bounds_,
440  ignoreCells
441  )
442  );
443  }
444  else
445  {
446  isoSurfPtr_.reset
447  (
448  new isoSurface
449  (
450  cellDistance,
451  pointDistance_,
452  distance_,
453  filter_,
454  bounds_,
455  1e-6
456  )
457  );
458  }
459 
460  if (debug)
461  {
462  print(Pout);
463  Pout<< endl;
464  }
465 }
466 
467 
469 {
470  os << " surface:" << surfaceName()
471  << " distance:" << distance()
472  << " faces:" << surface().surfFaces().size()
473  << " points:" << surface().points().size();
474 }
475 
476 
477 // ************************************************************************* //
Foam::expressions::patchExpr::debug
int debug
Static debugging option.
Foam::fvPatchField< scalar >
Foam::IOobject::NO_WRITE
Definition: IOobject.H:130
volFields.H
Foam::polyMesh::points
virtual const pointField & points() const
Return raw points.
Definition: polyMesh.C:1038
Foam::scalarField
Field< scalar > scalarField
Specialisation of Field<T> for scalar.
Definition: primitiveFieldsFwd.H:52
Foam::IOobject
Defines the attributes of an object for which implicit objectRegistry management is supported,...
Definition: IOobject.H:104
Foam::word
A class for handling words, derived from Foam::string.
Definition: word.H:62
Foam::bitSet
A bitSet stores bits (elements with only two states) in packed internal format and supports a variety...
Definition: bitSet.H:64
Foam::dimLength
const dimensionSet dimLength(0, 1, 0, 0, 0, 0, 0)
Definition: dimensionSets.H:53
Foam::primitiveMesh::cellPoints
const labelListList & cellPoints() const
Definition: primitiveMeshCellPoints.C:34
Foam::Zero
static constexpr const zero Zero
Global zero.
Definition: zero.H:128
Foam::boundBox::inflate
void inflate(const scalar s)
Inflate box by factor*mag(span) in all dimensions.
Definition: boundBox.C:175
Foam::isoSurfaceBase::ALGO_CELL
Definition: isoSurfaceBase.H:81
Foam::bitSet::any
bool any() const
True if any bits in this bitset are set.
Definition: bitSetI.H:460
Foam::MeshObject< polyMesh, UpdateableMeshObject, pointMesh >::New
static const pointMesh & New(const polyMesh &mesh, Args &&... args)
Get existing or create a new MeshObject.
Definition: MeshObject.C:48
Foam::Time::timeName
static word timeName(const scalar t, const int precision=precision_)
Definition: Time.C:764
Foam::isoSurfaceBase::ALGO_POINT
Definition: isoSurfaceBase.H:80
Foam::distanceSurface::distanceSurface
distanceSurface(const word &defaultSurfaceName, const polyMesh &mesh, const dictionary &dict)
Construct from dictionary.
Definition: distanceSurface.C:88
Foam::bitSet::set
void set(const bitSet &bitset)
Set specified bits from another bitset.
Definition: bitSetI.H:563
Foam::isoSurfaceBase::algorithmNames
static const Enum< algorithmType > algorithmNames
Names for the iso-surface algorithms.
Definition: isoSurfaceBase.H:100
Foam::endl
Ostream & endl(Ostream &os)
Add newline and flush stream.
Definition: Ostream.H:337
Foam::Pout
prefixOSstream Pout
An Ostream wrapper for parallel output to std::cout.
Foam::sign
dimensionedScalar sign(const dimensionedScalar &ds)
Definition: dimensionedScalar.C:166
Foam::MatrixTools::equal
bool equal(const Matrix< Form1, Type > &A, const Matrix< Form2, Type > &B, const bool verbose=false, const label maxDiffs=10, const scalar relTol=1e-5, const scalar absTol=1e-8)
Compare matrix elements for absolute or relative equality.
Definition: MatrixTools.C:34
Foam::PackedList::resize
void resize(const label nElem, const unsigned int val=0u)
Reset addressable list size, does not shrink the allocated size.
Definition: PackedListI.H:388
Foam::polyMesh
Mesh consisting of general polyhedral cells.
Definition: polyMesh.H:77
Foam::isoSurfaceCell
A surface formed by the iso value. After "Polygonising A Scalar Field Using Tetrahedrons",...
Definition: isoSurfaceCell.H:69
forAll
#define forAll(list, i)
Loop across all elements in list.
Definition: stdFoam.H:290
Foam::diff
scalar diff(const triad &A, const triad &B)
Return a quantity of the difference between two triads.
Definition: triad.C:378
Foam::isoSurfaceBase::algorithmType
algorithmType
The algorithm types.
Definition: isoSurfaceBase.H:78
Foam::isoSurface
A surface formed by the iso value. After "Regularised Marching Tetrahedra: improved iso-surface extra...
Definition: isoSurface.H:89
Foam::primitiveMesh::nCells
label nCells() const
Number of mesh cells.
Definition: primitiveMeshI.H:96
Foam::dictionary::readIfPresentCompat
bool readIfPresentCompat(const word &keyword, std::initializer_list< std::pair< const char *, int >> compat, T &val, enum keyType::option=keyType::REGEX) const
Definition: dictionaryTemplates.C:485
Foam::Field< vector >
Foam::dimensionedScalar
dimensioned< scalar > dimensionedScalar
Dimensioned scalar obtained from generic dimensioned type.
Definition: dimensionedScalarFwd.H:43
fld
gmvFile<< "tracers "<< particles.size()<< nl;for(const passiveParticle &p :particles){ gmvFile<< p.position().x()<< ' ';}gmvFile<< nl;for(const passiveParticle &p :particles){ gmvFile<< p.position().y()<< ' ';}gmvFile<< nl;for(const passiveParticle &p :particles){ gmvFile<< p.position().z()<< ' ';}gmvFile<< nl;for(const word &name :lagrangianScalarNames){ IOField< scalar > fld(IOobject(name, runTime.timeName(), cloud::prefix, mesh, IOobject::MUST_READ, IOobject::NO_WRITE))
Definition: gmvOutputLagrangian.H:23
Foam::fvMesh::C
const volVectorField & C() const
Return cell centres as volVectorField.
Definition: fvMeshGeometry.C:358
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:121
mesh
dynamicFvMesh & mesh
Definition: createDynamicFvMesh.H:6
addToRunTimeSelectionTable.H
Macros for easy insertion into run-time selection tables.
Foam::Ostream::write
virtual bool write(const token &tok)=0
Write token to stream or otherwise handle it.
Foam::fvMesh
Mesh data needed to do the Finite Volume discretisation.
Definition: fvMesh.H:84
fvMesh.H
Foam
Namespace for OpenFOAM.
Definition: atmBoundaryLayer.C:33
distanceSurface.H
Foam::distance
scalar distance(const vector &p1, const vector &p2)
Definition: curveTools.C:12
Foam::boundBox::clear
void clear()
Clear bounding box and make it an inverted box.
Definition: boundBoxI.H:184
Foam::GeometricField::primitiveFieldRef
Internal::FieldType & primitiveFieldRef(const bool updateAccessTime=true)
Return a reference to the internal field.
Definition: GeometricField.C:735
Foam::boundBox::contains
bool contains(const point &pt) const
Contains point? (inside or on edge)
Definition: boundBoxI.H:271
Foam::distanceSurface::print
void print(Ostream &os) const
Print information.
Definition: distanceSurface.C:468
Foam::New
tmp< DimensionedField< TypeR, GeoMesh > > New(const tmp< DimensionedField< TypeR, GeoMesh >> &tdf1, const word &name, const dimensionSet &dimensions)
Global function forwards to reuseTmpDimensionedField::New.
Definition: DimensionedFieldReuseFunctions.H:105
volPointInterpolation.H
Foam::GeometricField::boundaryFieldRef
Boundary & boundaryFieldRef(const bool updateAccessTime=true)
Return a reference to the boundary field.
Definition: GeometricField.C:752
Foam::Vector< scalar >
Foam::List
A 1D array of objects of type <T>, where the size of the vector is known and used for subscript bound...
Definition: HashTable.H:102
Foam::mag
dimensioned< typename typeOfMag< Type >::type > mag(const dimensioned< Type > &dt)
Foam::constant::electromagnetic::e
const dimensionedScalar e
Elementary charge.
Definition: createFields.H:11
Foam::primitiveMesh::nPoints
label nPoints() const
Number of mesh points.
Definition: primitiveMeshI.H:37
dictionary.H
Foam::getIsoAlgorithm
static isoSurfaceBase::algorithmType getIsoAlgorithm(const dictionary &dict)
Definition: distanceSurface.C:48
Foam::boundBox
A bounding box defined in terms of min/max extrema points.
Definition: boundBox.H:63
Foam::fvMesh::time
const Time & time() const
Return the top-level database.
Definition: fvMesh.H:246
Foam::distanceSurface::createGeometry
void createGeometry()
Create/recreate the distance surface.
Definition: distanceSurface.C:180
Foam::Ostream
An Ostream is an abstract base class for all output systems (streams, files, token lists,...
Definition: Ostream.H:56
Foam::isoSurfaceTopo
Marching tet iso surface algorithm with optional filtering to keep only points originating from mesh ...
Definition: isoSurfaceTopo.H:58
Foam::GeometricField< scalar, fvPatchField, volMesh >
Foam::isoSurfaceBase::ALGO_TOPO
Definition: isoSurfaceBase.H:82
Foam::IOobject::NO_READ
Definition: IOobject.H:123
Foam::equal
bool equal(const T &s1, const T &s2)
Compare two values for equality.
Definition: doubleFloat.H:46
Foam::defineTypeNameAndDebug
defineTypeNameAndDebug(combustionModel, 0)
Foam::fac::interpolate
static tmp< GeometricField< Type, faePatchField, edgeMesh > > interpolate(const GeometricField< Type, faPatchField, areaMesh > &tvf, const edgeScalarField &faceFlux, Istream &schemeData)
Interpolate field onto faces using scheme given by Istream.
Foam::PackedList::clear
void clear()
Clear the list, i.e. set addressable size to zero.
Definition: PackedListI.H:502
Foam::GeometricField::boundaryField
const Boundary & boundaryField() const
Return const-reference to the boundary field.
Definition: GeometricFieldI.H:62
Foam::isoSurfaceBase::filterType
filterType
The filtering (regularization) to apply.
Definition: isoSurfaceBase.H:87
Foam::boundBox::add
void add(const boundBox &bb)
Extend to include the second box.
Definition: boundBoxI.H:191