1 /*---------------------------------------------------------------------------*\
3 \\ / F ield | foam-extend: Open Source CFD
4 \\ / O peration | Version: 3.2
5 \\ / A nd | Web: http://www.foam-extend.org
6 \\/ M anipulation | For copyright notice see file Copyright
7 -------------------------------------------------------------------------------
9 This file is part of foam-extend.
11 foam-extend is free software: you can redistribute it and/or modify it
12 under the terms of the GNU General Public License as published by the
13 Free Software Foundation, either version 3 of the License, or (at your
14 option) any later version.
16 foam-extend is distributed in the hope that it will be useful, but
17 WITHOUT ANY WARRANTY; without even the implied warranty of
18 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19 General Public License for more details.
21 You should have received a copy of the GNU General Public License
22 along with foam-extend. If not, see <http://www.gnu.org/licenses/>.
25 Reads surface and applies surface regioning to a mesh. Uses boundaryMesh
28 \*---------------------------------------------------------------------------*/
31 #include "objectRegistry.H"
33 #include "boundaryMesh.H"
36 #include "directTopoChange.H"
37 #include "polyModifyFace.H"
38 #include "globalMeshData.H"
42 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
44 // Adds empty patch if not yet there. Returns patchID.
45 label addPatch(polyMesh& mesh, const word& patchName)
47 label patchI = mesh.boundaryMesh().findPatchID(patchName);
51 const polyBoundaryMesh& patches = mesh.boundaryMesh();
53 List<polyPatch*> newPatches(patches.size() + 1);
57 // Copy all old patches
60 const polyPatch& pp = patches[i];
74 // Add zero-sized patch
85 mesh.removeBoundary();
86 mesh.addPatches(newPatches);
88 Pout<< "Created patch " << patchName << " at " << patchI << endl;
92 Pout<< "Reusing patch " << patchName << " at " << patchI << endl;
99 // Repatch single face. Return true if patch changed.
102 const polyMesh& mesh,
103 const boundaryMesh& bMesh,
104 const labelList& nearest,
105 const labelList& surfToMeshPatch,
107 directTopoChange& meshMod
110 bool changed = false;
112 label bFaceI = faceI - mesh.nInternalFaces();
114 if (nearest[bFaceI] != -1)
116 // Use boundary mesh one.
117 label bMeshPatchID = bMesh.whichPatch(nearest[bFaceI]);
119 label patchID = surfToMeshPatch[bMeshPatchID];
121 if (patchID != mesh.boundaryMesh().whichPatch(faceI))
123 label own = mesh.faceOwner()[faceI];
125 label zoneID = mesh.faceZones().whichZone(faceI);
127 bool zoneFlip = false;
131 const faceZone& fZone = mesh.faceZones()[zoneID];
133 zoneFlip = fZone.flipMap()[fZone.whichFace(faceI)];
140 mesh.faces()[faceI],// modified face
141 faceI, // label of face being modified
145 patchID, // patch for face
146 false, // remove from zone
147 zoneID, // zone for face
148 zoneFlip // face flip in zone
165 int main(int argc, char *argv[])
167 argList::noParallel();
168 argList::validArgs.append("surface file");
169 argList::validOptions.insert("faceSet", "faceSet name");
170 argList::validOptions.insert("tol", "fraction of mesh size");
172 # include "setRootCase.H"
173 # include "createTime.H"
174 # include "createPolyMesh.H"
176 fileName surfName(args.additionalArgs()[0]);
178 Info<< "Reading surface from " << surfName << " ..." << endl;
180 bool readSet = args.optionFound("faceSet");
185 setName = args.option("faceSet");
187 Info<< "Repatching only the faces in faceSet " << setName
188 << " according to nearest surface triangle ..." << endl;
192 Info<< "Patching all boundary faces according to nearest surface"
193 << " triangle ..." << endl;
196 scalar searchTol = 1e-3;
197 args.optionReadIfPresent("tol", searchTol);
199 // Get search box. Anything not within this box will not be considered.
200 const boundBox& meshBb = mesh.globalData().bb();
201 const vector searchSpan = searchTol*meshBb.span();
203 Info<< "All boundary faces further away than " << searchTol
204 << " of mesh bounding box " << meshBb
205 << " will keep their patch label ..." << endl;
208 Info<< "Before patching:" << nl
209 << " patch\tsize" << endl;
211 forAll(mesh.boundaryMesh(), patchI)
213 Info<< " " << mesh.boundaryMesh()[patchI].name() << '\t'
214 << mesh.boundaryMesh()[patchI].size() << endl;
222 // Load in the surface.
223 bMesh.readTriSurface(surfName);
225 // Add all the boundaryMesh patches to the mesh.
226 const PtrList<boundaryPatch>& bPatches = bMesh.patches();
228 // Map from surface patch ( = boundaryMesh patch) to polyMesh patch
229 labelList patchMap(bPatches.size());
233 patchMap[i] = addPatch(mesh, bPatches[i].name());
236 // Obtain nearest face in bMesh for each boundary face in mesh that
237 // is within search span.
238 // Note: should only determine for faceSet if working with that.
239 labelList nearest(bMesh.getNearest(mesh, searchSpan));
242 // Dump unmatched faces to faceSet for debugging.
243 faceSet unmatchedFaces(mesh, "unmatchedFaces", nearest.size()/100);
245 forAll(nearest, bFaceI)
247 if (nearest[bFaceI] == -1)
249 unmatchedFaces.insert(mesh.nInternalFaces() + bFaceI);
253 Pout<< "Writing all " << unmatchedFaces.size()
254 << " unmatched faces to faceSet "
255 << unmatchedFaces.name()
258 unmatchedFaces.write();
262 directTopoChange meshMod(mesh);
268 faceSet faceLabels(mesh, setName);
269 Info<< "Read " << faceLabels.size() << " faces to repatch ..." << endl;
271 forAllConstIter(faceSet, faceLabels, iter)
273 label faceI = iter.key();
275 if (repatchFace(mesh, bMesh, nearest, patchMap, faceI, meshMod))
283 forAll(nearest, bFaceI)
285 label faceI = mesh.nInternalFaces() + bFaceI;
287 if (repatchFace(mesh, bMesh, nearest, patchMap, faceI, meshMod))
294 Pout<< "Changed " << nChanged << " boundary faces." << nl << endl;
298 meshMod.changeMesh(mesh, false);
300 Info<< "After patching:" << nl
301 << " patch\tsize" << endl;
303 forAll(mesh.boundaryMesh(), patchI)
305 Info<< " " << mesh.boundaryMesh()[patchI].name() << '\t'
306 << mesh.boundaryMesh()[patchI].size() << endl;
313 // Write resulting mesh
314 Info << "Writing modified mesh to time " << runTime.value() << endl;
319 Info<< "End\n" << endl;
325 // ************************************************************************* //