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

calculateMeshToMeshAddressing.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 Description
00026     private member of meshToMesh.
00027     Calculates mesh to mesh addressing pattern (for each cell from one mesh
00028     find the closest cell centre in the other mesh).
00029 
00030 \*---------------------------------------------------------------------------*/
00031 
00032 #include "meshToMesh.H"
00033 #include "SubField.H"
00034 
00035 #include "octree.H"
00036 #include "octreeDataCell.H"
00037 #include "octreeDataFace.H"
00038 
00039 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
00040 
00041 namespace Foam
00042 {
00043 
00044 // * * * * * * * * * * * * * Private Member Functions  * * * * * * * * * * * //
00045 
00046 void meshToMesh::calcAddressing()
00047 {
00048     if (debug)
00049     {
00050         Info<< "meshToMesh::calculateAddressing() : "
00051             << "calculating mesh-to-mesh cell addressing" << endl;
00052     }
00053 
00054     // set reference to cells
00055     const cellList& fromCells = fromMesh_.cells();
00056     const pointField& fromPoints = fromMesh_.points();
00057 
00058     // In an attempt to preserve the efficiency of linear search and prevent
00059     // failure, a RESCUE mechanism will be set up. Here, we shall mark all
00060     // cells next to the solid boundaries. If such a cell is found as the
00061     // closest, the relationship between the origin and cell will be examined.
00062     // If the origin is outside the cell, a global n-squared search is
00063     // triggered.
00064 
00065     // SETTING UP RESCUE
00066 
00067     // visit all boundaries and mark the cell next to the boundary.
00068 
00069     if (debug)
00070     {
00071         Info<< "meshToMesh::calculateAddressing() : "
00072             << "Setting up rescue" << endl;
00073     }
00074 
00075     List<bool> boundaryCell(fromCells.size(), false);
00076 
00077     // set reference to boundary
00078     const polyPatchList& patchesFrom = fromMesh_.boundaryMesh();
00079 
00080     forAll (patchesFrom, patchI)
00081     {
00082         // get reference to cells next to the boundary
00083         const unallocLabelList& bCells = patchesFrom[patchI].faceCells();
00084 
00085         forAll (bCells, faceI)
00086         {
00087             boundaryCell[bCells[faceI]] = true;
00088         }
00089     }
00090 
00091     treeBoundBox meshBb(fromPoints);
00092 
00093     scalar typDim = meshBb.avgDim()/(2.0*cbrt(scalar(fromCells.size())));
00094 
00095     treeBoundBox shiftedBb
00096     (
00097         meshBb.min(),
00098         meshBb.max() + vector(typDim, typDim, typDim)
00099     );
00100 
00101     if (debug)
00102     {
00103         Info<< "\nMesh" << endl;
00104         Info<< "   bounding box           : " << meshBb << endl;
00105         Info<< "   bounding box (shifted) : " << shiftedBb << endl;
00106         Info<< "   typical dimension      :" << shiftedBb.typDim() << endl;
00107     }
00108 
00109     // Wrap indices and mesh information into helper object
00110     octreeDataCell shapes(fromMesh_);
00111 
00112     octree<octreeDataCell> oc
00113     (
00114         shiftedBb,  // overall bounding box
00115         shapes,     // all information needed to do checks on cells
00116         1,          // min. levels
00117         10.0,       // max. size of leaves
00118         10.0        // maximum ratio of cubes v.s. cells
00119     );
00120 
00121     if (debug)
00122     {
00123         oc.printStats(Info);
00124     }
00125 
00126     cellAddresses
00127     (
00128         cellAddressing_,
00129         toMesh_.cellCentres(),
00130         fromMesh_,
00131         boundaryCell,
00132         oc
00133     );
00134 
00135     forAll (toMesh_.boundaryMesh(), patchi)
00136     {
00137         const polyPatch& toPatch = toMesh_.boundaryMesh()[patchi];
00138 
00139         if (cuttingPatches_.found(toPatch.name()))
00140         {
00141             boundaryAddressing_[patchi].setSize(toPatch.size());
00142 
00143             cellAddresses
00144             (
00145                 boundaryAddressing_[patchi],
00146                 toPatch.faceCentres(),
00147                 fromMesh_,
00148                 boundaryCell,
00149                 oc
00150             );
00151         }
00152         else if
00153         (
00154             patchMap_.found(toPatch.name())
00155          && fromMeshPatches_.found(patchMap_.find(toPatch.name())())
00156         )
00157         {
00158             const polyPatch& fromPatch = fromMesh_.boundaryMesh()
00159             [
00160                 fromMeshPatches_.find(patchMap_.find(toPatch.name())())()
00161             ];
00162 
00163             if (fromPatch.empty())
00164             {
00165                 WarningIn("meshToMesh::calcAddressing()")
00166                     << "Source patch " << fromPatch.name()
00167                     << " has no faces. Not performing mapping for it."
00168                     << endl;
00169                 boundaryAddressing_[patchi] = -1;
00170             }
00171             else
00172             {
00173                 treeBoundBox wallBb(fromPatch.localPoints());
00174                 scalar typDim =
00175                     wallBb.avgDim()/(2.0*sqrt(scalar(fromPatch.size())));
00176 
00177                 treeBoundBox shiftedBb
00178                 (
00179                     wallBb.min(),
00180                     wallBb.max() + vector(typDim, typDim, typDim)
00181                 );
00182 
00183                 // Wrap data for octree into container
00184                 octreeDataFace shapes(fromPatch);
00185 
00186                 octree<octreeDataFace> oc
00187                 (
00188                     shiftedBb,  // overall search domain
00189                     shapes,     // all information needed to do checks on cells
00190                     1,          // min levels
00191                     20.0,       // maximum ratio of cubes v.s. cells
00192                     2.0
00193                 );
00194 
00195 
00196                 const vectorField::subField centresToBoundary =
00197                     toPatch.faceCentres();
00198 
00199                 boundaryAddressing_[patchi].setSize(toPatch.size());
00200 
00201                 treeBoundBox tightest;
00202                 scalar tightestDist;
00203 
00204                 forAll(toPatch, toi)
00205                 {
00206                     tightest = wallBb;                  // starting search bb
00207                     tightestDist = Foam::GREAT;        // starting max distance
00208 
00209                     boundaryAddressing_[patchi][toi] = oc.findNearest
00210                     (
00211                         centresToBoundary[toi],
00212                         tightest,
00213                         tightestDist
00214                     );
00215                 }
00216             }
00217         }
00218     }
00219 
00220     if (debug)
00221     {
00222         Info<< "meshToMesh::calculateAddressing() : "
00223             << "finished calculating mesh-to-mesh acell ddressing" << endl;
00224     }
00225 }
00226 
00227 
00228 void meshToMesh::cellAddresses
00229 (
00230     labelList& cellAddressing_,
00231     const pointField& points,
00232     const fvMesh& fromMesh,
00233     const List<bool>& boundaryCell,
00234     const octree<octreeDataCell>& oc
00235 ) const
00236 {
00237     // the implemented search method is a simple neighbour array search.
00238     // It starts from a cell zero, searches its neighbours and finds one
00239     // which is nearer to the target point than the current position.
00240     // The location of the "current position" is reset to that cell and
00241     // search through the neighbours continues. The search is finished
00242     // when all the neighbours of the cell are farther from the target
00243     // point than the current cell
00244 
00245     // set curCell label to zero (start)
00246     register label curCell = 0;
00247 
00248     // set reference to cell to cell addressing
00249     const vectorField& centresFrom = fromMesh.cellCentres();
00250     const labelListList& cc = fromMesh.cellCells();
00251 
00252     forAll (points, toI)
00253     {
00254         // pick up target position
00255         const vector& p = points[toI];
00256 
00257         // set the sqr-distance
00258         scalar distSqr = magSqr(p - centresFrom[curCell]);
00259 
00260         bool closer;
00261 
00262         do
00263         {
00264             closer = false;
00265 
00266             // set the current list of neighbouring cells
00267             const labelList& neighbours = cc[curCell];
00268 
00269             forAll (neighbours, nI)
00270             {
00271                 scalar curDistSqr =
00272                     magSqr(p - centresFrom[neighbours[nI]]);
00273 
00274                 // search through all the neighbours.
00275                 // If the cell is closer, reset current cell and distance
00276                 if (curDistSqr < (1 - SMALL)*distSqr)
00277                 {
00278                     curCell = neighbours[nI];
00279                     distSqr = curDistSqr;
00280                     closer = true;    // a closer neighbour has been found
00281                 }
00282             }
00283         } while (closer);
00284 
00285         cellAddressing_[toI] = -1;
00286 
00287         // Check point is actually in the nearest cell
00288         if (fromMesh.pointInCell(p, curCell))
00289         {
00290             cellAddressing_[toI] = curCell;
00291         }
00292         else
00293         {
00294             // If curCell is a boundary cell then the point maybe either outside
00295             // the domain or in an other region of the doamin, either way use
00296             // the octree search to find it.
00297             if (boundaryCell[curCell])
00298             {
00299                 cellAddressing_[toI] = oc.find(p);
00300             }
00301             else
00302             {
00303                 // If not on the boundary search the neighbours
00304                 bool found = false;
00305 
00306                 // set the current list of neighbouring cells
00307                 const labelList& neighbours = cc[curCell];
00308 
00309                 forAll (neighbours, nI)
00310                 {
00311                     // search through all the neighbours.
00312                     // If point is in neighbour reset current cell
00313                     if (fromMesh.pointInCell(p, neighbours[nI]))
00314                     {
00315                         cellAddressing_[toI] = neighbours[nI];
00316                         found = true;
00317                         break;
00318                     }
00319                 }
00320 
00321                 if (!found)
00322                 {
00323                     // If still not found search the neighbour-neighbours
00324 
00325                     // set the current list of neighbouring cells
00326                     const labelList& neighbours = cc[curCell];
00327 
00328                     forAll (neighbours, nI)
00329                     {
00330                         // set the current list of neighbour-neighbouring cells
00331                         const labelList& nn = cc[neighbours[nI]];
00332 
00333                         forAll (nn, nI)
00334                         {
00335                             // search through all the neighbours.
00336                             // If point is in neighbour reset current cell
00337                             if (fromMesh.pointInCell(p, nn[nI]))
00338                             {
00339                                 cellAddressing_[toI] = nn[nI];
00340                                 found = true;
00341                                 break;
00342                             }
00343                         }
00344                         if (found) break;
00345                     }
00346                 }
00347 
00348                 if (!found)
00349                 {
00350                     // Still not found so us the octree
00351                     cellAddressing_[toI] = oc.find(p);
00352                 }
00353             }
00354         }
00355     }
00356 }
00357 
00358 
00359 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
00360 
00361 } // End namespace Foam
00362 
00363 // ************************************************************************* //
Copyright © 2000-2009 OpenCFD Ltd