Lesson7

Post Reply
admin
Site Admin
Posts: 3118
Joined: Wed Dec 11, 2019 8:31 am
Has thanked: 4 times

Lesson7

Post by admin »

Create the project FallingParticleCalcApp.
admin
Site Admin
Posts: 3118
Joined: Wed Dec 11, 2019 8:31 am
Has thanked: 4 times

Re: Lesson7

Post by admin »

  1. package fallingparticlecalcapp;
  2. import org.opensourcephysics.controls.*;
  3. //beginning of class definition
  4. public class FallingParticleCalcApp extends AbstractCalculation{
  5. public void calculate(){
  6. //gets initial conditions
  7. double y0 = control.getDouble("Initial y");
  8. double v0 = control.getDouble("Initial v");
  9. //sets initial conditions
  10. Particle ball = new FallingParticle(y0,v0);
  11. //reads parameters and sets dt
  12. ball.dt = control.getDouble("dt");
  13. while(ball.y>0){
  14. ball.step();
  15. }
  16. control.println("final time ="+ball.t);
  17. //displays numerical results
  18. control.println("y = "+ball.y+" v ="+ball.v);
  19. //displays analytic position
  20. control.println("analytic y="+ball.analyticPosition());
  21. //displays analytic velocity
  22. control.println("analytic v="+ball.analyticVelocity());
  23. }
  24. public void reset(){
  25. control.setValue("Initial y", 10);
  26. control.setValue("Initial v",0);
  27. control.setValue("dt",0.01);
  28. }
  29. //creates a calculation control structure using this class
  30. public static void main(String[] args){
  31. CalculationControl.createApp(new FallingParticleCalcApp());
  32. }
  33. }
admin
Site Admin
Posts: 3118
Joined: Wed Dec 11, 2019 8:31 am
Has thanked: 4 times

Re: Lesson7

Post by admin »

Code: Select all

package fallingparticlecalcapp;
import org.opensourcephysics.controls.*;
//beginning of class definition
public class FallingParticleCalcApp extends AbstractCalculation{
public void calculate(){
//gets initial conditions
double y0 = control.getDouble("Initial y");
double v0 = control.getDouble("Initial v");
//sets initial conditions
Particle ball = new FallingParticle(y0,v0);
//reads parameters and sets dt
ball.dt = control.getDouble("dt");
while(ball.y>0){
ball.step();
}
control.println("final time ="+ball.t);
//displays numerical results
control.println("y = "+ball.y+" v ="+ball.v);
//displays analytic position
control.println("analytic y="+ball.analyticPosition());
//displays analytic velocity
control.println("analytic v="+ball.analyticVelocity());
}
public void reset(){
control.setValue("Initial y", 10);
control.setValue("Initial v",0);
control.setValue("dt",0.01);
}
//creates a calculation control structure using this class
public static void main(String[] args){
CalculationControl.createApp(new FallingParticleCalcApp());
}
}
admin
Site Admin
Posts: 3118
Joined: Wed Dec 11, 2019 8:31 am
Has thanked: 4 times

Re: Lesson7

Post by admin »

Add the file FallingParticle (java application) to the project.
Right-click the package "fallingparticlecalcapp"->New->Java Class
Attachments
addClass.png
addClass.png (26.22 KiB) Viewed 644 times
admin
Site Admin
Posts: 3118
Joined: Wed Dec 11, 2019 8:31 am
Has thanked: 4 times

Re: Lesson7

Post by admin »

  1. package fallingparticlecalcapp;
  2. public class FallingParticle extends Particle {
  3. final static double g = 9.8; // constant
  4. private double y0 = 0, v0 = 0; // initial position and velocity
  5. public FallingParticle(double y, double v) { // constructor
  6. System.out.println("A new FallingParticle object is created.");
  7. this.y = y; // instance value set equal to passed value
  8. this.v = v; // instance value set equal to passed value
  9. y0 = y; // no need to use "this" because there is only one y0
  10. v0 = v;
  11. }
  12. public void step() {
  13. y = y+v*dt; // Euler algorithm
  14. v = v-g*dt;
  15. t = t+dt;
  16. }
  17. public double analyticPosition() {
  18. return y0+v0*t-(g*t*t)/2.0;
  19. }
  20. public double analyticVelocity() {
  21. return v0-g*t;
  22. }
  23. }
admin
Site Admin
Posts: 3118
Joined: Wed Dec 11, 2019 8:31 am
Has thanked: 4 times

Re: Lesson7

Post by admin »

Code: Select all

package fallingparticlecalcapp;
public class FallingParticle extends Particle {
  final static double g = 9.8;   // constant
  private double y0 = 0, v0 = 0; // initial position and velocity
  public FallingParticle(double y, double v) { // constructor
    System.out.println("A new FallingParticle object is created.");
    this.y = y; // instance value set equal to passed value
    this.v = v; // instance value set equal to passed value
    y0 = y;     // no need to use "this" because there is only one y0
    v0 = v;
  }
  public void step() {
    y = y+v*dt; // Euler algorithm
    v = v-g*dt;
    t = t+dt;
  }
  public double analyticPosition() {
    return y0+v0*t-(g*t*t)/2.0;
  }
  public double analyticVelocity() {
    return v0-g*t;
  }
}
admin
Site Admin
Posts: 3118
Joined: Wed Dec 11, 2019 8:31 am
Has thanked: 4 times

Re: Lesson7

Post by admin »

Add the file Particle (java application) to the project.
Add osp.jar to the project.
admin
Site Admin
Posts: 3118
Joined: Wed Dec 11, 2019 8:31 am
Has thanked: 4 times

Re: Lesson7

Post by admin »

  1. package fallingparticlecalcapp;
  2. abstract public class Particle {
  3. double y, v, t; // instance variables
  4. double dt; // time step
  5. public Particle() { // constructor
  6. System.out.println("A new Particle is created.");
  7. }
  8. abstract protected void step();
  9. abstract protected double analyticPosition();
  10. abstract protected double analyticVelocity();
  11. }
admin
Site Admin
Posts: 3118
Joined: Wed Dec 11, 2019 8:31 am
Has thanked: 4 times

Re: Lesson7

Post by admin »

Code: Select all

package fallingparticlecalcapp;
abstract public class Particle {
  double y, v, t; // instance variables
  double dt;      // time step
  public Particle() { // constructor
    System.out.println("A new Particle is created.");
  }
  abstract protected void step();
  abstract protected double analyticPosition();
  abstract protected double analyticVelocity();
}
admin
Site Admin
Posts: 3118
Joined: Wed Dec 11, 2019 8:31 am
Has thanked: 4 times

Re: Lesson7

Post by admin »

The screenshot of FallingParticleCalcApp program.
Attachments
FallingParticleCalcApp.png
FallingParticleCalcApp.png (12.88 KiB) Viewed 643 times
Post Reply