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

cellPointWeight.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 "cellPointWeight.H"
00028 #include "polyMesh.H"
00029 
00030 // * * * * * * * * * * * * * * Static Data Members * * * * * * * * * * * * * //
00031 
00032 int Foam::cellPointWeight::debug(debug::debugSwitch("cellPointWeight", 0));
00033 Foam::scalar Foam::cellPointWeight::tol(SMALL);
00034 
00035 // * * * * * * * * * * * * Private Member Functions  * * * * * * * * * * * * //
00036 
00037 void Foam::cellPointWeight::findTetrahedron
00038 (
00039     const polyMesh& mesh,
00040     const vector& position,
00041     const label cellIndex
00042 )
00043 {
00044     if (debug)
00045     {
00046         Pout<< "\nFoam::cellPointWeight::findTetrahedron" << nl
00047             << "position = " << position << nl
00048             << "cellIndex = " << cellIndex << endl;
00049     }
00050 
00051     // Initialise closest triangle variables
00052     scalar minUVWClose = VGREAT;
00053     label pointIClose = 0;
00054     label faceClose = 0;
00055 
00056     const vector& P0 = mesh.cellCentres()[cellIndex];
00057     const labelList& cellFaces = mesh.cells()[cellIndex];
00058     const scalar cellVolume = mesh.cellVolumes()[cellIndex];
00059 
00060     // Find the tet that the point occupies
00061     forAll(cellFaces, faceI)
00062     {
00063         // Decompose each face into triangles, making a tet when
00064         // augmented by the cell centre
00065         const labelList& facePoints = mesh.faces()[cellFaces[faceI]];
00066 
00067         label pointI = 1;
00068         while ((pointI + 1) < facePoints.size())
00069         {
00070             // Cartesian co-ordinates of the triangle vertices
00071             const vector& P1 = mesh.points()[facePoints[0]];
00072             const vector& P2 = mesh.points()[facePoints[pointI]];
00073             const vector& P3 = mesh.points()[facePoints[pointI + 1]];
00074 
00075             // Edge vectors
00076             const vector e1 = P1 - P0;
00077             const vector e2 = P2 - P0;
00078             const vector e3 = P3 - P0;
00079 
00080             // Solve for interpolation weighting factors
00081 
00082             // Source term
00083             const vector rhs = position - P0;
00084 
00085             // Determinant of coefficients matrix
00086             // Note: if det(A) = 0 the tet is degenerate
00087             const scalar detA =
00088                 e1.x()*e2.y()*e3.z() + e2.x()*e3.y()*e1.z()
00089               + e3.x()*e1.y()*e2.z() - e1.x()*e3.y()*e2.z()
00090               - e2.x()*e1.y()*e3.z() - e3.x()*e2.y()*e1.z();
00091 
00092             if (mag(detA/cellVolume) > tol)
00093             {
00094                 // Solve using Cramers' rule
00095                 const scalar u =
00096                 (
00097                    rhs.x()*e2.y()*e3.z() + e2.x()*e3.y()*rhs.z()
00098                   + e3.x()*rhs.y()*e2.z() - rhs.x()*e3.y()*e2.z()
00099                   - e2.x()*rhs.y()*e3.z() - e3.x()*e2.y()*rhs.z()
00100                 )/detA;
00101 
00102                 const scalar v =
00103                 (
00104                     e1.x()*rhs.y()*e3.z() + rhs.x()*e3.y()*e1.z()
00105                   + e3.x()*e1.y()*rhs.z() - e1.x()*e3.y()*rhs.z()
00106                   - rhs.x()*e1.y()*e3.z() - e3.x()*rhs.y()*e1.z()
00107                 )/detA;
00108 
00109                 const scalar w =
00110                 (
00111                     e1.x()*e2.y()*rhs.z() + e2.x()*rhs.y()*e1.z()
00112                   + rhs.x()*e1.y()*e2.z() - e1.x()*rhs.y()*e2.z()
00113                   - e2.x()*e1.y()*rhs.z() - rhs.x()*e2.y()*e1.z()
00114                 )/detA;
00115 
00116                 // Check if point is in tet
00117                 // value = 0 indicates position lies on a tet face
00118                 if
00119                 (
00120                    (u + tol > 0) && (v + tol > 0) && (w + tol > 0)
00121                 && (u + v + w < 1 + tol)
00122                 )
00123                 {
00124                     faceVertices_[0] = facePoints[0];
00125                     faceVertices_[1] = facePoints[pointI];
00126                     faceVertices_[2] = facePoints[pointI + 1];
00127 
00128                     weights_[0] = u;
00129                     weights_[1] = v;
00130                     weights_[2] = w;
00131                     weights_[3] = 1.0 - (u + v + w);
00132 
00133                     return;
00134                 }
00135                 else
00136                 {
00137                     scalar minU = mag(u);
00138                     scalar minV = mag(v);
00139                     scalar minW = mag(w);
00140                     if (minU > 1.0)
00141                     {
00142                         minU -= 1.0;
00143                     }
00144                     if (minV > 1.0)
00145                     {
00146                         minV -= 1.0;
00147                     }
00148                     if (minW > 1.0)
00149                     {
00150                         minW -= 1.0;
00151                     }
00152                     const scalar minUVW = mag(minU + minV + minW);
00153 
00154                     if (minUVW < minUVWClose)
00155                     {
00156                         minUVWClose = minUVW;
00157                         pointIClose = pointI;
00158                         faceClose = faceI;
00159                     }
00160                 }
00161             }
00162 
00163             pointI++;
00164         }
00165     }
00166 
00167     if (debug)
00168     {
00169         Pout<< "cellPointWeight::findTetrahedron" << nl
00170             << "    Tetrahedron search failed; using closest tet values to "
00171             << "point " << nl << "    cell: " << cellIndex << nl << endl;
00172     }
00173 
00174     const labelList& facePointsClose = mesh.faces()[cellFaces[faceClose]];
00175     faceVertices_[0] = facePointsClose[0];
00176     faceVertices_[1] = facePointsClose[pointIClose];
00177     faceVertices_[2] = facePointsClose[pointIClose + 1];
00178 
00179     weights_[0] = 0.25;
00180     weights_[1] = 0.25;
00181     weights_[2] = 0.25;
00182     weights_[3] = 0.25;
00183 }
00184 
00185 
00186 void Foam::cellPointWeight::findTriangle
00187 (
00188     const polyMesh& mesh,
00189     const vector& position,
00190     const label faceIndex
00191 )
00192 {
00193     if (debug)
00194     {
00195         Pout<< "\nbool Foam::cellPointWeight::findTriangle" << nl
00196             << "position = " << position << nl
00197             << "faceIndex = " << faceIndex << endl;
00198     }
00199 
00200     // Initialise closest triangle variables
00201     scalar minUVClose = VGREAT;
00202     label pointIClose = 0;
00203 
00204     // Decompose each face into triangles, making a tet when
00205     // augmented by the cell centre
00206     const labelList& facePoints = mesh.faces()[faceIndex];
00207 
00208     const scalar faceArea2 = magSqr(mesh.faceAreas()[faceIndex]);
00209 
00210     label pointI = 1;
00211     while ((pointI + 1) < facePoints.size())
00212     {
00213         // Cartesian co-ordinates of the triangle vertices
00214         const vector& P1 = mesh.points()[facePoints[0]];
00215         const vector& P2 = mesh.points()[facePoints[pointI]];
00216         const vector& P3 = mesh.points()[facePoints[pointI + 1]];
00217 
00218         // Direction vectors
00219         vector v1 = position - P1;
00220         const vector v2 = P2 - P1;
00221         const vector v3 = P3 - P1;
00222 
00223         // Plane normal
00224         vector n = v2 ^ v3;
00225         n /= mag(n);
00226 
00227         // Remove any offset to plane
00228         v1 -= (n & v1)*v1;
00229 
00230         // Helper variables
00231         const scalar d12 = v1 & v2;
00232         const scalar d13 = v1 & v3;
00233         const scalar d22 = v2 & v2;
00234         const scalar d23 = v2 & v3;
00235         const scalar d33 = v3 & v3;
00236 
00237         // Determinant of coefficients matrix
00238         // Note: if det(A) = 0 the triangle is degenerate
00239         const scalar detA = d22*d33 - d23*d23;
00240 
00241         if (0.25*detA/faceArea2 > tol)
00242         {
00243             // Solve using Cramers' rule
00244             const scalar u = (d12*d33 - d23*d13)/detA;
00245             const scalar v = (d22*d13 - d12*d23)/detA;
00246 
00247             // Check if point is in triangle
00248             if ((u + tol > 0) && (v + tol > 0) && (u + v < 1 + tol))
00249             {
00250                 // Indices of the cell vertices making up the triangle
00251                 faceVertices_[0] = facePoints[0];
00252                 faceVertices_[1] = facePoints[pointI];
00253                 faceVertices_[2] = facePoints[pointI + 1];
00254 
00255                 weights_[0] = u;
00256                 weights_[1] = v;
00257                 weights_[2] = 1.0 - (u + v);
00258                 weights_[3] = 0.0;
00259 
00260                 return;
00261             }
00262             else
00263             {
00264                 scalar minU = mag(u);
00265                 scalar minV = mag(v);
00266                 if (minU > 1.0)
00267                 {
00268                     minU -= 1.0;
00269                 }
00270                 if (minV > 1.0)
00271                 {
00272                     minV -= 1.0;
00273                 }
00274                 const scalar minUV = mag(minU + minV);
00275 
00276                 if (minUV < minUVClose)
00277                 {
00278                     minUVClose = minUV;
00279                     pointIClose = pointI;
00280                 }
00281             }
00282         }
00283 
00284         pointI++;
00285     }
00286 
00287     if (debug)
00288     {
00289         Pout<< "Foam::cellPointWeight::findTriangle"
00290             << "Triangle search failed; using closest triangle to point" << nl
00291             << "    cell face: " << faceIndex << nl << endl;
00292     }
00293 
00294     // Indices of the cell vertices making up the triangle
00295     faceVertices_[0] = facePoints[0];
00296     faceVertices_[1] = facePoints[pointIClose];
00297     faceVertices_[2] = facePoints[pointIClose + 1];
00298 
00299     weights_[0] = 1.0/3.0;
00300     weights_[1] = 1.0/3.0;
00301     weights_[2] = 1.0/3.0;
00302     weights_[3] = 0.0;
00303 }
00304 
00305 
00306 // * * * * * * * * * * * * * * * * Constructors  * * * * * * * * * * * * * * //
00307 
00308 Foam::cellPointWeight::cellPointWeight
00309 (
00310     const polyMesh& mesh,
00311     const vector& position,
00312     const label cellIndex,
00313     const label faceIndex
00314 )
00315 :
00316     cellIndex_(cellIndex)
00317 {
00318     if (faceIndex < 0)
00319     {
00320         // Face data not supplied
00321         findTetrahedron(mesh, position, cellIndex);
00322     }
00323     else
00324     {
00325         // Face data supplied
00326         findTriangle(mesh, position, faceIndex);
00327     }
00328 }
00329 
00330 
00331 // ************************************************************************* //
Copyright © 2000-2009 OpenCFD Ltd