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