-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathDynOPFlow.py
1402 lines (1036 loc) · 54.5 KB
/
DynOPFlow.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
# -*- coding: utf-8 -*-
"""
Created on Fri Nov 16 20:18:08 2012
@author:
Sebastien Gros
Assistant Professor
Department of Signals and Systems
Chalmers University of Technology
SE-412 96 Göteborg, SWEDEN
Python/casADi Module:
NMPC for Dynamic Optimal Power Flow and Power Dispatch
Requires the installation of the open-source Python module casADi together with the NLP solver ipopt
Required version of CasADi: v1.7.x
"""
from casadi import *
from casadi.tools import *
import math as math
import numpy as np
import matplotlib.pyplot as plt
import random as rand
#from ODE import *
plt.rcParams['text.usetex'] = False
#Fixed constants
#rho_air = 1.2
#rho_water = 1e3
#gravity = 9.81
def assertList(var):
if not(isinstance(var,list)):
var = [var]
return var
class Plant:
def __init__(self, Inputs = [], States = [], ExtParameters = [], R = 0., Directionality = 'Mono', Load = False, Bus = [], label = []): # set of reasons (strings) why the Dae cannot be modified (add new x/z/u/p/output)
self._frozen = False
self.Bus = Bus
self.label = label
self.Directionality = Directionality
self._Load = Load
self.R = R
#Plant Default Input structure (list ready to be embedded in a struct_sym)
InputList = [entry("CurrentReal"),
entry("CurrentImag")]
#Structure for INPUTS of various power plants
if (Load == True):
InputList.append(entry("ActivePower"))
InputList.append(entry("ReactivePower"))
elif (Directionality == 'Bi'):
InputList.append(entry("Pcharge"))
InputList.append(entry("Pdischarge"))
elif (Directionality == 'Mono'):
InputList.append(entry("Power"))
else:
print "Illegal option, ignored"
return
if (len(Inputs) > 0):
self._additionalInputs = Inputs #Keep the list for plotting purposes
for key in assertList(Inputs):
InputList.append(entry(key))
# States declared by the user
#if (len(States) > 0): ################ INTRODUCE THIS ######## !!!!
StateList = []
for key in assertList(States):
StateList.append(entry(key))
# External parameters declared by the user
if (len(ExtParameters) > 0):
ExtParamList = []
for key in assertList(ExtParameters):
ExtParamList.append(entry(key))
self.ExtParameters = struct_ssym(ExtParamList)
# lists of names (strings)
self.States = struct_ssym(StateList)
self.Inputs = struct_ssym(InputList)
self.InputsPrev = struct_ssym(InputList)
#Structure for plant bounds
Bound = [entry('Inputs', struct = self.Inputs)]
if (len(self.States.keys()) > 0):
Bound.append(entry('States', struct = self.States))
Bound = struct_ssym(Bound)
self.LB = Bound(-inf)
self.UB = Bound( inf)
if (Directionality == 'Mono') and (Load == False):
self.LB['Inputs','Power'] = 0.
elif (Directionality == 'Bi') and (Load == False):
self.LB['Inputs','Pcharge'] = 0.
self.LB['Inputs','Pdischarge'] = 0.
else:
self.LB['Inputs','ActivePower'] = 0.
def setDynamics(self, RHS = [], dt = 1., nstep = 10):
if (self._frozen == True):
print "Plant already added to the grid, call ignored"
return
print "Right-Hand side: ", RHS
if isinstance(RHS,list):
RHS = veccat(RHS)
X = self.States
U = self.Inputs
dtRK4 = dt/float(nstep)
fimplicit = SXFunction(daeIn(x=X,p=U),daeOut(ode=RHS))
fimplicit.init()
[k1] = daeOut(fimplicit.eval(daeIn( x=X,p=U )),"ode")
[k2] = daeOut(fimplicit.eval(daeIn( x=X+0.5*dtRK4*k1,p=U )),"ode")
[k3] = daeOut(fimplicit.eval(daeIn( x=X+0.5*dtRK4*k2,p=U )),"ode")
[k4] = daeOut(fimplicit.eval(daeIn( x=X+dtRK4*k3,p=U )),"ode")
rk4_step = SXFunction([X,U],[X + (1./6)*dtRK4*(k1 + 2*k2 + 2*k3 + k4)])
rk4_step.init()
#CONSTRUCT SHOOTING
# -----------------------------------------------------------------------------
out = X
for i in range(0,nstep):
[out] = rk4_step.eval([out,U])
Shoot = SXFunction([X,U],[out])
#Shoot = SXFunction([X,U],[X+dt*RHS]) #Invoke a 1st order Euler...
Shoot.init()
self._Shoot = Shoot
def _BuildFunc(self, Expr, Terminal):
X = self.States
U = self.Inputs
Uprev = self.InputsPrev
if Terminal == False:
listFuncInput = [U, Uprev]
if (X.size > 0):
listFuncInput.append(X)
else:
listFuncInput = [X]
if hasattr(self,'ExtParameters'):
listFuncInput.append(self.ExtParameters)
Func = SXFunction(listFuncInput,[Expr])
Func.init()
return Func
def setConstraints(self, Const, Terminal = False):
if (self._frozen == True):
print "Plant already added to the grid, call ignored"
return
if not(isinstance(Const,list)):
Const = [Const]
#ConstFunc = self._BuildFunc(veccat(Const), Terminal)
if (Terminal == False):
self._StageConst = self._BuildFunc(veccat(Const), Terminal)
elif (Terminal == True):
self._TerminalConst = self._BuildFunc(veccat(Const), Terminal)
def setCost(self, Cost, Terminal = False):
if (self._frozen == True):
print "Plant already added to the grid, call ignored"
return
#CostFunc = self._BuildFunc(Cost, Terminal)
if (Terminal == False):
self._StageCost = self._BuildFunc(Cost, Terminal)
elif (Terminal == True):
self._TerminalCost = self._BuildFunc(Cost, Terminal)
class PowerGrid:
"""
Generates:
- Power FLow equations and Power plants dynamics
- OPF solver
- optimal grid control solver
"""
def __init__(self, NBus = 0,Graph = []):
self.NBus = NBus
self.Graph = Graph
self.PlantList = []
self._hasStates = False
self.PowerFlowBounds = {'Vmin' : 0,
'Vmax' : inf,
'LineCurrentMax' : inf }
#CONSTRUCT POWER FLOW
def Flow(self, OPFSolver = False):
NBus = self.NBus
NLine = np.size( self.Graph ,axis = 0)
print "Constructing Power Flow Equations, #Bus =",NBus, ", #Line =",NLine
#CONSTRUCT THE POWER FLOW EQUATIONS
Graph = self.Graph
# Bus admittance matrix: Inodal_injection = Y*V (Nodal current injection)
Y = np.array([ [ 0.0*1j for i in range(NBus) ] for j in range(NBus) ])
for k in range(NLine):
Y[Graph[k][0],Graph[k][0]] += 1/Graph[k][2]
Y[Graph[k][1],Graph[k][1]] += 1/Graph[k][2]
Y[Graph[k][0],Graph[k][1]] -= 1/Graph[k][2]
Y[Graph[k][1],Graph[k][0]] -= 1/Graph[k][2]
# Line admittance matrix (directed): Iline = L*V
L = np.array([ [ 0.0*1j for i in range(NBus) ] for j in range(NLine) ])
for k in range(NLine):
L[k,Graph[k][0]] = 1/Graph[k][2]
L[k,Graph[k][1]] = -1/Graph[k][2]
######## BUILD POWER FLOW EQUATIONS (Results in Function: Bus voltage, Bus power -> Residual, to be satisfied at every time stage)
#Bus Voltages (real and complex parts)
BusVoltages = struct_ssym([entry("Real",repeat = NBus),
entry("Imag",repeat = NBus)])
#Bus currents (for current limitation) I = Y*V
BusCurrentsReal = mul(np.real(Y),BusVoltages["Real",veccat]) - mul(np.imag(Y),BusVoltages["Imag",veccat])
BusCurrentsImag = mul(np.real(Y),BusVoltages["Imag",veccat]) + mul(np.imag(Y),BusVoltages["Real",veccat])
BusCurrents2 = BusCurrentsReal*BusCurrentsReal + BusCurrentsImag*BusCurrentsImag
#Bus voltage modules square (for voltage limitation)
BusVoltages2 = BusVoltages["Real",veccat]*BusVoltages["Real",veccat] + BusVoltages["Imag",veccat]*BusVoltages["Imag",veccat]
#Line currents
LineCurrentsReal = mul(np.real(L),BusVoltages["Real",veccat]) - mul(np.imag(L),BusVoltages["Imag",veccat])
LineCurrentsImag = mul(np.real(L),BusVoltages["Imag",veccat]) + mul(np.imag(L),BusVoltages["Real",veccat])
LineCurrents2 = LineCurrentsReal*LineCurrentsReal + LineCurrentsImag*LineCurrentsImag
#Build complex power injections at the bus
SReal = BusCurrentsReal*BusVoltages["Real",veccat] + BusCurrentsImag*BusVoltages["Imag",veccat]
SImag = BusCurrentsReal*BusVoltages["Imag",veccat] - BusCurrentsImag*BusVoltages["Real",veccat]
#Create functions for Current and Voltages**2
self.BusActivePowerFunc = SXFunction([BusVoltages],[SReal])
self.BusReactivePowerFunc = SXFunction([BusVoltages],[SImag])
self.BusCurrentsRealFunc = SXFunction([BusVoltages],[BusCurrentsReal])
self.BusCurrentsImagFunc = SXFunction([BusVoltages],[BusCurrentsImag])
self.LineCurrents2Func = SXFunction([BusVoltages],[LineCurrents2])
self.BusVoltages2Func = SXFunction([BusVoltages],[BusVoltages2])
self.BusVoltages = BusVoltages
self.BusActivePowerFunc.init()
self.BusReactivePowerFunc.init()
self.BusCurrentsRealFunc.init()
self.BusCurrentsImagFunc.init()
self.LineCurrents2Func.init()
self.BusVoltages2Func.init()
self.OPF = True
#CONSTRUCT OPF SOLVER IS ASKED IN THE FLOW FUNCTION OPTIONS
if (OPFSolver == True):
print "Construct OPF Solver"
Power = struct_ssym([
entry('Active', repeat = NBus),
entry('Reactive', repeat = NBus)
])
V = struct_msym([
entry('BusPower', struct = Power),
entry('BusVoltages', struct = BusVoltages)
])
[BusActivePower] = self.BusActivePowerFunc.call([V['BusVoltages']])
[BusReactivePower] = self.BusReactivePowerFunc.call([V['BusVoltages']])
[LineCurrents2] = self.LineCurrents2Func.call([V['BusVoltages']])
[BusVoltages2] = self.BusVoltages2Func.call([V['BusVoltages']])
ActivePowerBalance = BusActivePower - V['BusPower','Active', veccat]
ReactivePowerBalance = BusReactivePower - V['BusPower','Reactive',veccat]
g = struct_MX([
entry('ActivePower', expr = ActivePowerBalance),
entry('ReactivePower', expr = ReactivePowerBalance),
entry('LineCurrents2', expr = LineCurrents2),
entry('BusVoltages2', expr = BusVoltages2)
])
Cost = 0
for line in range(NLine):
Cost += np.real(Graph[line][2])*LineCurrents2[line]
nl = MXFunction(nlpIn(x=V),nlpOut(f=Cost,g=g))
nl.init()
# set-up solver
solver = IpoptSolver(nl)
#solver.setOption("print_level",0)
solver.setOption("expand",True)
solver.setOption("parametric",False)
solver.setOption("generate_hessian",True)
solver.setOption("max_iter",1000)
solver.setOption("tol",1e-6)
solver.setOption("linear_solver","ma27")
solver.init()
Hessian = solver.hessLag()
Hessian.init()
Jacobian = solver.jacG()
Jacobian.init()
JacCost = solver.gradF()
JacCost.init()
self.VOPF = V
self.gOPF = g
self.OPF = solver
self._HessOPF = Hessian
self._JacOPF = Jacobian
self._JacCostOPF = JacCost
def OPFSolve(self, Grid = []):
lbV = self.VOPF(-inf)
ubV = self.VOPF( inf)
lbg = self.gOPF()
ubg = self.gOPF()
if not(hasattr(self,'OPF')):
#Check that .Flow() has been called
print "You must call .Flow(OPFSolver = True) to setup OPF before calling .OPFSolve()"
return []
if (self.OPF == True):
#Check that a solver exists
print "You must call .Flow(OPFSolver = True) to setup OPF before calling .OPFSolve()"
return []
#Set initial guess
init = self.VOPF()
init['BusVoltages','Real',veccat] = 1.
#Set the bounds (default values if not defined)
lbV = self.VOPF(-inf)
ubV = self.VOPF( inf)
lbg = self.gOPF()
ubg = self.gOPF()
#Ascertain the completness of the PowerFlowBounds dictionary, complete if necessary
if not('Vmin' in self.PowerFlowBounds.keys()):
self.PowerFlowBounds['Vmin'] = 0
print "Min Bus Voltage not provided, default value assigned (0)"
if not('Vmax' in self.PowerFlowBounds.keys()):
self.PowerFlowBounds['Vmax'] = inf
print "Max Bus Voltage not provided, default value assigned (inf)"
if not('LineCurrentMax' in self.PowerFlowBounds.keys()):
self.PowerFlowBounds['LineCurrentMax'] = inf
print "Max Line Current not provided, default value assigned (inf)"
ubg['LineCurrents2'] = np.array(self.PowerFlowBounds['LineCurrentMax'])**2
lbg['BusVoltages2'] = np.array(self.PowerFlowBounds['Vmin'])**2
ubg['BusVoltages2'] = np.array(self.PowerFlowBounds['Vmax'])**2
#Assign Network operational conditions
for entry in range(np.size(Grid)):
if (Grid[entry]['Property'] == 'slack'):
print "Bus", Grid[entry]['Bus'],"is slack"
lbg['BusVoltages2',Grid[entry]['Bus']] = Grid[entry]['V']**2
ubg['BusVoltages2',Grid[entry]['Bus']] = Grid[entry]['V']**2
lbV['BusVoltages','Imag',Grid[entry]['Bus']] = 0.0
ubV['BusVoltages','Imag',Grid[entry]['Bus']] = 0.0
elif (Grid[entry]['Property'] == 'PV'):
print "Bus", Grid[entry]['Bus'],"is PV"
lbg['BusVoltages2',Grid[entry]['Bus']] = Grid[entry]['V']**2
ubg['BusVoltages2',Grid[entry]['Bus']] = Grid[entry]['V']**2
lbV['BusPower','Active',Grid[entry]['Bus']] = Grid[entry]['P']
ubV['BusPower','Active',Grid[entry]['Bus']] = Grid[entry]['P']
elif (Grid[entry]['Property'] == 'PQ'):
print "Bus", Grid[entry]['Bus'],"is PQ"
lbV['BusPower','Active',Grid[entry]['Bus']] = Grid[entry]['P']
ubV['BusPower','Active',Grid[entry]['Bus']] = Grid[entry]['P']
lbV['BusPower','Reactive',Grid[entry]['Bus']] = Grid[entry]['Q']
ubV['BusPower','Reactive',Grid[entry]['Bus']] = Grid[entry]['Q']
self.OPF.setInput( lbV, "lbx")
self.OPF.setInput( ubV, "ubx")
self.OPF.setInput(init, "x0" )
self.OPF.setInput( lbg, "lbg")
self.OPF.setInput( ubg, "ubg")
self.OPF.solve()
self.lbg = lbg
self.ubg = ubg
self.lbV = lbV
self.ubV = ubV
return self.VOPF(self.OPF.output('x'))
########### POWER DISPACTH PROBLEM ##########
def addPlant(self, plant):
if isinstance(plant,list): #Treat list of plants
for plant_k in plant:
self.addPlant(plant_k)
else:
if (plant._frozen == True):
print "Plant already added to the grid, call ignored"
return
self.PlantList.append(plant)
plant._frozen = True
if hasattr(plant,'_Shoot'):
self._hasStates = True
def _VariableConstructor(self, N):
###### CONSTRUCT DECISION VARIABLES OF LENGTH N #######
List = []
for plant in self.PlantList:
List.append(entry(plant.label, struct = plant.Inputs))
Inputs = struct_ssym(List)
List = []
for plant in self.PlantList:
if (len(plant.States.keys()) > 0):
List.append(entry(plant.label, struct = plant.States))
States = struct_ssym(List)
#Structures to manipulate initial conditions and inputs
u0 = struct_msym(Inputs)
x0 = struct_msym(States)
#User-specified additional parameters
EPList = []
for plant in self.PlantList:
if hasattr(plant,'ExtParameters'):
EPList.append(entry(plant.label, struct = plant.ExtParameters))
ExtParameters = struct_msym(EPList)
EP = struct_msym([
entry('u0', struct = u0),
entry('ExtParameters', struct = ExtParameters)
])
Vlist = []
Vlist.append(entry("BusVoltages", repeat = N, struct = self.BusVoltages))
if (self._hasStates == True):
Vlist.append(entry("States", repeat = N+1, struct = States))
Vlist.append(entry("Inputs", repeat = N, struct = Inputs))
V = struct_msym(Vlist)
return V, u0, x0, EP, ExtParameters
def _CostConstructor(self, V, EP, Nstage, GridLoss):
"""
Constructor for the Cost function, handy to build the cost for different V:s
"""
Cost_Lagrange = 0
#Grid loss
if (GridLoss == True):
NLine = len( self.Graph )
for k in range(Nstage):
# Grid losses
[LineCurrents2_k] = self.LineCurrents2Func.call([V['BusVoltages',k]])
for line in range(NLine):
Cost_Lagrange += np.real(self.Graph[line][2])*LineCurrents2_k[line]
#Plants Lagrange cost
for plant in self.PlantList:
for k in range(Nstage):
if (hasattr(plant,'_StageCost')):
CostInputList = [V['Inputs',k,plant.label]]
if (k==0):
CostInputList.append( EP['u0',plant.label] )
else:
CostInputList.append( V['Inputs',k-1,plant.label])
if (plant.States.size > 0):
CostInputList.append(V['States',k,plant.label])
if hasattr(plant,'ExtParameters'):
CostInputList.append(EP['ExtParameters',plant.label])
[Cost_k] = plant._StageCost.call(CostInputList)
Cost_Lagrange += Cost_k
#Plants Terminal cost
Cost_Terminal = 0
for plant in self.PlantList:
if (hasattr(plant,'_TerminalCost')):
CostInputList = [V['States',-1,plant.label]]
if hasattr(plant,'ExtParameters'):
CostInputList.append(EP['ExtParameters',plant.label])
[Cost_k] = plant._TerminalCost.call(CostInputList)
Cost_Terminal += Cost_k
Cost = (Cost_Lagrange+Cost_Terminal)/Nstage
LagrangeCostFunc = MXFunction([V,EP],[Cost_Lagrange])
LagrangeCostFunc.init()
TerminalCostFunc = MXFunction([V,EP],[Cost_Terminal])
TerminalCostFunc.init()
return Cost, LagrangeCostFunc, TerminalCostFunc
def Dispatch(self, Horizon = 24, Simulation = 0, GridLoss = True):
"""
Constructs the power dispatch problem, default Horizon length (if argument Horizon is not provided) is 24 time units
"""
if (self.OPF == False):
Power.Flow(self)
print "Construct Dynamic OPF"
#THorizon = self.THorizon
Nstage = Horizon#self.TimeSetup['Horizon']
NBus = self.NBus
NLine = len( self.Graph )
TransferBus = []
BusProperties = []
for Bus in range(NBus):
Busk = 'transfer'
BuskProperties = []
for plant in self.PlantList:
if ( plant.Bus == Bus):
Busk = 'open'
BuskProperties.append(plant.label)
if (BuskProperties == []):
TransferBus.append(Bus)
BusProperties.append({Bus: BuskProperties})
################### CONSTRUCT VARIABLES ########################
V, u0, x0, EP, ExtParameters = self._VariableConstructor(Nstage)
#Structure for storing NMPC solutions if Nsim provided
if (Simulation > 0):
Vstore,_,_,_,_ = self._VariableConstructor(Simulation)
self.Vstore = Vstore()
############################### BUILD COST AND CONSTRAINTS ###############################
Cost, LagrangeCostFunc, TerminalCostFunc = self._CostConstructor(V, EP, Nstage, GridLoss)
if (Simulation > 0):
_, self.LagrangeCost, self.TerminalCost = self._CostConstructor(Vstore, EP, Simulation, GridLoss)
else:
self.LagrangeCost = LagrangeCostFunc
self.TerminalCost = TerminalCostFunc
# OPF constraints
CurrentBalance = []
LineCurrents2 = []
BusVoltages2 = []
# Generic constraints
PeriodicConst = []
EquConst = []
IneqConst = []
# Thermal constraints
ThermalConst = []
ThermalConstExt = []
######### BUILD COST & CONSTRAINTS #######
for k in range(Nstage): #k is reserved for time instant throughout the code
### CONSTRUCT POWER FLOW
#Construct (Bus Voltages)**2 and (Line current)**2 for bounding module
[LineCurrents2_k] = self.LineCurrents2Func.call([V['BusVoltages',k]])
[BusVoltages2_k] = self.BusVoltages2Func.call([V['BusVoltages',k]])
LineCurrents2.append(LineCurrents2_k)
BusVoltages2.append(BusVoltages2_k)
#Compute Bus Injection Currents
[CurrentsBalanceReal] = self.BusCurrentsRealFunc.call([V['BusVoltages',k]])
[CurrentsBalanceImag] = self.BusCurrentsImagFunc.call([V['BusVoltages',k]])
for plant in self.PlantList:
#Bus Voltage for the selected plant/load
BusVoltageReal = V['BusVoltages',k,'Real'][plant.Bus]
BusVoltageImag = V['BusVoltages',k,'Imag'][plant.Bus]
#Plant Current of the selected plant/load
PlantCurrentReal = V['Inputs',k,plant.label,'CurrentReal']
PlantCurrentImag = V['Inputs',k,plant.label,'CurrentImag']
# Balance the participating currents of the various plants and loads with
# the current injection @ the corresponding buses
CurrentsBalanceReal[plant.Bus] -= PlantCurrentReal
CurrentsBalanceImag[plant.Bus] -= PlantCurrentImag
# Re{V.iplant*} -> "Participating Active Power" // Im{V.iplant*} -> "Participating Reactive Power"
ParticipatingActivePower = BusVoltageReal*PlantCurrentReal + BusVoltageImag*PlantCurrentImag
ParticipatingReactivePower = BusVoltageImag*PlantCurrentReal - BusVoltageReal*PlantCurrentImag
# Plant participating current squared, i.e. |i|**2
PlantCurrent2 = PlantCurrentReal*PlantCurrentReal + PlantCurrentImag*PlantCurrentImag
if (plant._Load == True):
#Load fixing: [Active, Reactive] = Consumed Active / Reactive power
EquConst.append(ParticipatingActivePower - V['Inputs',k,plant.label,'ActivePower'])
EquConst.append(ParticipatingReactivePower - V['Inputs',k,plant.label,'ReactivePower'])
else:
if (plant.Directionality == 'Mono'):
PlantPower = V['Inputs',k,plant.label,'Power']
else:
PlantPower = V['Inputs',k,plant.label,'Pdischarge'] - V['Inputs',k,plant.label,'Pcharge']
#Compute balance between Pmech and participating power for each plant
# ParticipatingPower + R*|iplant|**2 - PlantPower = 0
EquConst.append(ParticipatingActivePower + plant.R*PlantCurrent2 - PlantPower)
CurrentBalance.append(CurrentsBalanceReal)
CurrentBalance.append(CurrentsBalanceImag)
### CONSTRUCT DYNAMIC CONSTRAINTS
for plant in self.PlantList:
if hasattr(plant,'_Shoot'):
[Xp] = plant._Shoot.call([V['States',k,plant.label],V['Inputs',k,plant.label]])
EquConst.append(Xp-V['States',k+1,plant.label])
#A bit ugly...
if hasattr(plant,'_StageConst'):
#print "Plant", plant.label, "has stage inequality constraints"
ConstInputList = [V['Inputs',k,plant.label]]
if (k==0):
ConstInputList.append( EP['u0',plant.label] )
else:
ConstInputList.append( V['Inputs',k-1,plant.label])
if (plant.States.size > 0):
ConstInputList.append(V['States',k,plant.label])
[Const_k] = plant._StageConst.call(ConstInputList)
IneqConst.append(Const_k)
### END OF STAGE CONSTRAINTS
for plant in self.PlantList:
if (hasattr(plant,'_TerminalConst')):
#print "Plant", plant.label, "has terminal inequality constraints"
[Const_k] = plant._TerminalConst.call([V['States',-1,plant.label]])
IneqConst.append(Const_k)
#### PERIODIC CONSTRAINTS
#PeriodicConst.append(V['States',-1,'Storage','Energy'] - V['States',0,'Storage','Energy'])
#PeriodicConst.append(V['States',-1,'Hydro','WaterHeight'] - V['States',0,'Hydro','WaterHeight'])
#PeriodicConst.append(V['Inputs',-1,'Thermal','Power'] - V['Inputs',0,'Thermal','Power'])
######## END CONSTRAINTS BUILDING ######
g = struct_MX([
entry("CurrentBalance", expr = CurrentBalance),
entry("BusVoltages2", expr = BusVoltages2),
entry("LineCurrents2", expr = LineCurrents2),
#entry('Periodic', expr = PeriodicConst),
entry('EquConst', expr = veccat(EquConst)),
entry('IneqConst', expr = veccat(IneqConst))
])
nl = MXFunction(nlpIn(x=V,p=EP),nlpOut(f=Cost,g=g))
nl.init()
# set-up solver
solver = IpoptSolver(nl)
solver.setOption("expand",True)
solver.setOption("print_level",0)
solver.setOption("parametric",True)
solver.setOption("hessian_approximation","exact")
solver.setOption("max_iter",2000)
solver.setOption("tol",1e-6)
solver.setOption("linear_solver","ma27")
solver.init()
Hessian = solver.hessLag()
Hessian.init()
Jacobian = solver.jacG()
Jacobian.init()
JacCost = solver.gradF()
JacCost.init()
self._HessOptDispatch = Hessian
self._JacOptDispatch = Jacobian
self._JacCostOptDispatch = JacCost
self.u0 = u0
self.x0 = x0
self.ExtParameters = ExtParameters
self._EP = EP
self.VOptDispatch = V
self.OptDispatch = solver
self.gOptDispatch = g
self.Properties = BusProperties
print self.Properties
############## SOLVER CONSTRUCTED ##############
#BUILD FIRST INITIAL GUESS
def init(self, x0 = [], u0 = []):
init = self.VOptDispatch()
NBus = self.NBus
NLine = len( self.Graph )
for plant in self.PlantList:
if hasattr(plant,'_Shoot'):
init['States',:,plant.label] = 0.5*(plant.LB['States'] + plant.UB['States'])
for index in range(init.size):
if not(init.cat[index] < inf):
init.cat[index] = 0.
for bus in range(NBus):
init['BusVoltages',:,'Real',bus] = 0.5*(self.PowerFlowBounds['Vmin'][bus]+self.PowerFlowBounds['Vmax'][bus])
init['BusVoltages',:,'Imag',bus] = 0.0
init['Inputs',:,...,'CurrentReal'] = 1.0
init['Inputs',:,...,'CurrentImag'] = 1.0
return init
def Profiles(self, N = 0):
"""
CREATE A STRUCTURE FOR HANDLING THE PROFILES OF THE POWER GRID:
If no argument passed, the profiles have the horizon length, if argument N is assigned, the profiles have the length of NSample
"""
if (N == 0):
if hasattr(self,'VOptDispatch'):
Nstage = len(self.VOptDispatch['Inputs'])
Nprofile = Nstage + 1
else:
print "Profile Error: cannot resolve the length of profile. Specify a horizon (N = ...) or call .Dispatch first"
return
else:
if hasattr(self,'VOptDispatch'):
Nstage = len(self.VOptDispatch['Inputs'])
Nprofile = N + Nstage + 1
else:
Nprofile = N + 1
self.Nprofile = Nprofile
VProfile,_,_,_,_ = self._VariableConstructor(self.Nprofile)
self.LBProfiles = VProfile()
self.UBProfiles = VProfile()
for plant in self.PlantList:
self.LBProfiles['Inputs',:,plant.label] = plant.LB['Inputs']
self.UBProfiles['Inputs',:,plant.label] = plant.UB['Inputs']
if hasattr(plant,'_Shoot'):
self.LBProfiles['States',:,plant.label] = plant.LB['States']
self.UBProfiles['States',:,plant.label] = plant.UB['States']
return Nprofile
#ASSIGN PROFILES & SOLVE
def DYNSolve(self, x0 = [], u0 = 0., ExtParameters = [], init = [], time = 0, Periodic = False):
lbV = self.VOptDispatch(-inf)
ubV = self.VOptDispatch( inf)
lbg = self.gOptDispatch()
ubg = self.gOptDispatch()
ubg["IneqConst"] = 0.
lbg["IneqConst"] = -inf
NBus = self.NBus
NLine = len( self.Graph )
####### SETUP THE BOUNDS #########
for plant in self.PlantList:
lbV['Inputs',:,plant.label] = self.LBProfiles['Inputs',time:,plant.label]
ubV['Inputs',:,plant.label] = self.UBProfiles['Inputs',time:,plant.label]
if hasattr(plant,'_Shoot'):
lbV['States',:,plant.label] = self.LBProfiles['States',time:,plant.label]
ubV['States',:,plant.label] = self.UBProfiles['States',time:,plant.label]
#Power flow limitations
lbg["BusVoltages2"] = np.array(self.PowerFlowBounds['Vmin'])**2
ubg["BusVoltages2"] = np.array(self.PowerFlowBounds['Vmax'])**2
ubg["LineCurrents2"] = np.array(self.PowerFlowBounds['LineCurrentMax'])**2
#Introduce additional bounds on all current and voltages (taken from Power flow limitation)
# Bus voltages
for bus in range(NBus):
ubV['BusVoltages',:,'Real',bus] = self.PowerFlowBounds['Vmax'][bus]
ubV['BusVoltages',:,'Imag',bus] = self.PowerFlowBounds['Vmax'][bus]
lbV['BusVoltages',:,'Real',bus] = -self.PowerFlowBounds['Vmax'][bus]
lbV['BusVoltages',:,'Imag',bus] = -self.PowerFlowBounds['Vmax'][bus]
ubV["BusVoltages",:,"Imag",0] = 0.
lbV["BusVoltages",:,"Imag",0] = 0.
###### EMBBED INITIAL CONDITIONS #######
if (self._hasStates == True):
print "Initial Condition embedding"
lbV['States',0] = x0
ubV['States',0] = x0
###### PERIODIC CONSTRAINTS (IF REQUIRED) #######
#if (Periodic == False):
# lbg['Periodic'] = -inf
# ubg['Periodic'] = inf
EP = self._EP()
if not(ExtParameters == []):
EP['ExtParameters'] = ExtParameters
EP['u0'] = u0
self.OptDispatch.setInput(lbV, "lbx")
self.OptDispatch.setInput(ubV, "ubx")
self.OptDispatch.setInput(init, "x0" )
self.OptDispatch.setInput(lbg, "lbg")
self.OptDispatch.setInput(ubg, "ubg")
self.OptDispatch.setInput(EP, "p")
self.OptDispatch.solve()
self.lbV = lbV
self.ubV = ubV
self.ubg = ubg
self.lbg = lbg
self.ep = EP
v_opt = self.VOptDispatch(self.OptDispatch.output("x"))
success = int(self.OptDispatch.getStat('return_status') == 'Solve_Succeeded')
return v_opt, success
def Shift(self, Sol):
SolShifted = self.VOptDispatch()
for key in Sol.keys():
Nelements = len(Sol[key])
IndexTime = [k for k in range( 1,Nelements ) ]
IndexTimePlus = [k for k in range( Nelements-1 ) ]
SolShifted[key,IndexTimePlus] = Sol[key,IndexTime]
SolShifted[key,-1] = Sol[key,-1]
return self.VOptDispatch(SolShifted)
def Simulate(self, Sol, x0, u0):
#To be replaced by a genuine simulation in the future...
x0plus = self.x0(Sol['States',1])
u0plus = self.u0(Sol['Inputs',0])
return x0plus, u0plus
def NMPCSimulation(self, x0 = [], u0 = [], ExtParameters = [], init = [], Simulation = 0):
##### NMPC Loop #####
NMPC = {'time': 0, 'success' : [], 'Traj' : []}
Vstore = self.Vstore
while (NMPC['time'] < Simulation):
Sol, stats = self.DYNSolve(x0 = x0, u0 = u0, ExtParameters = ExtParameters, time = NMPC['time'], init = init)
NMPC['success'].append(stats)
NMPC['Traj'].append(Sol)
Vstore[...,NMPC['time']] = Sol[...,0]
init = self.Shift(Sol)
x0, u0 = self.Simulate(Sol,x0, u0)
NMPC['time'] += 1
EP = self._EP()
EP['u0'] = u0
self.LagrangeCost.setInput(Vstore,0)
self.LagrangeCost.setInput(EP,1)
self.LagrangeCost.evaluate()
NMPC['LagrangeCost'] = self.LagrangeCost.output()
return Vstore, NMPC
#Extract results
def ExtractInfo(self, v_opt, BusPower = True, PlantPower = True, TotalPower = True):
self.SolutionInfo = {}
Nstage = len(v_opt['Inputs'])
NBus = self.NBus
NLine = len( self.Graph )
#DEFAULT EXTRACTION
#Bus voltages (module and angles), Line Currents
self.SolutionInfo['BusVoltagesModule'] = np.concatenate([np.array(np.sqrt(v_opt["BusVoltages",k,"Real",veccat]*v_opt["BusVoltages",k,"Real",veccat] + v_opt["BusVoltages",k,"Imag",veccat]*v_opt["BusVoltages",k,"Imag",veccat])).T for k in range(Nstage)],axis=0)
self.SolutionInfo['BusVoltagesAngle'] = np.concatenate([np.array([180*math.atan2(v_opt["BusVoltages",k,"Imag",bus],v_opt["BusVoltages",k,"Real",bus])/pi for bus in range(NBus)]).reshape(NBus,1) for k in range(Nstage)], axis = 1).T
LineCurrents_opt = []
for k in range(Nstage):
self.LineCurrents2Func.setInput(v_opt["BusVoltages",k])
self.LineCurrents2Func.evaluate()
LineCurrents_opt.append(sqrt(self.LineCurrents2Func.output()))