DSMCCloud.C 27.4 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
(
    const IOdictionary& dsmcInitialiseDict
)
{
    Info<< nl << "Initialising particles" << endl;

    const scalar temperature
    (
87
        dsmcInitialiseDict.get<scalar>("temperature")
Henry's avatar
Henry committed
88
89
    );

90
    const vector velocity(dsmcInitialiseDict.get<vector>("velocity"));
Henry's avatar
Henry committed
91
92
93
94
95
96
97
98
99
100
101
102

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

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

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

    forAll(molecules, i)
    {
103
        numberDensities[i] = numberDensitiesDict.get<scalar>(molecules[i]);
Henry's avatar
Henry committed
104
105
106
107
    }

    numberDensities /= nParticle_;

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

        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]);

126
                label typeId = typeIdList_.find(moleculeName);
Henry's avatar
Henry committed
127
128
129

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

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

                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)
151
                  > rndGen_.sample01<scalar>()
Henry's avatar
Henry committed
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
                )
                {
                    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;

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

    // 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
    );

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

    sigmaTcRMax_.correctBoundaryConditions();
}


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

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

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

    label collisionCandidates = 0;

    label collisions = 0;

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

        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());

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

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

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

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

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

255
            scalar sigmaTcRMax = sigmaTcRMax_[celli];
Henry's avatar
Henry committed
256
257

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

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

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

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

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

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

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

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

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

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

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

                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

334
                if (sigmaTcR > sigmaTcRMax_[celli])
Henry's avatar
Henry committed
335
                {
336
                    sigmaTcRMax_[celli] = sigmaTcR;
Henry's avatar
Henry committed
337
338
                }

339
                if ((sigmaTcR/sigmaTcRMax) > rndGen_.sample01<scalar>())
Henry's avatar
Henry committed
340
341
342
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
                {
                    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>
375
void Foam::DSMCCloud<ParcelType>::resetFields()
Henry's avatar
Henry committed
376
377
378
379
380
381
382
{
    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
383
        Zero
Henry's avatar
Henry committed
384
385
386
387
388
389
390
391
392
393
394
395
396
    );

    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
397
        Zero
Henry's avatar
Henry committed
398
399
400
401
402
    );
}


template<class ParcelType>
403
void Foam::DSMCCloud<ParcelType>::calculateFields()
Henry's avatar
Henry committed
404
{
405
406
407
408
409
410
411
    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
412

413
    forAllConstIter(typename DSMCCloud<ParcelType>, *this, iter)
Henry's avatar
Henry committed
414
415
    {
        const ParcelType& p = iter();
416
417
418
419
420
421
422
423
424
        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
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
    }

    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>
452
void Foam::DSMCCloud<ParcelType>::addNewParcel
Henry's avatar
Henry committed
453
454
(
    const vector& position,
455
    const label celli,
Henry's avatar
Henry committed
456
457
458
459
460
    const vector& U,
    const scalar Ei,
    const label typeId
)
{
461
    this->addParticle(new ParcelType(mesh_, position, celli, U, Ei, typeId));
Henry's avatar
Henry committed
462
463
464
465
466
467
}


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

template<class ParcelType>
468
Foam::DSMCCloud<ParcelType>::DSMCCloud
Henry's avatar
Henry committed
469
470
471
472
473
474
475
(
    const word& cloudName,
    const fvMesh& mesh,
    bool readFields
)
:
    Cloud<ParcelType>(mesh, cloudName, false),
476
    DSMCBaseCloud(),
Henry's avatar
Henry committed
477
478
479
480
481
482
483
484
485
486
487
488
489
490
    cloudName_(cloudName),
    mesh_(mesh),
    particleProperties_
    (
        IOobject
        (
            cloudName + "Properties",
            mesh_.time().constant(),
            mesh_,
            IOobject::MUST_READ_IF_MODIFIED,
            IOobject::NO_WRITE
        )
    ),
    typeIdList_(particleProperties_.lookup("typeIdList")),
491
    nParticle_(particleProperties_.get<scalar>("nEquivalentParticles")),
Henry's avatar
Henry committed
492
493
494
495
496
497
498
499
500
501
502
503
504
    cellOccupancy_(mesh_.nCells()),
    sigmaTcRMax_
    (
        IOobject
        (
            this->name() + "SigmaTcRMax",
            mesh_.time().timeName(),
            mesh_,
            IOobject::MUST_READ,
            IOobject::AUTO_WRITE
        ),
        mesh_
    ),
505
506
507
508
509
510
511
512
513
    collisionSelectionRemainder_
    (
        IOobject
        (
            this->name() + ":collisionSelectionRemainder",
            mesh_.time().timeName(),
            mesh_
        ),
        mesh_,
514
        dimensionedScalar(dimless, Zero)
515
    ),
Henry's avatar
Henry committed
516
517
518
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
    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_(),
625
    rndGen_(Pstream::myProcNo()),
Henry's avatar
Henry committed
626
627
628
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
    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_
    (
658
        BinaryCollisionModel<DSMCCloud<ParcelType>>::New
Henry's avatar
Henry committed
659
660
661
662
663
664
665
        (
            particleProperties_,
            *this
        )
    ),
    wallInteractionModel_
    (
666
        WallInteractionModel<DSMCCloud<ParcelType>>::New
Henry's avatar
Henry committed
667
668
669
670
671
672
673
        (
            particleProperties_,
            *this
        )
    ),
    inflowBoundaryModel_
    (
674
        InflowBoundaryModel<DSMCCloud<ParcelType>>::New
Henry's avatar
Henry committed
675
676
677
678
679
680
681
682
683
684
685
686
687
        (
            particleProperties_,
            *this
        )
    )
{
    buildConstProps();
    buildCellOccupancy();

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

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


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


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

template<class ParcelType>
913
Foam::DSMCCloud<ParcelType>::~DSMCCloud()
Henry's avatar
Henry committed
914
915
916
917
918
919
{}


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

template<class ParcelType>
920
void Foam::DSMCCloud<ParcelType>::evolve()
Henry's avatar
Henry committed
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
{
    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
936
    Cloud<ParcelType>::move(*this, td, mesh_.time().deltaTValue());
Henry's avatar
Henry committed
937
938
939
940
941
942
943
944
945
946
947
948
949

    // Update cell occupancy
    buildCellOccupancy();

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

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


template<class ParcelType>
950
void Foam::DSMCCloud<ParcelType>::info() const
Henry's avatar
Henry committed
951
{
952
953
    label nDSMCParticles = this->size();
    reduce(nDSMCParticles, sumOp<label>());
Henry's avatar
Henry committed
954

955
    scalar nMol = nDSMCParticles*nParticle_;
Henry's avatar
Henry committed
956
957
958
959
960
961
962
963
964
965
966
967

    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        = "
968
        << nDSMCParticles
Henry's avatar
Henry committed
969
970
        << endl;

971
    if (nDSMCParticles)
Henry's avatar
Henry committed
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
    {
        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>
993
Foam::vector Foam::DSMCCloud<ParcelType>::equipartitionLinearVelocity
Henry's avatar
Henry committed
994
995
996
997
998
999
1000
(
    scalar temperature,
    scalar mass
)
{
    return
        sqrt(physicoChemical::k.value()*temperature/mass)
1001
       *rndGen_.GaussNormal<vector>();
Henry's avatar
Henry committed
1002
1003
1004
1005
}


template<class ParcelType>
1006
Foam::scalar Foam::DSMCCloud<ParcelType>::equipartitionInternalEnergy
Henry's avatar
Henry committed
1007
1008
(
    scalar temperature,
1009
    direction iDof
Henry's avatar
Henry committed
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
)
{
    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;
1021
1022
1023
        Ei =
           -log(rndGen_.sample01<scalar>())
           *physicoChemical::k.value()*temperature;
Henry's avatar
Henry committed
1024
1025
1026
1027
1028
1029
1030
1031
1032
    }
    else
    {
        scalar a = 0.5*iDof - 1;
        scalar energyRatio;
        scalar P = -1;

        do
        {
1033
            energyRatio = 10*rndGen_.sample01<scalar>();
Henry's avatar
Henry committed
1034
            P = pow((energyRatio/a), a)*exp(a - energyRatio);
1035
        } while (P < rndGen_.sample01<scalar>());
Henry's avatar
Henry committed
1036
1037
1038
1039
1040
1041
1042
1043
1044

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

    return Ei;
}


template<class ParcelType>
1045
void Foam::DSMCCloud<ParcelType>::dumpParticlePositions() const
Henry's avatar
Henry committed
1046
1047
1048
1049
1050
1051
1052
1053
{
    OFstream pObj
    (
        this->db().time().path()/"parcelPositions_"
      + this->name() + "_"
      + this->db().time().timeName() + ".obj"
    );

1054
    forAllConstIter(typename DSMCCloud<ParcelType>, *this, iter)
Henry's avatar
Henry committed
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
    {
        const ParcelType& p = iter();

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

    pObj.flush();
}


1068
1069
1070
template<class ParcelType>
void Foam::DSMCCloud<ParcelType>::autoMap(const mapPolyMesh& mapper)
{
1071
    Cloud<ParcelType>::autoMap(mapper);
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081

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

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


Henry's avatar
Henry committed
1082
// ************************************************************************* //