BUG: forces/forceCoeffs - fields to be owned by mesh. See #2511

This commit is contained in:
Andrew Heather 2022-06-20 10:35:49 +01:00
parent ebac6c9b94
commit 307e380f8f
4 changed files with 143 additions and 111 deletions

View File

@ -61,13 +61,69 @@ void Foam::functionObjects::forceCoeffs::initialise()
}
Foam::volVectorField& Foam::functionObjects::forceCoeffs::forceCoeff()
{
auto* coeffPtr =
mesh_.getObjectPtr<volVectorField>(scopedName("forceCoeff"));
if (!coeffPtr)
{
coeffPtr = new volVectorField
(
IOobject
(
scopedName("forceCoeff"),
time_.timeName(),
mesh_,
IOobject::NO_READ,
IOobject::NO_WRITE
),
mesh_,
dimensionedVector(dimless, Zero)
);
mesh_.objectRegistry::store(coeffPtr);
}
return *coeffPtr;
}
Foam::volVectorField& Foam::functionObjects::forceCoeffs::momentCoeff()
{
auto* coeffPtr =
mesh_.getObjectPtr<volVectorField>(scopedName("momentCoeff"));
if (!coeffPtr)
{
coeffPtr = new volVectorField
(
IOobject
(
scopedName("momentCoeff"),
time_.timeName(),
mesh_,
IOobject::NO_READ,
IOobject::NO_WRITE
),
mesh_,
dimensionedVector(dimless, Zero)
);
mesh_.objectRegistry::store(coeffPtr);
}
return *coeffPtr;
}
void Foam::functionObjects::forceCoeffs::reset()
{
Cf_.reset();
Cm_.reset();
forceCoeff_ == dimensionedVector(dimless, Zero);
momentCoeff_ == dimensionedVector(dimless, Zero);
forceCoeff() == dimensionedVector(dimless, Zero);
momentCoeff() == dimensionedVector(dimless, Zero);
}
@ -111,7 +167,7 @@ void Foam::functionObjects::forceCoeffs::calcForceCoeffs()
const auto& coordSys = coordSysPtr_();
// Calculate force coefficients
forceCoeff_ = forceScaling*force_;
forceCoeff() = forceScaling*force();
Cf_.reset
(
@ -135,7 +191,7 @@ void Foam::functionObjects::forceCoeffs::calcMomentCoeffs()
const auto& coordSys = coordSysPtr_();
// Calculate moment coefficients
momentCoeff_ = momentScaling*moment_;
momentCoeff() = momentScaling*moment();
Cm_.reset
(
@ -225,32 +281,6 @@ Foam::functionObjects::forceCoeffs::forceCoeffs
forces(name, runTime, dict, false),
Cf_(),
Cm_(),
forceCoeff_
(
IOobject
(
"forceCoeff", // scopedName() is not available at ctor level
mesh_.time().timeName(),
mesh_,
IOobject::NO_READ,
IOobject::NO_WRITE
),
mesh_,
dimensionedVector(dimless, Zero)
),
momentCoeff_
(
IOobject
(
"momentCoeff",
mesh_.time().timeName(),
mesh_,
IOobject::NO_READ,
IOobject::NO_WRITE
),
mesh_,
dimensionedVector(dimless, Zero)
),
coeffFilePtr_(),
magUInf_(Zero),
lRef_(Zero),
@ -342,9 +372,6 @@ bool Foam::functionObjects::forceCoeffs::read(const dictionary& dict)
}
}
forceCoeff_.rename(scopedName("forceCoeff"));
momentCoeff_.rename(scopedName("momentCoeff"));
Info<< endl;
return true;
@ -421,8 +448,8 @@ bool Foam::functionObjects::forceCoeffs::write()
if (writeFields_)
{
forceCoeff_.write();
momentCoeff_.write();
forceCoeff().write();
momentCoeff().write();
}
Log << endl;

View File

@ -431,12 +431,6 @@ private:
//- Moment components
forceComponents Cm_;
//- Force coefficient field
volVectorField forceCoeff_;
//- Moment coefficient field
volVectorField momentCoeff_;
//- Operand force and moment coefficients
HashTable<coeffDesc> coeffs_;
@ -470,6 +464,13 @@ protected:
//- Initialise containers and fields
void initialise();
//- Return access to the force coefficients field
volVectorField& forceCoeff();
//- Return access to the moment coefficients field
volVectorField& momentCoeff();
//- Reset containers and fields
void reset();

View File

@ -91,6 +91,60 @@ void Foam::functionObjects::forces::setCoordinateSystem
}
Foam::volVectorField& Foam::functionObjects::forces::force()
{
auto* forcePtr = mesh_.getObjectPtr<volVectorField>(scopedName("force"));
if (!forcePtr)
{
forcePtr = new volVectorField
(
IOobject
(
scopedName("force"),
time_.timeName(),
mesh_,
IOobject::NO_READ,
IOobject::NO_WRITE
),
mesh_,
dimensionedVector(dimForce, Zero)
);
mesh_.objectRegistry::store(forcePtr);
}
return *forcePtr;
}
Foam::volVectorField& Foam::functionObjects::forces::moment()
{
auto* momentPtr = mesh_.getObjectPtr<volVectorField>(scopedName("moment"));
if (!momentPtr)
{
momentPtr = new volVectorField
(
IOobject
(
scopedName("moment"),
time_.timeName(),
mesh_,
IOobject::NO_READ,
IOobject::NO_WRITE
),
mesh_,
dimensionedVector(dimForce*dimLength, Zero)
);
mesh_.objectRegistry::store(momentPtr);
}
return *momentPtr;
}
void Foam::functionObjects::forces::initialise()
{
if (initialised_)
@ -143,8 +197,10 @@ void Foam::functionObjects::forces::reset()
sumInternalForces_ = Zero;
sumInternalMoments_ = Zero;
force_ == dimensionedVector(force_.dimensions(), Zero);
moment_ == dimensionedVector(moment_.dimensions(), Zero);
auto& force = this->force();
auto& moment = this->moment();
force == dimensionedVector(force.dimensions(), Zero);
moment == dimensionedVector(moment.dimensions(), Zero);
}
@ -291,14 +347,14 @@ void Foam::functionObjects::forces::addToPatchFields
{
sumPatchForcesP_ += sum(fP);
sumPatchForcesV_ += sum(fV);
force_.boundaryFieldRef()[patchi] += fP + fV;
force().boundaryFieldRef()[patchi] += fP + fV;
const vectorField mP(Md^fP);
const vectorField mV(Md^fV);
sumPatchMomentsP_ += sum(mP);
sumPatchMomentsV_ += sum(mV);
moment_.boundaryFieldRef()[patchi] += mP + mV;
moment().boundaryFieldRef()[patchi] += mP + mV;
}
@ -309,16 +365,19 @@ void Foam::functionObjects::forces::addToInternalField
const vectorField& f
)
{
auto& force = this->force();
auto& moment = this->moment();
forAll(cellIDs, i)
{
const label celli = cellIDs[i];
sumInternalForces_ += f[i];
force_[celli] += f[i];
force[celli] += f[i];
const vector m(Md[i]^f[i]);
sumInternalMoments_ += m;
moment_[celli] = m;
moment[celli] = m;
}
}
@ -449,32 +508,6 @@ Foam::functionObjects::forces::forces
:
fvMeshFunctionObject(name, runTime, dict),
writeFile(mesh_, name),
force_
(
IOobject
(
"force", // scopedName() is not available at ctor level
time_.timeName(),
mesh_,
IOobject::NO_READ,
IOobject::NO_WRITE
),
mesh_,
dimensionedVector(dimForce, Zero)
),
moment_
(
IOobject
(
"moment",
time_.timeName(),
mesh_,
IOobject::NO_READ,
IOobject::NO_WRITE
),
mesh_,
dimensionedVector(dimForce*dimLength, Zero)
),
sumPatchForcesP_(Zero),
sumPatchForcesV_(Zero),
sumPatchMomentsP_(Zero),
@ -515,32 +548,6 @@ Foam::functionObjects::forces::forces
:
fvMeshFunctionObject(name, obr, dict),
writeFile(mesh_, name),
force_
(
IOobject
(
"force",
time_.timeName(),
mesh_,
IOobject::NO_READ,
IOobject::NO_WRITE
),
mesh_,
dimensionedVector(dimForce, Zero)
),
moment_
(
IOobject
(
"moment",
time_.timeName(),
mesh_,
IOobject::NO_READ,
IOobject::NO_WRITE
),
mesh_,
dimensionedVector(dimForce*dimLength, Zero)
),
sumPatchForcesP_(Zero),
sumPatchForcesV_(Zero),
sumPatchMomentsP_(Zero),
@ -645,8 +652,6 @@ bool Foam::functionObjects::forces::read(const dictionary& dict)
Info<< " Fields will be written" << endl;
}
force_.rename(scopedName("force"));
moment_.rename(scopedName("moment"));
return true;
}
@ -823,8 +828,8 @@ bool Foam::functionObjects::forces::write()
{
Log << " writing force and moment fields." << endl;
force_.write();
moment_.write();
force().write();
moment().write();
}
Log << endl;

View File

@ -173,7 +173,7 @@ SourceFiles
#include "fvMeshFunctionObject.H"
#include "writeFile.H"
#include "coordinateSystem.H"
#include "volFields.H"
#include "volFieldsFwd.H"
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
@ -198,12 +198,6 @@ protected:
// Fields
//- Forces
volVectorField force_;
//- Moments
volVectorField moment_;
//- Sum of patch pressure forces
vector sumPatchForcesP_;
@ -281,13 +275,18 @@ protected:
const word& e1Name = word::null
);
//- Return access to the force field
volVectorField& force();
//- Return access to the moment field
volVectorField& moment();
//- Initialise containers and fields
void initialise();
//- Reset containers and fields
void reset();
// Evaluation
//- Return the effective stress (viscous + turbulent)