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
00047
00048
00049
00050
00051
00052
00053
00054
00055
00056
00057
00058 #include "argList.H"
00059 #include "Time.H"
00060 #include "timeSelector.H"
00061 #include "fvMesh.H"
00062 #include "mathematicalConstants.H"
00063 #include "polyTopoChange.H"
00064 #include "mapPolyMesh.H"
00065 #include "PackedBoolList.H"
00066 #include "meshTools.H"
00067 #include "OFstream.H"
00068 #include "meshDualiser.H"
00069 #include "ReadFields.H"
00070 #include "volFields.H"
00071 #include "surfaceFields.H"
00072
00073 using namespace Foam;
00074
00075
00076
00077
00078
00079
00080 void simpleMarkFeatures
00081 (
00082 const polyMesh& mesh,
00083 const PackedBoolList& isBoundaryEdge,
00084 const scalar featureAngle,
00085 const bool concaveMultiCells,
00086 const bool doNotPreserveFaceZones,
00087
00088 labelList& featureFaces,
00089 labelList& featureEdges,
00090 labelList& singleCellFeaturePoints,
00091 labelList& multiCellFeaturePoints
00092 )
00093 {
00094 scalar minCos = Foam::cos(featureAngle * mathematicalConstant::pi/180.0);
00095
00096 const polyBoundaryMesh& patches = mesh.boundaryMesh();
00097
00098
00099 labelHashSet featureEdgeSet;
00100 labelHashSet singleCellFeaturePointSet;
00101 labelHashSet multiCellFeaturePointSet;
00102
00103
00104
00105
00106
00107 forAll(patches, patchI)
00108 {
00109 const polyPatch& pp = patches[patchI];
00110 const labelList& meshEdges = pp.meshEdges();
00111
00112
00113 for (label edgeI = pp.nInternalEdges(); edgeI < pp.nEdges(); edgeI++)
00114 {
00115 label meshEdgeI = meshEdges[edgeI];
00116 featureEdgeSet.insert(meshEdgeI);
00117 singleCellFeaturePointSet.insert(mesh.edges()[meshEdgeI][0]);
00118 singleCellFeaturePointSet.insert(mesh.edges()[meshEdgeI][1]);
00119 }
00120 }
00121
00122
00123
00124
00125
00126
00127
00128
00129
00130
00131 primitivePatch allBoundary
00132 (
00133 SubList<face>
00134 (
00135 mesh.faces(),
00136 mesh.nFaces()-mesh.nInternalFaces(),
00137 mesh.nInternalFaces()
00138 ),
00139 mesh.points()
00140 );
00141
00142
00143 allBoundary.checkPointManifold(false, &singleCellFeaturePointSet);
00144
00145
00146 const labelListList& edgeFaces = allBoundary.edgeFaces();
00147 const labelList& meshPoints = allBoundary.meshPoints();
00148
00149 forAll(edgeFaces, edgeI)
00150 {
00151 const labelList& eFaces = edgeFaces[edgeI];
00152
00153 if (eFaces.size() > 2)
00154 {
00155 const edge& e = allBoundary.edges()[edgeI];
00156
00157
00158
00159
00160
00161
00162 singleCellFeaturePointSet.insert(meshPoints[e[0]]);
00163 singleCellFeaturePointSet.insert(meshPoints[e[1]]);
00164 }
00165 }
00166
00167
00168 forAll(edgeFaces, edgeI)
00169 {
00170 const labelList& eFaces = edgeFaces[edgeI];
00171
00172 if (eFaces.size() == 2)
00173 {
00174 label f0 = eFaces[0];
00175 label f1 = eFaces[1];
00176
00177
00178 const vector& n0 = allBoundary.faceNormals()[f0];
00179 const vector& n1 = allBoundary.faceNormals()[f1];
00180
00181 if ((n0 & n1) < minCos)
00182 {
00183 const edge& e = allBoundary.edges()[edgeI];
00184 label v0 = meshPoints[e[0]];
00185 label v1 = meshPoints[e[1]];
00186
00187 label meshEdgeI = meshTools::findEdge(mesh, v0, v1);
00188 featureEdgeSet.insert(meshEdgeI);
00189
00190
00191
00192 vector c1c0
00193 (
00194 allBoundary[f1].centre(allBoundary.points())
00195 - allBoundary[f0].centre(allBoundary.points())
00196 );
00197
00198 if (concaveMultiCells && (c1c0 & n0) > SMALL)
00199 {
00200
00201 Info<< "Detected concave feature edge:" << edgeI
00202 << " cos:" << (c1c0 & n0)
00203 << " coords:"
00204 << allBoundary.points()[v0]
00205 << allBoundary.points()[v1]
00206 << endl;
00207
00208 singleCellFeaturePointSet.erase(v0);
00209 multiCellFeaturePointSet.insert(v0);
00210 singleCellFeaturePointSet.erase(v1);
00211 multiCellFeaturePointSet.insert(v1);
00212 }
00213 else
00214 {
00215
00216 if (!multiCellFeaturePointSet.found(v0))
00217 {
00218 singleCellFeaturePointSet.insert(v0);
00219 }
00220 if (!multiCellFeaturePointSet.found(v1))
00221 {
00222 singleCellFeaturePointSet.insert(v1);
00223 }
00224 }
00225 }
00226 }
00227 }
00228
00229
00230
00231
00232
00233
00234 labelHashSet featureFaceSet(mesh.nFaces()-mesh.nInternalFaces());
00235
00236 for (label faceI = mesh.nInternalFaces(); faceI < mesh.nFaces(); faceI++)
00237 {
00238 featureFaceSet.insert(faceI);
00239 }
00240
00241
00242 const faceZoneMesh& faceZones = mesh.faceZones();
00243
00244 if (doNotPreserveFaceZones)
00245 {
00246 if (faceZones.size() > 0)
00247 {
00248 WarningIn("simpleMarkFeatures(..)")
00249 << "Detected " << faceZones.size()
00250 << " faceZones. These will not be preserved."
00251 << endl;
00252 }
00253 }
00254 else
00255 {
00256 if (faceZones.size() > 0)
00257 {
00258 Info<< "Detected " << faceZones.size()
00259 << " faceZones. Preserving these by marking their"
00260 << " points, edges and faces as features." << endl;
00261 }
00262
00263 forAll(faceZones, zoneI)
00264 {
00265 const faceZone& fz = faceZones[zoneI];
00266
00267 Info<< "Inserting all faces in faceZone " << fz.name()
00268 << " as features." << endl;
00269
00270 forAll(fz, i)
00271 {
00272 label faceI = fz[i];
00273 const face& f = mesh.faces()[faceI];
00274 const labelList& fEdges = mesh.faceEdges()[faceI];
00275
00276 featureFaceSet.insert(faceI);
00277 forAll(f, fp)
00278 {
00279
00280
00281 singleCellFeaturePointSet.erase(f[fp]);
00282 multiCellFeaturePointSet.insert(f[fp]);
00283
00284
00285 featureEdgeSet.insert(fEdges[fp]);
00286 }
00287 }
00288 }
00289 }
00290
00291
00292 featureFaces = featureFaceSet.toc();
00293 featureEdges = featureEdgeSet.toc();
00294 singleCellFeaturePoints = singleCellFeaturePointSet.toc();
00295 multiCellFeaturePoints = multiCellFeaturePointSet.toc();
00296 }
00297
00298
00299
00300 void dumpFeatures
00301 (
00302 const polyMesh& mesh,
00303 const labelList& featureFaces,
00304 const labelList& featureEdges,
00305 const labelList& singleCellFeaturePoints,
00306 const labelList& multiCellFeaturePoints
00307 )
00308 {
00309 {
00310 OFstream str("featureFaces.obj");
00311 Info<< "Dumping centres of featureFaces to obj file " << str.name()
00312 << endl;
00313 forAll(featureFaces, i)
00314 {
00315 meshTools::writeOBJ(str, mesh.faceCentres()[featureFaces[i]]);
00316 }
00317 }
00318 {
00319 OFstream str("featureEdges.obj");
00320 Info<< "Dumping featureEdges to obj file " << str.name() << endl;
00321 label vertI = 0;
00322
00323 forAll(featureEdges, i)
00324 {
00325 const edge& e = mesh.edges()[featureEdges[i]];
00326 meshTools::writeOBJ(str, mesh.points()[e[0]]);
00327 vertI++;
00328 meshTools::writeOBJ(str, mesh.points()[e[1]]);
00329 vertI++;
00330 str<< "l " << vertI-1 << ' ' << vertI << nl;
00331 }
00332 }
00333 {
00334 OFstream str("singleCellFeaturePoints.obj");
00335 Info<< "Dumping featurePoints that become a single cell to obj file "
00336 << str.name() << endl;
00337 forAll(singleCellFeaturePoints, i)
00338 {
00339 meshTools::writeOBJ(str, mesh.points()[singleCellFeaturePoints[i]]);
00340 }
00341 }
00342 {
00343 OFstream str("multiCellFeaturePoints.obj");
00344 Info<< "Dumping featurePoints that become multiple cells to obj file "
00345 << str.name() << endl;
00346 forAll(multiCellFeaturePoints, i)
00347 {
00348 meshTools::writeOBJ(str, mesh.points()[multiCellFeaturePoints[i]]);
00349 }
00350 }
00351 }
00352
00353
00354 int main(int argc, char *argv[])
00355 {
00356 argList::noParallel();
00357 timeSelector::addOptions(true, false);
00358
00359 argList::validArgs.append("feature angle[0-180]");
00360 argList::validOptions.insert("splitAllFaces", "");
00361 argList::validOptions.insert("concaveMultiCells", "");
00362 argList::validOptions.insert("doNotPreserveFaceZones", "");
00363 argList::validOptions.insert("overwrite", "");
00364
00365 # include "setRootCase.H"
00366 # include "createTime.H"
00367
00368 instantList timeDirs = timeSelector::select0(runTime, args);
00369
00370 # include "createMesh.H"
00371
00372 const word oldInstance = mesh.pointsInstance();
00373
00374
00375
00376
00377 PackedBoolList isBoundaryEdge(mesh.nEdges());
00378 for (label faceI = mesh.nInternalFaces(); faceI < mesh.nFaces(); faceI++)
00379 {
00380 const labelList& fEdges = mesh.faceEdges()[faceI];
00381
00382 forAll(fEdges, i)
00383 {
00384 isBoundaryEdge.set(fEdges[i], 1);
00385 }
00386 }
00387
00388 scalar featureAngle(readScalar(IStringStream(args.additionalArgs()[0])()));
00389
00390 scalar minCos = Foam::cos(featureAngle * mathematicalConstant::pi/180.0);
00391
00392 Info<< "Feature:" << featureAngle << endl
00393 << "minCos :" << minCos << endl
00394 << endl;
00395
00396
00397 const bool splitAllFaces = args.optionFound("splitAllFaces");
00398 if (splitAllFaces)
00399 {
00400 Info<< "Splitting all internal faces to create multiple faces"
00401 << " between two cells." << nl
00402 << endl;
00403 }
00404
00405 const bool overwrite = args.optionFound("overwrite");
00406 const bool doNotPreserveFaceZones = args.optionFound
00407 (
00408 "doNotPreserveFaceZones"
00409 );
00410 const bool concaveMultiCells = args.optionFound("concaveMultiCells");
00411 if (concaveMultiCells)
00412 {
00413 Info<< "Generating multiple cells for points on concave feature edges."
00414 << nl << endl;
00415 }
00416
00417
00418
00419 labelList featureFaces;
00420
00421 labelList featureEdges;
00422
00423 labelList singleCellFeaturePoints;
00424
00425 labelList multiCellFeaturePoints;
00426
00427
00428 simpleMarkFeatures
00429 (
00430 mesh,
00431 isBoundaryEdge,
00432 featureAngle,
00433 concaveMultiCells,
00434 doNotPreserveFaceZones,
00435
00436 featureFaces,
00437 featureEdges,
00438 singleCellFeaturePoints,
00439 multiCellFeaturePoints
00440 );
00441
00442
00443
00444
00445
00446 if (splitAllFaces)
00447 {
00448 featureEdges = identity(mesh.nEdges());
00449 featureFaces = identity(mesh.nFaces());
00450 }
00451
00452
00453 dumpFeatures
00454 (
00455 mesh,
00456 featureFaces,
00457 featureEdges,
00458 singleCellFeaturePoints,
00459 multiCellFeaturePoints
00460 );
00461
00462
00463
00464
00465 IOobjectList objects(mesh, runTime.timeName());
00466
00467
00468 PtrList<volScalarField> vsFlds;
00469 ReadFields(mesh, objects, vsFlds);
00470
00471 PtrList<volVectorField> vvFlds;
00472 ReadFields(mesh, objects, vvFlds);
00473
00474 PtrList<volSphericalTensorField> vstFlds;
00475 ReadFields(mesh, objects, vstFlds);
00476
00477 PtrList<volSymmTensorField> vsymtFlds;
00478 ReadFields(mesh, objects, vsymtFlds);
00479
00480 PtrList<volTensorField> vtFlds;
00481 ReadFields(mesh, objects, vtFlds);
00482
00483
00484 PtrList<surfaceScalarField> ssFlds;
00485 ReadFields(mesh, objects, ssFlds);
00486
00487 PtrList<surfaceVectorField> svFlds;
00488 ReadFields(mesh, objects, svFlds);
00489
00490 PtrList<surfaceSphericalTensorField> sstFlds;
00491 ReadFields(mesh, objects, sstFlds);
00492
00493 PtrList<surfaceSymmTensorField> ssymtFlds;
00494 ReadFields(mesh, objects, ssymtFlds);
00495
00496 PtrList<surfaceTensorField> stFlds;
00497 ReadFields(mesh, objects, stFlds);
00498
00499
00500
00501 polyTopoChange meshMod(mesh.boundaryMesh().size());
00502
00503
00504 meshDualiser dualMaker(mesh);
00505
00506
00507
00508 dualMaker.setRefinement
00509 (
00510 splitAllFaces,
00511 featureFaces,
00512 featureEdges,
00513 singleCellFeaturePoints,
00514 multiCellFeaturePoints,
00515 meshMod
00516 );
00517
00518
00519 autoPtr<mapPolyMesh> map = meshMod.changeMesh(mesh, false);
00520
00521
00522 mesh.updateMesh(map);
00523
00524
00525 if (map().hasMotionPoints())
00526 {
00527 mesh.movePoints(map().preMotionPoints());
00528 }
00529
00530 if (!overwrite)
00531 {
00532 runTime++;
00533 }
00534 else
00535 {
00536 mesh.setInstance(oldInstance);
00537 }
00538
00539 Info<< "Writing dual mesh to " << runTime.timeName() << endl;
00540
00541 mesh.write();
00542
00543 Info<< "End\n" << endl;
00544
00545 return 0;
00546 }
00547
00548
00549