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

Initializing array in udf

Register Blogs Community New Posts Updated Threads Search

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
Old   February 22, 2019, 12:35
Default Initializing array in udf
  #1
Member
 
Durgesh
Join Date: Oct 2018
Posts: 34
Rep Power: 7
durg is on a distinguished road
Hello!

I am defining an udf for my problem with define macros. I am defining array with 30 elements and want to initialize at time t=0. As we know that adjust get called at the beginning of the loop at every iteration. So, it gets initialized every time and old values get erased. It should be initialized at t=0 and keep on going with time. So, how do I initialize so that at any time it retain the values for that time?

Thank you!
durg is offline   Reply With Quote

Old   February 24, 2019, 21:05
Default
  #2
Senior Member
 
Alexander
Join Date: Apr 2013
Posts: 2,363
Rep Power: 34
AlexanderZ will become famous soon enoughAlexanderZ will become famous soon enough
make your array global so you can use it everywhere, initialize it in DEFINE_INIT macro


best regards
AlexanderZ is offline   Reply With Quote

Old   February 25, 2019, 11:33
Default
  #3
Member
 
Durgesh
Join Date: Oct 2018
Posts: 34
Rep Power: 7
durg is on a distinguished road
Thank you. I tried that. But when I am printing the initialized values in adjust macros it is giving me 0 value instead of the desired value. I am initializing it with 0.0075 to all elements but on printing, it is giving '0'.

Now I am trying with 30(no. of elements) user-defined macros, which will store values at current time and can be used for next time step for particular cell. Here it is:

//Initializing
for(int j=0;j<30;j++)
C_UDMI(c,t,j)=0.0075;

Thank you
durg is offline   Reply With Quote

Old   February 26, 2019, 00:16
Default
  #4
Senior Member
 
Alexander
Join Date: Apr 2013
Posts: 2,363
Rep Power: 34
AlexanderZ will become famous soon enoughAlexanderZ will become famous soon enough
Code:
DEFINE_INIT(init_vars, d)
{
  cell_t c;
  Thread *t;
int j;
  
  thread_loop_c (t,d)
  {
    begin_c_loop (c,t)
    {
for(int j=0;j<30;j++)
C_UDMI(c,t,j)=0.0075;
    }
    end_c_loop (c,t)
  }
  
}
C_UDMI is user defined value at specific finite volume of your model, so you should organize loop over all finite volumes (cells) of your model and define values there

best regards
AlexanderZ is offline   Reply With Quote

Old   February 26, 2019, 07:24
Default
  #5
Member
 
annan
Join Date: Nov 2016
Posts: 72
Rep Power: 9
annan is on a distinguished road
Dear Durg,

Why didn't you define a matrix where for each time step you store the new values in an array with 30 elements ?

If you use User Defined Memory, then you have to loop on all the cells of your domain and store the value in all of them. If you make the array global, you can initialize it as Array[29] = {0.0075};

Good luck
Best regards
Annan
annan is offline   Reply With Quote

Old   February 26, 2019, 11:50
Default
  #6
Member
 
Durgesh
Join Date: Oct 2018
Posts: 34
Rep Power: 7
durg is on a distinguished road
Quote:
Originally Posted by annan View Post
Dear Durg,

Why didn't you define a matrix where for each time step you store the new values in an array with 30 elements ?

If you use User Defined Memory, then you have to loop on all the cells of your domain and store the value in all of them. If you make the array global, you can initialize it as Array[29] = {0.0075};

Good luck
Best regards
Annan
I tried by defining global like below:

real arr[30]={0.0075};
DEFINE_ADJUST(myad, d)
{
cell_t c;
Thread *t;
printf("Value %f\n", arr[2]);
d=Get_Domain(1);
thread_loop_c (t,d)
{
begin_c_loop(c,t)
{

------
}
end_c_loop(c,t)
}
}

for checking, I am printing one of the values of the initialized array and it is showing me 0 but it should be 0.0075. Am I doing right?

Thank you
durg is offline   Reply With Quote

Old   February 26, 2019, 11:58
Default
  #7
Member
 
Durgesh
Join Date: Oct 2018
Posts: 34
Rep Power: 7
durg is on a distinguished road
Quote:
Originally Posted by AlexanderZ View Post
Code:
DEFINE_INIT(init_vars, d)
{
  cell_t c;
  Thread *t;
int j;
  
  thread_loop_c (t,d)
  {
    begin_c_loop (c,t)
    {
for(int j=0;j<30;j++)
C_UDMI(c,t,j)=0.0075;
    }
    end_c_loop (c,t)
  }
  
}
C_UDMI is user defined value at specific finite volume of your model, so you should organize loop over all finite volumes (cells) of your model and define values there

best regards
Thank you. Yeah, I wrote in the same way and using this. Also I understand that these 30 udm will store 30 values for every cell and can be used for next time for new calculation and new values can be stored in again these 30 udm. Old value can be saved in other variable.

Please clarify.

Thank you

Last edited by durg; February 26, 2019 at 14:48.
durg is offline   Reply With Quote

Old   February 26, 2019, 23:35
Default
  #8
Senior Member
 
Alexander
Join Date: Apr 2013
Posts: 2,363
Rep Power: 34
AlexanderZ will become famous soon enoughAlexanderZ will become famous soon enough
UDM is a variable which is defined in all cells , it may have different value in different cell.
Same UDM (for instance C_UDMI(c,t,0)) may have value =1 in the first cell (finite volume element of mesh) and value = 11 in cell number 2 and so on.
You may do with these values everything you want.

about other your question
Code:
real arr[30]={0.0075};
DEFINE_ADJUST(myad, d)
{
cell_t c;
Thread *t;
printf("Value %f\n", arr[2]);
d=Get_Domain(1);
thread_loop_c (t,d)
{
begin_c_loop(c,t)
{

------
}
end_c_loop(c,t)
}
}
your array arr consist of only 1 element
so if you want to pick first elemt from arry you should type
Code:
Message("Value %f\n", arr[0]);
it is recommended to you Message macro instead of printf in fluent UDF

best regards
AlexanderZ is offline   Reply With Quote

Old   February 27, 2019, 02:57
Default
  #9
Member
 
annan
Join Date: Nov 2016
Posts: 72
Rep Power: 9
annan is on a distinguished road
Quote:
Originally Posted by durg View Post
I tried by defining global like below:

real arr[30]={0.0075};
DEFINE_ADJUST(myad, d)
{
cell_t c;
Thread *t;
printf("Value %f\n", arr[2]);
d=Get_Domain(1);
thread_loop_c (t,d)
{
begin_c_loop(c,t)
{

------
}
end_c_loop(c,t)
}
}

for checking, I am printing one of the values of the initialized array and it is showing me 0 but it should be 0.0075. Am I doing right?

Thank you
Dear Durg,

If your compiler is GCC, this will definitely work :

real arr[30]={[0 ... 29] = 0.0075};
DEFINE_ON_DEMAND(Array_printing)
{
printf("Value %f\n", arr[2]);
}

I tested it and it worked

Good luck
Regards,
Annan
annan is offline   Reply With Quote

Old   February 27, 2019, 14:09
Default
  #10
Member
 
Durgesh
Join Date: Oct 2018
Posts: 34
Rep Power: 7
durg is on a distinguished road
Thanks a lot!!
durg 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
Save output of udf in another udf! JuanJoMex FLUENT 0 February 8, 2018 12:43
udf for valve closing a pipe using dynamic mesh chem engineer Fluent UDF and Scheme Programming 2 May 13, 2017 09:39
array in UDF Kamal FLUENT 1 November 7, 2013 01:15
UDF, UDF, UDF, UDF Luc SEMINEL Main CFD Forum 0 November 25, 2002 04:01
UDF for initializing VOF Ray Main CFD Forum 0 November 11, 1999 11:29


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