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/)
-   -   maximum y-position of nodes of a face (https://www.cfd-online.com/Forums/fluent-udf/196917-maximum-y-position-nodes-face.html)

ali_karimi December 20, 2017 18:55

maximum y-position of nodes of a face
 
hello all.
i want to shrink a circle.i define a udf code on the wall of circle(moving wall).this code is below.my circle reduce radius and comes down.i define begin_f_loop and f_node_loop that get all node of the wall and move it.i need max y-position of node of face each time for create center of circle.how can i?

my code:
#include "udf.h"
DEFINE_GRID_MOTION(gridmotion,domain,dt,time,dtime )
{
Thread *tf = DT_THREAD(dt);
face_t f;
Node *v;
double NV_VEC(dx),NV_VEC(a),c,d;
int n;
real r=0.1*time;
SET_DEFORMING_THREAD_FLAG(THREAD_T0(tf));
begin_f_loop(f,tf)
{
f_node_loop(f,tf,n)
{
v = F_NODE(f,tf,n);
c = NODE_Y(v);
d = MAx(c); >>>>>>>>>>>>>this is not correct.how can i?
if (NODE_POS_NEED_UPDATE (v))
{
NODE_POS_UPDATED(v);
a[0]=0;
a[1]=(d*0.5);
dx[0]=-r*(NODE_COORD(v)[0]-a[0]);
dx[1]=-r*((NODE_COORD(v)[1]-a[1])+(d*0.5));
NV_V(NODE_COORD(v), +=, dx);
}
}
end_f_loop(f,tf);
}
}

AlexanderZ December 20, 2017 20:14

make a loop over all faces using F_CENTROID(x,f,t) macros
and find min/max value of coordinate in y direction

Best regards

ali_karimi December 21, 2017 05:28

tanx
i dont understand.i want y-position of uppermost node of my circle each time.
for example a for-loop that get all node y-position and get maximum

doruk December 22, 2017 04:29

You can use PRF_GRHIGH1 macros. Check help for how to use them. Using them in parallel mode is requires lots of modification in udf so keep that in mind.

obscureed January 3, 2018 05:40

Hi Ali_Karimi,

There is a function MAX(a,b), as defined in global.h, which does what you would expect.

It is difficult to work out what you are aiming for, but I suspect that you need one loop (or set of loops) to calculate the maximum y-coordinate, and then a separate loop (or set of loops) to use that to move the nodes. So, the first set of loops could look like this:
Code:

#define VERY_NEGATIVE -0.99e38
  real max_y = VERY_NEGATIVE;

  begin_f_loop(f,tf)
  {
    f_node_loop(f,tf,n)
    {
      v = F_NODE(f,tf,n);
      max_y = MAX(max_y,NODE_Y(v));
    }
  }end_f_loop(f,tf)

#if RP_NODE
  max_y = PRF_GRHIGH1(max_y);
#endif

Note that "begin_f_loop(f,tf){" has to be balanced at the end by "}end_f_loop(f,tf)" -- note this includes "}"! The code you posted gets this wrong and is therefore very confusing. (Note also there is no final ";", which in my opinion is inconvenient.) As doruk pointed out, you need PRF_GRHIGH1 if you running in parallel.

I really cannot work out what you are intending to do in the calculations leading up to "NV_V(NODE_COORD(v), +=, dx);". I suspect that your current code is wrong. (For example, it looks like all the nodes collapse onto the same coordinate. And do you really want r to be zero at time zero?) You need to work this out for yourself, and I would advise you to do this outside of DEFINE_GRID_MOTION, which is a difficult UDF to debug.

Best regards, Ed.

doruk January 3, 2018 07:05

Quote:

Originally Posted by obscureed (Post 676779)

There is a function MAX(a,b), as defined in global.h, which does what you would expect.

Hi Ed,

I was hoping if you can explain MAX function a little bit more.

#define MAX(a,b)((b)>(a)?(b):(a))
This is how it is defined in global.h but it is very confusing to me and it is never explained in anywhere. Not in help or online I checked it a lot.

So it means if the "array b" has any bigger number than "a", "a" becomes maximum number in the "array b" ?

pakk January 3, 2018 08:59

No, MAX(a,b) is just the maximum of numbers a and b.

So:
MAX(3,10) is the same as 10
MAX(5,1) is the same as 5
MAX(2,2) is the same as 2
MAX(-1,1) is the same as 1
and so on.

obscureed January 3, 2018 15:00

(Thanks for the examples, pakk.)

The reason this is not explained in the Fluent manuals is because it is standard C programming language. (Specifically, the "#define" part is a preprocessor macro, and the "(X ? Y : Z)" part is a conditional operator.) See for example https://stackoverflow.com/questions/...n-and-max-in-c, where they also propose more complicated but more secure versions.

ali_karimi January 17, 2018 01:19

Quote:

Originally Posted by doruk (Post 675876)
You can use PRF_GRHIGH1 macros. Check help for how to use them. Using them in parallel mode is requires lots of modification in udf so keep that in mind.


Thank you. I can not use this function, but I found another way. I defined a loop that gets maximized before each step.
first c=0
begin_f_loop(f,tf)
{
f_node_loop(f,tf,n)
{
v = F_NODE(f,tf,n);
if(NODE_Y(v)>c) c=NODE_Y(v);
}
}
end_f_loop(f,tf);

ali_karimi January 17, 2018 01:41

Quote:

Originally Posted by obscureed (Post 676779)
Hi Ali_Karimi,

There is a function MAX(a,b), as defined in global.h, which does what you would expect.

It is difficult to work out what you are aiming for, but I suspect that you need one loop (or set of loops) to calculate the maximum y-coordinate, and then a separate loop (or set of loops) to use that to move the nodes. So, the first set of loops could look like this:
Code:

#define VERY_NEGATIVE -0.99e38
  real max_y = VERY_NEGATIVE;

  begin_f_loop(f,tf)
  {
    f_node_loop(f,tf,n)
    {
      v = F_NODE(f,tf,n);
      max_y = MAX(max_y,NODE_Y(v));
    }
  }end_f_loop(f,tf)

#if RP_NODE
  max_y = PRF_GRHIGH1(max_y);
#endif

Note that "begin_f_loop(f,tf){" has to be balanced at the end by "}end_f_loop(f,tf)" -- note this includes "}"! The code you posted gets this wrong and is therefore very confusing. (Note also there is no final ";", which in my opinion is inconvenient.) As doruk pointed out, you need PRF_GRHIGH1 if you running in parallel.

I really cannot work out what you are intending to do in the calculations leading up to "NV_V(NODE_COORD(v), +=, dx);". I suspect that your current code is wrong. (For example, it looks like all the nodes collapse onto the same coordinate. And do you really want r to be zero at time zero?) You need to work this out for yourself, and I would advise you to do this outside of DEFINE_GRID_MOTION, which is a difficult UDF to debug.

Best regards, Ed.

Hi ED,
Thanks for your attention,
Yes. I need one loop to calculate the maximum y-coordinate, and then a separate loop to use that to move the nodes.
I found a way to get the maximum, but it's just right in the serial mode.Speed is important to me and does not recognize the maximum in the parallel state.
This is first loop of my code:
Code:

#include "udf.h"
DEFINE_GRID_MOTION(gridmotion,domain,dt,time,dtime)
{
  Thread *tf = DT_THREAD(dt);
  face_t f;
  Node *v;
  Double max-y=0,int n;
    begin_f_loop(f,tf)
    {         
        f_node_loop(f,tf,n)
      {       
        v = F_NODE(f,tf,n);
      if(NODE_Y(v)>max-y) max-y=NODE_Y(v);
      }
        }         
    end_f_loop(f,tf);

Yes, I have to use the PRF_GRHIGH1 function, but I do not know how. Tell me please?

pakk January 17, 2018 03:18

"max-y" is not a good variable name. You can not have a minus sign inside a variable name. Try "max_y" instead.

obscureed January 17, 2018 05:15

Pakk is correct about "max-y" (in fact, too restrained -- it is worse than "not a good variable name"). Also, ", int n" should be "; int n" (probably with a newline after ";", to be readable).

Your version of the first loop is basically the same as the one that I proposed, except that yours will never return a negative value of max_y, which could be an error, and I showed you how to use PRF_GRHIGH1.

ali_karimi January 17, 2018 10:56

Quote:

Originally Posted by obscureed (Post 678437)
Pakk is correct about "max-y" (in fact, too restrained -- it is worse than "not a good variable name"). Also, ", int n" should be "; int n" (probably with a newline after ";", to be readable).

Your version of the first loop is basically the same as the one that I proposed, except that yours will never return a negative value of max_y, which could be an error, and I showed you how to use PRF_GRHIGH1.

tanx pakk and Ed,
yes.Sorry
I have put these values correct in the my code. Here it is wrong.
i change code to:
Code:

#include "udf.h"
DEFINE_GRID_MOTION(gridmotion,domain,dt,time,dtime)
{
#if !RP_HOST
  Thread *tf = DT_THREAD(dt);
  face_t f;
  Node *v;
  double max_y;
  int n;
    begin_f_loop(f,tf)
    {         
        f_node_loop(f,tf,n)
      {       
        v = F_NODE(f,tf,n);
      if(NODE_Y(v)>max_y) max_y=NODE_Y(v);
      }
    }         
    end_f_loop(f,tf);
#if PARALLEL
  max_y=PRF_GRHIGH1(max_y);
#endif
 #endif

but see this error when compile the udf:
Error: The UDF library you are trying to load (libudf) is not compiled for parallel use on the current platform (win64).
The system cannot find the file specified.

Where's the mistake? How do I compile a parallel udf?

pakk January 18, 2018 03:53

That is not the error that you get when you try to compile the udf, but the error that you get when you try to load the udf.

Please look at the error that you get after clicking 'build'.

ali_karimi January 18, 2018 10:32

Quote:

Originally Posted by pakk (Post 678534)
That is not the error that you get when you try to compile the udf, but the error that you get when you try to load the udf.

Please look at the error that you get after clicking 'build'.

yes.thats error is this:

ew 1.c to libudf3\src
Creating user_nt.udf file for 2ddp ...
(system "copy "C:\PROGRA~1\ANSYSI~1\v150\fluent"\fluent15.0.0\sr c\makefile_nt.udf "libudf3\win64\2ddp\makefile" ")
1 file(s) copied.
(chdir "libudf3")(chdir "win64\2ddp")
Done.

ali_karimi January 18, 2018 11:10

Thank you ED and pakk
The problem was solved.i changed my code to:
Code:

#include "udf.h"
DEFINE_GRID_MOTION(gridmotion,domain,dt,time,dtime)
{
  Thread *tf = DT_THREAD(dt);
  face_t f;
  Node *v;
  double c=-1;
  int n;
  begin_f_loop(f,tf)
  {
    f_node_loop(f,tf,n)
    {
      v = F_NODE(f,tf,n);
      c = MAX(c,NODE_Y(v));
    }
  }
  end_f_loop(f,tf)
#if RP_NODE
  c = PRF_GRHIGH1(c);
#endif


CFDavatar February 9, 2021 11:21

Find and store maximum value at initial condition
 
Hello guys, I am facing the same problem. However, I want to loop over the domain before my first time step, in other words before I change the values of the nodes and extract the maximum and minimum as parameters for the entire simulation.

Would you help me with some advice how do I manage the loop function? should I create a function, use another macro?

Thank you for the advice,

Code:

DEFINE_GRID_MOTION(motion, domain, dt, time, dtime)
{

        Thread* tf = DT_THREAD(dt);
        face_t f;
        Node* v;
       
        double xprev, yprev, hprev, d, h;
        int n;

        double x_max, x_min;
        x_max = -0.99e38;
        x_min = 0.99e38;
       
        begin_f_loop(f, tf) // I used this loop to find the maximum and minimum and maximum but it broke my entire code
        {
                f_node_loop(f, tf,n)
                {
                        v = F_NODE(f,tf, n);
                        x_max = MAX(x_max, NODE_X(v));
                        x_min = MIN(x_min, NODE_X(v));
                }
        }end_f_loop(f, tf)

#if RP_NODE
                x_max = PRF_GRHIGH1(x_max);
                x_min = PRF_GRHIGH1(x_min);
#endif
        SET_DEFORMING_THREAD_FLAG(THREAD_T0(tf));

        begin_f_loop(f, tf)
        {
                f_node_loop(f, tf, n)
                {
                        v = F_NODE(f, tf, n);

                        if (NODE_POS_NEED_UPDATE(v))
                        {
                                NODE_POS_UPDATED(v);
                               
                                if (CURRENT_TIME > 0.0)
                                {
                                        xprev = NODE_X(v);
                                        yprev = NODE_Y(v);
                                        hprev = kinematics(xprev, PREVIOUS_TIME,x_max,x_min);
                                        d = yprev;
                                        h = kinematics(xprev, CURRENT_TIME, x_max, x_min);
                                        NODE_Y(v) = d+hprev-h;
                                }
                        }
                }
        }
        end_f_loop(f,tf)
}


AlexanderZ March 8, 2021 23:59

use macro DEFINE_ON_DEMAND to calculate something before first iteration


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