Ticket #4047: Test.mo

File Test.mo, 4.3 KB (added by casella, 8 years ago)

Test model

Line 
1    model Test
2      Modelica.Blocks.Interfaces.RealInput SP annotation(Placement(visible = true, transformation(extent = {{-100, -20}, {-60, 20}}, rotation = 0), iconTransformation(extent = {{-100, -20}, {-60, 20}}, rotation = 0)));
3      Modelica.Blocks.Interfaces.RealInput PVvec[size(num, 1)] "PV(k)...PV(k-#num) at step ns*qs" annotation(Placement(visible = true, transformation(extent = {{-90, -84}, {-50, -44}}, rotation = 0), iconTransformation(extent = {{-100, -20}, {-60, 20}}, rotation = 0)));
4      Modelica.Blocks.Interfaces.RealOutput CS annotation(Placement(transformation(extent = {{60, -20}, {80, 0}}), iconTransformation(extent = {{60, -20}, {100, 20}})));
5      Modelica.Blocks.Interfaces.BooleanInput Etrig annotation(Placement(visible = true, transformation(origin = {-76, 58}, extent = {{-20, -20}, {20, 20}}, rotation = 0), iconTransformation(origin = {-80, 60}, extent = {{-20, -20}, {20, 20}}, rotation = 0)));
6      parameter Real num[:] = {0.5} "num in desc. powers of z, len must match npast+1 in sapmpler";
7      parameter Real den[:] = {1, -0.5} "nen in descending powers of z";
8      parameter Real qs = 0.1 "must match qs in sampler";
9      parameter Integer ns = 10 "must match qs in sampler";
10      parameter Real CSmax = 1;
11      parameter Real CSmin = -1;
12      parameter Real CSstart = 0;
13      //protected
14      parameter Real Ts = ns * qs;
15      parameter Real nnum[:] = num / den[1];
16      // normalise num and den by making the latter monic
17      parameter Real nden[:] = den / den[1];
18      parameter Real rden[:] = Functions.ArraySubset(den, 2, size(den, 1));
19      parameter Integer nPV = size(num, 1);
20      // No. of PVs to compute CS(k), from k to k-#num
21      parameter Integer nSP = nPV;
22      // need same number of SPs to compute the errors
23      parameter Integer nCS = size(den, 1) - 1;
24      // No. of CSs to compute CS(k), from k-1 to k-#den
25      discrete Real vCS[nCS](each start = CSstart);
26      // vector for the past CSs to compute CS(k)
27      discrete Real vSP[nSP](each start = 0);
28      // vector for the past SPs to compute CS(k)
29      // recorded CSs at events and their times: nCS*qs*ns/qs=nCS*ns is the max No. of events that
30      // may have occurred in the time span fron now back to nCS*Ts
31      discrete Real recCS[nCS * ns](each start = CSstart);
32      // recorded CSs
33      discrete Real trecCS[nCS * ns](each start = 0);
34      // corresponding times
35      // recorded SPs at events: same reasoning nut with nSP
36      discrete Real recSP[nSP * ns](each start = 0);
37      // recorded SPs
38      discrete Real trecSP[nSP * ns](each start = 0);
39      // times (redundant for readability)
40      discrete Real tCS, tSP;
41      discrete Integer dex;
42      discrete Real u;
43    equation
44      CS = u;
45    algorithm
46      when change(Etrig) then
47        for i in 1:nCS loop
48          tCS := time - i * Ts;
49          dex := 1;
50          while dex < nCS * ns and tCS < trecCS[dex] loop
51            dex := dex + 1;
52          end while;
53          vCS[i] := recCS[dex];
54        end for;
55        for i in 0:nCS * ns - 2 loop
56          recCS[nCS * ns - i] := recCS[nCS * ns - i - 1];
57          trecCS[nCS * ns - i] := trecCS[nCS * ns - i - 1];
58        end for;
59        trecCS[1] := time;
60        for i in 0:nSP * ns - 2 loop
61          recSP[nSP * ns - i] := recSP[nSP * ns - i - 1];
62          trecSP[nSP * ns - i] := trecSP[nSP * ns - i - 1];
63        end for;
64        trecSP[1] := time;
65        recSP[1] := SP;
66        for i in 1:nSP loop
67          tSP := time - (i - 1) * Ts;
68          dex := 1;
69          while dex < nSP * ns and tSP < trecSP[dex] loop
70            dex := dex + 1;
71          end while;
72          vSP[i] := recSP[dex];
73        end for;
74        u := rden * vCS + nnum * (vSP - PVvec);
75        u := max(CSmin, min(CSmax, u));
76        recCS[1] := u;
77      end when;
78      annotation(Diagram(coordinateSystem(preserveAspectRatio = false, extent = {{-100, -100}, {100, 100}}), graphics), Icon(coordinateSystem(preserveAspectRatio = false, initialScale = 0.1), graphics = {Rectangle(origin = {18, 124}, fillColor = {200, 180, 180}, fillPattern = FillPattern.Solid, extent = {{6, -24}, {82, -94}}), Text(origin = {0, -14}, extent = {{-40, 52}, {40, -52}}, textString = "R(z)
793TS
80oldUin", fontName = "DejaVu Sans Mono")}), uses(Modelica(version = "3.2.1")));
81    end Test;