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

mirrorFvMesh.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 "mirrorFvMesh.H"
00028 #include "Time.H"
00029 #include "plane.H"
00030 
00031 // * * * * * * * * * * * * * * Static Data Members * * * * * * * * * * * * * //
00032 
00033 const Foam::label Foam::mirrorFvMesh::cellRenumber[8][8] =
00034 {
00035     {-1, -1, -1, -1, -1, -1, -1, -1},    // unknown
00036     {-1, -1, -1, -1, -1, -1, -1, -1},    // 
00037     {-1, -1, -1, -1, -1, -1, -1, -1},    // 
00038     { 0,  3,  2,  1,  4,  7,  6,  5},    // hex
00039     { 2,  1,  0,  5,  4,  3,  6, -1},    // wedge
00040     { 0,  2,  1,  3,  5,  4, -1, -1},    // prism
00041     { 0,  3,  2,  1,  4, -1, -1, -1},    // pyramid
00042     { 2,  1,  0,  3, -1, -1, -1, -1},    // tet
00043 };
00044 
00045 // * * * * * * * * * * * * * * * * Constructors  * * * * * * * * * * * * * * //
00046 
00047 Foam::mirrorFvMesh::mirrorFvMesh(const IOobject& io)
00048 :
00049     fvMesh(io),
00050     mirrorMeshDict_
00051     (
00052         IOobject
00053         (
00054             "mirrorMeshDict",
00055             time().system(),
00056             *this,
00057             IOobject::MUST_READ,
00058             IOobject::NO_WRITE
00059         )
00060     ),
00061     mirrorMeshPtr_(NULL)
00062 {
00063     plane mirrorPlane(mirrorMeshDict_);
00064 
00065     scalar planeTolerance
00066     (
00067         readScalar(mirrorMeshDict_.lookup("planeTolerance"))
00068     );
00069 
00070     const pointField& oldPoints = points();
00071     const faceList& oldFaces = faces();
00072     const cellList& oldCells = cells();
00073     const label nOldInternalFaces = nInternalFaces();
00074     const polyPatchList& oldPatches = boundaryMesh();
00075 
00076     // Mirror the points
00077     Info << "Mirroring points. Old points: " << oldPoints.size();
00078 
00079     pointField newPoints(2*oldPoints.size());
00080     label nNewPoints = 0;
00081 
00082     labelList mirrorPointLookup(oldPoints.size(), -1);
00083 
00084     // Grab the old points
00085     forAll (oldPoints, pointI)
00086     {
00087         newPoints[nNewPoints] = oldPoints[pointI];
00088         nNewPoints++;
00089     }
00090 
00091     forAll (oldPoints, pointI)
00092     {
00093         scalar alpha =
00094             mirrorPlane.normalIntersect
00095             (
00096                 oldPoints[pointI],
00097                 mirrorPlane.normal()
00098             );
00099 
00100         // Check plane on tolerance
00101         if (mag(alpha) > planeTolerance)
00102         {
00103             // The point gets mirrored
00104             newPoints[nNewPoints] =
00105                 oldPoints[pointI] + 2.0*alpha*mirrorPlane.normal();
00106 
00107             // remember the point correspondence
00108             mirrorPointLookup[pointI] = nNewPoints;
00109             nNewPoints++;
00110         }
00111         else
00112         {
00113             // The point is on the plane and does not get mirrored
00114             // Adjust plane location
00115             newPoints[nNewPoints] =
00116                 oldPoints[pointI] + alpha*mirrorPlane.normal();
00117 
00118             mirrorPointLookup[pointI] = pointI;
00119         }
00120     }
00121 
00122     // Reset the size of the point list
00123     Info << " New points: " << nNewPoints << endl;
00124     newPoints.setSize(nNewPoints);
00125 
00126     Info << "Mirroring faces. Old faces: " << oldFaces.size();
00127 
00128     // Algorithm:
00129     // During mirroring, the faces that were previously boundary faces
00130     // in the mirror plane may become ineternal faces. In order to
00131     // deal with the ordering of the faces, the algorithm is split
00132     // into two parts.  For original faces, the internal faces are
00133     // distributed to their owner cells.  Once all internal faces are
00134     // distributed, the boundary faces are visited and if they are in
00135     // the mirror plane they are added to the master cells (the future
00136     // boundary faces are not touched).  After the first phase, the
00137     // internal faces are collected in the cell order and numbering
00138     // information is added.  Then, the internal faces are mirrored
00139     // and the face numbering data is stored for the mirrored section.
00140     // Once all the internal faces are mirrored, the boundary faces
00141     // are added by mirroring the faces patch by patch.
00142 
00143     // Distribute internal faces
00144     labelListList newCellFaces(oldCells.size());
00145 
00146     const unallocLabelList& oldOwnerStart = lduAddr().ownerStartAddr();
00147 
00148     forAll (newCellFaces, cellI)
00149     {
00150         labelList& curFaces = newCellFaces[cellI];
00151 
00152         const label s = oldOwnerStart[cellI];
00153         const label e = oldOwnerStart[cellI + 1];
00154 
00155         curFaces.setSize(e - s);
00156 
00157         forAll (curFaces, i)
00158         {
00159             curFaces[i] = s + i;
00160         }
00161     }
00162 
00163     // Distribute boundary faces.  Remember the faces that have been inserted
00164     // as internal
00165     boolListList insertedBouFace(oldPatches.size());
00166 
00167     forAll (oldPatches, patchI)
00168     {
00169         const polyPatch& curPatch = oldPatches[patchI];
00170         boolList& curInsBouFace = insertedBouFace[patchI];
00171 
00172         curInsBouFace.setSize(curPatch.size());
00173         curInsBouFace = false;
00174 
00175         // Get faceCells for face insertion
00176         const unallocLabelList& curFaceCells = curPatch.faceCells();
00177         const label curStart = curPatch.start();
00178 
00179         forAll (curPatch, faceI)
00180         {
00181             // Find out if the mirrored face is identical to the
00182             // original.  If so, the face needs to become internal and
00183             // added to its owner cell
00184             const face& origFace = curPatch[faceI];
00185 
00186             face mirrorFace(origFace.size());
00187             forAll (mirrorFace, pointI)
00188             {
00189                 mirrorFace[pointI] = mirrorPointLookup[origFace[pointI]];
00190             }
00191 
00192             if (origFace == mirrorFace)
00193             {
00194                 // The mirror is identical to current face.  This will
00195                 // become an internal face
00196                 const label oldSize = newCellFaces[curFaceCells[faceI]].size();
00197 
00198                 newCellFaces[curFaceCells[faceI]].setSize(oldSize + 1);
00199                 newCellFaces[curFaceCells[faceI]][oldSize] = curStart + faceI;
00200 
00201                 curInsBouFace[faceI] = true;
00202             }
00203         }
00204     }
00205 
00206     // Construct the new list of faces.  Boundary faces are added
00207     // last, cush that each patch is mirrored separately.  The
00208     // addressing is stored in two separate arrays: first for the
00209     // original cells (face order has changed) and then for the
00210     // mirrored cells.
00211     labelList masterFaceLookup(oldFaces.size(), -1);
00212     labelList mirrorFaceLookup(oldFaces.size(), -1);
00213 
00214     faceList newFaces(2*oldFaces.size());
00215     label nNewFaces = 0;
00216 
00217     // Insert original (internal) faces
00218     forAll (newCellFaces, cellI)
00219     {
00220         const labelList& curCellFaces = newCellFaces[cellI];
00221 
00222         forAll (curCellFaces, cfI)
00223         {
00224             newFaces[nNewFaces] = oldFaces[curCellFaces[cfI]];
00225             masterFaceLookup[curCellFaces[cfI]] = nNewFaces;
00226 
00227             nNewFaces++;
00228         }
00229     }
00230 
00231     // Mirror internal faces
00232     for (label faceI = 0; faceI < nOldInternalFaces; faceI++)
00233     {
00234         const face& oldFace = oldFaces[faceI];
00235         face& nf = newFaces[nNewFaces];
00236         nf.setSize(oldFace.size());
00237 
00238         nf[0] = mirrorPointLookup[oldFace[0]];
00239 
00240         for (label i = 1; i < oldFace.size(); i++)
00241         {
00242             nf[i] = mirrorPointLookup[oldFace[oldFace.size() - i]];
00243         }
00244 
00245         mirrorFaceLookup[faceI] = nNewFaces;
00246         nNewFaces++;
00247     }
00248 
00249     // Mirror boundary faces patch by patch
00250 
00251     wordList newPatchTypes(boundary().size());
00252     wordList newPatchNames(boundary().size());
00253     labelList newPatchSizes(boundary().size(), -1);
00254     labelList newPatchStarts(boundary().size(), -1);
00255     label nNewPatches = 0;
00256 
00257     forAll (boundaryMesh(), patchI)
00258     {
00259         const label curPatchSize = boundaryMesh()[patchI].size();
00260         const label curPatchStart = boundaryMesh()[patchI].start();
00261         const boolList& curInserted = insertedBouFace[patchI];
00262 
00263         newPatchStarts[nNewPatches] = nNewFaces;
00264 
00265         // Master side
00266         for (label faceI = 0; faceI < curPatchSize; faceI++)
00267         {
00268             // Check if the face has already been added.  If not, add it and
00269             // insert the numbering details.
00270             if (!curInserted[faceI])
00271             {
00272                 newFaces[nNewFaces] = oldFaces[curPatchStart + faceI];
00273 
00274                 masterFaceLookup[curPatchStart + faceI] = nNewFaces;
00275                 nNewFaces++;
00276             }
00277         }
00278 
00279         // Mirror side
00280         for (label faceI = 0; faceI < curPatchSize; faceI++)
00281         {
00282             // Check if the face has already been added.  If not, add it and
00283             // insert the numbering details.
00284             if (!curInserted[faceI])
00285             {
00286                 const face& oldFace = oldFaces[curPatchStart + faceI];
00287                 face& nf = newFaces[nNewFaces];
00288                 nf.setSize(oldFace.size());
00289 
00290                 nf[0] = mirrorPointLookup[oldFace[0]];
00291 
00292                 for (label i = 1; i < oldFace.size(); i++)
00293                 {
00294                     nf[i] = mirrorPointLookup[oldFace[oldFace.size() - i]];
00295                 }
00296 
00297                 mirrorFaceLookup[curPatchStart + faceI] = nNewFaces;
00298                 nNewFaces++;
00299             }
00300             else
00301             {
00302                 // Grab the index of the master face for the mirror side
00303                 mirrorFaceLookup[curPatchStart + faceI] =
00304                     masterFaceLookup[curPatchStart + faceI];
00305             }
00306         }
00307 
00308         // If patch exists, grab the name and type of the original patch
00309         if (nNewFaces > newPatchStarts[nNewPatches])
00310         {
00311             newPatchTypes[nNewPatches] = boundaryMesh()[patchI].type();
00312             newPatchNames[nNewPatches] = boundaryMesh()[patchI].name();
00313             newPatchSizes[nNewPatches] =
00314                 nNewFaces - newPatchStarts[nNewPatches];
00315 
00316             nNewPatches++;
00317         }
00318     }
00319 
00320     // Tidy up the lists
00321     newFaces.setSize(nNewFaces);
00322     Info << " New faces: " << nNewFaces << endl;
00323 
00324     newPatchTypes.setSize(nNewPatches);
00325     newPatchNames.setSize(nNewPatches);
00326     newPatchSizes.setSize(nNewPatches);
00327     newPatchStarts.setSize(nNewPatches);
00328 
00329     Info << "Mirroring patches. Old patches: " << boundary().size()
00330         << " New patches: " << nNewPatches << endl;
00331 
00332     Info<< "Mirroring cells.  Old cells: " << oldCells.size()
00333         << " New cells: " << 2*oldCells.size() << endl;
00334 
00335     cellList newCells(2*oldCells.size());
00336     label nNewCells = 0;
00337 
00338     // Grab the original cells.  Take care of face renumbering.
00339     forAll (oldCells, cellI)
00340     {
00341         const cell& oc = oldCells[cellI];
00342 
00343         cell& nc = newCells[nNewCells];
00344         nc.setSize(oc.size());
00345 
00346         forAll (oc, i)
00347         {
00348             nc[i] = masterFaceLookup[oc[i]];
00349         }
00350 
00351         nNewCells++;
00352     }
00353 
00354     // Mirror the cells
00355     forAll (oldCells, cellI)
00356     {
00357         const cell& oc = oldCells[cellI];
00358 
00359         cell& nc = newCells[nNewCells];
00360         nc.setSize(oc.size());
00361 
00362         forAll (oc, i)
00363         {
00364             nc[i] = mirrorFaceLookup[oc[i]];
00365         }
00366 
00367         nNewCells++;
00368     }
00369 
00370     // Mirror the cell shapes
00371     Info << "Mirroring cell shapes." << endl;
00372 
00373     Info << nl << "Creating new mesh" << endl;
00374     mirrorMeshPtr_ = new fvMesh
00375     (
00376         io,
00377         xferMove(newPoints),
00378         xferMove(newFaces),
00379         xferMove(newCells)
00380     );
00381 
00382     fvMesh& pMesh = *mirrorMeshPtr_;
00383 
00384     // Add the boundary patches
00385     List<polyPatch*> p(newPatchTypes.size());
00386 
00387     forAll (p, patchI)
00388     {
00389         p[patchI] = polyPatch::New
00390         (
00391             newPatchTypes[patchI],
00392             newPatchNames[patchI],
00393             newPatchSizes[patchI],
00394             newPatchStarts[patchI],
00395             patchI,
00396             pMesh.boundaryMesh()
00397         ).ptr();
00398     }
00399 
00400     pMesh.addPatches(p);
00401 }
00402 
00403 
00404 // * * * * * * * * * * * * * * * * Destructor  * * * * * * * * * * * * * * * //
00405 
00406 Foam::mirrorFvMesh::~mirrorFvMesh()
00407 {}
00408 
00409 
00410 // ************************************************************************* //
Copyright © 2000-2009 OpenCFD Ltd