KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > JSci > physics > ClassicalParticle3D


1 package JSci.physics;
2
3 import JSci.maths.vectors.AbstractDoubleVector;
4 import JSci.maths.vectors.Double3Vector;
5
6 /**
7 * The ClassicalParticle3D class provides an object for
8 * encapsulating classical point particles that live in 3D.
9 * @version 1.0
10 * @author Silvere Martin-Michiellot
11 * @author Mark Hale
12 */

13 public class ClassicalParticle3D extends AbstractClassicalParticle {
14         /**
15         * Mass.
16         */

17         protected double mass;
18         /**
19         * Position coordinates.
20         */

21         protected double x, y, z;
22         /**
23         * Velocity coordinates.
24         */

25         protected double vx, vy, vz;
26         /**
27         * Constructs a classical particle.
28         */

29         public ClassicalParticle3D() {}
30         public void setMass(double m) {
31                 mass=m;
32         }
33         public double getMass() {
34                 return mass;
35         }
36         public void setPosition(double xPos, double yPos, double zPos) {
37                 x=xPos;
38                 y=yPos;
39                 z=zPos;
40         }
41         public AbstractDoubleVector getPosition() {
42                 return new Double3Vector(x, y, z);
43         }
44         public void setXPosition(double xPos) {
45                 x=xPos;
46         }
47         public double getXPosition() {
48                 return x;
49         }
50         public void setYPosition(double yPos) {
51                 y=yPos;
52         }
53         public double getYPosition() {
54                 return y;
55         }
56         public void setZPosition(double zPos) {
57                 z=zPos;
58         }
59         public double getZPosition() {
60                 return z;
61         }
62         public void setVelocity(double xVel,double yVel,double zVel) {
63                 vx=xVel;
64                 vy=yVel;
65                 vz=zVel;
66         }
67         public AbstractDoubleVector getVelocity() {
68                 return new Double3Vector(vx, vy, vz);
69         }
70         public double getXVelocity() {
71                 return vx;
72         }
73         public double getYVelocity() {
74                 return vy;
75         }
76         public double getZVelocity() {
77                 return vz;
78         }
79         public double speed() {
80                 return Math.sqrt(vx*vx+vy*vy+vz*vz);
81         }
82         public void setMomentum(double xMom,double yMom,double zMom) {
83                 vx=xMom/mass;
84                 vy=yMom/mass;
85                 vz=zMom/mass;
86         }
87         public AbstractDoubleVector getMomentum() {
88                 return new Double3Vector(mass*vx, mass*vy, mass*vz);
89         }
90         public double getXMomentum() {
91                 return mass*vx;
92         }
93         public double getYMomentum() {
94                 return mass*vy;
95         }
96         public double getZMomentum() {
97                 return mass*vz;
98         }
99         /**
100         * Returns the kinetic energy.
101         */

102         public double energy() {
103                 return mass*(vx*vx+vy*vy+vz*vz)/2.0;
104         }
105         /**
106         * Evolves this particle forward according to its kinematics.
107         * This method changes the particle's position.
108         * @return this.
109         */

110         public ClassicalParticle3D move(double dt) {
111                 return translate(dt);
112         }
113         /**
114         * Evolves this particle forward according to its linear kinematics.
115         * This method changes the particle's position.
116         * @return this.
117         */

118         public ClassicalParticle3D translate(double dt) {
119                 x+=vx*dt;
120                 y+=vy*dt;
121                 z+=vz*dt;
122                 return this;
123         }
124         /**
125         * Accelerates this particle.
126         * This method changes the particle's velocity.
127         * It is additive, that is <code>accelerate(a1, dt).accelerate(a2, dt)</code>
128         * is equivalent to <code>accelerate(a1+a2, dt)</code>.
129         * @return this.
130         */

131         public ClassicalParticle3D accelerate(double ax,double ay,double az,double dt) {
132                 vx+=ax*dt;
133                 vy+=ay*dt;
134                 vz+=az*dt;
135                 return this;
136         }
137         /**
138         * Applies a force to this particle.
139         * This method changes the particle's velocity.
140         * It is additive, that is <code>applyForce(F1, dt).applyForce(F2, dt)</code>
141         * is equivalent to <code>applyForce(F1+F2, dt)</code>.
142         * @return this.
143         */

144         public ClassicalParticle3D applyForce(double Fx,double Fy,double Fz,double dt) {
145                 return accelerate(Fx/mass, Fy/mass, Fz/mass, dt);
146         }
147         /**
148         * Evolves two particles under their mutual gravitational attraction.
149         * This method changes the velocity of both particles.
150         * @return this.
151         */

152         public ClassicalParticle3D gravitate(ClassicalParticle3D p,double dt) {
153                 final double dx=p.x-x;
154                 final double dy=p.y-y;
155                 final double dz=p.z-z;
156                 final double rr=dx*dx+dy*dy+dz*dz;
157                 final double r=Math.sqrt(rr);
158                 final double g=p.mass/rr;
159                 final double pg=mass/rr;
160                 vx-=g*dx*dt/r;
161                 vy-=g*dy*dt/r;
162                 vz-=g*dz*dt/r;
163                 p.vx+=pg*dx*dt/r;
164                 p.vy+=pg*dy*dt/r;
165                 p.vz+=pg*dz*dt/r;
166                 return this;
167         }
168 }
169
170
Popular Tags