CFD Online Discussion Forums (http://www.cfd-online.com/Forums/)
-   OpenFOAM Post-Processing (http://www.cfd-online.com/Forums/openfoam-post-processing/)
-   -   Volume averaging in cell (http://www.cfd-online.com/Forums/openfoam-post-processing/129978-volume-averaging-cell.html)

 leroyv February 17, 2014 14:44

Volume averaging in cell

Dear OpenFOAM users,

I solved a flow problem over and array of cylinders:
https://www.dropbox.com/s/v5nz4mvx6user06/6-4_cell.png

I would like to get the volume average of the velocity and pressure fields over a limited, moving volume (typically the size of a single cell, see the mask on the screenshot). The resulting field would be continuously defined over the entire domain, including in the cylinders. This is field volume averaging / spatial frequency filtering, in the sense of Whitaker (1999) and is somehow like the spatial averaging operation used for LES theory.

Do you think it is possible to do this while solving the problem or as a post-processing operation? I didn't find a way to apply such a moving filter in Paraview or in the sample utility, and I don't know how to program this in a solver.

 leroyv February 18, 2014 12:57

Update

Additional (maybe simpler) question: How to export the fields to that kind format:
x y z Ux Uy Uz p
with (x,y,z) coordinates on a cartesian grid. Just doing that would be sufficient, the computation of the average being possible as a post-processing operation.

 leroyv February 27, 2014 11:09

Solution (non optimal)

1 Attachment(s)
After a week of hacking (or so), I put together a solution. I forked simpleFoam and added this piece of code in it:

Code:

{     Info<<"Computing average velocity field\n" << endl;     // Constants     IOdictionary filterProperties(         IOobject(             "filterProperties",             runTime.system(),             mesh,             IOobject::MUST_READ,             IOobject::NO_WRITE         )     );     // This is AWFUL hacking     scalar  filterRadius = dimensionedScalar(filterProperties.lookup("filterRadius")).value(),             filterXMin  = dimensionedScalar(filterProperties.lookup("filterXMin")).value(),             filterXMax  = dimensionedScalar(filterProperties.lookup("filterXMax")).value(),             filterYMin  = dimensionedScalar(filterProperties.lookup("filterYMin")).value(),             filterYMax  = dimensionedScalar(filterProperties.lookup("filterYMax")).value();     int    nX          = dimensionedScalar(filterProperties.lookup("nX")).value(),             nY          = dimensionedScalar(filterProperties.lookup("nY")).value();     scalar  hX = (filterXMax - filterXMin) / int(nX-1),             hY = (filterYMax - filterYMin) / int(nY-1);     const volVectorField & cellCenterField = mesh.C();     const DimensionedField<scalar, volMesh> & cellVolumeField = mesh.V();     // Variable initialization     scalar normInfty = 0.;     scalar filterCellVolume = 0.;     Vector<scalar> filterCenter(0.,0.,0.);     Vector<scalar> currentCellCenter(0., 0., 0.);     List<Vector<scalar> > filterCenterList;     List<Vector<scalar> > UBarList;     volScalarField filter(         IOobject(             "filter",             runTime.timeName(),             mesh,             IOobject::NO_READ,             IOobject::NO_WRITE         ),         mesh,         dimensionedScalar("filter", dimensionSet(0,0,0,0,0,0,0), 0.)     );     // Build field center list     for ( int j = 0; j < nY; ++j )     {         for ( int i = 0; i < nX; ++i)         {             filterCenterList.append(                 Vector<scalar>(                     filterXMin + hX * int(i),                     filterYMin + hY * int(j),                     0.                 )             );         }     }     UBarList.resize(filterCenterList.size());     // Loop over filter center coordinates     forAll(filterCenterList, iFilterCenterList)     {         // Define filter center         filterCenter = filterCenterList[iFilterCenterList];         //Info<< "filterCenter = " << filterCenter << endl;                 // Set filter         forAll(cellCenterField, iCellCenterField)         {             currentCellCenter = cellCenterField[iCellCenterField];             normInfty = max( mag(currentCellCenter.x() - filterCenter.x()),                         max( mag(currentCellCenter.y() - filterCenter.y()),                             mag(currentCellCenter.z() - filterCenter.z()) ) );             filter[iCellCenterField] = (( normInfty <= filterRadius ) ? 1. : 0.);         }         // Compute filter cell volume         filterCellVolume = fvc::domainIntegrate(filter * cellVolumeField).value();         // Compute average velocity         UBarList[iFilterCenterList] = fvc::domainIntegrate(filter * cellVolumeField * U).value() / filterCellVolume;         //Info<< "UBar = " << UBarList[iFilterCenterList] << endl << endl;         } // Loop over filter center coordinates     // Output the UBar field to a custom-format ASCII file     // (please note that the call for sqrt is ambiguous due to the damn     // using namespace statement)     // TODO (low priority): modify output directory cleanly (current solution is hack)     // TODO (low priority): add progression indicator     fileName outputFile("postProcessing/UBar.csv");     Info<< "outputFile = " << outputFile << endl;     OFstream os(outputFile);     os << "\"x\",\"y\",\"z\",\"UBarX\",\"UBarY\",\"UBarZ\",\"UBarMag\"" << endl;     forAll(filterCenterList, iFilterCenterList)     {         os  << filterCenterList[iFilterCenterList][0] << ","             << filterCenterList[iFilterCenterList][1] << ","             << filterCenterList[iFilterCenterList][2] << ","             << UBarList[iFilterCenterList][0] << ","             << UBarList[iFilterCenterList][1] << ","             << UBarList[iFilterCenterList][2] << ","             << Foam::sqrt(  sqr(UBarList[iFilterCenterList][0])                           + sqr(UBarList[iFilterCenterList][1])                           + sqr(UBarList[iFilterCenterList][2])                         )             << endl;         if ( (iFilterCenterList+1)%nX == 0 ) os << endl; // Useful for gnuplot pm3d     } }
This is pretty slow (very slow, actually) but it works fairly well. See the PDF attached for a Gnuplotted filtered velocity field associated with the previously mentioned case.

Handling multiprocessor cases is critical, and I found the solution there:
http://www.cfd-online.com/Forums/ope...ral-mag-u.html

I believe there's a bottleneck at the filter setting operation, which is not parallelized.

 leroyv March 6, 2014 13:56

Use of LES filters?

Dear foamers,

Does anyone know if an LES filter wouldn't be suitable to do this? I can't exactly understand what the simpleFilter, laplaceFilter, and such, do.

 sebas October 9, 2014 03:16

OK. I see this thread is rather old but still...

I implemented some explicit filtering/moving average, too. It works in parallel but takes ages. And edge effects are also an issue with this filtering stuff.

So here's what I'm doing now:

In k-space (wave number), the frequency response of a moving average filter (rectangular impulse response) is a sinc function:

<phi> = sin(L/2*k)/(L/2*k) phi

for a generic variable phi, filter length L and brackets <> denoting filtering.

you can express this as an infinite series:

sinc(L/2*k) = sum_n [L/2*(i*k)]^(2*n) /[(2*n+1)!]

for n from zero to infinity.

Note that (i*k)^2 is essentially the Laplacian.

Now let's write this in a recursive manner:
At iteration m
<phi>_m = <phi>_(m-1) + (L/2)^2/[2*m*(2*m+1)] Laplacian[<phi>_(m-1)]

This is basically the heat equation with a time-dependent diffusivity (tau is the numerical time step)

d<phi>/dt = (L/2)^2/tau /[2t/tau*(2t/tau+1)] Laplacian[<phi>]

Actually, from the series expansion we get Explicit Euler time integration which is numerically unstable unless Diffusion number is very small.

Although it's mathematically not that sound, I just use implicit time integration here.

Remember this is "pseudo time". You have to run this until some steady state at every real time step. But Laplace Equation is easy to solve and as the diffusivity decreases with time it should converge rather quickly.

Edit:

This derivation is for 1D. Physically it makes sense, I think. But I'd be glad if someone could comment on the multidimensional problem...

 leroyv October 9, 2014 11:01

Hi Sebastian,

This way of doing things seems quite elegant to me. However, there are a few things that remain unclear to me. I'm going to do a more detailed development of the calculus, using proper math notations for more clarity. That will help me explain where the things I don't understand are.

We start with a given field we want to get the average of using what I'll call the top-hat filter of width :

and 0 otherwise. Note that this filter is the first of a family of recursively defined filters that have interesting properties regarding control of the regularity of the resulting averaged field (see Angeli et al., 2013).

We get the average field by applying the filter using a convolution operator. 's expression in is:

Now, we go to the phase space by applying the Fourier transform operator :

where is the wave vector.

The only problem-independant thing we have here is the Fourier transform of the filter. As you mentioned, the Fourier transform of is a sinc:

and its Taylor series expansion is

This gives us the following expression for :

where

For there, we can move to a recursive definition of the sequence:

with the initial condition

Finding the limit of the sequence would clearly give us , indeed. What I don't understand at this point is how you proceed to the laplacian-based formulation you provide: the product meaning "apply laplacian" only works if you go back to the real space, in which the recursive definition doesn't work. Plus, we don't have in the recursive definition; only , a lot less convenient. Did I miss something?

I've also been trying to write this for a 2D problem, but I couldn't figure out how to get to a recursive expression for the Taylor series terms, mostly because the Fourier transform of the 2D filter is a product of two Taylor series (I'm not a good mathematician).

 All times are GMT -4. The time now is 05:43.