From e476f80e7a87c958d2b54e84606bda8fd8bfc697 Mon Sep 17 00:00:00 2001 From: Henry Weller Date: Wed, 24 Aug 2016 19:34:00 +0100 Subject: [PATCH] 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 --- .../fieldValueDelta/fieldValueDelta.C | 6 +- .../surfaceFieldValue/surfaceFieldValue.C | 4 +- .../fieldValues/volFieldValue/volFieldValue.C | 4 +- .../forces/forceCoeffs/forceCoeffs.C | 137 ++++++++-------- src/functionObjects/forces/forces/forces.C | 154 ++++++++++-------- src/functionObjects/forces/forces/forces.H | 7 + 6 files changed, 169 insertions(+), 143 deletions(-) diff --git a/src/functionObjects/field/fieldValues/fieldValueDelta/fieldValueDelta.C b/src/functionObjects/field/fieldValues/fieldValueDelta/fieldValueDelta.C index 4937334f53..8e44052381 100644 --- a/src/functionObjects/field/fieldValues/fieldValueDelta/fieldValueDelta.C +++ b/src/functionObjects/field/fieldValues/fieldValueDelta/fieldValueDelta.C @@ -72,12 +72,12 @@ void Foam::functionObjects::fieldValues::fieldValueDelta::writeFileHeader const wordList& fields2 = region2Ptr_->fields(); DynamicList commonFields(fields1.size()); - forAll(fields1, i) + forAll(fields1, fieldi) { - label index = findIndex(fields2, fields1[i]); + label index = findIndex(fields2, fields1[fieldi]); if (index != -1) { - commonFields.append(fields1[i]); + commonFields.append(fields1[fieldi]); } } diff --git a/src/functionObjects/field/fieldValues/surfaceFieldValue/surfaceFieldValue.C b/src/functionObjects/field/fieldValues/surfaceFieldValue/surfaceFieldValue.C index 2b62173c89..dc1a450b20 100644 --- a/src/functionObjects/field/fieldValues/surfaceFieldValue/surfaceFieldValue.C +++ b/src/functionObjects/field/fieldValues/surfaceFieldValue/surfaceFieldValue.C @@ -554,11 +554,11 @@ void Foam::functionObjects::fieldValues::surfaceFieldValue::writeFileHeader file() << tab << "Area"; } - forAll(fields_, i) + forAll(fields_, fieldi) { file() << tab << operationTypeNames_[operation_] - << "(" << fields_[i] << ")"; + << "(" << fields_[fieldi] << ")"; } file() << endl; diff --git a/src/functionObjects/field/fieldValues/volFieldValue/volFieldValue.C b/src/functionObjects/field/fieldValues/volFieldValue/volFieldValue.C index 6389143a45..5f3667e21f 100644 --- a/src/functionObjects/field/fieldValues/volFieldValue/volFieldValue.C +++ b/src/functionObjects/field/fieldValues/volFieldValue/volFieldValue.C @@ -96,11 +96,11 @@ void Foam::functionObjects::fieldValues::volFieldValue::writeFileHeader writeCommented(file(), "Time"); - forAll(fields_, i) + forAll(fields_, fieldi) { file() << tab << operationTypeNames_[operation_] - << "(" << fields_[i] << ")"; + << "(" << fields_[fieldi] << ")"; } file() << endl; diff --git a/src/functionObjects/forces/forceCoeffs/forceCoeffs.C b/src/functionObjects/forces/forceCoeffs/forceCoeffs.C index b3ac38067d..635edfd6f4 100644 --- a/src/functionObjects/forces/forceCoeffs/forceCoeffs.C +++ b/src/functionObjects/forces/forceCoeffs/forceCoeffs.C @@ -42,73 +42,80 @@ namespace functionObjects void Foam::functionObjects::forceCoeffs::writeFileHeader(const label i) { - if (i == 0) + switch (fileID(i)) { - // 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)"); - } - 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) + case MAIN_FILE: { - binPoints[pointi] = (binMin_ + (pointi + 1)*binDx_)*binDir_; - file(i) << tab << binPoints[pointi].x(); - } - file(i) << nl; + // force coeff data - writeCommented(file(i), "y co-ords :"); - forAll(binPoints, pointi) + 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)"); + + break; + } + case BINS_FILE: { - file(i) << tab << binPoints[pointi].y(); - } - file(i) << nl; + // bin coeff data - writeCommented(file(i), "z co-ords :"); - forAll(binPoints, pointi) + 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_; + 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; @@ -200,8 +207,8 @@ bool Foam::functionObjects::forceCoeffs::write() scalar Clf = Cl/2.0 + Cm; scalar Clr = Cl/2.0 - Cm; - writeTime(file(0)); - file(0) + writeTime(file(MAIN_FILE)); + file(MAIN_FILE) << tab << Cm << tab << Cd << 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) { - file(1) + file(BINS_FILE) << tab << coeffs[2][i] << tab << coeffs[1][i] << tab << coeffs[0][i]; } - file(1) << endl; + file(BINS_FILE) << endl; } Log << endl; diff --git a/src/functionObjects/forces/forces/forces.C b/src/functionObjects/forces/forces/forces.C index f2d13dba60..ec0f0810e5 100644 --- a/src/functionObjects/forces/forces/forces.C +++ b/src/functionObjects/forces/forces/forces.C @@ -53,6 +53,8 @@ Foam::wordList Foam::functionObjects::forces::createFileNames DynamicList names(1); const word forceType(dict.lookup("type")); + + // Name for file(MAIN_FILE=0) names.append(forceType); if (dict.found("binData")) @@ -61,6 +63,7 @@ Foam::wordList Foam::functionObjects::forces::createFileNames label nb = readLabel(binDict.lookup("nBin")); if (nb > 0) { + // Name for file(BINS_FILE=1) names.append(forceType + "_bins"); } } @@ -71,86 +74,95 @@ Foam::wordList Foam::functionObjects::forces::createFileNames void Foam::functionObjects::forces::writeFileHeader(const label i) { - if (i == 0) + switch (fileID(i)) { - // force data - - 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_) + case MAIN_FILE: { + // force data + + writeHeader(file(i), "Forces"); + writeHeaderValue(file(i), "CofR", coordSys_.origin()); + writeCommented(file(i), "Time"); + + const word forceTypes("(pressure viscous porous)"); file(i) - << tab - << "localForces(pressure,viscous,porous) " - << "localMoments(pressure,viscous,porous)"; + << "forces" << forceTypes << tab + << "moments" << forceTypes; + + if (localSystem_) + { + file(i) + << tab + << "localForces" << forceTypes << tab + << "localMoments" << forceTypes; + } + + break; } - } - 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) + case BINS_FILE: { - binPoints[pointi] = (binMin_ + (pointi + 1)*binDx_)*binDir_; - file(i) << tab << binPoints[pointi].x(); - } - file(i) << nl; + // bin data - writeCommented(file(i), "y co-ords :"); - forAll(binPoints, pointi) - { - file(i) << tab << binPoints[pointi].y(); - } - file(i) << nl; + 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_); - writeCommented(file(i), "z co-ords :"); - forAll(binPoints, pointi) - { - file(i) << tab << binPoints[pointi].z(); - } - file(i) << 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; - 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++) - { - const word jn('(' + Foam::name(j) + ')'); - const word f("forces" + jn + "[pressure,viscous,porous]"); - const word m("moments" + jn + "[pressure,viscous,porous]"); + writeCommented(file(i), "z co-ords :"); + forAll(binPoints, pointi) + { + file(i) << tab << binPoints[pointi].z(); + } + file(i) << nl; - file(i)<< tab << f << tab << m; - } - if (localSystem_) - { + writeCommented(file(i), "Time"); + + const word binForceTypes("[pressure,viscous,porous]"); for (label j = 0; j < nBin_; j++) { const word jn('(' + Foam::name(j) + ')'); - const word f("localForces" + jn + "[pressure,viscous,porous]"); - const word m("localMoments" + jn + "[pressure,viscous,porous]"); + const word f("forces" + jn + binForceTypes); + const word m("moments" + jn + binForceTypes); 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; @@ -406,8 +418,8 @@ void Foam::functionObjects::forces::writeForces() << " porous : " << sum(moment_[2]) << endl; - writeTime(file(0)); - file(0) << tab << setw(1) << '(' + writeTime(file(MAIN_FILE)); + file(MAIN_FILE) << tab << setw(1) << '(' << sum(force_[0]) << setw(1) << ' ' << sum(force_[1]) << setw(1) << ' ' << sum(force_[2]) << setw(3) << ") (" @@ -425,8 +437,8 @@ void Foam::functionObjects::forces::writeForces() vectorField localMomentT(coordSys_.localVector(moment_[1])); vectorField localMomentP(coordSys_.localVector(moment_[2])); - writeTime(file(0)); - file(0) << tab << setw(1) << '(' + writeTime(file(MAIN_FILE)); + file(MAIN_FILE) << tab << setw(1) << '(' << sum(localForceN) << setw(1) << ' ' << sum(localForceT) << setw(1) << ' ' << sum(localForceP) << setw(3) << ") (" @@ -462,11 +474,11 @@ void Foam::functionObjects::forces::writeBins() } } - writeTime(file(1)); + writeTime(file(BINS_FILE)); forAll(f[0], i) { - file(1) + file(BINS_FILE) << tab << setw(1) << '(' << f[0][i] << setw(1) << ' ' << f[1][i] << setw(1) << ' ' @@ -502,7 +514,7 @@ void Foam::functionObjects::forces::writeBins() forAll(lf[0], i) { - file(1) + file(BINS_FILE) << tab << setw(1) << '(' << lf[0][i] << setw(1) << ' ' << lf[1][i] << setw(1) << ' ' @@ -513,7 +525,7 @@ void Foam::functionObjects::forces::writeBins() } } - file(1) << endl; + file(BINS_FILE) << endl; } diff --git a/src/functionObjects/forces/forces/forces.H b/src/functionObjects/forces/forces/forces.H index fe93e48083..7c60cd4799 100644 --- a/src/functionObjects/forces/forces/forces.H +++ b/src/functionObjects/forces/forces/forces.H @@ -141,6 +141,13 @@ protected: // 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 List> force_;