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

stitchMesh.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     'Stitches' a mesh.
00027 
00028     Takes a mesh and two patches and merges the faces on the two patches
00029     (if geometrically possible) so the faces become internal.
00030 
00031     Can do
00032     - 'perfect' match: faces and points on patches align exactly. Order might
00033     be different though.
00034     - 'integral' match: where the surfaces on both patches exactly
00035     match but the individual faces not
00036     - 'partial' match: where the non-overlapping part of the surface remains
00037     in the respective patch.
00038 
00039     Note : Is just a front-end to perfectInterface/slidingInterface.
00040 
00041     Comparable to running a meshModifier of the form
00042     (if masterPatch is called "M" and slavePatch "S"):
00043 
00044     couple
00045     {
00046         type                    slidingInterface;
00047         masterFaceZoneName      MSMasterZone
00048         slaveFaceZoneName       MSSlaveZone
00049         cutPointZoneName        MSCutPointZone
00050         cutFaceZoneName         MSCutFaceZone
00051         masterPatchName         M;
00052         slavePatchName          S;
00053         typeOfMatch             partial or integral
00054     }
00055 
00056 
00057 \*---------------------------------------------------------------------------*/
00058 
00059 #include "fvCFD.H"
00060 #include "polyTopoChanger.H"
00061 #include "mapPolyMesh.H"
00062 #include "ListOps.H"
00063 #include "slidingInterface.H"
00064 #include "perfectInterface.H"
00065 #include "IOobjectList.H"
00066 #include "ReadFields.H"
00067 
00068 
00069 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
00070 
00071 // Checks whether patch present
00072 void checkPatch(const polyBoundaryMesh& bMesh, const word& name)
00073 {
00074     label patchI = bMesh.findPatchID(name);
00075 
00076     if (patchI == -1)
00077     {
00078         FatalErrorIn("checkPatch(const polyBoundaryMesh&, const word&)")
00079             << "Cannot find patch " << name << endl
00080             << "It should be present and of non-zero size" << endl
00081             << "Valid patches are " << bMesh.names()
00082             << exit(FatalError);
00083     }
00084 
00085     if (bMesh[patchI].empty())
00086     {
00087         FatalErrorIn("checkPatch(const polyBoundaryMesh&, const word&)")
00088             << "Patch " << name << " is present but zero size"
00089             << exit(FatalError);
00090     }
00091 }
00092 
00093 
00094 // Main program:
00095 
00096 int main(int argc, char *argv[])
00097 {
00098     Foam::argList::noParallel();
00099 
00100     Foam::argList::validArgs.append("masterPatch");
00101     Foam::argList::validArgs.append("slavePatch");
00102 
00103     Foam::argList::validOptions.insert("partial", "");
00104     Foam::argList::validOptions.insert("perfect", "");
00105 
00106     Foam::argList::validOptions.insert("overwrite", "");
00107 
00108 #   include "setRootCase.H"
00109 #   include "createTime.H"
00110     runTime.functionObjects().off();
00111 #   include "createMesh.H"
00112     const word oldInstance = mesh.pointsInstance();
00113 
00114 
00115     word masterPatchName(args.additionalArgs()[0]);
00116     word slavePatchName(args.additionalArgs()[1]);
00117 
00118     bool partialCover = args.optionFound("partial");
00119     bool perfectCover = args.optionFound("perfect");
00120     bool overwrite    = args.optionFound("overwrite");
00121 
00122     if (partialCover && perfectCover)
00123     {
00124         FatalErrorIn(args.executable())
00125             << "Cannot both supply partial and perfect." << endl
00126             << "Use perfect match option if the patches perfectly align"
00127             << " (both vertex positions and face centres)" << endl
00128             << exit(FatalError);
00129     }
00130 
00131 
00132     const word mergePatchName(masterPatchName + slavePatchName);
00133     const word cutZoneName(mergePatchName + "CutFaceZone");
00134 
00135     slidingInterface::typeOfMatch tom = slidingInterface::INTEGRAL;
00136 
00137     if (partialCover)
00138     {
00139         Info<< "Coupling partially overlapping patches "
00140             << masterPatchName << " and " << slavePatchName << nl
00141             << "Resulting internal faces will be in faceZone " << cutZoneName
00142             << nl
00143             << "Any uncovered faces will remain in their patch"
00144             << endl;
00145 
00146         tom = slidingInterface::PARTIAL;
00147     }
00148     else if (perfectCover)
00149     {
00150         Info<< "Coupling perfectly aligned patches "
00151             << masterPatchName << " and " << slavePatchName << nl
00152             << "Resulting (internal) faces will be in faceZone " << cutZoneName
00153             << nl << nl
00154             << "Note: both patches need to align perfectly." << nl
00155             << "Both the vertex"
00156             << " positions and the face centres need to align to within" << nl
00157             << "a tolerance given by the minimum edge length on the patch"
00158             << endl;
00159     }
00160     else
00161     {
00162         Info<< "Coupling patches " << masterPatchName << " and "
00163             << slavePatchName << nl
00164             << "Resulting (internal) faces will be in faceZone " << cutZoneName
00165             << nl << nl
00166             << "Note: the overall area covered by both patches should be"
00167             << " identical (\"integral\" interface)." << endl
00168             << "If this is not the case use the -partial option" << nl << endl;
00169     }
00170 
00171     // Check for non-empty master and slave patches
00172     checkPatch(mesh.boundaryMesh(), masterPatchName);
00173     checkPatch(mesh.boundaryMesh(), slavePatchName);
00174 
00175     // Create and add face zones and mesh modifiers
00176 
00177     // Master patch
00178     const polyPatch& masterPatch =
00179         mesh.boundaryMesh()
00180         [
00181             mesh.boundaryMesh().findPatchID(masterPatchName)
00182         ];
00183 
00184     // Make list of masterPatch faces
00185     labelList isf(masterPatch.size());
00186 
00187     forAll (isf, i)
00188     {
00189         isf[i] = masterPatch.start() + i;
00190     }
00191 
00192     polyTopoChanger stitcher(mesh);
00193     stitcher.setSize(1);
00194 
00195     DynamicList<pointZone*> pz;
00196     DynamicList<faceZone*> fz;
00197     DynamicList<cellZone*> cz;
00198 
00199     if (perfectCover)
00200     {
00201         // Add empty zone for resulting internal faces
00202         fz.append
00203         (
00204             new faceZone
00205             (
00206                 cutZoneName,
00207                 isf,
00208                 boolList(masterPatch.size(), false),
00209                 0,
00210                 mesh.faceZones()
00211             )
00212         );
00213 
00214         // Note: make sure to add the zones BEFORE constructing polyMeshModifier
00215         // (since looks up various zones at construction time)
00216         Info << "Adding point and face zones" << endl;
00217         mesh.addZones(pz.shrink(), fz.shrink(), cz.shrink());
00218 
00219         // Add the perfect interface mesh modifier
00220         stitcher.set
00221         (
00222             0,
00223             new perfectInterface
00224             (
00225                 "couple",
00226                 0,
00227                 stitcher,
00228                 cutZoneName,
00229                 masterPatchName,
00230                 slavePatchName
00231             )
00232         );
00233     }
00234     else
00235     {
00236         pz.append
00237         (
00238             new pointZone
00239             (
00240                 mergePatchName + "CutPointZone",
00241                 labelList(0),
00242                 0,
00243                 mesh.pointZones()
00244             )
00245         );
00246 
00247         fz.append
00248         (
00249             new faceZone
00250             (
00251                 mergePatchName + "MasterZone",
00252                 isf,
00253                 boolList(masterPatch.size(), false),
00254                 0,
00255                 mesh.faceZones()
00256             )
00257         );
00258 
00259         // Slave patch
00260         const polyPatch& slavePatch =
00261             mesh.boundaryMesh()
00262             [
00263                 mesh.boundaryMesh().findPatchID(slavePatchName)
00264             ];
00265 
00266         labelList osf(slavePatch.size());
00267 
00268         forAll (osf, i)
00269         {
00270             osf[i] = slavePatch.start() + i;
00271         }
00272 
00273         fz.append
00274         (
00275             new faceZone
00276             (
00277                 mergePatchName + "SlaveZone",
00278                 osf,
00279                 boolList(slavePatch.size(), false),
00280                 1,
00281                 mesh.faceZones()
00282             )
00283         );
00284 
00285         // Add empty zone for cut faces
00286         fz.append
00287         (
00288             new faceZone
00289             (
00290                 cutZoneName,
00291                 labelList(0),
00292                 boolList(0, false),
00293                 2,
00294                 mesh.faceZones()
00295             )
00296         );
00297 
00298 
00299         // Note: make sure to add the zones BEFORE constructing polyMeshModifier
00300         // (since looks up various zones at construction time)
00301         Info << "Adding point and face zones" << endl;
00302         mesh.addZones(pz.shrink(), fz.shrink(), cz.shrink());
00303 
00304         // Add the sliding interface mesh modifier
00305         stitcher.set
00306         (
00307             0,
00308             new slidingInterface
00309             (
00310                 "couple",
00311                 0,
00312                 stitcher,
00313                 mergePatchName + "MasterZone",
00314                 mergePatchName + "SlaveZone",
00315                 mergePatchName + "CutPointZone",
00316                 cutZoneName,
00317                 masterPatchName,
00318                 slavePatchName,
00319                 tom,                    // integral or partial
00320                 true                    // couple/decouple mode
00321             )
00322         );
00323     }
00324 
00325 
00326     // Search for list of objects for this time
00327     IOobjectList objects(mesh, runTime.timeName());
00328 
00329     // Read all current fvFields so they will get mapped
00330     Info<< "Reading all current volfields" << endl;
00331     PtrList<volScalarField> volScalarFields;
00332     ReadFields(mesh, objects, volScalarFields);
00333 
00334     PtrList<volVectorField> volVectorFields;
00335     ReadFields(mesh, objects, volVectorFields);
00336 
00337     PtrList<volSphericalTensorField> volSphericalTensorFields;
00338     ReadFields(mesh, objects, volSphericalTensorFields);
00339 
00340     PtrList<volSymmTensorField> volSymmTensorFields;
00341     ReadFields(mesh, objects, volSymmTensorFields);
00342 
00343     PtrList<volTensorField> volTensorFields;
00344     ReadFields(mesh, objects, volTensorFields);
00345 
00346     //- uncomment if you want to interpolate surface fields (usually bad idea)
00347     //Info<< "Reading all current surfaceFields" << endl;
00348     //PtrList<surfaceScalarField> surfaceScalarFields;
00349     //ReadFields(mesh, objects, surfaceScalarFields);
00350     //
00351     //PtrList<surfaceVectorField> surfaceVectorFields;
00352     //ReadFields(mesh, objects, surfaceVectorFields);
00353     //
00354     //PtrList<surfaceTensorField> surfaceTensorFields;
00355     //ReadFields(mesh, objects, surfaceTensorFields);
00356 
00357     if (!overwrite)
00358     {
00359         runTime++;
00360     }
00361 
00362     // Execute all polyMeshModifiers
00363     autoPtr<mapPolyMesh> morphMap = stitcher.changeMesh(true);
00364 
00365     mesh.movePoints(morphMap->preMotionPoints());
00366 
00367     // Write mesh
00368     if (overwrite)
00369     {
00370         mesh.setInstance(oldInstance);
00371         stitcher.instance() = oldInstance;
00372     }
00373     Info << nl << "Writing polyMesh to time " << runTime.timeName() << endl;
00374 
00375     IOstream::defaultPrecision(10);
00376 
00377     // Bypass runTime write (since only writes at outputTime)
00378     if
00379     (
00380        !runTime.objectRegistry::writeObject
00381         (
00382             runTime.writeFormat(),
00383             IOstream::currentVersion,
00384             runTime.writeCompression()
00385         )
00386     )
00387     {
00388         FatalErrorIn(args.executable())
00389             << "Failed writing polyMesh."
00390             << exit(FatalError);
00391     }
00392 
00393     mesh.faceZones().write();
00394     mesh.pointZones().write();
00395     mesh.cellZones().write();
00396 
00397     // Write fields
00398     runTime.write();
00399 
00400     Info<< nl << "end" << endl;
00401 
00402     return 0;
00403 }
00404 
00405 
00406 // ************************************************************************* //
Copyright © 2000-2009 OpenCFD Ltd