catalystFvMesh.C 8.59 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27
/*---------------------------------------------------------------------------*\
  =========                 |
  \\      /  F ield         | OpenFOAM: The Open Source CFD Toolbox
   \\    /   O peration     |
    \\  /    A nd           | Copyright (C) 2018 OpenCFD Ltd.
     \\/     M anipulation  | Copyright (C) 2018 CINECA
-------------------------------------------------------------------------------
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 "catalystFvMesh.H"
#include "addToRunTimeSelectionTable.H"
28
#include "fileNameList.H"
29 30 31 32 33 34 35 36 37

#include <vtkCPDataDescription.h>
#include <vtkMultiBlockDataSet.h>
#include <vtkInformation.h>

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

namespace Foam
{
38
namespace catalyst
39
{
40 41 42 43 44 45 46 47
    defineTypeNameAndDebug(fvMeshInput, 0);
    addNamedToRunTimeSelectionTable
    (
        catalystInput,
        fvMeshInput,
        dictionary,
        default
    );
48 49 50 51 52 53
}
}


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

54
Foam::fileName Foam::catalyst::fvMeshInput::channelName(channelEnum chan) const
55
{
56
    if (chan == channelEnum::INPUT)
57
    {
58
        return name();
59 60
    }

61
    return name()/vtk::fvMeshAdaptor::channelNames[chan];
62 63 64
}


65
void Foam::catalyst::fvMeshInput::update()
66
{
67 68 69 70 71
    // Backend requires a corresponding mesh
    backends_.filterKeys
    (
        [this](const word& k){ return meshes_.found(k); }
    );
72

73
    forAllConstIters(meshes_, iter)
74
    {
75
        if (!backends_.found(iter.key()))
76
        {
77
            backends_.set
78 79 80 81
            (
                iter.key(),
                new Foam::vtk::fvMeshAdaptor(*(iter.object()), decompose_)
            );
82 83 84 85 86 87 88
        }
    }
}


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

89
Foam::catalyst::fvMeshInput::fvMeshInput
90 91 92 93 94 95
(
    const word& name,
    const Time& runTime,
    const dictionary& dict
)
:
96
    catalystInput(name),
97 98 99
    time_(runTime),
    selectRegions_(),
    selectFields_(),
100
    decompose_(false),
101
    meshes_(),
102
    backends_()
103 104 105 106 107 108 109
{
    read(dict);
}


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

110
bool Foam::catalyst::fvMeshInput::read(const dictionary& dict)
111
{
112 113 114 115 116 117
    catalystInput::read(dict);

    backends_.clear();
    selectFields_.clear();
    selectRegions_.clear();
    decompose_ = dict.lookupOrDefault("decompose", false);
118

119 120 121 122 123 124 125 126 127 128 129 130 131
    // All possible meshes
    meshes_ = time_.lookupClass<fvMesh>();

    dict.readIfPresent("regions", selectRegions_);

    if (selectRegions_.empty())
    {
        selectRegions_.resize(1);
        selectRegions_.first() =
            dict.lookupOrDefault<word>("region", polyMesh::defaultRegion);
    }

    // Restrict to specified meshes
132
    meshes_.filterKeys(selectRegions_);
133 134 135 136 137 138 139

    dict.lookup("fields") >> selectFields_;

    return true;
}


140
void Foam::catalyst::fvMeshInput::update(polyMesh::readUpdateState state)
141
{
142
    // Trigger change of state
143

144 145
    // Be really paranoid and verify if the mesh actually exists
    const wordList regionNames(backends_.toc());
146

147
    for (const word& regionName : regionNames)
148
    {
149
        if (meshes_.found(regionName) && time_.found(regionName))
150
        {
151
            backends_[regionName]->updateState(state);
152
        }
153
        else
154
        {
155 156
            backends_.erase(regionName);
            meshes_.erase(regionName);
157 158
        }
    }
159
}
160 161


162 163 164 165 166 167 168 169
Foam::label Foam::catalyst::fvMeshInput::addChannels(dataQuery& dataq)
{
    update();   // Enforce sanity for backends and adaptor

    if (backends_.empty())
    {
        return 0;
    }
170

171
    // Gather all fields that we know how to convert
172 173 174 175 176 177 178
    wordHashSet allFields;
    forAllConstIters(backends_, iter)
    {
        allFields += iter.object()->knownFields(selectFields_);
    }


179
    // This solution may be a temporary measure...
180

181 182 183
    dataq.set(name(), allFields);  // channel::INPUT
    dataq.set(channelName(channelEnum::MESH),    allFields);
    dataq.set(channelName(channelEnum::PATCHES), allFields);
184

185 186
    return 3;
}
187

188 189 190 191 192 193 194 195 196 197

bool Foam::catalyst::fvMeshInput::convert
(
    dataQuery& dataq,
    outputChannels& outputs
)
{
    const wordList regionNames(backends_.sortedToc());

    if (regionNames.empty())
198
    {
199
        return false;  // skip - not available
200
    }
201

202 203 204 205 206 207 208 209
    // Multi-channel

    // This solution may be a temporary measure...

    unsigned whichChannels = channelEnum::NONE;

    // channel::INPUT
    if (dataq.found(name()))
210
    {
211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228
        whichChannels |= channelEnum::INPUT;
    }

    // channel::MESH
    if (dataq.found(channelName(channelEnum::MESH)))
    {
        whichChannels |= channelEnum::MESH;
    }

    // channel::PATCHES
    if (dataq.found(channelName(channelEnum::PATCHES)))
    {
        whichChannels |= channelEnum::PATCHES;
    }

    if (channelEnum::NONE == whichChannels)
    {
        return false;  // skip - not requested
229 230 231 232 233
    }


    // TODO: currently don't rely on the results from expecting much at all

234 235
    // Each region goes into a separate block.
    unsigned int blockNo = 0;
236 237
    for (const word& regionName : regionNames)
    {
238 239
        // define/redefine output channels
        backends_[regionName]->channels(whichChannels);
240

241 242
        vtkSmartPointer<vtkMultiBlockDataSet> dataset =
            backends_[regionName]->output(selectFields_);
243

244
        // MESH = block 0
245
        {
246
            const unsigned int channelNo = 0;
247

248
            const fileName channel(channelName(channelEnum::MESH));
249

250
            if (dataq.found(channel))
251 252 253 254 255
            {
                // Get existing or new
                vtkSmartPointer<vtkMultiBlockDataSet> block =
                    outputs.lookup
                    (
256
                        channel,
257 258 259
                        vtkSmartPointer<vtkMultiBlockDataSet>::New()
                    );

260
                block->SetBlock(blockNo, dataset->GetBlock(channelNo));
261

262
                block->GetMetaData(blockNo)->Set
263 264 265 266 267
                (
                    vtkCompositeDataSet::NAME(),
                    regionName
                );

268
                outputs.set(channel, block); // overwrites existing
269 270 271
            }
        }

272
        // PATCHES = block 1
273
        {
274
            const unsigned int channelNo = 1;
275

276
            const fileName channel(channelName(channelEnum::PATCHES));
277

278
            if (dataq.found(channel))
279 280 281 282 283
            {
                // Get existing or new
                vtkSmartPointer<vtkMultiBlockDataSet> block =
                    outputs.lookup
                    (
284
                        channel,
285 286 287
                        vtkSmartPointer<vtkMultiBlockDataSet>::New()
                    );

288
                block->SetBlock(blockNo, dataset->GetBlock(channelNo));
289

290
                block->GetMetaData(blockNo)->Set
291 292 293 294 295
                (
                    vtkCompositeDataSet::NAME(),
                    regionName
                );

296
                outputs.set(channel, block);  // overwrites existing
297 298 299
            }
        }

300
        // INPUT
301
        {
302
            const fileName channel = name();
303

304
            if (dataq.found(channel))
305 306 307 308 309
            {
                // Get existing or new
                vtkSmartPointer<vtkMultiBlockDataSet> block =
                    outputs.lookup
                    (
310
                        channel,
311 312 313
                        vtkSmartPointer<vtkMultiBlockDataSet>::New()
                    );

314
                block->SetBlock(blockNo, dataset);
315

316
                block->GetMetaData(blockNo)->Set
317 318 319 320 321
                (
                    vtkCompositeDataSet::NAME(),
                    regionName
                );

322
                outputs.set(channel, block);  // overwrites existing
323 324 325
            }
        }

326
        ++blockNo;
327 328 329 330 331 332
    }

    return true;
}


333
Foam::Ostream& Foam::catalyst::fvMeshInput::print(Ostream& os) const
334
{
335 336 337 338
    os  << name() << nl
        <<"    regions " << flatOutput(selectRegions_) << nl
        <<"    meshes  " << flatOutput(meshes_.sortedToc()) << nl
        <<"    fields  " << flatOutput(selectFields_) << nl;
339

340
    return os;
341 342 343 344
}


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