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

interpolationCellPointFace.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 Description
00026 
00027 \*---------------------------------------------------------------------------*/
00028 
00029 #include "interpolationCellPointFace.H"
00030 #include "volFields.H"
00031 #include "polyMesh.H"
00032 #include "volPointInterpolation.H"
00033 #include "linear.H"
00034 #include "findCellPointFaceTet.H"
00035 #include "findCellPointFaceTriangle.H"
00036 
00037 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
00038 
00039 namespace Foam
00040 {
00041 
00042 // * * * * * * * * * * * * * * * * Constructor * * * * * * * * * * * * * * * //
00043 
00044 template<class Type>
00045 interpolationCellPointFace<Type>::interpolationCellPointFace
00046 (
00047     const GeometricField<Type, fvPatchField, volMesh>& psi
00048 )
00049 :
00050     interpolation<Type>(psi),
00051     psip_(volPointInterpolation::New(psi.mesh()).interpolate(psi)),
00052     psis_(linearInterpolate(psi))
00053 {}
00054 
00055 
00056 // * * * * * * * * * * * * * * * Member Functions  * * * * * * * * * * * * * //
00057 
00058 template<class Type>
00059 Type interpolationCellPointFace<Type>::interpolate
00060 (
00061     const vector& position,
00062     const label nCell,
00063     const label facei
00064 ) const
00065 {
00066     Type ts[4];
00067     vector tetPoints[4];
00068     scalar phi[4], phiCandidate[4];
00069     label tetLabelCandidate[2], tetPointLabels[2];
00070 
00071     Type t = pTraits<Type>::zero;
00072 
00073     // only use face information when the position is on a face
00074     if (facei < 0)
00075     {
00076         const vector& cellCentre = this->pMesh_.cellCentres()[nCell];
00077         const labelList& cellFaces = this->pMesh_.cells()[nCell];
00078         
00079         vector projection = position - cellCentre;
00080         tetPoints[3] = cellCentre;
00081         
00082         // *********************************************************************
00083         // project the cell-center through the point onto the face
00084         // and get the closest face, ...
00085         // *********************************************************************
00086         
00087         bool foundTet = false;
00088         label closestFace = -1;
00089         scalar minDistance = GREAT;
00090         
00091         forAll(cellFaces, facei)
00092         {
00093             label nFace = cellFaces[facei];
00094         
00095             vector normal = this->pMeshFaceAreas_[nFace];
00096             normal /= mag(normal);
00097         
00098             const vector& faceCentreTmp = this->pMeshFaceCentres_[nFace];
00099         
00100             scalar multiplierNumerator = (faceCentreTmp - cellCentre) & normal;
00101             scalar multiplierDenominator = projection & normal;
00102         
00103             // if normal and projection are not orthogonal this could be the one...
00104             if (mag(multiplierDenominator) > SMALL)
00105             {
00106                 scalar multiplier = multiplierNumerator/multiplierDenominator;
00107                 vector iPoint = cellCentre + multiplier*projection;
00108                 scalar dist = mag(position - iPoint);
00109         
00110                 if (dist < minDistance)
00111                 {
00112                     closestFace = nFace;
00113                     minDistance = dist;
00114                 }
00115             }
00116         }
00117         
00118         // *********************************************************************
00119         // find the tetrahedron containing 'position'
00120         // from the cell center, face center and
00121         // two other points on the face
00122         // *********************************************************************
00123         
00124         minDistance = GREAT;
00125         if (closestFace != -1)
00126         {
00127             label nFace = closestFace;
00128             foundTet = findTet
00129             (
00130                 position,
00131                 nFace,
00132                 tetPoints,
00133                 tetLabelCandidate,
00134                 tetPointLabels,
00135                 phi,
00136                 phiCandidate,
00137                 closestFace,
00138                 minDistance
00139             );
00140         }
00141         
00142         if (!foundTet)
00143         {
00144             // check if the position is 'just' outside a tet
00145             if (minDistance < 1.0e-5)
00146             {
00147                 foundTet = true;
00148                 for (label i=0; i<4; i++)
00149                 {
00150                     phi[i] = phiCandidate[i];
00151                 }
00152                 tetPointLabels[0] = tetLabelCandidate[0];
00153                 tetPointLabels[1] = tetLabelCandidate[1];
00154             }
00155         }
00156         
00157         // *********************************************************************
00158         // if the search failed check all the cell-faces
00159         // *********************************************************************
00160         
00161         if (!foundTet)
00162         {
00163             minDistance = GREAT;
00164         
00165             label facei = 0;
00166             while (facei < cellFaces.size() && !foundTet)
00167             {
00168                 label nFace = cellFaces[facei];
00169                 if (nFace < this->pMeshFaceAreas_.size())
00170                 {
00171                     foundTet = findTet
00172                     (
00173                         position,
00174                         nFace,
00175                         tetPoints,
00176                         tetLabelCandidate,
00177                         tetPointLabels,
00178                         phi,
00179                         phiCandidate,
00180                         closestFace,
00181                         minDistance
00182                     );
00183                 }
00184                 facei++;
00185             }
00186         }
00187         
00188         if (!foundTet)
00189         {
00190             // check if the position is 'just' outside a tet
00191             // this time with a more tolerant limit
00192             if (minDistance < 1.0e-3)
00193             {
00194                 foundTet = true;
00195                 for (label i=0; i<4; i++)
00196                 {
00197                     phi[i] = phiCandidate[i];
00198                 }
00199                 tetPointLabels[0] = tetLabelCandidate[0];
00200                 tetPointLabels[1] = tetLabelCandidate[1];
00201             }
00202         }
00203         
00204         // *********************************************************************
00205         // if the tet was found do the interpolation,
00206         // otherwise use the closest face value
00207         // *********************************************************************
00208         
00209         if (foundTet)
00210         {
00211             for (label i=0; i<2; i++)
00212             {
00213                 ts[i] = psip_[tetPointLabels[i]];
00214             }
00215         
00216             if (closestFace < psis_.size())
00217             {
00218                 ts[2] = psis_[closestFace];
00219             }
00220             else
00221             {
00222                 label patchi = this->pMesh_.boundaryMesh().whichPatch(closestFace);
00223         
00224                 // If the boundary patch is not empty use the face value
00225                 // else use the cell value
00226                 if (this->psi_.boundaryField()[patchi].size())
00227                 {
00228                     ts[2] = this->psi_.boundaryField()[patchi]
00229                         [this->pMesh_.boundaryMesh()[patchi].whichFace(closestFace)];
00230                 }
00231                 else
00232                 {
00233                     ts[2] = this->psi_[nCell];
00234                 }
00235             }
00236             
00237             ts[3] = this->psi_[nCell];
00238             
00239             for (label n=0; n<4; n++)
00240             {
00241                 phi[n] = min(1.0, phi[n]);
00242                 phi[n] = max(0.0, phi[n]);
00243         
00244                 t += phi[n]*ts[n];
00245             }
00246         }
00247         else
00248         {
00249             Info<< "interpolationCellPointFace<Type>::interpolate"
00250                 << "(const vector&, const label nCell) const : "
00251                 << "search failed; using closest cellFace value" << endl
00252                 << "cell number " << nCell << tab
00253                 << "position " << position << endl;
00254         
00255             if (closestFace < psis_.size())
00256             {
00257                 t = psis_[closestFace];
00258             }
00259             else
00260             {
00261                 label patchi = this->pMesh_.boundaryMesh().whichPatch(closestFace);
00262         
00263                 // If the boundary patch is not empty use the face value
00264                 // else use the cell value
00265                 if (this->psi_.boundaryField()[patchi].size())
00266                 {
00267                     t = this->psi_.boundaryField()[patchi]
00268                         [this->pMesh_.boundaryMesh()[patchi].whichFace(closestFace)];
00269                 }
00270                 else
00271                 {
00272                     t = this->psi_[nCell];
00273                 }
00274             }
00275         }
00276     }
00277     else
00278     {
00279         bool foundTriangle = findTriangle
00280         (
00281             position,
00282             facei,
00283             tetPointLabels,
00284             phi
00285         );
00286 
00287         if (foundTriangle)
00288         {
00289             // add up the point values ...
00290             for (label i=0; i<2; i++)
00291             {
00292                 Type vel = psip_[tetPointLabels[i]];
00293                 t += phi[i]*vel;
00294             }
00295 
00296             // ... and the face value
00297             if (facei < psis_.size())
00298             {
00299                 t += phi[2]*psis_[facei];
00300             }
00301             else
00302             {
00303                 label patchi = this->pMesh_.boundaryMesh().whichPatch(facei);
00304                 
00305                 // If the boundary patch is not empty use the face value
00306                 // else use the cell value
00307                 if (this->psi_.boundaryField()[patchi].size())
00308                 {
00309                     t += phi[2]*this->psi_.boundaryField()[patchi]
00310                         [this->pMesh_.boundaryMesh()[patchi].whichFace(facei)];
00311                 }
00312                 else
00313                 {
00314                     t += phi[2]*this->psi_[nCell];
00315                 }
00316             }
00317         }
00318         else
00319         {
00320             // use face value only
00321             if (facei < psis_.size())
00322             {
00323                 t = psis_[facei];
00324             }
00325             else
00326             {
00327                 label patchi = this->pMesh_.boundaryMesh().whichPatch(facei);
00328                 
00329                 // If the boundary patch is not empty use the face value
00330                 // else use the cell value
00331                 if (this->psi_.boundaryField()[patchi].size())
00332                 {
00333                     t = this->psi_.boundaryField()[patchi]
00334                         [this->pMesh_.boundaryMesh()[patchi].whichFace(facei)];
00335                 }
00336                 else
00337                 {
00338                     t = this->psi_[nCell];
00339                 }
00340             }
00341         }
00342     }
00343 
00344     return t;
00345 }
00346 
00347 
00348 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
00349 
00350 } // End namespace Foam
00351 
00352 // ************************************************************************* //
Copyright © 2000-2009 OpenCFD Ltd