/* Solves Laplaces equation in a square Cartesian plane */ /* bounded by conducting walls. Compile with; */ /* gcc -O4 -o Laplace 2dlaplace.c -L/usr/X11R6/lib -lplot -lXaw -lXmu -lXt -lSM -lICE -lXext -lX11 -lm */ /* The output reguires GNU plotutils-2.2 or above */ /* J. R. Schmidt 1999 */ /* Voltage illustrated with coloration and height; plots z=V(x,y) */ /* Entire lattice is massivley vectorized to eliminate cache misses */ /* and to make stride considerations irrelevent. */ /* Point V(x,y) is translated to V[x+N*y] */ /* keep this in mind when you impose B.C.s */ #include #include #include #include #include #define N 61 /* should be odd , this is grid size */ /* creates a N X N grid, with middle being x-y axes */ /* rotates figure by two Euler angles for different */ /* camera angles */ #define PI 3.1415927 double V[N*N],Vnew[N*N],X[N*N],Y[N*N],Xn[N*N],Yn[N*N]; double theta,phi,tmpx,tmpy,tmpz,dx,dy,dt,x,y,dimensionx, dimensiony,t,V0; double ct,st,sp,cp; double axes[4][2]; int mod(int A, int B); int n,m,k,l; int pl_handle,timestep,num,COLOR,displ; FILE *fptr; main (int argc, char *argv[]) { V0=6.0; /* sets maximum voltage scale */ dx = 0.2; dy = 0.2; dt = 0.05; dimensionx=(double)N*dx; dimensiony=(double)N*dy; /* Do some error checking to avoid segmentation faults */ /* I hate segmentation faults. */ if(argc < 5 || argc > 5){ printf("usage: Laplace theta phi timesteps display\n"); printf("Display=0 for X, 1 for GIF\n"); exit(1);} theta=(double)atof(argv[1]); /* azimuthal rotation angle */ phi=(double)atof(argv[2]); /* xy-plane rotation angle */ num=atoi(argv[3]); /* number of time steps in relaxation */ displ=atoi(argv[4]); cp=cos(phi); sp=sin(phi); ct=cos(theta); st=sin(theta); if(displ==0) pl_handle = pl_newpl ("X", stdin, stdout, stderr); else{ fptr=fopen("laplace.gif","w+"); pl_parampl ("BITMAPSIZE", "500x500"); pl_parampl ("BG_COLOR", "white"); pl_parampl ("TRANSPARENT_COLOR", "white"); pl_handle = pl_newpl ("GIF", stdin, fptr, stderr);} pl_selectpl (pl_handle); if (pl_openpl () < 0) /* open Plotter */ { fprintf (stderr, "Couldn't open Plotter\n"); return 1; } /* set scales for the size of the plotting window */ pl_fspace (-dimensionx, -dimensiony,dimensionx, dimensiony); pl_flinewidth (0.01); /* this is why N must be odd */ axes[0][0]=-(double)(N+1)*dx/2; /* points to draw x,y axes */ axes[0][1]=0.0; axes[1][0]=(double)(N+1)*dx/2; axes[1][1]=0.0; axes[2][1]=-(double)(N+1)*dy/2; axes[2][0]=0.0; axes[3][1]=(double)(N+1)*dy/2; axes[3][0]=0.0; for(n=0;n=0) return(A-B*(A/B)); else{ do{A=A+B;}while(A<0); return(A);} }