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

globalPoints.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 "globalPoints.H"
00028 #include "processorPolyPatch.H"
00029 #include "cyclicPolyPatch.H"
00030 #include "polyMesh.H"
00031 
00032 // * * * * * * * * * * * * * * Static Data Members * * * * * * * * * * * * * //
00033 
00034 defineTypeNameAndDebug(Foam::globalPoints, 0);
00035 
00036 
00037 // * * * * * * * * * * * * * Private Member Functions  * * * * * * * * * * * //
00038 
00039 // Total number of points on processor patches. Is upper limit for number
00040 // of shared points
00041 Foam::label Foam::globalPoints::countPatchPoints
00042 (
00043     const polyBoundaryMesh& patches
00044 )
00045 {
00046     label nTotPoints = 0;
00047 
00048     forAll(patches, patchI)
00049     {
00050         const polyPatch& pp = patches[patchI];
00051 
00052         if
00053         (
00054             (Pstream::parRun() && isA<processorPolyPatch>(pp))
00055          || isA<cyclicPolyPatch>(pp)
00056         )
00057         {
00058             nTotPoints += pp.nPoints();
00059         }
00060     }
00061 
00062     return nTotPoints;
00063 }
00064 
00065 
00066 // Collect all topological information about a point on a patch.
00067 // (this information is the patch faces using the point and the relative
00068 // position of the point in the face)
00069 void Foam::globalPoints::addToSend
00070 (
00071     const primitivePatch& pp,
00072     const label patchPointI,
00073     const procPointList& knownInfo,
00074 
00075     DynamicList<label>& patchFaces,
00076     DynamicList<label>& indexInFace,
00077     DynamicList<procPointList>& allInfo
00078 )
00079 {
00080     label meshPointI = pp.meshPoints()[patchPointI];
00081 
00082     // Add all faces using the point so we are sure we find it on the
00083     // other side.
00084     const labelList& pFaces = pp.pointFaces()[patchPointI];
00085 
00086     forAll(pFaces, i)
00087     {
00088         label patchFaceI = pFaces[i];
00089 
00090         const face& f = pp[patchFaceI];
00091 
00092         patchFaces.append(patchFaceI);
00093         indexInFace.append(findIndex(f, meshPointI));
00094         allInfo.append(knownInfo);
00095     }
00096 }
00097 
00098 
00099 // Add nbrInfo to myInfo. Return true if anything changed.
00100 // nbrInfo is for a point a list of all the processors using it (and the
00101 // meshPoint label on that processor)
00102 bool Foam::globalPoints::mergeInfo
00103 (
00104     const procPointList& nbrInfo,
00105     procPointList& myInfo
00106 )
00107 {
00108     // Indices of entries in nbrInfo not yet in myInfo.
00109     DynamicList<label> newInfo(nbrInfo.size());
00110 
00111     forAll(nbrInfo, i)
00112     {
00113         const procPoint& info = nbrInfo[i];
00114 
00115         // Check if info already in myInfo.
00116         label index = -1;
00117 
00118         forAll(myInfo, j)
00119         {
00120             if (myInfo[j] == info)
00121             {
00122                 // Already have information for processor/point combination
00123                 // in my list so skip.
00124                 index = j;
00125 
00126                 break;
00127             }
00128         }
00129 
00130         if (index == -1)
00131         {
00132             // Mark this information as being not yet in myInfo
00133             newInfo.append(i);
00134         }
00135     }
00136 
00137     newInfo.shrink();
00138 
00139     // Append all nbrInfos referenced in newInfo to myInfo.
00140 
00141     label index = myInfo.size();
00142 
00143     myInfo.setSize(index + newInfo.size());
00144 
00145     forAll(newInfo, i)
00146     {
00147         myInfo[index++] = nbrInfo[newInfo[i]];
00148     }
00149 
00150     // Did anything change?
00151     return newInfo.size() > 0;
00152 }
00153 
00154 
00155 // Updates database of current information on meshpoints with nbrInfo.
00156 // Uses mergeInfo above. Returns true if data kept for meshPointI changed.
00157 bool Foam::globalPoints::storeInfo
00158 (
00159     const procPointList& nbrInfo,
00160     const label meshPointI
00161 )
00162 {
00163     label infoChanged = false;
00164 
00165     // Get the index into the procPoints list.
00166     Map<label>::iterator iter = meshToProcPoint_.find(meshPointI);
00167 
00168     if (iter != meshToProcPoint_.end())
00169     {
00170         procPointList& knownInfo = procPoints_[iter()];
00171 
00172         if (mergeInfo(nbrInfo, knownInfo))
00173         {
00174             infoChanged = true;
00175         }
00176     }
00177     else
00178     {
00179         procPointList knownInfo(1);
00180         knownInfo[0][0] = Pstream::myProcNo();
00181         knownInfo[0][1] = meshPointI;
00182 
00183         if (mergeInfo(nbrInfo, knownInfo))
00184         {
00185             // Update addressing from into procPoints
00186             meshToProcPoint_.insert(meshPointI, procPoints_.size());
00187             // Insert into list of equivalences.
00188             procPoints_.append(knownInfo);
00189 
00190             infoChanged = true;
00191         }
00192     }
00193     return infoChanged;
00194 }
00195 
00196 
00197 // Insert my own points into structure and mark as changed.
00198 void Foam::globalPoints::initOwnPoints
00199 (
00200     const bool allPoints,
00201     labelHashSet& changedPoints
00202 )
00203 {
00204     const polyBoundaryMesh& patches = mesh_.boundaryMesh();
00205 
00206     forAll(patches, patchI)
00207     {
00208         const polyPatch& pp = patches[patchI];
00209 
00210         if
00211         (
00212             (Pstream::parRun() && isA<processorPolyPatch>(pp))
00213          || isA<cyclicPolyPatch>(pp)
00214         )
00215         {
00216             const labelList& meshPoints = pp.meshPoints();
00217 
00218             if (allPoints)
00219             {
00220                 // All points on patch
00221                 forAll(meshPoints, i)
00222                 {
00223                     label meshPointI = meshPoints[i];
00224 
00225                     procPointList knownInfo(1);
00226                     knownInfo[0][0] = Pstream::myProcNo();
00227                     knownInfo[0][1] = meshPointI;
00228 
00229                     // Update addressing from meshpoint to index in procPoints
00230                     meshToProcPoint_.insert(meshPointI, procPoints_.size());
00231                     // Insert into list of equivalences.
00232                     procPoints_.append(knownInfo);
00233 
00234                     // Update changedpoints info.
00235                     changedPoints.insert(meshPointI);
00236                 }
00237             }
00238             else
00239             {
00240                 // Boundary points only
00241                 const labelList& boundaryPoints = pp.boundaryPoints();
00242 
00243                 forAll(boundaryPoints, i)
00244                 {
00245                     label meshPointI = meshPoints[boundaryPoints[i]];
00246 
00247                     procPointList knownInfo(1);
00248                     knownInfo[0][0] = Pstream::myProcNo();
00249                     knownInfo[0][1] = meshPointI;
00250 
00251                     // Update addressing from meshpoint to index in procPoints
00252                     meshToProcPoint_.insert(meshPointI, procPoints_.size());
00253                     // Insert into list of equivalences.
00254                     procPoints_.append(knownInfo);
00255 
00256                     // Update changedpoints info.
00257                     changedPoints.insert(meshPointI);
00258                 }
00259             }
00260         }
00261     }
00262 }
00263 
00264 
00265 // Send all my info on changedPoints_ to my neighbours.
00266 void Foam::globalPoints::sendPatchPoints(const labelHashSet& changedPoints)
00267  const
00268 {
00269     const polyBoundaryMesh& patches = mesh_.boundaryMesh();
00270 
00271     forAll(patches, patchI)
00272     {
00273         const polyPatch& pp = patches[patchI];
00274 
00275         if (Pstream::parRun() && isA<processorPolyPatch>(pp))
00276         {
00277             // Information to send:
00278             // patch face
00279             DynamicList<label> patchFaces(pp.nPoints());
00280             // index in patch face
00281             DynamicList<label> indexInFace(pp.nPoints());
00282             // all information I currently hold about this patchPoint
00283             DynamicList<procPointList> allInfo(pp.nPoints());
00284 
00285 
00286             // Now collect information on all mesh points mentioned in
00287             // changedPoints. Note that these points only should occur on
00288             // processorPatches (or rather this is a limitation!).
00289 
00290             const labelList& meshPoints = pp.meshPoints();
00291 
00292             forAll(meshPoints, patchPointI)
00293             {
00294                 label meshPointI = meshPoints[patchPointI];
00295 
00296                 if (changedPoints.found(meshPointI))
00297                 {
00298                     label index = meshToProcPoint_[meshPointI];
00299 
00300                     const procPointList& knownInfo = procPoints_[index];
00301 
00302                     // Add my information about meshPointI to the send buffers
00303                     addToSend
00304                     (
00305                         pp,
00306                         patchPointI,
00307                         knownInfo,
00308 
00309                         patchFaces,
00310                         indexInFace,
00311                         allInfo
00312                     );
00313                 }
00314             }
00315             patchFaces.shrink();
00316             indexInFace.shrink();
00317             allInfo.shrink();
00318 
00319             // Send to neighbour
00320             {
00321                 const processorPolyPatch& procPatch =
00322                     refCast<const processorPolyPatch>(pp);
00323 
00324                 if (debug)
00325                 {
00326                     Pout<< " Sending to "
00327                         << procPatch.neighbProcNo() << "   point information:"
00328                         << patchFaces.size() << endl;
00329                 }
00330 
00331                 OPstream toNeighbour
00332                 (
00333                     Pstream::blocking,
00334                     procPatch.neighbProcNo()
00335                 );
00336 
00337                 toNeighbour << patchFaces << indexInFace << allInfo;
00338             }
00339         }
00340     }
00341 }
00342 
00343 
00344 // Receive all my neighbours' information and merge with mine.
00345 // After finishing will have updated
00346 // - procPoints_ : all neighbour information merged in.
00347 // - meshToProcPoint_
00348 // - changedPoints: all meshPoints for which something changed.
00349 void Foam::globalPoints::receivePatchPoints(labelHashSet& changedPoints)
00350 {
00351     const polyBoundaryMesh& patches = mesh_.boundaryMesh();
00352 
00353     // Reset changed points
00354     changedPoints.clear();
00355 
00356     forAll(patches, patchI)
00357     {
00358         const polyPatch& pp = patches[patchI];
00359 
00360         if (Pstream::parRun() && isA<processorPolyPatch>(pp))
00361         {
00362             const processorPolyPatch& procPatch =
00363                 refCast<const processorPolyPatch>(pp);
00364 
00365             labelList patchFaces;
00366             labelList indexInFace;
00367             List<procPointList> nbrInfo;
00368 
00369             {
00370                 IPstream fromNeighbour
00371                 (
00372                     Pstream::blocking,
00373                     procPatch.neighbProcNo()
00374                 );
00375                 fromNeighbour >> patchFaces >> indexInFace >> nbrInfo;
00376             }
00377 
00378             if (debug)
00379             {
00380                 Pout<< " Received from "
00381                     << procPatch.neighbProcNo() << "   point information:"
00382                     << patchFaces.size() << endl;
00383             }
00384 
00385             forAll(patchFaces, i)
00386             {
00387                 const face& f = pp[patchFaces[i]];
00388 
00389                 // Get index in this face from index on face on other side.
00390                 label index = (f.size() - indexInFace[i]) % f.size();
00391 
00392                 // Get the meshpoint on my side
00393                 label meshPointI = f[index];
00394 
00395                 //const procPointList& info = nbrInfo[i];
00396                 //Pout << "Received for my coord "
00397                 //    << mesh_.points()[meshPointI];
00398                 //
00399                 //forAll(info, j)
00400                 //{
00401                 //    Pout<< ' ' <<info[j];
00402                 //}
00403                 //Pout<< endl;
00404 
00405                 if (storeInfo(nbrInfo[i], meshPointI))
00406                 {
00407                     changedPoints.insert(meshPointI);
00408                 }
00409             }
00410         }
00411         else if (isA<cyclicPolyPatch>(pp))
00412         {
00413             // Handle cyclics: send lower half to upper half and vice versa.
00414             // Or since they both are in memory just do it point by point.
00415 
00416             const cyclicPolyPatch& cycPatch =
00417                 refCast<const cyclicPolyPatch>(pp);
00418 
00419             const labelList& meshPoints = pp.meshPoints();
00420 
00421             //const edgeList& connections = cycPatch.coupledPoints();
00422             const edgeList connections(coupledPoints(cycPatch));
00423 
00424             forAll(connections, i)
00425             {
00426                 const edge& e = connections[i];
00427 
00428                 label meshPointA = meshPoints[e[0]];
00429                 label meshPointB = meshPoints[e[1]];
00430 
00431                 // Do we have information on meshPointA?
00432                 Map<label>::iterator procPointA =
00433                     meshToProcPoint_.find(meshPointA);
00434 
00435                 if (procPointA != meshToProcPoint_.end())
00436                 {
00437                     // Store A info onto pointB
00438                     if (storeInfo(procPoints_[procPointA()], meshPointB))
00439                     {
00440                         changedPoints.insert(meshPointB);
00441                     }
00442                 }
00443 
00444                 // Same for info on pointB
00445                 Map<label>::iterator procPointB =
00446                     meshToProcPoint_.find(meshPointB);
00447 
00448                 if (procPointB != meshToProcPoint_.end())
00449                 {
00450                     // Store B info onto pointA
00451                     if (storeInfo(procPoints_[procPointB()], meshPointA))
00452                     {
00453                         changedPoints.insert(meshPointA);
00454                     }
00455                 }
00456             }
00457         }
00458     }
00459 }
00460 
00461 
00462 // Remove entries which are handled by normal face-face communication. I.e.
00463 // those points where the equivalence list is only me and my (face)neighbour
00464 void Foam::globalPoints::remove(const Map<label>& directNeighbours)
00465 {
00466     // Save old ones.
00467     Map<label> oldMeshToProcPoint(meshToProcPoint_);
00468     meshToProcPoint_.clear();
00469 
00470     List<procPointList> oldProcPoints;
00471     oldProcPoints.transfer(procPoints_);
00472 
00473     // Go through all equivalences
00474     for
00475     (
00476         Map<label>::const_iterator iter = oldMeshToProcPoint.begin();
00477         iter != oldMeshToProcPoint.end();
00478         ++iter
00479     )
00480     {
00481         label meshPointI = iter.key();
00482         const procPointList& pointInfo = oldProcPoints[iter()];
00483 
00484         if (pointInfo.size() == 2)
00485         {
00486             // I will be in this equivalence list.
00487             // Check whether my direct (=face) neighbour
00488             // is in it. This would be an ordinary connection and can be
00489             // handled by normal face-face connectivity.
00490 
00491             const procPoint& a = pointInfo[0];
00492             const procPoint& b = pointInfo[1];
00493 
00494             if
00495             (
00496                 (a[0] == Pstream::myProcNo() && directNeighbours.found(a[1]))
00497              || (b[0] == Pstream::myProcNo() && directNeighbours.found(b[1]))
00498             )
00499             {
00500                 // Normal faceNeighbours
00501                 if (a[0] == Pstream::myProcNo())
00502                 {
00503                     //Pout<< "Removing direct neighbour:"
00504                     //    << mesh_.points()[a[1]]
00505                     //    << endl;
00506                 }
00507                 else if (b[0] == Pstream::myProcNo())
00508                 {
00509                     //Pout<< "Removing direct neighbour:"
00510                     //    << mesh_.points()[b[1]]
00511                     //    << endl;
00512                 }
00513             }
00514             else
00515             {
00516                 // This condition will be very rare: points are used by
00517                 // two processors which are not face-face connected.
00518                 // e.g.
00519                 // +------+------+
00520                 // | wall |  B   |
00521                 // +------+------+
00522                 // |   A  | wall |
00523                 // +------+------+
00524                 // Processor A and B share a point. Note that this only will
00525                 // be found if the two domains are face connected at all
00526                 // (not shown in the picture)
00527 
00528                 meshToProcPoint_.insert(meshPointI, procPoints_.size());
00529                 procPoints_.append(pointInfo);
00530             }
00531         }
00532         else if (pointInfo.size() == 1)
00533         {
00534             // This happens for 'wedge' like cyclics where the two halves
00535             // come together in the same point so share the same meshPoint.
00536             // So this meshPoint will have info of size one only.
00537             if
00538             (
00539                 pointInfo[0][0] != Pstream::myProcNo()
00540              || !directNeighbours.found(pointInfo[0][1])
00541             )
00542             {
00543                 meshToProcPoint_.insert(meshPointI, procPoints_.size());
00544                 procPoints_.append(pointInfo);
00545             }
00546         }
00547         else
00548         {
00549             meshToProcPoint_.insert(meshPointI, procPoints_.size());
00550             procPoints_.append(pointInfo);
00551         }
00552     }
00553 
00554     procPoints_.shrink();
00555 }
00556 
00557 
00558 // Get (indices of) points for which I am master (= lowest numbered point on
00559 // lowest numbered processor).
00560 // (equivalence lists should be complete by now)
00561 Foam::labelList Foam::globalPoints::getMasterPoints() const
00562 {
00563     labelList masterPoints(nPatchPoints_);
00564     label nMaster = 0;
00565 
00566     // Go through all equivalences and determine meshPoints where I am master.
00567     for
00568     (
00569         Map<label>::const_iterator iter = meshToProcPoint_.begin();
00570         iter != meshToProcPoint_.end();
00571         ++iter
00572     )
00573     {
00574         label meshPointI = iter.key();
00575         const procPointList& pointInfo = procPoints_[iter()];
00576 
00577         if (pointInfo.size() < 2)
00578         {
00579             // Points should have an equivalence list >= 2 since otherwise
00580             // they would be direct neighbours and have been removed in the
00581             // call to 'remove'.
00582             Pout<< "MeshPoint:" << meshPointI
00583                 << " coord:" << mesh_.points()[meshPointI]
00584                 << " has no corresponding point on a neighbouring processor"
00585                 << endl;
00586             FatalErrorIn("globalPoints::getMasterPoints()")
00587                 << '[' << Pstream::myProcNo() << ']'
00588                 << " MeshPoint:" << meshPointI
00589                 << " coord:" << mesh_.points()[meshPointI]
00590                 << " has no corresponding point on a neighbouring processor"
00591                 << abort(FatalError);
00592         }
00593         else
00594         {
00595             // Find lowest processor and lowest mesh point on this processor.
00596             label lowestProcI = labelMax;
00597             label lowestPointI = labelMax;
00598 
00599             forAll(pointInfo, i)
00600             {
00601                 label proc = pointInfo[i][0];
00602 
00603                 if (proc < lowestProcI)
00604                 {
00605                     lowestProcI = proc;
00606                     lowestPointI = pointInfo[i][1];
00607                 }
00608                 else if (proc == lowestProcI)
00609                 {
00610                     lowestPointI = min(lowestPointI, pointInfo[i][1]);
00611                 }
00612             }
00613 
00614             if
00615             (
00616                 lowestProcI == Pstream::myProcNo()
00617              && lowestPointI == meshPointI
00618             )
00619             {
00620                 // I am lowest numbered processor and point. Add to my list.
00621                 masterPoints[nMaster++] = meshPointI;
00622             }
00623         }
00624     }
00625 
00626     masterPoints.setSize(nMaster);
00627 
00628     return masterPoints;
00629 }
00630 
00631 
00632 // Send subset of lists
00633 void Foam::globalPoints::sendSharedPoints(const labelList& changedIndices) const
00634 {
00635     const polyBoundaryMesh& patches = mesh_.boundaryMesh();
00636 
00637     forAll(patches, patchI)
00638     {
00639         const polyPatch& pp = patches[patchI];
00640 
00641         if (Pstream::parRun() && isA<processorPolyPatch>(pp))
00642         {
00643             const processorPolyPatch& procPatch =
00644                 refCast<const processorPolyPatch>(pp);
00645 
00646             OPstream toNeighbour(Pstream::blocking, procPatch.neighbProcNo());
00647 
00648             if (debug)
00649             {
00650                 Pout<< "Sending to " << procPatch.neighbProcNo()
00651                     << "  changed sharedPoints info:"
00652                     << changedIndices.size() << endl;
00653             }
00654 
00655             toNeighbour
00656                 << UIndirectList<label>(sharedPointAddr_, changedIndices)()
00657                 << UIndirectList<label>(sharedPointLabels_, changedIndices)();
00658         }
00659     }
00660 }
00661 
00662 
00663 // Receive shared point indices for all my shared points. Note that since
00664 // there are only a few here we can build a reverse map using the meshpoint
00665 // instead of doing all this relative point indexing (patch face + index in
00666 // face) as in send/receivePatchPoints
00667 void Foam::globalPoints::receiveSharedPoints(labelList& changedIndices)
00668 {
00669     changedIndices.setSize(sharedPointAddr_.size());
00670     label nChanged = 0;
00671 
00672     const polyBoundaryMesh& patches = mesh_.boundaryMesh();
00673 
00674     // Receive and set shared points
00675     forAll(patches, patchI)
00676     {
00677         const polyPatch& pp = patches[patchI];
00678 
00679         if (Pstream::parRun() && isA<processorPolyPatch>(pp))
00680         {
00681             const processorPolyPatch& procPatch =
00682                 refCast<const processorPolyPatch>(pp);
00683 
00684             // Map from neighbouring meshPoint to sharedPoint)
00685             Map<label> nbrSharedPoints(sharedPointAddr_.size());
00686 
00687             {
00688                 // Receive meshPoints on neighbour and sharedPoints and build
00689                 // map from it. Note that we could have built the map on the
00690                 // neighbour and sent it over.
00691                 labelList nbrSharedPointAddr;
00692                 labelList nbrSharedPointLabels;
00693 
00694                 {
00695                     IPstream fromNeighbour
00696                     (
00697                         Pstream::blocking,
00698                         procPatch.neighbProcNo()
00699                     );
00700                     fromNeighbour >> nbrSharedPointAddr >> nbrSharedPointLabels;
00701                 }
00702 
00703                 // Insert into to map
00704                 forAll(nbrSharedPointLabels, i)
00705                 {
00706                     nbrSharedPoints.insert
00707                     (
00708                         nbrSharedPointLabels[i], // meshpoint on neighbour
00709                         nbrSharedPointAddr[i]    // sharedPoint label
00710                     );
00711                 }
00712             }
00713 
00714 
00715             // Merge into whatever information I hold.
00716             for
00717             (
00718                 Map<label>::const_iterator iter = meshToProcPoint_.begin();
00719                 iter != meshToProcPoint_.end();
00720                 ++iter
00721             )
00722             {
00723                 label meshPointI = iter.key();
00724                 label index = iter();
00725 
00726                 if (sharedPointAddr_[index] == -1)
00727                 {
00728                     // No shared point known yet for this meshPoint.
00729                     // See if was received from neighbour.
00730                     const procPointList& knownInfo = procPoints_[index];
00731 
00732                     // Check through the whole equivalence list for any
00733                     // point from the neighbour.
00734                     forAll(knownInfo, j)
00735                     {
00736                         const procPoint& info = knownInfo[j];
00737 
00738                         if
00739                         (
00740                             (info[0] == procPatch.neighbProcNo())
00741                          && nbrSharedPoints.found(info[1])
00742                         )
00743                         {
00744                             // So this knownInfo contains the neighbour point
00745                             label sharedPointI = nbrSharedPoints[info[1]];
00746 
00747                             sharedPointAddr_[index] = sharedPointI;
00748                             sharedPointLabels_[index] = meshPointI;
00749                             changedIndices[nChanged++] = index;
00750 
00751                             break;
00752                         }
00753                     }
00754                 }
00755             }
00756         }
00757         else if (isA<cyclicPolyPatch>(pp))
00758         {
00759             const cyclicPolyPatch& cycPatch =
00760                 refCast<const cyclicPolyPatch>(pp);
00761 
00762             // Build map from meshPoint to sharedPoint
00763             Map<label> meshToSharedPoint(sharedPointAddr_.size());
00764             forAll(sharedPointLabels_, i)
00765             {
00766                 label meshPointI = sharedPointLabels_[i];
00767 
00768                 meshToSharedPoint.insert(meshPointI, sharedPointAddr_[i]);
00769             }
00770 
00771             // Sync all info.
00772             //const edgeList& connections = cycPatch.coupledPoints();
00773             const edgeList connections(coupledPoints(cycPatch));
00774 
00775             forAll(connections, i)
00776             {
00777                 const edge& e = connections[i];
00778 
00779                 label meshPointA = pp.meshPoints()[e[0]];
00780                 label meshPointB = pp.meshPoints()[e[1]];
00781 
00782                 // Do we already have shared point for meshPointA?
00783                 Map<label>::iterator fndA = meshToSharedPoint.find(meshPointA);
00784                 Map<label>::iterator fndB = meshToSharedPoint.find(meshPointB);
00785 
00786                 if (fndA != meshToSharedPoint.end())
00787                 {
00788                     if (fndB != meshToSharedPoint.end())
00789                     {
00790                         if (fndA() != fndB())
00791                         {
00792                             FatalErrorIn
00793                             (
00794                                 "globalPoints::receiveSharedPoints"
00795                                 "(labelList&)"
00796                             )   << "On patch " << pp.name()
00797                                 << " connected points " << meshPointA
00798                                 << ' ' << mesh_.points()[meshPointA]
00799                                 << " and " << meshPointB
00800                                 << ' ' << mesh_.points()[meshPointB]
00801                                 << " are mapped to different shared points: "
00802                                 << fndA() << " and " << fndB()
00803                                 << abort(FatalError);
00804                         }
00805                     }
00806                     else
00807                     {
00808                         // No shared point yet for B.
00809                         label sharedPointI = fndA();
00810 
00811                         // Store shared point for meshPointB
00812                         label index = meshToProcPoint_[meshPointB];
00813 
00814                         sharedPointAddr_[index] = sharedPointI;
00815                         sharedPointLabels_[index] = meshPointB;
00816                         changedIndices[nChanged++] = index;
00817                     }
00818                 }
00819                 else
00820                 {
00821                     // No shared point yet for A.
00822                     if (fndB != meshToSharedPoint.end())
00823                     {
00824                         label sharedPointI = fndB();
00825 
00826                         // Store shared point for meshPointA
00827                         label index = meshToProcPoint_[meshPointA];
00828 
00829                         sharedPointAddr_[index] = sharedPointI;
00830                         sharedPointLabels_[index] = meshPointA;
00831                         changedIndices[nChanged++] = index;
00832                     }
00833                 }
00834             }
00835         }
00836     }
00837 
00838     changedIndices.setSize(nChanged);
00839 }
00840 
00841 
00842 Foam::edgeList Foam::globalPoints::coupledPoints(const cyclicPolyPatch& pp)
00843 {
00844     // Look at cyclic patch as two halves, A and B.
00845     // Now all we know is that relative face index in halfA is same
00846     // as coupled face in halfB and also that the 0th vertex
00847     // corresponds.
00848 
00849     // From halfA point to halfB or -1.
00850     labelList coupledPoint(pp.nPoints(), -1);
00851 
00852     for (label patchFaceA = 0; patchFaceA < pp.size()/2; patchFaceA++)
00853     {
00854         const face& fA = pp.localFaces()[patchFaceA];
00855 
00856         forAll(fA, indexA)
00857         {
00858             label patchPointA = fA[indexA];
00859 
00860             if (coupledPoint[patchPointA] == -1)
00861             {
00862                 const face& fB = pp.localFaces()[patchFaceA + pp.size()/2];
00863 
00864                 label indexB = (fB.size() - indexA) % fB.size();
00865 
00866                 coupledPoint[patchPointA] = fB[indexB];
00867             }
00868         }
00869     }
00870 
00871     edgeList connected(pp.nPoints());
00872 
00873     // Extract coupled points.
00874     label connectedI = 0;
00875 
00876     forAll(coupledPoint, i)
00877     {
00878         if (coupledPoint[i] != -1)
00879         {
00880             connected[connectedI++] = edge(i, coupledPoint[i]);
00881         }
00882     }
00883 
00884     connected.setSize(connectedI);
00885 
00886     return connected;
00887 }
00888 
00889 
00890 // * * * * * * * * * * * * * * * * Constructors  * * * * * * * * * * * * * * //
00891 
00892 // Construct from components
00893 Foam::globalPoints::globalPoints(const polyMesh& mesh)
00894 :
00895     mesh_(mesh),
00896     nPatchPoints_(countPatchPoints(mesh.boundaryMesh())),
00897     procPoints_(nPatchPoints_),
00898     meshToProcPoint_(nPatchPoints_),
00899     sharedPointAddr_(0),
00900     sharedPointLabels_(0),
00901     nGlobalPoints_(0)
00902 {
00903     if (debug)
00904     {
00905         Pout<< "globalPoints::globalPoints(const polyMesh&) : "
00906             << "doing processor to processor communication to get sharedPoints"
00907             << endl;
00908     }
00909 
00910     labelHashSet changedPoints(nPatchPoints_);
00911 
00912     // Initialize procPoints with my patch points. Keep track of points
00913     // inserted (in changedPoints)
00914     // There are two possible forms of this:
00915     // - initialize with all patch points (allPoints = true). This causes all
00916     //   patch points to be exchanged so a lot of information gets stored and
00917     //   transferred. This all gets filtered out later when removing the
00918     //   equivalence lists of size 2.
00919     // - initialize with boundary points of patches only (allPoints = false).
00920     //   This should work for all decompositions except extreme ones where a
00921     //   shared point is not on the boundary of any processor patches using it.
00922     //   This would happen if a domain was pinched such that two patches share
00923     //   a point or edge.
00924     initOwnPoints(true, changedPoints);
00925 
00926     // Do one exchange iteration to get neighbour points.
00927     sendPatchPoints(changedPoints);
00928     receivePatchPoints(changedPoints);
00929 
00930 
00931     // Save neighbours reachable through face-face communication.
00932     Map<label> neighbourList(meshToProcPoint_);
00933 
00934 
00935     // Exchange until nothing changes on all processors.
00936     bool changed = false;
00937 
00938     do
00939     {
00940         sendPatchPoints(changedPoints);
00941         receivePatchPoints(changedPoints);
00942 
00943         changed = changedPoints.size() > 0;
00944         reduce(changed, orOp<bool>());
00945 
00946     } while (changed);
00947 
00948 
00949     // Remove direct neighbours from point equivalences.
00950     remove(neighbourList);
00951 
00952 
00953     //Pout<< "After removing locally connected points:" << endl;
00954     //for
00955     //(
00956     //    Map<label>::const_iterator iter = meshToProcPoint_.begin();
00957     //    iter != meshToProcPoint_.end();
00958     //    ++iter
00959     //)
00960     //{
00961     //    label meshPointI = iter.key();
00962     //    const procPointList& pointInfo = procPoints_[iter()];
00963     //
00964     //    forAll(pointInfo, i)
00965     //    {
00966     //        Pout<< "    pointI:" << meshPointI << ' '
00967     //            << mesh.points()[meshPointI]
00968     //            << " connected to proc " << pointInfo[i][0]
00969     //            << " point:" << pointInfo[i][1]
00970     //        << endl;
00971     //    }
00972     //}
00973 
00974 
00975     // We now have - in procPoints_ - a list of points which are shared between
00976     // multiple processors. These are the ones for which are sharedPoint
00977     // needs to be determined. This is done by having the lowest numbered
00978     // processor in the equivalence list 'ask' for a sharedPoint number
00979     // and then distribute it across processor patches to the non-master
00980     // processors. Note: below piece of coding is not very efficient. Uses
00981     // a Map where possibly it shouldn't
00982 
00983     // Initialize sharedPoint addressing. Is for every entry in procPoints_
00984     // the sharedPoint.
00985     sharedPointAddr_.setSize(meshToProcPoint_.size());
00986     sharedPointAddr_ = -1;
00987     sharedPointLabels_.setSize(meshToProcPoint_.size());
00988     sharedPointLabels_ = -1;
00989 
00990 
00991     // Get points for which I am master (lowest numbered proc)
00992     labelList masterPoints(getMasterPoints());
00993 
00994 
00995     // Determine number of master points on all processors.
00996     labelList sharedPointSizes(Pstream::nProcs());
00997     sharedPointSizes[Pstream::myProcNo()] = masterPoints.size();
00998 
00999     Pstream::gatherList(sharedPointSizes);
01000     Pstream::scatterList(sharedPointSizes);
01001 
01002     if (debug)
01003     {
01004         Pout<< "sharedPointSizes:" << sharedPointSizes << endl;
01005     }
01006 
01007     // Get total number of shared points
01008     nGlobalPoints_ = 0;
01009     forAll(sharedPointSizes, procI)
01010     {
01011         nGlobalPoints_ += sharedPointSizes[procI];
01012     }
01013 
01014     // Assign sharedPoint labels. Every processor gets assigned consecutive
01015     // numbers for its master points.
01016     // These are assigned in processor order so processor0 gets
01017     // 0..masterPoints.size()-1 etc.
01018 
01019     // My point labels start after those of lower numbered processors
01020     label sharedPointI = 0;
01021     for (label procI = 0; procI < Pstream::myProcNo(); procI++)
01022     {
01023         sharedPointI += sharedPointSizes[procI];
01024     }
01025 
01026     forAll(masterPoints, i)
01027     {
01028         label meshPointI = masterPoints[i];
01029 
01030         label index = meshToProcPoint_[meshPointI];
01031 
01032         sharedPointLabels_[index] = meshPointI;
01033         sharedPointAddr_[index] = sharedPointI++;
01034     }
01035 
01036 
01037     // Now we have a sharedPointLabel for some of the entries in procPoints.
01038     // Send this information to neighbours. Receive their information.
01039     // Loop until nothing changes.
01040 
01041     // Initial subset to send is points for which I have sharedPoints
01042     labelList changedIndices(sharedPointAddr_.size());
01043     label nChanged = 0;
01044 
01045     forAll(sharedPointAddr_, i)
01046     {
01047         if (sharedPointAddr_[i] != -1)
01048         {
01049             changedIndices[nChanged++] = i;
01050         }
01051     }
01052     changedIndices.setSize(nChanged);
01053 
01054     changed = false;
01055 
01056     do
01057     {
01058         if (debug)
01059         {
01060             Pout<< "Determined " << changedIndices.size() << " shared points."
01061                 << " Exchanging them" << endl;
01062         }
01063         sendSharedPoints(changedIndices);
01064         receiveSharedPoints(changedIndices);
01065 
01066         changed = changedIndices.size() > 0;
01067         reduce(changed, orOp<bool>());
01068 
01069     } while (changed);
01070 
01071 
01072     forAll(sharedPointLabels_, i)
01073     {
01074         if (sharedPointLabels_[i] == -1)
01075         {
01076             FatalErrorIn("globalPoints::globalPoints(const polyMesh& mesh)")
01077                 << "Problem: shared point on processor " << Pstream::myProcNo()
01078                 << " not set at index " << sharedPointLabels_[i] << endl
01079                 << "This might mean the individual processor domains are not"
01080                 << " connected and the overall domain consists of multiple"
01081                 << " regions. You can check this with checkMesh"
01082                 << abort(FatalError);
01083         }
01084     }
01085 
01086     if (debug)
01087     {
01088         Pout<< "globalPoints::globalPoints(const polyMesh&) : "
01089             << "Finished global points" << endl;
01090     }
01091 }
01092 
01093 
01094 // ************************************************************************* //
Copyright © 2000-2009 OpenCFD Ltd