File: edgeFaceCirculatorI.H

package info (click to toggle)
openfoam 4.0%2Bdfsg1-7~bpo8%2B1
  • links: PTS, VCS
  • area: main
  • in suites: jessie-backports
  • size: 163,088 kB
  • sloc: cpp: 830,510; sh: 10,210; ansic: 8,215; xml: 745; lex: 437; awk: 194; sed: 91; makefile: 77; python: 18
file content (447 lines) | stat: -rw-r--r-- 11,195 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
/*---------------------------------------------------------------------------*\
  =========                 |
  \\      /  F ield         | OpenFOAM: The Open Source CFD Toolbox
   \\    /   O peration     |
    \\  /    A nd           | Copyright (C) 2011-2016 OpenFOAM Foundation
     \\/     M anipulation  |
-------------------------------------------------------------------------------
License
    This file is part of OpenFOAM.

    OpenFOAM is free software: you can redistribute it and/or modify it
    under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    OpenFOAM is distributed in the hope that it will be useful, but WITHOUT
    ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
    FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
    for more details.

    You should have received a copy of the GNU General Public License
    along with OpenFOAM.  If not, see <http://www.gnu.org/licenses/>.

\*---------------------------------------------------------------------------*/

#include "primitiveMesh.H"

// * * * * * * * * * * * * * Private Member Functions  * * * * * * * * * * * //

void Foam::edgeFaceCirculator::setEnd()
{
    faceLabel_ = -1;
    index_ = -1;
}


void Foam::edgeFaceCirculator::setFace
(
    const label facei,
    const label celli
)
{
    faceLabel_ = facei;

    if (!isBoundaryEdge_ && !mesh_.isInternalFace(facei))
    {
        FatalErrorInFunction
            << "Edge is not defined as boundary edge but still walked to"
            << " boundary face:" << facei << " on cell:" << celli
            << abort(FatalError);
    }
}


void Foam::edgeFaceCirculator::otherFace(const label celli)
{
    const face& f = mesh_.faces()[faceLabel_];
    label v0 = f[index_];
    label v1 = f.nextLabel(index_);

    const cell& cFaces = mesh_.cells()[celli];

    forAll(cFaces, i)
    {
        label faceB = cFaces[i];

        if (faceB != faceLabel_)
        {
            label fp = getMinIndex(mesh_.faces()[faceB], v0, v1);

            if (fp >= 0)
            {
                index_ = fp;
                setFace(faceB, celli);
                return;
            }
        }
    }

    FatalErrorInFunction
        << "Could not find next face stepping"
        << " through cell along edge." << endl
        << "face:" << faceLabel_ << " index in face:" << index_
        << " edge:" << mesh_.points()[v0] << mesh_.points()[v1]
        << abort(FatalError);
}


// * * * * * * * * * * * * * * * * Constructors  * * * * * * * * * * * * * * //

//- Construct from components
Foam::edgeFaceCirculator::edgeFaceCirculator
(
    const primitiveMesh& mesh,
    const label faceLabel,
    const bool ownerSide,
    const label index,
    const bool isBoundaryEdge
)
:
    mesh_(mesh),
    faceLabel_(faceLabel),
    ownerSide_(ownerSide),
    index_(index),
    isBoundaryEdge_(isBoundaryEdge),
    startFaceLabel_(faceLabel_)
{}


//- Construct copy
Foam::edgeFaceCirculator::edgeFaceCirculator(const edgeFaceCirculator& circ)
:
    mesh_(circ.mesh_),
    faceLabel_(circ.faceLabel_),
    ownerSide_(circ.ownerSide_),
    index_(circ.index_),
    isBoundaryEdge_(circ.isBoundaryEdge_),
    startFaceLabel_(circ.startFaceLabel_)
{}


// * * * * * * * * * * * * * * * Member Functions  * * * * * * * * * * * * * //

Foam::label Foam::edgeFaceCirculator::getMinIndex
(
    const face& f,
    const label v0,
    const label v1
)
{
    label fp = findIndex(f, v0);

    if (fp != -1)
    {
        label fpMin1 = f.rcIndex(fp);

        if (f[fpMin1] == v1)
        {
            fp = fpMin1;
        }
        else
        {
            label fpPlus1 = f.fcIndex(fp);

            if (f[fpPlus1] != v1)
            {
                fp = -1;
            }
        }
    }
    return fp;
}


Foam::label Foam::edgeFaceCirculator::faceLabel() const
{
    return faceLabel_;
}


bool Foam::edgeFaceCirculator::ownerSide() const
{
    return ownerSide_;
}


Foam::label Foam::edgeFaceCirculator::index() const
{
    return index_;
}


Foam::label Foam::edgeFaceCirculator::cellLabel() const
{
    if (ownerSide_)
    {
        return mesh_.faceOwner()[faceLabel_];
    }
    else if (mesh_.isInternalFace(faceLabel_))
    {
        return mesh_.faceNeighbour()[faceLabel_];
    }
    else
    {
        return -1;
    }
}


bool Foam::edgeFaceCirculator::sameOrder(const label v0, const label v1) const
{
    const face& f = mesh_.faces()[faceLabel_];

    label fp = getMinIndex(f, v0, v1);

    if (fp != index_)
    {
        FatalErrorInFunction
            << "v0:" << v1 << " and v1:" << v1
            << " not on position:" << index_ << " on face:" << faceLabel_
            << " verts:" << f << " or not consecutive." << abort(FatalError);
    }

    // If we are neighbour the face would point into us so the min index would
    // be v0.
    return ownerSide_ != (f[index_] == v0);
}


void Foam::edgeFaceCirculator::setCanonical()
{
    if (isBoundaryEdge_)
    {
        // Boundary edge. Step until we're on boundary face and ownerSide
        label i = 0;

        while (true)
        {
            if (mesh_.isInternalFace(faceLabel_))
            {
                if (ownerSide_)
                {
                    label celli = mesh_.faceNeighbour()[faceLabel_];
                    otherFace(celli);
                    // Maintain reverse direction of walking
                    ownerSide_ = (mesh_.faceOwner()[faceLabel_] == celli);
                }
                else
                {
                    label celli = mesh_.faceOwner()[faceLabel_];
                    otherFace(celli);
                    // Maintain reverse direction of walking
                    ownerSide_ = (mesh_.faceOwner()[faceLabel_] == celli);
                }
            }
            else if (ownerSide_)
            {
                break;
            }
            else
            {
                label celli = mesh_.faceOwner()[faceLabel_];
                otherFace(celli);
                // Maintain reverse direction of walking
                ownerSide_ = (mesh_.faceOwner()[faceLabel_] == celli);
            }

            i++;

            if (i >= 1000)
            {
                const face& f = mesh_.faces()[faceLabel_];

                FatalErrorInFunction
                    << "Walked " << i << " cells around edge "
                    << mesh_.points()[f[index_]]
                    << mesh_.points()[f.nextLabel(index_)]
                    << " without reaching a boundary face."
                    << " Are you sure this is a boundary edge?"
                    << abort(FatalError);
            }
        }

        // Set up for correct walking
        ownerSide_ = true;
        startFaceLabel_ = faceLabel_;
    }
    else
    {
        // Internal edge. Walk until we hit minimum face label.
        label minFacei = faceLabel_;
        bool minOwnerSide = ownerSide_;
        label minIndex = index_;

        while (true)
        {
            operator++();

            if (operator==(end()))
            {
                break;
            }

            if (!mesh_.isInternalFace(faceLabel_))
            {
                const face& f = mesh_.faces()[faceLabel_];

                FatalErrorInFunction
                    << "Reached boundary face " << faceLabel_
                    << " when walking around internal edge "
                    << mesh_.points()[f[index_]]
                    << mesh_.points()[f.nextLabel(index_)]
                    << "." << endl
                    << "Are you sure this is an internal edge?"
                    << abort(FatalError);
            }

            if (faceLabel_ < minFacei)
            {
                minFacei = faceLabel_;
                minOwnerSide = ownerSide_;
                minIndex = index_;
            }
        }

        faceLabel_ = minFacei;
        ownerSide_ = minOwnerSide;
        index_ = minIndex;
        startFaceLabel_ = faceLabel_;
    }
}


void Foam::edgeFaceCirculator::operator=(const edgeFaceCirculator& circ)
{
    faceLabel_ = circ.faceLabel_;
    ownerSide_ = circ.ownerSide_;
    index_ = circ.index_;
    isBoundaryEdge_ = circ.isBoundaryEdge_;
    startFaceLabel_ = circ.startFaceLabel_;
}


bool Foam::edgeFaceCirculator::operator==(const edgeFaceCirculator& circ) const
{
    return faceLabel_ == circ.faceLabel_ && index_ == circ.index_;

    ////- Note: do I need full comparison? If not we now have that circulators
    ////  around same edge but in different direction are considered not equal
    //if (faceLabel_ == -1 && circ.faceLabel_ == -1)
    //{
    //    // both endConstIter
    //    return true;
    //}
    //
    //return
    //    faceLabel_ == circ.faceLabel_
    // && ownerSide_ == circ.ownerSide_
    // && index_ == circ.index_;
    // && startFaceLabel_ == circ.startFaceLabel_;
}


bool Foam::edgeFaceCirculator::operator!=(const edgeFaceCirculator& circ) const
{
    return !(*this == circ);
}


//- Step to next face.
Foam::edgeFaceCirculator&
Foam::edgeFaceCirculator::operator++()
{
    if (faceLabel_ == -1)
    {
        FatalErrorInFunction
            << "Already reached end(). Cannot walk any further."
            << abort(FatalError);
    }
    else if (ownerSide_)
    {
        // Step to owner
        label celli = mesh_.faceOwner()[faceLabel_];
        otherFace(celli);
        // Maintain direction of walking
        ownerSide_ = (mesh_.faceOwner()[faceLabel_] != celli);

        // Check for internal edge : ends on starting face.
        if (!isBoundaryEdge_ && faceLabel_ == startFaceLabel_)
        {
            setEnd();
        }
    }
    else if (mesh_.isInternalFace(faceLabel_))
    {
        // Step to neighbour
        label celli = mesh_.faceNeighbour()[faceLabel_];
        otherFace(celli);
        // Maintain direction of walking
        ownerSide_ = (mesh_.faceOwner()[faceLabel_] != celli);

        // Check for internal edge : ends on starting face.
        if (!isBoundaryEdge_ && faceLabel_ == startFaceLabel_)
        {
            setEnd();
        }
    }
    else
    {
        // neighbour side of boundary face reached. Mark as endConstIter.
        setEnd();
    }

    return *this;
}


Foam::edgeFaceCirculator Foam::edgeFaceCirculator::begin() const
{
    edgeFaceCirculator iter
    (
        mesh_,
        faceLabel_,
        ownerSide_,
        index_,
        isBoundaryEdge_
    );

    if (isBoundaryEdge_)
    {
        iter.setCanonical();
    }
    return iter;
}


Foam::edgeFaceCirculator Foam::edgeFaceCirculator::cbegin() const
{
    edgeFaceCirculator iter
    (
        mesh_,
        faceLabel_,
        ownerSide_,
        index_,
        isBoundaryEdge_
    );

    if (isBoundaryEdge_)
    {
        iter.setCanonical();
    }
    return iter;
}


const Foam::edgeFaceCirculator& Foam::edgeFaceCirculator::end() const
{
    return endConstIter;
}

const Foam::edgeFaceCirculator& Foam::edgeFaceCirculator::cend() const
{
    return endConstIter;
}


// ************************************************************************* //