4 * A class used to representing particles in the N-body simulation.
6 final class Body extends Node
10 public MathVector newAcc;
17 * Create an empty body.
22 vel = new MathVector();
23 acc = new MathVector();
24 newAcc = new MathVector();
31 * Set the next body in the list.
34 public void setNext(Body n)
40 * Get the next body in the list.
41 * @return the next body
49 * Set the next body in the list.
52 public void setProcNext(Body n)
58 * Get the next body in the list.
59 * @return the next body
61 public Body getProcNext()
67 * Enlarge cubical "box", salvaging existing tree structure.
68 * @param tree the root of the tree.
69 * @param nsteps the current time step
71 public void expandBox(Tree tree, int nsteps)
73 MathVector rmid = new MathVector();
75 boolean inbox = icTest(tree);
77 double rsize = tree.rsize;
78 rmid.addScalar(tree.rmin, 0.5 * rsize);
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);
86 tree.rsize = 2.0 * rsize;
87 if (tree.root != null) {
88 MathVector ic = tree.intcoord(rmid);
90 //throw new Error("Value is out of bounds");
93 int k = oldSubindex(ic, IMAX >> 1);
94 Cell newt = new Cell();
95 newt.subp[k] = tree.root;
103 * Check the bounds of the body and return true if it isn't in the
106 public boolean icTest(Tree tree)
108 double pos0 = pos.value(0);
109 double pos1 = pos.value(1);
110 double pos2 = pos.value(2);
112 // by default, it is in bounds
113 boolean result = true;
115 double xsc = (pos0 - tree.rmin.value(0)) / tree.rsize;
116 if (!(0.0 < xsc && xsc < 1.0)) {
120 xsc = (pos1 - tree.rmin.value(1)) / tree.rsize;
121 if (!(0.0 < xsc && xsc < 1.0)) {
125 xsc = (pos2 - tree.rmin.value(2)) / tree.rsize;
126 if (!(0.0 < xsc && xsc < 1.0)) {
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
139 * @param tree the root of the data structure
140 * @return the subtree with the new body inserted
142 public Node loadTree(Body p, MathVector xpic, int l, Tree tree)
145 Cell retval = new Cell();
146 int si = subindex(tree, l);
147 // attach this Body node to the cell
148 retval.subp[si] = this;
150 // move down one level
151 si = oldSubindex(xpic, l);
152 Node rt = retval.subp[si];
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);
168 * Descend tree finding center of mass coordinates
169 * @return the mass of this node
171 public double hackcofm()
177 * Determine which subcell to select.
178 * Combination of intcoord and oldSubindex.
179 * @param t the root of the tree
181 public int subindex(Tree tree, int l)
183 MathVector xp = new MathVector();
185 double xsc = (pos.value(0) - tree.rmin.value(0)) / tree.rsize;
186 xp.value(0, Math.floor(1073741824/*IMAX*/ * xsc));
188 xsc = (pos.value(1) - tree.rmin.value(1)) / tree.rsize;
189 xp.value(1, Math.floor(1073741824/*IMAX*/ * xsc));
191 xsc = (pos.value(2) - tree.rmin.value(2)) / tree.rsize;
192 xp.value(2, Math.floor(1073741824/*IMAX*/ * xsc));
195 for (int k = 0; k < xp.NDIM; k++) {
196 if (((int)xp.value(k) & l) != 0) {
197 i += 8/*Cell.NSUB*/ >> (k + 1);
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.
208 public void hackGravity(double rsize, Node root)
210 MathVector pos0 = (MathVector)pos.clone();
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);
221 this.newAcc = hg.acc0;
225 * Recursively walk the tree to do hackwalk calculation
227 public HG walkSubTree(double dsq, HG hg)
229 if (this != hg.pskip)
235 * Return a string represenation of a body.
236 * @return a string represenation of a body.
238 /*public String toString()
240 return "Body " + super.toString();