-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathMiniNBody.c
1900 lines (1806 loc) · 75.1 KB
/
MiniNBody.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
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*-------------------------------------------------------------
* Title : MiniNBody.c
* Author : Léo Martire, Michael Richmond.
* Date : 2016.
* Description :
Implementation of a simple N-Body simulation. This program is
amply based on Michael Richmond's NBody program. Tweaks,
optimisations, more features and advanced schemes have been
added in order to fulfil a study on globular clusters.
* Usage :
1) Compile using the provided Makefile.
2) As command line :
nbody input=inputfile [verbose=]
where :
- inputfile is an ASCII text file with description of
each body, plus some controlling parameters
(see provided input file solarSystem.in for
further explanations)
- verbose if present, set verbosity level to 1
if not, set verbosity level to 0
- verbose=N if present, set verbosity level to N
-------------------------------------------------------------*/
/* Includes. ------------------------------------------------*/
#include <math.h>
#include <omp.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
/*-----------------------------------------------------------*/
/* Defines. -------------------------------------------------*/
#undef DEBUG
#define COMMENT_CHAR '#' // any line starting with this in input file is ignored
#define LINELEN 1024 // max length of lines in input file
#define MAX_NBODY 9000 // maximum number of bodies we can handle (change this according to the type of simulation : no need to allocate 9000 spaces to simulate 10 particles)
#define MAX_TECHNIQUE 10 // maximum number of intergration techniques we can handle
#define NAMELEN 30 // max length of names of objects
#define NBUF 1024
/*-----------------------------------------------------------*/
/* Type definitions. ----------------------------------------*/
typedef int(*PFI)(double, double *); // pointer to a function returning an integer
typedef struct s_vector { // vector
double val[3];
}
VECTOR;
typedef struct s_body { // body (state)
int index;
char name[NAMELEN];
double mass;
double pos[3];
double vel[3];
}
BODY;
typedef struct s_dbody { // body derivative (state derivative)
double dp[3];
double dv[3];
}
DBODY;
typedef struct s_technique { // integration technique
char name[NAMELEN];
PFI func;
}
TECHNIQUE;
/*-----------------------------------------------------------*/
/* Macros. --------------------------------------------------*/
/*
* set the given VECTOR elements to have the difference in
* positions between bodies i and j, in the sense
* g_body_array[i].pos[0]-g_body_array[j].pos[0]
* etc.
*/
#define SET_POS_DIFF(vec, i, j)\
vec.val[0] = g_body_array[i].pos[0] - g_body_array[j].pos[0];\
vec.val[1] = g_body_array[i].pos[1] - g_body_array[j].pos[1];\
vec.val[2] = g_body_array[i].pos[2] - g_body_array[j].pos[2];
/* the magnitude of a VECTOR */
#define VECTOR_MAG(vec)\
(sqrt(vec.val[0] * vec.val[0] + vec.val[1] * vec.val[1] + \
vec.val[2] * vec.val[2]))
/* the square of the magnitude of a VECTOR */
#define VECTOR_MAG_SQUARED(vec)\
(vec.val[0] * vec.val[0] + vec.val[1] * vec.val[1] + \
vec.val[2] * vec.val[2])
/* the SQUARE of distance between two bodies */
#define DIST_SQUARED(i, j)\
(((g_body_array[i].pos[0] - g_body_array[j].pos[0]) * \
(g_body_array[i].pos[0] - g_body_array[j].pos[0])) + \
((g_body_array[i].pos[1] - g_body_array[j].pos[1]) * \
(g_body_array[i].pos[1] - g_body_array[j].pos[1])) + \
((g_body_array[i].pos[2] - g_body_array[j].pos[2]) * \
(g_body_array[i].pos[2] - g_body_array[j].pos[2])))
/* the difference of two vectors */
#define VECTOR_SUBTRACT(a, b, difference)\
difference.val[0] = a.val[0] - b.val[0];\
difference.val[1] = a.val[1] - b.val[1];\
difference.val[2] = a.val[2] - b.val[2];
/* the cross product of two vectors */
#define VECTOR_CROSS(a, b, product)\
product.val[0] = a.val[1] * b.val[2] - a.val[2] * b.val[1];\
product.val[1] = a.val[2] * b.val[0] - a.val[0] * b.val[2];\
product.val[2] = a.val[0] * b.val[1] - a.val[1] * b.val[0];
#define max(a,b) \
({ __typeof__ (a) _a=(a); \
__typeof__ (b) _b=(b); \
_a>_b ? _a : _b; })
#define min(a,b) \
({ __typeof__ (a) _a=(a); \
__typeof__ (b) _b=(b); \
_a<_b ? _a : _b; })
/*-----------------------------------------------------------*/
/* Global variables. ----------------------------------------*/
BODY g_body_array[MAX_NBODY]; // allocate space for bodies
BODY pyn1[MAX_NBODY]; // PECE : predicted state
BODY yn1[MAX_NBODY]; // PECE : new state
DBODY fn1[MAX_NBODY]; // PECE : new derivative state
DBODY fp0[MAX_NBODY]; // PECE : current derivative state
DBODY fp1[MAX_NBODY]; // PECE : 1 step backwards derivative state
DBODY fp2[MAX_NBODY]; // PECE : 2 steps backwards derivative state
DBODY fp3[MAX_NBODY]; // PECE : 3 steps backwards derivative state
DBODY pfn1[MAX_NBODY]; // PECE : predicted derivative state
FILE *g_outfile_fp; // FILE pointer for output
PFI g_integration_func; // PFI pointer for techniques
TECHNIQUE g_technique_array[MAX_TECHNIQUE]; // allocate space for techniques
VECTOR g_cur_forces[MAX_NBODY][MAX_NBODY]; // variable made global to make sure enough memory space is allocated and to enable faster access for simulations with high number of bodies.
VECTOR tmpForcesForDerivation[MAX_NBODY]; // PECE : temporary vector for derivative state calculation
double g_AB_b03 = 55.0 / 24.0; // PECE : coefficient for Adams-Bashforth scheme with 3+1 steps
double g_AB_b13 = -59.0 / 24.0; // PECE : coefficient for Adams-Bashforth scheme with 3+1 steps
double g_AB_b23 = 37.0 / 24.0; // PECE : coefficient for Adams-Bashforth scheme with 3+1 steps
double g_AB_b33 = -9.0 / 24.0; // PECE : coefficient for Adams-Bashforth scheme with 3+1 steps
double g_AM_b03 = 646.0 / 720.0; // PECE : coefficient for Adams-Moulton scheme with 3+1 steps
double g_AM_b13 = -264.0 / 720.0; // PECE : coefficient for Adams-Moulton scheme with 3+1 steps
double g_AM_b23 = 106.0 / 720.0; // PECE : coefficient for Adams-Moulton scheme with 3+1 steps
double g_AM_b33 = -19.0 / 720.0; // PECE : coefficient for Adams-Moulton scheme with 3+1 steps
double g_AM_bp3 = 251.0 / 720.0; // PECE : coefficient for Adams-Moulton scheme with 3+1 steps
double g_G = -1; // gravitational constant (in [L^{3}][M^{-1}][T^{-2}], (to be set by reading the input file))
double g_ab = -1; // galactic parameter (to be set by reading the input file)
double g_ad = -1; // galactic parameter (to be set by reading the input file)
double g_ah = -1; // galactic parameter (to be set by reading the input file)
double g_duration = -1; // (to be set by reading the input file)
double g_hd = -1; // galactic parameter (to be set by reading the input file)
double g_mb = -1; // galactic parameter (to be set by reading the input file)
double g_md = -1; // galactic parameter (to be set by reading the input file)
double g_print_interval = -1; // (to be set by reading the input file)
double g_softening = -1; // softening (in [L], to be set by reading the input file)
double g_timestep = -1; // (to be set by reading the input file)
double g_vh = -1; // galactic parameter (to be set by reading the input file)
int g_body_number = -1; // (to be set by reading the input file)
int g_minimumRemTimePrintInt = 5; // minimum interval (in seconds) between printing of remaining time (to prevent flooding on stdout)
int g_galactic_flag = -1; // (to be set by reading the input file)
int g_nbStarsPerThread = 250; // wanted number of stars treated by thread
int g_recenter_flag = -1; // (to be set by reading the input file)
int g_technique_number = 0; // (to be set by reading the input file)
int g_usePECE_flag = -1; // PECE : use PECE (0 if no, 1 if yes) ?
int g_usePEC_flag = -1; // PEC : use PEC (0 if no, 1 if yes) ?
int g_verbose_flag = 0; // if set to 1, print messages as we execute (modifiable by passing arguments when executing)
/*-----------------------------------------------------------*/
/* Declarations of functions. -------------------------------*/
static double calc_total_gpe(void);
static double calc_total_ke(void);
static int AdamsBashforth(BODY *outState, double step, BODY *state0, DBODY *dStateB0, DBODY *dStateB1, DBODY *dStateB2, DBODY *dStateB3);
static int AdamsMoulton(BODY *outState, double step, BODY *state0, DBODY *predictedDState, DBODY *dStateB0, DBODY *dStateB1, DBODY *dStateB2, DBODY *dStateB3);
static int add_galactic_effect(VECTOR *forces);
static int calc_grav_forces(BODY *body_array, VECTOR *forces);
static int calc_total_angmom(VECTOR *result);
static int computeTotalForceOnBodies(VECTOR *forces);
static int print_positions(FILE *outfile_fp, double currentSimulationTime);
static int read_input(char *filename);
static int recenter_velocities(void);
static int getGravitationalForce(BODY body, VECTOR *tmpGravitationalForce);
static int shiftTemporaryStates(DBODY *dStateB3, DBODY *dStateB2, DBODY *dStateB1, DBODY *dStateB0, DBODY *newDState, BODY *state0, BODY *newState);
static int stateToDerivativeState(BODY *b, DBODY *db);
static int tech_euler_1(double suggested_timestep, double *actual_timestep);
static int tech_euler_1a(double suggested_timestep, double *actual_timestep);
static int tech_euler_2(double suggested_timestep, double *actual_timestep);
static int tech_rk4(double suggested_timestep, double *actual_timestep);
static void copy_bodies(int num_bodies, BODY *from_array, BODY *to_array);
static void printError(char *funcName, char *msg);
void kek(double suggested_timestep, double *actual_timestep);
/*-----------------------------------------------------------*/
int main(int argc, char *argv[]) {
/* Initialize stuff. --------------------------------------*/
VECTOR initial_ang_mom, final_ang_mom;
char inputfile_name[NBUF];
char timeUnit[8];
double actual_step, currentSimulationTime, energyTimeTaken1, energyTimeTaken2, final_gpe, final_ke, final_tot_e, initial_gpe, initial_ke, initial_tot_e, mainLoopClock, mainLoopTimeTaken, printing_clock, remainingRealTime, suggested_step, previousInfoClock;
int i, n, retval, remainingTimePrintModulo;
inputfile_name[0] = '\0';
strcpy(timeUnit, "seconds");
for (i = 0; i < MAX_NBODY; i++) {
g_body_array[i].index = -1;
strcpy(g_body_array[i].name, "");
g_body_array[i].mass = -1;
}
g_outfile_fp = (FILE *) NULL;
strcpy(g_technique_array[g_technique_number].name, "E1");
g_technique_array[g_technique_number++].func = tech_euler_1;
strcpy(g_technique_array[g_technique_number].name, "E1a");
g_technique_array[g_technique_number++].func = tech_euler_1a;
strcpy(g_technique_array[g_technique_number].name, "E2");
g_technique_array[g_technique_number++].func = tech_euler_2;
strcpy(g_technique_array[g_technique_number].name, "RK4");
g_technique_array[g_technique_number++].func = tech_rk4;
/*---------------------------------------------------------*/
/* Parse arguments. ---------------------------------------*/
if (argc < 2) {
fprintf(stderr, "usage: nbody input= [verbose=] \n");
exit(1);
}
for (i = 1; i < argc; i++) {
if (g_verbose_flag > 0) {
printf(" arg %d is ..%s.. \n", i, argv[i]);
}
/* read input file name */
if (strncmp(argv[i], "input=", 6) == 0) {
if (strlen(argv[i]) >= NBUF) {
fprintf(stderr, "inputfile name ..%s.. is too long, max %d chars. \n", argv[i], (int) strlen(argv[i]));
exit(1);
}
if (sscanf(argv[i] + 6, "%s", inputfile_name) != 1) {
fprintf(stderr, "can't read inputfile_name from ..%s.. \n", argv[i] + 6);
exit(1);
}
if (g_verbose_flag > 0) {
printf(" read input file name ..%s.. \n", inputfile_name);
}
}
if (strcmp(argv[i], "verbose") == 0) {
g_verbose_flag = 1;
printf(" set verbose level to ..%d.. \n", g_verbose_flag);
}
if (strncmp(argv[i], "verbose=", 8) == 0) {
if (sscanf(argv[i] + 8, "%d", &g_verbose_flag) != 1) {
fprintf(stderr, "can't read verbosity level from ..%s.. \n", argv[i] + 8);
exit(1);
}
if (g_verbose_flag > 0) {
printf(" set verbose level to ..%d.. \n", g_verbose_flag);
}
}
}
if (inputfile_name[0] == '\0') {
fprintf(stderr, "no input= argument provided ?! \n");
exit(1);
} // Check to make sure we read all required arguments.
printf("\n> Starting input file reading.\n");
if (read_input(inputfile_name) != 0) {
fprintf(stderr, "read_input fails \n");
exit(1);
} /* read all input information */
if (g_recenter_flag == 1) {
if (recenter_velocities() != 0) {
fprintf(stderr, "recenter_velocities returns with error \n");
exit(1);
}
} // If desired, add a constant velocity to all bodies so that the center of mass of the system is motionless.
/*---------------------------------------------------------*/
/* Compute initial quantities. ----------------------------*/
printf("\n");
double energyClock = clock(); // check time before
initial_ke = calc_total_ke();
if (g_verbose_flag > 0) {
printf("> Initial KE : %12.5e.\n", initial_ke);
}
initial_gpe = calc_total_gpe();
if (g_verbose_flag > 0) {
printf("> Initial GPE : %12.5e.\n", initial_gpe);
}
initial_tot_e = initial_ke + initial_gpe;
if (g_verbose_flag > 0) {
printf("> Initial E : %12.5e.\n", initial_tot_e);
}
if (calc_total_angmom( & initial_ang_mom) != 0) {
fprintf(stderr, "calc_total_angmom fails for initial \n");
exit(1);
}
if (g_verbose_flag > 0) {
printf("> Initial angular momentum : [%12.5e, %12.5e, %12.5e].\n",
initial_ang_mom.val[0],
initial_ang_mom.val[1],
initial_ang_mom.val[2]);
}
energyClock = clock() - energyClock; // check time after
energyTimeTaken1 = ((double) energyClock) / CLOCKS_PER_SEC; // time taken in seconds
/*---------------------------------------------------------*/
/* Print initial states to output. ------------------------*/
currentSimulationTime = 0;
if (print_positions(g_outfile_fp, currentSimulationTime) != 0) {
fprintf(stderr, "Iteration %16d, time %9.4e [T].\n", 0, currentSimulationTime);
printError("main", "Function print_positions failed.");
exit(1);
}
/*---------------------------------------------------------*/
/* Enter main loop. ---------------------------------------*/
printf("\n");
printf("> Available threads : %d.\n", omp_get_max_threads());
omp_set_num_threads(min(max((int)(floor(g_body_number / g_nbStarsPerThread)), 1), omp_get_max_threads()));
//omp_set_num_threads(3);
#pragma omp parallel
printf("> Launching simulation with %d thread(s) (i.e., roughly, %d star(s) per thread).\n", omp_get_num_threads(), (int)(g_body_number / omp_get_num_threads()));
printing_clock = 0.0; // initialise printing clock
mainLoopClock = clock(); // save timer before entering the main loop
n = 0; // to follow number of iterations
if (g_verbose_flag > 0) {
remainingTimePrintModulo = 20000;
previousInfoClock = clock();
} // every how many iterations print informations about remaining time and dummy value for first previousRemainingRealTime
printf("\n> Entering main integration loop.\n");
while (currentSimulationTime <= g_duration) {
/* Call the integration -*/
/* function. -*/
if (g_verbose_flag > 1) {
printf(" >> About to enter the integration function (t=%9.4e).\n", currentSimulationTime);
}
suggested_step = g_timestep;
actual_step = g_timestep; // safety, in case it does not get allocated in the upcoming calls
if (g_usePECE_flag == 0 && g_usePEC_flag == 0) { // PECE and PEC are desactivated : just use RK4 all the way.
retval = (*(g_integration_func))(suggested_step, &actual_step);
if (retval != 0) {
printError("main", "Integration function failed.");
return (1);
}
g_timestep = actual_step;
} else { // PECE or PEC is activated.
if (n <= 3) { // For the first 4 steps of PECE, call RK4.
retval = (*(g_integration_func))(suggested_step, &actual_step);
if (retval != 0) {
printError("main", "Integration function failed.");
return (1);
}
g_timestep = actual_step;
if (n == 0) {
stateToDerivativeState(g_body_array, fp3);
}
if (n == 1) {
stateToDerivativeState(g_body_array, fp2);
}
if (n == 2) {
stateToDerivativeState(g_body_array, fp1);
}
if (n == 3) {
stateToDerivativeState(g_body_array, fp0);
}
} else { // For n>=4, call PECE/PEC AB AM.
AdamsBashforth(pyn1, g_timestep, g_body_array, fp0, fp1, fp2, fp3); // prediction (Adams Bashforth)
//printf("n=%d, AB, position : [%6.2f, %6.2f, %6.2f], velocity : [%6.2f, %6.2f, %6.2f].\n", n, g_body_array[1].pos[0], g_body_array[1].pos[1], g_body_array[1].pos[2], g_body_array[1].vel[0], g_body_array[1].vel[1], g_body_array[1].vel[2]);
stateToDerivativeState(pyn1, pfn1); // evaluation (with forces computed using predicted state)
//printf("n=%d, evaluation : pyn1 : [%6.2f, %6.2f, %6.2f][%6.2f, %6.2f, %6.2f], pfn1 : [%6.2f, %6.2f, %6.2f][%6.2f, %6.2f, %6.2f]\n", n, pyn1[1].pos[0], pyn1[1].pos[1], pyn1[1].pos[2], pyn1[1].vel[0], pyn1[1].vel[1], pyn1[1].vel[2], pfn1[1].dp[0], pfn1[1].dp[1], pfn1[1].dp[2], pfn1[1].dv[0], pfn1[1].dv[1], pfn1[1].dv[2]);
AdamsMoulton(yn1, g_timestep, g_body_array, pfn1, fp0, fp1, fp2, fp3); // correction (Adams Moulton)
//printf("n=%d, AM, position : [%6.2f, %6.2f, %6.2f], velocity : [%6.2f, %6.2f, %6.2f].\n", n, g_body_array[1].pos[0], g_body_array[1].pos[1], g_body_array[1].pos[2], g_body_array[1].vel[0], g_body_array[1].vel[1], g_body_array[1].vel[2]);
if (g_usePEC_flag == 1) { // If only PEC, use pfn1 as fn1.
shiftTemporaryStates(fp3, fp2, fp1, fp0, pfn1, g_body_array, yn1);
} else { // If PECE, evaluate (with forces computed using corrected state).
stateToDerivativeState(yn1, fn1);
shiftTemporaryStates(fp3, fp2, fp1, fp0, fn1, g_body_array, yn1);
}
}
}
currentSimulationTime += g_timestep;
n += 1;
/*-----------------------*/
/* Update printing ---*/
/* clock and print to ---*/
/* output file. ---*/
printing_clock += actual_step;
if (printing_clock >= g_print_interval) {
/* print the new positions, etc. */
if (print_positions(g_outfile_fp, currentSimulationTime) != 0) {
fprintf(stderr, "Iteration %16d, time %9.4e [T].\n", n, currentSimulationTime);
printError("main", "Function print_positions failed.");
exit(1);
}
printing_clock = 0.0;
}
/*-----------------------*/
/* Print information ----*/
/* about simulation run. */
if (g_verbose_flag > 0) {
if (n == 100 || n == 1000 || n == 2000 || n % remainingTimePrintModulo == 0) { // after first 100, first 1000, first 2000 and then every 20000 integrations, estimate the remaining time
remainingRealTime = (((clock() - mainLoopClock) / currentSimulationTime) * (g_duration - currentSimulationTime)) / CLOCKS_PER_SEC; // estimated remaining time in seconds
if ((n > 2000) && ((clock() - previousInfoClock) < g_minimumRemTimePrintInt * CLOCKS_PER_SEC)) {
remainingTimePrintModulo *= 2;
} // if informations prints too fast, raise interval in terms of iterations
if (remainingRealTime < 60) {
strcpy(timeUnit, "seconds");
}
if (remainingRealTime >= 60 && remainingRealTime < 3600) {
strcpy(timeUnit, "minutes");
remainingRealTime /= 60;
}
if (remainingRealTime >= 3600) {
strcpy(timeUnit, "hours");
remainingRealTime /= 3600;
}
printf("> Iteration %16d, time %9.4e [T], estimated remaining time : %6.2f %s.\n", n, currentSimulationTime, remainingRealTime, timeUnit);
previousInfoClock = clock();
}
}
/*-----------------------*/
}
printf("> Main integration loop finished.\n");
mainLoopTimeTaken = ((double)(clock() - mainLoopClock) / CLOCKS_PER_SEC); // time taken by main loop (in seconds)
/*---------------------------------------------------------*/
/* Print final states to output. --------------------------*/
if (print_positions(g_outfile_fp, currentSimulationTime) != 0) {
fprintf(stderr, "Iteration %16d, time %9.4e [T].\n", n, currentSimulationTime);
printError("main", "Function print_positions failed.");
exit(1);
}
/*---------------------------------------------------------*/
/* Compute final quantities. ------------------------------*/
printf("\n");
energyClock = clock(); // check time before
final_ke = calc_total_ke();
if (g_verbose_flag > 0) {
printf("> Final KE : %12.5e.\n", final_ke);
}
final_gpe = calc_total_gpe();
if (g_verbose_flag > 0) {
printf("> Final GPE : %12.5e.\n", final_gpe);
}
final_tot_e = final_ke + final_gpe;
if (g_verbose_flag > 0) {
printf("> Final E : %12.5e.\n", final_tot_e);
}
if (calc_total_angmom( & final_ang_mom) != 0) {
fprintf(stderr, "calc_total_angmom fails for final \n");
exit(1);
}
if (g_verbose_flag > 0) {
printf("> Final angular momentum : [%12.5e, %12.5e, %12.5e].\n",
final_ang_mom.val[0],
final_ang_mom.val[1],
final_ang_mom.val[2]);
}
energyClock = clock() - energyClock; // check time after
energyTimeTaken2 = ((double) energyClock) / CLOCKS_PER_SEC; // time taken in seconds
/*---------------------------------------------------------*/
/* Compute change in quantities. --------------------------*/
{
double delta_e, fraction_delta_e;
double start_angmom_mag, end_angmom_mag, delta_angmom_mag;
double fraction_delta_angmom_mag;
printf("\n");
delta_e = final_tot_e - initial_tot_e;
fraction_delta_e = delta_e / fabs(initial_tot_e);
if (g_verbose_flag > 0) {
printf("> Delta_E =%12.5e,\n relative difference =%12.5e (the smaller the better, under 1e-02 is good).\n",
delta_e, fraction_delta_e);
}
start_angmom_mag = VECTOR_MAG(initial_ang_mom);
end_angmom_mag = VECTOR_MAG(final_ang_mom);
delta_angmom_mag = end_angmom_mag - start_angmom_mag;
fraction_delta_angmom_mag = delta_angmom_mag / fabs(start_angmom_mag);
if (g_verbose_flag > 0) {
printf("> Delta_angmom =%12.5e,\n relative difference =%12.5e (the smaller the better, under 1e-02 is good).\n",
delta_angmom_mag, fraction_delta_angmom_mag);
}
}
/*---------------------------------------------------------*/
/* Prepare to exit. ---------------------------------------*/
if (g_verbose_flag > 0) {
if (mainLoopTimeTaken < 60) {
strcpy(timeUnit, "seconds");
}
if (mainLoopTimeTaken >= 60 && mainLoopTimeTaken < 3600) {
strcpy(timeUnit, "minutes");
mainLoopTimeTaken /= 60;
}
if (mainLoopTimeTaken >= 3600) {
strcpy(timeUnit, "hours");
mainLoopTimeTaken /= 3600;
}
printf("\n");
printf("> All done.\n");
printf("> Main loop was %12d iterations (integrations) long.\n", n);
printf("> Main loop took %12.2f %s to execute.\n", mainLoopTimeTaken, timeUnit);
printf("> Energies computations took %12.2f seconds to execute.\n", energyTimeTaken1 + energyTimeTaken2);
}
// Close file opened in read_input routine.
fclose(g_outfile_fp);
exit(0);
/*---------------------------------------------------------*/
}
static int stateToDerivativeState(BODY * b, DBODY * db) {
// Get the state derivative of a state. In other terms, for the PDE Y'=f(Y), apply f to a Y to get Y'.
// @param *b array of bodies (in other terms, Y)
// @param *db (output) derivative state (in other terms, Y')
int i, j, c;
VECTOR dist, dist_frac, tmpGravitationalForce;
double dist_tot;
double dist_squared;
double top, bottom, force_mag;
#pragma omp for
for (i = 0; i < g_body_number; i++) { // on body i, ...
for (c = 0; c < 3; c++) { // initialise force at 0, ...
tmpForcesForDerivation[i].val[c] = 0.0;
}
for (j = i; j < g_body_number; j++) { // compute effect of body j, ...
if (i == j) {
g_cur_forces[i][j].val[0] = 0.0;
g_cur_forces[i][j].val[1] = 0.0;
g_cur_forces[i][j].val[2] = 0.0;
continue;
}
dist.val[0] = b[i].pos[0] - b[j].pos[0];
dist.val[1] = b[i].pos[1] - b[j].pos[1];
dist.val[2] = b[i].pos[2] - b[j].pos[2];
dist_squared = VECTOR_MAG_SQUARED(dist);
dist_tot = sqrt(dist_squared);
/* if(dist_tot<=0.0){
fprintf(stderr,
" calc_grav_forces: distance of %le -- quitting \n", dist_tot);
return(1);
} */
top = g_G * b[i].mass * b[j].mass;
bottom = (dist_tot + g_softening) * (dist_tot + g_softening);
force_mag = top / bottom;
for (c = 0; c < 3; c++) { // and for each coordinate, prepare the value and add it as contribution
dist_frac.val[c] = dist.val[c] / dist_tot;
g_cur_forces[i][j].val[c] = 0.0 - (force_mag * dist_frac.val[c]);
g_cur_forces[j][i].val[c] = 0.0 - g_cur_forces[i][j].val[c];
tmpForcesForDerivation[i].val[c] += -(force_mag * dist_frac.val[c]);
tmpForcesForDerivation[j].val[c] += (force_mag * dist_frac.val[c]);
}
}
for (j = 0; j < g_body_number; j++) { // add contribution of each body j on body i
for (c = 0; c < 3; c++) {
tmpForcesForDerivation[i].val[c] += g_cur_forces[i][j].val[c];
}
}
if (g_galactic_flag == 1) { // if the effect of a galaxy is wanted, add it to the current force on body i
getGravitationalForce(g_body_array[i], &tmpGravitationalForce);
for (c = 0; c < 3; c++) {
tmpForcesForDerivation[i].val[c] += tmpGravitationalForce.val[c];
}
}
for (c = 0; c < 3; c++) { // when all contributions have been added, for body i, apply the derivation
db[i].dp[c] = b[i].vel[c]; // dp=v
db[i].dv[c] = tmpForcesForDerivation[i].val[c] / b[i].mass; // dv=a/m
}
}
return (0);
}
static int AdamsBashforth(BODY * outState, double step, BODY * state0, DBODY * dStateB0, DBODY * dStateB1, DBODY * dStateB2, DBODY * dStateB3) {
// Apply an iteration of the Adams-Bashforth algorithm.
// @param *outState (output) the system state 1 iteration after (BODY array)
// @param step the step
// @param *state0 the system state at current iteration (BODY array)
// @param *dStateB0 the system state's derivative at current iteration (DBODY array)
// @param *dStateB1 the system state's derivative 1 iteration before (DBODY array)
// @param *dStateB2 the system state's derivative 2 iterations before (DBODY array)
// @param *dStateB3 the system state's derivative 3 iterations before (DBODY array)
int i, c;
#pragma omp for
for (i = 0; i < g_body_number; i++) {
for (c = 0; c < 3; c++) {
outState[i].pos[c] = state0[i].pos[c] + step * (g_AB_b03 * dStateB0[i].dp[c] + g_AB_b13 * dStateB1[i].dp[c] + g_AB_b23 * dStateB2[i].dp[c] + g_AB_b33 * dStateB3[i].dp[c]); // p=f(y)
outState[i].vel[c] = state0[i].vel[c] + step * (g_AB_b03 * dStateB0[i].dv[c] + g_AB_b13 * dStateB1[i].dv[c] + g_AB_b23 * dStateB2[i].dv[c] + g_AB_b33 * dStateB3[i].dv[c]); // v=f(y)
}
outState[i].mass = state0[i].mass; // m=f(y)
}
return (0);
}
static int AdamsMoulton(BODY * outState, double step, BODY * state0, DBODY * predictedDState, DBODY * dStateB0, DBODY * dStateB1, DBODY * dStateB2, DBODY * dStateB3) {
// Apply an iteration of the Adams-Moulton algorithm.
// @param *outState (output) the system state 1 iteration after (BODY array)
// @param step the step
// @param *state0 the system state at current iteration (BODY array)
// @param *predictedDState the predicted system state's derivative 1 iteration after (DBODY array)
// @param *dStateB0 the system state's derivative at current iteration (DBODY array)
// @param *dStateB1 the system state's derivative 1 iteration before (DBODY array)
// @param *dStateB2 the system state's derivative 2 iterations before (DBODY array)
// @param *dStateB3 the system state's derivative 3 iterations before (DBODY array)
int i, c;
#pragma omp for
for (i = 0; i < g_body_number; i++) {
for (c = 0; c < 3; c++) {
outState[i].pos[c] = state0[i].pos[c] + step * (g_AM_bp3 * predictedDState[i].dp[c] + g_AM_b03 * dStateB0[i].dp[c] + g_AM_b13 * dStateB1[i].dp[c] + g_AM_b23 * dStateB2[i].dp[c] + g_AM_b33 * dStateB3[i].dp[c]);
outState[i].vel[c] = state0[i].vel[c] + step * (g_AM_bp3 * predictedDState[i].dv[c] + g_AM_b03 * dStateB0[i].dv[c] + g_AM_b13 * dStateB1[i].dv[c] + g_AM_b23 * dStateB2[i].dv[c] + g_AM_b33 * dStateB3[i].dv[c]);
}
outState[i].mass = state0[i].mass; // m=f(y)
}
return (0);
}
static int shiftTemporaryStates(DBODY * dStateB3, DBODY * dStateB2, DBODY * dStateB1, DBODY * dStateB0, DBODY * newDState, BODY * state0, BODY * newState) {
// For the PECE scheme, shifts needed temporary states as number of iteration goes from n to n+1.
// @param *dStateB3 (output) the system state's derivative 3 iterations before (DBODY array)
// @param *dStateB2 (input/output) the system state's derivative 2 iterations before (DBODY array)
// @param *dStateB1 (input/output) the system state's derivative 1 iteration before (DBODY array)
// @param *dStateB0 (input/output)the system state's derivative at current iteration (DBODY array)
// @param *newDState the system state's derivative 1 iteration after (DBODY array)
// @param *state0 (output) the system state at current iteration (BODY array)
// @param *newState the system state 1 iteration after (BODY array)
int i, c;
#pragma omp for
for (i = 0; i < g_body_number; i++) {
for (c = 0; c < 3; c++) {
dStateB3[i].dp[c] = dStateB2[i].dp[c];
dStateB3[i].dv[c] = dStateB2[i].dv[c];
dStateB2[i].dp[c] = dStateB1[i].dp[c];
dStateB2[i].dv[c] = dStateB1[i].dv[c];
dStateB1[i].dp[c] = dStateB0[i].dp[c];
dStateB1[i].dv[c] = dStateB0[i].dv[c];
dStateB0[i].dp[c] = newDState[i].dp[c];
dStateB0[i].dv[c] = newDState[i].dv[c];
state0[i].pos[c] = newState[i].pos[c];
state0[i].vel[c] = newState[i].vel[c];
}
}
return (0);
}
static int tech_rk4(double suggested_timestep, double *actual_timestep) {
// Advance particle system one timestep using Runge-Kutta fourth-order method.
// @param suggested_timestep suggested timestep
// @param *actual_timestep actual timestep used (for now, this function does not try to change the timestep)
int i, c;
double timestep;
double half_step, sixth_step;
VECTOR v1[g_body_number];
VECTOR v2[g_body_number];
VECTOR v3[g_body_number];
VECTOR v4[g_body_number];
VECTOR a1[g_body_number];
VECTOR a2[g_body_number];
VECTOR a3[g_body_number];
VECTOR a4[g_body_number];
BODY step2_array[g_body_number];
BODY step3_array[g_body_number];
BODY step4_array[g_body_number];
timestep = suggested_timestep;
half_step = timestep * 0.5;
sixth_step = timestep / 6.0;
copy_bodies(g_body_number, g_body_array, step2_array); // For S3.
copy_bodies(g_body_number, g_body_array, step3_array); // For S6.
copy_bodies(g_body_number, g_body_array, step4_array); // For S9.
// S1: use current positions to compute accelerations. Call these "a1".
if (calc_grav_forces(step2_array, a1) != 0) {
printError("tech_rk4", "Function calc_grav_forces fails in step a1.");
return (1);
}
#pragma omp for
for (i = 0; i < g_body_number; i++) {
for (c = 0; c < 3; c++) {
a1[i].val[c] /= step2_array[i].mass; // Convert those forces to accelerations.
v1[i].val[c] = g_body_array[i].vel[c]; // S2 : copy the current velocities into an array for later use. Call these "v1".
step2_array[i].pos[c] += half_step * v1[i].val[c]; // S3 : use the "v1" velocities to predict positions of objects one-half a step into the future. We'll place the positions into the "step2_array[]."
v2[i].val[c] = v1[i].val[c] + half_step * a1[i].val[c]; // S4 : use the "a1" accelerations to predict future velocities at half a step into the future. Call these "v2".
}
}
// S5: use the "step2_array" positions (at half a step in future) to compute forces on the bodies in the future. Convert these forces to accelerations and place into the "a2" array.
if (calc_grav_forces(step2_array, a2) != 0) {
printError("tech_rk4", "Function calc_grav_forces fails in step a2.");
return (1);
}
#pragma omp for
for (i = 0; i < g_body_number; i++) {
for (c = 0; c < 3; c++) {
a2[i].val[c] /= step2_array[i].mass; // Convert those forces to accelerations.
v3[i].val[c] = v1[i].val[c] + half_step * a2[i].val[c]; // S6: use the "a2" accelerations to predict future velocities at half a step into the future. Call these "v3".
step3_array[i].pos[c] += half_step * v2[i].val[c]; // S6: use the "v2" velocities to predict positions of objects one-half a step into the future. We'll place the positions into the "step3_array[]."
}
}
// S7: use the "step3_array" positions (at half a step in future) to compute forces on the bodies in the future. Convert these forces to accelerations and place into the "a3" array.
if (calc_grav_forces(step3_array, a3) != 0) {
printError("tech_rk4", "Function calc_grav_forces fails in step a3.");
return (1);
}
#pragma omp for
for (i = 0; i < g_body_number; i++) {
for (c = 0; c < 3; c++) {
a3[i].val[c] /= step3_array[i].mass; // Convert those forces to accelerations.
v4[i].val[c] = v1[i].val[c] + timestep * a3[i].val[c]; // S8: use the "a3" accelerations to predict future velocities at one FULL step into the future. Call these "v4".
step4_array[i].pos[c] += timestep * v3[i].val[c]; // S9: use the "v3" velocities to predict positions of objects one FULL step into the future. We'll place the positions into the "step4_array[]."
}
}
// S10: use the "step4_array" positions (at one FULL step in future) to compute forces on the bodies in the future. Convert these forces to accelerations and place into the "a4" array.
if (calc_grav_forces(step4_array, a4) != 0) {
printError("tech_rk4", "Function calc_grav_forces fails in step a4.");
return (1);
}
#pragma omp for
for (i = 0; i < g_body_number; i++) {
for (c = 0; c < 3; c++) {
a4[i].val[c] /= step4_array[i].mass; // Convert those forces to accelerations.
// See note below.
double delta_vel, delta_pos;
delta_vel = sixth_step * (a1[i].val[c] + 2 * a2[i].val[c] + 2 * a3[i].val[c] + a4[i].val[c]);
delta_pos = sixth_step * (v1[i].val[c] + 2 * v2[i].val[c] + 2 * v3[i].val[c] + v4[i].val[c]);
g_body_array[i].vel[c] += delta_vel;
g_body_array[i].pos[c] += delta_pos;
}
}
/* Note :
* At this point, we have 4 velocities and 4 accelerations for each object :
* - v1 current velocity,
* - v2 velocity predicted half a step in future,
* - v3 improved velocity predicted half a step in future,
* - v4 velocity predicted one FULL step in future,
* - a1 current acceleration,
* - a2 acceleration predicted half a step in future,
* - a3 improved acceleration predicted half a step in future,
* - a4 acceleration predicted one FULL step in future.
* We can now combine these 4 measurements to produce one good value of velocity (or acceleration) one full step into the future.
*/
*actual_timestep = timestep;
return (0);
}
static int calc_grav_forces(BODY * body_array, VECTOR * forces) {
// Given an array of bodies, compute the gravitational forces between each pair. The forces are then placed into a vector array.
// @param *body_array array with pos and mass of all bodies (BODY array)
// @param *forces (output) forces on each body (VECTOR array)
int i, j, c;
// Compute the forces between all objects using the current positions and store this in the pre-allocated global variable g_cur_forces.
#pragma omp for
for (i = 0; i < g_body_number; i++) {
for (j = i; j < g_body_number; j++) {
VECTOR dist, dist_frac;
double dist_tot;
double dist_squared;
double top, bottom, force_mag;
if (i == j) {
g_cur_forces[i][j].val[0] = 0.0;
g_cur_forces[i][j].val[1] = 0.0;
g_cur_forces[i][j].val[2] = 0.0;
continue;
}
dist.val[0] = body_array[i].pos[0] - body_array[j].pos[0];
dist.val[1] = body_array[i].pos[1] - body_array[j].pos[1];
dist.val[2] = body_array[i].pos[2] - body_array[j].pos[2];
dist_squared = VECTOR_MAG_SQUARED(dist);
dist_tot = sqrt(dist_squared);
/* if(dist_tot<=0.0){
fprintf(stderr,
" calc_grav_forces: distance of %le -- quitting \n", dist_tot);
return(1);
} */
top = g_G * body_array[i].mass * body_array[j].mass;
bottom = (dist_tot + g_softening) * (dist_tot + g_softening);
force_mag = top / bottom;
for (c = 0; c < 3; c++) {
dist_frac.val[c] = dist.val[c] / dist_tot;
g_cur_forces[i][j].val[c] = 0.0 - (force_mag * dist_frac.val[c]);
g_cur_forces[j][i].val[c] = 0.0 - g_cur_forces[i][j].val[c];
}
}
}
computeTotalForceOnBodies(forces); // convert forces between each pair (stored in g_cur_forces) to a set of forces on each body
if (g_galactic_flag == 1) {
add_galactic_effect(forces);
} // if the effect of a galaxy is wanted, add it to the current forces
return (0);
}
static int computeTotalForceOnBodies(VECTOR * forces) {
// Convert forces between each pair (stored in g_cur_forces) to a set of forces on each body.
// @param *forces (output) forces on each body (VECTOR array)
int i, j, c;
#pragma omp for
for (i = 0; i < g_body_number; i++) {
for (c = 0; c < 3; c++) {
forces[i].val[c] = 0.0;
for (j = 0; j < g_body_number; j++) {
forces[i].val[c] += g_cur_forces[i][j].val[c];
}
}
}
return (0);
}
static int add_galactic_effect(VECTOR * forces) {
// Adds to an array of forces the effect of a galactic field.
// The galaxy is supposed to be centered on the origin of the
// reference (O=[0.0, 0.0, 0.0]) and its model is given by :
//-a Plummer type bulge,
//-a Miyamoto-Nagai disk,
//-a dark matter halo.
// @param *forces (input/output) forces on each body (VECTOR array)
int i;
VECTOR tmpGravitationalForce;
#pragma omp for
for (i = 0; i < g_body_number; i++) {
getGravitationalForce(g_body_array[i], &tmpGravitationalForce);
forces[i].val[0] += tmpGravitationalForce.val[0];
forces[i].val[1] += tmpGravitationalForce.val[1];
forces[i].val[2] += tmpGravitationalForce.val[2];
}
return (0);
}
static int getGravitationalForce(BODY body, VECTOR * tmpGravitationalForce) {
// Compute the graviational force on a body.
// @param body the considered body
// @param *tmpGravitationalForce (output) the graviational force (pointer to a VECTOR)
double tmpX, tmpY, tmpZ;
double tmpRSquared, tmpRCSquared, tmpZSquared; // temporary variables to store the spherical radius, cylindrical radius and cylindrical altitude of a body
double tmpPhiB, tmpPhiD, tmpPhiH; // temporary variables to store the bulge, disk and halo components
tmpX = body.pos[0];
tmpY = body.pos[1];
tmpZ = body.pos[2];
tmpRCSquared = tmpX * tmpX + tmpY * tmpY;
tmpZSquared = tmpZ * tmpZ;
tmpRSquared = tmpRCSquared + tmpZSquared;
// x
tmpPhiB = g_G * g_mb * pow(tmpRSquared + g_ab * g_ab, -1.5) * tmpX;
tmpPhiD = g_G * g_md * pow(tmpRCSquared + pow(g_ad + pow(tmpZSquared + g_hd * g_hd, 0.5), 2.0), -1.5) * tmpX;
tmpPhiH = -(g_vh * g_vh) / (tmpRSquared + g_ah * g_ah) * tmpX;
tmpGravitationalForce -> val[0] = tmpPhiB + tmpPhiD + tmpPhiH;
// y
tmpPhiB *= (tmpY / tmpX);
tmpPhiD *= (tmpY / tmpX);
tmpPhiH *= (tmpY / tmpX);
tmpGravitationalForce -> val[1] = tmpPhiB + tmpPhiD + tmpPhiH;
// z
tmpPhiB *= (tmpZ / tmpY);
tmpPhiD *= (tmpZ / tmpY);
tmpPhiH *= (tmpZ / tmpY);
tmpPhiD *= ((g_ad + pow(tmpZSquared + g_hd * g_hd, 0.5)) / (pow(tmpZSquared + g_hd * g_hd, 0.5)));
tmpGravitationalForce -> val[2] = tmpPhiB + tmpPhiD + tmpPhiH;
tmpGravitationalForce -> val[0] *= -body.mass;
tmpGravitationalForce -> val[1] *= -body.mass;
tmpGravitationalForce -> val[2] *= -body.mass;
return (0);
}
static void copy_bodies(int N, BODY *in, BODY *out) {
// Copy an array of BODY structures into a second array in such a manner that we can then modify the copies and leave the originals untouched.
// @param N number of bodies to be copied
// @param in copy from this array
// @param out (output) copy to this array
int i, c;
BODY *from_body, *to_body;
#pragma omp for
for (i = 0; i < N; i++) {
from_body = &(in [i]);
to_body = &(out[i]);
to_body->index = from_body->index;
strcpy(from_body->name, to_body->name);
to_body->mass = from_body->mass;
for (c = 0; c < 3; c++) {
to_body->pos[c] = from_body->pos[c];
to_body->vel[c] = from_body->vel[c];
}
}
}
static int recenter_velocities(void) {
// Compute the global velocity of the system. Then, subtract that value from all bodies, so that the center of mass of the system should remain motionless as time passes.
// @param none
int i, c;
double total_mass;
VECTOR momentum, com_velocity;
total_mass = 0.0;
for (c = 0; c < 3; c++) {
momentum.val[c] = 0.0;
}
for (i = 0; i < g_body_number; i++) {
total_mass += g_body_array[i].mass;
for (c = 0; c < 3; c++) {
momentum.val[c] += g_body_array[i].mass * g_body_array[i].vel[c];
}
}
if (total_mass == 0.0) {
return (0);
} // If the total mass is zero, just return now (why would that be ever the case anyways ?).
for (c = 0; c < 3; c++) {
com_velocity.val[c] = momentum.val[c] / total_mass;
} // Compute the velocity of the center of mass.
#pragma omp for
for (i = 0; i < g_body_number; i++) {
for (c = 0; c < 3; c++) {
g_body_array[i].vel[c] -= com_velocity.val[c];
}
} // Subtract this velocity from each body.
return (0);
}
static double calc_total_ke(void) {
// Compute the total KE of all bodies. The result will have [E]=[M]*[L^2]*[T^{-2}] as unit.
// @param none
// @return the total KE
int i;
double vSquared, ke, total_ke;
total_ke = 0;
for (i = 0; i < g_body_number; i++) {
vSquared = g_body_array[i].vel[0] * g_body_array[i].vel[0] + g_body_array[i].vel[1] * g_body_array[i].vel[1] + g_body_array[i].vel[2] * g_body_array[i].vel[2];
ke = 0.5 * g_body_array[i].mass * vSquared;
total_ke += ke;
if (g_verbose_flag > 1) {
printf(">> [calc_total_ke] : Body %5d has v**2=%9.4e and m=%9.4e, thus %9.4e of KE (total updated to %9.4e).\n", i, vSquared, g_body_array[i].mass, ke, total_ke);
}
}
return (total_ke);
}
static double calc_total_gpe(void) {
// Compute the total GPE of all bodies. The result will have [E]=[M]*[L^2]*[T^{-2}] as unit.
// @param none
// @return the total GPE
int i, j;
double d, d_squared, gpe, total_gpe;
total_gpe = 0.0;
for (i = 0; i < g_body_number - 1; i++) {
for (j = i + 1; j < g_body_number; j++) {
d_squared = DIST_SQUARED(i, j);
d = sqrt(d_squared);
if (d <= 0) {
fprintf(stderr, "Distance %9.4le (between bodies %d and %d).\n", d, i, j);
printError("calc_total_gpe", "Distance is invalid.");
exit(1);
}
gpe = 0.0 - ((g_G * g_body_array[i].mass * g_body_array[j].mass) / d);
total_gpe += gpe;
if (g_verbose_flag > 1) {
printf(">> [calc_total_gpe] Bodies %5d and %5d have %9.4e of GPE (total GPE updated to %9.4e).\n", i, j, gpe, total_gpe);
}
}
}
return (total_gpe);
}
static int calc_total_angmom(VECTOR * result) {
// Compute the total angular momentum of all the bodies, as a three-D vector, computed around the center of mass of the system. The result will have [M]*[L]*[T^{-1}] as unit.
// *result total angular momentum of system
int i, c;
double total_mass;
VECTOR body_pos, body_vel, body_ang_mom, com, com_vel, total_ang_mom;
total_mass = 0.0;
for (c = 0; c < 3; c++) {
com.val[c] = 0.0;
com_vel.val[c] = 0.0;
total_ang_mom.val[c] = 0.0;
}
// Compute the center of mass of the system and the velocity of the center of mass.
for (i = 0; i < g_body_number; i++) {
total_mass += g_body_array[i].mass;
for (c = 0; c < 3; c++) {
com.val[c] += g_body_array[i].mass * g_body_array[i].pos[c];
com_vel.val[c] += g_body_array[i].mass * g_body_array[i].vel[c];
}
}
for (c = 0; c < 3; c++) {
com.val[c] /= total_mass;
com_vel.val[c] /= total_mass;
}
// Compute the angular momentum of each body around this point.
for (i = 0; i < g_body_number; i++) {
// Compute the position vector from center of mass to body i.
body_pos.val[0] = g_body_array[i].pos[0] - com.val[0];
body_pos.val[1] = g_body_array[i].pos[1] - com.val[1];
body_pos.val[2] = g_body_array[i].pos[2] - com.val[2];
// Compute the velocity difference between center of mass and body i, and weight it by the mass of the body.
body_vel.val[0] = g_body_array[i].mass * (g_body_array[i].vel[0] - com_vel.val[0]);