Ansys Innovation Courses

Ansys Innovation Courses

Provide feedback or ask questions related to the Ansys Innovation Courses content

UDFs for adding Energy source term in PCM in Porous medium

    • Robbie Crosby
      Subscriber

      Hello, I am unsure if this is the place to ask this, however I am really struggling to create UDFs to fit a numerical model which must be applied as UDFs in ANSYS Fluent to model a phase change material in a porous medium for thermal energy transfer. I have attached the mathematical model I am trying to follow as well as my current code. I am stuck on how to add the source term Sl, currently it crashes each time I try to run it.

      #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;

      }

       

    • Atharva Nagarkar
      Ansys Employee

      Hello,

      As Ansys employees, we cannot debug UDFs on this platform. However, can you share more details about the error you receive when you run the solution? 

      One possible issue could be with the C_UDMI macro. You can use the C_UDMI macro to access or store the value of the user-defined memory in a cell. It can be used to allocate up to 500 memory locations in order to store and retrieve the values of cell field variables computed by UDFs. If the number of field variable values to be stored exceed 500, you may receive a memory storage error. Please find the link from the Ansys UDF Manual for this macro below.

      Additionally, I have attached links from the Ansys UDF Manual for the DEFINE_SOURCE, DEFINE_EXECUTE_ON_LOADING and Gradient macros. Please ensure that you have appropriately used these macros.

      DEFINE_SOURCE - 2.3. Model-Specific DEFINE Macros (ansys.com) -> Check section 2.3.43

      DEFINE_EXECUTE_ON_LOADING - 2.2. General Purpose DEFINE Macros (ansys.com) -> Check section 2.2.6

      Gradient - 3.2. Data Access Macros (ansys.com) -> Check section 3.2.3.7

      C_UDMI - 3.2. Data Access Macros (ansys.com) -> Check section 3.2.12.4

      If you are not able to access the link, please refer to this forum discussion: Using Help with links (ansys.com)

      Thanks!

Viewing 1 reply thread
  • The topic ‘UDFs for adding Energy source term in PCM in Porous medium’ is closed to new replies.