X-Git-Url: https://git.ucc.asn.au/?p=matches%2Fhonours.git;a=blobdiff_plain;f=course%2Fsemester2%2Fpprog%2Fassignment1%2Fsingle-thread%2Fnbody.c~;h=718f25cf3448a6f67804ca2cd594593513ca6a44;hp=df7cd9d071be1a6a875116c3bdfb7381cb1f8af7;hb=f2970e1b67ccd1adff8cd04a176efdc9682adf06;hpb=77f864ae45bc0940954ea7fdcb4150d4ed031d03 diff --git a/course/semester2/pprog/assignment1/single-thread/nbody.c~ b/course/semester2/pprog/assignment1/single-thread/nbody.c~ index df7cd9d0..718f25cf 100644 --- a/course/semester2/pprog/assignment1/single-thread/nbody.c~ +++ b/course/semester2/pprog/assignment1/single-thread/nbody.c~ @@ -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 + + } +}