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

FreeStream.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) 2009-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 "FreeStream.H"
00028 
00029 // * * * * * * * * * * * * * * * * Constructors  * * * * * * * * * * * * * * //
00030 
00031 template <class CloudType>
00032 Foam::FreeStream<CloudType>::FreeStream
00033 (
00034     const dictionary& dict,
00035     CloudType& cloud
00036 )
00037 :
00038     InflowBoundaryModel<CloudType>(dict, cloud, typeName),
00039     patches_(),
00040     moleculeTypeIds_(),
00041     numberDensities_(),
00042     particleFluxAccumulators_()
00043 {
00044     // Identify which patches to use
00045 
00046     DynamicList<label> patches;
00047 
00048     forAll(cloud.mesh().boundaryMesh(), p)
00049     {
00050         const polyPatch& patch = cloud.mesh().boundaryMesh()[p];
00051 
00052         if (patch.type() == polyPatch::typeName)
00053         {
00054             patches.append(p);
00055         }
00056     }
00057 
00058     patches_.transfer(patches);
00059 
00060     const dictionary& numberDensitiesDict
00061     (
00062         this->coeffDict().subDict("numberDensities")
00063     );
00064 
00065     List<word> molecules(numberDensitiesDict.toc());
00066 
00067     // Initialise the particleFluxAccumulators_
00068     particleFluxAccumulators_.setSize(patches_.size());
00069 
00070     forAll(patches_, p)
00071     {
00072         const polyPatch& patch = cloud.mesh().boundaryMesh()[patches_[p]];
00073 
00074         particleFluxAccumulators_[p] = List<Field<scalar> >
00075         (
00076             molecules.size(),
00077             Field<scalar>(patch.size(), 0.0)
00078         );
00079     }
00080 
00081     moleculeTypeIds_.setSize(molecules.size());
00082 
00083     numberDensities_.setSize(molecules.size());
00084 
00085     forAll(molecules, i)
00086     {
00087         numberDensities_[i] = readScalar
00088         (
00089             numberDensitiesDict.lookup(molecules[i])
00090         );
00091 
00092         moleculeTypeIds_[i] = findIndex(cloud.typeIdList(), molecules[i]);
00093 
00094         if (moleculeTypeIds_[i] == -1)
00095         {
00096             FatalErrorIn
00097             (
00098                 "Foam::FreeStream<CloudType>::FreeStream"
00099                 "("
00100                     "const dictionary&, "
00101                     "CloudType&"
00102                 ")"
00103             )   << "typeId " << molecules[i] << "not defined in cloud." << nl
00104                 << abort(FatalError);
00105         }
00106     }
00107 
00108     numberDensities_ /= cloud.nParticle();
00109 }
00110 
00111 
00112 
00113 // * * * * * * * * * * * * * * * * Destructor  * * * * * * * * * * * * * * * //
00114 
00115 template <class CloudType>
00116 Foam::FreeStream<CloudType>::~FreeStream()
00117 {}
00118 
00119 
00120 // * * * * * * * * * * * * * * * Member Functions  * * * * * * * * * * * * * //
00121 
00122 template <class CloudType>
00123 void Foam::FreeStream<CloudType>::inflow()
00124 {
00125     CloudType& cloud(this->owner());
00126 
00127     const polyMesh& mesh(cloud.mesh());
00128 
00129     const scalar deltaT = cloud.cachedDeltaT();
00130 
00131     Random& rndGen(cloud.rndGen());
00132 
00133     scalar sqrtPi = sqrt(mathematicalConstant::pi);
00134 
00135     label particlesInserted = 0;
00136 
00137     const volScalarField::GeometricBoundaryField& boundaryT
00138     (
00139         cloud.T().boundaryField()
00140     );
00141 
00142     const volVectorField::GeometricBoundaryField& boundaryU
00143     (
00144         cloud.U().boundaryField()
00145     );
00146 
00147     forAll(patches_, p)
00148     {
00149         label patchI = patches_[p];
00150 
00151         const polyPatch& patch = mesh.boundaryMesh()[patchI];
00152 
00153         // Add mass to the accumulators.  negative face area dotted with the
00154         // velocity to point flux into the domain.
00155 
00156         // Take a reference to the particleFluxAccumulator for this patch
00157         List<Field<scalar> >& pFA = particleFluxAccumulators_[p];
00158 
00159         forAll(pFA, i)
00160         {
00161             label typeId = moleculeTypeIds_[i];
00162 
00163             scalar mass = cloud.constProps(typeId).mass();
00164 
00165             if (min(boundaryT[patchI]) < SMALL)
00166             {
00167                 FatalErrorIn ("Foam::FreeStream<CloudType>::inflow()")
00168                     << "Zero boundary temperature detected, check boundaryT condition." << nl
00169                     << nl << abort(FatalError);
00170             }
00171 
00172             scalarField mostProbableSpeed
00173             (
00174                 cloud.maxwellianMostProbableSpeed
00175                 (
00176                     boundaryT[patchI],
00177                     mass
00178                 )
00179             );
00180 
00181             // Dotting boundary velocity with the face unit normal (which points
00182             // out of the domain, so it must be negated), dividing by the most
00183             // probable speed to form molecularSpeedRatio * cosTheta
00184 
00185             scalarField sCosTheta =
00186                 boundaryU[patchI]
00187               & -patch.faceAreas()/mag(patch.faceAreas())
00188                /mostProbableSpeed;
00189 
00190             // From Bird eqn 4.22
00191 
00192             pFA[i] +=
00193                 mag(patch.faceAreas())*numberDensities_[i]*deltaT
00194                *mostProbableSpeed
00195                *(
00196                    exp(-sqr(sCosTheta)) + sqrtPi*sCosTheta*(1 + erf(sCosTheta))
00197                 )
00198                /(2.0*sqrtPi);
00199         }
00200 
00201         forAll(patch, f)
00202         {
00203             // Loop over all faces as the outer loop to avoid calculating
00204             // geometrical properties multiple times.
00205 
00206             labelList faceVertices = patch[f];
00207 
00208             label nVertices = faceVertices.size();
00209 
00210             label globalFaceIndex = f + patch.start();
00211 
00212             label cell = mesh.faceOwner()[globalFaceIndex];
00213 
00214             const vector& fC = patch.faceCentres()[f];
00215 
00216             scalar fA = mag(patch.faceAreas()[f]);
00217 
00218             // Cumulative triangle area fractions
00219             List<scalar> cTriAFracs(nVertices);
00220 
00221             for (label v = 0; v < nVertices - 1; v++)
00222             {
00223                 const point& vA = mesh.points()[faceVertices[v]];
00224 
00225                 const point& vB = mesh.points()[faceVertices[(v + 1)]];
00226 
00227                 cTriAFracs[v] =
00228                     0.5*mag((vA - fC)^(vB - fC))/fA
00229                   + cTriAFracs[max((v - 1), 0)];
00230             }
00231 
00232             // Force the last area fraction value to 1.0 to avoid any
00233             // rounding/non-flat face errors giving a value < 1.0
00234             cTriAFracs[nVertices - 1] = 1.0;
00235 
00236             // Normal unit vector *negative* so normal is pointing into the
00237             // domain
00238             vector n = patch.faceAreas()[f];
00239             n /= -mag(n);
00240 
00241             // Wall tangential unit vector. Use the direction between the
00242             // face centre and the first vertex in the list
00243             vector t1 = fC - (mesh.points()[faceVertices[0]]);
00244             t1 /= mag(t1);
00245 
00246             // Other tangential unit vector.  Rescaling in case face is not
00247             // flat and n and t1 aren't perfectly orthogonal
00248             vector t2 = n^t1;
00249             t2 /= mag(t2);
00250 
00251             scalar faceTemperature = boundaryT[patchI][f];
00252 
00253             const vector& faceVelocity = boundaryU[patchI][f];
00254 
00255             forAll(pFA, i)
00256             {
00257                 scalar& faceAccumulator = pFA[i][f];
00258 
00259                 // Number of whole particles to insert
00260                 label nI = max(label(faceAccumulator), 0);
00261 
00262                 // Add another particle with a probability proportional to the
00263                 // remainder of taking the integer part of faceAccumulator
00264                 if ((faceAccumulator - nI) > rndGen.scalar01())
00265                 {
00266                     nI++;
00267                 }
00268 
00269                 faceAccumulator -= nI;
00270 
00271                 label typeId = moleculeTypeIds_[i];
00272 
00273                 scalar mass = cloud.constProps(typeId).mass();
00274 
00275                 for (label i = 0; i < nI; i++)
00276                 {
00277                     // Choose a triangle to insert on, based on their relative
00278                     // area
00279 
00280                     scalar triSelection = rndGen.scalar01();
00281 
00282                     // Selected triangle
00283                     label sTri = -1;
00284 
00285                     forAll(cTriAFracs, tri)
00286                     {
00287                         sTri = tri;
00288 
00289                         if (cTriAFracs[tri] >= triSelection)
00290                         {
00291                             break;
00292                         }
00293                     }
00294 
00295                     // Randomly distribute the points on the triangle, using the
00296                     // method from:
00297                     // Generating Random Points in Triangles
00298                     // by Greg Turk
00299                     // from "Graphics Gems", Academic Press, 1990
00300                     // http://tog.acm.org/GraphicsGems/gems/TriPoints.c
00301 
00302                     const point& A = fC;
00303                     const point& B = mesh.points()[faceVertices[sTri]];
00304                     const point& C =
00305                     mesh.points()[faceVertices[(sTri + 1) % nVertices]];
00306 
00307                     scalar s = rndGen.scalar01();
00308                     scalar t = sqrt(rndGen.scalar01());
00309 
00310                     point p = (1 - t)*A + (1 - s)*t*B + s*t*C;
00311 
00312                     // Velocity generation
00313 
00314                     scalar mostProbableSpeed
00315                     (
00316                         cloud.maxwellianMostProbableSpeed
00317                         (
00318                             faceTemperature,
00319                             mass
00320                         )
00321                     );
00322 
00323                     scalar sCosTheta = (faceVelocity & n)/mostProbableSpeed;
00324 
00325                     // Coefficients required for Bird eqn 12.5
00326                     scalar uNormProbCoeffA =
00327                         sCosTheta + sqrt(sqr(sCosTheta) + 2.0);
00328 
00329                     scalar uNormProbCoeffB =
00330                         0.5*
00331                         (
00332                             1.0
00333                           + sCosTheta*(sCosTheta - sqrt(sqr(sCosTheta) + 2.0))
00334                         );
00335 
00336                     // Equivalent to the QA value in Bird's DSMC3.FOR
00337                     scalar randomScaling = 3.0;
00338 
00339                     if (sCosTheta < -3)
00340                     {
00341                         randomScaling = mag(sCosTheta) + 1;
00342                     }
00343 
00344                     scalar P = -1;
00345 
00346                     // Normalised candidates for the normal direction velocity
00347                     // component
00348                     scalar uNormal;
00349                     scalar uNormalThermal;
00350 
00351                     // Select a velocity using Bird eqn 12.5
00352                     do
00353                     {
00354                         uNormalThermal =
00355                             randomScaling*(2.0*rndGen.scalar01() - 1);
00356 
00357                         uNormal = uNormalThermal + sCosTheta;
00358 
00359                         if (uNormal < 0.0)
00360                         {
00361                             P = -1;
00362                         }
00363                         else
00364                         {
00365                             P = 2.0*uNormal/uNormProbCoeffA
00366                                *exp(uNormProbCoeffB - sqr(uNormalThermal));
00367                         }
00368 
00369                     } while (P < rndGen.scalar01());
00370 
00371                     vector U =
00372                         sqrt(CloudType::kb*faceTemperature/mass)
00373                        *(
00374                             rndGen.GaussNormal()*t1
00375                           + rndGen.GaussNormal()*t2
00376                         )
00377                       + (t1 & faceVelocity)*t1
00378                       + (t2 & faceVelocity)*t2
00379                       + mostProbableSpeed*uNormal*n;
00380 
00381                     scalar Ei = cloud.equipartitionInternalEnergy
00382                     (
00383                         faceTemperature,
00384                         cloud.constProps(typeId).internalDegreesOfFreedom()
00385                     );
00386 
00387                     cloud.addNewParcel
00388                     (
00389                         p,
00390                         U,
00391                         Ei,
00392                         cell,
00393                         typeId
00394                     );
00395 
00396                     particlesInserted++;
00397                 }
00398             }
00399         }
00400     }
00401 
00402     reduce(particlesInserted, sumOp<label>());
00403 
00404     Info<< "    Particles inserted              = "
00405         << particlesInserted << endl;
00406 
00407 }
00408 
00409 
00410 // ************************************************************************* //
Copyright © 2000-2009 OpenCFD Ltd