Parallel Programming - Final version
[matches/honours.git] / course / semester2 / pprog / assignment1 / single-thread / nbody.c~
index df7cd9d..718f25c 100644 (file)
@@ -62,6 +62,9 @@ inline void Body_Forces(Body * a, System * s)
  */
 inline void Body_Force(Body * a, Body * b)
 {
+       float distance;
+       float con;
+       float gd;
        //Calculate distance between a and b
        distance = 0.0;
        for (unsigned i = 0; i < DIMENSIONS; ++i)
@@ -155,6 +158,14 @@ inline void System_Init(System * s, const char *fileName)
        FILE * file;
 
        file = fopen(fileName, "rt");
+
+       if (file == NULL)
+       {
+               fprintf(stderr, "%s\n", fileName);
+               perror("Couldn't open file");
+               exit(EXIT_FAILURE);
+       }
+
        s->N = atoi(fgets(line, LINE_SIZE, file));
        s->body = (Body*) calloc((size_t)s->N, sizeof(Body));
        s->steps = 0;
@@ -303,3 +314,32 @@ System * Split_System(System * s, unsigned n)
        }
        return result;
 }
+
+/**
+ * @function System_Random
+ * @purpose Randomly generate initial body field
+ * @param s - The system
+ * @param n - Number of bodies
+ */
+void System_Random(System * s, int n)
+{
+       srand(time(NULL));
+       s->N = (unsigned)n;
+       s->body = (Body*) calloc((size_t)s->N, sizeof(Body));
+       s->steps = 0;
+
+       s->body[0].mass = 1e11;
+
+       for (unsigned a = 1; a < s->N; ++a)
+       {
+               s->body[a].mass = 1e7;
+               for (unsigned i = 0; i < DIMENSIONS; ++i)
+               {
+                       s->body[a].x[i] = -10000 + rand() % 20000;
+                       s->body[a].v[i] = -2 + rand() % 4; 
+               }
+               s->body[a].v[2] = 0;
+               s->body[a].x[2] = 0; //set z to zero; force in plane
+               
+       }
+}

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