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

splitMesh.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     Splits mesh by making internal faces external. Uses attachDetach.
00027 
00028     Generates a meshModifier of the form:
00029 
00030     Splitter
00031     {
00032         type                       attachDetach;
00033         faceZoneName               membraneFaces;
00034         masterPatchName            masterPatch;
00035         slavePatchName             slavePatch;
00036         triggerTimes               runTime.value();
00037     }
00038 
00039     so will detach at the current time and split all faces in membraneFaces
00040     into masterPatch and slavePatch (which have to be present but of 0 size)
00041 
00042 \*---------------------------------------------------------------------------*/
00043 
00044 #include "argList.H"
00045 #include "polyMesh.H"
00046 #include "Time.H"
00047 #include "polyTopoChange.H"
00048 #include "mapPolyMesh.H"
00049 #include "faceSet.H"
00050 #include "attachDetach.H"
00051 #include "attachPolyTopoChanger.H"
00052 #include "regionSide.H"
00053 #include "primitiveFacePatch.H"
00054 
00055 using namespace Foam;
00056 
00057 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
00058 
00059 // Find edge between points v0 and v1.
00060 label findEdge(const primitiveMesh& mesh, const label v0, const label v1)
00061 {
00062     const labelList& pEdges = mesh.pointEdges()[v0];
00063 
00064     forAll(pEdges, pEdgeI)
00065     {
00066         label edgeI = pEdges[pEdgeI];
00067 
00068         const edge& e = mesh.edges()[edgeI];
00069 
00070         if (e.otherVertex(v0) == v1)
00071         {
00072             return edgeI;
00073         }
00074     }
00075 
00076     FatalErrorIn
00077     (
00078         "findEdge(const primitiveMesh&, const label, const label)"
00079     )   << "Cannot find edge between mesh points " << v0 << " and " << v1
00080         << abort(FatalError);
00081 
00082     return -1;
00083 }
00084 
00085 
00086 // Checks whether patch present
00087 void checkPatch(const polyBoundaryMesh& bMesh, const word& name)
00088 {
00089     label patchI = bMesh.findPatchID(name);
00090 
00091     if (patchI == -1)
00092     {
00093         FatalErrorIn("checkPatch(const polyBoundaryMesh&, const word&)")
00094             << "Cannot find patch " << name << endl
00095             << "It should be present but of zero size" << endl
00096             << "Valid patches are " << bMesh.names()
00097             << exit(FatalError);
00098     }
00099 
00100     if (bMesh[patchI].size())
00101     {
00102         FatalErrorIn("checkPatch(const polyBoundaryMesh&, const word&)")
00103             << "Patch " << name << " is present but non-zero size"
00104             << exit(FatalError);
00105     }
00106 }
00107 
00108 
00109 // Main program:
00110 
00111 int main(int argc, char *argv[])
00112 {
00113     Foam::argList::noParallel();
00114 
00115     Foam::argList::validArgs.append("faceSet");
00116     Foam::argList::validArgs.append("masterPatch");
00117     Foam::argList::validArgs.append("slavePatch");
00118     Foam::argList::validOptions.insert("overwrite", "");
00119 
00120 #   include "setRootCase.H"
00121 #   include "createTime.H"
00122     runTime.functionObjects().off();
00123 #   include "createPolyMesh.H"
00124     const word oldInstance = mesh.pointsInstance();
00125 
00126     word setName(args.additionalArgs()[0]);
00127     word masterPatch(args.additionalArgs()[1]);
00128     word slavePatch(args.additionalArgs()[2]);
00129     bool overwrite = args.optionFound("overwrite");
00130 
00131     // List of faces to split
00132     faceSet facesSet(mesh, setName);
00133 
00134     Info<< "Read " << facesSet.size() << " faces to split" << endl << endl;
00135 
00136 
00137     // Convert into labelList and check
00138 
00139     labelList faces(facesSet.toc());
00140 
00141     forAll(faces, i)
00142     {
00143         if (!mesh.isInternalFace(faces[i]))
00144         {
00145             FatalErrorIn(args.executable())
00146             << "Face " << faces[i] << " in faceSet " << setName
00147             << " is not an internal face."
00148             << exit(FatalError);
00149         }
00150     }
00151 
00152 
00153     // Check for empty master and slave patches
00154     checkPatch(mesh.boundaryMesh(), masterPatch);
00155     checkPatch(mesh.boundaryMesh(), slavePatch);
00156 
00157 
00158     //
00159     // Find 'side' of all faces on splitregion. Uses regionSide which needs
00160     // set of edges on side of this region. Use PrimitivePatch to find these.
00161     //
00162 
00163     // Addressing on faces only in mesh vertices.
00164     primitiveFacePatch fPatch
00165     (
00166         UIndirectList<face>
00167         (
00168             mesh.faces(),
00169             faces
00170         ),
00171         mesh.points()
00172     );
00173 
00174     const labelList& meshPoints = fPatch.meshPoints();
00175 
00176     // Mark all fence edges : edges on boundary of fPatch but not on boundary
00177     // of polyMesh
00178     labelHashSet fenceEdges(fPatch.size());
00179 
00180     const labelListList& allEdgeFaces = fPatch.edgeFaces();
00181 
00182     forAll(allEdgeFaces, patchEdgeI)
00183     {
00184         if (allEdgeFaces[patchEdgeI].size() == 1)
00185         {
00186             const edge& e = fPatch.edges()[patchEdgeI];
00187 
00188             label edgeI =
00189                 findEdge
00190                 (
00191                     mesh,
00192                     meshPoints[e.start()],
00193                     meshPoints[e.end()]
00194                 );
00195 
00196             fenceEdges.insert(edgeI);
00197         }
00198     }
00199 
00200     // Find sides reachable from 0th face of faceSet
00201     label startFaceI = faces[0];
00202 
00203     regionSide regionInfo
00204     (
00205         mesh,
00206         facesSet,
00207         fenceEdges,
00208         mesh.faceOwner()[startFaceI],
00209         startFaceI
00210     );
00211 
00212     // Determine flip state for all faces in faceSet
00213     boolList zoneFlip(faces.size());
00214 
00215     forAll(faces, i)
00216     {
00217         zoneFlip[i] = !regionInfo.sideOwner().found(faces[i]);
00218     }
00219 
00220 
00221     // Create and add face zones and mesh modifiers
00222     List<pointZone*> pz(0);
00223     List<faceZone*> fz(1);
00224     List<cellZone*> cz(0);
00225 
00226     fz[0] =
00227         new faceZone
00228         (
00229             "membraneFaces",
00230             faces,
00231             zoneFlip,
00232             0,
00233             mesh.faceZones()
00234         );
00235 
00236     Info << "Adding point and face zones" << endl;
00237     mesh.addZones(pz, fz, cz);
00238 
00239     attachPolyTopoChanger splitter(mesh);
00240     splitter.setSize(1);
00241 
00242     // Add the sliding interface mesh modifier to start working at current
00243     // time
00244     splitter.set
00245     (
00246         0,
00247         new attachDetach
00248         (
00249             "Splitter",
00250             0,
00251             splitter,
00252             "membraneFaces",
00253             masterPatch,
00254             slavePatch,
00255             scalarField(1, runTime.value())
00256         )
00257     );
00258 
00259     Info<< nl << "Constructed topologyModifier:" << endl;
00260     splitter[0].writeDict(Info);
00261 
00262     if (!overwrite)
00263     {
00264         runTime++;
00265     }
00266 
00267     splitter.attach();
00268 
00269     if (overwrite)
00270     {
00271         mesh.setInstance(oldInstance);
00272     }
00273 
00274     Info<< "Writing mesh to " << runTime.timeName() << endl;
00275     if (!mesh.write())
00276     {
00277         FatalErrorIn(args.executable())
00278             << "Failed writing polyMesh."
00279             << exit(FatalError);
00280     }
00281 
00282     Info<< nl << "end" << endl;
00283     return 0;
00284 }
00285 
00286 
00287 // ************************************************************************* //
Copyright © 2000-2009 OpenCFD Ltd