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

extendedUpwindCellToFaceStencil.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 "extendedUpwindCellToFaceStencil.H"
00028 #include "cellToFaceStencil.H"
00029 #include "syncTools.H"
00030 #include "SortableList.H"
00031 
00032 // * * * * * * * * * * * * * Private Member Functions  * * * * * * * * * * * //
00033 
00034 void Foam::extendedUpwindCellToFaceStencil::selectOppositeFaces
00035 (
00036     const boolList& nonEmptyFace,
00037     const scalar minOpposedness,
00038     const label faceI,
00039     const label cellI,
00040     DynamicList<label>& oppositeFaces
00041 ) const
00042 {
00043     const vectorField& areas = mesh_.faceAreas();
00044     const labelList& own = mesh_.faceOwner();
00045     const cell& cFaces = mesh_.cells()[cellI];
00046 
00047     SortableList<scalar> opposedness(cFaces.size(), -GREAT);
00048 
00049     // Pick up all the faces that oppose this one.
00050     forAll(cFaces, i)
00051     {
00052         label otherFaceI = cFaces[i];
00053 
00054         if (otherFaceI != faceI && nonEmptyFace[otherFaceI])
00055         {
00056             if ((own[otherFaceI] == cellI) == (own[faceI] == cellI))
00057             {
00058                 opposedness[i] = -(areas[otherFaceI] & areas[faceI]);
00059             }
00060             else
00061             {
00062                 opposedness[i] = (areas[otherFaceI] & areas[faceI]);
00063             }
00064         }
00065     }
00066 
00067     label sz = opposedness.size();
00068 
00069     oppositeFaces.clear();
00070 
00071     scalar myAreaSqr = magSqr(areas[faceI]);
00072 
00073     if (myAreaSqr > VSMALL)
00074     {
00075         forAll(opposedness, i)
00076         {
00077             opposedness[i] /= myAreaSqr;
00078         }
00079         // Sort in incrementing order
00080         opposedness.sort();
00081 
00082         // Pick largest no matter what
00083         oppositeFaces.append(cFaces[opposedness.indices()[sz-1]]);
00084 
00085         for (label i = sz-2; i >= 0; --i)
00086         {
00087             if (opposedness[i] < minOpposedness)
00088             {
00089                 break;
00090             }
00091             oppositeFaces.append(cFaces[opposedness.indices()[i]]);
00092         }
00093     }
00094     else
00095     {
00096         // Sort in incrementing order
00097         opposedness.sort();
00098 
00099         // Tiny face. Do what?
00100         // Pick largest no matter what
00101         oppositeFaces.append(cFaces[opposedness.indices()[sz-1]]);
00102     }
00103 }
00104 
00105 
00106 void Foam::extendedUpwindCellToFaceStencil::transportStencil
00107 (
00108     const boolList& nonEmptyFace,
00109     const labelListList& faceStencil,
00110     const scalar minOpposedness,
00111     const label faceI,
00112     const label cellI,
00113     const bool stencilHasNeighbour,
00114 
00115     DynamicList<label>& oppositeFaces,
00116     labelHashSet& faceStencilSet,
00117     labelList& transportedStencil
00118 ) const
00119 {
00120     label globalOwn = faceStencil[faceI][0];
00121     label globalNei = -1;
00122     if (stencilHasNeighbour && faceStencil[faceI].size() >= 2)
00123     {
00124         globalNei = faceStencil[faceI][1];
00125     }
00126 
00127 
00128     selectOppositeFaces
00129     (
00130         nonEmptyFace,
00131         minOpposedness,
00132         faceI,
00133         cellI,
00134         oppositeFaces
00135     );
00136 
00137     // Collect all stencils of oppositefaces
00138     faceStencilSet.clear();
00139     forAll(oppositeFaces, i)
00140     {
00141         const labelList& fStencil = faceStencil[oppositeFaces[i]];
00142 
00143         forAll(fStencil, j)
00144         {
00145             label globalI = fStencil[j];
00146 
00147             if (globalI != globalOwn && globalI != globalNei)
00148             {
00149                 faceStencilSet.insert(globalI);
00150             }
00151         }
00152     }
00153 
00154     // Add my owner and neighbour first.
00155     if (stencilHasNeighbour)
00156     {
00157         transportedStencil.setSize(faceStencilSet.size()+2);
00158         label n = 0;
00159         transportedStencil[n++] = globalOwn;
00160         transportedStencil[n++] = globalNei;
00161 
00162         forAllConstIter(labelHashSet, faceStencilSet, iter)
00163         {
00164             if (iter.key() != globalOwn && iter.key() != globalNei)
00165             {
00166                 transportedStencil[n++] = iter.key();
00167             }
00168         }
00169         if (n != transportedStencil.size())
00170         {
00171             FatalErrorIn
00172             (
00173                 "extendedUpwindCellToFaceStencil::transportStencil(..)"
00174             )   << "problem:" << faceStencilSet
00175                 << abort(FatalError);
00176         }
00177     }
00178     else
00179     {
00180         transportedStencil.setSize(faceStencilSet.size()+1);
00181         label n = 0;
00182         transportedStencil[n++] = globalOwn;
00183 
00184         forAllConstIter(labelHashSet, faceStencilSet, iter)
00185         {
00186             if (iter.key() != globalOwn)
00187             {
00188                 transportedStencil[n++] = iter.key();
00189             }
00190         }
00191         if (n != transportedStencil.size())
00192         {
00193             FatalErrorIn
00194             (
00195                 "extendedUpwindCellToFaceStencil::transportStencil(..)"
00196             )   << "problem:" << faceStencilSet
00197                 << abort(FatalError);
00198         }
00199     }
00200 }
00201 
00202 
00203 void Foam::extendedUpwindCellToFaceStencil::transportStencils
00204 (
00205     const labelListList& faceStencil,
00206     const scalar minOpposedness,
00207     labelListList& ownStencil,
00208     labelListList& neiStencil
00209 )
00210 {
00211     const polyBoundaryMesh& patches = mesh_.boundaryMesh();
00212     const label nBnd = mesh_.nFaces()-mesh_.nInternalFaces();
00213     const labelList& own = mesh_.faceOwner();
00214     const labelList& nei = mesh_.faceNeighbour();
00215 
00216     // Work arrays
00217     DynamicList<label> oppositeFaces;
00218     labelHashSet faceStencilSet;
00219 
00220 
00221     // For quick detection of empty faces
00222     boolList nonEmptyFace(mesh_.nFaces(), true);
00223     forAll(patches, patchI)
00224     {
00225         const polyPatch& pp = patches[patchI];
00226 
00227         if (isA<emptyPolyPatch>(pp))
00228         {
00229             label faceI = pp.start();
00230             forAll(pp, i)
00231             {
00232                 nonEmptyFace[faceI++] = false;
00233             }
00234         }
00235     }
00236 
00237 
00238     // Do the owner side
00239     // ~~~~~~~~~~~~~~~~~
00240     // stencil is synchronised at entry so no need to swap.
00241 
00242     ownStencil.setSize(mesh_.nFaces());
00243 
00244     // Internal faces
00245     for (label faceI = 0; faceI < mesh_.nInternalFaces(); faceI++)
00246     {
00247         // Get stencil as owner + neighbour + stencil from 'opposite' faces
00248         transportStencil
00249         (
00250             nonEmptyFace,
00251             faceStencil,
00252             minOpposedness,
00253             faceI,
00254             own[faceI],
00255             true,                   //stencilHasNeighbour
00256             oppositeFaces,
00257             faceStencilSet,
00258             ownStencil[faceI]
00259         );
00260     }
00261     // Boundary faces
00262     forAll(patches, patchI)
00263     {
00264         const polyPatch& pp = patches[patchI];
00265         label faceI = pp.start();
00266 
00267         if (pp.coupled())
00268         {
00269             forAll(pp, i)
00270             {
00271                 transportStencil
00272                 (
00273                     nonEmptyFace,
00274                     faceStencil,
00275                     minOpposedness,
00276                     faceI,
00277                     own[faceI],
00278                     true,                   //stencilHasNeighbour
00279 
00280                     oppositeFaces,
00281                     faceStencilSet,
00282                     ownStencil[faceI]
00283                 );
00284                 faceI++;
00285             }
00286         }
00287         else if (!isA<emptyPolyPatch>(pp))
00288         {
00289             forAll(pp, i)
00290             {
00291                 // faceStencil does not contain neighbour
00292                 transportStencil
00293                 (
00294                     nonEmptyFace,
00295                     faceStencil,
00296                     minOpposedness,
00297                     faceI,
00298                     own[faceI],
00299                     false,                  //stencilHasNeighbour
00300 
00301                     oppositeFaces,
00302                     faceStencilSet,
00303                     ownStencil[faceI]
00304                 );
00305                 faceI++;
00306             }
00307         }
00308     }
00309 
00310 
00311     // Swap coupled boundary stencil
00312     // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
00313     
00314     labelListList neiBndStencil(nBnd);
00315     for (label faceI = mesh_.nInternalFaces(); faceI < mesh_.nFaces(); faceI++)
00316     {
00317         neiBndStencil[faceI-mesh_.nInternalFaces()] = ownStencil[faceI];
00318     }
00319     syncTools::swapBoundaryFaceList(mesh_, neiBndStencil, false);
00320 
00321 
00322     // Do the neighbour side
00323     // ~~~~~~~~~~~~~~~~~~~~~
00324     // - internal faces : get opposite faces on neighbour side
00325     // - boundary faces : empty
00326     // - coupled faces  : in neiBndStencil
00327 
00328     neiStencil.setSize(mesh_.nFaces());
00329 
00330     // Internal faces
00331     for (label faceI = 0; faceI < mesh_.nInternalFaces(); faceI++)
00332     {
00333         transportStencil
00334         (
00335             nonEmptyFace,
00336             faceStencil,
00337             minOpposedness,
00338             faceI,
00339             nei[faceI],
00340             true,                   //stencilHasNeighbour
00341 
00342             oppositeFaces,
00343             faceStencilSet,
00344             neiStencil[faceI]
00345         );
00346     }
00347 
00348     // Boundary faces
00349     forAll(patches, patchI)
00350     {
00351         const polyPatch& pp = patches[patchI];
00352         label faceI = pp.start();
00353 
00354         if (pp.coupled())
00355         {
00356             forAll(pp, i)
00357             {
00358                 neiStencil[faceI].transfer
00359                 (
00360                     neiBndStencil[faceI-mesh_.nInternalFaces()]
00361                 );
00362                 faceI++;
00363             }
00364         }
00365         else
00366         {
00367             // Boundary has empty neighbour stencil
00368         }
00369     }
00370 }
00371 
00372 
00373 // * * * * * * * * * * * * * * * * Constructors  * * * * * * * * * * * * * * //
00374 
00375 Foam::extendedUpwindCellToFaceStencil::extendedUpwindCellToFaceStencil
00376 (
00377     const cellToFaceStencil& stencil,
00378     const bool pureUpwind,
00379     const scalar minOpposedness
00380 )
00381 :
00382     extendedCellToFaceStencil(stencil.mesh()),
00383     pureUpwind_(pureUpwind)
00384 {
00385     //forAll(stencil, faceI)
00386     //{
00387     //    const labelList& fCells = stencil[faceI];
00388     //
00389     //    Pout<< "Face:" << faceI << " at:" << mesh_.faceCentres()[faceI]
00390     //        << endl;
00391     //
00392     //    forAll(fCells, i)
00393     //    {
00394     //        label globalI = fCells[i];
00395     //
00396     //        if (globalI < mesh_.nCells())
00397     //        {
00398     //            Pout<< "    cell:" << globalI
00399     //                << " at:" << mesh_.cellCentres()[globalI] << endl;
00400     //        }
00401     //        else
00402     //        {
00403     //            label faceI = globalI-mesh_.nCells() + mesh_.nInternalFaces();
00404     //
00405     //            Pout<< "    boundary:" << faceI
00406     //                << " at:" << mesh_.faceCentres()[faceI] << endl;
00407     //        }
00408     //    }
00409     //}
00410     //Pout<< endl << endl;
00411 
00412 
00413     // Transport centred stencil to upwind/downwind face
00414     transportStencils
00415     (
00416         stencil,
00417         minOpposedness,
00418         ownStencil_,
00419         neiStencil_
00420     );
00421 
00422     ownMapPtr_ = calcDistributeMap
00423     (
00424         stencil.mesh(),
00425         stencil.globalNumbering(),
00426         ownStencil_
00427     );
00428 
00429     neiMapPtr_ = calcDistributeMap
00430     (
00431         stencil.mesh(),
00432         stencil.globalNumbering(),
00433         neiStencil_
00434     );
00435 
00436     // stencil now in compact form
00437     if (pureUpwind_)
00438     {
00439         const fvMesh& mesh = dynamic_cast<const fvMesh&>(stencil.mesh());
00440 
00441         List<List<point> > stencilPoints(ownStencil_.size());
00442 
00443         // Owner stencil
00444         // ~~~~~~~~~~~~~
00445 
00446         collectData(ownMapPtr_(), ownStencil_, mesh.C(), stencilPoints);
00447 
00448         // Mask off all stencil points on wrong side of face
00449         forAll(stencilPoints, faceI)
00450         {
00451             const point& fc = mesh.faceCentres()[faceI];
00452             const vector& fArea = mesh.faceAreas()[faceI];
00453 
00454             const List<point>& points = stencilPoints[faceI];
00455             const labelList& stencil = ownStencil_[faceI];
00456 
00457             DynamicList<label> newStencil(stencil.size());
00458             forAll(points, i)
00459             {
00460                 if (((points[i]-fc) & fArea) < 0)
00461                 {
00462                     newStencil.append(stencil[i]);
00463                 }
00464             }
00465             if (newStencil.size() != stencil.size())
00466             {
00467                 ownStencil_[faceI].transfer(newStencil);
00468             }
00469         }
00470 
00471 
00472         // Neighbour stencil
00473         // ~~~~~~~~~~~~~~~~~
00474 
00475         collectData(neiMapPtr_(), neiStencil_, mesh.C(), stencilPoints);
00476 
00477         // Mask off all stencil points on wrong side of face
00478         forAll(stencilPoints, faceI)
00479         {
00480             const point& fc = mesh.faceCentres()[faceI];
00481             const vector& fArea = mesh.faceAreas()[faceI];
00482 
00483             const List<point>& points = stencilPoints[faceI];
00484             const labelList& stencil = neiStencil_[faceI];
00485 
00486             DynamicList<label> newStencil(stencil.size());
00487             forAll(points, i)
00488             {
00489                 if (((points[i]-fc) & fArea) > 0)
00490                 {
00491                     newStencil.append(stencil[i]);
00492                 }
00493             }
00494             if (newStencil.size() != stencil.size())
00495             {
00496                 neiStencil_[faceI].transfer(newStencil);
00497             }
00498         }
00499 
00500         // Note: could compact schedule as well. for if cells are not needed
00501         // across any boundary anymore. However relatively rare.
00502     }
00503 }
00504 
00505 
00506 Foam::extendedUpwindCellToFaceStencil::extendedUpwindCellToFaceStencil
00507 (
00508     const cellToFaceStencil& stencil
00509 )
00510 :
00511     extendedCellToFaceStencil(stencil.mesh()),
00512     pureUpwind_(true)
00513 {
00514     // Calculate stencil points with full stencil
00515 
00516     ownStencil_ = stencil;
00517 
00518     ownMapPtr_ = calcDistributeMap
00519     (
00520         stencil.mesh(),
00521         stencil.globalNumbering(),
00522         ownStencil_
00523     );
00524 
00525     const fvMesh& mesh = dynamic_cast<const fvMesh&>(stencil.mesh());
00526 
00527     List<List<point> > stencilPoints(ownStencil_.size());
00528     collectData(ownMapPtr_(), ownStencil_, mesh.C(), stencilPoints);
00529 
00530     // Split stencil into owner and neighbour
00531     neiStencil_.setSize(ownStencil_.size());
00532 
00533     forAll(stencilPoints, faceI)
00534     {
00535         const point& fc = mesh.faceCentres()[faceI];
00536         const vector& fArea = mesh.faceAreas()[faceI];
00537 
00538         const List<point>& points = stencilPoints[faceI];
00539         const labelList& stencil = ownStencil_[faceI];
00540 
00541         DynamicList<label> newOwnStencil(stencil.size());
00542         DynamicList<label> newNeiStencil(stencil.size());
00543         forAll(points, i)
00544         {
00545             if (((points[i]-fc) & fArea) > 0)
00546             {
00547                 newNeiStencil.append(stencil[i]);
00548             }
00549             else
00550             {
00551                 newOwnStencil.append(stencil[i]);
00552             }
00553         }
00554         if (newNeiStencil.size() > 0)
00555         {
00556             ownStencil_[faceI].transfer(newOwnStencil);
00557             neiStencil_[faceI].transfer(newNeiStencil);
00558         }
00559     }
00560 
00561     // Should compact schedule. Or have both return the same schedule.
00562     neiMapPtr_.reset(new mapDistribute(ownMapPtr_()));
00563 }
00564 
00565 
00566 // ************************************************************************* //
Copyright © 2000-2009 OpenCFD Ltd