* @param a - The Body
* @param b - The System
*/
-inline void Body_Force(Body * a, System * s)
+inline void Body_Forces(Body * a, System * s)
{
- double distance;
- double con;
- double gd;
+
for (unsigned i = 0; i < DIMENSIONS; ++i) //Set Force to zero
a->F[i] = 0;
if (b == a)
continue; //Otherwise we would have infinite force
- //Calculate distance between a and b
- distance = 0.0;
- for (unsigned i = 0; i < DIMENSIONS; ++i)
- distance += square(b->x[i] - a->x[i]);
- distance = sqrt(distance);
- con = G * a->mass * b->mass / square(distance);
- gd = con / distance;
- for (unsigned i = 0; i < DIMENSIONS; ++i) //Add force from b to a's net force
- a->F[i] += gd * (b->x[i] - a->x[i]);
+ Body_Force(a, b);
}
}
+/**
+ * @function Body_Force
+ * @purpose Compute force on body a due to body b
+ * @param a - Body a
+ * @param b - Body b
+ */
+inline void Body_Force(Body * a, Body * b)
+{
+ double distance;
+ double con;
+ double gd;
+ //Calculate distance between a and b
+ distance = 0.0;
+ for (unsigned i = 0; i < DIMENSIONS; ++i)
+ distance += square(b->x[i] - a->x[i]);
+ distance = sqrt(distance);
+ con = G * a->mass * b->mass / square(distance);
+ gd = con / distance;
+ for (unsigned i = 0; i < DIMENSIONS; ++i) //Add force from b to a's net force
+ a->F[i] += gd * (b->x[i] - a->x[i]);
+}
+
/**
* @function Body_Velocity
* @purpose Compute velocity of a body
//printf("Compute forces for %p - %d bodies...\n", (void*)s1, s1->N);
for (unsigned i = 0; i < s1->N; ++i)
{
- Body_Force(s1->body+i, s2);
+ Body_Forces(s1->body+i, s2);
Body_Velocity(s1->body+i);
}
//printf("Compute positions for %p - Done!\n", (void*)s1);
//fprintf(stderr,"runstate %d\n timeout %d\n steps %d\n", (int)(runstate != RUN), (int)(options.timeout > 0.00 && ((unsigned)(time(NULL) - options.start_time.tv_sec) >= options.timeout)), (int)(options.num_steps > 0 && universe.steps > options.num_steps));
return result;
}
+
+/**
+ * @function Split_System
+ * @purpose Helper to divide one system into an array of systems
+ * Each sub system will have N = (s->N / n) bodies in it
+ * @param s - The original system (typically &universe)
+ * @param n - The number of sub systems in the array
+ *
+ * WARNING: It is the caller's responsibility to free() the returned array
+ */
+System * Split_System(System * s, unsigned n)
+{
+ System * result = (System*)(calloc(n, sizeof(System)));
+ if (result == NULL)
+ {
+ perror("Couldn't create array of sub systems");
+ exit(EXIT_FAILURE);
+ }
+
+ unsigned n_per_system = (s->N) / n;
+ unsigned remainder = (s->N) % n;
+
+ for (unsigned i = 0; i < n; ++i)
+ {
+ result[i].N = n_per_system;
+ if (i == n-1)
+ result[i].N += remainder;
+ result[i].body = (s->body) + (n_per_system * i);
+ result[i].steps = 0;
+ }
+ return result;
+}