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

why I cannot get the correct x and y values when using UDF?

Register Blogs Community New Posts Updated Threads Search

Like Tree3Likes
  • 1 Post By upeksa
  • 1 Post By ghost82
  • 1 Post By ghost82

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
Old   January 21, 2015, 23:54
Default why I cannot get the correct x and y values when using UDF?
  #1
New Member
 
Haoting Wang
Join Date: Jan 2015
Location: Virginia
Posts: 19
Rep Power: 11
hotin87 is on a distinguished road
Hi everybody,

I am trying to use UDF to do some control of my fluid field. By judging the temperature of some cells, the inlet velocity is changed. I tried to use the method in this link http://www.cfd-online.com/Forums/flu...ure-point.html

But I found that I can't even get the correct x and y values of the cell centroid. Here are some values I got from my code:

x: -536870912
y: -1610612736
x: -1610612736
y: -1610612736
x: 1610612736
y: 536870912
x: -536870912
y: 0
x: 1610612736
y: 536870912
thermocouple temperature is: 0
nt: 0
ND_ND: 2

The following is my code, I really don't know what is wrong. Please help me if you can, thank you!

#include "udf.h"
#define upTemp 298
#define lowTemp 296


DEFINE_EXECUTE_AT_END(tsensor)
{
real thermosensor_coordinate[ND_ND];
real thermosensor_temperature;
real xmin;
real xmax;
real ymin;
real ymax;

real x=1,y=1,nt;

cell_t c;
Domain *d;
Thread *t;
d = Get_Domain(1); /*get domain id, single phase is 1*/


xmin=0.24;
xmax=0.25;
ymin=-0.01;
ymax=0.01;

/* Begin loop to determine the temperature at the centroid of cells near the thermocouple */

thread_loop_c(t,d)
{
nt=0.0;
begin_c_loop(c,t)
{
C_CENTROID(thermosensor_coordinate,c,t);

x=thermosensor_coordinate[0];
y=thermosensor_coordinate[1];



if ((x >= xmin) && (x <= xmax))
{
if ((y >= ymin) && (y <= ymax))
{
thermosensor_temperature=thermosensor_temperature + C_T(c,t); /* get thermocouple temperature */
nt=nt+1.0; /* count number */
printf("x: %d\n", x);
printf("y: %d\n", y);
}
}
}
end_c_loop(c,t)
}

thermosensor_temperature=thermosensor_temperature/nt;

printf("thermocouple temperature is: %d\n", thermosensor_temperature);
printf("nt: %d\n", nt);
printf("ND_ND: %d\n", ND_ND);

}




DEFINE_PROFILE(inlet_V,t,i)
{
real thermosensor_temperature;

face_t f;

if (thermosensor_temperature>=upTemp)
{
begin_f_loop(f,t)
{
F_PROFILE(f,t,i) = 1.38;
}
end_f_loop(f,t)
}
else if (thermosensor_temperature<lowTemp)
{
begin_f_loop(f,t)
{
F_PROFILE(f,t,i) = 0;
}
end_f_loop(f,t)
}

}
hotin87 is offline   Reply With Quote

Old   January 22, 2015, 05:03
Default
  #2
Member
 
Join Date: Jul 2013
Posts: 80
Rep Power: 12
upeksa is on a distinguished road
Don't you see anything strange in your coordinates?

For example, your "y" coordinate:-1610612736. The distance from the Earth to the Moon is 384000000 m , so your device is 4 times further from your origin of coordinates than the Moon from the Earth, think about it.

No big deal actually, in your message

printf("x: %d\n", x);

"x" is real, and "%d" means integer, change you "%d" for a "%g" or "%f" and everything will be fine.

Your question is an old issue in this forum:

http://www.cfd-online.com/Forums/flu...t-problem.html

I have tried this function but it did not work. The solution given in the post you mention is easier and pretty accurate. It set a tolerance margins from your coordinates and it looks for a cell nearby.

Cheers.
hotin87 likes this.
upeksa is offline   Reply With Quote

Old   January 22, 2015, 07:46
Default
  #3
Senior Member
 
ghost82's Avatar
 
Rick
Join Date: Oct 2010
Posts: 1,016
Rep Power: 26
ghost82 will become famous soon enough
Moreover, I think you will get an error (or something strange) if upTemp<=thermosensor_temperature<lowTemp
Write a condition to define a profile when thermosensor_temperature is in this range.

And moreover (2) thermosensor temperature must be a global variable: see here:
http://www.cfd-online.com/Forums/flu...tml#post514940
hotin87 likes this.
__________________
Google is your friend and the same for the search button!
ghost82 is offline   Reply With Quote

Old   January 22, 2015, 10:21
Default
  #4
New Member
 
Haoting Wang
Join Date: Jan 2015
Location: Virginia
Posts: 19
Rep Power: 11
hotin87 is on a distinguished road
Thank you so much, I am not very familiar with c.
Quote:
Originally Posted by upeksa View Post
Don't you see anything strange in your coordinates?

For example, your "y" coordinate:-1610612736. The distance from the Earth to the Moon is 384000000 m , so your device is 4 times further from your origin of coordinates than the Moon from the Earth, think about it.

No big deal actually, in your message

printf("x: %d\n", x);

"x" is real, and "%d" means integer, change you "%d" for a "%g" or "%f" and everything will be fine.

Your question is an old issue in this forum:

http://www.cfd-online.com/Forums/flu...t-problem.html

I have tried this function but it did not work. The solution given in the post you mention is easier and pretty accurate. It set a tolerance margins from your coordinates and it looks for a cell nearby.

Cheers.
hotin87 is offline   Reply With Quote

Old   January 22, 2015, 10:32
Default
  #5
New Member
 
Haoting Wang
Join Date: Jan 2015
Location: Virginia
Posts: 19
Rep Power: 11
hotin87 is on a distinguished road
Thanks for your advice! I changed the sensor temperature to be a global variable. Now I can see them, but I am not quite sure why my "Execute_At_End" function looped 5 times when I run it. The results look like this:

nt outside loop: 0
thermocouple temperature is: 0.000000
ND_ND: 2
x: 0.245169
y: -0.000502
nt: 1
x: 0.245170
y: 0.000497
nt: 2
nt outside loop: 0
thermocouple temperature is: 593.720703
ND_ND: 2
nt outside loop: 0
thermocouple temperature is: 0.000000
ND_ND: 2
nt outside loop: 0
thermocouple temperature is: 0.000000
ND_ND: 2
nt outside loop: 0
thermocouple temperature is: 0.000000
ND_ND: 2

As you can see, during the first time loop, the code get the temperature and locations of the thermocouple point. But after that it loops 4 more times and covered the correct result. So weird.

The code I used looks like this:

#include "udf.h"
#define upTemp 298
#define lowTemp 296

real thermosensor_T;

DEFINE_EXECUTE_AT_END(tsensor)
{
real thermosensor_coordinate[ND_ND];
real thermosensor_temperature;
real xmin;
real xmax;
real ymin;
real ymax;

real x=1,y=1;
int nt;

cell_t c;
Domain *d;
Thread *t;
d = Get_Domain(1); /*get domain id, single phase is 1*/


xmin=0.2449;
xmax=0.2453;
ymin=-0.001;
ymax=0.001;

/* Begin loop to determine the temperature at the centroid of cells near the thermocouple */

thread_loop_c(t,d)
{
nt=0;
begin_c_loop(c,t)
{
C_CENTROID(thermosensor_coordinate,c,t);

x=thermosensor_coordinate[0];
y=thermosensor_coordinate[1];



if ((x >= xmin) && (x <= xmax))
{
if ((y >= ymin) && (y <= ymax))
{
thermosensor_temperature=thermosensor_temperature + C_T(c,t); /* get thermocouple temperature */
nt=nt+1; /* count number */
printf("x: %f\n", x);
printf("y: %f\n", y);
printf("nt: %d\n", nt);
}
}
}
end_c_loop(c,t)
}

//thermosensor_temperature=thermosensor_temperature/nt;
thermosensor_T=thermosensor_temperature;
printf("nt outside loop: %d\n", nt);
printf("thermocouple temperature is: %f\n", thermosensor_temperature);

printf("ND_ND: %d\n", ND_ND);

}




DEFINE_PROFILE(inlet_V,t,i)
{

face_t f;

if (thermosensor_T>=upTemp)
{
begin_f_loop(f,t)
{
F_PROFILE(f,t,i) = 1.38;
}
end_f_loop(f,t)
}
else if (thermosensor_T<lowTemp)
{
begin_f_loop(f,t)
{
F_PROFILE(f,t,i) = 0;
}
end_f_loop(f,t)
}

}


Quote:
Originally Posted by ghost82 View Post
Moreover, I think you will get an error (or something strange) if upTemp<=thermosensor_temperature<lowTemp
Write a condition to define a profile when thermosensor_temperature is in this range.

And moreover (2) thermosensor temperature must be a global variable: see here:
http://www.cfd-online.com/Forums/flu...tml#post514940
hotin87 is offline   Reply With Quote

Old   January 22, 2015, 10:43
Default
  #6
Senior Member
 
ghost82's Avatar
 
Rick
Join Date: Oct 2010
Posts: 1,016
Rep Power: 26
ghost82 will become famous soon enough
Are you running in parallel?
__________________
Google is your friend and the same for the search button!
ghost82 is offline   Reply With Quote

Old   January 22, 2015, 11:11
Default
  #7
New Member
 
Haoting Wang
Join Date: Jan 2015
Location: Virginia
Posts: 19
Rep Power: 11
hotin87 is on a distinguished road
I have five zones, 4 are solid zones, 1 is fluid zone. Does that have something to do with the 5 execution thing? If so, how can I modify my code to let it only execute on the fluid zone?
hotin87 is offline   Reply With Quote

Old   January 22, 2015, 11:12
Default
  #8
Senior Member
 
ghost82's Avatar
 
Rick
Join Date: Oct 2010
Posts: 1,016
Rep Power: 26
ghost82 will become famous soon enough
I think this has nothing to do with the 5 zones: are you running your simulation in parallel (with 4 processes)?
hotin87 likes this.
__________________
Google is your friend and the same for the search button!
ghost82 is offline   Reply With Quote

Old   January 22, 2015, 11:14
Default
  #9
New Member
 
Haoting Wang
Join Date: Jan 2015
Location: Virginia
Posts: 19
Rep Power: 11
hotin87 is on a distinguished road
Quote:
Originally Posted by ghost82 View Post
Are you running in parallel?
YES! I am, using 4 nodes to run.
hotin87 is offline   Reply With Quote

Old   January 22, 2015, 11:18
Default
  #10
New Member
 
Haoting Wang
Join Date: Jan 2015
Location: Virginia
Posts: 19
Rep Power: 11
hotin87 is on a distinguished road
Quote:
Originally Posted by ghost82 View Post
I think this has nothing to do with the 5 zones: are you running your simulation in parallel (with 4 processes)?
That's the problem! When I use 8 processes, it repeated 9 times! How can I fix this problem?
hotin87 is offline   Reply With Quote

Old   January 22, 2015, 11:22
Default
  #11
Senior Member
 
ghost82's Avatar
 
Rick
Join Date: Oct 2010
Posts: 1,016
Rep Power: 26
ghost82 will become famous soon enough
Ok, the udf above should work for a serial run.
If you want to parallelize the udf you must include some more code, read here:
http://www33.zippyshare.com/v/tsx9Z5Rk/file.html

If you want to run in parallel:
DEFINE_PROFILE macro will work as it is and it doesn't need to be parallelized, you need to parallelize the DEFINE_EXECUTE_AT_END macro.

PS: you still need to add a condtion when temperature is 296<T<298
__________________
Google is your friend and the same for the search button!
ghost82 is offline   Reply With Quote

Old   January 22, 2015, 15:57
Default
  #12
New Member
 
Haoting Wang
Join Date: Jan 2015
Location: Virginia
Posts: 19
Rep Power: 11
hotin87 is on a distinguished road
Is there anyway that a part of code is only performed on ONE node? I tried to use if (myid=0), it is very strange that the loop still goes 5 times instead of only once. Here are the results and code:


nt2: 8
nt2: 0
nt2: 0
nt2: 0
nt2: 0
nt outside loop2: 0
I am node 0 with thermocouple temperature: 1.#INF00


#include "udf.h"
#define upTemp 298
#define lowTemp 296
real thermosensor_T;



DEFINE_EXECUTE_AT_END(tsensor)
{
real thermosensor_coordinate[ND_ND];
real thermosensor_temperature;
real xmin;
real xmax;
real ymin;
real ymax;
int nt;
real x=1,y=1;


cell_t c;
Domain *d;
Thread *t;
d = Get_Domain(1); /*get domain id, single phase is 1*/


xmin=0.2449; //0.2449;
xmax=0.2455; //0.2453;
ymin=-0.002; //-0.001;
ymax=0.002; //0.001;

/* Begin loop to determine the temperature at the centroid of cells near the thermocouple */
if (myid==0)
{

thread_loop_c(t,d)
{

nt=0;

begin_c_loop(c,t)
{
C_CENTROID(thermosensor_coordinate,c,t);

x=thermosensor_coordinate[0];
y=thermosensor_coordinate[1];



if ((x >= xmin) && (x <= xmax))
{
if ((y >= ymin) && (y <= ymax))
{
thermosensor_temperature=thermosensor_temperature + C_T(c,t); /* get thermocouple temperature */
nt=nt+1; /* count number */

printf("x: %f\n", x);
printf("y: %f\n", y);
printf("nt1: %d\n", nt);


printf("I am node %d with thermocouple temperature: %f\n", myid, thermosensor_temperature);
}
}

}
end_c_loop(c,t)
printf("nt2: %d\n", nt);
thermosensor_T=thermosensor_temperature/nt;
}




printf("nt outside loop2: %d\n", nt);
printf("I am node %d with thermocouple temperature: %f\n", myid, thermosensor_T);

}

}




DEFINE_PROFILE(inlet_V,t,i)
{

face_t f;

if (thermosensor_T>=upTemp)
{
begin_f_loop(f,t)
{
F_PROFILE(f,t,i) = 1.38;
}
end_f_loop(f,t)
}
else if (thermosensor_T<lowTemp)
{
begin_f_loop(f,t)
{
F_PROFILE(f,t,i) = 0;
}
end_f_loop(f,t)
}

}
hotin87 is offline   Reply With Quote

Old   January 22, 2015, 15:58
Default
  #13
New Member
 
Haoting Wang
Join Date: Jan 2015
Location: Virginia
Posts: 19
Rep Power: 11
hotin87 is on a distinguished road
Does this has something to do with that I have five zones in my calculation domain? 4 of them are solid, 1 is fluid. But I have already used "d = Get_Domain(1)"
hotin87 is offline   Reply With Quote

Old   January 23, 2015, 06:06
Default
  #14
Senior Member
 
ghost82's Avatar
 
Rick
Join Date: Oct 2010
Posts: 1,016
Rep Power: 26
ghost82 will become famous soon enough
Don't cross post as it is forbidden by forum rules, point 6:
http://www.cfd-online.com/Forums/misc.php?do=showrules

Did you read the pdf I attached?
It should look something like this:

Code:
DEFINE_EXECUTE_AT_END(tsensor)
{
  real thermosensor_coordinate[ND_ND];
  real thermosensor_temperature;
  real xmin;
  real xmax;
  real ymin;
  real ymax;
  int nt;
  real x=1,y=1;

  cell_t c;
  Domain *d;
  Thread *t;
  d = Get_Domain(1); /*get domain id, single phase is 1*/

  xmin=0.2449; //0.2449;
  xmax=0.2455; //0.2453;
  ymin=-0.002; //-0.001;
  ymax=0.002; //0.001;
  nt=0;
  thermosensor_temperature=0.0;

  /* Begin loop to determine the temperature at the centroid of cells near the thermocouple */

  thread_loop_c(t,d)
  {
    #if !RP_HOST
    begin_c_loop_int(c,t)
    {
      C_CENTROID(thermosensor_coordinate,c,t);

      x=thermosensor_coordinate[0];
      y=thermosensor_coordinate[1];



      if ((x >= xmin) && (x <= xmax))
      {
        if ((y >= ymin) && (y <= ymax))
        {
          thermosensor_temperature=thermosensor_temperature + C_T(c,t); /* get thermocouple temperature */
          nt=nt+1; /* count number */
        }
      }
    }
    end_c_loop_int(c,t)
    #endif

    #if RP_NODE
      thermosensor_temperature=PRF_GRSUM1(thermosensor_temperature);
      nt=PRF_GISUM1(nt);
    #endif

    #if !RP_HOST
      thermosensor_T=thermosensor_temperature/nt;
    #endif

    node_to_host_real_2(thermosensor_temperature, thermosensor_T);
    node_to_host_int_1(nt);

    #if !RP_NODE
      printf("nt: %d\n", nt);
      printf("Thermocouple temperature: %f\n", thermosensor_T);
    #endif
  }
}
Correct it by yourself if there are errors.
__________________
Google is your friend and the same for the search button!

Last edited by ghost82; January 23, 2015 at 12:02.
ghost82 is offline   Reply With Quote

Old   January 24, 2015, 14:42
Default
  #15
New Member
 
Haoting Wang
Join Date: Jan 2015
Location: Virginia
Posts: 19
Rep Power: 11
hotin87 is on a distinguished road
yes, I've read it, thanks for sharing that to me. It is just that this code has some commands that can only be used if it is compiled. My computer has some problem in compiling the code, now I can only use by interpret.

Quote:
Originally Posted by ghost82 View Post
Don't cross post as it is forbidden by forum rules, point 6:
http://www.cfd-online.com/Forums/misc.php?do=showrules

Did you read the pdf I attached?
It should look something like this:

Code:
DEFINE_EXECUTE_AT_END(tsensor)
{
  real thermosensor_coordinate[ND_ND];
  real thermosensor_temperature;
  real xmin;
  real xmax;
  real ymin;
  real ymax;
  int nt;
  real x=1,y=1;

  cell_t c;
  Domain *d;
  Thread *t;
  d = Get_Domain(1); /*get domain id, single phase is 1*/

  xmin=0.2449; //0.2449;
  xmax=0.2455; //0.2453;
  ymin=-0.002; //-0.001;
  ymax=0.002; //0.001;
  nt=0;
  thermosensor_temperature=0.0;

  /* Begin loop to determine the temperature at the centroid of cells near the thermocouple */

  thread_loop_c(t,d)
  {
    #if !RP_HOST
    begin_c_loop_int(c,t)
    {
      C_CENTROID(thermosensor_coordinate,c,t);

      x=thermosensor_coordinate[0];
      y=thermosensor_coordinate[1];



      if ((x >= xmin) && (x <= xmax))
      {
        if ((y >= ymin) && (y <= ymax))
        {
          thermosensor_temperature=thermosensor_temperature + C_T(c,t); /* get thermocouple temperature */
          nt=nt+1; /* count number */
        }
      }
    }
    end_c_loop_int(c,t)
    #endif

    #if RP_NODE
      thermosensor_temperature=PRF_GRSUM1(thermosensor_temperature);
      nt=PRF_GISUM1(nt);
    #endif

    #if !RP_HOST
      thermosensor_T=thermosensor_temperature/nt;
    #endif

    node_to_host_real_2(thermosensor_temperature, thermosensor_T);
    node_to_host_int_1(nt);

    #if !RP_NODE
      printf("nt: %d\n", nt);
      printf("Thermocouple temperature: %f\n", thermosensor_T);
    #endif
  }
}
Correct it by yourself if there are errors.
hotin87 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
Setting cell variable values in a fluid zone using UDF eromon84 Fluent UDF and Scheme Programming 6 March 28, 2021 11:59
WILLING TO PAY/ FREELANCER REQUIRED / small UDF coding force loads over body / 6DOF acasas CFD Freelancers 1 January 23, 2015 07:26
Availability of previous time level values in UDF ranga sudarsan FLUENT 0 September 1, 2008 09:17
printing values from udf!!! Shekhar FLUENT 2 January 13, 2004 11:49
Printing Values From UDF Pravesh Gangwar FLUENT 1 October 22, 2002 06:08


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