foamVtkFvMeshAdaptor.C 9.61 KB
Newer Older
1 2 3 4
/*---------------------------------------------------------------------------*\
  =========                 |
  \\      /  F ield         | OpenFOAM: The Open Source CFD Toolbox
   \\    /   O peration     |
Mark Olesen's avatar
Mark Olesen committed
5 6
    \\  /    A nd           | Copyright (C) 2017-2018 OpenCFD Ltd.
     \\/     M anipulation  |
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
-------------------------------------------------------------------------------
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 "foamVtkFvMeshAdaptor.H"
#include "Pstream.H"

// OpenFOAM includes
#include "fvMesh.H"

// VTK includes
#include <vtkMultiBlockDataSet.h>
#include <vtkMultiPieceDataSet.h>
#include <vtkSmartPointer.h>
#include <vtkInformation.h>

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

namespace Foam
{
namespace vtk
{
    defineTypeNameAndDebug(fvMeshAdaptor, 0);
}
46 47
} // End namespace Foam

48 49 50

const Foam::Enum
<
51
    Foam::vtk::fvMeshAdaptor::channelType
52 53 54
>
Foam::vtk::fvMeshAdaptor::channelNames
{
55 56 57 58
    { channelType::NONE,     "none" },
    { channelType::INTERNAL, "internal" },
    { channelType::BOUNDARY, "boundary" },
    { channelType::ALL,      "all" },
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
// * * * * * * * * * * * * * Private Member Functions  * * * * * * * * * * * //

void Foam::vtk::fvMeshAdaptor::definePatchIds()
{
    // Generate or update the list of patchIds

    patchIds_.clear();

    if (!usingBoundary())
    {
        return;
    }

    // General patch information
    // Restrict to non-processor patches.
    // This value is invariant across all processors.

    const polyBoundaryMesh& patches = mesh_.boundaryMesh();
    const label nNonProcessor = patches.nNonProcessor();

    if (patchPatterns_.empty())
    {
        patchIds_ = identity(nNonProcessor);
    }
    else
    {
        labelHashSet ids(patches.patchSet(patchPatterns_, false, false));

        // Restricted to non-processor patches
        ids.filterKeys
        (
            [nNonProcessor](const label i){ return i < nNonProcessor; }
        );

        // MUST be sorted. Other internal logic relies upon this!
        patchIds_ = ids.sortedToc();
    }
}


102 103 104 105
// * * * * * * * * * * * * * * * * Constructors  * * * * * * * * * * * * * * //

Foam::vtk::fvMeshAdaptor::fvMeshAdaptor
(
106
    const fvMesh& mesh,
107 108
    const channelType channelsOpt,
    const wordRes& patchSelection
109 110 111
)
:
    mesh_(mesh),
112 113 114
    patchPatterns_(patchSelection),
    patchIds_(),
    channels_(channelsOpt),
115 116
    interpFields_(true),
    extrapPatches_(false),
117
    decomposePoly_(false),
118 119 120 121 122 123
    meshState_(polyMesh::TOPO_CHANGE)
{}


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

124
void Foam::vtk::fvMeshAdaptor::setChannels(const wordList& chanNames)
125
{
126 127
    unsigned chanIds = 0;
    for (const word& chan : chanNames)
128 129 130
    {
        if (channelNames.found(chan))
        {
131
            chanIds |= channelNames[chan];
132 133 134
        }
    }

135
    setChannels(chanIds);
136 137 138
}


139
void Foam::vtk::fvMeshAdaptor::setChannels(enum channelType chanIds)
140 141 142 143 144 145 146 147 148 149 150
{
    channels_ = chanIds;

    if (!usingInternal())
    {
        cachedVtu_.clear();
    }

    if (!usingBoundary())
    {
        cachedVtp_.clear();
151
        patchIds_.clear();
152 153 154 155
    }
}


156
void Foam::vtk::fvMeshAdaptor::setChannels(unsigned chanIds)
157
{
158
    channels_ = (chanIds & 0x3);
159

160
    if (!usingInternal())
161 162 163 164
    {
        cachedVtu_.clear();
    }

165
    if (!usingBoundary())
166 167
    {
        cachedVtp_.clear();
168 169 170 171 172 173 174 175 176 177 178
        patchIds_.clear();
    }
}


void Foam::vtk::fvMeshAdaptor::setDecompose(const bool val)
{
    if (usingInternal() && val != decomposePoly_)
    {
        cachedVtu_.clear();
        decomposePoly_ = val;
179 180 181 182
    }
}


183 184 185 186 187 188
Foam::label Foam::vtk::fvMeshAdaptor::channels() const
{
    return label(channels_);
}


189
bool Foam::vtk::fvMeshAdaptor::usingInternal() const
190
{
191
    return (channels_ & INTERNAL);
192 193 194
}


195
bool Foam::vtk::fvMeshAdaptor::usingBoundary() const
196
{
197
    return (channels_ & BOUNDARY);
198 199 200
}


201
const Foam::labelList& Foam::vtk::fvMeshAdaptor::patchIds() const
202
{
203
    return patchIds_;
204 205 206 207 208 209 210
}


void Foam::vtk::fvMeshAdaptor::updateContent(const wordRes& selectFields)
{
    const bool oldDecomp = decomposePoly_;

211 212 213 214 215 216 217 218
    // General patch information
    // Restrict to non-processor patches.
    // This value is invariant across all processors.

    const polyBoundaryMesh& patches = mesh_.boundaryMesh();
    const label nNonProcessor = patches.nNonProcessor();


219 220 221 222
    // Update cached, saved, unneed values.

    HashSet<string> nowActive;

223 224
    // INTERNAL
    if (usingInternal())
225
    {
226
        nowActive.insert(internalName());
227 228 229
    }


230 231
    // BOUNDARY
    if (usingBoundary())
232
    {
233 234 235 236 237
        for (label patchId=0; patchId < nNonProcessor; ++patchId)
        {
            const polyPatch& pp = patches[patchId];
            nowActive.insert(pp.name());
        }
238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260
    }

    // Dispose of unneeded components
    cachedVtp_.retain(nowActive);
    cachedVtu_.retain(nowActive);

    if
    (
        meshState_ == polyMesh::TOPO_CHANGE
     || meshState_ == polyMesh::TOPO_PATCH_CHANGE
    )
    {
        // Eliminate cached values that would be unreliable
        forAllIters(cachedVtp_, iter)
        {
            iter.object().clearGeom();
            iter.object().clear();
        }
        forAllIters(cachedVtu_, iter)
        {
            iter.object().clearGeom();
            iter.object().clear();
        }
261 262

        definePatchIds();
263 264 265 266 267 268 269 270 271 272 273 274
    }
    else if (oldDecomp != decomposePoly_)
    {
        // poly-decompose changed - dispose of cached values
        forAllIters(cachedVtu_, iter)
        {
            iter.object().clearGeom();
            iter.object().clear();
        }
    }

    convertGeometryInternal();
275 276
    convertGeometryBoundary();

277
    applyGhosting();
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
    convertVolFields(selectFields);
    meshState_ = polyMesh::UNCHANGED;
}


vtkSmartPointer<vtkMultiBlockDataSet>
Foam::vtk::fvMeshAdaptor::output(const wordRes& select)
{
    updateContent(select);

    // All individual datasets are vtkMultiPieceDataSet for improved
    // handling downstream.

    label rank = 0;
    label nproc = 1;

    if (Pstream::parRun())
    {
        rank  = Pstream::myProcNo();
        nproc = Pstream::nProcs();
    }


    auto outputs = vtkSmartPointer<vtkMultiBlockDataSet>::New();

303
    unsigned int blockNo = 0;
304

305 306 307
    // INTERNAL
    if (usingInternal())
    {
308 309
        do
        {
310
            const auto& longName = internalName();
311 312 313
            auto iter = cachedVtu_.find(longName);
            if (!iter.found() || !iter.object().dataset)
            {
314
                Pout<<"Cache miss for VTU " << longName << endl;
315 316 317 318 319 320 321 322 323 324 325 326 327 328 329
                break; // Should never happen
            }

            foamVtuData& vtuData = iter.object();

            auto pieces = vtkSmartPointer<vtkMultiPieceDataSet>::New();

            pieces->SetNumberOfPieces(nproc);
            pieces->SetPiece(rank, vtuData.dataset);

            outputs->SetBlock(blockNo, pieces);

            outputs->GetMetaData(blockNo)->Set
            (
                vtkCompositeDataSet::NAME(),
330
                internalName()
331 332 333 334 335 336
            );

            ++blockNo;
        } while (false);  // do once
    }

337
    // BOUNDARY
338
    if (!patchIds_.empty())
339
    {
340
        unsigned int subBlockNo = 0;
341 342 343 344 345

        auto output = vtkSmartPointer<vtkMultiBlockDataSet>::New();

        const polyBoundaryMesh& patches = mesh_.boundaryMesh();

346
        for (const label patchId : patchIds_)
347 348 349 350 351 352 353
        {
            const polyPatch& pp = patches[patchId];
            const word& longName = pp.name();

            auto iter = cachedVtp_.find(longName);
            if (!iter.found() || !iter.object().dataset)
            {
354
                Pout<<"Cache miss for VTP patch " << longName << endl;
355 356 357 358 359 360 361 362 363 364
                break; // Should never happen
            }

            foamVtpData& vtpData = iter.object();

            auto pieces = vtkSmartPointer<vtkMultiPieceDataSet>::New();

            pieces->SetNumberOfPieces(nproc);
            pieces->SetPiece(rank, vtpData.dataset);

365
            output->SetBlock(subBlockNo, pieces);
366

367
            output->GetMetaData(subBlockNo)->Set
368 369 370 371 372
            (
                vtkCompositeDataSet::NAME(),
                longName
            );

373
            ++subBlockNo;
374 375
        }

376 377 378
        outputs->SetBlock(blockNo, output);

        outputs->GetMetaData(blockNo)->Set
379 380
        (
            vtkCompositeDataSet::NAME(),
381
            boundaryName()
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
        );
    }

    // Would actually like to have this:
    //     outputs->SetName(mesh_.name().c_str());
    // but do that in the caller side

    return outputs;
}


void Foam::vtk::fvMeshAdaptor::updateState(polyMesh::readUpdateState state)
{
    // Only move to worse states
    switch (state)
    {
        case polyMesh::UNCHANGED:
            break;

        case polyMesh::POINTS_MOVED:
            if (meshState_ == polyMesh::UNCHANGED)
            {
                meshState_ = polyMesh::POINTS_MOVED;
            }
            break;

        case polyMesh::TOPO_CHANGE:
        case polyMesh::TOPO_PATCH_CHANGE:
            meshState_ = polyMesh::TOPO_CHANGE;
            break;
    }
}


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