# Creating a UDF for heat generation given data points

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

December 5, 2022, 07:09
Creating a UDF for heat generation given data points
#1
New Member

Aman
Join Date: Dec 2022
Posts: 5
Rep Power: 2
Hi, I am currently trying to create a UDF for a source term to input into ANSYS FLUENT. I am modeling heat generation within a Li-ion cell and have the current charging profile. The heat generated within the cell is modeled with the equation q = I^2*R where I is current, R is internal resistance and q is the heat generation.

I have this current charging profile which varies over time. The internal resistance value is constant throughout the charging. I would like to know how to write a UDF such that the current changes with time according to this curve (attached image) .
Attached Images
 Screenshot_20221205_110515.jpg (103.5 KB, 14 views)

 December 5, 2022, 07:45 #2 New Member   Anas Nur Fauzan Join Date: Oct 2019 Posts: 18 Rep Power: 5 To add a source term, I think it's pretty obvious that we should use DEFINE_SOURCE macro. And for the given data points you can convert these values to a polynomial equation that varies along the timestep. What about the constant values? I suggest you implement a boolean that checks the current timestep. If the current timestep is below 500 ms then use constant source value and then after the timestep above 500 ms then return the source value calculated from the polynomial. I understand the polynomial itself won't give you the exact value as in your sample data. But I think it would be still pretty valid.

December 5, 2022, 09:12
#3
New Member

Aman
Join Date: Dec 2022
Posts: 5
Rep Power: 2
Quote:
 Originally Posted by luzikato To add a source term, I think it's pretty obvious that we should use DEFINE_SOURCE macro. And for the given data points you can convert these values to a polynomial equation that varies along the timestep. What about the constant values? I suggest you implement a boolean that checks the current timestep. If the current timestep is below 500 ms then use constant source value and then after the timestep above 500 ms then return the source value calculated from the polynomial. I understand the polynomial itself won't give you the exact value as in your sample data. But I think it would be still pretty valid.

I'm not sure what you mean by "implement a boolean". How can this be done?

 December 5, 2022, 10:05 #4 New Member   Anas Nur Fauzan Join Date: Oct 2019 Posts: 18 Rep Power: 5 According to your provided data points, you can't use a polynomial to define the whole curve because the inconsistency of the curve (the source value from 0 ms to 500 ms is almost constant). So, I was thinking to use polynomial only for the source value from 500 ms and so on. And for below 500 ms, you can use the constant amount of heat source. What would I do to achieve this is something like this: Code: ```if (t == 500) { source = 4.199; // Constant value from 0 ms to 500 ms } else { source == x; // The x value should be your polynomial function from >500 ms until 2400 ms. }``` Of course, you have to add DEFINE_SOURCE macro above. But I think I will leave it to you.

 December 5, 2022, 13:54 #5 New Member   Aman Join Date: Dec 2022 Posts: 5 Rep Power: 2 Thank you luzikato! I have attempted it and have got the following. Does this seem correct or am i making a mistake? The constant values were obtained from a curve-fitting program. #include "udf.h" #define C1 -5E-08 //define constant term #define C2 0.0003 #define C3 -0.5805 #define C4 428.16 #define C5 4.194E-9 DEFINE_SOURCE(heat_gen_cell,cell,thread,dS,eqn) { real source; real time; time = CURRENT_TIME; //taking time value; if (time == 500) { source = 4.199; // Constant value from 0 ms to 500 ms } else { source == C1 * pow(time, 3) + C2 * pow(time, 2) + C3 * pow(time, 1) + C4 ; // The x value should be your polynomial function from >500 ms until 2400 ms. } dS[eqn] = 0; return source; }

 December 9, 2022, 08:32 #6 New Member   Anas Nur Fauzan Join Date: Oct 2019 Posts: 18 Rep Power: 5 Hi! Sorry for the late reply. The codes seem alright to me. Have you tried inserting it to your case? amanv likes this.

 December 12, 2022, 01:43 #7 Senior Member   Alexander Join Date: Apr 2013 Posts: 2,255 Rep Power: 32 UDF uses UNITS in SI 500 ms = 0.5 sec not 500 amanv likes this. __________________ best regards ****************************** press LIKE if this message was helpful

 Tags ansys 2021 r2, fluent - udf, udf and programming