sampledSetsTemplates.C 9.53 KB
Newer Older
1
2
3
4
/*---------------------------------------------------------------------------*\
  =========                 |
  \\      /  F ield         | OpenFOAM: The Open Source CFD Toolbox
   \\    /   O peration     |
5
    \\  /    A nd           | Copyright (C) 2011-2016 OpenFOAM Foundation
6
     \\/     M anipulation  | Copyright (C) 2015-2019 OpenCFD Ltd.
7
8
9
10
-------------------------------------------------------------------------------
License
    This file is part of OpenFOAM.

11
12
13
14
    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.
15
16
17
18
19
20
21

    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
22
    along with OpenFOAM.  If not, see <http://www.gnu.org/licenses/>.
23
24
25
26
27

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

#include "sampledSets.H"
#include "volFields.H"
28
#include "globalIndex.H"
29
30
31

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

Henry's avatar
Henry committed
32
template<class Type>
33
34
35
36
37
38
39
Foam::sampledSets::volFieldSampler<Type>::volFieldSampler
(
    const word& interpolationScheme,
    const GeometricField<Type, fvPatchField, volMesh>& field,
    const PtrList<sampledSet>& samplers
)
:
40
    List<Field<Type>>(samplers.size()),
41
42
    name_(field.name())
{
43
    autoPtr<interpolation<Type>> interpolator
44
    (
45
        interpolation<Type>::New(interpolationScheme, field)
46
47
    );

48
    forAll(samplers, setI)
49
    {
50
51
        Field<Type>& values = this->operator[](setI);
        const sampledSet& samples = samplers[setI];
52
53

        values.setSize(samples.size());
54
        forAll(samples, sampleI)
55
        {
56
            const point& samplePt = samples[sampleI];
57
58
            label celli = samples.cells()[sampleI];
            label facei = samples.faces()[sampleI];
59

60
            if (celli == -1 && facei == -1)
61
62
63
64
65
66
67
68
69
            {
                // Special condition for illegal sampling points
                values[sampleI] = pTraits<Type>::max;
            }
            else
            {
                values[sampleI] = interpolator().interpolate
                (
                    samplePt,
70
71
                    celli,
                    facei
72
73
                );
            }
74
75
76
77
78
        }
    }
}


Henry's avatar
Henry committed
79
template<class Type>
80
81
82
83
84
85
Foam::sampledSets::volFieldSampler<Type>::volFieldSampler
(
    const GeometricField<Type, fvPatchField, volMesh>& field,
    const PtrList<sampledSet>& samplers
)
:
86
    List<Field<Type>>(samplers.size()),
87
88
    name_(field.name())
{
89
    forAll(samplers, setI)
90
    {
91
92
        Field<Type>& values = this->operator[](setI);
        const sampledSet& samples = samplers[setI];
93
94

        values.setSize(samples.size());
95
        forAll(samples, sampleI)
96
        {
97
            label celli = samples.cells()[sampleI];
98

99
            if (celli ==-1)
100
101
102
103
104
            {
                values[sampleI] = pTraits<Type>::max;
            }
            else
            {
105
                values[sampleI] = field[celli];
106
            }
107
108
109
110
111
        }
    }
}


Henry's avatar
Henry committed
112
template<class Type>
113
114
Foam::sampledSets::volFieldSampler<Type>::volFieldSampler
(
115
    const List<Field<Type>>& values,
116
117
118
    const word& name
)
:
119
    List<Field<Type>>(values),
120
121
122
123
124
    name_(name)
{}


template<class Type>
125
Foam::fileName Foam::sampledSets::writeSampleFile
126
127
(
    const coordSet& masterSampleSet,
128
    const PtrList<volFieldSampler<Type>>& masterFields,
129
    const label setI,
130
131
132
133
134
135
136
137
138
139
    const fileName& timeDir,
    const writer<Type>& formatter
)
{
    wordList valueSetNames(masterFields.size());
    List<const Field<Type>*> valueSets(masterFields.size());

    forAll(masterFields, fieldi)
    {
        valueSetNames[fieldi] = masterFields[fieldi].name();
140
        valueSets[fieldi] = &masterFields[fieldi][setI];
141
142
143
144
145
146
147
    }

    fileName fName
    (
        timeDir/formatter.getFileName(masterSampleSet, valueSetNames)
    );

148
149
150
151
152
153
154
155
156
157
    OFstream ofs(fName);
    if (ofs.opened())
    {
        formatter.write
        (
            masterSampleSet,
            valueSetNames,
            valueSets,
            ofs
        );
158
        return fName;
159
160
161
    }
    else
    {
162
163
        WarningInFunction
            << "File " << ofs.name() << " could not be opened. "
164
            << "No data will be written" << endl;
165
        return fileName::null;
166
    }
167
168
169
170
171
172
}


template<class T>
void Foam::sampledSets::combineSampledValues
(
173
    const PtrList<volFieldSampler<T>>& sampledFields,
174
    const labelListList& indexSets,
175
    PtrList<volFieldSampler<T>>& masterFields
176
177
178
179
)
{
    forAll(sampledFields, fieldi)
    {
180
        List<Field<T>> masterValues(indexSets.size());
181

182
        forAll(indexSets, setI)
183
184
        {
            // Collect data from all processors
185
186
187

            Field<T> allData;
            globalIndex::gatherOp(sampledFields[fieldi][setI], allData);
188
189
190

            if (Pstream::master())
            {
191
                masterValues[setI] = UIndirectList<T>
mattijs's avatar
mattijs committed
192
193
                (
                    allData,
194
                    indexSets[setI]
mattijs's avatar
mattijs committed
195
                )();
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
            }
        }

        masterFields.set
        (
            fieldi,
            new volFieldSampler<T>
            (
                masterValues,
                sampledFields[fieldi].name()
            )
        );
    }
}


template<class Type>
213
void Foam::sampledSets::sampleAndWrite(fieldGroup<Type>& fields)
214
215
216
{
    if (fields.size())
    {
217
        const bool interpolate = interpolationScheme_ != "cell";
218
219

        // Create or use existing writer
220
        if (fields.formatter.empty())
221
        {
222
            fields = writeFormat_;
223
224
225
        }

        // Storage for interpolated values
226
        PtrList<volFieldSampler<Type>> sampledFields(fields.size());
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284

        forAll(fields, fieldi)
        {
            if (Pstream::master() && verbose_)
            {
                Pout<< "sampledSets::sampleAndWrite: "
                    << fields[fieldi] << endl;
            }

            if (loadFromFiles_)
            {
                GeometricField<Type, fvPatchField, volMesh> vf
                (
                    IOobject
                    (
                        fields[fieldi],
                        mesh_.time().timeName(),
                        mesh_,
                        IOobject::MUST_READ,
                        IOobject::NO_WRITE,
                        false
                    ),
                    mesh_
                );

                if (interpolate)
                {
                    sampledFields.set
                    (
                        fieldi,
                        new volFieldSampler<Type>
                        (
                            interpolationScheme_,
                            vf,
                            *this
                        )
                    );
                }
                else
                {
                    sampledFields.set
                    (
                        fieldi,
                        new volFieldSampler<Type>(vf, *this)
                    );
                }
            }
            else
            {
                if (interpolate)
                {
                    sampledFields.set
                    (
                        fieldi,
                        new volFieldSampler<Type>
                        (
                            interpolationScheme_,
                            mesh_.lookupObject
285
                            <GeometricField<Type, fvPatchField, volMesh>>
286
287
288
289
290
291
292
293
294
295
296
297
298
                            (fields[fieldi]),
                            *this
                        )
                    );
                }
                else
                {
                    sampledFields.set
                    (
                        fieldi,
                        new volFieldSampler<Type>
                        (
                            mesh_.lookupObject
299
                            <GeometricField<Type, fvPatchField, volMesh>>
300
301
302
303
304
305
306
307
308
309
310
                            (fields[fieldi]),
                            *this
                        )
                    );
                }
            }
        }

        // Combine sampled fields from processors.
        // Note: only master results are valid

311
        PtrList<volFieldSampler<Type>> masterFields(sampledFields.size());
312
313
        combineSampledValues(sampledFields, indexSets_, masterFields);

314
        forAll(masterSampledSets_, setI)
315
        {
316
317
            fileName sampleFile;
            if (Pstream::master())
318
            {
319
                sampleFile = writeSampleFile
320
                (
321
                    masterSampledSets_[setI],
322
                    masterFields,
323
                    setI,
324
325
326
327
                    outputPath_/mesh_.time().timeName(),
                    fields.formatter()
                );
            }
328
329
330
331

            Pstream::scatter(sampleFile);
            if (sampleFile.size())
            {
332
                // Case-local file name with "<case>" to make relocatable
333

334
335
336
                forAll(masterFields, fieldi)
                {
                    dictionary propsDict;
337
338
339
                    propsDict.add
                    (
                        "file",
340
                        time_.relativePath(sampleFile, true)
341
342
                    );

343
344
345
346
                    const word& fieldName = masterFields[fieldi].name();
                    setProperty(fieldName, propsDict);
                }
            }
347
348
349
350
351
352
        }
    }
}


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