TAGGED: udf
-
-
March 17, 2025 at 8:53 am
eng_m_m.abd89
SubscriberHello, this is my UDF for heat source; it worked well, but now I want to consider the average temperature instead of the cell temperature. My domain is three bodies [the battery, battery wall, and fluid around them]. I want to consider only the average temperature of the battery. Any help with performing this?
/*my_battery_heat_source_trial.c*/
#include "udf.h"
#include "stdio.h"
#include "math.h"
#include "mem.h"
#include "time.h"
// Inputs
#define V_bat 0.0000165 // Battery volume (m^3)
#define H_chem 11740 // Total heat release of chemical reaction (J)
#define H_sc 17560 // Total heat release of short circuit (J)
#define rho 2600 // Density of battery (kg/m^3)
#define Cp 1720 // Specific heat capacity (J/kg.K)
#define A 0.92 // Pre-exponential factor (unitless)
#define b 28.5 // Reaction rate constant (unitless)
#define T_ref 533.15 // Normalized temperature coefficient (K)
#define T_chem 413.15 // Start temperature of chemical reactions (K)
#define T_tr 533.15 // Triggering temperature of thermal runaway (K)
#define C_chem_max 12
#define C_elec_max 12
// UDF to define heat generation source term
DEFINE_SOURCE(my_battery_heat_source_trial,c,t,ds, eqn)
{
// Declaring Local variable for chemical and electrical energy concentration
real C_chem = 1.0;
real C_chem_old = 1.0;
real C_elec = 1.0;
real C_elec_old = 1.0;
real dC_chem_dt = 0.0;
real dC_elec_dt = 0.0;
real q_chem;
real q_elec;
real qv;
real T = C_T(c,t); // Get the temperature from the cell
real dt = CURRENT_TIMESTEP; // Get the simulation time step
// Function to compute the rate of change of C_chem (dC_chem/dt)
if (T>T_chem && T<=T_tr && C_chem>=0 && C_chem<=1)
dC_chem_dt =(Cp / H_chem) * A * rho * V_bat * pow((T / T_ref), b);
else if (T>T_tr && C_chem>=0 && C_chem<=1)
dC_chem_dt = C_chem_max;
else
dC_chem_dt = 0.0;
// Update the chemical energy concentration
C_chem = C_chem_old - (dC_chem_dt * dt);
// Function to compute volumetric chemical heat generation rate (q_chem)
q_chem = (1.0/V_bat) * H_chem * dC_chem_dt;
// Function to compute short-circuit reaction rate (dC_elec/dt)
if (T>T_tr && C_chem>=0 && C_chem<=1)
dC_elec_dt = C_elec_max;
else
dC_elec_dt = 0.0;
// Update the electrical energy concentration
C_elec = C_elec_old - (dC_elec_dt * dt);
// Function to compute volumetric electric heat generation rate (q_elec)
q_elec = (1.0/V_bat) * H_sc * dC_elec_dt;
// Compute heat generation rates
qv = q_chem + q_elec; // Total heat generation rate (W/m³)
ds[eqn]=0;
return qv; // Return total heat source term for Fluent
} -
March 17, 2025 at 9:59 am
Rob
Forum ModeratorYou'll need to loop over the cell zone cells and remember to account for NODE/HOST issues.Â
-
March 17, 2025 at 10:31 am
eng_m_m.abd89
SubscriberAny advice on where/how to start writing it, as I am new to the UDF and don't know how to make the loop over the battery cells?Â
-
March 17, 2025 at 11:25 am
Rob
Forum ModeratorLook for examples using begin_c_loop and end_c_loop for some ideas.Â
-
March 19, 2025 at 6:44 am
eng_m_m.abd89
SubscriberHi, Thanks for your support, I don`t know how to thank you!
I have updated the UDF and it worked with no error, but just because I am new to the Ansys and UDFs, I want to make sure that I am not making any mistake. the below is my UDF, what do you think of it? will it compute the heat source using the avergaed temperature of the battery cell?
Â
/*my_battery_heat_source_trial.c*/#include "udf.h"#include "stdio.h"#include "math.h"#include "mem.h"#include "time.h"Â// Inputs#define V_bat 0.0000165 // Battery volume (m^3)#define H_chem 11740   // Total heat release of chemical reaction (J)#define H_sc 17560    // Total heat release of short circuit (J)#define rho 2600     // Density of battery (kg/m^3)#define Cp 1720     // Specific heat capacity (J/kg.K)#define A 0.92      // Pre-exponential factor (unitless)#define b 28.5      // Reaction rate constant (unitless)#define T_ref 533.15   // Normalized temperature coefficient (K)#define T_chem 413.15  // Start temperature of chemical reactions (K)#define T_tr 533.15   // Triggering temperature of thermal runaway (K)#define C_chem_max 12  Â#define C_elec_max 12  ÂÂ// Global variable to store the computed average temperaturereal avg_temp = 300.0; // Initial assumption (K)Â// Function to Compute Average Temperature in Battery Zone (2D)real compute_avg_temperature(Domain *domain, int battery_zone_id){  real T_avg = 0.0, area = 0.0, area_tot = 0.0;  real T;  Thread *t;  cell_t c;  /* Get thread corresponding to battery zone */  t = Lookup_Thread(domain, battery_zone_id);  begin_c_loop(c, t)  {   area = C_VOLUME(c, t);   T = C_T(c,t);  // Get the temperature from the cell   area_tot += area;   T_avg += T * area;  }  end_c_loop(c, t)  T_avg /= area_tot; // Compute area-weighted avg temp}Â// Update Average Temperature Before Each IterationDEFINE_ADJUST(update_avg_temp, d){  int battery_zone_id = 6; // Set the battery zone ID  avg_temp = compute_avg_temperature(d, battery_zone_id);  printf("Updated Battery Avg Temperature: %g K\n", avg_temp);}ÂÂ// UDF to define heat generation source termDEFINE_SOURCE(my_battery_heat_source_trial_avg,c,t,ds,eqn)Â{// Declaring Local variable for chemical and electrical energy concentrationreal C_chem = 1.0;Âreal C_chem_old = 1.0;real C_elec = 1.0;Âreal C_elec_old = 1.0;Âreal dC_chem_dt = 0.0;real dC_elec_dt = 0.0;real q_chem;real q_elec;real qv;real dt = CURRENT_TIMESTEP; // Get the simulation time stepÂ// Function to compute the rate of change of C_chem (dC_chem/dt)if (avg_temp>T_chem && avg_temp<=T_tr && C_chem>=0 && C_chem<=1)dC_chem_dt =(Cp / H_chem) * A * rho * V_bat * pow((avg_temp / T_ref), b);else if (avg_temp>T_tr && C_chem>=0 && C_chem<=1)dC_chem_dt = C_chem_max;ÂelsedC_chem_dt = 0.0;// Update the chemical energy concentrationC_chem = C_chem_old - (dC_chem_dt * dt);// Function to compute volumetric chemical heat generation rate (q_chem)q_chem = (1.0/V_bat) * H_chem * dC_chem_dt;// Function to compute short-circuit reaction rate (dC_elec/dt)if (avg_temp>T_tr && C_chem>=0 && C_chem<=1)dC_elec_dt = C_elec_max;elsedC_elec_dt = 0.0;// Update the electrical energy concentrationC_elec = C_elec_old - (dC_elec_dt * dt);// Function to compute volumetric electric heat generation rate (q_elec)q_elec = (1.0/V_bat) * H_sc * dC_elec_dt;// Compute heat generation ratesqv = q_chem + q_elec; // Total heat generation rate (W/m³)ds[eqn]=0;return qv; // Return total heat source term for Fluent} -
March 19, 2025 at 9:57 am
Rob
Forum ModeratorYou're welcome.Â
I can't see anything from a quick look but you may want to print the result of  T_avg /= area_tot to the screen? I would use some caution using "area" labels for volume, I know why you've done it, but a comment may be useful for when you revisit the code in 3d. I have UDFs from about 2003 that are still valid, and most UDFs are written using something else (tutorials in my case - I code as little as possible) as a starting point.Â
-
- You must be logged in to reply to this topic.
- How do I get my hands on Ansys Rocky DEM
- Unburnt Hydrocarbons contour in ANSYS FORTE for sector mesh
- convergence issue for transonic flow
- Facing trouble regarding setting up boundary conditions for SOEC Modeling
- Point exception in erosion calculation
- Script Error Ansys
- Errors with multi-connected bodies using AQWA
- Quantitative results
-
2658
-
953
-
813
-
599
-
591
© 2025 Copyright ANSYS, Inc. All rights reserved.