-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathBarnesHut.java
More file actions
84 lines (71 loc) · 2.92 KB
/
BarnesHut.java
File metadata and controls
84 lines (71 loc) · 2.92 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
import java.awt.Color;
public class BarnesHut {
public static void main(String[] args) {
final double dt = 0.1; // time quantum
int N = StdIn.readInt(); // number of particles
double radius = StdIn.readDouble(); // radius of universe
//few variables for display purposes
long start, end;
MovingAverage avTime = new MovingAverage(100); //lets get rolling average
boolean showQuads = false, showCalcTime = false;
// turn on animation mode and rescale coordinate system
StdDraw.show(0);
StdDraw.setXscale(-radius, +radius);
StdDraw.setYscale(-radius, +radius);
start = System.currentTimeMillis();
// read in and initialize bodies
Body[] bodies = new Body[N]; // array of N bodies
for (int i = 0; i < N; i++) {
double px = StdIn.readDouble();
double py = StdIn.readDouble();
double vx = StdIn.readDouble();
double vy = StdIn.readDouble();
double mass = StdIn.readDouble();
int red = StdIn.readInt();
int green = StdIn.readInt();
int blue = StdIn.readInt();
Color color = new Color(red, green, blue);
bodies[i] = new Body(px, py, vx, vy, mass, color);
}
end = System.currentTimeMillis();
System.out.println("Bodies read in: " + (double) ((end - start)/1000.0) + " seconds");
// simulate the universe movement
for (double t = 0.0; true; t += dt) {
start = System.currentTimeMillis();
//Build Quad (we are creating a universe here!)
Quad q = new Quad(0,0,radius*2.0);
BHTree thetree = new BHTree(q);
//Populate universe with bodies (make sure body is in universe)
for (int i = 0; i < N; i++) {
if (bodies[i].in(q)) thetree.insert(bodies[i]);
}
// update the forces and bodies if still in universe
for (int i = 0; i < N; i++) {
bodies[i].resetForce();
if (bodies[i].in(q)) {
thetree.updateForce(bodies[i]);
//Calculate the new positions
bodies[i].update(dt);
}
}
end = System.currentTimeMillis();
if(showCalcTime) System.out.println("Time ("+t+") computed in: " + (double) ((end - start)/1000.0) + " seconds");
avTime.newNum(end - start);
// draw the bodies
StdDraw.clear(StdDraw.BLACK);
String writeout = "Time: " + t;
//StdDraw.text(0,0,writeout);
if(showQuads) thetree.draw();
for (int i = 0; i < N; i++) {
bodies[i].draw();
}
StdDraw.show((int) avTime.getAvg());
if ( StdDraw.hasNextKeyTyped() ) {
char key = StdDraw.nextKeyTyped();
if(key == 'd') showQuads = !showQuads;
if(key == 't') showCalcTime = !showCalcTime;
if(key == 'q') System.exit(0);
}
}
}
}