# distance between two cells

 Register Blogs Members List Search Today's Posts Mark Forums Read

 February 27, 2018, 01:56 distance between two cells #1 Member   annan Join Date: Nov 2016 Posts: 72 Rep Power: 9 Hello everyone, I want to compute the distance between cells in order to get dx and dy, so I thought I could use : C_CENTROID(x0,c0,t0); C_CENTROID(x1,c1,t1); real dx = x1[0]-x0[0]; real dy = x1[1]-x0[1]; But it doesn't give me the right values I should get. I also tried using connectivity macros like INTERIOR_FACE_GEOMETRY(f,t,A,ds,es,A_by_es,dr0,dr1 ) to get ds, but still don't get the right values. Thank you in advance for your help.

 February 27, 2018, 02:21 #2 Senior Member   Alexander Join Date: Apr 2013 Posts: 2,363 Rep Power: 34 how do you define c0,t0,c1,t1 here? Best regards

 February 27, 2018, 02:31 #3 Member   annan Join Date: Nov 2016 Posts: 72 Rep Power: 9 Thank you AlexanderZ for your reply. I define them like the following : t0 = THREAD_T0(t); c0 = F_C0(f,t); if (THREAD_TYPE(t)==THREAD_F_INTERIOR) { t1 = THREAD_T1(t); c1 = F_C1(f,t); ... } The overall is within a face loop.

 February 27, 2018, 02:41 #4 Senior Member   Alexander Join Date: Apr 2013 Posts: 2,363 Rep Power: 34 in chapter 2.7.3.3. Example from UDF manual you can find something that can be used for your case Code: ```/**********************************************************************/ /* UDF that implements a simplified advective term in the */ /* scalar transport equation */ /**********************************************************************/ #include "udf.h" DEFINE_UDS_FLUX(my_uds_flux,f,t,i) { cell_t c0, c1 = -1; Thread *t0, *t1 = NULL; real NV_VEC(psi_vec), NV_VEC(A), flux = 0.0; c0 = F_C0(f,t); t0 = F_C0_THREAD(f,t); F_AREA(A, f, t); /* If face lies at domain boundary, use face values; */ /* If face lies IN the domain, use average of adjacent cells. */ if (BOUNDARY_FACE_THREAD_P(t)) /*Most face values will be available*/ { real dens; /* Depending on its BC, density may not be set on face thread*/ if (NNULLP(THREAD_STORAGE(t,SV_DENSITY))) dens = F_R(f,t); /* Set dens to face value if available */ else dens = C_R(c0,t0); /* else, set dens to cell value */ NV_DS(psi_vec, =, F_U(f,t), F_V(f,t), F_W(f,t), *, dens); flux = NV_DOT(psi_vec, A); /* flux through Face */ } else { c1 = F_C1(f,t); /* Get cell on other side of face */ t1 = F_C1_THREAD(f,t); NV_DS(psi_vec, =, C_U(c0,t0),C_V(c0,t0),C_W(c0,t0),*,C_R(c0,t0)); NV_DS(psi_vec, +=, C_U(c1,t1),C_V(c1,t1),C_W(c1,t1),*,C_R(c1,t1)); flux = NV_DOT(psi_vec, A)/2.0; /* Average flux through face */ } /* ANSYS Fluent will multiply the returned value by phi_f (the scalar’s value at the face) to get the ‘‘complete’’ advective term. */ return flux; }``` Here t0 and t1 are defined in other way Code: ```t0 = F_C0_THREAD(f,t); t1 = F_C1_THREAD(f,t);``` You may try to use this approach Unfortunatelly, I can't test it Best regards

 February 27, 2018, 03:03 #5 Member   annan Join Date: Nov 2016 Posts: 72 Rep Power: 9 Thank’s for your answer. I’ve tried to implement your suggestion, but it didn’t change anything to the obtained values... Best regards

 February 27, 2018, 04:08 #6 Senior Member   Alexander Join Date: Apr 2013 Posts: 2,363 Rep Power: 34 try this code Code: ```#include "udf.h" DEFINE_ON_DEMAND(calc_distance) { Thread *t,*t0, *t1 = NULL; cell_t c0, c1 = -1; face_t f; Domain *domain; real x0[ND_ND]; real x1[ND_ND]; int n = 0; real dx = 0.0, dy = 0.0; domain = Get_Domain(1); Message("Start on demand calc_distance\n"); n = 0; thread_loop_f(t,domain) { begin_f_loop (f,t) { c0 = F_C0(f,t); t0 = F_C0_THREAD(f,t); if (BOUNDARY_FACE_THREAD_P(t)) {} else { n+=1; t1 = F_C1_THREAD(f,t); c1 = F_C1(f,t); C_CENTROID(x0,c0,t0); C_CENTROID(x1,c1,t1); dx = x1[0]-x0[0]; dy = x1[1]-x0[1]; Message("dx = %f, dy = %f, number of pairs = %d\n",dx,dy,n); } } end_f_loop (f,t) } Message("End on demand calc_distance\n"); }``` Best regards

 February 27, 2018, 07:01 #7 Member   annan Join Date: Nov 2016 Posts: 72 Rep Power: 9 Thank you very much for taking the time to write the code I just tried to run it, I get the right dx. However the dy is not right ... i get some few points right but the other values are equal to zero (everywhere) My grid is quad, uniform in the x direction (dx=constant) and variable in the y direction ( dy increases gradually). It’s an easy case normally

 Tags fluent - udf, fluent - udf - parallel, mesh 2d