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

enrichedPatchFaces.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 
00027 \*---------------------------------------------------------------------------*/
00028 
00029 #include "enrichedPatch.H"
00030 #include "DynamicList.H"
00031 
00032 
00033 // * * * * * * * * * * * * * * Static Data Members * * * * * * * * * * * * * //
00034 
00035 const Foam::label Foam::enrichedPatch::enrichedFaceRatio_ = 3;
00036 
00037 // * * * * * * * * * * * * * Private Member Functions  * * * * * * * * * * * //
00038 
00039 void Foam::enrichedPatch::calcEnrichedFaces
00040 (
00041     const labelListList& pointsIntoMasterEdges,
00042     const labelListList& pointsIntoSlaveEdges,
00043     const pointField& projectedSlavePoints
00044 )
00045 {
00046     if (enrichedFacesPtr_)
00047     {
00048         FatalErrorIn
00049         (
00050             "void enrichedPatch::calcEnrichedFaces\n"
00051             "(\n"
00052             "    const labelListList& pointsIntoMasterEdges,\n"
00053             "    const labelListList& pointsIntoSlaveEdges,\n"
00054             "    const pointField& projectedSlavePoints\n"
00055             ")"
00056         )   << "Enriched faces already calculated."
00057             << abort(FatalError);
00058     }
00059 
00060     // Create a list of enriched faces
00061     // Algorithm:
00062     // 1) Grab the original face and start from point zero.
00063     // 2) If the point has been merged away, grab the merge label;
00064     //    otherwise, keep the original label.
00065     // 3) Go to the next edge. Collect all the points to be added along
00066     //    the edge; order them in the necessary direction and insert onto the
00067     //    face.
00068     // 4) Grab the next point and return on step 2.
00069     enrichedFacesPtr_ = new faceList(masterPatch_.size() + slavePatch_.size());
00070     faceList& enrichedFaces = *enrichedFacesPtr_;
00071 
00072     label nEnrichedFaces = 0;
00073 
00074     const pointField& masterLocalPoints = masterPatch_.localPoints();
00075     const faceList& masterLocalFaces = masterPatch_.localFaces();
00076     const labelListList& masterFaceEdges = masterPatch_.faceEdges();
00077 
00078     const faceList& slaveLocalFaces = slavePatch_.localFaces();
00079     const labelListList& slaveFaceEdges = slavePatch_.faceEdges();
00080 
00081     // For correct functioning of the enrichedPatch class, the slave
00082     // faces need to be inserted first.  See comments in
00083     // enrichedPatch.H
00084 
00085     // Get reference to the point merge map
00086     const Map<label>& pmm = pointMergeMap();
00087 
00088     // Add slave faces into the enriched faces list
00089 
00090     forAll (slavePatch_, faceI)
00091     {
00092         const face oldFace = slavePatch_[faceI];
00093         const face oldLocalFace = slaveLocalFaces[faceI];
00094 //         Info << "old slave face " << faceI << ": " << oldFace << endl;
00095         const labelList& curEdges = slaveFaceEdges[faceI];
00096 
00097         DynamicList<label> newFace(oldFace.size()*enrichedFaceRatio_);
00098 
00099         // Note: The number of points and edges in a face is always identical
00100         // so both can be done is the same loop
00101         forAll (oldFace, i)
00102         {
00103             // Add the point
00104             Map<label>::const_iterator mpIter =
00105                 pmm.find(oldFace[i]);
00106 
00107             if (mpIter == pmm.end())
00108             {
00109                 // Point not mapped
00110                 newFace.append(oldFace[i]);
00111 
00112                 // Add the projected point into the patch support
00113                 pointMap().insert
00114                 (
00115                     oldFace[i],    // Global label of point
00116                     projectedSlavePoints[oldLocalFace[i]] // Projected position
00117                 );
00118             }
00119             else
00120             {
00121                 // Point mapped
00122                 newFace.append(mpIter());
00123 
00124                 // Add the projected point into the patch support
00125                 pointMap().insert
00126                 (
00127                     mpIter(),    // Merged global label of point
00128                     projectedSlavePoints[oldLocalFace[i]] // Projected position
00129                 );
00130             }
00131 
00132             // Grab the edge points
00133 
00134             const labelList& slavePointsOnEdge =
00135                 pointsIntoSlaveEdges[curEdges[i]];
00136 //             Info << "slavePointsOnEdge for " << curEdges[i] << ": " << slavePointsOnEdge << endl;
00137             // If there are no points on the edge, skip everything
00138             // If there is only one point, no need for sorting
00139             if (slavePointsOnEdge.size())
00140             {
00141                 // Sort edge points in order
00142                 scalarField edgePointWeights(slavePointsOnEdge.size());
00143                 const point& startPoint = projectedSlavePoints[oldLocalFace[i]];
00144 
00145                 vector e =
00146                     projectedSlavePoints[oldLocalFace.nextLabel(i)]
00147                   - startPoint;
00148 
00149                 scalar magSqrE = magSqr(e);
00150 
00151                 if (magSqrE > SMALL)
00152                 {
00153                     e /= magSqrE;
00154                 }
00155                 else
00156                 {
00157                     FatalErrorIn
00158                     (
00159                         "void enrichedPatch::calcEnrichedFaces\n"
00160                         "(\n"
00161                         "    const labelListList& pointsIntoMasterEdges,\n"
00162                         "    const labelListList& pointsIntoSlaveEdges,\n"
00163                         "    const pointField& projectedSlavePoints\n"
00164                         ")"
00165                     )   << "Zero length edge in slave patch for face " << i
00166                         << ".  This is not allowed."
00167                         << abort(FatalError);
00168                 }
00169 
00170                 pointField slavePosOnEdge(slavePointsOnEdge.size());
00171 
00172                 forAll (slavePointsOnEdge, edgePointI)
00173                 {
00174                     slavePosOnEdge[edgePointI] =
00175                         pointMap().find(slavePointsOnEdge[edgePointI])();
00176 
00177                     edgePointWeights[edgePointI] =
00178                         (e & (slavePosOnEdge[edgePointI] - startPoint));
00179                 }
00180 
00181                 if (debug)
00182                 {
00183                     // Check weights: all new points should be on the edge
00184                     if (min(edgePointWeights) < 0 || max(edgePointWeights) > 1)
00185                     {
00186                         FatalErrorIn
00187                         (
00188                             "void enrichedPatch::calcEnrichedFaces\n"
00189                             "(\n"
00190                             "    const labelListList& pointsIntoMasterEdges,\n"
00191                             "    const labelListList& pointsIntoSlaveEdges,\n"
00192                             "    const pointField& projectedSlavePoints\n"
00193                             ")"
00194                         )   << "Invalid point edge weights.  Some of points are"
00195                             << " not on the edge for edge " << curEdges[i]
00196                             << " of face " << faceI << " in slave patch." << nl
00197                             << "Min weight: " << min(edgePointWeights)
00198                             << " Max weight: " << max(edgePointWeights)
00199                             << abort(FatalError);
00200                     }
00201                 }
00202 
00203                 // Go through the points and collect them based on
00204                 // weights from lower to higher.  This gives the
00205                 // correct order of points along the edge.
00206                 for (label passI = 0; passI < edgePointWeights.size(); passI++)
00207                 {
00208                     // Max weight can only be one, so the sorting is
00209                     // done by elimination.
00210                     label nextPoint = -1;
00211                     scalar dist = 2;
00212 
00213                     forAll (edgePointWeights, wI)
00214                     {
00215                         if (edgePointWeights[wI] < dist)
00216                         {
00217                             dist = edgePointWeights[wI];
00218                             nextPoint = wI;
00219                         }
00220                     }
00221 
00222                     // Insert the next point and reset its weight to exclude it
00223                     // from future picks
00224                     newFace.append(slavePointsOnEdge[nextPoint]);
00225                     edgePointWeights[nextPoint] = GREAT;
00226 
00227                     // Add the point into patch support
00228                     pointMap().insert
00229                     (
00230                         slavePointsOnEdge[nextPoint],
00231                         slavePosOnEdge[nextPoint]
00232                     );
00233                 }
00234             }
00235         }
00236         // Info<< "New slave face " << faceI << ": " << newFace << endl;
00237 
00238         // Add the new face to the list
00239         enrichedFaces[nEnrichedFaces].transfer(newFace);
00240         nEnrichedFaces++;
00241     }
00242 
00243     // Add master faces into the enriched faces list
00244 
00245     forAll (masterPatch_, faceI)
00246     {
00247         const face& oldFace = masterPatch_[faceI];
00248         const face& oldLocalFace = masterLocalFaces[faceI];
00249 //         Info << "old master face: " << oldFace << endl;
00250         const labelList& curEdges = masterFaceEdges[faceI];
00251 
00252         DynamicList<label> newFace(oldFace.size()*enrichedFaceRatio_);
00253 
00254         // Note: The number of points and edges in a face is always identical
00255         // so both can be done is the same loop
00256         forAll (oldFace, i)
00257         {
00258             // Add the point
00259             Map<label>::const_iterator mpIter =
00260                 pmm.find(oldFace[i]);
00261 
00262             if (mpIter == pmm.end())
00263             {
00264                 // Point not mapped
00265                 newFace.append(oldFace[i]);
00266 
00267                 // Add the point into patch support
00268                 pointMap().insert
00269                 (
00270                     oldFace[i],
00271                     masterLocalPoints[oldLocalFace[i]]
00272                 );
00273             }
00274             else
00275             {
00276                 // Point mapped
00277                 newFace.append(mpIter());
00278 
00279                 // Add the point into support
00280                 pointMap().insert(mpIter(), masterLocalPoints[oldLocalFace[i]]);
00281             }
00282 
00283             // Grab the edge points
00284 
00285             const labelList& masterPointsOnEdge =
00286                 pointsIntoMasterEdges[curEdges[i]];
00287 
00288             // If there are no points on the edge, skip everything
00289             // If there is only one point, no need for sorting
00290             if (masterPointsOnEdge.size())
00291             {
00292                 // Sort edge points in order
00293                 scalarField edgePointWeights(masterPointsOnEdge.size());
00294                 const point& startPoint = masterLocalPoints[oldLocalFace[i]];
00295 
00296                 vector e =
00297                     masterLocalPoints[oldLocalFace.nextLabel(i)]
00298                   - startPoint;
00299 
00300                 scalar magSqrE = magSqr(e);
00301 
00302                 if (magSqrE > SMALL)
00303                 {
00304                     e /= magSqrE;
00305                 }
00306                 else
00307                 {
00308                     FatalErrorIn
00309                     (
00310                         "void enrichedPatch::calcEnrichedFaces\n"
00311                         "(\n"
00312                         "    const labelListList& pointsIntoMasterEdges,\n"
00313                         "    const labelListList& pointsIntoSlaveEdges,\n"
00314                         "    const pointField& projectedSlavePoints\n"
00315                         ")"
00316                     )   << "Zero length edge in master patch for face " << i
00317                         << ".  This is not allowed."
00318                         << abort(FatalError);
00319                 }
00320 
00321                 pointField masterPosOnEdge(masterPointsOnEdge.size());
00322 
00323                 forAll (masterPointsOnEdge, edgePointI)
00324                 {
00325                     masterPosOnEdge[edgePointI] =
00326                         pointMap().find(masterPointsOnEdge[edgePointI])();
00327 
00328                     edgePointWeights[edgePointI] =
00329                         (e & (masterPosOnEdge[edgePointI] - startPoint));
00330                 }
00331 
00332                 if (debug)
00333                 {
00334                     // Check weights: all new points should be on the edge
00335                     if (min(edgePointWeights) < 0 || max(edgePointWeights) > 1)
00336                     {
00337                         FatalErrorIn
00338                         (
00339                             "void enrichedPatch::calcEnrichedFaces\n"
00340                             "(\n"
00341                             "    const labelListList& pointsIntoMasterEdges,\n"
00342                             "    const labelListList& pointsIntoSlaveEdges,\n"
00343                             "    const pointField& projectedSlavePoints\n"
00344                             ")"
00345                         )   << "Invalid point edge weights.  Some of points are"
00346                             << " not on the edge for edge " << curEdges[i]
00347                             << " of face " << faceI << " in master patch." << nl
00348                             << "Min weight: " << min(edgePointWeights)
00349                             << " Max weight: " << max(edgePointWeights)
00350                             << abort(FatalError);
00351                     }
00352                 }
00353 
00354                 // Go through the points and collect them based on
00355                 // weights from lower to higher.  This gives the
00356                 // correct order of points along the edge.
00357                 for (label pass = 0; pass < edgePointWeights.size(); pass++)
00358                 {
00359                     // Max weight can only be one, so the sorting is
00360                     // done by elimination.
00361                     label nextPoint = -1;
00362                     scalar dist = 2;
00363 
00364                     forAll (edgePointWeights, wI)
00365                     {
00366                         if (edgePointWeights[wI] < dist)
00367                         {
00368                             dist = edgePointWeights[wI];
00369                             nextPoint = wI;
00370                         }
00371                     }
00372 
00373                     // Insert the next point and reset its weight to exclude it
00374                     // from future picks
00375                     newFace.append(masterPointsOnEdge[nextPoint]);
00376                     edgePointWeights[nextPoint] = GREAT;
00377 
00378                     // Add the point into patch support
00379                     pointMap().insert
00380                     (
00381                         masterPointsOnEdge[nextPoint],
00382                         masterPosOnEdge[nextPoint]
00383                     );
00384                 }
00385             }
00386         }
00387         // Info<< "New master face: " << newFace << endl;
00388 
00389         // Add the new face to the list
00390         enrichedFaces[nEnrichedFaces].transfer(newFace);
00391         nEnrichedFaces++;
00392     }
00393 
00394     // Check the support for the enriched patch
00395     if (debug)
00396     {
00397         if (!checkSupport())
00398         {
00399             Info<< "Enriched patch support OK. Slave faces: "
00400                 << slavePatch_.size() << " Master faces: "
00401                 << masterPatch_.size() << endl;
00402         }
00403         else
00404         {
00405             FatalErrorIn
00406             (
00407                 "void enrichedPatch::calcEnrichedFaces\n"
00408                 "(\n"
00409                 "    const labelListList& pointsIntoMasterEdges,\n"
00410                 "    const labelListList& pointsIntoSlaveEdges,\n"
00411                 "    const pointField& projectedSlavePoints\n"
00412                 ")"
00413             )   << "Error in enriched patch support"
00414                 << abort(FatalError);
00415         }
00416     }
00417 }
00418 
00419 
00420 // * * * * * * * * * * * * * * * Member Functions  * * * * * * * * * * * * * //
00421 
00422 const Foam::faceList& Foam::enrichedPatch::enrichedFaces() const
00423 {
00424     if (!enrichedFacesPtr_)
00425     {
00426         FatalErrorIn("const faceList& enrichedPatch::enrichedFaces() const")
00427             << "Enriched faces not available yet.  Please use "
00428             << "void enrichedPatch::calcEnrichedFaces\n"
00429             << "(\n"
00430             << "    const labelListList& pointsIntoMasterEdges,\n"
00431             << "    const labelListList& pointsIntoSlaveEdges,\n"
00432             << "    const pointField& projectedSlavePoints\n"
00433             << ")"
00434             << " before trying to access faces."
00435             << abort(FatalError);
00436     }
00437 
00438     return *enrichedFacesPtr_;
00439 }
00440 
00441 
00442 // ************************************************************************* //
Copyright © 2000-2009 OpenCFD Ltd