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

surfaceRedistributePar.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-2007 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 Application
00026     surfaceRedistributePar
00027 
00028 Description
00029     (Re)distribution of triSurface. Either takes an undecomposed surface
00030     or an already decomposed surface and redistribute it so each processor
00031     has all triangles that overlap its mesh.
00032 
00033 Note
00034     - best decomposition option is hierarchGeomDecomp since
00035     guarantees square decompositions.
00036     - triangles might be present on multiple processors.
00037     - merging uses geometric tolerance so take care with writing precision.
00038 
00039 \*---------------------------------------------------------------------------*/
00040 
00041 #include "treeBoundBox.H"
00042 #include "FixedList.H"
00043 #include "argList.H"
00044 #include "Time.H"
00045 #include "polyMesh.H"
00046 #include "distributedTriSurfaceMesh.H"
00047 #include "mapDistribute.H"
00048 #include "triSurfaceFields.H"
00049 #include "Pair.H"
00050 
00051 using namespace Foam;
00052 
00053 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
00054 
00055 // Print on master all the per-processor surface stats.
00056 void writeProcStats
00057 (
00058     const triSurface& s,
00059     const List<List<treeBoundBox> >& meshBb
00060 )
00061 {
00062     // Determine surface bounding boxes, faces, points
00063     List<treeBoundBox> surfBb(Pstream::nProcs());
00064     {
00065         surfBb[Pstream::myProcNo()] = boundBox(s.points(), false);
00066         Pstream::gatherList(surfBb);
00067         Pstream::scatterList(surfBb);
00068     }
00069 
00070     labelList nPoints(Pstream::nProcs());
00071     nPoints[Pstream::myProcNo()] = s.points().size();
00072     Pstream::gatherList(nPoints);
00073     Pstream::scatterList(nPoints);
00074 
00075     labelList nFaces(Pstream::nProcs());
00076     nFaces[Pstream::myProcNo()] = s.size();
00077     Pstream::gatherList(nFaces);
00078     Pstream::scatterList(nFaces);
00079 
00080     forAll(surfBb, procI)
00081     {
00082         const List<treeBoundBox>& bbs = meshBb[procI];
00083 
00084         Info<< "processor" << procI << endl
00085             << "\tMesh bounds          : " << bbs[0] << nl;
00086         for (label i = 1; i < bbs.size(); i++)
00087         {
00088             Info<< "\t                       " << bbs[i]<< nl;
00089         }
00090         Info<< "\tSurface bounding box : " << surfBb[procI] << nl
00091             << "\tTriangles            : " << nFaces[procI] << nl
00092             << "\tVertices             : " << nPoints[procI]
00093             << endl;
00094     }
00095     Info<< endl;
00096 }
00097 
00098 
00099 // Main program:
00100 
00101 int main(int argc, char *argv[])
00102 {
00103     argList::validArgs.append("triSurfaceMesh");
00104     argList::validArgs.append("distributionType");
00105 
00106     argList::validOptions.insert("keepNonMapped", "");
00107 #   include "setRootCase.H"
00108 #   include "createTime.H"
00109     runTime.functionObjects().off();
00110 
00111     fileName surfFileName(args.additionalArgs()[0]);
00112     Info<< "Reading surface from " << surfFileName << nl << endl;
00113 
00114     const word distType(args.additionalArgs()[1]);
00115 
00116     Info<< "Using distribution method "
00117         << distributedTriSurfaceMesh::distributionTypeNames_[distType]
00118         << " " << distType << nl << endl;
00119 
00120     bool keepNonMapped = args.options().found("keepNonMapped");
00121 
00122     if (keepNonMapped)
00123     {
00124         Info<< "Preserving surface outside of mesh bounds." << nl << endl;
00125     }
00126     else
00127     {
00128         Info<< "Removing surface outside of mesh bounds." << nl << endl;
00129     }
00130 
00131 
00132     if (!Pstream::parRun())
00133     {
00134         FatalErrorIn(args.executable())
00135             << "Please run this program on the decomposed case."
00136             << " It will read surface " << surfFileName
00137             << " and decompose it such that it overlaps the mesh bounding box."
00138             << exit(FatalError);
00139     }
00140 
00141 
00142 #   include "createPolyMesh.H"
00143 
00144     Random rndGen(653213);
00145 
00146     // Determine mesh bounding boxes:
00147     List<List<treeBoundBox> > meshBb(Pstream::nProcs());
00148     {
00149         meshBb[Pstream::myProcNo()] = List<treeBoundBox>
00150         (
00151             1,
00152             treeBoundBox
00153             (
00154                 boundBox(mesh.points(), false)
00155             ).extend(rndGen, 1E-3)
00156         );
00157         Pstream::gatherList(meshBb);
00158         Pstream::scatterList(meshBb);
00159     }
00160 
00161     IOobject io
00162     (
00163         surfFileName,         // name
00164         //runTime.findInstance("triSurface", surfFileName),   // instance
00165         runTime.constant(),   // instance
00166         "triSurface",         // local
00167         runTime,              // registry
00168         IOobject::MUST_READ,
00169         IOobject::NO_WRITE
00170     );
00171 
00172     const fileName actualPath(io.filePath());
00173     fileName localPath(actualPath);
00174     localPath.replace(runTime.rootPath() + '/', "");
00175 
00176     if (actualPath == io.objectPath())
00177     {
00178         Info<< "Loading local (decomposed) surface " << localPath << nl <<endl;
00179     }
00180     else
00181     {
00182         Info<< "Loading undecomposed surface " << localPath << nl << endl;
00183     }
00184 
00185 
00186     // Create dummy dictionary for bounding boxes if does not exist.
00187     if (!isFile(actualPath / "Dict"))
00188     {
00189         dictionary dict;
00190         dict.add("bounds", meshBb[Pstream::myProcNo()]);
00191         dict.add("distributionType", distType);
00192         dict.add("mergeDistance", SMALL);
00193 
00194         IOdictionary ioDict
00195         (
00196             IOobject
00197             (
00198                 io.name() + "Dict",
00199                 io.instance(),
00200                 io.local(),
00201                 io.db(),
00202                 IOobject::NO_READ,
00203                 IOobject::NO_WRITE,
00204                 false
00205             ),
00206             dict
00207         );
00208 
00209         Info<< "Writing dummy bounds dictionary to " << ioDict.name()
00210             << nl << endl;
00211 
00212         ioDict.regIOobject::writeObject
00213         (
00214             IOstream::ASCII,
00215             IOstream::currentVersion,
00216             ioDict.time().writeCompression()        
00217         );
00218     }
00219 
00220 
00221     // Load surface
00222     distributedTriSurfaceMesh surfMesh(io);
00223     Info<< "Loaded surface" << nl << endl;
00224 
00225 
00226     // Generate a test field
00227     {
00228         const triSurface& s = static_cast<const triSurface&>(surfMesh);
00229 
00230         autoPtr<triSurfaceVectorField> fcPtr
00231         (
00232             new triSurfaceVectorField
00233             (
00234                 IOobject
00235                 (
00236                     surfMesh.searchableSurface::name(),     // name
00237                     surfMesh.searchableSurface::instance(), // instance
00238                     surfMesh.searchableSurface::local(),    // local
00239                     surfMesh,
00240                     IOobject::NO_READ,
00241                     IOobject::AUTO_WRITE
00242                 ),
00243                 surfMesh,
00244                 dimLength
00245             )
00246         );
00247         triSurfaceVectorField& fc = fcPtr();
00248 
00249         forAll(fc, triI)
00250         {
00251             fc[triI] = s[triI].centre(s.points());
00252         }
00253 
00254         // Steal pointer and store object on surfMesh
00255         fcPtr.ptr()->store();
00256     }
00257 
00258 
00259     // Write per-processor stats
00260     Info<< "Before redistribution:" << endl;
00261     writeProcStats(surfMesh, meshBb);
00262 
00263 
00264     // Do redistribution
00265     Info<< "Redistributing surface" << nl << endl;
00266     autoPtr<mapDistribute> faceMap;
00267     autoPtr<mapDistribute> pointMap;
00268     surfMesh.distribute
00269     (
00270         meshBb[Pstream::myProcNo()],
00271         keepNonMapped,
00272         faceMap,
00273         pointMap
00274     );
00275     faceMap.clear();
00276     pointMap.clear();
00277 
00278     Info<< endl;
00279 
00280 
00281     // Write per-processor stats
00282     Info<< "After redistribution:" << endl;
00283     writeProcStats(surfMesh, meshBb);
00284 
00285 
00286     Info<< "Writing surface." << nl << endl;
00287     surfMesh.searchableSurface::write();
00288 
00289     Info<< "End\n" << endl;
00290 
00291     return 0;
00292 }
00293 
00294 
00295 // ************************************************************************* //
Copyright © 2000-2009 OpenCFD Ltd