Bouncing ball example.
const unsigned MaxBang = 10;
Constant g(9.81);
struct Ball {
unsigned count;
class LimitY: public ConditionDown {
Ball *b;
void Action() {
b->Bang();
}
public:
LimitY(Ball * ball):
ConditionDown(ball->y),
b(ball) { }
};
LimitY ylim;
Integrator v, y;
Ball(double initialposition):
count(0),
ylim(this),
v(-g - v * 0.1),
y(v, initialposition) { }
void Bang() {
Out();
Print(
"\n# Bounce#%u\n", ++count);
v = -0.9 * v.Value();
y = 0;
if (count >= MaxBang)
else
Out();
}
void Out() {
Print(
"%f %9.4g %9.4f\n",
T.
Value(), y.Value(), v.Value());
}
};
Ball b1(10);
Print(
"# Ball2 - bouncing ball model output\n");
}
virtual double Value()=0
get block output value this method should be defined in classes derived from aContiBlock
void Stop()
stop current simulation run
aContiBlock & T
simulation time block reference
void SetAccuracy(double _abserr, double _relerr)
set max.
void SetStep(double _dtmin, double _dtmax)
Set integration step interval.
void SetOutput(const char *name)
redirects Output(), Print() to file
void Run()
run simulation experiment
int Print(const char *fmt,...)
for Output methods, can be redirected
void Init(double t0, double t1=SIMLIB_MAXTIME)
Initialize simulator and model time.
const SIMLIB_statistics_t & SIMLIB_statistics
interface to internal run-time statistics structure
Main SIMLIB/C++ interface.
void Output() const
print run-time statistics to output