6 * @author Sam Moore (205030628)
7 * @purpose N-Body simulator: declarations of simulation related parameters
14 #include <sys/time.h> //POSIX time
18 #define SINGLE_THREADED
20 // --- The below macros will be undefined by the multithreaded versions, and replaced with functions --- //
22 //Sets up the simulation; in multithreaded versions, will spawn threads
23 #define Simulation_Run(argc, argv) \
24 if (options.draw_graphics) \
25 Graphics_Run(argc, argv); \
28 while (ExitCondition() == false) \
29 { BeforeDraw(); AfterDraw(); } \
32 #define QuitProgram(error) (runstate = (error == true) ? QUIT : QUIT_ERROR) //Prepares to exit program, is thread safe in multithreaded versions
34 //Macro to be overwritten in multithreaded versions, called before the graphics is allowed to draw anything
35 #define BeforeDraw() \
36 System_Compute(&universe); \
37 universe.steps += 1; \
38 if (options.verbosity != 0 && universe.steps % options.verbosity == 0) \
39 DisplayStatistics(); \
44 //Macro to be overwritten in multithreaded versions, called after the graphics has finished drawing.
47 // --- Constants and other Macros --- //
49 #define M_PI 3.14159265358979323846264338327950288 /* pi */
53 #define LINE_SIZE 1000
54 #define square(x) ((x)*(x))
58 * Structure to represent a single Body
59 * @param mass - Mass of the body
60 * @param x - Position vector (array)
61 * @param v - Velocity vector (array)
62 * @param F - Net force vector (array)
75 * Structure to store an array of bodies, along with the size of the array.
76 * The universe is represented by a single System.
77 * In the multithreaded program, the universe is subdivided into one system for each working thread to use.
78 * @param N - Size of the array
79 * @param body - The array of bodies
83 unsigned N; // Number of bodies in the System
84 Body * body; // Array of bodies
86 unsigned steps; //Number of steps simulated
91 * Structure to represent options passed to the program.
95 const char * input; // initial body field
96 const char * output; // file to write final positions / velocities of bodies to
97 const char * program; // program name
98 int num_threads; // number of worker threads to spawn (must be greater than 1 for any to be spawned)
99 int nested_threads; // number of threads to nest computations with (must be greater than 1 for any to be spawned)
100 int num_steps; // number of steps to run before stopping (run indefinately if less than zero)
101 int timeout; // number of seconds to run before stopping (run indefinately if less than zero)
102 bool draw_graphics; // whether or not to actually draw graphics
103 bool pedantic_graphics; // whether the graphics thread will synchronise with the computation thread (true) or just draw as fast as possible (false)
104 bool print_positions; // print positions of bodies to stdout on every step
105 int verbosity; // print statistics every number of steps indicated by this variable
106 clock_t start_clock; // clock cycles done when simulation starts
107 struct timeval start_time; // time at which simulation starts
110 void Body_Print(Body * a, FILE * out); //Print body a
111 void Body_Forces(Body * a, System * s); //Compute force on body a due to system of bodies s
112 void Body_Force(Body * a, Body * b); // Compute force on body a due to body b
113 void Body_Velocity(Body * a); //Compute velocity of body a
114 void Body_Position(Body * a); //Compute position of body a
116 void System_Init(System * s, const char * fileName); //Initialise System (array of bodies) from a text file
117 void System_Compute(System * s);
118 void System_Forces(System * s1, System * s2); //Compute forces for bodies in s1 due to bodies in s2 (also updates velocities)
119 void System_Positions(System * s); //Update positions for bodies in s1
120 System * Split_System(System * s, unsigned n); // Splits one system into a number of other systems, returns an array of size n
122 void Universe_Cleanup(); //Cleanup universe and write bodies to file
124 void DisplayStatistics(); // Print information about steps computed, total (real) runtime, and CPU cycles
127 bool ExitCondition(void); //Checks whether the program is supposed to exit automatically yet (ie: other than the user pressing "quit")
130 typedef enum {RUN, QUIT, QUIT_ERROR} RUNSTATE;
131 extern RUNSTATE runstate; // Set runstate to QUIT or QUIT_ERROR and the simulation will stop on the next step.
132 // This is fairly redundant in the single threaded version, but useful for making sure *all* threads know to exit in the multi-threaded version
135 extern System universe; // The main array of bodies; global variable.
136 extern Options options; // Parameters passed to program