-
Notifications
You must be signed in to change notification settings - Fork 4
/
91rgs.c
159 lines (150 loc) · 4.65 KB
/
91rgs.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
#include "math.h"
#include "stdio.h"
#include "stdlib.h"
#define PI 3.1415926
/////////////////////////////integrate function////////////////////////////////
/////////////////////////////integrate function////////////////////////////////
double px,py,pz;
double lrgs(a,b,eps,f,xx,yy,zz)
double a,b,eps,(*f)(),xx,yy,zz;
{
int m,i,j;
double s,p,ep,h,aa,bb,w,xxxx,g;
double t[5]={-0.9061798459,-0.5384693101,0.0,0.5384693101,0.9061798459};
double c[5]={0.2369268851,0.4786286705,0.5688888889,0.4786286705,0.2369268851};
m=1;
px=xx;
py=yy;
pz=zz;
h=b-a; s=fabs(0.001*h);
p=1.0e+35;ep=eps+1.0;
while ((ep>=eps)&&(fabs(h)>s))
{
g=0.0;
for (i=1;i<=m;i++)
{
aa=a+(i-1.0)*h; bb=a+i*h;
w=0.0;
for (j=0;j<=4;j++)
{
xxxx=((bb-aa)*t[j]+(bb+aa))/2.0;
w=w+(*f)(xxxx)*c[j];
}
g=g+w;
}
g=g*h/2.0;
ep=fabs(g-p)/(1.0+fabs(g));
p=g; m=m+1; h=(b-a)/m;
}
return (g);
}
/////////////////////////////////ode function//////////////////////////////////
/////////////////////////////////ode function//////////////////////////////////
void rkt1(t,y,n,h,k,z,f)
void (*f) ();
int n, k;
double t,h,y[],z[];
{
int i,j,l;
double a[4], tt, *b, *d ;
b=malloc(n*sizeof(double));
d=malloc(n*sizeof(double));
a[0]=h/2.0; a[1]=a[0];
a[2]=h; a[3]=h;
for ( i = 0; i <= n-1 ; i++) z[i*k]=y[i];
for ( l = 1; l <= k-1 ; l++)
{
(*f)(t,y,n,d);
for ( i = 0; i <= n-1 ; i++) b[i]=y[i];
for ( j = 0; j <= 2; j++)
{
for ( i = 0; i <= n-1 ; i++)
{
y[i]=z[i*k+l-1]+a[j]*d[i];
b[i]=b[i]+a[j+1]*d[i]/3.0;
}
tt=t+a[j];
(*f)(tt,y,n,d);
}
for ( i = 0; i <= n-1 ; i++)
y[i]=b[i]+h*d[i]/6.0;
for ( i = 0; i <= n-1 ; i++)
z[i*k+l]=y[i];
t=t+h;
}
free(b); free(d);
return;
}
/////////////////////////fuction of magnetic field/////////////////////////////
/////////////////////////fuction of magnetic field/////////////////////////////
double bx(double xxx)
{
double xx;
xx=(pz-1)*cos(xxx)/pow((px*px+py*py+(pz-1)*(pz-1)+1-2*px*cos(xxx)-2*py*sin(xxx)),1.5)+(pz+1)*cos(xxx)/pow((px*px+py*py+(pz+1)*(pz+1)+1-2*px*cos(xxx)-2*py*sin(xxx)),1.5);
return (xx);
}
double by(double xxx)
{
double yy;
yy=(pz-1)*sin(xxx)/pow((px*px+py*py+(pz-1)*(pz-1)+1-2*px*cos(xxx)-2*py*sin(xxx)),1.5)+(pz+1)*sin(xxx)/pow((px*px+py*py+(pz+1)*(pz+1)+1-2*px*cos(xxx)-2*py*sin(xxx)),1.5);
return (yy);
}
double bz(double xxx)
{
double zz;
zz=(1-px*cos(xxx)-py*sin(xxx))*pow((px*px+py*py+(pz-1)*(pz-1)+1-2*px*cos(xxx)-2*py*sin(xxx)),-1.5)+(1-px*cos(xxx)-py*sin(xxx))*pow((px*px+py*py+(pz+1)*(pz+1)+1-2*px*cos(xxx)-2*py*sin(xxx)),-1.5);
return (zz);
}
void rkt1f(t,y,n,d)
int n;
double t, y[],d[];
{
double a,b,eps,gx,gy,gz,bx(double),by(double),bz(double);
t=t; n=n;
a=0.0; b=2*PI; eps=0.000001;
d[0]=-615479*(y[1]*lrgs(a,b,eps,bz,y[3],y[4],y[5])-y[2]*lrgs(a,b,eps,by,y[3],y[4],y[5]));
d[1]=-615479*(y[2]*lrgs(a,b,eps,bx,y[3],y[4],y[5])-y[0]*lrgs(a,b,eps,bz,y[3],y[4],y[5]));
d[2]=-615479*(y[0]*lrgs(a,b,eps,by,y[3],y[4],y[5])-y[1]*lrgs(a,b,eps,bx,y[3],y[4],y[5]));
d[3]=y[0];
d[4]=y[1];
d[5]=y[2];
return;
}
/////////////////////////////////////main function////////////////////////////////////
/////////////////////////////////////main function////////////////////////////////////
int main()
{
int n=6,m=6000,i,j,k;
double a,b,eps,gx,gy,gz,bx(double),by(double),bz(double);
a=0.0; b=2*PI; eps=0.000001;
void rkt1f(double,double [],int, double []);
double t,h,y[n],z[n][m+1];
/////////////////////initial values and step////////////////////////////
y[0]=0.0; y[1]=0.3e6; y[2]=0.15e6;y[3]=0.0;y[4]=0.0;y[5]=0.0;
t=0.0; h=6e-5/m;
/////////////////////////////create a date file/////////////////////////
FILE* fp;
fp = fopen("xyz.dat", "w");
if (!fp)
{
perror("cannot open file");
//exit(-1);
}
////////////////////////////////calculation/////////////////////////////////
////////////////////////////////calculation/////////////////////////////////
rkt1(t,y,n,h,m+1,z,rkt1f);
printf("\n");
for ( i = 0; i <=m ; i++)
{
t=i*h;
// printf("t=%5.2f\n", t);
fprintf(fp,"%13.5e " ,t );
for ( j = 0; j <= n-1 ; j++)
{
fprintf(fp,"%13.5e " ,z[j][i]);
}
fprintf(fp,"\n");
}
fclose(fp);
printf("%13.5e\n", lrgs(a,b,eps,bz,-1.0,-1.0,-1.0));
}