KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > JSci > physics > ClassicalParticle


1 package JSci.physics;
2
3 import JSci.maths.vectors.AbstractDoubleVector;
4 import JSci.maths.vectors.DoubleVector;
5
6 /**
7 * The ClassicalParticle class provides an object for encapsulating classical point particles.
8 * This class is suitable for representing particles that live in an arbitrary number of dimensions.
9 * @version 1.0
10 * @author Mark Hale
11 */

12 public class ClassicalParticle extends AbstractClassicalParticle {
13         protected double mass;
14         protected AbstractDoubleVector x;
15         protected AbstractDoubleVector v;
16         /**
17         * Constructs a classical particle.
18         * @param n number of dimensions.
19         */

20         public ClassicalParticle(int n) {
21                 x = new DoubleVector(n);
22                 v = new DoubleVector(n);
23         }
24         /**
25         * Sets the mass of this particle.
26         */

27         public void setMass(double m) {
28                 mass = m;
29         }
30         /**
31         * Returns the mass of this particle.
32         */

33         public double getMass() {
34                 return mass;
35         }
36         public void setPosition(AbstractDoubleVector pos) {
37                 x = pos;
38         }
39         public AbstractDoubleVector getPosition() {
40                 return x;
41         }
42         public void setVelocity(AbstractDoubleVector vel) {
43                 v = vel;
44         }
45         public AbstractDoubleVector getVelocity() {
46                 return v;
47         }
48         private double speedSqr() {
49                 return v.scalarProduct(v);
50         }
51         public double speed() {
52                 return v.norm();
53         }
54         public void setMomentum(AbstractDoubleVector momentum) {
55                 v = momentum.scalarDivide(mass);
56         }
57         public AbstractDoubleVector getMomentum() {
58                 return v.scalarMultiply(mass);
59         }
60         /**
61         * Returns the energy of this particle.
62         */

63         public double energy() {
64                 return mass*speedSqr()/2.0;
65         }
66         /**
67         * Evolves this particle forward according to its kinematics.
68         * This method changes the particle's position.
69         * @return this.
70         */

71         public ClassicalParticle move(double dt) {
72                 x = x.add(v.scalarMultiply(dt));
73                 return this;
74         }
75         /**
76         * Accelerates this particle.
77         * This method changes the particle's velocity.
78         * It is additive, that is <code>accelerate(a1, dt).accelerate(a2, dt)</code>
79         * is equivalent to <code>accelerate(a1+a2, dt)</code>.
80         * @return this.
81         */

82         public ClassicalParticle accelerate(AbstractDoubleVector a, double dt) {
83                 v = v.add(a.scalarMultiply(dt));
84                 return this;
85         }
86         /**
87         * Applies a force to this particle.
88         * This method changes the particle's velocity.
89         * It is additive, that is <code>applyForce(F1, dt).applyForce(F2, dt)</code>
90         * is equivalent to <code>applyForce(F1+F2, dt)</code>.
91         * @return this.
92         */

93         public ClassicalParticle applyForce(AbstractDoubleVector F,double dt) {
94                 return accelerate(F.scalarDivide(mass), dt);
95         }
96 }
97
98
Popular Tags