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

decomposePar.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 Application
00026     decomposePar
00027 
00028 Description
00029     Automatically decomposes a mesh and fields of a case for parallel
00030     execution of OpenFOAM.
00031 
00032 Usage
00033 
00034     - decomposePar [OPTION]
00035 
00036     @param -cellDist \n
00037     Write the cell distribution as a labelList for use with 'manual'
00038     decomposition method and as a volScalarField for post-processing.
00039 
00040     @param -region regionName \n
00041     Decompose named region. Does not check for existence of processor*.
00042 
00043     @param -copyUniform \n
00044     Copy any @a uniform directories too.
00045 
00046     @param -fields \n
00047     Use existing geometry decomposition and convert fields only.
00048 
00049     @param -filterPatches \n
00050     Remove empty patches when decomposing the geometry.
00051 
00052     @param -force \n
00053     Remove any existing @a processor subdirectories before decomposing the
00054     geometry.
00055 
00056     @param -ifRequired \n
00057     Only decompose the geometry if the number of domains has changed from a
00058     previous decomposition. No @a processor subdirectories will be removed
00059     unless the @a -force option is also specified. This option can be used
00060     to avoid redundant geometry decomposition (eg, in scripts), but should
00061     be used with caution when the underlying (serial) geometry or the
00062     decomposition method etc. have been changed between decompositions.
00063 
00064 \*---------------------------------------------------------------------------*/
00065 
00066 #include "OSspecific.H"
00067 #include "fvCFD.H"
00068 #include "IOobjectList.H"
00069 #include "processorFvPatchFields.H"
00070 #include "domainDecomposition.H"
00071 #include "labelIOField.H"
00072 #include "scalarIOField.H"
00073 #include "vectorIOField.H"
00074 #include "sphericalTensorIOField.H"
00075 #include "symmTensorIOField.H"
00076 #include "tensorIOField.H"
00077 #include "pointFields.H"
00078 
00079 #include "readFields.H"
00080 #include "fvFieldDecomposer.H"
00081 #include "pointFieldDecomposer.H"
00082 #include "lagrangianFieldDecomposer.H"
00083 
00084 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
00085 
00086 int main(int argc, char *argv[])
00087 {
00088     argList::noParallel();
00089 #   include "addRegionOption.H"
00090     argList::validOptions.insert("cellDist", "");
00091     argList::validOptions.insert("copyUniform", "");
00092     argList::validOptions.insert("fields", "");
00093     argList::validOptions.insert("filterPatches", "");
00094     argList::validOptions.insert("force", "");
00095     argList::validOptions.insert("ifRequired", "");
00096 
00097 #   include "setRootCase.H"
00098 
00099     word regionName = fvMesh::defaultRegion;
00100     word regionDir = word::null;
00101 
00102     if (args.optionFound("region"))
00103     {
00104         regionName = args.option("region");
00105         regionDir = regionName;
00106         Info<< "Decomposing mesh " << regionName << nl << endl;
00107     }
00108 
00109 
00110     bool writeCellDist           = args.optionFound("cellDist");
00111     bool copyUniform             = args.optionFound("copyUniform");
00112     bool decomposeFieldsOnly     = args.optionFound("fields");
00113     bool filterPatches           = args.optionFound("filterPatches");
00114     bool forceOverwrite          = args.optionFound("force");
00115     bool ifRequiredDecomposition = args.optionFound("ifRequired");
00116 
00117 #   include "createTime.H"
00118 
00119     Info<< "Time = " << runTime.timeName() << endl;
00120 
00121     // determine the existing processor count directly
00122     label nProcs = 0;
00123     while
00124     (
00125         isDir
00126         (
00127             runTime.path()
00128            /(word("processor") + name(nProcs))
00129            /runTime.constant()
00130            /regionDir
00131            /polyMesh::meshSubDir
00132         )
00133     )
00134     {
00135         ++nProcs;
00136     }
00137 
00138     // get requested numberOfSubdomains
00139     label nDomains = 0;
00140     {
00141         IOdictionary decompDict
00142         (
00143             IOobject
00144             (
00145                 "decomposeParDict",
00146                 runTime.time().system(),
00147                 regionDir,          // use region if non-standard
00148                 runTime,
00149                 IOobject::MUST_READ,
00150                 IOobject::NO_WRITE,
00151                 false
00152             )
00153         );
00154 
00155         decompDict.lookup("numberOfSubdomains") >> nDomains;
00156     }
00157 
00158     if (decomposeFieldsOnly)
00159     {
00160         // Sanity check on previously decomposed case
00161         if (nProcs != nDomains)
00162         {
00163             FatalErrorIn(args.executable())
00164                 << "Specified -fields, but the case was decomposed with "
00165                 << nProcs << " domains"
00166                 << nl
00167                 << "instead of " << nDomains
00168                 << " domains as specified in decomposeParDict"
00169                 << nl
00170                 << exit(FatalError);
00171         }
00172     }
00173     else if (nProcs)
00174     {
00175         bool procDirsProblem = true;
00176 
00177         if (ifRequiredDecomposition && nProcs == nDomains)
00178         {
00179             // we can reuse the decomposition
00180             decomposeFieldsOnly = true;
00181             procDirsProblem = false;
00182             forceOverwrite = false;
00183 
00184             Info<< "Using existing processor directories" << nl;
00185         }
00186 
00187         if (forceOverwrite)
00188         {
00189             Info<< "Removing " << nProcs
00190                 << " existing processor directories" << endl;
00191 
00192             // remove existing processor dirs
00193             // reverse order to avoid gaps if someone interrupts the process
00194             for (label procI = nProcs-1; procI >= 0; --procI)
00195             {
00196                 fileName procDir
00197                 (
00198                     runTime.path()/(word("processor") + name(procI))
00199                 );
00200 
00201                 rmDir(procDir);
00202             }
00203 
00204             procDirsProblem = false;
00205         }
00206 
00207         if (procDirsProblem)
00208         {
00209             FatalErrorIn(args.executable())
00210                 << "Case is already decomposed with " << nProcs
00211                 << " domains, use the -force option or manually" << nl
00212                 << "remove processor directories before decomposing. e.g.,"
00213                 << nl
00214                 << "    rm -rf " << runTime.path().c_str() << "/processor*"
00215                 << nl
00216                 << exit(FatalError);
00217         }
00218     }
00219 
00220     Info<< "Create mesh" << endl;
00221     domainDecomposition mesh
00222     (
00223         IOobject
00224         (
00225             regionName,
00226             runTime.timeName(),
00227             runTime
00228         )
00229     );
00230 
00231     // Decompose the mesh
00232     if (!decomposeFieldsOnly)
00233     {
00234         mesh.decomposeMesh(filterPatches);
00235 
00236         mesh.writeDecomposition();
00237 
00238         if (writeCellDist)
00239         {
00240             // Write the decomposition as labelList for use with 'manual'
00241             // decomposition method.
00242 
00243             // FIXME: may attempt to write to a non-existent "region0/"
00244             OFstream os
00245             (
00246                 runTime.path()
00247               / mesh.facesInstance()
00248               / regionName
00249               / "cellDecomposition"
00250             );
00251 
00252             os << mesh.cellToProc();
00253 
00254             Info<< nl << "Wrote decomposition to "
00255                 << os.name() << " for use in manual decomposition."
00256                 << endl;
00257 
00258             // Write as volScalarField for postprocessing.
00259             volScalarField cellDist
00260             (
00261                 IOobject
00262                 (
00263                     "cellDist",
00264                     runTime.timeName(),
00265                     mesh,
00266                     IOobject::NO_READ,
00267                     IOobject::AUTO_WRITE
00268                 ),
00269                 mesh,
00270                 dimensionedScalar("cellDist", dimless, 0),
00271                 zeroGradientFvPatchScalarField::typeName
00272             );
00273 
00274             const labelList& procIds = mesh.cellToProc();
00275             forAll(procIds, celli)
00276             {
00277                cellDist[celli] = procIds[celli];
00278             }
00279 
00280             cellDist.write();
00281 
00282             Info<< nl << "Wrote decomposition as volScalarField to "
00283                 << cellDist.name() << " for use in postprocessing."
00284                 << endl;
00285         }
00286     }
00287 
00288 
00289     // Search for list of objects for this time
00290     IOobjectList objects(mesh, runTime.timeName());
00291 
00292     // Construct the vol fields
00293     // ~~~~~~~~~~~~~~~~~~~~~~~~
00294     PtrList<volScalarField> volScalarFields;
00295     readFields(mesh, objects, volScalarFields);
00296 
00297     PtrList<volVectorField> volVectorFields;
00298     readFields(mesh, objects, volVectorFields);
00299 
00300     PtrList<volSphericalTensorField> volSphericalTensorFields;
00301     readFields(mesh, objects, volSphericalTensorFields);
00302 
00303     PtrList<volSymmTensorField> volSymmTensorFields;
00304     readFields(mesh, objects, volSymmTensorFields);
00305 
00306     PtrList<volTensorField> volTensorFields;
00307     readFields(mesh, objects, volTensorFields);
00308 
00309 
00310     // Construct the surface fields
00311     // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~
00312     PtrList<surfaceScalarField> surfaceScalarFields;
00313     readFields(mesh, objects, surfaceScalarFields);
00314 
00315 
00316     // Construct the point fields
00317     // ~~~~~~~~~~~~~~~~~~~~~~~~~~
00318     pointMesh pMesh(mesh);
00319 
00320     PtrList<pointScalarField> pointScalarFields;
00321     readFields(pMesh, objects, pointScalarFields);
00322 
00323     PtrList<pointVectorField> pointVectorFields;
00324     readFields(pMesh, objects, pointVectorFields);
00325 
00326     PtrList<pointSphericalTensorField> pointSphericalTensorFields;
00327     readFields(pMesh, objects, pointSphericalTensorFields);
00328 
00329     PtrList<pointSymmTensorField> pointSymmTensorFields;
00330     readFields(pMesh, objects, pointSymmTensorFields);
00331 
00332     PtrList<pointTensorField> pointTensorFields;
00333     readFields(pMesh, objects, pointTensorFields);
00334 
00335 
00336     // Construct the Lagrangian fields
00337     // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
00338 
00339     fileNameList cloudDirs
00340     (
00341         readDir(runTime.timePath()/cloud::prefix, fileName::DIRECTORY)
00342     );
00343 
00344     // Particles
00345     PtrList<Cloud<indexedParticle> > lagrangianPositions(cloudDirs.size());
00346     // Particles per cell
00347     PtrList< List<SLList<indexedParticle*>*> > cellParticles(cloudDirs.size());
00348 
00349     PtrList<PtrList<labelIOField> > lagrangianLabelFields(cloudDirs.size());
00350     PtrList<PtrList<scalarIOField> > lagrangianScalarFields(cloudDirs.size());
00351     PtrList<PtrList<vectorIOField> > lagrangianVectorFields(cloudDirs.size());
00352     PtrList<PtrList<sphericalTensorIOField> > lagrangianSphericalTensorFields
00353     (
00354         cloudDirs.size()
00355     );
00356     PtrList<PtrList<symmTensorIOField> > lagrangianSymmTensorFields
00357     (
00358         cloudDirs.size()
00359     );
00360     PtrList<PtrList<tensorIOField> > lagrangianTensorFields
00361     (
00362         cloudDirs.size()
00363     );
00364 
00365     label cloudI = 0;
00366 
00367     forAll(cloudDirs, i)
00368     {
00369         IOobjectList sprayObjs
00370         (
00371             mesh,
00372             runTime.timeName(),
00373             cloud::prefix/cloudDirs[i]
00374         );
00375 
00376         IOobject* positionsPtr = sprayObjs.lookup("positions");
00377 
00378         if (positionsPtr)
00379         {
00380             // Read lagrangian particles
00381             // ~~~~~~~~~~~~~~~~~~~~~~~~~
00382 
00383             Info<< "Identified lagrangian data set: " << cloudDirs[i] << endl;
00384 
00385             lagrangianPositions.set
00386             (
00387                 cloudI,
00388                 new Cloud<indexedParticle>
00389                 (
00390                     mesh,
00391                     cloudDirs[i],
00392                     false
00393                 )
00394             );
00395 
00396 
00397             // Sort particles per cell
00398             // ~~~~~~~~~~~~~~~~~~~~~~~
00399 
00400             cellParticles.set
00401             (
00402                 cloudI,
00403                 new List<SLList<indexedParticle*>*>
00404                 (
00405                     mesh.nCells(),
00406                     static_cast<SLList<indexedParticle*>*>(NULL)
00407                 )
00408             );
00409 
00410             label i = 0;
00411 
00412             forAllIter
00413             (
00414                 Cloud<indexedParticle>,
00415                 lagrangianPositions[cloudI],
00416                 iter
00417             )
00418             {
00419                 iter().index() = i++;
00420 
00421                 label celli = iter().cell();
00422 
00423                 // Check
00424                 if (celli < 0 || celli >= mesh.nCells())
00425                 {
00426                     FatalErrorIn(args.executable())
00427                         << "Illegal cell number " << celli
00428                         << " for particle with index " << iter().index()
00429                         << " at position " << iter().position() << nl
00430                         << "Cell number should be between 0 and "
00431                         << mesh.nCells()-1 << nl
00432                         << "On this mesh the particle should be in cell "
00433                         << mesh.findCell(iter().position())
00434                         << exit(FatalError);
00435                 }
00436 
00437                 if (!cellParticles[cloudI][celli])
00438                 {
00439                     cellParticles[cloudI][celli] = new SLList<indexedParticle*>
00440                     ();
00441                 }
00442 
00443                 cellParticles[cloudI][celli]->append(&iter());
00444             }
00445 
00446             // Read fields
00447             // ~~~~~~~~~~~
00448 
00449             IOobjectList lagrangianObjects
00450             (
00451                 mesh,
00452                 runTime.timeName(),
00453                 cloud::prefix/cloudDirs[cloudI]
00454             );
00455 
00456             lagrangianFieldDecomposer::readFields
00457             (
00458                 cloudI,
00459                 lagrangianObjects,
00460                 lagrangianLabelFields
00461             );
00462 
00463             lagrangianFieldDecomposer::readFields
00464             (
00465                 cloudI,
00466                 lagrangianObjects,
00467                 lagrangianScalarFields
00468             );
00469 
00470             lagrangianFieldDecomposer::readFields
00471             (
00472                 cloudI,
00473                 lagrangianObjects,
00474                 lagrangianVectorFields
00475             );
00476 
00477             lagrangianFieldDecomposer::readFields
00478             (
00479                 cloudI,
00480                 lagrangianObjects,
00481                 lagrangianSphericalTensorFields
00482             );
00483 
00484             lagrangianFieldDecomposer::readFields
00485             (
00486                 cloudI,
00487                 lagrangianObjects,
00488                 lagrangianSymmTensorFields
00489             );
00490 
00491             lagrangianFieldDecomposer::readFields
00492             (
00493                 cloudI,
00494                 lagrangianObjects,
00495                 lagrangianTensorFields
00496             );
00497 
00498             cloudI++;
00499         }
00500     }
00501 
00502     lagrangianPositions.setSize(cloudI);
00503     cellParticles.setSize(cloudI);
00504     lagrangianLabelFields.setSize(cloudI);
00505     lagrangianScalarFields.setSize(cloudI);
00506     lagrangianVectorFields.setSize(cloudI);
00507     lagrangianSphericalTensorFields.setSize(cloudI);
00508     lagrangianSymmTensorFields.setSize(cloudI);
00509     lagrangianTensorFields.setSize(cloudI);
00510 
00511 
00512     // Any uniform data to copy/link?
00513     fileName uniformDir("uniform");
00514 
00515     if (isDir(runTime.timePath()/uniformDir))
00516     {
00517         Info<< "Detected additional non-decomposed files in "
00518             << runTime.timePath()/uniformDir
00519             << endl;
00520     }
00521     else
00522     {
00523         uniformDir.clear();
00524     }
00525 
00526     Info<< endl;
00527 
00528     // split the fields over processors
00529     for (label procI = 0; procI < mesh.nProcs(); procI++)
00530     {
00531         Info<< "Processor " << procI << ": field transfer" << endl;
00532 
00533         // open the database
00534         Time processorDb
00535         (
00536             Time::controlDictName,
00537             args.rootPath(),
00538             args.caseName()/fileName(word("processor") + name(procI))
00539         );
00540 
00541         processorDb.setTime(runTime);
00542 
00543         // remove files remnants that can cause horrible problems
00544         // - mut and nut are used to mark the new turbulence models,
00545         //   their existence prevents old models from being upgraded
00546         {
00547             fileName timeDir(processorDb.path()/processorDb.timeName());
00548 
00549             rm(timeDir/"mut");
00550             rm(timeDir/"nut");
00551         }
00552 
00553         // read the mesh
00554         fvMesh procMesh
00555         (
00556             IOobject
00557             (
00558                 regionName,
00559                 processorDb.timeName(),
00560                 processorDb
00561             )
00562         );
00563 
00564         labelIOList cellProcAddressing
00565         (
00566             IOobject
00567             (
00568                 "cellProcAddressing",
00569                 procMesh.facesInstance(),
00570                 procMesh.meshSubDir,
00571                 procMesh,
00572                 IOobject::MUST_READ,
00573                 IOobject::NO_WRITE
00574             )
00575         );
00576 
00577         labelIOList boundaryProcAddressing
00578         (
00579             IOobject
00580             (
00581                 "boundaryProcAddressing",
00582                 procMesh.facesInstance(),
00583                 procMesh.meshSubDir,
00584                 procMesh,
00585                 IOobject::MUST_READ,
00586                 IOobject::NO_WRITE
00587             )
00588         );
00589 
00590         // FV fields
00591         if
00592         (
00593             volScalarFields.size()
00594          || volVectorFields.size()
00595          || volSphericalTensorFields.size()
00596          || volSymmTensorFields.size()
00597          || volTensorFields.size()
00598          || surfaceScalarFields.size()
00599         )
00600         {
00601             labelIOList faceProcAddressing
00602             (
00603                 IOobject
00604                 (
00605                     "faceProcAddressing",
00606                     procMesh.facesInstance(),
00607                     procMesh.meshSubDir,
00608                     procMesh,
00609                     IOobject::MUST_READ,
00610                     IOobject::NO_WRITE
00611                 )
00612             );
00613 
00614             fvFieldDecomposer fieldDecomposer
00615             (
00616                 mesh,
00617                 procMesh,
00618                 faceProcAddressing,
00619                 cellProcAddressing,
00620                 boundaryProcAddressing
00621             );
00622 
00623             fieldDecomposer.decomposeFields(volScalarFields);
00624             fieldDecomposer.decomposeFields(volVectorFields);
00625             fieldDecomposer.decomposeFields(volSphericalTensorFields);
00626             fieldDecomposer.decomposeFields(volSymmTensorFields);
00627             fieldDecomposer.decomposeFields(volTensorFields);
00628 
00629             fieldDecomposer.decomposeFields(surfaceScalarFields);
00630         }
00631 
00632 
00633         // Point fields
00634         if
00635         (
00636             pointScalarFields.size()
00637          || pointVectorFields.size()
00638          || pointSphericalTensorFields.size()
00639          || pointSymmTensorFields.size()
00640          || pointTensorFields.size()
00641         )
00642         {
00643             labelIOList pointProcAddressing
00644             (
00645                 IOobject
00646                 (
00647                     "pointProcAddressing",
00648                     procMesh.facesInstance(),
00649                     procMesh.meshSubDir,
00650                     procMesh,
00651                     IOobject::MUST_READ,
00652                     IOobject::NO_WRITE
00653                 )
00654             );
00655 
00656             pointMesh procPMesh(procMesh, true);
00657 
00658             pointFieldDecomposer fieldDecomposer
00659             (
00660                 pMesh,
00661                 procPMesh,
00662                 pointProcAddressing,
00663                 boundaryProcAddressing
00664             );
00665 
00666             fieldDecomposer.decomposeFields(pointScalarFields);
00667             fieldDecomposer.decomposeFields(pointVectorFields);
00668             fieldDecomposer.decomposeFields(pointSphericalTensorFields);
00669             fieldDecomposer.decomposeFields(pointSymmTensorFields);
00670             fieldDecomposer.decomposeFields(pointTensorFields);
00671         }
00672 
00673 
00674         // If there is lagrangian data write it out
00675         forAll(lagrangianPositions, cloudI)
00676         {
00677             if (lagrangianPositions[cloudI].size())
00678             {
00679                 lagrangianFieldDecomposer fieldDecomposer
00680                 (
00681                     mesh,
00682                     procMesh,
00683                     cellProcAddressing,
00684                     cloudDirs[cloudI],
00685                     lagrangianPositions[cloudI],
00686                     cellParticles[cloudI]
00687                 );
00688 
00689                 // Lagrangian fields
00690                 if
00691                 (
00692                     lagrangianLabelFields[cloudI].size()
00693                  || lagrangianScalarFields[cloudI].size()
00694                  || lagrangianVectorFields[cloudI].size()
00695                  || lagrangianSphericalTensorFields[cloudI].size()
00696                  || lagrangianSymmTensorFields[cloudI].size()
00697                  || lagrangianTensorFields[cloudI].size()
00698                 )
00699                 {
00700                     fieldDecomposer.decomposeFields
00701                     (
00702                         cloudDirs[cloudI],
00703                         lagrangianLabelFields[cloudI]
00704                     );
00705                     fieldDecomposer.decomposeFields
00706                     (
00707                         cloudDirs[cloudI],
00708                         lagrangianScalarFields[cloudI]
00709                     );
00710                     fieldDecomposer.decomposeFields
00711                     (
00712                         cloudDirs[cloudI],
00713                         lagrangianVectorFields[cloudI]
00714                     );
00715                     fieldDecomposer.decomposeFields
00716                     (
00717                         cloudDirs[cloudI],
00718                         lagrangianSphericalTensorFields[cloudI]
00719                     );
00720                     fieldDecomposer.decomposeFields
00721                     (
00722                         cloudDirs[cloudI],
00723                         lagrangianSymmTensorFields[cloudI]
00724                     );
00725                     fieldDecomposer.decomposeFields
00726                     (
00727                         cloudDirs[cloudI],
00728                         lagrangianTensorFields[cloudI]
00729                     );
00730                 }
00731             }
00732         }
00733 
00734 
00735         // Any non-decomposed data to copy?
00736         if (uniformDir.size())
00737         {
00738             const fileName timePath = processorDb.timePath();
00739 
00740             if (copyUniform || mesh.distributed())
00741             {
00742                 cp
00743                 (
00744                     runTime.timePath()/uniformDir,
00745                     timePath/uniformDir
00746                 );
00747             }
00748             else
00749             {
00750                 // link with relative paths
00751                 const string parentPath = string("..")/"..";
00752 
00753                 fileName currentDir(cwd());
00754                 chDir(timePath);
00755                 ln
00756                 (
00757                     parentPath/runTime.timeName()/uniformDir,
00758                     uniformDir
00759                 );
00760                 chDir(currentDir);
00761             }
00762         }
00763     }
00764 
00765 
00766     Info<< "\nEnd.\n" << endl;
00767 
00768     return 0;
00769 }
00770 
00771 
00772 // ************************************************************************* //
Copyright © 2000-2009 OpenCFD Ltd