triSurfAddressing.C
Go to the documentation of this file.
1 /*---------------------------------------------------------------------------*\
2  ========= |
3  \\ / F ield | cfMesh: A library for mesh generation
4  \\ / O peration |
5  \\ / A nd | Author: Franjo Juretic (franjo.juretic@c-fields.com)
6  \\/ M anipulation | Copyright (C) Creative Fields, Ltd.
7 -------------------------------------------------------------------------------
8 License
9  This file is part of cfMesh.
10 
11  cfMesh is free software; you can redistribute it and/or modify it
12  under the terms of the GNU General Public License as published by the
13  Free Software Foundation; either version 3 of the License, or (at your
14  option) any later version.
15 
16  cfMesh 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 cfMesh. If not, see <http://www.gnu.org/licenses/>.
23 
24 Description
25 
26 \*---------------------------------------------------------------------------*/
27 
28 #include "triSurfAddressing.H"
29 #include "VRWGraphSMPModifier.H"
30 #include "demandDrivenData.H"
31 
32 #include <set>
33 
34 # ifdef USE_OMP
35 #include <omp.h>
36 # endif
37 
38 namespace Foam
39 {
40 
42 {
43  pointFacetsPtr_ = new VRWGraph();
44 
46 }
47 
49 {
50  edgesPtr_ = new edgeLongList();
51 
52  const VRWGraph& pFacets = pointFacets();
53 
54  # ifdef USE_OMP
55  label nThreads = 3 * omp_get_num_procs();
56  if( pFacets.size() < 1000 )
57  nThreads = 1;
58  # else
59  const label nThreads(1);
60  # endif
61 
62  labelList nEdgesForThread(nThreads);
63 
64  label edgeI(0);
65 
66  # ifdef USE_OMP
67  # pragma omp parallel num_threads(nThreads)
68  # endif
69  {
70  edgeLongList edgesHelper;
71 
72  # ifdef USE_OMP
73  # pragma omp for schedule(static)
74  # endif
75  forAll(pFacets, pI)
76  {
77  std::set<std::pair<label, label> > edgesAtPoint;
78 
79  forAllRow(pFacets, pI, pfI)
80  {
81  const label triI = pFacets(pI, pfI);
82  const labelledTri& tri = facets_[triI];
83 
84  forAll(tri, i)
85  {
86  if( tri[i] == pI )
87  {
88  if( tri[(i+1)%3] >= pI )
89  edgesAtPoint.insert
90  (
91  std::make_pair(pI, tri[(i+1)%3])
92  );
93  if( tri[(i+2)%3] >= pI )
94  edgesAtPoint.insert
95  (
96  std::make_pair(pI, tri[(i+2)%3])
97  );
98  }
99  }
100  }
101 
102  std::set<std::pair<label, label> >::const_iterator it;
103  for(it=edgesAtPoint.begin();it!=edgesAtPoint.end();++it)
104  edgesHelper.append(edge(it->first, it->second));
105  }
106 
107  //- this enables other threads to see the number of edges
108  //- generated by each thread
109  # ifdef USE_OMP
110  const label threadI = omp_get_thread_num();
111  # else
112  const label threadI(0);
113  # endif
114  nEdgesForThread[threadI] = edgesHelper.size();
115 
116  # ifdef USE_OMP
117  # pragma omp critical
118  # endif
119  edgeI += edgesHelper.size();
120 
121  # ifdef USE_OMP
122  # pragma omp barrier
123 
124  # pragma omp master
125  # endif
126  edgesPtr_->setSize(edgeI);
127 
128  # ifdef USE_OMP
129  # pragma omp barrier
130  # endif
131 
132  //- find the starting position of the edges generated by this thread
133  //- in the global list of edges
134  label localStart(0);
135  for(label i=0;i<threadI;++i)
136  localStart += nEdgesForThread[i];
137 
138  //- store edges into the global list
139  forAll(edgesHelper, i)
140  edgesPtr_->operator[](localStart+i) = edgesHelper[i];
141  }
142 }
143 
145 {
146  const edgeLongList& edges = this->edges();
147  const VRWGraph& pointFaces = this->pointFacets();
148 
149  facetEdgesPtr_ = new VRWGraph(facets_.size(), 3, -1);
150  VRWGraph& faceEdges = *facetEdgesPtr_;
151 
152  # ifdef USE_OMP
153  # pragma omp parallel for schedule(dynamic, 100)
154  # endif
155  forAll(edges, edgeI)
156  {
157  const edge ee = edges[edgeI];
158  const label pI = ee.start();
159 
160  forAllRow(pointFaces, pI, pfI)
161  {
162  const label fI = pointFaces(pI, pfI);
163 
164  const labelledTri& tri = facets_[fI];
165  forAll(tri, eI)
166  {
167  const edge e(tri[eI], tri[(eI+1)%3]);
168 
169  if( e == ee )
170  {
171  faceEdges(fI, eI) = edgeI;
172  }
173  }
174  }
175  }
176 
177  # ifdef DEBUGTriSurfAddressing
178  forAll(faceEdges, triI)
179  {
180  forAllRow(faceEdges, triI, feI)
181  {
182  if( facets_[triI][feI] < 0 || facets_[triI][feI] >= points_.size() )
184  (
185  "void triSurfAddressing::calculateFacetEdges() const"
186  ) << "Invalid entry in triangle " << triI
187  << " " << facets_[triI] << abort(FatalError);
188 
189  const label edgeI = faceEdges(triI, feI);
190 
191  if( edgeI < 0 || edgeI >= edges.size() )
193  (
194  "void triSurfAddressing::calculateFacetEdges() const"
195  ) << "Invalid entry in face " << triI << " "
196  << facets_[triI] << " edges "
197  << faceEdges[triI] << abort(FatalError);
198  }
199  }
200  # endif
201 }
202 
204 {
205  const edgeLongList& edges = this->edges();
206  const VRWGraph& faceEdges = this->facetEdges();
207 
209 
210  VRWGraphSMPModifier(*edgeFacetsPtr_).reverseAddressing(faceEdges);
211 }
212 
214 {
215  const edgeLongList& edges = this->edges();
216 
217  pointEdgesPtr_ = new VRWGraph(points_.size());
218 
220 }
221 
223 {
225 
226  const VRWGraph& facetEdges = this->facetEdges();
227  const VRWGraph& edgeFacets = this->edgeFacets();
228 
230 
231  forAll(facetEdges, facetI)
232  {
233  labelHashSet fLabels;
234 
235  forAllRow(facetEdges, facetI, feI)
236  {
237  const label edgeI = facetEdges(facetI, feI);
238 
239  forAllRow(edgeFacets, edgeI, efI)
240  fLabels.insert(edgeFacets(edgeI, efI));
241  }
242 
243  facetFacetsEdgesPtr_->setRowSize(facetI, fLabels.size());
244 
245  label counter(0);
246  forAllConstIter(labelHashSet, fLabels, iter)
247  facetFacetsEdgesPtr_->operator()(facetI, counter++) = iter.key();
248  }
249 }
250 
252 {
253  const VRWGraph& pFacets = pointFacets();
254  const vectorField& fNormals = facetNormals();
255 
256  pointNormalsPtr_ = new vectorField(pFacets.size());
257 
258  const label size = pFacets.size();
259  # ifdef USE_OMP
260  # pragma omp parallel for if( size > 1000 )
261  # endif
262  for(label pI=0;pI<size;++pI)
263  {
265 
266  forAllRow(pFacets, pI, pfI)
267  normal += fNormals[pFacets(pI, pfI)];
268 
269  const scalar d = mag(normal);
270  if( d > VSMALL )
271  {
272  normal /= d;
273  }
274  else
275  {
277  }
278 
279  (*pointNormalsPtr_)[pI] = normal;
280  }
281 }
282 
284 {
285  facetNormalsPtr_ = new vectorField(facets_.size());
286 
287  # ifdef USE_OMP
288  # pragma omp parallel for schedule(dynamic, 40)
289  # endif
290  forAll(facets_, fI)
291  {
292  vector v = facets_[fI].normal(points_);
293  v /= (mag(v) + VSMALL);
294  (*facetNormalsPtr_)[fI] = v;
295  }
296 }
297 
299 {
300  facetCentresPtr_ = new vectorField(facets_.size());
301 
302  # ifdef USE_OMP
303  # pragma omp parallel for schedule(dynamic, 40)
304  # endif
305  forAll(facets_, fI)
306  (*facetCentresPtr_)[fI] = facets_[fI].centre(points_);
307 }
308 
309 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
310 
312 (
313  const pointField& points,
314  const LongList<labelledTri>& triangles)
315 :
316  points_(points),
317  facets_(triangles),
318  pointFacetsPtr_(NULL),
319  edgesPtr_(NULL),
320  facetEdgesPtr_(NULL),
321  edgeFacetsPtr_(NULL),
322  pointEdgesPtr_(NULL),
323  facetFacetsEdgesPtr_(NULL),
324  pointNormalsPtr_(NULL),
325  facetNormalsPtr_(NULL),
326  facetCentresPtr_(NULL)
327 
328 {}
329 
330 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
331 
333 {
334  clearAddressing();
335  clearGeometry();
336 }
337 
338 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
339 
341 {
348 }
349 
351 {
355 }
356 
357 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
358 
359 } // End namespace Foam
360 
361 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
Foam::triSurfAddressing::points_
const pointField & points_
const reference to the points
Definition: triSurfAddressing.H:56
Foam::LongList::append
void append(const T &e)
Append an element at the end of the list.
Definition: LongListI.H:265
Foam::Vector< scalar >::zero
static const Vector zero
Definition: Vector.H:80
Foam::triSurfAddressing::facetNormals
const vectorField & facetNormals() const
return normals of facets
Definition: triSurfAddressingI.H:170
Foam::triSurfAddressing::pointNormalsPtr_
vectorField * pointNormalsPtr_
point normals
Definition: triSurfAddressing.H:82
forAll
#define forAll(list, i)
Loop across all elements in list.
Definition: UList.H:406
Foam::LongList::operator
friend Ostream & operator(Ostream &, const LongList< T, Offset > &)
demandDrivenData.H
Template functions to aid in the implementation of demand driven data.
Foam::triSurfAddressing::calculateFacetFacetsEdges
void calculateFacetFacetsEdges() const
calculate facet-faceys addressing
Definition: triSurfAddressing.C:222
Foam::triSurfAddressing::edgesPtr_
edgeLongList * edgesPtr_
edges in the triangulation
Definition: triSurfAddressing.H:66
Foam::triSurfAddressing::edgeFacetsPtr_
VRWGraph * edgeFacetsPtr_
labels of triangles connected to an edge
Definition: triSurfAddressing.H:72
Foam::edge
An edge is a list of two point labels. The functionality it provides supports the discretisation on a...
Definition: edge.H:58
Foam::triSurfAddressing::calculatePointEdges
void calculatePointEdges() const
calculate point-edges addressing
Definition: triSurfAddressing.C:213
Foam::LongList::size
label size() const
Size of the active part of the list.
Definition: LongListI.H:203
Foam::mag
dimensioned< scalar > mag(const dimensioned< Type > &)
Foam::HashSet< label, Hash< label > >
Foam::triSurfAddressing::facetCentresPtr_
vectorField * facetCentresPtr_
face centres
Definition: triSurfAddressing.H:88
Foam::triSurfAddressing::facets_
const LongList< labelledTri > & facets_
const reference to the facets
Definition: triSurfAddressing.H:60
Foam::triSurfAddressing::calculateFacetCentres
void calculateFacetCentres() const
calculate centres of facets
Definition: triSurfAddressing.C:298
forAllConstIter
forAllConstIter(PtrDictionary< phaseModel >, mixture.phases(), phase)
Definition: pEqn.H:39
Foam::vectorField
Field< vector > vectorField
Specialisation of Field<T> for vector.
Definition: primitiveFieldsFwd.H:49
Foam::LongList::setSize
void setSize(const label)
Reset size of List.
Definition: LongListI.H:223
Foam::LongList< edge >
Foam::deleteDemandDrivenData
void deleteDemandDrivenData(DataPtr &dataPtr)
Definition: demandDrivenData.H:40
VRWGraphSMPModifier.H
Foam::triSurfAddressing::clearAddressing
void clearAddressing()
delete all data
Definition: triSurfAddressing.C:340
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::triSurfAddressing::triSurfAddressing
triSurfAddressing(const triSurfAddressing &)
Foam::Field
Pre-declare SubField and related Field type.
Definition: Field.H:57
triSurfAddressing.H
Foam::triSurfAddressing::pointFacetsPtr_
VRWGraph * pointFacetsPtr_
facets connected to a point
Definition: triSurfAddressing.H:63
Foam::VRWGraph::size
label size() const
Returns the number of rows.
Definition: VRWGraphI.H:122
Foam::triSurfAddressing::clearGeometry
void clearGeometry()
delete geometry data
Definition: triSurfAddressing.C:350
Foam::VRWGraph::setSize
void setSize(const label)
Reset the number of rows.
Definition: VRWGraphI.H:132
forAllRow
#define forAllRow(graph, rowI, index)
Definition: VRWGraph.H:277
Foam::triSurfAddressing::pointFacets
const VRWGraph & pointFacets() const
return point-facets addressing
Definition: triSurfAddressingI.H:43
Foam::triSurfAddressing::facetEdgesPtr_
VRWGraph * facetEdgesPtr_
labels of edges in the triangles
Definition: triSurfAddressing.H:69
Foam::FatalError
error FatalError
Foam::HashTable::size
label size() const
Return number of elements in table.
Definition: HashTableI.H:65
Foam::triSurfAddressing::facetFacetsEdgesPtr_
VRWGraph * facetFacetsEdgesPtr_
facets connected tp a facet via edges
Definition: triSurfAddressing.H:78
Foam
Namespace for OpenFOAM.
Definition: combustionModel.C:30
Foam::abort
errorManip< error > abort(error &err)
Definition: errorManip.H:131
Foam::e
const double e
Elementary charge.
Definition: doubleFloat.H:94
Foam::VRWGraphSMPModifier::reverseAddressing
void reverseAddressing(const GraphType &origGraph)
Definition: VRWGraphSMPModifierTemplates.C:111
Foam::triSurfAddressing::calculateEdges
void calculateEdges() const
calculate edges, facet-edges and edge-facets addressing
Definition: triSurfAddressing.C:48
Foam::triSurfAddressing::calculateFacetEdges
void calculateFacetEdges() const
calculate facet-edges addresing
Definition: triSurfAddressing.C:144
Foam::edge::start
label start() const
Return start vertex label.
Definition: edgeI.H:81
Foam::VRWGraph::reverseAddressing
void reverseAddressing(const label nRows, const GraphType &origGraph)
Definition: VRWGraphI.H:406
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:59
Foam::triSurfAddressing::calculateEdgeFacets
void calculateEdgeFacets() const
calculate edge-facets addressing
Definition: triSurfAddressing.C:203
Foam::triSurfAddressing::facetEdges
const VRWGraph & facetEdges() const
return facet-edges addressing
Definition: triSurfAddressingI.H:79
points
const pointField & points
Definition: gmvOutputHeader.H:1
Foam::HashSet::insert
bool insert(const Key &key)
Insert a new entry.
Definition: HashSet.H:116
Foam::triSurfAddressing::edges
const LongList< edge > & edges() const
return edges
Definition: triSurfAddressingI.H:61
Foam::labelledTri
Triangle with additional region number.
Definition: labelledTri.H:49
Foam::triSurfAddressing::facetNormalsPtr_
vectorField * facetNormalsPtr_
face normals
Definition: triSurfAddressing.H:85
Foam::triSurfAddressing::calculateFacetNormals
void calculateFacetNormals() const
calculate normals of facets
Definition: triSurfAddressing.C:283
Foam::triSurfAddressing::calculatePointFacets
void calculatePointFacets() const
calculate point-facets addressing
Definition: triSurfAddressing.C:41
FatalErrorIn
#define FatalErrorIn(functionName)
Report an error message using Foam::FatalError.
Definition: error.H:313
Foam::edgeLongList
LongList< edge > edgeLongList
Definition: edgeLongList.H:46
Foam::VRWGraphSMPModifier
Definition: VRWGraphSMPModifier.H:50
Foam::VRWGraph
Definition: VRWGraph.H:101
Foam::VRWGraph::setRowSize
void setRowSize(const label rowI, const label newSize)
Reset the size of the given row.
Definition: VRWGraphI.H:204
Foam::triSurfAddressing::edgeFacets
const VRWGraph & edgeFacets() const
return edge-facets addressing
Definition: triSurfAddressingI.H:97
Foam::triSurfAddressing::calculatePointNormals
void calculatePointNormals() const
calculate point normals
Definition: triSurfAddressing.C:251
Foam::triSurfAddressing::~triSurfAddressing
~triSurfAddressing()
Definition: triSurfAddressing.C:332
normal
A normal distribution model.
Foam::triSurfAddressing::pointEdgesPtr_
VRWGraph * pointEdgesPtr_
labels of edges connected to a point
Definition: triSurfAddressing.H:75