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

mass+spring+damper

mass+spring+damper

////////////////////////////////////////////////////////////////////////////
// Model wheel.cc SIMLIB/C++
//
// Wheel on spring with damper model (Version 1)
//
// System description (governing equation):
// y'' = ( F - D * y' - k * y ) / M
// where:
// M is wheel mass
// y height of wheel center
// D damping coefficient
// k stiffness of spring
// F input force
//
#include "simlib.h"
struct Wheel { // wheel model
Integrator v, y;
Wheel(Input F, double M, double D, double k):
v( (F - D*v - k*y) / M ), // speed
y( v ) {} // height
};
// objects
Constant F(100); // constant input force
Wheel w(F, 10, 500, 5e4); // model
// print model state
void Sample() {
Print("%6.3f %.4g %.4g\n", T.Value(), w.y.Value(), w.v.Value());
}
Sampler S(Sample, 0.001); // periodic sampling
int main() { // experiment description
SetOutput("wheel.dat");
Print("# wheel --- model of wheel+spring+damper\n");
Print("# Time y v \n");
Init(0, 0.5); // set initial state and time
SetStep(1e-3, 0.1); // numerical method step size range
SetAccuracy(1e-5, 0.001); // allowed (abs.+rel.) error tolerance
Run(); // simulation run
SIMLIB_statistics.Output(); // print run statistics
}
Sampler S(Sample, 1)
Constant F(1.0)
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