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