Merge remote-tracking branch 'origin/nr/multiSolverFix' into nextRelease
[foam-extend-3.2.git] / src / sampling / sampledSet / sampledSets / sampledSetsTemplates.C
blobbe752468aadf5457d73689213690413e5282c525
1 /*---------------------------------------------------------------------------*\
2   =========                 |
3   \\      /  F ield         | OpenFOAM: The Open Source CFD Toolbox
4    \\    /   O peration     |
5     \\  /    A nd           | Copyright held by original author
6      \\/     M anipulation  |
7 -------------------------------------------------------------------------------
8 License
9     This file is part of OpenFOAM.
11     OpenFOAM is free software; you can redistribute it and/or modify it
12     under the terms of the GNU General Public License as published by the
13     Free Software Foundation; either version 2 of the License, or (at your
14     option) any later version.
16     OpenFOAM is distributed in the hope that it will be useful, but WITHOUT
17     ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
18     FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
19     for more details.
21     You should have received a copy of the GNU General Public License
22     along with OpenFOAM; if not, write to the Free Software Foundation,
23     Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
25 \*---------------------------------------------------------------------------*/
27 #include "sampledSets.H"
28 #include "volFields.H"
29 #include "ListListOps.H"
31 // * * * * * * * * * * * * * Private Member Functions  * * * * * * * * * * * //
33 template <class Type>
34 Foam::sampledSets::volFieldSampler<Type>::volFieldSampler
36     const word& interpolationScheme,
37     const GeometricField<Type, fvPatchField, volMesh>& field,
38     const PtrList<sampledSet>& samplers
41     List<Field<Type> >(samplers.size()),
42     name_(field.name())
44     autoPtr<interpolation<Type> > interpolator
45     (
46         interpolation<Type>::New(interpolationScheme, field)
47     );
49     forAll(samplers, seti)
50     {
51         Field<Type>& values = this->operator[](seti);
52         const sampledSet& samples = samplers[seti];
54         values.setSize(samples.size());
55         forAll(samples, samplei)
56         {
57             const point& samplePt = samples[samplei];
58             label celli = samples.cells()[samplei];
59             label facei = samples.faces()[samplei];
61             values[samplei] = interpolator().interpolate
62             (
63                 samplePt,
64                 celli,
65                 facei
66             );
67         }
68     }
72 template <class Type>
73 Foam::sampledSets::volFieldSampler<Type>::volFieldSampler
75     const GeometricField<Type, fvPatchField, volMesh>& field,
76     const PtrList<sampledSet>& samplers
79     List<Field<Type> >(samplers.size()),
80     name_(field.name())
82     forAll(samplers, seti)
83     {
84         Field<Type>& values = this->operator[](seti);
85         const sampledSet& samples = samplers[seti];
87         values.setSize(samples.size());
88         forAll(samples, samplei)
89         {
90             values[samplei] = field[samples.cells()[samplei]];
91         }
92     }
96 template <class Type>
97 Foam::sampledSets::volFieldSampler<Type>::volFieldSampler
99     const List<Field<Type> >& values,
100     const word& name
103     List<Field<Type> >(values),
104     name_(name)
108 template<class Type>
109 Foam::label Foam::sampledSets::grep
111     fieldGroup<Type>& fieldList,
112     const wordList& fieldTypes
113 ) const
115     fieldList.setSize(fieldNames_.size());
116     label nFields = 0;
118     forAll(fieldNames_, fieldi)
119     {
120         if
121         (
122             fieldTypes[fieldi]
123          == GeometricField<Type, fvPatchField, volMesh>::typeName
124         )
125         {
126             fieldList[nFields] = fieldNames_[fieldi];
127             nFields++;
128         }
129     }
131     fieldList.setSize(nFields);
133     return nFields;
137 template<class Type>
138 void Foam::sampledSets::writeSampleFile
140     const coordSet& masterSampleSet,
141     const PtrList<volFieldSampler<Type> >& masterFields,
142     const label seti,
143     const fileName& timeDir,
144     const writer<Type>& formatter
147     wordList valueSetNames(masterFields.size());
148     List<const Field<Type>*> valueSets(masterFields.size());
150     forAll(masterFields, fieldi)
151     {
152         valueSetNames[fieldi] = masterFields[fieldi].name();
153         valueSets[fieldi] = &masterFields[fieldi][seti];
154     }
156     fileName fName
157     (
158         timeDir/formatter.getFileName(masterSampleSet, valueSetNames)
159     );
161     formatter.write
162     (
163         masterSampleSet,
164         valueSetNames,
165         valueSets,
166         OFstream(fName)()
167     );
171 template<class T>
172 void Foam::sampledSets::combineSampledValues
174     const PtrList<volFieldSampler<T> >& sampledFields,
175     const labelListList& indexSets,
176     PtrList<volFieldSampler<T> >& masterFields
179     forAll(sampledFields, fieldi)
180     {
181         List<Field<T> > masterValues(indexSets.size());
183         forAll(indexSets, seti)
184         {
185             // Collect data from all processors
186             List<Field<T> > gatheredData(Pstream::nProcs());
187             gatheredData[Pstream::myProcNo()] = sampledFields[fieldi][seti];
188             Pstream::gatherList(gatheredData);
190             if (Pstream::master())
191             {
192                 Field<T> allData
193                 (
194                     ListListOps::combine<Field<T> >
195                     (
196                         gatheredData,
197                         Foam::accessOp<Field<T> >()
198                     )
199                 );
201                 masterValues[seti] = UIndirectList<T>
202                 (
203                     allData,
204                     indexSets[seti]
205                 )();
206             }
207         }
209         masterFields.set
210         (
211             fieldi,
212             new volFieldSampler<T>
213             (
214                 masterValues,
215                 sampledFields[fieldi].name()
216             )
217         );
218     }
222 template<class Type>
223 void Foam::sampledSets::sampleAndWrite
225     fieldGroup<Type>& fields
228     if (fields.size())
229     {
230         bool interpolate = interpolationScheme_ != "cell";
232         // Create or use existing writer
233         if (fields.formatter.empty())
234         {
235             fields.formatter = writer<Type>::New(writeFormat_);
236         }
238         // Storage for interpolated values
239         PtrList<volFieldSampler<Type> > sampledFields(fields.size());
241         forAll(fields, fieldi)
242         {
243             if (Pstream::master() && verbose_)
244             {
245                 Pout<< "sampledSets::sampleAndWrite: "
246                     << fields[fieldi] << endl;
247             }
249             if (loadFromFiles_)
250             {
251                 GeometricField<Type, fvPatchField, volMesh> vf
252                 (
253                     IOobject
254                     (
255                         fields[fieldi],
256                         mesh_.time().timeName(),
257                         mesh_,
258                         IOobject::MUST_READ,
259                         IOobject::NO_WRITE,
260                         false
261                     ),
262                     mesh_
263                 );
265                 if (interpolate)
266                 {
267                     sampledFields.set
268                     (
269                         fieldi,
270                         new volFieldSampler<Type>
271                         (
272                             interpolationScheme_,
273                             vf,
274                             *this
275                         )
276                     );
277                 }
278                 else
279                 {
280                     sampledFields.set
281                     (
282                         fieldi,
283                         new volFieldSampler<Type>(vf, *this)
284                     );
285                 }
286             }
287             else
288             {
289                 if (interpolate)
290                 {
291                     sampledFields.set
292                     (
293                         fieldi,
294                         new volFieldSampler<Type>
295                         (
296                             interpolationScheme_,
297                             mesh_.lookupObject
298                             <GeometricField<Type, fvPatchField, volMesh> >
299                             (fields[fieldi]),
300                             *this
301                         )
302                     );
303                 }
304                 else
305                 {
306                     sampledFields.set
307                     (
308                         fieldi,
309                         new volFieldSampler<Type>
310                         (
311                             mesh_.lookupObject
312                             <GeometricField<Type, fvPatchField, volMesh> >
313                             (fields[fieldi]),
314                             *this
315                         )
316                     );
317                 }
318             }
319         }
321         // Combine sampled fields from processors.
322         // Note: only master results are valid
324         PtrList<volFieldSampler<Type> > masterFields(sampledFields.size());
325         combineSampledValues(sampledFields, indexSets_, masterFields);
327         if (Pstream::master())
328         {
329             forAll(masterSampledSets_, seti)
330             {
331                 writeSampleFile
332                 (
333                     masterSampledSets_[seti],
334                     masterFields,
335                     seti,
336                     outputPath_/mesh_.time().timeName(),
337                     fields.formatter()
338                 );
339             }
340         }
341     }
345 // ************************************************************************* //