# UDF code problem about total value

 Register Blogs Members List Search Today's Posts Mark Forums Read

 January 9, 2012, 02:51 UDF code problem about total value #1 New Member   Yu-Pei Chen Join Date: Jun 2011 Posts: 26 Rep Power: 7 Hi, everybody, here is my code : Code: ```#include "udf.h" #include "mem.h" #include "math.h" #include "stdio.h" #define mass_alloy 0.295 DEFINE_ADJUST(adj_UDS,d) { cell_t c ; Thread *t; real H_M,mass,sum ; thread_loop_c(t,d) { begin_c_loop(c,t) { mass= (C_UDSI(c,t,0)-8238)*C_VOLUME(c,t)*3.14159; H_M = (mass/mass_alloy)*432.4; C_UDMI(c,t,0) = H_M ; sum += sum C_UDMI(c,t,0) ; } end_c_loop(c,t) } }``` where C_UDSI(c,t,0) represent the value calculated by UDS, and I wanna get the total scalar for H_M in every cell. My case is transient and It can work, but the result seems the summation did not work. My question is if the summation can't be used in DEFINE_ADJUST macro or another method can be appied to calcuate summation. Thanks.

January 9, 2012, 04:36
#2
Senior Member

Amir
Join Date: May 2009
Location: Montreal, QC
Posts: 739
Blog Entries: 1
Rep Power: 16
Hi,

First of all the summation command is something like this:
Quote:
 sum += C_UDMI(c,t,0);
Also you have to initialize the parameter "sum" in this macro:
Quote:
 real sum =0.0;
Using this macro is reasonable but you can also use "custom field function" for your purpose without using any UDF.

Bests,
__________________
Amir

January 9, 2012, 08:42
#3
New Member

Yu-Pei Chen
Join Date: Jun 2011
Posts: 26
Rep Power: 7
Hi, Amir,

the reason that I calculate "sum" is I should use the value to calculate "p_eq" as following :
Code:
```#include "udf.h"
#include "mem.h"
#include "math.h"
#include "stdio.h"
#define A 18.3
#define B 3704.6
#define a 0.0819
#define b 330

{
cell_t c ;
real tem ;
{

begin_c_loop(c,t)
{
tem = C_T(c,t);
if(sum<1.2972)
{
p_eq = 1000*exp((a*1.2972-(B/tem)+A)*(1-((sum-1.2972)*(sum-1.2972))/1.6827));
}
if(1.2972 <= sum <= 4.7564)
{
p_eq = 1000*exp((a*sum)-(B/tem)+A) ;
}
if(sum> 4.7564)
{
p_eq = 1000*exp(((a*4.7564)-(B/tem)+A)+b*((sum-4.7564)*(sum-4.7564))/tem) ;
}
C_UDMI(c,t,1) = p_eq ;
}
end_c_loop(c,t)

}

}```
where the "sum" is a global varible outside the two macros(or create a UDM to store).
can I import the "custom field function" by UDF ?
besides, I check the "UDF guide " and found that :
Quote:
 "A function that is defined using DEFINE ADJUST executes at every iteration and is called at the beginning of every iteration before transport equations are solved."
My case is transient, and I calculate every time step for many iterations. so I have a new qusetion :

if I add the iteration number, the H_M will be added more time ?

Thank you very much.

January 9, 2012, 09:06
#4
Senior Member

Amir
Join Date: May 2009
Location: Montreal, QC
Posts: 739
Blog Entries: 1
Rep Power: 16
Quote:
 Originally Posted by ypchen where the "sum" is a global varible outside the two macros(or create a UDM to store).
yes; global variable is a good option here and as you said it should be out of the macros unlike your first UDF. Performing another UDM is not proper for such purpose; although it works but just consumes memory.
Quote:
 Originally Posted by ypchen can I import the "custom field function" by UDF ?
I'm not sure about this. it may be possible with schemes where I don't have any experiences with. You can search threads or open a new one.

Quote:
 Originally Posted by ypchen if I add the iteration number, the H_M will be added more time ?
To avoid this feature you have 2 options:
1) use a simple if-clause in current UDF and use the commands when the time changes.
2) use DEFINE_EXECUTE_AT_THE_END macro which executes at the end of each time step automatically.

Bests,
__________________
Amir

January 10, 2012, 02:11
#5
New Member

Yu-Pei Chen
Join Date: Jun 2011
Posts: 26
Rep Power: 7
Quote:
 use a simple if-clause in current UDF and use the commands when the time changes.
Thank for your reply, Amir. But how to do this ? Can you show a simple example for me ? I have no ideal about this. Thank you .

January 10, 2012, 04:28
#6
Senior Member

Amir
Join Date: May 2009
Location: Montreal, QC
Posts: 739
Blog Entries: 1
Rep Power: 16
Quote:
 Originally Posted by ypchen But how to do this ? Can you show a simple example for me ? I have no ideal about this. Thank you .
Hi,

This is an example of manual:

Quote:
 static int last_ts = -1; /* Global variable. Time step is never <0 */ DEFINE_ADJUST(first_iter_only, domain) { int curr_ts; curr_ts = N_TIME; if (last_ts != curr_ts) { last_ts = curr_ts; /* things to be done only on first iteration of each time step can be put here */ } }
Bests,
__________________
Amir

 January 10, 2012, 23:32 #7 New Member   Yu-Pei Chen Join Date: Jun 2011 Posts: 26 Rep Power: 7 You really help me. Thank you very much, Amir.

 Thread Tools Display Modes Linear Mode

 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 OffTrackbacks are On Pingbacks are On Refbacks are On Forum Rules

 Similar Threads Thread Thread Starter Forum Replies Last Post Avin2407 Fluent UDF and Scheme Programming 1 March 13, 2015 03:02 cboss OpenFOAM 10 March 5, 2015 07:57 LICH Fluent UDF and Scheme Programming 2 May 2, 2013 02:45 july Fluent UDF and Scheme Programming 3 June 20, 2010 06:56 lichun Dong FLUENT 2 July 29, 2005 11:39

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