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

motionSmoother.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 \*---------------------------------------------------------------------------*/
00026 
00027 #include "motionSmoother.H"
00028 #include "twoDPointCorrector.H"
00029 #include "faceSet.H"
00030 #include "pointSet.H"
00031 #include "fixedValuePointPatchFields.H"
00032 #include "pointConstraint.H"
00033 #include "syncTools.H"
00034 #include "meshTools.H"
00035 #include "OFstream.H"
00036 
00037 namespace Foam
00038 {
00039 
00040 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
00041 
00042 defineTypeNameAndDebug(motionSmoother, 0);
00043 
00044 }
00045 
00046 
00047 // * * * * * * * * * * * * * Private Member Functions  * * * * * * * * * * * //
00048 
00049 // From pointPatchInterpolation
00050 void Foam::motionSmoother::makePatchPatchAddressing()
00051 {
00052     if (debug)
00053     {
00054         Pout<< "motionSmoother::makePatchPatchAddressing() : "
00055             << "constructing boundary addressing"
00056             << endl;
00057     }
00058 
00059     const polyBoundaryMesh& bm = mesh_.boundaryMesh();
00060     const pointBoundaryMesh& pbm = pMesh_.boundary();
00061 
00062     // first count the total number of patch-patch points
00063 
00064     label nPatchPatchPoints = 0;
00065 
00066     forAll(bm, patchi)
00067     {
00068         if(!isA<emptyPolyPatch>(bm[patchi]))
00069         {
00070             nPatchPatchPoints += bm[patchi].boundaryPoints().size();
00071         }
00072     }
00073 
00074 
00075     // Go through all patches and mark up the external edge points
00076     Map<label> patchPatchPointSet(2*nPatchPatchPoints);
00077 
00078     labelList patchPatchPoints(nPatchPatchPoints);
00079 
00080     List<pointConstraint> patchPatchPointConstraints(nPatchPatchPoints);
00081 
00082     label pppi = 0;
00083 
00084     forAll(bm, patchi)
00085     {
00086         if(!isA<emptyPolyPatch>(bm[patchi]))
00087         {
00088             const labelList& bp = bm[patchi].boundaryPoints();
00089             const labelList& meshPoints = bm[patchi].meshPoints();
00090 
00091             forAll(bp, pointi)
00092             {
00093                 label ppp = meshPoints[bp[pointi]];
00094 
00095                 Map<label>::iterator iter = patchPatchPointSet.find(ppp);
00096 
00097                 if (iter == patchPatchPointSet.end())
00098                 {
00099                     patchPatchPointSet.insert(ppp, pppi);
00100                     patchPatchPoints[pppi] = ppp;
00101                     pbm[patchi].applyConstraint
00102                     (
00103                         bp[pointi],
00104                         patchPatchPointConstraints[pppi]
00105                     );
00106                     pppi++;
00107                 }
00108                 else
00109                 {
00110                     pbm[patchi].applyConstraint
00111                     (
00112                         bp[pointi],
00113                         patchPatchPointConstraints[iter()]
00114                     );
00115                 }
00116             }
00117         }
00118     }
00119 
00120     nPatchPatchPoints = pppi;
00121     patchPatchPoints.setSize(nPatchPatchPoints);
00122     patchPatchPointConstraints.setSize(nPatchPatchPoints);
00123 
00124     patchPatchPointConstraintPoints_.setSize(nPatchPatchPoints);
00125     patchPatchPointConstraintTensors_.setSize(nPatchPatchPoints);
00126 
00127     label nConstraints = 0;
00128 
00129     forAll(patchPatchPointConstraints, i)
00130     {
00131         if (patchPatchPointConstraints[i].first() != 0)
00132         {
00133             patchPatchPointConstraintPoints_[nConstraints] =
00134                 patchPatchPoints[i];
00135 
00136             patchPatchPointConstraintTensors_[nConstraints] =
00137                 patchPatchPointConstraints[i].constraintTransformation();
00138 
00139             nConstraints++;
00140         }
00141     }
00142 
00143     patchPatchPointConstraintPoints_.setSize(nConstraints);
00144     patchPatchPointConstraintTensors_.setSize(nConstraints);
00145 
00146 
00147     if (debug)
00148     {
00149         OFstream str(mesh_.db().path()/"constraintPoints.obj");
00150 
00151         Pout<< "Dumping " << patchPatchPointConstraintPoints_.size()
00152             << " constraintPoints to " << str.name() << endl;
00153         forAll(patchPatchPointConstraintPoints_, i)
00154         {
00155             label pointI = patchPatchPointConstraintPoints_[i];
00156 
00157             meshTools::writeOBJ(str, mesh_.points()[pointI]);
00158         }
00159 
00160         Pout<< "motionSmoother::makePatchPatchAddressing() : "
00161             << "finished constructing boundary addressing"
00162             << endl;
00163     }
00164 }
00165 
00166 
00167 void Foam::motionSmoother::checkFld(const pointScalarField& fld)
00168 {
00169     forAll(fld, pointI)
00170     {
00171         const scalar val = fld[pointI];
00172 
00173         if ((val > -GREAT) && (val < GREAT))
00174         {}
00175         else
00176         {
00177             FatalErrorIn("motionSmoother::checkFld")
00178                 << "Problem : point:" << pointI << " value:" << val
00179                 << abort(FatalError);
00180         }
00181     }
00182 }
00183 
00184 
00185 Foam::labelHashSet Foam::motionSmoother::getPoints
00186 (
00187     const labelHashSet& faceLabels
00188 ) const
00189 {
00190     labelHashSet usedPoints(mesh_.nPoints()/100);
00191 
00192     forAllConstIter(labelHashSet, faceLabels, iter)
00193     {
00194         const face& f = mesh_.faces()[iter.key()];
00195 
00196         forAll(f, fp)
00197         {
00198             usedPoints.insert(f[fp]);
00199         }
00200     }
00201 
00202     return usedPoints;
00203 }
00204 
00205 
00206 // Smooth on selected points (usually patch points)
00207 void Foam::motionSmoother::minSmooth
00208 (
00209     const PackedBoolList& isAffectedPoint,
00210     const labelList& meshPoints,
00211     const pointScalarField& fld,
00212     pointScalarField& newFld
00213 ) const
00214 {
00215     tmp<pointScalarField> tavgFld = avg
00216     (
00217         fld,
00218         scalarField(mesh_.nEdges(), 1.0),   // uniform weighting
00219         false                               // fld is not position.
00220     );
00221     const pointScalarField& avgFld = tavgFld();
00222 
00223     forAll(meshPoints, i)
00224     {
00225         label pointI = meshPoints[i];
00226         if (isAffectedPoint.get(pointI) == 1)
00227         {
00228             newFld[pointI] = min
00229             (
00230                 fld[pointI],
00231                 0.5*fld[pointI] + 0.5*avgFld[pointI]
00232             );
00233         }
00234     }
00235 
00236     newFld.correctBoundaryConditions();
00237     applyCornerConstraints(newFld);
00238 }
00239 
00240 
00241 // Smooth on all internal points
00242 void Foam::motionSmoother::minSmooth
00243 (
00244     const PackedBoolList& isAffectedPoint,
00245     const pointScalarField& fld,
00246     pointScalarField& newFld
00247 ) const
00248 {
00249     tmp<pointScalarField> tavgFld = avg
00250     (
00251         fld,
00252         scalarField(mesh_.nEdges(), 1.0),   // uniform weighting
00253         false                               // fld is not position.
00254     );
00255     const pointScalarField& avgFld = tavgFld();
00256 
00257     forAll(fld, pointI)
00258     {
00259         if (isAffectedPoint.get(pointI) == 1 && isInternalPoint(pointI))
00260         {
00261             newFld[pointI] = min
00262             (
00263                 fld[pointI],
00264                 0.5*fld[pointI] + 0.5*avgFld[pointI]
00265             );
00266         }
00267     }
00268 
00269     newFld.correctBoundaryConditions();
00270     applyCornerConstraints(newFld);
00271 }
00272 
00273 
00274 // Scale on selected points
00275 void Foam::motionSmoother::scaleField
00276 (
00277     const labelHashSet& pointLabels,
00278     const scalar scale,
00279     pointScalarField& fld
00280 ) const
00281 {
00282     forAllConstIter(labelHashSet, pointLabels, iter)
00283     {
00284         if (isInternalPoint(iter.key()))
00285         {
00286             fld[iter.key()] *= scale;
00287         }
00288     }
00289     fld.correctBoundaryConditions();
00290     applyCornerConstraints(fld);
00291 }
00292 
00293 
00294 // Scale on selected points (usually patch points)
00295 void Foam::motionSmoother::scaleField
00296 (
00297     const labelList& meshPoints,
00298     const labelHashSet& pointLabels,
00299     const scalar scale,
00300     pointScalarField& fld
00301 ) const
00302 {
00303     forAll(meshPoints, i)
00304     {
00305         label pointI = meshPoints[i];
00306 
00307         if (pointLabels.found(pointI))
00308         {
00309             fld[pointI] *= scale;
00310         }
00311     }
00312 }
00313 
00314 
00315 bool Foam::motionSmoother::isInternalPoint(const label pointI) const
00316 {
00317     return isInternalPoint_.get(pointI) == 1;
00318 }
00319 
00320 
00321 void Foam::motionSmoother::getAffectedFacesAndPoints
00322 (
00323     const label nPointIter,
00324     const faceSet& wrongFaces,
00325 
00326     labelList& affectedFaces,
00327     PackedBoolList& isAffectedPoint
00328 ) const
00329 {
00330     isAffectedPoint.setSize(mesh_.nPoints());
00331     isAffectedPoint = 0;
00332 
00333     faceSet nbrFaces(mesh_, "checkFaces", wrongFaces);
00334 
00335     // Find possible points influenced by nPointIter iterations of
00336     // scaling and smoothing by doing pointCellpoint walk.
00337     // Also update faces-to-be-checked to extend one layer beyond the points
00338     // that will get updated.
00339 
00340     for (label i = 0; i < nPointIter; i++)
00341     {
00342         pointSet nbrPoints(mesh_, "grownPoints", getPoints(nbrFaces.toc()));
00343 
00344         forAllConstIter(pointSet, nbrPoints, iter)
00345         {
00346             const labelList& pCells = mesh_.pointCells(iter.key());
00347 
00348             forAll(pCells, pCellI)
00349             {
00350                 const cell& cFaces = mesh_.cells()[pCells[pCellI]];
00351 
00352                 forAll(cFaces, cFaceI)
00353                 {
00354                     nbrFaces.insert(cFaces[cFaceI]);
00355                 }
00356             }
00357         }
00358         nbrFaces.sync(mesh_);
00359 
00360         if (i == nPointIter - 2)
00361         {
00362             forAllConstIter(faceSet, nbrFaces, iter)
00363             {
00364                 const face& f = mesh_.faces()[iter.key()];
00365                 forAll(f, fp)
00366                 {
00367                     isAffectedPoint.set(f[fp], 1);
00368                 }
00369             }
00370         }
00371     }
00372 
00373     affectedFaces = nbrFaces.toc();
00374 }
00375 
00376 
00377 // * * * * * * * * * * * * * * * * Constructors  * * * * * * * * * * * * * * //
00378 
00379 Foam::motionSmoother::motionSmoother
00380 (
00381     polyMesh& mesh,
00382     pointMesh& pMesh,
00383     indirectPrimitivePatch& pp,
00384     const labelList& adaptPatchIDs,
00385     const dictionary& paramDict
00386 )
00387 :
00388     mesh_(mesh),
00389     pMesh_(pMesh),
00390     pp_(pp),
00391     adaptPatchIDs_(adaptPatchIDs),
00392     paramDict_(paramDict),
00393     displacement_
00394     (
00395         IOobject
00396         (
00397             "displacement",
00398             mesh_.time().timeName(),
00399             mesh_,
00400             IOobject::MUST_READ,
00401             IOobject::AUTO_WRITE
00402         ),
00403         pMesh_
00404     ),
00405     scale_
00406     (
00407         IOobject
00408         (
00409             "scale",
00410             mesh_.time().timeName(),
00411             mesh_,
00412             IOobject::NO_READ,
00413             IOobject::AUTO_WRITE
00414         ),
00415         pMesh_,
00416         dimensionedScalar("scale", dimless, 1.0)
00417     ),
00418     oldPoints_(mesh_.points()),
00419     isInternalPoint_(mesh_.nPoints(), 1),
00420     twoDCorrector_(mesh_)
00421 {
00422     updateMesh();
00423 }
00424 
00425 
00426 Foam::motionSmoother::motionSmoother
00427 (
00428     polyMesh& mesh,
00429     indirectPrimitivePatch& pp,
00430     const labelList& adaptPatchIDs,
00431     const pointVectorField& displacement,
00432     const dictionary& paramDict
00433 )
00434 :
00435     mesh_(mesh),
00436     pMesh_(const_cast<pointMesh&>(displacement.mesh())),
00437     pp_(pp),
00438     adaptPatchIDs_(adaptPatchIDs),
00439     paramDict_(paramDict),
00440     displacement_
00441     (
00442         IOobject
00443         (
00444             "displacement",
00445             mesh_.time().timeName(),
00446             mesh_,
00447             IOobject::NO_READ,
00448             IOobject::AUTO_WRITE
00449         ),
00450         displacement
00451     ),
00452     scale_
00453     (
00454         IOobject
00455         (
00456             "scale",
00457             mesh_.time().timeName(),
00458             mesh_,
00459             IOobject::NO_READ,
00460             IOobject::AUTO_WRITE
00461         ),
00462         pMesh_,
00463         dimensionedScalar("scale", dimless, 1.0)
00464     ),
00465     oldPoints_(mesh_.points()),
00466     isInternalPoint_(mesh_.nPoints(), 1),
00467     twoDCorrector_(mesh_)
00468 {
00469     updateMesh();
00470 }
00471 
00472 
00473 // * * * * * * * * * * * * * * * * Destructor  * * * * * * * * * * * * * * * //
00474 
00475 Foam::motionSmoother::~motionSmoother()
00476 {}
00477 
00478 
00479 // * * * * * * * * * * * * * * * Member Functions  * * * * * * * * * * * * * //
00480 
00481 const Foam::polyMesh& Foam::motionSmoother::mesh() const
00482 {
00483     return mesh_;
00484 }
00485 
00486 
00487 const Foam::pointMesh& Foam::motionSmoother::pMesh() const
00488 {
00489     return pMesh_;
00490 }
00491 
00492 
00493 const Foam::indirectPrimitivePatch& Foam::motionSmoother::patch() const
00494 {
00495     return pp_;
00496 }
00497 
00498 
00499 const Foam::labelList& Foam::motionSmoother::adaptPatchIDs() const
00500 {
00501     return adaptPatchIDs_;
00502 }
00503 
00504 
00505 const Foam::dictionary& Foam::motionSmoother::paramDict() const
00506 {
00507     return paramDict_;
00508 }
00509 
00510 
00511 Foam::pointVectorField& Foam::motionSmoother::displacement()
00512 {
00513     return displacement_;
00514 }
00515 
00516 
00517 const Foam::pointVectorField& Foam::motionSmoother::displacement() const
00518 {
00519     return displacement_;
00520 }
00521 
00522 
00523 const Foam::pointScalarField& Foam::motionSmoother::scale() const
00524 {
00525     return scale_;
00526 }
00527 
00528 
00529 const Foam::pointField& Foam::motionSmoother::oldPoints() const
00530 {
00531     return oldPoints_;
00532 }
00533 
00534 
00535 void Foam::motionSmoother::correct()
00536 {
00537     oldPoints_ = mesh_.points();
00538 
00539     scale_ = 1.0;
00540 
00541     // No need to update twoDmotion corrector since only holds edge labels
00542     // which will remain the same as before. So unless the mesh was distorted
00543     // severely outside of motionSmoother there will be no need.
00544 }
00545 
00546 
00547 void Foam::motionSmoother::setDisplacement(pointField& patchDisp)
00548 {
00549     // See comment in .H file about shared points.
00550     const polyBoundaryMesh& patches = mesh_.boundaryMesh();
00551 
00552     forAll(patches, patchI)
00553     {
00554         const polyPatch& pp = patches[patchI];
00555 
00556         if (pp.coupled())
00557         {
00558             const labelList& meshPoints = pp.meshPoints();
00559 
00560             forAll(meshPoints, i)
00561             {
00562                 displacement_[meshPoints[i]] = vector::zero;
00563             }
00564         }
00565     }
00566 
00567     const labelList& ppMeshPoints = pp_.meshPoints();
00568 
00569     // Set internal point data from displacement on combined patch points.
00570     forAll(ppMeshPoints, patchPointI)
00571     {
00572         displacement_[ppMeshPoints[patchPointI]] = patchDisp[patchPointI];
00573     }
00574 
00575     // Adapt the fixedValue bc's (i.e. copy internal point data to
00576     // boundaryField for all affected patches)
00577     forAll(adaptPatchIDs_, i)
00578     {
00579         label patchI = adaptPatchIDs_[i];
00580 
00581         displacement_.boundaryField()[patchI] ==
00582             displacement_.boundaryField()[patchI].patchInternalField();
00583     }
00584 
00585     // Make consistent with non-adapted bc's by evaluating those now and
00586     // resetting the displacement from the values.
00587     // Note that we're just doing a correctBoundaryConditions with
00588     // fixedValue bc's first.
00589     labelHashSet adaptPatchSet(adaptPatchIDs_);
00590 
00591     const lduSchedule& patchSchedule = mesh_.globalData().patchSchedule();
00592 
00593     forAll(patchSchedule, patchEvalI)
00594     {
00595         label patchI = patchSchedule[patchEvalI].patch;
00596 
00597         if (!adaptPatchSet.found(patchI))
00598         {
00599             if (patchSchedule[patchEvalI].init)
00600             {
00601                 displacement_.boundaryField()[patchI]
00602                     .initEvaluate(Pstream::scheduled);
00603             }
00604             else
00605             {
00606                 displacement_.boundaryField()[patchI]
00607                     .evaluate(Pstream::scheduled);
00608             }
00609         }
00610     }
00611 
00612     // Multi-patch constraints
00613     applyCornerConstraints(displacement_);
00614 
00615     // Correct for problems introduced by corner constraints
00616     syncTools::syncPointList
00617     (
00618         mesh_,
00619         displacement_,
00620         maxMagEqOp(),   // combine op
00621         vector::zero,   // null value
00622         false           // no separation
00623     );
00624 
00625     // Adapt the fixedValue bc's (i.e. copy internal point data to
00626     // boundaryField for all affected patches) to take the changes caused
00627     // by multi-corner constraints into account.
00628     forAll(adaptPatchIDs_, i)
00629     {
00630         label patchI = adaptPatchIDs_[i];
00631 
00632         displacement_.boundaryField()[patchI] ==
00633             displacement_.boundaryField()[patchI].patchInternalField();
00634     }
00635 
00636     if (debug)
00637     {
00638         OFstream str(mesh_.db().path()/"changedPoints.obj");
00639         label nVerts = 0;
00640         forAll(ppMeshPoints, patchPointI)
00641         {
00642             const vector& newDisp = displacement_[ppMeshPoints[patchPointI]];
00643 
00644             if (mag(newDisp-patchDisp[patchPointI]) > SMALL)
00645             {
00646                 const point& pt = mesh_.points()[ppMeshPoints[patchPointI]];
00647 
00648                 meshTools::writeOBJ(str, pt);
00649                 nVerts++;
00650                 //Pout<< "Point:" << pt
00651                 //    << " oldDisp:" << patchDisp[patchPointI]
00652                 //    << " newDisp:" << newDisp << endl;
00653             }
00654         }
00655         Pout<< "Written " << nVerts << " points that are changed to file "
00656             << str.name() << endl;
00657     }
00658 
00659     // Now reset input displacement
00660     forAll(ppMeshPoints, patchPointI)
00661     {
00662         patchDisp[patchPointI] = displacement_[ppMeshPoints[patchPointI]];
00663     }
00664 }
00665 
00666 
00667 // correctBoundaryConditions with fixedValue bc's first.
00668 void Foam::motionSmoother::correctBoundaryConditions
00669 (
00670     pointVectorField& displacement
00671 ) const
00672 {
00673     labelHashSet adaptPatchSet(adaptPatchIDs_);
00674 
00675     const lduSchedule& patchSchedule = mesh_.globalData().patchSchedule();
00676 
00677     // 1. evaluate on adaptPatches
00678     forAll(patchSchedule, patchEvalI)
00679     {
00680         label patchI = patchSchedule[patchEvalI].patch;
00681 
00682         if (adaptPatchSet.found(patchI))
00683         {
00684             if (patchSchedule[patchEvalI].init)
00685             {
00686                 displacement.boundaryField()[patchI]
00687                     .initEvaluate(Pstream::blocking);
00688             }
00689             else
00690             {
00691                 displacement.boundaryField()[patchI]
00692                     .evaluate(Pstream::blocking);
00693             }
00694         }
00695     }
00696 
00697 
00698     // 2. evaluate on non-AdaptPatches
00699     forAll(patchSchedule, patchEvalI)
00700     {
00701         label patchI = patchSchedule[patchEvalI].patch;
00702 
00703         if (!adaptPatchSet.found(patchI))
00704         {
00705             if (patchSchedule[patchEvalI].init)
00706             {
00707                 displacement.boundaryField()[patchI]
00708                     .initEvaluate(Pstream::blocking);
00709             }
00710             else
00711             {
00712                 displacement.boundaryField()[patchI]
00713                     .evaluate(Pstream::blocking);
00714             }
00715         }
00716     }
00717 
00718     // Multi-patch constraints
00719     applyCornerConstraints(displacement);
00720 
00721     // Correct for problems introduced by corner constraints
00722     syncTools::syncPointList
00723     (
00724         mesh_,
00725         displacement,
00726         maxMagEqOp(),           // combine op
00727         vector::zero,           // null value
00728         false                   // no separation
00729     );
00730 }
00731 
00732 
00733 Foam::tmp<Foam::scalarField> Foam::motionSmoother::movePoints
00734 (
00735     pointField& newPoints
00736 )
00737 {
00738     // Correct for 2-D motion
00739     if (twoDCorrector_.required())
00740     {
00741         Info<< "Correct-ing 2-D mesh motion";
00742 
00743         if (mesh_.globalData().parallel())
00744         {
00745             WarningIn("motionSmoother::movePoints(pointField& newPoints)")
00746                 << "2D mesh-motion probably not correct in parallel" << endl;
00747         }
00748 
00749         // We do not want to move 3D planes so project all points onto those
00750         const pointField& oldPoints = mesh_.points();
00751         const edgeList& edges = mesh_.edges();
00752 
00753         const labelList& neIndices = twoDCorrector().normalEdgeIndices();
00754         const vector& pn = twoDCorrector().planeNormal();
00755 
00756         forAll(neIndices, i)
00757         {
00758             const edge& e = edges[neIndices[i]];
00759 
00760             point& pStart = newPoints[e.start()];
00761 
00762             pStart += pn*(pn & (oldPoints[e.start()] - pStart));
00763 
00764             point& pEnd = newPoints[e.end()];
00765 
00766             pEnd += pn*(pn & (oldPoints[e.end()] - pEnd));
00767         }
00768 
00769         // Correct tangentially
00770         twoDCorrector_.correctPoints(newPoints);
00771         Info<< " ...done" << endl;
00772     }
00773 
00774     if (debug)
00775     {
00776         Pout<< "motionSmoother::movePoints : testing sync of newPoints."
00777             << endl;
00778         testSyncField
00779         (
00780             newPoints,
00781             minEqOp<point>(),           // combine op
00782             vector(GREAT,GREAT,GREAT),  // null
00783             true                        // separation
00784         );
00785     }
00786 
00787     tmp<scalarField> tsweptVol = mesh_.movePoints(newPoints);
00788 
00789     pp_.movePoints(mesh_.points());
00790 
00791     return tsweptVol;
00792 }
00793 
00794 
00795 Foam::scalar Foam::motionSmoother::setErrorReduction
00796 (
00797     const scalar errorReduction
00798 )
00799 {
00800     scalar oldErrorReduction = readScalar(paramDict_.lookup("errorReduction"));
00801 
00802     paramDict_.remove("errorReduction");
00803     paramDict_.add("errorReduction", errorReduction);
00804 
00805     return oldErrorReduction;
00806 }
00807 
00808 
00809 bool Foam::motionSmoother::scaleMesh
00810 (
00811     labelList& checkFaces,
00812     const bool smoothMesh,
00813     const label nAllowableErrors
00814 )
00815 {
00816     List<labelPair> emptyBaffles;
00817     return scaleMesh
00818     (
00819         checkFaces,
00820         emptyBaffles,
00821         smoothMesh,
00822         nAllowableErrors
00823     );
00824 }
00825 
00826 
00827 bool Foam::motionSmoother::scaleMesh
00828 (
00829     labelList& checkFaces,
00830     const List<labelPair>& baffles,
00831     const bool smoothMesh,
00832     const label nAllowableErrors
00833 )
00834 {
00835     return scaleMesh
00836     (
00837         checkFaces,
00838         baffles,
00839         paramDict_,
00840         paramDict_,
00841         smoothMesh,
00842         nAllowableErrors
00843     );
00844 }
00845 
00846 
00847 bool Foam::motionSmoother::scaleMesh
00848 (
00849     labelList& checkFaces,
00850     const List<labelPair>& baffles,
00851     const dictionary& paramDict,
00852     const dictionary& meshQualityDict,
00853     const bool smoothMesh,
00854     const label nAllowableErrors
00855 )
00856 {
00857     if (!smoothMesh && adaptPatchIDs_.empty())
00858     {
00859         FatalErrorIn("motionSmoother::scaleMesh(const bool")
00860             << "You specified both no movement on the internal mesh points"
00861             << " (smoothMesh = false)" << nl
00862             << "and no movement on the patch (adaptPatchIDs is empty)" << nl
00863             << "Hence nothing to adapt."
00864             << exit(FatalError);
00865     }
00866 
00867     if (debug)
00868     {
00869         // Had a problem with patches moved non-synced. Check transformations.
00870         const polyBoundaryMesh& patches = mesh_.boundaryMesh();
00871 
00872         Pout<< "Entering scaleMesh : coupled patches:" << endl;
00873         forAll(patches, patchI)
00874         {
00875             if (patches[patchI].coupled())
00876             {
00877                 const coupledPolyPatch& pp =
00878                     refCast<const coupledPolyPatch>(patches[patchI]);
00879 
00880                 Pout<< '\t' << patchI << '\t' << pp.name()
00881                     << " parallel:" << pp.parallel()
00882                     << " separated:" << pp.separated()
00883                     << " forwardT:" << pp.forwardT().size()
00884                     << endl;
00885             }
00886         }
00887     }
00888 
00889     const scalar errorReduction =
00890         readScalar(paramDict.lookup("errorReduction"));
00891     const label nSmoothScale =
00892         readLabel(paramDict.lookup("nSmoothScale"));
00893 
00894 
00895     // Note: displacement_ should already be synced already from setDisplacement
00896     // but just to make sure.
00897     syncTools::syncPointList
00898     (
00899         mesh_,
00900         displacement_,
00901         maxMagEqOp(),
00902         vector::zero,   // null value
00903         false           // no separation
00904     );
00905 
00906     // Set newPoints as old + scale*displacement
00907     pointField newPoints;
00908     {
00909         // Create overall displacement with same b.c.s as displacement_
00910         pointVectorField totalDisplacement
00911         (
00912             IOobject
00913             (
00914                 "totalDisplacement",
00915                 mesh_.time().timeName(),
00916                 mesh_,
00917                 IOobject::NO_READ,
00918                 IOobject::NO_WRITE,
00919                 false
00920             ),
00921             scale_*displacement_,
00922             displacement_.boundaryField().types()
00923         );
00924         correctBoundaryConditions(totalDisplacement);
00925 
00926         if (debug)
00927         {
00928             Pout<< "scaleMesh : testing sync of totalDisplacement" << endl;
00929             testSyncField
00930             (
00931                 totalDisplacement,
00932                 maxMagEqOp(),
00933                 vector::zero,   // null value
00934                 false           // separation
00935             );
00936         }
00937 
00938         newPoints = oldPoints_ + totalDisplacement.internalField();
00939     }
00940 
00941     Info<< "Moving mesh using diplacement scaling :"
00942         << " min:" << gMin(scale_.internalField())
00943         << "  max:" << gMax(scale_.internalField())
00944         << endl;
00945 
00946 
00947     // Move
00948     movePoints(newPoints);
00949 
00950     // Check. Returns parallel number of incorrect faces.
00951     faceSet wrongFaces(mesh_, "wrongFaces", mesh_.nFaces()/100+100);
00952     checkMesh(false, mesh_, meshQualityDict, checkFaces, baffles, wrongFaces);
00953 
00954     if (returnReduce(wrongFaces.size(), sumOp<label>()) <= nAllowableErrors)
00955     {
00956         return true;
00957     }
00958     else
00959     {
00960         // Sync across coupled faces by extending the set.
00961         wrongFaces.sync(mesh_);
00962 
00963         // Special case:
00964         // if errorReduction is set to zero, extend wrongFaces
00965         // to face-Cell-faces to ensure quick return to previously valid mesh
00966 
00967         if (mag(errorReduction) < SMALL)
00968         {
00969             labelHashSet newWrongFaces(wrongFaces);
00970             forAllConstIter(labelHashSet, wrongFaces, iter)
00971             {
00972                 label own = mesh_.faceOwner()[iter.key()];
00973                 const cell& ownFaces = mesh_.cells()[own];
00974 
00975                 forAll(ownFaces, cfI)
00976                 {
00977                     newWrongFaces.insert(ownFaces[cfI]);
00978                 }
00979 
00980                 if (iter.key() < mesh_.nInternalFaces())
00981                 {
00982                     label nei = mesh_.faceNeighbour()[iter.key()];
00983                     const cell& neiFaces = mesh_.cells()[nei];
00984 
00985                     forAll(neiFaces, cfI)
00986                     {
00987                         newWrongFaces.insert(neiFaces[cfI]);
00988                     }
00989                 }
00990             }
00991             wrongFaces.transfer(newWrongFaces);
00992             wrongFaces.sync(mesh_);
00993         }
00994 
00995 
00996         // Find out points used by wrong faces and scale displacement.
00997         // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
00998 
00999         pointSet usedPoints(mesh_, "usedPoints", getPoints(wrongFaces));
01000         usedPoints.sync(mesh_);
01001 
01002 
01003 
01004         // Grow a few layers to determine
01005         // - points to be smoothed
01006         // - faces to be checked in next iteration
01007         PackedBoolList isAffectedPoint(mesh_.nPoints());
01008         getAffectedFacesAndPoints
01009         (
01010             nSmoothScale,       // smoothing iterations
01011             wrongFaces,         // error faces
01012             checkFaces,
01013             isAffectedPoint
01014         );
01015 
01016         if (debug)
01017         {
01018             Pout<< "Faces in error:" << wrongFaces.size()
01019                 << "  with points:" << usedPoints.size()
01020                 << endl;
01021         }
01022 
01023         if (adaptPatchIDs_.size())
01024         {
01025             // Scale conflicting patch points
01026             scaleField(pp_.meshPoints(), usedPoints, errorReduction, scale_);
01027         }
01028         if (smoothMesh)
01029         {
01030             // Scale conflicting internal points
01031             scaleField(usedPoints, errorReduction, scale_);
01032         }
01033 
01034         for (label i = 0; i < nSmoothScale; i++)
01035         {
01036             if (adaptPatchIDs_.size())
01037             {
01038                 // Smooth patch values
01039                 pointScalarField oldScale(scale_);
01040                 minSmooth
01041                 (
01042                     isAffectedPoint,
01043                     pp_.meshPoints(),
01044                     oldScale,
01045                     scale_
01046                 );
01047                 checkFld(scale_);
01048             }
01049             if (smoothMesh)
01050             {
01051                 // Smooth internal values
01052                 pointScalarField oldScale(scale_);
01053                 minSmooth(isAffectedPoint, oldScale, scale_);
01054                 checkFld(scale_);
01055             }
01056         }
01057 
01058         syncTools::syncPointList
01059         (
01060             mesh_,
01061             scale_,
01062             maxEqOp<scalar>(),
01063             -GREAT,             // null value
01064             false               // no separation
01065         );
01066 
01067 
01068         if (debug)
01069         {
01070             Pout<< "scale_ after smoothing :"
01071                 << " min:" << Foam::gMin(scale_)
01072                 << " max:" << Foam::gMax(scale_)
01073                 << endl;
01074         }
01075 
01076         return false;
01077     }
01078 }
01079 
01080 
01081 void Foam::motionSmoother::updateMesh()
01082 {
01083     const pointBoundaryMesh& patches = pMesh_.boundary();
01084 
01085     // Check whether displacement has fixed value b.c. on adaptPatchID
01086     forAll(adaptPatchIDs_, i)
01087     {
01088         label patchI = adaptPatchIDs_[i];
01089 
01090         if
01091         (
01092            !isA<fixedValuePointPatchVectorField>
01093             (
01094                 displacement_.boundaryField()[patchI]
01095             )
01096         )
01097         {
01098             FatalErrorIn
01099             (
01100                 "motionSmoother::motionSmoother"
01101             )   << "Patch " << patches[patchI].name()
01102                 << " has wrong boundary condition "
01103                 << displacement_.boundaryField()[patchI].type()
01104                 << " on field " << displacement_.name() << nl
01105                 << "Only type allowed is "
01106                 << fixedValuePointPatchVectorField::typeName
01107                 << exit(FatalError);
01108         }
01109     }
01110 
01111 
01112     // Determine internal points. Note that for twoD there are no internal
01113     // points so we use the points of adaptPatchIDs instead
01114 
01115     twoDCorrector_.updateMesh();
01116 
01117     const labelList& meshPoints = pp_.meshPoints();
01118 
01119     forAll(meshPoints, i)
01120     {
01121         isInternalPoint_.set(meshPoints[i], 0);
01122     }
01123 
01124     // Calculate master edge addressing
01125     isMasterEdge_ = syncTools::getMasterEdges(mesh_);
01126 
01127     makePatchPatchAddressing();
01128 }
01129 
01130 
01131 // Specialisation of applyCornerConstraints for scalars because
01132 // no constraint need be applied
01133 template<>
01134 void Foam::motionSmoother::applyCornerConstraints<Foam::scalar>
01135 (
01136     GeometricField<scalar, pointPatchField, pointMesh>& pf
01137 ) const
01138 {}
01139 
01140 
01141 // ************************************************************************* //
Copyright © 2000-2009 OpenCFD Ltd