Ticket #2902: Example.3.mo

File Example.3.mo, 5.2 KB (added by anonymous, 10 years ago)

Run this example. Thanks

Line 
1within ;
2package Example
3 function conj "Conjugate of complex number"
4 input Complex c1 "Complex number";
5 output Complex c2 "= c1.re - j*c1.im";
6 algorithm
7 c2 := Complex(c1.re, -c1.im);
8 annotation(Inline = true, Documentation(info = "<html>
9 <p>This function returns the Complex conjugate of the Complex input.</p>
10 </html>"));
11 end conj;
12
13 function real "Real part of complex number"
14 input Complex c "Complex number";
15 output Real r "= c.re ";
16 algorithm
17 r := c.re;
18 annotation(Inline = true, Documentation(info = "<html>
19 <p>This function returns the real part of the Complex input.</p>
20 </html>"));
21 end real;
22
23 function imag "Imaginary part of complex number"
24 input Complex c "Complex number";
25 output Real r "= c.im ";
26 algorithm
27 r := c.im;
28 annotation(Inline = true, Documentation(info = "<html>
29 <p>This function returns the imaginary part of the Complex input.</p>
30 </html>"));
31 end imag;
32
33 function arg "Phase angle of complex number"
34 input Complex c "Complex number";
35 input Modelica.SIunits.Angle phi0 = 0
36 "Phase angle phi shall be in the range: -pi < phi-phi0 < pi";
37 output Modelica.SIunits.Angle phi "= phase angle of c";
38 algorithm
39 phi := Modelica.Math.atan3(
40 c.im,
41 c.re,
42 phi0);
43 annotation(Inline = true, Documentation(info = "<html>
44 <p>This function returns the Real argument of the Complex input, i.e., it's angle.</p>
45 </html>"));
46 end arg;
47
48 function 'abs' "Absolute value of complex number"
49 input Complex c "Complex number";
50 output Real result "= abs(c)";
51 algorithm
52 result := (c.re ^ 2 + c.im ^ 2) ^ 0.5;
53 //changed from sqrt
54 annotation(Inline = true, Documentation(info = "<html>
55 <p>This function returns the Real absolute of the Complex input, i.e., it's length.</p>
56 </html>"));
57 end 'abs';
58
59 function fromPolar "Complex from polar representation"
60 input Real len "abs of complex";
61 input Modelica.SIunits.Angle phi "arg of complex";
62 output Complex c "= len*cos(phi) + j*len*sin(phi)";
63 algorithm
64 c := Complex(len*Modelica.Math.cos(phi), len*Modelica.Math.sin(phi));
65 annotation(Inline = true, Documentation(info = "<html>
66 <p>This function constructs a Complex number from it's length (absolute) and angle (argument).</p>
67 </html>"));
68 end fromPolar;
69
70
71
72 model Test_complex
73
74 Complex_bindinglost complex_bindinglost1 annotation (Placement(
75 visible=true, transformation(
76 origin={-40,0},
77 extent={{-10,-10},{10,10}},
78 rotation=0)));
79 Complex_bindingOK complex_bindingok1 annotation (Placement(
80 visible=true, transformation(
81 origin={40,0},
82 extent={{-10,-10},{10,10}},
83 rotation=0)));
84 annotation(Icon(coordinateSystem(extent = {{-100, -100}, {100, 100}}, preserveAspectRatio = true, initialScale = 0.1, grid = {2, 2})), Diagram(coordinateSystem(extent = {{-100, -100}, {100, 100}}, preserveAspectRatio = true, initialScale = 0.1, grid = {2, 2})));
85 end Test_complex;
86
87
88
89 model Complex_bindinglost
90 constant Real pi = Modelica.Constants.pi;
91 parameter Real eterm = 0.999999 "terminal voltage";
92 //1.0
93 parameter Real anglev0 = 4.0463 "Power flow, node angle in degree";
94 parameter Real pelec = 0.399989 * 100 "active power MVA";
95 //80.0
96 parameter Real qelec = 5.41649 "reactive power MVA";
97 //50.0
98 parameter Real wbase = 2 * pi * 50 "system base speed";
99 parameter Real mbase = 100 "system base power rating MVA";
100 parameter Real Ra = 0 "amature resistance";
101 parameter Real Xpp = 1;
102 parameter Real anglev_rad = anglev0 * pi / 180
103 "initial value of bus anglev in rad";
104 parameter Real p0 = pelec / mbase
105 "initial value of bus active power in p.u.";
106 parameter Real q0 = qelec / mbase
107 "initial value of bus reactive power in p.u.";
108 parameter Complex Zs(re = Ra, im = Xpp) "Equivation impedance";
109 parameter Complex VT(re = eterm * cos(anglev_rad), im = eterm * sin(anglev_rad));
110 parameter Complex S(re = p0, im = q0);
111 parameter Complex It = conj(S / VT);
112 //Initialize current and voltage components of rotor reference fram (dq axes).
113 end Complex_bindinglost;
114
115 model Complex_bindingOK
116 constant Real pi = Modelica.Constants.pi;
117 parameter Complex Zs(re = 1, im = 1) "Equivation impedance";
118 parameter Complex VT(re = cos(pi), im = sin(pi));
119 parameter Complex S(re = 1, im = 1);
120 parameter Complex It = conj(S / VT);
121 annotation(Icon(coordinateSystem(extent = {{-100, -100}, {100, 100}}, preserveAspectRatio = true, initialScale = 0.1, grid = {2, 2})), Diagram(coordinateSystem(extent = {{-100, -100}, {100, 100}}, preserveAspectRatio = true, initialScale = 0.1, grid = {2, 2})));
122 end Complex_bindingOK;
123 annotation (uses(Complex(version="3.2.1"), Modelica(version="2.2.2")));
124end Example;