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
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041
00042
00043
00044
00045
00046 #include "PstreamReduceOps.H"
00047 #include "argList.H"
00048 #include "Time.H"
00049 #include "polyTopoChange.H"
00050 #include "polyModifyFace.H"
00051 #include "polyAddFace.H"
00052 #include "combineFaces.H"
00053 #include "removePoints.H"
00054 #include "polyMesh.H"
00055 #include "mapPolyMesh.H"
00056 #include "mathematicalConstants.H"
00057
00058 using namespace Foam;
00059
00060
00061
00062
00063
00064 const scalar defaultConcaveAngle = 30;
00065
00066
00067
00068 void checkSnapMesh
00069 (
00070 const Time& runTime,
00071 const polyMesh& mesh,
00072 labelHashSet& wrongFaces
00073 )
00074 {
00075 IOdictionary snapDict
00076 (
00077 IOobject
00078 (
00079 "snapMeshDict",
00080 runTime.system(),
00081 mesh,
00082 IOobject::MUST_READ,
00083 IOobject::NO_WRITE
00084 )
00085 );
00086
00087
00088 scalar maxNonOrtho(readScalar(snapDict.lookup("maxNonOrtho")));
00089
00090 scalar maxConcave(readScalar(snapDict.lookup("maxConcave")));
00091
00092 scalar relMinVol(readScalar(snapDict.lookup("minVol")));
00093 const scalar minCellVol = min(mesh.cellVolumes());
00094 const scalar minPyrVol = relMinVol*minCellVol;
00095
00096 scalar minArea(readScalar(snapDict.lookup("minArea")));
00097
00098 if (maxNonOrtho < 180.0-SMALL)
00099 {
00100 Pout<< "Checking non orthogonality" << endl;
00101
00102 label nOldSize = wrongFaces.size();
00103 mesh.setNonOrthThreshold(maxNonOrtho);
00104 mesh.checkFaceOrthogonality(false, &wrongFaces);
00105
00106 Pout<< "Detected " << wrongFaces.size() - nOldSize
00107 << " faces with non-orthogonality > " << maxNonOrtho << " degrees"
00108 << endl;
00109 }
00110
00111 if (minPyrVol > -GREAT)
00112 {
00113 Pout<< "Checking face pyramids" << endl;
00114
00115 label nOldSize = wrongFaces.size();
00116 mesh.checkFacePyramids(false, minPyrVol, &wrongFaces);
00117 Pout<< "Detected additional " << wrongFaces.size() - nOldSize
00118 << " faces with illegal face pyramids" << endl;
00119 }
00120
00121 if (maxConcave < 180.0-SMALL)
00122 {
00123 Pout<< "Checking face angles" << endl;
00124
00125 label nOldSize = wrongFaces.size();
00126 mesh.checkFaceAngles(false, maxConcave, &wrongFaces);
00127 Pout<< "Detected additional " << wrongFaces.size() - nOldSize
00128 << " faces with concavity > " << maxConcave << " degrees"
00129 << endl;
00130 }
00131
00132 if (minArea > -SMALL)
00133 {
00134 Pout<< "Checking face areas" << endl;
00135
00136 label nOldSize = wrongFaces.size();
00137
00138 const scalarField magFaceAreas = mag(mesh.faceAreas());
00139
00140 forAll(magFaceAreas, faceI)
00141 {
00142 if (magFaceAreas[faceI] < minArea)
00143 {
00144 wrongFaces.insert(faceI);
00145 }
00146 }
00147 Pout<< "Detected additional " << wrongFaces.size() - nOldSize
00148 << " faces with area < " << minArea << " m^2" << endl;
00149 }
00150 }
00151
00152
00153
00154
00155 label mergePatchFaces
00156 (
00157 const scalar minCos,
00158 const scalar concaveSin,
00159 const bool snapMeshDict,
00160 const Time& runTime,
00161 polyMesh& mesh
00162 )
00163 {
00164
00165 combineFaces faceCombiner(mesh);
00166
00167
00168 labelListList allFaceSets(faceCombiner.getMergeSets(minCos, concaveSin));
00169
00170 label nFaceSets = returnReduce(allFaceSets.size(), sumOp<label>());
00171
00172 Info<< "Merging " << nFaceSets << " sets of faces." << endl;
00173
00174 if (nFaceSets > 0)
00175 {
00176
00177 List<faceList> allFaceSetsFaces(allFaceSets.size());
00178 forAll(allFaceSets, setI)
00179 {
00180 allFaceSetsFaces[setI] = UIndirectList<face>
00181 (
00182 mesh.faces(),
00183 allFaceSets[setI]
00184 );
00185 }
00186
00187 autoPtr<mapPolyMesh> map;
00188 {
00189
00190 polyTopoChange meshMod(mesh);
00191
00192
00193 faceCombiner.setRefinement(allFaceSets, meshMod);
00194
00195
00196 map = meshMod.changeMesh(mesh, false, true);
00197
00198
00199 mesh.updateMesh(map);
00200
00201
00202 if (map().hasMotionPoints())
00203 {
00204 mesh.movePoints(map().preMotionPoints());
00205 }
00206 else
00207 {
00208
00209 mesh.clearOut();
00210 }
00211 }
00212
00213
00214
00215
00216
00217
00218 labelHashSet errorFaces;
00219
00220 if (snapMeshDict)
00221 {
00222 checkSnapMesh(runTime, mesh, errorFaces);
00223 }
00224 else
00225 {
00226 mesh.checkFacePyramids(false, -SMALL, &errorFaces);
00227 }
00228
00229
00230 labelHashSet errorSets;
00231
00232 forAll(allFaceSets, setI)
00233 {
00234 label newMasterI = map().reverseFaceMap()[allFaceSets[setI][0]];
00235
00236 if (errorFaces.found(newMasterI))
00237 {
00238 errorSets.insert(setI);
00239 }
00240 }
00241 label nErrorSets = returnReduce(errorSets.size(), sumOp<label>());
00242
00243 Info<< "Detected " << nErrorSets
00244 << " error faces on boundaries that have been merged."
00245 << " These will be restored to their original faces."
00246 << endl;
00247
00248 if (nErrorSets > 0)
00249 {
00250
00251 forAllConstIter(labelHashSet, errorSets, iter)
00252 {
00253 label setI = iter.key();
00254
00255 faceList& setFaceVerts = allFaceSetsFaces[setI];
00256
00257 forAll(setFaceVerts, i)
00258 {
00259 inplaceRenumber(map().reversePointMap(), setFaceVerts[i]);
00260
00261
00262 forAll(setFaceVerts[i], j)
00263 {
00264 label newVertI = setFaceVerts[i][j];
00265
00266 if (newVertI < 0)
00267 {
00268 FatalErrorIn("mergePatchFaces")
00269 << "In set:" << setI << " old face labels:"
00270 << allFaceSets[setI] << " new face vertices:"
00271 << setFaceVerts[i] << " are unmapped vertices!"
00272 << abort(FatalError);
00273 }
00274 }
00275 }
00276 }
00277
00278
00279
00280 polyTopoChange meshMod(mesh);
00281
00282
00283
00284 forAllConstIter(labelHashSet, errorSets, iter)
00285 {
00286 label setI = iter.key();
00287
00288 const labelList& setFaces = allFaceSets[setI];
00289 const faceList& setFaceVerts = allFaceSetsFaces[setI];
00290
00291 label newMasterI = map().reverseFaceMap()[setFaces[0]];
00292
00293
00294
00295 label own = mesh.faceOwner()[newMasterI];
00296 label zoneID = mesh.faceZones().whichZone(newMasterI);
00297 bool zoneFlip = false;
00298 if (zoneID >= 0)
00299 {
00300 const faceZone& fZone = mesh.faceZones()[zoneID];
00301 zoneFlip = fZone.flipMap()[fZone.whichFace(newMasterI)];
00302 }
00303 label patchI = mesh.boundaryMesh().whichPatch(newMasterI);
00304
00305
00306 Pout<< "Restoring new master face " << newMasterI
00307 << " to vertices " << setFaceVerts[0] << endl;
00308
00309
00310 meshMod.setAction
00311 (
00312 polyModifyFace
00313 (
00314 setFaceVerts[0],
00315 newMasterI,
00316 own,
00317 -1,
00318 false,
00319 patchI,
00320 false,
00321 zoneID,
00322 zoneFlip
00323 )
00324 );
00325
00326
00327
00328 for (label i = 1; i < setFaces.size(); i++)
00329 {
00330 Pout<< "Restoring removed face " << setFaces[i]
00331 << " with vertices " << setFaceVerts[i] << endl;
00332
00333 meshMod.setAction
00334 (
00335 polyAddFace
00336 (
00337 setFaceVerts[i],
00338 own,
00339 -1,
00340 -1,
00341 -1,
00342 newMasterI,
00343 false,
00344 patchI,
00345 zoneID,
00346 zoneFlip
00347 )
00348 );
00349 }
00350 }
00351
00352
00353 map = meshMod.changeMesh(mesh, false, true);
00354
00355
00356 mesh.updateMesh(map);
00357
00358
00359 if (map().hasMotionPoints())
00360 {
00361 mesh.movePoints(map().preMotionPoints());
00362 }
00363 else
00364 {
00365
00366 mesh.clearOut();
00367 }
00368 }
00369 }
00370 else
00371 {
00372 Info<< "No faces merged ..." << endl;
00373 }
00374
00375 return nFaceSets;
00376 }
00377
00378
00379
00380
00381 label mergeEdges(const scalar minCos, polyMesh& mesh)
00382 {
00383 Info<< "Merging all points on surface that" << nl
00384 << "- are used by only two boundary faces and" << nl
00385 << "- make an angle with a cosine of more than " << minCos
00386 << "." << nl << endl;
00387
00388
00389 removePoints pointRemover(mesh);
00390
00391
00392 boolList pointCanBeDeleted;
00393 label nRemove = pointRemover.countPointUsage(minCos, pointCanBeDeleted);
00394
00395 if (nRemove > 0)
00396 {
00397 Info<< "Removing " << nRemove
00398 << " straight edge points ..." << endl;
00399
00400
00401 polyTopoChange meshMod(mesh);
00402
00403 pointRemover.setRefinement(pointCanBeDeleted, meshMod);
00404
00405
00406 autoPtr<mapPolyMesh> map = meshMod.changeMesh(mesh, false, true);
00407
00408
00409 mesh.updateMesh(map);
00410
00411
00412 if (map().hasMotionPoints())
00413 {
00414 mesh.movePoints(map().preMotionPoints());
00415 }
00416 else
00417 {
00418
00419 mesh.clearOut();
00420 }
00421 }
00422 else
00423 {
00424 Info<< "No straight edges simplified and no points removed ..." << endl;
00425 }
00426
00427 return nRemove;
00428 }
00429
00430
00431
00432
00433 int main(int argc, char *argv[])
00434 {
00435 argList::validArgs.append("feature angle [0..180]");
00436 argList::validOptions.insert("concaveAngle", "[0..180]");
00437 argList::validOptions.insert("snapMesh", "");
00438 argList::validOptions.insert("overwrite", "");
00439
00440 # include "setRootCase.H"
00441 # include "createTime.H"
00442 runTime.functionObjects().off();
00443 # include "createPolyMesh.H"
00444 const word oldInstance = mesh.pointsInstance();
00445
00446 scalar featureAngle(readScalar(IStringStream(args.additionalArgs()[0])()));
00447
00448 scalar minCos = Foam::cos(featureAngle*mathematicalConstant::pi/180.0);
00449
00450 scalar concaveAngle = defaultConcaveAngle;
00451 args.optionReadIfPresent("concaveAngle", concaveAngle);
00452
00453 scalar concaveSin = Foam::sin(concaveAngle*mathematicalConstant::pi/180.0);
00454
00455 bool snapMeshDict = args.optionFound("snapMesh");
00456 bool overwrite = args.optionFound("overwrite");
00457
00458 Info<< "Merging all faces of a cell" << nl
00459 << " - which are on the same patch" << nl
00460 << " - which make an angle < " << featureAngle << " degrees"
00461 << nl
00462 << " (cos:" << minCos << ')' << nl
00463 << " - even when resulting face becomes concave by more than "
00464 << concaveAngle << " degrees" << nl
00465 << " (sin:" << concaveSin << ')' << nl
00466 << endl;
00467
00468 if (!overwrite)
00469 {
00470 runTime++;
00471 }
00472
00473
00474 label nChanged = mergePatchFaces
00475 (
00476 minCos,
00477 concaveSin,
00478 snapMeshDict,
00479 runTime,
00480 mesh
00481 );
00482
00483
00484 if (snapMeshDict)
00485 {
00486 Info<< "Merging all 'loose' points on surface edges"
00487 << ", regardless of the angle they make." << endl;
00488
00489
00490 nChanged += mergeEdges(-1, mesh);
00491 }
00492 else
00493 {
00494 nChanged += mergeEdges(minCos, mesh);
00495 }
00496
00497 if (nChanged > 0)
00498 {
00499 if (overwrite)
00500 {
00501 mesh.setInstance(oldInstance);
00502 }
00503
00504 Info<< "Writing morphed mesh to time " << runTime.timeName() << endl;
00505
00506 mesh.write();
00507 }
00508 else
00509 {
00510 Info<< "Mesh unchanged." << endl;
00511 }
00512
00513 Info << "End\n" << endl;
00514
00515 return 0;
00516 }
00517
00518
00519