X-Git-Url: https://git.ucc.asn.au/?a=blobdiff_plain;f=course%2Fsemester2%2Fpprog%2Fassignment1%2Fnbody.c;fp=course%2Fsemester2%2Fpprog%2Fassignment1%2Fnbody.c;h=0000000000000000000000000000000000000000;hb=7a341db1fbf5db94b711135b2a79e852c6fb1bc4;hp=d2062e46dc9fe6654b0fd6da1241104339b5e612;hpb=8050cacaa489292c82977b3d87971eaf9ca3c39b;p=matches%2Fhonours.git diff --git a/course/semester2/pprog/assignment1/nbody.c b/course/semester2/pprog/assignment1/nbody.c deleted file mode 100644 index d2062e46..00000000 --- a/course/semester2/pprog/assignment1/nbody.c +++ /dev/null @@ -1,137 +0,0 @@ -/** - * @file nbody.c - * @author Sam Moore (20503628) 2012 - * @purpose N-Body simulator - Definition of simulation functions; single threaded version - */ - -#include -#include -#include -#include -#include - -#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); -} - - -