diff --git a/trick_source/trick_utils/SAIntegrator/examples/CannonBall/CannonBall.cpp b/trick_source/trick_utils/SAIntegrator/examples/CannonBall/CannonBall.cpp index a896b8a3..0210aad6 100644 --- a/trick_source/trick_utils/SAIntegrator/examples/CannonBall/CannonBall.cpp +++ b/trick_source/trick_utils/SAIntegrator/examples/CannonBall/CannonBall.cpp @@ -6,23 +6,26 @@ struct Cannon { double pos[2]; double vel[2]; - double acc[2]; + Cannon(double px, double py, double vx, double vy); }; -void init_state ( Cannon& cannon ) { - cannon.pos[0] = 0.0; - cannon.pos[1] = 0.0; - cannon.vel[0] = 50.0 * cos(M_PI/6.0); - cannon.vel[1] = 50.0 * sin(M_PI/6.0); - cannon.acc[0] = 0.0; - cannon.acc[1] = -9.81; +Cannon::Cannon(double px, double py, double vx, double vy) { + pos[0] = px; + pos[1] = py; + vel[0] = vx; + vel[1] = vy; +} +void print_header() { + printf ("t, cannon.pos[0], cannon.pos[1], cannon.vel[0], cannon.vel[1]\n"); +} +void print_state( double t, Cannon& cannon) { + printf ("%5.3f, %5.10f, %5.10f, %5.10f, %5.10f\n", + t, cannon.pos[0], cannon.pos[1], cannon.vel[0], cannon.vel[1]); } void calc_derivs( double t, double state[], double derivs[], void* udata) { - derivs[0] = state[2]; // vel_x - derivs[1] = state[3]; // vel_y - derivs[2] = state[4]; // acc_x - derivs[3] = state[5]; // acc_y - derivs[4] = 0.0; // Zero acc rate of change - derivs[5] = 0.0; + derivs[0] = state[2]; + derivs[1] = state[3]; + derivs[2] = 0.0; + derivs[3] = -9.81; } double impact( double t, double state[], RootFinder* root_finder, void* udata) { double root_error = root_finder->find_roots(t, state[1]); @@ -33,23 +36,14 @@ double impact( double t, double state[], RootFinder* root_finder, void* udata) { } return (root_error); } -void print_header() { - printf ("t, cannon.pos[0], cannon.pos[1], cannon.vel[0], cannon.vel[1], cannon.acc[0], cannon.acc[1]\n"); -} -void print_state( double t, Cannon& cannon) { - printf ("%5.3f, %5.10f, %5.10f, %5.10f, %5.10f, %5.10f, %5.10f\n", - t, cannon.pos[0], cannon.pos[1], cannon.vel[0], cannon.vel[1], cannon.acc[0], cannon.acc[1]); -} int main ( int argc, char* argv[]) { - Cannon cannon; - double* state_var_p[6] = { &(cannon.pos[0]), &(cannon.pos[1]), - &(cannon.vel[0]), &(cannon.vel[1]), - &(cannon.acc[0]), &(cannon.acc[1]) }; + Cannon cannon(0.0, 0.0, 50.0*cos(M_PI/6.0), 50.0*sin(M_PI/6.0)); + double* state_var_p[4] = { &(cannon.pos[0]), &(cannon.pos[1]), + &(cannon.vel[0]), &(cannon.vel[1]) }; + double t = 0.0; double dt = 0.01; - double t = 0.0; - SA::RK2Integrator integ(dt, 6, state_var_p, state_var_p, calc_derivs, NULL); - integ.add_Rootfinder(0.00000000001, Negative, &impact); - init_state(cannon); + SA::RK2Integrator integ(dt, 4, state_var_p, state_var_p, calc_derivs, NULL); + integ.add_Rootfinder(1.0e-10, Negative, &impact); print_header(); print_state( t, cannon); while (t < 20.0) { diff --git a/trick_source/trick_utils/SAIntegrator/examples/CannonBall/README.md b/trick_source/trick_utils/SAIntegrator/examples/CannonBall/README.md index 4f63c07e..8f0cc561 100644 --- a/trick_source/trick_utils/SAIntegrator/examples/CannonBall/README.md +++ b/trick_source/trick_utils/SAIntegrator/examples/CannonBall/README.md @@ -1,10 +1,33 @@ # CannonBall -This is an example of using the RK2Integrator to create a simple cannon ball simulation. We also use a rootfinder with our integrator to detect contact with the ground, and bounce the cannonball. +The CannonBall program is an example of using the SA::RK2Integrator +to create a simple cannon ball simulation. We also use a rootfinder +with our integrator to detect contact with the ground, and bounce +the cannonball. + +For each numerical integration time-step, the simulation program prints: + +1. time (s) +2. 2D position vector (m) +3. 2D velocity vector (m/s) + +to ```stdout```, in Comma Separated Values (CSV) format. + +### Building & Running the Simulation Program + +Generate the results as follows: ``` $ make $ ./CannonBall > cannon.csv +``` +### Plotting the Results +The Python script, ```plot_trajectory.py``` is provided to plot the results +in ```cannon.csv``` using (Python) matplotlib. + +Plot the cannon ball trajectory as follows: + +``` $ python plot_trajectory.py ```