Update the CannonBall example for the Tutorial. #1097

This commit is contained in:
Penn, John M 047828115 2021-01-27 12:22:16 -06:00
parent 99f42bb6c4
commit 2be2aac0e3
3 changed files with 18 additions and 27 deletions

View File

@ -1,6 +1,5 @@
#include <math.h>
#include <stdio.h>
#include <iostream>
#include "SAIntegrator.hh"
struct Cannon {
@ -9,15 +8,13 @@ struct Cannon {
Cannon(double px, double py, double vx, double vy);
};
Cannon::Cannon(double px, double py, double vx, double vy) {
pos[0] = px;
pos[1] = py;
vel[0] = vx;
vel[1] = vy;
pos[0] = px; pos[1] = py;
vel[0] = vx; vel[1] = vy;
}
void print_header() {
void CSV_header() {
printf ("t, cannon.pos[0], cannon.pos[1], cannon.vel[0], cannon.vel[1]\n");
}
void print_state( double t, Cannon& cannon) {
void CSV_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]);
}
@ -27,28 +24,24 @@ void calc_derivs( double t, double state[], double derivs[], void* udata) {
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]);
if (root_error == 0.0) {
root_finder->init();
state[2] = 0.9 * state[2];
state[3] = -0.9 * state[3];
}
return (root_error);
}
int main ( int argc, char* argv[]) {
Cannon cannon(0.0, 0.0, 50.0*cos(M_PI/6.0), 50.0*sin(M_PI/6.0));
const double muzzle_speed = 50; // m/s
const double muzzle_angle = 30; // degrees
double vx0 = muzzle_speed * cos(muzzle_angle * M_PI / 180.0);
double vy0 = muzzle_speed * sin(muzzle_angle * M_PI / 180.0);
Cannon cannon(0.0, 0.0, vx0, vy0);
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;
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) {
SA::RK2Integrator integ(dt, 4, state_var_p, calc_derivs, NULL);
double t = 0.0;
CSV_header();
CSV_state( t, cannon);
while (t < 5.1) {
integ.integrate();
t = integ.getIndyVar();
print_state( t, cannon);
CSV_state( t, cannon);
}
}

View File

@ -1,9 +1,7 @@
# 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.
to create a simple cannon ball simulation.
For each numerical integration time-step, the simulation program prints:

Binary file not shown.

Before

Width:  |  Height:  |  Size: 160 KiB

After

Width:  |  Height:  |  Size: 42 KiB