functionObjects: Generating and storing fields on demand rather than on construction

Resolves bug report https://bugs.openfoam.org/view.php?id=3019
This commit is contained in:
Will Bainbridge
2018-08-03 15:51:41 +01:00
parent 1a0c91b3ba
commit 0f14683c11
11 changed files with 166 additions and 229 deletions

View File

@ -73,48 +73,33 @@ bool Foam::functionObjects::CourantNo::calc()
const surfaceScalarField& phi = const surfaceScalarField& phi =
lookupObject<surfaceScalarField>(fieldName_); lookupObject<surfaceScalarField>(fieldName_);
tmp<volScalarField::Internal> Coi tmp<volScalarField> tCo
( (
new volScalarField
(
IOobject
(
resultName_,
mesh_.time().timeName(),
mesh_
),
mesh_,
dimensionedScalar("0", dimless, 0.0),
zeroGradientFvPatchScalarField::typeName
)
);
tCo->ref() =
byRho byRho
( (
(0.5*mesh_.time().deltaT()) (0.5*mesh_.time().deltaT())
*fvc::surfaceSum(mag(phi))()() *fvc::surfaceSum(mag(phi))()()
/mesh_.V() /mesh_.V()
)
);
if (foundObject<volScalarField>(resultName_))
{
volScalarField& Co = lookupObjectRef<volScalarField>(resultName_);
Co.ref() = Coi();
Co.correctBoundaryConditions();
}
else
{
tmp<volScalarField> tCo
(
new volScalarField
(
IOobject
(
resultName_,
mesh_.time().timeName(),
mesh_,
IOobject::NO_READ,
IOobject::NO_WRITE
),
mesh_,
dimensionedScalar("0", dimless, 0.0),
zeroGradientFvPatchScalarField::typeName
)
); );
tCo.ref().ref() = Coi();
tCo.ref().correctBoundaryConditions();
mesh_.objectRegistry::store(tCo.ptr());
}
return true; tCo->correctBoundaryConditions();
return store(resultName_, tCo);
} }
else else
{ {

View File

@ -51,25 +51,6 @@ Foam::functionObjects::processorField::processorField
fvMeshFunctionObject(name, runTime, dict) fvMeshFunctionObject(name, runTime, dict)
{ {
read(dict); read(dict);
volScalarField* procFieldPtr
(
new volScalarField
(
IOobject
(
"processorID",
mesh_.time().timeName(),
mesh_,
IOobject::NO_READ,
IOobject::NO_WRITE
),
mesh_,
dimensionedScalar("0", dimless, 0.0)
)
);
mesh_.objectRegistry::store(procFieldPtr);
} }
@ -91,10 +72,24 @@ bool Foam::functionObjects::processorField::read(const dictionary& dict)
bool Foam::functionObjects::processorField::execute() bool Foam::functionObjects::processorField::execute()
{ {
mesh_.lookupObjectRef<volScalarField>("processorID") == word name("processorID");
dimensionedScalar("proci", dimless, Pstream::myProcNo());
return true; tmp<volScalarField> tprocField
(
new volScalarField
(
IOobject
(
name,
mesh_.time().timeName(),
mesh_
),
mesh_,
dimensionedScalar(name, dimless, Pstream::myProcNo())
)
);
return store(name, tprocField);
} }

View File

@ -71,25 +71,6 @@ Foam::functionObjects::turbulenceIntensity::turbulenceIntensity
logFiles(obr_, name), logFiles(obr_, name),
writeLocalObjects(obr_, log) writeLocalObjects(obr_, log)
{ {
volScalarField* turbulenceIntensityPtr
(
new volScalarField
(
IOobject
(
"I",
mesh_.time().timeName(),
mesh_,
IOobject::NO_READ,
IOobject::NO_WRITE
),
mesh_,
dimensionedScalar("0", dimless, 0.0)
)
);
mesh_.objectRegistry::store(turbulenceIntensityPtr);
read(dict); read(dict);
resetName(typeName); resetName(typeName);
resetLocalObjectName("I"); resetLocalObjectName("I");
@ -115,9 +96,6 @@ bool Foam::functionObjects::turbulenceIntensity::read(const dictionary& dict)
bool Foam::functionObjects::turbulenceIntensity::execute() bool Foam::functionObjects::turbulenceIntensity::execute()
{ {
volScalarField& turbulenceIntensity =
mesh_.lookupObjectRef<volScalarField>("I");
if (mesh_.foundObject<turbulenceModel>(turbulenceModel::propertiesName)) if (mesh_.foundObject<turbulenceModel>(turbulenceModel::propertiesName))
{ {
const turbulenceModel& turbModel = mesh_.lookupObject<turbulenceModel> const turbulenceModel& turbModel = mesh_.lookupObject<turbulenceModel>
@ -126,12 +104,19 @@ bool Foam::functionObjects::turbulenceIntensity::execute()
); );
volScalarField uPrime(sqrt((2.0/3.0)*turbModel.k())); volScalarField uPrime(sqrt((2.0/3.0)*turbModel.k()));
turbulenceIntensity =
uPrime word name("I");
/max
return
store
( (
max(uPrime, mag(turbModel.U())), name,
dimensionedScalar("small", dimVelocity, small) uPrime
/max
(
max(uPrime, mag(turbModel.U())),
dimensionedScalar("small", dimVelocity, small)
)
); );
} }
else else
@ -139,9 +124,9 @@ bool Foam::functionObjects::turbulenceIntensity::execute()
FatalErrorInFunction FatalErrorInFunction
<< "Unable to find turbulence model in the " << "Unable to find turbulence model in the "
<< "database" << exit(FatalError); << "database" << exit(FatalError);
}
return true; return false;
}
} }

View File

@ -58,15 +58,30 @@ void Foam::functionObjects::wallHeatFlux::writeFileHeader(const label i)
} }
void Foam::functionObjects::wallHeatFlux::calcHeatFlux Foam::tmp<Foam::volScalarField>
Foam::functionObjects::wallHeatFlux::calcWallHeatFlux
( (
const volScalarField& alpha, const volScalarField& alpha,
const volScalarField& he, const volScalarField& he
volScalarField& wallHeatFlux
) )
{ {
tmp<volScalarField> twallHeatFlux
(
new volScalarField
(
IOobject
(
type(),
mesh_.time().timeName(),
mesh_
),
mesh_,
dimensionedScalar("0", dimMass/pow3(dimTime), 0)
)
);
volScalarField::Boundary& wallHeatFluxBf = volScalarField::Boundary& wallHeatFluxBf =
wallHeatFlux.boundaryFieldRef(); twallHeatFlux.ref().boundaryFieldRef();
const volScalarField::Boundary& heBf = const volScalarField::Boundary& heBf =
he.boundaryField(); he.boundaryField();
@ -97,6 +112,8 @@ void Foam::functionObjects::wallHeatFlux::calcHeatFlux
} }
} }
} }
return twallHeatFlux;
} }
@ -114,25 +131,6 @@ Foam::functionObjects::wallHeatFlux::wallHeatFlux
writeLocalObjects(obr_, log), writeLocalObjects(obr_, log),
patchSet_() patchSet_()
{ {
volScalarField* wallHeatFluxPtr
(
new volScalarField
(
IOobject
(
type(),
mesh_.time().timeName(),
mesh_,
IOobject::NO_READ,
IOobject::NO_WRITE
),
mesh_,
dimensionedScalar("0", dimMass/pow3(dimTime), 0)
)
);
mesh_.objectRegistry::store(wallHeatFluxPtr);
read(dict); read(dict);
resetName(typeName); resetName(typeName);
resetLocalObjectName(typeName); resetLocalObjectName(typeName);
@ -205,7 +203,7 @@ bool Foam::functionObjects::wallHeatFlux::read(const dictionary& dict)
bool Foam::functionObjects::wallHeatFlux::execute() bool Foam::functionObjects::wallHeatFlux::execute()
{ {
volScalarField& wallHeatFlux = lookupObjectRef<volScalarField>(type()); word name(type());
if if
( (
@ -220,20 +218,17 @@ bool Foam::functionObjects::wallHeatFlux::execute()
( (
turbulenceModel::propertiesName turbulenceModel::propertiesName
); );
const volScalarField& alpha = turbModel.alphaEff();
const volScalarField& he = turbModel.transport().he();
calcHeatFlux return store(name, calcWallHeatFlux(alpha, he));
(
turbModel.alphaEff(),
turbModel.transport().he(),
wallHeatFlux
);
} }
else if (foundObject<solidThermo>(solidThermo::dictName)) else if (foundObject<solidThermo>(solidThermo::dictName))
{ {
const solidThermo& thermo = const solidThermo& thermo =
lookupObject<solidThermo>(solidThermo::dictName); lookupObject<solidThermo>(solidThermo::dictName);
calcHeatFlux(thermo.alpha(), thermo.he(), wallHeatFlux); return store(name, calcWallHeatFlux(thermo.alpha(), thermo.he()));
} }
else else
{ {

View File

@ -108,11 +108,10 @@ protected:
virtual void writeFileHeader(const label i); virtual void writeFileHeader(const label i);
//- Calculate the heat-flux //- Calculate the heat-flux
void calcHeatFlux tmp<volScalarField> calcWallHeatFlux
( (
const volScalarField& alpha, const volScalarField& alpha,
const volScalarField& he, const volScalarField& he
volScalarField& wallHeatFlux
); );

View File

@ -63,15 +63,35 @@ void Foam::functionObjects::wallHeatTransferCoeff::writeFileHeader
} }
void Foam::functionObjects::wallHeatTransferCoeff::calcHeatTransferCoeff Foam::tmp<Foam::volScalarField>
Foam::functionObjects::wallHeatTransferCoeff::calcHeatTransferCoeff
( (
const volScalarField& nu, const volScalarField& nu,
const volScalarField& nut, const volScalarField& nut
volScalarField& wallHeatTransferCoeff
) )
{ {
tmp<volScalarField> twallHeatTransferCoeff
(
new volScalarField
(
IOobject
(
type(),
mesh_.time().timeName(),
mesh_
),
mesh_,
dimensionedScalar
(
"0",
dimMass/pow3(dimTime)/(dimTemperature/dimLength),
0
)
)
);
volScalarField::Boundary& wallHeatTransferCoeffBf = volScalarField::Boundary& wallHeatTransferCoeffBf =
wallHeatTransferCoeff.boundaryFieldRef(); twallHeatTransferCoeff.ref().boundaryFieldRef();
const volScalarField::Boundary& nuBf = nu.boundaryField(); const volScalarField::Boundary& nuBf = nu.boundaryField();
const volScalarField::Boundary& nutBf = nut.boundaryField(); const volScalarField::Boundary& nutBf = nut.boundaryField();
@ -84,6 +104,8 @@ void Foam::functionObjects::wallHeatTransferCoeff::calcHeatTransferCoeff
rho_*Cp_*(nuBf[patchi]/Prl_ + nutBf[patchi]/Prt_); rho_*Cp_*(nuBf[patchi]/Prl_ + nutBf[patchi]/Prt_);
} }
} }
return twallHeatTransferCoeff;
} }
@ -101,30 +123,6 @@ Foam::functionObjects::wallHeatTransferCoeff::wallHeatTransferCoeff
writeLocalObjects(obr_, log), writeLocalObjects(obr_, log),
patchSet_() patchSet_()
{ {
volScalarField* wallHeatTransferCoeffPtr
(
new volScalarField
(
IOobject
(
type(),
mesh_.time().timeName(),
mesh_,
IOobject::NO_READ,
IOobject::NO_WRITE
),
mesh_,
dimensionedScalar
(
"0",
dimMass/pow3(dimTime)/(dimTemperature/dimLength),
0
)
)
);
mesh_.objectRegistry::store(wallHeatTransferCoeffPtr);
read(dict); read(dict);
resetName(typeName); resetName(typeName);
resetLocalObjectName(typeName); resetLocalObjectName(typeName);
@ -202,8 +200,7 @@ bool Foam::functionObjects::wallHeatTransferCoeff::read(const dictionary& dict)
bool Foam::functionObjects::wallHeatTransferCoeff::execute() bool Foam::functionObjects::wallHeatTransferCoeff::execute()
{ {
volScalarField& wallHeatTransferCoeff = word name(type());
lookupObjectRef<volScalarField>(type());
if if
( (
@ -219,21 +216,19 @@ bool Foam::functionObjects::wallHeatTransferCoeff::execute()
turbulenceModel::propertiesName turbulenceModel::propertiesName
); );
calcHeatTransferCoeff const volScalarField& nu = turbModel.nu();
( const volScalarField& nut = turbModel.nut();
turbModel.nu(),
turbModel.nut(), return store(name, calcHeatTransferCoeff(nu, nut));
wallHeatTransferCoeff
);
} }
else else
{ {
FatalErrorInFunction FatalErrorInFunction
<< "Unable to find incompressible turbulence model in the " << "Unable to find incompressible turbulence model in the "
<< "database" << exit(FatalError); << "database" << exit(FatalError);
}
return true; return false;
}
} }

View File

@ -130,11 +130,10 @@ protected:
virtual void writeFileHeader(const label i); virtual void writeFileHeader(const label i);
//- Calculate the heat transfer coefficient //- Calculate the heat transfer coefficient
void calcHeatTransferCoeff tmp<volScalarField> calcHeatTransferCoeff
( (
const volScalarField& nu, const volScalarField& nu,
const volScalarField& nut, const volScalarField& nut
volScalarField& wallHeatTransferCoeff
); );

View File

@ -57,25 +57,42 @@ void Foam::functionObjects::wallShearStress::writeFileHeader(const label i)
} }
void Foam::functionObjects::wallShearStress::calcShearStress Foam::tmp<Foam::volVectorField>
Foam::functionObjects::wallShearStress::calcShearStress
( (
const volSymmTensorField& Reff, const volSymmTensorField& Reff
volVectorField& shearStress
) )
{ {
shearStress.dimensions().reset(Reff.dimensions()); tmp<volVectorField> twallShearStress
(
new volVectorField
(
IOobject
(
type(),
mesh_.time().timeName(),
mesh_
),
mesh_,
dimensionedVector("0", Reff.dimensions(), Zero)
)
);
volVectorField::Boundary& wallShearStressBf =
twallShearStress.ref().boundaryFieldRef();
forAllConstIter(labelHashSet, patchSet_, iter) forAllConstIter(labelHashSet, patchSet_, iter)
{ {
label patchi = iter.key(); label patchi = iter.key();
vectorField& ssp = shearStress.boundaryFieldRef()[patchi];
const vectorField& Sfp = mesh_.Sf().boundaryField()[patchi]; const vectorField& Sfp = mesh_.Sf().boundaryField()[patchi];
const scalarField& magSfp = mesh_.magSf().boundaryField()[patchi]; const scalarField& magSfp = mesh_.magSf().boundaryField()[patchi];
const symmTensorField& Reffp = Reff.boundaryField()[patchi]; const symmTensorField& Reffp = Reff.boundaryField()[patchi];
ssp = (-Sfp/magSfp) & Reffp; wallShearStressBf[patchi] = (-Sfp/magSfp) & Reffp;
} }
return twallShearStress;
} }
@ -93,30 +110,6 @@ Foam::functionObjects::wallShearStress::wallShearStress
writeLocalObjects(obr_, log), writeLocalObjects(obr_, log),
patchSet_() patchSet_()
{ {
volVectorField* wallShearStressPtr
(
new volVectorField
(
IOobject
(
type(),
mesh_.time().timeName(),
mesh_,
IOobject::NO_READ,
IOobject::NO_WRITE
),
mesh_,
dimensionedVector
(
"0",
sqr(dimLength)/sqr(dimTime),
Zero
)
)
);
mesh_.objectRegistry::store(wallShearStressPtr);
read(dict); read(dict);
resetName(typeName); resetName(typeName);
resetLocalObjectName(typeName); resetLocalObjectName(typeName);
@ -192,9 +185,6 @@ bool Foam::functionObjects::wallShearStress::execute()
typedef compressible::turbulenceModel cmpModel; typedef compressible::turbulenceModel cmpModel;
typedef incompressible::turbulenceModel icoModel; typedef incompressible::turbulenceModel icoModel;
volVectorField& wallShearStress =
mesh_.lookupObjectRef<volVectorField>(type());
tmp<volSymmTensorField> Reff; tmp<volSymmTensorField> Reff;
if (mesh_.foundObject<cmpModel>(turbulenceModel::propertiesName)) if (mesh_.foundObject<cmpModel>(turbulenceModel::propertiesName))
{ {
@ -217,9 +207,9 @@ bool Foam::functionObjects::wallShearStress::execute()
<< "database" << exit(FatalError); << "database" << exit(FatalError);
} }
calcShearStress(Reff(), wallShearStress); word name(type());
return true; return store(name, calcShearStress(Reff));
} }

View File

@ -117,10 +117,9 @@ protected:
virtual void writeFileHeader(const label i); virtual void writeFileHeader(const label i);
//- Calculate the shear-stress //- Calculate the shear-stress
void calcShearStress tmp<volVectorField> calcShearStress
( (
const volSymmTensorField& Reff, const volSymmTensorField& Reff
volVectorField& shearStress
); );

View File

@ -62,12 +62,28 @@ void Foam::functionObjects::yPlus::writeFileHeader(const label i)
} }
void Foam::functionObjects::yPlus::calcYPlus Foam::tmp<Foam::volScalarField> Foam::functionObjects::yPlus::calcYPlus
( (
const turbulenceModel& turbModel, const turbulenceModel& turbModel
volScalarField& yPlus
) )
{ {
tmp<volScalarField> tyPlus
(
new volScalarField
(
IOobject
(
type(),
mesh_.time().timeName(),
mesh_
),
mesh_,
dimensionedScalar("0", dimless, 0.0)
)
);
volScalarField::Boundary& yPlusBf = tyPlus.ref().boundaryFieldRef();
volScalarField::Boundary d = nearWallDist(mesh_).y(); volScalarField::Boundary d = nearWallDist(mesh_).y();
const volScalarField::Boundary nutBf = const volScalarField::Boundary nutBf =
@ -81,8 +97,6 @@ void Foam::functionObjects::yPlus::calcYPlus
const fvPatchList& patches = mesh_.boundary(); const fvPatchList& patches = mesh_.boundary();
volScalarField::Boundary& yPlusBf = yPlus.boundaryFieldRef();
forAll(patches, patchi) forAll(patches, patchi)
{ {
const fvPatch& patch = patches[patchi]; const fvPatch& patch = patches[patchi];
@ -108,6 +122,8 @@ void Foam::functionObjects::yPlus::calcYPlus
)/nuBf[patchi]; )/nuBf[patchi];
} }
} }
return tyPlus;
} }
@ -124,25 +140,6 @@ Foam::functionObjects::yPlus::yPlus
logFiles(obr_, name), logFiles(obr_, name),
writeLocalObjects(obr_, log) writeLocalObjects(obr_, log)
{ {
volScalarField* yPlusPtr
(
new volScalarField
(
IOobject
(
type(),
mesh_.time().timeName(),
mesh_,
IOobject::NO_READ,
IOobject::NO_WRITE
),
mesh_,
dimensionedScalar("0", dimless, 0.0)
)
);
mesh_.objectRegistry::store(yPlusPtr);
read(dict); read(dict);
resetName(typeName); resetName(typeName);
resetLocalObjectName(typeName); resetLocalObjectName(typeName);
@ -168,9 +165,6 @@ bool Foam::functionObjects::yPlus::read(const dictionary& dict)
bool Foam::functionObjects::yPlus::execute() bool Foam::functionObjects::yPlus::execute()
{ {
volScalarField& yPlus =
mesh_.lookupObjectRef<volScalarField>(type());
if (mesh_.foundObject<turbulenceModel>(turbulenceModel::propertiesName)) if (mesh_.foundObject<turbulenceModel>(turbulenceModel::propertiesName))
{ {
const turbulenceModel& model = mesh_.lookupObject<turbulenceModel> const turbulenceModel& model = mesh_.lookupObject<turbulenceModel>
@ -178,7 +172,9 @@ bool Foam::functionObjects::yPlus::execute()
turbulenceModel::propertiesName turbulenceModel::propertiesName
); );
calcYPlus(model, yPlus); word name(type());
return store(name, calcYPlus(model));
} }
else else
{ {

View File

@ -95,10 +95,9 @@ class yPlus
virtual void writeFileHeader(const label i); virtual void writeFileHeader(const label i);
//- Calculate y+ //- Calculate y+
void calcYPlus tmp<volScalarField> calcYPlus
( (
const turbulenceModel& turbModel, const turbulenceModel& turbModel
volScalarField& yPlus
); );
//- Disallow default bitwise copy construct //- Disallow default bitwise copy construct