Commit ec24badb authored by Andrew Heather's avatar Andrew Heather
Browse files

ENH: forces/forceCoeffs FOs updated follwing changes to functionObjectFile

parent 4f4fc3fa
......@@ -3,7 +3,7 @@
\\ / F ield | OpenFOAM: The Open Source CFD Toolbox
\\ / O peration |
\\ / A nd | Copyright (C) 2011-2015 OpenFOAM Foundation
\\/ M anipulation |
\\/ M anipulation | Copyright (C) 2015 OpenCFD Ltd
-------------------------------------------------------------------------------
License
This file is part of OpenFOAM.
......@@ -28,6 +28,9 @@ License
#include "Time.H"
#include "Pstream.H"
#include "IOmanip.H"
#include "fvMesh.H"
#include "dimensionedTypes.H"
#include "volFields.H"
// * * * * * * * * * * * * * * Static Data Members * * * * * * * * * * * * * //
......@@ -39,81 +42,157 @@ namespace Foam
// * * * * * * * * * * * * Protected Member Functions * * * * * * * * * * * //
void Foam::forceCoeffs::writeFileHeader(const label i)
void Foam::forceCoeffs::createFiles()
{
if (i == 0)
// Note: Only possible to create bin files after bins have been initialised
if (writeToFile() && !coeffFilePtr_.valid())
{
coeffFilePtr_ = createFile("coefficient");
writeIntegratedHeader("Coefficients", coeffFilePtr_());
if (nBin_ > 1)
{
CmBinFilePtr_ = createFile("CmBin");
writeBinHeader("Moment coefficient bins", CmBinFilePtr_());
CdBinFilePtr_ = createFile("CdBin");
writeBinHeader("Drag coefficient bins", CdBinFilePtr_());
ClBinFilePtr_ = createFile("ClBin");
writeBinHeader("Lift coefficient bins", ClBinFilePtr_());
}
}
}
void Foam::forceCoeffs::writeIntegratedHeader
(
const word& header,
Ostream& os
) const
{
writeHeader(os, "Force coefficients");
writeHeaderValue(os, "liftDir", liftDir_);
writeHeaderValue(os, "dragDir", dragDir_);
writeHeaderValue(os, "pitchAxis", pitchAxis_);
writeHeaderValue(os, "magUInf", magUInf_);
writeHeaderValue(os, "lRef", lRef_);
writeHeaderValue(os, "Aref", Aref_);
writeHeaderValue(os, "CofR", coordSys_.origin());
writeHeader(os, "");
writeCommented(os, "Time");
writeTabbed(os, "Cm");
writeTabbed(os, "Cd");
writeTabbed(os, "Cl");
writeTabbed(os, "Cl(f)");
writeTabbed(os, "Cl(r)");
os << endl;
}
void Foam::forceCoeffs::writeBinHeader
(
const word& header,
Ostream& os
) const
{
writeHeader(os, header);
writeHeaderValue(os, "bins", nBin_);
writeHeaderValue(os, "start", binMin_);
writeHeaderValue(os, "delta", binDx_);
writeHeaderValue(os, "direction", binDir_);
vectorField binPoints(nBin_);
writeCommented(os, "x co-ords :");
forAll(binPoints, pointI)
{
// force coeff data
writeHeader(file(i), "Force coefficients");
writeHeaderValue(file(i), "liftDir", liftDir_);
writeHeaderValue(file(i), "dragDir", dragDir_);
writeHeaderValue(file(i), "pitchAxis", pitchAxis_);
writeHeaderValue(file(i), "magUInf", magUInf_);
writeHeaderValue(file(i), "lRef", lRef_);
writeHeaderValue(file(i), "Aref", Aref_);
writeHeaderValue(file(i), "CofR", coordSys_.origin());
writeCommented(file(i), "Time");
writeTabbed(file(i), "Cm");
writeTabbed(file(i), "Cd");
writeTabbed(file(i), "Cl");
writeTabbed(file(i), "Cl(f)");
writeTabbed(file(i), "Cl(r)");
file(i)
<< tab << "Cm" << tab << "Cd" << tab << "Cl" << tab << "Cl(f)"
<< tab << "Cl(r)";
binPoints[pointI] = (binMin_ + (pointI + 1)*binDx_)*binDir_;
os << tab << binPoints[pointI].x();
}
else if (i == 1)
os << nl;
writeCommented(os, "y co-ords :");
forAll(binPoints, pointI)
{
// bin coeff data
os << tab << binPoints[pointI].y();
}
os << nl;
writeHeader(file(i), "Force coefficient bins");
writeHeaderValue(file(i), "bins", nBin_);
writeHeaderValue(file(i), "start", binMin_);
writeHeaderValue(file(i), "delta", binDx_);
writeHeaderValue(file(i), "direction", binDir_);
writeCommented(os, "z co-ords :");
forAll(binPoints, pointI)
{
os << tab << binPoints[pointI].z();
}
os << nl;
vectorField binPoints(nBin_);
writeCommented(file(i), "x co-ords :");
forAll(binPoints, pointI)
{
binPoints[pointI] = (binMin_ + (pointI + 1)*binDx_)*binDir_;
file(i) << tab << binPoints[pointI].x();
}
file(i) << nl;
writeHeader(os, "");
writeCommented(os, "Time");
writeCommented(file(i), "y co-ords :");
forAll(binPoints, pointI)
{
file(i) << tab << binPoints[pointI].y();
}
file(i) << nl;
for (label j = 0; j < nBin_; j++)
{
word jn(Foam::name(j) + ':');
writeTabbed(os, jn + "total");
writeTabbed(os, jn + "pressure");
writeTabbed(os, jn + "viscous");
writeCommented(file(i), "z co-ords :");
forAll(binPoints, pointI)
if (porosity_)
{
file(i) << tab << binPoints[pointI].z();
writeTabbed(os, jn + "porous");
}
file(i) << nl;
}
os << endl;
}
writeCommented(file(i), "Time");
for (label j = 0; j < nBin_; j++)
void Foam::forceCoeffs::writeIntegratedData
(
const word& title,
const List<Field<scalar> >& coeff
) const
{
scalar pressure = sum(coeff[0]);
scalar viscous = sum(coeff[1]);
scalar porous = sum(coeff[2]);
scalar total = pressure + viscous + porous;
if (log_)
{
Info<< " " << title << " : " << total << token::TAB
<< "("
<< "pressure: " << pressure << token::TAB
<< "viscous: " << viscous;
if (porosity_)
{
const word jn('(' + Foam::name(j) + ')');
writeTabbed(file(i), "Cm" + jn);
writeTabbed(file(i), "Cd" + jn);
writeTabbed(file(i), "Cl" + jn);
Info<< token::TAB << "porous: " << porous;
}
Info<< ")" << endl;
}
else
}
void Foam::forceCoeffs::writeBinData
(
const List<Field<scalar> > coeffs,
Ostream& os
) const
{
os << obr_.time().value();
for (label binI = 0; binI < nBin_; binI++)
{
FatalErrorIn("void Foam::forces::writeFileHeader(const label)")
<< "Unhandled file index: " << i
<< abort(FatalError);
scalar total = coeffs[0][binI] + coeffs[1][binI] + coeffs[2][binI];
os << tab << total << tab << coeffs[0][binI] << tab << coeffs[1][binI];
if (porosity_)
{
os << tab << coeffs[2][binI];
}
}
file(i)<< endl;
os << endl;
}
......@@ -124,7 +203,8 @@ Foam::forceCoeffs::forceCoeffs
const word& name,
const objectRegistry& obr,
const dictionary& dict,
const bool loadFromFiles
const bool loadFromFiles,
const bool readFields
)
:
forces(name, obr, dict, loadFromFiles, false),
......@@ -133,11 +213,17 @@ Foam::forceCoeffs::forceCoeffs
pitchAxis_(vector::zero),
magUInf_(0.0),
lRef_(0.0),
Aref_(0.0)
Aref_(0.0),
coeffFilePtr_(),
CmBinFilePtr_(),
CdBinFilePtr_(),
ClBinFilePtr_()
{
read(dict);
Info<< endl;
if (readFields)
{
read(dict);
if (log_) Info << endl;
}
}
......@@ -151,115 +237,216 @@ Foam::forceCoeffs::~forceCoeffs()
void Foam::forceCoeffs::read(const dictionary& dict)
{
if (active_)
if (!active_)
{
forces::read(dict);
// Directions for lift and drag forces, and pitch moment
dict.lookup("liftDir") >> liftDir_;
dict.lookup("dragDir") >> dragDir_;
dict.lookup("pitchAxis") >> pitchAxis_;
// Free stream velocity magnitude
dict.lookup("magUInf") >> magUInf_;
// Reference length and area scales
dict.lookup("lRef") >> lRef_;
dict.lookup("Aref") >> Aref_;
return;
}
}
void Foam::forceCoeffs::execute()
{
// Do nothing - only valid on write
}
forces::read(dict);
// Directions for lift and drag forces, and pitch moment
dict.lookup("liftDir") >> liftDir_;
dict.lookup("dragDir") >> dragDir_;
dict.lookup("pitchAxis") >> pitchAxis_;
void Foam::forceCoeffs::end()
{
// Do nothing - only valid on write
}
// Free stream velocity magnitude
dict.lookup("magUInf") >> magUInf_;
// Reference length and area scales
dict.lookup("lRef") >> lRef_;
dict.lookup("Aref") >> Aref_;
void Foam::forceCoeffs::timeSet()
{
// Do nothing - only valid on write
if (writeFields_)
{
const fvMesh& mesh = refCast<const fvMesh>(obr_);
tmp<volVectorField> tforceCoeff
(
new volVectorField
(
IOobject
(
fieldName("forceCoeff"),
mesh.time().timeName(),
mesh,
IOobject::NO_READ,
IOobject::NO_WRITE
),
mesh,
dimensionedVector("0", dimless, vector::zero)
)
);
obr_.store(tforceCoeff.ptr());
tmp<volVectorField> tmomentCoeff
(
new volVectorField
(
IOobject
(
fieldName("momentCoeff"),
mesh.time().timeName(),
mesh,
IOobject::NO_READ,
IOobject::NO_WRITE
),
mesh,
dimensionedVector("0", dimless, vector::zero)
)
);
obr_.store(tmomentCoeff.ptr());
}
}
void Foam::forceCoeffs::write()
void Foam::forceCoeffs::execute()
{
forces::calcForcesMoment();
if (!active_)
{
return;
}
if (Pstream::master())
{
functionObjectFile::write();
forces::calcForcesMoment();
createFiles();
scalar pDyn = 0.5*rhoRef_*magUInf_*magUInf_;
scalar pDyn = 0.5*rhoRef_*magUInf_*magUInf_;
Field<vector> totForce(force_[0] + force_[1] + force_[2]);
Field<vector> totMoment(moment_[0] + moment_[1] + moment_[2]);
// Storage for pressure, viscous and porous contributions to coeffs
List<Field<scalar> > momentCoeffs(3);
List<Field<scalar> > dragCoeffs(3);
List<Field<scalar> > liftCoeffs(3);
forAll(liftCoeffs, i)
{
momentCoeffs[i].setSize(nBin_);
dragCoeffs[i].setSize(nBin_);
liftCoeffs[i].setSize(nBin_);
}
List<Field<scalar> > coeffs(3);
coeffs[0].setSize(nBin_);
coeffs[1].setSize(nBin_);
coeffs[2].setSize(nBin_);
// Calculate coefficients
scalar CmTot = 0;
scalar CdTot = 0;
scalar ClTot = 0;
forAll(liftCoeffs, i)
{
momentCoeffs[i] = (moment_[i] & pitchAxis_)/(Aref_*pDyn*lRef_);
dragCoeffs[i] = (force_[i] & dragDir_)/(Aref_*pDyn);
liftCoeffs[i] = (force_[i] & liftDir_)/(Aref_*pDyn);
// lift, drag and moment
coeffs[0] = (totForce & liftDir_)/(Aref_*pDyn);
coeffs[1] = (totForce & dragDir_)/(Aref_*pDyn);
coeffs[2] = (totMoment & pitchAxis_)/(Aref_*lRef_*pDyn);
CmTot += sum(momentCoeffs[i]);
CdTot += sum(dragCoeffs[i]);
ClTot += sum(liftCoeffs[i]);
}
scalar Cl = sum(coeffs[0]);
scalar Cd = sum(coeffs[1]);
scalar Cm = sum(coeffs[2]);
scalar ClfTot = ClTot/2.0 + CmTot;
scalar ClrTot = ClTot/2.0 - CmTot;
scalar Clf = Cl/2.0 + Cm;
scalar Clr = Cl/2.0 - Cm;
if (log_)
{
Info<< type() << " " << name_ << " output:" << nl
<< " Coefficients" << nl;
}
writeIntegratedData("Cm", momentCoeffs);
writeIntegratedData("Cd", dragCoeffs);
writeIntegratedData("Cl", liftCoeffs);
if (log_)
{
Info<< " Cl(f) : " << ClfTot << nl
<< " Cl(r) : " << ClrTot << nl
<< endl;
}
file(0)
<< obr_.time().value() << tab << Cm << tab << Cd
<< tab << Cl << tab << Clf << tab << Clr << endl;
if (writeToFile())
{
coeffFilePtr_()
<< obr_.time().value() << tab << CmTot << tab << CdTot
<< tab << ClTot << tab << ClfTot << tab << ClrTot << endl;
if (log_) Info<< type() << " " << name_ << " output:" << nl
<< " Cm = " << Cm << nl
<< " Cd = " << Cd << nl
<< " Cl = " << Cl << nl
<< " Cl(f) = " << Clf << nl
<< " Cl(r) = " << Clr << endl;
if (nBin_ > 1)
{
if (binCumulative_)
{
for (label i = 1; i < coeffs[0].size(); i++)
forAll(liftCoeffs, i)
{
coeffs[0][i] += coeffs[0][i-1];
coeffs[1][i] += coeffs[1][i-1];
coeffs[2][i] += coeffs[2][i-1];
for (label binI = 1; binI < nBin_; binI++)
{
liftCoeffs[i][binI] += liftCoeffs[i][binI-1];
dragCoeffs[i][binI] += dragCoeffs[i][binI-1];
momentCoeffs[i][binI] += momentCoeffs[i][binI-1];
}
}
}
file(1)<< obr_.time().value();
writeBinData(dragCoeffs, CdBinFilePtr_());
writeBinData(liftCoeffs, ClBinFilePtr_());
writeBinData(momentCoeffs, CmBinFilePtr_());
}
}
forAll(coeffs[0], i)
{
file(1)
<< tab << coeffs[2][i]
<< tab << coeffs[1][i]
<< tab << coeffs[0][i];
}
if (writeFields_)
{
const volVectorField& force =
obr_.lookupObject<volVectorField>(fieldName("force"));
file(1) << endl;
}
const volVectorField& moment =
obr_.lookupObject<volVectorField>(fieldName("moment"));
volVectorField& forceCoeff =
const_cast<volVectorField&>
(
obr_.lookupObject<volVectorField>(fieldName("forceCoeff"))
);
volVectorField& momentCoeff =
const_cast<volVectorField&>
(
obr_.lookupObject<volVectorField>(fieldName("momentCoeff"))
);
dimensionedScalar f0("f0", dimForce, Aref_*pDyn);
dimensionedScalar m0("m0", dimForce*dimLength, Aref_*lRef_*pDyn);
forceCoeff == force/f0;
momentCoeff == moment/m0;
}
}
void Foam::forceCoeffs::end()
{
if (active_)
{
execute();
}
}
void Foam::forceCoeffs::timeSet()
{
// Do nothing
}
void Foam::forceCoeffs::write()
{
if (!active_)
{
return;
}
if (writeFields_)
{
const volVectorField& forceCoeff =
obr_.lookupObject<volVectorField>(fieldName("forceCoeff"));
const volVectorField& momentCoeff =
obr_.lookupObject<volVectorField>(fieldName("momentCoeff"));
if (log_) Info<< endl;
forceCoeff.write();
momentCoeff.write();
}
}
......
......@@ -2,8 +2,8 @@
========= |
\\ / F ield | OpenFOAM: The Open Source CFD Toolbox
\\ / O peration |
\\ / A nd | Copyright (C) 2011-2013 OpenFOAM Foundation
\\/ M anipulation |
\\ / A nd | Copyright (C) 2011-2015 OpenFOAM Foundation
\\/ M anipulation | Copyright (C) 2015 OpenCFD Ltd
-------------------------------------------------------------------------------
License
This file is part of OpenFOAM.
......@@ -32,6 +32,19 @@ Description
lift, drag and moment coefficients. The data can optionally be output into
bins, defined in a given direction.
The binned data provides the total and consitituentcomponents per bin:
- total coefficient
- pressure coefficient contribution
- viscous coefficient contribution
- porous coefficient contribution