b9cac6fa8a4de32df2f346f1ea96b4ff51a14385
[matches/honours.git] / course / semester2 / pprog / assignment1 / mthread / 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
11 #define NUM_THREADS 5 //The number of *worker* threads (not including the computation thread) to use. 
12                         // If this is 1, the computation thread will not spawn any workers (faster to just do the work itself), 
13                         //      otherwise it will spawn NUM_THREADS workers.
14
15 #include <pthread.h>
16 #include <stdbool.h>
17
18 #define M_PI        3.14159265358979323846264338327950288   /* pi */
19 #define G 6.67428E-11
20 #define DELTA_T 0.05
21 #define DIMENSIONS 3
22 #define square(x) ((x)*(x))
23
24
25 /**
26  * Structure to represent a single Body
27  * @param mass - Mass of the body
28  * @param x - Position vector (array)
29  * @param v - Velocity vector (array)
30  * @param F - Net force vector (array)
31  */
32 typedef struct 
33 {
34
35         double mass;
36         double x[DIMENSIONS];
37         double v[DIMENSIONS];
38         double F[DIMENSIONS];
39
40 } Body;
41
42 /**
43  * Structure to store an array of bodies, along with the size of the array.
44  * The universe is represented in a single System. 
45  * @param N - Size of the array
46  * @param body - The array of bodies
47  */
48 typedef struct
49 {
50         unsigned N;
51         Body * body;
52
53 } System;
54
55 void Body_Print(Body * a); //Print body a
56 void Body_Force(Body * a, System * s); //Compute force on body a due to system of bodies s
57 void Body_Velocity(Body * a); //Compute velocity of body a
58 void Body_Position(Body * a); //Compute position of body a
59
60 void System_Init(System * s, char * fileName); //Initialise System (array of bodies) from a text file
61
62 void System_Forces(System * s1, System * s2); //Compute forces for bodies in s1 due to bodies in s2 (also updates velocities)
63 void System_Positions(System * s); //Update positions for bodies in s1
64 void Universe_Cleanup();
65
66 extern System universe; // The main array of bodies; global variable.
67
68
69 /**
70  * Multithreading stuff below here
71  */
72
73 extern pthread_t compute_thread; // ID of the thread that runs Compute_Thread. Set in main()
74 void * Compute_Thread(void * system); //Thread - Continuously perform computations for a System of bodies. May spawn additional worker threads.
75
76 void * Force_Thread(void * system); //Thread - Compute forces for all objects in a system
77 void * Position_Thread(void * system); //Thread - Compute positions for all objects in a system
78
79 typedef enum {RUN, QUIT, QUIT_ERROR} RUNSTATE;
80 extern RUNSTATE runstate;
81 extern pthread_mutex_t mutex_runstate; //Mutex around the "runstate" variable
82
83 void QuitProgram(bool error);
84
85
86
87 #endif //_NBODY_H

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