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

Particle.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 "Particle.H"
00028 #include "Cloud.H"
00029 #include "wedgePolyPatch.H"
00030 #include "symmetryPolyPatch.H"
00031 #include "cyclicPolyPatch.H"
00032 #include "processorPolyPatch.H"
00033 #include "wallPolyPatch.H"
00034 #include "transform.H"
00035 
00036 // * * * * * * * * * * * * * Private Member Functions  * * * * * * * * * * * //
00037 
00038 template<class ParticleType>
00039 void Foam::Particle<ParticleType>::findFaces
00040 (
00041     const vector& position,
00042     DynamicList<label>& faceList
00043 ) const
00044 {
00045     const polyMesh& mesh = cloud_.polyMesh_;
00046     const labelList& faces = mesh.cells()[celli_];
00047     const vector& C = mesh.cellCentres()[celli_];
00048 
00049     faceList.clear();
00050     forAll(faces, i)
00051     {
00052         label facei = faces[i];
00053         scalar lam = lambda(C, position, facei);
00054 
00055         if ((lam > 0) && (lam < 1.0))
00056         {
00057             faceList.append(facei);
00058         }
00059     }
00060 }
00061 
00062 
00063 template<class ParticleType>
00064 void Foam::Particle<ParticleType>::findFaces
00065 (
00066     const vector& position,
00067     const label celli,
00068     const scalar stepFraction,
00069     DynamicList<label>& faceList
00070 ) const
00071 {
00072     const polyMesh& mesh = cloud_.pMesh();
00073     const labelList& faces = mesh.cells()[celli];
00074     const vector& C = mesh.cellCentres()[celli];
00075 
00076     faceList.clear();
00077     forAll(faces, i)
00078     {
00079         label facei = faces[i];
00080         scalar lam = lambda(C, position, facei, stepFraction);
00081 
00082         if ((lam > 0) && (lam < 1.0))
00083         {
00084             faceList.append(facei);
00085         }
00086     }
00087 }
00088 
00089 
00090 template<class ParticleType>
00091 template<class TrackData>
00092 void Foam::Particle<ParticleType>::prepareForParallelTransfer
00093 (
00094     const label patchi,
00095     TrackData& td
00096 )
00097 {
00098     // Convert the face index to be local to the processor patch
00099     facei_ = patchFace(patchi, facei_);
00100 }
00101 
00102 
00103 template<class ParticleType>
00104 template<class TrackData>
00105 void Foam::Particle<ParticleType>::correctAfterParallelTransfer
00106 (
00107     const label patchi,
00108     TrackData& td
00109 )
00110 {
00111     const processorPolyPatch& ppp =
00112         refCast<const processorPolyPatch>
00113         (cloud_.pMesh().boundaryMesh()[patchi]);
00114 
00115     celli_ = ppp.faceCells()[facei_];
00116 
00117     if (!ppp.parallel())
00118     {
00119         if (ppp.forwardT().size() == 1)
00120         {
00121             const tensor& T = ppp.forwardT()[0];
00122             transformPosition(T);
00123             static_cast<ParticleType&>(*this).transformProperties(T);
00124         }
00125         else
00126         {
00127             const tensor& T = ppp.forwardT()[facei_];
00128             transformPosition(T);
00129             static_cast<ParticleType&>(*this).transformProperties(T);
00130         }
00131     }
00132     else if (ppp.separated())
00133     {
00134         if (ppp.separation().size() == 1)
00135         {
00136             position_ -= ppp.separation()[0];
00137             static_cast<ParticleType&>(*this).transformProperties
00138             (
00139                 -ppp.separation()[0]
00140             );
00141         }
00142         else
00143         {
00144             position_ -= ppp.separation()[facei_];
00145             static_cast<ParticleType&>(*this).transformProperties
00146             (
00147                 -ppp.separation()[facei_]
00148             );
00149         }
00150     }
00151 
00152     // Reset the face index for the next tracking operation
00153     if (stepFraction_ > (1.0 - SMALL))
00154     {
00155         stepFraction_ = 1.0;
00156         facei_ = -1;
00157     }
00158     else
00159     {
00160         facei_ += ppp.start();
00161     }
00162 }
00163 
00164 
00165 // * * * * * * * * * * * * * * * * Constructors  * * * * * * * * * * * * * * //
00166 
00167 template<class ParticleType>
00168 Foam::Particle<ParticleType>::Particle
00169 (
00170     const Cloud<ParticleType>& cloud,
00171     const vector& position,
00172     const label celli
00173 )
00174 :
00175     cloud_(cloud),
00176     position_(position),
00177     celli_(celli),
00178     facei_(-1),
00179     stepFraction_(0.0),
00180     origProc_(Pstream::myProcNo()),
00181     origId_(cloud_.getNewParticleID())
00182 {}
00183 
00184 
00185 template<class ParticleType>
00186 Foam::Particle<ParticleType>::Particle(const Particle<ParticleType>& p)
00187 :
00188     cloud_(p.cloud_),
00189     position_(p.position_),
00190     celli_(p.celli_),
00191     facei_(p.facei_),
00192     stepFraction_(p.stepFraction_),
00193     origProc_(p.origProc_),
00194     origId_(p.origId_)
00195 {}
00196 
00197 
00198 // * * * * * * * * * * * * * * * Member Functions  * * * * * * * * * * * * * //
00199 
00200 template<class ParticleType>
00201 template<class TrackData>
00202 Foam::label Foam::Particle<ParticleType>::track
00203 (
00204     const vector& endPosition,
00205     TrackData& td
00206 )
00207 {
00208     facei_ = -1;
00209 
00210     // Tracks to endPosition or stop on boundary
00211     while(!onBoundary() && stepFraction_ < 1.0 - SMALL)
00212     {
00213         stepFraction_ += trackToFace(endPosition, td)*(1.0 - stepFraction_);
00214     }
00215 
00216     return facei_;
00217 }
00218 
00219 
00220 
00221 template<class ParticleType>
00222 Foam::label Foam::Particle<ParticleType>::track(const vector& endPosition)
00223 {
00224     int dummyTd;
00225     return track(endPosition, dummyTd);
00226 }
00227 
00228 template<class ParticleType>
00229 template<class TrackData>
00230 Foam::scalar Foam::Particle<ParticleType>::trackToFace
00231 (
00232     const vector& endPosition,
00233     TrackData& td
00234 )
00235 {
00236     const polyMesh& mesh = cloud_.polyMesh_;
00237 
00238     DynamicList<label>& faces = cloud_.labels_;
00239     findFaces(endPosition, faces);
00240 
00241     facei_ = -1;
00242     scalar trackFraction = 0.0;
00243 
00244     if (faces.empty())  // inside cell
00245     {
00246         trackFraction = 1.0;
00247         position_ = endPosition;
00248     }
00249     else // hit face
00250     {
00251         scalar lambdaMin = GREAT;
00252 
00253         if (faces.size() == 1)
00254         {
00255             lambdaMin = lambda(position_, endPosition, faces[0], stepFraction_);
00256             facei_ = faces[0];
00257         }
00258         else
00259         {
00260             // If the particle has to cross more than one cell to reach the
00261             // endPosition, we check which way to go.
00262             // If one of the faces is a boundary face and the particle is
00263             // outside, we choose the boundary face.
00264             // The particle is outside if one of the lambda's is > 1 or < 0
00265             forAll(faces, i)
00266             {
00267                 scalar lam =
00268                     lambda(position_, endPosition, faces[i], stepFraction_);
00269 
00270                 if (lam < lambdaMin)
00271                 {
00272                     lambdaMin = lam;
00273                     facei_ = faces[i];
00274                 }
00275             }
00276         }
00277 
00278         bool internalFace = cloud_.internalFace(facei_);
00279 
00280         // For warped faces the particle can be 'outside' the cell.
00281         // This will yield a lambda larger than 1, or smaller than 0
00282         // For values < 0, the particle travels away from the cell
00283         // and we don't move the particle, only change cell.
00284         // For values larger than 1, we move the particle to endPosition only.
00285         if (lambdaMin > 0.0)
00286         {
00287             if (lambdaMin <= 1.0)
00288             {
00289                 trackFraction = lambdaMin;
00290                 position_ += trackFraction*(endPosition - position_);
00291             }
00292             else
00293             {
00294                 trackFraction = 1.0;
00295                 position_ = endPosition;
00296             }
00297         }
00298         else if (static_cast<ParticleType&>(*this).softImpact())
00299         {
00300             // Soft-sphere particles can travel outside the domain
00301             // but we don't use lambda since this the particle
00302             // is going away from face
00303             trackFraction = 1.0;
00304             position_ = endPosition;
00305         }
00306 
00307         // change cell
00308         if (internalFace) // Internal face
00309         {
00310             if (celli_ == mesh.faceOwner()[facei_])
00311             {
00312                 celli_ = mesh.faceNeighbour()[facei_];
00313             }
00314             else if (celli_ == mesh.faceNeighbour()[facei_])
00315             {
00316                 celli_ = mesh.faceOwner()[facei_];
00317             }
00318             else
00319             {
00320                 FatalErrorIn
00321                 (
00322                     "Particle::trackToFace(const vector&, TrackData&)"
00323                 )<< "addressing failure" << nl
00324                  << abort(FatalError);
00325             }
00326         }
00327         else
00328         {
00329             ParticleType& p = static_cast<ParticleType&>(*this);
00330 
00331             // Soft-sphere algorithm ignores the boundary
00332             if (p.softImpact())
00333             {
00334                 trackFraction = 1.0;
00335                 position_ = endPosition;
00336             }
00337 
00338             label patchi = patch(facei_);
00339             const polyPatch& patch = mesh.boundaryMesh()[patchi];
00340 
00341             if (!p.hitPatch(patch, td, patchi))
00342             {
00343                 if (isA<wedgePolyPatch>(patch))
00344                 {
00345                     p.hitWedgePatch
00346                     (
00347                         static_cast<const wedgePolyPatch&>(patch), td
00348                     );
00349                 }
00350                 else if (isA<symmetryPolyPatch>(patch))
00351                 {
00352                     p.hitSymmetryPatch
00353                     (
00354                         static_cast<const symmetryPolyPatch&>(patch), td
00355                     );
00356                 }
00357                 else if (isA<cyclicPolyPatch>(patch))
00358                 {
00359                     p.hitCyclicPatch
00360                     (
00361                         static_cast<const cyclicPolyPatch&>(patch), td
00362                     );
00363                 }
00364                 else if (isA<processorPolyPatch>(patch))
00365                 {
00366                     p.hitProcessorPatch
00367                     (
00368                         static_cast<const processorPolyPatch&>(patch), td
00369                     );
00370                 }
00371                 else if (isA<wallPolyPatch>(patch))
00372                 {
00373                     p.hitWallPatch
00374                     (
00375                         static_cast<const wallPolyPatch&>(patch), td
00376                     );
00377                 }
00378                 else
00379                 {
00380                     p.hitPatch(patch, td);
00381                 }
00382             }
00383         }
00384     }
00385 
00386     // If the trackFraction = 0 something went wrong.
00387     // Either the particle is flipping back and forth across a face perhaps
00388     // due to velocity interpolation errors or it is in a "hole" in the mesh
00389     // caused by face warpage.
00390     // In both cases resolve the positional ambiguity by moving the particle
00391     // slightly towards the cell-centre.
00392     if (trackFraction < SMALL)
00393     {
00394         position_ += 1.0e-3*(mesh.cellCentres()[celli_] - position_);
00395     }
00396 
00397     return trackFraction;
00398 }
00399 
00400 template<class ParticleType>
00401 Foam::scalar Foam::Particle<ParticleType>::trackToFace
00402 (
00403     const vector& endPosition
00404 )
00405 {
00406     int dummyTd;
00407     return trackToFace(endPosition, dummyTd);
00408 }
00409 
00410 template<class ParticleType>
00411 void Foam::Particle<ParticleType>::transformPosition(const tensor& T)
00412 {
00413     position_ = transform(T, position_);
00414 }
00415 
00416 
00417 template<class ParticleType>
00418 void Foam::Particle<ParticleType>::transformProperties(const tensor&)
00419 {}
00420 
00421 
00422 template<class ParticleType>
00423 void Foam::Particle<ParticleType>::transformProperties(const vector&)
00424 {}
00425 
00426 
00427 template<class ParticleType>
00428 template<class TrackData>
00429 bool Foam::Particle<ParticleType>::hitPatch
00430 (
00431     const polyPatch&,
00432     TrackData&,
00433     const label
00434 )
00435 {
00436     return false;
00437 }
00438 
00439 
00440 template<class ParticleType>
00441 template<class TrackData>
00442 void Foam::Particle<ParticleType>::hitWedgePatch
00443 (
00444     const wedgePolyPatch& wpp,
00445     TrackData&
00446 )
00447 {
00448     vector nf = wpp.faceAreas()[wpp.whichFace(facei_)];
00449     nf /= mag(nf);
00450 
00451     static_cast<ParticleType&>(*this).transformProperties(I - 2.0*nf*nf);
00452 }
00453 
00454 
00455 template<class ParticleType>
00456 template<class TrackData>
00457 void Foam::Particle<ParticleType>::hitSymmetryPatch
00458 (
00459     const symmetryPolyPatch& spp,
00460     TrackData&
00461 )
00462 {
00463     vector nf = spp.faceAreas()[spp.whichFace(facei_)];
00464     nf /= mag(nf);
00465 
00466     static_cast<ParticleType&>(*this).transformProperties(I - 2.0*nf*nf);
00467 }
00468 
00469 
00470 template<class ParticleType>
00471 template<class TrackData>
00472 void Foam::Particle<ParticleType>::hitCyclicPatch
00473 (
00474     const cyclicPolyPatch& cpp,
00475     TrackData&
00476 )
00477 {
00478     label patchFacei_ = cpp.whichFace(facei_);
00479 
00480     facei_ = cpp.transformGlobalFace(facei_);
00481 
00482     celli_ = cloud_.polyMesh_.faceOwner()[facei_];
00483 
00484     if (!cpp.parallel())
00485     {
00486         const tensor& T = cpp.transformT(patchFacei_);
00487 
00488         transformPosition(T);
00489         static_cast<ParticleType&>(*this).transformProperties(T);
00490     }
00491     else if (cpp.separated())
00492     {
00493         position_ += cpp.separation(patchFacei_);
00494         static_cast<ParticleType&>(*this).transformProperties
00495         (
00496             cpp.separation(patchFacei_)
00497         );
00498     }
00499 }
00500 
00501 
00502 template<class ParticleType>
00503 template<class TrackData>
00504 void Foam::Particle<ParticleType>::hitProcessorPatch
00505 (
00506     const processorPolyPatch& spp,
00507     TrackData& td
00508 )
00509 {}
00510 
00511 
00512 template<class ParticleType>
00513 template<class TrackData>
00514 void Foam::Particle<ParticleType>::hitWallPatch
00515 (
00516     const wallPolyPatch& spp,
00517     TrackData&
00518 )
00519 {}
00520 
00521 
00522 template<class ParticleType>
00523 template<class TrackData>
00524 void Foam::Particle<ParticleType>::hitPatch
00525 (
00526     const polyPatch& spp,
00527     TrackData&
00528 )
00529 {}
00530 
00531 
00532 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
00533 
00534 #include "ParticleIO.C"
00535 
00536 // ************************************************************************* //
Copyright © 2000-2009 OpenCFD Ltd