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

sampledSet.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 "sampledSet.H"
00028 #include "polyMesh.H"
00029 #include "primitiveMesh.H"
00030 #include "meshSearch.H"
00031 #include "writer.H"
00032 
00033 // * * * * * * * * * * * * * * Static Data Members * * * * * * * * * * * * * //
00034 
00035 namespace Foam
00036 {
00037     const scalar sampledSet::tol = 1e-6;
00038 
00039     defineTypeNameAndDebug(sampledSet, 0);
00040     defineRunTimeSelectionTable(sampledSet, word);
00041 }
00042 
00043 
00044 // * * * * * * * * * * * * * Private Member Functions  * * * * * * * * * * * //
00045 
00046 Foam::label Foam::sampledSet::getBoundaryCell(const label faceI) const
00047 {
00048     return mesh().faceOwner()[faceI];
00049 }
00050 
00051 
00052 Foam::label Foam::sampledSet::getCell
00053 (
00054     const label faceI,
00055     const point& sample
00056 ) const
00057 {
00058     if (faceI == -1)
00059     {
00060         FatalErrorIn
00061         (
00062             "sampledSet::getCell(const label, const point&)"
00063         )   << "Illegal face label " << faceI
00064             << abort(FatalError);
00065     }
00066 
00067     if (faceI >= mesh().nInternalFaces())
00068     {
00069         label cellI = getBoundaryCell(faceI);
00070 
00071         if (!mesh().pointInCell(sample, cellI))
00072         {
00073             FatalErrorIn
00074             (
00075                 "sampledSet::getCell(const label, const point&)"
00076             )   << "Found cell " << cellI << " using face " << faceI
00077                 << ". But cell does not contain point " << sample
00078                 << abort(FatalError);
00079         }
00080         return cellI;
00081     }
00082     else
00083     {
00084         // Try owner and neighbour to see which one contains sample
00085 
00086         label cellI = mesh().faceOwner()[faceI];
00087 
00088         if (mesh().pointInCell(sample, cellI))
00089         {
00090             return cellI;
00091         }
00092         else
00093         {
00094             cellI = mesh().faceNeighbour()[faceI];
00095 
00096             if (mesh().pointInCell(sample, cellI))
00097             {
00098                 return cellI;
00099             }
00100             else
00101             {
00102                 FatalErrorIn
00103                 (
00104                     "sampledSet::getCell(const label, const point&)"
00105                 )   << "None of the neighbours of face "
00106                     << faceI << " contains point " << sample
00107                     << abort(FatalError);
00108 
00109                 return -1;
00110             }
00111         }
00112     }
00113 }
00114 
00115 
00116 Foam::scalar Foam::sampledSet::calcSign
00117 (
00118     const label faceI,
00119     const point& sample
00120 ) const
00121 {
00122     vector vec = sample - mesh().faceCentres()[faceI];
00123 
00124     scalar magVec = mag(vec);
00125 
00126     if (magVec < VSMALL)
00127     {
00128         // sample on face centre. Regard as inside
00129         return -1;
00130     }
00131 
00132     vec /= magVec;
00133 
00134     vector n = mesh().faceAreas()[faceI];
00135 
00136     n /= mag(n) + VSMALL;
00137 
00138     return n & vec;
00139 }
00140 
00141 
00142 // Return face (or -1) of face which is within smallDist of sample
00143 Foam::label Foam::sampledSet::findNearFace
00144 (
00145     const label cellI,
00146     const point& sample,
00147     const scalar smallDist
00148 ) const
00149 {
00150     const cell& myFaces = mesh().cells()[cellI];
00151 
00152     forAll(myFaces, myFaceI)
00153     {
00154         const face& f = mesh().faces()[myFaces[myFaceI]];
00155 
00156         pointHit inter = f.nearestPoint(sample, mesh().points());
00157 
00158         scalar dist;
00159 
00160         if (inter.hit())
00161         {
00162             dist = mag(inter.hitPoint() - sample);
00163         }
00164         else
00165         {
00166             dist = mag(inter.missPoint() - sample);
00167         }
00168 
00169         if (dist < smallDist)
00170         {
00171             return myFaces[myFaceI];
00172         }
00173     }
00174     return -1;
00175 }
00176 
00177 
00178 // 'Pushes' point facePt (which is almost on face) in direction of cell centre
00179 // so it is clearly inside.
00180 Foam::point Foam::sampledSet::pushIn
00181 (
00182     const point& facePt,
00183     const label faceI
00184 ) const
00185 {
00186     label cellI = mesh().faceOwner()[faceI];
00187 
00188     const point& cellCtr = mesh().cellCentres()[cellI];
00189 
00190     point newSample =
00191         facePt + tol*(cellCtr - facePt);
00192 
00193     if (!searchEngine().pointInCell(newSample, cellI))
00194     {
00195         FatalErrorIn
00196         (
00197             "sampledSet::pushIn(const point&, const label)"
00198         )   << "After pushing " << facePt << " to " << newSample
00199             << " it is still outside faceI " << faceI << endl
00200             << "Please change your starting point"
00201             << abort(FatalError);
00202     }
00203     //Info<< "pushIn : moved " << facePt << " to " << newSample
00204     //    << endl;
00205 
00206     return newSample;
00207 }
00208 
00209 
00210 // Calculates start of tracking given samplePt and first boundary intersection
00211 // (bPoint, bFaceI). bFaceI == -1 if no boundary intersection.
00212 // Returns true if trackPt is sampling point
00213 bool Foam::sampledSet::getTrackingPoint
00214 (
00215     const vector& offset,
00216     const point& samplePt,
00217     const point& bPoint,
00218     const label bFaceI,
00219 
00220     point& trackPt,
00221     label& trackCellI,
00222     label& trackFaceI
00223 ) const
00224 {
00225     const scalar smallDist = mag(tol*offset);
00226 
00227     bool isGoodSample = false;
00228 
00229     if (bFaceI == -1)
00230     {
00231         // No boundary intersection. Try and find cell samplePt is in
00232         trackCellI = mesh().findCell(samplePt);
00233 
00234         if
00235         (
00236             (trackCellI == -1)
00237          || !mesh().pointInCell(samplePt, trackCellI)
00238         )
00239         {
00240             // Line samplePt - end_ does not intersect domain at all.
00241             // (or is along edge)
00242             //Info<< "getTrackingPoint : samplePt outside domain : "
00243             //    << "  samplePt:" << samplePt
00244             //    << endl;
00245 
00246             trackCellI = -1;
00247             trackFaceI = -1;
00248 
00249             isGoodSample = false;
00250         }
00251         else
00252         {
00253             // start is inside. Use it as tracking point
00254             //Info<< "getTrackingPoint : samplePt inside :"
00255             //    << "  samplePt:" << samplePt
00256             //    << "  trackCellI:" << trackCellI
00257             //    << endl;
00258 
00259             trackPt = samplePt;
00260             trackFaceI = -1;
00261 
00262             isGoodSample = true;
00263         }
00264     }
00265     else if (mag(samplePt - bPoint) < smallDist)
00266     {
00267         //Info<< "getTrackingPoint : samplePt:" << samplePt
00268         //    << " close to bPoint:"
00269         //    << bPoint << endl;
00270 
00271         // samplePt close to bPoint. Snap to it
00272         trackPt = pushIn(bPoint, bFaceI);
00273         trackFaceI = bFaceI;
00274         trackCellI = getBoundaryCell(trackFaceI);
00275 
00276         isGoodSample = true;
00277     }
00278     else
00279     {
00280         scalar sign = calcSign(bFaceI, samplePt);
00281 
00282         if (sign < 0)
00283         {
00284             // samplePt inside or marginally outside.
00285             trackPt = samplePt;
00286             trackFaceI = -1;
00287             trackCellI = mesh().findCell(trackPt);
00288 
00289             isGoodSample = true;
00290         }
00291         else
00292         {
00293             // samplePt outside. use bPoint
00294             trackPt = pushIn(bPoint, bFaceI);
00295             trackFaceI = bFaceI;
00296             trackCellI = getBoundaryCell(trackFaceI);
00297 
00298             isGoodSample = false;
00299         }
00300     }
00301 
00302     if (debug)
00303     {
00304         Info<< "sampledSet::getTrackingPoint :"
00305             << " offset:" << offset
00306             << " samplePt:" << samplePt
00307             << " bPoint:" << bPoint
00308             << " bFaceI:" << bFaceI
00309             << endl << "   Calculated first tracking point :"
00310             << " trackPt:" << trackPt
00311             << " trackCellI:" << trackCellI
00312             << " trackFaceI:" << trackFaceI
00313             << " isGoodSample:" << isGoodSample
00314             << endl;
00315     }
00316 
00317     return isGoodSample;
00318 }
00319 
00320 
00321 void Foam::sampledSet::setSamples
00322 (
00323     const List<point>& samplingPts,
00324     const labelList& samplingCells,
00325     const labelList& samplingFaces,
00326     const labelList& samplingSegments,
00327     const scalarList& samplingCurveDist
00328 )
00329 {
00330     setSize(samplingPts.size());
00331     cells_.setSize(samplingCells.size());
00332     faces_.setSize(samplingFaces.size());
00333     segments_.setSize(samplingSegments.size());
00334     curveDist_.setSize(samplingCurveDist.size());
00335 
00336     if
00337     (
00338         (cells_.size() != size())
00339      || (faces_.size() != size())
00340      || (segments_.size() != size())
00341      || (curveDist_.size() != size())
00342     )
00343     {
00344         FatalErrorIn("sampledSet::setSamples()")
00345             << "sizes not equal : "
00346             << "  points:" << size()
00347             << "  cells:" << cells_.size()
00348             << "  faces:" << faces_.size()
00349             << "  segments:" << segments_.size()
00350             << "  curveDist:" << curveDist_.size()
00351             << abort(FatalError);
00352     }
00353 
00354     forAll(samplingPts, sampleI)
00355     {
00356         operator[](sampleI) = samplingPts[sampleI];
00357     }
00358     cells_ = samplingCells;
00359     faces_ = samplingFaces;
00360     segments_ = samplingSegments;
00361     curveDist_ = samplingCurveDist;
00362 }
00363 
00364 
00365 // * * * * * * * * * * * * * * * * Constructors  * * * * * * * * * * * * * * //
00366 
00367 Foam::sampledSet::sampledSet
00368 (
00369     const word& name,
00370     const polyMesh& mesh,
00371     meshSearch& searchEngine,
00372     const word& axis
00373 )
00374 :
00375     coordSet(name, axis),
00376     mesh_(mesh),
00377     searchEngine_(searchEngine),
00378     segments_(0),
00379     curveDist_(0),
00380     cells_(0),
00381     faces_(0)
00382 {}
00383 
00384 
00385 Foam::sampledSet::sampledSet
00386 (
00387     const word& name,
00388     const polyMesh& mesh,
00389     meshSearch& searchEngine,
00390     const dictionary& dict
00391 )
00392 :
00393     coordSet(name, dict.lookup("axis")),
00394     mesh_(mesh),
00395     searchEngine_(searchEngine),
00396     segments_(0),
00397     curveDist_(0),
00398     cells_(0),
00399     faces_(0)
00400 {}
00401 
00402 
00403 // * * * * * * * * * * * * * * * * Destructor  * * * * * * * * * * * * * * * //
00404 
00405 Foam::sampledSet::~sampledSet()
00406 {}
00407 
00408 
00409 // * * * * * * * * * * * * * * * Member Functions  * * * * * * * * * * * * * //
00410 
00411 Foam::autoPtr<Foam::sampledSet> Foam::sampledSet::New
00412 (
00413     const word& name,
00414     const polyMesh& mesh,
00415     meshSearch& searchEngine,
00416     const dictionary& dict
00417 )
00418 {
00419     word sampleType(dict.lookup("type"));
00420 
00421     wordConstructorTable::iterator cstrIter =
00422         wordConstructorTablePtr_->find(sampleType);
00423 
00424     if (cstrIter == wordConstructorTablePtr_->end())
00425     {
00426         FatalErrorIn
00427         (
00428             "sampledSet::New(const word&, "
00429             "const polyMesh&, meshSearch&, const dictionary&)"
00430         )   << "Unknown sample type " << sampleType
00431             << endl << endl
00432             << "Valid sample types : " << endl
00433             << wordConstructorTablePtr_->toc()
00434             << exit(FatalError);
00435     }
00436 
00437     return autoPtr<sampledSet>
00438     (
00439         cstrIter()
00440         (
00441             name,
00442             mesh,
00443             searchEngine,
00444             dict
00445         )
00446     );
00447 }
00448 
00449 
00450 Foam::Ostream& Foam::sampledSet::write(Ostream& os) const
00451 {
00452     coordSet::write(os);
00453 
00454     os  << endl << "\t(cellI)\t(faceI)" << endl;
00455 
00456     forAll(*this, sampleI)
00457     {
00458         os  << '\t' << cells_[sampleI]
00459             << '\t' << faces_[sampleI]
00460             << endl;
00461     }
00462 
00463     return os;
00464 }
00465 
00466 
00467 // ************************************************************************* //
Copyright © 2000-2009 OpenCFD Ltd