Parallel Programming - Work on pthreaded version
[matches/honours.git] / course / semester2 / pprog / assignment1 / mthread / nbody.c
diff --git a/course/semester2/pprog/assignment1/mthread/nbody.c b/course/semester2/pprog/assignment1/mthread/nbody.c
new file mode 100644 (file)
index 0000000..290eeb6
--- /dev/null
@@ -0,0 +1,159 @@
+/**
+ * @file nbody.c
+ * @author Sam Moore (20503628) 2012
+ * @purpose N-Body simulator - Definition of simulation functions; single threaded version
+ */
+
+#include <stdlib.h>
+#include <stdio.h>
+#include <math.h>
+#include <time.h>
+#include <string.h>
+#include <stdbool.h>
+
+#include "nbody.h"
+
+/**
+ * Prints the body on screen
+ */
+void Body_Print(Body * a)
+{
+       printf("Body %p M=%f X=%f Y=%f Z=%f Fx=%f Fy=%f Fz=%f Vx=%f Vy=%f Vz=%f\n", 
+           (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]);
+}
+
+/**
+ * Computing forces
+ */
+void Body_Force(Body * a, System * s) 
+{
+       double distance;
+       double con;
+       double gd;
+
+       for (unsigned i = 0; i < DIMENSIONS; ++i)
+               a->F[i] = 0;
+
+       for (unsigned index = 0; index < s->N; ++index)
+       {
+               Body * b = s->body+index;
+               if (b == a)
+                       continue;
+               
+               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)
+                       a->F[i] += gd * (b->x[i] - a->x[i]);
+       }
+}
+
+/**
+ * Compute velocities
+ */
+void Body_Velocity(Body * a) 
+{
+       for (unsigned i = 0; i < DIMENSIONS; ++i)
+               a->v[i] += a->F[i] / a->mass * DELTA_T;
+}
+
+/**
+ * Compute positions
+ */
+void Body_Position(Body * a) 
+{
+       for (unsigned i = 0; i < DIMENSIONS; ++i)
+               a->x[i] += a->v[i] * DELTA_T;
+}
+
+/**
+ * Main compute function
+ */
+void System_Compute(System * s) 
+{
+//     clock_t start, finish;
+
+//     start = clock();
+
+       for (unsigned i = 0; i < s->N; ++i)
+       {
+               Body_Force(s->body+i, s);
+               Body_Velocity(s->body+i);
+               Body_Position(s->body+i);
+       }
+
+}
+
+
+
+
+/*
+ * This function reads an input file. You can change it if you choose a 
+ * different file format
+ */
+#define LINE_SIZE BUFSIZ
+void System_Init(System * s, char *fileName) 
+{
+       char line[LINE_SIZE];
+       char * token;
+       FILE * file;
+
+       file = fopen(fileName, "rt");
+       s->N = atoi(fgets(line, LINE_SIZE, file));
+       s->body = (Body*) calloc((size_t)s->N, sizeof(Body));
+
+       //printf("----------------------Initial field-------------------------------\n");
+
+       for (unsigned i = 0; i < s->N; ++i)
+       {
+               if (fgets(line, LINE_SIZE, file) != NULL)
+               {
+                       Body * a = s->body+i;
+                       token = strtok(line, ",; ");
+                       a->mass = atof(token);
+                       
+                       for (unsigned j = 0; j < DIMENSIONS; ++j)
+                       {
+                               token = strtok(NULL, ",; ");
+                               a->x[j] = atof(token);
+                       }
+                       for (unsigned j = 0; j < DIMENSIONS; ++j)
+                       {
+                               token = strtok(NULL, ",; ");
+                               a->v[j] = atof(token);
+                       }
+                       //Body_Print(a);
+               }
+       }
+
+       //printf("--------------------------------------------------------------\n");
+       
+       fclose(file);
+}
+
+/**
+ * Cleans up the universe by freeing all memory
+ */
+void Universe_Cleanup()
+{
+       free(universe.body);
+       pthread_exit(NULL);
+}
+
+/**
+ * Thread - Continuously computes steps for a system of bodies
+ */
+void * Compute_Thread(void * s)
+{
+       while (true)
+       {
+               if (terminate) pthread_exit(NULL);
+               System_Compute((System*)s);
+       }
+}
+
+
+

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