Fixed issues with compilation.
[IRC.git] / Robust / src / Benchmarks / Scheduling / GC / NON_BAMBOO / bh / Tree.java
1 package bh;
2
3 //import java.util.Enumeration;
4
5 /**
6  * A class that represents the root of the data structure used
7  * to represent the N-bodies in the Barnes-Hut algorithm.
8  **/
9 class Tree
10 {
11   public double DTIME; 
12
13   MathVector rmin;
14   public double     rsize;
15   /**
16    * A reference to the root node.
17    **/
18   public Node       root;
19   /**
20    * The complete list of bodies that have been created.
21    **/
22   public  Body       bodyTab;
23   /**
24    * The complete list of bodies that have been created - in reverse.
25    **/
26   public  Body       bodyTabRev;
27
28   /**
29    * Construct the root of the data structure that represents the N-bodies.
30    **/
31   public Tree(double DTIME)
32   {
33     rmin       = new MathVector();
34     rsize      = -2.0 * -2.0;
35     root       = null;
36     bodyTab    = null;
37     bodyTabRev = null;
38
39     rmin.value(0, -2.0);
40     rmin.value(1, -2.0);
41     rmin.value(2, -2.0);
42
43     this.DTIME = DTIME;
44   }
45
46   /**
47    * Return an enumeration of the bodies.
48    * @return an enumeration of the bodies.
49    **/
50   /*final Enumeration bodies()
51   {
52     return bodyTab.elements();
53   }*/
54
55   /**
56    * Return an enumeration of the bodies - in reverse.
57    * @return an enumeration of the bodies - in reverse.
58    **/
59   /*final Enumeration bodiesRev()
60   {
61     return bodyTabRev.elementsRev();
62   }*/
63
64   /**
65    * Random number generator used by the orignal BH benchmark.
66    * @param seed the seed to the generator
67    * @return a random number
68    **/
69   public double myRand(double seed)
70   {
71     double t = 16807.0*seed + 1;
72
73     double iseed = t - (2147483647.0 * Math.floor(t / 2147483647.0f));
74     return iseed;
75   }
76
77   /**
78    * Generate a doubleing point random number.  Used by
79    * the original BH benchmark.
80    *
81    * @param xl lower bound
82    * @param xh upper bound
83    * @param r seed
84    * @return a doubleing point randon number
85    **/
86   public double xRand(double xl, double xh, double r)
87   {
88     double res = xl + (xh-xl)*r/2147483647.0;
89     return res;
90   }
91
92   /**
93    * Create the testdata used in the benchmark.
94    * @param nbody the number of bodies to create
95    **/
96   public  void createTestData(int nbody)
97   {
98     MathVector cmr = new MathVector();
99     MathVector cmv = new MathVector();
100
101     Body head = new Body();
102     Body prev = head;
103
104     double rsc  = 3.0 * Math.PI() / 16.0;
105     double vsc  = Math.sqrt(1.0 / rsc);
106     double seed = 123.0;
107     //Random rand = new Random((long)seed);
108     //int max_int = ~(int)0x1+1;
109
110     for (int i = 0; i < nbody; i++) {
111       Body p = new Body();
112
113       prev.setNext(p);
114       prev = p;
115       p.mass = 1.0/nbody;
116
117       seed      = myRand(seed);
118       //seed = Math.abs((double)rand.nextInt()/max_int);
119       double t1 = xRand(0.0, 0.999, seed);
120       t1        = Math.pow(t1, (-2.0/3.0)) - 1.0;
121       double r  = 1.0 / Math.sqrt(t1);
122
123       double coeff = 4.0;
124       for (int k = 0; k < cmr.NDIM; k++) {
125         seed = myRand(seed);
126         //seed = Math.abs((double)rand.nextInt()/max_int);
127         r = xRand(0.0, 0.999, seed);
128         p.pos.value(k, coeff*r);
129       }
130
131       cmr.addition(p.pos);
132
133       double x, y;
134       do {
135         seed = myRand(seed);
136         //seed = Math.abs((double)rand.nextInt()/max_int);
137         x    = xRand(0.0, 1.0, seed);
138         seed = myRand(seed);
139         //seed = Math.abs((double)rand.nextInt()/max_int);
140         y    = xRand(0.0, 0.1, seed);
141       } while (y > (x*x * Math.pow((1.0f - x*x), 3.5)));
142
143       double v = Math.sqrt(2.0) * x / Math.pow(1 + r*r, 0.25);
144
145       double rad = vsc * v;
146       double rsq;
147       do {
148         for (int k = 0; k < cmr.NDIM; k++) {
149           seed     = myRand(seed);
150           //seed = Math.abs((double)rand.nextInt()/max_int);
151           p.vel.value(k, xRand(-1.0, 1.0, seed));
152         }
153         rsq = p.vel.dotProduct();
154       } while (rsq > 1.0);
155       double rsc1 = rad / Math.sqrt(rsq);
156       p.vel.multScalar(rsc1);
157       cmv.addition(p.vel);
158     }
159
160     // mark end of list
161     prev.setNext(null);
162     // toss the dummy node at the beginning and set a reference to the first element
163     bodyTab = head.getNext();
164
165     cmr.divScalar(nbody);
166     cmv.divScalar(nbody);
167
168     prev = null;
169     Body b = this.bodyTab;
170     do {
171       b.pos.subtraction(cmr);
172       b.vel.subtraction(cmv);
173       b.setProcNext(prev);
174       prev = b;
175       b = b.getNext();
176     } while(b != null);
177     // set the reference to the last element
178     bodyTabRev = prev;
179   }
180
181
182   /**
183    * Advance the N-body system one time-step.
184    * @param nstep the current time step
185    **/
186   public void stepSystem(int nstep)
187   {
188     // free the tree
189     this.root = null;
190
191     makeTree(nstep);
192
193     Body next = null;
194     Body b = this.bodyTabRev;
195     do {
196       b.hackGravity(this.rsize, this.root);
197       b = b.getProcNext();
198     } while(b != null);
199
200     vp(this.bodyTabRev, nstep);
201
202   }
203
204   /**
205    * Initialize the tree structure for hack force calculation.
206    * @param nsteps the current time step
207    **/
208   public  void makeTree(int nstep)
209   {
210     Body q = this.bodyTabRev;
211     do {
212       if (q.mass != 0.0) {
213         q.expandBox(this, nstep);
214         MathVector xqic = intcoord(q.pos);
215         if (this.root == null) {
216           this.root = q;
217         } else {
218           if(root instanceof Body) {
219             Body rootb = (Body) root;
220             this.root = rootb.loadTree(q, xqic, 1073741824/*Node.IMAX*/ >> 1, this);
221           } else if(root instanceof Cell) {
222             Cell rootc = (Cell)root;
223             this.root = rootc.loadTree(q, xqic, 1073741824/*Node.IMAX*/ >> 1, this);
224           }
225         }
226       }
227       q = q.getProcNext();
228     } while(q != null);
229     if(root instanceof Body) {
230       Body rootb = (Body)root;
231       rootb.hackcofm();
232     } else if(root instanceof Cell) {
233       Cell rootc = (Cell)root;
234       rootc.hackcofm();
235     }
236   }
237
238   /**
239    * Compute integerized coordinates.
240    * @return the coordinates or null if rp is out of bounds
241    **/
242   public  MathVector intcoord(MathVector vp)
243   {
244     MathVector xp = new MathVector();
245
246     double xsc = (vp.value(0) - rmin.value(0)) / rsize;
247     if (0.0 <= xsc && xsc < 1.0) {
248       xp.value(0, Math.floor(1073741824/*Node.IMAX*/ * xsc));
249     } else {
250       return null;
251     }
252
253     xsc = (vp.value(1) - rmin.value(1)) / rsize;
254     if (0.0 <= xsc && xsc < 1.0) {
255       xp.value(1, Math.floor(1073741824/*Node.IMAX*/ * xsc));
256     } else {
257       return null;
258     }
259
260     xsc = (vp.value(2) - rmin.value(2)) / rsize;
261     if (0.0 <= xsc && xsc < 1.0) {
262       xp.value(2, Math.floor(1073741824/*Node.IMAX*/ * xsc));
263     } else {
264       return null;
265     }
266     return xp;
267   }
268
269   public  void vp(Body p, int nstep)
270   {
271     MathVector dacc = new MathVector();
272     MathVector dvel = new MathVector();
273     double dthf = 0.5 * this.DTIME;
274
275     Body b = p;
276     do {
277       MathVector acc1 = (MathVector)b.newAcc.clone();
278       if (nstep > 0) {
279         dacc.subtraction(acc1, b.acc);
280         dvel.multScalar(dacc, dthf);
281         dvel.addition(b.vel);
282         b.vel = (MathVector)dvel.clone();
283       }
284       b.acc = (MathVector)acc1.clone();
285       dvel.multScalar(b.acc, dthf);
286
287       MathVector vel1 = (MathVector)b.vel.clone();
288       vel1.addition(dvel);
289       MathVector dpos = (MathVector)vel1.clone();
290       dpos.multScalar(this.DTIME);
291       dpos.addition(b.pos);
292       b.pos = (MathVector)dpos.clone();
293       vel1.addition(dvel);
294       b.vel = (MathVector)vel1.clone();
295
296       b = b.getProcNext();
297     } while(b != null);
298   }
299 }