1 ////////////////////////////////////////////////////////////////////////////
2 // Model: 2kmit.CPP
3 //
4
5 #include "simlib.h"
6
7
8 Constant g=9.81;
9
10 // popis syst�mu kola: y'' = ( F - D * y' - k * y ) / M
11
12
13 // struct Pruzina { double k,l0; input y1,y2 output force };
14
15 struct Mass : public aContiBlock1 {
16 Parameter mass;
17 Integrator speed, position;
MassMass18 Mass(Input force, double m, double pos0):
19 aContiBlock1( force ),
20 mass( m ),
21 speed( -g + force/mass ),
22 position( speed, pos0 ) {}
ValueMass23 double Value() { return position.Value(); }
24 };
25
26 struct Spring : aContiBlock2 {
27 double K, L0;
SpringSpring28 Spring(Input p1, Input p2, double k, double l0) :
29 aContiBlock2(p1,p2),
30 K(k),
31 L0(l0) {}
ValueSpring32 double Value() {
33 double l = Input1Value() - Input2Value();
34 if(l<=0) Error("Pruzina je stlacena na nulovou delku");
35 return K*(l-L0);
36 }
37 };
38
39
40 const double L10=1, L20=1;
41 const double K1=10, K2=15;
42 const double y00=5, y10=2, y20=0;
43
44 struct System2 {
45 Mass m1;
46 Mass m2;
47 Spring f1;
48 Spring f2;
49 //
System2System250 System2() :
51 m1(f1-f2, 2.0, y10),
52 m2(f2, 1.0, y20),
53 f1(y00, m1.position, K1, L10),
54 f2(m1.position, m2.position, K2, L20)
55 {}
56 };
57
58 // objekty modelu ...
59
60 System2 s;
61
62 // sledov�n� stavu modelu ...
Sample()63 void Sample() {
64 Print("%g %g %g\n", T.Value(),
65 s.m1.position.Value(), s.m2.position.Value());
66 }
67 Sampler S(Sample, 0.1);
68
main()69 int main() { // popis experimentu ...
70 SetOutput("test.dat");
71 _Print("# model 2 systemu \n");
72 Init(0,50); // inicializace parametr� experimentu
73 SetStep(1e-3,0.01); // rozsah kroku integrace
74 SetAccuracy(1e-5,0.001); // max. povolen� chyba integrace
75 Run(); // simulace
76 return 0;
77 }
78
79 // konec
80