fvMeshSubsetProxy.C 5.01 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 8
-------------------------------------------------------------------------------
    Copyright (C) 2016-2019 OpenCFD Ltd.
9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27
-------------------------------------------------------------------------------
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/>.

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

28
#include "fvMeshSubsetProxy.H"
29 30 31 32 33 34
#include "cellSet.H"
#include "cellZone.H"
#include "Time.H"

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

35 36 37 38 39 40 41
Foam::fvMeshSubsetProxy::fvMeshSubsetProxy(fvMesh& baseMesh)
:
    baseMesh_(baseMesh),
    subsetter_(baseMesh),
    exposedPatchId_(-1),
    type_(NONE),
    name_(),
42 43
    names_(),
    selectedCells_()
44 45 46 47 48 49 50 51
{
    if (useSubMesh())
    {
        correct();
    }
}


52
Foam::fvMeshSubsetProxy::fvMeshSubsetProxy
53
(
54 55 56 57
    fvMesh& baseMesh,
    const subsetType type,
    const word& selectionName,
    label exposedPatchId
58 59 60 61
)
:
    baseMesh_(baseMesh),
    subsetter_(baseMesh),
62 63
    exposedPatchId_(exposedPatchId),
    type_(selectionName.empty() ? NONE : type),
64
    name_(),
65 66
    names_(),
    selectedCells_()
67
{
68 69 70 71 72 73 74 75 76 77 78 79 80 81 82
    if (type_ == ZONES)
    {
        // Populate wordRes for ZONES
        names_.resize(1);
        names_.first() = selectionName;
    }
    else if (type_ != NONE)
    {
        name_ = selectionName;
    }

    if (useSubMesh())
    {
        correct();
    }
83 84 85
}


86
Foam::fvMeshSubsetProxy::fvMeshSubsetProxy
87 88
(
    fvMesh& baseMesh,
89 90 91 92 93 94 95 96 97
    const wordRes& zoneNames,
    label exposedPatchId
)
:
    baseMesh_(baseMesh),
    subsetter_(baseMesh),
    exposedPatchId_(exposedPatchId),
    type_(ZONES),
    name_(),
98 99
    names_(zoneNames),
    selectedCells_()
100 101 102 103 104 105 106 107 108 109 110 111 112
{
    if (useSubMesh())
    {
        correct();
    }
}


Foam::fvMeshSubsetProxy::fvMeshSubsetProxy
(
    fvMesh& baseMesh,
    wordRes&& zoneNames,
    label exposedPatchId
113 114 115 116
)
:
    baseMesh_(baseMesh),
    subsetter_(baseMesh),
117 118 119
    exposedPatchId_(exposedPatchId),
    type_(ZONES),
    name_(),
120 121
    names_(std::move(zoneNames)),
    selectedCells_()
122
{
123 124 125 126
    if (useSubMesh())
    {
        correct();
    }
127 128 129 130 131
}


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

132
bool Foam::fvMeshSubsetProxy::correct(bool verbose)
133
{
134 135 136
    if (type_ == NONE)
    {
        subsetter_.clear();
137 138
        selectedCells_.clearStorage();
        return false;
139 140 141 142 143 144
    }

    const label nCells = baseMesh_.nCells();

    bitSet selectedCells;

145
    if (type_ == SET)
146 147 148 149 150 151
    {
        if (verbose)
        {
            Info<< "Subsetting mesh based on cellSet " << name_ << endl;
        }

152 153 154 155 156 157 158
        cellSet cset(baseMesh_, name_);

        selectedCells.resize(nCells);
        for (const label idx : cset)
        {
            selectedCells.set(idx);
        }
159
    }
160
    else if (type_ == ZONE)
161 162 163 164 165 166
    {
        if (verbose)
        {
            Info<< "Subsetting mesh based on cellZone " << name_ << endl;
        }

167 168
        selectedCells.resize(nCells);
        selectedCells.set(baseMesh_.cellZones()[name_]);
169
    }
170 171 172 173 174 175 176 177 178 179 180
    else if (type_ == ZONES)
    {
        if (verbose)
        {
            Info<< "Subsetting mesh based on cellZones "
                << flatOutput(names_) << endl;
        }

        selectedCells = baseMesh_.cellZones().selection(names_);
    }

181 182 183 184 185 186 187 188 189 190 191 192

    const bool changed = (selectedCells_ != selectedCells);

    // Use as a cached value for next time
    selectedCells_.transfer(selectedCells);

    if (changed || selectedCells_.empty())
    {
        subsetter_.setCellSubset(selectedCells_, exposedPatchId_);
    }

    return returnReduce(changed, orOp<bool>());
193 194 195
}


196
Foam::polyMesh::readUpdateState Foam::fvMeshSubsetProxy::readUpdate()
197
{
198
    const polyMesh::readUpdateState meshState = baseMesh_.readUpdate();
199

200 201 202 203 204 205 206 207 208 209
    if (meshState == polyMesh::POINTS_MOVED)
    {
        if (correct(true))
        {
            // The cellSet/cellZone changed on POINTS_MOVED,
            // treat like TOPO_CHANGE
            return polyMesh::TOPO_CHANGE;
        }
    }
    else if
210 211 212 213
    (
        meshState == polyMesh::TOPO_CHANGE
     || meshState == polyMesh::TOPO_PATCH_CHANGE
    )
214 215 216 217 218 219 220 221 222
    {
        correct(true);
    }

    return meshState;
}


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