Trying to make more efficient

This commit is contained in:
2022-05-04 19:53:16 +02:00
parent bf9a6c6d43
commit 10eb2fbb0e
4 changed files with 62 additions and 22 deletions

View File

@ -37,12 +37,14 @@ public class Body {
* Hint: see simulation loop in Simulation.java to find out how this is done. * Hint: see simulation loop in Simulation.java to find out how this is done.
*/ */
public Vector gravitationalForce(Body b) { public Vector gravitationalForce(Body b) {
Vector direction = b.massCenter.minus(massCenter); Vector direction = b.massCenter.minus(this.massCenter);
double distance = direction.length(); double distance = direction.length();
if (distance == 0) return new Vector(); if (distance == 0) return direction;
direction.normalize(); direction.normalize();
double force = Simulation.G * mass * b.mass / (distance * distance); double force = Simulation.G * this.mass * b.mass / (distance * distance);
return direction.times(force); 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; if (root == null) return;
Vector[] forces = new Vector[bodies.length];
for (int i = 0; i < bodies.length; i++) { 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++) { for (int i = 0; i < bodies.length; i++) {
bodies[i].move(forces[i]); bodies[i].move(forces[i]);
@ -68,6 +67,8 @@ abstract class OctreeItem {
abstract protected Vector getForcesOnBody(Body body, double t); abstract protected Vector getForcesOnBody(Body body, double t);
abstract protected void addForcesOnBody(Body body, Vector force, double t);
abstract protected void draw(CodeDraw cd); abstract protected void draw(CodeDraw cd);
} }
@ -103,18 +104,24 @@ class OctreeNode extends OctreeItem {
@Override @Override
protected Vector getForcesOnBody(Body body, double t) { protected Vector getForcesOnBody(Body body, double t) {
double r = pseudoBody.massCenter().distanceTo(body.massCenter()); Vector v = new Vector();
if (r == 0) { addForcesOnBody(body, v, t);
return new Vector(); return v;
} else if (size / r < t) { }
return body.gravitationalForce(pseudoBody);
@Override
protected void addForcesOnBody(Body body, Vector force, double t) {
double r = center.distanceTo(body.massCenter());
if (r == 0) {
return;
} else if (size / r < t) {
force.add(body.gravitationalForce(pseudoBody));
return;
} }
Vector force = new Vector();
for (OctreeItem child : children) { for (OctreeItem child : children) {
if (child == null) continue; if (child == null) continue;
force.add(child.getForcesOnBody(body, t)); child.addForcesOnBody(body, force, t);
} }
return force;
} }
@Override @Override
@ -157,7 +164,7 @@ class OctreeLeaf extends OctreeItem {
public OctreeLeaf(Vector center, double size, Body body) { public OctreeLeaf(Vector center, double size, Body body) {
super(center, size); super(center, size);
this.body = body; this.body = body;
this.pseudoBody = new Body(this.body); //this.pseudoBody = new Body(this.body);
} }
@Override @Override
@ -167,14 +174,21 @@ class OctreeLeaf extends OctreeItem {
@Override @Override
protected Vector getForcesOnBody(Body body, double t) { 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 @Override
protected void draw(CodeDraw cd) { protected void draw(CodeDraw cd) {
/*
Vector p1 = center.minus(new Vector(size / 2)); Vector p1 = center.minus(new Vector(size / 2));
Vector p2 = new Vector(size).minus(new Vector(Simulation.SECTION_SIZE / 2)); Vector p2 = new Vector(size).minus(new Vector(Simulation.SECTION_SIZE / 2));
cd.setColor(Color.GREEN); cd.setColor(Color.GREEN);
cd.drawRectangle(p1.getScreenX(cd), p1.getScreenY(cd), p2.getScreenX(cd), p2.getScreenY(cd)); cd.drawRectangle(p1.getScreenX(cd), p1.getScreenY(cd), p2.getScreenX(cd), p2.getScreenY(cd));
*/
} }
} }

View File

@ -22,7 +22,7 @@ public class Simulation {
// set some system parameters // set some system parameters
public static final double SECTION_SIZE = 2 * AU; // the size of the square region in space public static final double SECTION_SIZE = 2 * AU; // the size of the square region in space
public static final int NUMBER_OF_BODIES = 220; public static final int NUMBER_OF_BODIES = 10000;
public static final double OVERALL_SYSTEM_MASS = 20 * SUN_MASS; // kilograms public static final double OVERALL_SYSTEM_MASS = 20 * SUN_MASS; // kilograms
public static void main(String[] args) { public static void main(String[] args) {
@ -48,6 +48,9 @@ public class Simulation {
} }
long seconds = 0; long seconds = 0;
Vector[] forces = new Vector[bodies.length];
for (int i = 0; i < forces.length; i++) forces[i] = new Vector();
while (true) { while (true) {
seconds++; seconds++;
@ -62,11 +65,17 @@ public class Simulation {
} }
bodies = mergedBodies; bodies = mergedBodies;
*/ */
//long n1 = System.nanoTime();
Octree octree = new Octree(new Vector(0, 0, 0), SECTION_SIZE); Octree octree = new Octree(new Vector(0, 0, 0), SECTION_SIZE);
for (Body body : bodies) { for (Body body : bodies) {
octree.add(body); octree.add(body);
} }
octree.applyForces(bodies, 1); //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) { if ((seconds % 60) == 0) {
cd.clear(Color.BLACK); cd.clear(Color.BLACK);

View File

@ -58,6 +58,18 @@ public class Vector {
return new Vector(x * d, y * d, z * d); 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. * Returns the sum of this vector and -1*v.
*/ */
@ -80,7 +92,7 @@ public class Vector {
* Returns the length (norm) of this vector. * Returns the length (norm) of this vector.
*/ */
public double length() { 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. * The direction and orientation of the vector is not affected.
*/ */
public void normalize() { public void normalize() {
double length = length(); div(length());
x /= length; }
y /= length;
z /= length; public void reset() {
x = 0;
y = 0;
z = 0;
} }
public double getScreenX(CodeDraw cd) { public double getScreenX(CodeDraw cd) {