CFD Online Discussion Forums

CFD Online Discussion Forums (https://www.cfd-online.com/Forums/)
-   OpenFOAM Programming & Development (https://www.cfd-online.com/Forums/openfoam-programming-development/)
-   -   Spatial average over a plane (https://www.cfd-online.com/Forums/openfoam-programming-development/116307-spatial-average-over-plane.html)

michel1988 April 16, 2013 18:44

Spatial average over a plane
 
Hello,
I want to calculate the average of a physical quantity over a plane in the computational domain during the runtime (not for postprocessing).
For example: the average of pressure over the plane z=constant for a cubic computational domain.
Anyone who can tell me how to do it?:confused: It should be easy to realize this function, but I am new to OpenFOAM and I am not familiar with it.
Thanks.

fredo490 April 16, 2013 22:57

It is quite easy:
- identify your patch
- loop over all the faces of your patch
- make your calculation

Your code can look like this:
Code:

label patchWall = mesh.boundaryMesh().findPatchID("wall"); //patchID = id of the patch wall
const fvPatch& cPatch = mesh.boundary()[patchWall];

    forAll(cPatch, facei) //facei = id of the face
    {
      // Make whatever you want
    }


michel1988 April 17, 2013 07:33

Thanks for your reply. Your code could be used to calculate the average on the boundary, but I am not sure if it is available for a given plane inside the computational domain. The plane over which the average is calculated is not necessarily a boundary.

fredo490 April 17, 2013 07:37

Oh ok, then my code only works if you build the mesh with an interface.

Do you need your treatment in the solver ? Or do you simply need the value at the end of each iteration ?

In the second case, you can use a sampling function just like the lift and drag function. You can include it inside the Control file and it will write the result of your computation in a dedicated file as you run the simulation.

timo_IHS April 17, 2013 07:47

Have a look for swak4foam this could work

JR22 April 17, 2013 10:37

Runtime integration/averaging on a computational plane (NOT inlet/outlet patch)
 
I have the same question. I would greatly appreciate it if somebody can illustrate how to do this with some level of detail. michel1988, If you do come up with the answer (maybe with the guidance of fredo and Timo), please come back and leave the solution for others to learn from it. I have done this only in post-processing by resampling and integration using paraview. However, this is prone to errors, since no matter how fine you make your meshes (both model and measurement plane), the measurement errors persist (In my model example, I get that Ux at entry is 5% greater than Ux at exit from the duct). The resampling has to interpolate from one mesh to another and it basically sucks.

fredo490 April 17, 2013 13:27

Did you try to play with the interpolation scheme of the sampling functions ? Some are more accurate than others.

Also, can you give more details about your case ? Maybe a picture of the mesh ? It would help us to find a technique suitable for your case.

And by the way, why do you want to do it at each time step ? To check the Mass Balance in your domain ?

michel1988 April 18, 2013 10:20

Sorry for the unclear description of my problem. I just want to calculate the spatial average over the directions of homogeneity. For example, the streamwise and spanwise directions in channel flow. I want to employ the averaged information to a turbulence model. That why I need to do it at each time step.
Thanks

fredo490 April 18, 2013 11:16

Is it a structured of unstructured mesh ?
I know there is a command to find the closest cell center from a given point but the algorithm you have to build is a bit complicated. If it is a structured mesh, you can try to loop over the neighbors cells and make a displacement over "i" or "j".

michel1988 April 22, 2013 08:28

The mesh is generated by blockMesh in OpenFOAM. So I think it should be unstructured mesh.

gschaider April 23, 2013 19:43

Quote:

Originally Posted by michel1988 (Post 421556)
Sorry for the unclear description of my problem. I just want to calculate the spatial average over the directions of homogeneity. For example, the streamwise and spanwise directions in channel flow. I want to employ the averaged information to a turbulence model. That why I need to do it at each time step.
Thanks

"Doing at every timestep": the answer is usually a functionObject.

Somebody else in this thread already suggest swak4Foam. But I'll do it again: the snipplet in the following example for instance gives you the area-weighted average of the z-component of the velocity on a plane.
Code:

    surfacePlane
    {
        type swakExpression;
        valueType surface;
        surfaceName testPlane;
        surface {
            type plane;
            basePoint      (0.0001 0.0001 0.0001);
            normalVector    (0 0 1);
            interpolate false;
        }
        verbose true;
        expression "U.z";
        accumulations (
            min
            max
            weightedAverage
        );
    }

(this is just an excerpt from the controlDict)

kentriarch April 25, 2013 00:02

Quote:

Originally Posted by michel1988 (Post 421556)
Sorry for the unclear description of my problem. I just want to calculate the spatial average over the directions of homogeneity. For example, the streamwise and spanwise directions in channel flow. I want to employ the averaged information to a turbulence model. That why I need to do it at each time step.
Thanks

Hi, I have the same issue as you. Am still looking for a solution to this problem of doing spatial average and wanting to implement this into a turbulence model.

Would you be, by any chance, working on a model that requires the mean strain in the x-y plane at all z levels?

michel1988 April 25, 2013 03:23

Not exactly but similarly. Although this kind of spatial average may not be necessary (sometimes it is possible to use time average instead), it is better when the flow is homogeneous in the specific directions.

kentriarch April 25, 2013 04:37

Quote:

Originally Posted by gschaider (Post 422714)
"Doing at every timestep": the answer is usually a functionObject.

Somebody else in this thread already suggest swak4Foam. But I'll do it again: the snipplet in the following example for instance gives you the area-weighted average of the z-component of the velocity on a plane.
Code:

    surfacePlane
    {
        type swakExpression;
        valueType surface;
        surfaceName testPlane;
        surface {
            type plane;
            basePoint      (0.0001 0.0001 0.0001);
            normalVector    (0 0 1);
            interpolate false;
        }
        verbose true;
        expression "U.z";
        accumulations (
            min
            max
            weightedAverage
        );
    }

(this is just an excerpt from the controlDict)

Yes, I understand that this snippet will give an output of the average of U.z of the xy-plane at z=0.0001. But how can I reference to these values during runTime when solving?

Like michel1988, I am looking to implement this spatial averaging within a turbulence model. How would I go about invoking this function within the turbulence file? Also, since I wish to get the spatial average at every z level, if there a way to let the basepoint definition vary dynamically?

Thanks

gschaider April 25, 2013 11:40

Quote:

Originally Posted by kentriarch (Post 423025)
Yes, I understand that this snippet will give an output of the average of U.z of the xy-plane at z=0.0001. But how can I reference to these values during runTime when solving?

It said nothing about "reference" in the original posting. Only later there was something about using that value in a turbulence model.

Please be more specific about "reference". Only then can I tell you what might work (there ARE ways to access swak-results from the solver. The question is whether it is worth the while for your application)

Quote:

Originally Posted by kentriarch (Post 423025)
Like michel1988, I am looking to implement this spatial averaging within a turbulence model. How would I go about invoking this function within the turbulence file? Also, since I wish to get the spatial average at every z level, if there a way to let the basepoint definition vary dynamically?

Thanks

Well. You might want to take a look at the sampledPlane in Doxygen and figure out how to use that in your program. You'll have to create a new plane for every basepoint

kentriarch April 26, 2013 00:27

Quote:

Originally Posted by gschaider (Post 423162)
It said nothing about "reference" in the original posting. Only later there was something about using that value in a turbulence model.

Please be more specific about "reference". Only then can I tell you what might work (there ARE ways to access swak-results from the solver. The question is whether it is worth the while for your application)



Well. You might want to take a look at the sampledPlane in Doxygen and figure out how to use that in your program. You'll have to create a new plane for every basepoint

In the turbulence model that I am trying to implement, I need to get the plane average for the strain tensor, <S_ij> at every z level.

That is to say at z = 1, I need to get the averages of S_xy, S_xz, S_yz etc at this level and repeat for all z.

Then, having obtained the plane average tensor, I will use each strain to compute the respective tau_ij. I will also be using the double inner product of <S_ij> to obtain a scalar parameter.

Hence, I first need a runTime function that can do this plane average, then I will need to be able to use the calculated values in the turbulence model at each time step. So when I said "reference", I meant accessing/using the output of the plane averaging function.

Yes, thanks for the tip of looking into sampledPlane. I am working on generating the planes at every basepoint.

gschaider April 26, 2013 05:10

Quote:

Originally Posted by kentriarch (Post 423231)
In the turbulence model that I am trying to implement, I need to get the plane average for the strain tensor, <S_ij> at every z level.

That is to say at z = 1, I need to get the averages of S_xy, S_xz, S_yz etc at this level and repeat for all z.

Then, having obtained the plane average tensor, I will use each strain to compute the respective tau_ij. I will also be using the double inner product of <S_ij> to obtain a scalar parameter.

Hence, I first need a runTime function that can do this plane average, then I will need to be able to use the calculated values in the turbulence model at each time step. So when I said "reference", I meant accessing/using the output of the plane averaging function.

Yes, thanks for the tip of looking into sampledPlane. I am working on generating the planes at every basepoint.

As the quantity you want to sample is fixed the flexibility of swak4foam is a bit of an overkill here (a bit like using dynamite for going after moles: a lot of fun, but the garden looks messy afterwards and the neighbours complain and usually the moles survive). So I'd go for using samplePlanes inside your turbulence model if you need that kind of stuff

Z.Q. Niu November 30, 2014 02:41

Hi,HECKMANN,
your code is used for averaging based on area, yes? Do you know how to average based on point value?


All times are GMT -4. The time now is 19:04.