00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031 #include "argList.H"
00032 #include "Time.H"
00033 #include "boundaryMesh.H"
00034 #include "polyMesh.H"
00035 #include "faceSet.H"
00036 #include "polyTopoChange.H"
00037 #include "polyModifyFace.H"
00038 #include "globalMeshData.H"
00039
00040 using namespace Foam;
00041
00042
00043
00044
00045 label addPatch(polyMesh& mesh, const word& patchName)
00046 {
00047 label patchI = mesh.boundaryMesh().findPatchID(patchName);
00048
00049 if (patchI == -1)
00050 {
00051 const polyBoundaryMesh& patches = mesh.boundaryMesh();
00052
00053 List<polyPatch*> newPatches(patches.size() + 1);
00054
00055 patchI = 0;
00056
00057
00058 forAll(patches, i)
00059 {
00060 const polyPatch& pp = patches[i];
00061
00062 newPatches[patchI] =
00063 pp.clone
00064 (
00065 patches,
00066 patchI,
00067 pp.size(),
00068 pp.start()
00069 ).ptr();
00070
00071 patchI++;
00072 }
00073
00074
00075 newPatches[patchI] =
00076 new polyPatch
00077 (
00078 patchName,
00079 0,
00080 mesh.nFaces(),
00081 patchI,
00082 patches
00083 );
00084
00085 mesh.removeBoundary();
00086 mesh.addPatches(newPatches);
00087
00088 Pout<< "Created patch " << patchName << " at " << patchI << endl;
00089 }
00090 else
00091 {
00092 Pout<< "Reusing patch " << patchName << " at " << patchI << endl;
00093 }
00094
00095 return patchI;
00096 }
00097
00098
00099
00100 bool repatchFace
00101 (
00102 const polyMesh& mesh,
00103 const boundaryMesh& bMesh,
00104 const labelList& nearest,
00105 const labelList& surfToMeshPatch,
00106 const label faceI,
00107 polyTopoChange& meshMod
00108 )
00109 {
00110 bool changed = false;
00111
00112 label bFaceI = faceI - mesh.nInternalFaces();
00113
00114 if (nearest[bFaceI] != -1)
00115 {
00116
00117 label bMeshPatchID = bMesh.whichPatch(nearest[bFaceI]);
00118
00119 label patchID = surfToMeshPatch[bMeshPatchID];
00120
00121 if (patchID != mesh.boundaryMesh().whichPatch(faceI))
00122 {
00123 label own = mesh.faceOwner()[faceI];
00124
00125 label zoneID = mesh.faceZones().whichZone(faceI);
00126
00127 bool zoneFlip = false;
00128
00129 if (zoneID >= 0)
00130 {
00131 const faceZone& fZone = mesh.faceZones()[zoneID];
00132
00133 zoneFlip = fZone.flipMap()[fZone.whichFace(faceI)];
00134 }
00135
00136 meshMod.setAction
00137 (
00138 polyModifyFace
00139 (
00140 mesh.faces()[faceI],
00141 faceI,
00142 own,
00143 -1,
00144 false,
00145 patchID,
00146 false,
00147 zoneID,
00148 zoneFlip
00149 )
00150 );
00151
00152 changed = true;
00153 }
00154 }
00155 else
00156 {
00157 changed = false;
00158 }
00159 return changed;
00160 }
00161
00162
00163
00164
00165 int main(int argc, char *argv[])
00166 {
00167 argList::noParallel();
00168 argList::validArgs.append("surface file");
00169 argList::validOptions.insert("faceSet", "faceSet name");
00170 argList::validOptions.insert("tol", "fraction of mesh size");
00171
00172 # include "setRootCase.H"
00173 # include "createTime.H"
00174 # include "createPolyMesh.H"
00175
00176 fileName surfName(args.additionalArgs()[0]);
00177
00178 Info<< "Reading surface from " << surfName << " ..." << endl;
00179
00180 bool readSet = args.optionFound("faceSet");
00181 word setName;
00182
00183 if (readSet)
00184 {
00185 setName = args.option("faceSet");
00186
00187 Info<< "Repatching only the faces in faceSet " << setName
00188 << " according to nearest surface triangle ..." << endl;
00189 }
00190 else
00191 {
00192 Info<< "Patching all boundary faces according to nearest surface"
00193 << " triangle ..." << endl;
00194 }
00195
00196 scalar searchTol = 1E-3;
00197 args.optionReadIfPresent("tol", searchTol);
00198
00199
00200 const boundBox& meshBb = mesh.globalData().bb();
00201 const vector searchSpan = searchTol * meshBb.span();
00202
00203 Info<< "All boundary faces further away than " << searchTol
00204 << " of mesh bounding box " << meshBb
00205 << " will keep their patch label ..." << endl;
00206
00207
00208 Info<< "Before patching:" << nl
00209 << " patch\tsize" << endl;
00210
00211 forAll(mesh.boundaryMesh(), patchI)
00212 {
00213 Info<< " " << mesh.boundaryMesh()[patchI].name() << '\t'
00214 << mesh.boundaryMesh()[patchI].size() << endl;
00215 }
00216 Info<< endl;
00217
00218
00219
00220 boundaryMesh bMesh;
00221
00222
00223 bMesh.readTriSurface(surfName);
00224
00225
00226 const PtrList<boundaryPatch>& bPatches = bMesh.patches();
00227
00228
00229 labelList patchMap(bPatches.size());
00230
00231 forAll(bPatches, i)
00232 {
00233 patchMap[i] = addPatch(mesh, bPatches[i].name());
00234 }
00235
00236
00237
00238
00239 labelList nearest(bMesh.getNearest(mesh, searchSpan));
00240
00241 {
00242
00243 faceSet unmatchedFaces(mesh, "unmatchedFaces", nearest.size()/100);
00244
00245 forAll(nearest, bFaceI)
00246 {
00247 if (nearest[bFaceI] == -1)
00248 {
00249 unmatchedFaces.insert(mesh.nInternalFaces() + bFaceI);
00250 }
00251 }
00252
00253 Pout<< "Writing all " << unmatchedFaces.size()
00254 << " unmatched faces to faceSet "
00255 << unmatchedFaces.name()
00256 << endl;
00257
00258 unmatchedFaces.write();
00259 }
00260
00261
00262 polyTopoChange meshMod(mesh);
00263
00264 label nChanged = 0;
00265
00266 if (readSet)
00267 {
00268 faceSet faceLabels(mesh, setName);
00269 Info<< "Read " << faceLabels.size() << " faces to repatch ..." << endl;
00270
00271 forAllConstIter(faceSet, faceLabels, iter)
00272 {
00273 label faceI = iter.key();
00274
00275 if (repatchFace(mesh, bMesh, nearest, patchMap, faceI, meshMod))
00276 {
00277 nChanged++;
00278 }
00279 }
00280 }
00281 else
00282 {
00283 forAll(nearest, bFaceI)
00284 {
00285 label faceI = mesh.nInternalFaces() + bFaceI;
00286
00287 if (repatchFace(mesh, bMesh, nearest, patchMap, faceI, meshMod))
00288 {
00289 nChanged++;
00290 }
00291 }
00292 }
00293
00294 Pout<< "Changed " << nChanged << " boundary faces." << nl << endl;
00295
00296 if (nChanged > 0)
00297 {
00298 meshMod.changeMesh(mesh, false);
00299
00300 Info<< "After patching:" << nl
00301 << " patch\tsize" << endl;
00302
00303 forAll(mesh.boundaryMesh(), patchI)
00304 {
00305 Info<< " " << mesh.boundaryMesh()[patchI].name() << '\t'
00306 << mesh.boundaryMesh()[patchI].size() << endl;
00307 }
00308 Info<< endl;
00309
00310
00311 runTime++;
00312
00313
00314 Info << "Writing modified mesh to time " << runTime.value() << endl;
00315 mesh.write();
00316 }
00317
00318
00319 Info<< "End\n" << endl;
00320
00321 return 0;
00322 }
00323
00324
00325