|
| 1 | +model airconds_cont |
| 2 | + function getSection |
| 3 | + input Integer x; |
| 4 | + output Real y; |
| 5 | + external "C" y = getSection(x) annotation( |
| 6 | + Include="#include \"sections.c\""); |
| 7 | + end getSection; |
| 8 | + |
| 9 | + import math; |
| 10 | + constant Integer N = 4000; |
| 11 | + constant Integer SECTIONS = 4; |
| 12 | + parameter Real CAP[N], RES[N], POT[N], THA = 32,pmax=0,Kp=1,Ki=1,tref=20; |
| 13 | + parameter Integer sections[N]; |
| 14 | + parameter Integer sections_init[SECTIONS]; |
| 15 | + parameter Integer sections_end[SECTIONS]; |
| 16 | + Real th[N]; |
| 17 | + Real ierr; |
| 18 | + discrete Real ptotals; |
| 19 | + discrete Real partTotal[SECTIONS]; |
| 20 | + discrete Real sendPartTotal[SECTIONS]; |
| 21 | + Real ptotal; |
| 22 | + |
| 23 | + discrete Real on[N]; |
| 24 | + discrete Real dtref,pref(start=0.5); |
| 25 | + discrete Real nextSample(start=1); |
| 26 | + discrete Real partSample[SECTIONS](each start=1); |
| 27 | + discrete Real noise[N]; |
| 28 | + discrete Real update[SECTIONS]; |
| 29 | + discrete Real sampleNoise[N]; |
| 30 | + discrete Real inputs[N]; |
| 31 | + |
| 32 | + initial algorithm |
| 33 | + for i in 1:N loop |
| 34 | + th[i] := random(4)+ 18; |
| 35 | + CAP[i] := random(100)+ 550; |
| 36 | + RES[i] := random(0.4)+ 1.8; |
| 37 | + POT[i] := random(0.2)+ 13; |
| 38 | + pmax:=pmax+POT[i]; |
| 39 | + noise[i] := 2*sin(i)^2; |
| 40 | + sampleNoise[i] := random(2); |
| 41 | + sections[i] := getSection(i); |
| 42 | + end for; |
| 43 | + |
| 44 | + for i in 1:SECTIONS loop |
| 45 | + sections_init[i] := (i-1)*N/SECTIONS; |
| 46 | + sections_end[i] := i*N/SECTIONS; |
| 47 | + end for; |
| 48 | + |
| 49 | + |
| 50 | + for i in 1:N loop |
| 51 | + if th[i] - tref - 0.5> 0 then |
| 52 | + on[i] := 1; |
| 53 | + ptotal := ptotal + POT[i]; |
| 54 | + end if; |
| 55 | + end for; |
| 56 | + |
| 57 | + equation |
| 58 | + for i in 1:N loop |
| 59 | + der(th[i]) = (THA/RES[i]-POT[i]*on[i]-th[i]/RES[i]+noise[i]/RES[i])/CAP[i]; |
| 60 | + end for; |
| 61 | + der(ierr) = pref-ptotals/pmax; |
| 62 | + der(ptotal) = 0; |
| 63 | + |
| 64 | + algorithm |
| 65 | + for i in 1:SECTIONS loop |
| 66 | + when time > partSample[i] then |
| 67 | + partSample[i] := partSample[i]+1; |
| 68 | + update[i] := 1; |
| 69 | + sendPartTotal[i] := partTotal[i]; |
| 70 | + reinit(ptotal, ptotal + sendPartTotal[i]); |
| 71 | + partTotal[i] := 0; |
| 72 | + end when annotation(MMO_Event_Id="Ev_1", |
| 73 | + MMO_HH={(Ev_6[_d2],[1:SECTIONS, sections_init[i]:sections_end[i]])}); |
| 74 | + end for; |
| 75 | + |
| 76 | + for i in 1:SECTIONS loop |
| 77 | + when update[i] > 0.5 then |
| 78 | + reinit(ptotal, ptotal + sendPartTotal[i]); |
| 79 | + update[i] := 0; |
| 80 | + end when; |
| 81 | + end for; |
| 82 | + |
| 83 | + when time > 1000 then |
| 84 | + pref := 0.4; |
| 85 | + end when; |
| 86 | + when time > 2000 then |
| 87 | + pref := 0.5; |
| 88 | + end when; |
| 89 | + |
| 90 | + when time > nextSample then |
| 91 | + nextSample := nextSample+1; |
| 92 | + ptotals := ptotal; |
| 93 | + dtref := Kp*(ptotals/pmax-pref)-Ki*ierr; |
| 94 | + end when; |
| 95 | + |
| 96 | + for i in 1:N loop |
| 97 | + when th[i] - tref -dtref + on[i] - 0.5 > 0 then |
| 98 | + on[i] := 1; |
| 99 | + partTotal[sections[i]]:= partTotal[sections[i]] + POT[i]; |
| 100 | + elsewhen th[i] - tref -dtref + on[i] - 0.5 < 0 then |
| 101 | + on[i] := 0; |
| 102 | + partTotal[sections[i]]:= partTotal[sections[i]] - POT[i]; |
| 103 | + end when annotation(MMO_Event_Id="Ev_6", |
| 104 | + MMO_LHS_DSC={partTotal[sections[i]]}, |
| 105 | + MMO_HH={Ev_1[sections[i]]} ); |
| 106 | + |
| 107 | + end for; |
| 108 | + |
| 109 | + for i in 1:N loop |
| 110 | + when time > sampleNoise[i] then |
| 111 | + sampleNoise[i] := sampleNoise[i] + 1; |
| 112 | + inputs[i] := inputs[i] + 1; |
| 113 | + noise[i] := 2*sin(i*inputs[i])^2; |
| 114 | + end when; |
| 115 | + end for; |
| 116 | + annotation( |
| 117 | +
|
| 118 | + experiment( |
| 119 | + MMO_Description="Control of the power consumption of a large populaion of air conditioners.", |
| 120 | + MMO_Solver=QSS2, |
| 121 | + MMO_Period={3000/5000}, |
| 122 | + MMO_Parallel=true, |
| 123 | + MMO_PartitionMethod=Manual, |
| 124 | + MMO_LPS=4, |
| 125 | + MMO_DT_Synch=SD_DT_Fixed, |
| 126 | + MMO_DT_Min=10, |
| 127 | + MMO_Output={ptotals}, |
| 128 | + Jacobian=Dense, |
| 129 | + MMO_BDF_PDepth=1, |
| 130 | + MMO_BDF_Max_Step=0, |
| 131 | + StartTime=0, |
| 132 | + StopTime=3000, |
| 133 | + Tolerance={1e-4}, |
| 134 | + AbsTolerance={1e-4} |
| 135 | + )); |
| 136 | +end airconds_cont; |
0 commit comments