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

searchableSurfaceCollection.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 "searchableSurfaceCollection.H"
00028 #include "addToRunTimeSelectionTable.H"
00029 #include "SortableList.H"
00030 #include "Time.H"
00031 #include "ListOps.H"
00032 
00033 // * * * * * * * * * * * * * * Static Data Members * * * * * * * * * * * * * //
00034 
00035 namespace Foam
00036 {
00037 
00038 defineTypeNameAndDebug(searchableSurfaceCollection, 0);
00039 addToRunTimeSelectionTable
00040 (
00041     searchableSurface,
00042     searchableSurfaceCollection,
00043     dict
00044 );
00045 
00046 }
00047 
00048 
00049 // * * * * * * * * * * * * * Private Member Functions  * * * * * * * * * * * //
00050 
00051 void Foam::searchableSurfaceCollection::findNearest
00052 (
00053     const pointField& samples,
00054     scalarField& minDistSqr,
00055     List<pointIndexHit>& nearestInfo,
00056     labelList& nearestSurf
00057 ) const
00058 {
00059     // Initialise
00060     nearestInfo.setSize(samples.size());
00061     nearestInfo = pointIndexHit();
00062     nearestSurf.setSize(samples.size());
00063     nearestSurf = -1;
00064 
00065     List<pointIndexHit> hitInfo(samples.size());
00066 
00067     const scalarField localMinDistSqr(samples.size(), GREAT);
00068 
00069     forAll(subGeom_, surfI)
00070     {
00071         subGeom_[surfI].findNearest
00072         (
00073             cmptDivide  // Transform then divide
00074             (
00075                 transform_[surfI].localPosition(samples),
00076                 scale_[surfI]
00077             ),
00078             localMinDistSqr,
00079             hitInfo
00080         );
00081 
00082         forAll(hitInfo, pointI)
00083         {
00084             if (hitInfo[pointI].hit())
00085             {
00086                 // Rework back into global coordinate sys. Multiply then
00087                 // transform
00088                 point globalPt = transform_[surfI].globalPosition
00089                 (
00090                     cmptMultiply
00091                     (
00092                         hitInfo[pointI].rawPoint(),
00093                         scale_[surfI]
00094                     )
00095                 );
00096 
00097                 scalar distSqr = magSqr(globalPt - samples[pointI]);
00098 
00099                 if (distSqr < minDistSqr[pointI])
00100                 {
00101                     minDistSqr[pointI] = distSqr;
00102                     nearestInfo[pointI].setPoint(globalPt);
00103                     nearestInfo[pointI].setHit();
00104                     nearestInfo[pointI].setIndex(hitInfo[pointI].index());
00105                     nearestSurf[pointI] = surfI;
00106                 }
00107             }
00108         }
00109     }
00110 }
00111 
00112 
00113 // * * * * * * * * * * * * * * * * Constructors  * * * * * * * * * * * * * * //
00114 
00115 Foam::searchableSurfaceCollection::searchableSurfaceCollection
00116 (
00117     const IOobject& io,
00118     const dictionary& dict
00119 )
00120 :
00121     searchableSurface(io),
00122     instance_(dict.size()),
00123     scale_(dict.size()),
00124     transform_(dict.size()),
00125     subGeom_(dict.size()),
00126     mergeSubRegions_(dict.lookup("mergeSubRegions"))
00127 {
00128     Info<< "SearchableCollection : " << name() << endl;
00129 
00130     label surfI = 0;
00131     forAllConstIter(dictionary, dict, iter)
00132     {
00133         if (dict.isDict(iter().keyword()))
00134         {
00135             instance_[surfI] = iter().keyword();
00136 
00137             const dictionary& subDict = dict.subDict(instance_[surfI]);
00138 
00139             scale_[surfI] = subDict.lookup("scale");
00140             transform_.set
00141             (
00142                 surfI,
00143                 coordinateSystem::New
00144                 (
00145                     "",
00146                     subDict.subDict("transform")
00147                 )
00148             );
00149 
00150             const word subGeomName(subDict.lookup("surface"));
00151             //Pout<< "Trying to find " << subGeomName << endl;
00152 
00153             const searchableSurface& s =
00154                 io.db().lookupObject<searchableSurface>(subGeomName);
00155 
00156             subGeom_.set(surfI, &const_cast<searchableSurface&>(s));
00157 
00158             Info<< "    instance : " << instance_[surfI] << endl;
00159             Info<< "    surface  : " << s.name() << endl;
00160             Info<< "    scale    : " << scale_[surfI] << endl;
00161             Info<< "    coordsys : " << transform_[surfI] << endl;
00162 
00163             surfI++;
00164         }
00165     }
00166     instance_.setSize(surfI);
00167     scale_.setSize(surfI);
00168     transform_.setSize(surfI);
00169     subGeom_.setSize(surfI);
00170 }
00171 
00172 
00173 // * * * * * * * * * * * * * * * * Destructor  * * * * * * * * * * * * * * * //
00174 
00175 Foam::searchableSurfaceCollection::~searchableSurfaceCollection()
00176 {}
00177 
00178 
00179 // * * * * * * * * * * * * * * * Member Functions  * * * * * * * * * * * * * //
00180 
00181 const Foam::wordList& Foam::searchableSurfaceCollection::regions() const
00182 {
00183     if (regions_.size() == 0)
00184     {
00185         regionOffset_.setSize(subGeom_.size());
00186 
00187         DynamicList<word> allRegions;
00188         forAll(subGeom_, surfI)
00189         {
00190             regionOffset_[surfI] = allRegions.size();
00191 
00192             if (mergeSubRegions_)
00193             {
00194                 // Single name regardless how many regions subsurface has
00195                 allRegions.append(instance_[surfI] + "_" + Foam::name(surfI));
00196             }
00197             else
00198             {
00199                 const wordList& subRegions = subGeom_[surfI].regions();
00200 
00201                 forAll(subRegions, i)
00202                 {
00203                     allRegions.append(instance_[surfI] + "_" + subRegions[i]);
00204                 }
00205             }
00206         }
00207         regions_.transfer(allRegions.shrink());
00208     }
00209     return regions_;
00210 }
00211 
00212 
00213 Foam::label Foam::searchableSurfaceCollection::size() const
00214 {
00215     label n = 0;
00216     forAll(subGeom_, surfI)
00217     {
00218         n += subGeom_[surfI].size();
00219     }
00220     return n;
00221 }
00222 
00223 
00224 void Foam::searchableSurfaceCollection::findNearest
00225 (
00226     const pointField& samples,
00227     const scalarField& nearestDistSqr,
00228     List<pointIndexHit>& nearestInfo
00229 ) const
00230 {
00231     // How to scale distance?
00232     scalarField minDistSqr(nearestDistSqr);
00233 
00234     labelList nearestSurf;
00235     findNearest
00236     (
00237         samples,
00238         minDistSqr,
00239         nearestInfo,
00240         nearestSurf
00241     );
00242 }
00243 
00244 
00245 void Foam::searchableSurfaceCollection::findLine
00246 (
00247     const pointField& start,
00248     const pointField& end,
00249     List<pointIndexHit>& info
00250 ) const
00251 {
00252     info.setSize(start.size());
00253     info = pointIndexHit();
00254 
00255     // Current nearest (to start) intersection
00256     pointField nearest(end);
00257 
00258     List<pointIndexHit> hitInfo(start.size());
00259 
00260     forAll(subGeom_, surfI)
00261     {
00262         // Starting point
00263         tmp<pointField> e0 = cmptDivide
00264         (
00265             transform_[surfI].localPosition
00266             (
00267                 start
00268             ),
00269             scale_[surfI]
00270         );
00271 
00272         // Current best end point
00273         tmp<pointField> e1 = cmptDivide
00274         (
00275             transform_[surfI].localPosition
00276             (
00277                 nearest
00278             ),
00279             scale_[surfI]
00280         );
00281 
00282         subGeom_[surfI].findLine(e0, e1, hitInfo);
00283 
00284         forAll(hitInfo, pointI)
00285         {
00286             if (hitInfo[pointI].hit())
00287             {
00288                 // Transform back to global coordinate sys.
00289                 nearest[pointI] = transform_[surfI].globalPosition
00290                 (
00291                     cmptMultiply
00292                     (
00293                         hitInfo[pointI].rawPoint(),
00294                         scale_[surfI]
00295                     )
00296                 );
00297                 info[pointI] = hitInfo[pointI];
00298                 info[pointI].rawPoint() = nearest[pointI];
00299             }
00300         }
00301     }
00302 
00303 
00304     // Debug check
00305     if (false)
00306     {
00307         forAll(info, pointI)
00308         {
00309             if (info[pointI].hit())
00310             {
00311                 vector n(end[pointI] - start[pointI]);
00312                 scalar magN = mag(n);
00313 
00314                 if (magN > SMALL)
00315                 {
00316                     n /= mag(n);
00317 
00318                     scalar s = ((info[pointI].rawPoint()-start[pointI])&n);
00319 
00320                     if (s < 0 || s > 1)
00321                     {
00322                         FatalErrorIn
00323                         (
00324                             "searchableSurfaceCollection::findLine(..)"
00325                         )   << "point:" << info[pointI]
00326                             << " s:" << s
00327                             << " outside vector "
00328                             << " start:" << start[pointI]
00329                             << " end:" << end[pointI]
00330                             << abort(FatalError);
00331                     }
00332                 }
00333             }
00334         }
00335     }
00336 }
00337 
00338 
00339 void Foam::searchableSurfaceCollection::findLineAny
00340 (
00341     const pointField& start,
00342     const pointField& end,
00343     List<pointIndexHit>& info
00344 ) const
00345 {
00346     // To be done ...
00347     findLine(start, end, info);
00348 }
00349 
00350 
00351 void Foam::searchableSurfaceCollection::findLineAll
00352 (
00353     const pointField& start,
00354     const pointField& end,
00355     List<List<pointIndexHit> >& info
00356 ) const
00357 {
00358     // To be done. Assume for now only one intersection.
00359     List<pointIndexHit> nearestInfo;
00360     findLine(start, end, nearestInfo);
00361 
00362     info.setSize(start.size());
00363     forAll(info, pointI)
00364     {
00365         if (nearestInfo[pointI].hit())
00366         {
00367             info[pointI].setSize(1);
00368             info[pointI][0] = nearestInfo[pointI];
00369         }
00370         else
00371         {
00372             info[pointI].clear();
00373         }
00374     }
00375 }
00376 
00377 
00378 void Foam::searchableSurfaceCollection::getRegion
00379 (
00380     const List<pointIndexHit>& info,
00381     labelList& region
00382 ) const
00383 {
00384     if (subGeom_.size() == 0)
00385     {}
00386     else if (subGeom_.size() == 1)
00387     {
00388         if (mergeSubRegions_)
00389         {
00390             region.setSize(info.size());
00391             region = regionOffset_[0];
00392         }
00393         else
00394         {
00395             subGeom_[0].getRegion(info, region);
00396         }
00397     }
00398     else
00399     {
00400         region.setSize(info.size());
00401         region = -1;
00402 
00403         // Which region did point come from. Retest for now to see which
00404         // surface it originates from - crap solution! Should use global indices
00405         // in index inside pointIndexHit to do this better.
00406 
00407         pointField samples(info.size());
00408         forAll(info, pointI)
00409         {
00410             if (info[pointI].hit())
00411             {
00412                 samples[pointI] = info[pointI].hitPoint();
00413             }
00414             else
00415             {
00416                 samples[pointI] = vector::zero;
00417             }
00418         }
00419         //scalarField minDistSqr(info.size(), SMALL);
00420         scalarField minDistSqr(info.size(), GREAT);
00421 
00422         labelList nearestSurf;
00423         List<pointIndexHit> nearestInfo;
00424         findNearest
00425         (
00426             samples,
00427             minDistSqr,
00428             nearestInfo,
00429             nearestSurf
00430         );
00431 
00432         // Check
00433         {
00434             forAll(info, pointI)
00435             {
00436                 if (info[pointI].hit() && nearestSurf[pointI] == -1)
00437                 {
00438                     FatalErrorIn
00439                     (
00440                         "searchableSurfaceCollection::getRegion(..)"
00441                     )   << "pointI:" << pointI
00442                         << " sample:" << samples[pointI]
00443                         << " nearest:" << nearestInfo[pointI]
00444                         << " nearestsurf:" << nearestSurf[pointI]
00445                         << abort(FatalError);
00446                 }
00447             }
00448         }
00449 
00450         forAll(subGeom_, surfI)
00451         {
00452             // Collect points from my surface
00453             labelList indices(findIndices(nearestSurf, surfI));
00454 
00455             if (mergeSubRegions_)
00456             {
00457                 forAll(indices, i)
00458                 {
00459                     region[indices[i]] = regionOffset_[surfI];
00460                 }
00461             }
00462             else
00463             {
00464                 labelList surfRegion;
00465                 subGeom_[surfI].getRegion
00466                 (
00467                     UIndirectList<pointIndexHit>(info, indices),
00468                     surfRegion
00469                 );
00470                 forAll(indices, i)
00471                 {
00472                     region[indices[i]] = regionOffset_[surfI] + surfRegion[i];
00473                 }
00474             }
00475         }
00476     }
00477 }
00478 
00479 
00480 void Foam::searchableSurfaceCollection::getNormal
00481 (
00482     const List<pointIndexHit>& info,
00483     vectorField& normal
00484 ) const
00485 {
00486     if (subGeom_.size() == 0)
00487     {}
00488     else if (subGeom_.size() == 1)
00489     {
00490         subGeom_[0].getNormal(info, normal);
00491     }
00492     else
00493     {
00494         normal.setSize(info.size());
00495 
00496         // See above - crap retest to find surface point originates from.
00497         pointField samples(info.size());
00498         forAll(info, pointI)
00499         {
00500             if (info[pointI].hit())
00501             {
00502                 samples[pointI] = info[pointI].hitPoint();
00503             }
00504             else
00505             {
00506                 samples[pointI] = vector::zero;
00507             }
00508         }
00509         //scalarField minDistSqr(info.size(), SMALL);
00510         scalarField minDistSqr(info.size(), GREAT);
00511 
00512         labelList nearestSurf;
00513         List<pointIndexHit> nearestInfo;
00514         findNearest
00515         (
00516             samples,
00517             minDistSqr,
00518             nearestInfo,
00519             nearestSurf
00520         );
00521 
00522 
00523         forAll(subGeom_, surfI)
00524         {
00525             // Collect points from my surface
00526             labelList indices(findIndices(nearestSurf, surfI));
00527 
00528             vectorField surfNormal;
00529             subGeom_[surfI].getNormal
00530             (
00531                 UIndirectList<pointIndexHit>(info, indices),
00532                 surfNormal
00533             );
00534             forAll(indices, i)
00535             {
00536                 normal[indices[i]] = surfNormal[i];
00537             }
00538         }
00539     }
00540 }
00541 
00542 
00543 void Foam::searchableSurfaceCollection::getVolumeType
00544 (
00545     const pointField& points,
00546     List<volumeType>& volType
00547 ) const
00548 {
00549     FatalErrorIn
00550     (
00551         "searchableSurfaceCollection::getVolumeType(const pointField&"
00552         ", List<volumeType>&) const"
00553     )   << "Volume type not supported for collection."
00554         << exit(FatalError);
00555 }
00556 
00557 
00558 // ************************************************************************* //
Copyright © 2000-2009 OpenCFD Ltd