CFD Online Logo CFD Online URL
www.cfd-online.com
[Sponsors]
Home > Forums > Software User Forums > OpenFOAM > OpenFOAM Post-Processing

position weighted volume average

Register Blogs Community New Posts Updated Threads Search

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
Old   March 30, 2018, 14:49
Default centroid | position weighted volume average
  #1
Senior Member
 
Lucky
Join Date: Apr 2011
Location: Orlando, FL USA
Posts: 5,750
Rep Power: 66
LuckyTran has a spectacular aura aboutLuckyTran has a spectacular aura aboutLuckyTran has a spectacular aura about
I'm trying to output the centroid of several fields, Temperature in this example. This seems doable using the weightedVolAverage function object and doing a position weighted volume average. But what field name should I set for the weight field to get the x,y,z coordinates? Or conversely, average the coordinates and weight it by the T field.

Code:
T.x
    {
          functionObjectLibs ("libfieldFunctionObjects.so");
          type                  cellSource;
          source                all;
          weightField        banana; // <<< x coordinate of T field
          fields                 ( T );
          operation             volAverage;
    }
I'm using OF 2.4 which only has the operation volAverage instead of weightedVolAverage (but supports weighting anyway). weightedVolIntegral would be nice, but I can't seem to find it.

Or maybe it is not possible with built-in function opbjects? swak4Foam, defining the new field in createFields, or a coded function object comes to mind.

Last edited by LuckyTran; April 9, 2018 at 17:16.
LuckyTran is online now   Reply With Quote

Old   April 1, 2018, 13:14
Default
  #2
Senior Member
 
Taher Chegini
Join Date: Nov 2014
Location: Houston, Texas
Posts: 125
Rep Power: 12
Taataa is on a distinguished road
The weightedVolAverage doesn't give the centroid only the value of average, you need to use the coded directive in the functions and write a code that calculates the centroid. For example:

Code:
const volScalarField& T = mesh().lookupObject<volScalarField>("T");
const volVectorField& centers = mesh().C();
const scalarField& vols = mesh().V();
const objectRegistry& db = mesh().thisDb();
scalar ts = db.time().value();

scalar sumX = 0;
scalar sumY = 0;
scalar sumTV = 0;
scalar sumV = 0;

forAll(centers, I)
{
    sumX    += T[I]*vols[I]*centers[I].x();
    sumY    += T[I]*vols[I]*centers[I].y();
    sumTV   += T[I]*vols[I];
    sumV    += vols[I];
}
reduce(sumX , sumOp<scalar>());
reduce(sumY , sumOp<scalar>());
reduce(sumTV, sumOp<scalar>());
reduce(sumV , sumOp<scalar>());

if (Pstream::master())
{
    scalar cX = sumX/max(sumTV, SMALL);
    scalar cY = sumY/max(sumTV, SMALL);
    scalar Ta = sumTV/max(sumV, SMALL);
    
    const fileName& outputFile = "TCentroids";
    OFstream os
    (
        outputFile,
        IOstream::ASCII,
        IOstream::currentVersion,
        IOstream::UNCOMPRESSED,
        true
    );
    
    Info<< "Writing temperature field centroid" << endl;
    os  << ts << "  " << Ta << "  " << cX << "  " << cY << endl;
}
I haven't tested it but it should work. Also, since you're using version 2 you may have to change the syntax, for example instead of mesh() I think you need to use mesh. I am not sure though.
Taataa is offline   Reply With Quote

Old   April 9, 2018, 17:36
Default
  #3
Senior Member
 
Lucky
Join Date: Apr 2011
Location: Orlando, FL USA
Posts: 5,750
Rep Power: 66
LuckyTran has a spectacular aura aboutLuckyTran has a spectacular aura aboutLuckyTran has a spectacular aura about
With minor tinkering to suit my purpose it works except for appending to an open file. In particular,

Code:
        OFstream os
        (
        outputFile,
        IOstream::ASCII,
        IOstream::currentVersion,
        IOstream::UNCOMPRESSED
        true
        );
This seems to work for only OpenFOAM-dev. OFstream does not have a constructor to take the append argument for 2.4, 4.0, and not even 5.0. Is there are workaround that doesn't involve recompiling the main source code? I'm okay with recompiling my custom solver, but I'd like to leave the main OF source alone.
LuckyTran is online now   Reply With Quote

Old   April 9, 2018, 17:53
Default
  #4
Senior Member
 
Taher Chegini
Join Date: Nov 2014
Location: Houston, Texas
Posts: 125
Rep Power: 12
Taataa is on a distinguished road
Quote:
Originally Posted by LuckyTran View Post
With minor tinkering it works except for appending to an open file. In particular,

Code:
        OFstream os
        (
        outputFile,
        ios_base::app,
        IOstream::ASCII,
        IOstream::currentVersion,
        IOstream::UNCOMPRESSED
        true
        );
This seems to work for only OpenFOAM-dev. OFstream does not have a constructor to take the append argument for 2.4, 4.0, and not even 5.0. Is there are workaround that doesn't involve recompiling the main source code? I'm okay with recompiling my custom solver, but I'd like to leave the main OF source alone.
It was added in version 5 (here). I remember when I was looking for a way to append a file this was the only way to use OF classes the rest was using C++ standard libraries. You can find a discussion here.
Taataa is offline   Reply With Quote

Old   April 9, 2018, 18:50
Default
  #5
Senior Member
 
Lucky
Join Date: Apr 2011
Location: Orlando, FL USA
Posts: 5,750
Rep Power: 66
LuckyTran has a spectacular aura aboutLuckyTran has a spectacular aura aboutLuckyTran has a spectacular aura about
So std :: ofstream writes to file but formatting is required and need to include iomanip (not the IOmanip.H in openfoam). Since OFstream is not used, it's no longer necessary to include OFstream.H. And std :: ofstream does not need any include.

Code:
#include <iomanip>
std::ofstream os;
    os.open ("Centroid", std::ofstream::out | std::ofstream::app);
    os <<std::setPrecision(9)<< ts << "  " << Ta << "  " << cX << "  " << cY << "  " << cZ << nl << endl;
    os.close();
LuckyTran is online now   Reply With Quote

Reply


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
how to calculate mass flow rate on patches and summation of that during the run? immortality OpenFOAM Post-Processing 104 February 16, 2021 09:46
area weighted average or mass weighted average sa har Main CFD Forum 0 January 5, 2016 14:16
how to read in a position file jiejie OpenFOAM 0 May 23, 2011 05:38
DPM UDF particle position using the macro P_POS(p)[i] dm2747 FLUENT 0 April 17, 2009 02:29
Combustion Convergence problems Art Stretton Phoenics 5 April 2, 2002 06:59


All times are GMT -4. The time now is 17:09.