-
Notifications
You must be signed in to change notification settings - Fork 4
/
torus.c
375 lines (322 loc) · 9.33 KB
/
torus.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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
#ifdef __APPLE_CC__
#include <GLUT/glut.h>
#else
#include <GL/glut.h>
#endif
//#include <GLFW/glfw3.h>
#include "math.h"
#include "stdio.h"
#include "stdlib.h"
#define PI 3.1415926
#define steps 600
/////////////////////////////integrate function////////////////////////////////
/////////////////////////////integrate function////////////////////////////////
GLdouble xRotated=0.0, yRotated=90.0, zRotated=0.0;
GLdouble xx=0.0, yy=0.0, zz=0.0 ;
GLdouble lx, ly, lz;
GLdouble innerRaidus=0.10;
GLdouble outterRaidus=1.0;
GLint sides =50;
GLint rings =50;
// angle of rotation for the camera direction
float angle = 0.0f;
// actual vector representing the camera's direction
float llx=0.0f,llz=-1.0f;
// XZ position of the camera
float cx=0.0f, cz=5.0f;
// the key states. These variables will be zero
//when no key is being presses
float deltaAngle = 0.0f;
float deltaMove = 0;
int xOrigin = -1;
/////////////////////////////////////////////////////////////////////////////
double px,py,pz;
double z[6][steps+1];
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;
}
void sphere(GLfloat radius, int slices, int stacks)
{
glPushMatrix();
glColor3f(1.0, 0.0, 0.0);
glTranslatef(xx, yy, zz);
glutWireSphere(radius, slices, stacks);
glPopMatrix();
}
void displayTorus(void)
{
glMatrixMode(GL_MODELVIEW); // clear the drawing buffer.
glClear(GL_COLOR_BUFFER_BIT); // clear the identity matrix.
glLoadIdentity();
glPushMatrix();
glTranslatef(1.0,0.0,0.0);
glColor3f(1.0, 1.0, 0.0);
glRotatef(xRotated,1.0,0.0,0.0);
glRotatef(yRotated,0.0,1.0,0.0);
glRotatef(zRotated,0.0,0.0,1.0);
glScalef(1.0,1.0,1.0);
glutSolidTorus(innerRaidus,outterRaidus,sides,rings);
glPopMatrix();
glPushMatrix();
glTranslatef(-1.0,0.0,0.0);
glColor3f(1.0, 1.0, 0.0);
glRotatef(xRotated,1.0,0.0,0.0);
glRotatef(yRotated,0.0,1.0,0.0);
glRotatef(zRotated,0.0,0.0,1.0);
glScalef(1.0,1.0,1.0);
glutSolidTorus(innerRaidus,outterRaidus,sides,rings);
glPopMatrix();
// glBegin(GL_LINES);
// glColor3f(1.0, 1.0, 1.0);
// glVertex3f(1.0,0.0,-2.5);
// glVertex3f(xx,yy,zz);
// glEnd();
// glBegin(GL_LINE_STRIP);
// glColor3f(1.0, 1.0, 1.0);
// for(angle = 0; angle <= steps; angle ++)
// {
// lx = z[5][angle];
// ly = z[4][angle];
// lz = z[3][angle]-3;
// glVertex3f(lx, ly, lz);
// }
// glEnd();
sphere(0.05, 50, 50);
glFlush();
// sawp buffers called because we are using double buffering
// glutSwapBuffers();
}
/////////////////////////////////////////////////////////////////
void computePos(float deltaMove) {
cx += deltaMove * llx * 0.1f;
cz += deltaMove * llz * 0.1f;
}
void reshapeTorus(int x, int y)
{
if (y == 0 || x == 0) return; //Nothing is visible then, so return
//Set a new projection matrix
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(80.0,(GLdouble)x/(GLdouble)y,0.01,50.0);
glViewport(0,0,x,y); //Use the whole window for rendering
if (deltaMove)
computePos(deltaMove);
// Clear Color and Depth Buffers
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
// Reset transformations
// glLoadIdentity();
// Set the camera
gluLookAt( cx, 1.0f, cz,
cx+llx, 1.0f, cz+llz,
0.0f, 1.0f, 0.0f);
}
void processNormalKeys(unsigned char key, int cxx, int cyy) {
if (key == 27)
exit(0);
}
void pressKey(int key, int cxx, int cyy) {
switch (key) {
case GLUT_KEY_UP : deltaMove = 0.5f; break;
case GLUT_KEY_DOWN : deltaMove = -0.5f; break;
}
}
void releaseKey(int key, int x, int y) {
switch (key) {
case GLUT_KEY_UP :
case GLUT_KEY_DOWN : deltaMove = 0;break;
}
}
void mouseMove(int x, int y) {
// this will only be true when the left button is down
if (xOrigin >= 0) {
// update deltaAngle
deltaAngle = (x - xOrigin) * 0.001f;
// update camera's direction
llx = sin(angle + deltaAngle);
llz = -cos(angle + deltaAngle);
}
}
void mouseButton(int button, int state, int x, int y) {
// only start motion if the left button is pressed
if (button == GLUT_LEFT_BUTTON) {
// when the button is released
if (state == GLUT_UP) {
angle += deltaAngle;
xOrigin = -1;
}
else {// state = GLUT_DOWN
xOrigin = x;
}
}
}
void idleTorus(void)
{
int i;
for (i = 0; i < steps; i++)
{
xx = z[5][i];
yy = z[4][i];
zz = z[3][i];
displayTorus();
}
}
int main (int argc, char **argv)
{
int n=6,m=steps,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];
/////////////////////initial values and step////////////////////////////
y[0]=0.0; y[1]=0.4e6; 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);
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
glutInitWindowSize(1200,1200);
glutCreateWindow("Plasma");
glPolygonMode(GL_FRONT_AND_BACK,GL_LINE);
glClearColor(0.0,0.0,0.0,0.0);
//Assign the function used in events
glutDisplayFunc(displayTorus);
glutReshapeFunc(reshapeTorus);
glutIdleFunc(idleTorus);
//Let start glut loop
glutIgnoreKeyRepeat(1);
glutKeyboardFunc(processNormalKeys);
glutSpecialFunc(pressKey);
glutSpecialUpFunc(releaseKey);
// here are the two new functions
glutMouseFunc(mouseButton);
glutMotionFunc(mouseMove);
// OpenGL init
glEnable(GL_DEPTH_TEST);
glutMainLoop();
return 0;
}
//g++ -framework OpenGL -framework GLUT torus.cpp