Ticket #3521: WhenPackage.mo

File WhenPackage.mo, 4.1 KB (added by Christian Kral <dr.christian.kral@…>, 9 years ago)

Example showing the issue

Line 
1package WhenPackage
2 extends Modelica.Icons.Package;
3
4 model WhenExample
5 extends Modelica.Icons.Example;
6 parameter String outputFile = "output.dat";
7 parameter Modelica.SIunits.Voltage V = 10 "Voltage";
8 parameter Modelica.SIunits.Resistance R = 1 "Resistance";
9 parameter Modelica.SIunits.Inductance L = 0.5 "Inductance";
10 parameter Modelica.SIunits.Time tStart = 1 "Start time";
11 final parameter Modelica.SIunits.Time T = L / R "Time constant";
12 Modelica.Electrical.Analog.Sources.ConstantVoltage constantvoltage1(V = V) annotation(Placement(visible = true, transformation(origin = {-80, 9.642860000000001}, extent = {{-10, -10}, {10, 10}}, rotation = -90)));
13 Modelica.Electrical.Analog.Ideal.IdealClosingSwitch idealclosingswitchOn annotation(Placement(visible = true, transformation(origin = {-50.3571, 20}, extent = {{-10, -10}, {10, 10}}, rotation = 0)));
14 Modelica.Electrical.Analog.Basic.Ground ground annotation(Placement(visible = true, transformation(origin = {-80, -30}, extent = {{-10, -10}, {10, 10}}, rotation = 0)));
15 Modelica.Electrical.Analog.Basic.Inductor inductor1(L = L) annotation(Placement(visible = true, transformation(origin = {40, 10}, extent = {{-10, -10}, {10, 10}}, rotation = -90)));
16 Modelica.Electrical.Analog.Basic.Resistor resistor1(R = R) annotation(Placement(visible = true, transformation(origin = {-10, 20}, extent = {{-10, -10}, {10, 10}}, rotation = 0)));
17 Modelica.Blocks.Sources.BooleanStep booleanstep1(startTime = tStart) annotation(Placement(visible = true, transformation(origin = {-69.28570000000001, 50.7143}, extent = {{-10, -10}, {10, 10}}, rotation = 0)));
18 initial equation
19 inductor1.i = 0;
20 equation
21 connect(booleanstep1.y, idealclosingswitchOn.control) annotation(Line(points = {{-58.2857, 50.7143}, {-49.2857, 50.3571}, {-49.2857, 27}, {-50.3571, 27}}));
22 connect(constantvoltage1.n, ground.p) annotation(Line(points = {{-80, -0.35714}, {-80, -19.2857}, {-80, -20}}));
23 connect(inductor1.n, constantvoltage1.n) annotation(Line(points = {{40, 1.22125e-015}, {40, 0}, {-80, 0}, {-80, -0.35714}}));
24 connect(resistor1.n, inductor1.p) annotation(Line(points = {{5.55112e-16, 20}, {39.6429, 20}, {40, 20}}));
25 connect(idealclosingswitchOn.n, resistor1.p) annotation(Line(points = {{-40.3571, 20}, {-20, 20}}));
26 connect(constantvoltage1.p, idealclosingswitchOn.p) annotation(Line(points = {{-80, 19.6429}, {-80, 20.3571}, {-60.3571, 20.3571}, {-60.3571, 20}}));
27 when time >= tStart then
28 writeRealParameters(outputFile, {"i0", "di0/dt"}, {0, inductor1.v / L}, append = false);
29 end when;
30 when time >= tStart + 1 * T then
31 writeRealParameters(outputFile, {"i1"}, {resistor1.i}, append = true);
32 end when;
33 when time >= tStart + 2 * T then
34 writeRealParameters(outputFile, {"i2"}, {resistor1.i}, append = true);
35 end when;
36 when time >= tStart + 5 * T then
37 writeRealParameters(outputFile, {"i5"}, {resistor1.i}, append = true);
38 end when;
39 when terminal() then
40 writeRealParameters(outputFile, {"vR", "vL"}, {V, 0}, append = true);
41 end when;
42 annotation(experiment(StopTime = 11), Icon(coordinateSystem(extent = {{-100, -100}, {100, 100}}, preserveAspectRatio = true, initialScale = 0.1, grid = {5, 5})), Diagram(coordinateSystem(extent = {{-100, -100}, {100, 100}}, preserveAspectRatio = true, initialScale = 0.1, grid = {2, 2})));
43 end WhenExample;
44
45 function writeRealParameters "Writing multiple real parameters to file"
46 input String fileName "Name of file";
47 input String name[:] "Name of parameter";
48 input Real data[:] "Actual value of parameter";
49 input Boolean append = false "Append data to file";
50 algorithm
51 // Check sizes of name and data
52 if size(name, 1) <> size(data, 1) then
53 assert(false, "writeReadParameters: Lengths of name and data have to be equal");
54 end if;
55 // Write data to file
56 if not append then
57 Modelica.Utilities.Files.removeFile(fileName);
58 end if;
59 for k in 1:size(name, 1) loop
60 Modelica.Utilities.Streams.print(name[k] + " = " + String(data[k]), fileName);
61 end for;
62 end writeRealParameters;
63 annotation(uses(Modelica(version = "3.2.1")));
64end WhenPackage;