
import ibis.satin.SharedObject;

final public class Bodies extends SharedObject implements BodiesInterface {

    Body[] bodyArray;
    
    transient BodyTreeNode bodyTreeRoot;

    int numBodies;

    int maxLeafBodies;

    double theta;

    public int iteration = -1;
    
    public Bodies(int numBodies, int maxLeafBodies, double theta) {
    
        this.numBodies = numBodies;
	this.maxLeafBodies = maxLeafBodies;
	this.theta = theta;
        bodyArray = new Plummer().generate(numBodies);
	bodyTreeRoot = new BodyTreeNode(bodyArray, maxLeafBodies, theta);
        bodyTreeRoot.computeCentersOfMass();
    }
    
    /*write method*/
    public void updateBodies(double[] accs_x, double[] accs_y, double[] accs_z,
			     int iteration) {
	updateBodiesLocally(accs_x, accs_y, accs_z, iteration);
    }
    
    public void updateBodiesLocally(double[] accs_x, double[] accs_y, double[] accs_z,
			     int iteration) {

        for (int i = 0; i < bodyArray.length; i++) {
            bodyArray[i].computeNewPosition(iteration != 0, BarnesHut.DT, accs_x[i],
                    accs_y[i], accs_z[i]);
        }  
	bodyTreeRoot = null; /*to prevent OutOfMemoryError (maik)*/
	bodyTreeRoot = new BodyTreeNode(bodyArray, maxLeafBodies, theta);
	bodyTreeRoot.computeCentersOfMass();
	this.iteration = iteration;
    }    

    public BodyTreeNode findTreeNode(byte[] treeNodeIdentifier) {

	/*no consistency check for the time being*/
	if (bodyTreeRoot == null) {
	    System.err.println("bodyTreeRoot is null!");
	}

	BodyTreeNode treeNode = bodyTreeRoot;
	if (treeNodeIdentifier != null) {
	    for (int i = 0; i < treeNodeIdentifier.length; i++) {
		treeNode = treeNode.children[treeNodeIdentifier[i]];
	    }
	}
	return treeNode;
    }

    private void readObject(java.io.ObjectInputStream in) 
    throws java.io.IOException, ClassNotFoundException {
	in.defaultReadObject();
	bodyTreeRoot = new BodyTreeNode(bodyArray, maxLeafBodies, theta);
	bodyTreeRoot.computeCentersOfMass();
    }
	

}

@@ -1,9 +1,8 @@
-%0A
 import i
@@ -125,20 +125,16 @@
 yArray;%0A
-    
 %0A    tra
@@ -262,20 +262,16 @@
 n = -1;%0A
-    
 %0A    pub
@@ -331,20 +331,16 @@
 heta) %7B%0A
-    
 %0A       
@@ -364,25 +364,32 @@
  numBodies;%0A
-%09
+        
 this.maxLeaf
@@ -412,17 +412,24 @@
 Bodies;%0A
-%09
+        
 this.the
@@ -495,17 +495,24 @@
 odies);%0A
-%09
+        
 bodyTree
@@ -616,28 +616,24 @@
 ss();%0A    %7D%0A
-    
 %0A    /*write
@@ -714,35 +714,39 @@
 ouble%5B%5D accs_z,%0A
-%09%09%09
+       
      int iterati
@@ -751,17 +751,24 @@
 tion) %7B%0A
-%09
+        
 updateBo
@@ -821,20 +821,16 @@
 ;%0A    %7D%0A
-    
 %0A    pub
@@ -883,32 +883,44 @@
 double%5B%5D accs_y,
+%0A           
  double%5B%5D accs_z
@@ -924,16 +924,8 @@
 s_z,
-%0A%09%09%09    
  int
@@ -1069,19 +1069,8 @@
 .DT,
- accs_x%5Bi%5D,
 %0A   
@@ -1082,19 +1082,26 @@
         
-   
+accs_x%5Bi%5D,
  accs_y%5B
@@ -1129,12 +1129,17 @@
    %7D
+%0A      
   
-%0A%09
 body
@@ -1194,17 +1194,24 @@
 maik)*/%0A
-%09
+        
 bodyTree
@@ -1260,33 +1260,40 @@
 Bodies, theta);%0A
-%09
+        
 bodyTreeRoot.com
@@ -1313,17 +1313,24 @@
 Mass();%0A
-%09
+        
 this.ite
@@ -1354,20 +1354,16 @@
 n;%0A    %7D
-    
 %0A%0A    pu
@@ -1423,17 +1423,24 @@
 ier) %7B%0A%0A
-%09
+        
 /*no con
@@ -1475,17 +1475,24 @@
 being*/%0A
-%09
+        
 if (body
@@ -1507,25 +1507,32 @@
  == null) %7B%0A
-%09
+        
     System.e
@@ -1572,13 +1572,27 @@
 %22);%0A
-%09%7D%0A%0A%09
+        %7D%0A%0A        
 Body
@@ -1625,17 +1625,24 @@
 eeRoot;%0A
-%09
+        
 if (tree
@@ -1667,17 +1667,24 @@
 null) %7B%0A
-%09
+        
     for 
@@ -1737,10 +1737,24 @@
 ) %7B%0A
-%09%09
+                
 tree
@@ -1806,19 +1806,40 @@
 %5D%5D;%0A
-%09
+       
     
+ 
 %7D%0A
-%09%7D%0A%09
+        %7D%0A        
 retu
@@ -1919,10 +1919,17 @@
  in)
- 
 %0A
+        
     
@@ -1985,34 +1985,212 @@
 n %7B%0A
-%09in.defaultReadObject();%0A%09
+        long start = System.currentTimeMillis();%0A        in.defaultReadObject();%0A        double time = System.currentTimeMillis() - start;%0A        long rebuildStart = System.currentTimeMillis(); %0A        
 body
@@ -2251,17 +2251,24 @@
 theta);%0A
-%09
+        
 bodyTree
@@ -2304,11 +2304,181 @@
     
-%7D%0A%09%0A
+    double rebuildTime = System.currentTimeMillis() - start;%0A        System.err.println(%22readTree: deserializtion = %22 + time + %22 ms, rebuild = %22 + rebuildTime + %22 ms%22);%0A    %7D
 %0A%7D%0A

debug print