1 package JSci.physics; 2 3 import JSci.maths.vectors.AbstractDoubleVector; 4 import JSci.maths.vectors.DoubleVector; 5 6 public class Spring { 7 private final double coefficient; 8 private AbstractDoubleVector x; 9 10 14 public Spring(double coeff) { 15 coefficient = coeff; 16 } 17 public void setPosition(AbstractDoubleVector pos) { 18 x = pos; 19 } 20 public AbstractDoubleVector getPosition() { 21 return x; 22 } 23 26 public double energy(AbstractClassicalParticle p) { 27 double rr = 0.0; 28 AbstractDoubleVector ppos = p.getPosition(); 29 for(int i=0; i<x.dimension(); i++) { 30 final double dx = ppos.getComponent(i)-x.getComponent(i); 31 rr += dx*dx; 32 } 33 return coefficient*rr/2.0; 34 } 35 public Force createForce(AbstractClassicalParticle p) { 36 return new Force(p); 37 } 38 private class Force extends JSci.physics.Force { 39 private final AbstractClassicalParticle p; 40 public Force(AbstractClassicalParticle p) { 41 this.p = p; 42 } 43 public AbstractDoubleVector getVector(double t) { 44 AbstractDoubleVector ppos = p.getPosition(); 45 final double vec[] = new double[x.dimension()]; 46 for(int i=0; i<vec.length; i++) 47 vec[i] = -coefficient*(ppos.getComponent(i)-x.getComponent(i)); 48 return new DoubleVector(vec); 49 } 50 } 51 } 52 53 | Popular Tags |