77import networkx as nx
88import pandas as pd
99from collections import defaultdict
10- from pyomo .environ import Constraint , Objective , minimize , Set , RangeSet , Param
10+ from pyomo .environ import Constraint , Expression , Objective , minimize , Set , RangeSet , Param
1111
1212
1313def TotalObjectiveFunction (OptModel , mTEPES , pIndLogConsole ):
@@ -522,15 +522,9 @@ def eESSReserveDwIfEnergy(OptModel,n,es):
522522
523523 def eBalanceElec (OptModel ,n ,nd ):
524524 if sum (1 for g in g2n [nd ]) + sum (1 for nf ,cc in lout [nd ]) + sum (1 for ni ,cc in lin [nd ]):
525- if mTEPES .pIndPTDF == 0 :
526- return (sum (OptModel .vTotalOutput [p ,sc ,n ,g ] for g in g2n [nd ] if (p ,g ) in mTEPES .pg ) - sum (OptModel .vESSTotalCharge [p ,sc ,n ,eh ] for eh in e2n [nd ] if (p ,eh ) in mTEPES .peh ) + OptModel .vENS [p ,sc ,n ,nd ] -
527- sum (OptModel .vLineLosses [p ,sc ,n ,nd ,nf ,cc ] for nf ,cc in loutl [nd ] if (p ,nd ,nf ,cc ) in mTEPES .pll ) - sum (OptModel .vFlowElec [p ,sc ,n ,nd ,nf ,cc ] for nf ,cc in lout [nd ] if (p ,nd ,nf ,cc ) in mTEPES .pla ) -
528- sum (OptModel .vLineLosses [p ,sc ,n ,ni ,nd ,cc ] for ni ,cc in linl [nd ] if (p ,ni ,nd ,cc ) in mTEPES .pll ) + sum (OptModel .vFlowElec [p ,sc ,n ,ni ,nd ,cc ] for ni ,cc in lin [nd ] if (p ,ni ,nd ,cc ) in mTEPES .pla )) == mTEPES .pDemandElec [p ,sc ,n ,nd ]
529- else :
530- return (mTEPES .vNetPosition [p ,sc ,n ,nd ] == sum (OptModel .vTotalOutput [p ,sc ,n ,g ] for g in g2n [nd ] if (p ,g ) in mTEPES .pg ) - sum (OptModel .vESSTotalCharge [p ,sc ,n ,eh ] for eh in e2n [nd ] if (p ,eh ) in mTEPES .peh ) + OptModel .vENS [p ,sc ,n ,nd ] -
531- sum (OptModel .vLineLosses [p ,sc ,n ,nd ,nf ,cc ] for nf ,cc in loutl [nd ] if (p ,nd ,nf ,cc ) in mTEPES .pll ) - sum (OptModel .vFlowElec [p ,sc ,n ,nd ,nf ,cc ] for nf ,cc in lout [nd ] if (p ,nd ,nf ,cc ) in mTEPES .pla ) -
532- sum (OptModel .vLineLosses [p ,sc ,n ,ni ,nd ,cc ] for ni ,cc in linl [nd ] if (p ,ni ,nd ,cc ) in mTEPES .pll ) + sum (OptModel .vFlowElec [p ,sc ,n ,ni ,nd ,cc ] for ni ,cc in lin [nd ] if (p ,ni ,nd ,cc ) in mTEPES .pla ) -
533- mTEPES .pDemandElec [p ,sc ,n ,nd ])
525+ return (sum (OptModel .vTotalOutput [p ,sc ,n ,g ] for g in g2n [nd ] if (p ,g ) in mTEPES .pg ) - sum (OptModel .vESSTotalCharge [p ,sc ,n ,eh ] for eh in e2n [nd ] if (p ,eh ) in mTEPES .peh ) + OptModel .vENS [p ,sc ,n ,nd ] -
526+ sum (OptModel .vLineLosses [p ,sc ,n ,nd ,nf ,cc ] for nf ,cc in loutl [nd ] if (p ,nd ,nf ,cc ) in mTEPES .pll ) - sum (OptModel .vFlowElec [p ,sc ,n ,nd ,nf ,cc ] for nf ,cc in lout [nd ] if (p ,nd ,nf ,cc ) in mTEPES .pla ) -
527+ sum (OptModel .vLineLosses [p ,sc ,n ,ni ,nd ,cc ] for ni ,cc in linl [nd ] if (p ,ni ,nd ,cc ) in mTEPES .pll ) + sum (OptModel .vFlowElec [p ,sc ,n ,ni ,nd ,cc ] for ni ,cc in lin [nd ] if (p ,ni ,nd ,cc ) in mTEPES .pla )) == mTEPES .pDemandElec [p ,sc ,n ,nd ]
534528 else :
535529 return Constraint .Skip
536530 setattr (OptModel , f'eBalanceElec_{ p } _{ sc } _{ st } ' , Constraint (mTEPES .n , mTEPES .nd , rule = eBalanceElec , doc = 'electric load generation balance [GW]' ))
@@ -1375,12 +1369,29 @@ def eLineLosses2(OptModel,n,ni,nf,cc):
13751369 if pIndLogConsole == 1 :
13761370 print ('eLineLosses2 ... ' , len (getattr (OptModel , f'eLineLosses2_{ p } _{ sc } _{ st } ' )), ' rows' )
13771371
1372+ # nodes to generators (g2n)
1373+ g2n = defaultdict (list )
1374+ for nd ,g in mTEPES .n2g :
1375+ g2n [nd ].append (g )
1376+ e2n = defaultdict (list )
1377+ for nd ,eh in mTEPES .nd * mTEPES .eh :
1378+ if (nd ,eh ) in mTEPES .n2g :
1379+ e2n [nd ].append (eh )
1380+
1381+ def eNetPosition (OptModel ,n ,nd ):
1382+ if mTEPES .pIndBinSingleNode () == 0 and mTEPES .pIndPTDF == 1 :
1383+ """Net position NP_n = Σ P_g in node n − demand"""
1384+ return (OptModel .vNetPosition [p ,sc ,n ,nd ] == sum (OptModel .vTotalOutput [p ,sc ,n ,g ] for g in g2n [nd ] if (p ,g ) in mTEPES .pg ) - sum (OptModel .vESSTotalCharge [p ,sc ,n ,eh ] for eh in e2n [nd ] if (p ,eh ) in mTEPES .peh ) + OptModel .vENS [p ,sc ,n ,nd ] - mTEPES .pDemandElec [p ,sc ,n ,nd ])
1385+ else :
1386+ return Constraint .Skip
1387+ setattr (OptModel , f'eNetPosition_{ p } _{ sc } _{ st } ' , Constraint (mTEPES .n , mTEPES .nd , rule = eNetPosition , doc = 'net position [GW]' ))
1388+
13781389 def eFlowBasedCalcu1 (OptModel ,n ,ni ,nf ,cc ):
1379- if mTEPES .pIndBinSingleNode () == 0 and mTEPES .pElecNetPeriodIni [ni ,nf ,cc ] <= p and mTEPES .pElecNetPeriodFin [ni ,nf ,cc ] >= p and mTEPES .pIndPTDF == 1 :
1390+ if mTEPES .pIndBinSingleNode () == 0 and mTEPES .pElecNetPeriodIni [ni ,nf ,cc ] <= p and mTEPES .pElecNetPeriodFin [ni ,nf ,cc ] >= p and mTEPES .pIndPTDF == 1 and mTEPES . pIndBinLinePTDF [ ni , nf , cc ] == 1 :
13801391 if (ni ,nf ,cc ) in mTEPES .lca :
1381- return OptModel .vFlowElec [p ,sc ,n ,ni ,nf ,cc ] - sum (mTEPES .pPTDF [p ,sc ,n ,ni ,nf ,cc ,nd ] * OptModel .vNetPosition [p ,sc ,n ,nd ] for nd in mTEPES .nd ) >= - 1 + OptModel .vLineCommit [p ,sc ,n ,ni ,nf ,cc ]
1392+ return OptModel .vFlowElec [p ,sc ,n ,ni ,nf ,cc ] - sum (mTEPES .pPTDF [p ,sc ,n ,ni ,nf ,cc ,nd ] * OptModel .vNetPosition [p ,sc ,n ,nd ] for nd in mTEPES .nd if ( p , sc , n , ni , nf , cc , nd ) in mTEPES . psnland ) >= - 1 + OptModel .vLineCommit [p ,sc ,n ,ni ,nf ,cc ]
13821393 else :
1383- return OptModel .vFlowElec [p ,sc ,n ,ni ,nf ,cc ] - sum (mTEPES .pPTDF [p ,sc ,n ,ni ,nf ,cc ,nd ] * OptModel .vNetPosition [p ,sc ,n ,nd ] for nd in mTEPES .nd ) == 0
1394+ return OptModel .vFlowElec [p ,sc ,n ,ni ,nf ,cc ] - sum (mTEPES .pPTDF [p ,sc ,n ,ni ,nf ,cc ,nd ] * OptModel .vNetPosition [p ,sc ,n ,nd ] for nd in mTEPES .nd if ( p , sc , n , ni , nf , cc , nd ) in mTEPES . psnland ) == 0
13841395 else :
13851396 return Constraint .Skip
13861397 setattr (OptModel , f'eFlowBasedCalcu1_{ p } _{ sc } _{ st } ' , Constraint (mTEPES .n , mTEPES .laa , rule = eFlowBasedCalcu1 , doc = 'flow based calculation [p.u.]' ))
@@ -1389,34 +1400,34 @@ def eFlowBasedCalcu1(OptModel,n,ni,nf,cc):
13891400 print ('eFlowBasedCalcu1 ... ' , len (getattr (OptModel , f'eFlowBasedCalcu1_{ p } _{ sc } _{ st } ' )), ' rows' )
13901401
13911402 def eFlowBasedCalcu2 (OptModel ,n ,ni ,nf ,cc ):
1392- if mTEPES .pIndBinSingleNode () == 0 and mTEPES .pElecNetPeriodIni [ni ,nf ,cc ] <= p and mTEPES .pElecNetPeriodFin [ni ,nf ,cc ] >= p and mTEPES .pIndPTDF == 1 :
1393- return OptModel .vFlowElec [p ,sc ,n ,ni ,nf ,cc ] - sum (mTEPES .pPTDF [p ,sc ,n ,ni ,nf ,cc ,nd ] * OptModel .vNetPosition [p ,sc ,n ,nd ] for nd in mTEPES .nd ) <= 1 - OptModel .vLineCommit [p ,sc ,n ,ni ,nf ,cc ]
1403+ if mTEPES .pIndBinSingleNode () == 0 and mTEPES .pElecNetPeriodIni [ni ,nf ,cc ] <= p and mTEPES .pElecNetPeriodFin [ni ,nf ,cc ] >= p and mTEPES .pIndPTDF == 1 and mTEPES . pIndBinLinePTDF [ ni , nf , cc ] == 1 :
1404+ return OptModel .vFlowElec [p ,sc ,n ,ni ,nf ,cc ] - sum (mTEPES .pPTDF [p ,sc ,n ,ni ,nf ,cc ,nd ] * OptModel .vNetPosition [p ,sc ,n ,nd ] for nd in mTEPES .nd if ( p , sc , n , ni , nf , cc , nd ) in mTEPES . psnland ) <= 1 - OptModel .vLineCommit [p ,sc ,n ,ni ,nf ,cc ]
13941405 else :
13951406 return Constraint .Skip
13961407 setattr (OptModel , f'eFlowBasedCalcu2_{ p } _{ sc } _{ st } ' , Constraint (mTEPES .n , mTEPES .lca , rule = eFlowBasedCalcu2 , doc = 'flow based calculation [p.u.]' ))
13971408
13981409 if pIndLogConsole == 1 :
13991410 print ('eFlowBasedCalcu2 ... ' , len (getattr (OptModel , f'eFlowBasedCalcu2_{ p } _{ sc } _{ st } ' )), ' rows' )
14001411
1401- def eSecurityMargingTTCFrw (OptModel ,n ,ni ,nf ,cc ):
1402- if mTEPES .pIndBinSingleNode () == 0 and mTEPES .pElecNetPeriodIni [ni ,nf ,cc ] <= p and mTEPES .pElecNetPeriodFin [ni ,nf ,cc ] >= p and mTEPES .pIndPTDF == 1 :
1403- return OptModel .vFlowElec [p ,sc ,n ,ni ,nf ,cc ] <= mTEPES .pVariableTTCFrw [p ,sc ,n ,ni ,nf ,cc ]
1404- else :
1405- return Constraint .Skip
1406- setattr (OptModel , f'eSecurityMargingTTCFrw_{ p } _{ sc } _{ st } ' , Constraint (mTEPES .n , mTEPES .lca , rule = eSecurityMargingTTCFrw , doc = 'security margin TTC for flow based calculation [p.u.]' ))
1407-
1408- if pIndLogConsole == 1 :
1409- print ('eSecurityMargingTTCFrw... ' , len (getattr (OptModel , f'eSecurityMargingTTCFrw_{ p } _{ sc } _{ st } ' )), ' rows' )
1410-
1411- def eSecurityMargingTTCBck (OptModel ,n ,ni ,nf ,cc ):
1412- if mTEPES .pIndBinSingleNode () == 0 and mTEPES .pElecNetPeriodIni [ni ,nf ,cc ] <= p and mTEPES .pElecNetPeriodFin [ni ,nf ,cc ] >= p and mTEPES .pIndPTDF == 1 :
1413- return OptModel .vFlowElec [p ,sc ,n ,ni ,nf ,cc ] >= - mTEPES .pVariableTTCBck [p ,sc ,n ,ni ,nf ,cc ]
1414- else :
1415- return Constraint .Skip
1416- setattr (OptModel , f'eSecurityMargingTTCBck_{ p } _{ sc } _{ st } ' , Constraint (mTEPES .n , mTEPES .lca , rule = eSecurityMargingTTCBck , doc = 'security margin TTC for flow based calculation [p.u.]' ))
1417-
1418- if pIndLogConsole == 1 :
1419- print ('eSecurityMargingTTCBck... ' , len (getattr (OptModel , f'eSecurityMargingTTCBck_{ p } _{ sc } _{ st } ' )), ' rows' )
1412+ # def eSecurityMargingTTCFrw(OptModel,n,ni,nf,cc):
1413+ # if mTEPES.pIndBinSingleNode() == 0 and mTEPES.pElecNetPeriodIni[ni,nf,cc] <= p and mTEPES.pElecNetPeriodFin[ni,nf,cc] >= p and mTEPES.pIndBinLinePTDF[ni,nf,cc] == 1 and mTEPES.pIndPTDF == 1:
1414+ # return OptModel.vFlowElec[p,sc,n,ni,nf,cc] <= mTEPES.pVariableTTCFrw[p,sc,n,ni,nf,cc]
1415+ # else:
1416+ # return Constraint.Skip
1417+ # setattr(OptModel, f'eSecurityMargingTTCFrw_{p}_{sc}_{st}', Constraint(mTEPES.n, mTEPES.lca, rule=eSecurityMargingTTCFrw, doc='security margin TTC for flow based calculation [p.u.]'))
1418+ #
1419+ # if pIndLogConsole == 1:
1420+ # print('eSecurityMargingTTCFrw... ', len(getattr(OptModel, f'eSecurityMargingTTCFrw_{p}_{sc}_{st}')), ' rows')
1421+ #
1422+ # def eSecurityMargingTTCBck(OptModel,n,ni,nf,cc):
1423+ # if mTEPES.pIndBinSingleNode() == 0 and mTEPES.pElecNetPeriodIni[ni,nf,cc] <= p and mTEPES.pElecNetPeriodFin[ni,nf,cc] >= p and mTEPES.pIndBinLinePTDF[ni,nf,cc] == 1 and mTEPES.pIndPTDF == 1:
1424+ # return OptModel.vFlowElec[p,sc,n,ni,nf,cc] >= - mTEPES.pVariableTTCBck[p,sc,n,ni,nf,cc]
1425+ # else:
1426+ # return Constraint.Skip
1427+ # setattr(OptModel, f'eSecurityMargingTTCBck_{p}_{sc}_{st}', Constraint(mTEPES.n, mTEPES.lca, rule=eSecurityMargingTTCBck, doc='security margin TTC for flow based calculation [p.u.]'))
1428+ #
1429+ # if pIndLogConsole == 1:
1430+ # print('eSecurityMargingTTCBck... ', len(getattr(OptModel, f'eSecurityMargingTTCBck_{p}_{sc}_{st}')), ' rows')
14201431
14211432 GeneratingTime = time .time () - StartTime
14221433 if pIndLogConsole == 1 :
0 commit comments