CFD Online Discussion Forums

CFD Online Discussion Forums (
-   FLUENT (
-   -   for loop inside a cell_loop? (

MHDWill September 26, 2007 21:24

for loop inside a cell_loop?
Hi all,

I was wondering if there's something I have to keep in mind when I'm doing a for loop inside a predefined loop macro for FLUENT. Basically I'm trying to generate a magnetic field by adding up magnetic field contribution from multiple sources. I create these multiple sources through a for loop inside a cell loop. All constants are defined at the beginning of the code using #define, int, and/or real (#define isn't shown in the code below).

Basically, I have a lot of helmholtz coil in the system located along the z-axis. At each cell centroid, I want to calculate the magnetic field contribution from all the coils, hence I use the for loop inside the cell_loop macro. I calculate the contribution of i'th coil and add them up. bix, biy, biz is the magnetic field contribution at each cell due to all the coil.

So, after the iteration is done, I assign C_UDMI(c,t,0)=bix, C_UDMI(c,t,1)=biy, C_UDMI(c,t,2)=biz to define the magnetic field strength at each cell centroid.

But when I do this and generate a contour plot, the magnetic field is somewhat uniform rather than what we expect to see in a helmholtz coil.

Is there a problem how I define the variables in int and real?

The code is shown below.

DEFINE_INIT(Define_B_Field,d) {

int i;

real dx, theta, phi, px, p, x[ND_ND],r, rx,

rhomag, themag, ci, ct, co, bix,biy,biz ;

Thread *t;

cell_t c;

ci = ri*ri/fabs(nix); constant

co = ro*ro/fabs(nox); constant

ct = (pow(ci*nix,0.5)-rt)/(nix*nix); constant

dx=lsys/nc; /*increment between coils in meters */

/*Now calculating magnetic field @ each cell centroid*/









for (i;i<=nc;i++)


rx= -lsys/2 + (i-1)*dx;

if (rx < cp)


r = pow(ci*fabs(rx),0.5);


else if (rx > cp & rx < dp)




else if (rx > dp)


r = pow(co*fabs(rx),0.5);


px=x[0]-rx; /*x-coordinate of the cell centroid with respect to the current (i'th) coil center*/

p=pow(px*px + x[1]*x[1] + x[2]*x[2],0.5); /*Position vector magnitude of cell center from center of the "i'th" Helmholz coil*/

theta=atan2((pow(px*px+x[1]*x[1],0.5)),x[2]);/*Longitudinal angle from z-axis*/

phi = atan2(x[1],px); /*Azimuthal angle in the xy-plane with respect to z-axis*/

rhomag= pow( cos(theta)*cos(phi)*cos(theta)*cos(phi) + sin(theta)*sin(phi)*sin(theta)*sin(phi)+cos(theta) *cos(theta),0.5);

themag= pow( p*p*(sin(theta)*sin(phi)*sin(theta)*sin(phi) + cos(theta)*sin(phi)*cos(theta)*sin(phi)+sin(theta) *sin(theta)),.5);

bix +=permeability*(I/2*r*r*cos(theta)*(1+(15*r*r*p*p*sin(theta)*sin(the ta))/(4*pow(r*r+p*p,2)))/(pow(r*r+p*p,3/2))*cos(theta)*sin(phi)/rhomag-I/4*sin(theta)*(2*r*r - p*p +(15*r*r*p*p*sin(theta)*sin(theta)*(4*r*r - 3*p*p)/(8*pow(r*r + p*p,2))))/(pow(r*r + p*p,5/2))*p*sin(theta)*sin(phi)/themag); /*x-component of magnetic field for each helmholtz coil*/

biy +=permeability*(I/2*r*r*cos(theta)*(1+(15*r*r*p*p*sin(theta)*sin(the ta))/(4*pow(r*r+p*p,2)))/(pow(r*r+p*p,3/2))*sin(theta)*sin(phi)/rhomag-I/4*sin(theta)*(2*r*r - p*p +(15*r*r*p*p*sin(theta)*sin(theta)*(4*r*r - 3*p*p)/(8*pow(r*r + p*p,2))))/(pow(r*r + p*p,5/2))*p*cos(theta)*sin(phi)/themag); /*y-component of magnetic field for each helmholtz coil*/

biz +=permeability*(I/2*r*r*cos(theta)*(1+(15*r*r*p*p*sin(theta)*sin(the ta))/(4*pow(r*r+p*p,2)))/(pow(r*r+p*p,3/2))*cos(theta)/rhomag+I/4*sin(theta)*(2*r*r - p*p +(15*r*r*p*p*sin(theta)*sin(theta)*(4*r*r - 3*p*p)/(8*pow(r*r + p*p,2))))/(pow(r*r + p*p,5/2))*sin(theta)*p/themag); /*z-component of magnetic field for each helmholtz coil*/







} }

All times are GMT -4. The time now is 11:49.