1 Commits

Author SHA1 Message Date
10eb2fbb0e Trying to make more efficient 2022-05-04 19:53:16 +02:00
4 changed files with 73 additions and 49 deletions

View File

@ -37,12 +37,14 @@ public class Body {
* Hint: see simulation loop in Simulation.java to find out how this is done.
*/
public Vector gravitationalForce(Body b) {
Vector direction = b.massCenter.minus(massCenter);
Vector direction = b.massCenter.minus(this.massCenter);
double distance = direction.length();
if (distance == 0) return new Vector();
if (distance == 0) return direction;
direction.normalize();
double force = Simulation.G * mass * b.mass / (distance * distance);
return direction.times(force);
double force = Simulation.G * this.mass * b.mass / (distance * distance);
direction.mul(force);
return direction;
}
/**

View File

@ -36,11 +36,10 @@ public class Octree {
}
}
public void applyForces(Body[] bodies, double t) {
public void applyForces(Body[] bodies, Vector[] forces, double t) {
if (root == null) return;
Vector[] forces = new Vector[bodies.length];
for (int i = 0; i < bodies.length; i++) {
forces[i] = root.getForcesOnBody(bodies[i], t);
root.addForcesOnBody(bodies[i], forces[i], t);
}
for (int i = 0; i < bodies.length; i++) {
bodies[i].move(forces[i]);
@ -51,12 +50,6 @@ public class Octree {
if (root == null) return;
root.draw(cd);
}
public int getNumberOfBalancedRegions() {
if (root == null || !(root instanceof OctreeNode r)) return 0;
return r.getNumberOfBalancedRegions();
}
}
abstract class OctreeItem {
@ -74,6 +67,8 @@ abstract class OctreeItem {
abstract protected Vector getForcesOnBody(Body body, double t);
abstract protected void addForcesOnBody(Body body, Vector force, double t);
abstract protected void draw(CodeDraw cd);
}
@ -109,18 +104,24 @@ class OctreeNode extends OctreeItem {
@Override
protected Vector getForcesOnBody(Body body, double t) {
double r = pseudoBody.massCenter().distanceTo(body.massCenter());
Vector v = new Vector();
addForcesOnBody(body, v, t);
return v;
}
@Override
protected void addForcesOnBody(Body body, Vector force, double t) {
double r = center.distanceTo(body.massCenter());
if (r == 0) {
return new Vector();
return;
} else if (size / r < t) {
return body.gravitationalForce(pseudoBody);
force.add(body.gravitationalForce(pseudoBody));
return;
}
Vector force = new Vector();
for (OctreeItem child : children) {
if (child == null) continue;
force.add(child.getForcesOnBody(body, t));
child.addForcesOnBody(body, force, t);
}
return force;
}
@Override
@ -155,26 +156,6 @@ class OctreeNode extends OctreeItem {
((octNum & 4) != 0) ? -1 : 1)
.times(this.size / 4));
}
private boolean isBalanced() {
for (OctreeItem i : children) {
if (!(i instanceof OctreeLeaf))
return false;
}
return true;
}
protected int getNumberOfBalancedRegions() {
if (isBalanced()) return 1;
int c = 0;
for (OctreeItem i : children) {
if (i instanceof OctreeNode n) {
c += n.getNumberOfBalancedRegions();
}
}
return c;
}
}
class OctreeLeaf extends OctreeItem {
@ -183,7 +164,7 @@ class OctreeLeaf extends OctreeItem {
public OctreeLeaf(Vector center, double size, Body body) {
super(center, size);
this.body = body;
this.pseudoBody = new Body(this.body);
//this.pseudoBody = new Body(this.body);
}
@Override
@ -193,14 +174,21 @@ class OctreeLeaf extends OctreeItem {
@Override
protected Vector getForcesOnBody(Body body, double t) {
return body.gravitationalForce(pseudoBody);
return body.gravitationalForce(body);
}
@Override
protected void addForcesOnBody(Body body, Vector force, double t) {
force.add(getForcesOnBody(body, t));
}
@Override
protected void draw(CodeDraw cd) {
/*
Vector p1 = center.minus(new Vector(size / 2));
Vector p2 = new Vector(size).minus(new Vector(Simulation.SECTION_SIZE / 2));
cd.setColor(Color.GREEN);
cd.drawRectangle(p1.getScreenX(cd), p1.getScreenY(cd), p2.getScreenX(cd), p2.getScreenY(cd));
*/
}
}

View File

@ -23,7 +23,7 @@ public class Simulation {
// set some system parameters
public static final double SECTION_SIZE = 2 * AU; // the size of the square region in space
public static final int NUMBER_OF_BODIES = 10000;
public static final double OVERALL_SYSTEM_MASS = 2000 * SUN_MASS; // kilograms
public static final double OVERALL_SYSTEM_MASS = 20 * SUN_MASS; // kilograms
public static void main(String[] args) {
CodeDraw cd = new CodeDraw();
@ -48,15 +48,34 @@ public class Simulation {
}
long seconds = 0;
Vector[] forces = new Vector[bodies.length];
for (int i = 0; i < forces.length; i++) forces[i] = new Vector();
while (true) {
seconds++;
/*
BodyLinkedList mergedBodies = new BodyLinkedList();
for (Body b1 : bodies) {
BodyLinkedList colliding = bodies.removeCollidingWith(b1);
for (Body b2 : colliding) {
b1 = b1.merge(b2);
}
mergedBodies.addLast(b1);
}
bodies = mergedBodies;
*/
//long n1 = System.nanoTime();
Octree octree = new Octree(new Vector(0, 0, 0), SECTION_SIZE);
for (Body body : bodies) {
octree.add(body);
}
System.out.println(octree.getNumberOfBalancedRegions());
octree.applyForces(bodies, 100);
//long n2 = System.nanoTime();
for (Vector f : forces) f.reset();
octree.applyForces(bodies, forces, 1);
//long n3 = System.nanoTime();
//System.out.format("%8.3f µs, %8.3f µs\n", (n2 - n1) / 1000.0, (n3 - n2) / 1000.0);
if ((seconds % 60) == 0) {
cd.clear(Color.BLACK);

View File

@ -58,6 +58,18 @@ public class Vector {
return new Vector(x * d, y * d, z * d);
}
public void mul(double d) {
this.x *= d;
this.y *= d;
this.z *= d;
}
public void div(double d) {
this.x /= d;
this.y /= d;
this.z /= d;
}
/**
* Returns the sum of this vector and -1*v.
*/
@ -80,7 +92,7 @@ public class Vector {
* Returns the length (norm) of this vector.
*/
public double length() {
return distanceTo(new Vector());
return Math.sqrt(x * x + y * y + z * z);
}
/**
@ -88,10 +100,13 @@ public class Vector {
* The direction and orientation of the vector is not affected.
*/
public void normalize() {
double length = length();
x /= length;
y /= length;
z /= length;
div(length());
}
public void reset() {
x = 0;
y = 0;
z = 0;
}
public double getScreenX(CodeDraw cd) {