DSMCCloud.C 27.2 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
383
384
385
386
    q_ = dimensionedScalar("0", dimensionSet(1, 0, -3, 0, 0), Zero);
    fD_ = dimensionedVector("0", dimensionSet(1, -1, -2, 0, 0), Zero);

    rhoN_ = dimensionedScalar("0", dimensionSet(0, -3, 0, 0, 0), VSMALL);
    rhoM_ =  dimensionedScalar("0", dimensionSet(1, -3, 0, 0, 0), VSMALL);
    dsmcRhoN_ = dimensionedScalar("0", dimensionSet(0, -3, 0, 0, 0), Zero);
    linearKE_ = dimensionedScalar("0", dimensionSet(1, -1, -2, 0, 0), Zero);
    internalE_ = dimensionedScalar("0", dimensionSet(1, -1, -2, 0, 0), Zero);
    iDof_ = dimensionedScalar("0", dimensionSet(0, -3, 0, 0, 0), VSMALL);
    momentum_ = dimensionedVector("0", dimensionSet(1, -2, -1, 0, 0), Zero);
Henry's avatar
Henry committed
387
388
389
390
}


template<class ParcelType>
391
void Foam::DSMCCloud<ParcelType>::calculateFields()
Henry's avatar
Henry committed
392
{
393
394
395
396
397
398
399
    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
400

401
    forAllConstIter(typename DSMCCloud<ParcelType>, *this, iter)
Henry's avatar
Henry committed
402
403
    {
        const ParcelType& p = iter();
404
405
406
407
408
409
410
411
412
        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
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
    }

    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>
440
void Foam::DSMCCloud<ParcelType>::addNewParcel
Henry's avatar
Henry committed
441
442
(
    const vector& position,
443
    const label celli,
Henry's avatar
Henry committed
444
445
446
447
448
    const vector& U,
    const scalar Ei,
    const label typeId
)
{
449
    this->addParticle(new ParcelType(mesh_, position, celli, U, Ei, typeId));
Henry's avatar
Henry committed
450
451
452
453
454
455
}


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

template<class ParcelType>
456
Foam::DSMCCloud<ParcelType>::DSMCCloud
Henry's avatar
Henry committed
457
458
459
460
461
462
463
(
    const word& cloudName,
    const fvMesh& mesh,
    bool readFields
)
:
    Cloud<ParcelType>(mesh, cloudName, false),
464
    DSMCBaseCloud(),
Henry's avatar
Henry committed
465
466
467
468
469
470
471
472
473
474
475
476
477
478
    cloudName_(cloudName),
    mesh_(mesh),
    particleProperties_
    (
        IOobject
        (
            cloudName + "Properties",
            mesh_.time().constant(),
            mesh_,
            IOobject::MUST_READ_IF_MODIFIED,
            IOobject::NO_WRITE
        )
    ),
    typeIdList_(particleProperties_.lookup("typeIdList")),
479
    nParticle_(particleProperties_.get<scalar>("nEquivalentParticles")),
Henry's avatar
Henry committed
480
481
482
483
484
485
486
487
488
489
490
491
492
    cellOccupancy_(mesh_.nCells()),
    sigmaTcRMax_
    (
        IOobject
        (
            this->name() + "SigmaTcRMax",
            mesh_.time().timeName(),
            mesh_,
            IOobject::MUST_READ,
            IOobject::AUTO_WRITE
        ),
        mesh_
    ),
493
494
495
496
497
498
499
500
501
    collisionSelectionRemainder_
    (
        IOobject
        (
            this->name() + ":collisionSelectionRemainder",
            mesh_.time().timeName(),
            mesh_
        ),
        mesh_,
502
        dimensionedScalar(dimless, Zero)
503
    ),
Henry's avatar
Henry committed
504
505
506
507
508
509
510
511
512
513
514
515
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
    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_(),
613
    rndGen_(Pstream::myProcNo()),
Henry's avatar
Henry committed
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
    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_
    (
646
        BinaryCollisionModel<DSMCCloud<ParcelType>>::New
Henry's avatar
Henry committed
647
648
649
650
651
652
653
        (
            particleProperties_,
            *this
        )
    ),
    wallInteractionModel_
    (
654
        WallInteractionModel<DSMCCloud<ParcelType>>::New
Henry's avatar
Henry committed
655
656
657
658
659
660
661
        (
            particleProperties_,
            *this
        )
    ),
    inflowBoundaryModel_
    (
662
        InflowBoundaryModel<DSMCCloud<ParcelType>>::New
Henry's avatar
Henry committed
663
664
665
666
667
668
669
670
671
672
673
674
675
        (
            particleProperties_,
            *this
        )
    )
{
    buildConstProps();
    buildCellOccupancy();

    // Initialise the collision selection remainder to a random value between 0
    // and 1.
    forAll(collisionSelectionRemainder_, i)
    {
676
        collisionSelectionRemainder_[i] = rndGen_.sample01<scalar>();
Henry's avatar
Henry committed
677
678
679
680
681
682
683
684
685
686
    }

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


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


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

template<class ParcelType>
901
Foam::DSMCCloud<ParcelType>::~DSMCCloud()
Henry's avatar
Henry committed
902
903
904
905
906
907
{}


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

template<class ParcelType>
908
void Foam::DSMCCloud<ParcelType>::evolve()
Henry's avatar
Henry committed
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
{
    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
924
    Cloud<ParcelType>::move(*this, td, mesh_.time().deltaTValue());
Henry's avatar
Henry committed
925
926
927
928
929
930
931
932
933
934
935
936
937

    // Update cell occupancy
    buildCellOccupancy();

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

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


template<class ParcelType>
938
void Foam::DSMCCloud<ParcelType>::info() const
Henry's avatar
Henry committed
939
{
940
941
    label nDSMCParticles = this->size();
    reduce(nDSMCParticles, sumOp<label>());
Henry's avatar
Henry committed
942

943
    scalar nMol = nDSMCParticles*nParticle_;
Henry's avatar
Henry committed
944
945
946
947
948
949
950
951
952
953
954
955

    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        = "
956
        << nDSMCParticles
Henry's avatar
Henry committed
957
958
        << endl;

959
    if (nDSMCParticles)
Henry's avatar
Henry committed
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
    {
        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>
981
Foam::vector Foam::DSMCCloud<ParcelType>::equipartitionLinearVelocity
Henry's avatar
Henry committed
982
983
984
985
986
987
988
(
    scalar temperature,
    scalar mass
)
{
    return
        sqrt(physicoChemical::k.value()*temperature/mass)
989
       *rndGen_.GaussNormal<vector>();
Henry's avatar
Henry committed
990
991
992
993
}


template<class ParcelType>
994
Foam::scalar Foam::DSMCCloud<ParcelType>::equipartitionInternalEnergy
Henry's avatar
Henry committed
995
996
(
    scalar temperature,
997
    direction iDof
Henry's avatar
Henry committed
998
999
)
{
1000
    if (iDof == 0)
Henry's avatar
Henry committed
1001
    {
1002
        return 0;
Henry's avatar
Henry committed
1003
    }
1004
    else if (iDof == 2)
Henry's avatar
Henry committed
1005
1006
    {
        // Special case for iDof = 2, i.e. diatomics;
1007
1008
1009
1010
1011
        return
        (
            -log(rndGen_.sample01<scalar>())
            *physicoChemical::k.value()*temperature
        );
Henry's avatar
Henry committed
1012
1013
1014
    }


1015
1016
1017
1018
1019
1020
1021
1022
1023
    const scalar a = 0.5*iDof - 1;
    scalar energyRatio = 0;
    scalar P = -1;

    do
    {
        energyRatio = 10*rndGen_.sample01<scalar>();
        P = pow((energyRatio/a), a)*exp(a - energyRatio);
    } while (P < rndGen_.sample01<scalar>());
Henry's avatar
Henry committed
1024

1025
    return energyRatio*physicoChemical::k.value()*temperature;
Henry's avatar
Henry committed
1026
1027
1028
1029
}


template<class ParcelType>
1030
void Foam::DSMCCloud<ParcelType>::dumpParticlePositions() const
Henry's avatar
Henry committed
1031
1032
1033
1034
1035
1036
1037
1038
{
    OFstream pObj
    (
        this->db().time().path()/"parcelPositions_"
      + this->name() + "_"
      + this->db().time().timeName() + ".obj"
    );

1039
    forAllConstIter(typename DSMCCloud<ParcelType>, *this, iter)
Henry's avatar
Henry committed
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
    {
        const ParcelType& p = iter();

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

    pObj.flush();
}


1053
1054
1055
template<class ParcelType>
void Foam::DSMCCloud<ParcelType>::autoMap(const mapPolyMesh& mapper)
{
1056
    Cloud<ParcelType>::autoMap(mapper);
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066

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

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


Henry's avatar
Henry committed
1067
// ************************************************************************* //