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
|
/*
* force.cc
*/
#include "def.h"
inline vect_t newton_acceleration (t_real mass, vect_t cg, vect_t pos)
{
t_real x0 = VX(pos), y0 = VY(pos), z0 = VZ(pos);
t_real x1 = VX(cg), y1 = VY(cg), z1 = VZ(cg);
t_real dx = x1 - x0, dy = y1 - y0, dz = z1 - z0;
t_real l2 = dx * dx + dy * dy + dz * dz;
t_real k = mass / (l2 * sqrt (l2));
return make_vect(k * dx, k * dy, k * dz);
}
vect_t space::calc_accel(vect_t pos)
{
switch (state) {
case ONE_PARTICLE: {
return make_vect(0.0, 0.0, 0.0);
} /* ONE_PARTICLE */
case MULTIPLE_PARTICLES: {
int idx = select_covering_rectangle(pos, area);
t_real resultx = 0.0, resulty = 0.0, resultz = 0.0;
for (int i = 0; i < N_CHILDREN; i++) {
space * s = subspaces[i];
if (i == idx) {
vect_t sv = s->calc_accel(pos);
resultx += VX(sv);
resulty += VY(sv);
resultz += VZ(sv);
} else {
if (s) {
vect_t sv = s->calc_accel1(pos);
resultx += VX(sv);
resulty += VY(sv);
resultz += VZ(sv);
}
}
}
return make_vect(resultx, resulty, resultz);
} /* MULTIPLE_PARTICLES */
default: {
printf("NO_PARTICLES!\n");
return make_vect(0.0, 0.0, 0.0); /* never reach */
} /* default */
}
}
vect_t space::calc_accel_morton(vect_t pos)
{
switch (state) {
case ONE_PARTICLE: {
return make_vect(0.0, 0.0, 0.0);
} /* ONE_PARTICLE */
case MULTIPLE_PARTICLES: {
unsigned int stride = (midx.high - midx.low) / 8;
int idx = (vect2morton(pos) - midx.low) / stride + 1;
t_real resultx = 0.0, resulty = 0.0, resultz = 0.0;
for (int i = 0; i < N_CHILDREN; i++) {
space * s = subspaces[i];
if (s && i == idx) {
vect_t sv = s->calc_accel_morton(pos);
resultx += VX(sv);
resulty += VY(sv);
resultz += VZ(sv);
} else {
if (s) {
vect_t sv = s->calc_accel1(pos);
resultx += VX(sv);
resulty += VY(sv);
resultz += VZ(sv);
}
}
}
return make_vect(resultx, resulty, resultz);
} /* MULTIPLE_PARTICLES */
default: {
printf("NO_PARTICLES!\n");
return make_vect(0.0, 0.0, 0.0); /* never reach */
} /* default */
}
}
#if 0
const t_real approx_theta = 0.67 * 0.67; /* 1.05263 */
#else
const t_real approx_theta = 1.0 * 1.0; /* 1.05263 */
#endif
vect_t space::calc_accel1(vect_t pos)
{
switch (state) {
case ONE_PARTICLE: {
return newton_acceleration(mass, cg, pos);
} /* ONE_PARTICLE */
case MULTIPLE_PARTICLES: {
t_real x0 = VX(pos), y0 = VY(pos), z0 = VZ(pos);
t_real x1 = VX(cg), y1 = VY(cg), z1 = VZ(cg);
t_real dx = x1 - x0, dy = y1 - y0, dz = z1 - z0;
t_real l2 = dx * dx + dy * dy + dz * dz;
if (diameter2 < l2 * approx_theta) {
t_real k = mass / (l2 * sqrt (l2));
return make_vect(k * dx, k * dy, k * dz);
} else {
t_real resultx = 0.0, resulty = 0.0, resultz = 0.0;
for (int i = 0; i < N_CHILDREN; i++) {
space * s = subspaces[i];
if (s) {
vect_t sv = s->calc_accel1(pos);
resultx += VX(sv);
resulty += VY(sv);
resultz += VZ(sv);
}
}
return make_vect(resultx, resulty, resultz);
}
} /* MULTIPLE_PARTICLES */
default: {
printf("NO_PARTICLES!\n");
return make_vect(0.0, 0.0, 0.0); /* never reach */
}
} /* default */
}
void particle::set_accel(space * sp)
{
#if USE_MORTON
vect_t a = sp->calc_accel_morton(pos);
#else
vect_t a = sp->calc_accel(pos);
#endif
accel = a;
}
struct thread_dat {
particle ** particles;
int begin;
int end;
space * sp;
};
void * set_accels_dac(void *args)
{
thread_dat * p = (thread_dat *) args;
if (p->end - p->begin < 5) {
for (int i = p->begin; i <= p->end; i++)
p->particles[i]->set_accel(p->sp);
} else {
int c = (p->begin + p->end - 1) / 2;
thread_dat left, right;
void * ret;
pthread_t th;
left.particles = p->particles;
left.begin = p->begin;
left.end = c;
left.sp = p->sp;
right.particles = p->particles;
right.begin = c + 1;
right.end = p->end;
right.sp = p->sp;
pthread_create(&th, NULL, set_accels_dac, (void *) &left);
set_accels_dac(&right);
pthread_join(th, (void **) ret);
}
}
void set_accels(particle ** particles, int n_particles, space * sp)
{
thread_dat data;
data.particles = particles;
data.begin = 0;
data.end = n_particles - 1;
data.sp = sp;
set_accels_dac(&data);
}
void particle::move(t_real dt)
{
vect_t vel_prime = v_plus_v (vel, k_times_v(dt, accel));
pos = v_plus_v (pos, k_times_v(dt, vel_prime));
vel = vel_prime;
}
void move_particles(particle ** particles, int n_particles, t_real dt)
{
for (int i = 0; i < n_particles; i++)
particles[i]->move(dt);
}
|