Computational Methods Junior Honours : Checkpoint 4

 

Assessment: MarkingCP4.htm

 

 Starting point: 1d leap frog

·       Download and read the example codes Particle1d.java  Particle1dTester.java

·        Compile them by typing javac Particle1d.java Particle1dTester.java

·       Run them by typing java Particle1dTester

·       Graph the output by running xmgrace Particle1.dat

 

Your task: Particle3d class

·       We shall now reuse your previously debugged Vector3d to do more advanced codes

    1. Data members of type Vector3d called position, velocity
    2. Data members of type double called mass, charge
    3. getter and setter Methods (s)getPosition, (s)getVelocity etc...
    4. A constructor to initialise these variable through the setters
    5. A default constructor
    6. A toString method
    7. A method to return the particle's kinetic energy
    8. A method "void leapVelocity(double dt,Vector3d force)"
    9. A method "void leapPosition(double dt)"
    10. A method "readFromTokens(StreamTokenizer)" that fills out the members from an open input file
    11. Static methods to compute the relative separation vector and relative velocity vectors for a pair of particles A and B

Make readFromTokens set the Tokenizer to ignore '('  ')'  '{'  '}'  and ','  characters
Make readFromTokens set the Tokenizer to skip C++ comments
See StreamTokenizer.slashSlashComments() (http://java.sun.com/j2se/1.4.2/docs/api/java/io/StreamTokenizer.html)
In what follows you can now write your input file in a more readable format like

0.01 // dt
10000 // numsteps
// Particle 1 starts off as follows
( 0, 0, 0 ) // initial position
( -1, 1, 0 ) // inital velocity
1 // mass
1// charge

 

Write a data file for the trajectory (x-position vs y-position) a single particle in an electric field E=(-x,-4y,0),

This program should read the initial status from an input file. This should contain anything you are likely to

change later to avoid having to modify and recompile your code all the time. Think through what these should be.


Produce a plot for mass=1, charge=1 starting with velocity (-1,1,0) at position (0,0,0).


Note the field depends on the coordinate, and recall that the force on the particle of charge q is F = q E(x) .

 

Verify your code by calculating the final trajectory analytically for this situation. 

·       Harder

o   Increase the timestep to 0.1 - why does it no longer repeat in a stable fashion.

o   Design a numerical experiment to measure the error your computer program makes

§  Hint: your analytic solution tells you the exact period

o   Plot the error at fixed time t as a function of dt.

Add a resistive force -b v and replot for b=0.01.

 

               

       2. Magnetic field

Plot the x-y trajectories for a magnetic field (0,0,1) with the particles of charge 1,-1 having a initial velocities (1, 0, 0) and position (0, 0, 0).
Your program should open and write two files at the same time.
(You will need to use your Vector.cross cross product method now that the force is qv x B).
You will have to use an "old" velocity to compute the force
Why do you think the timestep errors seem bigger?