3 * @author Sam Moore (20503628) 2012
4 * @purpose N-Body simulator - Definition of simulation functions; single threaded version
16 * Prints the body on screen
18 void Body_Print(Body * a)
20 printf("Body %p M=%f X=%f Y=%f Z=%f Fx=%f Fy=%f Fz=%f Vx=%f Vy=%f Vz=%f\n",
21 (void*)a, a->mass, a->x[0], a->x[1], a->x[2], a->F[0], a->F[1], a->F[2], a->v[0], a->v[1], a->v[2]);
27 void Body_Force(Body * a, System * s)
33 for (unsigned i = 0; i < DIMENSIONS; ++i)
36 for (unsigned index = 0; index < s->N; ++index)
38 Body * b = s->body+index;
43 for (unsigned i = 0; i < DIMENSIONS; ++i)
44 distance += square(b->x[i] - a->x[i]);
45 distance = sqrt(distance);
46 con = G * a->mass * b->mass / square(distance);
48 for (unsigned i = 0; i < DIMENSIONS; ++i)
49 a->F[i] += gd * (b->x[i] - a->x[i]);
56 void Body_Velocity(Body * a)
58 for (unsigned i = 0; i < DIMENSIONS; ++i)
59 a->v[i] += a->F[i] / a->mass * DELTA_T;
65 void Body_Position(Body * a)
67 for (unsigned i = 0; i < DIMENSIONS; ++i)
68 a->x[i] += a->v[i] * DELTA_T;
72 * Main compute function
74 void System_Compute(System * s)
76 // clock_t start, finish;
80 for (unsigned i = 0; i < s->N; ++i)
82 Body_Force(s->body+i, s);
83 Body_Velocity(s->body+i);
84 Body_Position(s->body+i);
93 * This function reads an input file. You can change it if you choose a
94 * different file format
96 #define LINE_SIZE BUFSIZ
97 void System_Init(System * s, char *fileName)
103 file = fopen(fileName, "rt");
104 s->N = atoi(fgets(line, LINE_SIZE, file));
105 s->body = (Body*) calloc((size_t)s->N, sizeof(Body));
107 //printf("----------------------Initial field-------------------------------\n");
109 for (unsigned i = 0; i < s->N; ++i)
111 if (fgets(line, LINE_SIZE, file) != NULL)
113 Body * a = s->body+i;
114 token = strtok(line, ",; ");
115 a->mass = atof(token);
117 for (unsigned j = 0; j < DIMENSIONS; ++j)
119 token = strtok(NULL, ",; ");
120 a->x[j] = atof(token);
122 for (unsigned j = 0; j < DIMENSIONS; ++j)
124 token = strtok(NULL, ",; ");
125 a->v[j] = atof(token);
131 //printf("--------------------------------------------------------------\n");
137 * Cleans up the universe by freeing all memory
139 void Universe_Cleanup()