You are on page 1of 6

#include

#include
#include
#include
#include

<stdlib.h>
<stdio.h>
<math.h>
<time.h>
<conio.h>

double E(double Q[], double X[], double Y[], int n, double x, double y, double e
[])
{
int j;
double sum=0;
for(j=0; j<n; j++){
e[j] = 0;
}
for(j=0; j<n; j++){
e[j] = ((Q[j]*(x-X[j]))/(pow(((x-X[j])*(x-X[j]) + (y
-Y[j])*(y-Y[j])), 1.5)));
}
for(j=0; j<n; j++){
sum += e[j];
}
return sum;
}
double vydot(double X[], double Y[], double Q[], int n, double e[], int j1, int
i1, double rk[])
{
int j2;
double sum=0, c=0;
for(j2=0; j2<n; j2++){
e[j2] = 0;
}
for(j2=0; j2<n; j2++){
if(i1==0){c=0;}
if(i1==1 || i1==2){c=0.5;}
if(i1==3){c=1;}
if(j2 == j1){e[j2]=0;}
else{
e[j2] = (0.025*Q[j1]*(Q[j2]*((X[j1]+c*rk[4*j1 ])-(X[j2]+c*rk[4*
j2 ])))/
(pow((((X[j1]+c*rk[4*j1 ])-(X[j2]+c*rk[4*
j2 ]))*((X[j1]+c*rk[4*j1 ])-(X[j2]+c*rk[4*j2 ]))
+ ((Y[j1]+c*rk[4*j1+2])-(Y[j2]+c*rk[4*
j2+2]))*((Y[j1]+c*rk[4*j1+2])-(Y[j2]+c*rk[4*j2+2]))), 1.5)));
}
}
for(j2=0; j2<n; j2++){
sum += e[j2];
}
return sum;
}

double vxdot(double X[], double Y[], double Q[], int n, double e[], int j1, int
i1, double rk[])
{
int j2;
double sum=0, c;
for(j2=0; j2<n; j2++){
e[j2] = 0;
}
for(j2=0; j2<n; j2++){
if(i1==0){c=0;}
if(i1==1 || i1==2){c=0.5;}
if(i1==3){c=1;}
if(j2 == j1){e[j2]=0;}
else{
e[j2] = (0.025*Q[j1]*(Q[j2]*((X[j1]+c*rk[4*j1 ])-(X[j2]+c*rk[4*
j2 ])))/
(pow((((X[j1]+c*rk[4*j1 ])-(X[j2]+c*rk[4*
j2 ]))*((X[j1]+c*rk[4*j1 ])-(X[j2]+c*rk[4*j2 ]))
+ ((Y[j1]+c*rk[4*j1+2])-(Y[j2]+c*rk[4*
j2+2]))*((Y[j1]+c*rk[4*j1+2])-(Y[j2]+c*rk[4*j2+2]))), 1.5)));
}
}
for(j2=0; j2<n; j2++){
sum += e[j2];
}
return sum;
}
int main(void)
{
int n=1, i, i1, j1, k1, m1, j, k, m, N=12000, t;
double x, y, Ex, Ey, dx, dy, dl=0.01, qtemp, temp;
FILE *fout, *fin, *chargepos, *chargeneg;
if((fin=fopen("data.txt", "r"))==NULL)
{
fprintf(stdout, "cannot open data.txt\n");
exit (EXIT_FAILURE);
}
while(!feof(fin))
{
int c;
c = fgetc(fin);
if (c == '\n')
n++;
}
if((fout=fopen("general.txt", "w"))==NULL)
{
fprintf(stdout, "cannot open general.txt\n");
exit (EXIT_FAILURE);
}
double Q[n], q[n], X[n], Y[n], e[n], D[n], Vx[n], Vy[n], rk1[4*n], rk2[4*n],

rk3[4*n], rk4[4*n], null[4*n];


if((fin=fopen("data.txt", "r"))==NULL)
{
fprintf(stdout, "cannot open data.txt\n");
exit (EXIT_FAILURE);
}
for(i=0; i<n; i++){
fscanf(fin, "%lf %lf %lf %lf %lf\n", &qtemp, &X[i], &Y[i], &Vx[i],
&Vy[i]);
Q[i] = qtemp;
q[i] = -qtemp;
}
for (i=0; i<n; i++){
printf("\n");
printf("Charge: %fC\t x: %f\t y: %f\n Vx: %f\t Vy: %f\n", Q[i], X[i], Y[i],
Vx[i], Vy[i]);
}
for(j=0; j<4*n; j++){
null[j]
rk1 [j]
rk2 [j]
rk3 [j]
rk4 [j]
}

=
=
=
=
=

0;
0;
0;
0;
0;

for(t=0; t<300; t++){

//set all runge-kutta arrays to 0

//300 Iterations of the algo

rithm
printf("%d\t", t);
}
for(i1=0; i1<4; i1++){
for(j1=0; j1<n; j1++){
if(i1==0){
rk1[4*j1] = 0.025*Vx[j1];
rk1[4*j1+1] = vxdot(X, Y, Q, n, e, j1, i1,
null);

//first run of RK
rk1[4*j1+2] = 0.025*Vy[j1];
rk1[4*j1+3] = vydot(X, Y, Q, n, e, j1, i1,

null);
}
if(i1==1){
rk2[4*j1] = 0.025*Vx[j1];
rk2[4*j1+1] = vxdot(X, Y, Q, n, e, j1, i1,
rk1);
rk2[4*j1+2] = 0.025*Vy[j1];
//second run of RK
rk2[4*j1+3] = vydot(X, Y, Q, n, e, j1, i1,
rk1);
}
if(i1==2){
rk3[4*j1] = 0.025*Vx[j1];
rk3[4*j1+1] = vxdot(X, Y, Q, n, e, j1, i1,

rk2);
rk3[4*j1+2] = 0.025*Vy[j1];
rk3[4*j1+3] = vydot(X, Y, Q, n, e, j1, i1,
rk2);
}
if(i1==3){
rk4[4*j1] = 0.025*Vx[j1];
rk4[4*j1+1] = vxdot(X, Y, Q, n, e, j1, i1,
rk3);
rk4[4*j1+2] = 0.025*Vy[j1];
rk4[4*j1+3] = vydot(X, Y, Q, n, e, j1, i1,
rk3);
}
}

*k1] + rk4[4*k1])/6;
bles

for(k1=0; k1<n; k1++){


X[k1] += (rk1[4*k1] + 2*rk2[4*k1] + 2*rk3[4
//find the next values for all the varia
Y[k1] += (rk1[4*k1 + 2] + 2*rk2[4*k1 + 2] +

2*rk3[4*k1 + 2] + rk4[4*k1 + 2])/6;


Vx[k1] += (rk1[4*k1 + 1] + 2*rk2[4*k1 + 1] +
2*rk3[4*k1 + 1] + rk4[4*k1 + 1])/6;
Vy[k1] += (rk1[4*k1 + 3] + 2*rk2[4*k1 + 3] +
2*rk3[4*k1 + 3] + rk4[4*k1 + 3])/6;
}
if((chargepos=fopen("charge+.txt", "w"))==NULL)
{
fprintf(stdout, "can
not open charge+.txt\n");
exit (EXIT_FAILURE);
}
for(k1=0; k1<n; k1++){
if(Q[k1]>0){
fprintf(chargepos, "%f\t%f\n", X[k1], Y[k1]);
//positive and negative charges
}
//plot seperate files for
}
fclose(chargepos);
if((chargeneg=fopen("charge-.txt", "w"))==NULL)
{
fprintf(stdo
ut, "cannot open charge-.txt\n");
exit (EXIT_
FAILURE);
}
for(k1=0; k1<n; k1++){
if(Q[k1]<0){
fprintf(chargeneg, "%f\t%f\n", X[k1], Y[k1]);
}
}
fclose(chargeneg);

}
if((fout=fopen("general.txt", "w"))==NULL)
{
fprintf(stdout, "cannot open
general.txt\n");
exit (EXIT_FAILURE);
}
for(i=0; i<n; i++){
for(m=0; m<20; m++){
x = X[i] +dl*cos(M_PI*(m + (double)i/n)/10);
y = Y[i] + dl*sin(M_PI*(m + (double)i/n)/10);
//plot the electic field lines
for(k=1; k<N; k++){
if(Q[i]>0){
Ex = E(Q, X, Y, n, x,
y, e);
Ey = E(Q, Y, X, n, y,
x, e);
}
else{
;

Ex = E(q, X, Y, n, x, y, e)
//use the inverted charge array for

Ey = E(q, Y, X, n, y, x, e)
//field lines starting from negative charges
}
dx = (dl*Ex)/(sqrt(Ex*Ex + Ey*Ey));
dy = (dl*Ey)/(sqrt(Ex*Ex + Ey*Ey));
x += dx;
y += dy;

//

printf("%f\t%f\n", x, y);
fprintf(fout, "%f\t%f\n", x, y);

//

if(fabs(x)>100.1 || fabs(y)>5.1

){
//
//

-x) + (Y[j]-y)*(Y[j]-y));

k=N+1;
}
for(j=0; j<n; j++){
D[j] = sqrt((X[j]-x)*(X[j]
//end the plot if the line gets too close
}
//to another charge

for(j=0; j<n; j++){


if(D[j]<0.01){
j = n+1;
k = N+1;
}
}
}
fprintf(fout, "\n");
}
}
fclose(fout);
FILE *gnuplotPipe = popen("C:\\Users\\Patrick\\Docume
nts\\Programming\\gnuplot\\binary\\gnuplot","w");
if (gnuplotPipe) {
fprintf(gnuplotPipe, "set terminal png nocrop enhanced size
1920,1080; set output 'eom%d.png'\n", (t+1));
fprintf(gnuplotPipe, "set title 'frame%d'\n", (t+1));
fprintf(gnuplotPipe, "set multiplot\n");
fprintf(gnuplotPipe, "set xrange [-20:20]; set yrange [-20:
20]\n");
fprintf(gnuplotPipe, "unset key\n");
fprintf(gnuplotPipe, "plot'C:\\Users\\Patrick\\Documents\\P
rogramming\\E-field\\general.txt' with lines lw 2 \n");
fprintf(gnuplotPipe, "plot 'C:\\Users\\Patrick\\Documents\\
Programming\\E-field\\charge-.txt' with points pt 7 ps 3 lc rgb 'black'\n");
fprintf(gnuplotPipe, "plot 'C:\\Users\\Patrick\\Documents\\
Programming\\E-field\\charge+.txt' with points pt 7 ps 3 lc rgb 'green'\n");
fprintf(gnuplotPipe, "set nomultiplot\n");
fflush(gnuplotPipe);
fflush(stderr);
fprintf(gnuplotPipe,"exit \n");
pclose(gnuplotPipe);
}

}
}

You might also like