Spiff up SAInteger Cannonball sim example, like the other examples.

This commit is contained in:
Penn, John M 047828115 2021-01-06 17:17:49 -06:00
parent 0c4907f78c
commit 69b8d2f0ad
2 changed files with 47 additions and 30 deletions

View File

@ -6,23 +6,26 @@
struct Cannon { struct Cannon {
double pos[2]; double pos[2];
double vel[2]; double vel[2];
double acc[2]; Cannon(double px, double py, double vx, double vy);
}; };
void init_state ( Cannon& cannon ) { Cannon::Cannon(double px, double py, double vx, double vy) {
cannon.pos[0] = 0.0; pos[0] = px;
cannon.pos[1] = 0.0; pos[1] = py;
cannon.vel[0] = 50.0 * cos(M_PI/6.0); vel[0] = vx;
cannon.vel[1] = 50.0 * sin(M_PI/6.0); vel[1] = vy;
cannon.acc[0] = 0.0; }
cannon.acc[1] = -9.81; 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) { void calc_derivs( double t, double state[], double derivs[], void* udata) {
derivs[0] = state[2]; // vel_x derivs[0] = state[2];
derivs[1] = state[3]; // vel_y derivs[1] = state[3];
derivs[2] = state[4]; // acc_x derivs[2] = 0.0;
derivs[3] = state[5]; // acc_y derivs[3] = -9.81;
derivs[4] = 0.0; // Zero acc rate of change
derivs[5] = 0.0;
} }
double impact( double t, double state[], RootFinder* root_finder, void* udata) { double impact( double t, double state[], RootFinder* root_finder, void* udata) {
double root_error = root_finder->find_roots(t, state[1]); 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); 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[]) { int main ( int argc, char* argv[]) {
Cannon cannon; Cannon cannon(0.0, 0.0, 50.0*cos(M_PI/6.0), 50.0*sin(M_PI/6.0));
double* state_var_p[6] = { &(cannon.pos[0]), &(cannon.pos[1]), double* state_var_p[4] = { &(cannon.pos[0]), &(cannon.pos[1]),
&(cannon.vel[0]), &(cannon.vel[1]), &(cannon.vel[0]), &(cannon.vel[1]) };
&(cannon.acc[0]), &(cannon.acc[1]) }; double t = 0.0;
double dt = 0.01; double dt = 0.01;
double t = 0.0; SA::RK2Integrator integ(dt, 4, state_var_p, state_var_p, calc_derivs, NULL);
SA::RK2Integrator integ(dt, 6, state_var_p, state_var_p, calc_derivs, NULL); integ.add_Rootfinder(1.0e-10, Negative, &impact);
integ.add_Rootfinder(0.00000000001, Negative, &impact);
init_state(cannon);
print_header(); print_header();
print_state( t, cannon); print_state( t, cannon);
while (t < 20.0) { while (t < 20.0) {

View File

@ -1,10 +1,33 @@
# CannonBall # 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 $ make
$ ./CannonBall > cannon.csv $ ./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 $ python plot_trajectory.py
``` ```