290eeb61c86c7a8f459bd4d5d31bdd78dc5aad5f
[matches/honours.git] / course / semester2 / pprog / assignment1 / mthread / nbody.c
1 /**
2  * @file nbody.c
3  * @author Sam Moore (20503628) 2012
4  * @purpose N-Body simulator - Definition of simulation functions; single threaded version
5  */
6
7 #include <stdlib.h>
8 #include <stdio.h>
9 #include <math.h>
10 #include <time.h>
11 #include <string.h>
12 #include <stdbool.h>
13
14 #include "nbody.h"
15
16 /**
17  * Prints the body on screen
18  */
19 void Body_Print(Body * a)
20 {
21         printf("Body %p M=%f X=%f Y=%f Z=%f Fx=%f Fy=%f Fz=%f Vx=%f Vy=%f Vz=%f\n", 
22            (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]);
23 }
24
25 /**
26  * Computing forces
27  */
28 void Body_Force(Body * a, System * s) 
29 {
30         double distance;
31         double con;
32         double gd;
33
34         for (unsigned i = 0; i < DIMENSIONS; ++i)
35                 a->F[i] = 0;
36
37         for (unsigned index = 0; index < s->N; ++index)
38         {
39                 Body * b = s->body+index;
40                 if (b == a)
41                         continue;
42                 
43                 distance = 0.0;
44                 for (unsigned i = 0; i < DIMENSIONS; ++i)
45                         distance += square(b->x[i] - a->x[i]);
46                 distance = sqrt(distance);
47                 con = G * a->mass * b->mass / square(distance);
48                 gd = con / distance;    
49                 for (unsigned i = 0; i < DIMENSIONS; ++i)
50                         a->F[i] += gd * (b->x[i] - a->x[i]);
51         }
52 }
53
54 /**
55  * Compute velocities
56  */
57 void Body_Velocity(Body * a) 
58 {
59         for (unsigned i = 0; i < DIMENSIONS; ++i)
60                 a->v[i] += a->F[i] / a->mass * DELTA_T;
61 }
62
63 /**
64  * Compute positions
65  */
66 void Body_Position(Body * a) 
67 {
68         for (unsigned i = 0; i < DIMENSIONS; ++i)
69                 a->x[i] += a->v[i] * DELTA_T;
70 }
71
72 /**
73  * Main compute function
74  */
75 void System_Compute(System * s) 
76 {
77 //      clock_t start, finish;
78
79 //      start = clock();
80
81         for (unsigned i = 0; i < s->N; ++i)
82         {
83                 Body_Force(s->body+i, s);
84                 Body_Velocity(s->body+i);
85                 Body_Position(s->body+i);
86         }
87
88 }
89
90
91
92
93 /*
94  * This function reads an input file. You can change it if you choose a 
95  * different file format
96  */
97 #define LINE_SIZE BUFSIZ
98 void System_Init(System * s, char *fileName) 
99 {
100         char line[LINE_SIZE];
101         char * token;
102         FILE * file;
103
104         file = fopen(fileName, "rt");
105         s->N = atoi(fgets(line, LINE_SIZE, file));
106         s->body = (Body*) calloc((size_t)s->N, sizeof(Body));
107
108         //printf("----------------------Initial field-------------------------------\n");
109
110         for (unsigned i = 0; i < s->N; ++i)
111         {
112                 if (fgets(line, LINE_SIZE, file) != NULL)
113                 {
114                         Body * a = s->body+i;
115                         token = strtok(line, ",; ");
116                         a->mass = atof(token);
117                         
118                         for (unsigned j = 0; j < DIMENSIONS; ++j)
119                         {
120                                 token = strtok(NULL, ",; ");
121                                 a->x[j] = atof(token);
122                         }
123                         for (unsigned j = 0; j < DIMENSIONS; ++j)
124                         {
125                                 token = strtok(NULL, ",; ");
126                                 a->v[j] = atof(token);
127                         }
128                         //Body_Print(a);
129                 }
130         }
131
132         //printf("--------------------------------------------------------------\n");
133         
134         fclose(file);
135 }
136
137 /**
138  * Cleans up the universe by freeing all memory
139  */
140 void Universe_Cleanup()
141 {
142         free(universe.body);
143         pthread_exit(NULL);
144 }
145
146 /**
147  * Thread - Continuously computes steps for a system of bodies
148  */
149 void * Compute_Thread(void * s)
150 {
151         while (true)
152         {
153                 if (terminate) pthread_exit(NULL);
154                 System_Compute((System*)s);
155         }
156 }
157
158
159

UCC git Repository :: git.ucc.asn.au