1 ////////////////////////////////////////////////////////////////////////////
2 // Model rlc.cc -- SIMLIB/C++ continuous model example
3 //
4 // 2007
5 // RLC circuit with initial voltage on capacitor
6 //
7 // Manual conversion of circuit to differential equation
8 //
9
10 #include "simlib.h"
11 #include <cmath>
12
13 const double PI = 3.141592653589793;
14
15 struct SystemRLC { // LC circuit:
16 //
17 // +-----o-----o-----o
18 // | | |
19 // | | ) |
20 // | | R ) L === C Uout
21 // | | ) |
22 // | | |
23 // +-----o-----o-----o
24 //
25 // Uout - output voltage = Uc = Ul = Ur
26 //
27 // Parameters:
28 Parameter R; // resistance
29 Parameter L; // inductance
30 Parameter C; // capacity
31 Expression Current1;
32 Integrator Current2;
33 Integrator Uout; // output voltage
SystemRLCSystemRLC34 SystemRLC(double U0, double R0, double L0, double C0):
35 R(R0), L(L0), C(C0),
36 Current1( -Uout/R ),
37 Current2( Uout/L, 0 ),
38 Uout( (Current1-Current2)/C, U0 ) {}
ResFrequencySystemRLC39 double ResFrequency() {
40 return 1/(2*PI * std::sqrt(L.Value() * C.Value()));
41 }
ResPeriodSystemRLC42 double ResPeriod() {
43 return 1/ResFrequency();
44 }
45 };
46
47
48 ////////////////////////////////////////////////////////////////////////////
49 SystemRLC rlc(1 /*Volt*/, 1e3 /*Ohm*/, 1e-6 /*Henry*/, 1e-9 /*Farad*/);
50 ////////////////////////////////////////////////////////////////////////////
51
Sample1()52 void Sample1() { // output from model (form suitable for GNUplot)
53 Print("%g %g %g\n", T.Value(), rlc.Uout.Value(), rlc.Current2.Value());
54 }
55 Sampler s(Sample1, 0.01*rlc.ResPeriod());
56
main()57 int main() { // experiment control ...
58 SetOutput("rlc.dat");
59 Print("# RLC circuit simulation output\n");
60 Print("# Fres = %g\n", rlc.ResFrequency());
61 Init(0, 30 * rlc.ResPeriod()); // initialization
62 SetStep(1e-9, 0.1 * rlc.ResPeriod());
63 Run(); // simulation run
64 }
65
66 //
67