import java.util.Random; public class ForceLaw{ int numberOfParticles = 3; double time, finish, dt; Particle[] system; boolean running; public void init(){ setTime(0.0); setFinish(1.0); setDt(0.1); setRunning(false); system = new Particle[ numberOfParticles ]; for (int i = 0; i < numberOfParticles; i++){ system[i] = new Particle(); } report(system); } public void run(){ while (isRunning()){ advance(system); setTime(getTime() + getDt()); report(system); if (getTime() >= getFinish()) { setRunning(false); } } } void advance(Particle[] system){ for (int i = 0; i < system.length; i++){ system[i].newAcceleration( system, i ); } for (int i = 0; i < system.length; i++){ system[i].newVelocity( getDt() ); } for (int i = 0; i < system.length; i++){ system[i].newPosition( getDt() ); } } void report(Particle[] system){ System.out.println("Time: " + getTime()); for (int i = 1; i <= system.length; i++){ System.out.println("Particle " + i); System.out.println("Position: " + system[ i - 1 ].reportPosition()); System.out.println("Velocity: " + system[ i - 1 ].reportVelocity()); } System.out.println(); } public void setTime(double desiredTime){ time = desiredTime; } public double getTime(){ return time; } public String reportTime(){ return String.valueOf(((int)(10*getTime())) / 10 ); } public void setFinish(double desiredFinish){ finish = desiredFinish; } public double getFinish(){ return finish; } public void setDt(double desiredDt){ dt = desiredDt; } public double getDt(){ return dt; } public void setRunning(boolean desiredRunning){ running = desiredRunning; } public boolean isRunning(){ return running; } public static void main(String[] argv){ ForceLaw universe = new ForceLaw(); universe.init(); universe.setRunning(true); universe.run(); } } class Particle { double[] position, velocity, acceleration; double mass; int dimensions = 3; Random randomNumbers; public Particle(){ randomNumbers = new Random( (long)(System.currentTimeMillis()*Math.random()) ); position = new double[ dimensions ]; velocity = new double[ dimensions ]; acceleration = new double[ dimensions ]; //setMass(2.0 * (randomNumbers.nextDouble() - 0.5)); setMass(1.0); for (int i = 0; i < dimensions; i++){ setPosition(i, 2.0 * (randomNumbers.nextDouble() - 0.5)); //setVelocity(i, 2.0 * (randomNumbers.nextDouble() - 0.5)); setVelocity(i, 0.0); } } public void newVelocity(double dt){ setVelocity( addVectors(getVelocity(), getAcceleration(), dt) ); } public void newPosition(double dt){ setPosition( addVectors(getPosition(), getVelocity(), dt) ); } public double[] addVectors(double[] first, double[] second, double dt){ double[] summation = new double[ dimensions ]; for (int i = 0; i < Math.min(first.length, second.length); i++){ summation[i] = first[i] + second[i] * dt; } return summation; } public void setMass(double desiredMass){ mass = desiredMass; } public double getMass(){ return mass; }; public void setPosition( double[] desiredPosition){ for (int i = 0; i < desiredPosition.length; i++){ setPosition(i, desiredPosition[i]); } } public void setPosition(int i, double desiredPosition){ try{ position[i] = desiredPosition; } catch (ArrayIndexOutOfBoundsException aioobe) {}; } public double getPosition(int i){ try{ return position[i]; } catch(ArrayIndexOutOfBoundsException aioobe){ return 0.0; } } public double[] getPosition(){ return position; } public String reportPosition(){ String report = new String(); for (int i = 0; i < position.length; i++){ report += (double)( (int)(1000 * getPosition(i)) / 1000.0) + " "; } return report; } public void setVelocity( double[] desiredVelocity){ for (int i = 0; i < desiredVelocity.length; i++){ setVelocity(i, desiredVelocity[i]); } } public void setVelocity(int i, double desiredVelocity){ try{ velocity[i] = desiredVelocity; } catch (ArrayIndexOutOfBoundsException aioobe) {}; } public double getVelocity(int i){ try{ return velocity[i]; } catch(ArrayIndexOutOfBoundsException aioobe){ return 0.0; } } public double[] getVelocity(){ return velocity; } public String reportVelocity(){ String report = new String(); for (int i = 0; i < velocity.length; i++){ report += (double)( (int)(1000 * getVelocity(i)) / 1000.0) + " "; } return report; } public void setAcceleration( double[] desiredAcceleration){ for (int i = 0; i < desiredAcceleration.length; i++){ setAcceleration(i, desiredAcceleration[i]); } } public void setAcceleration(int i, double desiredAcceleration){ try{ acceleration[i] = desiredAcceleration; } catch (ArrayIndexOutOfBoundsException aioobe) {}; } public double getAcceleration(int i){ try{ return acceleration[i]; } catch (ArrayIndexOutOfBoundsException aioobe) { return 0.0; } } public double[] getAcceleration(){ return acceleration; } public void newAcceleration(Particle[] system, int thisOne){ double[] newAcceleration = new double[ dimensions ]; double[] r; double distance; for (int i = 0; i < system.length; i++){ if (i != thisOne){ r = addVectors(getPosition(), system[i].getPosition(), -1.0); distance = 0.0; for (int j = 0; j < r.length; j++){ distance += r[j]*r[j]; } distance = Math.sqrt(distance); for (int j = 0; j < r.length; j++){ newAcceleration[j] += - system[i].getMass() *r[j] / (Math.pow(distance, 3)); } } } setAcceleration( newAcceleration ); } }