3 * A class used to representing particles in the N-body simulation.
5 final class Body extends Node
9 public MathVector newAcc;
16 * Create an empty body.
21 vel = new MathVector();
22 acc = new MathVector();
23 newAcc = new MathVector();
30 * Set the next body in the list.
33 public void setNext(Body n)
39 * Get the next body in the list.
40 * @return the next body
48 * Set the next body in the list.
51 public void setProcNext(Body n)
57 * Get the next body in the list.
58 * @return the next body
60 public Body getProcNext()
66 * Enlarge cubical "box", salvaging existing tree structure.
67 * @param tree the root of the tree.
68 * @param nsteps the current time step
70 public void expandBox(Tree tree, int nsteps)
72 MathVector rmid = new MathVector();
74 boolean inbox = icTest(tree);
76 double rsize = tree.rsize;
77 rmid.addScalar(tree.rmin, 0.5 * rsize);
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);
85 tree.rsize = 2.0 * rsize;
86 if (tree.root != null) {
87 MathVector ic = tree.intcoord(rmid);
89 //throw new Error("Value is out of bounds");
92 int k = oldSubindex(ic, IMAX >> 1);
93 Cell newt = new Cell();
94 newt.subp[k] = tree.root;
102 * Check the bounds of the body and return true if it isn't in the
105 public boolean icTest(Tree tree)
107 double pos0 = pos.value(0);
108 double pos1 = pos.value(1);
109 double pos2 = pos.value(2);
111 // by default, it is in bounds
112 boolean result = true;
114 double xsc = (pos0 - tree.rmin.value(0)) / tree.rsize;
115 if (!(0.0 < xsc && xsc < 1.0)) {
119 xsc = (pos1 - tree.rmin.value(1)) / tree.rsize;
120 if (!(0.0 < xsc && xsc < 1.0)) {
124 xsc = (pos2 - tree.rmin.value(2)) / tree.rsize;
125 if (!(0.0 < xsc && xsc < 1.0)) {
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
138 * @param tree the root of the data structure
139 * @return the subtree with the new body inserted
141 public Node loadTree(Body p, MathVector xpic, int l, Tree tree)
144 Cell retval = new Cell();
145 int si = subindex(tree, l);
146 // attach this Body node to the cell
147 retval.subp[si] = this;
149 // move down one level
150 si = oldSubindex(xpic, l);
151 Node rt = retval.subp[si];
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);
167 * Descend tree finding center of mass coordinates
168 * @return the mass of this node
170 public double hackcofm()
176 * Determine which subcell to select.
177 * Combination of intcoord and oldSubindex.
178 * @param t the root of the tree
180 public int subindex(Tree tree, int l)
182 MathVector xp = new MathVector();
184 double xsc = (pos.value(0) - tree.rmin.value(0)) / tree.rsize;
185 xp.value(0, Math.floor(1073741824/*IMAX*/ * xsc));
187 xsc = (pos.value(1) - tree.rmin.value(1)) / tree.rsize;
188 xp.value(1, Math.floor(1073741824/*IMAX*/ * xsc));
190 xsc = (pos.value(2) - tree.rmin.value(2)) / tree.rsize;
191 xp.value(2, Math.floor(1073741824/*IMAX*/ * xsc));
194 for (int k = 0; k < xp.NDIM; k++) {
195 if (((int)xp.value(k) & l) != 0) {
196 i += 8/*Cell.NSUB*/ >> (k + 1);
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.
207 public void hackGravity(double rsize, Node root)
209 MathVector pos0 = (MathVector)pos.clone();
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);
220 this.newAcc = hg.acc0;
224 * Recursively walk the tree to do hackwalk calculation
226 public HG walkSubTree(double dsq, HG hg)
228 if (this != hg.pskip)
234 * Return a string represenation of a body.
235 * @return a string represenation of a body.
237 /*public String toString()
239 return "Body " + super.toString();