KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > JSci > physics > Drag


1 package JSci.physics;
2
3 import JSci.maths.vectors.AbstractDoubleVector;
4 import JSci.maths.vectors.DoubleVector;
5
6 /**
7 * The drag force is proportional to the square of a particle's speed.
8 * D(v) = -k |v| v
9 */

10 public class Drag {
11         private final double coefficient;
12
13         public Drag(double coeff) {
14                 coefficient = coeff;
15         }
16         public Force createForce(AbstractClassicalParticle p) {
17                 return new Force(p);
18         }
19         private class Force extends JSci.physics.Force {
20                 private final AbstractClassicalParticle p;
21                 public Force(AbstractClassicalParticle p) {
22                         this.p = p;
23                 }
24                 public AbstractDoubleVector getVector(double t) {
25                         AbstractDoubleVector pvel = p.getVelocity();
26                         final double vec[] = new double[pvel.dimension()];
27                         final double k = -coefficient*p.speed();
28                         for(int i=0; i<vec.length; i++)
29                                 vec[i] = k*pvel.getComponent(i);
30                         return new DoubleVector(vec);
31                 }
32         }
33 }
34
35
Popular Tags