-
Notifications
You must be signed in to change notification settings - Fork 19
/
AmplNetworkWriter.java
1442 lines (1383 loc) · 68.2 KB
/
AmplNetworkWriter.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)
* Copyright (c) 2016-2017, RTE (http://www.rte-france.com)
* 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.export.ampl;
import com.powsybl.commons.util.StringToIntMapper;
import com.powsybl.commons.datasource.DataSource;
import com.powsybl.commons.io.table.Column;
import com.powsybl.commons.io.table.TableFormatter;
import eu.itesla_project.iidm.export.ampl.util.AmplDatTableFormatter;
import com.powsybl.iidm.network.*;
import com.powsybl.iidm.network.CurrentLimits.TemporaryLimit;
import com.powsybl.iidm.network.util.ConnectedComponents;
import com.powsybl.iidm.network.util.SV;
import eu.itesla_project.merge.MergeUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.io.Writer;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
/**
*
* @author Geoffroy Jamgotchian <geoffroy.jamgotchian at rte-france.com>
*/
public class AmplNetworkWriter implements AmplConstants {
private static final Logger LOGGER = LoggerFactory.getLogger(AmplNetworkWriter.class);
private static final String XNODE_COUNTRY_NAME = "XNODE";
private final Network network;
private final int faultNum;
private final int actionNum;
private final DataSource dataSource;
private final boolean append;
private final StringToIntMapper<AmplSubset> mapper; // id mapper
private final AmplExportConfig config;
private static class AmplExportContext {
private int otherCcNum = Integer.MAX_VALUE;
public final Set<String> busIdsToExport = new HashSet<>();
public final Set<String> voltageLevelIdsToExport = new HashSet<>();
public final Set<String> generatorIdsToExport = new HashSet<>();
public final Set<String> loadsToExport = new HashSet<>();
}
public AmplNetworkWriter(Network network, DataSource dataSource, int faultNum, int actionNum,
boolean append, StringToIntMapper<AmplSubset> mapper, AmplExportConfig config) {
this.network = network;
this.faultNum = faultNum;
this.actionNum = actionNum;
this.dataSource = dataSource;
this.append = append;
this.mapper = mapper;
this.config = config;
}
public AmplNetworkWriter(Network network, DataSource dataSource, StringToIntMapper<AmplSubset> mapper,
AmplExportConfig config) {
this(network, dataSource, 0, 0, false, mapper, config);
}
public AmplNetworkWriter(Network network, DataSource dataSource, AmplExportConfig config) {
this(network, dataSource, 0, 0, false, AmplUtil.createMapper(network), config);
}
public static String getTableTitle(Network network, String tableName) {
return tableName + " (" + network.getId() + "/" + network.getStateManager().getWorkingStateId() + ")";
}
private String getTableTitle(String tableName) {
return getTableTitle(network, tableName);
}
private static String getThreeWindingsTransformerMiddleBusId(ThreeWindingsTransformer twt) {
return twt.getId(); // same id as the transformer
}
private static String getThreeWindingsTransformerMiddleVoltageLevelId(ThreeWindingsTransformer twt) {
return twt.getId(); // same id as the transformer
}
private static String getDanglingLineMiddleBusId(DanglingLine dl) {
return dl.getId(); // same id as the dangling line
}
private static String getDanglingLineMiddleVoltageLevelId(DanglingLine dl) {
return dl.getId(); // same id as the dangling line
}
private void writeSubstations(AmplExportContext context) throws IOException {
try (Writer writer = new OutputStreamWriter(dataSource.newOutputStream("_network_substations", "txt", append), StandardCharsets.UTF_8);
TableFormatter formatter = new AmplDatTableFormatter(
writer,
getTableTitle("Substations"),
INVALID_FLOAT_VALUE,
!append,
LOCALE,
new Column("num"),
new Column("horizon"),
new Column("reference date distance (minutes)"),
new Column("nomV (KV)"),
new Column("minV (pu)"),
new Column("maxV (pu)"),
new Column("fault"),
new Column(config.getActionType().getLabel()),
new Column("country"),
new Column("id"),
new Column("description"))) {
for (VoltageLevel vl : network.getVoltageLevels()) {
// if (!context.voltageLevelIdsToExport.contains(vl.getId())) {
// continue;
// }
int num = mapper.getInt(AmplSubset.VOLTAGE_LEVEL, vl.getId());
float nomV = vl.getNominalV();
float minV = vl.getLowVoltageLimit() / nomV;
float maxV = vl.getHighVoltageLimit() / nomV;
formatter.writeCell(num)
.writeCell(MergeUtil.getHorizon(vl))
.writeCell(MergeUtil.getDateDistanceToReference(vl))
.writeCell(nomV)
.writeCell(minV)
.writeCell(maxV)
.writeCell(faultNum)
.writeCell(actionNum)
.writeCell(vl.getSubstation().getCountry().toString())
.writeCell(vl.getId())
.writeCell(vl.getName());
}
// voltage level associated to 3 windings transformers middle bus
for (ThreeWindingsTransformer twt : network.getThreeWindingsTransformers()) {
String vlId = getThreeWindingsTransformerMiddleVoltageLevelId(twt);
// if (!context.voltageLevelIdsToExport.contains(vlId)) {
// continue;
// }
int num = mapper.getInt(AmplSubset.VOLTAGE_LEVEL, vlId);
Terminal t1 = twt.getLeg1().getTerminal();
VoltageLevel vl1 = t1.getVoltageLevel();
formatter.writeCell(num)
.writeCell(MergeUtil.getHorizon(vl1))
.writeCell(MergeUtil.getDateDistanceToReference(vl1))
.writeCell(vl1.getNominalV())
.writeCell(Float.NaN)
.writeCell(Float.NaN)
.writeCell(faultNum)
.writeCell(actionNum)
.writeCell(vl1.getSubstation().getCountry().toString())
.writeCell(vlId)
.writeEmptyCell();
}
// voltage level associated to dangling lines middle bus
for (DanglingLine dl : network.getDanglingLines()) {
String vlId = getDanglingLineMiddleVoltageLevelId(dl);
// if (!context.voltageLevelIdsToExport.contains(vlId)) {
// continue;
// }
int num = mapper.getInt(AmplSubset.VOLTAGE_LEVEL, vlId);
VoltageLevel vl = dl.getTerminal().getVoltageLevel();
float nomV = vl.getNominalV();
float minV = vl.getLowVoltageLimit() / nomV;
float maxV = vl.getHighVoltageLimit() / nomV;
formatter.writeCell(num)
.writeCell(MergeUtil.getHorizon(vl))
.writeCell(MergeUtil.getDateDistanceToReference(vl))
.writeCell(nomV)
.writeCell(minV)
.writeCell(maxV)
.writeCell(faultNum)
.writeCell(actionNum)
.writeCell(vl.getSubstation().getCountry().toString())
.writeCell(dl.getId() + "_voltageLevel")
.writeEmptyCell();
}
if (config.isExportXNodes()) {
for (Line l : network.getLines()) {
if (!l.isTieLine()) {
continue;
}
TieLine tieLine = (TieLine) l;
String vlId = AmplUtil.getXnodeVoltageLevelId(tieLine);
int num = mapper.getInt(AmplSubset.VOLTAGE_LEVEL, vlId);
formatter.writeCell(num)
.writeCell(MergeUtil.getHorizon(tieLine.getTerminal1().getVoltageLevel()))
.writeCell(MergeUtil.getDateDistanceToReference(tieLine.getTerminal1().getVoltageLevel()))
.writeCell(l.getTerminal1().getVoltageLevel().getNominalV())
.writeCell(Float.NaN)
.writeCell(Float.NaN)
.writeCell(faultNum)
.writeCell(actionNum)
.writeCell(XNODE_COUNTRY_NAME)
.writeCell(AmplUtil.getXnodeBusId(tieLine) + "_voltageLevel")
.writeEmptyCell();
}
}
}
}
private boolean isOnlyMainCc() {
switch (config.getExportScope()) {
case ONLY_MAIN_CC:
case ONLY_MAIN_CC_AND_CONNECTABLE_GENERATORS_AND_SHUNTS:
case ONLY_MAIN_CC_AND_CONNECTABLE_GENERATORS_AND_SHUNTS_AND_ALL_LOADS:
return true;
case ALL:
return false;
default:
throw new AssertionError();
}
}
private void writeBuses(AmplExportContext context) throws IOException {
try (Writer writer = new OutputStreamWriter(dataSource.newOutputStream("_network_buses", "txt", append), StandardCharsets.UTF_8);
TableFormatter formatter = new AmplDatTableFormatter(
writer,
getTableTitle("Buses"),
INVALID_FLOAT_VALUE,
!append,
LOCALE,
new Column("num"),
new Column("substation"),
new Column("cc"),
new Column("v (pu)"),
new Column("theta (rad)"),
new Column("p (MW)"),
new Column("q (MVar)"),
new Column("fault"),
new Column(config.getActionType().getLabel()),
new Column("id"))) {
for (Bus b : AmplUtil.getBuses(network)) {
int ccNum = ConnectedComponents.getCcNum(b);
// skip buses not in the main connected component
if (isOnlyMainCc() && ccNum != Component.MAIN_NUM) {
continue;
}
String id = b.getId();
VoltageLevel vl = b.getVoltageLevel();
context.busIdsToExport.add(id);
int num = mapper.getInt(AmplSubset.BUS, id);
int vlNum = mapper.getInt(AmplSubset.VOLTAGE_LEVEL, vl.getId());
float nomV = vl.getNominalV();
float v = b.getV() / nomV;
float theta = (float) Math.toRadians(b.getAngle());
formatter.writeCell(num)
.writeCell(vlNum)
.writeCell(ccNum)
.writeCell(v)
.writeCell(theta)
.writeCell(b.getP())
.writeCell(b.getQ())
.writeCell(faultNum)
.writeCell(actionNum)
.writeCell(id);
}
// 3 windings transformers middle bus
for (ThreeWindingsTransformer twt : network.getThreeWindingsTransformers()) {
Terminal t1 = twt.getLeg1().getTerminal();
Terminal t2 = twt.getLeg2().getTerminal();
Terminal t3 = twt.getLeg3().getTerminal();
Bus b1 = AmplUtil.getBus(t1);
Bus b2 = AmplUtil.getBus(t2);
Bus b3 = AmplUtil.getBus(t3);
int middleCcNum;
if (b1 != null) {
middleCcNum = ConnectedComponents.getCcNum(b1);
} else if (b2 != null) {
middleCcNum = ConnectedComponents.getCcNum(b2);
} else if (b3 != null) {
middleCcNum = ConnectedComponents.getCcNum(b3);
} else {
middleCcNum = context.otherCcNum--;
}
// skip buses not in the main connected component
if (isOnlyMainCc() && middleCcNum != Component.MAIN_NUM) {
continue;
}
String middleBusId = getThreeWindingsTransformerMiddleBusId(twt);
String middleVlId = getThreeWindingsTransformerMiddleVoltageLevelId(twt);
context.busIdsToExport.add(middleBusId);
int middleBusNum = mapper.getInt(AmplSubset.BUS, middleBusId);
int middleVlNum = mapper.getInt(AmplSubset.VOLTAGE_LEVEL, middleVlId);
formatter.writeCell(middleBusNum)
.writeCell(middleVlNum)
.writeCell(middleCcNum)
.writeCell(Float.NaN)
.writeCell(Float.NaN)
.writeCell(0f)
.writeCell(0f)
.writeCell(faultNum)
.writeCell(actionNum)
.writeCell(middleBusId);
}
// dangling line middle bus
for (DanglingLine dl : network.getDanglingLines()) {
Terminal t = dl.getTerminal();
Bus b = AmplUtil.getBus(t);
int middleCcNum;
// if the connection bus of the dangling line is null or not in the main cc, the middle bus is
// obviously not in the main cc
if (b != null) {
middleCcNum = ConnectedComponents.getCcNum(b);
} else {
middleCcNum = context.otherCcNum--;
}
// skip buses not in the main connected component
if (isOnlyMainCc() && middleCcNum != Component.MAIN_NUM) {
continue;
}
String middleBusId = getDanglingLineMiddleBusId(dl);
String middleVlId = getDanglingLineMiddleVoltageLevelId(dl);
context.busIdsToExport.add(middleBusId);
int middleBusNum = mapper.getInt(AmplSubset.BUS, middleBusId);
int middleVlNum = mapper.getInt(AmplSubset.VOLTAGE_LEVEL, middleVlId);
SV sv = new SV(t.getP(), t.getQ(), b != null ? b.getV() : Float.NaN, b != null ? b.getAngle() : Float.NaN).otherSide(dl);
float nomV = t.getVoltageLevel().getNominalV();
float v = sv.getU() / nomV;
float theta = (float) Math.toRadians(sv.getA());
formatter.writeCell(middleBusNum)
.writeCell(middleVlNum)
.writeCell(middleCcNum)
.writeCell(v)
.writeCell(theta)
.writeCell(0f) // 0 MW injected at dangling line internal bus
.writeCell(0f) // 0 MVar injected at dangling line internal bus
.writeCell(faultNum)
.writeCell(actionNum)
.writeCell(middleBusId);
}
if (config.isExportXNodes()) {
for (Line l : network.getLines()) {
if (!l.isTieLine()) {
continue;
}
TieLine tieLine = (TieLine) l;
Terminal t1 = tieLine.getTerminal1();
Terminal t2 = tieLine.getTerminal2();
Bus b1 = AmplUtil.getBus(t1);
Bus b2 = AmplUtil.getBus(t2);
int xNodeCcNum;
if (b1 != null) {
xNodeCcNum = ConnectedComponents.getCcNum(b1);
} else if (b2 != null) {
xNodeCcNum = ConnectedComponents.getCcNum(b2);
} else {
xNodeCcNum = context.otherCcNum--;
}
// skip buses not in the main connected component
if (isOnlyMainCc() && xNodeCcNum != Component.MAIN_NUM) {
continue;
}
String xNodeBusId = AmplUtil.getXnodeBusId(tieLine);
int xNodeBusNum = mapper.getInt(AmplSubset.BUS, xNodeBusId);
int xNodeVlNum = mapper.getInt(AmplSubset.VOLTAGE_LEVEL, AmplUtil.getXnodeVoltageLevelId(tieLine));
formatter.writeCell(xNodeBusNum)
.writeCell(xNodeVlNum)
.writeCell(xNodeCcNum)
.writeCell(Float.NaN)
.writeCell(Float.NaN)
.writeCell(0f)
.writeCell(0f)
.writeCell(faultNum)
.writeCell(actionNum)
.writeCell(xNodeBusId);
}
}
}
}
private static float getPermanentLimit(CurrentLimits limits) {
if (limits != null) {
return limits.getPermanentLimit();
}
return Float.NaN;
}
private static boolean isBusExported(AmplExportContext context, String busId) {
return busId != null && context.busIdsToExport.contains(busId);
}
private void writeBranches(AmplExportContext context) throws IOException {
try (Writer writer = new OutputStreamWriter(dataSource.newOutputStream("_network_branches", "txt", append), StandardCharsets.UTF_8);
TableFormatter formatter = new AmplDatTableFormatter(
writer,
getTableTitle("Branches"),
INVALID_FLOAT_VALUE,
!append,
LOCALE,
new Column("num"),
new Column("bus1"),
new Column("bus2"),
new Column("3wt num"),
new Column("sub.1"),
new Column("sub.2"),
new Column("r (pu)"),
new Column("x (pu)"),
new Column("g1 (pu)"),
new Column("g2 (pu)"),
new Column("b1 (pu)"),
new Column("b2 (pu)"),
new Column("cst ratio (pu)"),
new Column("ratio tc"),
new Column("phase tc"),
new Column("p1 (MW)"),
new Column("p2 (MW)"),
new Column("q1 (MVar)"),
new Column("q2 (MVar)"),
new Column("patl1 (A)"),
new Column("patl2 (A)"),
new Column("merged"),
new Column("fault"),
new Column(config.getActionType().getLabel()),
new Column("id"),
new Column("description"))) {
for (Line l : network.getLines()) {
Terminal t1 = l.getTerminal1();
Terminal t2 = l.getTerminal2();
Bus bus1 = AmplUtil.getBus(t1);
Bus bus2 = AmplUtil.getBus(t2);
if (bus1 != null && bus2 != null && bus1 == bus2) {
LOGGER.warn("Skipping line '{}' connected to the same bus at both sides", l.getId());
continue;
}
String bus1Id = null;
int bus1Num = -1;
if (bus1 != null) {
bus1Id = bus1.getId();
bus1Num = mapper.getInt(AmplSubset.BUS, bus1.getId());
}
String bus2Id = null;
int bus2Num = -1;
if (bus2 != null) {
bus2Id = bus2.getId();
bus2Num = mapper.getInt(AmplSubset.BUS, bus2.getId());
}
if (isOnlyMainCc() && !(isBusExported(context, bus1Id) || isBusExported(context, bus2Id))) {
continue;
}
VoltageLevel vl1 = t1.getVoltageLevel();
VoltageLevel vl2 = t2.getVoltageLevel();
context.voltageLevelIdsToExport.add(vl1.getId());
context.voltageLevelIdsToExport.add(vl2.getId());
String id = l.getId();
int num = mapper.getInt(AmplSubset.BRANCH, id);
int vl1Num = mapper.getInt(AmplSubset.VOLTAGE_LEVEL, vl1.getId());
int vl2Num = mapper.getInt(AmplSubset.VOLTAGE_LEVEL, vl2.getId());
float vb = vl1.getNominalV();
float zb = vb * vb / SB;
boolean merged = !config.isExportXNodes() && l.isTieLine();
if (config.isExportXNodes() && l.isTieLine()) {
TieLine tl = (TieLine) l;
String half1Id = tl.getHalf1().getId();
String half2Id = tl.getHalf2().getId();
int half1Num = mapper.getInt(AmplSubset.BRANCH, half1Id);
int half2Num = mapper.getInt(AmplSubset.BRANCH, half2Id);
String xNodeBusId = AmplUtil.getXnodeBusId(tl);
String xnodeVoltageLevelId = AmplUtil.getXnodeVoltageLevelId(tl);
int xNodeBusNum = mapper.getInt(AmplSubset.BUS, xNodeBusId);
int xNodeVoltageLevelNum = mapper.getInt(AmplSubset.VOLTAGE_LEVEL, xnodeVoltageLevelId);
formatter.writeCell(half1Num)
.writeCell(bus1Num)
.writeCell(xNodeBusNum)
.writeCell(-1)
.writeCell(vl1Num)
.writeCell(xNodeVoltageLevelNum)
.writeCell(tl.getHalf1().getR() / zb)
.writeCell(tl.getHalf1().getX() / zb)
.writeCell(tl.getHalf1().getG1() * zb)
.writeCell(tl.getHalf1().getG2() * zb)
.writeCell(tl.getHalf1().getB1() * zb)
.writeCell(tl.getHalf1().getB2() * zb)
.writeCell(1f) // constant ratio
.writeCell(-1) // no ratio tap changer
.writeCell(-1) // no phase tap changer
.writeCell(t1.getP())
.writeCell(-tl.getHalf1().getXnodeP()) // xnode node flow side 1
.writeCell(t1.getQ())
.writeCell(-tl.getHalf1().getXnodeQ()) // xnode node flow side 1
.writeCell(getPermanentLimit(l.getCurrentLimits1()))
.writeCell(Float.NaN)
.writeCell(merged)
.writeCell(faultNum)
.writeCell(actionNum)
.writeCell(half1Id)
.writeCell(tl.getHalf1().getName());
formatter.writeCell(half2Num)
.writeCell(xNodeBusNum)
.writeCell(bus2Num)
.writeCell(-1)
.writeCell(xNodeVoltageLevelNum)
.writeCell(vl2Num)
.writeCell(tl.getHalf2().getR() / zb)
.writeCell(tl.getHalf2().getX() / zb)
.writeCell(tl.getHalf2().getG1() * zb)
.writeCell(tl.getHalf2().getG2() * zb)
.writeCell(tl.getHalf2().getB1() * zb)
.writeCell(tl.getHalf2().getB2() * zb)
.writeCell(1f) // constant ratio
.writeCell(-1) // no ratio tap changer
.writeCell(-1) // no phase tap changer
.writeCell(-tl.getHalf2().getXnodeP()) // xnode node flow side 2
.writeCell(t2.getP())
.writeCell(-tl.getHalf2().getXnodeQ()) // xnode node flow side 2
.writeCell(t2.getQ())
.writeCell(Float.NaN)
.writeCell(getPermanentLimit(l.getCurrentLimits2()))
.writeCell(merged)
.writeCell(faultNum)
.writeCell(actionNum)
.writeCell(half2Id)
.writeCell(tl.getHalf2().getName());
} else {
formatter.writeCell(num)
.writeCell(bus1Num)
.writeCell(bus2Num)
.writeCell(-1)
.writeCell(vl1Num)
.writeCell(vl2Num)
.writeCell(l.getR() / zb)
.writeCell(l.getX() / zb)
.writeCell(l.getG1() * zb)
.writeCell(l.getG2() * zb)
.writeCell(l.getB1() * zb)
.writeCell(l.getB2() * zb)
.writeCell(1f) // constant ratio
.writeCell(-1) // no ratio tap changer
.writeCell(-1) // no phase tap changer
.writeCell(t1.getP())
.writeCell(t2.getP())
.writeCell(t1.getQ())
.writeCell(t2.getQ())
.writeCell(getPermanentLimit(l.getCurrentLimits1()))
.writeCell(getPermanentLimit(l.getCurrentLimits2()))
.writeCell(merged)
.writeCell(faultNum)
.writeCell(actionNum)
.writeCell(id)
.writeCell(l.getName());
}
}
for (TwoWindingsTransformer twt : network.getTwoWindingsTransformers()) {
Terminal t1 = twt.getTerminal1();
Terminal t2 = twt.getTerminal2();
Bus bus1 = AmplUtil.getBus(t1);
Bus bus2 = AmplUtil.getBus(t2);
if (bus1 != null && bus2 != null && bus1 == bus2) {
LOGGER.warn("Skipping transformer '{}' connected to the same bus at both sides", twt.getId());
continue;
}
String bus1Id = null;
int bus1Num = -1;
if (bus1 != null) {
bus1Id = bus1.getId();
bus1Num = mapper.getInt(AmplSubset.BUS, bus1.getId());
}
String bus2Id = null;
int bus2Num = -1;
if (bus2 != null) {
bus2Id = bus2.getId();
bus2Num = mapper.getInt(AmplSubset.BUS, bus2.getId());
}
if (isOnlyMainCc() && !(isBusExported(context, bus1Id) || isBusExported(context, bus2Id))) {
continue;
}
VoltageLevel vl1 = t1.getVoltageLevel();
VoltageLevel vl2 = t2.getVoltageLevel();
context.voltageLevelIdsToExport.add(vl1.getId());
context.voltageLevelIdsToExport.add(vl2.getId());
String id = twt.getId();
int num = mapper.getInt(AmplSubset.BRANCH, id);
int vl1Num = mapper.getInt(AmplSubset.VOLTAGE_LEVEL, vl1.getId());
int vl2Num = mapper.getInt(AmplSubset.VOLTAGE_LEVEL, vl2.getId());
float vb1 = vl1.getNominalV();
float vb2 = vl2.getNominalV();
float zb2 = vb2 * vb2 / SB;
float r = twt.getR() / zb2;
float x = twt.getX() / zb2;
float g = twt.getG() * zb2;
float b = twt.getB() * zb2;
float ratedU1 = twt.getRatedU1();
float ratedU2 = twt.getRatedU2();
float ratio = ratedU2 / vb2 / (ratedU1 / vb1);
RatioTapChanger rtc = twt.getRatioTapChanger();
PhaseTapChanger ptc = twt.getPhaseTapChanger();
int rtcNum = rtc != null ? mapper.getInt(AmplSubset.RATIO_TAP_CHANGER, twt.getId()) : -1;
int ptcNum = ptc != null ? mapper.getInt(AmplSubset.PHASE_TAP_CHANGER, twt.getId()) : -1;
formatter.writeCell(num)
.writeCell(bus1Num)
.writeCell(bus2Num)
.writeCell(-1)
.writeCell(vl1Num)
.writeCell(vl2Num)
.writeCell(r)
.writeCell(x)
.writeCell(g)
.writeCell(0)
.writeCell(b)
.writeCell(0)
.writeCell(ratio)
.writeCell(rtcNum)
.writeCell(ptcNum)
.writeCell(t1.getP())
.writeCell(t2.getP())
.writeCell(t1.getQ())
.writeCell(t2.getQ())
.writeCell(getPermanentLimit(twt.getCurrentLimits1()))
.writeCell(getPermanentLimit(twt.getCurrentLimits2()))
.writeCell(false) // TODO to update
.writeCell(faultNum)
.writeCell(actionNum)
.writeCell(id)
.writeCell(twt.getName());
}
for (ThreeWindingsTransformer twt : network.getThreeWindingsTransformers()) {
Terminal t1 = twt.getLeg1().getTerminal();
Terminal t2 = twt.getLeg2().getTerminal();
Terminal t3 = twt.getLeg3().getTerminal();
Bus bus1 = AmplUtil.getBus(t1);
Bus bus2 = AmplUtil.getBus(t2);
Bus bus3 = AmplUtil.getBus(t3);
// TODO could be connected to the same bus at 2 or 3 ends ?
VoltageLevel vl1 = t1.getVoltageLevel();
VoltageLevel vl2 = t2.getVoltageLevel();
VoltageLevel vl3 = t3.getVoltageLevel();
context.voltageLevelIdsToExport.add(vl1.getId());
context.voltageLevelIdsToExport.add(vl2.getId());
context.voltageLevelIdsToExport.add(vl3.getId());
String id1 = twt.getId() + "_leg1";
String id2 = twt.getId() + "_leg2";
String id3 = twt.getId() + "_leg3";
int num3wt = mapper.getInt(AmplSubset.THREE_WINDINGS_TRANSFO, twt.getId());
int num1 = mapper.getInt(AmplSubset.BRANCH, id1);
int num2 = mapper.getInt(AmplSubset.BRANCH, id2);
int num3 = mapper.getInt(AmplSubset.BRANCH, id3);
int vl1Num = mapper.getInt(AmplSubset.VOLTAGE_LEVEL, vl1.getId());
int vl2Num = mapper.getInt(AmplSubset.VOLTAGE_LEVEL, vl2.getId());
int vl3Num = mapper.getInt(AmplSubset.VOLTAGE_LEVEL, vl3.getId());
String bus1Id = null;
int bus1Num = -1;
if (bus1 != null) {
bus1Id = bus1.getId();
bus1Num = mapper.getInt(AmplSubset.BUS, bus1.getId());
}
String bus2Id = null;
int bus2Num = -1;
if (bus2 != null) {
bus2Id = bus2.getId();
bus2Num = mapper.getInt(AmplSubset.BUS, bus2.getId());
}
String bus3Id = null;
int bus3Num = -1;
if (bus3 != null) {
bus3Id = bus3.getId();
bus3Num = mapper.getInt(AmplSubset.BUS, bus3.getId());
}
float vb1 = vl1.getNominalV();
float vb2 = vl2.getNominalV();
float vb3 = vl3.getNominalV();
float zb1 = vb1 * vb1 / SB;
float zb2 = vb2 * vb2 / SB;
float zb3 = vb3 * vb3 / SB;
float r1 = twt.getLeg1().getR() / zb1;
float x1 = twt.getLeg1().getX() / zb1;
float g1 = twt.getLeg1().getG() * zb1;
float b1 = twt.getLeg1().getB() * zb1;
float r2 = twt.getLeg2().getR() / zb2;
float x2 = twt.getLeg2().getX() / zb2;
float r3 = twt.getLeg3().getR() / zb3;
float x3 = twt.getLeg3().getX() / zb3;
float ratedU1 = twt.getLeg1().getRatedU();
float ratedU2 = twt.getLeg2().getRatedU();
float ratedU3 = twt.getLeg3().getRatedU();
float ratio2 = ratedU1 / ratedU2;
float ratio3 = ratedU1 / ratedU3;
RatioTapChanger rtc2 = twt.getLeg2().getRatioTapChanger();
RatioTapChanger rtc3 = twt.getLeg2().getRatioTapChanger();
int rtc2Num = rtc2 != null ? mapper.getInt(AmplSubset.RATIO_TAP_CHANGER, id2) : -1;
int rtc3Num = rtc3 != null ? mapper.getInt(AmplSubset.RATIO_TAP_CHANGER, id3) : -1;
int middleVlNum = mapper.getInt(AmplSubset.VOLTAGE_LEVEL, getThreeWindingsTransformerMiddleVoltageLevelId(twt));
String middleBusId = getThreeWindingsTransformerMiddleBusId(twt);
int middleBusNum = mapper.getInt(AmplSubset.BUS, middleBusId);
if (!isOnlyMainCc() || isBusExported(context, middleBusId) || isBusExported(context, bus1Id)) {
formatter.writeCell(num1)
.writeCell(middleBusNum)
.writeCell(bus1Num)
.writeCell(num3wt)
.writeCell(middleVlNum)
.writeCell(vl1Num)
.writeCell(r1)
.writeCell(x1)
.writeCell(g1)
.writeCell(0)
.writeCell(b1)
.writeCell(0)
.writeCell(1f) // ratio is one at primary leg
.writeCell(-1)
.writeCell(-1)
.writeCell(Float.NaN)
.writeCell(t1.getP())
.writeCell(Float.NaN)
.writeCell(t1.getQ())
.writeCell(Float.NaN)
.writeCell(getPermanentLimit(twt.getLeg1().getCurrentLimits()))
.writeCell(false)
.writeCell(faultNum)
.writeCell(actionNum)
.writeCell(id1)
.writeEmptyCell();
}
if (!isOnlyMainCc() || isBusExported(context, middleBusId) || isBusExported(context, bus2Id)) {
formatter.writeCell(num2)
.writeCell(bus2Num)
.writeCell(middleBusNum)
.writeCell(num3wt)
.writeCell(vl2Num)
.writeCell(middleVlNum)
.writeCell(r2)
.writeCell(x2)
.writeCell(0)
.writeCell(0)
.writeCell(0)
.writeCell(0)
.writeCell(ratio2)
.writeCell(rtc2Num)
.writeCell(-1)
.writeCell(t2.getP())
.writeCell(Float.NaN)
.writeCell(t2.getQ())
.writeCell(Float.NaN)
.writeCell(getPermanentLimit(twt.getLeg2().getCurrentLimits()))
.writeCell(Float.NaN)
.writeCell(false)
.writeCell(faultNum)
.writeCell(actionNum)
.writeCell(id2)
.writeEmptyCell();
}
if (!isOnlyMainCc() || isBusExported(context, middleBusId) || isBusExported(context, bus3Id)) {
formatter.writeCell(num3)
.writeCell(bus3Num)
.writeCell(middleBusNum)
.writeCell(num3wt)
.writeCell(vl3Num)
.writeCell(middleVlNum)
.writeCell(r3)
.writeCell(x3)
.writeCell(0)
.writeCell(0)
.writeCell(0)
.writeCell(0)
.writeCell(ratio3)
.writeCell(rtc3Num)
.writeCell(-1)
.writeCell(t3.getP())
.writeCell(Float.NaN)
.writeCell(t3.getQ())
.writeCell(Float.NaN)
.writeCell(getPermanentLimit(twt.getLeg3().getCurrentLimits()))
.writeCell(Float.NaN)
.writeCell(false)
.writeCell(faultNum)
.writeCell(actionNum)
.writeCell(id3)
.writeEmptyCell();
}
}
for (DanglingLine dl : network.getDanglingLines()) {
Terminal t = dl.getTerminal();
Bus bus1 = AmplUtil.getBus(t);
String bus1Id = null;
int bus1Num = -1;
if (bus1 != null) {
bus1Id = bus1.getId();
bus1Num = mapper.getInt(AmplSubset.BUS, bus1Id);
}
String middleBusId = getDanglingLineMiddleBusId(dl);
int middleBusNum = mapper.getInt(AmplSubset.BUS, middleBusId);
if (isOnlyMainCc() && !(isBusExported(context, bus1Id) || isBusExported(context, middleBusId))) {
continue;
}
VoltageLevel vl = t.getVoltageLevel();
String middleVlId = getDanglingLineMiddleVoltageLevelId(dl);
context.voltageLevelIdsToExport.add(vl.getId());
context.voltageLevelIdsToExport.add(middleVlId);
String id = dl.getId();
int num = mapper.getInt(AmplSubset.BRANCH, id);
int vl1Num = mapper.getInt(AmplSubset.VOLTAGE_LEVEL, vl.getId());
int middleVlNum = mapper.getInt(AmplSubset.VOLTAGE_LEVEL, middleVlId);
float vb = vl.getNominalV();
float zb = vb * vb / SB;
float p1 = t.getP();
float q1 = t.getQ();
SV sv = new SV(p1, q1, bus1 != null ? bus1.getV() : Float.NaN, bus1 != null ? bus1.getAngle() : Float.NaN).otherSide(dl);
float p2 = sv.getP();
float q2 = sv.getQ();
float patl = getPermanentLimit(dl.getCurrentLimits());
formatter.writeCell(num)
.writeCell(bus1Num)
.writeCell(middleBusNum)
.writeCell(-1)
.writeCell(vl1Num)
.writeCell(middleVlNum)
.writeCell(dl.getR() / zb)
.writeCell(dl.getX() / zb)
.writeCell(dl.getG() / 2 * zb)
.writeCell(dl.getG() / 2 * zb)
.writeCell(dl.getB() / 2 * zb)
.writeCell(dl.getB() / 2 * zb)
.writeCell(1f) // constant ratio
.writeCell(-1) // no ratio tap changer
.writeCell(-1) // no phase tap changer
.writeCell(p1)
.writeCell(p2)
.writeCell(q1)
.writeCell(q2)
.writeCell(patl)
.writeCell(patl)
.writeCell(false)
.writeCell(faultNum)
.writeCell(actionNum)
.writeCell(id)
.writeCell(dl.getName());
}
}
}
private void writeTapChangerTable() throws IOException {
try (Writer writer = new OutputStreamWriter(dataSource.newOutputStream("_network_tct", "txt", append), StandardCharsets.UTF_8);
TableFormatter formatter = new AmplDatTableFormatter(
writer,
getTableTitle("Tap changer table"),
INVALID_FLOAT_VALUE,
!append,
LOCALE,
new Column("num"),
new Column("tap"),
new Column("var ratio"),
new Column("x (pu)"),
new Column("angle (rad)"),
new Column("fault"),
new Column(config.getActionType().getLabel()))) {
for (TwoWindingsTransformer twt : network.getTwoWindingsTransformers()) {
Terminal t2 = twt.getTerminal2();
float vb2 = t2.getVoltageLevel().getNominalV();
float zb2 = vb2 * vb2 / SB;
RatioTapChanger rtc = twt.getRatioTapChanger();
if (rtc != null) {
String id = twt.getId() + "_ratio_table";
int num = mapper.getInt(AmplSubset.TAP_CHANGER_TABLE, id);
for (int position = rtc.getLowTapPosition(); position <= rtc.getHighTapPosition(); position++) {
RatioTapChangerStep step = rtc.getStep(position);
float x = twt.getX() * (1 + step.getX() / 100) / zb2;
formatter.writeCell(num)
.writeCell(position - rtc.getLowTapPosition() + 1)
.writeCell(step.getRho())
.writeCell(x)
.writeCell(0f)
.writeCell(faultNum)
.writeCell(actionNum);
}
}
PhaseTapChanger ptc = twt.getPhaseTapChanger();
if (ptc != null) {
String id = twt.getId() + "_phase_table";
int num = mapper.getInt(AmplSubset.TAP_CHANGER_TABLE, id);
for (int position = ptc.getLowTapPosition(); position <= ptc.getHighTapPosition(); position++) {
PhaseTapChangerStep step = ptc.getStep(position);
float x = twt.getX() * (1 + step.getX() / 100) / zb2;
formatter.writeCell(num)
.writeCell(position - ptc.getLowTapPosition() + 1)
.writeCell(step.getRho())
.writeCell(x)
.writeCell((float) Math.toRadians(step.getAlpha()))
.writeCell(faultNum)
.writeCell(actionNum);
}
}
}
for (ThreeWindingsTransformer twt : network.getThreeWindingsTransformers()) {
Terminal t2 = twt.getLeg2().getTerminal();
Terminal t3 = twt.getLeg3().getTerminal();
float vb2 = t2.getVoltageLevel().getNominalV();
float vb3 = t3.getVoltageLevel().getNominalV();
float zb2 = vb2 * vb2 / SB;
float zb3 = vb3 * vb3 / SB;
RatioTapChanger rtc2 = twt.getLeg2().getRatioTapChanger();
if (rtc2 != null) {
String id = twt.getId() + "_leg2_ratio_table";
int num = mapper.getInt(AmplSubset.TAP_CHANGER_TABLE, id);
for (int position = rtc2.getLowTapPosition(); position <= rtc2.getHighTapPosition(); position++) {
RatioTapChangerStep step = rtc2.getStep(position);
float x = twt.getLeg2().getX() * (1 + step.getX() / 100) / zb2;
formatter.writeCell(num)
.writeCell(position - rtc2.getLowTapPosition() + 1)
.writeCell(step.getRho())
.writeCell(x)
.writeCell(0f)
.writeCell(faultNum)
.writeCell(actionNum);
}
}
RatioTapChanger rtc3 = twt.getLeg3().getRatioTapChanger();
if (rtc3 != null) {
String id = twt.getId() + "_leg3_ratio_table";
int num = mapper.getInt(AmplSubset.TAP_CHANGER_TABLE, id);
for (int position = rtc3.getLowTapPosition(); position <= rtc3.getHighTapPosition(); position++) {
RatioTapChangerStep step = rtc3.getStep(position);
float x = twt.getLeg3().getX() * (1 + step.getX() / 100) / zb3;
formatter.writeCell(num)
.writeCell(position - rtc3.getLowTapPosition() + 1)
.writeCell(step.getRho())
.writeCell(x)
.writeCell(0f)
.writeCell(faultNum)
.writeCell(actionNum);
}
}
}
}
}
private void writeRatioTapChanger(TableFormatter formatter, String rtcId,
RatioTapChanger rtc, String tcsId) throws IOException {
int rtcNum = mapper.getInt(AmplSubset.RATIO_TAP_CHANGER, rtcId);
int tcsNum = mapper.getInt(AmplSubset.TAP_CHANGER_TABLE, tcsId);
formatter.writeCell(rtcNum)
.writeCell(rtc.getTapPosition() - rtc.getLowTapPosition() + 1)
.writeCell(tcsNum)
.writeCell(rtc.hasLoadTapChangingCapabilities() && rtc.isRegulating());
if (config.isExportRatioTapChangerVoltageTarget()) {
formatter.writeCell(rtc.getTargetV());
}
formatter.writeCell(faultNum)
.writeCell(actionNum)
.writeCell(rtcId);
}
private void writeRatioTapChangers() throws IOException {
List<Column> columns = new ArrayList<>(8);
columns.add(new Column("num"));
columns.add(new Column("tap"));
columns.add(new Column("table"));
columns.add(new Column("onLoad"));
if (config.isExportRatioTapChangerVoltageTarget()) {
columns.add(new Column("targetV"));
}
columns.add(new Column("fault"));
columns.add(new Column(config.getActionType().getLabel()));
columns.add(new Column("id"));
try (Writer writer = new OutputStreamWriter(dataSource.newOutputStream("_network_rtc", "txt", append), StandardCharsets.UTF_8);
TableFormatter formatter = new AmplDatTableFormatter(
writer,
getTableTitle("Ratio tap changers"),
INVALID_FLOAT_VALUE,
!append,
LOCALE,
columns.toArray(new Column[columns.size()]))) {
for (TwoWindingsTransformer twt : network.getTwoWindingsTransformers()) {
RatioTapChanger rtc = twt.getRatioTapChanger();
if (rtc != null) {
String rtcId = twt.getId();
String tcsId = twt.getId() + "_ratio_table";
writeRatioTapChanger(formatter, rtcId, rtc, tcsId);
}
}
for (ThreeWindingsTransformer twt : network.getThreeWindingsTransformers()) {
RatioTapChanger rtc2 = twt.getLeg2().getRatioTapChanger();
if (rtc2 != null) {
String rtc2Id = twt.getId() + "_leg2";
String tcs2Id = twt.getId() + "_leg2_ratio_table";