polyTopoChange.C 94.4 KB
Newer Older
1
2
3
4
/*---------------------------------------------------------------------------*\
  =========                 |
  \\      /  F ield         | OpenFOAM: The Open Source CFD Toolbox
   \\    /   O peration     |
5
    \\  /    A nd           | Copyright (C) 2011-2014 OpenFOAM Foundation
6
7
8
9
10
     \\/     M anipulation  |
-------------------------------------------------------------------------------
License
    This file is part of OpenFOAM.

11
12
13
14
    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.
15
16
17
18
19
20
21

    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
22
    along with OpenFOAM.  If not, see <http://www.gnu.org/licenses/>.
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40

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

#include "polyTopoChange.H"
#include "SortableList.H"
#include "polyMesh.H"
#include "polyAddPoint.H"
#include "polyModifyPoint.H"
#include "polyRemovePoint.H"
#include "polyAddFace.H"
#include "polyModifyFace.H"
#include "polyRemoveFace.H"
#include "polyAddCell.H"
#include "polyModifyCell.H"
#include "polyRemoveCell.H"
#include "objectMap.H"
#include "processorPolyPatch.H"
#include "fvMesh.H"
41
#include "CompactListList.H"
laurence's avatar
laurence committed
42
#include "ListOps.H"
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

// * * * * * * * * * * * * * * Static Data Members * * * * * * * * * * * * * //

namespace Foam
{
    defineTypeNameAndDebug(polyTopoChange, 0);
}


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

// Renumber with special handling for merged items (marked with <-1)
void Foam::polyTopoChange::renumberReverseMap
(
    const labelList& map,
    DynamicList<label>& elems
)
{
    forAll(elems, elemI)
    {
        label val = elems[elemI];

        if (val >= 0)
        {
            elems[elemI] = map[val];
        }
        else if (val < -1)
        {
            label mergedVal = -val-2;
            elems[elemI] = -map[mergedVal]-2;
        }
    }
}


void Foam::polyTopoChange::renumber
(
    const labelList& map,
    labelHashSet& elems
)
{
    labelHashSet newElems(elems.size());

    forAllConstIter(labelHashSet, elems, iter)
    {
        label newElem = map[iter.key()];

        if (newElem >= 0)
        {
            newElems.insert(newElem);
        }
    }

    elems.transfer(newElems);
}


// Renumber and remove -1 elements.
void Foam::polyTopoChange::renumberCompact
(
    const labelList& map,
    labelList& elems
)
{
    label newElemI = 0;

    forAll(elems, elemI)
    {
        label newVal = map[elems[elemI]];

        if (newVal != -1)
        {
            elems[newElemI++] = newVal;
        }
    }
    elems.setSize(newElemI);
}


void Foam::polyTopoChange::countMap
(
    const labelList& map,
    const labelList& reverseMap,
    label& nAdd,
    label& nInflate,
    label& nMerge,
    label& nRemove
)
{
    nAdd = 0;
    nInflate = 0;
    nMerge = 0;
    nRemove = 0;

    forAll(map, newCellI)
    {
        label oldCellI = map[newCellI];

        if (oldCellI >= 0)
        {
            if (reverseMap[oldCellI] == newCellI)
            {
                // unchanged
            }
            else
            {
                // Added (from another cell v.s. inflated from face/point)
                nAdd++;
            }
        }
        else if (oldCellI == -1)
        {
            // Created from nothing
            nInflate++;
        }
        else
        {
            FatalErrorIn("countMap") << "old:" << oldCellI
                << " new:" << newCellI << abort(FatalError);
        }
    }

    forAll(reverseMap, oldCellI)
    {
        label newCellI = reverseMap[oldCellI];

        if (newCellI >= 0)
        {
            // unchanged
        }
        else if (newCellI == -1)
        {
            // removed
            nRemove++;
        }
        else
        {
            // merged into -newCellI-2
            nMerge++;
        }
    }
}


mattijs's avatar
mattijs committed
187
188
189
190
Foam::labelHashSet Foam::polyTopoChange::getSetIndices
(
    const PackedBoolList& lst
)
mattijs's avatar
mattijs committed
191
192
193
194
195
196
197
198
199
200
201
202
203
{
    labelHashSet values(lst.count());
    forAll(lst, i)
    {
        if (lst[i])
        {
            values.insert(i);
        }
    }
    return values;
}


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
void Foam::polyTopoChange::writeMeshStats(const polyMesh& mesh, Ostream& os)
{
    const polyBoundaryMesh& patches = mesh.boundaryMesh();

    labelList patchSizes(patches.size());
    labelList patchStarts(patches.size());
    forAll(patches, patchI)
    {
        patchSizes[patchI] = patches[patchI].size();
        patchStarts[patchI] = patches[patchI].start();
    }

    os  << "    Points      : " << mesh.nPoints() << nl
        << "    Faces       : " << mesh.nFaces() << nl
        << "    Cells       : " << mesh.nCells() << nl
        << "    PatchSizes  : " << patchSizes << nl
        << "    PatchStarts : " << patchStarts << nl
        << endl;
}


void Foam::polyTopoChange::getMergeSets
(
    const labelList& reverseCellMap,
    const labelList& cellMap,
    List<objectMap>& cellsFromCells
)
{
    // Per new cell the number of old cells that have been merged into it
    labelList nMerged(cellMap.size(), 1);

    forAll(reverseCellMap, oldCellI)
    {
        label newCellI = reverseCellMap[oldCellI];

        if (newCellI < -1)
        {
            label mergeCellI = -newCellI-2;

            nMerged[mergeCellI]++;
        }
    }

    // From merged cell to set index
    labelList cellToMergeSet(cellMap.size(), -1);

    label nSets = 0;

    forAll(nMerged, cellI)
    {
        if (nMerged[cellI] > 1)
        {
            cellToMergeSet[cellI] = nSets++;
        }
    }

    // Collect cell labels.
    // Each objectMap will have
    // - index : new mesh cell label
    // - masterObjects : list of old cells that have been merged. Element 0
    //                   will be the original destination cell label.

    cellsFromCells.setSize(nSets);

    forAll(reverseCellMap, oldCellI)
    {
        label newCellI = reverseCellMap[oldCellI];

        if (newCellI < -1)
        {
            label mergeCellI = -newCellI-2;

            // oldCellI was merged into mergeCellI

            label setI = cellToMergeSet[mergeCellI];

            objectMap& mergeSet = cellsFromCells[setI];

282
            if (mergeSet.masterObjects().empty())
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
            {
                // First occurrence of master cell mergeCellI

                mergeSet.index() = mergeCellI;
                mergeSet.masterObjects().setSize(nMerged[mergeCellI]);

                // old master label
                mergeSet.masterObjects()[0] = cellMap[mergeCellI];

                // old slave label
                mergeSet.masterObjects()[1] = oldCellI;

                nMerged[mergeCellI] = 2;
            }
            else
            {
                mergeSet.masterObjects()[nMerged[mergeCellI]++] = oldCellI;
            }
        }
    }
}


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
bool Foam::polyTopoChange::hasValidPoints(const face& f) const
{
    forAll(f, fp)
    {
        if (f[fp] < 0 || f[fp] >= points_.size())
        {
            return false;
        }
    }
    return true;
}


Foam::pointField Foam::polyTopoChange::facePoints(const face& f) const
{
    pointField points(f.size());
    forAll(f, fp)
    {
        if (f[fp] < 0 && f[fp] >= points_.size())
        {
            FatalErrorIn("polyTopoChange::facePoints(const face&) const")
                << "Problem." << abort(FatalError);
        }
        points[fp] = points_[f[fp]];
    }
    return points;
}


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
void Foam::polyTopoChange::checkFace
(
    const face& f,
    const label faceI,
    const label own,
    const label nei,
    const label patchI,
    const label zoneI
) const
{
    if (nei == -1)
    {
        if (own == -1 && zoneI != -1)
        {
            // retired face
        }
        else if (patchI == -1 || patchI >= nPatches_)
        {
            FatalErrorIn
            (
                "polyTopoChange::checkFace(const face&, const label"
                ", const label, const label, const label)"
            )   << "Face has no neighbour (so external) but does not have"
                << " a valid patch" << nl
                << "f:" << f
                << " faceI(-1 if added face):" << faceI
                << " own:" << own << " nei:" << nei
362
363
364
365
366
367
368
369
                << " patchI:" << patchI << nl;
            if (hasValidPoints(f))
            {
                FatalError
                        << "points (removed points marked with "
                        << vector::max << ") " << facePoints(f);
            }
            FatalError << abort(FatalError);
370
371
372
373
374
375
376
377
378
379
380
381
382
383
        }
    }
    else
    {
        if (patchI != -1)
        {
            FatalErrorIn
            (
                "polyTopoChange::checkFace(const face&, const label"
                ", const label, const label, const label)"
            )   << "Cannot both have valid patchI and neighbour" << nl
                << "f:" << f
                << " faceI(-1 if added face):" << faceI
                << " own:" << own << " nei:" << nei
384
385
386
387
388
389
390
391
                << " patchI:" << patchI << nl;
            if (hasValidPoints(f))
            {
                FatalError
                        << "points (removed points marked with "
                        << vector::max << ") : " << facePoints(f);
            }
            FatalError << abort(FatalError);
392
393
394
395
396
397
398
399
400
401
402
403
404
        }

        if (nei <= own)
        {
            FatalErrorIn
            (
                "polyTopoChange::checkFace(const face&, const label"
                ", const label, const label, const label)"
            )   << "Owner cell label should be less than neighbour cell label"
                << nl
                << "f:" << f
                << " faceI(-1 if added face):" << faceI
                << " own:" << own << " nei:" << nei
405
406
407
408
409
410
411
412
                << " patchI:" << patchI << nl;
            if (hasValidPoints(f))
            {
                FatalError
                        << "points (removed points marked with "
                        << vector::max << ") : " << facePoints(f);
            }
            FatalError << abort(FatalError);
413
414
415
416
417
418
419
420
421
422
423
424
425
426
        }
    }

    if (f.size() < 3 || findIndex(f, -1) != -1)
    {
        FatalErrorIn
        (
            "polyTopoChange::checkFace(const face&, const label"
            ", const label, const label, const label)"
        )   << "Illegal vertices in face"
            << nl
            << "f:" << f
            << " faceI(-1 if added face):" << faceI
            << " own:" << own << " nei:" << nei
427
428
429
430
431
432
433
434
            << " patchI:" << patchI << nl;
            if (hasValidPoints(f))
            {
                FatalError
                        << "points (removed points marked with "
                        << vector::max << ") : " << facePoints(f);
            }
            FatalError << abort(FatalError);
435
    }
mattijs's avatar
mattijs committed
436
437
438
439
440
441
442
443
444
445
446
    if (faceI >= 0 && faceI < faces_.size() && faceRemoved(faceI))
    {
        FatalErrorIn
        (
            "polyTopoChange::checkFace(const face&, const label"
            ", const label, const label, const label)"
        )   << "Face already marked for removal"
            << nl
            << "f:" << f
            << " faceI(-1 if added face):" << faceI
            << " own:" << own << " nei:" << nei
447
448
449
450
451
452
453
454
            << " patchI:" << patchI << nl;
            if (hasValidPoints(f))
            {
                FatalError
                        << "points (removed points marked with "
                        << vector::max << ") : " << facePoints(f);
            }
            FatalError << abort(FatalError);
mattijs's avatar
mattijs committed
455
456
457
458
459
460
461
462
463
464
465
466
467
468
    }
    forAll(f, fp)
    {
        if (f[fp] < points_.size() && pointRemoved(f[fp]))
        {
            FatalErrorIn
            (
                "polyTopoChange::checkFace(const face&, const label"
                ", const label, const label, const label)"
            )   << "Face uses removed vertices"
                << nl
                << "f:" << f
                << " faceI(-1 if added face):" << faceI
                << " own:" << own << " nei:" << nei
469
470
471
472
473
474
475
476
                << " patchI:" << patchI << nl;
            if (hasValidPoints(f))
            {
                FatalError
                        << "points (removed points marked with "
                        << vector::max << ") : " << facePoints(f);
            }
            FatalError << abort(FatalError);
mattijs's avatar
mattijs committed
477
478
        }
    }
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
}


void Foam::polyTopoChange::makeCells
(
    const label nActiveFaces,
    labelList& cellFaces,
    labelList& cellFaceOffsets
) const
{
    cellFaces.setSize(2*nActiveFaces);
    cellFaceOffsets.setSize(cellMap_.size() + 1);

    // Faces per cell
    labelList nNbrs(cellMap_.size(), 0);

    // 1. Count faces per cell

    for (label faceI = 0; faceI < nActiveFaces; faceI++)
    {
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
        if (faceOwner_[faceI] < 0)
        {
            FatalErrorIn
            (
                "polyTopoChange::makeCells\n"
                "(\n"
                "    const label,\n"
                "    labelList&,\n"
                "    labelList&\n"
                ") const\n"
            )   << "Face " << faceI << " is active but its owner has"
                << " been deleted. This is usually due to deleting cells"
                << " without modifying exposed faces to be boundary faces."
                << exit(FatalError);
        }
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
        nNbrs[faceOwner_[faceI]]++;
    }
    for (label faceI = 0; faceI < nActiveFaces; faceI++)
    {
        if (faceNeighbour_[faceI] >= 0)
        {
            nNbrs[faceNeighbour_[faceI]]++;
        }
    }

    // 2. Calculate offsets

    cellFaceOffsets[0] = 0;
    forAll(nNbrs, cellI)
    {
        cellFaceOffsets[cellI+1] = cellFaceOffsets[cellI] + nNbrs[cellI];
    }

    // 3. Fill faces per cell

    // reset the whole list to use as counter
    nNbrs = 0;

    for (label faceI = 0; faceI < nActiveFaces; faceI++)
    {
        label cellI = faceOwner_[faceI];

        cellFaces[cellFaceOffsets[cellI] + nNbrs[cellI]++] = faceI;
    }

    for (label faceI = 0; faceI < nActiveFaces; faceI++)
    {
        label cellI = faceNeighbour_[faceI];

        if (cellI >= 0)
        {
            cellFaces[cellFaceOffsets[cellI] + nNbrs[cellI]++] = faceI;
        }
    }

    // Last offset points to beyond end of cellFaces.
    cellFaces.setSize(cellFaceOffsets[cellMap_.size()]);
}


// Create cell-cell addressing. Called after compaction (but before ordering)
// of faces
void Foam::polyTopoChange::makeCellCells
(
    const label nActiveFaces,
    CompactListList<label>& cellCells
) const
{
    // Neighbours per cell
    labelList nNbrs(cellMap_.size(), 0);

    // 1. Count neighbours (through internal faces) per cell

    for (label faceI = 0; faceI < nActiveFaces; faceI++)
    {
        if (faceNeighbour_[faceI] >= 0)
        {
            nNbrs[faceOwner_[faceI]]++;
            nNbrs[faceNeighbour_[faceI]]++;
        }
    }

mattijs's avatar
mattijs committed
581
582
    // 2. Construct csr
    cellCells.setSize(nNbrs);
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617


    // 3. Fill faces per cell

    // reset the whole list to use as counter
    nNbrs = 0;

    for (label faceI = 0; faceI < nActiveFaces; faceI++)
    {
        label nei = faceNeighbour_[faceI];

        if (nei >= 0)
        {
            label own = faceOwner_[faceI];
            cellCells.m()[cellCells.index(own, nNbrs[own]++)] = nei;
            cellCells.m()[cellCells.index(nei, nNbrs[nei]++)] = own;
        }
    }
}


// Cell ordering (based on bandCompression).
// Handles removed cells. Returns number of remaining cells.
Foam::label Foam::polyTopoChange::getCellOrder
(
    const CompactListList<label>& cellCellAddressing,
    labelList& oldToNew
) const
{
    labelList newOrder(cellCellAddressing.size());

    // Fifo buffer for string of cells
    SLList<label> nextCell;

    // Whether cell has been done already
618
    PackedBoolList visited(cellCellAddressing.size());
619
620
621
622

    label cellInOrder = 0;


623
624
625
626
627
628
629
630
631
632
633
    // Work arrays. Kept outside of loop to minimise allocations.
    // - neighbour cells
    DynamicList<label> nbrs;
    // - corresponding weights
    DynamicList<label> weights;

    // - ordering
    labelList order;


    while (true)
634
    {
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
        // For a disconnected region find the lowest connected cell.

        label currentCell = -1;
        label minWeight = labelMax;

        forAll(visited, cellI)
        {
            // find the lowest connected cell that has not been visited yet
            if (!cellRemoved(cellI) && !visited[cellI])
            {
                if (cellCellAddressing[cellI].size() < minWeight)
                {
                    minWeight = cellCellAddressing[cellI].size();
                    currentCell = cellI;
                }
            }
        }


        if (currentCell == -1)
655
        {
656
657
            break;
        }
658
659


660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
        // Starting from currentCell walk breadth-first


        // use this cell as a start
        nextCell.append(currentCell);

        // loop through the nextCell list. Add the first cell into the
        // cell order if it has not already been visited and ask for its
        // neighbours. If the neighbour in question has not been visited,
        // add it to the end of the nextCell list

        while (nextCell.size())
        {
            currentCell = nextCell.removeHead();

            if (!visited[currentCell])
676
            {
677
                visited[currentCell] = 1;
678

679
680
681
                // add into cellOrder
                newOrder[cellInOrder] = currentCell;
                cellInOrder++;
682

683
                // find if the neighbours have been visited
684
                const labelUList neighbours = cellCellAddressing[currentCell];
685

686
                // Add in increasing order of connectivity
687

688
689
690
                // 1. Count neighbours of unvisited neighbours
                nbrs.clear();
                weights.clear();
691

692
693
694
695
696
697
698
699
                forAll(neighbours, nI)
                {
                    label nbr = neighbours[nI];
                    if (!cellRemoved(nbr) && !visited[nbr])
                    {
                        // not visited, add to the list
                        nbrs.append(nbr);
                        weights.append(cellCellAddressing[nbr].size());
700
701
                    }
                }
702
703
704
705
706
707
708
                // 2. Sort
                sortedOrder(weights, order);
                // 3. Add in sorted order
                forAll(order, i)
                {
                    nextCell.append(nbrs[i]);
                }
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
            }
        }
    }

    // Now we have new-to-old in newOrder.
    newOrder.setSize(cellInOrder);

    // Invert to get old-to-new. Make sure removed (i.e. unmapped) cells are -1.
    oldToNew = invert(cellCellAddressing.size(), newOrder);

    return cellInOrder;
}


// Determine order for faces:
// - upper-triangular order for internal faces
// - external faces after internal faces and in patch order.
void Foam::polyTopoChange::getFaceOrder
(
    const label nActiveFaces,
    const labelList& cellFaces,
    const labelList& cellFaceOffsets,

    labelList& oldToNew,
    labelList& patchSizes,
    labelList& patchStarts
) const
{
    oldToNew.setSize(faceOwner_.size());
    oldToNew = -1;

    // First unassigned face
    label newFaceI = 0;

743
744
745
    labelList nbr;
    labelList order;

746
747
748
749
750
751
    forAll(cellMap_, cellI)
    {
        label startOfCell = cellFaceOffsets[cellI];
        label nFaces = cellFaceOffsets[cellI+1] - startOfCell;

        // Neighbouring cells
752
753
        //SortableList<label> nbr(nFaces);
        nbr.setSize(nFaces);
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791

        for (label i = 0; i < nFaces; i++)
        {
            label faceI = cellFaces[startOfCell + i];

            label nbrCellI = faceNeighbour_[faceI];

            if (faceI >= nActiveFaces)
            {
                // Retired face.
                nbr[i] = -1;
            }
            else if (nbrCellI != -1)
            {
                // Internal face. Get cell on other side.
                if (nbrCellI == cellI)
                {
                    nbrCellI = faceOwner_[faceI];
                }

                if (cellI < nbrCellI)
                {
                    // CellI is master
                    nbr[i] = nbrCellI;
                }
                else
                {
                    // nbrCell is master. Let it handle this face.
                    nbr[i] = -1;
                }
            }
            else
            {
                // External face. Do later.
                nbr[i] = -1;
            }
        }

792
793
794
795
796
797
798
799
800
801
802
803
804
        //nbr.sort();
        order.setSize(nFaces);
        sortedOrder(nbr, order);

        //forAll(nbr, i)
        //{
        //    if (nbr[i] != -1)
        //    {
        //        oldToNew[cellFaces[startOfCell + nbr.indices()[i]]] =
        //            newFaceI++;
        //    }
        //}
        forAll(order, i)
805
        {
806
807
            label index = order[i];
            if (nbr[index] != -1)
808
            {
809
                oldToNew[cellFaces[startOfCell + index]] = newFaceI++;
810
811
812
813
814
815
816
817
818
819
820
            }
        }
    }


    // Pick up all patch faces in patch face order.
    patchStarts.setSize(nPatches_);
    patchStarts = 0;
    patchSizes.setSize(nPatches_);
    patchSizes = 0;

821
    if (nPatches_ > 0)
822
    {
823
824
825
        patchStarts[0] = newFaceI;

        for (label faceI = 0; faceI < nActiveFaces; faceI++)
826
        {
827
828
829
830
            if (region_[faceI] >= 0)
            {
                patchSizes[region_[faceI]]++;
            }
831
832
        }

833
        label faceI = patchStarts[0];
834

835
836
837
838
839
        forAll(patchStarts, patchI)
        {
            patchStarts[patchI] = faceI;
            faceI += patchSizes[patchI];
        }
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
    }

    //if (debug)
    //{
    //    Pout<< "patchSizes:" << patchSizes << nl
    //        << "patchStarts:" << patchStarts << endl;
    //}

    labelList workPatchStarts(patchStarts);

    for (label faceI = 0; faceI < nActiveFaces; faceI++)
    {
        if (region_[faceI] >= 0)
        {
            oldToNew[faceI] = workPatchStarts[region_[faceI]]++;
        }
    }

    // Retired faces.
    for (label faceI = nActiveFaces; faceI < oldToNew.size(); faceI++)
    {
        oldToNew[faceI] = faceI;
    }

    // Check done all faces.
    forAll(oldToNew, faceI)
    {
        if (oldToNew[faceI] == -1)
        {
            FatalErrorIn
            (
                "polyTopoChange::getFaceOrder"
                "(const label, const labelList&, const labelList&)"
                " const"
            )   << "Did not determine new position"
                << " for face " << faceI
876
877
878
879
                << " owner " << faceOwner_[faceI]
                << " neighbour " << faceNeighbour_[faceI]
                << " region " << region_[faceI] << endl
                << "This is usually caused by not specifying a patch for"
880
881
882
883
884
885
886
887
888
889
                << " a boundary face." << nl
                << "Switch on the polyTopoChange::debug flag to catch"
                << " this error earlier." << nl;
            if (hasValidPoints(faces_[faceI]))
            {
                FatalError
                        << "points (removed points marked with "
                        << vector::max << ") " << facePoints(faces_[faceI]);
            }
            FatalError << abort(FatalError);
890
891
892
893
894
        }
    }
}


Mark Olesen's avatar
Mark Olesen committed
895
// Reorder and compact faces according to map.
896
897
898
899
900
901
902
void Foam::polyTopoChange::reorderCompactFaces
(
    const label newSize,
    const labelList& oldToNew
)
{
    reorder(oldToNew, faces_);
mattijs's avatar
mattijs committed
903
    faces_.setCapacity(newSize);
904
905

    reorder(oldToNew, region_);
mattijs's avatar
mattijs committed
906
    region_.setCapacity(newSize);
907
908

    reorder(oldToNew, faceOwner_);
mattijs's avatar
mattijs committed
909
    faceOwner_.setCapacity(newSize);
910
911

    reorder(oldToNew, faceNeighbour_);
mattijs's avatar
mattijs committed
912
    faceNeighbour_.setCapacity(newSize);
913
914
915

    // Update faceMaps.
    reorder(oldToNew, faceMap_);
mattijs's avatar
mattijs committed
916
917
    faceMap_.setCapacity(newSize);

918
919
920
921
    renumberReverseMap(oldToNew, reverseFaceMap_);

    renumberKey(oldToNew, faceFromPoint_);
    renumberKey(oldToNew, faceFromEdge_);
mattijs's avatar
mattijs committed
922
923
    inplaceReorder(oldToNew, flipFaceFlux_);
    flipFaceFlux_.setCapacity(newSize);
924
    renumberKey(oldToNew, faceZone_);
mattijs's avatar
mattijs committed
925
926
    inplaceReorder(oldToNew, faceZoneFlip_);
    faceZoneFlip_.setCapacity(newSize);
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
}


// Compact all and orders points and faces:
// - points into internal followed by external points
// - internalfaces upper-triangular
// - externalfaces after internal ones.
void Foam::polyTopoChange::compact
(
    const bool orderCells,
    const bool orderPoints,
    label& nInternalPoints,
    labelList& patchSizes,
    labelList& patchStarts
)
{
    points_.shrink();
    pointMap_.shrink();
    reversePointMap_.shrink();

    faces_.shrink();
    region_.shrink();
    faceOwner_.shrink();
    faceNeighbour_.shrink();
    faceMap_.shrink();
    reverseFaceMap_.shrink();

    cellMap_.shrink();
    reverseCellMap_.shrink();
    cellZone_.shrink();


    // Compact points
    label nActivePoints = 0;
    {
        labelList localPointMap(points_.size(), -1);
        label newPointI = 0;

        if (!orderPoints)
        {
            nInternalPoints = -1;

            forAll(points_, pointI)
            {
                if (!pointRemoved(pointI) && !retiredPoints_.found(pointI))
                {
                    localPointMap[pointI] = newPointI++;
                }
            }
            nActivePoints = newPointI;
        }
        else
        {
            forAll(points_, pointI)
            {
                if (!pointRemoved(pointI) && !retiredPoints_.found(pointI))
                {
                    nActivePoints++;
                }
            }

            // Mark boundary points
            forAll(faceOwner_, faceI)
            {
                if
                (
                   !faceRemoved(faceI)
                 && faceOwner_[faceI] >= 0
                 && faceNeighbour_[faceI] < 0
                )
                {
                    // Valid boundary face
                    const face& f = faces_[faceI];

For faster browsing, not all history is shown. View entire blame