00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027 #include "DsmcCloud.H"
00028 #include "BinaryCollisionModel.H"
00029 #include "WallInteractionModel.H"
00030 #include "InflowBoundaryModel.H"
00031
00032
00033
00034 template<class ParcelType>
00035 Foam::scalar Foam::DsmcCloud<ParcelType>::kb = 1.380650277e-23;
00036
00037
00038
00039
00040 template<class ParcelType>
00041 void Foam::DsmcCloud<ParcelType>::buildConstProps()
00042 {
00043 Info<< nl << "Constructing constant properties for" << endl;
00044 constProps_.setSize(typeIdList_.size());
00045
00046 dictionary moleculeProperties
00047 (
00048 particleProperties_.subDict("moleculeProperties")
00049 );
00050
00051 forAll(typeIdList_, i)
00052 {
00053 const word& id(typeIdList_[i]);
00054
00055 Info<< " " << id << endl;
00056
00057 const dictionary& molDict(moleculeProperties.subDict(id));
00058
00059 constProps_[i] =
00060 typename ParcelType::constantProperties::constantProperties(molDict);
00061 }
00062 }
00063
00064
00065 template<class ParcelType>
00066 void Foam::DsmcCloud<ParcelType>::buildCellOccupancy()
00067 {
00068 forAll(cellOccupancy_, cO)
00069 {
00070 cellOccupancy_[cO].clear();
00071 }
00072
00073 forAllIter(typename DsmcCloud<ParcelType>, *this, iter)
00074 {
00075 cellOccupancy_[iter().cell()].append(&iter());
00076 }
00077 }
00078
00079
00080 template<class ParcelType>
00081 void Foam::DsmcCloud<ParcelType>::initialise
00082 (
00083 const IOdictionary& dsmcInitialiseDict
00084 )
00085 {
00086 Info<< nl << "Initialising particles" << endl;
00087
00088 const scalar temperature
00089 (
00090 readScalar(dsmcInitialiseDict.lookup("temperature"))
00091 );
00092
00093 const vector velocity(dsmcInitialiseDict.lookup("velocity"));
00094
00095 const dictionary& numberDensitiesDict
00096 (
00097 dsmcInitialiseDict.subDict("numberDensities")
00098 );
00099
00100 List<word> molecules(numberDensitiesDict.toc());
00101
00102 Field<scalar> numberDensities(molecules.size());
00103
00104 forAll(molecules, i)
00105 {
00106 numberDensities[i] = readScalar
00107 (
00108 numberDensitiesDict.lookup(molecules[i])
00109 );
00110 }
00111
00112 numberDensities /= nParticle_;
00113
00114 forAll(mesh_.cells(), cell)
00115 {
00116 const vector& cC = mesh_.cellCentres()[cell];
00117 const labelList& cellFaces = mesh_.cells()[cell];
00118 const scalar cV = mesh_.cellVolumes()[cell];
00119
00120 label nTets = 0;
00121
00122
00123 forAll(cellFaces, face)
00124 {
00125 nTets += mesh_.faces()[cellFaces[face]].size() - 2;
00126 }
00127
00128
00129
00130 scalarList cTetVFracs(nTets, 0.0);
00131
00132 List<labelList> tetPtIs(nTets, labelList(3,-1));
00133
00134
00135 label tet = 0;
00136
00137 forAll(cellFaces, face)
00138 {
00139 const labelList& facePoints = mesh_.faces()[cellFaces[face]];
00140
00141 label pointI = 1;
00142 while ((pointI + 1) < facePoints.size())
00143 {
00144
00145 const vector& pA = mesh_.points()[facePoints[0]];
00146 const vector& pB = mesh_.points()[facePoints[pointI]];
00147 const vector& pC = mesh_.points()[facePoints[pointI + 1]];
00148
00149 cTetVFracs[tet] =
00150 mag(((pA - cC) ^ (pB - cC)) & (pC - cC))/(cV*6.0)
00151 + cTetVFracs[max((tet - 1),0)];
00152
00153 tetPtIs[tet][0] = facePoints[0];
00154 tetPtIs[tet][1] = facePoints[pointI];
00155 tetPtIs[tet][2] = facePoints[pointI + 1];
00156
00157 pointI++;
00158 tet++;
00159 }
00160 }
00161
00162
00163
00164 cTetVFracs[nTets - 1] = 1.0;
00165
00166 forAll(molecules, i)
00167 {
00168 const word& moleculeName(molecules[i]);
00169
00170 label typeId(findIndex(typeIdList_, moleculeName));
00171
00172 if (typeId == -1)
00173 {
00174 FatalErrorIn("Foam::DsmcCloud<ParcelType>::initialise")
00175 << "typeId " << moleculeName << "not defined." << nl
00176 << abort(FatalError);
00177 }
00178
00179 const typename ParcelType::constantProperties& cP =
00180 constProps(typeId);
00181
00182 scalar numberDensity = numberDensities[i];
00183
00184
00185 scalar particlesRequired = numberDensity*mesh_.cellVolumes()[cell];
00186
00187
00188 label nParticlesToInsert = label(particlesRequired);
00189
00190
00191
00192 if ((particlesRequired - nParticlesToInsert) > rndGen_.scalar01())
00193 {
00194 nParticlesToInsert++;
00195 }
00196
00197 for (label pI = 0; pI < nParticlesToInsert; pI++)
00198 {
00199
00200
00201 scalar s = rndGen_.scalar01();
00202 scalar t = rndGen_.scalar01();
00203 scalar u = rndGen_.scalar01();
00204
00205 if (s + t > 1.0)
00206 {
00207 s = 1.0 - s;
00208 t = 1.0 - t;
00209 }
00210
00211 if (t + u > 1.0)
00212 {
00213 scalar tmp = u;
00214 u = 1.0 - s - t;
00215 t = 1.0 - tmp;
00216 }
00217 else if (s + t + u > 1.0)
00218 {
00219 scalar tmp = u;
00220 u = s + t + u - 1.0;
00221 s = 1.0 - t - tmp;
00222 }
00223
00224
00225
00226 scalar tetSelection = rndGen_.scalar01();
00227
00228
00229 label sTet = -1;
00230
00231 forAll(cTetVFracs, tet)
00232 {
00233 sTet = tet;
00234
00235 if (cTetVFracs[tet] >= tetSelection)
00236 {
00237 break;
00238 }
00239 }
00240
00241 vector p =
00242 (1 - s - t - u)*cC
00243 + s*mesh_.points()[tetPtIs[sTet][0]]
00244 + t*mesh_.points()[tetPtIs[sTet][1]]
00245 + u*mesh_.points()[tetPtIs[sTet][2]];
00246
00247 vector U = equipartitionLinearVelocity
00248 (
00249 temperature,
00250 cP.mass()
00251 );
00252
00253 scalar Ei = equipartitionInternalEnergy
00254 (
00255 temperature,
00256 cP.internalDegreesOfFreedom()
00257 );
00258
00259 U += velocity;
00260
00261 addNewParcel
00262 (
00263 p,
00264 U,
00265 Ei,
00266 cell,
00267 typeId
00268 );
00269 }
00270 }
00271 }
00272
00273
00274
00275
00276
00277 label mostAbundantType(findMax(numberDensities));
00278
00279 const typename ParcelType::constantProperties& cP = constProps
00280 (
00281 mostAbundantType
00282 );
00283
00284 sigmaTcRMax_.internalField() = cP.sigmaT()*maxwellianMostProbableSpeed
00285 (
00286 temperature,
00287 cP.mass()
00288 );
00289
00290 sigmaTcRMax_.correctBoundaryConditions();
00291 }
00292
00293
00294 template<class ParcelType>
00295 void Foam::DsmcCloud<ParcelType>::collisions()
00296 {
00297 buildCellOccupancy();
00298
00299
00300 List<DynamicList<label> > subCells(8);
00301
00302 scalar deltaT = cachedDeltaT();
00303
00304 label collisionCandidates = 0;
00305
00306 label collisions = 0;
00307
00308 forAll(cellOccupancy_, celli)
00309 {
00310 const DynamicList<ParcelType*>& cellParcels(cellOccupancy_[celli]);
00311
00312 label nC(cellParcels.size());
00313
00314 if (nC > 1)
00315 {
00316
00317
00318
00319
00320
00321 forAll(subCells, i)
00322 {
00323 subCells[i].clear();
00324 }
00325
00326
00327 List<label> whichSubCell(cellParcels.size());
00328
00329 const point& cC = mesh_.cellCentres()[celli];
00330
00331 forAll(cellParcels, i)
00332 {
00333 ParcelType* p = cellParcels[i];
00334
00335 vector relPos = p->position() - cC;
00336
00337 label subCell =
00338 pos(relPos.x()) + 2*pos(relPos.y()) + 4*pos(relPos.z());
00339
00340 subCells[subCell].append(i);
00341
00342 whichSubCell[i] = subCell;
00343 }
00344
00345
00346
00347 scalar sigmaTcRMax = sigmaTcRMax_[celli];
00348
00349 scalar selectedPairs =
00350 collisionSelectionRemainder_[celli]
00351 + 0.5*nC*(nC - 1)*nParticle_*sigmaTcRMax*deltaT
00352 /mesh_.cellVolumes()[celli];
00353
00354 label nCandidates(selectedPairs);
00355
00356 collisionSelectionRemainder_[celli] = selectedPairs - nCandidates;
00357
00358 collisionCandidates += nCandidates;
00359
00360 for (label c = 0; c < nCandidates; c++)
00361 {
00362
00363
00364
00365
00366 label candidateP = rndGen_.integer(0, nC - 1);
00367
00368
00369 label candidateQ = -1;
00370
00371 List<label> subCellPs = subCells[whichSubCell[candidateP]];
00372
00373 label nSC = subCellPs.size();
00374
00375 if (nSC > 1)
00376 {
00377
00378
00379
00380
00381 do
00382 {
00383 candidateQ = subCellPs[rndGen_.integer(0, nSC - 1)];
00384
00385 } while(candidateP == candidateQ);
00386 }
00387 else
00388 {
00389
00390
00391
00392
00393 do
00394 {
00395 candidateQ = rndGen_.integer(0, nC - 1);
00396
00397 } while(candidateP == candidateQ);
00398 }
00399
00400
00401
00402
00403
00404
00405
00406
00407
00408
00409
00410
00411
00412
00413
00414
00415
00416
00417 ParcelType* parcelP = cellParcels[candidateP];
00418 ParcelType* parcelQ = cellParcels[candidateQ];
00419
00420 label typeIdP = parcelP->typeId();
00421 label typeIdQ = parcelQ->typeId();
00422
00423 scalar sigmaTcR = binaryCollision().sigmaTcR
00424 (
00425 typeIdP,
00426 typeIdQ,
00427 parcelP->U(),
00428 parcelQ->U()
00429 );
00430
00431
00432
00433
00434
00435 if (sigmaTcR > sigmaTcRMax_[celli])
00436 {
00437 sigmaTcRMax_[celli] = sigmaTcR;
00438 }
00439
00440 if ((sigmaTcR/sigmaTcRMax) > rndGen_.scalar01())
00441 {
00442 binaryCollision().collide
00443 (
00444 typeIdP,
00445 typeIdQ,
00446 parcelP->U(),
00447 parcelQ->U(),
00448 parcelP->Ei(),
00449 parcelQ->Ei()
00450 );
00451
00452 collisions++;
00453 }
00454 }
00455 }
00456 }
00457
00458 reduce(collisions, sumOp<label>());
00459
00460 reduce(collisionCandidates, sumOp<label>());
00461
00462 if (collisionCandidates)
00463 {
00464 Info<< " Collisions = "
00465 << collisions << nl
00466 << " Acceptance rate = "
00467 << scalar(collisions)/scalar(collisionCandidates) << nl
00468 << endl;
00469 }
00470 else
00471 {
00472 Info<< " No collisions" << endl;
00473 }
00474 }
00475
00476
00477 template<class ParcelType>
00478 void Foam::DsmcCloud<ParcelType>::resetSurfaceDataFields()
00479 {
00480 volScalarField::GeometricBoundaryField& qBF(q_.boundaryField());
00481
00482 forAll(qBF, p)
00483 {
00484 qBF[p] = 0.0;
00485 }
00486
00487 volVectorField::GeometricBoundaryField& fDBF(fD_.boundaryField());
00488
00489 forAll(fDBF, p)
00490 {
00491 fDBF[p] = vector::zero;
00492 }
00493 }
00494
00495
00496
00497
00498 template<class ParcelType>
00499 void Foam::DsmcCloud<ParcelType>::addNewParcel
00500 (
00501 const vector& position,
00502 const vector& U,
00503 const scalar Ei,
00504 const label cellId,
00505 const label typeId
00506 )
00507 {
00508 ParcelType* pPtr = new ParcelType
00509 (
00510 *this,
00511 position,
00512 U,
00513 Ei,
00514 cellId,
00515 typeId
00516 );
00517
00518 addParticle(pPtr);
00519 }
00520
00521
00522
00523
00524 template<class ParcelType>
00525 Foam::DsmcCloud<ParcelType>::DsmcCloud
00526 (
00527 const word& cloudName,
00528 const volScalarField& T,
00529 const volVectorField& U
00530 )
00531 :
00532 Cloud<ParcelType>(T.mesh(), cloudName, false),
00533 DsmcBaseCloud(),
00534 cloudName_(cloudName),
00535 mesh_(T.mesh()),
00536 particleProperties_
00537 (
00538 IOobject
00539 (
00540 cloudName + "Properties",
00541 mesh_.time().constant(),
00542 mesh_,
00543 IOobject::MUST_READ,
00544 IOobject::NO_WRITE
00545 )
00546 ),
00547 typeIdList_(particleProperties_.lookup("typeIdList")),
00548 nParticle_(readScalar(particleProperties_.lookup("nEquivalentParticles"))),
00549 cellOccupancy_(mesh_.nCells()),
00550 sigmaTcRMax_
00551 (
00552 IOobject
00553 (
00554 this->name() + "SigmaTcRMax",
00555 mesh_.time().timeName(),
00556 mesh_,
00557 IOobject::MUST_READ,
00558 IOobject::AUTO_WRITE
00559 ),
00560 mesh_
00561 ),
00562 collisionSelectionRemainder_(mesh_.nCells(), 0),
00563 q_
00564 (
00565 IOobject
00566 (
00567 this->name() + "q_",
00568 mesh_.time().timeName(),
00569 mesh_,
00570 IOobject::NO_READ,
00571 IOobject::NO_WRITE
00572 ),
00573 mesh_,
00574 dimensionedScalar("zero", dimensionSet(1, 0, -3, 0, 0), 0.0)
00575 ),
00576 fD_
00577 (
00578 IOobject
00579 (
00580 this->name() + "fD_",
00581 mesh_.time().timeName(),
00582 mesh_,
00583 IOobject::NO_READ,
00584 IOobject::NO_WRITE
00585 ),
00586 mesh_,
00587 dimensionedVector
00588 (
00589 "zero",
00590 dimensionSet(1, -1, -2, 0, 0),
00591 vector::zero
00592 )
00593 ),
00594 constProps_(),
00595 rndGen_(label(149382906) + 7183*Pstream::myProcNo()),
00596 T_(T),
00597 U_(U),
00598 binaryCollisionModel_
00599 (
00600 BinaryCollisionModel<DsmcCloud<ParcelType> >::New
00601 (
00602 particleProperties_,
00603 *this
00604 )
00605 ),
00606 wallInteractionModel_
00607 (
00608 WallInteractionModel<DsmcCloud<ParcelType> >::New
00609 (
00610 particleProperties_,
00611 *this
00612 )
00613 ),
00614 inflowBoundaryModel_
00615 (
00616 InflowBoundaryModel<DsmcCloud<ParcelType> >::New
00617 (
00618 particleProperties_,
00619 *this
00620 )
00621 )
00622 {
00623 buildConstProps();
00624
00625 buildCellOccupancy();
00626
00627
00628
00629 forAll(collisionSelectionRemainder_, i)
00630 {
00631 collisionSelectionRemainder_[i] = rndGen_.scalar01();
00632 }
00633 }
00634
00635
00636 template<class ParcelType>
00637 Foam::DsmcCloud<ParcelType>::DsmcCloud
00638 (
00639 const word& cloudName,
00640 const fvMesh& mesh
00641 )
00642 :
00643 Cloud<ParcelType>(mesh, cloudName, false),
00644 DsmcBaseCloud(),
00645 cloudName_(cloudName),
00646 mesh_(mesh),
00647 particleProperties_
00648 (
00649 IOobject
00650 (
00651 cloudName + "Properties",
00652 mesh_.time().constant(),
00653 mesh_,
00654 IOobject::MUST_READ,
00655 IOobject::NO_WRITE
00656 )
00657 ),
00658 typeIdList_(particleProperties_.lookup("typeIdList")),
00659 nParticle_(readScalar(particleProperties_.lookup("nEquivalentParticles"))),
00660 cellOccupancy_(),
00661 sigmaTcRMax_
00662 (
00663 IOobject
00664 (
00665 this->name() + "SigmaTcRMax",
00666 mesh_.time().timeName(),
00667 mesh_,
00668 IOobject::NO_READ,
00669 IOobject::AUTO_WRITE
00670 ),
00671 mesh_,
00672 dimensionedScalar("zero", dimensionSet(0, 3, -1, 0, 0), 0.0)
00673 ),
00674 collisionSelectionRemainder_(),
00675 q_
00676 (
00677 IOobject
00678 (
00679 this->name() + "q_",
00680 mesh_.time().timeName(),
00681 mesh_,
00682 IOobject::NO_READ,
00683 IOobject::NO_WRITE
00684 ),
00685 mesh_,
00686 dimensionedScalar("zero", dimensionSet(1, 0, -3, 0, 0), 0.0)
00687 ),
00688 fD_
00689 (
00690 IOobject
00691 (
00692 this->name() + "fD_",
00693 mesh_.time().timeName(),
00694 mesh_,
00695 IOobject::NO_READ,
00696 IOobject::NO_WRITE
00697 ),
00698 mesh_,
00699 dimensionedVector
00700 (
00701 "zero",
00702 dimensionSet(1, -1, -2, 0, 0),
00703 vector::zero
00704 )
00705 ),
00706 constProps_(),
00707 rndGen_(label(971501) + 1526*Pstream::myProcNo()),
00708 T_
00709 (
00710 volScalarField
00711 (
00712 IOobject
00713 (
00714 "T",
00715 mesh_.time().timeName(),
00716 mesh_,
00717 IOobject::NO_READ,
00718 IOobject::NO_WRITE
00719 ),
00720 mesh_,
00721 dimensionedScalar("zero", dimensionSet(0, 0, 0, 1, 0), 0.0)
00722 )
00723 ),
00724 U_
00725 (
00726 volVectorField
00727 (
00728 IOobject
00729 (
00730 "U",
00731 mesh_.time().timeName(),
00732 mesh_,
00733 IOobject::NO_READ,
00734 IOobject::NO_WRITE
00735 ),
00736 mesh_,
00737 dimensionedVector
00738 (
00739 "zero",
00740 dimensionSet(0, 1, -1, 0, 0),
00741 vector::zero
00742 )
00743 )
00744 ),
00745 binaryCollisionModel_(),
00746 wallInteractionModel_(),
00747 inflowBoundaryModel_()
00748 {
00749 clear();
00750
00751 buildConstProps();
00752
00753 IOdictionary dsmcInitialiseDict
00754 (
00755 IOobject
00756 (
00757 "dsmcInitialiseDict",
00758 mesh_.time().system(),
00759 mesh_,
00760 IOobject::MUST_READ,
00761 IOobject::NO_WRITE
00762 )
00763 );
00764
00765 initialise(dsmcInitialiseDict);
00766 }
00767
00768
00769
00770
00771 template<class ParcelType>
00772 Foam::DsmcCloud<ParcelType>::~DsmcCloud()
00773 {}
00774
00775
00776
00777
00778 template<class ParcelType>
00779 void Foam::DsmcCloud<ParcelType>::evolve()
00780 {
00781
00782 storeDeltaT();
00783
00784 typename ParcelType::trackData td(*this);
00785
00786
00787 resetSurfaceDataFields();
00788
00789 if (debug)
00790 {
00791 this->dumpParticlePositions();
00792 }
00793
00794
00795 this->inflowBoundary().inflow();
00796
00797
00798 Cloud<ParcelType>::move(td);
00799
00800
00801 collisions();
00802 }
00803
00804
00805 template<class ParcelType>
00806 void Foam::DsmcCloud<ParcelType>::info() const
00807 {
00808 label nDsmcParticles = this->size();
00809 reduce(nDsmcParticles, sumOp<label>());
00810
00811 scalar nMol = nDsmcParticles*nParticle_;
00812
00813 vector linearMomentum = linearMomentumOfSystem();
00814 reduce(linearMomentum, sumOp<vector>());
00815
00816 scalar linearKineticEnergy = linearKineticEnergyOfSystem();
00817 reduce(linearKineticEnergy, sumOp<scalar>());
00818
00819 scalar internalEnergy = internalEnergyOfSystem();
00820 reduce(internalEnergy, sumOp<scalar>());
00821
00822 Info<< "Cloud name: " << this->name() << nl
00823 << " Number of dsmc particles = "
00824 << nDsmcParticles
00825 << endl;
00826
00827 if (nDsmcParticles)
00828 {
00829 Info<< " Number of molecules = "
00830 << nMol << nl
00831 << " Mass in system = "
00832 << returnReduce(massInSystem(), sumOp<scalar>()) << nl
00833 << " Average linear momentum = "
00834 << linearMomentum/nMol << nl
00835 << " |Average linear momentum| = "
00836 << mag(linearMomentum)/nMol << nl
00837 << " Average linear kinetic energy = "
00838 << linearKineticEnergy/nMol << nl
00839 << " Average internal energy = "
00840 << internalEnergy/nMol << nl
00841 << " Average total energy = "
00842 << (internalEnergy + linearKineticEnergy)/nMol
00843 << endl;
00844 }
00845 }
00846
00847
00848 template<class ParcelType>
00849 Foam::vector Foam::DsmcCloud<ParcelType>::equipartitionLinearVelocity
00850 (
00851 scalar temperature,
00852 scalar mass
00853 )
00854 {
00855 return
00856 sqrt(kb*temperature/mass)
00857 *vector
00858 (
00859 rndGen_.GaussNormal(),
00860 rndGen_.GaussNormal(),
00861 rndGen_.GaussNormal()
00862 );
00863 }
00864
00865
00866 template<class ParcelType>
00867 Foam::scalar Foam::DsmcCloud<ParcelType>::equipartitionInternalEnergy
00868 (
00869 scalar temperature,
00870 scalar iDof
00871 )
00872 {
00873 scalar Ei = 0.0;
00874
00875 if (iDof < SMALL)
00876 {
00877 return Ei;
00878 }
00879 else if (iDof < 2.0 + SMALL && iDof > 2.0 - SMALL)
00880 {
00881
00882 Ei = -log(rndGen_.scalar01())*kb*temperature;
00883 }
00884 else
00885 {
00886 scalar a = 0.5*iDof - 1;
00887
00888 scalar energyRatio;
00889
00890 scalar P = -1;
00891
00892 do
00893 {
00894 energyRatio = 10*rndGen_.scalar01();
00895
00896 P = pow((energyRatio/a), a)*exp(a - energyRatio);
00897
00898 } while (P < rndGen_.scalar01());
00899
00900 Ei = energyRatio*kb*temperature;
00901 }
00902
00903 return Ei;
00904 }
00905
00906
00907 template<class ParcelType>
00908 void Foam::DsmcCloud<ParcelType>::dumpParticlePositions() const
00909 {
00910 OFstream pObj
00911 (
00912 this->db().time().path()/"parcelPositions_"
00913 + this->name() + "_"
00914 + this->db().time().timeName() + ".obj"
00915 );
00916
00917 forAllConstIter(typename DsmcCloud<ParcelType>, *this, iter)
00918 {
00919 const ParcelType& p = iter();
00920
00921 pObj<< "v " << p.position().x()
00922 << " " << p.position().y()
00923 << " " << p.position().z()
00924 << nl;
00925 }
00926
00927 pObj.flush();
00928 }
00929
00930
00931