/*---------------------------------------------------------------------------*\
========= |
\\ / 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 .
\*---------------------------------------------------------------------------*/
#include "LagrangianSubMesh.H"
#include "patchInjection.H"
#include "LagrangianFields.H"
#include "cloud.H"
#include "cloudFunctionObjectUList.H"
#include "tetIndices.H"
#include "addToRunTimeSelectionTable.H"
// * * * * * * * * * * * * * * Static Data Members * * * * * * * * * * * * * //
namespace Foam
{
namespace Lagrangian
{
defineTypeNameAndDebug(patchInjection, 0);
addToRunTimeSelectionTable(LagrangianModel, patchInjection, dictionary);
}
}
// * * * * * * * * * * * * * Private Member Functions * * * * * * * * * * * //
void Foam::Lagrangian::patchInjection::readCoeffs(const dictionary& modelDict)
{
patchName_ = modelDict.lookup("patch");
numberRate_.reset
(
Function1::New
(
"numberRate",
mesh().time().userUnits(),
dimensions::rate,
modelDict
).ptr()
);
numberDeferred_ = 0;
}
void Foam::Lagrangian::patchInjection::calcSumAreas()
{
const polyPatch& patch = mesh().poly().boundary()[patchName_];
// Create a point field mid-way through the fraction. We distribute
// particles uniformly through the time-step, so using the mid-time-step
// geometry ensures that the particles get distributed evenly even in the
// presence of motion.
const tmp tPoints
(
mesh().poly().moving()
? mesh().poly().oldPoints()/2 + mesh().poly().points()/2
: tmp(mesh().poly().points())
);
const pointField& points = tPoints();
// Count the number of triangles in each face
labelList patchFaceNTris(patch.size(), label(0));
forAll(patch, patchFacei)
{
patchFaceNTris[patchFacei] = patch[patchFacei].nTriangles();
}
// Construct cumulative sums of the areas in the patch faces and patch
// face-triangles
patchFaceSumArea_.resize(patch.size());
patchFaceSumArea_ = 0;
patchFaceTriSumArea_.resize(patchFaceNTris);
forAll(patch, patchFacei)
{
const label facei = patch.start() + patchFacei;
const face& f = patch[patchFacei];
for (label fTrii = 0; fTrii < f.nTriangles(); ++ fTrii)
{
patchFaceSumArea_[patchFacei] +=
tetIndices(mesh().poly().faceOwner()[facei], facei, fTrii + 1)
.faceTri(mesh().poly(), points)
.mag();
patchFaceTriSumArea_[patchFacei][fTrii] =
patchFaceSumArea_[patchFacei];
}
}
for (label patchFacei = 1; patchFacei < patch.size(); ++ patchFacei)
{
patchFaceSumArea_[patchFacei] += patchFaceSumArea_[patchFacei - 1];
}
// Construct a cumulative sum of the areas across the processes
procSumArea_ = scalar(-vGreat);
procSumArea_[Pstream::myProcNo()] =
patch.size() ? patchFaceSumArea_.last() : scalar(0);
Pstream::listCombineGather(procSumArea_, maxEqOp());
Pstream::listCombineScatter(procSumArea_);
for (label proci = 1; proci < Pstream::nProcs(); proci ++)
{
procSumArea_[proci] += procSumArea_[proci - 1];
}
}
// * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
Foam::Lagrangian::patchInjection::patchInjection
(
const word& name,
const LagrangianMesh& mesh,
const dictionary& modelDict,
const dictionary& stateDict
)
:
LagrangianInjection(name, mesh),
cloudLagrangianModel(static_cast(*this)),
patchName_(word::null),
numberRate_(nullptr),
numberDeferred_(stateDict.lookupOrDefault("numberDeferred", 0)),
globalRndGen_("globalRndGen", stateDict, name, true),
localRndGen_("localRndGen", stateDict, name, false),
timeIndex_(-1),
procSumArea_(Pstream::nProcs()),
patchFaceSumArea_(),
patchFaceTriSumArea_()
{
readCoeffs(modelDict);
calcSumAreas();
}
// * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * * //
Foam::LagrangianSubMesh Foam::Lagrangian::patchInjection::modify
(
LagrangianMesh& mesh,
const LagrangianSubMesh&
) const
{
const scalar t1 = mesh.time().value();
const scalar t0 = t1 - mesh.time().deltaT().value();
// Restart the generators if necessary and set the time index up to date
localRndGen_.start(timeIndex_ == time().timeIndex());
globalRndGen_.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
const scalarField fraction(globalRndGen_.scalar01(numberInt));
// Binary search for a given volume within a supplied cumulative volume sum.
// Once the interval has been found, subtract the lower value from that
// volume so that a subsequent binary search can be done on a sub-set of
// volumes. Note that the supplied cumulative volume sum does not include
// the leading zero.
auto findVolume = []
(
const scalarUList& volumes,
scalar& volume
)
{
label i0 = -1, i1 = volumes.size() - 1;
while (i0 + 1 != i1)
{
const label i = (i0 + i1)/2;
(volume < volumes[i] ? i1 : i0) = i;
}
if (i0 != -1)
{
volume -= volumes[i0];
}
return i1;
};
// Get the mesh index of the first face in the patch
const polyPatch& patch = mesh.poly().boundary()[patchName_];
const label facei0 = patch.start();
// Initialise storage for the injection geometry and topology. This is
// dynamic as we don't know how much will end up on each processor yet.
DynamicList injectCoordinates(numberInt/Pstream::nProcs());
DynamicList