Ticket #5281: TestSpatialDistribution.mo

File TestSpatialDistribution.mo, 4.7 KB (added by Francesco Casella, 5 years ago)
Line 
1within ;
2package TestSpatialDistribution
3 model Test1
4 import Modelica.Constants.pi;
5 parameter Real f0 = 9;
6 parameter Real f1 = 7;
7 Real w01(start = 1, fixed = true);
8 Real w02(start = 0, fixed = true);
9 Real w11(start = 1, fixed = true);
10 Real w12(start = 0, fixed = true);
11 Real in0, in1, out0, out1;
12 Real x(start = 0, fixed = true);
13 Real v;
14 Real w01_exact = cos(2*pi*f0*time);
15 Real w11_exact = cos(2*pi*f1*time);
16 Real out1_exact = if time < 0.35 then 0
17 elseif time < 0.5 then cos(2*pi*f0*(time - 0.25))
18 else in1;
19 Real out0_exact = if time < 0.5 then in0 else
20 if time < 0.75 then cos(2*pi*f0*(0.5 - (time - 0.5))) else
21 cos(2*pi*f1*(time - 0.25));
22 Real err0(start = 0, fixed = true);
23 Real err1(start = 0, fixed = true);
24 equation
25 der(w01) = w02;
26 der(w02) = -(2*pi*f0)^2*w01;
27 der(w11) = w12;
28 der(w12) = -(2*pi*f1)^2*w11;
29 der(x) = v;
30 v = if time < 0.5 then 4 else -4;
31 in0 = if time < 0.1 then 0 else w01;
32 in1 = if time < 0.1 then 0 else w11;
33 (out0, out1) = spatialDistribution(in0, in1, x, v > 0);
34 der(err0) = (out0_exact - out0)^2;
35 der(err1) = (out1_exact - out1)^2;
36 assert(err0 < 1e-3, "Numerical solution too far from exact one");
37 assert(err1 < 1e-3, "Numerical solution too far from exact one");
38
39 annotation(experiment(stopTime = 2, Interval = 1e-3, Tolerance = 1e-7));
40 end Test1;
41
42 model Test2
43 import Modelica.Constants.pi;
44 parameter Real f0 = 9;
45 parameter Real f1 = 7;
46 parameter Real z0 = 1;
47 Real w01(start = 1, fixed = true);
48 Real w02(start = 0, fixed = true);
49 Real w11(start = 1, fixed = true);
50 Real w12(start = 0, fixed = true);
51 Real in0, in1, out0, out1;
52 Real x(start = 0, fixed = true);
53 Real v;
54 Real w01_exact = cos(2*pi*f0*time);
55 Real w11_exact = cos(2*pi*f1*time);
56 Real out1_exact = if time < 0.35 then z0 else
57 if time < 0.5 then z0 + cos(2*pi*f0*(time - 0.25))
58 else in1;
59 Real out0_exact = if time < 0.5 then in0 else
60 if time < 0.75 then z0 + cos(2*pi*f0*(0.5 - (time - 0.5))) else
61 z0 + cos(2*pi*f1*(time - 0.25));
62 Real err0(start = 0, fixed = true);
63 Real err1(start = 0, fixed = true);
64 equation
65 der(w01) = w02;
66 der(w02) = -(2*pi*f0)^2*w01;
67 der(w11) = w12;
68 der(w12) = -(2*pi*f1)^2*w11;
69 der(x) = v;
70 v = if time < 0.5 then 4 else -4;
71 in0 = if time < 0.1 then z0 else z0+w01;
72 in1 = if time < 0.1 then z0 else z0+w11;
73 (out0, out1) = spatialDistribution(in0, in1, x, v > 0, {0, 1}, {z0, z0});
74 der(err0) = (out0_exact - out0)^2;
75 der(err1) = (out1_exact - out1)^2;
76 assert(err0 < 1e-3, "Numerical solution too far from exact one");
77 assert(err1 < 1e-3, "Numerical solution too far from exact one");
78
79 annotation(experiment(stopTime = 2, Interval = 1e-3, Tolerance = 1e-7));
80 end Test2;
81
82 model Test3
83 import Modelica.Constants.pi;
84 parameter Real f0 = 20;
85 parameter Real f1 = 4;
86 parameter Real z0 = 0.1;
87 Real w01(start = 1, fixed = true);
88 Real w02(start = 0, fixed = true);
89 Real w11(start = 1, fixed = true);
90 Real w12(start = 0, fixed = true);
91 Real in0, in1, out0, out1;
92 Real x(start = 0, fixed = true);
93 Real v;
94 Real w01_exact = cos(2*pi*f0*time);
95 Real w11_exact = cos(2*pi*f1*time);
96 Real aux1 = (if time < 0.5 then 0 else
97 if time < 0.75 then cos(2*pi*f0*(time + (-4*time +3 + sqrt(16*time^2-24*time+17))/4)) else
98 in1);
99 Real out1_exact = if time < 0.5 then z0 elseif aux1 < 0.5 then aux1 else z0;
100 Real aux0 = (if time < 0.75 + sqrt(2)/2 then in0 else
101 cos(2*pi*f1*(time + (-4*time +3 + sqrt(16*time^2-24*time+1))/4)));
102 Real out0_exact = if aux0 < 0.5 then aux0 else z0;
103 Real err0(start = 0, fixed = true);
104 Real err1(start = 0, fixed = true);
105 equation
106 der(w01) = w02;
107 der(w02) = -(2*pi*f0)^2*w01;
108 der(w11) = w12;
109 der(w12) = -(2*pi*f1)^2*w11;
110 der(x) = v;
111 v = 3 - 4*time;
112 in0 = if w01 <= 0.5 then w01 else z0;
113 in1 = if w11 <= 0.5 then w11 else z0;
114 (out0, out1) = spatialDistribution(in0, in1, x, v > 0, {0, 1}, {z0, z0});
115 der(err0) = (out0_exact - out0)^2;
116 der(err1) = (out1_exact - out1)^2;
117 assert(err0 < 1e-3, "Numerical solution too far from exact one");
118 assert(err1 < 1e-3, "Numerical solution too far from exact one");
119
120 annotation(experiment(stopTime = 2, Interval = 1e-3, Tolerance = 1e-7));
121 end Test3;
122annotation(uses(Modelica(version="3.2.3")));
123end TestSpatialDistribution;