fc19d059f32179f243909a5262adc5ebf09b9605
[matches/honours.git] / course / semester2 / pprog / assignment1 / single-thread / nbody.h
1 #ifndef _NBODY_H
2 #define _NBODY_H
3
4 /**
5  * @file nbody.h
6  * @author Sam Moore (205030628)
7  * @purpose N-Body simulator: declarations of simulation related parameters
8  */
9
10 #include <stdlib.h>
11 #include <stdbool.h>
12 #include <stdio.h>
13 #include <time.h>
14 #include <sys/time.h> //POSIX time
15
16           
17
18 #define SINGLE_THREADED
19
20 //These macros will be undefined by the multithreaded versions
21 #define Simulation_Run(argc, argv) (Graphics_Run(argc, argv)) //Sets up the simulation; in multithreaded versions, will spawn threads
22 #define QuitProgram(error) (runstate = (error == true) ? QUIT : QUIT_ERROR) //Prepares to exit program, is thread safe in multithreaded versions
23
24 #define M_PI        3.14159265358979323846264338327950288   /* pi */
25 #define G 6.67428E-11
26 #define DELTA_T 0.05
27 #define DIMENSIONS 3
28 #define LINE_SIZE 1000
29 #define square(x) ((x)*(x))
30
31
32 /**
33  * Structure to represent a single Body
34  * @param mass - Mass of the body
35  * @param x - Position vector (array)
36  * @param v - Velocity vector (array)
37  * @param F - Net force vector (array)
38  */
39 typedef struct 
40 {
41
42         double mass;
43         double x[DIMENSIONS];
44         double v[DIMENSIONS];
45         double F[DIMENSIONS];
46
47 } Body;
48
49 /**
50  * Structure to store an array of bodies, along with the size of the array.
51  * The universe is represented by a single System. 
52  * In the multithreaded program, the universe is subdivided into one system for each working thread to use.
53  * @param N - Size of the array
54  * @param body - The array of bodies
55  */
56 typedef struct
57 {
58         unsigned N; // Number of bodies in the System
59         Body * body; // Array of bodies
60
61         unsigned steps; //Number of steps simulated
62
63 } System;
64
65 /**
66  * Structure to represent options passed to the program. 
67  */
68 typedef struct
69 {
70         const char * input; // initial body field
71         const char * output; // file to write final positions / velocities of bodies to
72         const char * program; // program name
73         unsigned num_threads; // number of worker threads to spawn (must be greater than 1 for any to be spawned)
74         unsigned num_steps; // number of steps to run before stopping (run indefinately if equal to zero)
75         unsigned timeout; // number of seconds to run before stopping (run indefinately if equal to zero)
76         bool draw_graphics; // whether or not to actually draw graphics
77         bool print_positions; // print positions of bodies to stdout on every step
78         unsigned verbosity; // print statistics every number of steps indicated by this variable
79         clock_t start_clock;  // clock cycles done when simulation starts
80         struct timeval start_time; // time at which simulation starts
81 } Options;
82
83 void Body_Print(Body * a, FILE * out); //Print body a
84 void Body_Force(Body * a, System * s); //Compute force on body a due to system of bodies s
85 void Body_Velocity(Body * a); //Compute velocity of body a
86 void Body_Position(Body * a); //Compute position of body a
87
88 void System_Init(System * s, const char * fileName); //Initialise System (array of bodies) from a text file
89 void System_Compute(System * s);
90 void System_Forces(System * s1, System * s2); //Compute forces for bodies in s1 due to bodies in s2 (also updates velocities)
91 void System_Positions(System * s); //Update positions for bodies in s1
92
93
94 void Universe_Cleanup(); //Cleanup universe and write bodies to file
95
96 void DisplayStatistics(); // Print information about steps computed, total (real) runtime, and CPU cycles
97
98
99 bool ExitCondition(void); //Checks whether the program is supposed to exit automatically yet (ie: other than the user pressing "quit")
100
101
102 typedef enum {RUN, QUIT, QUIT_ERROR} RUNSTATE;
103 extern RUNSTATE runstate; // Set runstate to QUIT or QUIT_ERROR and the simulation will stop on the next step.
104 // This is fairly redundant in the single threaded version, but useful for making sure *all* threads know to exit in the multi-threaded version
105
106
107 extern System universe; // The main array of bodies; global variable.
108 extern Options options; // Parameters passed to program
109
110
111
112 #endif //_NBODY_H

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