DSMCCloud.C 27.8 KB
Newer Older
Henry's avatar
Henry committed
1
2
3
4
/*---------------------------------------------------------------------------*\
  =========                 |
  \\      /  F ield         | OpenFOAM: The Open Source CFD Toolbox
   \\    /   O peration     |
5
    \\  /    A nd           | Copyright (C) 2011-2017 OpenFOAM Foundation
Henry's avatar
Henry committed
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
     \\/     M anipulation  |
-------------------------------------------------------------------------------
License
    This file is part of OpenFOAM.

    OpenFOAM is free software: you can redistribute it and/or modify it
    under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    OpenFOAM is distributed in the hope that it will be useful, but WITHOUT
    ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
    FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
    for more details.

    You should have received a copy of the GNU General Public License
    along with OpenFOAM.  If not, see <http://www.gnu.org/licenses/>.

\*---------------------------------------------------------------------------*/

26
#include "DSMCCloud.H"
Henry's avatar
Henry committed
27
28
29
30
31
32
33
34
35
36
37
38
#include "BinaryCollisionModel.H"
#include "WallInteractionModel.H"
#include "InflowBoundaryModel.H"
#include "constants.H"
#include "zeroGradientFvPatchFields.H"
#include "polyMeshTetDecomposition.H"

using namespace Foam::constant;

// * * * * * * * * * * * * * Private Member Functions  * * * * * * * * * * * //

template<class ParcelType>
39
void Foam::DSMCCloud<ParcelType>::buildConstProps()
Henry's avatar
Henry committed
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
{
    Info<< nl << "Constructing constant properties for" << endl;
    constProps_.setSize(typeIdList_.size());

    dictionary moleculeProperties
    (
        particleProperties_.subDict("moleculeProperties")
    );

    forAll(typeIdList_, i)
    {
        const word& id(typeIdList_[i]);

        Info<< "    " << id << endl;

        const dictionary& molDict(moleculeProperties.subDict(id));

57
        constProps_[i] = typename ParcelType::constantProperties(molDict);
Henry's avatar
Henry committed
58
59
60
61
62
    }
}


template<class ParcelType>
63
void Foam::DSMCCloud<ParcelType>::buildCellOccupancy()
Henry's avatar
Henry committed
64
65
66
67
68
69
{
    forAll(cellOccupancy_, cO)
    {
        cellOccupancy_[cO].clear();
    }

70
    forAllIter(typename DSMCCloud<ParcelType>, *this, iter)
Henry's avatar
Henry committed
71
72
73
74
75
76
77
    {
        cellOccupancy_[iter().cell()].append(&iter());
    }
}


template<class ParcelType>
78
void Foam::DSMCCloud<ParcelType>::initialise
Henry's avatar
Henry committed
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
(
    const IOdictionary& dsmcInitialiseDict
)
{
    Info<< nl << "Initialising particles" << endl;

    const scalar temperature
    (
        readScalar(dsmcInitialiseDict.lookup("temperature"))
    );

    const vector velocity(dsmcInitialiseDict.lookup("velocity"));

    const dictionary& numberDensitiesDict
    (
        dsmcInitialiseDict.subDict("numberDensities")
    );

    List<word> molecules(numberDensitiesDict.toc());

    Field<scalar> numberDensities(molecules.size());

    forAll(molecules, i)
    {
        numberDensities[i] = readScalar
        (
            numberDensitiesDict.lookup(molecules[i])
        );
    }

    numberDensities /= nParticle_;

111
    forAll(mesh_.cells(), celli)
Henry's avatar
Henry committed
112
113
114
115
    {
        List<tetIndices> cellTets = polyMeshTetDecomposition::cellTetIndices
        (
            mesh_,
116
            celli
Henry's avatar
Henry committed
117
118
119
120
121
122
123
124
125
126
127
128
        );

        forAll(cellTets, tetI)
        {
            const tetIndices& cellTetIs = cellTets[tetI];
            tetPointRef tet = cellTetIs.tet(mesh_);
            scalar tetVolume = tet.mag();

            forAll(molecules, i)
            {
                const word& moleculeName(molecules[i]);

129
                label typeId = typeIdList_.find(moleculeName);
Henry's avatar
Henry committed
130
131
132

                if (typeId == -1)
                {
133
                    FatalErrorInFunction
Henry's avatar
Henry committed
134
135
136
137
138
                        << "typeId " << moleculeName << "not defined." << nl
                        << abort(FatalError);
                }

                const typename ParcelType::constantProperties& cP =
139
                    constProps(typeId);
Henry's avatar
Henry committed
140
141
142
143
144
145
146
147
148
149
150
151
152
153

                scalar numberDensity = numberDensities[i];

                // Calculate the number of particles required
                scalar particlesRequired = numberDensity*tetVolume;

                // Only integer numbers of particles can be inserted
                label nParticlesToInsert = label(particlesRequired);

                // Add another particle with a probability proportional to the
                // remainder of taking the integer part of particlesRequired
                if
                (
                    (particlesRequired - nParticlesToInsert)
154
                  > rndGen_.sample01<scalar>()
Henry's avatar
Henry committed
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
                )
                {
                    nParticlesToInsert++;
                }

                for (label pI = 0; pI < nParticlesToInsert; pI++)
                {
                    point p = tet.randomPoint(rndGen_);

                    vector U = equipartitionLinearVelocity
                    (
                        temperature,
                        cP.mass()
                    );

                    scalar Ei = equipartitionInternalEnergy
                    (
                        temperature,
                        cP.internalDegreesOfFreedom()
                    );

                    U += velocity;

178
                    addNewParcel(p, celli, U, Ei, typeId);
Henry's avatar
Henry committed
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
                }
            }
        }
    }

    // Initialise the sigmaTcRMax_ field to the product of the cross section of
    // the most abundant species and the most probable thermal speed (Bird,
    // p222-223)

    label mostAbundantType(findMax(numberDensities));

    const typename ParcelType::constantProperties& cP = constProps
    (
        mostAbundantType
    );

195
    sigmaTcRMax_.primitiveFieldRef() = cP.sigmaT()*maxwellianMostProbableSpeed
Henry's avatar
Henry committed
196
197
198
199
200
201
202
203
204
205
    (
        temperature,
        cP.mass()
    );

    sigmaTcRMax_.correctBoundaryConditions();
}


template<class ParcelType>
206
void Foam::DSMCCloud<ParcelType>::collisions()
Henry's avatar
Henry committed
207
208
209
210
211
212
213
{
    if (!binaryCollision().active())
    {
        return;
    }

    // Temporary storage for subCells
214
    List<DynamicList<label>> subCells(8);
Henry's avatar
Henry committed
215
216
217
218
219
220
221

    scalar deltaT = mesh().time().deltaTValue();

    label collisionCandidates = 0;

    label collisions = 0;

222
    forAll(cellOccupancy_, celli)
Henry's avatar
Henry committed
223
    {
224
        const DynamicList<ParcelType*>& cellParcels(cellOccupancy_[celli]);
Henry's avatar
Henry committed
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241

        label nC(cellParcels.size());

        if (nC > 1)
        {
            // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
            // Assign particles to one of 8 Cartesian subCells

            // Clear temporary lists
            forAll(subCells, i)
            {
                subCells[i].clear();
            }

            // Inverse addressing specifying which subCell a parcel is in
            List<label> whichSubCell(cellParcels.size());

242
            const point& cC = mesh_.cellCentres()[celli];
Henry's avatar
Henry committed
243
244
245
246
247
248
249

            forAll(cellParcels, i)
            {
                const ParcelType& p = *cellParcels[i];
                vector relPos = p.position() - cC;

                label subCell =
Henry Weller's avatar
Henry Weller committed
250
                    pos0(relPos.x()) + 2*pos0(relPos.y()) + 4*pos0(relPos.z());
Henry's avatar
Henry committed
251
252
253
254
255
256
257

                subCells[subCell].append(i);
                whichSubCell[i] = subCell;
            }

            // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

258
            scalar sigmaTcRMax = sigmaTcRMax_[celli];
Henry's avatar
Henry committed
259
260

            scalar selectedPairs =
261
                collisionSelectionRemainder_[celli]
Henry's avatar
Henry committed
262
              + 0.5*nC*(nC - 1)*nParticle_*sigmaTcRMax*deltaT
263
               /mesh_.cellVolumes()[celli];
Henry's avatar
Henry committed
264
265

            label nCandidates(selectedPairs);
266
            collisionSelectionRemainder_[celli] = selectedPairs - nCandidates;
Henry's avatar
Henry committed
267
268
269
270
271
272
273
274
            collisionCandidates += nCandidates;

            for (label c = 0; c < nCandidates; c++)
            {
                // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
                // subCell candidate selection procedure

                // Select the first collision candidate
275
                label candidateP = rndGen_.position<label>(0, nC - 1);
Henry's avatar
Henry committed
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290

                // Declare the second collision candidate
                label candidateQ = -1;

                List<label> subCellPs = subCells[whichSubCell[candidateP]];
                label nSC = subCellPs.size();

                if (nSC > 1)
                {
                    // If there are two or more particle in a subCell, choose
                    // another from the same cell.  If the same candidate is
                    // chosen, choose again.

                    do
                    {
291
292
                        label i = rndGen_.position<label>(0, nSC - 1);
                        candidateQ = subCellPs[i];
Henry's avatar
Henry committed
293
294
295
296
297
298
299
300
301
302
                    } while (candidateP == candidateQ);
                }
                else
                {
                    // Select a possible second collision candidate from the
                    // whole cell.  If the same candidate is chosen, choose
                    // again.

                    do
                    {
303
                        candidateQ = rndGen_.position<label>(0, nC - 1);
Henry's avatar
Henry committed
304
305
306
307
308
309
310
                    } while (candidateP == candidateQ);
                }

                // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
                // uniform candidate selection procedure

                // // Select the first collision candidate
311
                // label candidateP = rndGen_.position<label>(0, nC-1);
Henry's avatar
Henry committed
312
313

                // // Select a possible second collision candidate
314
                // label candidateQ = rndGen_.position<label>(0, nC-1);
Henry's avatar
Henry committed
315
316
317
318

                // // If the same candidate is chosen, choose again
                // while (candidateP == candidateQ)
                // {
319
                //     candidateQ = rndGen_.position<label>(0, nC-1);
Henry's avatar
Henry committed
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
                // }

                // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

                ParcelType& parcelP = *cellParcels[candidateP];
                ParcelType& parcelQ = *cellParcels[candidateQ];

                scalar sigmaTcR = binaryCollision().sigmaTcR
                (
                    parcelP,
                    parcelQ
                );

                // Update the maximum value of sigmaTcR stored, but use the
                // initial value in the acceptance-rejection criteria because
                // the number of collision candidates selected was based on this

337
                if (sigmaTcR > sigmaTcRMax_[celli])
Henry's avatar
Henry committed
338
                {
339
                    sigmaTcRMax_[celli] = sigmaTcR;
Henry's avatar
Henry committed
340
341
                }

342
                if ((sigmaTcR/sigmaTcRMax) > rndGen_.sample01<scalar>())
Henry's avatar
Henry committed
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
                {
                    binaryCollision().collide
                    (
                        parcelP,
                        parcelQ
                    );

                    collisions++;
                }
            }
        }
    }

    reduce(collisions, sumOp<label>());

    reduce(collisionCandidates, sumOp<label>());

    sigmaTcRMax_.correctBoundaryConditions();

    if (collisionCandidates)
    {
        Info<< "    Collisions                      = "
            << collisions << nl
            << "    Acceptance rate                 = "
            << scalar(collisions)/scalar(collisionCandidates) << nl
            << endl;
    }
    else
    {
        Info<< "    No collisions" << endl;
    }
}


template<class ParcelType>
378
void Foam::DSMCCloud<ParcelType>::resetFields()
Henry's avatar
Henry committed
379
380
381
382
383
384
385
{
    q_ = dimensionedScalar("zero",  dimensionSet(1, 0, -3, 0, 0), 0.0);

    fD_ = dimensionedVector
    (
        "zero",
        dimensionSet(1, -1, -2, 0, 0),
Henry Weller's avatar
Henry Weller committed
386
        Zero
Henry's avatar
Henry committed
387
388
389
390
391
392
393
394
395
396
397
398
399
    );

    rhoN_ = dimensionedScalar("zero",  dimensionSet(0, -3, 0, 0, 0), VSMALL);
    rhoM_ =  dimensionedScalar("zero",  dimensionSet(1, -3, 0, 0, 0), VSMALL);
    dsmcRhoN_ = dimensionedScalar("zero",  dimensionSet(0, -3, 0, 0, 0), 0.0);
    linearKE_ = dimensionedScalar("zero",  dimensionSet(1, -1, -2, 0, 0), 0.0);
    internalE_ = dimensionedScalar("zero",  dimensionSet(1, -1, -2, 0, 0), 0.0);
    iDof_ = dimensionedScalar("zero",  dimensionSet(0, -3, 0, 0, 0), VSMALL);

    momentum_ = dimensionedVector
    (
        "zero",
        dimensionSet(1, -2, -1, 0, 0),
Henry Weller's avatar
Henry Weller committed
400
        Zero
Henry's avatar
Henry committed
401
402
403
404
405
    );
}


template<class ParcelType>
406
void Foam::DSMCCloud<ParcelType>::calculateFields()
Henry's avatar
Henry committed
407
{
408
409
410
411
412
413
414
    scalarField& rhoN = rhoN_.primitiveFieldRef();
    scalarField& rhoM = rhoM_.primitiveFieldRef();
    scalarField& dsmcRhoN = dsmcRhoN_.primitiveFieldRef();
    scalarField& linearKE = linearKE_.primitiveFieldRef();
    scalarField& internalE = internalE_.primitiveFieldRef();
    scalarField& iDof = iDof_.primitiveFieldRef();
    vectorField& momentum = momentum_.primitiveFieldRef();
Henry's avatar
Henry committed
415

416
    forAllConstIter(typename DSMCCloud<ParcelType>, *this, iter)
Henry's avatar
Henry committed
417
418
    {
        const ParcelType& p = iter();
419
420
421
422
423
424
425
426
427
        const label celli = p.cell();

        rhoN[celli]++;
        rhoM[celli] += constProps(p.typeId()).mass();
        dsmcRhoN[celli]++;
        linearKE[celli] += 0.5*constProps(p.typeId()).mass()*(p.U() & p.U());
        internalE[celli] += p.Ei();
        iDof[celli] += constProps(p.typeId()).internalDegreesOfFreedom();
        momentum[celli] += constProps(p.typeId()).mass()*p.U();
Henry's avatar
Henry committed
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
    }

    rhoN *= nParticle_/mesh().cellVolumes();
    rhoN_.correctBoundaryConditions();

    rhoM *= nParticle_/mesh().cellVolumes();
    rhoM_.correctBoundaryConditions();

    dsmcRhoN_.correctBoundaryConditions();

    linearKE *= nParticle_/mesh().cellVolumes();
    linearKE_.correctBoundaryConditions();

    internalE *= nParticle_/mesh().cellVolumes();
    internalE_.correctBoundaryConditions();

    iDof *= nParticle_/mesh().cellVolumes();
    iDof_.correctBoundaryConditions();

    momentum *= nParticle_/mesh().cellVolumes();
    momentum_.correctBoundaryConditions();
}


// * * * * * * * * * * * Protected Member Functions  * * * * * * * * * * * * //

template<class ParcelType>
455
void Foam::DSMCCloud<ParcelType>::addNewParcel
Henry's avatar
Henry committed
456
457
(
    const vector& position,
458
    const label celli,
Henry's avatar
Henry committed
459
460
461
462
463
    const vector& U,
    const scalar Ei,
    const label typeId
)
{
464
    this->addParticle(new ParcelType(mesh_, position, celli, U, Ei, typeId));
Henry's avatar
Henry committed
465
466
467
468
469
470
}


// * * * * * * * * * * * * * * * * Constructors  * * * * * * * * * * * * * * //

template<class ParcelType>
471
Foam::DSMCCloud<ParcelType>::DSMCCloud
Henry's avatar
Henry committed
472
473
474
475
476
477
478
(
    const word& cloudName,
    const fvMesh& mesh,
    bool readFields
)
:
    Cloud<ParcelType>(mesh, cloudName, false),
479
    DSMCBaseCloud(),
Henry's avatar
Henry committed
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
    cloudName_(cloudName),
    mesh_(mesh),
    particleProperties_
    (
        IOobject
        (
            cloudName + "Properties",
            mesh_.time().constant(),
            mesh_,
            IOobject::MUST_READ_IF_MODIFIED,
            IOobject::NO_WRITE
        )
    ),
    typeIdList_(particleProperties_.lookup("typeIdList")),
    nParticle_(readScalar(particleProperties_.lookup("nEquivalentParticles"))),
    cellOccupancy_(mesh_.nCells()),
    sigmaTcRMax_
    (
        IOobject
        (
            this->name() + "SigmaTcRMax",
            mesh_.time().timeName(),
            mesh_,
            IOobject::MUST_READ,
            IOobject::AUTO_WRITE
        ),
        mesh_
    ),
508
509
510
511
512
513
514
515
516
517
518
    collisionSelectionRemainder_
    (
        IOobject
        (
            this->name() + ":collisionSelectionRemainder",
            mesh_.time().timeName(),
            mesh_
        ),
        mesh_,
        dimensionedScalar("collisionSelectionRemainder", dimless, 0)
    ),
Henry's avatar
Henry committed
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
    q_
    (
        IOobject
        (
            "q",
            mesh_.time().timeName(),
            mesh_,
            IOobject::MUST_READ,
            IOobject::AUTO_WRITE
        ),
        mesh_
    ),
    fD_
    (
        IOobject
        (
            "fD",
            mesh_.time().timeName(),
            mesh_,
            IOobject::MUST_READ,
            IOobject::AUTO_WRITE
        ),
        mesh_
    ),
    rhoN_
    (
        IOobject
        (
            "rhoN",
            mesh_.time().timeName(),
            mesh_,
            IOobject::MUST_READ,
            IOobject::AUTO_WRITE
        ),
        mesh_
    ),
    rhoM_
    (
        IOobject
        (
            "rhoM",
            mesh_.time().timeName(),
            mesh_,
            IOobject::MUST_READ,
            IOobject::AUTO_WRITE
        ),
        mesh_
    ),
    dsmcRhoN_
    (
        IOobject
        (
            "dsmcRhoN",
            mesh_.time().timeName(),
            mesh_,
            IOobject::MUST_READ,
            IOobject::AUTO_WRITE
        ),
        mesh_
    ),
    linearKE_
    (
        IOobject
        (
            "linearKE",
            mesh_.time().timeName(),
            mesh_,
            IOobject::MUST_READ,
            IOobject::AUTO_WRITE
        ),
        mesh_
    ),
    internalE_
    (
        IOobject
        (
            "internalE",
            mesh_.time().timeName(),
            mesh_,
            IOobject::MUST_READ,
            IOobject::AUTO_WRITE
        ),
        mesh_
    ),
    iDof_
    (
        IOobject
        (
            "iDof",
            mesh_.time().timeName(),
            mesh_,
            IOobject::MUST_READ,
            IOobject::AUTO_WRITE
        ),
        mesh_
    ),
    momentum_
    (
        IOobject
        (
            "momentum",
            mesh_.time().timeName(),
            mesh_,
            IOobject::MUST_READ,
            IOobject::AUTO_WRITE
        ),
        mesh_
    ),
    constProps_(),
628
    rndGen_(Pstream::myProcNo()),
Henry's avatar
Henry committed
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
    boundaryT_
    (
        volScalarField
        (
            IOobject
            (
                "boundaryT",
                mesh_.time().timeName(),
                mesh_,
                IOobject::MUST_READ,
                IOobject::AUTO_WRITE
            ),
            mesh_
        )
    ),
    boundaryU_
    (
        volVectorField
        (
            IOobject
            (
                "boundaryU",
                mesh_.time().timeName(),
                mesh_,
                IOobject::MUST_READ,
                IOobject::AUTO_WRITE
            ),
            mesh_
        )
    ),
    binaryCollisionModel_
    (
661
        BinaryCollisionModel<DSMCCloud<ParcelType>>::New
Henry's avatar
Henry committed
662
663
664
665
666
667
668
        (
            particleProperties_,
            *this
        )
    ),
    wallInteractionModel_
    (
669
        WallInteractionModel<DSMCCloud<ParcelType>>::New
Henry's avatar
Henry committed
670
671
672
673
674
675
676
        (
            particleProperties_,
            *this
        )
    ),
    inflowBoundaryModel_
    (
677
        InflowBoundaryModel<DSMCCloud<ParcelType>>::New
Henry's avatar
Henry committed
678
679
680
681
682
683
684
685
686
687
688
689
690
        (
            particleProperties_,
            *this
        )
    )
{
    buildConstProps();
    buildCellOccupancy();

    // Initialise the collision selection remainder to a random value between 0
    // and 1.
    forAll(collisionSelectionRemainder_, i)
    {
691
        collisionSelectionRemainder_[i] = rndGen_.sample01<scalar>();
Henry's avatar
Henry committed
692
693
694
695
696
697
698
699
700
701
    }

    if (readFields)
    {
        ParcelType::readFields(*this);
    }
}


template<class ParcelType>
702
Foam::DSMCCloud<ParcelType>::DSMCCloud
Henry's avatar
Henry committed
703
704
705
706
707
708
709
(
    const word& cloudName,
    const fvMesh& mesh,
    const IOdictionary& dsmcInitialiseDict
)
    :
    Cloud<ParcelType>(mesh, cloudName, false),
710
    DSMCBaseCloud(),
Henry's avatar
Henry committed
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
    cloudName_(cloudName),
    mesh_(mesh),
    particleProperties_
    (
        IOobject
        (
            cloudName + "Properties",
            mesh_.time().constant(),
            mesh_,
            IOobject::MUST_READ_IF_MODIFIED,
            IOobject::NO_WRITE
        )
    ),
    typeIdList_(particleProperties_.lookup("typeIdList")),
    nParticle_(readScalar(particleProperties_.lookup("nEquivalentParticles"))),
    cellOccupancy_(),
    sigmaTcRMax_
    (
        IOobject
        (
            this->name() + "SigmaTcRMax",
            mesh_.time().timeName(),
            mesh_,
            IOobject::NO_READ,
            IOobject::AUTO_WRITE
        ),
        mesh_,
        dimensionedScalar("zero",  dimensionSet(0, 3, -1, 0, 0), 0.0),
        zeroGradientFvPatchScalarField::typeName
    ),
741
742
743
744
745
746
747
748
749
750
751
    collisionSelectionRemainder_
    (
        IOobject
        (
            this->name() + ":collisionSelectionRemainder",
            mesh_.time().timeName(),
            mesh_
        ),
        mesh_,
        dimensionedScalar("collisionSelectionRemainder", dimless, 0)
    ),
Henry's avatar
Henry committed
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
    q_
    (
        IOobject
        (
            this->name() + "q_",
            mesh_.time().timeName(),
            mesh_,
            IOobject::NO_READ,
            IOobject::NO_WRITE
        ),
        mesh_,
        dimensionedScalar("zero",  dimensionSet(1, 0, -3, 0, 0), 0.0)
    ),
    fD_
    (
        IOobject
        (
            this->name() + "fD_",
            mesh_.time().timeName(),
            mesh_,
            IOobject::NO_READ,
            IOobject::NO_WRITE
        ),
        mesh_,
        dimensionedVector
        (
            "zero",
            dimensionSet(1, -1, -2, 0, 0),
Henry Weller's avatar
Henry Weller committed
780
            Zero
Henry's avatar
Henry committed
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
        )
    ),
    rhoN_
    (
        IOobject
        (
            this->name() + "rhoN_",
            mesh_.time().timeName(),
            mesh_,
            IOobject::NO_READ,
            IOobject::NO_WRITE
        ),
        mesh_,
        dimensionedScalar("zero",  dimensionSet(0, -3, 0, 0, 0), VSMALL)
    ),
    rhoM_
    (
        IOobject
        (
            this->name() + "rhoM_",
            mesh_.time().timeName(),
            mesh_,
            IOobject::NO_READ,
            IOobject::NO_WRITE
        ),
        mesh_,
        dimensionedScalar("zero",  dimensionSet(1, -3, 0, 0, 0), VSMALL)
    ),
    dsmcRhoN_
    (
        IOobject
        (
            this->name() + "dsmcRhoN_",
            mesh_.time().timeName(),
            mesh_,
            IOobject::NO_READ,
            IOobject::NO_WRITE
        ),
        mesh_,
        dimensionedScalar("zero",  dimensionSet(0, -3, 0, 0, 0), 0.0)
    ),
    linearKE_
    (
        IOobject
        (
            this->name() + "linearKE_",
            mesh_.time().timeName(),
            mesh_,
            IOobject::NO_READ,
            IOobject::NO_WRITE
        ),
        mesh_,
        dimensionedScalar("zero",  dimensionSet(1, -1, -2, 0, 0), 0.0)
    ),
    internalE_
    (
        IOobject
        (
            this->name() + "internalE_",
            mesh_.time().timeName(),
            mesh_,
            IOobject::NO_READ,
            IOobject::NO_WRITE
        ),
        mesh_,
        dimensionedScalar("zero",  dimensionSet(1, -1, -2, 0, 0), 0.0)
    ),
    iDof_
    (
        IOobject
        (
            this->name() + "iDof_",
            mesh_.time().timeName(),
            mesh_,
            IOobject::NO_READ,
            IOobject::NO_WRITE
        ),
        mesh_,
        dimensionedScalar("zero",  dimensionSet(0, -3, 0, 0, 0), VSMALL)
    ),
    momentum_
    (
        IOobject
        (
            this->name() + "momentum_",
            mesh_.time().timeName(),
            mesh_,
            IOobject::NO_READ,
            IOobject::NO_WRITE
        ),
        mesh_,
        dimensionedVector
        (
            "zero",
            dimensionSet(1, -2, -1, 0, 0),
Henry Weller's avatar
Henry Weller committed
876
            Zero
Henry's avatar
Henry committed
877
878
879
        )
    ),
    constProps_(),
880
    rndGen_(Pstream::myProcNo()),
Henry's avatar
Henry committed
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
    boundaryT_
    (
        volScalarField
        (
            IOobject
            (
                "boundaryT",
                mesh_.time().timeName(),
                mesh_,
                IOobject::NO_READ,
                IOobject::NO_WRITE
            ),
            mesh_,
            dimensionedScalar("zero",  dimensionSet(0, 0, 0, 1, 0), 0.0)
        )
    ),
    boundaryU_
    (
        volVectorField
        (
            IOobject
            (
                "boundaryU",
                mesh_.time().timeName(),
                mesh_,
                IOobject::NO_READ,
                IOobject::NO_WRITE
            ),
            mesh_,
            dimensionedVector
            (
                "zero",
                dimensionSet(0, 1, -1, 0, 0),
Henry Weller's avatar
Henry Weller committed
914
                Zero
Henry's avatar
Henry committed
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
            )
        )
    ),
    binaryCollisionModel_(),
    wallInteractionModel_(),
    inflowBoundaryModel_()
{
    clear();
    buildConstProps();
    initialise(dsmcInitialiseDict);
}


// * * * * * * * * * * * * * * * * Destructor  * * * * * * * * * * * * * * * //

template<class ParcelType>
931
Foam::DSMCCloud<ParcelType>::~DSMCCloud()
Henry's avatar
Henry committed
932
933
934
935
936
937
{}


// * * * * * * * * * * * * * * * Member Functions  * * * * * * * * * * * * * //

template<class ParcelType>
938
void Foam::DSMCCloud<ParcelType>::evolve()
Henry's avatar
Henry committed
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
{
    typename ParcelType::trackingData td(*this);

    // Reset the data collection fields
    resetFields();

    if (debug)
    {
        this->dumpParticlePositions();
    }

    // Insert new particles from the inflow boundary
    this->inflowBoundary().inflow();

    // Move the particles ballistically with their current velocities
954
    Cloud<ParcelType>::move(*this, td, mesh_.time().deltaTValue());
Henry's avatar
Henry committed
955
956
957
958
959
960
961
962
963
964
965
966
967

    // Update cell occupancy
    buildCellOccupancy();

    // Calculate new velocities via stochastic collisions
    collisions();

    // Calculate the volume field data
    calculateFields();
}


template<class ParcelType>
968
void Foam::DSMCCloud<ParcelType>::info() const
Henry's avatar
Henry committed
969
{
970
971
    label nDSMCParticles = this->size();
    reduce(nDSMCParticles, sumOp<label>());
Henry's avatar
Henry committed
972

973
    scalar nMol = nDSMCParticles*nParticle_;
Henry's avatar
Henry committed
974
975
976
977
978
979
980
981
982
983
984
985

    vector linearMomentum = linearMomentumOfSystem();
    reduce(linearMomentum, sumOp<vector>());

    scalar linearKineticEnergy = linearKineticEnergyOfSystem();
    reduce(linearKineticEnergy, sumOp<scalar>());

    scalar internalEnergy = internalEnergyOfSystem();
    reduce(internalEnergy, sumOp<scalar>());

    Info<< "Cloud name: " << this->name() << nl
        << "    Number of dsmc particles        = "
986
        << nDSMCParticles
Henry's avatar
Henry committed
987
988
        << endl;

989
    if (nDSMCParticles)
Henry's avatar
Henry committed
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
    {
        Info<< "    Number of molecules             = "
            << nMol << nl
            << "    Mass in system                  = "
            << returnReduce(massInSystem(), sumOp<scalar>()) << nl
            << "    Average linear momentum         = "
            << linearMomentum/nMol << nl
            << "   |Average linear momentum|        = "
            << mag(linearMomentum)/nMol << nl
            << "    Average linear kinetic energy   = "
            << linearKineticEnergy/nMol << nl
            << "    Average internal energy         = "
            << internalEnergy/nMol << nl
            << "    Average total energy            = "
            << (internalEnergy + linearKineticEnergy)/nMol
            << endl;
    }
}


template<class ParcelType>
1011
Foam::vector Foam::DSMCCloud<ParcelType>::equipartitionLinearVelocity
Henry's avatar
Henry committed
1012
1013
1014
1015
1016
1017
1018
(
    scalar temperature,
    scalar mass
)
{
    return
        sqrt(physicoChemical::k.value()*temperature/mass)
1019
       *rndGen_.GaussNormal<vector>();
Henry's avatar
Henry committed
1020
1021
1022
1023
}


template<class ParcelType>
1024
Foam::scalar Foam::DSMCCloud<ParcelType>::equipartitionInternalEnergy
Henry's avatar
Henry committed
1025
1026
(
    scalar temperature,
1027
    direction iDof
Henry's avatar
Henry committed
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
)
{
    scalar Ei = 0.0;

    if (iDof < SMALL)
    {
        return Ei;
    }
    else if (iDof < 2.0 + SMALL && iDof > 2.0 - SMALL)
    {
        // Special case for iDof = 2, i.e. diatomics;
1039
1040
1041
        Ei =
           -log(rndGen_.sample01<scalar>())
           *physicoChemical::k.value()*temperature;
Henry's avatar
Henry committed
1042
1043
1044
1045
1046
1047
1048
1049
1050
    }
    else
    {
        scalar a = 0.5*iDof - 1;
        scalar energyRatio;
        scalar P = -1;

        do
        {
1051
            energyRatio = 10*rndGen_.sample01<scalar>();
Henry's avatar
Henry committed
1052
            P = pow((energyRatio/a), a)*exp(a - energyRatio);
1053
        } while (P < rndGen_.sample01<scalar>());
Henry's avatar
Henry committed
1054
1055
1056
1057
1058
1059
1060
1061
1062

        Ei = energyRatio*physicoChemical::k.value()*temperature;
    }

    return Ei;
}


template<class ParcelType>
1063
void Foam::DSMCCloud<ParcelType>::dumpParticlePositions() const
Henry's avatar
Henry committed
1064
1065
1066
1067
1068
1069
1070
1071
{
    OFstream pObj
    (
        this->db().time().path()/"parcelPositions_"
      + this->name() + "_"
      + this->db().time().timeName() + ".obj"
    );

1072
    forAllConstIter(typename DSMCCloud<ParcelType>, *this, iter)
Henry's avatar
Henry committed
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
    {
        const ParcelType& p = iter();

        pObj<< "v " << p.position().x()
            << " "  << p.position().y()
            << " "  << p.position().z()
            << nl;
    }

    pObj.flush();
}


1086
1087
1088
template<class ParcelType>
void Foam::DSMCCloud<ParcelType>::autoMap(const mapPolyMesh& mapper)
{
1089
    Cloud<ParcelType>::autoMap(mapper);
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099

    // Update the cell occupancy field
    cellOccupancy_.setSize(mesh_.nCells());
    buildCellOccupancy();

    // Update the inflow BCs
    this->inflowBoundary().autoMap(mapper);
}


Henry's avatar
Henry committed
1100
// ************************************************************************* //