We’re putting the final touches on our new badges platform. Badge issuance remains temporarily paused, but all completions are being recorded and will be fulfilled once the platform is live. Thank you for your patience.

Ansys Learning Forum Forums Discuss Simulation Fluids Modelling porous material Reply To: Modelling porous material

Robbie Crosby
Subscriber

Hi Rob, I completely understand that. Thank you for all your help so far. Would you be able to help with my code for an individual UDF for this energy source above. I have been stuck on this for the past week, and tried multiple methods but to no avail, with ANSYS crashing each time I run this as an energy source term in the porous material. 

I have attached the equation I am modelling for reference.

 

#include "udf.h"


 

#define Tsolidus 300.0

#define Tliquidus 304.0

#define L_f 250 //Latent heat of fusion


 

#define UDM_RHO_F_PREV 0  // UDM index for storing previous rho_f

#define UDM_LAMBDA_PREV 1  // UDM index for storing previous lambda


 

DEFINE_EXECUTE_ON_LOADING(init_values, udf_name)

{

    Thread *t;

    cell_t c;

    Domain *d = Get_Domain(1); // Get domain using Fluent API, assuming single-phase flow

   

    /* Loop over all cells to initialize UDM values */

    thread_loop_c(t, d)

    {

        begin_c_loop_all(c, t)

        {

            C_UDMI(c,t,UDM_RHO_F_PREV) = 1000.0;  //initializing previous rho_f

            C_UDMI(c,t,UDM_LAMBDA_PREV) = 0.0;  //initializing previous lambda

        }

        end_c_loop_all(c, t)

    }

}


 

DEFINE_SOURCE(energy_sourceN, c, t, dS, eqn)

{

  real lambda, lambda_prev, V[ND_ND], rho_f, Sl, d_lambda_dt, grad_lambda_dot_V, T, rho_f_prev;

 

  T = C_T(c, t);

  rho_f = C_R(c, t);

  C_CENTROID(V, c, t);

 

  rho_f_prev = C_UDMI(c, t, UDM_RHO_F_PREV);  // Fetch the previous value of rho_f from UDM

  lambda_prev = C_UDMI(c, t, UDM_LAMBDA_PREV);  // Fetch the previous value of lambda from UDM


 

  // Calculate lambda

  if (T < Tsolidus)

    lambda = 0.0;

  else if (T > Tliquidus)

    lambda = 1.0;

  else

    lambda = (T - Tsolidus) / (Tliquidus - Tsolidus);


 

  // Calculate d(lambda*rho_f*L_f)/dt

  d_lambda_dt = ((rho_f * lambda * L_f * 0.85) - (rho_f_prev * lambda_prev * L_f * 0.85)) / CURRENT_TIMESTEP;


 

  // The gradient calculation requires more attention. Below is just a placeholder, and you need to implement the correct gradient calculation.

  grad_lambda_dot_V = (C_R_G(c,t)[0] * V[0] + C_R_G(c,t)[1] * V[1]) * lambda * L_f;


 

  Sl = d_lambda_dt + grad_lambda_dot_V;


 

  dS[eqn] = 0; // Adjust the source term linearization if necessary


 

  // Store the current time step values of rho_f and lambda to UDM

  C_UDMI(c, t, UDM_RHO_F_PREV) = rho_f;

  C_UDMI(c, t, UDM_LAMBDA_PREV) = lambda;


 

  return Sl;

}