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 #include "autoRefineDriver.H"
00028 #include "meshRefinement.H"
00029 #include "fvMesh.H"
00030 #include "Time.H"
00031 #include "boundBox.H"
00032 #include "mapDistributePolyMesh.H"
00033 #include "cellSet.H"
00034 #include "syncTools.H"
00035 #include "refinementParameters.H"
00036 #include "featureEdgeMesh.H"
00037 #include "refinementSurfaces.H"
00038 #include "shellSurfaces.H"
00039
00040
00041
00042 namespace Foam
00043 {
00044
00045 defineTypeNameAndDebug(autoRefineDriver, 0);
00046
00047 }
00048
00049
00050
00051
00052
00053 Foam::label Foam::autoRefineDriver::readFeatureEdges
00054 (
00055 const PtrList<dictionary>& featDicts,
00056 PtrList<featureEdgeMesh>& featureMeshes,
00057 labelList& featureLevels
00058 ) const
00059 {
00060 Info<< "Reading external feature lines." << endl;
00061
00062 const fvMesh& mesh = meshRefiner_.mesh();
00063
00064 featureMeshes.setSize(featDicts.size());
00065 featureLevels.setSize(featDicts.size());
00066
00067 forAll(featDicts, i)
00068 {
00069 const dictionary& dict = featDicts[i];
00070
00071 fileName featFileName(dict.lookup("file"));
00072
00073 featureMeshes.set
00074 (
00075 i,
00076 new featureEdgeMesh
00077 (
00078 IOobject
00079 (
00080 featFileName,
00081 mesh.time().findInstance("triSurface", featFileName),
00082
00083 "triSurface",
00084 mesh.time(),
00085 IOobject::MUST_READ,
00086 IOobject::NO_WRITE,
00087 false
00088 )
00089 )
00090 );
00091
00092 featureMeshes[i].mergePoints(meshRefiner_.mergeDistance());
00093 featureLevels[i] = readLabel(dict.lookup("level"));
00094
00095 Info<< "Refinement level " << featureLevels[i]
00096 << " for all cells crossed by feature " << featFileName
00097 << " (" << featureMeshes[i].points().size() << " points, "
00098 << featureMeshes[i].edges().size() << " edges)." << endl;
00099 }
00100
00101 Info<< "Read feature lines in = "
00102 << mesh.time().cpuTimeIncrement() << " s" << nl << endl;
00103
00104 return featureMeshes.size();
00105 }
00106
00107
00108
00109
00110
00111 Foam::autoRefineDriver::autoRefineDriver
00112 (
00113 meshRefinement& meshRefiner,
00114 decompositionMethod& decomposer,
00115 fvMeshDistribute& distributor,
00116 const labelList& globalToPatch
00117 )
00118 :
00119 meshRefiner_(meshRefiner),
00120 decomposer_(decomposer),
00121 distributor_(distributor),
00122 globalToPatch_(globalToPatch)
00123 {}
00124
00125
00126
00127
00128 Foam::label Foam::autoRefineDriver::featureEdgeRefine
00129 (
00130 const refinementParameters& refineParams,
00131 const PtrList<dictionary>& featDicts,
00132 const label maxIter,
00133 const label minRefine
00134 )
00135 {
00136 const fvMesh& mesh = meshRefiner_.mesh();
00137
00138
00139 PtrList<featureEdgeMesh> featureMeshes;
00140
00141 labelList featureLevels;
00142 readFeatureEdges(featDicts, featureMeshes, featureLevels);
00143
00144
00145 label iter = 0;
00146
00147 if (featureMeshes.size() && maxIter > 0)
00148 {
00149 for (; iter < maxIter; iter++)
00150 {
00151 Info<< nl
00152 << "Feature refinement iteration " << iter << nl
00153 << "------------------------------" << nl
00154 << endl;
00155
00156 labelList candidateCells
00157 (
00158 meshRefiner_.refineCandidates
00159 (
00160 refineParams.keepPoints()[0],
00161 refineParams.curvature(),
00162
00163 featureMeshes,
00164 featureLevels,
00165
00166 true,
00167 false,
00168 false,
00169 false,
00170 refineParams.maxGlobalCells(),
00171 refineParams.maxLocalCells()
00172 )
00173 );
00174 labelList cellsToRefine
00175 (
00176 meshRefiner_.meshCutter().consistentRefinement
00177 (
00178 candidateCells,
00179 true
00180 )
00181 );
00182 Info<< "Determined cells to refine in = "
00183 << mesh.time().cpuTimeIncrement() << " s" << endl;
00184
00185
00186
00187 label nCellsToRefine = cellsToRefine.size();
00188 reduce(nCellsToRefine, sumOp<label>());
00189
00190 Info<< "Selected for feature refinement : " << nCellsToRefine
00191 << " cells (out of " << mesh.globalData().nTotalCells()
00192 << ')' << endl;
00193
00194 if (nCellsToRefine <= minRefine)
00195 {
00196 Info<< "Stopping refining since too few cells selected."
00197 << nl << endl;
00198 break;
00199 }
00200
00201
00202 if (debug > 0)
00203 {
00204 const_cast<Time&>(mesh.time())++;
00205 }
00206
00207 meshRefiner_.refineAndBalance
00208 (
00209 "feature refinement iteration " + name(iter),
00210 decomposer_,
00211 distributor_,
00212 cellsToRefine
00213 );
00214 }
00215 }
00216 return iter;
00217 }
00218
00219
00220 Foam::label Foam::autoRefineDriver::surfaceOnlyRefine
00221 (
00222 const refinementParameters& refineParams,
00223 const label maxIter
00224 )
00225 {
00226 const fvMesh& mesh = meshRefiner_.mesh();
00227
00228
00229
00230 label overallMaxLevel = max(meshRefiner_.surfaces().maxLevel());
00231
00232 label iter;
00233 for (iter = 0; iter < maxIter; iter++)
00234 {
00235 Info<< nl
00236 << "Surface refinement iteration " << iter << nl
00237 << "------------------------------" << nl
00238 << endl;
00239
00240
00241
00242
00243
00244
00245
00246 const PtrList<featureEdgeMesh> dummyFeatures;
00247
00248 labelList candidateCells
00249 (
00250 meshRefiner_.refineCandidates
00251 (
00252 refineParams.keepPoints()[0],
00253 refineParams.curvature(),
00254
00255 dummyFeatures,
00256 labelList(0),
00257
00258 false,
00259 false,
00260 true,
00261 true,
00262 refineParams.maxGlobalCells(),
00263 refineParams.maxLocalCells()
00264 )
00265 );
00266 labelList cellsToRefine
00267 (
00268 meshRefiner_.meshCutter().consistentRefinement
00269 (
00270 candidateCells,
00271 true
00272 )
00273 );
00274 Info<< "Determined cells to refine in = "
00275 << mesh.time().cpuTimeIncrement() << " s" << endl;
00276
00277
00278 label nCellsToRefine = cellsToRefine.size();
00279 reduce(nCellsToRefine, sumOp<label>());
00280
00281 Info<< "Selected for refinement : " << nCellsToRefine
00282 << " cells (out of " << mesh.globalData().nTotalCells()
00283 << ')' << endl;
00284
00285
00286
00287 if
00288 (
00289 nCellsToRefine == 0
00290 || (
00291 iter >= overallMaxLevel
00292 && nCellsToRefine <= refineParams.minRefineCells()
00293 )
00294 )
00295 {
00296 Info<< "Stopping refining since too few cells selected."
00297 << nl << endl;
00298 break;
00299 }
00300
00301
00302 if (debug)
00303 {
00304 const_cast<Time&>(mesh.time())++;
00305 }
00306
00307 meshRefiner_.refineAndBalance
00308 (
00309 "surface refinement iteration " + name(iter),
00310 decomposer_,
00311 distributor_,
00312 cellsToRefine
00313 );
00314 }
00315 return iter;
00316 }
00317
00318
00319 void Foam::autoRefineDriver::removeInsideCells
00320 (
00321 const refinementParameters& refineParams,
00322 const label nBufferLayers
00323 )
00324 {
00325 Info<< nl
00326 << "Removing mesh beyond surface intersections" << nl
00327 << "------------------------------------------" << nl
00328 << endl;
00329
00330 const fvMesh& mesh = meshRefiner_.mesh();
00331
00332 if (debug)
00333 {
00334 const_cast<Time&>(mesh.time())++;
00335 }
00336
00337 meshRefiner_.splitMesh
00338 (
00339 nBufferLayers,
00340 globalToPatch_,
00341 refineParams.keepPoints()[0]
00342 );
00343
00344 if (debug)
00345 {
00346 Pout<< "Writing subsetted mesh to time "
00347 << meshRefiner_.timeName() << '.' << endl;
00348 meshRefiner_.write(debug, mesh.time().path()/meshRefiner_.timeName());
00349 Pout<< "Dumped mesh in = "
00350 << mesh.time().cpuTimeIncrement() << " s\n" << nl << endl;
00351 }
00352 }
00353
00354
00355 Foam::label Foam::autoRefineDriver::shellRefine
00356 (
00357 const refinementParameters& refineParams,
00358 const label maxIter
00359 )
00360 {
00361 const fvMesh& mesh = meshRefiner_.mesh();
00362
00363
00364 meshRefiner_.userFaceData().setSize(1);
00365
00366
00367 meshRefiner_.userFaceData()[0].first() = meshRefinement::REMOVE;
00368 meshRefiner_.userFaceData()[0].second() = createWithValues<labelList>
00369 (
00370 mesh.nFaces(),
00371 -1,
00372 meshRefiner_.intersectedFaces(),
00373 0
00374 );
00375
00376
00377
00378
00379 label overallMaxShellLevel = meshRefiner_.shells().maxLevel();
00380
00381 label iter;
00382 for (iter = 0; iter < maxIter; iter++)
00383 {
00384 Info<< nl
00385 << "Shell refinement iteration " << iter << nl
00386 << "----------------------------" << nl
00387 << endl;
00388
00389 const PtrList<featureEdgeMesh> dummyFeatures;
00390
00391 labelList candidateCells
00392 (
00393 meshRefiner_.refineCandidates
00394 (
00395 refineParams.keepPoints()[0],
00396 refineParams.curvature(),
00397
00398 dummyFeatures,
00399 labelList(0),
00400
00401 false,
00402 true,
00403 false,
00404 false,
00405 refineParams.maxGlobalCells(),
00406 refineParams.maxLocalCells()
00407 )
00408 );
00409
00410 if (debug)
00411 {
00412 Pout<< "Dumping " << candidateCells.size()
00413 << " cells to cellSet candidateCellsFromShells." << endl;
00414
00415 cellSet(mesh, "candidateCellsFromShells", candidateCells).write();
00416 }
00417
00418
00419
00420
00421
00422
00423
00424
00425 labelList bFaces
00426 (
00427 findIndices(meshRefiner_.userFaceData()[0].second(), 0)
00428 );
00429
00430
00431
00432
00433 labelList cellsToRefine;
00434
00435 if (refineParams.nBufferLayers() <= 2)
00436 {
00437 cellsToRefine = meshRefiner_.meshCutter().consistentSlowRefinement
00438 (
00439 refineParams.nBufferLayers(),
00440 candidateCells,
00441 bFaces,
00442 1,
00443 meshRefiner_.intersectedPoints()
00444 );
00445 }
00446 else
00447 {
00448 cellsToRefine = meshRefiner_.meshCutter().consistentSlowRefinement2
00449 (
00450 refineParams.nBufferLayers(),
00451 candidateCells,
00452 bFaces
00453 );
00454 }
00455
00456 Info<< "Determined cells to refine in = "
00457 << mesh.time().cpuTimeIncrement() << " s" << endl;
00458
00459
00460 label nCellsToRefine = cellsToRefine.size();
00461 reduce(nCellsToRefine, sumOp<label>());
00462
00463 Info<< "Selected for internal refinement : " << nCellsToRefine
00464 << " cells (out of " << mesh.globalData().nTotalCells()
00465 << ')' << endl;
00466
00467
00468
00469 if
00470 (
00471 nCellsToRefine == 0
00472 || (
00473 iter >= overallMaxShellLevel
00474 && nCellsToRefine <= refineParams.minRefineCells()
00475 )
00476 )
00477 {
00478 Info<< "Stopping refining since too few cells selected."
00479 << nl << endl;
00480 break;
00481 }
00482
00483
00484 if (debug)
00485 {
00486 const_cast<Time&>(mesh.time())++;
00487 }
00488
00489 meshRefiner_.refineAndBalance
00490 (
00491 "shell refinement iteration " + name(iter),
00492 decomposer_,
00493 distributor_,
00494 cellsToRefine
00495 );
00496 }
00497 meshRefiner_.userFaceData().clear();
00498
00499 return iter;
00500 }
00501
00502
00503 void Foam::autoRefineDriver::baffleAndSplitMesh
00504 (
00505 const refinementParameters& refineParams,
00506 const bool handleSnapProblems,
00507 const dictionary& motionDict
00508 )
00509 {
00510 Info<< nl
00511 << "Splitting mesh at surface intersections" << nl
00512 << "---------------------------------------" << nl
00513 << endl;
00514
00515 const fvMesh& mesh = meshRefiner_.mesh();
00516
00517
00518
00519
00520 meshRefiner_.baffleAndSplitMesh
00521 (
00522 handleSnapProblems,
00523 false,
00524 scalarField(0),
00525 !handleSnapProblems,
00526 motionDict,
00527 const_cast<Time&>(mesh.time()),
00528 globalToPatch_,
00529 refineParams.keepPoints()[0]
00530 );
00531 }
00532
00533
00534 void Foam::autoRefineDriver::zonify
00535 (
00536 const refinementParameters& refineParams
00537 )
00538 {
00539
00540
00541
00542
00543
00544
00545 if (meshRefiner_.surfaces().getNamedSurfaces().size())
00546 {
00547 Info<< nl
00548 << "Introducing zones for interfaces" << nl
00549 << "--------------------------------" << nl
00550 << endl;
00551
00552 const fvMesh& mesh = meshRefiner_.mesh();
00553
00554 if (debug)
00555 {
00556 const_cast<Time&>(mesh.time())++;
00557 }
00558
00559 meshRefiner_.zonify(refineParams.keepPoints()[0]);
00560
00561 if (debug)
00562 {
00563 Pout<< "Writing zoned mesh to time "
00564 << meshRefiner_.timeName() << '.' << endl;
00565 meshRefiner_.write
00566 (
00567 debug,
00568 mesh.time().path()/meshRefiner_.timeName()
00569 );
00570 }
00571
00572
00573 meshRefinement::checkCoupledFaceZones(mesh);
00574 }
00575 }
00576
00577
00578 void Foam::autoRefineDriver::splitAndMergeBaffles
00579 (
00580 const refinementParameters& refineParams,
00581 const bool handleSnapProblems,
00582 const dictionary& motionDict
00583 )
00584 {
00585 Info<< nl
00586 << "Handling cells with snap problems" << nl
00587 << "---------------------------------" << nl
00588 << endl;
00589
00590 const fvMesh& mesh = meshRefiner_.mesh();
00591
00592
00593 if (debug)
00594 {
00595 const_cast<Time&>(mesh.time())++;
00596 }
00597
00598 const scalarField& perpAngle = meshRefiner_.surfaces().perpendicularAngle();
00599
00600 meshRefiner_.baffleAndSplitMesh
00601 (
00602 handleSnapProblems,
00603 handleSnapProblems,
00604 perpAngle,
00605 false,
00606 motionDict,
00607 const_cast<Time&>(mesh.time()),
00608 globalToPatch_,
00609 refineParams.keepPoints()[0]
00610 );
00611
00612 if (debug)
00613 {
00614 const_cast<Time&>(mesh.time())++;
00615 }
00616
00617
00618
00619 meshRefiner_.dupNonManifoldPoints();
00620
00621
00622
00623 List<labelPair> couples
00624 (
00625 meshRefiner_.getDuplicateFaces
00626 (
00627 identity(mesh.nFaces()-mesh.nInternalFaces())
00628 + mesh.nInternalFaces()
00629 )
00630 );
00631
00632 label nCouples = returnReduce(couples.size(), sumOp<label>());
00633
00634 Info<< "Detected unsplittable baffles : "
00635 << nCouples << endl;
00636
00637 if (nCouples > 0)
00638 {
00639
00640
00641
00642 meshRefiner_.mergeBaffles(couples);
00643
00644 if (debug)
00645 {
00646
00647 meshRefiner_.checkData();
00648 }
00649 Info<< "Merged free-standing baffles in = "
00650 << mesh.time().cpuTimeIncrement() << " s." << endl;
00651 }
00652
00653 if (debug)
00654 {
00655 Pout<< "Writing handleProblemCells mesh to time "
00656 << meshRefiner_.timeName() << '.' << endl;
00657 meshRefiner_.write(debug, mesh.time().path()/meshRefiner_.timeName());
00658 }
00659 }
00660
00661
00662 void Foam::autoRefineDriver::mergePatchFaces
00663 (
00664 const refinementParameters& refineParams
00665 )
00666 {
00667 const fvMesh& mesh = meshRefiner_.mesh();
00668
00669 Info<< nl
00670 << "Merge refined boundary faces" << nl
00671 << "----------------------------" << nl
00672 << endl;
00673
00674 if (debug)
00675 {
00676 const_cast<Time&>(mesh.time())++;
00677 }
00678
00679 meshRefiner_.mergePatchFaces
00680 (
00681 Foam::cos(45*mathematicalConstant::pi/180.0),
00682 Foam::cos(45*mathematicalConstant::pi/180.0),
00683 meshRefiner_.meshedPatches()
00684 );
00685
00686 if (debug)
00687 {
00688 meshRefiner_.checkData();
00689 }
00690
00691 meshRefiner_.mergeEdges(Foam::cos(45*mathematicalConstant::pi/180.0));
00692
00693 if (debug)
00694 {
00695 meshRefiner_.checkData();
00696 }
00697 }
00698
00699
00700 void Foam::autoRefineDriver::doRefine
00701 (
00702 const dictionary& refineDict,
00703 const refinementParameters& refineParams,
00704 const bool prepareForSnapping,
00705 const dictionary& motionDict
00706 )
00707 {
00708 Info<< nl
00709 << "Refinement phase" << nl
00710 << "----------------" << nl
00711 << endl;
00712
00713 const fvMesh& mesh = meshRefiner_.mesh();
00714
00715
00716 refineParams.findCells(mesh);
00717
00718 PtrList<dictionary> featDicts(refineDict.lookup("features"));
00719
00720
00721 featureEdgeRefine
00722 (
00723 refineParams,
00724 featDicts,
00725 100,
00726 0
00727 );
00728
00729
00730 surfaceOnlyRefine
00731 (
00732 refineParams,
00733 100
00734 );
00735
00736
00737 removeInsideCells
00738 (
00739 refineParams,
00740 1
00741 );
00742
00743
00744 shellRefine
00745 (
00746 refineParams,
00747 100
00748 );
00749
00750
00751 baffleAndSplitMesh(refineParams, prepareForSnapping, motionDict);
00752
00753
00754 zonify(refineParams);
00755
00756
00757 splitAndMergeBaffles(refineParams, prepareForSnapping, motionDict);
00758
00759
00760 if (prepareForSnapping)
00761 {
00762 mergePatchFaces(refineParams);
00763 }
00764
00765
00766 if (Pstream::parRun())
00767 {
00768 Info<< nl
00769 << "Doing final balancing" << nl
00770 << "---------------------" << nl
00771 << endl;
00772
00773 if (debug)
00774 {
00775 const_cast<Time&>(mesh.time())++;
00776 }
00777
00778
00779 meshRefiner_.balance
00780 (
00781 true,
00782 false,
00783 decomposer_,
00784 distributor_
00785 );
00786 }
00787 }
00788
00789
00790