catalystTools.C 6.79 KB
Newer Older
1
2
3
4
/*---------------------------------------------------------------------------*\
  =========                 |
  \\      /  F ield         | OpenFOAM: The Open Source CFD Toolbox
   \\    /   O peration     |
5
    \\  /    A nd           | Copyright (C) 2018-2019 OpenCFD Ltd.
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
     \\/     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 "catalystTools.H"
#include "Time.H"

28
#include <vtkPVConfig.h>
29
30
31
#include <vtkCPDataDescription.h>
#include <vtkCPInputDataDescription.h>
#include <vtkCPProcessor.h>
32
#include <vtkDataObject.h>
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
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

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

const Foam::wordHashSet Foam::catalyst::dataQuery::emptyWordHash_;

// * * * * * * * * * * * * * * * Global Functions  * * * * * * * * * * * * * //

void Foam::catalyst::printInfo(Ostream& os, vtkCPDataDescription* descrip)
{
    if (!descrip)
    {
        return;
    }

    const unsigned nItems = descrip->GetNumberOfInputDescriptions();

    for (unsigned itemi = 0; itemi < nItems; ++itemi)
    {
        vtkCPInputDataDescription* input = descrip->GetInputDescription(itemi);
        if (!input) continue;  // should not happen

        os <<"input: " << descrip->GetInputDescriptionName(itemi) << nl;

        const unsigned nFields = input->GetNumberOfFields();
        for (unsigned fieldi = 0; fieldi < nFields; ++fieldi)
        {
            os << "    field: " << input->GetFieldName(fieldi) << nl;
        }
        if (!nFields) os <<"     no fields requested" << nl;
    }
}


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

Foam::catalyst::timeQuery::timeQuery(const Foam::Time& when)
:
    timeValue(when.timeOutputValue()),
    timeIndex(when.timeIndex()),
    forced(when.timeOutputValue() >= when.endTime().value())
{}


Foam::catalyst::dataQuery::dataQuery(const Foam::Time& when)
:
    timeQuery(when),
    names_(),
    fields_(),
    active_(),
    descrip_(vtkCPDataDescription::New())
{}


// * * * * * * * * * * * * * * * * Destructor  * * * * * * * * * * * * * * * //

Foam::catalyst::dataQuery::~dataQuery()
{
    descrip_->Delete();
}


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

void Foam::catalyst::dataQuery::set
(
98
    const word& channel,
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
    const wordHashSet& fields
)
{
    auto iter = fields_.find(channel);

    if (iter.found())
    {
        *iter = fields;
    }
    else
    {
        active_.insert(names_.size());
        fields_.insert(channel, fields);
        names_.append(channel);
    }
}


Foam::label Foam::catalyst::dataQuery::query(vtkCPProcessor* coproc)
{
    dataQuery& dataq = *this;

    if (!coproc)
    {
        Info<< "No ParaView Catalyst initialized" << endl;
        dataq.clear();
        return dataq.size();
    }

    if (dataq.empty())
    {
        // No channels names have been published by the simulation
        return dataq.size();
    }

    vtkCPDataDescription* descrip = dataq.description();

    descrip->SetTimeData(dataq.timeValue, dataq.timeIndex);
    descrip->SetForceOutput(dataq.forced);

139
    const wordList inputNames(dataq.names());
140
141
142
143

    // Sort out which channels already exist, are new, or disappeared

    // The currently defined channels
144
    wordHashSet currChannels;
145
146
147
148
149
150

    const unsigned n = descrip->GetNumberOfInputDescriptions();
    for (unsigned i=0; i < n; ++i)
    {
        currChannels.insert
        (
151
            word::validate(descrip->GetInputDescriptionName(i))
152
153
154
        );
    }

155
    wordHashSet oldChannels(currChannels);
156
157
158
159
160
161
162
163
164
165
166
167
168
169
    oldChannels.erase(inputNames);

    if (oldChannels.size())
    {
        // Some channels disappear - remove and redo everything
        descrip->ResetAll();
        currChannels.clear();
    }

    // Add channels.

    // Note: this misses updating field information for previously
    // existing inputs.

170
    for (const word& channel : inputNames)
171
172
173
174
175
176
177
    {
        if (currChannels.found(channel))
        {
            continue;
        }

        descrip->AddInput(channel.c_str());
178
        auto* input = descrip->GetInputDescriptionByName(channel.c_str());
179

180
        for (const word& fldName : dataq.fields(channel))
181
        {
182
183
184
185
186
187
188
189
            #if (PARAVIEW_VERSION_MAJOR == 5) && \
                (PARAVIEW_VERSION_MINOR < 6)
            input->AddPointField(fldName.c_str());
            input->AddCellField(fldName.c_str());
            #else
            input->AddField(fldName.c_str(), vtkDataObject::CELL);
            input->AddField(fldName.c_str(), vtkDataObject::POINT);
            #endif
190
191
192
193
194
195
196
197
198
199
200
201
202
        }
    }

    if
    (
        !coproc->RequestDataDescription(descrip)
     || !descrip->GetIfAnyGridNecessary()
    )
    {
        dataq.clear();
        return dataq.size();
    }

203
    for (const word& channel : inputNames)
204
205
206
207
208
209
210
    {
        auto* input = descrip->GetInputDescriptionByName(channel.c_str());

        if (input && input->GetIfGridIsNecessary())
        {
            wordHashSet requestedFields;

211
            for (const word& fldName : dataq.fields(channel))
212
            {
213
214
215
                #if (PARAVIEW_VERSION_MAJOR == 5) && \
                    (PARAVIEW_VERSION_MINOR < 6)
                if (input->IsFieldNeeded(fldName.c_str()))
216
                {
217
                    requestedFields.insert(fldName);
218
                }
219
220
221
222
223
224
225
226
227
228
                #else
                if
                (
                    input->IsFieldNeeded(fldName.c_str(), vtkDataObject::CELL)
                 || input->IsFieldNeeded(fldName.c_str(), vtkDataObject::POINT)
                )
                {
                    requestedFields.insert(fldName);
                }
                #endif
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
            }

            dataq.set(channel, requestedFields);
        }
        else
        {
            dataq.clear(channel);
        }
    }

    return dataq.size();
}


// * * * * * * * * * * * * * * * Ostream Operator  * * * * * * * * * * * * * //

Foam::Ostream& Foam::operator<<(Ostream& os, const catalyst::timeQuery& when)
{
    os << "Time = " << when.timeValue << ", index: " << when.timeIndex;
    return os;
}


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