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 #include "argList.H"
00042 #include "Time.H"
00043 #include "polyMesh.H"
00044 #include "IOdictionary.H"
00045 #include "twoDPointCorrector.H"
00046 #include "OFstream.H"
00047 #include "meshTools.H"
00048
00049 #include "triSurface.H"
00050 #include "triSurfaceSearch.H"
00051 #include "meshSearch.H"
00052 #include "cellClassification.H"
00053 #include "cellSet.H"
00054 #include "cellInfo.H"
00055 #include "MeshWave.H"
00056 #include "edgeStats.H"
00057 #include "treeDataTriSurface.H"
00058 #include "indexedOctree.H"
00059
00060 using namespace Foam;
00061
00062
00063
00064
00065 static const label MESH = cellClassification::INSIDE;
00066 static const label NONMESH = cellClassification::OUTSIDE;
00067
00068
00069 void writeSet(const cellSet& cells, const string& msg)
00070 {
00071 Info<< "Writing " << msg << " (" << cells.size() << ") to cellSet "
00072 << cells.instance()/cells.local()/cells.name()
00073 << endl << endl;
00074 cells.write();
00075 }
00076
00077
00078 void getType(const labelList& elems, const label type, labelHashSet& set)
00079 {
00080 forAll(elems, i)
00081 {
00082 if (elems[i] == type)
00083 {
00084 set.insert(i);
00085 }
00086 }
00087 }
00088
00089
00090 void cutBySurface
00091 (
00092 const polyMesh& mesh,
00093 const meshSearch& queryMesh,
00094 const triSurfaceSearch& querySurf,
00095
00096 const pointField& outsidePts,
00097 const bool selectCut,
00098 const bool selectInside,
00099 const bool selectOutside,
00100 const scalar nearDist,
00101
00102 cellClassification& cellType
00103 )
00104 {
00105
00106 cellType =
00107 cellClassification
00108 (
00109 mesh,
00110 queryMesh,
00111 querySurf,
00112 outsidePts
00113 );
00114
00115
00116 cellSet inside(mesh, "inside", mesh.nCells()/10);
00117 getType(cellType, cellClassification::INSIDE, inside);
00118 writeSet(inside, "inside cells");
00119
00120 cellSet outside(mesh, "outside", mesh.nCells()/10);
00121 getType(cellType, cellClassification::OUTSIDE, outside);
00122 writeSet(outside, "outside cells");
00123
00124 cellSet cutCells(mesh, "cutCells", mesh.nCells()/10);
00125 getType(cellType, cellClassification::CUT, cutCells);
00126 writeSet(cutCells, "cells cut by surface");
00127
00128
00129
00130
00131
00132
00133
00134
00135 forAll(cellType, cellI)
00136 {
00137 label cType = cellType[cellI];
00138
00139 if (cType == cellClassification::CUT)
00140 {
00141 if (selectCut)
00142 {
00143 cellType[cellI] = MESH;
00144 }
00145 else
00146 {
00147 cellType[cellI] = NONMESH;
00148 }
00149 }
00150 else if (cType == cellClassification::INSIDE)
00151 {
00152 if (selectInside)
00153 {
00154 cellType[cellI] = MESH;
00155 }
00156 else
00157 {
00158 cellType[cellI] = NONMESH;
00159 }
00160 }
00161 else if (cType == cellClassification::OUTSIDE)
00162 {
00163 if (selectOutside)
00164 {
00165 cellType[cellI] = MESH;
00166 }
00167 else
00168 {
00169 cellType[cellI] = NONMESH;
00170 }
00171 }
00172 else
00173 {
00174 FatalErrorIn("cutBySurface")
00175 << "Multiple mesh regions in original mesh" << endl
00176 << "Please use splitMeshRegions to separate these"
00177 << exit(FatalError);
00178 }
00179 }
00180
00181
00182 if (nearDist > 0)
00183 {
00184 Info<< "Removing cells with points closer than " << nearDist
00185 << " to the surface ..." << nl << endl;
00186
00187 const pointField& pts = mesh.points();
00188 const indexedOctree<treeDataTriSurface>& tree = querySurf.tree();
00189
00190 label nRemoved = 0;
00191
00192 forAll(pts, pointI)
00193 {
00194 const point& pt = pts[pointI];
00195
00196 pointIndexHit hitInfo = tree.findNearest(pt, sqr(nearDist));
00197
00198 if (hitInfo.hit())
00199 {
00200 const labelList& pCells = mesh.pointCells()[pointI];
00201
00202 forAll(pCells, i)
00203 {
00204 if (cellType[pCells[i]] != NONMESH)
00205 {
00206 cellType[pCells[i]] = NONMESH;
00207 nRemoved++;
00208 }
00209 }
00210 }
00211 }
00212
00213
00214
00215
00216
00217
00218
00219
00220
00221
00222
00223
00224
00225
00226
00227
00228
00229
00230
00231
00232
00233
00234
00235 Info<< "Removed " << nRemoved << " cells since too close to surface"
00236 << nl << endl;
00237 }
00238 }
00239
00240
00241
00242
00243
00244 label selectOutsideCells
00245 (
00246 const polyMesh& mesh,
00247 const meshSearch& queryMesh,
00248 const pointField& outsidePts,
00249 cellClassification& cellType
00250 )
00251 {
00252
00253
00254
00255
00256
00257
00258 labelHashSet outsideFacesMap(outsidePts.size() * 6 * 2);
00259 DynamicList<label> outsideFaces(outsideFacesMap.size());
00260
00261 DynamicList<cellInfo> outsideFacesInfo(outsideFacesMap.size());
00262
00263
00264 const cellInfo meshInfo(MESH);
00265
00266 forAll(outsidePts, outsidePtI)
00267 {
00268
00269 label cellI = queryMesh.findCell(outsidePts[outsidePtI], -1, false);
00270
00271 if (cellType[cellI] == MESH)
00272 {
00273 Info<< "Marking cell " << cellI << " containing outside point "
00274 << outsidePts[outsidePtI] << " with type " << cellType[cellI]
00275 << " ..." << endl;
00276
00277
00278
00279
00280
00281
00282 const labelList& cFaces = mesh.cells()[cellI];
00283 forAll(cFaces, i)
00284 {
00285 label faceI = cFaces[i];
00286
00287 if (outsideFacesMap.insert(faceI))
00288 {
00289 outsideFaces.append(faceI);
00290 outsideFacesInfo.append(meshInfo);
00291 }
00292 }
00293 }
00294 }
00295
00296
00297 MeshWave<cellInfo> regionCalc
00298 (
00299 mesh,
00300 outsideFaces.shrink(),
00301 outsideFacesInfo.shrink(),
00302 mesh.nCells()
00303 );
00304
00305
00306
00307 const List<cellInfo>& allCellInfo = regionCalc.allCellInfo();
00308
00309 label nChanged = 0;
00310
00311 forAll(allCellInfo, cellI)
00312 {
00313 if (cellType[cellI] == MESH)
00314 {
00315
00316
00317 if (allCellInfo[cellI].type() != MESH)
00318 {
00319 cellType[cellI] = NONMESH;
00320 nChanged++;
00321 }
00322 }
00323 }
00324
00325 return nChanged;
00326 }
00327
00328
00329
00330
00331 int main(int argc, char *argv[])
00332 {
00333 argList::noParallel();
00334
00335 # include "setRootCase.H"
00336 # include "createTime.H"
00337 # include "createPolyMesh.H"
00338
00339
00340 edgeStats edgeCalc(mesh);
00341
00342
00343 IOdictionary refineDict
00344 (
00345 IOobject
00346 (
00347 "selectCellsDict",
00348 runTime.system(),
00349 mesh,
00350 IOobject::MUST_READ,
00351 IOobject::NO_WRITE
00352 )
00353 );
00354
00355 fileName surfName(refineDict.lookup("surface"));
00356 pointField outsidePts(refineDict.lookup("outsidePoints"));
00357 bool useSurface(readBool(refineDict.lookup("useSurface")));
00358 bool selectCut(readBool(refineDict.lookup("selectCut")));
00359 bool selectInside(readBool(refineDict.lookup("selectInside")));
00360 bool selectOutside(readBool(refineDict.lookup("selectOutside")));
00361 scalar nearDist(readScalar(refineDict.lookup("nearDistance")));
00362
00363
00364 if (useSurface)
00365 {
00366 Info<< "Cells to be used for meshing (0=false, 1=true):" << nl
00367 << " cells cut by surface : " << selectCut << nl
00368 << " cells inside of surface : " << selectInside << nl
00369 << " cells outside of surface : " << selectOutside << nl
00370 << " cells with points further than : " << nearDist << nl
00371 << endl;
00372 }
00373 else
00374 {
00375 Info<< "Cells to be used for meshing (0=false, 1=true):" << nl
00376 << " cells reachable from outsidePoints:" << selectOutside << nl
00377 << endl;
00378 }
00379
00380
00381 (void)edgeCalc.minLen(Info);
00382
00383
00384 meshSearch queryMesh(mesh, true);
00385
00386
00387 forAll(outsidePts, outsideI)
00388 {
00389 const point& outsidePoint = outsidePts[outsideI];
00390
00391 if (queryMesh.findCell(outsidePoint, -1, false) == -1)
00392 {
00393 FatalErrorIn(args.executable())
00394 << "outsidePoint " << outsidePoint
00395 << " is not inside any cell"
00396 << exit(FatalError);
00397 }
00398 }
00399
00400
00401
00402 cellClassification cellType
00403 (
00404 mesh,
00405 labelList
00406 (
00407 mesh.nCells(),
00408 cellClassification::MESH
00409 )
00410 );
00411
00412
00413
00414 autoPtr<triSurface> surf(NULL);
00415
00416 autoPtr<triSurfaceSearch> querySurf(NULL);
00417
00418 if (useSurface)
00419 {
00420 surf.reset(new triSurface(surfName));
00421
00422
00423 surf().writeStats(Info);
00424
00425
00426 querySurf.reset(new triSurfaceSearch(surf));
00427
00428
00429 cutBySurface
00430 (
00431 mesh,
00432 queryMesh,
00433 querySurf,
00434
00435 outsidePts,
00436 selectCut,
00437 selectInside,
00438 selectOutside,
00439 nearDist,
00440
00441 cellType
00442 );
00443 }
00444
00445
00446
00447
00448
00449 label nHanging, nRegionEdges, nRegionPoints, nOutside;
00450
00451 do
00452 {
00453 Info<< "Removing cells which after subsetting would have all points"
00454 << " on outside ..." << nl << endl;
00455
00456 nHanging = cellType.fillHangingCells
00457 (
00458 MESH,
00459 NONMESH,
00460 mesh.nCells()
00461 );
00462
00463
00464 Info<< "Removing edges connecting cells unconnected by faces ..."
00465 << nl << endl;
00466
00467 nRegionEdges = cellType.fillRegionEdges
00468 (
00469 MESH,
00470 NONMESH,
00471 mesh.nCells()
00472 );
00473
00474
00475 Info<< "Removing points connecting cells unconnected by faces ..."
00476 << nl << endl;
00477
00478 nRegionPoints = cellType.fillRegionPoints
00479 (
00480 MESH,
00481 NONMESH,
00482 mesh.nCells()
00483 );
00484
00485 nOutside = 0;
00486 if (selectOutside)
00487 {
00488
00489
00490
00491 nOutside = selectOutsideCells
00492 (
00493 mesh,
00494 queryMesh,
00495 outsidePts,
00496 cellType
00497 );
00498 }
00499 } while
00500 (
00501 nHanging != 0
00502 || nRegionEdges != 0
00503 || nRegionPoints != 0
00504 || nOutside != 0
00505 );
00506
00507 cellSet selectedCells(mesh, "selected", mesh.nCells()/10);
00508 getType(cellType, MESH, selectedCells);
00509
00510 writeSet(selectedCells, "cells selected for meshing");
00511
00512
00513 Info << "End\n" << endl;
00514
00515 return 0;
00516 }
00517
00518
00519