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

shellSurfaces.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 "searchableSurface.H"
00028 #include "shellSurfaces.H"
00029 #include "boundBox.H"
00030 #include "triSurfaceMesh.H"
00031 #include "refinementSurfaces.H"
00032 #include "searchableSurfaces.H"
00033 #include "orientedSurface.H"
00034 #include "pointIndexHit.H"
00035 
00036 // * * * * * * * * * * * * * * Static Data Members * * * * * * * * * * * * * //
00037 
00038 
00039 namespace Foam
00040 {
00041 
00042 template<>
00043 const char*
00044 NamedEnum<shellSurfaces::refineMode, 3>::
00045 names[] =
00046 {
00047     "inside",
00048     "outside",
00049     "distance"
00050 };
00051 
00052 const NamedEnum<shellSurfaces::refineMode, 3> shellSurfaces::refineModeNames_;
00053 
00054 } // End namespace Foam
00055 
00056 
00057 
00058 // * * * * * * * * * * * * * Private Member Functions  * * * * * * * * * * * //
00059 
00060 void Foam::shellSurfaces::setAndCheckLevels
00061 (
00062     const label shellI,
00063     const List<Tuple2<scalar, label> >& distLevels
00064 )
00065 {
00066     if (modes_[shellI] != DISTANCE && distLevels.size() != 1)
00067     {
00068         FatalErrorIn
00069         (
00070             "shellSurfaces::shellSurfaces"
00071             "(const searchableSurfaces&, const dictionary&)"
00072         )   << "For refinement mode "
00073             << refineModeNames_[modes_[shellI]]
00074             << " specify only one distance+level."
00075             << " (its distance gets discarded)"
00076             << exit(FatalError);
00077     }
00078     // Extract information into separate distance and level
00079     distances_[shellI].setSize(distLevels.size());
00080     levels_[shellI].setSize(distLevels.size());
00081 
00082     forAll(distLevels, j)
00083     {
00084         distances_[shellI][j] = distLevels[j].first();
00085         levels_[shellI][j] = distLevels[j].second();
00086 
00087         // Check in incremental order
00088         if (j > 0)
00089         {
00090             if
00091             (
00092                 (distances_[shellI][j] <= distances_[shellI][j-1])
00093              || (levels_[shellI][j] > levels_[shellI][j-1])
00094             )
00095             {
00096                 FatalErrorIn
00097                 (
00098                     "shellSurfaces::shellSurfaces"
00099                     "(const searchableSurfaces&, const dictionary&)"
00100                 )   << "For refinement mode "
00101                     << refineModeNames_[modes_[shellI]]
00102                     << " : Refinement should be specified in order"
00103                     << " of increasing distance"
00104                     << " (and decreasing refinement level)." << endl
00105                     << "Distance:" << distances_[shellI][j]
00106                     << " refinementLevel:" << levels_[shellI][j]
00107                     << exit(FatalError);
00108             }
00109         }
00110     }
00111 
00112     const searchableSurface& shell = allGeometry_[shells_[shellI]];
00113 
00114     if (modes_[shellI] == DISTANCE)
00115     {
00116         Info<< "Refinement level according to distance to "
00117             << shell.name() << endl;
00118         forAll(levels_[shellI], j)
00119         {
00120             Info<< "    level " << levels_[shellI][j]
00121                 << " for all cells within " << distances_[shellI][j]
00122                 << " meter." << endl;
00123         }
00124     }
00125     else
00126     {
00127         if (!allGeometry_[shells_[shellI]].hasVolumeType())
00128         {
00129             FatalErrorIn
00130             (
00131                 "shellSurfaces::shellSurfaces"
00132                 "(const searchableSurfaces&"
00133                 ", const PtrList<dictionary>&)"
00134             )   << "Shell " << shell.name()
00135                 << " does not support testing for "
00136                 << refineModeNames_[modes_[shellI]] << endl
00137                 << "Probably it is not closed."
00138                 << exit(FatalError);
00139         }
00140 
00141         if (modes_[shellI] == INSIDE)
00142         {
00143             Info<< "Refinement level " << levels_[shellI][0]
00144                 << " for all cells inside " << shell.name() << endl;
00145         }
00146         else
00147         {
00148             Info<< "Refinement level " << levels_[shellI][0]
00149                 << " for all cells outside " << shell.name() << endl;
00150         }
00151     }
00152 }
00153 
00154 
00155 // Specifically orient triSurfaces using a calculated point outside.
00156 // Done since quite often triSurfaces not of consistent orientation which
00157 // is (currently) necessary for sideness calculation
00158 void Foam::shellSurfaces::orient()
00159 {
00160     // Determine outside point.
00161     boundBox overallBb = boundBox::invertedBox;
00162 
00163     bool hasSurface = false;
00164 
00165     forAll(shells_, shellI)
00166     {
00167         const searchableSurface& s = allGeometry_[shells_[shellI]];
00168 
00169         if (modes_[shellI] != DISTANCE && isA<triSurfaceMesh>(s))
00170         {
00171             const triSurfaceMesh& shell = refCast<const triSurfaceMesh>(s);
00172 
00173             if (shell.triSurface::size())
00174             {
00175                 const pointField& points = shell.points();
00176 
00177                 hasSurface = true;
00178 
00179                 boundBox shellBb(points[0], points[0]);
00180                 // Assume surface is compact!
00181                 for (label i = 0; i < points.size(); i++)
00182                 {
00183                     const point& pt = points[i];
00184                     shellBb.min() = min(shellBb.min(), pt);
00185                     shellBb.max() = max(shellBb.max(), pt);
00186                 }
00187 
00188                 overallBb.min() = min(overallBb.min(), shellBb.min());
00189                 overallBb.max() = max(overallBb.max(), shellBb.max());
00190             }
00191         }
00192     }
00193 
00194     if (hasSurface)
00195     {
00196         const point outsidePt = overallBb.max() + overallBb.span();
00197 
00198         //Info<< "Using point " << outsidePt << " to orient shells" << endl;
00199 
00200         forAll(shells_, shellI)
00201         {
00202             const searchableSurface& s = allGeometry_[shells_[shellI]];
00203 
00204             if (modes_[shellI] != DISTANCE && isA<triSurfaceMesh>(s))
00205             {
00206                 triSurfaceMesh& shell = const_cast<triSurfaceMesh&>
00207                 (
00208                     refCast<const triSurfaceMesh>(s)
00209                 );
00210 
00211                 // Flip surface so outsidePt is outside.
00212                 bool anyFlipped = orientedSurface::orient
00213                 (
00214                     shell,
00215                     outsidePt,
00216                     true
00217                 );
00218 
00219                 if (anyFlipped)
00220                 {
00221                     // orientedSurface will have done a clearOut of the surface.
00222                     // we could do a clearout of the triSurfaceMeshes::trees()
00223                     // but these aren't affected by orientation
00224                     // (except for cached
00225                     // sideness which should not be set at this point.
00226                     // !!Should check!)
00227 
00228                     Info<< "shellSurfaces : Flipped orientation of surface "
00229                         << s.name()
00230                         << " so point " << outsidePt << " is outside." << endl;
00231                 }
00232             }
00233         }
00234     }
00235 }
00236 
00237 
00238 // Find maximum level of a shell.
00239 void Foam::shellSurfaces::findHigherLevel
00240 (
00241     const pointField& pt,
00242     const label shellI,
00243     labelList& maxLevel
00244 ) const
00245 {
00246     const labelList& levels = levels_[shellI];
00247 
00248     if (modes_[shellI] == DISTANCE)
00249     {
00250         // Distance mode.
00251 
00252         const scalarField& distances = distances_[shellI];
00253 
00254         // Collect all those points that have a current maxLevel less than
00255         // (any of) the shell. Also collect the furthest distance allowable
00256         // to any shell with a higher level.
00257 
00258         pointField candidates(pt.size());
00259         labelList candidateMap(pt.size());
00260         scalarField candidateDistSqr(pt.size());
00261         label candidateI = 0;
00262 
00263         forAll(maxLevel, pointI)
00264         {
00265             forAllReverse(levels, levelI)
00266             {
00267                 if (levels[levelI] > maxLevel[pointI])
00268                 {
00269                     candidates[candidateI] = pt[pointI];
00270                     candidateMap[candidateI] = pointI;
00271                     candidateDistSqr[candidateI] = sqr(distances[levelI]);
00272                     candidateI++;
00273                     break;
00274                 }
00275             }
00276         }
00277         candidates.setSize(candidateI);
00278         candidateMap.setSize(candidateI);
00279         candidateDistSqr.setSize(candidateI);
00280 
00281         // Do the expensive nearest test only for the candidate points.
00282         List<pointIndexHit> nearInfo;
00283         allGeometry_[shells_[shellI]].findNearest
00284         (
00285             candidates,
00286             candidateDistSqr,
00287             nearInfo
00288         );
00289 
00290         // Update maxLevel
00291         forAll(nearInfo, candidateI)
00292         {
00293             if (nearInfo[candidateI].hit())
00294             {
00295                 // Check which level it actually is in.
00296                 label minDistI = findLower
00297                 (
00298                     distances,
00299                     mag(nearInfo[candidateI].hitPoint()-candidates[candidateI])
00300                 );
00301 
00302                 label pointI = candidateMap[candidateI];
00303 
00304                 // pt is inbetween shell[minDistI] and shell[minDistI+1]
00305                 maxLevel[pointI] = levels[minDistI+1];
00306             }
00307         }
00308     }
00309     else
00310     {
00311         // Inside/outside mode
00312 
00313         // Collect all those points that have a current maxLevel less than the
00314         // shell.
00315 
00316         pointField candidates(pt.size());
00317         labelList candidateMap(pt.size());
00318         label candidateI = 0;
00319 
00320         forAll(maxLevel, pointI)
00321         {
00322             if (levels[0] > maxLevel[pointI])
00323             {
00324                 candidates[candidateI] = pt[pointI];
00325                 candidateMap[candidateI] = pointI;
00326                 candidateI++;
00327             }
00328         }
00329         candidates.setSize(candidateI);
00330         candidateMap.setSize(candidateI);
00331 
00332         // Do the expensive nearest test only for the candidate points.
00333         List<searchableSurface::volumeType> volType;
00334         allGeometry_[shells_[shellI]].getVolumeType(candidates, volType);
00335 
00336         forAll(volType, i)
00337         {
00338             label pointI = candidateMap[i];
00339 
00340             if
00341             (
00342                 (
00343                     modes_[shellI] == INSIDE
00344                  && volType[i] == searchableSurface::INSIDE
00345                 )
00346              || (
00347                     modes_[shellI] == OUTSIDE
00348                  && volType[i] == searchableSurface::OUTSIDE
00349                 )
00350             )
00351             {
00352                 maxLevel[pointI] = levels[0];
00353             }
00354         }
00355     }
00356 }
00357 
00358 
00359 // * * * * * * * * * * * * * * * * Constructors  * * * * * * * * * * * * * * //
00360 
00361 Foam::shellSurfaces::shellSurfaces
00362 (
00363     const searchableSurfaces& allGeometry,
00364     const PtrList<dictionary>& shellDicts
00365 )
00366 :
00367     allGeometry_(allGeometry)
00368 {
00369     shells_.setSize(shellDicts.size());
00370     modes_.setSize(shellDicts.size());
00371     distances_.setSize(shellDicts.size());
00372     levels_.setSize(shellDicts.size());
00373 
00374     forAll(shellDicts, shellI)
00375     {
00376         const dictionary& dict = shellDicts[shellI];
00377         const word name = dict.lookup("name");
00378         const word type = dict.lookup("type");
00379 
00380         shells_[shellI] = allGeometry_.findSurfaceID(name);
00381 
00382         if (shells_[shellI] == -1)
00383         {
00384             FatalErrorIn
00385             (
00386                 "shellSurfaces::shellSurfaces"
00387                 "(const searchableSurfaces&, const PtrList<dictionary>&)"
00388             )   << "No surface called " << name << endl
00389                 << "Valid surfaces are " << allGeometry_.names()
00390                 << exit(FatalError);
00391         }
00392 
00393         modes_[shellI] = refineModeNames_.read(dict.lookup("refineMode"));
00394 
00395         // Read pairs of distance+level
00396         setAndCheckLevels(shellI, dict.lookup("levels"));
00397     }
00398 
00399     // Orient shell surfaces before any searching is done. Note that this
00400     // only needs to be done for inside or outside. Orienting surfaces
00401     // constructs lots of addressing which we want to avoid.
00402     orient();
00403 }
00404 
00405 
00406 Foam::shellSurfaces::shellSurfaces
00407 (
00408     const searchableSurfaces& allGeometry,
00409     const dictionary& shellsDict
00410 )
00411 :
00412     allGeometry_(allGeometry)
00413 {
00414     shells_.setSize(shellsDict.size());
00415     modes_.setSize(shellsDict.size());
00416     distances_.setSize(shellsDict.size());
00417     levels_.setSize(shellsDict.size());
00418 
00419     label shellI = 0;
00420     forAllConstIter(dictionary, shellsDict, iter)
00421     {
00422         shells_[shellI] = allGeometry_.findSurfaceID(iter().keyword());
00423 
00424         if (shells_[shellI] == -1)
00425         {
00426             FatalErrorIn
00427             (
00428                 "shellSurfaces::shellSurfaces"
00429                 "(const searchableSurfaces&, const dictionary>&"
00430             )   << "No surface called " << iter().keyword() << endl
00431                 << "Valid surfaces are " << allGeometry_.names()
00432                 << exit(FatalError);
00433         }
00434         const dictionary& dict = shellsDict.subDict(iter().keyword());
00435 
00436         modes_[shellI] = refineModeNames_.read(dict.lookup("mode"));
00437 
00438         // Read pairs of distance+level
00439         setAndCheckLevels(shellI, dict.lookup("levels"));
00440 
00441         shellI++;
00442     }
00443 
00444     // Orient shell surfaces before any searching is done. Note that this
00445     // only needs to be done for inside or outside. Orienting surfaces
00446     // constructs lots of addressing which we want to avoid.
00447     orient();
00448 }
00449 
00450 
00451 // * * * * * * * * * * * * * * * Member Functions  * * * * * * * * * * * * * //
00452 
00453 // Highest shell level
00454 Foam::label Foam::shellSurfaces::maxLevel() const
00455 {
00456     label overallMax = 0;
00457     forAll(levels_, shellI)
00458     {
00459         overallMax = max(overallMax, max(levels_[shellI]));
00460     }
00461     return overallMax;
00462 }
00463 
00464 
00465 void Foam::shellSurfaces::findHigherLevel
00466 (
00467     const pointField& pt,
00468     const labelList& ptLevel,
00469     labelList& maxLevel
00470 ) const
00471 {
00472     // Maximum level of any shell. Start off with level of point.
00473     maxLevel = ptLevel;
00474 
00475     forAll(shells_, shellI)
00476     {
00477         findHigherLevel(pt, shellI, maxLevel);
00478     }
00479 }
00480 
00481 
00482 // ************************************************************************* //
Copyright © 2000-2009 OpenCFD Ltd