OpenFOAM logo
The Open Source CFD Toolbox
  Source Guide OpenCFD Solutions Contact OpenFOAM

meshToMeshInterpolate.C

Go to the documentation of this file.
00001 /*---------------------------------------------------------------------------*\
00002   =========                 |
00003   \\      /  F ield         | OpenFOAM: The Open Source CFD Toolbox
00004    \\    /   O peration     |
00005     \\  /    A nd           | Copyright (C) 1991-2009 OpenCFD Ltd.
00006      \\/     M anipulation  |
00007 -------------------------------------------------------------------------------
00008 License
00009     This file is part of OpenFOAM.
00010 
00011     OpenFOAM is free software; you can redistribute it and/or modify it
00012     under the terms of the GNU General Public License as published by the
00013     Free Software Foundation; either version 2 of the License, or (at your
00014     option) any later version.
00015 
00016     OpenFOAM is distributed in the hope that it will be useful, but WITHOUT
00017     ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
00018     FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
00019     for more details.
00020 
00021     You should have received a copy of the GNU General Public License
00022     along with OpenFOAM; if not, write to the Free Software Foundation,
00023     Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
00024 
00025 \*---------------------------------------------------------------------------*/
00026 
00027 #include "meshToMesh.H"
00028 #include "volFields.H"
00029 #include "interpolationCellPoint.H"
00030 #include "SubField.H"
00031 #include "mixedFvPatchField.H"
00032 
00033 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
00034 
00035 namespace Foam
00036 {
00037 
00038 // * * * * * * * * * * * * * * * Member Functions  * * * * * * * * * * * * * //
00039 
00040 template<class Type>
00041 void meshToMesh::mapField
00042 (
00043     Field<Type>& toF,
00044     const Field<Type>& fromVf,
00045     const labelList& adr
00046 ) const
00047 {
00048     // Direct mapping of nearest-cell values
00049 
00050     forAll(toF, celli)
00051     {
00052         if (adr[celli] != -1)
00053         {
00054             toF[celli] = fromVf[adr[celli]];
00055         }
00056     }
00057 
00058     //toF.map(fromVf, adr);
00059 }
00060 
00061 
00062 template<class Type>
00063 void meshToMesh::interpolateField
00064 (
00065     Field<Type>& toF,
00066     const GeometricField<Type, fvPatchField, volMesh>& fromVf,
00067     const labelList& adr,
00068     const scalarListList& weights
00069 ) const
00070 {
00071     // Inverse distance weighted interpolation
00072 
00073     // get reference to cellCells
00074     const labelListList& cc = fromMesh_.cellCells();
00075 
00076     forAll (toF, celli)
00077     {
00078         if (adr[celli] != -1)
00079         {
00080             const labelList& neighbours = cc[adr[celli]];
00081             const scalarList& w = weights[celli];
00082             
00083             toF[celli] = fromVf[adr[celli]]*w[0];
00084 
00085             for (label ni = 1; ni < w.size(); ni++)
00086             {
00087                 toF[celli] += fromVf[neighbours[ni - 1]]*w[ni];
00088             }
00089         }
00090     }
00091 }
00092 
00093 
00094 template<class Type>
00095 void meshToMesh::interpolateField
00096 (
00097     Field<Type>& toF,
00098     const GeometricField<Type, fvPatchField, volMesh>& fromVf,
00099     const labelList& adr,
00100     const vectorField& centres
00101 ) const
00102 {
00103     // Cell-Point interpolation
00104     interpolationCellPoint<Type> interpolator(fromVf);
00105 
00106     forAll (toF, celli)
00107     {
00108         if (adr[celli] != -1)
00109         {
00110             toF[celli] = interpolator.interpolate
00111             (
00112                 centres[celli],
00113                 adr[celli]
00114             );
00115         }
00116     }
00117 }
00118 
00119 
00120 template<class Type>
00121 void meshToMesh::interpolateInternalField
00122 (
00123     Field<Type>& toF,
00124     const GeometricField<Type, fvPatchField, volMesh>& fromVf,
00125     meshToMesh::order ord
00126 ) const
00127 {
00128     if (fromVf.mesh() != fromMesh_)
00129     {
00130         FatalErrorIn
00131         (
00132             "meshToMesh::interpolateInternalField(Field<Type>& toF, "
00133             "const GeometricField<Type, fvPatchField, volMesh>& fromVf, "
00134             "meshToMesh::order ord) const"
00135         )   << "the argument field does not correspond to the right mesh. "
00136             << "Field size: " << fromVf.size()
00137             << " mesh size: " << fromMesh_.nCells()
00138             << exit(FatalError);
00139     }
00140 
00141     if (toF.size() != toMesh_.nCells())
00142     {
00143         FatalErrorIn
00144         (
00145             "meshToMesh::interpolateInternalField(Field<Type>& toF, "
00146             "const GeometricField<Type, fvPatchField, volMesh>& fromVf, "
00147             "meshToMesh::order ord) const"
00148         )   << "the argument field does not correspond to the right mesh. "
00149             << "Field size: " << toF.size()
00150             << " mesh size: " << toMesh_.nCells()
00151             << exit(FatalError);
00152     }
00153 
00154     switch(ord)
00155     {
00156         case MAP:
00157             mapField(toF, fromVf, cellAddressing_);
00158         break;
00159 
00160         case INTERPOLATE:
00161             interpolateField
00162             (
00163                 toF, 
00164                 fromVf,
00165                 cellAddressing_,
00166                 inverseDistanceWeights()
00167             );
00168         break;
00169 
00170         case CELL_POINT_INTERPOLATE:
00171             interpolateField
00172             (
00173                 toF, 
00174                 fromVf,
00175                 cellAddressing_,
00176                 toMesh_.cellCentres()
00177             );
00178         break;
00179 
00180         default:
00181             FatalErrorIn
00182             (
00183                 "meshToMesh::interpolateInternalField(Field<Type>& toF, "
00184                 "const GeometricField<Type, fvPatchField, volMesh>& fromVf, "
00185                 "meshToMesh::order ord) const"
00186             )   << "unknown interpolation scheme " << ord
00187                 << exit(FatalError);
00188     }
00189 }
00190 
00191 
00192 template<class Type>
00193 void meshToMesh::interpolateInternalField
00194 (
00195     Field<Type>& toF,
00196     const tmp<GeometricField<Type, fvPatchField, volMesh> >& tfromVf,
00197     meshToMesh::order ord
00198 ) const
00199 {
00200     interpolateInternalField(toF, tfromVf(), ord);
00201     tfromVf.clear();
00202 }
00203 
00204 
00205 template<class Type>
00206 void meshToMesh::interpolate
00207 (
00208     GeometricField<Type, fvPatchField, volMesh>& toVf,
00209     const GeometricField<Type, fvPatchField, volMesh>& fromVf,
00210     meshToMesh::order ord
00211 ) const
00212 {
00213     interpolateInternalField(toVf, fromVf, ord);
00214 
00215     forAll (toMesh_.boundaryMesh(), patchi)
00216     {
00217         const fvPatch& toPatch = toMesh_.boundary()[patchi];
00218 
00219         if (cuttingPatches_.found(toPatch.name()))
00220         {
00221             switch(ord)
00222             {
00223                 case MAP:
00224                     mapField
00225                     (
00226                         toVf.boundaryField()[patchi],
00227                         fromVf,
00228                         boundaryAddressing_[patchi]
00229                     );
00230                 break;
00231 
00232                 case INTERPOLATE:
00233                     interpolateField
00234                     (
00235                         toVf.boundaryField()[patchi],
00236                         fromVf,
00237                         boundaryAddressing_[patchi],
00238                         toPatch.Cf()
00239                     );
00240                 break;
00241 
00242                 case CELL_POINT_INTERPOLATE:
00243                     interpolateField
00244                     (
00245                         toVf.boundaryField()[patchi],
00246                         fromVf,
00247                         boundaryAddressing_[patchi],
00248                         toPatch.Cf()
00249                     );
00250                 break;
00251 
00252                 default:
00253                     FatalErrorIn
00254                     (
00255                         "meshToMesh::interpolate("
00256                         "GeometricField<Type, fvPatchField, volMesh>& toVf, "
00257                         "const GeometricField<Type, fvPatchField, volMesh>& "
00258                         "fromVf, meshToMesh::order ord) const"
00259                     )   << "unknown interpolation scheme " << ord
00260                         << exit(FatalError);
00261             }
00262 
00263             if (isA<mixedFvPatchField<Type> >(toVf.boundaryField()[patchi]))
00264             {
00265                 refCast<mixedFvPatchField<Type> >
00266                 (
00267                     toVf.boundaryField()[patchi]
00268                 ).refValue() = toVf.boundaryField()[patchi];
00269             }
00270         }
00271         else if 
00272         (
00273             patchMap_.found(toPatch.name())
00274          && fromMeshPatches_.found(patchMap_.find(toPatch.name())())
00275         )
00276         {
00277             /*
00278             toVf.boundaryField()[patchi].map
00279             (
00280                 fromVf.boundaryField()
00281                 [
00282                     fromMeshPatches_.find(patchMap_.find(toPatch.name())())()
00283                 ],
00284                 boundaryAddressing_[patchi]
00285             );
00286             */
00287 
00288             mapField
00289             (
00290                 toVf.boundaryField()[patchi],
00291                 fromVf.boundaryField()
00292                 [
00293                     fromMeshPatches_.find(patchMap_.find(toPatch.name())())()
00294                 ],
00295                 boundaryAddressing_[patchi]
00296             );
00297         }
00298     }
00299 }
00300 
00301 
00302 template<class Type>
00303 void meshToMesh::interpolate
00304 (
00305     GeometricField<Type, fvPatchField, volMesh>& toVf,
00306     const tmp<GeometricField<Type, fvPatchField, volMesh> >& tfromVf,
00307     meshToMesh::order ord
00308 ) const
00309 {
00310     interpolate(toVf, tfromVf(), ord);
00311     tfromVf.clear();
00312 }
00313 
00314 
00315 template<class Type>
00316 tmp<GeometricField<Type, fvPatchField, volMesh> > meshToMesh::interpolate
00317 (
00318     const GeometricField<Type, fvPatchField, volMesh>& fromVf,
00319     meshToMesh::order ord
00320 ) const
00321 {
00322     // Create and map the internal-field values
00323     Field<Type> internalField(toMesh_.nCells());
00324     interpolateInternalField(internalField, fromVf, ord);
00325 
00326     // check whether both meshes have got the same number
00327     // of boundary patches
00328     if (fromMesh_.boundary().size() != toMesh_.boundary().size())
00329     {
00330         FatalErrorIn
00331         (
00332             "meshToMesh::interpolate"
00333             "(const GeometricField<Type, fvPatchField, volMesh>& fromVf,"
00334             "meshToMesh::order ord) const"
00335         )   << "Incompatible meshes: different number of boundaries, "
00336                "only internal field may be interpolated"
00337             << exit(FatalError);
00338     }
00339 
00340     // Create and map the patch field values
00341     PtrList<fvPatchField<Type> > patchFields
00342     (
00343         boundaryAddressing_.size()
00344     );
00345 
00346     forAll (boundaryAddressing_, patchI)
00347     {
00348         patchFields.set
00349         (
00350             patchI,
00351             fvPatchField<Type>::New
00352             (
00353                 fromVf.boundaryField()[patchI],
00354                 toMesh_.boundary()[patchI],
00355                 DimensionedField<Type, volMesh>::null(),
00356                 patchFieldInterpolator
00357                 (
00358                     boundaryAddressing_[patchI]
00359                 )
00360             )
00361         );
00362     }
00363 
00364 
00365     // Create the complete field from the pieces
00366     tmp<GeometricField<Type, fvPatchField, volMesh> > ttoF
00367     (
00368         new GeometricField<Type, fvPatchField, volMesh>
00369         (
00370             IOobject
00371             (
00372                 "interpolated(" + fromVf.name() + ')',
00373                 toMesh_.time().timeName(),
00374                 toMesh_,
00375                 IOobject::NO_READ,
00376                 IOobject::NO_WRITE
00377             ),
00378             toMesh_,
00379             fromVf.dimensions(),
00380             internalField,
00381             patchFields
00382         )
00383     );
00384 
00385     return ttoF;
00386 }
00387 
00388 
00389 template<class Type>
00390 tmp<GeometricField<Type, fvPatchField, volMesh> > meshToMesh::interpolate
00391 (
00392     const tmp<GeometricField<Type, fvPatchField, volMesh> >& tfromVf,
00393     meshToMesh::order ord
00394 ) const
00395 {
00396     tmp<GeometricField<Type, fvPatchField, volMesh> > tint =
00397         interpolate(tfromVf(), ord);
00398     tfromVf.clear();
00399 
00400     return tint;
00401 }
00402 
00403 
00404 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
00405 
00406 } // End namespace Foam
00407 
00408 // ************************************************************************* //
Copyright © 2000-2009 OpenCFD Ltd