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

mergeOrSplitBaffles.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     mergeOrSplitBaffles
00027 
00028 Description
00029     Detects faces that share points (baffles). Either merge them or
00030     duplicate the points.
00031 
00032     Notes:
00033     - can only handle pairwise boundary faces. So three faces using
00034       the same points is not handled (is illegal mesh anyway)
00035 
00036     - there is no option to only split/merge some baffles.
00037 
00038     - surfaces consisting of duplicate faces can be topologically split
00039     if the points on the interior of the surface cannot walk to all the
00040     cells that use them in one go.
00041 
00042     - Parallel operation (where duplicate face is perpendicular to a coupled
00043     boundary) is supported but not really tested.
00044     (Note that coupled faces themselves are not seen as duplicate faces)
00045 
00046 \*---------------------------------------------------------------------------*/
00047 
00048 #include "argList.H"
00049 #include "Time.H"
00050 #include "syncTools.H"
00051 #include "faceSet.H"
00052 #include "pointSet.H"
00053 #include "meshTools.H"
00054 #include "polyTopoChange.H"
00055 #include "polyRemoveFace.H"
00056 #include "polyModifyFace.H"
00057 #include "indirectPrimitivePatch.H"
00058 #include "processorPolyPatch.H"
00059 #include "localPointRegion.H"
00060 #include "duplicatePoints.H"
00061 #include "ReadFields.H"
00062 #include "volFields.H"
00063 #include "surfaceFields.H"
00064 
00065 using namespace Foam;
00066 
00067 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
00068 
00069 void insertDuplicateMerge
00070 (
00071     const polyMesh& mesh,
00072     const labelList& duplicates,
00073     polyTopoChange& meshMod
00074 )
00075 {
00076     const faceList& faces = mesh.faces();
00077     const labelList& faceOwner = mesh.faceOwner();
00078     const faceZoneMesh& faceZones = mesh.faceZones();
00079 
00080     forAll(duplicates, bFaceI)
00081     {
00082         label otherFaceI = duplicates[bFaceI];
00083 
00084         if (otherFaceI != -1 && otherFaceI > bFaceI)
00085         {
00086             // Two duplicate faces. Merge.
00087 
00088             label face0 = mesh.nInternalFaces() + bFaceI;
00089             label face1 = mesh.nInternalFaces() + otherFaceI;
00090 
00091             label own0 = faceOwner[face0];
00092             label own1 = faceOwner[face1];
00093 
00094             if (own0 < own1)
00095             {
00096                 // Use face0 as the new internal face.
00097                 label zoneID = faceZones.whichZone(face0);
00098                 bool zoneFlip = false;
00099 
00100                 if (zoneID >= 0)
00101                 {
00102                     const faceZone& fZone = faceZones[zoneID];
00103                     zoneFlip = fZone.flipMap()[fZone.whichFace(face0)];
00104                 }
00105 
00106                 meshMod.setAction(polyRemoveFace(face1));
00107                 meshMod.setAction
00108                 (
00109                     polyModifyFace
00110                     (
00111                         faces[face0],           // modified face
00112                         face0,                  // label of face being modified
00113                         own0,                   // owner
00114                         own1,                   // neighbour
00115                         false,                  // face flip
00116                         -1,                     // patch for face
00117                         false,                  // remove from zone
00118                         zoneID,                 // zone for face
00119                         zoneFlip                // face flip in zone
00120                     )
00121                 );
00122             }
00123             else
00124             {
00125                 // Use face1 as the new internal face.
00126                 label zoneID = faceZones.whichZone(face1);
00127                 bool zoneFlip = false;
00128 
00129                 if (zoneID >= 0)
00130                 {
00131                     const faceZone& fZone = faceZones[zoneID];
00132                     zoneFlip = fZone.flipMap()[fZone.whichFace(face1)];
00133                 }
00134 
00135                 meshMod.setAction(polyRemoveFace(face0));
00136                 meshMod.setAction
00137                 (
00138                     polyModifyFace
00139                     (
00140                         faces[face1],           // modified face
00141                         face1,                  // label of face being modified
00142                         own1,                   // owner
00143                         own0,                   // neighbour
00144                         false,                  // face flip
00145                         -1,                     // patch for face
00146                         false,                  // remove from zone
00147                         zoneID,                 // zone for face
00148                         zoneFlip                // face flip in zone
00149                     )
00150                 );
00151             }
00152         }
00153     }
00154 }
00155 
00156 
00157 labelList findBaffles(const polyMesh& mesh, const labelList& boundaryFaces)
00158 {
00159     // Get all duplicate face labels (in boundaryFaces indices!).
00160     labelList duplicates = localPointRegion::findDuplicateFaces
00161     (
00162         mesh,
00163         boundaryFaces
00164     );
00165 
00166 
00167     // Check that none are on processor patches
00168     const polyBoundaryMesh& patches = mesh.boundaryMesh();
00169 
00170     forAll(duplicates, bFaceI)
00171     {
00172         if (duplicates[bFaceI] != -1)
00173         {
00174             label faceI = mesh.nInternalFaces() + bFaceI;
00175             label patchI = patches.whichPatch(faceI);
00176 
00177             if (isA<processorPolyPatch>(patches[patchI]))
00178             {
00179                 FatalErrorIn("findBaffles(const polyMesh&, const labelList&)")
00180                     << "Duplicate face " << faceI
00181                     << " is on a processorPolyPatch."
00182                     << "This is not allowed." << nl
00183                     << "Face:" << faceI
00184                     << " is on patch:" << patches[patchI].name()
00185                     << abort(FatalError);
00186             }
00187         }
00188     }
00189 
00190 
00191     // Write to faceSet for ease of postprocessing.
00192     {
00193         faceSet duplicateSet
00194         (
00195             mesh,
00196             "duplicateFaces",
00197             (mesh.nFaces() - mesh.nInternalFaces())/256
00198         );
00199 
00200         forAll(duplicates, bFaceI)
00201         {
00202             label otherFaceI = duplicates[bFaceI];
00203 
00204             if (otherFaceI != -1 && otherFaceI > bFaceI)
00205             {
00206                 duplicateSet.insert(mesh.nInternalFaces() + bFaceI);
00207                 duplicateSet.insert(mesh.nInternalFaces() + otherFaceI);
00208             }
00209         }
00210 
00211         Pout<< "Writing " << duplicateSet.size()
00212             << " duplicate faces to faceSet " << duplicateSet.objectPath()
00213             << nl << endl;
00214         duplicateSet.write();
00215     }
00216 
00217     return duplicates;
00218 }
00219 
00220 
00221 
00222 
00223 int main(int argc, char *argv[])
00224 {
00225     argList::validOptions.insert("split", "");
00226     argList::validOptions.insert("overwrite", "");
00227     argList::validOptions.insert("detectOnly", "");
00228 #   include "setRootCase.H"
00229 #   include "createTime.H"
00230     runTime.functionObjects().off();
00231 #   include "createMesh.H"
00232     const word oldInstance = mesh.pointsInstance();
00233 
00234     bool split      = args.optionFound("split");
00235     bool overwrite  = args.optionFound("overwrite");
00236     bool detectOnly = args.optionFound("detectOnly");
00237 
00238     // Collect all boundary faces
00239     labelList boundaryFaces(mesh.nFaces() - mesh.nInternalFaces());
00240 
00241     forAll(boundaryFaces, i)
00242     {
00243         boundaryFaces[i] = i+mesh.nInternalFaces();
00244     }
00245 
00246 
00247     if (detectOnly)
00248     {
00249         findBaffles(mesh, boundaryFaces);
00250 
00251         return 0;
00252     }
00253 
00254 
00255 
00256     // Read objects in time directory
00257     IOobjectList objects(mesh, runTime.timeName());
00258 
00259     // Read vol fields.
00260 
00261     PtrList<volScalarField> vsFlds;
00262     ReadFields(mesh, objects, vsFlds);
00263 
00264     PtrList<volVectorField> vvFlds;
00265     ReadFields(mesh, objects, vvFlds);
00266 
00267     PtrList<volSphericalTensorField> vstFlds;
00268     ReadFields(mesh, objects, vstFlds);
00269 
00270     PtrList<volSymmTensorField> vsymtFlds;
00271     ReadFields(mesh, objects, vsymtFlds);
00272 
00273     PtrList<volTensorField> vtFlds;
00274     ReadFields(mesh, objects, vtFlds);
00275 
00276     // Read surface fields.
00277 
00278     PtrList<surfaceScalarField> ssFlds;
00279     ReadFields(mesh, objects, ssFlds);
00280 
00281     PtrList<surfaceVectorField> svFlds;
00282     ReadFields(mesh, objects, svFlds);
00283 
00284     PtrList<surfaceSphericalTensorField> sstFlds;
00285     ReadFields(mesh, objects, sstFlds);
00286 
00287     PtrList<surfaceSymmTensorField> ssymtFlds;
00288     ReadFields(mesh, objects, ssymtFlds);
00289 
00290     PtrList<surfaceTensorField> stFlds;
00291     ReadFields(mesh, objects, stFlds);
00292 
00293 
00294     // Mesh change engine
00295     polyTopoChange meshMod(mesh);
00296 
00297 
00298     if (split)
00299     {
00300         Pout<< "Topologically splitting duplicate surfaces"
00301             << ", i.e. duplicating points internal to duplicate surfaces."
00302             << nl << endl;
00303 
00304         // Analyse which points need to be duplicated
00305         localPointRegion regionSide(mesh);
00306 
00307         // Point duplication engine
00308         duplicatePoints pointDuplicator(mesh);
00309 
00310         // Insert topo changes
00311         pointDuplicator.setRefinement(regionSide, meshMod);
00312     }
00313     else
00314     {
00315         Pout<< "Merging duplicate faces."
00316             << nl << endl;
00317 
00318         // Get all duplicate face labels (in boundaryFaces indices!).
00319         labelList duplicates(findBaffles(mesh, boundaryFaces));
00320 
00321         // Merge into internal faces.
00322         insertDuplicateMerge(mesh, duplicates, meshMod);
00323     }
00324 
00325     if (!overwrite)
00326     {
00327         runTime++;
00328     }
00329 
00330     // Change the mesh. No inflation.
00331     autoPtr<mapPolyMesh> map = meshMod.changeMesh(mesh, false);
00332 
00333     // Update fields
00334     mesh.updateMesh(map);
00335 
00336     // Move mesh (since morphing does not do this)
00337     if (map().hasMotionPoints())
00338     {
00339         mesh.movePoints(map().preMotionPoints());
00340     }
00341 
00342     if (overwrite)
00343     {
00344         mesh.setInstance(oldInstance);
00345     }
00346     Pout<< "Writing mesh to time " << runTime.timeName() << endl;
00347     mesh.write();
00348 
00349     // Dump duplicated points (if any)
00350     if (split)
00351     {
00352         const labelList& pointMap = map().pointMap();
00353 
00354         labelList nDupPerPoint(map().nOldPoints(), 0);
00355 
00356         pointSet dupPoints(mesh, "duplicatedPoints", 100);
00357 
00358         forAll(pointMap, pointI)
00359         {
00360             label oldPointI = pointMap[pointI];
00361 
00362             nDupPerPoint[oldPointI]++;
00363 
00364             if (nDupPerPoint[oldPointI] > 1)
00365             {
00366                 dupPoints.insert(map().reversePointMap()[oldPointI]);
00367                 dupPoints.insert(pointI);
00368             }
00369         }
00370 
00371         Pout<< "Writing " << dupPoints.size()
00372             << " duplicated points to pointSet "
00373             << dupPoints.objectPath() << nl << endl;
00374 
00375         dupPoints.write();
00376     }
00377 
00378     Info<< "End\n" << endl;
00379 
00380     return 0;
00381 }
00382 
00383 
00384 // ************************************************************************* //
Copyright © 2000-2009 OpenCFD Ltd