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 #include "argList.H"
00043 #include "Time.H"
00044 #include "polyTopoChange.H"
00045 #include "polyTopoChanger.H"
00046 #include "mapPolyMesh.H"
00047 #include "polyMesh.H"
00048 #include "cellCuts.H"
00049 #include "cellSet.H"
00050 #include "cellModeller.H"
00051 #include "meshCutter.H"
00052 #include "mathematicalConstants.H"
00053 #include "geomCellLooper.H"
00054 #include "plane.H"
00055 #include "edgeVertex.H"
00056 #include "meshTools.H"
00057 #include "ListOps.H"
00058
00059 using namespace Foam;
00060
00061
00062
00063
00064 labelList pack(const boolList& lst)
00065 {
00066 labelList packedLst(lst.size());
00067 label packedI = 0;
00068
00069 forAll(lst, i)
00070 {
00071 if (lst[i])
00072 {
00073 packedLst[packedI++] = i;
00074 }
00075 }
00076 packedLst.setSize(packedI);
00077
00078 return packedLst;
00079 }
00080
00081
00082 scalarField pack(const boolList& lst, const scalarField& elems)
00083 {
00084 scalarField packedElems(lst.size());
00085 label packedI = 0;
00086
00087 forAll(lst, i)
00088 {
00089 if (lst[i])
00090 {
00091 packedElems[packedI++] = elems[i];
00092 }
00093 }
00094 packedElems.setSize(packedI);
00095
00096 return packedElems;
00097 }
00098
00099
00100
00101
00102 bool largerAngle
00103 (
00104 const primitiveMesh& mesh,
00105 const scalar cosAngle,
00106 const scalar sinAngle,
00107
00108 const label cellI,
00109 const label f0,
00110 const label f1,
00111
00112 const vector& n0,
00113 const vector& n1
00114 )
00115 {
00116 const labelList& own = mesh.faceOwner();
00117
00118 bool sameFaceOrder = !((own[f0] == cellI) ^ (own[f1] == cellI));
00119
00120
00121
00122 scalar normalCosAngle = n0 & n1;
00123
00124 if (sameFaceOrder)
00125 {
00126 normalCosAngle = -normalCosAngle;
00127 }
00128
00129
00130
00131
00132
00133 vector c1c0(mesh.faceCentres()[f1] - mesh.faceCentres()[f0]);
00134 c1c0 /= mag(c1c0) + VSMALL;
00135
00136 scalar fcCosAngle = n0 & c1c0;
00137
00138 if (own[f0] != cellI)
00139 {
00140 fcCosAngle = -fcCosAngle;
00141 }
00142
00143 if (sinAngle < 0.0)
00144 {
00145
00146 if (fcCosAngle <= 0)
00147 {
00148
00149 return false;
00150 }
00151 else
00152 {
00153 if (normalCosAngle < cosAngle)
00154 {
00155 return false;
00156 }
00157 else
00158 {
00159 return true;
00160 }
00161 }
00162 }
00163 else
00164 {
00165
00166 if (fcCosAngle > 0)
00167 {
00168
00169 return true;
00170 }
00171 else
00172 {
00173
00174 if (normalCosAngle > cosAngle)
00175 {
00176 return false;
00177 }
00178 else
00179 {
00180 return true;
00181 }
00182 }
00183 }
00184 }
00185
00186
00187
00188 bool splitHex
00189 (
00190 const polyMesh& mesh,
00191 const label cellI,
00192 const label edgeI,
00193
00194 DynamicList<label>& cutCells,
00195 DynamicList<labelList>& cellLoops,
00196 DynamicList<scalarField>& cellEdgeWeights
00197 )
00198 {
00199
00200 edgeVertex ev(mesh);
00201
00202 const edgeList& edges = mesh.edges();
00203 const faceList& faces = mesh.faces();
00204
00205 const edge& e = edges[edgeI];
00206
00207
00208
00209
00210 label leftI = -1;
00211 label rightI = -1;
00212 label leftFp = -1;
00213 label rightFp = -1;
00214
00215 const cell& cFaces = mesh.cells()[cellI];
00216
00217 forAll(cFaces, i)
00218 {
00219 label faceI = cFaces[i];
00220
00221 const face& f = faces[faceI];
00222
00223 label fp0 = findIndex(f, e[0]);
00224 label fp1 = findIndex(f, e[1]);
00225
00226 if (fp0 == -1)
00227 {
00228 if (fp1 != -1)
00229 {
00230
00231 rightI = faceI;
00232 rightFp = fp1;
00233
00234 if (leftI != -1)
00235 {
00236
00237 break;
00238 }
00239 }
00240 }
00241 else
00242 {
00243 if (fp1 != -1)
00244 {
00245
00246 }
00247 else
00248 {
00249 leftI = faceI;
00250 leftFp = fp0;
00251
00252 if (rightI != -1)
00253 {
00254 break;
00255 }
00256 }
00257 }
00258 }
00259
00260 if (leftI == -1 || rightI == -1)
00261 {
00262 FatalErrorIn("splitHex") << "Problem : leftI:" << leftI
00263 << " rightI:" << rightI << abort(FatalError);
00264 }
00265
00266
00267
00268 const face& leftF = faces[leftI];
00269
00270 label leftV = leftF[(leftFp + 2) % leftF.size()];
00271
00272 const face& rightF = faces[rightI];
00273
00274 label rightV = rightF[(rightFp + 2) % rightF.size()];
00275
00276 labelList loop(4);
00277 loop[0] = ev.vertToEVert(e[0]);
00278 loop[1] = ev.vertToEVert(leftV);
00279 loop[2] = ev.vertToEVert(rightV);
00280 loop[3] = ev.vertToEVert(e[1]);
00281
00282 scalarField loopWeights(4);
00283 loopWeights[0] = -GREAT;
00284 loopWeights[1] = -GREAT;
00285 loopWeights[2] = -GREAT;
00286 loopWeights[3] = -GREAT;
00287
00288 cutCells.append(cellI);
00289 cellLoops.append(loop);
00290 cellEdgeWeights.append(loopWeights);
00291
00292 return true;
00293 }
00294
00295
00296
00297 bool splitCell
00298 (
00299 const polyMesh& mesh,
00300 const geomCellLooper& cellCutter,
00301
00302 const label cellI,
00303 const label edgeI,
00304 const vector& halfNorm,
00305
00306 const boolList& vertIsCut,
00307 const boolList& edgeIsCut,
00308 const scalarField& edgeWeight,
00309
00310 DynamicList<label>& cutCells,
00311 DynamicList<labelList>& cellLoops,
00312 DynamicList<scalarField>& cellEdgeWeights
00313 )
00314 {
00315 const edge& e = mesh.edges()[edgeI];
00316
00317 vector eVec = e.vec(mesh.points());
00318 eVec /= mag(eVec);
00319
00320 vector planeN = eVec ^ halfNorm;
00321
00322
00323
00324
00325 planeN += 0.01*halfNorm;
00326
00327 planeN /= mag(planeN);
00328
00329
00330 plane cutPlane(mesh.points()[e.start()], planeN);
00331
00332 labelList loop;
00333 scalarField loopWeights;
00334
00335 if
00336 (
00337 cellCutter.cut
00338 (
00339 cutPlane,
00340 cellI,
00341 vertIsCut,
00342 edgeIsCut,
00343 edgeWeight,
00344 loop,
00345 loopWeights
00346 )
00347 )
00348 {
00349
00350 cutCells.append(cellI);
00351 cellLoops.append(loop);
00352 cellEdgeWeights.append(loopWeights);
00353
00354 return true;
00355 }
00356 else
00357 {
00358 return false;
00359 }
00360 }
00361
00362
00363
00364 void collectCuts
00365 (
00366 const polyMesh& mesh,
00367 const geomCellLooper& cellCutter,
00368 const bool geometry,
00369 const scalar minCos,
00370 const scalar minSin,
00371 const cellSet& cellsToCut,
00372
00373 DynamicList<label>& cutCells,
00374 DynamicList<labelList>& cellLoops,
00375 DynamicList<scalarField>& cellEdgeWeights
00376 )
00377 {
00378
00379 const cellShapeList& cellShapes = mesh.cellShapes();
00380 const labelList& own = mesh.faceOwner();
00381 const labelListList& cellEdges = mesh.cellEdges();
00382 const vectorField& faceAreas = mesh.faceAreas();
00383
00384
00385 const cellModel& hex = *(cellModeller::lookup("hex"));
00386
00387
00388 edgeVertex ev(mesh);
00389
00390
00391
00392 boolList vertIsCut(mesh.nPoints(), false);
00393 boolList edgeIsCut(mesh.nEdges(), false);
00394 scalarField edgeWeight(mesh.nEdges(), -GREAT);
00395
00396 for
00397 (
00398 cellSet::const_iterator iter = cellsToCut.begin();
00399 iter != cellsToCut.end();
00400 ++iter
00401 )
00402 {
00403 label cellI = iter.key();
00404
00405 const labelList& cEdges = cellEdges[cellI];
00406
00407 forAll(cEdges, i)
00408 {
00409 label edgeI = cEdges[i];
00410
00411 label f0, f1;
00412 meshTools::getEdgeFaces(mesh, cellI, edgeI, f0, f1);
00413
00414 vector n0 = faceAreas[f0];
00415 n0 /= mag(n0);
00416
00417 vector n1 = faceAreas[f1];
00418 n1 /= mag(n1);
00419
00420 if
00421 (
00422 largerAngle
00423 (
00424 mesh,
00425 minCos,
00426 minSin,
00427
00428 cellI,
00429 f0,
00430 f1,
00431 n0,
00432 n1
00433 )
00434 )
00435 {
00436 bool splitOk = false;
00437
00438 if (!geometry && cellShapes[cellI].model() == hex)
00439 {
00440 splitOk =
00441 splitHex
00442 (
00443 mesh,
00444 cellI,
00445 edgeI,
00446
00447 cutCells,
00448 cellLoops,
00449 cellEdgeWeights
00450 );
00451 }
00452 else
00453 {
00454 vector halfNorm;
00455
00456 if ((own[f0] == cellI) ^ (own[f1] == cellI))
00457 {
00458
00459 halfNorm = 0.5*(n0 - n1);
00460 }
00461 else
00462 {
00463
00464
00465 halfNorm = 0.5*(n0 + n1);
00466 }
00467
00468 splitOk =
00469 splitCell
00470 (
00471 mesh,
00472 cellCutter,
00473 cellI,
00474 edgeI,
00475 halfNorm,
00476
00477 vertIsCut,
00478 edgeIsCut,
00479 edgeWeight,
00480
00481 cutCells,
00482 cellLoops,
00483 cellEdgeWeights
00484 );
00485 }
00486
00487
00488 if (splitOk)
00489 {
00490
00491 label index = cellLoops.size() - 1;
00492 const labelList& loop = cellLoops[index];
00493 const scalarField& loopWeights = cellEdgeWeights[index];
00494
00495 forAll(loop, i)
00496 {
00497 label cut = loop[i];
00498
00499 if (ev.isEdge(cut))
00500 {
00501 edgeIsCut[ev.getEdge(cut)] = true;
00502 edgeWeight[ev.getEdge(cut)] = loopWeights[i];
00503 }
00504 else
00505 {
00506 vertIsCut[ev.getVertex(cut)] = true;
00507 }
00508 }
00509
00510
00511 break;
00512 }
00513 }
00514 }
00515 }
00516
00517 cutCells.shrink();
00518 cellLoops.shrink();
00519 cellEdgeWeights.shrink();
00520 }
00521
00522
00523
00524
00525 int main(int argc, char *argv[])
00526 {
00527 argList::noParallel();
00528 argList::validOptions.insert("set", "cellSet name");
00529 argList::validOptions.insert("geometry", "");
00530 argList::validOptions.insert("tol", "edge snap tolerance");
00531 argList::validOptions.insert("overwrite", "");
00532 argList::validArgs.append("edge angle [0..360]");
00533
00534 # include "setRootCase.H"
00535 # include "createTime.H"
00536 runTime.functionObjects().off();
00537 # include "createPolyMesh.H"
00538 const word oldInstance = mesh.pointsInstance();
00539
00540 scalar featureAngle(readScalar(IStringStream(args.additionalArgs()[0])()));
00541
00542 scalar radAngle = featureAngle * mathematicalConstant::pi/180.0;
00543 scalar minCos = Foam::cos(radAngle);
00544 scalar minSin = Foam::sin(radAngle);
00545
00546 bool readSet = args.optionFound("set");
00547 bool geometry = args.optionFound("geometry");
00548 bool overwrite = args.optionFound("overwrite");
00549
00550 scalar edgeTol = 0.2;
00551 args.optionReadIfPresent("tol", edgeTol);
00552
00553 Info<< "Trying to split cells with internal angles > feature angle\n" << nl
00554 << "featureAngle : " << featureAngle << nl
00555 << "edge snapping tol : " << edgeTol << nl;
00556 if (readSet)
00557 {
00558 Info<< "candidate cells : cellSet " << args.option("set") << nl;
00559 }
00560 else
00561 {
00562 Info<< "candidate cells : all cells" << nl;
00563 }
00564 if (geometry)
00565 {
00566 Info<< "hex cuts : geometric; using edge tolerance" << nl;
00567 }
00568 else
00569 {
00570 Info<< "hex cuts : topological; cut to opposite edge" << nl;
00571 }
00572 Info<< endl;
00573
00574
00575
00576 geomCellLooper cellCutter(mesh);
00577
00578 geomCellLooper::setSnapTol(edgeTol);
00579
00580
00581 cellSet cellsToCut(mesh, "cellsToCut", mesh.nCells()/100);
00582
00583 if (readSet)
00584 {
00585
00586 cellSet cells(mesh, args.option("set"));
00587
00588 cellsToCut = cells;
00589 }
00590
00591 while (true)
00592 {
00593 if (!readSet)
00594 {
00595
00596 for (label cellI = 0; cellI < mesh.nCells(); cellI++)
00597 {
00598 cellsToCut.insert(cellI);
00599 }
00600 }
00601
00602
00603
00604 DynamicList<label> cutCells(mesh.nCells()/10 + 10);
00605 DynamicList<labelList> cellLoops(mesh.nCells()/10 + 10);
00606 DynamicList<scalarField> cellEdgeWeights(mesh.nCells()/10 + 10);
00607
00608 collectCuts
00609 (
00610 mesh,
00611 cellCutter,
00612 geometry,
00613 minCos,
00614 minSin,
00615 cellsToCut,
00616
00617 cutCells,
00618 cellLoops,
00619 cellEdgeWeights
00620 );
00621
00622 cellSet cutSet(mesh, "cutSet", cutCells.size());
00623 forAll(cutCells, i)
00624 {
00625 cutSet.insert(cutCells[i]);
00626 }
00627
00628
00629 Info<< "Writing " << cutSet.size() << " cells to cut to cellSet "
00630 << cutSet.instance()/cutSet.local()/cutSet.name()
00631 << endl << endl;
00632 cutSet.write();
00633
00634
00635 cellCuts cuts
00636 (
00637 mesh,
00638 cutCells,
00639 cellLoops,
00640 cellEdgeWeights
00641 );
00642
00643 Info<< "Actually cut cells:" << cuts.nLoops() << nl << endl;
00644
00645 if (cuts.nLoops() == 0)
00646 {
00647 break;
00648 }
00649
00650
00651 forAll(cuts.cellLoops(), cellI)
00652 {
00653 if (cuts.cellLoops()[cellI].size())
00654 {
00655
00656
00657 cellsToCut.erase(cellI);
00658 }
00659 }
00660
00661
00662 polyTopoChange meshMod(mesh);
00663
00664
00665 meshCutter cutter(mesh);
00666
00667
00668 cutter.setRefinement(cuts, meshMod);
00669
00670
00671 Info<< "Morphing ..." << endl;
00672
00673 if (!overwrite)
00674 {
00675 runTime++;
00676 }
00677
00678 autoPtr<mapPolyMesh> morphMap = meshMod.changeMesh(mesh, false);
00679
00680 if (morphMap().hasMotionPoints())
00681 {
00682 mesh.movePoints(morphMap().preMotionPoints());
00683 }
00684
00685
00686 cutter.updateMesh(morphMap());
00687
00688
00689 cellsToCut.updateMesh(morphMap());
00690
00691 Info<< "Remaining:" << cellsToCut.size() << endl;
00692
00693
00694 if (overwrite)
00695 {
00696 mesh.setInstance(oldInstance);
00697 }
00698
00699 Info<< "Writing refined morphMesh to time " << runTime.timeName()
00700 << endl;
00701
00702 mesh.write();
00703 }
00704
00705 Info << "End\n" << endl;
00706
00707 return 0;
00708 }
00709
00710
00711