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

globalMeshData.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 "globalMeshData.H"
00028 #include "Time.H"
00029 #include "Pstream.H"
00030 #include "PstreamCombineReduceOps.H"
00031 #include "processorPolyPatch.H"
00032 #include "demandDrivenData.H"
00033 #include "globalPoints.H"
00034 //#include "geomGlobalPoints.H"
00035 #include "labelIOList.H"
00036 #include "PackedList.H"
00037 #include "mergePoints.H"
00038 #include "matchPoints.H"
00039 #include "OFstream.H"
00040 
00041 // * * * * * * * * * * * * * * Static Data Members * * * * * * * * * * * * * //
00042 
00043 defineTypeNameAndDebug(Foam::globalMeshData, 0);
00044 
00045 // Geometric matching tolerance. Factor of mesh bounding box.
00046 const Foam::scalar Foam::globalMeshData::matchTol_ = 1E-8;
00047 
00048 
00049 // * * * * * * * * * * * * * Private Member Functions  * * * * * * * * * * * //
00050 
00051 // Collect processor patch addressing.
00052 void Foam::globalMeshData::initProcAddr()
00053 {
00054     processorPatchIndices_.setSize(mesh_.boundaryMesh().size());
00055     processorPatchIndices_ = -1;
00056 
00057     processorPatchNeighbours_.setSize(mesh_.boundaryMesh().size());
00058     processorPatchNeighbours_ = -1;
00059 
00060     // Construct processor patch indexing. processorPatchNeighbours_ only
00061     // set if running in parallel!
00062     processorPatches_.setSize(mesh_.boundaryMesh().size());
00063 
00064     label nNeighbours = 0;
00065 
00066     forAll (mesh_.boundaryMesh(), patchi)
00067     {
00068         if (isA<processorPolyPatch>(mesh_.boundaryMesh()[patchi]))
00069         {
00070             processorPatches_[nNeighbours] = patchi;
00071             processorPatchIndices_[patchi] = nNeighbours++;
00072         }
00073     }
00074     processorPatches_.setSize(nNeighbours);
00075 
00076 
00077     if (Pstream::parRun())
00078     {
00079         // Send indices of my processor patches to my neighbours
00080         forAll (processorPatches_, i)
00081         {
00082             label patchi = processorPatches_[i];
00083 
00084             OPstream toNeighbour
00085             (
00086                 Pstream::blocking,
00087                 refCast<const processorPolyPatch>
00088                 (
00089                     mesh_.boundaryMesh()[patchi]
00090                 ).neighbProcNo()
00091             );
00092 
00093             toNeighbour << processorPatchIndices_[patchi];
00094         }
00095 
00096         forAll(processorPatches_, i)
00097         {
00098             label patchi = processorPatches_[i];
00099             
00100             IPstream fromNeighbour
00101             (
00102                 Pstream::blocking,
00103                 refCast<const processorPolyPatch>
00104                 (
00105                     mesh_.boundaryMesh()[patchi]
00106                 ).neighbProcNo()
00107             );
00108             
00109             fromNeighbour >> processorPatchNeighbours_[patchi];
00110         }
00111     }
00112 }
00113 
00114 
00115 // Given information about locally used edges allocate global shared edges.
00116 void Foam::globalMeshData::countSharedEdges
00117 (
00118     const HashTable<labelList, edge, Hash<edge> >& procSharedEdges,
00119     HashTable<label, edge, Hash<edge> >& globalShared,
00120     label& sharedEdgeI
00121 )
00122 {
00123     // Count occurrences of procSharedEdges in global shared edges table.
00124     for
00125     (
00126         HashTable<labelList, edge, Hash<edge> >::const_iterator iter =
00127             procSharedEdges.begin();
00128         iter != procSharedEdges.end();
00129         ++iter
00130     )
00131     {
00132         const edge& e = iter.key();
00133 
00134         HashTable<label, edge, Hash<edge> >::iterator globalFnd =
00135             globalShared.find(e);
00136 
00137         if (globalFnd == globalShared.end())
00138         {
00139             // First time occurrence of this edge. Check how many we are adding.
00140             if (iter().size() == 1)
00141             {
00142                 // Only one edge. Mark with special value.
00143                 globalShared.insert(e, -1);
00144             }
00145             else
00146             {
00147                 // Edge used more than once (even by local shared edges alone)
00148                 // so allocate proper shared edge label.
00149                 globalShared.insert(e, sharedEdgeI++);
00150             }
00151         }
00152         else
00153         {
00154             if (globalFnd() == -1)
00155             {
00156                 // Second time occurence of this edge. Assign proper
00157                 // edge label.
00158                 globalFnd() = sharedEdgeI++;
00159             }
00160         }
00161     }
00162 }
00163 
00164 
00165 // Shared edges are shared between multiple processors. By their nature both
00166 // of their endpoints are shared points. (but not all edges using two shared
00167 // points are shared edges! There might e.g. be an edge between two unrelated
00168 // clusters of shared points)
00169 void Foam::globalMeshData::calcSharedEdges() const
00170 {
00171     if (nGlobalEdges_ != -1 || sharedEdgeLabelsPtr_ || sharedEdgeAddrPtr_)
00172     {
00173         FatalErrorIn("globalMeshData::calcSharedEdges()")
00174             << "Shared edge addressing already done" << abort(FatalError);
00175     }
00176 
00177 
00178     const labelList& sharedPtAddr = sharedPointAddr();
00179     const labelList& sharedPtLabels = sharedPointLabels();
00180 
00181     // Since don't want to construct pointEdges for whole mesh create
00182     // Map for all shared points.
00183     Map<label> meshToShared(2*sharedPtLabels.size());
00184     forAll(sharedPtLabels, i)
00185     {
00186         meshToShared.insert(sharedPtLabels[i], i);
00187     }
00188 
00189     // Find edges using shared points. Store correspondence to local edge
00190     // numbering. Note that multiple local edges can have the same shared
00191     // points! (for cyclics or separated processor patches)
00192     HashTable<labelList, edge, Hash<edge> > localShared
00193     (
00194         2*sharedPtAddr.size()
00195     );
00196 
00197     const edgeList& edges = mesh_.edges();
00198 
00199     forAll(edges, edgeI)
00200     {
00201         const edge& e = edges[edgeI];
00202 
00203         Map<label>::const_iterator e0Fnd = meshToShared.find(e[0]);
00204 
00205         if (e0Fnd != meshToShared.end())
00206         {
00207             Map<label>::const_iterator e1Fnd = meshToShared.find(e[1]);
00208 
00209             if (e1Fnd != meshToShared.end())
00210             {
00211                 // Found edge which uses shared points. Probably shared.
00212 
00213                 // Construct the edge in shared points (or rather global indices
00214                 // of the shared points)
00215                 edge sharedEdge
00216                 (
00217                     sharedPtAddr[e0Fnd()],
00218                     sharedPtAddr[e1Fnd()]
00219                 );
00220 
00221                 HashTable<labelList, edge, Hash<edge> >::iterator iter =
00222                     localShared.find(sharedEdge);
00223 
00224                 if (iter == localShared.end())
00225                 {
00226                     // First occurrence of this point combination. Store.
00227                     localShared.insert(sharedEdge, labelList(1, edgeI));
00228                 }
00229                 else
00230                 {
00231                     // Add this edge to list of edge labels.
00232                     labelList& edgeLabels = iter();
00233 
00234                     label sz = edgeLabels.size();
00235                     edgeLabels.setSize(sz+1);
00236                     edgeLabels[sz] = edgeI;
00237                 }
00238             }
00239         }
00240     }
00241 
00242 
00243     // Now we have a table on every processors which gives its edges which use
00244     // shared points. Send this all to the master and have it allocate
00245     // global edge numbers for it. But only allocate a global edge number for
00246     // edge if it is used more than once!
00247     // Note that we are now sending the whole localShared to the master whereas
00248     // we only need the local count (i.e. the number of times a global edge is
00249     // used). But then this only gets done once so not too bothered about the
00250     // extra global communication.
00251 
00252     HashTable<label, edge, Hash<edge> > globalShared(nGlobalPoints());
00253 
00254     if (Pstream::master())
00255     {
00256         label sharedEdgeI = 0;
00257 
00258         // Merge my shared edges into the global list
00259         if (debug)
00260         {
00261             Pout<< "globalMeshData::calcSharedEdges : Merging in from proc0 : "
00262                 << localShared.size() << endl;
00263         }
00264         countSharedEdges(localShared, globalShared, sharedEdgeI);
00265 
00266         // Receive data from slaves and insert
00267         if (Pstream::parRun())
00268         {
00269             for
00270             (
00271                 int slave=Pstream::firstSlave();
00272                 slave<=Pstream::lastSlave();
00273                 slave++
00274             )
00275             {
00276                 // Receive the edges using shared points from the slave.
00277                 IPstream fromSlave(Pstream::blocking, slave);
00278                 HashTable<labelList, edge, Hash<edge> > procSharedEdges
00279                 (
00280                     fromSlave
00281                 );
00282 
00283                 if (debug)
00284                 {
00285                     Pout<< "globalMeshData::calcSharedEdges : "
00286                         << "Merging in from proc"
00287                         << Foam::name(slave) << " : " << procSharedEdges.size()
00288                         << endl;
00289                 }
00290                 countSharedEdges(procSharedEdges, globalShared, sharedEdgeI);
00291             }
00292         }
00293 
00294         // Now our globalShared should have some edges with -1 as edge label
00295         // These were only used once so are not proper shared edges.
00296         // Remove them.
00297         {
00298             HashTable<label, edge, Hash<edge> > oldSharedEdges(globalShared);
00299 
00300             globalShared.clear();
00301 
00302             for
00303             (
00304                 HashTable<label, edge, Hash<edge> >::const_iterator iter =
00305                     oldSharedEdges.begin();
00306                 iter != oldSharedEdges.end();
00307                 ++iter
00308             )
00309             {
00310                 if (iter() != -1)
00311                 {
00312                     globalShared.insert(iter.key(), iter());
00313                 }
00314             }
00315             if (debug)
00316             {
00317                 Pout<< "globalMeshData::calcSharedEdges : Filtered "
00318                     << oldSharedEdges.size()
00319                     << " down to " << globalShared.size() << endl;
00320             }
00321         }
00322 
00323 
00324         // Send back to slaves.
00325         if (Pstream::parRun())
00326         {
00327             for
00328             (
00329                 int slave=Pstream::firstSlave();
00330                 slave<=Pstream::lastSlave();
00331                 slave++
00332             )
00333             {
00334                 // Receive the edges using shared points from the slave.
00335                 OPstream toSlave(Pstream::blocking, slave);
00336                 toSlave << globalShared;
00337             }
00338         }
00339     }
00340     else
00341     {
00342         // Send local edges to master
00343         {
00344             OPstream toMaster(Pstream::blocking, Pstream::masterNo());
00345 
00346             toMaster << localShared;
00347         }
00348         // Receive merged edges from master.
00349         {
00350             IPstream fromMaster(Pstream::blocking, Pstream::masterNo());
00351 
00352             fromMaster >> globalShared;
00353         }
00354     }
00355 
00356     // Now use the global shared edges list (globalShared) to classify my local
00357     // ones (localShared)
00358 
00359     nGlobalEdges_ = globalShared.size();
00360 
00361     DynamicList<label> dynSharedEdgeLabels(globalShared.size());
00362     DynamicList<label> dynSharedEdgeAddr(globalShared.size());
00363 
00364     for
00365     (
00366         HashTable<labelList, edge, Hash<edge> >::const_iterator iter =
00367             localShared.begin();
00368         iter != localShared.end();
00369         ++iter
00370     )
00371     {
00372         const edge& e = iter.key();
00373 
00374         HashTable<label, edge, Hash<edge> >::const_iterator edgeFnd =
00375             globalShared.find(e);
00376 
00377         if (edgeFnd != globalShared.end())
00378         {
00379             // My local edge is indeed a shared one. Go through all local edge
00380             // labels with this point combination.
00381             const labelList& edgeLabels = iter();
00382 
00383             forAll(edgeLabels, i)
00384             {
00385                 // Store label of local mesh edge
00386                 dynSharedEdgeLabels.append(edgeLabels[i]);
00387 
00388                 // Store label of shared edge
00389                 dynSharedEdgeAddr.append(edgeFnd());
00390             }
00391         }
00392     }
00393 
00394     sharedEdgeLabelsPtr_ = new labelList();
00395     labelList& sharedEdgeLabels = *sharedEdgeLabelsPtr_;
00396     sharedEdgeLabels.transfer(dynSharedEdgeLabels);
00397 
00398     sharedEdgeAddrPtr_ = new labelList();
00399     labelList& sharedEdgeAddr = *sharedEdgeAddrPtr_;
00400     sharedEdgeAddr.transfer(dynSharedEdgeAddr);
00401 
00402     if (debug)
00403     {
00404         Pout<< "globalMeshData : nGlobalEdges_:" << nGlobalEdges_ << nl
00405             << "globalMeshData : sharedEdgeLabels:" << sharedEdgeLabels.size()
00406             << nl
00407             << "globalMeshData : sharedEdgeAddr:" << sharedEdgeAddr.size()
00408             << endl;
00409     }
00410 }
00411 
00412 
00413 // Helper function to count coincident faces. This part used to be
00414 // in updateMesh but I've had to move it to a separate function
00415 // because of aliasing optimisation errors in icc9.1 on the
00416 // Itanium.
00417 Foam::label Foam::globalMeshData::countCoincidentFaces
00418 (
00419     const scalar tolDim,
00420     const vectorField& separationDist
00421 )
00422 {
00423     label nCoincident = 0;
00424 
00425     forAll(separationDist, faceI)
00426     {
00427         if (mag(separationDist[faceI]) < tolDim)
00428         {
00429             // Faces coincide
00430             nCoincident++;
00431         }
00432     }
00433     return nCoincident;
00434 }
00435 
00436 
00437 // * * * * * * * * * * * * * * * * Constructors  * * * * * * * * * * * * * * //
00438 
00439 // Construct from polyMesh
00440 Foam::globalMeshData::globalMeshData(const polyMesh& mesh)
00441 :
00442     processorTopology(mesh.boundaryMesh()),
00443     mesh_(mesh),
00444     bb_(vector::zero, vector::zero),
00445     nTotalPoints_(-1),
00446     nTotalFaces_(-1),
00447     nTotalCells_(-1),
00448     processorPatches_(0),
00449     processorPatchIndices_(0),
00450     processorPatchNeighbours_(0),
00451     nGlobalPoints_(-1),
00452     sharedPointLabels_(0),
00453     sharedPointAddr_(0),
00454     sharedPointGlobalLabelsPtr_(NULL),
00455     nGlobalEdges_(-1),
00456     sharedEdgeLabelsPtr_(NULL),
00457     sharedEdgeAddrPtr_(NULL)
00458 {
00459     updateMesh();
00460 }
00461 
00462 
00463 // Read constructor given IOobject and a polyMesh reference
00464 Foam::globalMeshData::globalMeshData(const IOobject& io, const polyMesh& mesh)
00465 :
00466     processorTopology(mesh.boundaryMesh()),
00467     mesh_(mesh),
00468     bb_(mesh.points()),
00469     nTotalPoints_(-1),
00470     nTotalFaces_(-1),
00471     nTotalCells_(-1),
00472     processorPatches_(0),
00473     processorPatchIndices_(0),
00474     processorPatchNeighbours_(0),
00475     nGlobalPoints_(-1),
00476     sharedPointLabels_(0),
00477     sharedPointAddr_(0),
00478     sharedPointGlobalLabelsPtr_(NULL),
00479     nGlobalEdges_(-1),
00480     sharedEdgeLabelsPtr_(NULL),
00481     sharedEdgeAddrPtr_(NULL)
00482 {
00483     initProcAddr();
00484 
00485     IOdictionary dict(io);
00486 
00487     dict.lookup("nTotalPoints") >> nTotalPoints_;
00488     dict.lookup("nTotalFaces") >> nTotalFaces_;
00489     dict.lookup("nTotalCells") >> nTotalCells_;
00490     dict.lookup("nGlobalPoints") >> nGlobalPoints_;
00491     dict.lookup("sharedPointLabels") >> sharedPointLabels_;
00492     dict.lookup("sharedPointAddr") >> sharedPointAddr_;
00493     labelList sharedPointGlobalLabels(dict.lookup("sharedPointGlobalLabels"));
00494 
00495     sharedPointGlobalLabelsPtr_ = new labelList(sharedPointGlobalLabels);
00496 }
00497 
00498 
00499 // * * * * * * * * * * * * * * * * Destructor  * * * * * * * * * * * * * * * //
00500 
00501 Foam::globalMeshData::~globalMeshData()
00502 {
00503     clearOut();
00504 }
00505 
00506 
00507 void Foam::globalMeshData::clearOut()
00508 {
00509     deleteDemandDrivenData(sharedPointGlobalLabelsPtr_);
00510     // Edge
00511     nGlobalPoints_ = -1;
00512     deleteDemandDrivenData(sharedEdgeLabelsPtr_);
00513     deleteDemandDrivenData(sharedEdgeAddrPtr_);
00514 }
00515 
00516 
00517 // * * * * * * * * * * * * * * * Member Functions  * * * * * * * * * * * * * //
00518 
00519 // Return shared point global labels.
00520 const Foam::labelList& Foam::globalMeshData::sharedPointGlobalLabels() const
00521 {
00522     if (!sharedPointGlobalLabelsPtr_)
00523     {
00524         sharedPointGlobalLabelsPtr_ = new labelList(sharedPointLabels_.size());
00525         labelList& sharedPointGlobalLabels = *sharedPointGlobalLabelsPtr_;
00526 
00527         IOobject addrHeader
00528         (
00529             "pointProcAddressing",
00530             mesh_.facesInstance()/mesh_.meshSubDir,
00531             mesh_,
00532             IOobject::MUST_READ
00533         );
00534 
00535         if (addrHeader.headerOk())
00536         {
00537             // There is a pointProcAddressing file so use it to get labels
00538             // on the original mesh
00539             Pout<< "globalMeshData::sharedPointGlobalLabels : "
00540                 << "Reading pointProcAddressing" << endl;
00541 
00542             labelIOList pointProcAddressing(addrHeader);
00543 
00544             forAll(sharedPointLabels_, i)
00545             {
00546                 // Get my mesh point
00547                 label pointI = sharedPointLabels_[i];
00548 
00549                 // Map to mesh point of original mesh
00550                 sharedPointGlobalLabels[i] = pointProcAddressing[pointI];
00551             }
00552         }
00553         else
00554         {
00555             Pout<< "globalMeshData::sharedPointGlobalLabels :"
00556                 << " Setting pointProcAddressing to -1" << endl;
00557 
00558             sharedPointGlobalLabels = -1;
00559         }
00560     }
00561     return *sharedPointGlobalLabelsPtr_;
00562 }
00563 
00564 
00565 // Collect coordinates of shared points. (does parallel communication!)
00566 Foam::pointField Foam::globalMeshData::sharedPoints() const
00567 {
00568     // Get all processors to send their shared points to master.
00569     // (not very efficient)
00570 
00571     pointField sharedPoints(nGlobalPoints_);
00572 
00573     if (Pstream::master())
00574     {
00575         // Master:
00576         // insert my own data first
00577         forAll(sharedPointLabels_, i)
00578         {
00579             label sharedPointI = sharedPointAddr_[i];
00580 
00581             sharedPoints[sharedPointI] = mesh_.points()[sharedPointLabels_[i]];
00582         }
00583 
00584         // Receive data from slaves and insert
00585         for
00586         (
00587             int slave=Pstream::firstSlave();
00588             slave<=Pstream::lastSlave();
00589             slave++
00590         )
00591         {
00592             IPstream fromSlave(Pstream::blocking, slave);
00593 
00594             labelList nbrSharedPointAddr;
00595             pointField nbrSharedPoints;
00596             fromSlave >> nbrSharedPointAddr >> nbrSharedPoints;
00597 
00598             forAll(nbrSharedPointAddr, i)
00599             {
00600                 label sharedPointI = nbrSharedPointAddr[i];
00601 
00602                 sharedPoints[sharedPointI] = nbrSharedPoints[i];
00603             }
00604         }
00605 
00606         // Send back
00607         for
00608         (
00609             int slave=Pstream::firstSlave();
00610             slave<=Pstream::lastSlave();
00611             slave++
00612         )
00613         {
00614             OPstream toSlave
00615             (
00616                 Pstream::blocking,
00617                 slave,
00618                 sharedPoints.size()*sizeof(vector::zero)
00619             );
00620             toSlave << sharedPoints;
00621         }
00622     }
00623     else
00624     {
00625         // Slave:
00626         // send points
00627         {
00628             OPstream toMaster(Pstream::blocking, Pstream::masterNo());
00629 
00630             toMaster
00631                 << sharedPointAddr_ 
00632                 << UIndirectList<point>(mesh_.points(), sharedPointLabels_)();
00633         }
00634 
00635         // Receive sharedPoints
00636         {
00637             IPstream fromMaster(Pstream::blocking, Pstream::masterNo());
00638             fromMaster >> sharedPoints;
00639         }
00640     }
00641 
00642     return sharedPoints;
00643 }
00644 
00645 
00646 // Collect coordinates of shared points. (does parallel communication!)
00647 Foam::pointField Foam::globalMeshData::geometricSharedPoints() const
00648 {
00649     // Get coords of my shared points
00650     pointField sharedPoints(sharedPointLabels_.size());
00651 
00652     forAll(sharedPointLabels_, i)
00653     {
00654         label meshPointI = sharedPointLabels_[i];
00655 
00656         sharedPoints[i] = mesh_.points()[meshPointI];
00657     }
00658 
00659     // Append from all processors
00660     combineReduce(sharedPoints, plusEqOp<pointField>());
00661 
00662     // Merge tolerance
00663     scalar tolDim = matchTol_ * bb_.mag();
00664 
00665     // And see how many are unique
00666     labelList pMap;
00667     pointField mergedPoints;
00668 
00669     mergePoints
00670     (
00671         sharedPoints,   // coordinates to merge
00672         tolDim,         // tolerance
00673         false,          // verbosity
00674         pMap,
00675         mergedPoints
00676     );
00677 
00678     return mergedPoints;
00679 }
00680 
00681 
00682 Foam::label Foam::globalMeshData::nGlobalEdges() const
00683 {
00684     if (nGlobalEdges_ == -1)
00685     {
00686         calcSharedEdges();
00687     }
00688     return nGlobalEdges_;
00689 }
00690 
00691 
00692 const Foam::labelList& Foam::globalMeshData::sharedEdgeLabels() const
00693 {
00694     if (!sharedEdgeLabelsPtr_)
00695     {
00696         calcSharedEdges();
00697     }
00698     return *sharedEdgeLabelsPtr_;
00699 }
00700 
00701 
00702 const Foam::labelList& Foam::globalMeshData::sharedEdgeAddr() const
00703 {
00704     if (!sharedEdgeAddrPtr_)
00705     {
00706         calcSharedEdges();
00707     }
00708     return *sharedEdgeAddrPtr_;
00709 }
00710 
00711 
00712 void Foam::globalMeshData::movePoints(const pointField& newPoints)
00713 {
00714     // Topology does not change and we don't store any geometry so nothing
00715     // needs to be done.
00716 }
00717 
00718 
00719 // Update all data after morph
00720 void Foam::globalMeshData::updateMesh()
00721 {
00722     // Clear out old data
00723     clearOut();
00724 
00725     // Do processor patch addressing
00726     initProcAddr();
00727 
00728     // Note: boundBox does reduce
00729     bb_ = boundBox(mesh_.points());
00730 
00731     scalar tolDim = matchTol_ * bb_.mag();
00732 
00733     if (debug)
00734     {
00735         Pout<< "globalMeshData : bb_:" << bb_
00736             << " merge dist:" << tolDim << endl;
00737     }
00738 
00739 
00740     // Option 1. Topological
00741     {
00742         // Calculate all shared points. This does all the hard work.
00743         globalPoints parallelPoints(mesh_);
00744 
00745         // Copy data out.
00746         nGlobalPoints_ = parallelPoints.nGlobalPoints();
00747         sharedPointLabels_ = parallelPoints.sharedPointLabels();
00748         sharedPointAddr_ = parallelPoints.sharedPointAddr();
00749     }
00751     //{
00752     //    // Calculate all shared points. This does all the hard work.
00753     //    geomGlobalPoints parallelPoints(mesh_, tolDim);
00754     //
00755     //    // Copy data out.
00756     //    nGlobalPoints_ = parallelPoints.nGlobalPoints();
00757     //    sharedPointLabels_ = parallelPoints.sharedPointLabels();
00758     //    sharedPointAddr_ = parallelPoints.sharedPointAddr();
00759     //
00760     //    nGlobalEdges_ = parallelPoints.nGlobalEdges();
00761     //    sharedEdgeLabels_ = parallelPoints.sharedEdgeLabels();
00762     //    sharedEdgeAddr_ = parallelPoints.sharedEdgeAddr();
00763     //}
00764 
00765     // Total number of faces. Start off from all faces. Remove coincident
00766     // processor faces (on highest numbered processor) before summing.
00767     nTotalFaces_ = mesh_.nFaces();
00768 
00769     // Do not count processor-patch faces that are coincident.
00770     forAll(processorPatches_, i)
00771     {
00772         label patchI = processorPatches_[i];
00773 
00774         const processorPolyPatch& procPatch =
00775             refCast<const processorPolyPatch>(mesh_.boundaryMesh()[patchI]);
00776 
00777         if (Pstream::myProcNo() > procPatch.neighbProcNo())
00778         {
00779             // Uncount my faces. Handle cyclics separately.
00780 
00781             if (procPatch.separated())
00782             {
00783                 const vectorField& separationDist = procPatch.separation();
00784 
00785                 nTotalFaces_ -= countCoincidentFaces(tolDim, separationDist);
00786             }
00787             else
00788             {
00789                 // Normal, unseparated processor patch. Remove duplicates.
00790                 nTotalFaces_ -= procPatch.size();
00791             }
00792         }
00793     }
00794     reduce(nTotalFaces_, sumOp<label>());
00795 
00796     if (debug)
00797     {
00798         Pout<< "globalMeshData : nTotalFaces_:" << nTotalFaces_ << endl;
00799     }
00800 
00801 
00802     nTotalCells_ = mesh_.nCells();
00803     reduce(nTotalCells_, sumOp<label>());
00804 
00805     if (debug)
00806     {
00807         Pout<< "globalMeshData : nTotalCells_:" << nTotalCells_ << endl;
00808     }
00809 
00810     nTotalPoints_ = mesh_.nPoints();
00811 
00812     // Correct points for duplicate ones. We have
00813     // - points shared between 2 processor patches only. Count only on
00814     //   lower numbered processor. Make sure to count only once since points
00815     //   can be on multiple patches on the same processor.
00816     // - globally shared points.
00817 
00818     if (Pstream::parRun())
00819     {
00820         const label UNSET = 0;      // not set
00821         const label SHARED = 1;     // globally shared
00822         const label VISITED = 2;    // corrected for
00823 
00824         // Mark globally shared points
00825         PackedList<2> pointStatus(mesh_.nPoints(), UNSET);
00826 
00827         forAll(sharedPointLabels_, i)
00828         {
00829             label meshPointI = sharedPointLabels_[i];
00830 
00831             pointStatus.set(meshPointI, SHARED);
00832         }
00833 
00834         // Send patch local points
00835         forAll(processorPatches_, i)
00836         {
00837             label patchI = processorPatches_[i];
00838 
00839             const processorPolyPatch& procPatch =
00840                 refCast<const processorPolyPatch>(mesh_.boundaryMesh()[patchI]);
00841 
00842             OPstream toNeighbour(Pstream::blocking, procPatch.neighbProcNo());
00843 
00844             toNeighbour << procPatch.localPoints();
00845         }
00846 
00847         // Receive patch local points and uncount if coincident (and not shared)
00848         forAll(processorPatches_, i)
00849         {
00850             label patchI = processorPatches_[i];
00851 
00852             const processorPolyPatch& procPatch =
00853                 refCast<const processorPolyPatch>(mesh_.boundaryMesh()[patchI]);
00854 
00855             IPstream fromNeighbour(Pstream::blocking, procPatch.neighbProcNo());
00856 
00857             pointField nbrPoints(fromNeighbour);
00858 
00859             if (Pstream::myProcNo() > procPatch.neighbProcNo())
00860             {
00861                 labelList pMap;
00862                 matchPoints
00863                 (
00864                     procPatch.localPoints(),
00865                     nbrPoints,
00866                     scalarField(procPatch.nPoints(), tolDim),   // tolerance
00867                     false,      // verbosity
00868                     pMap        // map from my points to nbrPoints
00869                 );
00870 
00871                 forAll(pMap, patchPointI)
00872                 {
00873                     label meshPointI = procPatch.meshPoints()[patchPointI];
00874 
00875                     label stat = pointStatus.get(meshPointI);
00876 
00877                     if (stat == UNSET)
00878                     {
00879                         // Mark point as visited so if point is on multiple proc
00880                         // patches it only gets uncounted once.
00881                         pointStatus.set(meshPointI, VISITED);
00882 
00883                         if (pMap[patchPointI] != -1)
00884                         {
00885                             // Points share same coordinate so uncount.
00886                             nTotalPoints_--;
00887                         }
00888                     }
00889                 }
00890             }
00891         }
00892         // Sum all points
00893         reduce(nTotalPoints_, sumOp<label>());
00894     }
00895 
00896     // nTotalPoints has not been corrected yet for shared points. For these
00897     // just collect all their coordinates and count unique ones.
00898 
00899     label mySharedPoints = sharedPointLabels_.size();
00900     reduce(mySharedPoints, sumOp<label>());
00901 
00902     // Collect and merge shared points (does parallel communication)
00903     pointField geomSharedPoints(geometricSharedPoints());
00904     label nGeomSharedPoints = geomSharedPoints.size();
00905 
00906     // Shared points merged down to mergedPoints size.
00907     nTotalPoints_ -= mySharedPoints - nGeomSharedPoints;
00908 
00909     if (debug)
00910     {
00911         Pout<< "globalMeshData : nTotalPoints_:" << nTotalPoints_ << endl;
00912     }
00913 
00914     //
00915     // Now we have all info we wanted.
00916     // Do some checking (if debug is set)
00917     //
00918 
00919     if (debug)
00920     {
00921         if (Pstream::master())
00922         {
00923             // We have the geometricSharedPoints already so write them.
00924             // Ideally would like to write the networks of connected points as
00925             // well but this is harder. (Todo)
00926             Pout<< "globalMeshData : writing geometrically separated shared"
00927                 << " points to geomSharedPoints.obj" << endl;
00928 
00929             OFstream str("geomSharedPoints.obj");
00930 
00931             forAll(geomSharedPoints, i)
00932             {
00933                 const point& pt = geomSharedPoints[i];
00934 
00935                 str << "v " << pt.x() << ' ' << pt.y() << ' ' << pt.z()
00936                     << nl;
00937             }
00938         }
00939     }
00940 }
00941 
00942 
00943 // Write data
00944 bool Foam::globalMeshData::write() const
00945 {
00946     IOdictionary dict
00947     (
00948         IOobject
00949         (
00950             "parallelData",
00951             mesh_.facesInstance(),
00952             mesh_.meshSubDir,
00953             mesh_
00954         )
00955     );
00956 
00957     dict.add("nTotalPoints", nTotalPoints());
00958     dict.add("nTotalFaces", nTotalFaces());
00959     dict.add("nTotalCells", nTotalCells());
00960 
00961     dict.add("nGlobalPoints", nGlobalPoints());
00962     dict.add("sharedPointLabels", sharedPointLabels());
00963     dict.add("sharedPointAddr", sharedPointAddr());
00964     dict.add("sharedPointGlobalLabels", sharedPointGlobalLabels());
00965 
00966     return dict.writeObject
00967     (
00968         IOstream::ASCII,
00969         IOstream::currentVersion,
00970         IOstream::UNCOMPRESSED
00971     );
00972 }
00973 
00974 
00975 // * * * * * * * * * * * * * * * Ostream Operators * * * * * * * * * * * * * //
00976 
00977 Foam::Ostream& Foam::operator<<(Ostream& os, const globalMeshData& p)
00978 {
00979     os  << "nTotalPoints " << p.nTotalPoints() << token::END_STATEMENT << nl
00980         << "nTotalFaces " << p.nTotalFaces() << token::END_STATEMENT << nl
00981         << "nTotalCells " << p.nTotalCells() << token::END_STATEMENT << nl
00982         << "nGlobalPoints " << p.nGlobalPoints() << token::END_STATEMENT << nl
00983         << "sharedPointLabels " << p.sharedPointLabels()
00984         << token::END_STATEMENT << nl
00985         << "sharedPointAddr " << p.sharedPointAddr()
00986         << token::END_STATEMENT << endl;
00987 
00988     return os;
00989 }
00990 
00991 
00992 // ************************************************************************* //
Copyright © 2000-2009 OpenCFD Ltd