CFD Online Discussion Forums

CFD Online Discussion Forums (https://www.cfd-online.com/Forums/)
-   Fluent UDF and Scheme Programming (https://www.cfd-online.com/Forums/fluent-udf/)
-   -   Calculating a variable based on the data from two different cell zones (https://www.cfd-online.com/Forums/fluent-udf/220043-calculating-variable-based-data-two-different-cell-zones.html)

Jasonf August 21, 2019 12:47

Calculating a variable based on the data from two different cell zones
 
Hi, everyone!
I woulk like to kindly ask for some help with a UDF that I am trying to write. I am modelling the membrane permeation. I want to calculate the flux near the solid-liquid interface based on the pressure difference. However, can the pressure difference be gotten by direct subtraction, and how to guarantee the one-to-one correspondence of two subtracted cell value?

AlexanderZ August 22, 2019 00:34

Code:

begin_f_loop(f1,ft1)
  {
    c01=F_C0(f1,ft1);
    t01=F_C0_THREAD(f1,ft1);
    p1=C_P(c01,t01);
  }
  end_f_loop(f1,ft1)

here you have a loop over all faces and get pressure from adjusted cells. So p1 is not a single value,but array of values (each cell has it own pressure)
So I suggest you to use UDMs to store pressure

Code:

DEFINE_ADJUST(adjust_flux,domain)
{
  Domain *domain=Get_Domain(1);
  real p1, p2, flux;
  face_t f1, f2;
  Thread *t,*ft1=Lookup_Thread(domain,8);
  Thread *ft2=Lookup_Thread(domain,6);
  cell_t c,c01, c02;
  Thread *t01, t02;

  begin_f_loop(f1,ft1)
  {
    c01=F_C0(f1,ft1);
    t01=F_C0_THREAD(f1,ft1);
    C_UDMI(c01,t01,0)=C_P(c01,t01);
  }
  end_f_loop(f1,ft1)

  begin_f_loop(f2,ft2)
  {
    c02=F_C0(f2,ft2);
    t02=F_C0_THREAD(f2,ft2);
    C_UDMI(c02,t02,1)=C_P(c02,t02);
  }
  end_f_loop(f2,ft2)
 
begin_c_loop(c, t)
{
  C_UDMI(c,t,2)=C_UDMI(c,t,0)-C_UDMI(c,t,1); //flux=p1-p2;
}
end_f_loop(c,t)
}

best regards

Jasonf August 22, 2019 01:04

@AlexanderZ,thank you! Your help is very helpful to me, and I will try it.

Jasonf September 1, 2019 11:03

1 Attachment(s)
Quote:

Originally Posted by AlexanderZ (Post 742708)
Code:

DEFINE_ADJUST(adjust_flux,domain)
{
  Domain *domain=Get_Domain(1);
  real p1, p2, flux;
  face_t f1, f2;
  Thread *t,*ft1=Lookup_Thread(domain,8);
  Thread *ft2=Lookup_Thread(domain,6);
  cell_t c,c01, c02;
  Thread *t01, t02;

  begin_f_loop(f1,ft1)
  {
    c01=F_C0(f1,ft1);
    t01=F_C0_THREAD(f1,ft1);
    C_UDMI(c01,t01,0)=C_P(c01,t01);
  }
  end_f_loop(f1,ft1)

  begin_f_loop(f2,ft2)
  {
    c02=F_C0(f2,ft2);
    t02=F_C0_THREAD(f2,ft2);
    C_UDMI(c02,t02,1)=C_P(c02,t02);
  }
  end_f_loop(f2,ft2)
 
begin_c_loop(c, t)
{
  C_UDMI(c,t,2)=C_UDMI(c,t,0)-C_UDMI(c,t,1); //flux=p1-p2;
}
end_f_loop(c,t)
}

best regards

Hi AlexanderZ! Sorry to bother you. I have revised the UDF code to compute the pressure difference across the solid membrane as follows.
Code:

DEFINE_INIT(UDM,domain)
{
  cell_t c;
  Thread *t;
  thread_loop_c(t,domain}
  {
    begin_c_loop_all(c,t)
    {
      C_UDMI(c,t,0)=1.e3;
      C_UDMI(c,t,1)=200.;
      C_UDMI(c,t,2)=0.;
    }
    end_c_loop_all(c,t)
  }
}
DEFINE_ADJUST(adjust_flux,domain)
{
  real p1, p2, flux;
  face_t f1, f2;
  Thread *t,*ft1=Lookup_Thread(domain,8);
  Thread *ft2=Lookup_Thread(domain,6);
  cell_t c,c01, c02;
  Thread *t01, t02;

  begin_f_loop(f1,ft1)
  {
    c01=F_C0(f1,ft1);
    t01=F_C0_THREAD(f1,ft1);
    C_UDMI(c01,t01,0)=C_P(c01,t01);
  }
  end_f_loop(f1,ft1)

  begin_f_loop(f2,ft2)
  {
    c02=F_C0(f2,ft2);
    t02=F_C0_THREAD(f2,ft2);
    C_UDMI(c02,t02,1)=C_P(c02,t02);
  }
  end_f_loop(f2,ft2)

thread_loop_c(t,domain)
{
  begin_c_loop(c, t)
  {
    C_UDMI(c,t,2)=C_UDMI(c,t,0)-C_UDMI(c,t,1); //transmembrane pressure=p1-p2;
  }
  end_c_loop(c,t)
}

However, I am still wondering whether the pressures stored in C_UDMI(c,t,0) could be subtracted by the corresponding ones in C_UDMI(c,t,1), since I heard the C_UDMI macro stores the data into each specified cells.
As shown in the attached FIG, a) the pressures on the left side of membrane are stored into 4th layer cells of C_UDMI(c,t,0), and the ones on the right side are stored into 7th layer of C_UDMI(c,t,1)? b) the formula C_UDMI(c,t,2)=C_UDMI(c,t,0)-C_UDMI(c,t,1) can achieve the subtraction of 4th layer and 7th layer cells? c) if not, how to achieve this aim?
Maybe my understanding is wrong, so hope your suggestions. Thanks for any help!

AlexanderZ September 2, 2019 01:46

my bad, you are right, you can't compare udm_0 with udm_1 because they have data in different cells

may be you can make your own arrays and compare them directly:
Code:

#include "udf.h"

DEFINE_INIT(my_init_func,d)
{
cell_t c;
Thread *t;
/* loop over all cell threads in the domain */
thread_loop_c(t,d)
        {
        /* loop over all cells */
                begin_c_loop_all(c,t)
                {
                        C_UDMI(c,t,0)=1.e3;
                        C_UDMI(c,t,1)=200.;
                        C_UDMI(c,t,2)=0.;
                        C_UDMI(c,t,3)=0.;
                        C_UDMI(c,t,4)=0.;
                }
                end_c_loop_all(c,t)
        }
}

DEFINE_ADJUST(adjust_flux,domain)
{
  real p1, p2, flux;
  face_t f1, f2;
  Thread *t,*ft1=Lookup_Thread(domain,8);
  Thread *ft2=Lookup_Thread(domain,6);
  cell_t c,c01, c02;
  Thread *t01, *t02;
  real *c0_array,*c1_array;
  int n;
        n=0;
  begin_f_loop(f1,ft1)
  {
    c01=F_C0(f1,ft1);
    t01=F_C0_THREAD(f1,ft1);
    C_UDMI(c01,t01,0)=C_P(c01,t01);
        c0_array[n] = C_P(c01,t01);
        n++;
  }
  end_f_loop(f1,ft1)
        n=0;
  begin_f_loop(f2,ft2)
  {
    c02=F_C0(f2,ft2);
    t02=F_C0_THREAD(f2,ft2);
    C_UDMI(c02,t02,1)=C_P(c02,t02);
        c1_array[n] = C_P(c01,t01);
        n++;
  }
  end_f_loop(f2,ft2)
 
  if ((sizeof(c0_array) / sizeof(c0_array[0])) != (sizeof(c1_array) / sizeof(c1_array[0])))
          Message0("ERROR ********* size of face 6 is not equal to size of face 8  ********* \n");

        n=0;
       
        // You may check order in array and in cell/thread loop       
       
thread_loop_c(t,domain)
{
  begin_c_loop(c, t)
  {
    C_UDMI(c,t,2)= c0_array[n] - c1_array[n]; //transmembrane pressure=p1-p2;
        // Start -> You may check order in array and in cell/thread loop | compare C_UDMI(c,t,0) with C_UDMI(c,t,3) and C_UDMI(c,t,1) with C_UDMI(c,t,4)
        C_UDMI(c,t,3)= c0_array[n]; //transmembrane pressure=p1-p2;
        C_UDMI(c,t,4)= c1_array[n]; //transmembrane pressure=p1-p2;
        // End -> You may check order in array and in cell/thread loop
        n++;
  }
  end_c_loop(c,t)
}
}

best regards

Jasonf September 2, 2019 23:53

Quote:

Originally Posted by AlexanderZ (Post 743604)
Code:

DEFINE_ADJUST(adjust_flux,domain)
{
  real p1, p2, flux;
  face_t f1, f2;
  Thread *t,*ft1=Lookup_Thread(domain,8);
  Thread *ft2=Lookup_Thread(domain,6);
  cell_t c,c01, c02;
  Thread *t01, *t02;
  real *c0_array,*c1_array;
  int n;
        n=0;
  begin_f_loop(f1,ft1)
  {
    c01=F_C0(f1,ft1);
    t01=F_C0_THREAD(f1,ft1);
    C_UDMI(c01,t01,0)=C_P(c01,t01);
        c0_array[n] = C_P(c01,t01);
        n++;
  }
  end_f_loop(f1,ft1)
        n=0;
  begin_f_loop(f2,ft2)
  {
    c02=F_C0(f2,ft2);
    t02=F_C0_THREAD(f2,ft2);
    C_UDMI(c02,t02,1)=C_P(c02,t02);
        c1_array[n] = C_P(c01,t01);
        n++;
  }
  end_f_loop(f2,ft2)
 
  if ((sizeof(c0_array) / sizeof(c0_array[0])) != (sizeof(c1_array) / sizeof(c1_array[0])))
          Message0("ERROR ********* size of face 6 is not equal to size of face 8  ********* \n");

        n=0;
       
        // You may check order in array and in cell/thread loop       
       
thread_loop_c(t,domain)
{
  begin_c_loop(c, t)
  {
    C_UDMI(c,t,2)= c0_array[n] - c1_array[n]; //transmembrane pressure=p1-p2;
        // Start -> You may check order in array and in cell/thread loop | compare C_UDMI(c,t,0) with C_UDMI(c,t,3) and C_UDMI(c,t,1) with C_UDMI(c,t,4)
        C_UDMI(c,t,3)= c0_array[n]; //transmembrane pressure=p1-p2;
        C_UDMI(c,t,4)= c1_array[n]; //transmembrane pressure=p1-p2;
        // End -> You may check order in array and in cell/thread loop
        n++;
  }
  end_c_loop(c,t)
}
}


Thanks for your help, AlexanderZ! I have revised my code based on your solution, but when compiled it reported an error ''uninitialized variables are used, 'c0_array' and 'c1_array'.'' I am not good at C language, so I used a blunt way below. However, it reported another error "the size of array cannot be assigned as zero". Maybe the array length cannot be '[n]', so may I directly assign the number of face cells (obtained from ICEM) to the arrays? Is there any better solution? Thank you again!

Code:

face_t f1, f2;
Thread *t,*ft1=Lookup_Thread(domain,8);
Thread *ft2=Lookup_Thread(domain,6);
cell_t c01, c02;
Thread *t01, *t02;
int n=0;
begin_f_loop(f1,ft1)
{
  n++;
}
end_f_loop(f1,ft1)

real c0_array[n]={0};
n=0;

begin_f_loop(f1,ft1)
{
    c01=F_C0(f1,ft1);
    t01=F_C0_THREAD(f1,ft1);
    C_UDMI(c01,t01,0)=C_P(c01,t01);
    c0_array[n] = C_P(c01,t01);
    n++;
}
end_f_loop(f1,ft1)

......


AlexanderZ September 3, 2019 04:17

variable should be defined in the top of function, before other instructions
try this
Code:

#include "udf.h"

DEFINE_INIT(my_init_func,d)
{
cell_t c;
Thread *t;
/* loop over all cell threads in the domain */
thread_loop_c(t,d)
        {
        /* loop over all cells */
                begin_c_loop_all(c,t)
                {
                        C_UDMI(c,t,0)=1.e3;
                        C_UDMI(c,t,1)=200.;
                        C_UDMI(c,t,2)=0.;
                        C_UDMI(c,t,3)=0.;
                        C_UDMI(c,t,4)=0.;
                }
                end_c_loop_all(c,t)
        }
}

DEFINE_ADJUST(adjust_flux,domain)
{
  real p1, p2, flux;
  face_t f1, f2;
  Thread *t,*ft1=Lookup_Thread(domain,8);
  Thread *ft2=Lookup_Thread(domain,6);
  cell_t c,c01, c02;
  Thread *t01, *t02;
  int n;
  real c0_array[100000],c1_array[100000];
 
        n=0;
  begin_f_loop(f1,ft1)
  {
    c01=F_C0(f1,ft1);
    t01=F_C0_THREAD(f1,ft1);
    C_UDMI(c01,t01,0)=C_P(c01,t01);
        c0_array[n] = C_P(c01,t01);
        n++;
  }
  end_f_loop(f1,ft1)
        n=0;
  begin_f_loop(f2,ft2)
  {
    c02=F_C0(f2,ft2);
    t02=F_C0_THREAD(f2,ft2);
    C_UDMI(c02,t02,1)=C_P(c02,t02);
        c1_array[n] = C_P(c01,t01);
        n++;
  }
  end_f_loop(f2,ft2)
 
  if ((sizeof(c0_array) / sizeof(c0_array[0])) != (sizeof(c1_array) / sizeof(c1_array[0])))
          Message0("ERROR ********* size of face 6 is not equal to size of face 8  ********* \n");

        n=0;
       
        // You may check order in array and in cell/thread loop       
       
thread_loop_c(t,domain)
{
  begin_c_loop(c, t)
  {
    C_UDMI(c,t,2)= c0_array[n] - c1_array[n]; //transmembrane pressure=p1-p2;
        // Start -> You may check order in array and in cell/thread loop | compare C_UDMI(c,t,0) with C_UDMI(c,t,3) and C_UDMI(c,t,1) with C_UDMI(c,t,4)
        C_UDMI(c,t,3)= c0_array[n]; //transmembrane pressure=p1-p2;
        C_UDMI(c,t,4)= c1_array[n]; //transmembrane pressure=p1-p2;
        // End -> You may check order in array and in cell/thread loop
        n++;
  }
  end_c_loop(c,t)
}
}

best regards

mbhendura August 22, 2022 11:21

Quote:

Originally Posted by AlexanderZ (Post 743692)
variable should be defined in the top of function, before other instructions
try this
Code:

#include "udf.h"

DEFINE_INIT(my_init_func,d)
{
cell_t c;
Thread *t;
/* loop over all cell threads in the domain */
thread_loop_c(t,d)
        {
        /* loop over all cells */
                begin_c_loop_all(c,t)
                {
                        C_UDMI(c,t,0)=1.e3;
                        C_UDMI(c,t,1)=200.;
                        C_UDMI(c,t,2)=0.;
                        C_UDMI(c,t,3)=0.;
                        C_UDMI(c,t,4)=0.;
                }
                end_c_loop_all(c,t)
        }
}

DEFINE_ADJUST(adjust_flux,domain)
{
  real p1, p2, flux;
  face_t f1, f2;
  Thread *t,*ft1=Lookup_Thread(domain,8);
  Thread *ft2=Lookup_Thread(domain,6);
  cell_t c,c01, c02;
  Thread *t01, *t02;
  int n;
  real c0_array[100000],c1_array[100000];
 
        n=0;
  begin_f_loop(f1,ft1)
  {
    c01=F_C0(f1,ft1);
    t01=F_C0_THREAD(f1,ft1);
    C_UDMI(c01,t01,0)=C_P(c01,t01);
        c0_array[n] = C_P(c01,t01);
        n++;
  }
  end_f_loop(f1,ft1)
        n=0;
  begin_f_loop(f2,ft2)
  {
    c02=F_C0(f2,ft2);
    t02=F_C0_THREAD(f2,ft2);
    C_UDMI(c02,t02,1)=C_P(c02,t02);
        c1_array[n] = C_P(c01,t01);
        n++;
  }
  end_f_loop(f2,ft2)
 
  if ((sizeof(c0_array) / sizeof(c0_array[0])) != (sizeof(c1_array) / sizeof(c1_array[0])))
          Message0("ERROR ********* size of face 6 is not equal to size of face 8  ********* \n");

        n=0;
       
        // You may check order in array and in cell/thread loop       
       
thread_loop_c(t,domain)
{
  begin_c_loop(c, t)
  {
    C_UDMI(c,t,2)= c0_array[n] - c1_array[n]; //transmembrane pressure=p1-p2;
        // Start -> You may check order in array and in cell/thread loop | compare C_UDMI(c,t,0) with C_UDMI(c,t,3) and C_UDMI(c,t,1) with C_UDMI(c,t,4)
        C_UDMI(c,t,3)= c0_array[n]; //transmembrane pressure=p1-p2;
        C_UDMI(c,t,4)= c1_array[n]; //transmembrane pressure=p1-p2;
        // End -> You may check order in array and in cell/thread loop
        n++;
  }
  end_c_loop(c,t)
}
}

best regards

Dear Alexander,

I am solving the same problem. I tried this approach and was able to store saturation pressure (function of temperature) in my arrays. However, the sequence of storing values in each array is different from each other. Thus a direct comparison is not helpful.

my code:

thread_loop_c(t,d)
{
begin_c_loop(c,t)
{
C_CENTROID(x,c,t);
if (x[1] < 0.001001 && x[1] > 0.001){
psc[i] = exp(25.317-5144/C_T(c,t));
i++;
}}
end_c_loop(c,t)
i=0;
begin_c_loop(c,t)
{
C_CENTROID(x,c,t);
if (x[1] > 0.00123 && x[1] < 0.00124){
psh[i] = exp(25.317-5144/C_T(c,t));
C_UDMI(c,t,1) = psh[i];
i++;
}}
end_c_loop(c,t)

I printed the values of the x-coordinate(x[0]) for both c-loops to see the sequence of storing values. A sample (initial 5 values) for both loops are :
i = 0,1,2,3,4,......
loop 1: 22.783333, 22.816667, 15.05, 22.85, 15.083333,........
loop 2: 84.916667, 84.883333, 26.516667, 84.85, 118.25,....

In my understanding, these two loops should return the same values to get the correct pressure difference at each cell.

Thank you

AlexanderZ August 24, 2022 01:11

check this thread, the way to get link between cells is described there:
https://www.cfd-online.com/Forums/fl...-transfer.html

mbhendura September 2, 2022 07:38

Dear Alexander

Thank you so much. Your suggestion worked.

Can you suggest any reference for CX_Find_Cell_With_Point() function for more clarity? It is not available in the ANSYS UDF manual.


All times are GMT -4. The time now is 22:37.