Do you have any suggestions on how I could implement it with UDFs for mass and energy sources?
A quite big issue I'm having right now is that even with a well defined UDF, how can I specify the cells that need to be considered as injection cells? For the source terms I can only apply them to a fluid zone, but I would need to apply them only to some cells within that zone. How can I do that?
I tried by using a DEFINE_ADJUST macro in which by giving the boundary of the subvolume in which I want to apply the sources it loops over all the cells in the zone and check if the centroid coordinates fall within this box. If so, it check the cell with C_UDMI(c, t, 0) = 1.0.Â
#include "udf.h"
#include "surf.h"
#include "dpm.h"
/***********************************************************
 Define the total mass flow for the region and mark the cells.
 **********************************************************/
real total_mass_flow = 3e-5; Â /* Total mass flow [kg/s] */
int num_marked_cells = 0; Â Â Â /* Number of cells in the specified region */
real mass_per_cell = 0.0; Â Â Â /* Mass flow per cell */
/* Define the coordinates that limit the mass source region */
real X_MIN = 0.200;
real X_MAX = 0.261;
real Y_MIN = 0.0;
real Y_MAX = 0.00001;
real Z_MIN = 0.0;
real Z_MAX = 0.001;
/**********************************************************
1. Mark cells within the specified region and calculate mass per cell.
**********************************************************/
DEFINE_ADJUST(mark_cells_and_calculate_mass, d)
{
  Thread *t;
  cell_t c;
  real c_centroid[ND_ND]; /* Cell centroid */
  num_marked_cells = 0;  /* Reset the number of marked cells */
  /* Loop over all cell threads */
  thread_loop_c(t, d)
  {
    begin_c_loop(c, t)  /* Loop over all cells in the thread */
    {
      /* Get the cell centroid */
      C_CENTROID(c_centroid, c, t);
      /* Check if the cell is within the specified region */
      if (c_centroid[0] >= X_MIN && c_centroid[0] <= X_MAX &&
        c_centroid[1] >= Y_MIN && c_centroid[1] <= Y_MAX &&
        c_centroid[2] >= Z_MIN && c_centroid[2] <= Z_MAX)
      {
        /* Mark the cell by storing 1 in UDMI (User-Defined Memory Index) */
        C_UDMI(c, t, 0) = 1.0;
        num_marked_cells++;  /* Count the marked cell */
      }
      else
      {
        /* Unmark cells outside the region */
        C_UDMI(c, t, 0) = 0.0;
      }
    }
    end_c_loop(c, t)
  }
  /* Calculate the mass flow per cell */
  if (num_marked_cells > 0)
  {
    mass_per_cell = total_mass_flow / num_marked_cells;
  }
  else
  {
    mass_per_cell = 0.0;  /* No cells marked, no mass flow */
  }
}
/**********************************************************
2. Apply the mass source only to the marked cells
**********************************************************/
DEFINE_SOURCE(mass_source_2, c, t, dS, eqn)
{
  real mass_source = 0.0;
  /* Check if the cell is marked */
  if (C_UDMI(c, t, 0) == 1.0)
  {
    real c_volume = C_VOLUME(c, t);           /* Get the volume of the cell */
    mass_source = mass_per_cell / c_volume;    /* Apply the mass source per unit volume */
    dS[eqn] = 0;                            /* No derivative with respect to any transport variable */
  }
  else
  {
    mass_source = 0.0;
    dS[eqn] = 0;
  }
  return mass_source;               /* Return the mass source */
}