3 * @purpose Declaration of tree structures for Barnes Hut clustering algorithm for N-Body simulator
4 * @author Sam Moore (20503628) - 2012
12 #define SUB_DIVISIONS 8
14 // Define to break everything and nest the threading
15 // (Program becomes impossibly slow, but feel free to try)
16 // #define USE_THREADS
19 * Structure to represent each node of the tree (a cube (actually a prism) in space)
20 * I call them.... "Data Prisms"
21 * But that takes too long to type, so they are Node instead
25 float x[DIMENSIONS]; // The corner of the cube with smallest valued co-ordinates
26 float size[DIMENSIONS]; // The lengths of the cube faces.
28 Body * * body; // Array of pointers to bodies in the node; dynamically created
29 unsigned N; // Number of bodies in the node; valid length of array
30 unsigned allocated; //space allocated for bodies; used to dynamically resize array (doubles every time array is filled).
33 float com[DIMENSIONS]; // Centre of Mass
35 struct n * children[SUB_DIVISIONS]; // children nodes (smaller data prisms)
36 struct n * parent; // parent node; the bodies in this node are also in the parent node (bigger data prism)
41 void Node_SetSpace(Node * node, float x[DIMENSIONS], float size[DIMENSIONS]); //set the position and volume of a Node
42 void Node_Destroy(Node * node); // Cleanup a Node
43 unsigned Node_FindBodies(Node * n, System * s); // Find all bodies in System s contained in Node n
44 unsigned Node_Subdivide(Node * node, System * s); // Divide Node n into smaller nodes until each node has 1 Body in it
46 float Node_CalculateMass(Node * node); // Calculate the total mass of a Node
48 unsigned Node_AddBody(Node * n, Body * b); // Add a body to a Node
50 unsigned Generate_Tree(System * s, Node * n); // Generate the tree of Nodes for the *first* time
52 unsigned Update_Tree(System * s, Node * n); // Subsequently update the tree
54 unsigned Node_Forces(Body * b, Node * node, float theta); // Calculate forces on body due to node. Theta is the approximation factor.
56 void Node_FitSystem(Node * n, System * s); // Take a node and increase the size until it contains the entire system.
59 Node * Node_Create(Node * parent); // Construct a node for the first time
61 bool Node_ContainsBody(Node * n, Body * b); // Check if a Body is in a Node
62 Body * Node_Body(Node * n, unsigned index); // Identify the n'th body in a Node. Required because the Body* array is slightly special.
63 void Draw_Node(Node * n); // Draw a cube corresponding to Node n
66 // Used if nested threading is desired; not recommended. Breaks things
68 void Prepare_Threads(unsigned max);
69 extern unsigned nested_used;
70 extern omp_lock_t nested_lock;