-
Notifications
You must be signed in to change notification settings - Fork 19
/
IIDM2DB.java
517 lines (437 loc) · 23.4 KB
/
IIDM2DB.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
/**
* 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.modules.histo;
import com.google.common.collect.HashMultimap;
import com.google.common.collect.Multimap;
import eu.itesla_project.iidm.network.*;
import eu.itesla_project.iidm.network.util.SV;
import org.apache.commons.codec.binary.Base64;
import org.json.JSONArray;
import org.json.JSONException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.io.UnsupportedEncodingException;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.*;
/**
*
* @author Philippe Duchesne <ph.duchesne@pepite.be>
* @author Geoffroy Jamgotchian <geoffroy.jamgotchian at rte-france.com>
*/
public class IIDM2DB {
private static Logger log = LoggerFactory.getLogger(IIDM2DB.class);
public static class HorizonKey {
public final int forecastDistance;
public final String horizon;
public HorizonKey(int forecastDistance, String horizon) {
this.forecastDistance = forecastDistance;
this.horizon = horizon;
}
@Override
public int hashCode() {
return toString().hashCode();
}
@Override
public boolean equals(Object obj) {
return obj != null && obj instanceof HorizonKey && obj.toString().equals(this.toString());
}
@Override
public String toString() {
return (horizon + forecastDistance);
}
}
public static class CimValuesMap extends HashMap<HorizonKey, LinkedHashMap<HistoDbAttributeId, Object>> {
Map<String, Map<String, JSONArray>> toposPerSubstation = new HashMap();
LinkedHashMap<HistoDbAttributeId,Object> getValueMap(HorizonKey key) {
LinkedHashMap<HistoDbAttributeId,Object> valueMap = this.get(key);
if (valueMap == null) this.put(key, valueMap = new LinkedHashMap<HistoDbAttributeId, Object>());
return valueMap;
}
public boolean hasForecastValues() {
return keySet().size()>1 || (keySet().size()==1 && keySet().iterator().next().forecastDistance>0);
}
public LinkedHashMap<HistoDbAttributeId,Object> getSingleValueMap() {
if (this.size() == 0) throw new IllegalStateException("This CimValuesMap has no horizons");
if (this.size() > 1) throw new IllegalStateException("This CimValuesMap has several horizons " + this.keySet());
return this.values().iterator().next();
}
void addTopology(String substationId, String topoHash, JSONArray topo) {
Map<String, JSONArray> topos = toposPerSubstation.get(substationId);
if (topos == null) toposPerSubstation.put(substationId, topos = new HashMap());
topos.put(topoHash, topo);
}
public Map<String, Map<String, JSONArray>> getToposPerSubstation() {
return toposPerSubstation;
}
}
public static JSONArray toTopoSet(VoltageLevel vl) throws JSONException {
SortedMap<String, JSONArray> topoList = new TreeMap<>();
Multimap<String, String> topoTmp = HashMultimap.create();
vl.visitEquipments(new TerminalTopologyVisitor() {
@Override
public void visitTerminal(Terminal t) {
Connectable c = t.getConnectable();
Bus b = t.getBusView().getBus();
if (b == null) {
if (c.getType() == ConnectableType.LOAD
|| c.getType() == ConnectableType.GENERATOR
|| c.getType() == ConnectableType.SHUNT_COMPENSATOR) {
// add the injection in the topo set even if not connected but just connectable to this bus
// see WP4.2 data mining topology spec for more detailed information
b = t.getBusView().getConnectableBus();
}
}
if (b != null) {
topoTmp.put(b.getId(), c.getId());
} else {
// connect the equipment to its own bus
topoTmp.put(c.getId() + "FICTIVE_BUS", c.getId());
}
}
});
for (Map.Entry<String, Collection<String>> entry : topoTmp.asMap().entrySet()) {
SortedSet<String> topoContent = new TreeSet<>(entry.getValue());
JSONArray topoArray = new JSONArray(topoContent.toArray(new String[]{}));
topoList.put(topoArray.toString(), topoArray);
}
return new JSONArray(topoList.values().toArray(new JSONArray[]{}));
}
public static class Config {
private final String cimName;
private final boolean extractTemporalFields;
private final boolean replaceMissingValues;
private final Set<Country> countryFilter;
private final int minBaseVoltageFilter;
public Config(String cimName, boolean extractTemporalFields) {
this(cimName, extractTemporalFields, false);
}
public Config(String cimName, boolean extractTemporalFields, boolean replaceMissingValues) {
this(cimName, extractTemporalFields, replaceMissingValues, null);
}
public Config(String cimName, boolean extractTemporalFields, boolean replaceMissingValues, Set<Country> countryFilter) {
this(cimName, extractTemporalFields, replaceMissingValues, countryFilter, 0);
}
public Config(String cimName, boolean extractTemporalFields, boolean replaceMissingValues, Set<Country> countryFilter, int minBaseVoltageFilter) {
this.cimName = cimName;
this.extractTemporalFields = extractTemporalFields;
this.replaceMissingValues = replaceMissingValues;
this.countryFilter = countryFilter;
this.minBaseVoltageFilter = minBaseVoltageFilter;
}
public String getCimName() {
return cimName;
}
public boolean isExtractTemporalFields() {
return extractTemporalFields;
}
public boolean isReplaceMissingValues() {
return replaceMissingValues;
}
public Set<Country> getCountryFilter() {
return countryFilter;
}
public int getMinBaseVoltageFilter() {
return minBaseVoltageFilter;
}
}
private static class TerminalContext {
float p = Float.NaN;
float q = Float.NaN;
float v = Float.NaN;
float i = Float.NaN;
private void update(Terminal t) {
if (t.getBusView().getBus() != null) {
p = t.getP();
q = t.getQ();
v = t.getBusView().getBus().getV();
i = t.getI();
}
}
private static TerminalContext create(Terminal t) {
TerminalContext context = new TerminalContext();
context.update(t);
return context;
}
}
public static CimValuesMap extractCimValues(Network n, Config config) {
CimValuesMap valuesMap = new CimValuesMap();
for (Substation ss : n.getSubstations()) {
if (config.getCountryFilter() != null && !config.getCountryFilter().contains(ss.getCountry())) {
continue;
}
for (VoltageLevel vl : ss.getVoltageLevels()) {
if (vl.getNominalV() < config.getMinBaseVoltageFilter()) {
continue;
}
String horizon = n.getForecastDistance() > 0 ? "DACF" : "SN"; // for backward compatibility
final LinkedHashMap<HistoDbAttributeId, Object> valueMap = valuesMap.getValueMap(new HorizonKey(n.getForecastDistance(), horizon));
if (config.getCimName() != null && !valueMap.containsKey(HistoDbMetaAttributeId.cimName)) valueMap.put(HistoDbMetaAttributeId.cimName, config.getCimName());
if (config.isExtractTemporalFields()) {
if (!valueMap.containsKey(HistoDbMetaAttributeId.datetime)) valueMap.put(HistoDbMetaAttributeId.datetime, n.getCaseDate().toDate());
if (!valueMap.containsKey(HistoDbMetaAttributeId.daytime)) valueMap.put(HistoDbMetaAttributeId.daytime, n.getCaseDate().getMillisOfDay());
if (!valueMap.containsKey(HistoDbMetaAttributeId.month)) valueMap.put(HistoDbMetaAttributeId.month, n.getCaseDate().getMonthOfYear());
if (!valueMap.containsKey(HistoDbMetaAttributeId.forecastTime)) valueMap.put(HistoDbMetaAttributeId.forecastTime, n.getForecastDistance());
if (!valueMap.containsKey(HistoDbMetaAttributeId.horizon)) valueMap.put(HistoDbMetaAttributeId.horizon, horizon);
}
vl.visitEquipments(new AbstractTopologyVisitor() {
private void visitInjection(Injection inj) {
visitInjection(inj, new TerminalContext());
}
private void visitInjection(Injection inj, TerminalContext context) {
Terminal t = inj.getTerminal();
context.update(t);
if (config.isReplaceMissingValues()) {
if (Float.isNaN(context.p)) {
context.p = 0f;
}
if (Float.isNaN(context.q)) {
context.q = 0f;
}
if (Float.isNaN(context.v)) {
// use connectable bus voltage, better than nothing...
context.v = t.getBusBreakerView().getConnectableBus().getV();
}
if (Float.isNaN(context.v)) {
context.v = 0f; // TODO is there a better value?
}
if (Float.isNaN(context.i)) {
context.i = 0f;
}
}
valueMap.put(new HistoDbNetworkAttributeId(inj.getId(), HistoDbAttr.P), context.p);
valueMap.put(new HistoDbNetworkAttributeId(inj.getId(), HistoDbAttr.Q), context.q);
valueMap.put(new HistoDbNetworkAttributeId(inj.getId(), HistoDbAttr.V), context.v);
valueMap.put(new HistoDbNetworkAttributeId(inj.getId(), HistoDbAttr.I), context.i);
}
private void visitBranch(Branch branch, Branch.Side side, float r, float x, float g1, float b1, float g2, float b2, float ratio) {
Terminal t = side == Branch.Side.ONE ? branch.getTerminal1() : branch.getTerminal2();
TerminalContext context = TerminalContext.create(t);
if (config.isReplaceMissingValues()) {
if (Float.isNaN(context.p)) {
context.p = 0f;
}
if (Float.isNaN(context.q)) {
context.q = 0f;
}
if (Float.isNaN(context.v)) {
Terminal otherT = t == branch.getTerminal1() ? branch.getTerminal2() : branch.getTerminal1();
Bus otherBus = otherT.getBusView().getBus();
if (otherBus != null && !Float.isNaN(otherBus.getV())) {
// compute the voltage from the other side physical values
// TODO approx we do not consider voltage drop due to branch impedance
if (t == branch.getTerminal1()) {
// we are on side 1 disconnected and side 2 is connected
context.v = otherBus.getV() / ratio;
} else if (t == branch.getTerminal2()) {
// we are on side 2 disconnected and side 1 is connected
context.v = otherBus.getV() * ratio;
} else {
throw new AssertionError();
}
} else {
// use connectable bus voltage, better than nothing...
context.v = t.getBusBreakerView().getConnectableBus().getV();
}
}
if (Float.isNaN(context.v)) {
context.v = 0; // TODO is there a better value?
}
if (Float.isNaN(context.i)) {
context.i = 0;
}
}
valueMap.put(new HistoDbNetworkAttributeId(branch.getId(), t.getVoltageLevel().getId(), HistoDbAttr.P), context.p);
valueMap.put(new HistoDbNetworkAttributeId(branch.getId(), t.getVoltageLevel().getId(), HistoDbAttr.Q), context.q);
valueMap.put(new HistoDbNetworkAttributeId(branch.getId(), t.getVoltageLevel().getId(), HistoDbAttr.V), context.v);
valueMap.put(new HistoDbNetworkAttributeId(branch.getId(), t.getVoltageLevel().getId(), HistoDbAttr.I), context.i);
}
@Override
public void visitGenerator(Generator g) {
TerminalContext context = new TerminalContext();
visitInjection(g, context);
// reactive limit
float qmax = g.getReactiveLimits().getMaxQ(context.p);
valueMap.put(new HistoDbNetworkAttributeId(g.getId(), HistoDbAttr.QR), Math.abs(qmax - context.q));
}
@Override
public void visitLoad(Load l) {
if (l.getLoadType() != LoadType.FICTITIOUS) {
visitInjection(l);
}
}
@Override
public void visitShuntCompensator(ShuntCompensator sc) {
visitInjection(sc);
}
@Override
public void visitDanglingLine(DanglingLine dl) {
visitInjection(dl);
valueMap.put(new HistoDbNetworkAttributeId(dl.getId(), HistoDbAttr.P0), dl.getP0());
valueMap.put(new HistoDbNetworkAttributeId(dl.getId(), HistoDbAttr.Q0), dl.getQ0());
}
@Override
public void visitLine(Line l, Line.Side side) {
visitBranch(l, side, l.getR(), l.getX(), l.getG1(), l.getB1(), l.getG2(), l.getB2(), 1);
}
@Override
public void visitTwoWindingsTransformer(TwoWindingsTransformer twt, TwoWindingsTransformer.Side side) {
visitBranch(twt, side, twt.getR(), twt.getX(), twt.getG(), twt.getB(), 0, 0, SV.getRatio(twt));
}
@Override
public void visitThreeWindingsTransformer(ThreeWindingsTransformer twt, ThreeWindingsTransformer.Side side) {
Terminal t;
switch (side) {
case ONE:
t = twt.getLeg1().getTerminal();
break;
case TWO:
t = twt.getLeg2().getTerminal();
break;
case THREE:
t = twt.getLeg3().getTerminal();
break;
default:
throw new AssertionError();
}
TerminalContext context = TerminalContext.create(t);
if (config.isReplaceMissingValues()) {
if (Float.isNaN(context.p)) {
context.p = 0f;
}
if (Float.isNaN(context.q)) {
context.q = 0f;
}
if (Float.isNaN(context.v)) {
context.v = 0; // TODO is possible to find a better replacement value?
}
if (Float.isNaN(context.i)) {
context.i = 0f;
}
}
valueMap.put(new HistoDbNetworkAttributeId(twt.getId(), t.getVoltageLevel().getId(), HistoDbAttr.V), context.v);
valueMap.put(new HistoDbNetworkAttributeId(twt.getId(), t.getVoltageLevel().getId(), HistoDbAttr.I), context.i);
valueMap.put(new HistoDbNetworkAttributeId(twt.getId(), t.getVoltageLevel().getId(), HistoDbAttr.P), context.p);
valueMap.put(new HistoDbNetworkAttributeId(twt.getId(), t.getVoltageLevel().getId(), HistoDbAttr.Q), context.q);
}
});
// taps
for (TwoWindingsTransformer twt : ss.getTwoWindingsTransformers()) {
if (twt.getPhaseTapChanger() != null) {
valueMap.put(new HistoDbNetworkAttributeId(twt.getId(), HistoDbAttr.PTC), twt.getPhaseTapChanger().getTapPosition());
}
if (twt.getRatioTapChanger() != null) {
valueMap.put(new HistoDbNetworkAttributeId(twt.getId(), HistoDbAttr.RTC), twt.getRatioTapChanger().getTapPosition());
}
}
for (ThreeWindingsTransformer twt : ss.getThreeWindingsTransformers()) {
valueMap.put(new HistoDbNetworkAttributeId(twt.getId(), twt.getLeg2().getTerminal().getVoltageLevel().getId(), HistoDbAttr.RTC), twt.getLeg2().getRatioTapChanger().getTapPosition());
valueMap.put(new HistoDbNetworkAttributeId(twt.getId(), twt.getLeg3().getTerminal().getVoltageLevel().getId(), HistoDbAttr.RTC), twt.getLeg3().getRatioTapChanger().getTapPosition());
}
/**
* Extract topologies and mean tension
*/
try {
JSONArray toposArray = toTopoSet(vl);
String jsonRep = toposArray.toString();
valueMap.put(new HistoDbNetworkAttributeId(vl.getId(), HistoDbAttr.TOPO), jsonRep);
String base64hash = computeTopoHash(jsonRep);
valuesMap.addTopology(vl.getId(), base64hash, toposArray);
valueMap.put(new HistoDbNetworkAttributeId(vl.getId(), HistoDbAttr.TOPOHASH), base64hash);
} catch (JSONException e) {
throw new RuntimeException("Failed to gather topologies", e);
}
float pgen = 0;
float qgen = 0;
float pload = 0;
float qload = 0;
float qshunt = 0;
for (Generator g: vl.getGenerators()) {
Terminal t = g.getTerminal();
if (t.getBusView().getBus() != null) {
if (!Float.isNaN(t.getP())) {
pgen += t.getP();
}
if (!Float.isNaN(t.getQ())) {
qgen += t.getQ();
}
}
}
for (Load l: vl.getLoads()) {
Terminal t = l.getTerminal();
if (t.getBusView().getBus() != null) {
if (!Float.isNaN(t.getP())) {
pload += t.getP();
}
if (!Float.isNaN(t.getQ())) {
qload += t.getQ();
}
}
}
for (ShuntCompensator s: vl.getShunts()) {
Terminal t = s.getTerminal();
if (t.getBusView().getBus() != null) {
if (!Float.isNaN(t.getQ())) {
qshunt += t.getQ();
}
}
}
float vSum = 0;
int validBusCount = 0;
int busCount = 0;
float vMin = Float.NaN;
float vMax = Float.NaN;
for (Bus b : vl.getBusView().getBuses()) {
if (!Float.isNaN(b.getV())) {
vSum += b.getV();
validBusCount++;
vMin = Float.isNaN(vMin) ? b.getV() : Math.min(vMin, b.getV());
vMax = Float.isNaN(vMax) ? b.getV() : Math.max(vMax, b.getV());
}
busCount++;
}
float meanV = Float.NaN;
if (validBusCount > 0) {
meanV = vSum / validBusCount;
}
if (config.isReplaceMissingValues()) {
if (Float.isNaN(meanV)) {
meanV = 0; // TODO is there a better value?
}
if (Float.isNaN(vMin)) {
vMin = 0; // TODO is there a better value?
}
if (Float.isNaN(vMax)) {
vMax = 0; // TODO is there a better value?
}
}
valueMap.put(new HistoDbNetworkAttributeId(vl.getId(), HistoDbAttr.PGEN), pgen);
valueMap.put(new HistoDbNetworkAttributeId(vl.getId(), HistoDbAttr.QGEN), qgen);
valueMap.put(new HistoDbNetworkAttributeId(vl.getId(), HistoDbAttr.PLOAD), pload);
valueMap.put(new HistoDbNetworkAttributeId(vl.getId(), HistoDbAttr.QLOAD), qload);
valueMap.put(new HistoDbNetworkAttributeId(vl.getId(), HistoDbAttr.QSHUNT), qshunt);
valueMap.put(new HistoDbNetworkAttributeId(vl.getId(), HistoDbAttr.V), meanV);
valueMap.put(new HistoDbNetworkAttributeId(vl.getId(), HistoDbAttr.VMIN), vMin);
valueMap.put(new HistoDbNetworkAttributeId(vl.getId(), HistoDbAttr.VMAX), vMax);
valueMap.put(new HistoDbNetworkAttributeId(vl.getId(), HistoDbAttr.BC), busCount);
}
}
return valuesMap;
}
public static String computeTopoHash(String topoRep) {
String base64hash;
try {
byte[] digest = MessageDigest.getInstance("SHA-1").digest(topoRep.getBytes("UTF-8"));
// create a hash from the SHA-1 first 6 bytes
digest = Arrays.copyOfRange(digest, 0, 6);
base64hash = Base64.encodeBase64String(digest).trim();
} catch (UnsupportedEncodingException | NoSuchAlgorithmException e) {
throw new RuntimeException("Failed to compute topology hash from"+topoRep, e);
}
return base64hash;
}
}