Ported over bamboo benchmarks for use as non-Bamboo java benchmarks.
[IRC.git] / Robust / src / Benchmarks / Scheduling / GC / NON_BAMBOO / bh / Body.java
1
2 /**
3  * A class used to representing particles in the N-body simulation.
4  **/
5 final class Body extends Node
6 {
7   public MathVector vel;
8   public MathVector acc;
9   public MathVector newAcc;
10   public double     phi;
11
12   public  Body next;
13   public  Body procNext;
14
15   /**
16    * Create an empty body.
17    **/
18   public Body()
19   {
20     super();
21     vel      = new MathVector();
22     acc      = new MathVector();
23     newAcc   = new MathVector();
24     phi      = 0.0;
25     next     = null;
26     procNext = null;
27   }
28
29   /**
30    * Set the next body in the list.
31    * @param n the body
32    **/
33   public  void setNext(Body n)
34   {
35     next = n;
36   }
37
38   /**
39    * Get the next body in the list.
40    * @return the next body
41    **/
42   public  Body getNext()
43   {
44     return next;
45   }
46
47   /**
48    * Set the next body in the list.
49    * @param n the body
50    **/
51   public  void setProcNext(Body n)
52   {
53     procNext = n;
54   }
55
56   /**
57    * Get the next body in the list.
58    * @return the next body
59    **/
60   public  Body getProcNext()
61   {
62     return procNext;
63   }
64
65   /**
66    * Enlarge cubical "box", salvaging existing tree structure.
67    * @param tree the root of the tree.
68    * @param nsteps the current time step
69    **/
70   public  void expandBox(Tree tree, int nsteps)
71   {
72     MathVector rmid = new MathVector();
73
74     boolean inbox = icTest(tree);
75     while (!inbox) {
76       double rsize = tree.rsize;
77       rmid.addScalar(tree.rmin, 0.5 * rsize);
78
79       for (int k = 0; k < rmid.NDIM; k++) {
80         if (pos.value(k) < rmid.value(k)) {
81           double rmin = tree.rmin.value(k);
82           tree.rmin.value(k, rmin - rsize);
83         }
84       }
85       tree.rsize = 2.0 * rsize;
86       if (tree.root != null) {
87         MathVector ic = tree.intcoord(rmid);
88         if (ic == null) {
89           //throw new Error("Value is out of bounds");
90           System.exit(-2);
91         }
92         int k = oldSubindex(ic, IMAX >> 1);
93         Cell newt = new Cell();
94         newt.subp[k] = tree.root;
95         tree.root = newt;
96         inbox = icTest(tree);
97       }
98     }
99   }
100
101   /**
102    * Check the bounds of the body and return true if it isn't in the
103    * correct bounds.
104    **/
105   public  boolean icTest(Tree tree)
106   {
107     double pos0 = pos.value(0);
108     double pos1 = pos.value(1);
109     double pos2 = pos.value(2);
110
111     // by default, it is in bounds
112     boolean result = true;
113
114     double xsc = (pos0 - tree.rmin.value(0)) / tree.rsize;
115     if (!(0.0 < xsc && xsc < 1.0)) {
116       result = false;
117     }
118
119     xsc = (pos1 - tree.rmin.value(1)) / tree.rsize;
120     if (!(0.0 < xsc && xsc < 1.0)) {
121       result = false;
122     }
123
124     xsc = (pos2 - tree.rmin.value(2)) / tree.rsize;
125     if (!(0.0 < xsc && xsc < 1.0)) {
126       result = false;
127     }
128
129     return result;
130   }
131
132   /**
133    * Descend Tree and insert particle.  We're at a body so we need to
134    * create a cell and attach this body to the cell.
135    * @param p the body to insert
136    * @param xpic
137    * @param l 
138    * @param tree the root of the data structure
139    * @return the subtree with the new body inserted
140    **/
141   public  Node loadTree(Body p, MathVector xpic, int l, Tree tree)
142   {
143     // create a Cell
144     Cell retval = new Cell();
145     int si = subindex(tree, l);
146     // attach this Body node to the cell
147     retval.subp[si] = this;
148
149     // move down one level
150     si = oldSubindex(xpic, l);
151     Node rt = retval.subp[si];
152     if (rt != null)  {
153       if(rt instanceof Body) {
154         Body rtb = (Body) rt;
155         retval.subp[si] = rtb.loadTree(p, xpic, l >> 1, tree);
156       } else if(rt instanceof Cell){
157         Cell rtc = (Cell) rt;
158         retval.subp[si] = rtc.loadTree(p, xpic, l >> 1, tree);
159       }
160     } else {
161       retval.subp[si] = p;
162     }
163     return retval;
164   }
165
166   /**
167    * Descend tree finding center of mass coordinates
168    * @return the mass of this node
169    **/
170   public  double hackcofm()
171   {
172     return mass;
173   }
174
175   /**
176    * Determine which subcell to select.
177    * Combination of intcoord and oldSubindex.
178    * @param t the root of the tree
179    **/
180   public  int subindex(Tree tree, int l)
181   {
182     MathVector xp = new MathVector();
183
184     double xsc = (pos.value(0) - tree.rmin.value(0)) / tree.rsize;
185     xp.value(0, Math.floor(1073741824/*IMAX*/ * xsc));
186
187     xsc = (pos.value(1) - tree.rmin.value(1)) / tree.rsize;
188     xp.value(1, Math.floor(1073741824/*IMAX*/ * xsc));
189
190     xsc = (pos.value(2) - tree.rmin.value(2)) / tree.rsize;
191     xp.value(2, Math.floor(1073741824/*IMAX*/ * xsc));
192
193     int i = 0;
194     for (int k = 0; k < xp.NDIM; k++) {
195       if (((int)xp.value(k) & l) != 0) {
196         i += 8/*Cell.NSUB*/ >> (k + 1);
197       }
198     }
199     return i;
200   }
201
202   /**
203    * Evaluate gravitational field on the body.
204    * The original olden version calls a routine named "walkscan",
205    * but we use the same name that is in the Barnes code.
206    **/
207   public  void hackGravity(double rsize, Node root)
208   {
209     MathVector pos0 = (MathVector)pos.clone();
210
211     HG hg = new HG(this, pos);
212     if(root instanceof Body) {
213       Body rootb = (Body)root;
214       hg = rootb.walkSubTree(rsize * rsize, hg);
215     } else if(root instanceof Cell) {
216       Cell rootc = (Cell)root;
217       hg = rootc.walkSubTree(rsize * rsize, hg);
218     }
219     this.phi = hg.phi0;
220     this.newAcc = hg.acc0;
221   }
222
223   /**
224    * Recursively walk the tree to do hackwalk calculation
225    **/
226   public  HG walkSubTree(double dsq, HG hg)
227   {
228     if (this != hg.pskip)
229       hg = gravSub(hg);
230     return hg;
231   }
232
233   /**
234    * Return a string represenation of a body.
235    * @return a string represenation of a body.
236    **/
237   /*public String toString()
238   {
239     return "Body " + super.toString();
240   }*/
241
242 }