6
6
*/
7
7
package org .gridsuite .voltageinit .server .service .parameters ;
8
8
9
+ import java .text .DecimalFormat ;
9
10
import java .util .*;
10
11
import java .util .concurrent .TimeUnit ;
12
+ import java .util .concurrent .atomic .AtomicReference ;
11
13
import java .util .stream .Collectors ;
12
14
13
15
import com .google .common .annotations .VisibleForTesting ;
20
22
import com .powsybl .openreac .parameters .input .VoltageLimitOverride ;
21
23
import com .powsybl .openreac .parameters .input .VoltageLimitOverride .VoltageLimitType ;
22
24
import com .powsybl .openreac .parameters .input .algo .ReactiveSlackBusesMode ;
23
- import lombok .NonNull ;
24
25
import org .apache .commons .lang3 .mutable .MutableInt ;
25
26
import org .gridsuite .voltageinit .server .dto .parameters .FilterEquipments ;
26
27
import org .gridsuite .voltageinit .server .dto .parameters .IdentifiableAttributes ;
45
46
public class VoltageInitParametersService {
46
47
47
48
private static final Logger LOGGER = LoggerFactory .getLogger (VoltageInitParametersService .class );
49
+ private static final DecimalFormat DF = new DecimalFormat ("0.0" );
48
50
49
51
private final FilterService filterService ;
50
52
@@ -111,25 +113,25 @@ private static void fillSpecificVoltageLimits(List<VoltageLimitOverride> specifi
111
113
boolean isLowVoltageLimitDefaultSet = voltageLevelDefaultLimits .containsKey (voltageLevel .getId ()) && voltageLevelDefaultLimits .get (voltageLevel .getId ()).getLowVoltageLimit () != null ;
112
114
boolean isHighVoltageLimitDefaultSet = voltageLevelDefaultLimits .containsKey (voltageLevel .getId ()) && voltageLevelDefaultLimits .get (voltageLevel .getId ()).getHighVoltageLimit () != null ;
113
115
114
- final CountVoltageLimit counterToIncrementLow = generateLowVoltageLimit (specificVoltageLimits , voltageLevelModificationLimits , voltageLevelDefaultLimits , isLowVoltageLimitModificationSet , isLowVoltageLimitDefaultSet , voltageLevel , voltageLevelsIdsRestricted );
115
- final CountVoltageLimit counterToIncrementHigh = generateHighVoltageLimit (specificVoltageLimits , voltageLevelModificationLimits , voltageLevelDefaultLimits , isHighVoltageLimitModificationSet , isHighVoltageLimitDefaultSet , voltageLevel );
116
- if (counterToIncrementLow == CountVoltageLimit .DEFAULT || counterToIncrementLow == CountVoltageLimit .BOTH ||
117
- counterToIncrementHigh == CountVoltageLimit .DEFAULT || counterToIncrementHigh == CountVoltageLimit .BOTH ) {
116
+ final CounterToIncrement counterToIncrementLow = generateLowVoltageLimit (specificVoltageLimits , voltageLevelModificationLimits , voltageLevelDefaultLimits , isLowVoltageLimitModificationSet , isLowVoltageLimitDefaultSet , voltageLevel , voltageLevelsIdsRestricted );
117
+ final CounterToIncrement counterToIncrementHigh = generateHighVoltageLimit (specificVoltageLimits , voltageLevelModificationLimits , voltageLevelDefaultLimits , isHighVoltageLimitModificationSet , isHighVoltageLimitDefaultSet , voltageLevel );
118
+ if (counterToIncrementLow == CounterToIncrement .DEFAULT || counterToIncrementLow == CounterToIncrement .BOTH ||
119
+ counterToIncrementHigh == CounterToIncrement .DEFAULT || counterToIncrementHigh == CounterToIncrement .BOTH ) {
118
120
counterMissingVoltageLimits .increment ();
119
121
}
120
- if (counterToIncrementLow == CountVoltageLimit .MODIFICATION || counterToIncrementLow == CountVoltageLimit .BOTH ||
121
- counterToIncrementHigh == CountVoltageLimit .MODIFICATION || counterToIncrementHigh == CountVoltageLimit .BOTH ) {
122
+ if (counterToIncrementLow == CounterToIncrement .MODIFICATION || counterToIncrementLow == CounterToIncrement .BOTH ||
123
+ counterToIncrementHigh == CounterToIncrement .MODIFICATION || counterToIncrementHigh == CounterToIncrement .BOTH ) {
122
124
counterVoltageLimitModifications .increment ();
123
125
}
124
126
}
125
127
126
- private static CountVoltageLimit generateLowVoltageLimit (List <VoltageLimitOverride > specificVoltageLimits ,
127
- Map <String , VoltageLimitEntity > voltageLevelModificationLimits ,
128
- Map <String , VoltageLimitEntity > voltageLevelDefaultLimits ,
129
- boolean isLowVoltageLimitModificationSet ,
130
- boolean isLowVoltageLimitDefaultSet ,
131
- VoltageLevel voltageLevel ,
132
- Map <String , Double > voltageLevelsIdsRestricted ) {
128
+ private static CounterToIncrement generateLowVoltageLimit (List <VoltageLimitOverride > specificVoltageLimits ,
129
+ Map <String , VoltageLimitEntity > voltageLevelModificationLimits ,
130
+ Map <String , VoltageLimitEntity > voltageLevelDefaultLimits ,
131
+ boolean isLowVoltageLimitModificationSet ,
132
+ boolean isLowVoltageLimitDefaultSet ,
133
+ VoltageLevel voltageLevel ,
134
+ Map <String , Double > voltageLevelsIdsRestricted ) {
133
135
double newLowVoltageLimit ;
134
136
double lowVoltageLimit = voltageLevel .getLowVoltageLimit ();
135
137
if (!Double .isNaN (lowVoltageLimit ) && isLowVoltageLimitModificationSet ) {
@@ -141,7 +143,7 @@ private static CountVoltageLimit generateLowVoltageLimit(List<VoltageLimitOverri
141
143
newLowVoltageLimit = lowVoltageLimitModification ;
142
144
}
143
145
specificVoltageLimits .add (new VoltageLimitOverride (voltageLevel .getId (), VoltageLimitType .LOW_VOLTAGE_LIMIT , true , newLowVoltageLimit ));
144
- return CountVoltageLimit .MODIFICATION ;
146
+ return CounterToIncrement .MODIFICATION ;
145
147
146
148
} else if (Double .isNaN (lowVoltageLimit ) && isLowVoltageLimitDefaultSet ) {
147
149
double voltageLimit = voltageLevelDefaultLimits .get (voltageLevel .getId ()).getLowVoltageLimit () + (isLowVoltageLimitModificationSet ? voltageLevelModificationLimits .get (voltageLevel .getId ()).getLowVoltageLimit () : 0. );
@@ -153,39 +155,39 @@ private static CountVoltageLimit generateLowVoltageLimit(List<VoltageLimitOverri
153
155
}
154
156
specificVoltageLimits .add (new VoltageLimitOverride (voltageLevel .getId (), VoltageLimitType .LOW_VOLTAGE_LIMIT , false , newLowVoltageLimit ));
155
157
if (isLowVoltageLimitModificationSet ) {
156
- return CountVoltageLimit .BOTH ;
158
+ return CounterToIncrement .BOTH ;
157
159
} else {
158
- return CountVoltageLimit .DEFAULT ;
160
+ return CounterToIncrement .DEFAULT ;
159
161
}
160
162
} else {
161
- return CountVoltageLimit .NONE ;
163
+ return CounterToIncrement .NONE ;
162
164
}
163
165
}
164
166
165
- private static CountVoltageLimit generateHighVoltageLimit (List <VoltageLimitOverride > specificVoltageLimits ,
166
- Map <String , VoltageLimitEntity > voltageLevelModificationLimits ,
167
- Map <String , VoltageLimitEntity > voltageLevelDefaultLimits ,
168
- boolean isHighVoltageLimitModificationSet ,
169
- boolean isHighVoltageLimitDefaultSet ,
170
- VoltageLevel voltageLevel ) {
167
+ private static CounterToIncrement generateHighVoltageLimit (List <VoltageLimitOverride > specificVoltageLimits ,
168
+ Map <String , VoltageLimitEntity > voltageLevelModificationLimits ,
169
+ Map <String , VoltageLimitEntity > voltageLevelDefaultLimits ,
170
+ boolean isHighVoltageLimitModificationSet ,
171
+ boolean isHighVoltageLimitDefaultSet ,
172
+ VoltageLevel voltageLevel ) {
171
173
if (!Double .isNaN (voltageLevel .getHighVoltageLimit ()) && isHighVoltageLimitModificationSet ) {
172
174
specificVoltageLimits .add (new VoltageLimitOverride (voltageLevel .getId (), VoltageLimitType .HIGH_VOLTAGE_LIMIT , true , voltageLevelModificationLimits .get (voltageLevel .getId ()).getHighVoltageLimit ()));
173
- return CountVoltageLimit .MODIFICATION ;
175
+ return CounterToIncrement .MODIFICATION ;
174
176
} else if (Double .isNaN (voltageLevel .getHighVoltageLimit ()) && isHighVoltageLimitDefaultSet ) {
175
177
specificVoltageLimits .add (new VoltageLimitOverride (voltageLevel .getId (), VoltageLimitType .HIGH_VOLTAGE_LIMIT , false , voltageLevelDefaultLimits .get (voltageLevel .getId ()).getHighVoltageLimit () + (isHighVoltageLimitModificationSet ? voltageLevelModificationLimits .get (voltageLevel .getId ()).getHighVoltageLimit () : 0. )));
176
178
if (isHighVoltageLimitModificationSet ) {
177
- return CountVoltageLimit .BOTH ;
179
+ return CounterToIncrement .BOTH ;
178
180
} else {
179
- return CountVoltageLimit .DEFAULT ;
181
+ return CounterToIncrement .DEFAULT ;
180
182
}
181
183
} else {
182
- return CountVoltageLimit .NONE ;
184
+ return CounterToIncrement .NONE ;
183
185
}
184
186
}
185
187
186
188
@ Transactional (readOnly = true )
187
189
public OpenReacParameters buildOpenReacParameters (VoltageInitRunContext context , Network network ) {
188
- final long startTime = System .nanoTime ();
190
+ AtomicReference < Long > startTime = new AtomicReference <>( System .nanoTime () );
189
191
final Reporter reporter = context .getRootReporter ().createSubReporter ("OpenReacParameters" , "OpenReac parameters" , Map .of (
190
192
"parameters_id" , new TypedValue (Objects .toString (context .getParametersUuid ()), "ID" )
191
193
));
@@ -214,7 +216,7 @@ public OpenReacParameters buildOpenReacParameters(VoltageInitRunContext context,
214
216
.toList ());
215
217
216
218
network .getVoltageLevelStream ()
217
- .filter (voltageLevel -> voltageLevelDefaultLimits .keySet (). contains ( voltageLevel .getId ()) || voltageLevelModificationLimits .keySet (). contains (voltageLevel .getId ()))
219
+ .filter (voltageLevel -> voltageLevelDefaultLimits .containsKey ( voltageLevel .getId ()) || voltageLevelModificationLimits .containsKey (voltageLevel .getId ()))
218
220
.forEach (voltageLevel -> fillSpecificVoltageLimits (specificVoltageLimits ,
219
221
counterMissingVoltageLimits , counterVoltageLimitModifications ,
220
222
voltageLevelModificationLimits , voltageLevelDefaultLimits ,
@@ -283,7 +285,7 @@ public OpenReacParameters buildOpenReacParameters(VoltageInitRunContext context,
283
285
//The optimizer will attach reactive slack variables to all buses
284
286
parameters .setReactiveSlackBusesMode (ReactiveSlackBusesMode .ALL );
285
287
286
- LOGGER .info ("Parameters built in {}s" , TimeUnit .NANOSECONDS .toSeconds (System .nanoTime () - startTime ));
288
+ LOGGER .info ("Parameters built in {}s" , TimeUnit .NANOSECONDS .toSeconds (System .nanoTime () - startTime . get () ));
287
289
return parameters ;
288
290
}
289
291
@@ -302,7 +304,7 @@ private List<String> toEquipmentIdsList(UUID networkUuid, String variantId, List
302
304
}
303
305
304
306
private static String voltageToString (double voltage ) {
305
- return Double .isNaN (voltage ) ? Double . toString (voltage ) : voltage + "\u202F kV" ;
307
+ return Double .isNaN (voltage ) ? DF . format (voltage ) : voltage + "\u202F kV" ;
306
308
}
307
309
308
310
private static String computeRelativeVoltageLevel (final double initialVoltageLimit , @ Nullable final VoltageLimitOverride override ) {
@@ -317,17 +319,7 @@ private static String computeRelativeVoltageLevel(final double initialVoltageLim
317
319
* We count modifications per substation only once in {@link #filterService}, not twice
318
320
*/
319
321
@ VisibleForTesting
320
- enum CountVoltageLimit {
321
- NONE , DEFAULT , MODIFICATION , BOTH ;
322
-
323
- public CountVoltageLimit merge (@ NonNull final CountVoltageLimit other ) {
324
- if (this == BOTH || other == BOTH || this == DEFAULT && other == MODIFICATION || this == MODIFICATION && other == DEFAULT ) {
325
- return BOTH ;
326
- } else if (this == NONE ) {
327
- return other ;
328
- } else { // other == NONE
329
- return this ;
330
- }
331
- }
322
+ enum CounterToIncrement {
323
+ NONE , DEFAULT , MODIFICATION , BOTH
332
324
}
333
325
}
0 commit comments