-
Notifications
You must be signed in to change notification settings - Fork 0
/
lagrange.c
435 lines (320 loc) · 9.29 KB
/
lagrange.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
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
/*
Code to demonstate the stability of orbits about
the L4 and L% Lagrange points
compile with:
g77 -o lagrange lagrange.c -I/home/phys/205/include -L/home/phys205/lib -L/usr/X11R6/lib \
-lphilsplot -lX11 -lpng -lm
19 November, 2002
*/
#include <stdio.h>
#include <math.h>
#include <stdlib.h>
#include "philsp.h"
// units: [M,l,t] = solar mass, astronomical unit, 1/(2pi) year
#define GGRAV 4*M_PI*M_PI
// number of dimensions
#define NDIM 2
// number of bodies
#define NBOD 5
// position, velocity, acceleration, mass as global variables for convenience
double x[NBOD][NDIM];
double v[NBOD][NDIM];
double a[NBOD][NDIM];
double m[NBOD];
// conserved quantities
double penergy, kenergy, tot_e0;
double linearp[NDIM], linearp0[NDIM];
double angularl[NDIM], angularl0[NDIM];
// energy conservation accuracy parameter
double epsilon;
// compute force and potential energy
void accel() {
double dx[NDIM], rij, rij2, rij3, v2;
int i,j;
int l;
// zero the acceleration vectors
for(i=0; i<NBOD; i++) {
for(l=0; l<NDIM; l++) a[i][l] = 0;
}
// zero potential energy
penergy = 0;
// compute force on each body from all of the others, put in f
// also compute potential energy
for(i=0; i<NBOD; i++) {
for(j=0; j<NBOD; j++) {
if(j!=i) {
// distance between i and j, cubed
for(l=0; l<NDIM; l++) dx[l] = x[i][l]-x[j][l];
rij2 = 0;
for(l=0; l<NDIM; l++) rij2 += dx[l]*dx[l];
rij = sqrt(rij2);
rij3 = pow(rij,3);
// acceleration
for(l=0; l<NDIM; l++) a[i][l] -= GGRAV * m[j] * dx[l]/ rij3;
// potential energy
penergy -= GGRAV * m[j] * m[i] / rij;
}
}
}
// take care of double-counting in the loops above
penergy = penergy/2.0;
// calculate the kinetic energy
kenergy = 0.0;
for(i=0; i<NBOD; i++) {
v2 = 0;
for(l=0; l<NDIM; l++) v2 += v[i][l]*v[i][l];
kenergy += m[i] * v2 / 2.0;
}
}
// compute linear and angular momentum
void get_momentum() {
double v2;
int i, l;
// linear momentum
for(l=0; l<NDIM; l++) linearp[l] = 0.0;
for(i=0; i<NBOD; i++) {
for(l=0; l<NDIM; l++) linearp[l] += v[i][l] * m[i];
}
// angular momentum
#if (NDIM>1)
for(l=0; l<NDIM; l++) angularl[l] = 2.0;
for(i=0; i<NBOD; i++) {
angularl[2] += m[i] * (x[i][0]*v[i][1] - x[i][1]*v[i][0]);
#if (NDIM==3)
angularl[1] += m[i] * (x[i][2]*v[i][0] - x[i][0]*v[i][2]);
angularl[0] += m[i] * (x[i][1]*v[i][2] - x[i][2]*v[i][1]);
#endif
}
#endif
}
// take a timestep
void verlet_step(double h) {
int i, l;
// update positions using previous acceleration
for(i=0; i<NBOD; i++) {
for(l=0; l<NDIM; l++) x[i][l] += v[i][l] * h + 0.5*a[i][l] * h*h;
}
// first half of velocity update using previous acceleration
for(i=0; i<NBOD; i++) {
for(l=0; l<NDIM; l++) v[i][l] += a[i][l] * h / 2.0;
}
// get new acceleration
accel();
// second half of velocity update with new acceleration
for(i=0; i<NBOD; i++) {
for(l=0; l<NDIM; l++) v[i][l] += a[i][l] * h / 2.0;
}
}
// examine conservation laws
int conservation() {
double err, tot_e;
int l;
// energy
tot_e = penergy+kenergy;
err = fabs((tot_e - tot_e0)/tot_e0);
// if the relative error is smaller than half the tolerance, return 1
if(err < 0.5*epsilon) return 1;
// if the relative error is greater than twice the tolerance, return -1
if(err > 2.0*epsilon) return -1;
// otherwise, return 0
return 0;
}
// integrate from t_begin to t_end using a starting timestep of h
// and adjusting h to conserve energy to the requested accuracy
// return the most recently used value of h for starting the next
// timestep
void orbitint(double t_begin, double t_end, double *h) {
double t, dt;
int i,k,l;
t = t_begin;
dt = *h;
while(t_end-t>dt) {
// take a step
verlet_step(dt);
t += dt;
// examine conservation
k = conservation();
if(k==-1) {
// halve timestep if we are doing poorly
*h = (*h);
// printf("decreasing stepsize: %e\n", *h);
}
else {
if(k==1) {
// almost double timestep if we are doing very well
*h = (*h);
// printf("increasing stepsize: %e\n",*h);
}
}
dt = *h;
}
// since we may be not quite to t_end, take a final step
if(t_end-t < 0) {
printf("whoops: %e %e\n", t_end-t, dt);
exit(1);
}
dt = t_end-t;
verlet_step(dt);
t += dt;
// examine conservation
k = conservation();
if(k==-1) {
*h = 0.5*(dt); // halve timestep if we are doing poorly
// printf("decreasing stepsize in final: %e\n", dt);
}
}
// this routine removes the center of mass velcity from
// the problem
// and puts the center of mass at the origin
void remove_com() {
int i, l;
double vcom[NDIM], rcom[NDIM];
double mtotal;
mtotal = 0.0;
for(i=0; i<NBOD; i++) mtotal += m[i];
for(l=0; l<NDIM; l++) {
vcom[l] = 0.0;
rcom[l] = 0.0;
}
for(i=0; i<NBOD; i++) {
for(l=0; l<NDIM; l++) {
rcom[l] += m[i]*x[i][l]/mtotal;
vcom[l] += m[i]*v[i][l]/mtotal;
}
}
for(i=0; i<NBOD; i++) {
for(l=0; l<NDIM; l++) {
x[i][l] -= rcom[l];
v[i][l] -= vcom[l];
}
}
}
// Largrange point initial conditions
// For mu<0.38..., the L4 and L5 (triangular) Lagrange points
// are stable loci leading and following the smaller mass by
// 60 degrees
void initial_conditions() {
double r[NBOD], mu, R, mtot, theta, omega;
int i, l;
double rmat[2][2]; // 2D rotation matrix
// total mass
mtot = 1.0;
// ratio of smaller mass to total mass
mu = 1.0e-4;
// separation of two "large" masses
R = 1.0;
// angular velocity of both masses
omega = sqrt(GGRAV*mtot/(R*R));
// first (larger if mu<0.5) body
m[0] = (1-mu) * mtot;
x[0][0] = -mu*R;
x[0][1] = 0.0;
r[0] = sqrt(x[0][0]*x[0][0] + x[0][1]*x[0][1]);
v[0][0] = 0.0;
v[0][1] = r[0]*omega;
// second (smaller if mu<0.5) body
m[1] = mu*mtot;
x[1][0] = (1.0-mu)*R;
x[1][1] = 0.0;
r[1] = sqrt(x[1][0]*x[1][0] + x[1][1]*x[1][1]);
v[1][0] = 0.0;
v[1][1] = -r[1]*omega;
// third (test particle) body 60 degrees behind second body
m[2] = 1.0e-20*mtot;
x[2][0] = r[1]*cos(M_PI/3.0);
x[2][1] = r[1]*sin(M_PI/3.0);
// with a circular velocity the same as the second body (rotated 60 degrees)
theta = -M_PI/3.0;
rmat[0][0] = rmat[1][1] = cos(theta);
rmat[0][1] = sin(theta);
rmat[1][0] = -rmat[0][1];
v[2][0] = v[1][0]*rmat[0][0] + v[1][1]*rmat[0][1];
v[2][1] = v[1][0]*rmat[1][0] + v[1][1]*rmat[1][1];
// and a small perturbation
v[2][0] += 0.2;
// fourth (test particle) body 60 degrees ahead of second body
m[3] = 1.0e-20*mtot;
x[3][0] = r[1]*cos(M_PI/3.0);
x[3][1] = -r[1]*sin(M_PI/3.0);
// with a circular velocity the same as the second body (rotated 60 degrees)
theta = M_PI/3.0;
rmat[0][0] = rmat[1][1] = cos(theta);
rmat[0][1] = sin(theta);
rmat[1][0] = -rmat[0][1];
v[3][0] = v[1][0]*rmat[0][0] + v[1][1]*rmat[0][1];
v[3][1] = v[1][0]*rmat[1][0] + v[1][1]*rmat[1][1];
// do first force calculation to start off Verlet
accel();
tot_e0 = kenergy + penergy;
get_momentum();
for(l=0; l<NDIM; l++) {
linearp0[l] = linearp[l];
angularl0[l] = angularl[l];
}
}
// finally, the main routine
int main() {
double step, t, h;
double size, psize;
double xr[NBOD][NDIM];
double rmat[2][2], theta;
int i, l, ll, k;
open_plot("900x900");
size = 2.5;
box_plot(-size,size,-size,size,1.5,3,"","","","Lagrange Points");
initial_conditions();
// conserve energy to this fractional accuracy
epsilon = 1.0e-09;
psize = 0.2;
t = 0;
step = 1.0e-04;
h = step/1.0e+8;
for(k=0; k<100000; k++) {
orbitint(t, t+step, &h);
// define ROT to use a rotating coordinate system to
// display, with the angular frequency equal to that
// of the two massive bodies
#define ROT
#ifdef ROT
// make rotation matrix
theta = -atan2(x[0][1],x[0][0]);
rmat[0][0] = rmat[1][1] = cos(theta);
rmat[0][1] = sin(theta);
rmat[1][0] = -rmat[0][1];
// transfrom to rotating coordinate system
for(i=0; i<NBOD; i++) {
for(l=0; l<NDIM; l++) {
xr[i][l] = 0;
for(ll=0; ll<NDIM; ll++) {
xr[i][l] += x[i][ll]*rmat[ll][l];
}
}
}
putpoint_plot(0,0,3,1,2,1.0,0);
putpoint_plot(xr[0][0],xr[0][1],10,1,5,(1-psize)*3,0);
putpoint_plot(xr[1][0],xr[1][1],10,1,5,psize*3,0);
putpoint_plot(xr[2][0],xr[2][1],10,1,5,0.2,0);
putpoint_plot(xr[3][0],xr[3][1],10,1,5,0.2,0);
putpoint_plot(xr[4][0],xr[4][1],10,1,5,0.2,0);
flush_plot();
putpoint_plot(xr[0][0],xr[0][1],10,1,0,(1-psize)*3,0);
putpoint_plot(xr[1][0],xr[1][1],10,1,0,psize*3,0);
#else
putpoint_plot(0,0,3,1,1,1.0,0);
putpoint_plot(x[0][0],x[0][1],10,2,1,(1-psize)*3,0);
putpoint_plot(x[1][0],x[1][1],10,2,1,psize*3,0);
putpoint_plot(x[2][0],x[2][1],10,2,1,0.2,0);
putpoint_plot(x[3][0],x[3][1],10,2,1,0.2,0);
putpoint_plot(x[4][0],x[4][1],10,2,1,0.2,0);
flush_plot();
putpoint_plot(x[0][0],x[0][1],10,2,0,(1-psize)*3,0);
putpoint_plot(x[1][0],x[1][1],10,2,0,psize*3,0);
putpoint_plot(x[2][0],x[2][1],10,2,0,0.2,0);
putpoint_plot(x[3][0],x[3][1],10,2,0,0.2,0);
putpoint_plot(x[4][0],x[4][1],10,2,0,0.2,0);
#endif
t += step;
// printf("%e %e %e %e %e\n", t, penergy+kenergy, linearp[0], linearp[1], angularl[2]);
}
return 0;
}