CFD Online Discussion Forums

CFD Online Discussion Forums (https://www.cfd-online.com/Forums/)
-   Fluent UDF and Scheme Programming (https://www.cfd-online.com/Forums/fluent-udf/)
-   -   calculate distance from wall to neighbourig cell centroid (https://www.cfd-online.com/Forums/fluent-udf/63211-calculate-distance-wall-neighbourig-cell-centroid.html)

rr123 April 1, 2009 04:24

calculate distance from wall to neighbourig cell centroid
 
dear all,

i need to calculate the distance between the wall and the centroid of the cell touching this wall. my system is 2D.

this is the main code snippet:

begin_c_loop (c,t)
{
c_face_loop(c,t,n)
{
tf = C_FACE_THREAD(c,t,n);
if(THREAD_TYPE(tf) == THREAD_F_WALL)
{
f = C_FACE(c,t,n);
F_CENTROID(x,f,t);
wf = x[1]; /* for 2D */
C_CENTROID(cell,c,t);
nc = cell[1];
dist = FABS(wf-nc);
}
}
}
end_c_loop (c,t)


am i right ? is it correct ordering of the loop structure ?

hopign for a reply.

best regards.

HenrikS April 1, 2009 06:37

I think your code will do what you want, yes. If you have any problems with
if(THREAD_TYPE(tf) == THREAD_F_WALL)
you might consider using
if (BOUNDARY_FACE_THREAD(tf))
although I guess they would work equally well.

Anyway, you could use C_WALL_DIST(c,t) directly instead of looping, I guess?

/Henrik

rr123 April 1, 2009 06:41

dear henrik,

i didnt get you clearly with the using of the C_WALL_DIST(c,t).

can you remake the code snippet with your suggestion so that i can try from my end.

sincerely.

HenrikS April 1, 2009 07:07

In your code, you calculate "dist" for every cell next to a wall boundary. My guess is that you want to use this value for some purpose elsewhere in your code. Now, there is a macro called C_WALL_DIST(c,t) which I believe (although I have not verified that) will give you this distance directly.

In your case, your snippet would become:

begin_c_loop (c,t)
{
dist = C_WALL_DISTANCE(c,t);
}
end_c_loop (c,t)

This is probably not only shorter but also quicker. Anyway, I suggest you validate this approach before relying on it. You can store "dist" in a UDM and compare to the cell wall distance in a contour plot in Fluent for example.

/Henrik

rr123 April 1, 2009 07:47

dear henrik,

thanks for the reply. so you mean that the c_face_loop which i used, is no more required with your code snippet using C_WALL_DIST(c,t) ?

one more query:
i want to add a source term, S_epsilon in the epsilon eqn in the k-epsilon model. which DEFINE macro must i choose ?

many thanks in advance for the help.
best regards.

HenrikS April 1, 2009 07:56

It was quite some time ago since I worked with wall distances in UDFs, but I seem to remember that the distance between a cell centre and the nearest wall should be available in C_WALL_DISTANCE(c,t). (As you can see, it is available for plotting under Grid...):

"Cell Wall Distance is the distribution of the normal distance of each cell centroid from the wall boundaries. Its unit quantity is length. "

Anyway, if this is what C_WALL_DISTANCE(c,t) returns, then all you need is the cell thread and index (c and t) to obtain it, i.e. no need to loop over faces and compare coordinates yourself.

To add a source term for the epsilon equation (to use inside your domain), write a DEFINE_SOURCE macro and hook it up under the source terms for the boundary conditions of the fluid zone.

Hope this helps!

/Henrik

HenrikS April 1, 2009 07:58

You would also have to add some kind of check to see whether the current cell is next to a boundary, if you are only interested in boundary cells (which seems the case in your first post).

rr123 April 1, 2009 08:05

yes, Henrik you are righ indeed. my only intention is to calculate the distance from the centroid of the cell which touches the wall.
so thats why i intended to use the THREAD_TYPE macro.

so if i use C_WALL_DIST(c,t) macro, what should i write to implement that check ?

sincerely.

HenrikS April 1, 2009 08:11

How about

begin_c_loop(c, t)
{
dist = 0.0;
c_face_loop(c, t, n)
{
if (BOUNDARY_FACE_THREAD_P(C_FACE_THREAD(c,t,n)))
{
dist = C_WALL_DIST(c,t);
}
}
if (dist > 0.0)
{
/* do whatever it is you wish to do with dist here... */
/* if dist = 0.0 then this cell does not touch the boundary */
}
}
end_c_loop(c, t)

or something like that... :-)

/Henrik

rr123 April 1, 2009 08:13

henrik, thanks again.

but how do we know that BOUNDARY_FACE_THREAD is the wall ??

sincerely.

HenrikS April 1, 2009 08:23

Hmm... BOUNDARY_FACE_THREAD_P(t) expands to a function that returns TRUE if Thread *t is a boundary face thread. I don't know for sure, maybe this would also include inlets and outlets. Then maybe stick with your initial code or supply an array containing the wall thread IDs, whatever you prefer.

coglione April 2, 2009 03:16

Hello,

there is a macro THREAD_TYPE(tf) which returns an integer for each boundary face thread tf. If the boundary face is a wall this number will be "3"
--> if (THREAD_TYPE(tf)) == 3) can be used to check wether the boundary is a wall.

cheers

rr123 April 2, 2009 03:32

hi,

is

if (THREAD_TYPE(tf)) == 3)

the same as

if(THREAD_TYPE(tf) == THREAD_F_WALL)

thanks very much for your reply.

sincerely.

HenrikS April 2, 2009 03:42

Yes, according to threads.h:

THREAD_F_WALL = 3,

so those should be the same.

rr123 April 2, 2009 05:27

thanks Henrik and coglione !!

i have two small queries:

i am trying to add a secondary source term, S to the epsilon eqn of the k-epsilon model.
S = max[0.83(l/l_e)(epsilon/k),0]
where, l tubulence length scale and l_e is the is the near-wall equilibrium length scale.
i am using the DEFINE SOURCE(name,c,t,dS,eqn) macro.

so the query is do i need to define my dS to be ds[eqn] = 0.0 or shall i need to assign an expresison to it ?
i read the UDF manual, but is not clear for me.

and if i need to assign an expresison to dS, then with what should i
differetiate S. With l or with l_e ? i mean, what is the dependent variable in the above for S ?

thanks for your reply.

HenrikS April 2, 2009 06:18

You do not need to specify an expression for dS, you can set it to 0.0. If you have convergence issues, consider adding an expression for dS. As I understand it, dS should then be the derivative with respect to epsilon?

coglione April 2, 2009 07:48

Yes, the dependent variable here is epsilon as the source is defined for this quantity. in this specific case a linearization of the source term will not add stability because this is only true if dS/depsilon is negativ which will never be possible since the definition of source as >0.0 ! Thus dS = 0.0 is the only possibility and FLUENT automatically does so if you do not specify dS at all.

cheers

rr123 April 2, 2009 10:25

thanks for helping me henrik and max !
i gratefully acknowledge your support.
i keep you informed of this.

rr123 April 3, 2009 10:39

dear henrik and max,

why should the S (in my case) be differentiated w.r.t epsilon ?
i mean, what is the reason behind it ??

because, in some examples i find that for modifying a source term in the omega equation (of k-omega model), the S is differentiated w.r.t. x-velocity.

pls help me catch what i am failing to understand.

kind regards.

Mohamed Refaat January 7, 2015 16:56

Quote:

Originally Posted by HenrikS (Post 211557)
I think your code will do what you want, yes. If you have any problems with
if(THREAD_TYPE(tf) == THREAD_F_WALL)
you might consider using
if (BOUNDARY_FACE_THREAD(tf))
although I guess they would work equally well.

Anyway, you could use C_WALL_DIST(c,t) directly instead of looping, I guess?

/Henrik

dear Henrik
i have the same problem of rr123
when i use this macro C_WALL_DIST(c,t) this message appeared

D:\PROGRA~1\ansys\ANSYSI~1\v145\fluent\fluent14.5. 0\win64\3d\fl1450s.exe received fatal signal (ACCESS_VIOLATION)
1. Note exact events leading to error.
2. Save case/data under new name.
3. Exit program and restart to continue.
4. Report error to your distributor.

so please do you have a solution for this?

HenrikS January 8, 2015 05:22

That error message most probably tells you that C_WALL_DIST(c,t) has not been assigned a value (so that you receive an access violation when you try to retrieve it). I haven't worked with this macro for many years now, but I would seem to recall that it is mainly used in conjunction with near-wall turbulence modelling, so perhaps the Viscous model of choice may influence whether it is used by the solver or not. You might also want to try to plot the cell wall distance and/or the cell coordinates in a graphical window to make sure that the appropriate loops have been performed.

macfly January 8, 2015 15:52

After a search in the UDF manual it seems that C_WALL_DIST doesn't exist anymore. I'd be curious to know how we can retrieve the distance between a cell centroid and the nearest wall.

HenrikS January 9, 2015 03:56

I don't think that these macros were ever covered by the manual. I just had a look in the source files for Fluent 15.0.7 and all of the following are there still:
C_WALL_DIST(c,t)
C_WALL_DIST_G(c,t)
C_WALL_DIST_RG(c,t)
C_WALL_NORMAL(c,t)
However, depending on your situation it might be "safer" to construct your own macro. All you need then is a UDM and to loop once and use the standard (and well documented) macros for cell and face centroid locations, plus the aforementioned boolean macro to judge if the cell is next to a boundary.
Hope this helps!

Mohamed Refaat January 9, 2015 13:24

Thank you very much HenrikS

wwhblue March 19, 2015 07:28

Define_wall_function
 
Quote:

Originally Posted by Mohamed Refaat (Post 526787)
Thank you very much HenrikS

:confused:

wwhblue March 19, 2015 07:30

Udf DEFINE_WALL_FUNCTION
 
Quote:

Originally Posted by HenrikS (Post 526695)
I don't think that these macros were ever covered by the manual. I just had a look in the source files for Fluent 15.0.7 and all of the following are there still:
C_WALL_DIST(c,t)
C_WALL_DIST_G(c,t)
C_WALL_DIST_RG(c,t)
C_WALL_NORMAL(c,t)
However, depending on your situation it might be "safer" to construct your own macro. All you need then is a UDM and to loop once and use the standard (and well documented) macros for cell and face centroid locations, plus the aforementioned boolean macro to judge if the cell is next to a boundary.
Hope this helps!

:confused:
dear HenrikS,
i need the turbulece production at the near wall cell, but i don't know which UDF macro to use.
pls help me.

kind regards.

`e` March 19, 2015 15:47

Turbulent kinetic energy: C_K(c,t) and turbulent kinetic energy dissipation rate: C_D(c,t).

HenrikS March 20, 2015 03:38

There's an undocumented macro called that might be useful (see below). However, I wouldn't recommend using something like this unless you know what you're doing. The safest bet is probably to define your own macro, then you will know exactly what you are calculating :). Good luck!

Get_Wall_k_Prod(face_t f, Thread *t, cell_t c0, Thread *t0,
real ks, real rkcon, real yp, real uStar, real up,
real rho, real mu,
real alpha, real beta, real gamma, real vwPlus,
int ictyp);


pratikddhoot April 11, 2016 11:26

Quote:

Originally Posted by rr123 (Post 211540)
dear all,

i need to calculate the distance between the wall and the centroid of the cell touching this wall. my system is 2D.

this is the main code snippet:

begin_c_loop (c,t)
{
c_face_loop(c,t,n)
{
tf = C_FACE_THREAD(c,t,n);
if(THREAD_TYPE(tf) == THREAD_F_WALL)
{
f = C_FACE(c,t,n);
F_CENTROID(x,f,t);
wf = x[1]; /* for 2D */
C_CENTROID(cell,c,t);
nc = cell[1];
dist = FABS(wf-nc);
}
}
}
end_c_loop (c,t)


am i right ? is it correct ordering of the loop structure ?

hopign for a reply.

best regards.

Hi,
How do you find out the closest wall in a 2D model?

pratikddhoot April 13, 2016 16:40

Quote:

Originally Posted by HenrikS (Post 211568)
In your code, you calculate "dist" for every cell next to a wall boundary. My guess is that you want to use this value for some purpose elsewhere in your code. Now, there is a macro called C_WALL_DIST(c,t) which I believe (although I have not verified that) will give you this distance directly.

In your case, your snippet would become:

begin_c_loop (c,t)
{
dist = C_WALL_DISTANCE(c,t);
}
end_c_loop (c,t)

This is probably not only shorter but also quicker. Anyway, I suggest you validate this approach before relying on it. You can store "dist" in a UDM and compare to the cell wall distance in a contour plot in Fluent for example.

/Henrik

Dear Henrik,

By using C_WALL_DISTANCE(c,t); as a macro, FLUENT fails to initialize the solution. I do not understand what is the error in this.

Mohamed Refaat April 14, 2016 18:25

I have the same problem too.

pratikddhoot April 18, 2016 10:36

Quote:

Originally Posted by rr123 (Post 211737)
thanks for helping me henrik and max !
i gratefully acknowledge your support.
i keep you informed of this.

Has your code worked (interpreted without errors and solution initialized without errors)?
If yes, Do you mind sharing the wall distance part of it. I am struggling to find the wall distance from a cell (centroid).

Mohamed Refaat April 18, 2016 17:21

Unfortunately, the macros
C_WALL_DIST(c,t)
C_WALL_DIST_G(c,t)
C_WALL_DIST_RG(c,t)
C_WALL_NORMAL(c,t)
did not worked in my code so that, I have used a constant cell wall distance when generating the grid by using the inflation technique in the grid generation near the wall.
I wish I could help you

alinik December 19, 2016 17:34

Mohamed,

This is because fluent recently has removed C_WALL_DIST from its macros and for that reason we cannot use it anymore.
Do you by any chance have found a solution for this? I am having the same problem and I cannot find an alternate solution.
Any kind of help is much appreciated

Mohamed Refaat December 21, 2016 09:44

Quote:

Originally Posted by alinik (Post 630483)
Mohamed,

This is because fluent recently has removed C_WALL_DIST from its macros and for that reason we cannot use it anymore.
Do you by any chance have found a solution for this? I am having the same problem and I cannot find an alternate solution.
Any kind of help is much appreciated

Dear Ali,

I suggest using a constant height of an inflation layer near the wall, so that you knows the height of the first cell centroid and it is also constant. you can now use it in your application.


Another solution in to know the thread ID and use the macro
C_CENTROID(c,f,t);
y = x[1]; to get the y-distance for each cell

alinik December 21, 2016 11:36

1 Attachment(s)
Thank you Mohamed,

You mean in the equations for F1 and F2 as you see in this picture, you used a constant value for y? and that constant value is the thickness of the first layer?

Mohamed Refaat December 22, 2016 12:52

the value in the equations are the first cell centroid distance from the wall, isn't it?

So, you can use a constant value or the second method to get every distance for the wall adjacent cells for non-uniform grids near the wall


Hope it will help

alinik December 23, 2016 02:53

Quote:

Originally Posted by Mohamed Refaat (Post 630979)
the value in the equations are the first cell centroid distance from the wall, isn't it?

I am not sure. Are you sure about this?

alinik March 13, 2017 13:34

Mohamed,

I recently got in touch with a person who is an expert in Fluent UDF and he told me that C_WALL_DIST(c,t) is still available. It just has to be filled. He sent me this code and said that it contains one way that this macro could be filled.
I do not understand what this code is doing and how I can fill it and then use it.
Do you mind taking a closer look at this code?

#include "udf.h"
#include "prox.h"

static cxboolean wall_dist_set = FALSE;

DEFINE_ON_DEMAND(set_wall_dist_udm0)
{
#if !RP_HOST

Domain *domain;
Thread *t;
cell_t c;

if (! wall_dist_set)
{
domain = Get_Domain(ROOT_DOMAIN_ID);

Alloc_Storage_Vars(domain, SV_RTMP_0, SV_NULL);

Calc_Cell_Wall_Distance_New(domain, SV_RTMP_0);

thread_loop_c(t,domain)
{
begin_c_loop(c,t)
{
C_UDMI(c,t,0) = C_TMP0(c,t);
}
end_c_loop(c,t)
}

wall_dist_set = TRUE;
}
#endif /* !RP_HOST */
}

DEFINE_ON_DEMAND(reset_udm0)
{
#if !RP_HOST

Domain *domain;
Thread *t;
cell_t c;

domain = Get_Domain(ROOT_DOMAIN_ID);

thread_loop_c(t,domain)
{
begin_c_loop(c,t)
{
C_UDMI(c,t,0) = 0.0;
}
end_c_loop(c,t)
}

#endif /* !RP_HOST */

wall_dist_set = FALSE;
}

HenrikS March 14, 2017 01:41

Hi,

The code in question will introduce two Define-on-Demand functions:
set_wall_dist_udm0
reset_udm0
that use the user-defined memory location 0 to store the wall distances.

The second one will initialize UDM0 to zero everywhere, the first one will use an undocumented built-in function
Calc_Cell_Wall_Distance_New
to fill it with the wall distance.

I don't know whether this works in practice, but it looks fine. Don't forget to activate the user-defined memory or the code will crash for you :)

/Henrik


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