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

InjectionModel.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 "InjectionModel.H"
00028 #include "mathematicalConstants.H"
00029 #include "meshTools.H"
00030 
00031 // * * * * * * * * * * * Protected Member Functions  * * * * * * * * * * * * //
00032 
00033 template<class CloudType>
00034 void Foam::InjectionModel<CloudType>::readProps()
00035 {
00036     IOobject propsDictHeader
00037     (
00038         "injectionProperties",
00039         owner_.db().time().timeName(),
00040         "uniform"/cloud::prefix/owner_.name(),
00041         owner_.db(),
00042         IOobject::MUST_READ,
00043         IOobject::NO_WRITE,
00044         false
00045     );
00046 
00047     if (propsDictHeader.headerOk())
00048     {
00049         const IOdictionary propsDict(propsDictHeader);
00050 
00051         propsDict.readIfPresent("massInjected", massInjected_);
00052         propsDict.readIfPresent("nInjections", nInjections_);
00053         propsDict.readIfPresent("parcelsAddedTotal", parcelsAddedTotal_);
00054         propsDict.readIfPresent("timeStep0", timeStep0_);
00055     }
00056 }
00057 
00058 
00059 template<class CloudType>
00060 void Foam::InjectionModel<CloudType>::writeProps()
00061 {
00062     if (owner_.db().time().outputTime())
00063     {
00064         IOdictionary propsDict
00065         (
00066             IOobject
00067             (
00068                 "injectionProperties",
00069                 owner_.db().time().timeName(),
00070                 "uniform"/cloud::prefix/owner_.name(),
00071                 owner_.db(),
00072                 IOobject::NO_READ,
00073                 IOobject::NO_WRITE,
00074                 false
00075             )
00076         );
00077 
00078         propsDict.add("massInjected", massInjected_);
00079         propsDict.add("nInjections", nInjections_);
00080         propsDict.add("parcelsAddedTotal", parcelsAddedTotal_);
00081         propsDict.add("timeStep0", timeStep0_);
00082 
00083         propsDict.regIOobject::write();
00084     }
00085 }
00086 
00087 
00088 template<class CloudType>
00089 void Foam::InjectionModel<CloudType>::prepareForNextTimeStep
00090 (
00091     const scalar time,
00092     label& newParcels,
00093     scalar& newVolume
00094 )
00095 {
00096     // Initialise values
00097     newParcels = 0;
00098     newVolume = 0.0;
00099 
00100     // Return if not started injection event
00101     if (time < SOI_)
00102     {
00103         timeStep0_ = time;
00104         return;
00105     }
00106 
00107     // Make times relative to SOI
00108     scalar t0 = timeStep0_ - SOI_;
00109     scalar t1 = time - SOI_;
00110 
00111     // Number of parcels to inject
00112     newParcels = parcelsToInject(t0, t1);
00113 
00114     // Volume of parcels to inject
00115     newVolume = volumeToInject(t0, t1);
00116 
00117     // Hold previous time if no parcels, but non-zero volume fraction
00118     if ((newParcels == 0) && (newVolume > 0.0))
00119     {
00120         // hold value of timeStep0_
00121     }
00122     else
00123     {
00124         // advance value of timeStep0_
00125         timeStep0_ = time;
00126     }
00127 }
00128 
00129 
00130 template<class CloudType>
00131 void Foam::InjectionModel<CloudType>::findCellAtPosition
00132 (
00133     label& cellI,
00134     vector& position
00135 )
00136 {
00137     const vector p0 = position;
00138 
00139     bool foundCell = false;
00140 
00141     cellI = owner_.mesh().findCell(position);
00142 
00143     if (cellI >= 0)
00144     {
00145         const vector& C = owner_.mesh().C()[cellI];
00146         position += SMALL*(C - position);
00147 
00148         foundCell = owner_.mesh().pointInCell(position, cellI);
00149     }
00150     reduce(foundCell, orOp<bool>());
00151 
00152     // Last chance - find nearest cell and try that one
00153     // - the point is probably on an edge
00154     if (!foundCell)
00155     {
00156         cellI = owner_.mesh().findNearestCell(position);
00157 
00158         if (cellI >= 0)
00159         {
00160             const vector& C = owner_.mesh().C()[cellI];
00161             position += SMALL*(C - position);
00162 
00163             foundCell = owner_.mesh().pointInCell(position, cellI);
00164         }
00165         reduce(foundCell, orOp<bool>());
00166     }
00167 
00168     if (!foundCell)
00169     {
00170         FatalErrorIn
00171         (
00172             "Foam::InjectionModel<CloudType>::findCellAtPosition"
00173             "("
00174                 "label&, "
00175                 "vector&"
00176             ")"
00177         )<< "Cannot find parcel injection cell. "
00178          << "Parcel position = " << p0 << nl
00179          << abort(FatalError);
00180     }
00181 }
00182 
00183 
00184 template<class CloudType>
00185 Foam::scalar Foam::InjectionModel<CloudType>::setNumberOfParticles
00186 (
00187     const label parcels,
00188     const scalar volume,
00189     const scalar diameter,
00190     const scalar rho
00191 )
00192 {
00193     scalar nP = 0.0;
00194     switch (parcelBasis_)
00195     {
00196         case pbMass:
00197         {
00198             nP = volume/volumeTotal_
00199                 *massTotal_/rho
00200                /(parcels*mathematicalConstant::pi/6.0*pow3(diameter));
00201             break;
00202         }
00203         case pbNumber:
00204         {
00205             nP = massTotal_/(rho*volumeTotal_*parcels);
00206             break;
00207         }
00208         default:
00209         {
00210             nP = 0.0;
00211             FatalErrorIn
00212             (
00213                 "Foam::scalar "
00214                 "Foam::InjectionModel<CloudType>::setNumberOfParticles"
00215                 "("
00216                 "    const label, "
00217                 "    const scalar, "
00218                 "    const scalar, "
00219                 "    const scalar, "
00220                 "    const scalar"
00221                 ")"
00222             )<< "Unknown parcelBasis type" << nl
00223              << exit(FatalError);
00224         }
00225     }
00226 
00227     return nP;
00228 }
00229 
00230 
00231 template<class CloudType>
00232 void Foam::InjectionModel<CloudType>::postInjectCheck(const label parcelsAdded)
00233 {
00234     if (parcelsAdded > 0)
00235     {
00236         Pout<< nl
00237             << "--> Cloud: " << owner_.name() << nl
00238             << "    Added " << parcelsAdded
00239             << " new parcels" << nl << endl;
00240     }
00241 
00242     // Increment total number of parcels added
00243     parcelsAddedTotal_ += parcelsAdded;
00244 
00245     // Update time for start of next injection
00246     time0_ = owner_.db().time().value();
00247 
00248     // Increment number of injections
00249     nInjections_++;
00250 
00251     // Write current state to properties file
00252     writeProps();
00253 }
00254 
00255 
00256 // * * * * * * * * * * * * * * * * Constructors  * * * * * * * * * * * * * * //
00257 
00258 template<class CloudType>
00259 Foam::InjectionModel<CloudType>::InjectionModel(CloudType& owner)
00260 :
00261     dict_(dictionary::null),
00262     owner_(owner),
00263     coeffDict_(dictionary::null),
00264     SOI_(0.0),
00265     volumeTotal_(0.0),
00266     massTotal_(0.0),
00267     massInjected_(0.0),
00268     nInjections_(0),
00269     parcelsAddedTotal_(0),
00270     parcelBasis_(pbNumber),
00271     time0_(0.0),
00272     timeStep0_(0.0)
00273 {
00274     readProps();
00275 }
00276 
00277 
00278 template<class CloudType>
00279 Foam::InjectionModel<CloudType>::InjectionModel
00280 (
00281     const dictionary& dict,
00282     CloudType& owner,
00283     const word& type
00284 )
00285 :
00286     dict_(dict),
00287     owner_(owner),
00288     coeffDict_(dict.subDict(type + "Coeffs")),
00289     SOI_(readScalar(coeffDict_.lookup("SOI"))),
00290     volumeTotal_(0.0),
00291     massTotal_(dimensionedScalar(coeffDict_.lookup("massTotal")).value()),
00292     massInjected_(0.0),
00293     nInjections_(0),
00294     parcelsAddedTotal_(0),
00295     parcelBasis_(pbNumber),
00296     time0_(owner.db().time().value()),
00297     timeStep0_(0.0)
00298 {
00299     // Provide some info
00300     // - also serves to initialise mesh dimensions - needed for parallel runs
00301     //   due to lazy evaluation of valid mesh dimensions
00302     Info<< "    Constructing " << owner.mesh().nGeometricD() << "-D injection"
00303         << endl;
00304 
00305     word parcelBasisType = coeffDict_.lookup("parcelBasisType");
00306     if (parcelBasisType == "mass")
00307     {
00308         parcelBasis_ = pbMass;
00309     }
00310     else if (parcelBasisType == "number")
00311     {
00312         parcelBasis_ = pbNumber;
00313     }
00314     else
00315     {
00316         FatalErrorIn
00317         (
00318             "Foam::InjectionModel<CloudType>::InjectionModel"
00319             "("
00320                 "const dictionary&, "
00321                 "CloudType&, "
00322                 "const word&"
00323             ")"
00324         )<< "parcelBasisType must be either 'number' or 'mass'" << nl
00325          << exit(FatalError);
00326     }
00327 
00328     readProps();
00329 }
00330 
00331 
00332 // * * * * * * * * * * * * * * * * Destructor  * * * * * * * * * * * * * * * //
00333 
00334 template<class CloudType>
00335 Foam::InjectionModel<CloudType>::~InjectionModel()
00336 {}
00337 
00338 
00339 // * * * * * * * * * * * * * * * Member Functions  * * * * * * * * * * * * * //
00340 
00341 template<class CloudType>
00342 template<class TrackData>
00343 void Foam::InjectionModel<CloudType>::inject(TrackData& td)
00344 {
00345     if (!active())
00346     {
00347         return;
00348     }
00349 
00350     const scalar time = owner_.db().time().value();
00351     const scalar carrierDt = owner_.db().time().deltaT().value();
00352     const polyMesh& mesh = owner_.mesh();
00353 
00354     // Prepare for next time step
00355     label newParcels = 0;
00356     scalar newVolume = 0.0;
00357     prepareForNextTimeStep(time, newParcels, newVolume);
00358 
00359     // Return if no parcels are required
00360     if (newParcels == 0)
00361     {
00362         postInjectCheck(0);
00363         return;
00364     }
00365 
00366     // Duration of injection period during this timestep
00367     const scalar deltaT =
00368         max(0.0, min(carrierDt, min(time - SOI_, timeEnd() - time0_)));
00369 
00370     // Pad injection time if injection starts during this timestep
00371     const scalar padTime = max(0.0, SOI_ - time0_);
00372 
00373     // Introduce new parcels linearly across carrier phase timestep
00374     label parcelsAdded = 0;
00375     for (label parcelI=0; parcelI<newParcels; parcelI++)
00376     {
00377         if (validInjection(parcelI))
00378         {
00379             // Calculate the pseudo time of injection for parcel 'parcelI'
00380             scalar timeInj = time0_ + padTime + deltaT*parcelI/newParcels;
00381 
00382             // Determine the injection position and owner cell
00383             label cellI = -1;
00384             vector pos = vector::zero;
00385             setPositionAndCell(parcelI, newParcels, timeInj, pos, cellI);
00386 
00387             if (cellI > -1)
00388             {
00389                 // Lagrangian timestep
00390                 scalar dt = time - timeInj;
00391 
00392                 // Apply corrections to position for 2-D cases
00393                 meshTools::constrainToMeshCentre(mesh, pos);
00394 
00395                 // Create a new parcel
00396                 parcelType* pPtr = new parcelType(td.cloud(), pos, cellI);
00397 
00398                 // Assign new parcel properties in injection model
00399                 setProperties(parcelI, newParcels, timeInj, *pPtr);
00400 
00401                 // Check new parcel properties
00402                 td.cloud().checkParcelProperties(*pPtr, dt, fullyDescribed());
00403 
00404                 // Apply correction to velocity for 2-D cases
00405                 meshTools::constrainDirection
00406                 (
00407                     mesh,
00408                     mesh.solutionD(),
00409                     pPtr->U()
00410                 );
00411 
00412                 // Number of particles per parcel
00413                 pPtr->nParticle() =
00414                     setNumberOfParticles
00415                     (
00416                         newParcels,
00417                         newVolume,
00418                         pPtr->d(),
00419                         pPtr->rho()
00420                     );
00421 
00422                 // Add the new parcel
00423                 td.cloud().addParticle(pPtr);
00424 
00425                 massInjected_ += pPtr->nParticle()*pPtr->mass();
00426                 parcelsAdded++;
00427             }
00428         }
00429     }
00430 
00431     postInjectCheck(parcelsAdded);
00432 }
00433 
00434 
00435 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
00436 
00437 #include "NewInjectionModel.C"
00438 
00439 // ************************************************************************* //
Copyright © 2000-2009 OpenCFD Ltd