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

coupledPolyPatch.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 "coupledPolyPatch.H"
00028 #include "ListOps.H"
00029 #include "transform.H"
00030 #include "OFstream.H"
00031 
00032 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
00033 
00034 defineTypeNameAndDebug(Foam::coupledPolyPatch, 0);
00035 
00036 Foam::scalar Foam::coupledPolyPatch::matchTol = 1E-3;
00037 
00038 
00039 // * * * * * * * * * * * * Private Member Functions  * * * * * * * * * * * * //
00040 
00041 void Foam::coupledPolyPatch::writeOBJ(Ostream& os, const point& pt)
00042 {
00043     os << "v " << pt.x() << ' ' << pt.y() << ' ' << pt.z() << endl;
00044 }
00045 
00046 
00047 void Foam::coupledPolyPatch::writeOBJ
00048 (
00049     Ostream& os,
00050     const pointField& points,
00051     const labelList& pointLabels
00052 )
00053 {
00054     forAll(pointLabels, i)
00055     {
00056         writeOBJ(os, points[pointLabels[i]]);
00057     }
00058 }
00059 
00060 
00061 void Foam::coupledPolyPatch::writeOBJ
00062 (
00063     Ostream& os,
00064     const point& p0,
00065     const point& p1,
00066     label& vertI
00067 )
00068 {
00069     writeOBJ(os, p0);
00070     vertI++;
00071 
00072     writeOBJ(os, p1);
00073     vertI++;
00074 
00075     os<< "l " << vertI-1 << ' ' << vertI << nl;
00076 }
00077 
00078 
00079 void Foam::coupledPolyPatch::writeOBJ
00080 (
00081     const fileName& fName,
00082     const UList<face>& faces,
00083     const pointField& points
00084 )
00085 {
00086     OFstream os(fName);
00087 
00088     Map<label> foamToObj(4*faces.size());
00089 
00090     label vertI = 0;
00091 
00092     forAll(faces, i)
00093     {
00094         const face& f = faces[i];
00095 
00096         forAll(f, fp)
00097         {
00098             if (foamToObj.insert(f[fp], vertI))
00099             {
00100                 writeOBJ(os, points[f[fp]]);
00101                 vertI++;
00102             }
00103         }
00104 
00105         os << 'l';
00106         forAll(f, fp)
00107         {
00108             os << ' ' << foamToObj[f[fp]]+1;
00109         }
00110         os << ' ' << foamToObj[f[0]]+1 << nl;
00111     }
00112 }
00113 
00114 
00115 Foam::pointField Foam::coupledPolyPatch::calcFaceCentres
00116 (
00117     const UList<face>& faces,
00118     const pointField& points
00119 )
00120 {
00121     pointField ctrs(faces.size());
00122 
00123     forAll(faces, faceI)
00124     {
00125         ctrs[faceI] = faces[faceI].centre(points);
00126     }
00127 
00128     return ctrs;
00129 }
00130 
00131 
00132 Foam::pointField Foam::coupledPolyPatch::getAnchorPoints
00133 (
00134     const UList<face>& faces,
00135     const pointField& points
00136 )
00137 {
00138     pointField anchors(faces.size());
00139 
00140     forAll(faces, faceI)
00141     {
00142         anchors[faceI] = points[faces[faceI][0]];
00143     }
00144 
00145     return anchors;
00146 }
00147 
00148 
00149 bool Foam::coupledPolyPatch::inPatch
00150 (
00151     const labelList& oldToNew,
00152     const label oldFaceI
00153 ) const
00154 {
00155     label faceI = oldToNew[oldFaceI];
00156 
00157     return faceI >= start() && faceI < start()+size();
00158 }
00159 
00160 
00161 Foam::label Foam::coupledPolyPatch::whichPatch
00162 (
00163     const labelList& patchStarts,
00164     const label faceI
00165 )
00166 {
00167     forAll(patchStarts, patchI)
00168     {
00169         if (patchStarts[patchI] <= faceI)
00170         {
00171             if (patchI == patchStarts.size()-1)
00172             {
00173                 return patchI;
00174             }
00175             else if (patchStarts[patchI+1] > faceI)
00176             {
00177                 return patchI;
00178             }
00179         }
00180     }
00181 
00182     return -1;
00183 }
00184 
00185 
00186 Foam::scalarField Foam::coupledPolyPatch::calcFaceTol
00187 (
00188     const UList<face>& faces,
00189     const pointField& points,
00190     const pointField& faceCentres
00191 )
00192 {
00193     // Calculate typical distance per face
00194     scalarField tols(faces.size());
00195 
00196     forAll(faces, faceI)
00197     {
00198         const point& cc = faceCentres[faceI];
00199 
00200         const face& f = faces[faceI];
00201 
00202         scalar maxLenSqr = -GREAT;
00203 
00204         forAll(f, fp)
00205         {
00206             maxLenSqr = max(maxLenSqr, magSqr(points[f[fp]] - cc));
00207         }
00208         tols[faceI] = matchTol * Foam::sqrt(maxLenSqr);
00209     }
00210     return tols;
00211 }
00212 
00213 
00214 Foam::label Foam::coupledPolyPatch::getRotation
00215 (
00216     const pointField& points,
00217     const face& f,
00218     const point& anchor,
00219     const scalar tol
00220 )
00221 {
00222     label anchorFp = -1;
00223     scalar minDistSqr = GREAT;
00224 
00225     forAll(f, fp)
00226     {
00227         scalar distSqr = magSqr(anchor - points[f[fp]]);
00228 
00229         if (distSqr < minDistSqr)
00230         {
00231             minDistSqr = distSqr;
00232             anchorFp = fp;
00233         }
00234     }
00235 
00236     if (anchorFp == -1 || mag(minDistSqr) > tol)
00237     {
00238         return -1;
00239     }
00240     else
00241     {
00242         // Positive rotation
00243         return (f.size() - anchorFp) % f.size();
00244     }
00245 }
00246 
00247 
00248 void Foam::coupledPolyPatch::calcTransformTensors
00249 (
00250     const vectorField& Cf,
00251     const vectorField& Cr,
00252     const vectorField& nf,
00253     const vectorField& nr,
00254     const scalarField& smallDist,
00255     const scalar absTol
00256 ) const
00257 {
00258     if (debug)
00259     {
00260         Pout<< "coupledPolyPatch::calcTransformTensors : " << name() << endl
00261             << "    (half)size:" << Cf.size() << nl
00262             << "    absTol:" << absTol << nl
00263             //<< "    smallDist:" << smallDist << nl
00264             << "    sum(mag(nf & nr)):" << sum(mag(nf & nr)) << endl;
00265     }
00266 
00267     // Tolerance calculation.
00268     // - normal calculation: assume absTol is the absolute error in a
00269     // single normal/transformation calculation. Consists both of numerical
00270     // precision (on the order of SMALL and of writing precision
00271     // (from e.g. decomposition)
00272     // Then the overall error of summing the normals is sqrt(size())*absTol
00273     // - separation calculation: pass in from the outside an allowable error.
00274 
00275     if (size() == 0)
00276     {
00277         // Dummy geometry.
00278         separation_.setSize(0);
00279         forwardT_ = I;
00280         reverseT_ = I;
00281     }
00282     else
00283     {
00284         scalar error = absTol*Foam::sqrt(1.0*Cf.size());
00285 
00286         if (debug)
00287         {
00288             Pout<< "    error:" << error << endl;
00289         }
00290 
00291         if (sum(mag(nf & nr)) < Cf.size()-error)
00292         {
00293             // Rotation, no separation
00294 
00295             separation_.setSize(0);
00296 
00297             forwardT_.setSize(Cf.size());
00298             reverseT_.setSize(Cf.size());
00299 
00300             forAll (forwardT_, facei)
00301             {
00302                 forwardT_[facei] = rotationTensor(-nr[facei], nf[facei]);
00303                 reverseT_[facei] = rotationTensor(nf[facei], -nr[facei]);
00304             }
00305 
00306             if (debug)
00307             {
00308                 Pout<< "    sum(mag(forwardT_ - forwardT_[0])):"
00309                     << sum(mag(forwardT_ - forwardT_[0]))
00310                     << endl;
00311             }
00312 
00313             if (sum(mag(forwardT_ - forwardT_[0])) < error)
00314             {
00315                 forwardT_.setSize(1);
00316                 reverseT_.setSize(1);
00317 
00318                 if (debug)
00319                 {
00320                     Pout<< "    difference in rotation less than"
00321                         << " local tolerance "
00322                         << error << ". Assuming uniform rotation." << endl;
00323                 }
00324             }
00325         }
00326         else
00327         {
00328             forwardT_.setSize(0);
00329             reverseT_.setSize(0);
00330 
00331             separation_ = (nf&(Cr - Cf))*nf;
00332 
00333             // Three situations:
00334             // - separation is zero. No separation.
00335             // - separation is same. Single separation vector.
00336             // - separation differs per face. Separation vectorField.
00337 
00338             // Check for different separation per face
00339             bool sameSeparation = true;
00340 
00341             forAll(separation_, facei)
00342             {
00343                 scalar smallSqr = sqr(smallDist[facei]);
00344 
00345                 if (magSqr(separation_[facei] - separation_[0]) > smallSqr)
00346                 {
00347                     if (debug)
00348                     {
00349                         Pout<< "    separation " << separation_[facei]
00350                             << " at " << facei
00351                             << " differs from separation[0] " << separation_[0]
00352                             << " by more than local tolerance "
00353                             << smallDist[facei]
00354                             << ". Assuming non-uniform separation." << endl;
00355                     }
00356                     sameSeparation = false;
00357                     break;
00358                 }
00359             }
00360 
00361             if (sameSeparation)
00362             {
00363                 // Check for zero separation (at 0 so everywhere)
00364                 if (magSqr(separation_[0]) < sqr(smallDist[0]))
00365                 {
00366                     if (debug)
00367                     {
00368                         Pout<< "    separation " << mag(separation_[0])
00369                             << " less than local tolerance " << smallDist[0]
00370                             << ". Assuming zero separation." << endl;
00371                     }
00372 
00373                     separation_.setSize(0);
00374                 }
00375                 else
00376                 {
00377                     if (debug)
00378                     {
00379                         Pout<< "    separation " << mag(separation_[0])
00380                             << " more than local tolerance " << smallDist[0]
00381                             << ". Assuming uniform separation." << endl;
00382                     }
00383 
00384                     separation_.setSize(1);
00385                 }
00386             }
00387         }
00388     }
00389 
00390     if (debug)
00391     {
00392         Pout<< "    separation_:" << separation_.size() << nl
00393             << "    forwardT size:" << forwardT_.size() << endl;
00394     }
00395 }
00396 
00397 
00398 // * * * * * * * * * * * * Constructors  * * * * * * * * * * * * * * * * * * //
00399 
00400 Foam::coupledPolyPatch::coupledPolyPatch
00401 (
00402     const word& name,
00403     const label size,
00404     const label start,
00405     const label index,
00406     const polyBoundaryMesh& bm
00407 )
00408 :
00409     polyPatch(name, size, start, index, bm)
00410 {}
00411 
00412 
00413 Foam::coupledPolyPatch::coupledPolyPatch
00414 (
00415     const word& name,
00416     const dictionary& dict,
00417     const label index,
00418     const polyBoundaryMesh& bm
00419 )
00420 :
00421     polyPatch(name, dict, index, bm)
00422 {}
00423 
00424 
00425 Foam::coupledPolyPatch::coupledPolyPatch
00426 (
00427     const coupledPolyPatch& pp,
00428     const polyBoundaryMesh& bm
00429 )
00430 :
00431     polyPatch(pp, bm)
00432 {}
00433 
00434 
00435 Foam::coupledPolyPatch::coupledPolyPatch
00436 (
00437     const coupledPolyPatch& pp,
00438     const polyBoundaryMesh& bm,
00439     const label index,
00440     const label newSize,
00441     const label newStart
00442 )
00443 :
00444     polyPatch(pp, bm, index, newSize, newStart)
00445 {}
00446 
00447 
00448 // * * * * * * * * * * * * * * * * Destructor  * * * * * * * * * * * * * * * //
00449 
00450 Foam::coupledPolyPatch::~coupledPolyPatch()
00451 {}
00452 
00453 
00454 // ************************************************************************* //
Copyright © 2000-2009 OpenCFD Ltd