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 "processorPolyPatch.H"
00028 #include "addToRunTimeSelectionTable.H"
00029 #include "dictionary.H"
00030 #include "SubField.H"
00031 #include "demandDrivenData.H"
00032 #include "matchPoints.H"
00033 #include "OFstream.H"
00034 #include "polyMesh.H"
00035 #include "Time.H"
00036 #include "transformList.H"
00037
00038
00039
00040 namespace Foam
00041 {
00042 defineTypeNameAndDebug(processorPolyPatch, 0);
00043 addToRunTimeSelectionTable(polyPatch, processorPolyPatch, dictionary);
00044 }
00045
00046
00047
00048
00049 Foam::processorPolyPatch::processorPolyPatch
00050 (
00051 const word& name,
00052 const label size,
00053 const label start,
00054 const label index,
00055 const polyBoundaryMesh& bm,
00056 const int myProcNo,
00057 const int neighbProcNo
00058 )
00059 :
00060 coupledPolyPatch(name, size, start, index, bm),
00061 myProcNo_(myProcNo),
00062 neighbProcNo_(neighbProcNo),
00063 neighbFaceCentres_(),
00064 neighbFaceAreas_(),
00065 neighbFaceCellCentres_(),
00066 neighbPointsPtr_(NULL),
00067 neighbEdgesPtr_(NULL)
00068 {}
00069
00070
00071 Foam::processorPolyPatch::processorPolyPatch
00072 (
00073 const word& name,
00074 const dictionary& dict,
00075 const label index,
00076 const polyBoundaryMesh& bm
00077 )
00078 :
00079 coupledPolyPatch(name, dict, index, bm),
00080 myProcNo_(readLabel(dict.lookup("myProcNo"))),
00081 neighbProcNo_(readLabel(dict.lookup("neighbProcNo"))),
00082 neighbFaceCentres_(),
00083 neighbFaceAreas_(),
00084 neighbFaceCellCentres_(),
00085 neighbPointsPtr_(NULL),
00086 neighbEdgesPtr_(NULL)
00087 {}
00088
00089
00090 Foam::processorPolyPatch::processorPolyPatch
00091 (
00092 const processorPolyPatch& pp,
00093 const polyBoundaryMesh& bm
00094 )
00095 :
00096 coupledPolyPatch(pp, bm),
00097 myProcNo_(pp.myProcNo_),
00098 neighbProcNo_(pp.neighbProcNo_),
00099 neighbFaceCentres_(),
00100 neighbFaceAreas_(),
00101 neighbFaceCellCentres_(),
00102 neighbPointsPtr_(NULL),
00103 neighbEdgesPtr_(NULL)
00104 {}
00105
00106
00107 Foam::processorPolyPatch::processorPolyPatch
00108 (
00109 const processorPolyPatch& pp,
00110 const polyBoundaryMesh& bm,
00111 const label index,
00112 const label newSize,
00113 const label newStart
00114 )
00115 :
00116 coupledPolyPatch(pp, bm, index, newSize, newStart),
00117 myProcNo_(pp.myProcNo_),
00118 neighbProcNo_(pp.neighbProcNo_),
00119 neighbFaceCentres_(),
00120 neighbFaceAreas_(),
00121 neighbFaceCellCentres_(),
00122 neighbPointsPtr_(NULL),
00123 neighbEdgesPtr_(NULL)
00124 {}
00125
00126
00127
00128
00129 Foam::processorPolyPatch::~processorPolyPatch()
00130 {
00131 deleteDemandDrivenData(neighbPointsPtr_);
00132 deleteDemandDrivenData(neighbEdgesPtr_);
00133 }
00134
00135
00136
00137
00138 void Foam::processorPolyPatch::initGeometry()
00139 {
00140 if (Pstream::parRun())
00141 {
00142 OPstream toNeighbProc
00143 (
00144 Pstream::blocking,
00145 neighbProcNo(),
00146 3*(sizeof(label) + size()*sizeof(vector) + sizeof(scalar))
00147 );
00148
00149 toNeighbProc
00150 << faceCentres()
00151 << faceAreas()
00152 << faceCellCentres();
00153 }
00154 }
00155
00156
00157 void Foam::processorPolyPatch::calcGeometry()
00158 {
00159 if (Pstream::parRun())
00160 {
00161 {
00162 IPstream fromNeighbProc
00163 (
00164 Pstream::blocking,
00165 neighbProcNo(),
00166 3*(sizeof(label) + size()*sizeof(vector) + sizeof(scalar))
00167 );
00168 fromNeighbProc
00169 >> neighbFaceCentres_
00170 >> neighbFaceAreas_
00171 >> neighbFaceCellCentres_;
00172 }
00173
00174
00175 vectorField faceNormals(size());
00176
00177
00178 vectorField nbrFaceNormals(neighbFaceAreas_.size());
00179
00180
00181 forAll(faceNormals, facei)
00182 {
00183 scalar magSf = mag(faceAreas()[facei]);
00184 scalar nbrMagSf = mag(neighbFaceAreas_[facei]);
00185 scalar avSf = (magSf + nbrMagSf)/2.0;
00186
00187 if (magSf < ROOTVSMALL && nbrMagSf < ROOTVSMALL)
00188 {
00189
00190
00191
00192 faceNormals[facei] = point(1, 0, 0);
00193 nbrFaceNormals[facei] = faceNormals[facei];
00194 }
00195 else if (mag(magSf - nbrMagSf)/avSf > coupledPolyPatch::matchTol)
00196 {
00197 FatalErrorIn
00198 (
00199 "processorPolyPatch::calcGeometry()"
00200 ) << "face " << facei << " area does not match neighbour by "
00201 << 100*mag(magSf - nbrMagSf)/avSf
00202 << "% -- possible face ordering problem." << endl
00203 << "patch:" << name()
00204 << " my area:" << magSf
00205 << " neighbour area:" << nbrMagSf
00206 << " matching tolerance:" << coupledPolyPatch::matchTol
00207 << endl
00208 << "Mesh face:" << start()+facei
00209 << " vertices:"
00210 << UIndirectList<point>(points(), operator[](facei))()
00211 << endl
00212 << "Rerun with processor debug flag set for"
00213 << " more information." << exit(FatalError);
00214 }
00215 else
00216 {
00217 faceNormals[facei] = faceAreas()[facei]/magSf;
00218 nbrFaceNormals[facei] = neighbFaceAreas_[facei]/nbrMagSf;
00219 }
00220 }
00221
00222 calcTransformTensors
00223 (
00224 faceCentres(),
00225 neighbFaceCentres_,
00226 faceNormals,
00227 nbrFaceNormals,
00228 calcFaceTol(*this, points(), faceCentres())
00229 );
00230 }
00231 }
00232
00233
00234 void Foam::processorPolyPatch::initMovePoints(const pointField& p)
00235 {
00236 polyPatch::movePoints(p);
00237 processorPolyPatch::initGeometry();
00238 }
00239
00240
00241 void Foam::processorPolyPatch::movePoints(const pointField&)
00242 {
00243 processorPolyPatch::calcGeometry();
00244 }
00245
00246
00247 void Foam::processorPolyPatch::initUpdateMesh()
00248 {
00249 polyPatch::initUpdateMesh();
00250
00251 deleteDemandDrivenData(neighbPointsPtr_);
00252 deleteDemandDrivenData(neighbEdgesPtr_);
00253
00254 if (Pstream::parRun())
00255 {
00256
00257 labelList pointFace(nPoints());
00258 labelList pointIndex(nPoints());
00259
00260 for (label patchPointI = 0; patchPointI < nPoints(); patchPointI++)
00261 {
00262 label faceI = pointFaces()[patchPointI][0];
00263
00264 pointFace[patchPointI] = faceI;
00265
00266 const face& f = localFaces()[faceI];
00267
00268 pointIndex[patchPointI] = findIndex(f, patchPointI);
00269 }
00270
00271
00272 labelList edgeFace(nEdges());
00273 labelList edgeIndex(nEdges());
00274
00275 for (label patchEdgeI = 0; patchEdgeI < nEdges(); patchEdgeI++)
00276 {
00277 label faceI = edgeFaces()[patchEdgeI][0];
00278
00279 edgeFace[patchEdgeI] = faceI;
00280
00281 const labelList& fEdges = faceEdges()[faceI];
00282
00283 edgeIndex[patchEdgeI] = findIndex(fEdges, patchEdgeI);
00284 }
00285
00286 OPstream toNeighbProc
00287 (
00288 Pstream::blocking,
00289 neighbProcNo(),
00290 8*sizeof(label)
00291 + 2*nPoints()*sizeof(label)
00292 + 2*nEdges()*sizeof(label)
00293 );
00294
00295 toNeighbProc
00296 << pointFace
00297 << pointIndex
00298 << edgeFace
00299 << edgeIndex;
00300 }
00301 }
00302
00303
00304 void Foam::processorPolyPatch::updateMesh()
00305 {
00306
00307 polyPatch::updateMesh();
00308
00309 if (Pstream::parRun())
00310 {
00311 labelList nbrPointFace;
00312 labelList nbrPointIndex;
00313 labelList nbrEdgeFace;
00314 labelList nbrEdgeIndex;
00315
00316 {
00317
00318
00319 IPstream fromNeighbProc(Pstream::blocking, neighbProcNo());
00320
00321 fromNeighbProc
00322 >> nbrPointFace
00323 >> nbrPointIndex
00324 >> nbrEdgeFace
00325 >> nbrEdgeIndex;
00326 }
00327
00328
00329
00330
00331
00332
00333
00334 neighbPointsPtr_ = new labelList(nPoints(), -1);
00335 labelList& neighbPoints = *neighbPointsPtr_;
00336
00337 forAll(nbrPointFace, nbrPointI)
00338 {
00339
00340 const face& f = localFaces()[nbrPointFace[nbrPointI]];
00341 label index = (f.size() - nbrPointIndex[nbrPointI]) % f.size();
00342 label patchPointI = f[index];
00343
00344 if (neighbPoints[patchPointI] == -1)
00345 {
00346
00347 neighbPoints[patchPointI] = nbrPointI;
00348 }
00349 else if (neighbPoints[patchPointI] >= 0)
00350 {
00351
00352 neighbPoints[patchPointI] = -2;
00353 }
00354 }
00355
00356
00357 forAll(neighbPoints, patchPointI)
00358 {
00359 if (neighbPoints[patchPointI] == -2)
00360 {
00361 neighbPoints[patchPointI] = -1;
00362 }
00363 }
00364
00365
00366
00367
00368 neighbEdgesPtr_ = new labelList(nEdges(), -1);
00369 labelList& neighbEdges = *neighbEdgesPtr_;
00370
00371 forAll(nbrEdgeFace, nbrEdgeI)
00372 {
00373
00374 const labelList& f = faceEdges()[nbrEdgeFace[nbrEdgeI]];
00375 label index = (f.size() - nbrEdgeIndex[nbrEdgeI] - 1) % f.size();
00376 label patchEdgeI = f[index];
00377
00378 if (neighbEdges[patchEdgeI] == -1)
00379 {
00380
00381 neighbEdges[patchEdgeI] = nbrEdgeI;
00382 }
00383 else if (neighbEdges[patchEdgeI] >= 0)
00384 {
00385
00386 neighbEdges[patchEdgeI] = -2;
00387 }
00388 }
00389
00390
00391 forAll(neighbEdges, patchEdgeI)
00392 {
00393 if (neighbEdges[patchEdgeI] == -2)
00394 {
00395 neighbEdges[patchEdgeI] = -1;
00396 }
00397 }
00398
00399
00400 primitivePatch::clearOut();
00401 }
00402 }
00403
00404
00405 const Foam::labelList& Foam::processorPolyPatch::neighbPoints() const
00406 {
00407 if (!neighbPointsPtr_)
00408 {
00409 FatalErrorIn("processorPolyPatch::neighbPoints() const")
00410 << "No extended addressing calculated for patch " << name()
00411 << abort(FatalError);
00412 }
00413 return *neighbPointsPtr_;
00414 }
00415
00416
00417 const Foam::labelList& Foam::processorPolyPatch::neighbEdges() const
00418 {
00419 if (!neighbEdgesPtr_)
00420 {
00421 FatalErrorIn("processorPolyPatch::neighbEdges() const")
00422 << "No extended addressing calculated for patch " << name()
00423 << abort(FatalError);
00424 }
00425 return *neighbEdgesPtr_;
00426 }
00427
00428
00429 void Foam::processorPolyPatch::initOrder(const primitivePatch& pp) const
00430 {
00431 if (!Pstream::parRun())
00432 {
00433 return;
00434 }
00435
00436 if (debug)
00437 {
00438 fileName nm
00439 (
00440 boundaryMesh().mesh().time().path()
00441 /name()+"_faces.obj"
00442 );
00443 Pout<< "processorPolyPatch::order : Writing my " << pp.size()
00444 << " faces to OBJ file " << nm << endl;
00445 writeOBJ(nm, pp, pp.points());
00446
00447
00448 pointField ctrs(calcFaceCentres(pp, pp.points()));
00449
00450 OFstream localStr
00451 (
00452 boundaryMesh().mesh().time().path()
00453 /name() + "_localFaceCentres.obj"
00454 );
00455 Pout<< "processorPolyPatch::order : "
00456 << "Dumping " << ctrs.size()
00457 << " local faceCentres to " << localStr.name() << endl;
00458
00459 forAll(ctrs, faceI)
00460 {
00461 writeOBJ(localStr, ctrs[faceI]);
00462 }
00463 }
00464
00465 const bool isMaster = Pstream::myProcNo() < neighbProcNo();
00466
00467 if (isMaster)
00468 {
00469 pointField ctrs(calcFaceCentres(pp, pp.points()));
00470
00471 pointField anchors(getAnchorPoints(pp, pp.points()));
00472
00473
00474 OPstream toNeighbour(Pstream::blocking, neighbProcNo());
00475 toNeighbour << ctrs << anchors;
00476 }
00477 }
00478
00479
00480
00481
00482
00483
00484 bool Foam::processorPolyPatch::order
00485 (
00486 const primitivePatch& pp,
00487 labelList& faceMap,
00488 labelList& rotation
00489 ) const
00490 {
00491 if (!Pstream::parRun())
00492 {
00493 return false;
00494 }
00495
00496 faceMap.setSize(pp.size());
00497 faceMap = -1;
00498
00499 rotation.setSize(pp.size());
00500 rotation = 0;
00501
00502 const bool isMaster = Pstream::myProcNo() < neighbProcNo();
00503
00504 if (isMaster)
00505 {
00506
00507
00508 forAll(faceMap, patchFaceI)
00509 {
00510 faceMap[patchFaceI] = patchFaceI;
00511 }
00512
00513 return false;
00514 }
00515 else
00516 {
00517 vectorField masterCtrs;
00518 vectorField masterAnchors;
00519
00520
00521 {
00522 IPstream fromNeighbour(Pstream::blocking, neighbProcNo());
00523 fromNeighbour >> masterCtrs >> masterAnchors;
00524 }
00525
00526
00527 pointField ctrs(calcFaceCentres(pp, pp.points()));
00528
00529
00530 scalarField tols(calcFaceTol(pp, pp.points(), ctrs));
00531
00532 if (debug || masterCtrs.size() != pp.size())
00533 {
00534 {
00535 OFstream nbrStr
00536 (
00537 boundaryMesh().mesh().time().path()
00538 /name() + "_nbrFaceCentres.obj"
00539 );
00540 Pout<< "processorPolyPatch::order : "
00541 << "Dumping neighbour faceCentres to " << nbrStr.name()
00542 << endl;
00543 forAll(masterCtrs, faceI)
00544 {
00545 writeOBJ(nbrStr, masterCtrs[faceI]);
00546 }
00547 }
00548
00549 if (masterCtrs.size() != pp.size())
00550 {
00551 FatalErrorIn
00552 (
00553 "processorPolyPatch::order(const primitivePatch&"
00554 ", labelList&, labelList&) const"
00555 ) << "in patch:" << name() << " : "
00556 << "Local size of patch is " << pp.size() << " (faces)."
00557 << endl
00558 << "Received from neighbour " << masterCtrs.size()
00559 << " faceCentres!"
00560 << abort(FatalError);
00561 }
00562 }
00563
00564
00565
00566
00567
00568 bool matchedAll = false;
00569
00570 if
00571 (
00572 separated()
00573 && (separation().size() == 1 || separation().size() == pp.size())
00574 )
00575 {
00576 vectorField transformedCtrs;
00577
00578 const vectorField& v = separation();
00579
00580 if (v.size() == 1)
00581 {
00582 transformedCtrs = masterCtrs-v[0];
00583 }
00584 else
00585 {
00586 transformedCtrs = masterCtrs-v;
00587 }
00588 matchedAll = matchPoints
00589 (
00590 ctrs,
00591 transformedCtrs,
00592 tols,
00593 true,
00594 faceMap
00595 );
00596
00597 if (matchedAll)
00598 {
00599
00600 masterCtrs = transformedCtrs;
00601
00602
00603 if (v.size() == 1)
00604 {
00605 masterAnchors -= v[0];
00606 }
00607 else
00608 {
00609 masterAnchors -= v;
00610 }
00611 }
00612 }
00613 else if
00614 (
00615 !parallel()
00616 && (forwardT().size() == 1 || forwardT().size() == pp.size())
00617 )
00618 {
00619 vectorField transformedCtrs = masterCtrs;
00620 transformList(forwardT(), transformedCtrs);
00621 matchedAll = matchPoints
00622 (
00623 ctrs,
00624 transformedCtrs,
00625 tols,
00626 true,
00627 faceMap
00628 );
00629
00630 if (matchedAll)
00631 {
00632
00633 masterCtrs = transformedCtrs;
00634
00635
00636 transformList(forwardT(), masterAnchors);
00637 }
00638 }
00639
00640
00641
00642 if (!matchedAll)
00643 {
00644 matchedAll = matchPoints(ctrs, masterCtrs, tols, true, faceMap);
00645 }
00646
00647 if (!matchedAll || debug)
00648 {
00649
00650 fileName str
00651 (
00652 boundaryMesh().mesh().time().path()
00653 /name()/name()+"_faces.obj"
00654 );
00655 Pout<< "processorPolyPatch::order :"
00656 << " Writing faces to OBJ file " << str.name() << endl;
00657 writeOBJ(str, pp, pp.points());
00658
00659 OFstream ccStr
00660 (
00661 boundaryMesh().mesh().time().path()
00662 /name() + "_faceCentresConnections.obj"
00663 );
00664
00665 Pout<< "processorPolyPatch::order :"
00666 << " Dumping newly found match as lines between"
00667 << " corresponding face centres to OBJ file " << ccStr.name()
00668 << endl;
00669
00670 label vertI = 0;
00671
00672 forAll(ctrs, faceI)
00673 {
00674 label masterFaceI = faceMap[faceI];
00675
00676 if (masterFaceI != -1)
00677 {
00678 const point& c0 = masterCtrs[masterFaceI];
00679 const point& c1 = ctrs[faceI];
00680 writeOBJ(ccStr, c0, c1, vertI);
00681 }
00682 }
00683 }
00684
00685 if (!matchedAll)
00686 {
00687 SeriousErrorIn
00688 (
00689 "processorPolyPatch::order(const primitivePatch&"
00690 ", labelList&, labelList&) const"
00691 ) << "in patch:" << name() << " : "
00692 << "Cannot match vectors to faces on both sides of patch"
00693 << endl
00694 << " masterCtrs[0]:" << masterCtrs[0] << endl
00695 << " ctrs[0]:" << ctrs[0] << endl
00696 << " Please check your topology changes or maybe you have"
00697 << " multiple separated (from cyclics) processor patches"
00698 << endl
00699 << " Continuing with incorrect face ordering from now on!"
00700 << endl;
00701
00702 return false;
00703 }
00704
00705
00706 forAll(faceMap, oldFaceI)
00707 {
00708
00709
00710
00711
00712 label newFaceI = faceMap[oldFaceI];
00713
00714 const point& wantedAnchor = masterAnchors[newFaceI];
00715
00716 rotation[newFaceI] = getRotation
00717 (
00718 pp.points(),
00719 pp[oldFaceI],
00720 wantedAnchor,
00721 tols[oldFaceI]
00722 );
00723
00724 if (rotation[newFaceI] == -1)
00725 {
00726 SeriousErrorIn
00727 (
00728 "processorPolyPatch::order(const primitivePatch&"
00729 ", labelList&, labelList&) const"
00730 ) << "in patch " << name()
00731 << " : "
00732 << "Cannot find point on face " << pp[oldFaceI]
00733 << " with vertices "
00734 << UIndirectList<point>(pp.points(), pp[oldFaceI])()
00735 << " that matches point " << wantedAnchor
00736 << " when matching the halves of processor patch " << name()
00737 << "Continuing with incorrect face ordering from now on!"
00738 << endl;
00739
00740 return false;
00741 }
00742 }
00743
00744 forAll(faceMap, faceI)
00745 {
00746 if (faceMap[faceI] != faceI || rotation[faceI] != 0)
00747 {
00748 return true;
00749 }
00750 }
00751
00752 return false;
00753 }
00754 }
00755
00756
00757 void Foam::processorPolyPatch::write(Ostream& os) const
00758 {
00759 polyPatch::write(os);
00760 os.writeKeyword("myProcNo") << myProcNo_
00761 << token::END_STATEMENT << nl;
00762 os.writeKeyword("neighbProcNo") << neighbProcNo_
00763 << token::END_STATEMENT << nl;
00764 }
00765
00766
00767