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

tetrahedron.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     Calculation of shape function product for a tetrahedron
00027 
00028 \*---------------------------------------------------------------------------*/
00029 
00030 #include "tetrahedron.H"
00031 #include "triPointRef.H"
00032 #include "scalarField.H"
00033 
00034 
00035 // * * * * * * * * * * * * * * * Member Functions  * * * * * * * * * * * * * //
00036 
00037 // (Probably very inefficient) minimum containment sphere calculation.
00038 // From http://www.imr.sandia.gov/papers/imr11/shewchuk2.pdf:
00039 // Sphere ctr is smallest one of
00040 // - tet circumcentre
00041 // - triangle circumcentre
00042 // - edge mids
00043 template<class Point, class PointRef>
00044 Foam::pointHit Foam::tetrahedron<Point, PointRef>::containmentSphere
00045 (
00046     const scalar tol
00047 ) const
00048 {
00049     const scalar fac = 1 + tol;
00050 
00051     // Halve order of tolerance for comparisons of sqr.
00052     const scalar facSqr = Foam::sqrt(fac);
00053 
00054 
00055     // 1. Circumcentre itself.
00056 
00057     pointHit pHit(circumCentre());
00058     pHit.setHit();
00059     scalar minRadiusSqr = magSqr(pHit.rawPoint() - a_);
00060 
00061 
00062     // 2. Try circumcentre of tet triangles. Create circumcircle for triFace and
00063     // check if 4th point is inside.
00064 
00065     {
00066         point ctr = triPointRef(a_, b_, c_).circumCentre();
00067         scalar radiusSqr = magSqr(ctr - a_);
00068 
00069         if
00070         (
00071             radiusSqr < minRadiusSqr
00072          && Foam::magSqr(d_-ctr) <= facSqr*radiusSqr
00073         )
00074         {
00075             pHit.setMiss(false);
00076             pHit.setPoint(ctr);
00077             minRadiusSqr = radiusSqr;
00078         }
00079     }
00080     {
00081         point ctr = triPointRef(a_, b_, d_).circumCentre();
00082         scalar radiusSqr = magSqr(ctr - a_);
00083 
00084         if
00085         (
00086             radiusSqr < minRadiusSqr
00087          && Foam::magSqr(c_-ctr) <= facSqr*radiusSqr
00088         )
00089         {
00090             pHit.setMiss(false);
00091             pHit.setPoint(ctr);
00092             minRadiusSqr = radiusSqr;
00093         }
00094     }
00095     {
00096         point ctr = triPointRef(a_, c_, d_).circumCentre();
00097         scalar radiusSqr = magSqr(ctr - a_);
00098 
00099         if
00100         (
00101             radiusSqr < minRadiusSqr
00102          && Foam::magSqr(b_-ctr) <= facSqr*radiusSqr
00103         )
00104         {
00105             pHit.setMiss(false);
00106             pHit.setPoint(ctr);
00107             minRadiusSqr = radiusSqr;
00108         }
00109     }
00110     {
00111         point ctr = triPointRef(b_, c_, d_).circumCentre();
00112         scalar radiusSqr = magSqr(ctr - b_);
00113 
00114         if
00115         (
00116             radiusSqr < minRadiusSqr
00117          && Foam::magSqr(a_-ctr) <= facSqr*radiusSqr
00118         )
00119         {
00120             pHit.setMiss(false);
00121             pHit.setPoint(ctr);
00122             minRadiusSqr = radiusSqr;
00123         }
00124     }
00125 
00126 
00127     // 3. Try midpoints of edges
00128 
00129     // mid of edge A-B
00130     {
00131         point ctr = 0.5*(a_ + b_);
00132         scalar radiusSqr = magSqr(a_ - ctr);
00133         scalar testRadSrq = facSqr*radiusSqr;
00134 
00135         if
00136         (
00137             radiusSqr < minRadiusSqr
00138          && magSqr(c_-ctr) <= testRadSrq
00139          && magSqr(d_-ctr) <= testRadSrq)
00140         {
00141             pHit.setMiss(false);
00142             pHit.setPoint(ctr);
00143             minRadiusSqr = radiusSqr;
00144         }
00145     }
00146 
00147     // mid of edge A-C
00148     {
00149         point ctr = 0.5*(a_ + c_);
00150         scalar radiusSqr = magSqr(a_ - ctr);
00151         scalar testRadSrq = facSqr*radiusSqr;
00152 
00153         if
00154         (
00155             radiusSqr < minRadiusSqr
00156          && magSqr(b_-ctr) <= testRadSrq
00157          && magSqr(d_-ctr) <= testRadSrq
00158         )
00159         {
00160             pHit.setMiss(false);
00161             pHit.setPoint(ctr);
00162             minRadiusSqr = radiusSqr;
00163         }
00164     }
00165 
00166     // mid of edge A-D
00167     {
00168         point ctr = 0.5*(a_ + d_);
00169         scalar radiusSqr = magSqr(a_ - ctr);
00170         scalar testRadSrq = facSqr*radiusSqr;
00171 
00172         if
00173         (
00174             radiusSqr < minRadiusSqr
00175          && magSqr(b_-ctr) <= testRadSrq
00176          && magSqr(c_-ctr) <= testRadSrq
00177         )
00178         {
00179             pHit.setMiss(false);
00180             pHit.setPoint(ctr);
00181             minRadiusSqr = radiusSqr;
00182         }
00183     }
00184 
00185     // mid of edge B-C
00186     {
00187         point ctr = 0.5*(b_ + c_);
00188         scalar radiusSqr = magSqr(b_ - ctr);
00189         scalar testRadSrq = facSqr*radiusSqr;
00190 
00191         if
00192         (
00193             radiusSqr < minRadiusSqr
00194          && magSqr(a_-ctr) <= testRadSrq
00195          && magSqr(d_-ctr) <= testRadSrq
00196         )
00197         {
00198             pHit.setMiss(false);
00199             pHit.setPoint(ctr);
00200             minRadiusSqr = radiusSqr;
00201         }
00202     }
00203 
00204     // mid of edge B-D
00205     {
00206         point ctr = 0.5*(b_ + d_);
00207         scalar radiusSqr = magSqr(b_ - ctr);
00208         scalar testRadSrq = facSqr*radiusSqr;
00209 
00210         if
00211         (
00212             radiusSqr < minRadiusSqr
00213          && magSqr(a_-ctr) <= testRadSrq
00214          && magSqr(c_-ctr) <= testRadSrq)
00215         {
00216             pHit.setMiss(false);
00217             pHit.setPoint(ctr);
00218             minRadiusSqr = radiusSqr;
00219         }
00220     }
00221 
00222     // mid of edge C-D
00223     {
00224         point ctr = 0.5*(c_ + d_);
00225         scalar radiusSqr = magSqr(c_ - ctr);
00226         scalar testRadSrq = facSqr*radiusSqr;
00227 
00228         if
00229         (
00230             radiusSqr < minRadiusSqr
00231          && magSqr(a_-ctr) <= testRadSrq
00232          && magSqr(b_-ctr) <= testRadSrq
00233         )
00234         {
00235             pHit.setMiss(false);
00236             pHit.setPoint(ctr);
00237             minRadiusSqr = radiusSqr;
00238         }
00239     }
00240 
00241 
00242     pHit.setDistance(sqrt(minRadiusSqr));
00243 
00244     return pHit;
00245 }
00246 
00247 
00248 template<class Point, class PointRef>
00249 void Foam::tetrahedron<Point, PointRef>::gradNiSquared
00250 (
00251     scalarField& buffer
00252 ) const
00253 {
00254     // Change of sign between face area vector and gradient
00255     // does not matter because of square
00256 
00257     // Warning: Added a mag to produce positive coefficients even if
00258     // the tetrahedron is twisted inside out.  This is pretty
00259     // dangerous, but essential for mesh motion.
00260     scalar magVol = Foam::mag(mag());
00261 
00262     buffer[0] = (1.0/9.0)*magSqr(Sa())/magVol;
00263     buffer[1] = (1.0/9.0)*magSqr(Sb())/magVol;
00264     buffer[2] = (1.0/9.0)*magSqr(Sc())/magVol;
00265     buffer[3] = (1.0/9.0)*magSqr(Sd())/magVol;
00266 }
00267 
00268 
00269 template<class Point, class PointRef>
00270 void Foam::tetrahedron<Point, PointRef>::gradNiDotGradNj
00271 (
00272     scalarField& buffer
00273 ) const
00274 {
00275     // Warning. Ordering of edges needs to be the same for a tetrahedron
00276     // class, a tetrahedron cell shape model and a tetCell
00277 
00278     // Warning: Added a mag to produce positive coefficients even if
00279     // the tetrahedron is twisted inside out.  This is pretty
00280     // dangerous, but essential for mesh motion.
00281 
00282     // Double change of sign between face area vector and gradient
00283 
00284     scalar magVol = Foam::mag(mag());
00285     vector sa = Sa();
00286     vector sb = Sb();
00287     vector sc = Sc();
00288     vector sd = Sd();
00289 
00290     buffer[0] = (1.0/9.0)*(sa & sb)/magVol;
00291     buffer[1] = (1.0/9.0)*(sa & sc)/magVol;
00292     buffer[2] = (1.0/9.0)*(sa & sd)/magVol;
00293     buffer[3] = (1.0/9.0)*(sd & sb)/magVol;
00294     buffer[4] = (1.0/9.0)*(sb & sc)/magVol;
00295     buffer[5] = (1.0/9.0)*(sd & sc)/magVol;
00296 }
00297 
00298 
00299 template<class Point, class PointRef>
00300 void Foam::tetrahedron<Point, PointRef>::gradNiGradNi
00301 (
00302     tensorField& buffer
00303 ) const
00304 {
00305     // Change of sign between face area vector and gradient
00306     // does not matter because of square
00307 
00308     scalar magVol = Foam::mag(mag());
00309 
00310     buffer[0] = (1.0/9.0)*sqr(Sa())/magVol;
00311     buffer[1] = (1.0/9.0)*sqr(Sb())/magVol;
00312     buffer[2] = (1.0/9.0)*sqr(Sc())/magVol;
00313     buffer[3] = (1.0/9.0)*sqr(Sd())/magVol;
00314 }
00315 
00316 
00317 template<class Point, class PointRef>
00318 void Foam::tetrahedron<Point, PointRef>::gradNiGradNj
00319 (
00320     tensorField& buffer
00321 ) const
00322 {
00323     // Warning. Ordering of edges needs to be the same for a tetrahedron
00324     // class, a tetrahedron cell shape model and a tetCell
00325 
00326     // Double change of sign between face area vector and gradient
00327 
00328     scalar magVol = Foam::mag(mag());
00329     vector sa = Sa();
00330     vector sb = Sb();
00331     vector sc = Sc();
00332     vector sd = Sd();
00333 
00334     buffer[0] = (1.0/9.0)*(sa * sb)/magVol;
00335     buffer[1] = (1.0/9.0)*(sa * sc)/magVol;
00336     buffer[2] = (1.0/9.0)*(sa * sd)/magVol;
00337     buffer[3] = (1.0/9.0)*(sd * sb)/magVol;
00338     buffer[4] = (1.0/9.0)*(sb * sc)/magVol;
00339     buffer[5] = (1.0/9.0)*(sd * sc)/magVol;
00340 }
00341 
00342 
00343 // ************************************************************************* //
Copyright © 2000-2009 OpenCFD Ltd