faceShading.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) 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 "faceShading.H"
27 #include "fvMesh.H"
29 #include "OFstream.H"
30 #include "cyclicAMIPolyPatch.H"
31 #include "volFields.H"
33 
34 
35 // * * * * * * * * * * * * * * Static Data Members * * * * * * * * * * * * * //
36 
37 namespace Foam
38 {
39  defineTypeNameAndDebug(faceShading, 0);
40 }
41 
42 // * * * * * * * * * * * * * Private Member Functions * * * * * * * * * * * //
43 
44 
46 (
47  const fileName& fName,
48  const DynamicField<point>& endCf,
49  const pointField& myFc
50 )
51 {
52  OFstream str(fName);
53  label vertI = 0;
54 
55  Pout<< "Dumping rays to " << str.name() << endl;
56 
57  forAll(myFc, faceI)
58  {
59  meshTools::writeOBJ(str, myFc[faceI]);
60  vertI++;
61  meshTools::writeOBJ(str, endCf[faceI]);
62  vertI++;
63  str << "l " << vertI-1 << ' ' << vertI << nl;
64  }
65  string cmd("objToVTK " + fName + " " + fName.lessExt() + ".vtk");
66  Pout<< "cmd:" << cmd << endl;
67  system(cmd);
68 }
69 
70 
72 (
73  const labelHashSet& includePatches,
74  const List<labelHashSet>& includeAllFacesPerPatch
75 )
76 {
77  const polyBoundaryMesh& bMesh = mesh_.boundaryMesh();
78 
79  // Storage for surfaceMesh. Size estimate.
80  DynamicList<labelledTri> triangles
81  (
82  mesh_.nFaces() - mesh_.nInternalFaces()
83  );
84 
85  label newPatchI = 0;
86 
87  forAllConstIter(labelHashSet, includePatches, iter)
88  {
89  const label patchI = iter.key();
90  const polyPatch& patch = bMesh[patchI];
91  const pointField& points = patch.points();
92 
93  label nTriTotal = 0;
94 
95  if (includeAllFacesPerPatch[patchI].size() > 0)
96  {
98  (
100  includeAllFacesPerPatch[patchI],
101  iter1
102  )
103  {
104  const label patchFaceI = iter1.key();
105 
106  const face& f = patch[patchFaceI];
107 
108  faceList triFaces(f.nTriangles(points));
109 
110  label nTri = 0;
111 
112  f.triangles(points, nTri, triFaces);
113 
114  forAll(triFaces, triFaceI)
115  {
116  const face& f = triFaces[triFaceI];
117 
118  triangles.append
119  (
120  labelledTri(f[0], f[1], f[2], newPatchI)
121  );
122  nTriTotal++;
123  }
124  }
125  newPatchI++;
126  }
127  }
128 
129  triangles.shrink();
130 
131  // Create globally numbered tri surface
132  triSurface rawSurface(triangles, mesh_.points());
133 
134  // Create locally numbered tri surface
136  (
137  rawSurface.localFaces(),
138  rawSurface.localPoints()
139  );
140 
141  // Add patch names to surface
142  surface.patches().setSize(newPatchI);
143 
144  newPatchI = 0;
145 
146  forAllConstIter(labelHashSet, includePatches, iter)
147  {
148  const label patchI = iter.key();
149  const polyPatch& patch = bMesh[patchI];
150 
151  if (includeAllFacesPerPatch[patchI].size() > 0)
152  {
153  surface.patches()[newPatchI].name() = patch.name();
154  surface.patches()[newPatchI].geometricType() = patch.type();
155 
156  newPatchI++;
157  }
158  }
159 
160  return surface;
161 }
162 
163 
165 {
166  const radiation::boundaryRadiationProperties& boundaryRadiation =
168 
169  label nFaces = 0; //total number of direct hit faces
170 
172 
173  DynamicList<point> dynCf(nFaces);
174  DynamicList<label> dynFacesI;
175 
176  forAll(patches, patchI)
177  {
178  const polyPatch& pp = patches[patchI];
179  const pointField& cf = pp.faceCentres();
180 
181  if (!pp.coupled() && !isA<cyclicAMIPolyPatch>(pp))
182  {
183  const tmp<scalarField> tt =
184  boundaryRadiation.transmissivity(patchI);
185  const scalarField& t = tt();
186  const vectorField& n = pp.faceNormals();
187 
188  forAll(n, faceI)
189  {
190  const vector nf(n[faceI]);
191  if (((direction_ & nf) > 0) && (t[faceI] == 0.0))
192  {
193  dynFacesI.append(faceI + pp.start());
194  dynCf.append(cf[faceI]);
195  nFaces++;
196  }
197  }
198  }
199  }
200 
201  label numberPotentialHits = nFaces;
202 
203  reduce(numberPotentialHits, sumOp<label>());
204 
205  Info<< "Number of 'potential' direct hits : "
206  << numberPotentialHits << endl;
207 
208  labelList hitFacesIds(nFaces);
209  hitFacesIds.transfer(dynFacesI);
210 
211  pointField Cfs(hitFacesIds.size());
212  Cfs.transfer(dynCf);
213 
214  // * * * * * * * * * * * * * * *
215  // Create distributedTriSurfaceMesh
216  Random rndGen(653213);
217 
218  // Determine mesh bounding boxes:
220  (
221  1,
223  (
224  boundBox(mesh_.points(), false)
225  ).extend(rndGen, 1e-3)
226  );
227 
228  // Dummy bounds dictionary
230  dict.add("bounds", meshBb);
231  dict.add
232  (
233  "distributionType",
235  [
237  ]
238  );
239  dict.add("mergeDistance", SMALL);
240 
241  labelHashSet includePatches;
242  List<labelHashSet> includeAllFacesPerPatch(patches.size());
243 
244  forAll(patches, patchI)
245  {
246  const polyPatch& pp = patches[patchI];
247  if (!pp.coupled() && !isA<cyclicAMIPolyPatch>(pp))
248  {
249  includePatches.insert(patchI);
250 
251  const tmp<scalarField> tt =
252  boundaryRadiation.transmissivity(patchI);
253  const scalarField& tau = tt();
254 
255  forAll(pp, faceI)
256  {
257  if (tau[faceI] == 0.0)
258  {
259  includeAllFacesPerPatch[patchI].insert
260  (
261  faceI //pp.start()
262  );
263  }
264  }
265  }
266  }
267 
268  labelList triSurfaceToAgglom(5*nFaces);
269 
271  (
272  includePatches,
273  includeAllFacesPerPatch
274  );
275 
277  (
278  IOobject
279  (
280  "opaqueSurface.stl",
281  mesh_.time().constant(), // directory
282  "triSurface", // instance
283  mesh_.time(), // registry
286  ),
287  localSurface,
288  dict
289  );
290 
291  surfacesMesh.searchableSurface::write();
292 
293  triSurfaceToAgglom.resize(surfacesMesh.size());
294 
295  scalar maxBounding = 5.0*mag(mesh_.bounds().max() - mesh_.bounds().min());
296 
297  reduce(maxBounding, maxOp<scalar>());
298 
299  // Calculate index of faces which have a direct hit (local)
300  DynamicList<label> rayStartFace(nFaces + 0.01*nFaces);
301 
302  // Shoot Rays
303  // * * * * * * * * * * * * * * * *
304  {
305 
306  DynamicField<point> start(nFaces);
307  DynamicField<point> end(start.size());
308  DynamicList<label> startIndex(start.size());
309 
310  label i = 0;
311  do
312  {
313  for (; i < Cfs.size(); i++)
314  {
315  const point& fc = Cfs[i];
316 
317  const label myFaceId = hitFacesIds[i];
318 
319  const vector d(direction_*maxBounding);
320 
321  start.append(fc - SMALL*d);
322 
323  startIndex.append(myFaceId);
324 
325  end.append(fc - d);
326 
327  }
328 
329  }while (returnReduce(i < Cfs.size(), orOp<bool>()));
330 
331  List<pointIndexHit> hitInfo(startIndex.size());
332  surfacesMesh.findLine(start, end, hitInfo);
333 
334  // Collect the rays which has 'only one not wall' obstacle bettween
335  // start and end.
336  // If the ray hit itself get stored in dRayIs
337  forAll (hitInfo, rayI)
338  {
339  if (!hitInfo[rayI].hit())
340  {
341  rayStartFace.append(startIndex[rayI]);
342  }
343  }
344 
345  // Plot all rays between visible faces.
346  if (debug)
347  {
348  writeRays
349  (
350  mesh_.time().path()/"allVisibleFaces.obj",
351  end,
352  Cfs
353  );
354  }
355 
356  start.clear();
357  startIndex.clear();
358  end.clear();
359  }
360 
361  rayStartFaces_.transfer(rayStartFace);
362 
363  if (debug)
364  {
365  tmp<volScalarField> thitFaces
366  (
367  new volScalarField
368  (
369  IOobject
370  (
371  "hitFaces",
372  mesh_.time().timeName(),
373  mesh_,
376  ),
377  mesh_,
378  dimensionedScalar("zero", dimless, 0)
379  )
380  );
381 
382  volScalarField& hitFaces = thitFaces();
383 
384  hitFaces.boundaryField() = 0.0;
386  {
387  const label faceI = rayStartFaces_[i];
388  label patchID = patches.whichPatch(faceI);
389  const polyPatch& pp = patches[patchID];
390  hitFaces.boundaryField()[patchID][faceI - pp.start()] = 1.0;
391  }
392  hitFaces.write();
393  }
394 
395  label totalHitFaces = rayStartFaces_.size();
396 
397  reduce(totalHitFaces, sumOp<label>());
398 
399  Info<< "Total number of hit faces : " << totalHitFaces << endl;
400 }
401 
402 
403 // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
404 
406 (
407  const fvMesh& mesh,
408  const vector dir,
409  const labelList& hitFaceList
410 )
411 :
412  mesh_(mesh),
413  direction_(dir),
414  rayStartFaces_(hitFaceList)
415 {}
416 
417 
418 
420 (
421  const fvMesh& mesh,
422  const vector dir
423 )
424 :
425  mesh_(mesh),
426  direction_(dir),
427  rayStartFaces_(0)
428 {
429  calculate();
430 }
431 
432 
433 // * * * * * * * * * * * * * * * * Destructor * * * * * * * * * * * * * * * //
434 
436 {}
437 
438 
439 // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
440 
442 {
443  calculate();
444 }
445 
446 
447 // ************************************************************************* //
Foam::DynamicField::clear
void clear()
Clear the addressed list, i.e. set the size to zero.
Definition: DynamicFieldI.H:277
volFields.H
localSurface
const triSurface localSurface
Definition: searchingEngine.H:28
Foam::Random
Simple random number generator.
Definition: Random.H:49
Foam::polyMesh::points
virtual const pointField & points() const
Return raw points.
Definition: polyMesh.C:979
Foam::maxOp
Definition: ops.H:172
Foam::IOobject
IOobject defines the attributes of an object for which implicit objectRegistry management is supporte...
Definition: IOobject.H:91
Foam::faceShading::~faceShading
~faceShading()
Destructor.
Definition: faceShading.C:435
distributedTriSurfaceMesh.H
Foam::dimless
const dimensionSet dimless(0, 0, 0, 0, 0, 0, 0)
Definition: dimensionSets.H:47
Foam::PrimitivePatch::points
const Field< PointType > & points() const
Return reference to global points.
Definition: PrimitivePatchTemplate.H:282
Foam::faceShading::faceShading
faceShading(const faceShading &)
Disallow default bitwise copy construct.
Foam::fileName
A class for handling file names.
Definition: fileName.H:69
Foam::boundBox::max
const point & max() const
Maximum describing the bounding box.
Definition: boundBoxI.H:60
Foam::returnReduce
T returnReduce(const T &Value, const BinaryOp &bop, const int tag=Pstream::msgType(), const label comm=UPstream::worldComm)
Definition: PstreamReduceOps.H:86
Foam::polyBoundaryMesh
Foam::polyBoundaryMesh.
Definition: polyBoundaryMesh.H:60
forAll
#define forAll(list, i)
Loop across all elements in list.
Definition: UList.H:406
writeOBJ
void writeOBJ(Ostream &os, label &vertI, const tetPoints &tet)
Definition: Test-tetTetOverlap.C:38
Foam::tmp
A class for managing temporary objects.
Definition: PtrList.H:118
Foam::DynamicList
A 1D vector of objects of type <T> that resizes itself as necessary to accept the new objects.
Definition: DynamicList.H:56
Foam::PrimitivePatch::localPoints
const Field< PointType > & localPoints() const
Return pointField of points in patch.
Definition: PrimitivePatchTemplate.C:432
surfacesMesh
distributedTriSurfaceMesh surfacesMesh(IOobject("wallSurface.stl", runTime.constant(), "triSurface", runTime, IOobject::NO_READ, IOobject::NO_WRITE), localSurface, dict)
Foam::treeBoundBox
Standard boundBox + extra functionality for use in octree.
Definition: treeBoundBox.H:75
Foam::polyPatch::coupled
virtual bool coupled() const
Return true if this patch is geometrically coupled (i.e. faces and.
Definition: polyPatch.H:322
Foam::distributedTriSurfaceMesh::distributionTypeNames_
static const NamedEnum< distributionType, 3 > distributionTypeNames_
Definition: distributedTriSurfaceMesh.H:88
Foam::faceShading::rayStartFaces_
labelList rayStartFaces_
Faces directly hit by vector direction.
Definition: faceShading.H:69
Foam::List::transfer
void transfer(List< T > &)
Transfer the contents of the argument List into this list.
Foam::GeometricField::boundaryField
GeometricBoundaryField & boundaryField()
Return reference to GeometricBoundaryField.
Definition: GeometricField.C:735
Foam::polyMesh::boundaryMesh
const polyBoundaryMesh & boundaryMesh() const
Return boundary mesh.
Definition: polyMesh.H:421
Foam::endl
Ostream & endl(Ostream &os)
Add newline and flush stream.
Definition: Ostream.H:251
Foam::fileName::lessExt
fileName lessExt() const
Return file name without extension (part before last .)
Definition: fileName.C:313
Foam::mag
dimensioned< scalar > mag(const dimensioned< Type > &)
Foam::HashSet< label, Hash< label > >
meshBb
List< treeBoundBox > meshBb(1, treeBoundBox(boundBox(coarseMesh.points(), false)).extend(rndGen, 1e-3))
Foam::IOobject::NO_WRITE
@ NO_WRITE
Definition: IOobject.H:118
Foam::MeshObject< fvMesh, Foam::GeometricMeshObject, boundaryRadiationProperties >::New
static const boundaryRadiationProperties & New(const fvMesh &mesh)
Definition: MeshObject.C:44
Foam::distributedTriSurfaceMesh::FROZEN
@ FROZEN
Definition: distributedTriSurfaceMesh.H:85
forAllConstIter
forAllConstIter(PtrDictionary< phaseModel >, mixture.phases(), phase)
Definition: pEqn.H:39
OFstream.H
Foam::DynamicField
Dynamically sized Field.
Definition: DynamicField.H:49
Foam::radiation::boundaryRadiationProperties::transmissivity
tmp< scalarField > transmissivity(const label patchId, const label bandI=0) const
Access boundary transmissivity on patch.
Definition: boundaryRadiationProperties.C:156
n
label n
Definition: TABSMDCalcMethod2.H:31
Foam::faceShading::direction_
vector direction_
Direction.
Definition: faceShading.H:66
Foam::reduce
void reduce(const List< UPstream::commsStruct > &comms, T &Value, const BinaryOp &bop, const int tag, const label comm)
Definition: PstreamReduceOps.H:43
Foam::label
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
Foam::Field
Pre-declare SubField and related Field type.
Definition: Field.H:57
Foam::triSurface
Triangulated surface description with patch information.
Definition: triSurface.H:57
Foam::faceShading::mesh_
const fvMesh & mesh_
Reference to mesh.
Definition: faceShading.H:63
Foam::treeBoundBox::extend
treeBoundBox extend(Random &, const scalar s) const
Return slightly wider bounding box.
Definition: treeBoundBoxI.H:317
Foam::orOp
Definition: ops.H:178
Foam::IOobject::NO_READ
@ NO_READ
Definition: IOobject.H:111
Foam::nl
static const char nl
Definition: Ostream.H:260
Foam::Info
messageStream Info
Foam::polyPatch
A patch is a list of labels that address the faces in the global face list.
Definition: polyPatch.H:66
Foam::DynamicList::clear
void clear()
Clear the addressed list, i.e. set the size to zero.
Definition: DynamicListI.H:242
Foam::DynamicField::append
DynamicField< T, SizeInc, SizeMult, SizeDiv > & append(const T &)
Append an element at the end of the list.
faceShading.H
Foam::DynamicList::shrink
DynamicList< T, SizeInc, SizeMult, SizeDiv > & shrink()
Shrink the allocated space to the number of elements used.
Definition: DynamicListI.H:258
Foam::boundBox::min
const point & min() const
Minimum describing the bounding box.
Definition: boundBoxI.H:54
Foam::faceShading::triangulate
triSurface triangulate(const labelHashSet &includePatches, const List< labelHashSet > &includeAllFaces)
Construct a triSurface from patches and faces on global local index.
Definition: faceShading.C:72
Foam::radiation::boundaryRadiationProperties
Definition: boundaryRadiationProperties.H:56
dict
dictionary dict
Definition: searchingEngine.H:14
Foam::dictionary
A list of keyword definitions, which are a keyword followed by any number of values (e....
Definition: dictionary.H:137
triSurfaceToAgglom
labelList triSurfaceToAgglom(5 *nFineFaces)
mesh
dynamicFvMesh & mesh
Definition: createDynamicFvMesh.H:18
Foam::dimensioned
Generic dimensioned Type class.
Definition: dimensionedScalarFwd.H:41
Foam::geometryBase::name
const word & name() const
Return the name.
Definition: geometryBase.C:124
Foam::fvMesh
Mesh data needed to do the Finite Volume discretisation.
Definition: fvMesh.H:78
fvMesh.H
Foam
Namespace for OpenFOAM.
Definition: combustionModel.C:30
Foam::e
const double e
Elementary charge.
Definition: doubleFloat.H:94
Foam::polyPatch::start
label start() const
Return start label of this patch in the polyMesh face list.
Definition: polyPatch.H:312
cyclicAMIPolyPatch.H
Foam::DynamicList::append
DynamicList< T, SizeInc, SizeMult, SizeDiv > & append(const T &)
Append an element at the end of the list.
Foam::OFstream
Output to file stream.
Definition: OFstream.H:81
Foam::polyMesh::bounds
const boundBox & bounds() const
Return mesh bounding box.
Definition: polyMesh.H:427
Foam::faceShading::calculate
void calculate()
Calculate ray start faces.
Definition: faceShading.C:164
Foam::faceShading::writeRays
void writeRays(const fileName &fName, const DynamicField< point > &endCf, const pointField &myFc)
Write rays.
Definition: faceShading.C:46
Foam::sumOp
Definition: ops.H:162
Foam::Time::path
fileName path() const
Return path.
Definition: Time.H:281
Foam::polyPatch::faceCentres
const vectorField::subField faceCentres() const
Return face centres.
Definition: polyPatch.C:308
Foam::Pout
prefixOSstream Pout(cout, "Pout")
Definition: IOstreams.H:53
Foam::PrimitivePatch::faceNormals
const Field< PointType > & faceNormals() const
Return face normals for patch.
Definition: PrimitivePatchTemplate.C:520
f
labelList f(nPoints)
Foam::Vector< scalar >
Foam::Time::timeName
static word timeName(const scalar, const int precision=precision_)
Return time name of given scalar time.
Definition: Time.C:741
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:59
Foam::distributedTriSurfaceMesh
IOoject and searching on distributed triSurface. All processor hold (possibly overlapping) part of th...
Definition: distributedTriSurfaceMesh.H:73
Foam::TimePaths::constant
const word & constant() const
Return constant name.
Definition: TimePaths.H:130
points
const pointField & points
Definition: gmvOutputHeader.H:1
Foam::surface
Definition: surface.H:55
Foam::faceShading::correct
void correct()
Recalculate rayStartFaces using direction vector.
Definition: faceShading.C:441
Foam::HashSet::insert
bool insert(const Key &key)
Insert a new entry.
Definition: HashSet.H:116
Foam::labelledTri
Triangle with additional region number.
Definition: labelledTri.H:49
Foam::boundBox
A bounding box defined in terms of the points at its extremities.
Definition: boundBox.H:55
Foam::fvMesh::time
const Time & time() const
Return the top-level database.
Definition: fvMesh.H:243
Foam::PrimitivePatch::localFaces
const List< Face > & localFaces() const
Return patch faces addressing into local point list.
Definition: PrimitivePatchTemplate.C:372
patches
patches[0]
Definition: createSingleCellMesh.H:36
Foam::face
A face is a list of labels corresponding to mesh vertices.
Definition: face.H:75
Foam::OFstream::name
const fileName & name() const
Return the name of the stream.
Definition: OFstream.H:118
Foam::List::size
void size(const label)
Override size to be inconsistent with allocated storage.
Foam::GeometricField
Generic GeometricField class.
Definition: surfaceFieldsFwd.H:52
Foam::defineTypeNameAndDebug
defineTypeNameAndDebug(combustionModel, 0)
Foam::patchIdentifier::name
const word & name() const
Return name.
Definition: patchIdentifier.H:109
Foam::system
int system(const std::string &command)
Execute the specified command.
Definition: POSIX.C:1155
rndGen
cachedRandom rndGen(label(0), -1)
boundaryRadiationProperties.H
Foam::PrimitivePatch
A list of faces which address into the list of points.
Definition: PrimitivePatchTemplate.H:88