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

Bouncing ball example.

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
}
//
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 Stop()
stop current simulation run
Definition: run.cc:147
aContiBlock & T
simulation time block reference
Definition: continuous.cc:276
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