functionObjects: Improved handling of multi-file indexing and code style

Patch contributed by Bruno Santos
Resolves bug-report http://bugs.openfoam.org/view.php?id=2216
This commit is contained in:
Henry Weller
2016-08-24 19:34:00 +01:00
parent fc44390db3
commit e476f80e7a
6 changed files with 169 additions and 143 deletions

View File

@ -72,12 +72,12 @@ void Foam::functionObjects::fieldValues::fieldValueDelta::writeFileHeader
const wordList& fields2 = region2Ptr_->fields(); const wordList& fields2 = region2Ptr_->fields();
DynamicList<word> commonFields(fields1.size()); DynamicList<word> commonFields(fields1.size());
forAll(fields1, i) forAll(fields1, fieldi)
{ {
label index = findIndex(fields2, fields1[i]); label index = findIndex(fields2, fields1[fieldi]);
if (index != -1) if (index != -1)
{ {
commonFields.append(fields1[i]); commonFields.append(fields1[fieldi]);
} }
} }

View File

@ -554,11 +554,11 @@ void Foam::functionObjects::fieldValues::surfaceFieldValue::writeFileHeader
file() << tab << "Area"; file() << tab << "Area";
} }
forAll(fields_, i) forAll(fields_, fieldi)
{ {
file() file()
<< tab << operationTypeNames_[operation_] << tab << operationTypeNames_[operation_]
<< "(" << fields_[i] << ")"; << "(" << fields_[fieldi] << ")";
} }
file() << endl; file() << endl;

View File

@ -96,11 +96,11 @@ void Foam::functionObjects::fieldValues::volFieldValue::writeFileHeader
writeCommented(file(), "Time"); writeCommented(file(), "Time");
forAll(fields_, i) forAll(fields_, fieldi)
{ {
file() file()
<< tab << operationTypeNames_[operation_] << tab << operationTypeNames_[operation_]
<< "(" << fields_[i] << ")"; << "(" << fields_[fieldi] << ")";
} }
file() << endl; file() << endl;

View File

@ -42,73 +42,80 @@ namespace functionObjects
void Foam::functionObjects::forceCoeffs::writeFileHeader(const label i) void Foam::functionObjects::forceCoeffs::writeFileHeader(const label i)
{ {
if (i == 0) switch (fileID(i))
{ {
// force coeff data case MAIN_FILE:
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)");
}
else if (i == 1)
{
// bin coeff data
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_);
vectorField binPoints(nBin_);
writeCommented(file(i), "x co-ords :");
forAll(binPoints, pointi)
{ {
binPoints[pointi] = (binMin_ + (pointi + 1)*binDx_)*binDir_; // force coeff data
file(i) << tab << binPoints[pointi].x();
}
file(i) << nl;
writeCommented(file(i), "y co-ords :"); writeHeader(file(i), "Force coefficients");
forAll(binPoints, pointi) 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)");
break;
}
case BINS_FILE:
{ {
file(i) << tab << binPoints[pointi].y(); // bin coeff data
}
file(i) << nl;
writeCommented(file(i), "z co-ords :"); writeHeader(file(i), "Force coefficient bins");
forAll(binPoints, pointi) writeHeaderValue(file(i), "bins", nBin_);
writeHeaderValue(file(i), "start", binMin_);
writeHeaderValue(file(i), "delta", binDx_);
writeHeaderValue(file(i), "direction", binDir_);
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;
writeCommented(file(i), "y co-ords :");
forAll(binPoints, pointi)
{
file(i) << tab << binPoints[pointi].y();
}
file(i) << nl;
writeCommented(file(i), "z co-ords :");
forAll(binPoints, pointi)
{
file(i) << tab << binPoints[pointi].z();
}
file(i) << nl;
writeCommented(file(i), "Time");
for (label j = 0; j < nBin_; j++)
{
const word jn('(' + Foam::name(j) + ')');
writeTabbed(file(i), "Cm" + jn);
writeTabbed(file(i), "Cd" + jn);
writeTabbed(file(i), "Cl" + jn);
}
break;
}
default:
{ {
file(i) << tab << binPoints[pointi].z(); FatalErrorInFunction
<< "Unhandled file index: " << i
<< abort(FatalError);
} }
file(i) << nl;
writeCommented(file(i), "Time");
for (label j = 0; j < nBin_; j++)
{
const word jn('(' + Foam::name(j) + ')');
writeTabbed(file(i), "Cm" + jn);
writeTabbed(file(i), "Cd" + jn);
writeTabbed(file(i), "Cl" + jn);
}
}
else
{
FatalErrorInFunction
<< "Unhandled file index: " << i
<< abort(FatalError);
} }
file(i)<< endl; file(i)<< endl;
@ -200,8 +207,8 @@ bool Foam::functionObjects::forceCoeffs::write()
scalar Clf = Cl/2.0 + Cm; scalar Clf = Cl/2.0 + Cm;
scalar Clr = Cl/2.0 - Cm; scalar Clr = Cl/2.0 - Cm;
writeTime(file(0)); writeTime(file(MAIN_FILE));
file(0) file(MAIN_FILE)
<< tab << Cm << tab << Cd << tab << Cm << tab << Cd
<< tab << Cl << tab << Clf << tab << Clr << endl; << tab << Cl << tab << Clf << tab << Clr << endl;
@ -224,17 +231,17 @@ bool Foam::functionObjects::forceCoeffs::write()
} }
} }
writeTime(file(1)); writeTime(file(BINS_FILE));
forAll(coeffs[0], i) forAll(coeffs[0], i)
{ {
file(1) file(BINS_FILE)
<< tab << coeffs[2][i] << tab << coeffs[2][i]
<< tab << coeffs[1][i] << tab << coeffs[1][i]
<< tab << coeffs[0][i]; << tab << coeffs[0][i];
} }
file(1) << endl; file(BINS_FILE) << endl;
} }
Log << endl; Log << endl;

View File

@ -53,6 +53,8 @@ Foam::wordList Foam::functionObjects::forces::createFileNames
DynamicList<word> names(1); DynamicList<word> names(1);
const word forceType(dict.lookup("type")); const word forceType(dict.lookup("type"));
// Name for file(MAIN_FILE=0)
names.append(forceType); names.append(forceType);
if (dict.found("binData")) if (dict.found("binData"))
@ -61,6 +63,7 @@ Foam::wordList Foam::functionObjects::forces::createFileNames
label nb = readLabel(binDict.lookup("nBin")); label nb = readLabel(binDict.lookup("nBin"));
if (nb > 0) if (nb > 0)
{ {
// Name for file(BINS_FILE=1)
names.append(forceType + "_bins"); names.append(forceType + "_bins");
} }
} }
@ -71,86 +74,95 @@ Foam::wordList Foam::functionObjects::forces::createFileNames
void Foam::functionObjects::forces::writeFileHeader(const label i) void Foam::functionObjects::forces::writeFileHeader(const label i)
{ {
if (i == 0) switch (fileID(i))
{ {
// force data case MAIN_FILE:
writeHeader(file(i), "Forces");
writeHeaderValue(file(i), "CofR", coordSys_.origin());
writeCommented(file(i), "Time");
file(i)
<< "forces(pressure viscous porous) "
<< "moment(pressure viscous porous)";
if (localSystem_)
{ {
// force data
writeHeader(file(i), "Forces");
writeHeaderValue(file(i), "CofR", coordSys_.origin());
writeCommented(file(i), "Time");
const word forceTypes("(pressure viscous porous)");
file(i) file(i)
<< tab << "forces" << forceTypes << tab
<< "localForces(pressure,viscous,porous) " << "moments" << forceTypes;
<< "localMoments(pressure,viscous,porous)";
if (localSystem_)
{
file(i)
<< tab
<< "localForces" << forceTypes << tab
<< "localMoments" << forceTypes;
}
break;
} }
} case BINS_FILE:
else if (i == 1)
{
// bin data
writeHeader(file(i), "Force bins");
writeHeaderValue(file(i), "bins", nBin_);
writeHeaderValue(file(i), "start", binMin_);
writeHeaderValue(file(i), "delta", binDx_);
writeHeaderValue(file(i), "direction", binDir_);
vectorField binPoints(nBin_);
writeCommented(file(i), "x co-ords :");
forAll(binPoints, pointi)
{ {
binPoints[pointi] = (binMin_ + (pointi + 1)*binDx_)*binDir_; // bin data
file(i) << tab << binPoints[pointi].x();
}
file(i) << nl;
writeCommented(file(i), "y co-ords :"); writeHeader(file(i), "Force bins");
forAll(binPoints, pointi) writeHeaderValue(file(i), "bins", nBin_);
{ writeHeaderValue(file(i), "start", binMin_);
file(i) << tab << binPoints[pointi].y(); writeHeaderValue(file(i), "delta", binDx_);
} writeHeaderValue(file(i), "direction", binDir_);
file(i) << nl;
writeCommented(file(i), "z co-ords :"); vectorField binPoints(nBin_);
forAll(binPoints, pointi) writeCommented(file(i), "x co-ords :");
{ forAll(binPoints, pointi)
file(i) << tab << binPoints[pointi].z(); {
} binPoints[pointi] = (binMin_ + (pointi + 1)*binDx_)*binDir_;
file(i) << nl; file(i) << tab << binPoints[pointi].x();
}
file(i) << nl;
writeCommented(file(i), "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++) writeCommented(file(i), "z co-ords :");
{ forAll(binPoints, pointi)
const word jn('(' + Foam::name(j) + ')'); {
const word f("forces" + jn + "[pressure,viscous,porous]"); file(i) << tab << binPoints[pointi].z();
const word m("moments" + jn + "[pressure,viscous,porous]"); }
file(i) << nl;
file(i)<< tab << f << tab << m; writeCommented(file(i), "Time");
}
if (localSystem_) const word binForceTypes("[pressure,viscous,porous]");
{
for (label j = 0; j < nBin_; j++) for (label j = 0; j < nBin_; j++)
{ {
const word jn('(' + Foam::name(j) + ')'); const word jn('(' + Foam::name(j) + ')');
const word f("localForces" + jn + "[pressure,viscous,porous]"); const word f("forces" + jn + binForceTypes);
const word m("localMoments" + jn + "[pressure,viscous,porous]"); const word m("moments" + jn + binForceTypes);
file(i)<< tab << f << tab << m; file(i)<< tab << f << tab << m;
} }
if (localSystem_)
{
for (label j = 0; j < nBin_; j++)
{
const word jn('(' + Foam::name(j) + ')');
const word f("localForces" + jn + binForceTypes);
const word m("localMoments" + jn + binForceTypes);
file(i)<< tab << f << tab << m;
}
}
break;
}
default:
{
FatalErrorInFunction
<< "Unhandled file index: " << i
<< abort(FatalError);
} }
}
else
{
FatalErrorInFunction
<< "Unhandled file index: " << i
<< abort(FatalError);
} }
file(i)<< endl; file(i)<< endl;
@ -406,8 +418,8 @@ void Foam::functionObjects::forces::writeForces()
<< " porous : " << sum(moment_[2]) << " porous : " << sum(moment_[2])
<< endl; << endl;
writeTime(file(0)); writeTime(file(MAIN_FILE));
file(0) << tab << setw(1) << '(' file(MAIN_FILE) << tab << setw(1) << '('
<< sum(force_[0]) << setw(1) << ' ' << sum(force_[0]) << setw(1) << ' '
<< sum(force_[1]) << setw(1) << ' ' << sum(force_[1]) << setw(1) << ' '
<< sum(force_[2]) << setw(3) << ") (" << sum(force_[2]) << setw(3) << ") ("
@ -425,8 +437,8 @@ void Foam::functionObjects::forces::writeForces()
vectorField localMomentT(coordSys_.localVector(moment_[1])); vectorField localMomentT(coordSys_.localVector(moment_[1]));
vectorField localMomentP(coordSys_.localVector(moment_[2])); vectorField localMomentP(coordSys_.localVector(moment_[2]));
writeTime(file(0)); writeTime(file(MAIN_FILE));
file(0) << tab << setw(1) << '(' file(MAIN_FILE) << tab << setw(1) << '('
<< sum(localForceN) << setw(1) << ' ' << sum(localForceN) << setw(1) << ' '
<< sum(localForceT) << setw(1) << ' ' << sum(localForceT) << setw(1) << ' '
<< sum(localForceP) << setw(3) << ") (" << sum(localForceP) << setw(3) << ") ("
@ -462,11 +474,11 @@ void Foam::functionObjects::forces::writeBins()
} }
} }
writeTime(file(1)); writeTime(file(BINS_FILE));
forAll(f[0], i) forAll(f[0], i)
{ {
file(1) file(BINS_FILE)
<< tab << setw(1) << '(' << tab << setw(1) << '('
<< f[0][i] << setw(1) << ' ' << f[0][i] << setw(1) << ' '
<< f[1][i] << setw(1) << ' ' << f[1][i] << setw(1) << ' '
@ -502,7 +514,7 @@ void Foam::functionObjects::forces::writeBins()
forAll(lf[0], i) forAll(lf[0], i)
{ {
file(1) file(BINS_FILE)
<< tab << setw(1) << '(' << tab << setw(1) << '('
<< lf[0][i] << setw(1) << ' ' << lf[0][i] << setw(1) << ' '
<< lf[1][i] << setw(1) << ' ' << lf[1][i] << setw(1) << ' '
@ -513,7 +525,7 @@ void Foam::functionObjects::forces::writeBins()
} }
} }
file(1) << endl; file(BINS_FILE) << endl;
} }

View File

@ -141,6 +141,13 @@ protected:
// Protected data // Protected data
//- Enumeration for ensuring the right file is accessed
enum fileID
{
MAIN_FILE = 0,
BINS_FILE = 1
};
//- Pressure, viscous and porous force per bin //- Pressure, viscous and porous force per bin
List<Field<vector>> force_; List<Field<vector>> force_;