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 "cyclicPolyPatch.H"
00028 #include "addToRunTimeSelectionTable.H"
00029 #include "polyBoundaryMesh.H"
00030 #include "polyMesh.H"
00031 #include "demandDrivenData.H"
00032 #include "OFstream.H"
00033 #include "patchZones.H"
00034 #include "matchPoints.H"
00035 #include "EdgeMap.H"
00036 #include "Time.H"
00037 #include "transformList.H"
00038
00039
00040
00041 namespace Foam
00042 {
00043 defineTypeNameAndDebug(cyclicPolyPatch, 0);
00044
00045 addToRunTimeSelectionTable(polyPatch, cyclicPolyPatch, word);
00046 addToRunTimeSelectionTable(polyPatch, cyclicPolyPatch, dictionary);
00047
00048
00049 template<>
00050 const char* NamedEnum<cyclicPolyPatch::transformType, 3>::names[] =
00051 {
00052 "unknown",
00053 "rotational",
00054 "translational"
00055 };
00056
00057 const NamedEnum<cyclicPolyPatch::transformType, 3>
00058 cyclicPolyPatch::transformTypeNames;
00059 }
00060
00061
00062
00063
00064 Foam::label Foam::cyclicPolyPatch::findMaxArea
00065 (
00066 const pointField& points,
00067 const faceList& faces
00068 )
00069 {
00070 label maxI = -1;
00071 scalar maxAreaSqr = -GREAT;
00072
00073 forAll(faces, faceI)
00074 {
00075 scalar areaSqr = magSqr(faces[faceI].normal(points));
00076
00077 if (areaSqr > maxAreaSqr)
00078 {
00079 maxAreaSqr = areaSqr;
00080 maxI = faceI;
00081 }
00082 }
00083 return maxI;
00084 }
00085
00086
00087 void Foam::cyclicPolyPatch::calcTransforms()
00088 {
00089 if (size())
00090 {
00091 const pointField& points = this->points();
00092
00093
00094
00095
00096 primitivePatch half0
00097 (
00098 SubList<face>
00099 (
00100 *this,
00101 size()/2
00102 ),
00103 points
00104 );
00105
00106 pointField half0Ctrs(calcFaceCentres(half0, half0.points()));
00107
00108 scalarField half0Tols(calcFaceTol(half0, half0.points(), half0Ctrs));
00109
00110 primitivePatch half1
00111 (
00112 SubList<face>
00113 (
00114 *this,
00115 size()/2,
00116 size()/2
00117 ),
00118 points
00119 );
00120 pointField half1Ctrs(calcFaceCentres(half1, half1.points()));
00121
00122
00123 if (debug)
00124 {
00125 fileName casePath(boundaryMesh().mesh().time().path());
00126
00127 fileName nm0(casePath/name()+"_half0_faces.obj");
00128 Pout<< "cyclicPolyPatch::calcTransforms : Writing half0"
00129 << " faces to OBJ file " << nm0 << endl;
00130 writeOBJ(nm0, half0, half0.points());
00131
00132 fileName nm1(casePath/name()+"_half1_faces.obj");
00133 Pout<< "cyclicPolyPatch::calcTransforms : Writing half1"
00134 << " faces to OBJ file " << nm1 << endl;
00135 writeOBJ(nm1, half1, half1.points());
00136
00137 OFstream str(casePath/name()+"_half0_to_half1.obj");
00138 label vertI = 0;
00139 Pout<< "cyclicPolyPatch::calcTransforms :"
00140 << " Writing coupled face centres as lines to " << str.name()
00141 << endl;
00142 forAll(half0Ctrs, i)
00143 {
00144 const point& p0 = half0Ctrs[i];
00145 str << "v " << p0.x() << ' ' << p0.y() << ' ' << p0.z() << nl;
00146 vertI++;
00147 const point& p1 = half1Ctrs[i];
00148 str << "v " << p1.x() << ' ' << p1.y() << ' ' << p1.z() << nl;
00149 vertI++;
00150 str << "l " << vertI-1 << ' ' << vertI << nl;
00151 }
00152 }
00153
00154 vectorField half0Normals(half0.size());
00155 vectorField half1Normals(half1.size());
00156
00157 for (label facei = 0; facei < size()/2; facei++)
00158 {
00159 half0Normals[facei] = operator[](facei).normal(points);
00160 label nbrFacei = facei+size()/2;
00161 half1Normals[facei] = operator[](nbrFacei).normal(points);
00162
00163 scalar magSf = mag(half0Normals[facei]);
00164 scalar nbrMagSf = mag(half1Normals[facei]);
00165 scalar avSf = (magSf + nbrMagSf)/2.0;
00166
00167 if (magSf < ROOTVSMALL && nbrMagSf < ROOTVSMALL)
00168 {
00169
00170
00171
00172 half0Normals[facei] = point(1, 0, 0);
00173 half1Normals[facei] = half0Normals[facei];
00174 }
00175 else if (mag(magSf - nbrMagSf)/avSf > coupledPolyPatch::matchTol)
00176 {
00177 FatalErrorIn
00178 (
00179 "cyclicPolyPatch::calcTransforms()"
00180 ) << "face " << facei << " area does not match neighbour "
00181 << nbrFacei << " by "
00182 << 100*mag(magSf - nbrMagSf)/avSf
00183 << "% -- possible face ordering problem." << endl
00184 << "patch:" << name()
00185 << " my area:" << magSf
00186 << " neighbour area:" << nbrMagSf
00187 << " matching tolerance:" << coupledPolyPatch::matchTol
00188 << endl
00189 << "Mesh face:" << start()+facei
00190 << " vertices:"
00191 << UIndirectList<point>(points, operator[](facei))()
00192 << endl
00193 << "Neighbour face:" << start()+nbrFacei
00194 << " vertices:"
00195 << UIndirectList<point>(points, operator[](nbrFacei))()
00196 << endl
00197 << "Rerun with cyclic debug flag set"
00198 << " for more information." << exit(FatalError);
00199 }
00200 else
00201 {
00202 half0Normals[facei] /= magSf;
00203 half1Normals[facei] /= nbrMagSf;
00204 }
00205 }
00206
00207
00208
00209
00210
00211 switch (transform_)
00212 {
00213 case ROTATIONAL:
00214 {
00215
00216
00217
00218 label face0 = getConsistentRotationFace(half0Ctrs);
00219 label face1 = face0;
00220
00221 vector n0 =
00222 (
00223 (half0Ctrs[face0] - rotationCentre_)
00224 ^ rotationAxis_
00225 );
00226 vector n1 =
00227 (
00228 (half1Ctrs[face1] - rotationCentre_)
00229 ^ -rotationAxis_
00230 );
00231 n0 /= mag(n0) + VSMALL;
00232 n1 /= mag(n1) + VSMALL;
00233
00234 if (debug)
00235 {
00236 Pout<< "cyclicPolyPatch::calcTransforms :"
00237 << " Specified rotation :"
00238 << " n0:" << n0 << " n1:" << n1 << endl;
00239 }
00240
00241
00242
00243 calcTransformTensors
00244 (
00245 pointField(1, half0Ctrs[face0]),
00246 pointField(1, half1Ctrs[face1]),
00247 vectorField(1, n0),
00248 vectorField(1, n1),
00249 scalarField(1, half0Tols[face0]),
00250 1E-4
00251 );
00252
00253 break;
00254 }
00255
00256 default:
00257 {
00258
00259 calcTransformTensors
00260 (
00261 half0Ctrs,
00262 half1Ctrs,
00263 half0Normals,
00264 half1Normals,
00265 half0Tols
00266 );
00267
00268 break;
00269 }
00270 }
00271 }
00272 }
00273
00274
00275
00276
00277
00278
00279
00280 bool Foam::cyclicPolyPatch::getGeometricHalves
00281 (
00282 const primitivePatch& pp,
00283 labelList& half0ToPatch,
00284 labelList& half1ToPatch
00285 ) const
00286 {
00287
00288 const vectorField& faceNormals = pp.faceNormals();
00289
00290
00291 boolList regionEdge(pp.nEdges(), false);
00292
00293 const labelListList& edgeFaces = pp.edgeFaces();
00294
00295 label nRegionEdges = 0;
00296
00297 forAll(edgeFaces, edgeI)
00298 {
00299 const labelList& eFaces = edgeFaces[edgeI];
00300
00301
00302
00303 if (eFaces.size() == 2)
00304 {
00305 if ((faceNormals[eFaces[0]] & faceNormals[eFaces[1]])< featureCos_)
00306 {
00307 regionEdge[edgeI] = true;
00308
00309 nRegionEdges++;
00310 }
00311 }
00312 }
00313
00314
00315
00316
00317 patchZones ppZones(pp, regionEdge);
00318
00319 if (debug)
00320 {
00321 Pout<< "cyclicPolyPatch::getGeometricHalves : "
00322 << "Found " << nRegionEdges << " edges on patch " << name()
00323 << " where the cos of the angle between two connected faces"
00324 << " was less than " << featureCos_ << nl
00325 << "Patch divided by these and by single sides edges into "
00326 << ppZones.nZones() << " parts." << endl;
00327
00328
00329
00330
00331 labelList nZoneFaces(ppZones.nZones());
00332
00333 for (label zoneI = 0; zoneI < ppZones.nZones(); zoneI++)
00334 {
00335 OFstream stream
00336 (
00337 boundaryMesh().mesh().time().path()
00338 /name()+"_zone_"+Foam::name(zoneI)+".obj"
00339 );
00340 Pout<< "cyclicPolyPatch::getGeometricHalves : Writing zone "
00341 << zoneI << " face centres to OBJ file " << stream.name()
00342 << endl;
00343
00344 labelList zoneFaces(findIndices(ppZones, zoneI));
00345
00346 forAll(zoneFaces, i)
00347 {
00348 writeOBJ(stream, pp[zoneFaces[i]].centre(pp.points()));
00349 }
00350
00351 nZoneFaces[zoneI] = zoneFaces.size();
00352 }
00353 }
00354
00355
00356 if (ppZones.nZones() == 2)
00357 {
00358 half0ToPatch = findIndices(ppZones, 0);
00359 half1ToPatch = findIndices(ppZones, 1);
00360 }
00361 else
00362 {
00363 if (debug)
00364 {
00365 Pout<< "cyclicPolyPatch::getGeometricHalves :"
00366 << " falling back to face-normal comparison" << endl;
00367 }
00368 label n0Faces = 0;
00369 half0ToPatch.setSize(pp.size());
00370
00371 label n1Faces = 0;
00372 half1ToPatch.setSize(pp.size());
00373
00374
00375 forAll(faceNormals, faceI)
00376 {
00377 if ((faceNormals[faceI] & faceNormals[0]) > 0)
00378 {
00379 half0ToPatch[n0Faces++] = faceI;
00380 }
00381 else
00382 {
00383 half1ToPatch[n1Faces++] = faceI;
00384 }
00385 }
00386 half0ToPatch.setSize(n0Faces);
00387 half1ToPatch.setSize(n1Faces);
00388
00389 if (debug)
00390 {
00391 Pout<< "cyclicPolyPatch::getGeometricHalves :"
00392 << " Number of faces per zone:("
00393 << n0Faces << ' ' << n1Faces << ')' << endl;
00394 }
00395 }
00396
00397 if (half0ToPatch.size() != half1ToPatch.size())
00398 {
00399 fileName casePath(boundaryMesh().mesh().time().path());
00400
00401
00402 {
00403 fileName nm0(casePath/name()+"_half0_faces.obj");
00404 Pout<< "cyclicPolyPatch::getGeometricHalves : Writing half0"
00405 << " faces to OBJ file " << nm0 << endl;
00406 writeOBJ(nm0, UIndirectList<face>(pp, half0ToPatch)(), pp.points());
00407
00408 fileName nm1(casePath/name()+"_half1_faces.obj");
00409 Pout<< "cyclicPolyPatch::getGeometricHalves : Writing half1"
00410 << " faces to OBJ file " << nm1 << endl;
00411 writeOBJ(nm1, UIndirectList<face>(pp, half1ToPatch)(), pp.points());
00412 }
00413
00414
00415 {
00416 OFstream str0(casePath/name()+"_half0.obj");
00417 Pout<< "cyclicPolyPatch::getGeometricHalves : Writing half0"
00418 << " face centres to OBJ file " << str0.name() << endl;
00419
00420 forAll(half0ToPatch, i)
00421 {
00422 writeOBJ(str0, pp[half0ToPatch[i]].centre(pp.points()));
00423 }
00424
00425 OFstream str1(casePath/name()+"_half1.obj");
00426 Pout<< "cyclicPolyPatch::getGeometricHalves : Writing half1"
00427 << " face centres to OBJ file " << str1.name() << endl;
00428 forAll(half1ToPatch, i)
00429 {
00430 writeOBJ(str1, pp[half1ToPatch[i]].centre(pp.points()));
00431 }
00432 }
00433
00434 SeriousErrorIn
00435 (
00436 "cyclicPolyPatch::getGeometricHalves"
00437 "(const primitivePatch&, labelList&, labelList&) const"
00438 ) << "Patch " << name() << " gets decomposed in two zones of"
00439 << "inequal size: " << half0ToPatch.size()
00440 << " and " << half1ToPatch.size() << endl
00441 << "This means that the patch is either not two separate regions"
00442 << " or one region where the angle between the different regions"
00443 << " is not sufficiently sharp." << endl
00444 << "Please adapt the featureCos setting." << endl
00445 << "Continuing with incorrect face ordering from now on!" << endl;
00446
00447 return false;
00448 }
00449 else
00450 {
00451 return true;
00452 }
00453 }
00454
00455
00456
00457
00458
00459 void Foam::cyclicPolyPatch::getCentresAndAnchors
00460 (
00461 const primitivePatch& pp,
00462 const faceList& half0Faces,
00463 const faceList& half1Faces,
00464
00465 pointField& ppPoints,
00466 pointField& half0Ctrs,
00467 pointField& half1Ctrs,
00468 pointField& anchors0,
00469 scalarField& tols
00470 ) const
00471 {
00472
00473 half0Ctrs = calcFaceCentres(half0Faces, pp.points());
00474 anchors0 = getAnchorPoints(half0Faces, pp.points());
00475 half1Ctrs = calcFaceCentres(half1Faces, pp.points());
00476
00477 switch (transform_)
00478 {
00479 case ROTATIONAL:
00480 {
00481 label face0 = getConsistentRotationFace(half0Ctrs);
00482 label face1 = getConsistentRotationFace(half1Ctrs);
00483
00484 vector n0 = ((half0Ctrs[face0] - rotationCentre_) ^ rotationAxis_);
00485 vector n1 = ((half1Ctrs[face1] - rotationCentre_) ^ -rotationAxis_);
00486 n0 /= mag(n0) + VSMALL;
00487 n1 /= mag(n1) + VSMALL;
00488
00489 if (debug)
00490 {
00491 Pout<< "cyclicPolyPatch::getCentresAndAnchors :"
00492 << " Specified rotation :"
00493 << " n0:" << n0 << " n1:" << n1 << endl;
00494 }
00495
00496
00497 const tensor reverseT(rotationTensor(n0, -n1));
00498
00499
00500 forAll(half0Ctrs, faceI)
00501 {
00502 half0Ctrs[faceI] = Foam::transform(reverseT, half0Ctrs[faceI]);
00503 anchors0[faceI] = Foam::transform(reverseT, anchors0[faceI]);
00504 }
00505
00506 ppPoints = Foam::transform(reverseT, pp.points());
00507
00508 break;
00509 }
00510
00511
00512
00513
00514
00515
00516
00517
00518
00519
00520
00521
00522
00523
00524
00525
00526 default:
00527 {
00528
00529
00530
00531
00532
00533 label max0I = findMaxArea(pp.points(), half0Faces);
00534 vector n0 = half0Faces[max0I].normal(pp.points());
00535 n0 /= mag(n0) + VSMALL;
00536
00537 label max1I = findMaxArea(pp.points(), half1Faces);
00538 vector n1 = half1Faces[max1I].normal(pp.points());
00539 n1 /= mag(n1) + VSMALL;
00540
00541 if (mag(n0 & n1) < 1-coupledPolyPatch::matchTol)
00542 {
00543 if (debug)
00544 {
00545 Pout<< "cyclicPolyPatch::getCentresAndAnchors :"
00546 << " Detected rotation :"
00547 << " n0:" << n0 << " n1:" << n1 << endl;
00548 }
00549
00550
00551 const tensor reverseT(rotationTensor(n0, -n1));
00552
00553
00554 forAll(half0Ctrs, faceI)
00555 {
00556 half0Ctrs[faceI] = Foam::transform
00557 (
00558 reverseT,
00559 half0Ctrs[faceI]
00560 );
00561 anchors0[faceI] = Foam::transform
00562 (
00563 reverseT,
00564 anchors0[faceI]
00565 );
00566 }
00567 ppPoints = Foam::transform(reverseT, pp.points());
00568 }
00569 else
00570 {
00571
00572
00573 primitiveFacePatch half0(half0Faces, pp.points());
00574 const pointField& half0Pts = half0.localPoints();
00575 const point ctr0(sum(half0Pts)/half0Pts.size());
00576
00577 primitiveFacePatch half1(half1Faces, pp.points());
00578 const pointField& half1Pts = half1.localPoints();
00579 const point ctr1(sum(half1Pts)/half1Pts.size());
00580
00581 if (debug)
00582 {
00583 Pout<< "cyclicPolyPatch::getCentresAndAnchors :"
00584 << " Detected translation :"
00585 << " n0:" << n0 << " n1:" << n1
00586 << " ctr0:" << ctr0 << " ctr1:" << ctr1 << endl;
00587 }
00588
00589 half0Ctrs += ctr1 - ctr0;
00590 anchors0 += ctr1 - ctr0;
00591 ppPoints = pp.points() + ctr1 - ctr0;
00592 }
00593 break;
00594 }
00595 }
00596
00597
00598
00599 tols = calcFaceTol(half1Faces, pp.points(), half1Ctrs);
00600 }
00601
00602
00603
00604 bool Foam::cyclicPolyPatch::matchAnchors
00605 (
00606 const bool report,
00607 const primitivePatch& pp,
00608 const labelList& half0ToPatch,
00609 const pointField& anchors0,
00610
00611 const labelList& half1ToPatch,
00612 const faceList& half1Faces,
00613 const labelList& from1To0,
00614
00615 const scalarField& tols,
00616
00617 labelList& faceMap,
00618 labelList& rotation
00619 ) const
00620 {
00621
00622
00623
00624 forAll(half0ToPatch, half0FaceI)
00625 {
00626
00627 label patchFaceI = half0ToPatch[half0FaceI];
00628
00629 faceMap[patchFaceI] = half0FaceI;
00630
00631
00632 rotation[patchFaceI] = 0;
00633 }
00634
00635 bool fullMatch = true;
00636
00637 forAll(from1To0, half1FaceI)
00638 {
00639 label patchFaceI = half1ToPatch[half1FaceI];
00640
00641
00642 label half0FaceI = from1To0[half1FaceI];
00643
00644 label newFaceI = half0FaceI + pp.size()/2;
00645
00646 faceMap[patchFaceI] = newFaceI;
00647
00648
00649
00650
00651
00652 const point& wantedAnchor = anchors0[half0FaceI];
00653
00654 rotation[newFaceI] = getRotation
00655 (
00656 pp.points(),
00657 half1Faces[half1FaceI],
00658 wantedAnchor,
00659 tols[half1FaceI]
00660 );
00661
00662 if (rotation[newFaceI] == -1)
00663 {
00664 fullMatch = false;
00665
00666 if (report)
00667 {
00668 const face& f = half1Faces[half1FaceI];
00669 SeriousErrorIn
00670 (
00671 "cyclicPolyPatch::matchAnchors(..)"
00672 ) << "Patch:" << name() << " : "
00673 << "Cannot find point on face " << f
00674 << " with vertices:"
00675 << UIndirectList<point>(pp.points(), f)()
00676 << " that matches point " << wantedAnchor
00677 << " when matching the halves of cyclic patch " << name()
00678 << endl
00679 << "Continuing with incorrect face ordering from now on!"
00680 << endl;
00681 }
00682 }
00683 }
00684 return fullMatch;
00685 }
00686
00687
00688 Foam::label Foam::cyclicPolyPatch::getConsistentRotationFace
00689 (
00690 const pointField& faceCentres
00691 ) const
00692 {
00693 const scalarField magRadSqr =
00694 magSqr((faceCentres - rotationCentre_) ^ rotationAxis_);
00695 scalarField axisLen = (faceCentres - rotationCentre_) & rotationAxis_;
00696 axisLen = axisLen - min(axisLen);
00697 const scalarField magLenSqr = magRadSqr + axisLen*axisLen;
00698
00699 label rotFace = -1;
00700 scalar maxMagLenSqr = -GREAT;
00701 scalar maxMagRadSqr = -GREAT;
00702 forAll(faceCentres, i)
00703 {
00704 if (magLenSqr[i] >= maxMagLenSqr)
00705 {
00706 if (magRadSqr[i] > maxMagRadSqr)
00707 {
00708 rotFace = i;
00709 maxMagLenSqr = magLenSqr[i];
00710 maxMagRadSqr = magRadSqr[i];
00711 }
00712 }
00713 }
00714
00715 if (debug)
00716 {
00717 Info<< "getConsistentRotationFace(const pointField&)" << nl
00718 << " rotFace = " << rotFace << nl
00719 << " point = " << faceCentres[rotFace] << endl;
00720 }
00721
00722 return rotFace;
00723 }
00724
00725
00726
00727
00728 Foam::cyclicPolyPatch::cyclicPolyPatch
00729 (
00730 const word& name,
00731 const label size,
00732 const label start,
00733 const label index,
00734 const polyBoundaryMesh& bm
00735 )
00736 :
00737 coupledPolyPatch(name, size, start, index, bm),
00738 coupledPointsPtr_(NULL),
00739 coupledEdgesPtr_(NULL),
00740 featureCos_(0.9),
00741 transform_(UNKNOWN),
00742 rotationAxis_(vector::zero),
00743 rotationCentre_(point::zero),
00744 separationVector_(vector::zero)
00745 {}
00746
00747
00748 Foam::cyclicPolyPatch::cyclicPolyPatch
00749 (
00750 const word& name,
00751 const dictionary& dict,
00752 const label index,
00753 const polyBoundaryMesh& bm
00754 )
00755 :
00756 coupledPolyPatch(name, dict, index, bm),
00757 coupledPointsPtr_(NULL),
00758 coupledEdgesPtr_(NULL),
00759 featureCos_(0.9),
00760 transform_(UNKNOWN),
00761 rotationAxis_(vector::zero),
00762 rotationCentre_(point::zero),
00763 separationVector_(vector::zero)
00764 {
00765 dict.readIfPresent("featureCos", featureCos_);
00766
00767 if (dict.found("transform"))
00768 {
00769 transform_ = transformTypeNames.read(dict.lookup("transform"));
00770 switch (transform_)
00771 {
00772 case ROTATIONAL:
00773 {
00774 dict.lookup("rotationAxis") >> rotationAxis_;
00775 dict.lookup("rotationCentre") >> rotationCentre_;
00776 break;
00777 }
00778 case TRANSLATIONAL:
00779 {
00780 dict.lookup("separationVector") >> separationVector_;
00781 break;
00782 }
00783 default:
00784 {
00785
00786 }
00787 }
00788 }
00789 }
00790
00791
00792 Foam::cyclicPolyPatch::cyclicPolyPatch
00793 (
00794 const cyclicPolyPatch& pp,
00795 const polyBoundaryMesh& bm
00796 )
00797 :
00798 coupledPolyPatch(pp, bm),
00799 coupledPointsPtr_(NULL),
00800 coupledEdgesPtr_(NULL),
00801 featureCos_(pp.featureCos_),
00802 transform_(pp.transform_),
00803 rotationAxis_(pp.rotationAxis_),
00804 rotationCentre_(pp.rotationCentre_),
00805 separationVector_(pp.separationVector_)
00806 {}
00807
00808
00809 Foam::cyclicPolyPatch::cyclicPolyPatch
00810 (
00811 const cyclicPolyPatch& pp,
00812 const polyBoundaryMesh& bm,
00813 const label index,
00814 const label newSize,
00815 const label newStart
00816 )
00817 :
00818 coupledPolyPatch(pp, bm, index, newSize, newStart),
00819 coupledPointsPtr_(NULL),
00820 coupledEdgesPtr_(NULL),
00821 featureCos_(pp.featureCos_),
00822 transform_(pp.transform_),
00823 rotationAxis_(pp.rotationAxis_),
00824 rotationCentre_(pp.rotationCentre_),
00825 separationVector_(pp.separationVector_)
00826 {}
00827
00828
00829
00830
00831 Foam::cyclicPolyPatch::~cyclicPolyPatch()
00832 {
00833 deleteDemandDrivenData(coupledPointsPtr_);
00834 deleteDemandDrivenData(coupledEdgesPtr_);
00835 }
00836
00837
00838
00839
00840
00841 void Foam::cyclicPolyPatch::initGeometry()
00842 {
00843 polyPatch::initGeometry();
00844 }
00845
00846 void Foam::cyclicPolyPatch::calcGeometry()
00847 {
00848 polyPatch::calcGeometry();
00849 calcTransforms();
00850 }
00851
00852 void Foam::cyclicPolyPatch::initMovePoints(const pointField& p)
00853 {
00854 polyPatch::initMovePoints(p);
00855 }
00856
00857 void Foam::cyclicPolyPatch::movePoints(const pointField& p)
00858 {
00859 polyPatch::movePoints(p);
00860 calcTransforms();
00861 }
00862
00863 void Foam::cyclicPolyPatch::initUpdateMesh()
00864 {
00865 polyPatch::initUpdateMesh();
00866 }
00867
00868 void Foam::cyclicPolyPatch::updateMesh()
00869 {
00870 polyPatch::updateMesh();
00871 deleteDemandDrivenData(coupledPointsPtr_);
00872 deleteDemandDrivenData(coupledEdgesPtr_);
00873 }
00874
00875
00876 const Foam::edgeList& Foam::cyclicPolyPatch::coupledPoints() const
00877 {
00878 if (!coupledPointsPtr_)
00879 {
00880
00881
00882
00883
00884
00885
00886 labelList coupledPoint(nPoints(), -1);
00887
00888 for (label patchFaceA = 0; patchFaceA < size()/2; patchFaceA++)
00889 {
00890 const face& fA = localFaces()[patchFaceA];
00891
00892 forAll(fA, indexA)
00893 {
00894 label patchPointA = fA[indexA];
00895
00896 if (coupledPoint[patchPointA] == -1)
00897 {
00898 const face& fB = localFaces()[patchFaceA + size()/2];
00899
00900 label indexB = (fB.size() - indexA) % fB.size();
00901
00902
00903 if (patchPointA != fB[indexB])
00904 {
00905 coupledPoint[patchPointA] = fB[indexB];
00906 }
00907 }
00908 }
00909 }
00910
00911 coupledPointsPtr_ = new edgeList(nPoints());
00912 edgeList& connected = *coupledPointsPtr_;
00913
00914
00915 label connectedI = 0;
00916
00917 forAll(coupledPoint, i)
00918 {
00919 if (coupledPoint[i] != -1)
00920 {
00921 connected[connectedI++] = edge(i, coupledPoint[i]);
00922 }
00923 }
00924
00925 connected.setSize(connectedI);
00926
00927 if (debug)
00928 {
00929 OFstream str
00930 (
00931 boundaryMesh().mesh().time().path()
00932 /"coupledPoints.obj"
00933 );
00934 label vertI = 0;
00935
00936 Pout<< "Writing file " << str.name() << " with coordinates of "
00937 << "coupled points" << endl;
00938
00939 forAll(connected, i)
00940 {
00941 const point& a = points()[meshPoints()[connected[i][0]]];
00942 const point& b = points()[meshPoints()[connected[i][1]]];
00943
00944 str<< "v " << a.x() << ' ' << a.y() << ' ' << a.z() << nl;
00945 str<< "v " << b.x() << ' ' << b.y() << ' ' << b.z() << nl;
00946 vertI += 2;
00947
00948 str<< "l " << vertI-1 << ' ' << vertI << nl;
00949 }
00950 }
00951
00952
00953 const_cast<primitivePatch&>(static_cast<const primitivePatch&>(*this))
00954 .clearOut();
00955 }
00956 return *coupledPointsPtr_;
00957 }
00958
00959
00960 const Foam::edgeList& Foam::cyclicPolyPatch::coupledEdges() const
00961 {
00962 if (!coupledEdgesPtr_)
00963 {
00964
00965 const edgeList& pointCouples = coupledPoints();
00966
00967 Map<label> aToB(2*pointCouples.size());
00968
00969 forAll(pointCouples, i)
00970 {
00971 const edge& e = pointCouples[i];
00972
00973 aToB.insert(e[0], e[1]);
00974 }
00975
00976
00977 EdgeMap<label> edgeMap(nEdges());
00978
00979 for (label patchFaceA = 0; patchFaceA < size()/2; patchFaceA++)
00980 {
00981 const labelList& fEdges = faceEdges()[patchFaceA];
00982
00983 forAll(fEdges, i)
00984 {
00985 label edgeI = fEdges[i];
00986
00987 const edge& e = edges()[edgeI];
00988
00989
00990
00991 Map<label>::const_iterator fnd0 = aToB.find(e[0]);
00992 if (fnd0 != aToB.end())
00993 {
00994 Map<label>::const_iterator fnd1 = aToB.find(e[1]);
00995 if (fnd1 != aToB.end())
00996 {
00997 edgeMap.insert(edge(fnd0(), fnd1()), edgeI);
00998 }
00999 }
01000 }
01001 }
01002
01003 coupledEdgesPtr_ = new edgeList(nEdges()/2);
01004 edgeList& coupledEdges = *coupledEdgesPtr_;
01005 label coupleI = 0;
01006
01007 for (label patchFaceB = size()/2; patchFaceB < size(); patchFaceB++)
01008 {
01009 const labelList& fEdges = faceEdges()[patchFaceB];
01010
01011 forAll(fEdges, i)
01012 {
01013 label edgeI = fEdges[i];
01014
01015 const edge& e = edges()[edgeI];
01016
01017
01018 EdgeMap<label>::iterator iter = edgeMap.find(e);
01019
01020 if (iter != edgeMap.end())
01021 {
01022 label halfAEdgeI = iter();
01023
01024
01025 if (halfAEdgeI != edgeI)
01026 {
01027 coupledEdges[coupleI++] = edge(halfAEdgeI, edgeI);
01028 }
01029
01030
01031 edgeMap.erase(iter);
01032 }
01033 }
01034 }
01035 coupledEdges.setSize(coupleI);
01036
01037
01038
01039
01040 forAll(coupledEdges, i)
01041 {
01042 const edge& e = coupledEdges[i];
01043
01044 if (e[0] == e[1] || e[0] < 0 || e[1] < 0)
01045 {
01046 FatalErrorIn("cyclicPolyPatch::coupledEdges() const")
01047 << "Problem : at position " << i
01048 << " illegal couple:" << e
01049 << abort(FatalError);
01050 }
01051 }
01052
01053 if (debug)
01054 {
01055 OFstream str
01056 (
01057 boundaryMesh().mesh().time().path()
01058 /"coupledEdges.obj"
01059 );
01060 label vertI = 0;
01061
01062 Pout<< "Writing file " << str.name() << " with centres of "
01063 << "coupled edges" << endl;
01064
01065 forAll(coupledEdges, i)
01066 {
01067 const edge& e = coupledEdges[i];
01068
01069 const point& a = edges()[e[0]].centre(localPoints());
01070 const point& b = edges()[e[1]].centre(localPoints());
01071
01072 str<< "v " << a.x() << ' ' << a.y() << ' ' << a.z() << nl;
01073 str<< "v " << b.x() << ' ' << b.y() << ' ' << b.z() << nl;
01074 vertI += 2;
01075
01076 str<< "l " << vertI-1 << ' ' << vertI << nl;
01077 }
01078 }
01079
01080
01081 const_cast<primitivePatch&>(static_cast<const primitivePatch&>(*this))
01082 .clearOut();
01083 }
01084 return *coupledEdgesPtr_;
01085 }
01086
01087
01088 void Foam::cyclicPolyPatch::initOrder(const primitivePatch& pp) const
01089 {}
01090
01091
01092
01093
01094
01095
01096 bool Foam::cyclicPolyPatch::order
01097 (
01098 const primitivePatch& pp,
01099 labelList& faceMap,
01100 labelList& rotation
01101 ) const
01102 {
01103 faceMap.setSize(pp.size());
01104 faceMap = -1;
01105
01106 rotation.setSize(pp.size());
01107 rotation = 0;
01108
01109 if (pp.empty())
01110 {
01111
01112 return false;
01113 }
01114
01115 if (pp.size()&1)
01116 {
01117 FatalErrorIn("cyclicPolyPatch::order(..)")
01118 << "Size of cyclic " << name() << " should be a multiple of 2"
01119 << ". It is " << pp.size() << abort(FatalError);
01120 }
01121
01122 label halfSize = pp.size()/2;
01123
01124
01125
01126
01127
01128
01129
01130
01131 labelList half0ToPatch;
01132
01133 labelList half1ToPatch;
01134
01135
01136
01137
01138
01139 half0ToPatch = identity(halfSize);
01140 half1ToPatch = half0ToPatch + halfSize;
01141
01142
01143 faceList half0Faces(UIndirectList<face>(pp, half0ToPatch));
01144 faceList half1Faces(UIndirectList<face>(pp, half1ToPatch));
01145
01146
01147 pointField half0Ctrs, half1Ctrs, anchors0, ppPoints;
01148 scalarField tols;
01149 getCentresAndAnchors
01150 (
01151 pp,
01152 half0Faces,
01153 half1Faces,
01154
01155 ppPoints,
01156 half0Ctrs,
01157 half1Ctrs,
01158 anchors0,
01159 tols
01160 );
01161
01162
01163 labelList from1To0;
01164 bool matchedAll = matchPoints
01165 (
01166 half1Ctrs,
01167 half0Ctrs,
01168 tols,
01169 false,
01170 from1To0
01171 );
01172
01173 if (debug)
01174 {
01175 Pout<< "cyclicPolyPatch::order : test if already ordered:"
01176 << matchedAll << endl;
01177
01178
01179 fileName nm0("match1_"+name()+"_half0_faces.obj");
01180 Pout<< "cyclicPolyPatch::order : Writing half0"
01181 << " faces to OBJ file " << nm0 << endl;
01182 writeOBJ(nm0, half0Faces, ppPoints);
01183
01184 fileName nm1("match1_"+name()+"_half1_faces.obj");
01185 Pout<< "cyclicPolyPatch::order : Writing half1"
01186 << " faces to OBJ file " << nm1 << endl;
01187 writeOBJ(nm1, half1Faces, pp.points());
01188
01189 OFstream ccStr
01190 (
01191 boundaryMesh().mesh().time().path()
01192 /"match1_"+ name() + "_faceCentres.obj"
01193 );
01194 Pout<< "cyclicPolyPatch::order : "
01195 << "Dumping currently found cyclic match as lines between"
01196 << " corresponding face centres to file " << ccStr.name()
01197 << endl;
01198
01199
01200
01201 label vertI = 0;
01202
01203 forAll(half1Ctrs, i)
01204 {
01205
01206 {
01207
01208
01209
01210 const point& c0 = half0Ctrs[i];
01211 const point& c1 = half1Ctrs[i];
01212 writeOBJ(ccStr, c0, c1, vertI);
01213 }
01214 }
01215 }
01216
01217
01218
01219
01220
01221 if (!matchedAll)
01222 {
01223 label faceI = 0;
01224 for (label i = 0; i < halfSize; i++)
01225 {
01226 half0ToPatch[i] = faceI++;
01227 half1ToPatch[i] = faceI++;
01228 }
01229
01230
01231 half0Faces = UIndirectList<face>(pp, half0ToPatch);
01232 half1Faces = UIndirectList<face>(pp, half1ToPatch);
01233
01234 getCentresAndAnchors
01235 (
01236 pp,
01237 half0Faces,
01238 half1Faces,
01239
01240 ppPoints,
01241 half0Ctrs,
01242 half1Ctrs,
01243 anchors0,
01244 tols
01245 );
01246
01247
01248 matchedAll = matchPoints
01249 (
01250 half1Ctrs,
01251 half0Ctrs,
01252 tols,
01253 false,
01254 from1To0
01255 );
01256
01257 if (debug)
01258 {
01259 Pout<< "cyclicPolyPatch::order : test if pairwise ordered:"
01260 << matchedAll << endl;
01261
01262 fileName nm0("match2_"+name()+"_half0_faces.obj");
01263 Pout<< "cyclicPolyPatch::order : Writing half0"
01264 << " faces to OBJ file " << nm0 << endl;
01265 writeOBJ(nm0, half0Faces, ppPoints);
01266
01267 fileName nm1("match2_"+name()+"_half1_faces.obj");
01268 Pout<< "cyclicPolyPatch::order : Writing half1"
01269 << " faces to OBJ file " << nm1 << endl;
01270 writeOBJ(nm1, half1Faces, pp.points());
01271
01272 OFstream ccStr
01273 (
01274 boundaryMesh().mesh().time().path()
01275 /"match2_"+name()+"_faceCentres.obj"
01276 );
01277 Pout<< "cyclicPolyPatch::order : "
01278 << "Dumping currently found cyclic match as lines between"
01279 << " corresponding face centres to file " << ccStr.name()
01280 << endl;
01281
01282
01283
01284 label vertI = 0;
01285
01286 forAll(half1Ctrs, i)
01287 {
01288 if (from1To0[i] != -1)
01289 {
01290
01291
01292 const point& c0 = half0Ctrs[from1To0[i]];
01293 const point& c1 = half1Ctrs[i];
01294 writeOBJ(ccStr, c0, c1, vertI);
01295 }
01296 }
01297 }
01298 }
01299
01300
01301
01302
01303
01304 if (!matchedAll)
01305 {
01306 label baffleI = 0;
01307
01308 forAll(*this, faceI)
01309 {
01310 const face& f = pp.localFaces()[faceI];
01311 const labelList& pFaces = pp.pointFaces()[f[0]];
01312
01313 label matchedFaceI = -1;
01314
01315 forAll(pFaces, i)
01316 {
01317 label otherFaceI = pFaces[i];
01318
01319 if (otherFaceI > faceI)
01320 {
01321 const face& otherF = pp.localFaces()[otherFaceI];
01322
01323
01324
01325 if (f == otherF)
01326 {
01327 matchedFaceI = otherFaceI;
01328 break;
01329 }
01330 }
01331 }
01332
01333 if (matchedFaceI != -1)
01334 {
01335 half0ToPatch[baffleI] = faceI;
01336 half1ToPatch[baffleI] = matchedFaceI;
01337 baffleI++;
01338 }
01339 }
01340
01341 if (baffleI == halfSize)
01342 {
01343
01344 half0Faces = UIndirectList<face>(pp, half0ToPatch);
01345 half1Faces = UIndirectList<face>(pp, half1ToPatch);
01346
01347 getCentresAndAnchors
01348 (
01349 pp,
01350 half0Faces,
01351 half1Faces,
01352
01353 ppPoints,
01354 half0Ctrs,
01355 half1Ctrs,
01356 anchors0,
01357 tols
01358 );
01359
01360
01361 matchedAll = matchPoints
01362 (
01363 half1Ctrs,
01364 half0Ctrs,
01365 tols,
01366 false,
01367 from1To0
01368 );
01369
01370 if (debug)
01371 {
01372 Pout<< "cyclicPolyPatch::order : test if baffles:"
01373 << matchedAll << endl;
01374
01375 fileName nm0("match3_"+name()+"_half0_faces.obj");
01376 Pout<< "cyclicPolyPatch::order : Writing half0"
01377 << " faces to OBJ file " << nm0 << endl;
01378 writeOBJ(nm0, half0Faces, ppPoints);
01379
01380 fileName nm1("match3_"+name()+"_half1_faces.obj");
01381 Pout<< "cyclicPolyPatch::order : Writing half1"
01382 << " faces to OBJ file " << nm1 << endl;
01383 writeOBJ(nm1, half1Faces, pp.points());
01384
01385 OFstream ccStr
01386 (
01387 boundaryMesh().mesh().time().path()
01388 /"match3_"+ name() + "_faceCentres.obj"
01389 );
01390 Pout<< "cyclicPolyPatch::order : "
01391 << "Dumping currently found cyclic match as lines between"
01392 << " corresponding face centres to file " << ccStr.name()
01393 << endl;
01394
01395
01396
01397 label vertI = 0;
01398
01399 forAll(half1Ctrs, i)
01400 {
01401 if (from1To0[i] != -1)
01402 {
01403
01404
01405 const point& c0 = half0Ctrs[from1To0[i]];
01406 const point& c1 = half1Ctrs[i];
01407 writeOBJ(ccStr, c0, c1, vertI);
01408 }
01409 }
01410 }
01411 }
01412 }
01413
01414
01415
01416
01417
01418 if (!matchedAll)
01419 {
01420
01421 bool okSplit = getGeometricHalves(pp, half0ToPatch, half1ToPatch);
01422
01423 if (!okSplit)
01424 {
01425
01426 return false;
01427 }
01428
01429
01430 half0Faces = UIndirectList<face>(pp, half0ToPatch);
01431 half1Faces = UIndirectList<face>(pp, half1ToPatch);
01432
01433 getCentresAndAnchors
01434 (
01435 pp,
01436 half0Faces,
01437 half1Faces,
01438
01439 ppPoints,
01440 half0Ctrs,
01441 half1Ctrs,
01442 anchors0,
01443 tols
01444 );
01445
01446
01447 matchedAll = matchPoints
01448 (
01449 half1Ctrs,
01450 half0Ctrs,
01451 tols,
01452 false,
01453 from1To0
01454 );
01455
01456 if (debug)
01457 {
01458 Pout<< "cyclicPolyPatch::order : automatic ordering result:"
01459 << matchedAll << endl;
01460
01461 fileName nm0("match4_"+name()+"_half0_faces.obj");
01462 Pout<< "cyclicPolyPatch::order : Writing half0"
01463 << " faces to OBJ file " << nm0 << endl;
01464 writeOBJ(nm0, half0Faces, ppPoints);
01465
01466 fileName nm1("match4_"+name()+"_half1_faces.obj");
01467 Pout<< "cyclicPolyPatch::order : Writing half1"
01468 << " faces to OBJ file " << nm1 << endl;
01469 writeOBJ(nm1, half1Faces, pp.points());
01470
01471 OFstream ccStr
01472 (
01473 boundaryMesh().mesh().time().path()
01474 /"match4_"+ name() + "_faceCentres.obj"
01475 );
01476 Pout<< "cyclicPolyPatch::order : "
01477 << "Dumping currently found cyclic match as lines between"
01478 << " corresponding face centres to file " << ccStr.name()
01479 << endl;
01480
01481
01482
01483 label vertI = 0;
01484
01485 forAll(half1Ctrs, i)
01486 {
01487 if (from1To0[i] != -1)
01488 {
01489
01490
01491 const point& c0 = half0Ctrs[from1To0[i]];
01492 const point& c1 = half1Ctrs[i];
01493 writeOBJ(ccStr, c0, c1, vertI);
01494 }
01495 }
01496 }
01497 }
01498
01499
01500 if (!matchedAll || debug)
01501 {
01502
01503 fileName nm0(name()+"_half0_faces.obj");
01504 Pout<< "cyclicPolyPatch::order : Writing half0"
01505 << " faces to OBJ file " << nm0 << endl;
01506 writeOBJ(nm0, half0Faces, pp.points());
01507
01508 fileName nm1(name()+"_half1_faces.obj");
01509 Pout<< "cyclicPolyPatch::order : Writing half1"
01510 << " faces to OBJ file " << nm1 << endl;
01511 writeOBJ(nm1, half1Faces, pp.points());
01512
01513 OFstream ccStr
01514 (
01515 boundaryMesh().mesh().time().path()
01516 /name() + "_faceCentres.obj"
01517 );
01518 Pout<< "cyclicPolyPatch::order : "
01519 << "Dumping currently found cyclic match as lines between"
01520 << " corresponding face centres to file " << ccStr.name()
01521 << endl;
01522
01523
01524
01525 label vertI = 0;
01526
01527 forAll(half1Ctrs, i)
01528 {
01529 if (from1To0[i] != -1)
01530 {
01531
01532
01533 const point& c0 = half0Ctrs[from1To0[i]];
01534 const point& c1 = half1Ctrs[i];
01535 writeOBJ(ccStr, c0, c1, vertI);
01536 }
01537 }
01538 }
01539
01540
01541 if (!matchedAll)
01542 {
01543 SeriousErrorIn
01544 (
01545 "cyclicPolyPatch::order"
01546 "(const primitivePatch&, labelList&, labelList&) const"
01547 ) << "Patch:" << name() << " : "
01548 << "Cannot match vectors to faces on both sides of patch" << endl
01549 << " Perhaps your faces do not match?"
01550 << " The obj files written contain the current match." << endl
01551 << " Continuing with incorrect face ordering from now on!"
01552 << endl;
01553
01554 return false;
01555 }
01556
01557
01558
01559
01560 matchAnchors
01561 (
01562 true,
01563 pp,
01564 half0ToPatch,
01565 anchors0,
01566 half1ToPatch,
01567 half1Faces,
01568 from1To0,
01569 tols,
01570 faceMap,
01571 rotation
01572 );
01573
01574
01575
01576 forAll(faceMap, faceI)
01577 {
01578 if (faceMap[faceI] != faceI || rotation[faceI] != 0)
01579 {
01580 return true;
01581 }
01582 }
01583
01584 return false;
01585 }
01586
01587
01588 void Foam::cyclicPolyPatch::write(Ostream& os) const
01589 {
01590 polyPatch::write(os);
01591 os.writeKeyword("featureCos") << featureCos_ << token::END_STATEMENT << nl;
01592 switch (transform_)
01593 {
01594 case ROTATIONAL:
01595 {
01596 os.writeKeyword("transform") << transformTypeNames[ROTATIONAL]
01597 << token::END_STATEMENT << nl;
01598 os.writeKeyword("rotationAxis") << rotationAxis_
01599 << token::END_STATEMENT << nl;
01600 os.writeKeyword("rotationCentre") << rotationCentre_
01601 << token::END_STATEMENT << nl;
01602 break;
01603 }
01604 case TRANSLATIONAL:
01605 {
01606 os.writeKeyword("transform") << transformTypeNames[TRANSLATIONAL]
01607 << token::END_STATEMENT << nl;
01608 os.writeKeyword("separationVector") << separationVector_
01609 << token::END_STATEMENT << nl;
01610 break;
01611 }
01612 default:
01613 {
01614
01615 }
01616 }
01617 }
01618
01619
01620