volRegion.C 5.87 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
OpenFOAM bot's avatar
OpenFOAM bot committed
6 7
     \\/     M anipulation  |
-------------------------------------------------------------------------------
OpenFOAM bot's avatar
OpenFOAM bot committed
8
    Copyright (C) 2016 OpenFOAM Foundation
9
    Copyright (C) 2016-2020 OpenCFD Ltd.
10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30
-------------------------------------------------------------------------------
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 "volRegion.H"
#include "volMesh.H"
31
#include "cellSet.H"
32 33 34 35 36 37 38 39 40 41 42 43 44
#include "globalMeshData.H"

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

namespace Foam
{
namespace functionObjects
{
    defineTypeNameAndDebug(volRegion, 0);
}
}


45
const Foam::Enum
46
<
47 48 49
    Foam::functionObjects::volRegion::regionTypes
>
Foam::functionObjects::volRegion::regionTypeNames_
Mark Olesen's avatar
Mark Olesen committed
50
({
51
    { regionTypes::vrtAll, "all" },
52 53
    { regionTypes::vrtCellSet, "cellSet" },
    { regionTypes::vrtCellZone, "cellZone" },
Mark Olesen's avatar
Mark Olesen committed
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
// * * * * * * * * * * * * * Private Member Functions  * * * * * * * * * * * //

void Foam::functionObjects::volRegion::calculateCache()
{
    regionID_ = -1;
    cellIds_.clear();

    switch (regionType_)
    {
        case vrtAll:
        {
            nCells_ = volMesh_.globalData().nTotalCells();
            V_ = gSum(volMesh_.V());
            return;
            break;
        }

        case vrtCellSet:
        {
            cellIds_ = cellSet(volMesh_, regionName_).sortedToc();
            break;
        }

        case vrtCellZone:
        {
            regionID_ = volMesh_.cellZones().findZoneID(regionName_);

            if (regionID_ < 0)
            {
                FatalErrorInFunction
                    << "Unknown cell zone name: " << regionName_
                    << ". Valid cell zones    : "
                    << flatOutput(volMesh_.cellZones().names())
                    << exit(FatalError);
            }
            break;
        }
    }

    // Cached value for nCells()
    nCells_ = returnReduce(cellIDs().size(), sumOp<label>());

    // Cached value for V()
    V_ = 0;
    for (const label celli : cellIDs())
    {
        V_ += volMesh_.V()[celli];
    }
    reduce(V_, sumOp<scalar>());

    if (!nCells_)
    {
        FatalErrorInFunction
            << regionTypeNames_[regionType_]
            << "(" << regionName_ << "):" << nl
            << "    Region has no cells"
            << exit(FatalError);
    }
115 116

    requireUpdate_ = false;
117 118 119
}


120 121 122 123 124 125
// * * * * * * * * * * * * Protected Member Functions  * * * * * * * * * * * //

void Foam::functionObjects::volRegion::writeFileHeader
(
    const writeFile& wf,
    Ostream& file
126
) const
127
{
128 129 130 131 132
    wf.writeCommented(file, "Region");
    file<< setw(1) << ':' << setw(1) << ' '
        << regionTypeNames_[regionType_] << " " << regionName_ << endl;
    wf.writeHeaderValue(file, "Cells", nCells_);
    wf.writeHeaderValue(file, "Volume", V_);
133 134 135 136 137 138 139 140 141 142 143
}


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

Foam::functionObjects::volRegion::volRegion
(
    const fvMesh& mesh,
    const dictionary& dict
)
:
144
    volMesh_(mesh),
145
    requireUpdate_(true),
146 147 148
    cellIds_(),
    nCells_(0),
    V_(Zero),
149 150
    regionType_
    (
151
        regionTypeNames_.getOrDefault
152 153 154 155 156
        (
            "regionType",
            dict,
            regionTypes::vrtAll
        )
157
    ),
158
    regionName_(volMesh_.name()),
159 160 161 162 163 164 165 166
    regionID_(-1)
{
    read(dict);
}


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

167
bool Foam::functionObjects::volRegion::read(const dictionary& dict)
168 169 170
{
    switch (regionType_)
    {
171
        case vrtAll:
172
        {
173
            regionName_ = volMesh_.name();
174 175 176
            break;
        }

177
        case vrtCellSet:
178 179
        case vrtCellZone:
        {
180
            dict.readEntry("name", regionName_);
181 182 183 184 185 186 187
            break;
        }

        default:
        {
            FatalIOErrorInFunction(dict)
                << "Unknown region type. Valid region types are:"
188
                << flatOutput(regionTypeNames_.names()) << nl
189
                << exit(FatalIOError);
190
            break;
191 192 193
        }
    }

194
    calculateCache();
195 196 197 198 199 200
    return true;
}


const Foam::labelList& Foam::functionObjects::volRegion::cellIDs() const
{
201 202 203 204 205 206 207 208 209
    #ifdef FULLDEBUG
    if (requireUpdate_)
    {
        FatalErrorInFunction
            << "Retrieving cached values that are not up-to-date" << nl
            << exit(FatalError);
    }
    #endif

210
    switch (regionType_)
211
    {
212 213 214 215 216 217 218 219 220 221
        case vrtCellSet:
            return cellIds_;
            break;

        case vrtCellZone:
            return volMesh_.cellZones()[regionID_];
            break;

        default:
            break;
222
    }
223 224

    return labelList::null();
225 226 227
}


228 229 230 231 232 233 234 235 236 237 238 239
bool Foam::functionObjects::volRegion::update()
{
    if (requireUpdate_)
    {
        calculateCache();
        return true;
    }

    return false;
}


240
void Foam::functionObjects::volRegion::updateMesh(const mapPolyMesh&)
241
{
242
    requireUpdate_ = true;
243 244 245
}


246
void Foam::functionObjects::volRegion::movePoints(const polyMesh&)
247
{
248
    requireUpdate_ = true;
249 250 251 252
}


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