STYLE: improve robustness of wall interactions code (issue #737)

This commit is contained in:
Mark Olesen
2018-02-20 12:19:38 +01:00
parent ad871a16fc
commit 63edb6024b
14 changed files with 229 additions and 209 deletions

View File

@ -37,21 +37,23 @@ Foam::LocalInteraction<CloudType>::LocalInteraction
PatchInteractionModel<CloudType>(dict, cloud, typeName),
patchData_(cloud.mesh(), this->coeffDict()),
nEscape_(patchData_.size()),
massEscape_(patchData_.size()),
nStick_(patchData_.size()),
massStick_(patchData_.size()),
massEscape_(nEscape_.size()),
nStick_(nEscape_.size()),
massStick_(nEscape_.size()),
writeFields_(this->coeffDict().lookupOrDefault("writeFields", false)),
outputByInjectorId_(this->coeffDict().lookupOrDefault("outputByInjectorId", false)),
injIdToIndex_(cloud.injectors().size()),
injIdToIndex_(),
massEscapePtr_(nullptr),
massStickPtr_(nullptr)
{
const bool outputByInjectorId
= this->coeffDict().lookupOrDefault("outputByInjectorId", false);
if (writeFields_)
{
word massEscapeName(this->owner().name() + ":massEscape");
word massStickName(this->owner().name() + ":massStick");
Info<< " Interaction fields will be written to " << massEscapeName
<< " and " << massStickName << endl;
Info<< " Interaction fields will be written to "
<< this->owner().name() << ":massEscape"
<< " and "
<< this->owner().name() << ":massStick" << endl;
(void)massEscape();
(void)massStick();
@ -61,7 +63,23 @@ Foam::LocalInteraction<CloudType>::LocalInteraction
Info<< " Interaction fields will not be written" << endl;
}
// check that interactions are valid/specified
// Determine the number of injectors and the injector mapping
label nInjectors = 0;
if (outputByInjectorId)
{
for (const auto& inj : cloud.injectors())
{
injIdToIndex_.insert(inj.injectorID(), nInjectors++);
}
}
// The normal case, and safety if injector mapping was somehow null.
if (!nInjectors)
{
nInjectors = 1;
}
// Check that interactions are valid/specified
forAll(patchData_, patchi)
{
const word& interactionTypeName =
@ -80,20 +98,10 @@ Foam::LocalInteraction<CloudType>::LocalInteraction
<< nl << exit(FatalError);
}
label nInjectors(1);
if (outputByInjectorId_)
{
nInjectors = cloud.injectors().size();
for (label i=0; i<nInjectors; i++)
{
injIdToIndex_.insert(cloud.injectors()[i].injectorID(), i);
}
}
nEscape_[patchi].setSize(nInjectors, 0);
massEscape_[patchi].setSize(nInjectors, 0.0);
nStick_[patchi].setSize(nInjectors, 0);
massStick_[patchi].setSize(nInjectors, 0.0);
nEscape_[patchi].setSize(nInjectors, Zero);
massEscape_[patchi].setSize(nInjectors, Zero);
nStick_[patchi].setSize(nInjectors, Zero);
massStick_[patchi].setSize(nInjectors, Zero);
}
}
@ -111,20 +119,12 @@ Foam::LocalInteraction<CloudType>::LocalInteraction
nStick_(pim.nStick_),
massStick_(pim.massStick_),
writeFields_(pim.writeFields_),
outputByInjectorId_(pim.outputByInjectorId_),
injIdToIndex_(pim.injIdToIndex_),
massEscapePtr_(nullptr),
massStickPtr_(nullptr)
{}
// * * * * * * * * * * * * * * * * Destructor * * * * * * * * * * * * * * * //
template<class CloudType>
Foam::LocalInteraction<CloudType>::~LocalInteraction()
{}
// * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * * //
template<class CloudType>
@ -193,12 +193,20 @@ bool Foam::LocalInteraction<CloudType>::correct
bool& keepParticle
)
{
label patchi = patchData_.applyToPatch(pp.index());
const label patchi = patchData_.applyToPatch(pp.index());
if (patchi >= 0)
{
vector& U = p.U();
// Location for storing the stats.
const label idx =
(
injIdToIndex_.size()
? injIdToIndex_.lookup(p.typeId(), 0)
: 0
);
typename PatchInteractionModel<CloudType>::interactionType it =
this->wordToInteractionType
(
@ -213,50 +221,38 @@ bool Foam::LocalInteraction<CloudType>::correct
}
case PatchInteractionModel<CloudType>::itEscape:
{
scalar dm = p.mass()*p.nParticle();
keepParticle = false;
p.active(false);
U = Zero;
if (outputByInjectorId_)
{
nEscape_[patchi][injIdToIndex_[p.typeId()]]++;
massEscape_[patchi][injIdToIndex_[p.typeId()]] += dm;
}
else
{
nEscape_[patchi][0]++;
massEscape_[patchi][0] += dm;
}
const scalar dm = p.mass()*p.nParticle();
nEscape_[patchi][idx]++;
massEscape_[patchi][idx] += dm;
if (writeFields_)
{
label pI = pp.index();
label fI = pp.whichFace(p.face());
const label pI = pp.index();
const label fI = pp.whichFace(p.face());
massEscape().boundaryFieldRef()[pI][fI] += dm;
}
break;
}
case PatchInteractionModel<CloudType>::itStick:
{
scalar dm = p.mass()*p.nParticle();
keepParticle = true;
p.active(false);
U = Zero;
if (outputByInjectorId_)
{
nStick_[patchi][injIdToIndex_[p.typeId()]]++;
massStick_[patchi][injIdToIndex_[p.typeId()]] += dm;
}
else
{
nStick_[patchi][0]++;
massStick_[patchi][0] += dm;
}
const scalar dm = p.mass()*p.nParticle();
nStick_[patchi][idx]++;
massStick_[patchi][idx] += dm;
if (writeFields_)
{
label pI = pp.index();
label fI = pp.whichFace(p.face());
const label pI = pp.index();
const label fI = pp.whichFace(p.face());
massStick().boundaryFieldRef()[pI][fI] += dm;
}
break;
@ -355,21 +351,28 @@ void Foam::LocalInteraction<CloudType>::info(Ostream& os)
mps[i] = mps[i] + mps0[i];
}
if (outputByInjectorId_)
if (injIdToIndex_.size())
{
// Since injIdToIndex_ is a one-to-one mapping (starting as zero),
// can simply invert it.
labelList indexToInjector(injIdToIndex_.size());
forAllConstIters(injIdToIndex_, iter)
{
indexToInjector[iter.object()] = iter.key();
}
forAll(patchData_, i)
{
forAll (mpe[i], injId)
forAll(mpe[i], idx)
{
os << " Parcel fate: patch " << patchData_[i].patchName()
<< " (number, mass)" << nl
<< " - escape (injector " << injIdToIndex_.toc()[injId]
<< " ) = " << npe[i][injId]
<< ", " << mpe[i][injId] << nl
<< " - stick (injector " << injIdToIndex_.toc()[injId]
<< " ) = " << nps[i][injId]
<< ", " << mps[i][injId] << nl;
<< " - escape (injector " << indexToInjector[idx]
<< " ) = " << npe[i][idx]
<< ", " << mpe[i][idx] << nl
<< " - stick (injector " << indexToInjector[idx]
<< " ) = " << nps[i][idx]
<< ", " << mps[i][idx] << nl;
}
}
}
@ -379,25 +382,23 @@ void Foam::LocalInteraction<CloudType>::info(Ostream& os)
{
os << " Parcel fate: patch " << patchData_[i].patchName()
<< " (number, mass)" << nl
<< " - escape = " << npe[i][0]
<< ", " << mpe[i][0] << nl
<< " - stick = " << nps[i][0]
<< ", " << mps[i][0] << nl;
<< " - escape = "
<< npe[i][0] << ", " << mpe[i][0] << nl
<< " - stick = "
<< nps[i][0] << ", " << mps[i][0] << nl;
}
}
if (this->writeTime())
{
this->setModelProperty("nEscape", npe);
nEscape_ = Zero;
this->setModelProperty("massEscape", mpe);
massEscape_ = Zero;
this->setModelProperty("nStick", nps);
nStick_ = Zero;
this->setModelProperty("massStick", mps);
nEscape_ = Zero;
massEscape_ = Zero;
nStick_ = Zero;
massStick_ = Zero;
}
}

View File

@ -43,6 +43,7 @@ Description
namespace Foam
{
/*---------------------------------------------------------------------------*\
Class LocalInteraction Declaration
\*---------------------------------------------------------------------------*/
@ -57,29 +58,25 @@ class LocalInteraction
//- List of participating patches
const patchInteractionDataList patchData_;
// Bookkeeping for particle fates
// Counters for particle fates
//- Number of parcels escaped
List<List<label>> nEscape_;
//- Number of parcels escaped
List<List<label>> nEscape_;
//- Mass of parcels escaped
List<List<scalar>> massEscape_;
//- Mass of parcels escaped
List<List<scalar>> massEscape_;
//- Number of parcels stuck to patches
List<List<label>> nStick_;
//- Mass of parcels stuck to patches
List<List<scalar>> massStick_;
//- Number of parcels stuck to patches
List<List<label>> nStick_;
//- Mass of parcels stuck to patches
List<List<scalar>> massStick_;
//- Flag to output data as fields
Switch writeFields_;
bool writeFields_;
//- Flag to output escaped/mass particles sorted by injectorID
Switch outputByInjectorId_;
//- InjectorId to index map
//- InjectorId to index map, when outputting escaped/stick/...
// particles sorted by injectorID
Map<label> injIdToIndex_;
//- Mass escape field
@ -115,7 +112,7 @@ public:
//- Destructor
virtual ~LocalInteraction();
virtual ~LocalInteraction() = default;
// Member Functions
@ -136,10 +133,8 @@ public:
);
// I-O
//- Write patch interaction info to stream
virtual void info(Ostream& os);
//- Write patch interaction info to stream
virtual void info(Ostream& os);
};

View File

@ -108,13 +108,6 @@ Foam::MultiInteraction<CloudType>::MultiInteraction
{}
// * * * * * * * * * * * * * * * * Destructor * * * * * * * * * * * * * * * //
template<class CloudType>
Foam::MultiInteraction<CloudType>::~MultiInteraction()
{}
// * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * * //
template<class CloudType>

View File

@ -130,7 +130,7 @@ public:
//- Destructor
virtual ~MultiInteraction();
virtual ~MultiInteraction() = default;
// Member Functions

View File

@ -48,13 +48,6 @@ Foam::NoInteraction<CloudType>::NoInteraction
{}
// * * * * * * * * * * * * * * * * Destructor * * * * * * * * * * * * * * * //
template<class CloudType>
Foam::NoInteraction<CloudType>::~NoInteraction()
{}
// * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
template<class CloudType>

View File

@ -76,7 +76,7 @@ public:
//- Destructor
virtual ~NoInteraction();
virtual ~NoInteraction() = default;
// Member Functions

View File

@ -151,13 +151,6 @@ Foam::PatchInteractionModel<CloudType>::PatchInteractionModel
{}
// * * * * * * * * * * * * * * * * Destructor * * * * * * * * * * * * * * * //
template<class CloudType>
Foam::PatchInteractionModel<CloudType>::~PatchInteractionModel()
{}
// * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
template<class CloudType>

View File

@ -136,7 +136,7 @@ public:
//- Destructor
virtual ~PatchInteractionModel();
virtual ~PatchInteractionModel() = default;
//- Selector
@ -174,10 +174,8 @@ public:
void addToEscapedParcels(const scalar mass);
// I-O
//- Write patch interaction info to stream
virtual void info(Ostream& os);
//- Write patch interaction info to stream
virtual void info(Ostream& os);
};

View File

@ -47,13 +47,6 @@ Foam::Rebound<CloudType>::Rebound(const Rebound<CloudType>& pim)
{}
// * * * * * * * * * * * * * * * * Destructor * * * * * * * * * * * * * * * //
template<class CloudType>
Foam::Rebound<CloudType>::~Rebound()
{}
// * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
template<class CloudType>

View File

@ -82,10 +82,11 @@ public:
//- Destructor
virtual ~Rebound();
virtual ~Rebound() = default;
// Member Functions
//- Apply velocity correction
// Returns true if particle remains in same cell
virtual bool correct

View File

@ -46,12 +46,11 @@ Foam::StandardWallInteraction<CloudType>::StandardWallInteraction
massEscape_(nEscape_.size()),
nStick_(nEscape_.size()),
massStick_(nEscape_.size()),
outputByInjectorId_
(
this->coeffDict().lookupOrDefault("outputByInjectorId", false)
),
injIdToIndex_(cloud.injectors().size())
injIdToIndex_()
{
const bool outputByInjectorId
= this->coeffDict().lookupOrDefault("outputByInjectorId", false);
switch (interactionType_)
{
case PatchInteractionModel<CloudType>::itOther:
@ -76,22 +75,28 @@ Foam::StandardWallInteraction<CloudType>::StandardWallInteraction
{}
}
// Determine the number of injectors and the injector mapping
label nInjectors = 0;
if (outputByInjectorId)
{
for (const auto& inj : cloud.injectors())
{
injIdToIndex_.insert(inj.injectorID(), nInjectors++);
}
}
// The normal case, and safety if injector mapping was somehow null.
if (injIdToIndex_.empty())
{
nInjectors = 1;
}
forAll(nEscape_, patchi)
{
label nInjectors(1);
if (outputByInjectorId_)
{
nInjectors = cloud.injectors().size();
for (label i=0; i<nInjectors; i++)
{
injIdToIndex_.insert(cloud.injectors()[i].injectorID(), i);
}
}
nEscape_[patchi].setSize(nInjectors, 0);
massEscape_[patchi].setSize(nInjectors, 0.0);
nStick_[patchi].setSize(nInjectors, 0);
massStick_[patchi].setSize(nInjectors, 0.0);
nEscape_[patchi].setSize(nInjectors, Zero);
massEscape_[patchi].setSize(nInjectors, Zero);
nStick_[patchi].setSize(nInjectors, Zero);
massStick_[patchi].setSize(nInjectors, Zero);
}
}
@ -111,7 +116,6 @@ Foam::StandardWallInteraction<CloudType>::StandardWallInteraction
massEscape_(pim.massEscape_),
nStick_(pim.nStick_),
massStick_(pim.massStick_),
outputByInjectorId_(pim.outputByInjectorId_),
injIdToIndex_(pim.injIdToIndex_)
{}
@ -130,6 +134,14 @@ bool Foam::StandardWallInteraction<CloudType>::correct
if (isA<wallPolyPatch>(pp))
{
// Location for storing the stats.
const label idx =
(
injIdToIndex_.size()
? injIdToIndex_.lookup(p.typeId(), 0)
: 0
);
switch (interactionType_)
{
case PatchInteractionModel<CloudType>::itNone:
@ -141,17 +153,11 @@ bool Foam::StandardWallInteraction<CloudType>::correct
keepParticle = false;
p.active(false);
U = Zero;
const scalar dm = p.nParticle()*p.mass();
if (outputByInjectorId_)
{
nEscape_[pp.index()][injIdToIndex_[p.typeId()]]++;
massEscape_[pp.index()][injIdToIndex_[p.typeId()]] += dm;
}
else
{
nEscape_[pp.index()][0]++;
massEscape_[pp.index()][0] += dm;
}
nEscape_[pp.index()][idx]++;
massEscape_[pp.index()][idx] += dm;
break;
}
case PatchInteractionModel<CloudType>::itStick:
@ -159,17 +165,11 @@ bool Foam::StandardWallInteraction<CloudType>::correct
keepParticle = true;
p.active(false);
U = Zero;
const scalar dm = p.nParticle()*p.mass();
if (outputByInjectorId_)
{
nStick_[pp.index()][injIdToIndex_[p.typeId()]]++;
massStick_[pp.index()][injIdToIndex_[p.typeId()]] += dm;
}
else
{
nStick_[pp.index()][0]++;
massStick_[pp.index()][0] += dm;
}
nStick_[pp.index()][idx]++;
massStick_[pp.index()][idx] += dm;
break;
}
case PatchInteractionModel<CloudType>::itRebound:
@ -234,7 +234,7 @@ void Foam::StandardWallInteraction<CloudType>::info(Ostream& os)
scalarListList mps0(massStick_);
this->getModelProperty("massStick", mps0);
// accumulate current data
// Accumulate current data
labelListList npe(nEscape_);
forAll(npe, i)
@ -264,20 +264,28 @@ void Foam::StandardWallInteraction<CloudType>::info(Ostream& os)
mps[i] = mps[i] + mps0[i];
}
if (outputByInjectorId_)
if (injIdToIndex_.size())
{
// Since injIdToIndex_ is a one-to-one mapping (starting as zero),
// can simply invert it.
labelList indexToInjector(injIdToIndex_.size());
forAllConstIters(injIdToIndex_, iter)
{
indexToInjector[iter.object()] = iter.key();
}
forAll(npe, i)
{
forAll (mpe[i], injId)
forAll(mpe[i], idx)
{
os << " Parcel fate: patch " << mesh_.boundary()[i].name()
<< " (number, mass)" << nl
<< " - escape (injector " << injIdToIndex_.toc()[injId]
<< " ) = " << npe[i][injId]
<< ", " << mpe[i][injId] << nl
<< " - stick (injector " << injIdToIndex_.toc()[injId]
<< " ) = " << nps[i][injId]
<< ", " << mps[i][injId] << nl;
<< " - escape (injector " << indexToInjector[idx]
<< ") = " << npe[i][idx]
<< ", " << mpe[i][idx] << nl
<< " - stick (injector " << indexToInjector[idx]
<< ") = " << nps[i][idx]
<< ", " << mps[i][idx] << nl;
}
}
}
@ -285,7 +293,6 @@ void Foam::StandardWallInteraction<CloudType>::info(Ostream& os)
{
forAll(npe, i)
{
os << " Parcel fate: patch (number, mass) "
<< mesh_.boundary()[i].name() << nl
<< " - escape = "
@ -299,15 +306,15 @@ void Foam::StandardWallInteraction<CloudType>::info(Ostream& os)
if (this->writeTime())
{
this->setModelProperty("nEscape", npe);
nEscape_ = Zero;
this->setModelProperty("massEscape", mpe);
massEscape_ = Zero;
this->setModelProperty("nStick", nps);
nStick_ = Zero;
this->setModelProperty("massStick", mps);
nEscape_ = Zero;
massEscape_ = Zero;
nStick_ = Zero;
massStick_ = Zero;
}
}

View File

@ -82,26 +82,26 @@ protected:
//- Restitution coefficient
scalar mu_;
// Bookkeeping for particle fates
// Counters for particle fates
//- Number of parcels escaped
List<List<label>> nEscape_;
//- Number of parcels escaped
List<List<label>> nEscape_;
//- Mass of parcels escaped
List<List<scalar>> massEscape_;
//- Mass of parcels escaped
List<List<scalar>> massEscape_;
//- Number of parcels stuck to patches
List<List<label>> nStick_;
//- Number of parcels stuck to patches
List<List<label>> nStick_;
//- Mass of parcels stuck to patches
List<List<scalar>> massStick_;
//- Mass of parcels stuck to patches
List<List<scalar>> massStick_;
//- Flag to output escaped/mass particles sorted by injectorID
bool outputByInjectorId_;
//- Flag to output escaped/mass particles sorted by injectorID
bool outputByInjectorId_;
//- InjectorId to index map
Map<label> injIdToIndex_;
//- InjectorId to index map, when outputting escaped/stick/...
// particles sorted by injectorID
Map<label> injIdToIndex_;
public:
@ -144,10 +144,8 @@ public:
);
// I-O
//- Write patch interaction info to stream
virtual void info(Ostream& os);
//- Write patch interaction info to stream
virtual void info(Ostream& os);
};

View File

@ -0,0 +1,18 @@
#!/bin/sh
cd ${0%/*} || exit 1 # Run from this directory
. $WM_PROJECT_DIR/bin/tools/RunFunctions # Tutorial run functions
restore0Dir
runApplication blockMesh
runApplication potentialFoam
# Remove incompatible (volumetric) flux field
\rm -f 0/phi 2>/dev/null
runApplication decomposePar
runParallel $(getApplication)
#------------------------------------------------------------------------------

View File

@ -0,0 +1,30 @@
/*--------------------------------*- C++ -*----------------------------------*\
| ========= | |
| \\ / F ield | OpenFOAM: The Open Source CFD Toolbox |
| \\ / O peration | Version: plus |
| \\ / A nd | Web: www.OpenFOAM.com |
| \\/ M anipulation | |
\*---------------------------------------------------------------------------*/
FoamFile
{
version 2.0;
format ascii;
class dictionary;
object decomposeParDict;
}
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
numberOfSubdomains 4;
method scotch;
coeffs
{
n (2 2 1);
}
distributed no;
roots ( );
// ************************************************************************* //