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

interactionLists.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) 2008-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 "interactionLists.H"
00028 
00029 // * * * * * * * * * * * * * * Static Data Members * * * * * * * * * * * * * //
00030 
00031 Foam::scalar Foam::interactionLists::transTol = 1e-12;
00032 
00033 
00034 // * * * * * * * * * * * * * Private Member Functions  * * * * * * * * * * * //
00035 
00036 void Foam::interactionLists::buildCellReferralLists()
00037 {
00038     Info<< nl << "Determining molecule referring schedule" << endl;
00039 
00040     const referredCellList& refIntL(ril());
00041 
00042     DynamicList<label> referralProcs;
00043 
00044     // Run through all referredCells to build list of interacting processors
00045 
00046     forAll(refIntL, rIL)
00047     {
00048         const referredCell& rC(refIntL[rIL]);
00049 
00050         if (findIndex(referralProcs, rC.sourceProc()) == -1)
00051         {
00052             referralProcs.append(rC.sourceProc());
00053         }
00054     }
00055 
00056     referralProcs.shrink();
00057 
00058 //     Pout << "referralProcs: " << nl << referralProcs << endl;
00059 
00060     List<DynamicList<label> > cellSendingReferralLists(referralProcs.size());
00061 
00062     List<DynamicList<DynamicList<label> > >
00063         cellReceivingReferralLists(referralProcs.size());
00064 
00065     // Run through all referredCells again building up send and receive info
00066 
00067     forAll(refIntL, rIL)
00068     {
00069         const referredCell& rC(refIntL[rIL]);
00070 
00071         label rPI = findIndex(referralProcs, rC.sourceProc());
00072 
00073         DynamicList<DynamicList<label> >& rRL(cellReceivingReferralLists[rPI]);
00074 
00075         DynamicList<label>& sRL(cellSendingReferralLists[rPI]);
00076 
00077         label existingSource = findIndex(sRL, rC.sourceCell());
00078 
00079         // Check to see if this source cell has already been allocated to
00080         // come to this processor.  If not, add the source cell to the sending
00081         // list and add the current referred cell to the receiving list.
00082 
00083         // It shouldn't be possible for the sending and receiving lists to be
00084         // different lengths, because their append operations happen at the
00085         // same time.
00086 
00087         if (existingSource == -1)
00088         {
00089             sRL.append(rC.sourceCell());
00090 
00091             rRL.append
00092             (
00093                 DynamicList<label> (labelList(1,rIL))
00094             );
00095         }
00096         else
00097         {
00098             rRL[existingSource].append(rIL);
00099 
00100             rRL[existingSource].shrink();
00101         }
00102     }
00103 
00104     forAll(referralProcs, rPI)
00105     {
00106         DynamicList<DynamicList<label> >& rRL(cellReceivingReferralLists[rPI]);
00107 
00108         DynamicList<label>& sRL(cellSendingReferralLists[rPI]);
00109 
00110         sRL.shrink();
00111 
00112         rRL.shrink();
00113     }
00114 
00115     // It is assumed that cell exchange is reciprocal, if proc A has cells to
00116     // send to proc B, then proc B must have some to send to proc A.
00117 
00118     cellReceivingReferralLists_.setSize(referralProcs.size());
00119 
00120     cellSendingReferralLists_.setSize(referralProcs.size());
00121 
00122     forAll(referralProcs, rPI)
00123     {
00124         DynamicList<DynamicList<label> >& rRL(cellReceivingReferralLists[rPI]);
00125 
00126         labelListList translLL(rRL.size());
00127 
00128         forAll(rRL, rRLI)
00129         {
00130             translLL[rRLI] = rRL[rRLI];
00131         }
00132 
00133         cellReceivingReferralLists_[rPI] = receivingReferralList
00134         (
00135             referralProcs[rPI],
00136             translLL
00137         );
00138     }
00139 
00140     // Send sendingReferralLists to each interacting processor.
00141 
00142     forAll(referralProcs, rPI)
00143     {
00144 
00145         DynamicList<label>& sRL(cellSendingReferralLists[rPI]);
00146 
00147         if (referralProcs[rPI] != Pstream::myProcNo())
00148         {
00149             if (Pstream::parRun())
00150             {
00151                 OPstream toInteractingProc
00152                 (
00153                     Pstream::blocking,
00154                     referralProcs[rPI]
00155                 );
00156 
00157                 toInteractingProc << sendingReferralList
00158                 (
00159                     Pstream::myProcNo(),
00160                     sRL
00161                 );
00162             }
00163         }
00164     }
00165 
00166     // Receive sendingReferralLists from each interacting processor.
00167 
00168     forAll(referralProcs, rPI)
00169     {
00170         if (referralProcs[rPI] != Pstream::myProcNo())
00171         {
00172             if (Pstream::parRun())
00173             {
00174                 IPstream fromInteractingProc
00175                 (
00176                     Pstream::blocking,
00177                     referralProcs[rPI]
00178                 );
00179 
00180                 fromInteractingProc >> cellSendingReferralLists_[rPI];
00181             }
00182         }
00183         else
00184         {
00185             cellSendingReferralLists_[rPI] = sendingReferralList
00186             (
00187                 Pstream::myProcNo(),
00188                 cellSendingReferralLists[rPI]
00189             );
00190         }
00191     }
00192 }
00193 
00194 
00195 // * * * * * * * * * * * * * * * * Constructors  * * * * * * * * * * * * * * //
00196 
00197 
00198 Foam::interactionLists::interactionLists
00199 (
00200     const polyMesh& mesh,
00201     scalar rCutMaxSqr,
00202     bool pointPointListBuild
00203 )
00204 :
00205     mesh_(mesh),
00206     rCutMaxSqr_(rCutMaxSqr),
00207     dil_(*this, pointPointListBuild),
00208     ril_(*this, pointPointListBuild),
00209     cellSendingReferralLists_(),
00210     cellReceivingReferralLists_()
00211 {
00212     buildCellReferralLists();
00213 }
00214 
00215 
00216 Foam::interactionLists::interactionLists(const polyMesh& mesh)
00217 :
00218     mesh_(mesh),
00219     dil_(*this),
00220     ril_(*this)
00221 {}
00222 
00223 
00224 // * * * * * * * * * * * * * * * * Destructor  * * * * * * * * * * * * * * * //
00225 
00226 Foam::interactionLists::~interactionLists()
00227 {}
00228 
00229 
00230 // * * * * * * * * * * * * * * * Member Functions  * * * * * * * * * * * * * //
00231 
00232 bool Foam::interactionLists::testPointPointDistance
00233 (
00234     const label ptI,
00235     const label ptJ
00236 ) const
00237 {
00238     return (magSqr(mesh_.points()[ptI] - mesh_.points()[ptJ]) <= rCutMaxSqr_);
00239 }
00240 
00241 
00242 bool Foam::interactionLists::testEdgeEdgeDistance
00243 (
00244     const edge& eI,
00245     const edge& eJ
00246 ) const
00247 {
00248     const vector& eJs(mesh_.points()[eJ.start()]);
00249     const vector& eJe(mesh_.points()[eJ.end()]);
00250 
00251     return testEdgeEdgeDistance(eI, eJs, eJe);
00252 }
00253 
00254 
00255 bool Foam::interactionLists::testPointFaceDistance
00256 (
00257     const label p,
00258     const label faceNo
00259 ) const
00260 {
00261     const vector& pointPosition(mesh_.points()[p]);
00262 
00263     return testPointFaceDistance(pointPosition, faceNo);
00264 }
00265 
00266 
00267 bool Foam::interactionLists::testPointFaceDistance
00268 (
00269     const label p,
00270     const referredCell& refCell
00271 ) const
00272 {
00273     const vector& pointPosition(mesh_.points()[p]);
00274 
00275     forAll (refCell.faces(), rCF)
00276     {
00277         if
00278         (
00279             testPointFaceDistance
00280             (
00281                 pointPosition,
00282                 refCell.faces()[rCF],
00283                 refCell.vertexPositions(),
00284                 refCell.faceCentres()[rCF],
00285                 refCell.faceAreas()[rCF]
00286             )
00287         )
00288         {
00289             return true;
00290         }
00291     }
00292 
00293     return false;
00294 }
00295 
00296 
00297 bool Foam::interactionLists::testPointFaceDistance
00298 (
00299     const vectorList& pointsToTest,
00300     const label faceNo
00301 ) const
00302 {
00303     forAll(pointsToTest, pTT)
00304     {
00305         const vector& p(pointsToTest[pTT]);
00306 
00307         // if any point in the list is in range of the face
00308         // then the rest do not need to be tested and
00309         // true can be returned
00310 
00311         if (testPointFaceDistance(p, faceNo))
00312         {
00313             return true;
00314         }
00315     }
00316 
00317     return false;
00318 }
00319 
00320 
00321 bool Foam::interactionLists::testPointFaceDistance
00322 (
00323     const vector& p,
00324     const label faceNo
00325 ) const
00326 {
00327     const face& faceToTest(mesh_.faces()[faceNo]);
00328 
00329     const vector& faceC(mesh_.faceCentres()[faceNo]);
00330 
00331     const vector& faceA(mesh_.faceAreas()[faceNo]);
00332 
00333     const vectorList& points(mesh_.points());
00334 
00335     return testPointFaceDistance
00336     (
00337         p,
00338         faceToTest,
00339         points,
00340         faceC,
00341         faceA
00342     );
00343 }
00344 
00345 
00346 bool Foam::interactionLists::testPointFaceDistance
00347 (
00348     const vector& p,
00349     const labelList& faceToTest,
00350     const vectorList& points,
00351     const vector& faceC,
00352     const vector& faceA
00353 ) const
00354 {
00355     vector faceN(faceA/mag(faceA));
00356 
00357     scalar perpDist((p - faceC) & faceN);
00358 
00359     if (magSqr(perpDist) > rCutMaxSqr_)
00360     {
00361         return false;
00362     }
00363 
00364     vector pointOnPlane = (p - faceN * perpDist);
00365 
00366     if (magSqr(faceC - pointOnPlane) < rCutMaxSqr_*1e-8)
00367     {
00368         // If pointOnPlane is very close to the face centre
00369         // then defining the local axes will be inaccurate
00370         // and it is very likely that pointOnPlane will be
00371         // inside the face, so return true if the points
00372         // are in range to be safe
00373 
00374         return (magSqr(pointOnPlane - p) <= rCutMaxSqr_);
00375     }
00376 
00377     vector xAxis = (faceC - pointOnPlane)/mag(faceC - pointOnPlane);
00378 
00379     vector yAxis =
00380         ((faceC - pointOnPlane) ^ faceN)
00381        /mag((faceC - pointOnPlane) ^ faceN);
00382 
00383     List<vector2D> local2DVertices(faceToTest.size());
00384 
00385     forAll(faceToTest, fTT)
00386     {
00387         const vector& V(points[faceToTest[fTT]]);
00388 
00389         if (magSqr(V-p) <= rCutMaxSqr_)
00390         {
00391             return true;
00392         }
00393 
00394         local2DVertices[fTT] = vector2D
00395         (
00396             ((V - pointOnPlane) & xAxis),
00397             ((V - pointOnPlane) & yAxis)
00398         );
00399     }
00400 
00401     scalar localFaceCx((faceC - pointOnPlane) & xAxis);
00402 
00403     scalar la_valid = -1;
00404 
00405     forAll(local2DVertices, fV)
00406     {
00407         const vector2D& va(local2DVertices[fV]);
00408 
00409         const vector2D& vb
00410         (
00411             local2DVertices[(fV + 1) % local2DVertices.size()]
00412         );
00413 
00414         if (mag(vb.y()-va.y()) > SMALL)
00415         {
00416             scalar la =
00417                 (
00418                     va.x() - va.y()*((vb.x() - va.x())/(vb.y() - va.y()))
00419                 )
00420                /localFaceCx;
00421 
00422             scalar lv = -va.y()/(vb.y() - va.y());
00423 
00424 
00425             if (la >= 0 && la <= 1 && lv >= 0 && lv <= 1)
00426             {
00427                 la_valid = la;
00428 
00429                 break;
00430             }
00431         }
00432     }
00433 
00434     if (la_valid < 0)
00435     {
00436         // perpendicular point inside face, nearest point is pointOnPlane;
00437         return (magSqr(pointOnPlane-p) <= rCutMaxSqr_);
00438     }
00439     else
00440     {
00441         // perpendicular point outside face, nearest point is
00442         // on edge that generated la_valid;
00443         return
00444         (
00445             magSqr(pointOnPlane + la_valid*(faceC - pointOnPlane) - p)
00446          <= rCutMaxSqr_
00447         );
00448     }
00449 
00450     // if the algorithm hasn't returned anything by now then something has
00451     // gone wrong.
00452 
00453     FatalErrorIn("interactionLists.C") << nl
00454         << "point " << p << " to face " << faceToTest
00455         << " comparison did not find a nearest point"
00456         << " to be inside or outside face."
00457         << abort(FatalError);
00458 
00459     return false;
00460 }
00461 
00462 
00463 bool Foam::interactionLists::testEdgeEdgeDistance
00464 (
00465     const edge& eI,
00466     const vector& eJs,
00467     const vector& eJe
00468 ) const
00469 {
00470     vector a(eI.vec(mesh_.points()));
00471     vector b(eJe - eJs);
00472 
00473     const vector& eIs(mesh_.points()[eI.start()]);
00474 
00475     vector c(eJs - eIs);
00476 
00477     vector crossab = a ^ b;
00478     scalar magCrossSqr = magSqr(crossab);
00479 
00480     if (magCrossSqr > VSMALL)
00481     {
00482         // If the edges are parallel then a point-face
00483         // search will pick them up
00484 
00485         scalar s = ((c ^ b) & crossab)/magCrossSqr;
00486         scalar t = ((c ^ a) & crossab)/magCrossSqr;
00487 
00488         // Check for end points outside of range 0..1
00489         // If the closest point is outside this range
00490         // a point-face search will have found it.
00491 
00492         return
00493         (
00494             s >= 0
00495          && s <= 1
00496          && t >= 0
00497          && t <= 1
00498          && magSqr(eIs + a*s - eJs - b*t) <= rCutMaxSqr_
00499         );
00500     }
00501 
00502     return false;
00503 }
00504 
00505 
00506 const Foam::labelList Foam::interactionLists::realCellsInRangeOfSegment
00507 (
00508     const labelList& segmentFaces,
00509     const labelList& segmentEdges,
00510     const labelList& segmentPoints
00511 ) const
00512 {
00513     DynamicList<label> realCellsFoundInRange;
00514 
00515     forAll(segmentFaces, sF)
00516     {
00517         const label f = segmentFaces[sF];
00518 
00519         forAll (mesh_.points(), p)
00520         {
00521             if (testPointFaceDistance(p, f))
00522             {
00523                 const labelList& pCells(mesh_.pointCells()[p]);
00524 
00525                 forAll(pCells, pC)
00526                 {
00527                     const label cellI(pCells[pC]);
00528 
00529                     if (findIndex(realCellsFoundInRange, cellI) == -1)
00530                     {
00531                         realCellsFoundInRange.append(cellI);
00532                     }
00533                 }
00534             }
00535         }
00536     }
00537 
00538     forAll(segmentPoints, sP)
00539     {
00540         const label p = segmentPoints[sP];
00541 
00542         forAll(mesh_.faces(), f)
00543         {
00544             if (testPointFaceDistance(p, f))
00545             {
00546                 const label cellO(mesh_.faceOwner()[f]);
00547 
00548                 if (findIndex(realCellsFoundInRange, cellO) == -1)
00549                 {
00550                     realCellsFoundInRange.append(cellO);
00551                 }
00552 
00553                 if (mesh_.isInternalFace(f))
00554                 {
00555                     // boundary faces will not have neighbour information
00556 
00557                     const label cellN(mesh_.faceNeighbour()[f]);
00558 
00559                     if (findIndex(realCellsFoundInRange, cellN) == -1)
00560                     {
00561                         realCellsFoundInRange.append(cellN);
00562                     }
00563                 }
00564             }
00565         }
00566     }
00567 
00568     forAll(segmentEdges, sE)
00569     {
00570         const edge& eJ(mesh_.edges()[segmentEdges[sE]]);
00571 
00572         forAll (mesh_.edges(), edgeIIndex)
00573         {
00574             const edge& eI(mesh_.edges()[edgeIIndex]);
00575 
00576             if (testEdgeEdgeDistance(eI, eJ))
00577             {
00578                 const labelList& eICells(mesh_.edgeCells()[edgeIIndex]);
00579 
00580                 forAll(eICells, eIC)
00581                 {
00582                     const label cellI(eICells[eIC]);
00583 
00584                     if (findIndex(realCellsFoundInRange, cellI) == -1)
00585                     {
00586                         realCellsFoundInRange.append(cellI);
00587                     }
00588                 }
00589             }
00590         }
00591     }
00592 
00593     return realCellsFoundInRange.shrink();
00594 }
00595 
00596 
00597 const Foam::labelList Foam::interactionLists::referredCellsInRangeOfSegment
00598 (
00599     const List<referredCell>& referredInteractionList,
00600     const labelList& segmentFaces,
00601     const labelList& segmentEdges,
00602     const labelList& segmentPoints
00603 ) const
00604 {
00605     DynamicList<label> referredCellsFoundInRange;
00606 
00607     forAll(segmentFaces, sF)
00608     {
00609         const label f = segmentFaces[sF];
00610 
00611         forAll(referredInteractionList, rIL)
00612         {
00613             const vectorList& refCellPoints
00614                 = referredInteractionList[rIL].vertexPositions();
00615 
00616             if (testPointFaceDistance(refCellPoints, f))
00617             {
00618                 if (findIndex(referredCellsFoundInRange, rIL) == -1)
00619                 {
00620                     referredCellsFoundInRange.append(rIL);
00621                 }
00622             }
00623         }
00624     }
00625 
00626     forAll(segmentPoints, sP)
00627     {
00628         const label p = segmentPoints[sP];
00629 
00630         forAll(referredInteractionList, rIL)
00631         {
00632             const referredCell& refCell(referredInteractionList[rIL]);
00633 
00634             if (testPointFaceDistance(p, refCell))
00635             {
00636                 if (findIndex(referredCellsFoundInRange, rIL) == -1)
00637                 {
00638                     referredCellsFoundInRange.append(rIL);
00639                 }
00640             }
00641         }
00642     }
00643 
00644     forAll(segmentEdges, sE)
00645     {
00646         const edge& eI(mesh_.edges()[segmentEdges[sE]]);
00647 
00648         forAll(referredInteractionList, rIL)
00649         {
00650             const vectorList& refCellPoints
00651                 = referredInteractionList[rIL].vertexPositions();
00652 
00653             const edgeList& refCellEdges
00654                 = referredInteractionList[rIL].edges();
00655 
00656             forAll(refCellEdges, rCE)
00657             {
00658                 const edge& eJ(refCellEdges[rCE]);
00659 
00660                 if
00661                 (
00662                     testEdgeEdgeDistance
00663                     (
00664                         eI,
00665                         refCellPoints[eJ.start()],
00666                         refCellPoints[eJ.end()]
00667                     )
00668                 )
00669                 {
00670                     if(findIndex(referredCellsFoundInRange, rIL) == -1)
00671                     {
00672                         referredCellsFoundInRange.append(rIL);
00673                     }
00674                 }
00675             }
00676         }
00677     }
00678 
00679     return referredCellsFoundInRange.shrink();
00680 }
00681 
00682 
00683 // ************************************************************************* //
Copyright © 2000-2009 OpenCFD Ltd