1 | #include <stdio.h> |
---|
2 | #include <string.h> |
---|
3 | #include <math.h> |
---|
4 | #include <stdlib.h> |
---|
5 | #include <time.h> |
---|
6 | |
---|
7 | |
---|
8 | #include "Model.h" |
---|
9 | |
---|
10 | body* bodies; //vector that contains all the bodies in the system |
---|
11 | |
---|
12 | void setBody(body* b, double mass, double pos[3], double vel[3]){ |
---|
13 | |
---|
14 | b->mass = mass; |
---|
15 | memcpy(b->pos, pos, 3 * sizeof(double)); |
---|
16 | memcpy(b->vel, vel, 3 * sizeof(double)); |
---|
17 | } |
---|
18 | |
---|
19 | |
---|
20 | void initializeNBody(int n){ |
---|
21 | int i; |
---|
22 | double mass = 0; |
---|
23 | double pos[3] = {0, 0, 0}; |
---|
24 | double vel[3] = {0, 0, 0}; |
---|
25 | |
---|
26 | |
---|
27 | for (i = 0; i < n; i++){ |
---|
28 | setBody(&bodies[i], mass, pos, vel); |
---|
29 | //printf("%f %f\n", bodies[i].mass, bodies[i].pos[2]); |
---|
30 | } |
---|
31 | |
---|
32 | } |
---|
33 | |
---|
34 | //generate random float number between [low, high) |
---|
35 | double frand(double low, double high) |
---|
36 | { |
---|
37 | double temp; |
---|
38 | |
---|
39 | /* swap low & high around if the user makes no sense */ |
---|
40 | if (low > high){ |
---|
41 | temp = low; |
---|
42 | low = high; |
---|
43 | high = temp; |
---|
44 | } |
---|
45 | |
---|
46 | /* calculate the random number & return it */ |
---|
47 | temp = (rand() / ( RAND_MAX + 1.0)) |
---|
48 | * (high - low) + low; |
---|
49 | return temp; |
---|
50 | } |
---|
51 | |
---|
52 | |
---|
53 | |
---|
54 | void mkplummer(int n, unsigned int seed){ |
---|
55 | int i; |
---|
56 | double mass; |
---|
57 | double pos[3]; |
---|
58 | double vel[3]; |
---|
59 | double radius, theta, phi; |
---|
60 | double x, y; |
---|
61 | double velocity; |
---|
62 | |
---|
63 | if(seed == 0) |
---|
64 | srand(time(NULL)); //different succession of results in the subsequent calls to rand |
---|
65 | else srand(seed); //used for debugging |
---|
66 | |
---|
67 | initializeNBody(n); |
---|
68 | |
---|
69 | |
---|
70 | for(i = 0;i < n; i++){ |
---|
71 | mass = 1.0 / n; |
---|
72 | radius = 1.0 / sqrt(pow(frand(0, 1),-(2.0 / 3.0)) - 1.0); |
---|
73 | theta = acos(frand(-1, 1)); |
---|
74 | phi = frand(0, 2 * PI); |
---|
75 | pos[0] = radius * sin(theta) * cos(phi); |
---|
76 | pos[1] = radius * sin(theta) * sin(phi); |
---|
77 | pos[2] = radius * cos(theta); |
---|
78 | x = 0.0; |
---|
79 | y = 0.1; |
---|
80 | while( y > x * x * pow (1.0 - x*x, 3.5)){ |
---|
81 | x = frand(0, 1); |
---|
82 | y = frand(0, 0.1); |
---|
83 | } |
---|
84 | velocity = x * sqrt(2.0) * pow(1.0 + radius * radius, -0.25); |
---|
85 | theta = acos(frand(-1, 1)); |
---|
86 | phi = frand(0, 2 * PI); |
---|
87 | vel[0] = velocity * sin(theta) * cos(phi); |
---|
88 | vel[1] = velocity * sin(theta) * sin(phi); |
---|
89 | vel[2] = velocity * cos(theta); |
---|
90 | setBody(&bodies[i], mass, pos, vel); |
---|
91 | } |
---|
92 | |
---|
93 | |
---|
94 | |
---|
95 | } |
---|
96 | |
---|
97 | void writeData(int n){ |
---|
98 | char line[256]; |
---|
99 | int i; |
---|
100 | FILE *f = fopen(FILE_NAME, "w+"); |
---|
101 | if(f == NULL){ |
---|
102 | printf("Error opening file\n"); |
---|
103 | return; |
---|
104 | } |
---|
105 | sprintf(line, "%d\n", n); |
---|
106 | fputs(line, f); |
---|
107 | sprintf(line, "%d\n", 0); |
---|
108 | fputs(line, f); |
---|
109 | for(i = 0; i < n; i++){ |
---|
110 | //printf("here\n"); |
---|
111 | sprintf(line, "%f %f %f %f %f %f %f\n", |
---|
112 | bodies[i].mass, |
---|
113 | bodies[i].pos[0], bodies[i].pos[1], bodies[i].pos[2], |
---|
114 | bodies[i].vel[0], bodies[i].vel[1], bodies[i].vel[2]); |
---|
115 | fputs(line, f); |
---|
116 | } |
---|
117 | fclose(f); |
---|
118 | |
---|
119 | } |
---|
120 | |
---|
121 | int main(int argv, char** argc){ |
---|
122 | int n; |
---|
123 | unsigned int seed; |
---|
124 | if(argv != 3){ |
---|
125 | printf("usage ./Model nr_bodies seed\n"); |
---|
126 | return 0; |
---|
127 | } |
---|
128 | |
---|
129 | n = atoi(argc[1]); |
---|
130 | seed = atoi(argc[2]); |
---|
131 | |
---|
132 | bodies = (body*) malloc(n * sizeof(body)); |
---|
133 | mkplummer(n, seed); |
---|
134 | //writeData(n); |
---|
135 | return 1; |
---|
136 | } |
---|