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 #include "cyclicPolyPatch.H"
00038 #include "syncTools.H"
00039 #include "argList.H"
00040 #include "polyMesh.H"
00041 #include "Time.H"
00042 #include "SortableList.H"
00043 #include "OFstream.H"
00044 #include "meshTools.H"
00045 #include "faceSet.H"
00046 #include "IOPtrList.H"
00047 #include "polyTopoChange.H"
00048 #include "polyModifyFace.H"
00049
00050 using namespace Foam;
00051
00052
00053
00054 namespace Foam
00055 {
00056 defineTemplateTypeNameAndDebug(IOPtrList<dictionary>, 0);
00057 }
00058
00059
00060
00061 class nearestEqOp
00062 {
00063
00064 public:
00065
00066 void operator()(vector& x, const vector& y) const
00067 {
00068 if (magSqr(y) < magSqr(x))
00069 {
00070 x = y;
00071 }
00072 }
00073 };
00074
00075
00076 void changePatchID
00077 (
00078 const polyMesh& mesh,
00079 const label faceID,
00080 const label patchID,
00081 polyTopoChange& meshMod
00082 )
00083 {
00084 const label zoneID = mesh.faceZones().whichZone(faceID);
00085
00086 bool zoneFlip = false;
00087
00088 if (zoneID >= 0)
00089 {
00090 const faceZone& fZone = mesh.faceZones()[zoneID];
00091
00092 zoneFlip = fZone.flipMap()[fZone.whichFace(faceID)];
00093 }
00094
00095 meshMod.setAction
00096 (
00097 polyModifyFace
00098 (
00099 mesh.faces()[faceID],
00100 faceID,
00101 mesh.faceOwner()[faceID],
00102 -1,
00103 false,
00104 patchID,
00105 false,
00106 zoneID,
00107 zoneFlip
00108 )
00109 );
00110 }
00111
00112
00113
00114 void filterPatches(polyMesh& mesh)
00115 {
00116 const polyBoundaryMesh& patches = mesh.boundaryMesh();
00117
00118
00119 DynamicList<polyPatch*> allPatches(patches.size());
00120
00121 label nOldPatches = returnReduce(patches.size(), sumOp<label>());
00122
00123
00124 forAll(patches, patchI)
00125 {
00126 const polyPatch& pp = patches[patchI];
00127
00128
00129 if (!isA<processorPolyPatch>(pp))
00130 {
00131 if (returnReduce(pp.size(), sumOp<label>()) > 0)
00132 {
00133 allPatches.append
00134 (
00135 pp.clone
00136 (
00137 patches,
00138 allPatches.size(),
00139 pp.size(),
00140 pp.start()
00141 ).ptr()
00142 );
00143 }
00144 else
00145 {
00146 Info<< "Removing empty patch " << pp.name() << " at position "
00147 << patchI << endl;
00148 }
00149 }
00150 }
00151
00152 forAll(patches, patchI)
00153 {
00154 const polyPatch& pp = patches[patchI];
00155
00156 if (isA<processorPolyPatch>(pp))
00157 {
00158 if (pp.size())
00159 {
00160 allPatches.append
00161 (
00162 pp.clone
00163 (
00164 patches,
00165 allPatches.size(),
00166 pp.size(),
00167 pp.start()
00168 ).ptr()
00169 );
00170 }
00171 else
00172 {
00173 Info<< "Removing empty processor patch " << pp.name()
00174 << " at position " << patchI << endl;
00175 }
00176 }
00177 }
00178
00179 label nAllPatches = returnReduce(allPatches.size(), sumOp<label>());
00180 if (nAllPatches != nOldPatches)
00181 {
00182 Info<< "Removing patches." << endl;
00183 allPatches.shrink();
00184 mesh.removeBoundary();
00185 mesh.addPatches(allPatches);
00186 }
00187 else
00188 {
00189 Info<< "No patches removed." << endl;
00190 }
00191 }
00192
00193
00194
00195 void dumpCyclicMatch(const fileName& prefix, const polyMesh& mesh)
00196 {
00197 const polyBoundaryMesh& patches = mesh.boundaryMesh();
00198
00199 forAll(patches, patchI)
00200 {
00201 if (isA<cyclicPolyPatch>(patches[patchI]))
00202 {
00203 const cyclicPolyPatch& cycPatch =
00204 refCast<const cyclicPolyPatch>(patches[patchI]);
00205
00206 label halfSize = cycPatch.size()/2;
00207
00208
00209 {
00210 OFstream str(prefix+cycPatch.name()+"_half0.obj");
00211 Pout<< "Dumping cycPatch.name() half0 faces to " << str.name()
00212 << endl;
00213 meshTools::writeOBJ
00214 (
00215 str,
00216 static_cast<faceList>
00217 (
00218 SubList<face>
00219 (
00220 cycPatch,
00221 halfSize
00222 )
00223 ),
00224 cycPatch.points()
00225 );
00226 }
00227 {
00228 OFstream str(prefix+cycPatch.name()+"_half1.obj");
00229 Pout<< "Dumping cycPatch.name() half1 faces to " << str.name()
00230 << endl;
00231 meshTools::writeOBJ
00232 (
00233 str,
00234 static_cast<faceList>
00235 (
00236 SubList<face>
00237 (
00238 cycPatch,
00239 halfSize,
00240 halfSize
00241 )
00242 ),
00243 cycPatch.points()
00244 );
00245 }
00246
00247
00248
00249 OFstream str(prefix+cycPatch.name()+"_match.obj");
00250 label vertI = 0;
00251
00252 Pout<< "Dumping cyclic match as lines between face centres to "
00253 << str.name() << endl;
00254
00255 for (label faceI = 0; faceI < halfSize; faceI++)
00256 {
00257 const point& fc0 = mesh.faceCentres()[cycPatch.start()+faceI];
00258 meshTools::writeOBJ(str, fc0);
00259 vertI++;
00260
00261 label nbrFaceI = halfSize + faceI;
00262 const point& fc1 =
00263 mesh.faceCentres()[cycPatch.start()+nbrFaceI];
00264 meshTools::writeOBJ(str, fc1);
00265 vertI++;
00266
00267 str<< "l " << vertI-1 << ' ' << vertI << nl;
00268 }
00269 }
00270 }
00271 }
00272
00273
00274 void separateList
00275 (
00276 const vectorField& separation,
00277 UList<vector>& field
00278 )
00279 {
00280 if (separation.size() == 1)
00281 {
00282
00283
00284 forAll(field, i)
00285 {
00286 field[i] += separation[0];
00287 }
00288 }
00289 else if (separation.size() == field.size())
00290 {
00291 forAll(field, i)
00292 {
00293 field[i] += separation[i];
00294 }
00295 }
00296 else
00297 {
00298 FatalErrorIn
00299 (
00300 "separateList(const vectorField&, UList<vector>&)"
00301 ) << "Sizes of field and transformation not equal. field:"
00302 << field.size() << " transformation:" << separation.size()
00303 << abort(FatalError);
00304 }
00305 }
00306
00307
00308
00309 template <class CombineOp>
00310 void syncPoints
00311 (
00312 const polyMesh& mesh,
00313 pointField& points,
00314 const CombineOp& cop,
00315 const point& nullValue
00316 )
00317 {
00318 if (points.size() != mesh.nPoints())
00319 {
00320 FatalErrorIn
00321 (
00322 "syncPoints"
00323 "(const polyMesh&, pointField&, const CombineOp&, const point&)"
00324 ) << "Number of values " << points.size()
00325 << " is not equal to the number of points in the mesh "
00326 << mesh.nPoints() << abort(FatalError);
00327 }
00328
00329 const polyBoundaryMesh& patches = mesh.boundaryMesh();
00330
00331
00332 bool hasTransformation = false;
00333
00334 if (Pstream::parRun())
00335 {
00336
00337
00338 forAll(patches, patchI)
00339 {
00340 const polyPatch& pp = patches[patchI];
00341
00342 if
00343 (
00344 isA<processorPolyPatch>(pp)
00345 && pp.nPoints() > 0
00346 && refCast<const processorPolyPatch>(pp).owner()
00347 )
00348 {
00349 const processorPolyPatch& procPatch =
00350 refCast<const processorPolyPatch>(pp);
00351
00352
00353 pointField patchInfo(procPatch.nPoints(), nullValue);
00354
00355 const labelList& meshPts = procPatch.meshPoints();
00356 const labelList& nbrPts = procPatch.neighbPoints();
00357
00358 forAll(nbrPts, pointI)
00359 {
00360 label nbrPointI = nbrPts[pointI];
00361 if (nbrPointI >= 0 && nbrPointI < patchInfo.size())
00362 {
00363 patchInfo[nbrPointI] = points[meshPts[pointI]];
00364 }
00365 }
00366
00367 OPstream toNbr(Pstream::blocking, procPatch.neighbProcNo());
00368 toNbr << patchInfo;
00369 }
00370 }
00371
00372
00373
00374
00375 forAll(patches, patchI)
00376 {
00377 const polyPatch& pp = patches[patchI];
00378
00379 if
00380 (
00381 isA<processorPolyPatch>(pp)
00382 && pp.nPoints() > 0
00383 && !refCast<const processorPolyPatch>(pp).owner()
00384 )
00385 {
00386 const processorPolyPatch& procPatch =
00387 refCast<const processorPolyPatch>(pp);
00388
00389 pointField nbrPatchInfo(procPatch.nPoints());
00390 {
00391
00392
00393 IPstream fromNbr
00394 (
00395 Pstream::blocking,
00396 procPatch.neighbProcNo()
00397 );
00398 fromNbr >> nbrPatchInfo;
00399 }
00400
00401 nbrPatchInfo.setSize(procPatch.nPoints(), nullValue);
00402
00403 if (!procPatch.parallel())
00404 {
00405 hasTransformation = true;
00406 transformList(procPatch.forwardT(), nbrPatchInfo);
00407 }
00408 else if (procPatch.separated())
00409 {
00410 hasTransformation = true;
00411 separateList(-procPatch.separation(), nbrPatchInfo);
00412 }
00413
00414 const labelList& meshPts = procPatch.meshPoints();
00415
00416 forAll(meshPts, pointI)
00417 {
00418 label meshPointI = meshPts[pointI];
00419 points[meshPointI] = nbrPatchInfo[pointI];
00420 }
00421 }
00422 }
00423 }
00424
00425
00426 forAll(patches, patchI)
00427 {
00428 const polyPatch& pp = patches[patchI];
00429
00430 if (isA<cyclicPolyPatch>(pp))
00431 {
00432 const cyclicPolyPatch& cycPatch =
00433 refCast<const cyclicPolyPatch>(pp);
00434
00435 const edgeList& coupledPoints = cycPatch.coupledPoints();
00436 const labelList& meshPts = cycPatch.meshPoints();
00437
00438 pointField half0Values(coupledPoints.size());
00439
00440 forAll(coupledPoints, i)
00441 {
00442 const edge& e = coupledPoints[i];
00443 label point0 = meshPts[e[0]];
00444 half0Values[i] = points[point0];
00445 }
00446
00447 if (!cycPatch.parallel())
00448 {
00449 hasTransformation = true;
00450 transformList(cycPatch.reverseT(), half0Values);
00451 }
00452 else if (cycPatch.separated())
00453 {
00454 hasTransformation = true;
00455 const vectorField& v = cycPatch.coupledPolyPatch::separation();
00456 separateList(v, half0Values);
00457 }
00458
00459 forAll(coupledPoints, i)
00460 {
00461 const edge& e = coupledPoints[i];
00462 label point1 = meshPts[e[1]];
00463 points[point1] = half0Values[i];
00464 }
00465 }
00466 }
00467
00468
00469
00470
00471
00472
00473 const globalMeshData& pd = mesh.globalData();
00474
00475 if (pd.nGlobalPoints() > 0)
00476 {
00477 if (hasTransformation)
00478 {
00479 WarningIn
00480 (
00481 "syncPoints"
00482 "(const polyMesh&, pointField&, const CombineOp&, const point&)"
00483 ) << "There are decomposed cyclics in this mesh with"
00484 << " transformations." << endl
00485 << "This is not supported. The result will be incorrect"
00486 << endl;
00487 }
00488
00489
00490
00491 pointField sharedPts(pd.nGlobalPoints(), nullValue);
00492
00493 forAll(pd.sharedPointLabels(), i)
00494 {
00495 label meshPointI = pd.sharedPointLabels()[i];
00496
00497 sharedPts[pd.sharedPointAddr()[i]] = points[meshPointI];
00498 }
00499
00500
00501 Pstream::listCombineGather(sharedPts, cop);
00502 Pstream::listCombineScatter(sharedPts);
00503
00504
00505
00506 forAll(pd.sharedPointLabels(), i)
00507 {
00508 label meshPointI = pd.sharedPointLabels()[i];
00509 points[meshPointI] = sharedPts[pd.sharedPointAddr()[i]];
00510 }
00511 }
00512 }
00513
00514
00515
00516
00517 int main(int argc, char *argv[])
00518 {
00519 argList::validOptions.insert("overwrite", "");
00520
00521 # include "setRootCase.H"
00522 # include "createTime.H"
00523 runTime.functionObjects().off();
00524
00525 const bool overwrite = args.optionFound("overwrite");
00526
00527 Info<< "Reading createPatchDict." << nl << endl;
00528
00529 IOdictionary dict
00530 (
00531 IOobject
00532 (
00533 "createPatchDict",
00534 runTime.system(),
00535 runTime,
00536 IOobject::MUST_READ,
00537 IOobject::NO_WRITE,
00538 false
00539 )
00540 );
00541
00542
00543
00544 const Switch pointSync(dict.lookup("pointSync"));
00545
00546
00547
00548 scalar tol = readScalar(dict.lookup("matchTolerance"));
00549 Info<< "Using relative tolerance " << tol
00550 << " to match up faces and points" << nl << endl;
00551 coupledPolyPatch::matchTol = tol;
00552
00553
00554 # include "createPolyMesh.H"
00555 const word oldInstance = mesh.pointsInstance();
00556
00557 const polyBoundaryMesh& patches = mesh.boundaryMesh();
00558
00559
00560 patches.checkParallelSync(true);
00561
00562
00563 dumpCyclicMatch("initial_", mesh);
00564
00565
00566 PtrList<dictionary> patchSources(dict.lookup("patches"));
00567
00568
00569
00570
00571
00572
00573 if (patchSources.size())
00574 {
00575
00576 DynamicList<polyPatch*> allPatches(patches.size()+patchSources.size());
00577
00578 label startFaceI = mesh.nInternalFaces();
00579
00580
00581 forAll(patches, patchI)
00582 {
00583 const polyPatch& pp = patches[patchI];
00584
00585 if (!isA<processorPolyPatch>(pp))
00586 {
00587 allPatches.append
00588 (
00589 pp.clone
00590 (
00591 patches,
00592 patchI,
00593 pp.size(),
00594 startFaceI
00595 ).ptr()
00596 );
00597 startFaceI += pp.size();
00598 }
00599 }
00600
00601 forAll(patchSources, addedI)
00602 {
00603 const dictionary& dict = patchSources[addedI];
00604
00605 word patchName(dict.lookup("name"));
00606
00607 label destPatchI = patches.findPatchID(patchName);
00608
00609 if (destPatchI == -1)
00610 {
00611 dictionary patchDict(dict.subDict("dictionary"));
00612
00613 destPatchI = allPatches.size();
00614
00615 Info<< "Adding new patch " << patchName
00616 << " as patch " << destPatchI
00617 << " from " << patchDict << endl;
00618
00619 patchDict.set("nFaces", 0);
00620 patchDict.set("startFace", startFaceI);
00621
00622
00623 allPatches.append
00624 (
00625 polyPatch::New
00626 (
00627 patchName,
00628 patchDict,
00629 destPatchI,
00630 patches
00631 ).ptr()
00632 );
00633 }
00634 }
00635
00636
00637 forAll(patches, patchI)
00638 {
00639 const polyPatch& pp = patches[patchI];
00640
00641 if (isA<processorPolyPatch>(pp))
00642 {
00643 allPatches.append
00644 (
00645 pp.clone
00646 (
00647 patches,
00648 patchI,
00649 pp.size(),
00650 startFaceI
00651 ).ptr()
00652 );
00653 startFaceI += pp.size();
00654 }
00655 }
00656
00657 allPatches.shrink();
00658 mesh.removeBoundary();
00659 mesh.addPatches(allPatches);
00660
00661 Info<< endl;
00662 }
00663
00664
00665
00666
00667
00668
00669 polyTopoChange meshMod(mesh);
00670
00671
00672 forAll(patchSources, addedI)
00673 {
00674 const dictionary& dict = patchSources[addedI];
00675
00676 word patchName(dict.lookup("name"));
00677
00678 label destPatchI = patches.findPatchID(patchName);
00679
00680 if (destPatchI == -1)
00681 {
00682 FatalErrorIn(args.executable()) << "patch " << patchName
00683 << " not added. Problem." << abort(FatalError);
00684 }
00685
00686 word sourceType(dict.lookup("constructFrom"));
00687
00688 if (sourceType == "patches")
00689 {
00690 labelHashSet patchSources(patches.patchSet(dict.lookup("patches")));
00691
00692
00693 forAllConstIter(labelHashSet, patchSources, iter)
00694 {
00695 const polyPatch& pp = patches[iter.key()];
00696
00697 Info<< "Moving faces from patch " << pp.name()
00698 << " to patch " << destPatchI << endl;
00699
00700 forAll(pp, i)
00701 {
00702 changePatchID
00703 (
00704 mesh,
00705 pp.start() + i,
00706 destPatchI,
00707 meshMod
00708 );
00709 }
00710 }
00711 }
00712 else if (sourceType == "set")
00713 {
00714 word setName(dict.lookup("set"));
00715
00716 faceSet faces(mesh, setName);
00717
00718 Info<< "Read " << returnReduce(faces.size(), sumOp<label>())
00719 << " faces from faceSet " << faces.name() << endl;
00720
00721
00722 labelList faceLabels(faces.toc());
00723
00724 SortableList<label> patchFaces(faceLabels);
00725
00726 forAll(patchFaces, i)
00727 {
00728 label faceI = patchFaces[i];
00729
00730 if (mesh.isInternalFace(faceI))
00731 {
00732 FatalErrorIn(args.executable())
00733 << "Face " << faceI << " specified in set "
00734 << faces.name()
00735 << " is not an external face of the mesh." << endl
00736 << "This application can only repatch existing boundary"
00737 << " faces." << exit(FatalError);
00738 }
00739
00740 changePatchID
00741 (
00742 mesh,
00743 faceI,
00744 destPatchI,
00745 meshMod
00746 );
00747 }
00748 }
00749 else
00750 {
00751 FatalErrorIn(args.executable())
00752 << "Invalid source type " << sourceType << endl
00753 << "Valid source types are 'patches' 'set'" << exit(FatalError);
00754 }
00755 }
00756 Info<< endl;
00757
00758
00759
00760
00761 Info<< "Doing topology modification to order faces." << nl << endl;
00762 autoPtr<mapPolyMesh> map = meshMod.changeMesh(mesh, true);
00763 mesh.movePoints(map().preMotionPoints());
00764
00765 dumpCyclicMatch("coupled_", mesh);
00766
00767
00768 if (!pointSync)
00769 {
00770 Info<< "Not synchronising points." << nl << endl;
00771 }
00772 else
00773 {
00774 Info<< "Synchronising points." << nl << endl;
00775
00776
00777
00778
00779
00780
00781
00782
00783
00784
00785
00786
00787
00788 forAll(mesh.boundaryMesh(), patchI)
00789 {
00790 const polyPatch& pp = mesh.boundaryMesh()[patchI];
00791
00792 if (pp.size() && isA<coupledPolyPatch>(pp))
00793 {
00794 const coupledPolyPatch& cpp =
00795 refCast<const coupledPolyPatch>(pp);
00796
00797 if (cpp.separated())
00798 {
00799 Info<< "On coupled patch " << pp.name()
00800 << " separation[0] was "
00801 << cpp.separation()[0] << endl;
00802
00803 if (isA<cyclicPolyPatch>(pp))
00804 {
00805 const cyclicPolyPatch& cycpp =
00806 refCast<const cyclicPolyPatch>(pp);
00807
00808 if (cycpp.transform() == cyclicPolyPatch::TRANSLATIONAL)
00809 {
00810 Info<< "On cyclic translation patch " << pp.name()
00811 << " forcing uniform separation of "
00812 << cycpp.separationVector() << endl;
00813 const_cast<vectorField&>(cpp.separation()) =
00814 pointField(1, cycpp.separationVector());
00815 }
00816 else
00817 {
00818 const_cast<vectorField&>(cpp.separation()) =
00819 pointField
00820 (
00821 1,
00822 pp[pp.size()/2].centre(mesh.points())
00823 - pp[0].centre(mesh.points())
00824 );
00825 }
00826 }
00827 else
00828 {
00829 const_cast<vectorField&>(cpp.separation())
00830 .setSize(1);
00831 }
00832 Info<< "On coupled patch " << pp.name()
00833 << " forcing uniform separation of "
00834 << cpp.separation() << endl;
00835 }
00836 else if (!cpp.parallel())
00837 {
00838 Info<< "On coupled patch " << pp.name()
00839 << " forcing uniform rotation of "
00840 << cpp.forwardT()[0] << endl;
00841
00842 const_cast<tensorField&>
00843 (
00844 cpp.forwardT()
00845 ).setSize(1);
00846 const_cast<tensorField&>
00847 (
00848 cpp.reverseT()
00849 ).setSize(1);
00850
00851 Info<< "On coupled patch " << pp.name()
00852 << " forcing uniform rotation of "
00853 << cpp.forwardT() << endl;
00854 }
00855 }
00856 }
00857
00858 Info<< "Synchronising points." << endl;
00859
00860 pointField newPoints(mesh.points());
00861
00862 syncPoints
00863 (
00864 mesh,
00865 newPoints,
00866 nearestEqOp(),
00867 point(GREAT, GREAT, GREAT)
00868 );
00869
00870 scalarField diff(mag(newPoints-mesh.points()));
00871 Info<< "Points changed by average:" << gAverage(diff)
00872 << " max:" << gMax(diff) << nl << endl;
00873
00874 mesh.movePoints(newPoints);
00875 }
00876
00877
00878
00879
00880 Info<< "Removing patches with no faces in them." << nl<< endl;
00881 filterPatches(mesh);
00882
00883
00884 dumpCyclicMatch("final_", mesh);
00885
00886
00887
00888 IOstream::defaultPrecision(10);
00889
00890 if (!overwrite)
00891 {
00892 runTime++;
00893 }
00894 else
00895 {
00896 mesh.setInstance(oldInstance);
00897 }
00898
00899
00900 Info<< "Writing repatched mesh to " << runTime.timeName() << nl << endl;
00901 mesh.write();
00902
00903 Info<< "End\n" << endl;
00904
00905 return 0;
00906 }
00907
00908
00909