OpenFOAM logo
The Open Source CFD Toolbox
  Source Guide OpenCFD Solutions Contact OpenFOAM

perfectInterface.C

Go to the documentation of this file.
00001 /*---------------------------------------------------------------------------*\
00002   =========                 |
00003   \\      /  F ield         | OpenFOAM: The Open Source CFD Toolbox
00004    \\    /   O peration     |
00005     \\  /    A nd           | Copyright (C) 1991-2009 OpenCFD Ltd.
00006      \\/     M anipulation  |
00007 -------------------------------------------------------------------------------
00008 License
00009     This file is part of OpenFOAM.
00010 
00011     OpenFOAM is free software; you can redistribute it and/or modify it
00012     under the terms of the GNU General Public License as published by the
00013     Free Software Foundation; either version 2 of the License, or (at your
00014     option) any later version.
00015 
00016     OpenFOAM is distributed in the hope that it will be useful, but WITHOUT
00017     ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
00018     FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
00019     for more details.
00020 
00021     You should have received a copy of the GNU General Public License
00022     along with OpenFOAM; if not, write to the Free Software Foundation,
00023     Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
00024 
00025 Description
00026     Best thing is probably to look at attachDetach which does almost exactly
00027     the same but for the geometric matching of points and face centres.
00028 
00029 \*---------------------------------------------------------------------------*/
00030 
00031 #include "perfectInterface.H"
00032 #include "polyTopoChanger.H"
00033 #include "polyMesh.H"
00034 #include "polyTopoChange.H"
00035 #include "addToRunTimeSelectionTable.H"
00036 #include "mapPolyMesh.H"
00037 #include "matchPoints.H"
00038 #include "polyModifyFace.H"
00039 #include "polyRemovePoint.H"
00040 #include "polyRemoveFace.H"
00041 
00042 // * * * * * * * * * * * * * * Static Data Members * * * * * * * * * * * * * //
00043 
00044 namespace Foam
00045 {
00046     defineTypeNameAndDebug(perfectInterface, 0);
00047     addToRunTimeSelectionTable
00048     (
00049         polyMeshModifier,
00050         perfectInterface,
00051         dictionary
00052     );
00053 }
00054 
00055 
00056 // Tolerance used as fraction of minimum edge length.
00057 const Foam::scalar Foam::perfectInterface::tol_ = 1E-3;
00058 
00059 
00060 // * * * * * * * * * * * * * Private Member Functions  * * * * * * * * * * * //
00061 
00062 Foam::pointField Foam::perfectInterface::calcFaceCentres
00063 (
00064     const primitivePatch& pp
00065 )
00066 {
00067     const pointField& points = pp.points();
00068 
00069     pointField ctrs(pp.size());
00070 
00071     forAll(ctrs, patchFaceI)
00072     {
00073         ctrs[patchFaceI] = pp[patchFaceI].centre(points);
00074     }
00075 
00076     return ctrs;
00077 }
00078 
00079 
00080 // * * * * * * * * * * * * * * * * Constructors  * * * * * * * * * * * * * * //
00081 
00082 // Construct from components
00083 Foam::perfectInterface::perfectInterface
00084 (
00085     const word& name,
00086     const label index,
00087     const polyTopoChanger& mme,
00088     const word& faceZoneName,
00089     const word& masterPatchName,
00090     const word& slavePatchName
00091 )
00092 :
00093     polyMeshModifier(name, index, mme, true),
00094     faceZoneID_(faceZoneName, mme.mesh().faceZones()),
00095     masterPatchID_(masterPatchName, mme.mesh().boundaryMesh()),
00096     slavePatchID_(slavePatchName, mme.mesh().boundaryMesh())
00097 {}
00098 
00099 
00100 // Construct from dictionary
00101 Foam::perfectInterface::perfectInterface
00102 (
00103     const word& name,
00104     const dictionary& dict,
00105     const label index,
00106     const polyTopoChanger& mme
00107 )
00108 :
00109     polyMeshModifier(name, index, mme, readBool(dict.lookup("active"))),
00110     faceZoneID_
00111     (
00112         dict.lookup("faceZoneName"),
00113         mme.mesh().faceZones()
00114     ),
00115     masterPatchID_
00116     (
00117         dict.lookup("masterPatchName"),
00118         mme.mesh().boundaryMesh()
00119     ),
00120     slavePatchID_
00121     (
00122         dict.lookup("slavePatchName"),
00123         mme.mesh().boundaryMesh()
00124     )
00125 {}
00126 
00127 
00128 // * * * * * * * * * * * * * * * * Destructor  * * * * * * * * * * * * * * * //
00129 
00130 Foam::perfectInterface::~perfectInterface()
00131 {}
00132 
00133 
00134 // * * * * * * * * * * * * * * * Member Functions  * * * * * * * * * * * * * //
00135 
00136 bool Foam::perfectInterface::changeTopology() const
00137 {
00138     // If modifier is inactive, skip change
00139     if (!active())
00140     {
00141         if (debug)
00142         {
00143             Pout<< "bool perfectInterface::changeTopology() const "
00144                 << "for object " << name() << " : "
00145                 << "Inactive" << endl;
00146         }
00147 
00148         return false;
00149     }
00150     else
00151     {
00152         // I want topo change every time step.
00153         return true;
00154     }
00155 }
00156 
00157 
00158 void Foam::perfectInterface::setRefinement(polyTopoChange& ref) const
00159 {
00160     if (debug)
00161     {
00162         Pout<< "bool perfectInterface::setRefinement(polyTopoChange&) const : "
00163             << "for object " << name() << " : "
00164             << "masterPatchID_:" << masterPatchID_
00165             << " slavePatchID_:" << slavePatchID_
00166             << " faceZoneID_:" << faceZoneID_ << endl;
00167     }
00168 
00169     if
00170     (
00171         masterPatchID_.active()
00172      && slavePatchID_.active()
00173      && faceZoneID_.active()
00174     )
00175     {
00176         const polyMesh& mesh = topoChanger().mesh();
00177 
00178         const polyBoundaryMesh& patches = mesh.boundaryMesh();
00179 
00180         const polyPatch& pp0 = patches[masterPatchID_.index()];
00181         const polyPatch& pp1 = patches[slavePatchID_.index()];
00182 
00183         // Some aliases
00184         const edgeList& edges0 = pp0.edges();
00185         const pointField& pts0 = pp0.localPoints();
00186         const pointField& pts1 = pp1.localPoints();    
00187         const labelList& meshPts0 = pp0.meshPoints();
00188         const labelList& meshPts1 = pp1.meshPoints();
00189                         
00190 
00191         // Get local dimension as fraction of minimum edge length
00192 
00193         scalar minLen = GREAT;
00194 
00195         forAll(edges0, edgeI)
00196         {
00197             minLen = min(minLen, edges0[edgeI].mag(pts0));
00198         }
00199         scalar typDim = tol_*minLen;
00200 
00201         if (debug)
00202         {
00203             Pout<< "typDim:" << typDim << " edges0:" << edges0.size()
00204                 << " pts0:" << pts0.size() << " pts1:" << pts1.size()
00205                 << " pp0:" << pp0.size() << " pp1:" << pp1.size() << endl;
00206         }
00207 
00208 
00209         // Determine pointMapping in mesh point labels. Uses geometric
00210         // comparison to find correspondence between patch points.
00211 
00212         labelList renumberPoints(mesh.points().size());
00213         forAll(renumberPoints, i)
00214         {
00215             renumberPoints[i] = i;
00216         }
00217         {
00218             labelList from1To0Points(pts1.size());
00219 
00220             bool matchOk = matchPoints
00221             (
00222                 pts1,
00223                 pts0,
00224                 scalarField(pts1.size(), typDim),   // tolerance
00225                 true,                               // verbose
00226                 from1To0Points
00227             );
00228 
00229             if (!matchOk)
00230             {
00231                 FatalErrorIn
00232                 (
00233                     "perfectInterface::setRefinement(polyTopoChange& ref) const"
00234                 )   << "Points on patches " << pp0.name() << " and "
00235                     << pp1.name() << " do not match to within tolerance "
00236                     << typDim << exit(FatalError);
00237             }
00238 
00239             forAll(pts1, i)
00240             {
00241                 renumberPoints[meshPts1[i]] = meshPts0[from1To0Points[i]];
00242             }
00243         }
00244 
00245 
00246 
00247         // Calculate correspondence between patch faces
00248 
00249         labelList from0To1Faces(pp1.size());
00250 
00251         bool matchOk = matchPoints
00252         (
00253             calcFaceCentres(pp0),
00254             calcFaceCentres(pp1),
00255             scalarField(pp0.size(), typDim),    // tolerance
00256             true,                               // verbose
00257             from0To1Faces
00258         );
00259 
00260         if (!matchOk)
00261         {
00262             FatalErrorIn
00263             (
00264                 "perfectInterface::setRefinement(polyTopoChange& ref) const"
00265             )   << "Face centres of patches " << pp0.name() << " and "
00266                 << pp1.name() << " do not match to within tolerance " << typDim
00267                 << exit(FatalError);
00268         }
00269 
00270 
00271 
00272         // Now
00273         // - renumber faces using pts1 (except patch1 faces)
00274         // - remove patch1 faces. Remember cell label on owner side.
00275         // - modify patch0 faces to be internal.
00276 
00277         // 1. Get faces to be renumbered
00278         labelHashSet affectedFaces(2*pp1.size());
00279         forAll(meshPts1, i)
00280         {
00281             label meshPointI = meshPts1[i];
00282 
00283             if (meshPointI != renumberPoints[meshPointI])
00284             {
00285                 const labelList& pFaces = mesh.pointFaces()[meshPointI];
00286 
00287                 forAll(pFaces, pFaceI)
00288                 {
00289                     affectedFaces.insert(pFaces[pFaceI]);
00290                 }
00291             }
00292         }
00293         forAll(pp1, i)
00294         {
00295             affectedFaces.erase(pp1.start() + i);
00296         }
00297         // Remove patch0 from renumbered faces. Should not be nessecary since
00298         // patch0 and 1 should not share any point (if created by mergeMeshing)
00299         // so affectedFaces should not contain any patch0 faces but you can
00300         // never be sure what the user is doing.
00301         forAll(pp0, i)
00302         {
00303             if (affectedFaces.erase(pp0.start() + i))
00304             {
00305                 WarningIn
00306                 (
00307                     "perfectInterface::setRefinement(polyTopoChange&) const"
00308                 )   << "Found face " << pp0.start() + i << " vertices "
00309                     << mesh.faces()[pp0.start() + i] << " whose points are"
00310                     << " used both by master patch " << pp0.name()
00311                     << " and slave patch " << pp1.name()
00312                     << endl;
00313             }
00314         }
00315 
00316 
00317         // 2. Renumber (non patch0/1) faces.
00318         for
00319         (
00320             labelHashSet::const_iterator iter = affectedFaces.begin();
00321             iter != affectedFaces.end();
00322             ++iter
00323         )
00324         {
00325             label faceI = iter.key();
00326 
00327             const face& f = mesh.faces()[faceI];
00328 
00329             face newFace(f.size());
00330 
00331             forAll(newFace, fp)
00332             {
00333                 newFace[fp] = renumberPoints[f[fp]];
00334             }
00335 
00336             label nbr = -1;
00337 
00338             label patchI = -1;
00339 
00340             if (mesh.isInternalFace(faceI))
00341             {
00342                 nbr = mesh.faceNeighbour()[faceI];
00343             }
00344             else
00345             {
00346                 patchI = patches.whichPatch(faceI);
00347             }
00348 
00349             label zoneID = mesh.faceZones().whichZone(faceI);
00350 
00351             bool zoneFlip = false;
00352 
00353             if (zoneID >= 0)
00354             {
00355                 const faceZone& fZone = mesh.faceZones()[zoneID];
00356 
00357                 zoneFlip = fZone.flipMap()[fZone.whichFace(faceI)];
00358             }
00359 
00360             ref.setAction
00361             (
00362                 polyModifyFace
00363                 (
00364                     newFace,                    // modified face
00365                     faceI,                      // label of face being modified
00366                     mesh.faceOwner()[faceI],    // owner
00367                     nbr,                        // neighbour
00368                     false,                      // face flip
00369                     patchI,                     // patch for face
00370                     false,                      // remove from zone
00371                     zoneID,                     // zone for face
00372                     zoneFlip                    // face flip in zone
00373                 )
00374             );
00375         }
00376 
00377 
00378         // 3. Remove patch1 points
00379         forAll(meshPts1, i)
00380         {
00381             label meshPointI = meshPts1[i];
00382 
00383             if (meshPointI != renumberPoints[meshPointI])
00384             {
00385                 ref.setAction(polyRemovePoint(meshPointI));
00386             }
00387         }
00388 
00389 
00390         // 4. Remove patch1 faces
00391         forAll(pp1, i)
00392         {
00393             ref.setAction(polyRemoveFace(pp1.start() + i));
00394         }
00395 
00396 
00397         // 5. Modify patch0 faces for new points (not really nessecary; see
00398         // comment above about patch1 and patch0 never sharing points) and
00399         // becoming internal.
00400         const boolList& mfFlip =
00401             mesh.faceZones()[faceZoneID_.index()].flipMap();
00402 
00403         forAll(pp0, i)
00404         {
00405             label faceI = pp0.start() + i;
00406 
00407             const face& f = mesh.faces()[faceI];
00408 
00409             face newFace(f.size());
00410 
00411             forAll(newFace, fp)
00412             {
00413                 newFace[fp] = renumberPoints[f[fp]];
00414             }
00415 
00416             label own = mesh.faceOwner()[faceI];
00417 
00418             label pp1FaceI = pp1.start() + from0To1Faces[i];
00419 
00420             label nbr = mesh.faceOwner()[pp1FaceI];
00421 
00422             if (own < nbr)
00423             {
00424                 ref.setAction
00425                 (
00426                     polyModifyFace
00427                     (
00428                         newFace,                // modified face
00429                         faceI,                  // label of face being modified
00430                         own,                    // owner
00431                         nbr,                    // neighbour
00432                         false,                  // face flip
00433                         -1,                     // patch for face
00434                         false,                  // remove from zone
00435                         faceZoneID_.index(),    // zone for face
00436                         mfFlip[i]               // face flip in zone
00437                     )
00438                 );
00439             }
00440             else
00441             {
00442                 ref.setAction
00443                 (
00444                     polyModifyFace
00445                     (
00446                         newFace.reverseFace(),  // modified face
00447                         faceI,                  // label of face being modified
00448                         nbr,                    // owner
00449                         own,                    // neighbour
00450                         false,                  // face flip
00451                         -1,                     // patch for face
00452                         false,                  // remove from zone
00453                         faceZoneID_.index(),    // zone for face
00454                         !mfFlip[i]              // face flip in zone
00455                     )
00456                 );
00457             }
00458         }
00459     }
00460 }
00461 
00462 
00463 void Foam::perfectInterface::modifyMotionPoints(pointField& motionPoints) const
00464 {
00465     // Update only my points. Nothing to be done here as points already
00466     // shared by now.
00467 }
00468 
00469 
00470 void Foam::perfectInterface::updateMesh(const mapPolyMesh& morphMap)
00471 {
00472     // Mesh has changed topologically.  Update local topological data
00473     const polyMesh& mesh = topoChanger().mesh();
00474 
00475     faceZoneID_.update(mesh.faceZones());
00476     masterPatchID_.update(mesh.boundaryMesh());
00477     slavePatchID_.update(mesh.boundaryMesh());
00478 }
00479 
00480 
00481 void Foam::perfectInterface::write(Ostream& os) const
00482 {
00483     os  << nl << type() << nl
00484         << name()<< nl
00485         << faceZoneID_.name() << nl
00486         << masterPatchID_.name() << nl
00487         << slavePatchID_.name() << endl;
00488 }
00489 
00490 
00491 void Foam::perfectInterface::writeDict(Ostream& os) const
00492 {
00493     os  << nl << name() << nl << token::BEGIN_BLOCK << nl
00494 
00495         << "    type " << type()
00496         << token::END_STATEMENT << nl
00497 
00498         << "    active " << active()
00499         << token::END_STATEMENT << nl
00500 
00501         << "    faceZoneName " << faceZoneID_.name()
00502         << token::END_STATEMENT << nl
00503 
00504         << "    masterPatchName " << masterPatchID_.name()
00505         << token::END_STATEMENT << nl
00506 
00507         << "    slavePatchName " << slavePatchID_.name()
00508         << token::END_STATEMENT << nl
00509 
00510         << token::END_BLOCK << endl;
00511 }
00512 
00513 
00514 // * * * * * * * * * * * * * * * Member Operators  * * * * * * * * * * * * * //
00515 
00516 
00517 // * * * * * * * * * * * * * * * Friend Functions  * * * * * * * * * * * * * //
00518 
00519 
00520 // * * * * * * * * * * * * * * * Friend Operators  * * * * * * * * * * * * * //
00521 
00522 
00523 // ************************************************************************* //
Copyright © 2000-2009 OpenCFD Ltd