mirror of
https://github.com/nasa/trick.git
synced 2025-02-07 03:19:27 +00:00
Update the CannonBall example for the Tutorial. #1097
This commit is contained in:
parent
99f42bb6c4
commit
2be2aac0e3
@ -1,6 +1,5 @@
|
|||||||
#include <math.h>
|
#include <math.h>
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#include <iostream>
|
|
||||||
#include "SAIntegrator.hh"
|
#include "SAIntegrator.hh"
|
||||||
|
|
||||||
struct Cannon {
|
struct Cannon {
|
||||||
@ -9,15 +8,13 @@ struct Cannon {
|
|||||||
Cannon(double px, double py, double vx, double vy);
|
Cannon(double px, double py, double vx, double vy);
|
||||||
};
|
};
|
||||||
Cannon::Cannon(double px, double py, double vx, double vy) {
|
Cannon::Cannon(double px, double py, double vx, double vy) {
|
||||||
pos[0] = px;
|
pos[0] = px; pos[1] = py;
|
||||||
pos[1] = py;
|
vel[0] = vx; vel[1] = vy;
|
||||||
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");
|
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",
|
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]);
|
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[2] = 0.0;
|
||||||
derivs[3] = -9.81;
|
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[]) {
|
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]),
|
double* state_var_p[4] = { &(cannon.pos[0]), &(cannon.pos[1]),
|
||||||
&(cannon.vel[0]), &(cannon.vel[1]) };
|
&(cannon.vel[0]), &(cannon.vel[1]) };
|
||||||
double t = 0.0;
|
|
||||||
double dt = 0.01;
|
double dt = 0.01;
|
||||||
SA::RK2Integrator integ(dt, 4, state_var_p, state_var_p, calc_derivs, NULL);
|
SA::RK2Integrator integ(dt, 4, state_var_p, calc_derivs, NULL);
|
||||||
integ.add_Rootfinder(1.0e-10, Negative, &impact);
|
|
||||||
print_header();
|
double t = 0.0;
|
||||||
print_state( t, cannon);
|
CSV_header();
|
||||||
while (t < 20.0) {
|
CSV_state( t, cannon);
|
||||||
|
while (t < 5.1) {
|
||||||
integ.integrate();
|
integ.integrate();
|
||||||
t = integ.getIndyVar();
|
t = integ.getIndyVar();
|
||||||
print_state( t, cannon);
|
CSV_state( t, cannon);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,9 +1,7 @@
|
|||||||
# CannonBall
|
# CannonBall
|
||||||
|
|
||||||
The CannonBall program is an example of using the SA::RK2Integrator
|
The CannonBall program is an example of using the SA::RK2Integrator
|
||||||
to create a simple cannon ball simulation. We also use a rootfinder
|
to create a simple cannon ball simulation.
|
||||||
with our integrator to detect contact with the ground, and bounce
|
|
||||||
the cannonball.
|
|
||||||
|
|
||||||
For each numerical integration time-step, the simulation program prints:
|
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 |
Loading…
x
Reference in New Issue
Block a user