-
Notifications
You must be signed in to change notification settings - Fork 0
/
world.c
233 lines (195 loc) · 5.57 KB
/
world.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
/*
* world.c
*
* @author: phdenzel
*
* DYDAMA universe properties
*
*/
#include "world.h"
#include "sort.h"
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <assert.h>
void init_particleProperties(particle_t *p, double m, double size, char* type) {
// initialize particle properties
p->m = m;
p->size = size;
p->type = type;
}
void set_particleRVA(particle_t *p, int dimension,
double x, double v, double a) {
// initialize particle position/velocity/acceleration in dimension
vector4_setIndex(&p->r, dimension, x);
vector4_setIndex(&p->v, dimension, v);
vector4_setIndex(&p->a, dimension, a);
}
void set_particleR(particle_t *p, double x, double y, double z) {
// set the particles position r
vector4_set(&p->r, 0, x, y, z);
}
void set_particleV(particle_t *p, double vx, double vy, double vz) {
// set the particles velocity v
vector4_set(&p->v, 0, vx, vy, vz);
}
void set_particleA(particle_t *p, double ax, double ay, double az) {
// set the particles acceleration a
vector4_set(&p->a, 0, ax, ay, az);
}
void set_totalMass(universe *u) {
// set the total mass parameter of u
assert(u->N > 0 || u->particles != NULL);
double totalMass = 0;
for (int i = 0; i < u->N; i++) {
totalMass += u->particles[i].m;
}
u->Mtot = totalMass;
}
void set_projection(universe *u,
double xmin, double xmax, double ymin, double ymax) {
// set the projection parameters of u
u->proj[0] = xmin;
u->proj[1] = xmax;
u->proj[2] = ymin;
u->proj[3] = ymax;
}
inline int sgn(double val) {
return (val > 0) - (val < 0);
}
inline void gForceUpdate(universe *u) {
// calculate gravitational force and update particle accelerations
double G = u->G;
switch (u->D) {
case 1: { // 1D gravity
int this, other;
double a, this_x;
#pragma omp simd
for (this = 0; this < u->N; this++) {
u->particles[this].a.x = 0;
}
#pragma omp simd
for (this = 0; this < u->N-1; this++) {
this_x = u->particles[this].r.x;
for (other = this+1; other < u->N; other++) {
// copysign seems to be faster than the sgn function
a = G * copysign(1, u->particles[other].r.x - this_x);
u->particles[this].a.x += a*u->particles[other].m;
u->particles[other].a.x -= a*u->particles[this].m;
}
}
} break;
case 2: { // 2D gravity
printf("2D gravity not yet implemented!\n");
} break;
case 3: { // 3D gravity
int this, other;
// reset forces
for (this = 0; this < u->N; this++) {
vector4_set(&u->particles[this].a, 0, 0, 0, 0);
}
double n;
vector4_t r;
#pragma omp simd
for (this = 0; this < u->N-1; this++) {
for (other = this+1; other < u->N; other++) {
r = vector4_subtractcpy(&u->particles[this].r, &u->particles[other].r);
n = 1./vector4_length(&r);
n = n*n*n;
n *= u->G;
vector4_scale(&r, n*u->particles[other].m);
vector4_add(&u->particles[this].a, &r);
vector4_scale(&r, -1*u->particles[this].m/u->particles[other].m);
vector4_add(&u->particles[other].a, &r);
}
}
} break;
}
}
inline void gForceFast(universe *u) {
// sort the particles first and then calculate accelerations
// only possible if particle masses are all equal
double G = u->G;
switch (u->D) {
case 1: { // 1D gravity
int i;
double Meff = u->Mtot;
xQuickSort(u, 0, u->N-1);
#pragma omp simd
for (i = 0; i < u->N/2; i++) {
Meff -= u->particles[i].m;
u->particles[i].a.x = G*Meff;
u->particles[u->N-i-1].a.x = -G*Meff;
Meff -= u->particles[i].m;
}
} break;
case 2: { // 2D gravity
printf("2D gravity not yet implemented!\n");
} break;
case 3: { // 3D gravity
printf("3D gravity not yet implemented!\n");
} break;
}
}
void drift(particle_t *p, double dt, unsigned char dimensions) {
// perform a drift step for particle p of amount dt
switch (dimensions) {
case 1: { // 1D x-coordinate drift
p->r.x += p->v.x * dt;
} break;
case 2: { // 2D x and y-coordinate drift
p->r.x += p->v.x * dt;
p->r.y += p->v.y * dt;
//vector4_t vdt = vector4_scalecpy(&p->v, dt);
//p->r = vector4_addcpy(&p->r, &vdt);
} break;
case 3: { // full 3D drift
vector4_t vdt = vector4_scalecpy(&p->v, dt);
vector4_add(&p->r, &vdt);
} break;
}
}
void kick(particle_t *p, double dt, unsigned char dimensions) {
// perform a drift step for particle p of amount dt
switch (dimensions) {
case 1: { // 1D x-coordinate drift
p->v.x += p->a.x * dt;
} break;
case 2: { // 2D x and y-coordinate drift
p->v.x += p->a.x * dt;
p->v.y += p->a.y * dt;
//vector4_t adt = vector4_scalecpy(&p->a, dt);
//p->v = vector4_addcpy(&p->v, &adt);
} break;
case 3: { // full 3D drift
vector4_t adt = vector4_scalecpy(&p->a, dt);
vector4_add(&p->v, &adt);
} break;
}
}
void drift_halfStep(universe *u) {
// perform a half drift step for all particles in universe u
for (int i = 0; i < u->N; i++) {
drift(&u->particles[i], u->dt/2, u->D);
}
}
void kick_fullStep(universe *u) {
// perform a full kick step of all particles in universe u
for (int i = 0; i < u->N; i++) {
kick(&u->particles[i], u->dt, u->D);
}
}
inline void evolve(universe *u) {
// perform a leap frog step for all particles in universe u
drift_halfStep(u);
gForceUpdate(u);
kick_fullStep(u);
drift_halfStep(u);
}
inline void evolveFast(universe *u) {
// perform a leap frog step with improved force calculation
drift_halfStep(u);
gForceFast(u);
kick_fullStep(u);
drift_halfStep(u);
}