CFD Online Logo CFD Online URL
www.cfd-online.com
[Sponsors]
Home > Forums > Software User Forums > ANSYS > FLUENT > Fluent UDF and Scheme Programming

Calculating a variable based on the data from two different cell zones

Register Blogs Community New Posts Updated Threads Search

Like Tree2Likes
  • 1 Post By AlexanderZ
  • 1 Post By AlexanderZ

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
Old   August 21, 2019, 13:47
Default Calculating a variable based on the data from two different cell zones
  #1
New Member
 
Jason FANG
Join Date: Aug 2019
Posts: 6
Rep Power: 7
Jasonf is on a distinguished road
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?

Last edited by Jasonf; August 22, 2019 at 22:36.
Jasonf is offline   Reply With Quote

Old   August 22, 2019, 01:34
Default
  #2
Senior Member
 
Alexander
Join Date: Apr 2013
Posts: 2,363
Rep Power: 34
AlexanderZ will become famous soon enoughAlexanderZ will become famous soon enough
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 likes this.
AlexanderZ is offline   Reply With Quote

Old   August 22, 2019, 02:04
Default
  #3
New Member
 
Jason FANG
Join Date: Aug 2019
Posts: 6
Rep Power: 7
Jasonf is on a distinguished road
@AlexanderZ,thank you! Your help is very helpful to me, and I will try it.
Jasonf is offline   Reply With Quote

Old   September 1, 2019, 12:03
Default
  #4
New Member
 
Jason FANG
Join Date: Aug 2019
Posts: 6
Rep Power: 7
Jasonf is on a distinguished road
Quote:
Originally Posted by AlexanderZ View Post
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!
Attached Images
File Type: jpg Illustration of question.jpg (94.5 KB, 25 views)
Jasonf is offline   Reply With Quote

Old   September 2, 2019, 02:46
Default
  #5
Senior Member
 
Alexander
Join Date: Apr 2013
Posts: 2,363
Rep Power: 34
AlexanderZ will become famous soon enoughAlexanderZ will become famous soon enough
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
AlexanderZ is offline   Reply With Quote

Old   September 3, 2019, 00:53
Default
  #6
New Member
 
Jason FANG
Join Date: Aug 2019
Posts: 6
Rep Power: 7
Jasonf is on a distinguished road
Quote:
Originally Posted by AlexanderZ View Post
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)

......

Last edited by Jasonf; September 3, 2019 at 04:40.
Jasonf is offline   Reply With Quote

Old   September 3, 2019, 05:17
Default
  #7
Senior Member
 
Alexander
Join Date: Apr 2013
Posts: 2,363
Rep Power: 34
AlexanderZ will become famous soon enoughAlexanderZ will become famous soon enough
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
AlexanderZ is offline   Reply With Quote

Old   August 22, 2022, 12:21
Default
  #8
New Member
 
Manish Bhendura
Join Date: Apr 2018
Posts: 3
Rep Power: 8
mbhendura is on a distinguished road
Quote:
Originally Posted by AlexanderZ View Post
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
mbhendura is offline   Reply With Quote

Old   August 24, 2022, 02:11
Default
  #9
Senior Member
 
Alexander
Join Date: Apr 2013
Posts: 2,363
Rep Power: 34
AlexanderZ will become famous soon enoughAlexanderZ will become famous soon enough
check this thread, the way to get link between cells is described there:
Writing a UDF for species mass transfer
mbhendura likes this.
__________________
best regards


******************************
press LIKE if this message was helpful
AlexanderZ is offline   Reply With Quote

Old   September 2, 2022, 08:38
Default
  #10
New Member
 
Manish Bhendura
Join Date: Apr 2018
Posts: 3
Rep Power: 8
mbhendura is on a distinguished road
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.

Last edited by mbhendura; September 5, 2022 at 01:56.
mbhendura is offline   Reply With Quote

Reply

Tags
adjacent cell layer, define_adjust, multiple cell zones


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
[Other] refineWallLayer Error Yuby OpenFOAM Meshing & Mesh Conversion 2 November 11, 2021 12:04
Creatin macros for multiple data sets and assigning multiple zones Skyler Tecplot 0 January 24, 2019 13:24
paraview - cell data or point data on plot over line bye bye my blue OpenFOAM 0 December 13, 2016 07:07
Exporting data (cell values, ascii) and obtaining more data than cells! TfG FLUENT 3 April 3, 2015 01:18
[OpenFOAM] Cell Data to Point Data Issues mcintoshjamie ParaView 2 November 19, 2009 04:55


All times are GMT -4. The time now is 21:54.