diff --git a/etc/caseDicts/postProcessing/graphs/graphLayerAverage b/etc/caseDicts/postProcessing/graphs/graphLayerAverage index 23223ac58a..429089f587 100644 --- a/etc/caseDicts/postProcessing/graphs/graphLayerAverage +++ b/etc/caseDicts/postProcessing/graphs/graphLayerAverage @@ -10,18 +10,22 @@ Description \*---------------------------------------------------------------------------*/ -patches (); // Patches from which layers extrude +patches (); // Patches from which layers extrude -zones (); // Zones from which layers extrude +zones (); // Zones from which layers extrude -axis distance; // The independent variable of the graph. Can - // be "x", "y", "z", "xyz" (all coordinates - // written out), or "distance" (from the start - // point). +axis distance; // The independent variable of the graph. + // Can be "x", "y", "z", "xyz" (all + // coordinates written out), or + // "distance" (from the start point). -symmetric false; // Are the layers symmetric about the centre? +symmetric false; // Are the layers symmetric about the + // centre? -fields (); // Fields to plot +fields (); // Fields to average + +//weightField ; // Field or fields with which to weight +//weightFields (); // the average #includeEtc "caseDicts/postProcessing/graphs/graphLayerAverage.cfg" diff --git a/src/functionObjects/field/layerAverage/layerAverage.C b/src/functionObjects/field/layerAverage/layerAverage.C index 7a33dee0bd..e42edb8a09 100644 --- a/src/functionObjects/field/layerAverage/layerAverage.C +++ b/src/functionObjects/field/layerAverage/layerAverage.C @@ -126,10 +126,10 @@ void Foam::functionObjects::layerAverage::calcLayers() } // Sum number of entries per layer - layerCount_ = sum(scalarField(mesh_.nCells(), 1)); + layerVolume_ = sum(mesh_.V()); // Average the cell centres - layerCentre_ = sum(mesh_.cellCentres())/layerCount_; + layerCentre_ = sum(mesh_.V()*mesh_.C())/layerVolume_; // If symmetric, keep only half of the coordinates if (symmetric_) @@ -139,6 +139,34 @@ void Foam::functionObjects::layerAverage::calcLayers() } +Foam::tmp> +Foam::functionObjects::layerAverage::weight() const +{ + if (weightFields_.empty()) + { + return tmp>(); + } + + tmp> tresult = + VolInternalField::New + ( + "weight", + mesh_, + dimensionedScalar(dimless, scalar(1)) + ); + + forAll(weightFields_, i) + { + const VolInternalField& weightField = + mesh_.lookupObject>(weightFields_[i]); + + tresult.ref() *= weightField; + } + + return tresult; +} + + template<> Foam::vector Foam::functionObjects::layerAverage::symmetricCoeff() const @@ -245,6 +273,13 @@ bool Foam::functionObjects::layerAverage::read(const dictionary& dict) fields_ = dict.lookup("fields"); + weightFields_ = + dict.found("weightFields") + ? dict.lookup("weightFields") + : dict.found("weightField") + ? wordList(1, dict.lookup("weightField")) + : wordList(); + formatter_ = setWriter::New(dict.lookup("setFormat"), dict); calcLayers(); @@ -255,7 +290,9 @@ bool Foam::functionObjects::layerAverage::read(const dictionary& dict) Foam::wordList Foam::functionObjects::layerAverage::fields() const { - return fields_; + wordList result(fields_); + result.append(weightFields_); + return result; } @@ -267,6 +304,13 @@ bool Foam::functionObjects::layerAverage::execute() bool Foam::functionObjects::layerAverage::write() { + // Get the weights + tmp> weight(this->weight()); + tmp> layerWeight + ( + weight.valid() ? sum(mesh_.V()*weight) : tmp>() + ); + // Create list of available fields wordList fieldNames; forAll(fields_, fieldi) @@ -275,7 +319,7 @@ bool Foam::functionObjects::layerAverage::write() ( false #define FoundTypeField(Type, nullArg) \ - || foundObject>(fields_[fieldi]) + || foundObject>(fields_[fieldi]) FOR_ALL_FIELD_TYPES(FoundTypeField) #undef FoundTypeField ) @@ -295,16 +339,18 @@ bool Foam::functionObjects::layerAverage::write() #undef DeclareTypeValueSets forAll(fieldNames, fieldi) { + const word& fieldName = fieldNames[fieldi]; + #define CollapseTypeFields(Type, nullArg) \ - if (mesh_.foundObject>(fieldNames[fieldi])) \ + if (mesh_.foundObject>(fieldName)) \ { \ - const VolField& field = \ - mesh_.lookupObject>(fieldNames[fieldi]); \ + const VolInternalField& field = \ + mesh_.lookupObject>(fieldName); \ \ Type##ValueSets.set \ ( \ fieldi, \ - average(field.primitiveField()) \ + average(weight, layerWeight, field) \ ); \ } FOR_ALL_FIELD_TYPES(CollapseTypeFields); diff --git a/src/functionObjects/field/layerAverage/layerAverage.H b/src/functionObjects/field/layerAverage/layerAverage.H index 3634a961a7..17e69ea97c 100644 --- a/src/functionObjects/field/layerAverage/layerAverage.H +++ b/src/functionObjects/field/layerAverage/layerAverage.H @@ -2,7 +2,7 @@ ========= | \\ / F ield | OpenFOAM: The Open Source CFD Toolbox \\ / O peration | Website: https://openfoam.org - \\ / A nd | Copyright (C) 2011-2022 OpenFOAM Foundation + \\ / A nd | Copyright (C) 2011-2023 OpenFOAM Foundation \\/ M anipulation | ------------------------------------------------------------------------------- License @@ -59,7 +59,9 @@ Usage axis | Component of the position to plot against | yes | symmetric | Is the geometry symmetric around the centre layer? \ | no | false - field | Fields to average and plot | yes | + fields | Fields to average and plot | yes | + weightField | Field with which to weight the average | no | none + weightFields | Fields with which to weight the average | no | () \endtable SourceFiles @@ -74,6 +76,7 @@ SourceFiles #include "fvMeshFunctionObject.H" #include "setWriter.H" #include "boolList.H" +#include "volFields.H" // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // @@ -113,15 +116,18 @@ class layerAverage //- Per cell the global layer labelList cellLayer_; - //- Per global layer the number of cells - scalarField layerCount_; + //- Per global layer the volume + scalarField layerVolume_; //- The average centre of each layer pointField layerCentre_; - //- Fields to sample + //- Fields to average wordList fields_; + //- Fields with which to weight the averages + wordList weightFields_; + //- Set formatter autoPtr formatter_; @@ -131,17 +137,26 @@ class layerAverage //- Create the layer information, the sort map, and the scalar axis void calcLayers(); + //- Calculate and return the weight field, or a null pointer if there + // are no weight fields + tmp> weight() const; + //- Return the coefficient to multiply onto symmetric values template T symmetricCoeff() const; //- Sum field per layer template - Field sum(const Field& cellField) const; + tmp> sum(const VolInternalField& cellField) const; //- Average a field per layer template - Field average(const Field& cellField) const; + tmp> average + ( + const tmp>& cellWeight, + const tmp>& layerWeight, + const VolInternalField& cellField + ) const; public: diff --git a/src/functionObjects/field/layerAverage/layerAverageTemplates.C b/src/functionObjects/field/layerAverage/layerAverageTemplates.C index 27154e3b62..18dfe2868a 100644 --- a/src/functionObjects/field/layerAverage/layerAverageTemplates.C +++ b/src/functionObjects/field/layerAverage/layerAverageTemplates.C @@ -2,7 +2,7 @@ ========= | \\ / F ield | OpenFOAM: The Open Source CFD Toolbox \\ / O peration | Website: https://openfoam.org - \\ / A nd | Copyright (C) 2011-2022 OpenFOAM Foundation + \\ / A nd | Copyright (C) 2011-2023 OpenFOAM Foundation \\/ M anipulation | ------------------------------------------------------------------------------- License @@ -24,6 +24,7 @@ License \*---------------------------------------------------------------------------*/ #include "layerAverage.H" +#include "fvMesh.H" // * * * * * * * * * * * * * Private Member Functions * * * * * * * * * * * // @@ -35,12 +36,13 @@ T Foam::functionObjects::layerAverage::symmetricCoeff() const template -Foam::Field Foam::functionObjects::layerAverage::sum +Foam::tmp> Foam::functionObjects::layerAverage::sum ( - const Field& cellField + const VolInternalField& cellField ) const { - Field layerField(nLayers_, Zero); + tmp> tlayerField(new Field(nLayers_, Zero)); + Field& layerField = tlayerField.ref(); forAll(cellLayer_, celli) { @@ -53,22 +55,30 @@ Foam::Field Foam::functionObjects::layerAverage::sum Pstream::listCombineGather(layerField, plusEqOp()); Pstream::listCombineScatter(layerField); - return layerField; + return tlayerField; } template -Foam::Field Foam::functionObjects::layerAverage::average +Foam::tmp> Foam::functionObjects::layerAverage::average ( - const Field& cellField + const tmp>& cellWeight, + const tmp>& layerWeight, + const VolInternalField& cellField ) const { - // Sum and average - Field layerField(sum(cellField)/layerCount_); + tmp> tlayerField + ( + cellWeight.valid() + ? sum(mesh_.V()*cellWeight*cellField)/layerWeight + : sum(mesh_.V()*cellField)/layerVolume_ + ); // Handle symmetry if (symmetric_) { + Field& layerField = tlayerField.ref(); + const T coeff = symmetricCoeff(); for (label i=0; i Foam::functionObjects::layerAverage::average layerField.setSize(nLayers_/2); } - return layerField; + return tlayerField; }