#include<stdio.h> #include<math.h> int main(){ //Set up the array int i,j; int count=1; float ok=0.0001; float diff=0; float max=0.002; double v[100][100]; double u[100][100]; int flag=0; for(i=0; i<100; i++){ for(j=0; j<100; j++){v[i][j]=0.0;u[i][j]=0.0;}} float w=1.93908; float alpha=0; //The SOR Method: Avoid boundaries //A grounded box surrounds a central //plate held at potential located at i=49 //from j=25 to 75 for(j=25;j<=75;j++){v[49][j]=1.0;u[49][j]=1.0;} //A while(max>ok){ printf("%f\n",max); //(B-C) max=ok; count+=1; for(i=1; i<99; i++){ //B for(j=1; j<99; j++){ //C if(i==49)if(j>=25)if(j<=75){flag=100;} \\check for boundry if(i==49)if(j<25){flag=0;} if(i==49)if(j>75){flag=0;} if(flag<1){alpha=0.25*(v[i+1][j]+u[i-1][j]+v[i][j+1]+u[i][j-1]);} if(flag<1){u[i][j]=v[i][j]+w*(alpha-v[i][j]);} diff=fabs(v[i][j]-u[i][j]); if(diff>max){max=diff;} }} //~B ~C //~(B-C) for(i=1; i<99; i++){for(j=1; j<99; j++){v[i][j]=u[i][j];}} }//~A //Print to file FILE *fp; fp=fopen("so.txt","w"); for(i=0; i<100; i++){for(j=0; j<100; j++){ fprintf(fp,"%i %i %f \n", i,j,v[i][j]);}fprintf(fp,"\n");} printf("SOR took %i iterations.\n",count); }
The above code plots a simple bar at potential V. We can modify it for our hyperbolic electordes as,
#include<stdio.h> #include<math.h> int main(){ //Set up the array int i,j; int count=1; float ok=0.0001; float diff=0; float max=0.002; double v[1000][1000]; double u[1000][1000]; int flag=0; for(i=0; i<1000; i++){ for(j=0; j<1000; j++){v[i][j]=0.0;u[i][j]=0.0;}} float w=1.93908; float alpha=0; //Set hyperbolic conditions: // Upper for(j=250;j<=750;j++){ i=10*(int)floor(50+sqrt(100+2.4*(j/10-50)*(j/10-50))); v[i][j]=1.0; v[i+1][j]=1.0; v[i-1][j]=1.0; v[i][j+1]=1.0; \ v[i][j-1]=1.0; u[i][j]=1.0; u[i+1][j]=1.0; u[i-1][j]=1.0; u[i][j+1]=1.0; \ u[i][j-1]=1.0;} //Lower for(j=250;j<=750;j++){ i=10*(int)floor(50-sqrt(100+2.4*(j/10-50)*(j/10-50))); v[i][j]=1.0; v[i+1][j]=1.0; v[i-1][j]=1.0; v[i][j+1]=1.0; \ v[i][j-1]=1.0; u[i][j]=1.0; u[i+1][j]=1.0; u[i-1][j]=1.0; u[i][j+1]=1.0; \ u[i][j-1]=1.0;} // Left for(i=250;i<=750;i++){ j=10*(int)floor(50+sqrt(100+2.4*(i/10-50)*(i/10-50))); v[i][j]=-1.0; v[i+1][j]=-1.0; v[i-1][j]=-1.0; v[i][j+1]=-1.0; \ v[i][j-1]=-1.0; u[i][j]=-1.0; u[i+1][j]=-1.0; u[i-1][j]=-1.0; u[i][j+1]=-1.0; \ u[i][j-1]=-1.0;} //Right for(i=250;i<=750;i++){ j=10*(int)floor(50-sqrt(100+2.4*(i/10-50)*(i/10-50))); v[i][j]=-1.0; v[i+1][j]=-1.0; v[i-1][j]=-1.0; v[i][j+1]=-1.0; \ v[i][j-1]=-1.0; u[i][j]=-1.0; u[i+1][j]=-1.0; u[i-1][j]=-1.0; u[i][j+1]=-1.0; \ u[i][j-1]=-1.0;} //A while(max>ok){ printf("%f\n",max); //(B-C) max=ok; count+=1; for(i=1; i<999; i++){ //B for(j=1; j<999; j++){ //C // if(v[i][j]>1){v[i][j]=1.0;} //Strays // if(v[i][j]<-1){v[i][j]=-1.0;} //if(v[i][j]<=-1.0){flag=100;} if(v[i][j]>=1){flag=100;} if(v[i][j]<=-1){flag=100;} if(v[i][j]<1 && v[i][j]>-1){flag=0;} //Problem: what if more if(flag<1){alpha=0.25*(v[i+1][j]+u[i-1][j]+v[i][j+1]+u[i][j-1]);} if(flag<1){ u[i][j]=v[i][j]+w*(alpha-v[i][j]); if(u[i][j]>1){u[i][j]=1;} if(u[i][j]<-1){u[i][j]=-1;}} diff=fabs(v[i][j]-u[i][j]); if(diff>max){max=diff;} }} //~B ~C //~(B-C) for(i=1; i<999; i++){for(j=1; j<999; j++){v[i][j]=u[i][j];}} }//~A //Print to file FILE *fp; fp=fopen("so2.txt","w"); int num=0; int numy=0; for(i=0; i<1000; i++){for(j=0; j<1000; j++){ fprintf(fp,"%i %i %f \n", i,j,v[i][j]);} fprintf(fp,"\n"); numy++; } printf("SOR took %i iterations.\n",count); }
We also used the following gnuplot script, where our previous program's output put a blank line following each j cycle to conform with gnuplot's contour method:
set data style lines set terminal postscript #set nokey set cntrparam levels 20 set output 'hyper1.ps' set title "Hyperbolic Electrodes via SOR, n=1000, Tolerate=0.0001" splot 'so.txt' set contour set nosurface set view 0,0 set output 'hyper2.ps' splot 'so.txt'