SIMLIB/C++ 3.09
Loading...
Searching...
No Matches
examples/ball.cc

Bouncing ball example.

Bouncing ball example

////////////////////////////////////////////////////////////////////////////
// Model ball SIMLIB/C++
//
// Bouncing ball (combined simulation model, variant 1)
// Simple: zero ball diameter, bounce approximated by state-event
//
#include "simlib.h"
const double g = 9.81; // gravity acceleration
class Ball : ConditionDown { // ball model description
Integrator v,y; // state variables
unsigned count; // bounce event count
void Action() { // state event description
Print("# Bounce#%u:\n", ++count);
Out(); // print state
v = -0.8 * v.Value(); // the energy loss
y = 0; // this is needed for detection when small energy!
if(count>=20) // after 20 bounces:
Stop(); // end simulation run
}
public:
Ball(double initialposition) :
ConditionDown(y), // bounce condition: (y>=0) from TRUE to FALSE
v(-g), // y' = INTG( - m * g )
y(v, initialposition), // y = INTG( y' )
count(0) {} // init bounce count
void Out() {
Print("%-9.3f % -9.3g % -9.3g\n",
T.Value(), y.Value(), v.Value());
}
};
Ball m1(1.0); // model of system
void Sample() { m1.Out(); } // output the ball state periodically
Sampler S(Sample,0.01);
int main() { // experiment description
SetOutput("ball.dat");
Print("# Ball --- model of bouncing ball\n");
Print("# Time y v \n");
Init(0); // initialize experiment
SetStep(1e-10,0.5); // bisection needs small minstep
SetAccuracy(1e-5,0.001); // set numerical error tolerance
Run(); // run simulation, print results
SIMLIB_statistics.Output(); // print run statistics
}
Sampler S(Sample, 1)
void Sample()
Definition: _test_.cc:38
int main()
Definition: _test_.cc:44
virtual double Value()=0
get block output value this method should be defined in classes derived from aContiBlock
void SetAccuracy(double _abserr, double _relerr)
set max.
Definition: intg.cc:106
void SetStep(double _dtmin, double _dtmax)
Set integration step interval.
Definition: intg.cc:92
void SetOutput(const char *name)
redirects Output(), Print() to file
Definition: print.cc:50
void Run()
run simulation experiment
Definition: run.cc:228
int Print(const char *fmt,...)
for Output methods, can be redirected
Definition: print.cc:92
void Init(double t0, double t1=SIMLIB_MAXTIME)
Initialize simulator and model time.
Definition: simlib.h:181
const SIMLIB_statistics_t & SIMLIB_statistics
interface to internal run-time statistics structure
Definition: run.cc:78
Main SIMLIB/C++ interface.
void Output() const
print run-time statistics to output
Definition: output1.cc:63