CFD Online Logo CFD Online URL
www.cfd-online.com
[Sponsors]
Home > Forums > Software User Forums > ANSYS > FLUENT > Fluent UDF and Scheme Programming

UDF function for grid spacing

Register Blogs Community New Posts Updated Threads Search

Like Tree2Likes
  • 1 Post By pakk
  • 1 Post By pakk

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
Old   February 6, 2014, 15:22
Default UDF function for grid spacing
  #1
Member
 
Ali.E
Join Date: Sep 2010
Location: Lisboa
Posts: 83
Rep Power: 16
behest is on a distinguished road
Dear all,
I want to write a UDF for calculating the grid spacing in 3D. Actually, I want to campute delta_x,delta_y and delta_z for each cell. My study case is a 3 dimension problem around a wing. my quastion is which function should be used? How can I write this udf?

(delta_x means the length of a cell across x direction)
behest is offline   Reply With Quote

Old   February 6, 2014, 15:33
Default
  #2
Senior Member
 
Join Date: Nov 2013
Posts: 1,965
Rep Power: 27
pakk will become famous soon enough
Quote:
Originally Posted by behest View Post
Dear all,
I want to write a UDF for calculating the grid spacing in 3D. Actually, I want to campute delta_x,delta_y and delta_z for each cell. My study case is a 3 dimension problem around a wing. my quastion is which function should be used? How can I write this udf?

(delta_x means the length of a cell across x direction)
I think you have an XY problem.
It is hard to imagine that the final output that you are interested in is the cell lengths. Probably you want to use this to calculate something different. (A gradient?)

You should explain what you want in the end. Not just to please me, but because the answer depends on what you want. Maybe you can avoid these calculations. Or if you really need them, what you need them for determines how you should deal with triangular meshes etc.
pakk is offline   Reply With Quote

Old   February 7, 2014, 06:33
Default
  #3
Member
 
Ali.E
Join Date: Sep 2010
Location: Lisboa
Posts: 83
Rep Power: 16
behest is on a distinguished road
Thank you for your response.
Acutally, I want to compute length scale of DES model and the formulation is given as:

L_s=sqrt(k)/(Bs*delta_max*C_des*w)
Bs=0.09 , C_des=0.61
k=turbulent kinitic energy
w=specific dissipation rate
delta_max=max(delta_x,delta_y,delta_z)

To honest, I am trying to write a UDF that computes this function.

Quote:
Originally Posted by pakk View Post
I think you have an XY problem.
It is hard to imagine that the final output that you are interested in is the cell lengths. Probably you want to use this to calculate something different. (A gradient?)

You should explain what you want in the end. Not just to please me, but because the answer depends on what you want. Maybe you can avoid these calculations. Or if you really need them, what you need them for determines how you should deal with triangular meshes etc.
behest is offline   Reply With Quote

Old   February 7, 2014, 06:59
Default
  #4
Senior Member
 
Join Date: Nov 2013
Posts: 1,965
Rep Power: 27
pakk will become famous soon enough
Then I guess, from a physical point of view, what you really need is the maximum distance between two points in a cell. Since cells are convex, this is always between two nodes of that cell, so you could loop over all nodes.
Something like this:

Code:
 DEFINE_ON_DEMAND(calculate_grid_distance)
 {
  Domain *domain=Get_Domain(ROOT_DOMAIN_ID);
  Thread *c_thread;
  cell_t c;
  Node *node;
  int n;
 
  thread_loop_c(c_thread, domain) /*loops over all cell threads in domain*/
  {
    begin_c_loop(c, c_thread) /* loops over cells in a cell thread */
    {
      c_node_loop(c,t,n)
      {
        node = C_NODE(c,t,n);
        /* x position is NODE_X(node), 
           y position is NODE_Y(node), 
           z position is NODE_Z(node); 
           do some trick here to calculate distances 
           between all nodes of a cell and store the 
           maximum distance in a UDM 
           (C_UDMI(c,t,0)=maxdistance)*/
      }       
    }
    end_c_loop(c, c_thread) 
   } 
 }
This code is not finished, and therefore not tested, it is just a hint in the right direction.
behest likes this.
pakk is offline   Reply With Quote

Old   February 7, 2014, 09:32
Default
  #5
Member
 
Ali.E
Join Date: Sep 2010
Location: Lisboa
Posts: 83
Rep Power: 16
behest is on a distinguished road
Thanks alot for the code. I have another question, how can I compute the distance? I mean that I have x,y,z coordinates of a node whereas I should have previous node coordinates too. In the cell node loop, I give just only one node coordinates. how does UDF save previous node coordinates?

Quote:
Originally Posted by pakk View Post
Then I guess, from a physical point of view, what you really need is the maximum distance between two points in a cell. Since cells are convex, this is always between two nodes of that cell, so you could loop over all nodes.
Something like this:

Code:
 DEFINE_ON_DEMAND(calculate_grid_distance)
 {
  Domain *domain=Get_Domain(ROOT_DOMAIN_ID);
  Thread *c_thread;
  cell_t c;
  Node *node;
  int n;
 
  thread_loop_c(c_thread, domain) /*loops over all cell threads in domain*/
  {
    begin_c_loop(c, c_thread) /* loops over cells in a cell thread */
    {
      c_node_loop(c,t,n)
      {
        node = C_NODE(c,t,n);
        /* x position is NODE_X(node), 
           y position is NODE_Y(node), 
           z position is NODE_Z(node); 
           do some trick here to calculate distances 
           between all nodes of a cell and store the 
           maximum distance in a UDM 
           (C_UDMI(c,t,0)=maxdistance)*/
      }       
    }
    end_c_loop(c, c_thread) 
   } 
 }
This code is not finished, and therefore not tested, it is just a hint in the right direction.
behest is offline   Reply With Quote

Old   February 7, 2014, 09:40
Default
  #6
Senior Member
 
Join Date: Nov 2013
Posts: 1,965
Rep Power: 27
pakk will become famous soon enough
The UDF does not save the previous coordinates yet, this part you have to add
pakk is offline   Reply With Quote

Old   February 7, 2014, 11:51
Default
  #7
Member
 
Ali.E
Join Date: Sep 2010
Location: Lisboa
Posts: 83
Rep Power: 16
behest is on a distinguished road
yes, thank you very much, but counter of the loop is the main problem, each cell has several nodes and in a cell node loop, I want to save the the coordinates, but i do not know what is the counter (???). I am beginer in C program and I have worked with Fortran.

c_node_loop(c,t,n) {
node = C_NODE(c,t,n);
x[???]=NODE_X(node);
y[???]=NODE_Y(node);
z[???]=NODE_Z(node);
}


Quote:
Originally Posted by pakk View Post
The UDF does not save the previous coordinates yet, this part you have to add
behest is offline   Reply With Quote

Old   February 7, 2014, 12:15
Default
  #8
Senior Member
 
Join Date: Nov 2013
Posts: 1,965
Rep Power: 27
pakk will become famous soon enough
Quote:
Originally Posted by behest View Post
yes, thank you very much, but counter of the loop is the main problem, each cell has several nodes and in a cell node loop, I want to save the the coordinates, but i do not know what is the counter (???). I am beginer in C program and I have worked with Fortran.
There is no counter yet, if you want one then you have to add it.


Code:
 int counter;
 float x[20],y[20],z[20]; //assuming each cell has 20 nodes or less
  
 ...
  
 counter=0;
 c_node_loop(c,t,n) {
  node = C_NODE(c,t,n);
  x[counter]=NODE_X(node);
  y[counter]=NODE_Y(node);
  z[counter]=NODE_Z(node);
  ++counter;
 }
This still does not calculate distances, you still have to add it.
pakk is offline   Reply With Quote

Old   February 7, 2014, 14:36
Default
  #9
Member
 
Ali.E
Join Date: Sep 2010
Location: Lisboa
Posts: 83
Rep Power: 16
behest is on a distinguished road
Thank you very much. I have just written the UDF as below:

/* This UDF is used to compute relative length scale in DES model*/

#include "udf.h"

DEFINE_ON_DEMAND(rls)
{
Domain *domain=Get_Domain(ROOT_DOMAIN_ID);
Thread *c_thread;
cell_t c;
face_t f;
Thread *tf;
Node *node;
int n,k,counter;
real delx,dely,delz,deltamax,maxx=0,maxy=0,maxz=0;
float x[10],y[10],z[10];

thread_loop_c(c_thread,domain) /*loops over all cell threads in domain*/
{
begin_c_loop(c,c_thread) /* loops over cells in a cell thread */
{
c_face_loop(c,c_thread,n) /* loops over faces in a cell thread */
{
f=C_FACE(c,c_thread,n);
tf = C_FACE_THREAD(c,c_thread,n);
counter=0;
f_node_loop(f,c_thread,n) /* loops over nodes in a face thread */
{
node=F_NODE(c,c_thread,n);
x[counter]=NODE_X(node);
y[counter]=NODE_Y(node);
z[counter]=NODE_Z(node);
++counter;
}
for (i=0; i<(counter-1); ++i)
{
for (k=i+1; k<counter; k++)
{
delx=fabs(fabs(x[i])-fabs(x[k]));
dely=fabs(fabs(y[i])-fabs(y[k]));
delz=fabs(fabs(z[i])-fabs(z[k]));
if (delx>maxx)
{
maxx=delx;
}
if (dely>maxy)
{
maxy=dely;
}
if (delz>maxz)
{
maxz=delz;
}
}
}
}
deltamax=0;
if (deltamax<maxx)
{
deltamax=maxx;
}
if (deltamax<maxy)
{
deltamax=maxy;
}
if (deltamax<maxz)
{
deltamax=maxz;
}
C_UDMI(c,c_thread,0)=sqrt(C_K(c,c_thread))/(0.09*0.61*deltamax*C_O(c,c_thread));
}
end_c_loop(c,c_thread)
}
}

But it is not worked and it gives an error in Line 20 (c_face_loop(c,c_thread,n)).
may I request you to make a comment about the UDF and the error?

thanks,

Quote:
Originally Posted by pakk View Post
There is no counter yet, if you want one then you have to add it.


Code:
 int counter;
 float x[20],y[20],z[20]; //assuming each cell has 20 nodes or less
  
 ...
  
 counter=0;
 c_node_loop(c,t,n) {
  node = C_NODE(c,t,n);
  x[counter]=NODE_X(node);
  y[counter]=NODE_Y(node);
  z[counter]=NODE_Z(node);
  ++counter;
 }
This still does not calculate distances, you still have to add it.
behest is offline   Reply With Quote

Old   February 7, 2014, 14:59
Default
  #10
Senior Member
 
Join Date: Nov 2013
Posts: 1,965
Rep Power: 27
pakk will become famous soon enough
Hmm, I think I copied that from the Fluent manual, I can not access that now, I'm sorry...

One different thing: your calculation of distance is wrong:
Code:
delx=fabs(fabs(x[i])-fabs(x[k]));
should be
Code:
delx=fabs(x[i]-x[k]);
and similarly for y and z. Otherwise, if you are comparing for example -1 and 1, the code will think they have distance zero, while in reality it is 2.
Amili likes this.
pakk is offline   Reply With Quote

Old   February 7, 2014, 15:21
Default
  #11
Member
 
Ali.E
Join Date: Sep 2010
Location: Lisboa
Posts: 83
Rep Power: 16
behest is on a distinguished road
Many many thanks. I changed the delx,dely,delz, you are right. anyway, I recieve the error too. Please make a comment if you find any idea.
thanks again

Quote:
Originally Posted by pakk View Post
Hmm, I think I copied that from the Fluent manual, I can not access that now, I'm sorry...

One different thing: your calculation of distance is wrong:
Code:
delx=fabs(fabs(x[i])-fabs(x[k]));
should be
Code:
delx=fabs(x[i]-x[k]);
and similarly for y and z. Otherwise, if you are comparing for example -1 and 1, the code will think they have distance zero, while in reality it is 2.
behest is offline   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
whats the cause of error? immortality OpenFOAM Running, Solving & CFD 13 March 24, 2021 08:15
compressible flow in turbocharger riesotto OpenFOAM 50 May 26, 2014 02:47
udf for radial distribution function in a fluidised bed ravindra Fluent UDF and Scheme Programming 0 February 24, 2013 07:26
using METIS functions in fortran dokeun Main CFD Forum 7 January 29, 2013 05:06
Version 15 on Mac OS X gschaider OpenFOAM Installation 113 December 2, 2009 11:23


All times are GMT -4. The time now is 18:48.