-
Notifications
You must be signed in to change notification settings - Fork 19
/
CIM1Converter.java
1487 lines (1358 loc) · 72 KB
/
CIM1Converter.java
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
/**
* Copyright (c) 2016, All partners of the iTesla project (http://www.itesla-project.eu/consortium)
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
*/
package eu.itesla_project.iidm.import_.cim1;
import com.google.common.collect.HashMultimap;
import com.google.common.collect.ImmutableMap;
import com.google.common.collect.Multimap;
import eu.itesla_project.iidm.network.*;
import eu.itesla_project.entsoe.util.EntsoeFileName;
import org.jgrapht.UndirectedGraph;
import org.jgrapht.alg.ConnectivityInspector;
import org.jgrapht.graph.Pseudograph;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.util.*;
import java.util.function.Supplier;
/**
* Building of an IIDM network model based on a CIM network model
*
* @author Olivier Bretteville <olivier.bretteville at rte-france.com>
*/
class CIM1Converter implements CIM1Constants {
private static final Logger LOGGER = LoggerFactory.getLogger(CIM1Converter.class);
private static final String CIM_ENTSOE_PROFILE_1ST_EDITION_VERSION = "IEC61970CIM14v02";
private static final String XNODE_V_PROPERTY = "xnode_v";
private static final String XNODE_ANGLE_PROPERTY = "xnode_angle";
private final cim1.model.CIMModel cimModel;
private final String fileName;
private final CIM1ConverterConfig config;
private final CIM1NamingStrategy namingStrategy;
private final Map<Bus, cim1.model.SvVoltage> svVoltages = new IdentityHashMap<>();
private final List<cim1.model.Switch> lowImpedanceLines = new ArrayList<>();
CIM1Converter(cim1.model.CIMModel cimModel, String fileName, CIM1ConverterConfig config) {
this.cimModel = Objects.requireNonNull(cimModel);
this.fileName = Objects.requireNonNull(fileName);
if (fileName.isEmpty()) {
throw new IllegalArgumentException("File name is null");
}
this.config = Objects.requireNonNull(config);
namingStrategy = config.getNamingStrategyFactory().create(cimModel);
}
private final Multimap<String, Terminal> terminalMapping = HashMultimap.create();
private final Map<cim1.model.TopologicalNode, cim1.model.EnergyConsumer> boundaryXNodes = new IdentityHashMap<>();
private final Map<cim1.model.TopologicalNode, List<cim1.model.ConductingEquipment>> mergedXNodes = new IdentityHashMap<>();
private void addTerminalMapping(cim1.model.TopologicalNode tn, Terminal t) {
terminalMapping.put(tn.getId(), t);
}
private Terminal getTerminalMapping(cim1.model.TopologicalNode tn) {
Collection<Terminal> terminals = terminalMapping.get(tn.getId());
if (terminals.isEmpty()) {
throw new CIM1Exception("Cannot find an IIDM terminal for CIM topological node "
+ tn.getId());
}
return terminals.iterator().next();
}
private static boolean isXNode(cim1.model.TopologicalNode tn) {
return tn.isFromBoundary() || tn.getConnectivityNodeContainer() == null;
}
private static String findUcteXnodeCode(cim1.model.TopologicalNode tn) {
// the xnode name is contained in the description field, starting
// from the letter X until ; character
int pos1 = tn.getDescription().indexOf('X');
int pos2 = tn.getDescription().indexOf(';');
if (pos1 == -1 || pos2 == -1) {
throw new CIM1Exception("Cannot find Xnode name from topological node description field '"
+ tn.getDescription() + "'");
}
return tn.getDescription().substring(pos1, pos2);
}
private static float[] getVoltageLimits(cim1.model.VoltageLevel vl, Set<String> noOperationalLimitInOperationalLimitSet) {
float lowVoltageLimit = Float.NaN;
float highVoltageLimit = Float.NaN;
if (vl.getTopologicalNode() != null) {
for (cim1.model.TopologicalNode tn : vl.getTopologicalNode()) {
if (tn.getTerminal() != null) {
for (cim1.model.Terminal t : tn.getTerminal()) {
if (t.getOperationalLimitSet() != null) {
for (cim1.model.OperationalLimitSet ols : t.getOperationalLimitSet()) {
if (ols.getOperationalLimitValue() == null) {
noOperationalLimitInOperationalLimitSet.add(ols.getId());
continue;
}
for (cim1.model.OperationalLimit ol : ols.getOperationalLimitValue()) {
cim1.model.OperationalLimitType olt = ol.getOperationalLimitType();
float value;
switch (olt.getName()) {
case "LowVoltage":
value = ((cim1.model.VoltageLimit) ol).getValue();
if (Float.isNaN(lowVoltageLimit)) {
lowVoltageLimit = value;
} else {
lowVoltageLimit = Math.min(lowVoltageLimit, value);
}
break;
case "HighVoltage":
value = ((cim1.model.VoltageLimit) ol).getValue();
if (Float.isNaN(highVoltageLimit)) {
highVoltageLimit = value;
} else {
highVoltageLimit = Math.max(highVoltageLimit, value);
}
break;
}
}
}
}
}
}
}
}
return new float[] {lowVoltageLimit, highVoltageLimit};
}
private static void createCurrentLimits(cim1.model.Terminal t, Supplier<CurrentLimitsAdder> owner, Set<String> noOperationalLimitInOperationalLimitSet) {
if (t.getOperationalLimitSet() != null) {
CurrentLimitsAdder cla = owner.get();
for (cim1.model.OperationalLimitSet ols : t.getOperationalLimitSet()) {
if (ols.getOperationalLimitValue() == null) {
noOperationalLimitInOperationalLimitSet.add(ols.getId());
continue;
}
for (cim1.model.OperationalLimit ol : ols.getOperationalLimitValue()) {
cim1.model.OperationalLimitType olt = ol.getOperationalLimitType();
if (ol instanceof cim1.model.CurrentLimit) {
float value = ((cim1.model.CurrentLimit) ol).getValue();
if (value <= 0) {
LOGGER.warn("Invalid current limit {} for {}", value, ols.getId());
} else {
switch (olt.getName()) {
case "PATL":
cla.setPermanentLimit(value);
break;
case "TATL":
if (olt.getDirection() != cim1.model.OperationalLimitDirectionKind.absoluteValue) {
throw new RuntimeException("Direction not supported " + olt.getDirection());
}
cla.beginTemporaryLimit()
.setLimit(value)
.setAcceptableDuration((int) olt.getAcceptableDuration())
.endTemporaryLimit();
}
}
}
}
}
cla.add();
}
}
private void createLine(Network network, cim1.model.ACLineSegment l, Set<String> noOperationalLimitInOperationalLimitSet) {
cim1.model.Terminal t1 = l.getTerminals().get(0);
cim1.model.Terminal t2 = l.getTerminals().get(1);
cim1.model.TopologicalNode tn1 = t1.getTopologicalNode();
cim1.model.TopologicalNode tn2 = t2.getTopologicalNode();
String voltageLevelId1 = namingStrategy.getId(tn1.getConnectivityNodeContainer());
String voltageLevelId2 = namingStrategy.getId(tn2.getConnectivityNodeContainer());
cim1.model.SvPowerFlow svpf1 = t1.getSvPowerFlow();
cim1.model.SvPowerFlow svpf2 = t2.getSvPowerFlow();
float r = l.getR();
float x = l.getX();
float b = l.getBch();
float g = l.getGch();
if (LOGGER.isTraceEnabled()) {
LOGGER.trace("Create line {} between bus {} and bus {}",
namingStrategy.getId(l), namingStrategy.getId(tn1), namingStrategy.getId(tn2));
}
final Line line = network.newLine()
.setId(namingStrategy.getId(l))
.setName(namingStrategy.getName(l))
.setEnsureIdUnicity(false)
.setBus1(t1.isConnected() ? namingStrategy.getId(tn1) : null)
.setBus2(t2.isConnected() ? namingStrategy.getId(tn2) : null)
.setConnectableBus1(namingStrategy.getId(tn1))
.setConnectableBus2(namingStrategy.getId(tn2))
.setVoltageLevel1(voltageLevelId1)
.setVoltageLevel2(voltageLevelId2)
.setR(r)
.setX(x)
.setG1(g / 2)
.setG2(g / 2)
.setB1(b / 2)
.setB2(b / 2)
.add();
addTerminalMapping(tn1, line.getTerminal1());
addTerminalMapping(tn2, line.getTerminal2());
createCurrentLimits(t1, line::newCurrentLimits1, noOperationalLimitInOperationalLimitSet);
createCurrentLimits(t2, line::newCurrentLimits2, noOperationalLimitInOperationalLimitSet);
if (svpf1 != null) {
line.getTerminal1().setP(svpf1.getP()).setQ(svpf1.getQ());
}
if (svpf2 != null) {
line.getTerminal2().setP(svpf2.getP()).setQ(svpf2.getQ());
}
}
/* xnode is on side 2 */
private void createDanglingLine(Network network, cim1.model.ACLineSegment l,
cim1.model.Terminal t1, cim1.model.Terminal t2,
cim1.model.TopologicalNode tn1, cim1.model.TopologicalNode tn2,
cim1.model.EnergyConsumer ec2,
Set<String> noOperationalLimitInOperationalLimitSet) {
assert isXNode(tn2) && !isXNode(tn1);
String voltageLevelId1 = namingStrategy.getId(tn1.getConnectivityNodeContainer());
VoltageLevel voltageLevel1 = network.getVoltageLevel(voltageLevelId1);
boolean ect2isConnected = true;
float p0 = 0;
float q0 = 0;
if (ec2 != null) {
cim1.model.Terminal ect2 = ec2.getTerminals().get(0);
cim1.model.SvPowerFlow ect2svpf = ect2.getSvPowerFlow();
ect2isConnected = ect2.isConnected();
p0 = ect2svpf.getP();
q0 = ect2svpf.getQ();
}
cim1.model.SvPowerFlow svpf1 = t1.getSvPowerFlow();
cim1.model.SvVoltage svv2 = tn2.getSvVoltage();
float r = l.getR();
float x = l.getX();
float b = l.getBch();
float g = l.getGch();
if (LOGGER.isTraceEnabled()) {
LOGGER.trace("Create dangling line {} connected to bus {}", namingStrategy.getId(l), namingStrategy.getId(tn1));
}
boolean connected = t1.isConnected() && t2.isConnected() && ect2isConnected;
DanglingLine dl = voltageLevel1.newDanglingLine()
.setId(namingStrategy.getId(l))
.setName(namingStrategy.getName(l))
.setEnsureIdUnicity(false)
.setBus(connected ? namingStrategy.getId(tn1) : null)
.setConnectableBus(namingStrategy.getId(tn1))
.setR(r)
.setX(x)
.setG(g)
.setB(b)
.setUcteXnodeCode(findUcteXnodeCode(tn2))
.setP0(p0)
.setQ0(q0)
.add();
addTerminalMapping(tn1, dl.getTerminal());
createCurrentLimits(t1, dl::newCurrentLimits, noOperationalLimitInOperationalLimitSet);
if (svpf1 != null) {
dl.getTerminal().setP(svpf1.getP()).setQ(svpf1.getQ());
}
// for debug only
if (svv2 != null) {
dl.getProperties().setProperty(XNODE_V_PROPERTY, Float.toString(svv2.getV()));
dl.getProperties().setProperty(XNODE_ANGLE_PROPERTY, Float.toString(svv2.getAngle()));
}
}
/**
* xnode is on side 2 of l1
*
* l1 l2
* *---------*---------*
* t1 xnode t2
* tn1 tn2
*/
private void createMergedLine(Network network,
cim1.model.ACLineSegment l1, cim1.model.ACLineSegment l2,
cim1.model.Terminal t1, cim1.model.TopologicalNode tn1,
cim1.model.TopologicalNode xn,
Set<String> noOperationalLimitInOperationalLimitSet) {
cim1.model.Terminal t2;
cim1.model.TopologicalNode tn2;
if (l2.getTerminals().get(0).getTopologicalNode() == xn) {
t2 = l2.getTerminals().get(1);
tn2 = t2.getTopologicalNode();
} else if (l2.getTerminals().get(1).getTopologicalNode() == xn) {
t2 = l2.getTerminals().get(0);
tn2 = t2.getTopologicalNode();
} else {
throw new AssertionError();
}
String voltageLevelId1 = namingStrategy.getId(tn1.getConnectivityNodeContainer());
String voltageLevelId2 = namingStrategy.getId(tn2.getConnectivityNodeContainer());
cim1.model.SvPowerFlow svpf1 = t1.getSvPowerFlow();
cim1.model.SvPowerFlow svpf2 = t2.getSvPowerFlow();
if (LOGGER.isTraceEnabled()) {
LOGGER.trace("Create merged line {} between bus {} and bus {}",
namingStrategy.getId(l1), namingStrategy.getId(tn1), namingStrategy.getId(xn));
}
Line line = network.newTieLine()
.setId(namingStrategy.getId(l1) + " + " + namingStrategy.getId(l2))
.setName(namingStrategy.getName(l1) + " + " + namingStrategy.getName(l2))
.setEnsureIdUnicity(false)
.setBus1(t1.isConnected() ? tn1.getId() : null)
.setBus2(t2.isConnected() ? tn2.getId() : null)
.setConnectableBus1(namingStrategy.getId(tn1))
.setConnectableBus2(namingStrategy.getId(tn2))
.setVoltageLevel1(voltageLevelId1)
.setVoltageLevel2(voltageLevelId2)
.line1().setId(namingStrategy.getId(l1))
.setName(namingStrategy.getName(l1))
.setR(l1.getR())
.setX(l1.getX())
.setG1(l1.getGch() / 2)
.setG2(l1.getGch() / 2)
.setB1(l1.getBch() / 2)
.setB2(l1.getBch() / 2)
.setXnodeP(0)
.setXnodeQ(0)
.line2().setId(namingStrategy.getId(l2))
.setName(namingStrategy.getName(l2))
.setR(l2.getR())
.setX(l2.getX())
.setG1(l2.getGch() / 2)
.setG2(l2.getGch() / 2)
.setB1(l2.getBch() / 2)
.setB2(l2.getBch() / 2)
.setXnodeP(0)
.setXnodeQ(0)
.setUcteXnodeCode(findUcteXnodeCode(xn))
.add();
addTerminalMapping(tn1, line.getTerminal1());
addTerminalMapping(tn2, line.getTerminal2());
createCurrentLimits(t1, line::newCurrentLimits1, noOperationalLimitInOperationalLimitSet);
createCurrentLimits(t2, line::newCurrentLimits2, noOperationalLimitInOperationalLimitSet);
if (svpf1 != null) {
line.getTerminal1().setP(svpf1.getP()).setQ(svpf1.getQ());
}
if (svpf2 != null) {
line.getTerminal2().setP(svpf2.getP()).setQ(svpf2.getQ());
}
}
private void createLines(Network network, Set<String> noOperationalLimitInOperationalLimitSet) {
for (cim1.model.ACLineSegment l : cimModel.getId_ACLineSegment().values()) {
cim1.model.Terminal t1 = l.getTerminals().get(0);
cim1.model.Terminal t2 = l.getTerminals().get(1);
cim1.model.TopologicalNode tn1 = t1.getTopologicalNode();
cim1.model.TopologicalNode tn2 = t2.getTopologicalNode();
if (isXNode(tn1)) {
if (boundaryXNodes.containsKey(tn1)) {
// side 1 of the line is connected to a XNODE. Only one load
// is connected to the XNODE => replace the line, the XNODE
// and the load by an IIDM dangling line
cim1.model.EnergyConsumer ec1 = boundaryXNodes.get(tn1);
createDanglingLine(network, l, t2, t1, tn2, tn1, ec1, noOperationalLimitInOperationalLimitSet);
}
// nothing to do because merged line has already been created
// by test on tn2
} else if (isXNode(tn2)) {
if (boundaryXNodes.containsKey(tn2)) {
// side 2 of the line is connected to a XNODE. Only one load
// is connected to the XNODE => replace the line, the XNODE
// and the load by an IIDM dangling line
cim1.model.EnergyConsumer ec2 = boundaryXNodes.get(tn2);
createDanglingLine(network, l, t1, t2, tn1, tn2, ec2, noOperationalLimitInOperationalLimitSet);
} else {
// side 2 of the line is connected to a XNODE. Another line
// is connected to the XNODE and no non null injection is
// connected to the XNODE => replace both lines and the XNODE
// by an IIDM line
List<cim1.model.ConductingEquipment> linesToMerge = mergedXNodes.get(tn2);
cim1.model.ACLineSegment l2 = (cim1.model.ACLineSegment) (linesToMerge.get(0) == l ? linesToMerge.get(1) : linesToMerge.get(0));
createMergedLine(network, l, l2, t1, tn1, tn2, noOperationalLimitInOperationalLimitSet);
}
} else {
createLine(network, l, noOperationalLimitInOperationalLimitSet);
}
}
}
private static float getStepXforAsymmetrical(float xStepMin, float xStepMax,
float alpha, float alphaMax, float theta) {
double numer = Math.sin(theta) - Math.tan (alphaMax) * Math.cos(theta);
double denom = Math.sin(theta) - Math.tan (alpha) * Math.cos(theta);
return xStepMin + (xStepMax - xStepMin)
* (float) Math.pow(Math.tan(alpha) / Math.tan(alphaMax) * numer / denom, 2);
}
private static float getStepXforSymmetrical(float xStepMin, float xStepMax,
float alpha, float alphaMax) {
return xStepMin + (xStepMax - xStepMin)
* (float) Math.pow(Math.sin(alpha / 2) / Math.sin(alphaMax / 2), 2);
}
private void createPhaseTapChanger(cim1.model.PhaseTapChanger ptc, cim1.model.Terminal t1, cim1.model.Terminal t2,
TwoWindingsTransformer transfo) {
int lowStep = ptc.getLowStep();
int highStep = ptc.getHighStep();
int neutralStep = ptc.getNeutralStep();
if (neutralStep < lowStep || neutralStep > highStep) {
throw new CIM1Exception("Malformed ratio tap changer: neutral step ("
+ neutralStep + ") isn't between low (" + lowStep + ") and high ("
+ highStep + ")");
}
int position = (int) ptc.svTapStep.getContinuousPosition();
PhaseTapChangerAdder ptca = transfo.newPhaseTapChanger()
.setLowTapPosition(lowStep)
.setTapPosition(position);
double du0 = ptc.neutralU / ptc.transformerWinding.ratedU;
if (Math.abs(du0) > 0.5) {
du0 = 0;
}
float du;
if (ptc.voltageStepIncrementOutOfPhaseIsSet() && ptc.getVoltageStepIncrementOutOfPhase() != 0) {
du = (config.isInvertVoltageStepIncrementOutOfPhase() ? -1 : 1) * ptc.getVoltageStepIncrementOutOfPhase() / ptc.getTransformerWinding().getRatedU();
} else if (ptc.stepVoltageIncrementIsSet() && ptc.getStepVoltageIncrement() != 0) {
du = ptc.getStepVoltageIncrement() / 100f;
} else {
LOGGER.warn("Phase tap changer '{}' of power transformer '{}'" +
" do not have a valid value for voltageStepIncrementOutOfPhase or " +
"stepVoltageIncrement attribute, default to 1",
ptc.getId(), transfo.getId());
du = 1f / 100;
}
float theta;
if (ptc.windingConnectionAngleIsSet()) {
theta = (float) Math.toRadians(ptc.getWindingConnectionAngle());
} else {
theta = (float) Math.PI / 2;
LOGGER.warn("Phase tap changer '{}' of power transformer '{}'" +
" do not have windingConnectionAngle attribute, default to 90",
ptc.getId(), transfo.getId());
}
float xStepMin = 0;
float xStepMax = 0;
if (ptc.xStepMinIsSet() && ptc.xStepMaxIsSet()) {
xStepMin = ptc.xStepMin;
xStepMax = ptc.xStepMax;
}
boolean xStepRangeIsInconsistent = false;
if (xStepMin < 0 || xStepMax <= 0 || xStepMin > xStepMax) {
xStepRangeIsInconsistent = true;
LOGGER.info("xStepMin and xStepMax are inconsistents for transformer {}", transfo.getId());
}
List<Float> alphaList = new ArrayList<>();
List<Float> rhoList = new ArrayList<>();
switch (ptc.getPhaseTapChangerType()) {
case asymmetrical: {
for (int step = lowStep; step <= highStep; step++) {
int n = step - neutralStep;
double dx = (n * du - du0) * Math.cos(theta);
double dy = (n * du - du0) * Math.sin(theta);
float alpha = (float) Math.atan2(dy, 1 + dx);
float rho = (float) (1 / Math.hypot(dy, 1 + dx));
alphaList.add(alpha);
rhoList.add(rho);
}
}
break;
case symmetrical:
if (ptc.stepPhaseShiftIncrementIsSet() && ptc.stepPhaseShiftIncrement != 0) {
for (int step = lowStep; step <= highStep; step++) {
int n = step - neutralStep;
float alpha = n * (float) Math.toRadians((config.isInvertVoltageStepIncrementOutOfPhase() ? -1 : 1) * ptc.stepPhaseShiftIncrement);
float rho = 1f;
alphaList.add(alpha);
rhoList.add(rho);
}
} else {
for (int step = lowStep; step <= highStep; step++) {
int n = step - neutralStep;
double dy = (n * du / 2 - du0) * Math.sin(theta);
float alpha = (float) (2 * Math.asin(dy));
float rho = 1f;
alphaList.add(alpha);
rhoList.add(rho);
}
}
break;
default:
throw new InternalError();
}
float alphaMax = (float) alphaList.stream().mapToDouble(Float::doubleValue).max().getAsDouble();
for (int i = 0 ; i < alphaList.size(); i++) {
float alpha = alphaList.get(i);
float rho = rhoList.get(i);
float x;
if (xStepRangeIsInconsistent || alphaMax == 0) {
x = transfo.getX();
} else {
switch (ptc.getPhaseTapChangerType()) {
case asymmetrical:
x = getStepXforAsymmetrical(xStepMin, xStepMax, alpha, alphaMax, theta);
break;
case symmetrical:
x = getStepXforSymmetrical(xStepMin, xStepMax, alpha, alphaMax);
break;
default:
throw new InternalError();
}
}
ptca.beginStep()
.setAlpha((float) Math.toDegrees(alpha))
.setRho(rho)
.setR(0f)
.setX((x - transfo.getX()) / transfo.getX() * 100)
.setG(0f)
.setB(0f)
.endStep();
}
if (ptc.regulatingControlIsSet()) {
cim1.model.RegulatingControl rc = ptc.getRegulatingControl();
switch (rc.getMode()) {
case currentFlow:
Terminal regulationTerminal;
if (rc.getTerminal() == t1) {
regulationTerminal = transfo.getTerminal1();
} else if (rc.getTerminal() == t2) {
regulationTerminal = transfo.getTerminal2();
} else {
regulationTerminal = getTerminalMapping(rc.getTerminal().getTopologicalNode());
}
ptca.setRegulationMode(PhaseTapChanger.RegulationMode.CURRENT_LIMITER)
.setRegulationValue(rc.getTargetValue())
.setRegulating(true)
.setRegulationTerminal(regulationTerminal);
break;
case fixed:
break;
default:
LOGGER.warn("Phase tap changer '{}' of power transformer '{}'" +
" has an unsupported regulating mode: {}",
ptc.getId(), transfo.getId(), rc.getMode());
}
}
ptca.add();
}
private void createRatioTapChanger(cim1.model.RatioTapChanger rtc, Supplier<RatioTapChangerAdder> transfo,
boolean rtcSide1, Map<cim1.model.Terminal, Terminal> terminals) {
int lowStep = rtc.getLowStep();
int highStep = rtc.getHighStep();
int neutralStep = rtc.getNeutralStep();
if (neutralStep < lowStep || neutralStep > highStep) {
throw new CIM1Exception("Malformed ratio tap changer: neutral step ("
+ neutralStep + ") isn't between low (" + lowStep + ") and high ("
+ highStep + ")");
}
int position = (int) rtc.svTapStep.getContinuousPosition();
RatioTapChangerAdder rtca = transfo.get()
.setLowTapPosition(lowStep)
.setTapPosition(position);
for (int step = lowStep; step <= highStep; step++) {
int n = step - neutralStep;
float du = rtc.getStepVoltageIncrement() / 100;
float rho = rtcSide1 ? 1 / (1 + n * du) : (1 + n * du);
rtca.beginStep()
.setRho(rho)
.setR(0f)
.setX(0f)
.setG(0f)
.setB(0f)
.endStep();
}
if (rtc.regulatingControlIsSet()) {
cim1.model.RegulatingControl rc = rtc.getRegulatingControl();
switch (rc.getMode()) {
case voltage:
boolean regulating = true;
float targetV = rc.getTargetValue();
if (targetV <= 0) {
LOGGER.warn("Ratio tap changer '{}' of power transformer '{}'" +
" has a bad target voltage {}, switch off regulation",
rtc.getId(), transfo.toString(), targetV);
regulating = false;
targetV = Float.NaN;
}
Terminal regulationTerminal = null;
for (Map.Entry<cim1.model.Terminal, Terminal> e : terminals.entrySet()) {
if (rc.getTerminal() == e.getKey()) {
regulationTerminal = e.getValue();
}
}
if (regulationTerminal == null) {
regulationTerminal = getTerminalMapping(rc.getTerminal().getTopologicalNode());
}
rtca.setLoadTapChangingCapabilities(true)
.setRegulating(regulating)
.setTargetV(targetV)
.setRegulationTerminal(regulationTerminal);
break;
case fixed:
rtca.setLoadTapChangingCapabilities(false);
break;
default:
rtca.setLoadTapChangingCapabilities(false);
LOGGER.warn("Ratio tap changer '{}' of power transformer '{}'" +
" has an unsupported regulating mode: {}",
rtc.getId(), transfo.toString(), rc.getMode());
}
} else {
rtca.setLoadTapChangingCapabilities(false);
}
rtca.add();
}
class RatioTapChangerToCreate {
cim1.model.RatioTapChanger rtc;
RatioTapChangerHolder transfo;
boolean rtcSide1;
Map<cim1.model.Terminal, Terminal> terminals;
RatioTapChangerToCreate(cim1.model.RatioTapChanger rtc, RatioTapChangerHolder transfo, boolean rtcSide1, Map<cim1.model.Terminal, Terminal> terminals) {
this.rtc = rtc;
this.rtcSide1 = rtcSide1;
this.transfo = transfo;
this.terminals = terminals;
}
}
private void create2WTransfos(cim1.model.PowerTransformer pt,
cim1.model.TransformerWinding tw1,
cim1.model.TransformerWinding tw2,
Network network,
Set<String> noOperationalLimitInOperationalLimitSet,
List<RatioTapChangerToCreate> ratioTapChangerToCreateList) {
cim1.model.Terminal t1 = tw1.getTerminals().get(0);
cim1.model.Terminal t2 = tw2.getTerminals().get(0);
cim1.model.TopologicalNode tn1 = t1.getTopologicalNode();
cim1.model.TopologicalNode tn2 = t2.getTopologicalNode();
cim1.model.VoltageLevel vl1 = (cim1.model.VoltageLevel) tn1.getConnectivityNodeContainer();
cim1.model.VoltageLevel vl2 = (cim1.model.VoltageLevel) tn2.getConnectivityNodeContainer();
// check that there is only one ratio tap changer
TwoWindingsTransformer.Side rtcSide = null;
cim1.model.RatioTapChanger rtc = null;
cim1.model.PhaseTapChanger ptc = null;
if (tw1.ratioTapChangerIsSet()) {
if (tw2.ratioTapChangerIsSet()) {
throw new CIM1Exception("Unsupported modelling: transformer with two ratio tap changer"
+ pt.getId());
}
rtc = tw1.getRatioTapChanger();
rtcSide = TwoTerminalsConnectable.Side.ONE;
} else {
if (tw2.ratioTapChangerIsSet()) {
rtc = tw2.getRatioTapChanger();
rtcSide = TwoTerminalsConnectable.Side.TWO;
}
}
// check that there is only one phase tap changer
TwoWindingsTransformer.Side ptcSide = null;
if (tw1.phaseTapChangerIsSet()) {
if (tw2.phaseTapChangerIsSet()) {
throw new CIM1Exception("Unsupported modelling: transformer with two phase tap changer"
+ pt.getId());
}
ptc = tw1.getPhaseTapChanger();
ptcSide = TwoTerminalsConnectable.Side.ONE;
} else {
if (tw2.phaseTapChangerIsSet()) {
ptc = tw2.getPhaseTapChanger();
ptcSide = TwoTerminalsConnectable.Side.TWO;
}
}
if (rtcSide != null && ptcSide != null && rtcSide != ptcSide) {
throw new CIM1Exception("Unsupported modelling: transformer with ratio and tap changer not on the same winding "
+ pt.getId());
}
if (isXNode(tn1)) {
throw new CIM1Exception("Not supported: transformer '" + pt.getId() + "' connected to XNODE "
+ tn1.getId() + "(" + findUcteXnodeCode(tn1) + ")");
} else if (isXNode(tn2)) {
throw new CIM1Exception("Not supported: transformer '" + pt.getId() + "' connected to XNODE "
+ tn2.getId() + "(" + findUcteXnodeCode(tn2) + ")");
} else {
// IIDM 2 windings transformer modelling: impedances are specfied at
// side 2, in this case at the secondary voltage side.
float rho0 = tw2.getRatedU() / tw1.getRatedU();
float rho0Square = rho0 * rho0;
float r0 = tw1.getR() * rho0Square + tw2.getR();
float x0 = tw1.getX() * rho0Square + tw2.getX();
float g0 = tw1.getG() / rho0Square + tw2.getG();
float b0 = tw1.getB() / rho0Square + tw2.getB();
VoltageLevel voltageLevel1 = network.getVoltageLevel(namingStrategy.getId(vl1));
Substation substation = voltageLevel1.getSubstation();
TwoWindingsTransformer transfo = substation.newTwoWindingsTransformer()
.setId(namingStrategy.getId(pt))
.setName(namingStrategy.getName(pt))
.setEnsureIdUnicity(false)
.setR(r0)
.setX(x0)
.setG(g0)
.setB(b0)
.setRatedU1(tw1.getRatedU())
.setBus1(t1.isConnected() ? namingStrategy.getId(tn1) : null)
.setConnectableBus1(namingStrategy.getId(tn1))
.setVoltageLevel1(namingStrategy.getId(vl1))
.setRatedU2(tw2.getRatedU())
.setBus2(t2.isConnected() ? namingStrategy.getId(tn2) : null)
.setConnectableBus2(namingStrategy.getId(tn2))
.setVoltageLevel2(namingStrategy.getId(vl2))
.add();
addTerminalMapping(tn1, transfo.getTerminal1());
addTerminalMapping(tn2, transfo.getTerminal2());
if (rtc != null) {
ratioTapChangerToCreateList.add(new RatioTapChangerToCreate(rtc, transfo, rtcSide == TwoTerminalsConnectable.Side.ONE,
ImmutableMap.of(t1, transfo.getTerminal1(),
t2, transfo.getTerminal2())));
}
if (ptc != null) {
createPhaseTapChanger(ptc, t1, t2, transfo);
}
createCurrentLimits(t1, transfo::newCurrentLimits1, noOperationalLimitInOperationalLimitSet);
createCurrentLimits(t2, transfo::newCurrentLimits2, noOperationalLimitInOperationalLimitSet);
cim1.model.SvPowerFlow svpf1 = t1.getSvPowerFlow();
cim1.model.SvPowerFlow svpf2 = t2.getSvPowerFlow();
if (svpf1 != null) {
transfo.getTerminal1().setP(svpf1.getP()).setQ(svpf1.getQ());
}
if (svpf2 != null) {
transfo.getTerminal2().setP(svpf2.getP()).setQ(svpf2.getQ());
}
}
}
private void create3WTransfos(cim1.model.PowerTransformer pt,
cim1.model.TransformerWinding tw1,
cim1.model.TransformerWinding tw2,
cim1.model.TransformerWinding tw3,
Network network,
Set<String> noOperationalLimitInOperationalLimitSet,
List<RatioTapChangerToCreate> ratioTapChangerToCreateList) {
// side 1 is the high voltage, side 2 is the medium voltage, side 3
// is the low voltage
if (tw1.getRatedU() < tw2.getRatedU()) {
throw new IllegalStateException("ratedU1 < ratedU2");
}
if (tw2.getRatedU() < tw3.getRatedU()) {
throw new IllegalStateException("ratedU2 < ratedU3");
}
cim1.model.Terminal t1 = tw1.getTerminals().get(0);
cim1.model.Terminal t2 = tw2.getTerminals().get(0);
cim1.model.Terminal t3 = tw3.getTerminals().get(0);
cim1.model.TopologicalNode tn1 = t1.getTopologicalNode();
cim1.model.TopologicalNode tn2 = t2.getTopologicalNode();
cim1.model.TopologicalNode tn3 = t3.getTopologicalNode();
cim1.model.VoltageLevel vl1 = (cim1.model.VoltageLevel) tn1.getConnectivityNodeContainer();
cim1.model.VoltageLevel vl2 = (cim1.model.VoltageLevel) tn2.getConnectivityNodeContainer();
cim1.model.VoltageLevel vl3 = (cim1.model.VoltageLevel) tn3.getConnectivityNodeContainer();
if (isXNode(tn1) || isXNode(tn2) || isXNode(tn3)) {
throw new CIM1Exception("XNODE connected to a 3 windings transformer not supported");
}
if (tw1.ratioTapChangerIsSet()) {
LOGGER.warn("Power transformer " + pt.getId() + ": ratio tap changer on primary winding is not supported");
}
if (tw1.phaseTapChangerIsSet() || tw2.phaseTapChangerIsSet() || tw3.phaseTapChangerIsSet()) {
throw new CIM1Exception("Power transformer " + pt.getId() + ": phase tap changers on three windings transformer are not allowed");
}
cim1.model.RatioTapChanger rtc2 = tw2.getRatioTapChanger();
cim1.model.RatioTapChanger rtc3 = tw3.getRatioTapChanger();
VoltageLevel voltageLevel1 = network.getVoltageLevel(namingStrategy.getId(vl1));
Substation substation = voltageLevel1.getSubstation();
float ratedU1 = tw1.getRatedU();
float ratedU2 = tw2.getRatedU();
float ratedU3 = tw3.getRatedU();
float rho2Square = (float) Math.pow(tw2.getRatedU() / tw1.getRatedU(), 2);
float rho3Square = (float) Math.pow(tw3.getRatedU() / tw1.getRatedU(), 2);
float r1 = tw1.getR();
float x1 = tw1.getX();
float g1 = tw1.getG() + tw2.getG() * rho2Square + tw3.getG() * rho3Square;
float b1 = tw1.getB() + tw2.getB() * rho2Square + tw3.getB() * rho3Square;
float r2 = tw2.getR() / rho2Square;
float x2 = tw2.getX() / rho2Square;
float r3 = tw3.getR() / rho3Square;
float x3 = tw3.getX() / rho3Square;
ThreeWindingsTransformer transfo = substation.newThreeWindingsTransformer()
.setId(namingStrategy.getId(pt))
.setName(namingStrategy.getName(pt))
.setEnsureIdUnicity(false)
.newLeg1()
.setR(r1)
.setX(x1)
.setG(g1)
.setB(b1)
.setRatedU(ratedU1)
.setVoltageLevel(namingStrategy.getId(vl1))
.setBus(t1.isConnected() ? namingStrategy.getId(tn1) : null)
.setConnectableBus(namingStrategy.getId(tn1))
.add()
.newLeg2()
.setR(r2)
.setX(x2)
.setRatedU(ratedU2)
.setVoltageLevel(namingStrategy.getId(vl2))
.setBus(t2.isConnected() ? namingStrategy.getId(tn2) : null)
.setConnectableBus(namingStrategy.getId(tn2))
.add()
.newLeg3()
.setR(r3)
.setX(x3)
.setRatedU(ratedU3)
.setVoltageLevel(namingStrategy.getId(vl3))
.setBus(t3.isConnected() ? namingStrategy.getId(tn3) : null)
.setConnectableBus(namingStrategy.getId(tn3))
.add()
.add();
addTerminalMapping(tn1, transfo.getLeg1().getTerminal());
addTerminalMapping(tn2, transfo.getLeg2().getTerminal());
addTerminalMapping(tn3, transfo.getLeg3().getTerminal());
if (rtc2 != null) {
ratioTapChangerToCreateList.add(new RatioTapChangerToCreate(rtc2, transfo.getLeg2(), false,
ImmutableMap.of(t2, transfo.getLeg2().getTerminal()))); // TODO true/false?
}
if (rtc3 != null) {
ratioTapChangerToCreateList.add(new RatioTapChangerToCreate(rtc3, transfo.getLeg3(), false,
ImmutableMap.of(t3, transfo.getLeg3().getTerminal()))); // TODO true/false?
}
createCurrentLimits(t1, transfo.getLeg1()::newCurrentLimits, noOperationalLimitInOperationalLimitSet);
createCurrentLimits(t2, transfo.getLeg2()::newCurrentLimits, noOperationalLimitInOperationalLimitSet);
createCurrentLimits(t3, transfo.getLeg3()::newCurrentLimits, noOperationalLimitInOperationalLimitSet);
cim1.model.SvPowerFlow svpf1 = t1.getSvPowerFlow();
if (svpf1 != null) {
transfo.getLeg1().getTerminal().setP(svpf1.getP()).setQ(svpf1.getQ());
}
cim1.model.SvPowerFlow svpf2 = t2.getSvPowerFlow();
if (svpf2 != null) {
transfo.getLeg2().getTerminal().setP(svpf2.getP()).setQ(svpf2.getQ());
}
cim1.model.SvPowerFlow svpf3 = t3.getSvPowerFlow();
if (svpf3 != null) {
transfo.getLeg3().getTerminal().setP(svpf3.getP()).setQ(svpf3.getQ());
}
}
private void createTransfos(Network network, Set<String> noOperationalLimitInOperationalLimitSet, List<RatioTapChangerToCreate> ratioTapChangerToCreateList) {
for (cim1.model.PowerTransformer pt : cimModel.getId_PowerTransformer().values()) {
LOGGER.trace("Create power transformer {}", namingStrategy.getId(pt));
List<cim1.model.TransformerWinding> windings = pt.getContains_TransformerWindings();
if (windings.size() == 2) {
cim1.model.TransformerWinding tw1 = windings.get(0);
cim1.model.TransformerWinding tw2 = windings.get(1);
create2WTransfos(pt, tw1, tw2, network, noOperationalLimitInOperationalLimitSet, ratioTapChangerToCreateList);
} else if (windings.size() == 3) {
List<cim1.model.TransformerWinding> sortedWindings = new ArrayList<>(3);
sortedWindings.add(windings.get(0));
sortedWindings.add(windings.get(1));
sortedWindings.add(windings.get(2));
Collections.sort(sortedWindings, new Comparator<cim1.model.TransformerWinding>() {
@Override
public int compare(cim1.model.TransformerWinding tw1, cim1.model.TransformerWinding tw2) {
return (int) (tw2.getRatedU() - tw1.getRatedU());
}
});
cim1.model.TransformerWinding tw1 = sortedWindings.get(0);
cim1.model.TransformerWinding tw2 = sortedWindings.get(1);
cim1.model.TransformerWinding tw3 = sortedWindings.get(2);
create3WTransfos(pt, tw1, tw2, tw3, network, noOperationalLimitInOperationalLimitSet, ratioTapChangerToCreateList);
} else {
throw new CIM1Exception("Inconsistent power transformer found ("
+ pt.getId() + "): only 2 or 3 windings are supported");
}
}
}
private void createShunt(VoltageLevel voltageLevel, cim1.model.ShuntCompensator sc) {
LOGGER.trace("Create shunt compensator {}", namingStrategy.getId(sc));
cim1.model.Terminal t = sc.getTerminals().get(0);
cim1.model.TopologicalNode tn = t.getTopologicalNode();
int sectionCount = (int) sc.getSvShuntCompensatorSections().getContinuousSections();
sectionCount = Math.abs(sectionCount); // RTE Convergence CIM export bug (SVC)
float bPerSection = sc.getBPerSection();
if (bPerSection == 0) {
bPerSection = Float.MIN_VALUE;
LOGGER.warn("Fix {} susceptance per section: 0 -> {}", sc.getId(), bPerSection);
}
ShuntCompensator shunt = voltageLevel.newShunt()
.setId(namingStrategy.getId(sc))
.setName(namingStrategy.getName(sc))
.setEnsureIdUnicity(false)
.setBus(t.isConnected() ? namingStrategy.getId(tn) : null)
.setConnectableBus(namingStrategy.getId(tn))
.setCurrentSectionCount(sectionCount)
.setbPerSection(bPerSection)
.setMaximumSectionCount(sc.getMaximumSections())
.add();
addTerminalMapping(tn, shunt.getTerminal());
cim1.model.SvPowerFlow svfp = t.getSvPowerFlow();
if (svfp != null) {
shunt.getTerminal().setQ(svfp.getQ());
}
}
private void createLoad(VoltageLevel voltageLevel, cim1.model.EnergyConsumer ec) {
cim1.model.Terminal t = ec.getTerminals().get(0);
cim1.model.TopologicalNode tn = t.getTopologicalNode();
// We take P and Q from SvPowerFlow as nominal P and nominal Q,
// we don't deal with LoadResponseCharacteristic
float p = 0;
float q = 0;
cim1.model.SvPowerFlow svpf = t.getSvPowerFlow();
if (svpf != null) {
p = svpf.getP();
q = svpf.getQ();
} else {
LOGGER.warn("No active and reactive power value for load {}", ec.getId());
}
LoadType loadType = ec.getId().contains("fict") ? LoadType.FICTITIOUS : LoadType.UNDEFINED;
LOGGER.trace("Create load {}", namingStrategy.getId(ec));
Load load = voltageLevel.newLoad()
.setId(namingStrategy.getId(ec))
.setName(namingStrategy.getName(ec))
.setEnsureIdUnicity(false)
.setBus(t.isConnected() ? namingStrategy.getId(tn) : null)
.setConnectableBus(namingStrategy.getId(tn))
.setP0(p)
.setQ0(q)
.setLoadType(loadType)
.add();
addTerminalMapping(tn, load.getTerminal());
if (svpf != null) {
load.getTerminal().setP(p).setQ(q);
}
}
private static EnergySource getEnergySource(cim1.model.GeneratingUnit gu) {
EnergySource es = EnergySource.OTHER;
if (gu instanceof cim1.model.HydroGeneratingUnit) {
es = EnergySource.HYDRO;
} else if (gu instanceof cim1.model.NuclearGeneratingUnit) {
es = EnergySource.NUCLEAR;
} else if (gu instanceof cim1.model.ThermalGeneratingUnit) {
es = EnergySource.THERMAL;
} else if (gu instanceof cim1.model.WindGeneratingUnit) {
es = EnergySource.WIND;
}
return es;
}
private void createReactiveCapabilityCurve(Generator generator, cim1.model.SynchronousMachine sm, List<String> synchronousMachinesWithReactiveRangeForMinus9999MW) {