Ported over bamboo benchmarks for use as non-Bamboo java benchmarks.
[IRC.git] / Robust / src / Benchmarks / Scheduling / GC / NON_BAMBOO / bh / Cell.java
1
2 /**
3  * A class used to represent internal nodes in the tree
4  **/
5 final class Cell extends Node
6 {
7   // subcells per cell
8   public final int NSUB;  // 1 << NDIM
9
10   /**
11    * The children of this cell node.  Each entry may contain either 
12    * another cell or a body.
13    **/
14   public Node[] subp;
15   public Cell   next;
16
17   public Cell()
18   {
19     super();
20     NSUB = 8;
21     subp = new Node[NSUB];
22     next = null;
23   }
24
25   /**
26    * Descend Tree and insert particle.  We're at a cell so 
27    * we need to move down the tree.
28    * @param p the body to insert into the tree
29    * @param xpic
30    * @param l
31    * @param tree the root of the tree
32    * @return the subtree with the new body inserted
33    **/
34   public  Node loadTree(Body p, MathVector xpic, int l, Tree tree)
35   {
36     // move down one level
37     int si = oldSubindex(xpic, l);
38     Node rt = subp[si];
39     if (rt != null) {
40       if(rt instanceof Body) {
41         Body rtb = (Body)rt;
42         subp[si] = rtb.loadTree(p, xpic, l >> 1, tree);
43       } else if(rt instanceof Cell) {
44         Cell rtc = (Cell)rt;
45         subp[si] = rtc.loadTree(p, xpic, l >> 1, tree);
46       }
47     } else { 
48       subp[si] = p;
49     }
50     return this;
51   }
52
53   /**
54    * Descend tree finding center of mass coordinates
55    * @return the mass of this node
56    **/
57   public  double hackcofm()
58   {
59     double mq = 0.0;
60     MathVector tmpPos = new MathVector();
61     MathVector tmpv   = new MathVector();
62     for (int i=0; i < NSUB; i++) {
63       Node r = subp[i];
64       if (r != null) {
65         double mr = 0.0;
66         if(r instanceof Body) {
67           Body rb = (Body)r;
68           mr = rb.hackcofm();
69         } else if(r instanceof Cell) {
70           Cell rc = (Cell)r;
71           mr = rc.hackcofm();
72         }
73         mq = mr + mq;
74         tmpv.multScalar(r.pos, mr);
75         tmpPos.addition(tmpv);
76       }
77     }
78     mass = mq;
79     pos = tmpPos;
80     pos.divScalar(mass);
81
82     return mq;
83   }
84
85   /**
86    * Recursively walk the tree to do hackwalk calculation
87    **/
88   public  HG walkSubTree(double dsq, HG hg)
89   {
90     if (subdivp(dsq, hg)) {
91       for (int k = 0; k < this.NSUB; k++) {
92         Node r = subp[k];
93         if (r != null) {
94           if(r instanceof Body) {
95             Body rb = (Body)r;
96             hg = rb.walkSubTree(dsq / 4.0, hg);
97           } else if(r instanceof Cell) {
98             Cell rc = (Cell)r;
99             hg = rc.walkSubTree(dsq / 4.0, hg);
100           }
101         }
102       }
103     } else {
104       hg = gravSub(hg);
105     }
106     return hg;
107   }
108
109   /**
110    * Decide if the cell is too close to accept as a single term.
111    * @return true if the cell is too close.
112    **/
113   public  boolean subdivp(double dsq, HG hg)
114   {
115     MathVector dr = new MathVector();
116     dr.subtraction(pos, hg.pos0);
117     double drsq = dr.dotProduct();
118
119     // in the original olden version drsp is multiplied by 1.0
120     return (drsq < dsq);
121   }
122
123   /**
124    * Return a string represenation of a cell.
125    * @return a string represenation of a cell.
126    **/
127   /*public String toString()
128   {
129     return "Cell " + super.toString();
130   }*/
131
132 }