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

uniformSet.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 "uniformSet.H"
00028 #include "meshSearch.H"
00029 #include "DynamicList.H"
00030 #include "polyMesh.H"
00031 
00032 #include "Cloud.H"
00033 #include "passiveParticle.H"
00034 #include "IDLList.H"
00035 
00036 #include "addToRunTimeSelectionTable.H"
00037 
00038 // * * * * * * * * * * * * * * Static Data Members * * * * * * * * * * * * * //
00039 
00040 namespace Foam
00041 {
00042     defineTypeNameAndDebug(uniformSet, 0);
00043     addToRunTimeSelectionTable(sampledSet, uniformSet, word);
00044 }
00045 
00046 
00047 // * * * * * * * * * * * * * Private Member Functions  * * * * * * * * * * * //
00048 
00049 // Finds along line (samplePt + t * offset) next sample beyond or equal to
00050 // currentPt.
00051 // Updates samplePt, sampleI
00052 bool Foam::uniformSet::nextSample
00053 (
00054     const point& currentPt,
00055     const vector& offset,
00056     const scalar smallDist,
00057     point& samplePt,
00058     label& sampleI
00059 ) const
00060 {
00061     bool pointFound = false;
00062 
00063     const vector normOffset = offset/mag(offset);
00064 
00065     samplePt += offset;
00066     sampleI++;
00067 
00068     for(; sampleI < nPoints_; sampleI++)
00069     {
00070         scalar s = (samplePt - currentPt) & normOffset;
00071 
00072         if (s > -smallDist)
00073         {
00074             // samplePt is close to or beyond currentPt -> use it
00075             pointFound = true;
00076 
00077             break;
00078         }
00079         samplePt += offset;
00080     }
00081 
00082     return pointFound;
00083 }
00084 
00085 
00086 // Sample singly connected segment. Returns false if end_ reached.
00087 bool Foam::uniformSet::trackToBoundary
00088 (
00089     Particle<passiveParticle>& singleParticle,
00090     point& samplePt,
00091     label& sampleI,
00092     DynamicList<point>& samplingPts,
00093     DynamicList<label>& samplingCells,
00094     DynamicList<label>& samplingFaces,
00095     DynamicList<scalar>& samplingCurveDist
00096 ) const
00097 {
00098     // distance vector between sampling points
00099     const vector offset = (end_ - start_)/(nPoints_ - 1);
00100     const vector smallVec = tol*offset;
00101     const scalar smallDist = mag(smallVec);
00102 
00103     // Alias
00104     const point& trackPt = singleParticle.position();
00105 
00106     while(true)
00107     {
00108         // Find next samplePt on/after trackPt. Update samplePt, sampleI
00109         if (!nextSample(trackPt, offset, smallDist, samplePt, sampleI))
00110         {
00111             // no more samples.
00112             if (debug)
00113             {
00114                 Info<< "trackToBoundary : Reached end : samplePt now:"
00115                     << samplePt << "  sampleI now:" << sampleI << endl;
00116             }
00117             return false;
00118         }
00119 
00120         if (mag(samplePt - trackPt) < smallDist)
00121         {
00122             // trackPt corresponds with samplePt. Store and use next samplePt
00123             if (debug)
00124             {
00125                 Info<< "trackToBoundary : samplePt corresponds to trackPt : "
00126                     << "  trackPt:" << trackPt << "  samplePt:" << samplePt
00127                     << endl;
00128             }
00129 
00130             samplingPts.append(trackPt);
00131             samplingCells.append(singleParticle.cell());
00132             samplingFaces.append(-1);
00133             samplingCurveDist.append(mag(trackPt - start_));
00134 
00135             // go to next samplePt
00136             if (!nextSample(trackPt, offset, smallDist, samplePt, sampleI))
00137             {
00138                 // no more samples.
00139                 if (debug)
00140                 {
00141                     Info<< "trackToBoundary : Reached end : "
00142                         << "  samplePt now:" << samplePt
00143                         << "  sampleI now:" << sampleI
00144                         << endl;
00145                 }
00146 
00147                 return false;
00148             }
00149         }
00150 
00151 
00152         if (debug)
00153         {
00154             Info<< "Searching along trajectory from "
00155                 << "  trackPt:" << trackPt
00156                 << "  trackCellI:" << singleParticle.cell()
00157                 << "  to:" << samplePt << endl;
00158         }
00159 
00160         point oldPos = trackPt;
00161         label facei = -1;
00162         do
00163         {
00164             singleParticle.stepFraction() = 0;
00165             singleParticle.track(samplePt);
00166 
00167             if (debug)
00168             {
00169                 Info<< "Result of tracking "
00170                     << "  trackPt:" << trackPt
00171                     << "  trackCellI:" << singleParticle.cell()
00172                     << "  trackFaceI:" << singleParticle.face()
00173                     << "  onBoundary:" << singleParticle.onBoundary()
00174                     << "  samplePt:" << samplePt
00175                     << "  smallDist:" << smallDist
00176                     << endl;
00177             }
00178         }
00179         while
00180         (
00181             !singleParticle.onBoundary()
00182          && (mag(trackPt - oldPos) < smallDist)
00183         );
00184 
00185         if (singleParticle.onBoundary())
00186         {
00187             //Info<< "trackToBoundary : reached boundary" << endl;
00188             if (mag(trackPt - samplePt) < smallDist)
00189             {
00190                 //Info<< "trackToBoundary : boundary is also sampling point"
00191                 //    << endl;
00192                 // Reached samplePt on boundary
00193                 samplingPts.append(trackPt);
00194                 samplingCells.append(singleParticle.cell());
00195                 samplingFaces.append(facei);
00196                 samplingCurveDist.append(mag(trackPt - start_));
00197             }
00198 
00199             return true;
00200         }
00201 
00202         //Info<< "trackToBoundary : reached internal sampling point" << endl;
00203         // Reached samplePt in cell or on internal face
00204         samplingPts.append(trackPt);
00205         samplingCells.append(singleParticle.cell());
00206         samplingFaces.append(-1);
00207         samplingCurveDist.append(mag(trackPt - start_));
00208 
00209         // go to next samplePt
00210     }
00211 }
00212 
00213 
00214 void Foam::uniformSet::calcSamples
00215 (
00216     DynamicList<point>& samplingPts,
00217     DynamicList<label>& samplingCells,
00218     DynamicList<label>& samplingFaces,
00219     DynamicList<label>& samplingSegments,
00220     DynamicList<scalar>& samplingCurveDist
00221 ) const
00222 {
00223     // distance vector between sampling points
00224     if ((nPoints_ < 2) || (mag(end_ - start_) < SMALL))
00225     {
00226         FatalErrorIn("uniformSet::calcSamples()")
00227             << "Incorrect sample specification. Either too few points or"
00228             << " start equals end point." << endl
00229             << "nPoints:" << nPoints_
00230             << "  start:" << start_
00231             << "  end:" << end_
00232             << exit(FatalError);
00233     }
00234 
00235     const vector offset = (end_ - start_)/(nPoints_ - 1);
00236     const vector normOffset = offset/mag(offset);
00237     const vector smallVec = tol*offset;
00238     const scalar smallDist = mag(smallVec);
00239 
00240     // Get all boundary intersections
00241     List<pointIndexHit> bHits = searchEngine().intersections
00242     (
00243         start_ - smallVec,
00244         end_ + smallVec
00245     );
00246 
00247     point bPoint(GREAT, GREAT, GREAT);
00248     label bFaceI = -1;
00249 
00250     if (bHits.size())
00251     {
00252         bPoint = bHits[0].hitPoint();
00253         bFaceI = bHits[0].index();
00254     }
00255 
00256     // Get first tracking point. Use bPoint, bFaceI if provided.
00257 
00258     point trackPt;
00259     label trackCellI = -1;
00260     label trackFaceI = -1;
00261 
00262     bool isSample =
00263         getTrackingPoint
00264         (
00265             offset,
00266             start_,
00267             bPoint,
00268             bFaceI,
00269 
00270             trackPt,
00271             trackCellI,
00272             trackFaceI
00273         );
00274 
00275     if (trackCellI == -1)
00276     {
00277         // Line start_ - end_ does not intersect domain at all.
00278         // (or is along edge)
00279         // Set points and cell/face labels to empty lists
00280 
00281         return;
00282     }
00283 
00284     if (isSample)
00285     {
00286         samplingPts.append(start_);
00287         samplingCells.append(trackCellI);
00288         samplingFaces.append(trackFaceI);
00289         samplingCurveDist.append(0.0);
00290     }
00291 
00292     //
00293     // Track until hit end of all boundary intersections
00294     //
00295 
00296     // current segment number
00297     label segmentI = 0;
00298 
00299     // starting index of current segment in samplePts
00300     label startSegmentI = 0;
00301 
00302     label sampleI = 0;
00303     point samplePt = start_;
00304 
00305     // index in bHits; current boundary intersection
00306     label bHitI = 1;
00307 
00308     while(true)
00309     {
00310         // Initialize tracking starting from trackPt
00311         Cloud<passiveParticle> particles(mesh(), IDLList<passiveParticle>());
00312 
00313         passiveParticle singleParticle
00314         (
00315             particles,
00316             trackPt,
00317             trackCellI
00318         );
00319 
00320         bool reachedBoundary = trackToBoundary
00321         (
00322             singleParticle,
00323             samplePt,
00324             sampleI,
00325             samplingPts,
00326             samplingCells,
00327             samplingFaces,
00328             samplingCurveDist
00329         );
00330 
00331         // fill sampleSegments
00332         for(label i = samplingPts.size() - 1; i >= startSegmentI; --i)
00333         {
00334             samplingSegments.append(segmentI);
00335         }
00336 
00337 
00338         if (!reachedBoundary)
00339         {
00340             if (debug)
00341             {
00342                 Info<< "calcSamples : Reached end of samples: "
00343                     << "  samplePt now:" << samplePt
00344                     << "  sampleI now:" << sampleI
00345                     << endl;
00346             }
00347             break;
00348         }
00349 
00350 
00351         bool foundValidB = false;
00352 
00353         while (bHitI < bHits.size())
00354         {
00355             scalar dist =
00356                 (bHits[bHitI].hitPoint() - singleParticle.position())
00357               & normOffset;
00358 
00359             if (debug)
00360             {
00361                 Info<< "Finding next boundary : "
00362                     << "bPoint:" << bHits[bHitI].hitPoint()
00363                     << "  tracking:" << singleParticle.position()
00364                     << "  dist:" << dist
00365                     << endl;
00366             }
00367 
00368             if (dist > smallDist)
00369             {
00370                 // hitpoint is past tracking position
00371                 foundValidB = true;
00372                 break;
00373             }
00374             else
00375             {
00376                 bHitI++;
00377             }
00378         }
00379 
00380         if (!foundValidB)
00381         {
00382             // No valid boundary intersection found beyond tracking position
00383             break;
00384         }
00385 
00386         // Update starting point for tracking
00387         trackFaceI = bFaceI;
00388         trackPt = pushIn(bPoint, trackFaceI);
00389         trackCellI = getBoundaryCell(trackFaceI);
00390 
00391         segmentI++;
00392 
00393         startSegmentI = samplingPts.size();
00394     }
00395 }
00396 
00397 
00398 void Foam::uniformSet::genSamples()
00399 {
00400     // Storage for sample points
00401     DynamicList<point> samplingPts;
00402     DynamicList<label> samplingCells;
00403     DynamicList<label> samplingFaces;
00404     DynamicList<label> samplingSegments;
00405     DynamicList<scalar> samplingCurveDist;
00406 
00407     calcSamples
00408     (
00409         samplingPts,
00410         samplingCells,
00411         samplingFaces,
00412         samplingSegments,
00413         samplingCurveDist
00414     );
00415 
00416     samplingPts.shrink();
00417     samplingCells.shrink();
00418     samplingFaces.shrink();
00419     samplingSegments.shrink();
00420     samplingCurveDist.shrink();
00421 
00422     setSamples
00423     (
00424         samplingPts,
00425         samplingCells,
00426         samplingFaces,
00427         samplingSegments,
00428         samplingCurveDist
00429     );
00430 }
00431 
00432 // * * * * * * * * * * * * * * * * Constructors  * * * * * * * * * * * * * * //
00433 
00434 Foam::uniformSet::uniformSet
00435 (
00436     const word& name,
00437     const polyMesh& mesh,
00438     meshSearch& searchEngine,
00439     const word& axis,
00440     const point& start,
00441     const point& end,
00442     const label nPoints
00443 )
00444 :
00445     sampledSet(name, mesh, searchEngine, axis),
00446     start_(start),
00447     end_(end),
00448     nPoints_(nPoints)
00449 {
00450     genSamples();
00451 
00452     if (debug)
00453     {
00454         write(Info);
00455     }
00456 }
00457 
00458 
00459 Foam::uniformSet::uniformSet
00460 (
00461     const word& name,
00462     const polyMesh& mesh,
00463     meshSearch& searchEngine,
00464     const dictionary& dict
00465 )
00466 :
00467     sampledSet(name, mesh, searchEngine, dict),
00468     start_(dict.lookup("start")),
00469     end_(dict.lookup("end")),
00470     nPoints_(readLabel(dict.lookup("nPoints")))
00471 {
00472     genSamples();
00473 
00474     if (debug)
00475     {
00476         write(Info);
00477     }
00478 }
00479 
00480 
00481 // * * * * * * * * * * * * * * * * Destructor  * * * * * * * * * * * * * * * //
00482 
00483 Foam::uniformSet::~uniformSet()
00484 {}
00485 
00486 
00487 // * * * * * * * * * * * * * * * Member Functions  * * * * * * * * * * * * * //
00488 
00489 
00490 Foam::point Foam::uniformSet::getRefPoint(const List<point>& pts) const
00491 {
00492     // Use start point as reference for 'distance'
00493     return start_;
00494 }
00495 
00496 
00497 // ************************************************************************* //
Copyright © 2000-2009 OpenCFD Ltd