catalystFaMesh.C 6.18 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
28
29
30
31
32
33
34
35
36
37
38
/*---------------------------------------------------------------------------*\
  =========                 |
  \\      /  F ield         | OpenFOAM: The Open Source CFD Toolbox
   \\    /   O peration     |
    \\  /    A nd           | Copyright (C) 2018 OpenCFD Ltd.
     \\/     M anipulation  |
-------------------------------------------------------------------------------
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 "catalystFaMesh.H"
#include "addToRunTimeSelectionTable.H"
#include "faMesh.H"
#include "fvMesh.H"

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

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

namespace Foam
{
39
namespace catalyst
40
{
41
42
43
44
45
46
47
48
    defineTypeNameAndDebug(faMeshInput, 0);
    addNamedToRunTimeSelectionTable
    (
        catalystInput,
        faMeshInput,
        dictionary,
        area
    );
49
}
50
} // End namespace Foam
51
52
53
54


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

55
void Foam::catalyst::faMeshInput::update()
56
{
57
58
59
60
61
    // Backend requires a corresponding mesh
    backends_.filterKeys
    (
        [this](const word& k){ return meshes_.found(k); }
    );
62

63
    forAllConstIters(meshes_, iter)
64
    {
65
66
67
        const word& areaName(iter.key());

        if (!backends_.found(areaName))
68
        {
69
70
71
72
73
74
75
            auto backend =
                autoPtr<Foam::vtk::faMeshAdaptor>::New(*(iter.object()));

            // Apply any configuration options
            // ...

            backends_.set(areaName, backend);
76
77
78
79
80
81
82
        }
    }
}


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

83
Foam::catalyst::faMeshInput::faMeshInput
84
85
86
87
88
89
(
    const word& name,
    const Time& runTime,
    const dictionary& dict
)
:
90
91
92
    catalystInput(name),
    time_(runTime),
    regionName_(),
93
94
95
    selectAreas_(),
    selectFields_(),
    meshes_(),
96
    backends_()
97
98
99
100
101
102
103
{
    read(dict);
}


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

104
bool Foam::catalyst::faMeshInput::read(const dictionary& dict)
105
{
106
    catalystInput::read(dict);
107

108
109
110
    selectAreas_.clear();
    selectFields_.clear();
    backends_.clear();
111

112
113
114
115
116
117
118
119
    regionName_ =
        dict.lookupOrDefault<word>("region", polyMesh::defaultRegion);

    const objectRegistry& obr =
        time_.lookupObject<objectRegistry>(regionName_);

    // All possible meshes for the given region
    meshes_ = obr.lookupClass<faMesh>();
120
121
122
123
124
125
126
127

    dict.readIfPresent("areas", selectAreas_);

    if (selectAreas_.empty())
    {
        word areaName;
        if (!dict.readIfPresent("area", areaName))
        {
128
            wordList available = obr.sortedNames<faMesh>();
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143

            if (available.size())
            {
                areaName = available.first();
            }
        }

        if (!areaName.empty())
        {
            selectAreas_.resize(1);
            selectAreas_.first() = areaName;
        }
    }

    // Restrict to specified meshes
144
    meshes_.filterKeys(selectAreas_);
145

146
    dict.readEntry("fields", selectFields_);
147
148
149
150
151

    return true;
}


152
void Foam::catalyst::faMeshInput::update(polyMesh::readUpdateState state)
153
{
154
    // Trigger change of state
155

156
157
158
159
    const objectRegistry& obr =
        time_.lookupObject<objectRegistry>(regionName_);

    // Be really paranoid and verify if the mesh actually exists
160
    const wordList areaNames(backends_.toc());
161

162
    for (const word& areaName : areaNames)
163
    {
164
        if (meshes_.found(areaName) && obr.found(areaName))
165
        {
166
            backends_[areaName]->updateState(state);
167
        }
168
        else
169
        {
170
171
            backends_.erase(areaName);
            meshes_.erase(areaName);
172
173
        }
    }
174
}
175
176


177
178
179
180
181
182
183
184
Foam::label Foam::catalyst::faMeshInput::addChannels(dataQuery& dataq)
{
    update();   // Enforce sanity for backends and adaptor

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

186
    // Gather all fields that we know how to convert
187
188
189
190
191
192
    wordHashSet allFields;
    forAllConstIters(backends_, iter)
    {
        allFields += iter.object()->knownFields(selectFields_);
    }

193
    dataq.set(name(), allFields);
194

195
196
    return 1;
}
197
198


199
200
201
202
203
204
bool Foam::catalyst::faMeshInput::convert
(
    dataQuery& dataq,
    outputChannels& outputs
)
{
205
206
    const word channelName(name());
    const wordList areaNames(backends_.sortedToc());
207

208
    if (areaNames.empty() || !dataq.found(channelName))
209
    {
210
211
        // Not available, or not requested
        return false;
212
213
    }

214

215
216
    // A separate block for each area mesh
    unsigned int blockNo = 0;
217

218
    for (const word& areaName : areaNames)
219
    {
220
        auto dataset = backends_[areaName]->output(selectFields_);
221

222
223
224
225
226
227
228
        // Existing or new
        vtkSmartPointer<vtkMultiBlockDataSet> block =
            outputs.lookup
            (
                channelName,
                vtkSmartPointer<vtkMultiBlockDataSet>::New()
            );
229

230
        block->SetBlock(blockNo, dataset);
231

232
233
234
235
236
        block->GetMetaData(blockNo)->Set
        (
            vtkCompositeDataSet::NAME(),
            areaName                        // block name = area mesh name
        );
237

238
        outputs.set(channelName, block);    // overwrites existing
239

240
        ++blockNo;
241
242
243
244
245
246
    }

    return true;
}


247
Foam::Ostream& Foam::catalyst::faMeshInput::print(Ostream& os) const
248
{
249
250
251
252
    os  << name() << nl
        <<"    areas   " << flatOutput(selectAreas_) << nl
        <<"    meshes  " << flatOutput(meshes_.sortedToc()) << nl
        <<"    fields  " << flatOutput(selectFields_) << nl;
253

254
    return os;
255
256
257
258
}


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