Ticket #2135: HeatedRoom V14_UTF8.mo

File HeatedRoom V14_UTF8.mo, 34.0 KB (added by mbouissou@…, 12 years ago)

An example of package able to trigger the bug

Line 
1within ;
2package HeatedRoom
3 model HeatedRoom_BlockDiagram
4
5 Modelica.Blocks.Sources.Constant const(k=5)
6 annotation (Placement(transformation(extent={{-180,90},{-160,110}})));
7 Modelica.Blocks.Sources.Constant Outside_Temperature(k=13)
8 annotation (Placement(transformation(extent={{-180,10},{-160,30}})));
9 Modelica.Blocks.Math.Add add(k1=-0.1, k2=+0.1)
10 annotation (Placement(transformation(extent={{-120,20},{-100,40}})));
11 Modelica.Blocks.Math.Add add1
12 annotation (Placement(transformation(extent={{-60,60},{-40,80}})));
13 Modelica.Blocks.Math.Product product
14 annotation (Placement(transformation(extent={{-120,100},{-100,120}})));
15 Modelica.Blocks.Continuous.Integrator integrator(y_start=17)
16 annotation (Placement(transformation(extent={{-20,62},{0,82}})));
17 Modelica.Blocks.Logical.Hysteresis hysteresis(uLow=15, uHigh=20)
18 annotation (Placement(transformation(extent={{20,60},{40,80}})));
19 Modelica.Blocks.Math.BooleanToReal booleanToReal
20 annotation (Placement(transformation(extent={{100,60},{120,80}})));
21 Modelica.Blocks.Logical.Not not1
22 annotation (Placement(transformation(extent={{60,60},{80,80}})));
23 equation
24 connect(const.y, product.u2) annotation (Line(
25 points={{-159,100},{-140,100},{-140,104},{-122,104}},
26 color={0,0,127},
27 smooth=Smooth.None));
28 connect(Outside_Temperature.y, add.u2) annotation (Line(
29 points={{-159,20},{-140,20},{-140,24},{-122,24}},
30 color={0,0,127},
31 smooth=Smooth.None));
32 connect(product.y, add1.u1) annotation (Line(
33 points={{-99,110},{-80,110},{-80,76},{-62,76}},
34 color={0,0,127},
35 smooth=Smooth.None));
36 connect(add.y, add1.u2) annotation (Line(
37 points={{-99,30},{-80,30},{-80,64},{-62,64}},
38 color={0,0,127},
39 smooth=Smooth.None));
40 connect(add1.y, integrator.u) annotation (Line(
41 points={{-39,70},{-30,70},{-30,72},{-22,72}},
42 color={0,0,127},
43 smooth=Smooth.None));
44 connect(integrator.y, hysteresis.u) annotation (Line(
45 points={{1,72},{10,72},{10,70},{18,70}},
46 color={0,0,127},
47 smooth=Smooth.None));
48 connect(booleanToReal.y, product.u1) annotation (Line(
49 points={{121,70},{140,70},{140,160},{-140,160},{-140,116},{-122,116}},
50 color={0,0,127},
51 smooth=Smooth.None));
52 connect(integrator.y, add.u1) annotation (Line(
53 points={{1,72},{10,72},{10,50},{-140,50},{-140,36},{-122,36}},
54 color={0,0,127},
55 smooth=Smooth.None));
56 connect(hysteresis.y, not1.u) annotation (Line(
57 points={{41,70},{58,70}},
58 color={255,0,255},
59 smooth=Smooth.None));
60 connect(not1.y, booleanToReal.u) annotation (Line(
61 points={{81,70},{98,70}},
62 color={255,0,255},
63 smooth=Smooth.None));
64 annotation (Diagram(coordinateSystem(preserveAspectRatio=true, extent={{
65 -200,-200},{200,200}}), graphics), Icon(coordinateSystem(
66 preserveAspectRatio=true, extent={{-200,-200},{200,200}})));
67 end HeatedRoom_BlockDiagram;
68
69 model HeatedRoom_BlockDiagramStructured
70
71 Modelica.Blocks.Sources.Constant const(k=5)
72 annotation (Placement(transformation(extent={{-60,20},{-40,40}})));
73 Modelica.Blocks.Sources.Constant Outside_Temperature(k=13)
74 annotation (Placement(transformation(extent={{-220,-70},{-200,-50}})));
75 Modelica.Blocks.Math.Add add(k1=-0.1, k2=+0.1)
76 annotation (Placement(transformation(extent={{0,-60},{20,-40}})));
77 Modelica.Blocks.Math.Add add1
78 annotation (Placement(transformation(extent={{60,-10},{80,10}})));
79 Modelica.Blocks.Math.Product product
80 annotation (Placement(transformation(extent={{0,30},{20,50}})));
81 Modelica.Blocks.Continuous.Integrator integrator(y_start=17)
82 annotation (Placement(transformation(extent={{100,-10},{120,10}})));
83 Modelica.Blocks.Logical.Hysteresis hysteresis(uLow=15, uHigh=20)
84 annotation (Placement(transformation(extent={{-220,80},{-200,100}})));
85 Modelica.Blocks.Math.BooleanToReal booleanToReal
86 annotation (Placement(transformation(extent={{-140,80},{-120,100}})));
87 Modelica.Blocks.Logical.Not not1
88 annotation (Placement(transformation(extent={{-180,80},{-160,100}})));
89 equation
90 connect(const.y, product.u2) annotation (Line(
91 points={{-39,30},{-20,30},{-20,34},{-2,34}},
92 color={0,0,127},
93 smooth=Smooth.None));
94 connect(Outside_Temperature.y, add.u2) annotation (Line(
95 points={{-199,-60},{-20,-60},{-20,-56},{-2,-56}},
96 color={0,0,127},
97 smooth=Smooth.None));
98 connect(product.y, add1.u1) annotation (Line(
99 points={{21,40},{40,40},{40,6},{58,6}},
100 color={0,0,127},
101 smooth=Smooth.None));
102 connect(add.y, add1.u2) annotation (Line(
103 points={{21,-50},{40,-50},{40,-6},{58,-6}},
104 color={0,0,127},
105 smooth=Smooth.None));
106 connect(add1.y, integrator.u) annotation (Line(
107 points={{81,0},{98,0}},
108 color={0,0,127},
109 smooth=Smooth.None));
110 connect(integrator.y, add.u1) annotation (Line(
111 points={{121,0},{180,0},{180,-30},{-20,-30},{-20,-44},{-2,-44}},
112 color={0,0,127},
113 smooth=Smooth.None));
114 connect(integrator.y, hysteresis.u) annotation (Line(
115 points={{121,0},{180,0},{180,140},{-260,140},{-260,90},{-222,90}},
116 color={0,0,127},
117 smooth=Smooth.None));
118 connect(hysteresis.y, not1.u) annotation (Line(
119 points={{-199,90},{-182,90}},
120 color={255,0,255},
121 smooth=Smooth.None));
122 connect(not1.y, booleanToReal.u) annotation (Line(
123 points={{-159,90},{-142,90}},
124 color={255,0,255},
125 smooth=Smooth.None));
126 connect(booleanToReal.y, product.u1) annotation (Line(
127 points={{-119,90},{-20,90},{-20,46},{-2,46}},
128 color={0,0,127},
129 smooth=Smooth.None));
130 annotation (Diagram(coordinateSystem(preserveAspectRatio=true, extent={{
131 -300,-200},{300,200}}), graphics={
132 Rectangle(
133 extent={{-80,60},{160,-100}},
134 lineColor={0,0,255},
135 fillColor={170,255,213},
136 fillPattern=FillPattern.Solid),
137 Rectangle(
138 extent={{-240,-20},{-100,-100}},
139 lineColor={0,0,255},
140 fillColor={170,213,255},
141 fillPattern=FillPattern.Solid),
142 Rectangle(
143 extent={{-240,120},{-100,60}},
144 lineColor={0,0,255},
145 fillColor={255,213,170},
146 fillPattern=FillPattern.Solid),
147 Text(
148 extent={{-244,70},{-168,66}},
149 lineColor={0,0,255},
150 fillColor={170,213,255},
151 fillPattern=FillPattern.Solid,
152 textString="Heater controller"),
153 Text(
154 extent={{-232,-92},{-172,-92}},
155 lineColor={0,0,255},
156 fillColor={170,213,255},
157 fillPattern=FillPattern.Solid,
158 textString="Outside weather"),
159 Text(
160 extent={{-68,-90},{-18,-92}},
161 lineColor={0,0,255},
162 fillColor={170,213,255},
163 fillPattern=FillPattern.Solid,
164 textString="Heated room")}), Icon(coordinateSystem(
165 preserveAspectRatio=true, extent={{-300,-200},{300,200}})));
166 end HeatedRoom_BlockDiagramStructured;
167
168 model HeatedRoom_Equational
169
170 HeatedRoom heatedRoom
171 annotation (Placement(transformation(extent={{20,-6},{60,14}})));
172 HeaterController heaterController
173 annotation (Placement(transformation(extent={{-86,18},{-42,38}})));
174 OutsideWeather outsideWeather
175 annotation (Placement(transformation(extent={{-64,-20},{-20,0}})));
176 Heater heater
177 annotation (Placement(transformation(extent={{-22,18},{-2,38}})));
178 equation
179 connect(heatedRoom.RoomTemperature, heaterController.RoomTemperature)
180 annotation (Line(
181 points={{62,4},{80,4},{80,60},{-98,60},{-98,28},{-90.4,28}},
182 color={0,0,127},
183 smooth=Smooth.None));
184 connect(outsideWeather.Outside_Temperature, heatedRoom.Outside_Temperature)
185 annotation (Line(
186 points={{-17.8,-10},{0,-10},{0,-2},{16,-2}},
187 color={0,0,127},
188 smooth=Smooth.Bezier));
189 connect(heaterController.HeaterControl, heater.u) annotation (Line(
190 points={{-39.8,28},{-24,28}},
191 color={0,0,127},
192 smooth=Smooth.None));
193 connect(heater.y, heatedRoom.HeaterControl) annotation (Line(
194 points={{-1,28},{6,28},{6,10},{16,10}},
195 color={0,0,127},
196 smooth=Smooth.Bezier));
197 annotation (Diagram(graphics),
198 experiment(StopTime=1000),
199 __Dymola_experimentSetupOutput);
200 end HeatedRoom_Equational;
201
202 model HeatedRoom
203 parameter Real CoeffOutsideTemperature = 0.1
204 "Coefficient to take into account the outside temperature (-)";
205 parameter Real CoeffHeaterController = 5
206 "Coefficient to take into account the heater controller (-)";
207 parameter Real RoomTemperatureInit = 17
208 "Initial temperature of the room (°C)";
209 Real T(start=RoomTemperatureInit) "Room temperature";
210
211 Real MeanTemperature[10]( start = 17* ones(10)); //creates bad results
212 // Real MeanTemperature[10];
213 Real temper[10];
214 Integer k(start=0);
215 Integer i;
216
217 Modelica.Blocks.Interfaces.RealInput HeaterControl
218 annotation (Placement(transformation(extent={{-140,40},{-100,80}}),
219 iconTransformation(extent={{-140,40},{-100,80}})));
220 Modelica.Blocks.Interfaces.RealInput Outside_Temperature
221 annotation (Placement(transformation(extent={{-140,-80},{-100,-40}})));
222 Modelica.Blocks.Interfaces.RealOutput RoomTemperature
223 annotation (Placement(transformation(extent={{100,-10},{120,10}})));
224
225 equation
226 der(T)= CoeffOutsideTemperature*(Outside_Temperature - T) + CoeffHeaterController*HeaterControl;
227 RoomTemperature = T;
228
229 algorithm
230
231 when sample(0,100) then
232 reinit( T,RoomTemperatureInit);
233 k:=1+integer(time/100);
234 end when;
235
236 when sample(0,10) then
237 i:=1+integer((time-(k-1)*100)/ 10);
238 temper[i]:=T + temper[i];
239 MeanTemperature[i]:=temper[i]/k;
240 end when;
241
242 annotation (Diagram(graphics={Rectangle(extent={{-100,100},{100,-100}},
243 lineColor={0,0,255})}), Icon(graphics={Rectangle(extent={{-100,100},
244 {100,-100}}, lineColor={0,0,255}), Text(
245 extent={{-62,22},{54,-18}},
246 lineColor={0,0,255},
247 fillPattern=FillPattern.Solid,
248 textString="%name")}),
249 experiment(StopTime=1000),
250 __Dymola_experimentSetupOutput);
251 end HeatedRoom;
252
253 model HeaterController
254 parameter Real TemperatureMin = 15
255 "Minimal temperature from which the heater starts (°C)";
256 parameter Real TemperatureMax = 20
257 "Maximal temperature from which the heater stops (°C)";
258 Modelica.Blocks.Interfaces.RealInput RoomTemperature
259 annotation (Placement(transformation(extent={{-140,-20},{-100,20}})));
260 Modelica.Blocks.Interfaces.RealOutput HeaterControl
261 annotation (Placement(transformation(extent={{100,-10},{120,10}})));
262 Modelica.Blocks.Logical.Hysteresis hysteresis(uLow=TemperatureMin, uHigh=
263 TemperatureMax)
264 annotation (Placement(transformation(extent={{-60,-10},{-40,10}})));
265 Modelica.Blocks.Logical.Not not1
266 annotation (Placement(transformation(extent={{-20,-10},{0,10}})));
267 Modelica.Blocks.Math.BooleanToReal booleanToReal
268 annotation (Placement(transformation(extent={{20,-10},{40,10}})));
269 equation
270 connect(RoomTemperature, hysteresis.u) annotation (Line(
271 points={{-120,0},{-62,0}},
272 color={0,0,127},
273 smooth=Smooth.None));
274 connect(hysteresis.y, not1.u) annotation (Line(
275 points={{-39,0},{-22,0}},
276 color={255,0,255},
277 smooth=Smooth.None));
278 connect(not1.y, booleanToReal.u) annotation (Line(
279 points={{1,0},{18,0}},
280 color={255,0,255},
281 smooth=Smooth.None));
282 connect(booleanToReal.y, HeaterControl) annotation (Line(
283 points={{41,0},{110,0}},
284 color={0,0,127},
285 smooth=Smooth.None));
286 annotation (Diagram(graphics={Rectangle(extent={{-100,100},{100,-100}},
287 lineColor={0,0,255})}), Icon(graphics={Rectangle(extent={{-100,100},
288 {100,-100}}, lineColor={0,0,255}), Text(
289 extent={{-62,22},{54,-18}},
290 lineColor={0,0,255},
291 fillPattern=FillPattern.Solid,
292 textString="%name")}));
293 end HeaterController;
294
295 model OutsideWeather
296 parameter Real OutsideTemperature = 13 "Outside temperature (°C)";
297 Modelica.Blocks.Interfaces.RealOutput Outside_Temperature
298 annotation (Placement(transformation(extent={{100,-10},{120,10}})));
299 Modelica.Blocks.Sources.Constant Outside_Temperature1(k=OutsideTemperature)
300 annotation (Placement(transformation(extent={{-20,-10},{0,10}})));
301 equation
302 connect(Outside_Temperature1.y, Outside_Temperature) annotation (Line(
303 points={{1,0},{110,0}},
304 color={0,0,127},
305 smooth=Smooth.None));
306 annotation (Diagram(graphics={Rectangle(extent={{-100,100},{100,-100}},
307 lineColor={0,0,255})}), Icon(graphics={Rectangle(extent={{-100,100},
308 {100,-100}}, lineColor={0,0,255}), Text(
309 extent={{-62,22},{54,-18}},
310 lineColor={0,0,255},
311 fillPattern=FillPattern.Solid,
312 textString="%name")}));
313 end OutsideWeather;
314
315 model Heater
316
317 extends Modelica.Blocks.Interfaces.SISO;
318
319 Boolean working(start = true);
320 Real starttime_working( start = 0);
321 Real starttime_notworking( start = 0);
322
323 // In fact the 3 following parameters are integers, but they must be declared as real numbers because
324 // the maximum value of an integer is 2^31 (?)
325 // parameter Real m(start = 4294967296); // m = 2^32, parameter of the pseudo-random generator
326 // parameter Real a(start=214013); // parameter of the pseudo-random generator
327 //parameter Real c(start=2531011); // parameter of the pseudo-random generator
328 Real x;
329 Real x1;
330 parameter Real lambda = 0.03;
331 parameter Real mu = 0.1;
332
333 //parameter Real seed( start = 1973272912);
334
335 //Real F; //The successive values of F will be a suite of pseudo-random numbers with a uniform distribution on [0,1]
336 Real X;
337 Real Y;
338 parameter Integer[3] seed_vector( start={1,15,3});
339 Integer[3] rand_vector;
340 Integer[3] pre_rand_vector;
341 algorithm
342
343 //when initial() then
344 // F := seed;
345 //end when;
346 when initial() then
347 rand_vector:=seed_vector;
348 end when;
349 // Attention: the two following rules must not be merged in a single one!
350 //when initial() then //calculating the first random working time
351 // F := mod(a*F+c, m);
352 // x := F / m;
353 // X:= (-log(1-x))/lambda;
354 //end when;
355
356 when working then //random draw of the next working time
357 //F := mod(a*F+c, m);
358 pre_rand_vector:=pre(rand_vector);
359 (x,rand_vector):=trials.GoodRandom(pre_rand_vector);
360 //x := F / m;
361 X:= (-log(1-x))/lambda;
362 end when;
363
364 when (not working) then //random draw of the next failure time
365 //F:= mod(a*F+c, m);
366 //x1 := F / m;
367 pre_rand_vector:=pre(rand_vector);
368 (x1, rand_vector):=trials.GoodRandom(pre_rand_vector);
369 Y:= (-log(1-x1))/mu;
370 end when;
371
372 // X is the working time
373 when working and (time - starttime_working) > X then
374 working := false;
375 starttime_notworking := time;
376 end when;
377
378 // Y is the failure time
379 when (not working) and (time - starttime_notworking) > Y then
380 working := true;
381 starttime_working := time;
382 end when;
383
384 // Reinitialization every 100 hours
385 when sample(0, 100) then
386 working:=true;
387 starttime_working := time;
388 end when;
389
390 // Input-output relation
391 equation
392 if working then
393 y = u;
394 else
395 y = 0.;
396 end if;
397
398 annotation (experiment(StopTime=1000, NumberOfIntervals=10),
399 __Dymola_experimentSetupOutput,
400 Icon(graphics={
401 Text(
402 extent={{-14,-8},{-18,2}},
403 lineColor={0,0,255},
404 textString="%name"),
405 Rectangle(extent={{-68,68},{-14,34}}, lineColor={0,0,255}),
406 Text(
407 extent={{-84,20},{92,-16}},
408 lineColor={0,0,255},
409 textString="%name")}),
410 Diagram(graphics));
411 end Heater;
412
413 package trials
414
415 function rand "rand"
416 output Integer y;
417 external "C" y = rand(0);
418 annotation (
419 Coordsys(
420 extent=[-100, -100; 100, 100],
421 grid=[2, 2],
422 component=[20, 20]),
423 Window(
424 x=0.45,
425 y=0.01,
426 width=0.35,
427 height=0.49),
428 Icon(
429 Text(
430 extent=[-84, 18; 84, -30],
431 string="fonction",
432 style(color=45)),
433 Text(extent=[-134, 104; 142, 44], string="%name"),
434 Ellipse(extent=[-100, 40; 100, -100], style(color=45, fillPattern=0)),
435 Text(
436 extent=[-82, -22; 86, -70],
437 string="externe",
438 style(color=45))),
439 Documentation(info="<html>
440<p><b>Version 1.0</b></p>
441</HTML>
442"));
443 end rand;
444
445 function srand "rand"
446 input Integer u;
447 external "C" srand(u);
448 annotation (
449 Coordsys(
450 extent=[-100, -100; 100, 100],
451 grid=[2, 2],
452 component=[20, 20]),
453 Window(
454 x=0.26,
455 y=0.28,
456 width=0.6,
457 height=0.6),
458 Icon(
459 Text(extent=[-134, 104; 142, 44], string="%name"),
460 Ellipse(extent=[-100, 40; 100, -100], style(color=45, fillPattern=0)),
461 Text(
462 extent=[-84, 18; 84, -30],
463 string="fonction",
464 style(color=45)),
465 Text(
466 extent=[-82, -22; 86, -70],
467 string="externe",
468 style(color=45))),
469 Documentation(info="<html>
470<p><b>Version 1.0</b></p>
471</HTML>
472"));
473 end srand;
474
475 function fmod "fmod"
476 input Real u1;
477 input Real u2;
478 output Real y;
479 external "C" y = fmod(u1, u2);
480 annotation (Icon(
481 Text(extent=[-134,104; 142,44], string="%name"),
482 Ellipse(extent=[-100,40; 100,-100], style(color=45, fillPattern=0)),
483 Text(
484 extent=[-84,18; 84,-30],
485 string="fonction",
486 style(color=45)),
487 Text(
488 extent=[-82,-22; 86,-70],
489 string="externe",
490 style(color=45))), Documentation(info="<html>
491<p><b>Version 1.6</b></p>
492</HTML>
493"));
494 end fmod;
495
496 function random
497 annotation (DymolaStoredErrors(thetext="function random
498
499algorithm
500
501 parameter Integer seed=1 \"Source du générateur aléatoire\";
502 parameter Real SampleOffset=0 \"Instant de départ de l'échantillonnage (s)\";
503 parameter Real SampleInterval=0.01 \"Période d'échantillonnage (s)\";
504
505equation
506
507 when initial() then
508 Commun.srand(seed);
509 end when;
510
511 when sample(SampleOffset, SampleInterval) then
512 y.signal = Commun.fmod(Commun.rand()/32768*10, 1);
513 end when;
514
515
516end random;
517"));
518 end random;
519
520 model test_states
521 Modelica.StateGraph.InitialStep initialStep
522 annotation (Placement(transformation(extent={{-82,16},{-62,36}})));
523 Modelica.StateGraph.Step step
524 annotation (Placement(transformation(extent={{-68,-34},{-48,-14}})));
525 Modelica.StateGraph.Transition transition(
526 enableTimer=true,
527 condition=true,
528 waitTime=-10)
529 annotation (Placement(transformation(extent={{-38,16},{-18,36}})));
530 Modelica.StateGraph.Alternative alternative
531 annotation (Placement(transformation(extent={{-16,-72},{52,-4}})));
532 Modelica.StateGraph.Step step1
533 annotation (Placement(transformation(extent={{-74,-80},{-54,-60}})));
534 equation
535 connect(transition.outPort, step.inPort[1]) annotation (Line(
536 points={{-26.5,26},{12,26},{12,4},{-86,4},{-86,-24},{-69,-24}},
537 color={0,0,0},
538 smooth=Smooth.None));
539 connect(initialStep.outPort[1], transition.inPort) annotation (Line(
540 points={{-61.5,26},{-32,26}},
541 color={0,0,0},
542 smooth=Smooth.None));
543 connect(step.outPort[1], alternative.inPort) annotation (Line(
544 points={{-47.5,-24},{-34,-24},{-34,-38},{-17.02,-38}},
545 color={0,0,0},
546 smooth=Smooth.None));
547 connect(step1.outPort[1], alternative.inPort) annotation (Line(
548 points={{-53.5,-70},{-32,-70},{-32,-38},{-17.02,-38}},
549 color={0,0,0},
550 smooth=Smooth.None));
551 annotation (Diagram(graphics));
552 end test_states;
553
554 function MyRandom
555 "Attempt to define a \"global\" function that could be called from various classes"
556
557 parameter Integer m(start = (2^31)-1);
558 parameter Integer a(start=214013);
559 parameter Integer c(start=2531011); /// Microsoft Visual/Quick C/C++
560 Integer x;
561 Integer xiplus;
562 Boolean first( start = true);
563 output Real u; // pseudo random number in [0,1]
564 parameter Integer seed( start = 12345);
565
566 // the instructions below do not work because algorithm
567 // is repeatedly executed !!
568 algorithm
569 if first then
570 xiplus := seed;
571 first := false;
572 else
573 xiplus := mod(a*x+c, m);
574 end if;
575
576 x := xiplus;
577 u := xiplus / m;
578
579 end MyRandom;
580
581 block MyHysteresis
582 "Transform Real to Boolean signal with Hysteresis and periodic reinitialization"
583
584 extends Modelica.Blocks.Interfaces.partialBooleanBlockIcon;
585 parameter Real uLow(start=0) "if y=true and u<=uLow, switch to y=false";
586 parameter Real uHigh(start=1) "if y=false and u>=uHigh, switch to y=true";
587 parameter Boolean pre_y_start=false "Value of pre(y) at initial time";
588
589 Modelica.Blocks.Interfaces.RealInput u annotation (Placement(
590 transformation(extent={
591 {-140,-20},{-100,20}}, rotation=0)));
592 Modelica.Blocks.Interfaces.BooleanOutput y
593 annotation (Placement(transformation(extent={{100,-10},{120,10}},
594 rotation=0)));
595
596 //initial equation
597 // pre(y) = pre_y_start;
598
599 equation
600 when sample(0, 100) then
601 reinit(y, pre_y_start);
602 end when;
603 y = u > uHigh or pre(y) and u >= uLow;
604 annotation (
605 Diagram(coordinateSystem(preserveAspectRatio=true, extent={{-100,-100},{
606 100,100}}), graphics={
607 Polygon(
608 points={{-65,89},{-73,67},{-57,67},{-65,89}},
609 lineColor={192,192,192},
610 fillColor={192,192,192},
611 fillPattern=FillPattern.Solid),
612 Line(points={{-65,67},{-65,-81}}, color={192,192,192}),
613 Line(points={{-90,-70},{82,-70}}, color={192,192,192}),
614 Polygon(
615 points={{90,-70},{68,-62},{68,-78},{90,-70}},
616 lineColor={192,192,192},
617 fillColor={192,192,192},
618 fillPattern=FillPattern.Solid),
619 Text(
620 extent={{70,-80},{94,-100}},
621 lineColor={160,160,164},
622 textString="u"),
623 Text(
624 extent={{-65,93},{-12,75}},
625 lineColor={160,160,164},
626 textString="y"),
627 Line(
628 points={{-80,-70},{30,-70}},
629 color={0,0,0},
630 thickness=0.5),
631 Line(
632 points={{-50,10},{80,10}},
633 color={0,0,0},
634 thickness=0.5),
635 Line(
636 points={{-50,10},{-50,-70}},
637 color={0,0,0},
638 thickness=0.5),
639 Line(
640 points={{30,10},{30,-70}},
641 color={0,0,0},
642 thickness=0.5),
643 Line(
644 points={{-10,-65},{0,-70},{-10,-75}},
645 color={0,0,0},
646 thickness=0.5),
647 Line(
648 points={{-10,15},{-20,10},{-10,5}},
649 color={0,0,0},
650 thickness=0.5),
651 Line(
652 points={{-55,-20},{-50,-30},{-44,-20}},
653 color={0,0,0},
654 thickness=0.5),
655 Line(
656 points={{25,-30},{30,-19},{35,-30}},
657 color={0,0,0},
658 thickness=0.5),
659 Text(
660 extent={{-99,2},{-70,18}},
661 lineColor={160,160,164},
662 textString="true"),
663 Text(
664 extent={{-98,-87},{-66,-73}},
665 lineColor={160,160,164},
666 textString="false"),
667 Text(
668 extent={{19,-87},{44,-70}},
669 lineColor={0,0,0},
670 textString="uHigh"),
671 Text(
672 extent={{-63,-88},{-38,-71}},
673 lineColor={0,0,0},
674 textString="uLow"),
675 Line(points={{-69,10},{-60,10}}, color={160,160,164})}),
676 Icon(coordinateSystem(preserveAspectRatio=true, extent={{-100,-100},{100,
677 100}}), graphics={
678 Polygon(
679 points={{-80,90},{-88,68},{-72,68},{-80,90}},
680 lineColor={192,192,192},
681 fillColor={192,192,192},
682 fillPattern=FillPattern.Solid),
683 Line(points={{-80,68},{-80,-29}}, color={192,192,192}),
684 Polygon(
685 points={{92,-29},{70,-21},{70,-37},{92,-29}},
686 lineColor={192,192,192},
687 fillColor={192,192,192},
688 fillPattern=FillPattern.Solid),
689 Line(points={{-79,-29},{84,-29}}, color={192,192,192}),
690 Line(points={{-79,-29},{41,-29}}, color={0,0,0}),
691 Line(points={{-15,-21},{1,-29},{-15,-36}}, color={0,0,0}),
692 Line(points={{41,51},{41,-29}}, color={0,0,0}),
693 Line(points={{33,3},{41,22},{50,3}}, color={0,0,0}),
694 Line(points={{-49,51},{81,51}}, color={0,0,0}),
695 Line(points={{-4,59},{-19,51},{-4,43}}, color={0,0,0}),
696 Line(points={{-59,29},{-49,11},{-39,29}}, color={0,0,0}),
697 Line(points={{-49,51},{-49,-29}}, color={0,0,0}),
698 Text(
699 extent={{-92,-49},{-9,-92}},
700 lineColor={192,192,192},
701 textString="%uLow"),
702 Text(
703 extent={{2,-49},{91,-92}},
704 lineColor={192,192,192},
705 textString="%uHigh"),
706 Rectangle(extent={{-91,-49},{-8,-92}}, lineColor={192,192,192}),
707 Line(points={{-49,-29},{-49,-49}}, color={192,192,192}),
708 Rectangle(extent={{2,-49},{91,-92}}, lineColor={192,192,192}),
709 Line(points={{41,-29},{41,-49}}, color={192,192,192})}),
710 Documentation(info="<HTML>
711<p>
712This block transforms a <b>Real</b> input signal into a <b>Boolean</b>
713output signal:
714</p>
715<ul>
716<li> When the output was <b>false</b> and the input becomes
717 <b>greater</b> than parameter <b>uHigh</b>, the output
718 switches to <b>true</b>.</li>
719<li> When the output was <b>true</b> and the input becomes
720 <b>less</b> than parameter <b>uLow</b>, the output
721 switches to <b>false</b>.</li>
722</ul>
723<p>
724The start value of the output is defined via parameter
725<b>pre_y_start</b> (= value of pre(y) at initial time).
726The default value of this parameter is <b>false</b>.
727</p>
728</HTML>
729"));
730 end MyHysteresis;
731
732 function GoodRandom "Pseudo random number generator"
733
734 input Integer seedIn[3]
735 "Integer vector defining random number sequence, e.g., {23,87,187}" annotation (extent=[-85, 15; -15, 85]);
736 output Real x "Random number between 0 and 1";
737 output Integer seedOut[3]
738 "Modified seed to be used for next call of random()"
739 annotation (extent=[15, 15; 85, 85]);
740 algorithm
741 seedOut[1] := rem((171*seedIn[1]), 30269);
742 seedOut[2] := rem((172*seedIn[2]), 30307);
743 seedOut[3] := rem((170*seedIn[3]), 30323);
744 // zero is a poor seed, therfore substitute 1;
745 if seedOut[1] == 0 then
746 seedOut[1] := 1;
747 end if;
748 if seedOut[2] == 0 then
749 seedOut[2] := 1;
750 end if;
751 if seedOut[3] == 0 then
752 seedOut[3] := 1;
753 end if;
754 x := rem((seedOut[1]/30269.0 + seedOut[2]/30307.0 + seedOut[3]/30323.0), 1.0);
755
756 annotation (
757 Coordsys(
758 extent=[-100, -100; 100, 100],
759 grid=[2, 2],
760 component=[20, 20]),
761 Window(
762 x=0.45,
763 y=0.01,
764 width=0.35,
765 height=0.49),
766 Documentation(info="<HTML>
767<p>
768<i>Random</i> generates a sequence of uniform distributed
769pseudo-random numbers. The algorithm is a variant of the
770multiplicative congruential algorithm, known as the
771Wichmann-Hill generator:
772<pre> x(k) = (a1*x(k-1)) mod m1
773 y(k) = (a2*y(k-1)) mod m2
774 z(k) = (a3*z(k-1)) mod m3
775 U(k) = (x(k)/m1 + y(k)/m2 + z(k)/m3) mod 1
776</pre>
777This generates pseudo-random numbers U(k) uniformly distributed
778in the interval (0,1). There are many forms of generators depending
779on the parameters m (prime numbers) and a. The sequence needs an
780initial Integer vector {x,y,z} known as the seed. The use of the same
781seed will lead to the same sequence of numbers.
782</p>
783<p>
784<b>Remarks</b>
785</p>
786<p>Random number generators (RNG) are pseudo-functions which are not true
787functions but algorithms which deliver a fixed sequence of (usually Integer) numbers
788which should have a very large period before they repeat itself and
789appropriate statistic properties such that the sequence appears to be
790a random draw. For real-valued random numbers, the integers are scaled to
791the real interval 0.0-1.0. They result in a uniformly distributed random variate
792between 0-1, which has to be tranformed to give a random variate of a wanted
793distribution. There are two types of techniques for transforming random variates:
794</p>
795<ul>
796<li>Acceptance-Rejection techniques</li>
797<li>Transformation techniques</li>
798</ul>
799<p>Acceptance-Rejection techniques throw away some of the generated variates and are thus less efficient. They can not be avoided for all distributions. A good summary about random number generation and most of the transformation techniques used below is given in:</p>
800 <address> Discrete Event Simulation <br>
801 Jerry Banks and John S. Carson II<br>
802 Prentice Hall Inc.<br>
803 Englewood Cliffs, New Jersey<br>
804 </address>
805<p>Some of the other references are quoted below.</p>
806<pre>
807 WICHMANN-HILL RANDOM NUMBER GENERATOR
808 Wichmann, B. A. & Hill, I. D. (1982)
809 Algorithm AS 183:
810 An efficient and portable pseudo-random number generator
811 Applied Statistics 31 (1982) 188-190
812 see also:
813 Correction to Algorithm AS 183
814 Applied Statistics 33 (1984) 123
815 McLeod, A. I. (1985)
816 A remark on Algorithm AS 183
817 Applied Statistics 34 (1985),198-200
818 In order to completely avoid external functions, all seeds are
819 set via parameters. For simulation purposes this is almost
820 always the desired behaviour.
821 Translated by Hubertus Tummescheit from Python source provided by
822 Guido van Rossum translated from C source by Adrian Baddeley.
823 http://www.python.org/doc/current/lib/module-random.html
824 R A N D O M V A R I A B L E G E N E R A T O R S
825 distributions on the real line:
826 ------------------------------
827 normal (Gaussian) 2 versions
828</pre>
829<h4>Reference Literature:</h4>
830<ul>
831<li>function random: Wichmann, B. A. & Hill, I. D. (1982), Algorithm AS 183:
832 <br>
833 An efficient and portable pseudo-random number generator, Applied Statistics 31 (1982) 188-190<br>
834 see also: Correction to Algorithm AS 183, Applied Statistics 33 (1984) 123 <br>
835 McLeod, A. I. (1985), A remark on Algorithm AS 183, Applied Statistics 34 (1985),198-200</li>
836<li>function normalvariate: Kinderman, A.J. and Monahan, J.F., 'Computer generation of random
837 variables using the ratio of uniform deviates', ACM Trans Math Software, 3, (1977),
838 pp257-260.</li>
839<li>function gaussianvariate: Discrete Event Simulation, Jerry Banks and John S. Carson II,
840<br>
841 Prentice Hall Inc. Englewood Cliffs, New Jersey, page 315/316</li>
842</ul>
843<p>
844Copyright &copy; Hubertus Tummescheit and Department of Automatic Control, Lund University, Sweden.
845</p>
846<p>
847<i>This Modelica function is <b>free</b> software; it can be redistributed and/or modified
848under the terms of the <b>Modelica license</b>, see the license conditions
849and the accompanying <b>disclaimer</b>
850<a href=\"Modelica://Modelica.UsersGuide.ModelicaLicense\">here</a>.</i>
851</p>
852</HTML>
853"));
854 end GoodRandom;
855
856 model testGoodRandom
857
858 parameter Integer[3] seed_vector( start={1,2,3});
859 Integer[3] rand_vector;
860 Integer[3] pre_rand_vector;
861 output Real x;
862
863 algorithm
864 when initial() then
865 rand_vector:=seed_vector;
866 end when;
867
868 when sample(0, 1) then
869
870 pre_rand_vector:=pre(rand_vector);
871 (x,rand_vector):=GoodRandom(pre_rand_vector);
872
873 end when;
874 end testGoodRandom;
875 annotation (
876 Coordsys(
877 extent=[0, 0; 313, 206],
878 grid=[2, 2],
879 component=[20, 20]),
880 Icon(
881 Rectangle(extent=[-100, -100; 80, 50], style(fillColor=30, fillPattern=1)),
882 Polygon(points=[-100, 50; -80, 70; 100, 70; 80, 50; -100, 50], style(
883 fillColor=30, fillPattern=1)),
884 Polygon(points=[100, 70; 100, -80; 80, -100; 80, 50; 100, 70], style(
885 fillColor=30, fillPattern=1)),
886 Text(
887 extent=[-90, 40; 70, 10],
888 string="Unites",
889 style(
890 color=9,
891 fillColor=0,
892 fillPattern=1)),
893 Rectangle(extent=[-32, -6; 16, -35], style(color=0)),
894 Rectangle(extent=[-32, -56; 16, -85], style(color=0)),
895 Line(points=[16, -20; 49, -20; 49, -71; 16, -71], style(color=0)),
896 Line(points=[-32, -72; -64, -72; -64, -21; -32, -21], style(color=0)),
897 Text(
898 extent=[-120, 135; 120, 70],
899 string="%name",
900 style(color=1))),
901 Window(
902 x=0.05,
903 y=0.26,
904 width=0.25,
905 height=0.25,
906 library=1,
907 autolayout=1),
908 Documentation(info="<html>
909<p><b>Version 1.1</b></p>
910</HTML>
911"));
912 end trials;
913
914 annotation (uses(Modelica(version="3.2")));
915end HeatedRoom;