BUG: UListIO: byteSize overflowing on really big faceLists
[OpenFOAM-2.0.x.git] / src / sampling / meshToMeshInterpolation / meshToMesh / meshToMeshInterpolate.C
blob991ae31826625a416690570860cda1526e9a737f
1 /*---------------------------------------------------------------------------*\
2   =========                 |
3   \\      /  F ield         | OpenFOAM: The Open Source CFD Toolbox
4    \\    /   O peration     |
5     \\  /    A nd           | Copyright (C) 2011 OpenFOAM Foundation
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
13     the Free Software Foundation, either version 3 of the License, or
14     (at your 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, see <http://www.gnu.org/licenses/>.
24 \*---------------------------------------------------------------------------*/
26 #include "meshToMesh.H"
27 #include "volFields.H"
28 #include "interpolationCellPoint.H"
29 #include "SubField.H"
30 #include "mixedFvPatchField.H"
32 // * * * * * * * * * * * * * * * Member Functions  * * * * * * * * * * * * * //
34 template<class Type>
35 void Foam::meshToMesh::mapField
37     Field<Type>& toF,
38     const Field<Type>& fromVf,
39     const labelList& adr
40 ) const
42     // Direct mapping of nearest-cell values
44     forAll(toF, celli)
45     {
46         if (adr[celli] != -1)
47         {
48             toF[celli] = fromVf[adr[celli]];
49         }
50     }
52     //toF.map(fromVf, adr);
56 template<class Type>
57 void Foam::meshToMesh::interpolateField
59     Field<Type>& toF,
60     const GeometricField<Type, fvPatchField, volMesh>& fromVf,
61     const labelList& adr,
62     const scalarListList& weights
63 ) const
65     // Inverse distance weighted interpolation
67     // get reference to cellCells
68     const labelListList& cc = fromMesh_.cellCells();
70     forAll(toF, celli)
71     {
72         if (adr[celli] != -1)
73         {
74             const labelList& neighbours = cc[adr[celli]];
75             const scalarList& w = weights[celli];
77             toF[celli] = fromVf[adr[celli]]*w[0];
79             for (label ni = 1; ni < w.size(); ni++)
80             {
81                 toF[celli] += fromVf[neighbours[ni - 1]]*w[ni];
82             }
83         }
84     }
88 template<class Type>
89 void Foam::meshToMesh::interpolateField
91     Field<Type>& toF,
92     const GeometricField<Type, fvPatchField, volMesh>& fromVf,
93     const labelList& adr,
94     const vectorField& centres
95 ) const
97     // Cell-Point interpolation
98     interpolationCellPoint<Type> interpolator(fromVf);
100     forAll(toF, celli)
101     {
102         if (adr[celli] != -1)
103         {
104             toF[celli] = interpolator.interpolate
105             (
106                 centres[celli],
107                 adr[celli]
108             );
109         }
110     }
114 template<class Type>
115 void Foam::meshToMesh::interpolateInternalField
117     Field<Type>& toF,
118     const GeometricField<Type, fvPatchField, volMesh>& fromVf,
119     meshToMesh::order ord
120 ) const
122     if (fromVf.mesh() != fromMesh_)
123     {
124         FatalErrorIn
125         (
126             "meshToMesh::interpolateInternalField(Field<Type>& toF, "
127             "const GeometricField<Type, fvPatchField, volMesh>& fromVf, "
128             "meshToMesh::order ord) const"
129         )   << "the argument field does not correspond to the right mesh. "
130             << "Field size: " << fromVf.size()
131             << " mesh size: " << fromMesh_.nCells()
132             << exit(FatalError);
133     }
135     if (toF.size() != toMesh_.nCells())
136     {
137         FatalErrorIn
138         (
139             "meshToMesh::interpolateInternalField(Field<Type>& toF, "
140             "const GeometricField<Type, fvPatchField, volMesh>& fromVf, "
141             "meshToMesh::order ord) const"
142         )   << "the argument field does not correspond to the right mesh. "
143             << "Field size: " << toF.size()
144             << " mesh size: " << toMesh_.nCells()
145             << exit(FatalError);
146     }
148     switch(ord)
149     {
150         case MAP:
151             mapField(toF, fromVf, cellAddressing_);
152         break;
154         case INTERPOLATE:
155             interpolateField
156             (
157                 toF,
158                 fromVf,
159                 cellAddressing_,
160                 inverseDistanceWeights()
161             );
162         break;
164         case CELL_POINT_INTERPOLATE:
165             interpolateField
166             (
167                 toF,
168                 fromVf,
169                 cellAddressing_,
170                 toMesh_.cellCentres()
171             );
172         break;
174         default:
175             FatalErrorIn
176             (
177                 "meshToMesh::interpolateInternalField(Field<Type>& toF, "
178                 "const GeometricField<Type, fvPatchField, volMesh>& fromVf, "
179                 "meshToMesh::order ord) const"
180             )   << "unknown interpolation scheme " << ord
181                 << exit(FatalError);
182     }
186 template<class Type>
187 void Foam::meshToMesh::interpolateInternalField
189     Field<Type>& toF,
190     const tmp<GeometricField<Type, fvPatchField, volMesh> >& tfromVf,
191     meshToMesh::order ord
192 ) const
194     interpolateInternalField(toF, tfromVf(), ord);
195     tfromVf.clear();
199 template<class Type>
200 void Foam::meshToMesh::interpolate
202     GeometricField<Type, fvPatchField, volMesh>& toVf,
203     const GeometricField<Type, fvPatchField, volMesh>& fromVf,
204     meshToMesh::order ord
205 ) const
207     interpolateInternalField(toVf, fromVf, ord);
209     forAll(toMesh_.boundaryMesh(), patchi)
210     {
211         const fvPatch& toPatch = toMesh_.boundary()[patchi];
213         if (cuttingPatches_.found(toPatch.name()))
214         {
215             switch(ord)
216             {
217                 case MAP:
218                     mapField
219                     (
220                         toVf.boundaryField()[patchi],
221                         fromVf,
222                         boundaryAddressing_[patchi]
223                     );
224                 break;
226                 case INTERPOLATE:
227                     interpolateField
228                     (
229                         toVf.boundaryField()[patchi],
230                         fromVf,
231                         boundaryAddressing_[patchi],
232                         toPatch.Cf()
233                     );
234                 break;
236                 case CELL_POINT_INTERPOLATE:
237                     interpolateField
238                     (
239                         toVf.boundaryField()[patchi],
240                         fromVf,
241                         boundaryAddressing_[patchi],
242                         toPatch.Cf()
243                     );
244                 break;
246                 default:
247                     FatalErrorIn
248                     (
249                         "meshToMesh::interpolate("
250                         "GeometricField<Type, fvPatchField, volMesh>& toVf, "
251                         "const GeometricField<Type, fvPatchField, volMesh>& "
252                         "fromVf, meshToMesh::order ord) const"
253                     )   << "unknown interpolation scheme " << ord
254                         << exit(FatalError);
255             }
257             if (isA<mixedFvPatchField<Type> >(toVf.boundaryField()[patchi]))
258             {
259                 refCast<mixedFvPatchField<Type> >
260                 (
261                     toVf.boundaryField()[patchi]
262                 ).refValue() = toVf.boundaryField()[patchi];
263             }
264         }
265         else if
266         (
267             patchMap_.found(toPatch.name())
268          && fromMeshPatches_.found(patchMap_.find(toPatch.name())())
269         )
270         {
271             /*
272             toVf.boundaryField()[patchi].map
273             (
274                 fromVf.boundaryField()
275                 [
276                     fromMeshPatches_.find(patchMap_.find(toPatch.name())())()
277                 ],
278                 boundaryAddressing_[patchi]
279             );
280             */
282             mapField
283             (
284                 toVf.boundaryField()[patchi],
285                 fromVf.boundaryField()
286                 [
287                     fromMeshPatches_.find(patchMap_.find(toPatch.name())())()
288                 ],
289                 boundaryAddressing_[patchi]
290             );
291         }
292     }
296 template<class Type>
297 void Foam::meshToMesh::interpolate
299     GeometricField<Type, fvPatchField, volMesh>& toVf,
300     const tmp<GeometricField<Type, fvPatchField, volMesh> >& tfromVf,
301     meshToMesh::order ord
302 ) const
304     interpolate(toVf, tfromVf(), ord);
305     tfromVf.clear();
309 template<class Type>
310 Foam::tmp< Foam::GeometricField<Type, Foam::fvPatchField, Foam::volMesh> >
311 Foam::meshToMesh::interpolate
313     const GeometricField<Type, fvPatchField, volMesh>& fromVf,
314     meshToMesh::order ord
315 ) const
317     // Create and map the internal-field values
318     Field<Type> internalField(toMesh_.nCells());
319     interpolateInternalField(internalField, fromVf, ord);
321     // check whether both meshes have got the same number
322     // of boundary patches
323     if (fromMesh_.boundary().size() != toMesh_.boundary().size())
324     {
325         FatalErrorIn
326         (
327             "meshToMesh::interpolate"
328             "(const GeometricField<Type, fvPatchField, volMesh>& fromVf,"
329             "meshToMesh::order ord) const"
330         )   << "Incompatible meshes: different number of boundaries, "
331                "only internal field may be interpolated"
332             << exit(FatalError);
333     }
335     // Create and map the patch field values
336     PtrList<fvPatchField<Type> > patchFields
337     (
338         boundaryAddressing_.size()
339     );
341     forAll(boundaryAddressing_, patchI)
342     {
343         patchFields.set
344         (
345             patchI,
346             fvPatchField<Type>::New
347             (
348                 fromVf.boundaryField()[patchI],
349                 toMesh_.boundary()[patchI],
350                 DimensionedField<Type, volMesh>::null(),
351                 patchFieldInterpolator
352                 (
353                     boundaryAddressing_[patchI]
354                 )
355             )
356         );
357     }
360     // Create the complete field from the pieces
361     tmp<GeometricField<Type, fvPatchField, volMesh> > ttoF
362     (
363         new GeometricField<Type, fvPatchField, volMesh>
364         (
365             IOobject
366             (
367                 "interpolated(" + fromVf.name() + ')',
368                 toMesh_.time().timeName(),
369                 toMesh_,
370                 IOobject::NO_READ,
371                 IOobject::NO_WRITE
372             ),
373             toMesh_,
374             fromVf.dimensions(),
375             internalField,
376             patchFields
377         )
378     );
380     return ttoF;
384 template<class Type>
385 Foam::tmp< Foam::GeometricField<Type, Foam::fvPatchField, Foam::volMesh> >
386 Foam::meshToMesh::interpolate
388     const tmp<GeometricField<Type, fvPatchField, volMesh> >& tfromVf,
389     meshToMesh::order ord
390 ) const
392     tmp<GeometricField<Type, fvPatchField, volMesh> > tint =
393         interpolate(tfromVf(), ord);
394     tfromVf.clear();
396     return tint;
400 // ************************************************************************* //