SIMLIB/C++
3.09
Toggle main menu visibility
Loading...
Searching...
No Matches
examples/ball2.cc
Bouncing ball example
////////////////////////////////////////////////////////////////////////////
// Model ball2.cc SIMLIB/C++
//
// Bouncing ball (combined simulation, v2)
//
#include "
simlib.h
"
const
unsigned
MaxBang = 10;
// total number of take-offs
// model description:
Constant g(9.81);
// gravity acceleration
struct
Ball {
// ball model
unsigned
count;
// number of take-offs
class
LimitY:
public
ConditionDown {
// state-event detector
Ball *b;
void
Action() {
// state-event action
b->Bang();
}
public
:
LimitY(Ball * ball):
ConditionDown(ball->y),
// condition (y>=0) change TRUE-->FALSE
b(ball) { }
};
LimitY ylim;
// detector object
Integrator v, y;
// ball status
Ball(
double
initialposition):
count(0),
ylim(this),
// detector
v(-g - v * 0.1),
// physics
y(v, initialposition) { }
void
Bang() {
// state event code - take-off
Out();
// output od status before
Print
(
"\n# Bounce#%u\n"
, ++count);
v = -0.9 * v.Value();
// loss of energy
y = 0;
// this is important for accurate simulation
if
(count >= MaxBang)
Stop
();
// end of simulation run
else
Out();
// output od status after
}
void
Out() {
Print
(
"%f %9.4g %9.4f\n"
,
T
.
Value
(), y.Value(), v.Value());
}
};
Ball b1(10);
// ball object
void
Sample
() { b1.Out(); }
// periodic status sampling
Sampler
S
(
Sample
, 0.05);
int
main
() {
// experiment control
// DebugON();
SetOutput
(
"ball2.dat"
);
Print
(
"# Ball2 - bouncing ball model output\n"
);
Print
(
"# Time y v \n"
);
Init
(0);
// initial time 0
SetStep
(1e-10, 0.5);
// minstep defines accuracy of detection
SetAccuracy
(1e-5, 0.001);
// accuracy of numerical method
Run
();
// simulation
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::Stop
void Stop()
stop current simulation run
Definition
run.cc:147
simlib3::T
aContiBlock & T
simulation time block reference
Definition
continuous.cc:276
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