SIMLIB/C++
3.09
Toggle main menu visibility
Loading...
Searching...
No Matches
examples/ball.cc
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
}
S
Sampler S(Sample, 1)
Sample
void Sample()
Definition
_test_.cc:38
main
int main()
Definition
_test_.cc:44
simlib3::aContiBlock::Value
virtual double Value()=0
simlib3::SetOutput
void SetOutput(const char *name)
Definition
print.cc:50
simlib3::SetStep
void SetStep(double _dtmin, double _dtmax)
Definition
intg.cc:92
simlib3::Run
void Run()
run simulation experiment
Definition
run.cc:228
simlib3::Print
int Print(const char *fmt,...)
for Output methods, can be redirected
Definition
print.cc:92
simlib3::Init
void Init(double t0, double t1=SIMLIB_MAXTIME)
Definition
simlib.h:186
simlib3::SetAccuracy
void SetAccuracy(double _abserr, double _relerr)
Definition
intg.cc:106
simlib3::SIMLIB_statistics
const SIMLIB_statistics_t & SIMLIB_statistics
interface to internal run-time statistics structure
Definition
run.cc:78
simlib.h
Main SIMLIB/C++ interface.
simlib3::SIMLIB_statistics_t::Output
void Output() const
print run-time statistics to output
Definition
output1.cc:63
Generated on
for SIMLIB/C++ by
1.18.0