GAMGAgglomeration.C 17.9 KB
Newer Older
1 2 3 4
/*---------------------------------------------------------------------------*\
  =========                 |
  \\      /  F ield         | OpenFOAM: The Open Source CFD Toolbox
   \\    /   O peration     |
OpenFOAM bot's avatar
OpenFOAM bot committed
5
    \\  /    A nd           | www.openfoam.com
6
     \\/     M anipulation  |
OpenFOAM bot's avatar
OpenFOAM bot committed
7
-------------------------------------------------------------------------------
OpenFOAM bot's avatar
OpenFOAM bot committed
8
    Copyright (C) 2011-2016 OpenFOAM Foundation
9
    Copyright (C) 2019-2020 OpenCFD Ltd.
10 11 12 13
-------------------------------------------------------------------------------
License
    This file is part of OpenFOAM.

14 15 16 17
    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.
18 19 20 21 22 23 24

    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
25
    along with OpenFOAM.  If not, see <http://www.gnu.org/licenses/>.
26 27 28 29 30 31 32

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

#include "GAMGAgglomeration.H"
#include "lduMesh.H"
#include "lduMatrix.H"
#include "Time.H"
33
#include "GAMGInterface.H"
mattijs's avatar
mattijs committed
34
#include "GAMGProcAgglomeration.H"
35
#include "pairGAMGAgglomeration.H"
36
#include "IOmanip.H"
37 38 39 40 41 42 43 44

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

namespace Foam
{
    defineTypeNameAndDebug(GAMGAgglomeration, 0);
    defineRunTimeSelectionTable(GAMGAgglomeration, lduMesh);
    defineRunTimeSelectionTable(GAMGAgglomeration, lduMatrix);
mattijs's avatar
mattijs committed
45
    defineRunTimeSelectionTable(GAMGAgglomeration, geometry);
46 47 48
}


Henry Weller's avatar
Henry Weller committed
49
// * * * * * * * * * * * * Protected Member Functions  * * * * * * * * * * * //
50 51 52 53 54

void Foam::GAMGAgglomeration::compactLevels(const label nCreatedLevels)
{
    nCells_.setSize(nCreatedLevels);
    restrictAddressing_.setSize(nCreatedLevels);
55 56 57 58 59
    nFaces_.setSize(nCreatedLevels);
    faceRestrictAddressing_.setSize(nCreatedLevels);
    faceFlipMap_.setSize(nCreatedLevels);
    nPatchFaces_.setSize(nCreatedLevels);
    patchFaceRestrictAddressing_.setSize(nCreatedLevels);
60
    meshLevels_.setSize(nCreatedLevels);
61 62 63

    // Have procCommunicator_ always, even if not procAgglomerating
    procCommunicator_.setSize(nCreatedLevels + 1);
mattijs's avatar
mattijs committed
64
    if (processorAgglomerate())
65 66 67 68 69 70 71 72
    {
        procAgglomMap_.setSize(nCreatedLevels);
        agglomProcIDs_.setSize(nCreatedLevels);
        procCellOffsets_.setSize(nCreatedLevels);
        procFaceMap_.setSize(nCreatedLevels);
        procBoundaryMap_.setSize(nCreatedLevels);
        procBoundaryFaceMap_.setSize(nCreatedLevels);

mattijs's avatar
mattijs committed
73
        procAgglomeratorPtr_().agglomerate();
mattijs's avatar
mattijs committed
74

75

76
    }
77

78
    // Print a bit
mattijs's avatar
mattijs committed
79
    if (debug)
80 81 82 83 84 85
    {
        Info<< "GAMGAgglomeration:" << nl
            << "    local agglomerator     : " << type() << nl;
        if (processorAgglomerate())
        {
            Info<< "    processor agglomerator : "
86 87
                << procAgglomeratorPtr_().type() << nl
                << nl;
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
        Info<< setw(36) << "nCells"
            << setw(20) << "nFaces/nCells"
            << setw(20) << "nInterfaces"
            << setw(20) << "nIntFaces/nCells"
            << setw(12) << "profile"
            << nl
            << setw(8) << "Level"
            << setw(8) << "nProcs"
            << "    "
            << setw(8) << "avg"
            << setw(8) << "max"
            << "    "
            << setw(8) << "avg"
            << setw(8) << "max"
            << "    "
            << setw(8) << "avg"
            << setw(8) << "max"
            << "    "
            << setw(8) << "avg"
            << setw(8) << "max"
            //<< "    "
            << setw(12) << "avg"
            << nl
            << setw(8) << "-----"
            << setw(8) << "------"
            << "    "
            << setw(8) << "---"
            << setw(8) << "---"
            << "    "
            << setw(8) << "---"
            << setw(8) << "---"
            << "    "
            << setw(8) << "---"
            << setw(8) << "---"
            << "    "
            << setw(8) << "---"
            << setw(8) << "---"
            //<< "    "
            << setw(12) << "---"
            //<< "    "
            << nl;

        for (label levelI = 0; levelI <= size(); levelI++)
        {
            label nProcs = 0;
            label nCells = 0;
            scalar faceCellRatio = 0;
            label nInterfaces = 0;
            label nIntFaces = 0;
            scalar ratio = 0.0;
            scalar profile = 0.0;

            if (hasMeshLevel(levelI))
mattijs's avatar
mattijs committed
143
            {
144
                nProcs = 1;
145

146 147 148 149
                const lduMesh& fineMesh = meshLevel(levelI);
                nCells = fineMesh.lduAddr().size();
                faceCellRatio =
                    scalar(fineMesh.lduAddr().lowerAddr().size())/nCells;
150

151 152 153 154 155
                const lduInterfacePtrsList interfaces =
                    fineMesh.interfaces();
                forAll(interfaces, i)
                {
                    if (interfaces.set(i))
156
                    {
157 158
                        nInterfaces++;
                        nIntFaces += interfaces[i].faceCells().size();
159 160
                    }
                }
161
                ratio = scalar(nIntFaces)/nCells;
162

163
                profile = fineMesh.lduAddr().band().second();
mattijs's avatar
mattijs committed
164
            }
165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183

            label totNprocs = returnReduce(nProcs, sumOp<label>());

            label maxNCells = returnReduce(nCells, maxOp<label>());
            label totNCells = returnReduce(nCells, sumOp<label>());

            scalar maxFaceCellRatio =
                returnReduce(faceCellRatio, maxOp<scalar>());
            scalar totFaceCellRatio =
                returnReduce(faceCellRatio, sumOp<scalar>());

            label maxNInt = returnReduce(nInterfaces, maxOp<label>());
            label totNInt = returnReduce(nInterfaces, sumOp<label>());

            scalar maxRatio = returnReduce(ratio, maxOp<scalar>());
            scalar totRatio = returnReduce(ratio, sumOp<scalar>());

            scalar totProfile = returnReduce(profile, sumOp<scalar>());

mattijs's avatar
mattijs committed
184 185
            int oldPrecision = Info().precision(4);

186 187 188 189 190 191
            Info<< setw(8) << levelI
                << setw(8) << totNprocs
                << "    "
                << setw(8) << totNCells/totNprocs
                << setw(8) << maxNCells
                << "    "
mattijs's avatar
mattijs committed
192 193
                << setw(8) << totFaceCellRatio/totNprocs
                << setw(8) << maxFaceCellRatio
194 195 196 197
                << "    "
                << setw(8) << scalar(totNInt)/totNprocs
                << setw(8) << maxNInt
                << "    "
mattijs's avatar
mattijs committed
198 199 200
                << setw(8) << totRatio/totNprocs
                << setw(8) << maxRatio
                << setw(12) << totProfile/totNprocs
201
                << nl;
mattijs's avatar
mattijs committed
202 203

            Info().precision(oldPrecision);
mattijs's avatar
mattijs committed
204
        }
205
        Info<< endl;
mattijs's avatar
mattijs committed
206
    }
207 208 209 210 211
}


bool Foam::GAMGAgglomeration::continueAgglomerating
(
212
    const label nFineCells,
213 214 215
    const label nCoarseCells
) const
{
216
    const label nTotalCoarseCells = returnReduce(nCoarseCells, sumOp<label>());
217
    if (nTotalCoarseCells < Pstream::nProcs()*nCellsInCoarsestLevel_)
218
    {
219
        return false;
220 221 222 223 224 225
    }
    else
    {
        const label nTotalFineCells = returnReduce(nFineCells, sumOp<label>());
        return nTotalCoarseCells < nTotalFineCells;
    }
226 227 228 229 230 231 232 233 234 235 236
}


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

Foam::GAMGAgglomeration::GAMGAgglomeration
(
    const lduMesh& mesh,
    const dictionary& controlDict
)
:
237
    MeshObject<lduMesh, Foam::GeometricMeshObject, GAMGAgglomeration>(mesh),
238 239 240 241 242

    maxLevels_(50),

    nCellsInCoarsestLevel_
    (
243
        controlDict.getOrDefault<label>("nCellsInCoarsestLevel", 10)
244
    ),
245
    meshInterfaces_(mesh.interfaces()),
mattijs's avatar
mattijs committed
246
    procAgglomeratorPtr_
247
    (
248 249 250 251
        (
            (UPstream::nProcs(mesh.comm()) > 1)
         && controlDict.found("processorAgglomerator")
        )
mattijs's avatar
mattijs committed
252 253
      ? GAMGProcAgglomeration::New
        (
254
            controlDict.get<word>("processorAgglomerator"),
mattijs's avatar
mattijs committed
255 256 257
            *this,
            controlDict
        )
258
      : autoPtr<GAMGProcAgglomeration>()
259
    ),
260 261 262

    nCells_(maxLevels_),
    restrictAddressing_(maxLevels_),
263
    nFaces_(maxLevels_),
264
    faceRestrictAddressing_(maxLevels_),
265 266 267
    faceFlipMap_(maxLevels_),
    nPatchFaces_(maxLevels_),
    patchFaceRestrictAddressing_(maxLevels_),
268

269
    meshLevels_(maxLevels_)
270
{
271 272 273 274 275 276 277 278
    // Limit the cells in the coarsest level based on the local number of
    // cells.  Note: 2 for pair-wise
    nCellsInCoarsestLevel_ =
        max(1, min(mesh.lduAddr().size()/2, nCellsInCoarsestLevel_));

    // Ensure all procs see the same nCellsInCoarsestLevel_
    reduce(nCellsInCoarsestLevel_, minOp<label>());

279
    procCommunicator_.setSize(maxLevels_ + 1, -1);
mattijs's avatar
mattijs committed
280
    if (processorAgglomerate())
281 282 283 284 285 286 287 288 289
    {
        procAgglomMap_.setSize(maxLevels_);
        agglomProcIDs_.setSize(maxLevels_);
        procCellOffsets_.setSize(maxLevels_);
        procFaceMap_.setSize(maxLevels_);
        procBoundaryMap_.setSize(maxLevels_);
        procBoundaryFaceMap_.setSize(maxLevels_);
    }
}
290 291 292 293 294 295 296 297 298 299


const Foam::GAMGAgglomeration& Foam::GAMGAgglomeration::New
(
    const lduMesh& mesh,
    const dictionary& controlDict
)
{
    if
    (
mattijs's avatar
mattijs committed
300
        !mesh.thisDb().foundObject<GAMGAgglomeration>
301 302 303 304 305
        (
            GAMGAgglomeration::typeName
        )
    )
    {
306 307
        const word agglomeratorType
        (
308
            controlDict.getOrDefault<word>("agglomerator", "faceAreaPair")
309
        );
310

311
        const_cast<Time&>(mesh.thisDb().time()).libs().open
312 313 314 315 316 317
        (
            controlDict,
            "geometricGAMGAgglomerationLibs",
            lduMeshConstructorTablePtr_
        );

318
        auto cstrIter = lduMeshConstructorTablePtr_->cfind(agglomeratorType);
319

320
        if (!cstrIter.found())
321
        {
322 323
            FatalErrorInFunction
                << "Unknown GAMGAgglomeration type "
324
                << agglomeratorType << ".\n"
325
                << "Valid matrix GAMGAgglomeration types :"
326
                << lduMatrixConstructorTablePtr_->sortedToc() << endl
327
                << "Valid geometric GAMGAgglomeration types :"
328
                << lduMeshConstructorTablePtr_->sortedToc()
329 330 331 332 333 334 335
                << exit(FatalError);
        }

        return store(cstrIter()(mesh, controlDict).ptr());
    }
    else
    {
mattijs's avatar
mattijs committed
336
        return mesh.thisDb().lookupObject<GAMGAgglomeration>
337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353
        (
            GAMGAgglomeration::typeName
        );
    }
}


const Foam::GAMGAgglomeration& Foam::GAMGAgglomeration::New
(
    const lduMatrix& matrix,
    const dictionary& controlDict
)
{
    const lduMesh& mesh = matrix.mesh();

    if
    (
mattijs's avatar
mattijs committed
354
        !mesh.thisDb().foundObject<GAMGAgglomeration>
355 356 357 358 359
        (
            GAMGAgglomeration::typeName
        )
    )
    {
360 361
        const word agglomeratorType
        (
362
            controlDict.getOrDefault<word>("agglomerator", "faceAreaPair")
363
        );
364

365
        const_cast<Time&>(mesh.thisDb().time()).libs().open
366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381
        (
            controlDict,
            "algebraicGAMGAgglomerationLibs",
            lduMatrixConstructorTablePtr_
        );

        if
        (
            !lduMatrixConstructorTablePtr_
         || !lduMatrixConstructorTablePtr_->found(agglomeratorType)
        )
        {
            return New(mesh, controlDict);
        }
        else
        {
382 383
            auto cstrIter =
                lduMatrixConstructorTablePtr_->cfind(agglomeratorType);
384 385 386 387 388 389

            return store(cstrIter()(matrix, controlDict).ptr());
        }
    }
    else
    {
mattijs's avatar
mattijs committed
390
        return mesh.thisDb().lookupObject<GAMGAgglomeration>
391 392 393 394 395 396 397
        (
            GAMGAgglomeration::typeName
        );
    }
}


mattijs's avatar
mattijs committed
398 399 400 401 402 403 404
Foam::autoPtr<Foam::GAMGAgglomeration> Foam::GAMGAgglomeration::New
(
    const lduMesh& mesh,
    const scalarField& cellVolumes,
    const vectorField& faceAreas,
    const dictionary& controlDict
)
405
{
406 407
    const word agglomeratorType
    (
408
        controlDict.getOrDefault<word>("agglomerator", "faceAreaPair")
409
    );
mattijs's avatar
mattijs committed
410 411 412 413 414 415 416 417

    const_cast<Time&>(mesh.thisDb().time()).libs().open
    (
        controlDict,
        "geometricGAMGAgglomerationLibs",
        geometryConstructorTablePtr_
    );

418
    auto cstrIter = geometryConstructorTablePtr_->cfind(agglomeratorType);
mattijs's avatar
mattijs committed
419

420
    if (!cstrIter.found())
421
    {
422 423
        FatalErrorInFunction
            << "Unknown GAMGAgglomeration type "
mattijs's avatar
mattijs committed
424
            << agglomeratorType << ".\n"
425
            << "Valid geometric GAMGAgglomeration types :"
mattijs's avatar
mattijs committed
426 427
            << geometryConstructorTablePtr_->sortedToc()
            << exit(FatalError);
428
    }
mattijs's avatar
mattijs committed
429 430 431 432 433 434 435 436 437 438 439

    return autoPtr<GAMGAgglomeration>
    (
        cstrIter()
        (
            mesh,
            cellVolumes,
            faceAreas,
            controlDict
        )
    );
440 441 442
}


mattijs's avatar
mattijs committed
443 444 445 446 447 448
// * * * * * * * * * * * * * * * * Destructor  * * * * * * * * * * * * * * * //

Foam::GAMGAgglomeration::~GAMGAgglomeration()
{}


449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466
// * * * * * * * * * * * * * * * Member Functions  * * * * * * * * * * * * * //

const Foam::lduMesh& Foam::GAMGAgglomeration::meshLevel
(
    const label i
) const
{
    if (i == 0)
    {
        return mesh_;
    }
    else
    {
        return meshLevels_[i - 1];
    }
}


467 468 469 470 471 472 473 474 475 476 477 478 479
bool Foam::GAMGAgglomeration::hasMeshLevel(const label i) const
{
    if (i == 0)
    {
        return true;
    }
    else
    {
        return meshLevels_.set(i - 1);
    }
}


480 481 482 483 484
const Foam::lduInterfacePtrsList& Foam::GAMGAgglomeration::interfaceLevel
(
    const label i
) const
{
485 486 487 488 489 490 491 492
    if (i == 0)
    {
        return meshInterfaces_;
    }
    else
    {
        return meshLevels_[i - 1].rawInterfaces();
    }
493 494 495
}


496 497 498 499
void Foam::GAMGAgglomeration::clearLevel(const label i)
{
    if (hasMeshLevel(i))
    {
500
        meshLevels_.set(i - 1, nullptr);
501 502 503 504

        if (i < nCells_.size())
        {
            nCells_[i] = -555;
505
            restrictAddressing_.set(i, nullptr);
506
            nFaces_[i] = -666;
507 508 509 510
            faceRestrictAddressing_.set(i, nullptr);
            faceFlipMap_.set(i, nullptr);
            nPatchFaces_.set(i, nullptr);
            patchFaceRestrictAddressing_.set(i, nullptr);
511 512 513 514 515
        }
    }
}


516
const Foam::labelList& Foam::GAMGAgglomeration::procAgglomMap
517
(
518
    const label leveli
519 520
) const
{
521 522
    return procAgglomMap_[leveli];
}
523 524


525 526 527 528 529 530 531
const Foam::labelList& Foam::GAMGAgglomeration::agglomProcIDs
(
    const label leveli
) const
{
    return agglomProcIDs_[leveli];
}
532

533

534 535
bool Foam::GAMGAgglomeration::hasProcMesh(const label leveli) const
{
536
    return procCommunicator_[leveli] != -1;
537 538 539
}


540
Foam::label Foam::GAMGAgglomeration::procCommunicator(const label leveli) const
541
{
542
    return procCommunicator_[leveli];
543 544 545
}


546 547 548 549
const Foam::labelList& Foam::GAMGAgglomeration::cellOffsets
(
    const label leveli
) const
550
{
551
    return procCellOffsets_[leveli];
552 553 554
}


555 556 557 558
const Foam::labelListList& Foam::GAMGAgglomeration::faceMap
(
    const label leveli
) const
559
{
560
    return procFaceMap_[leveli];
561 562 563
}


564 565 566 567
const Foam::labelListList& Foam::GAMGAgglomeration::boundaryMap
(
    const label leveli
) const
568
{
569
    return procBoundaryMap_[leveli];
570 571 572
}


573 574 575 576
const Foam::labelListListList& Foam::GAMGAgglomeration::boundaryFaceMap
(
    const label leveli
) const
577
{
578
    return procBoundaryFaceMap_[leveli];
579 580 581
}


582 583 584 585 586
bool Foam::GAMGAgglomeration::checkRestriction
(
    labelList& newRestrict,
    label& nNewCoarse,
    const lduAddressing& fineAddressing,
587
    const labelUList& restriction,
588 589 590
    const label nCoarse
)
{
591
    if (fineAddressing.size() != restriction.size())
592
    {
593 594
        FatalErrorInFunction
            << "nCells:" << fineAddressing.size()
595
            << " agglom:" << restriction.size()
596 597 598 599 600 601 602 603 604 605 606 607 608 609
            << abort(FatalError);
    }

    // Seed (master) for every region
    labelList master(identity(fineAddressing.size()));

    // Now loop and transport master through region
    const labelUList& lower = fineAddressing.lowerAddr();
    const labelUList& upper = fineAddressing.upperAddr();

    while (true)
    {
        label nChanged = 0;

610
        forAll(lower, facei)
611
        {
612 613
            label own = lower[facei];
            label nei = upper[facei];
614

615
            if (restriction[own] == restriction[nei])
616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643
            {
                // coarse-mesh-internal face

                if (master[own] < master[nei])
                {
                    master[nei] = master[own];
                    nChanged++;
                }
                else if (master[own] > master[nei])
                {
                    master[own] = master[nei];
                    nChanged++;
                }
            }
        }

        reduce(nChanged, sumOp<label>());

        if (nChanged == 0)
        {
            break;
        }
    }


    // Count number of regions/masters per coarse cell
    labelListList coarseToMasters(nCoarse);
    nNewCoarse = 0;
644
    forAll(restriction, celli)
645
    {
646
        labelList& masters = coarseToMasters[restriction[celli]];
647

648
        if (!masters.found(master[celli]))
649
        {
650
            masters.append(master[celli]);
651 652 653 654 655 656
            nNewCoarse++;
        }
    }

    if (nNewCoarse > nCoarse)
    {
657
        //WarningInFunction
658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674
        //    << "Have " << nCoarse
        //    << " agglomerated cells but " << nNewCoarse
        //    << " disconnected regions" << endl;

        // Keep coarseToMasters[0] the original coarse, allocate new ones
        // for the others
        labelListList coarseToNewCoarse(coarseToMasters.size());

        nNewCoarse = nCoarse;

        forAll(coarseToMasters, coarseI)
        {
            const labelList& masters = coarseToMasters[coarseI];

            labelList& newCoarse = coarseToNewCoarse[coarseI];
            newCoarse.setSize(masters.size());
            newCoarse[0] = coarseI;
675
            for (label i=1; i<newCoarse.size(); i++)
676 677 678 679 680 681
            {
                newCoarse[i] = nNewCoarse++;
            }
        }

        newRestrict.setSize(fineAddressing.size());
682
        forAll(restriction, celli)
683
        {
684
            const label coarseI = restriction[celli];
685

686
            label index = coarseToMasters[coarseI].find(master[celli]);
687
            newRestrict[celli] = coarseToNewCoarse[coarseI][index];
688 689 690 691
        }

        return false;
    }
692 693

    return true;
694 695 696
}


697
// ************************************************************************* //