File size: 8,680 Bytes
5a600cf
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
/*---------------------------------------------------------------------------*\
  =========                 |
  \\      /  F ield         | OpenFOAM: The Open Source CFD Toolbox
   \\    /   O peration     | Website:  https://openfoam.org
    \\  /    A nd           | Copyright (C) 2025-2026 OpenFOAM Foundation
     \\/     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/>.

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

#include "pointInjection.H"
#include "addToRunTimeSelectionTable.H"
#include "LagrangianFields.H"

// * * * * * * * * * * * * * * Static Data Members * * * * * * * * * * * * * //

namespace Foam
{
namespace Lagrangian
{
    defineTypeNameAndDebug(pointInjection, 0);
    addToRunTimeSelectionTable(LagrangianModel, pointInjection, dictionary);
}
}


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

void Foam::Lagrangian::pointInjection::readCoeffs(const dictionary& modelDict)
{
    point_.reset
    (
        Function1<vector>::New
        (
            "point",
            mesh().time().userUnits(),
            dimensions::length,
            modelDict
        ).ptr()
    );

    numberRate_.reset
    (
        Function1<scalar>::New
        (
            "numberRate",
            mesh().time().userUnits(),
            dimensions::rate,
            modelDict
        ).ptr()
    );

    numberDeferred_ = 0;

    injectionLocation_ = injectionLocation::unset;
}


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

Foam::Lagrangian::pointInjection::pointInjection
(
    const word& name,
    const LagrangianMesh& mesh,
    const dictionary& modelDict,
    const dictionary& stateDict
)
:
    LagrangianInjection(name, mesh),
    point_(nullptr),
    numberRate_(nullptr),
    numberDeferred_(stateDict.lookupOrDefault<scalar>("numberDeferred", 0)),
    rndGen_("rndGen", stateDict, name, true),
    timeIndex_(-1),
    injectionLocation_(injectionLocation::unset),
    coordinates_(barycentric::uniform(NaN)),
    celli_(-1),
    facei_(-1),
    faceTrii_(-1)
{
    readCoeffs(modelDict);
}


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

void Foam::Lagrangian::pointInjection::correct()
{
    // If anything is moving then all coordinates have to be constructed on the
    // fly, so don't store anything
    if (mesh().poly().moving() || !point_->constant())
    {
        injectionLocation_ =
            injectionLocation::multiplePoints;
    }

    // If nothing is moving, and coordinates are not already stored, then
    // construct and store the coordinates for repeated usage
    if
    (
        injectionLocation_ == injectionLocation::unset
     || injectionLocation_ == injectionLocation::multiplePoints
    )
    {
        const scalar t1 = mesh().time().value();
        const scalar t0 = t1 - mesh().time().deltaT().value();

        // Evaluate the position
        const point p = point_->value(t0);

        // Locate within the mesh
        const LagrangianMesh::location l =
            mesh().locate(p, coordinates_, celli_, facei_, faceTrii_, 0);

        // Check for failure
        checkLocation(l, p);

        // Set the location flag
        injectionLocation_ =
            celli_ >= 0
          ? injectionLocation::fixedPointOnThisProcessor
          : injectionLocation::fixedPointOnAnotherProcessor;
    }
}


Foam::LagrangianSubMesh Foam::Lagrangian::pointInjection::modify
(
    LagrangianMesh& mesh,
    const LagrangianSubMesh&
) const
{
    const scalar t1 = mesh.time().value();
    const scalar t0 = t1 - mesh.time().deltaT().value();

    // Restart the generator if necessary and set the time index up to date
    rndGen_.start(timeIndex_ == time().timeIndex());
    timeIndex_ = time().timeIndex();

    // Calculate the number of particles to inject. Round down to get an
    // integer number. Store the excess to apply at a later time.
    const scalar number = numberRate_->integral(t0, t1) + numberDeferred_;
    const label numberInt = floor(number);
    numberDeferred_ = number - numberInt;

    // Inject at random times throughout the time-step
    scalarField fraction(rndGen_.scalar01(numberInt));

    // Create particles in the Lagrangian mesh
    switch (injectionLocation_)
    {
        case injectionLocation::unset:
            FatalErrorInFunction
                << "Injection location has not been set"
                << exit(FatalError);
            return LagrangianSubMesh(mesh, LagrangianGroup::none, -1, -1);

        case injectionLocation::fixedPointOnThisProcessor:
        {
            // Use stored coordinates to inject the particles
            return
                mesh.inject
                (
                    *this,
                    barycentricField(numberInt, coordinates_),
                    labelField(numberInt, celli_),
                    labelField(numberInt, facei_),
                    labelField(numberInt, faceTrii_),
                    LagrangianMesh::fractionName,
                    fraction
                );
        }

        case injectionLocation::fixedPointOnAnotherProcessor:
        {
            // Inject nothing. We need this because source conditions evaluated
            // on the process containing the point may communicate; e.g., to
            // sum the flow rate across the processes.
            return
                mesh.inject
                (
                    *this,
                    barycentricField(),
                    labelField(),
                    labelField(),
                    labelField(),
                    LagrangianMesh::fractionName,
                    scalarField()
                );
        }

        case injectionLocation::multiplePoints:
        {
            // Evaluate the variable positions
            const pointField positions(point_->value(t0 + fraction*(t1 - t0)));

            // Locate within the moving mesh
            barycentricField coordinates(numberInt);
            labelField celli(numberInt), facei(numberInt), faceTrii(numberInt);
            const List<LagrangianMesh::location> locations =
                mesh.locate
                (
                    positions,
                    coordinates,
                    celli,
                    facei,
                    faceTrii,
                    fraction
                );

            // Check for any failures
            checkLocation(locations, positions);

            // Remove particles not on this process
            filter(coordinates, celli, facei, faceTrii, fraction);

            // Inject particles
            return
                mesh.inject
                (
                    *this,
                    coordinates,
                    celli,
                    facei,
                    faceTrii,
                    LagrangianMesh::fractionName,
                    fraction
                );
        }
    }

    return LagrangianSubMesh(mesh, LagrangianGroup::none, -1, -1);
}


void Foam::Lagrangian::pointInjection::topoChange(const polyTopoChangeMap&)
{
    injectionLocation_ = injectionLocation::unset;
}


void Foam::Lagrangian::pointInjection::mapMesh(const polyMeshMap&)
{
    injectionLocation_ = injectionLocation::unset;
}


void Foam::Lagrangian::pointInjection::distribute(const polyDistributionMap&)
{
    injectionLocation_ = injectionLocation::unset;
}


bool Foam::Lagrangian::pointInjection::read(const dictionary& modelDict)
{
    if (LagrangianInjection::read(modelDict))
    {
        readCoeffs(modelDict);
        return true;
    }
    else
    {
        return false;
    }
}


void Foam::Lagrangian::pointInjection::writeState(Ostream& os) const
{
    LagrangianInjection::writeState(os);

    writeEntry(os, "numberDeferred", numberDeferred_);
    writeEntry(os, "rndGen", rndGen_);
}


// ************************************************************************* //