Skip to content

Commit

Permalink
Merge branch 'master' into eurostag_forbidden_chars
Browse files Browse the repository at this point in the history
  • Loading branch information
massimo-ferraro committed Mar 20, 2017
2 parents d613a18 + be2e826 commit 40c8481
Show file tree
Hide file tree
Showing 16 changed files with 88 additions and 133 deletions.
@@ -1,27 +1,23 @@
/**
* Copyright (c) 2016, RTE (http://www.rte-france.com)
* 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.modules.constraints;

import java.util.HashSet;
import java.util.List;
import java.util.Objects;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import eu.itesla_project.iidm.network.Line;
import eu.itesla_project.iidm.network.Network;
import eu.itesla_project.iidm.network.StateManager;
import eu.itesla_project.iidm.network.TwoTerminalsConnectable;
import eu.itesla_project.iidm.network.TwoWindingsTransformer;
import eu.itesla_project.iidm.network.VoltageLevel;
import eu.itesla_project.security.LimitViolation;
import eu.itesla_project.security.LimitViolationFilter;
import eu.itesla_project.security.LimitViolationType;
import eu.itesla_project.security.Security;

/**
Expand Down Expand Up @@ -96,7 +92,7 @@ public void looseConstraints(String stateId, List<LimitViolation> violations, fl
for (LimitViolation violation : filteredViolations) {
LOGGER.debug("Fixing the constraints causing the {} violation on equipment {}",
violation.getLimitType(),
violation.getSubject().getId());
violation.getSubjectId());
switch (violation.getLimitType()) {
case CURRENT:
setNewCurrentLimit(stateId, violation, margin, applyToBaseCase);
Expand All @@ -116,13 +112,10 @@ public void looseConstraints(String stateId, List<LimitViolation> violations, fl
}

private void setNewCurrentLimit(String stateId, LimitViolation violation, float margin, boolean applyToBaseCase) {
TwoTerminalsConnectable violatedBranch = (TwoTerminalsConnectable) violation.getSubject();
// not sure if I need to reload the branch from the network ...
TwoTerminalsConnectable branch = null;
if ( violatedBranch instanceof Line )
branch = network.getLine(violatedBranch.getId());
else if ( violatedBranch instanceof TwoWindingsTransformer )
branch = network.getTwoWindingsTransformer(violatedBranch.getId());
TwoTerminalsConnectable branch = network.getLine(violation.getSubjectId());
if ( branch == null ) {
branch = network.getTwoWindingsTransformer(violation.getSubjectId());
}
if ( branch != null ) {
float newLimit = getNewUpperLimit(violation, margin);
if ( branch.getTerminal1().getI() == violation.getValue() ) {
Expand All @@ -133,24 +126,17 @@ else if ( violatedBranch instanceof TwoWindingsTransformer )
newLimit);
branch.newCurrentLimits1().setPermanentLimit(newLimit).add();
if ( applyToBaseCase && !StateManager.INITIAL_STATE_ID.equals(stateId) ) { // change the limit also to basecase
String initialStateId = StateManager.INITIAL_STATE_ID;
network.getStateManager().setWorkingState(initialStateId);
if ( violatedBranch instanceof Line )
branch = network.getLine(violatedBranch.getId());
else if ( violatedBranch instanceof TwoWindingsTransformer )
branch = network.getTwoWindingsTransformer(violatedBranch.getId());
if ( branch != null ) {
LOGGER.debug("State {}: changing current limit 1 of branch {}: {} -> {}",
initialStateId,
network.getStateManager().setWorkingState(StateManager.INITIAL_STATE_ID);
branch = network.getLine(violation.getSubjectId());
if ( branch == null ) {
branch = network.getTwoWindingsTransformer(violation.getSubjectId());
}
LOGGER.debug("State {}: changing current limit 1 of branch {}: {} -> {}",
StateManager.INITIAL_STATE_ID,
branch.getId(),
violation.getLimit(),
newLimit);
branch.newCurrentLimits1().setPermanentLimit(newLimit).add();
} else {
LOGGER.warn("State {}: cannot change current limit of branch {}: no branch with this id in the network",
initialStateId,
violatedBranch.getId());
}
branch.newCurrentLimits1().setPermanentLimit(newLimit).add();
network.getStateManager().setWorkingState(stateId);
}
} else if ( branch.getTerminal2().getI() == violation.getValue() ) {
Expand All @@ -161,37 +147,29 @@ else if ( violatedBranch instanceof TwoWindingsTransformer )
newLimit);
branch.newCurrentLimits2().setPermanentLimit(newLimit).add();
if ( applyToBaseCase && !StateManager.INITIAL_STATE_ID.equals(stateId) ) { // change the limit also to basecase
String initialStateId = StateManager.INITIAL_STATE_ID;
network.getStateManager().setWorkingState(initialStateId);
if ( violatedBranch instanceof Line )
branch = network.getLine(violatedBranch.getId());
else if ( violatedBranch instanceof TwoWindingsTransformer )
branch = network.getTwoWindingsTransformer(violatedBranch.getId());
if ( branch != null ) {
LOGGER.debug("State {}: changing current limit 2 of branch {}: {} -> {}",
initialStateId,
network.getStateManager().setWorkingState(StateManager.INITIAL_STATE_ID);
branch = network.getLine(violation.getSubjectId());
if ( branch == null ) {
branch = network.getTwoWindingsTransformer(violation.getSubjectId());
}
LOGGER.debug("State {}: changing current limit 2 of branch {}: {} -> {}",
StateManager.INITIAL_STATE_ID,
branch.getId(),
violation.getLimit(),
newLimit);
branch.newCurrentLimits2().setPermanentLimit(newLimit).add();
} else {
LOGGER.warn("State {}: cannot change current limit of branch {}: no branch with this id in the network",
initialStateId,
violatedBranch.getId());
}
branch.newCurrentLimits2().setPermanentLimit(newLimit).add();
network.getStateManager().setWorkingState(stateId);
}
}
} else {
LOGGER.warn("State {}: cannot change current limit of branch {}: no branch with this id in the network",
stateId,
violatedBranch.getId());
violation.getSubjectId());
}
}

private void setNewHighVoltageLimit(String stateId, LimitViolation violation, float margin, boolean applyToBaseCase) {
VoltageLevel violatedVoltageLevel = (VoltageLevel) violation.getSubject();
VoltageLevel voltageLevel = network.getVoltageLevel(violatedVoltageLevel.getId());
VoltageLevel voltageLevel = network.getVoltageLevel(violation.getSubjectId());
if ( voltageLevel != null ) {
if ( violation.getValue() > voltageLevel.getHighVoltageLimit() ) { // it could already have been fixed
float newLimit = getNewUpperLimit(violation, margin);
Expand All @@ -202,34 +180,26 @@ private void setNewHighVoltageLimit(String stateId, LimitViolation violation, fl
newLimit);
voltageLevel.setHighVoltageLimit(newLimit);
if ( applyToBaseCase && !StateManager.INITIAL_STATE_ID.equals(stateId) ) { // change the limit also to basecase
String initialStateId = StateManager.INITIAL_STATE_ID;
network.getStateManager().setWorkingState(initialStateId);
voltageLevel = network.getVoltageLevel(violatedVoltageLevel.getId());
if ( voltageLevel != null ) {
LOGGER.debug("State {}: changing high voltage limit of voltage level {}: {} -> {}",
initialStateId,
network.getStateManager().setWorkingState(StateManager.INITIAL_STATE_ID);
voltageLevel = network.getVoltageLevel(violation.getSubjectId());
LOGGER.debug("State {}: changing high voltage limit of voltage level {}: {} -> {}",
StateManager.INITIAL_STATE_ID,
voltageLevel.getId(),
violation.getLimit(),
newLimit);
voltageLevel.setHighVoltageLimit(newLimit);
} else {
LOGGER.warn("State {}: cannot change high voltage limit of voltage level {}: no voltage level with this id in the network",
initialStateId,
violatedVoltageLevel.getId());
}
voltageLevel.setHighVoltageLimit(newLimit);
network.getStateManager().setWorkingState(stateId);
}
}
} else {
LOGGER.warn("State {}: cannot change high voltage limit of voltage level {}: no voltage level with this id in the network",
stateId,
violatedVoltageLevel.getId());
violation.getSubjectId());
}
}

private void setNewLowVoltageLimit(String stateId, LimitViolation violation, float margin, boolean applyToBaseCase) {
VoltageLevel violatedVoltageLevel = (VoltageLevel) violation.getSubject();
VoltageLevel voltageLevel = network.getVoltageLevel(violatedVoltageLevel.getId());
VoltageLevel voltageLevel = network.getVoltageLevel(violation.getSubjectId());
if ( voltageLevel != null ) {
if ( violation.getValue() < voltageLevel.getLowVoltageLimit() ) { // it could already have been fixed
float newLimit = getNewLowerLimit(violation, margin);
Expand All @@ -240,28 +210,20 @@ private void setNewLowVoltageLimit(String stateId, LimitViolation violation, flo
newLimit);
voltageLevel.setLowVoltageLimit(newLimit);
if ( applyToBaseCase && !StateManager.INITIAL_STATE_ID.equals(stateId) ) { // change the limit also to basecase
String initialStateId = StateManager.INITIAL_STATE_ID;
network.getStateManager().setWorkingState(initialStateId);
voltageLevel = network.getVoltageLevel(violatedVoltageLevel.getId());
if ( voltageLevel != null ) {
LOGGER.debug("State {}: changing low voltage limit of voltage level {}: {} -> {}",
initialStateId,
network.getStateManager().setWorkingState(StateManager.INITIAL_STATE_ID);
voltageLevel = network.getVoltageLevel(violation.getSubjectId());
LOGGER.debug("State {}: changing low voltage limit of voltage level {}: {} -> {}",
StateManager.INITIAL_STATE_ID,
voltageLevel.getId(),
violation.getLimit(),
newLimit);
voltageLevel.setLowVoltageLimit(newLimit);
} else {
LOGGER.warn("State {}: cannot change high voltage limit of voltage level {}: no voltage level with this id in the network",
initialStateId,
violatedVoltageLevel.getId());
}
network.getStateManager().setWorkingState(stateId);
voltageLevel.setLowVoltageLimit(newLimit);
}
}
} else {
LOGGER.warn("State {}: cannot change low voltage limit of voltage level {}: no voltage level with this id in the network",
stateId,
violatedVoltageLevel.getId());
violation.getSubjectId());
}
}

Expand Down
Expand Up @@ -45,7 +45,7 @@ public void setUp() throws Exception {
fileSystem = ShrinkWrapFileSystems.newFileSystem(archive);
platformConfig = new InMemoryPlatformConfig(fileSystem);
network = ConstraintsModifierTestUtils.getNetwork();
violations = ConstraintsModifierTestUtils.getViolations(network);
violations = ConstraintsModifierTestUtils.getViolations();
}

@After
Expand Down
Expand Up @@ -36,7 +36,7 @@ public class ConstraintsModifierTest {
@Before
public void setUp() throws Exception {
network = ConstraintsModifierTestUtils.getNetwork();
violations = ConstraintsModifierTestUtils.getViolations(network);
violations = ConstraintsModifierTestUtils.getViolations();
Set<LimitViolationType> violationTypes = EnumSet.of(LimitViolationType.CURRENT,
LimitViolationType.HIGH_VOLTAGE,
LimitViolationType.LOW_VOLTAGE);
Expand Down
Expand Up @@ -98,14 +98,11 @@ public static Network getNetwork() {
return n;
}

public static List<LimitViolation> getViolations(Network network) {
public static List<LimitViolation> getViolations() {
List<LimitViolation> violations = new ArrayList<LimitViolation>();
Line line = network.getLine(LINE_ID);
violations.add(new LimitViolation(line, LimitViolationType.CURRENT, CURRENT_LIMIT, null, 1, CURRENT_VALUE, COUNTRY, Float.NaN));
VoltageLevel voltageLevel = network.getVoltageLevel(VOLTAGE_LEVEL_1_ID);
violations.add(new LimitViolation(voltageLevel, LimitViolationType.HIGH_VOLTAGE, HIGH_VOLTAGE_LIMIT, null, 1, V, COUNTRY, Float.NaN));
VoltageLevel voltageLevel2 = network.getVoltageLevel(VOLTAGE_LEVEL_2_ID);
violations.add(new LimitViolation(voltageLevel2, LimitViolationType.LOW_VOLTAGE, LOW_VOLTAGE_LIMIT, null, 1, V, COUNTRY, Float.NaN));
violations.add(new LimitViolation(LINE_ID, LimitViolationType.CURRENT, CURRENT_LIMIT, null, 1, CURRENT_VALUE, COUNTRY, Float.NaN));
violations.add(new LimitViolation(VOLTAGE_LEVEL_1_ID, LimitViolationType.HIGH_VOLTAGE, HIGH_VOLTAGE_LIMIT, null, 1, V, COUNTRY, Float.NaN));
violations.add(new LimitViolation(VOLTAGE_LEVEL_2_ID, LimitViolationType.LOW_VOLTAGE, LOW_VOLTAGE_LIMIT, null, 1, V, COUNTRY, Float.NaN));
return violations;
}

Expand Down
Expand Up @@ -143,7 +143,7 @@ public WorkflowResult getWorkflowResult(String processId, String workflowId) thr
PreContingencyResult pcr = new PreContingencyResult(state, viols.isEmpty(),
status != null && "SUCCESS".equals(status));
viols.forEach(lv -> {
pcr.addViolation(new Violation(lv.getCountry().toString(), lv.getSubject().getId(),
pcr.addViolation(new Violation(lv.getCountry().toString(), lv.getSubjectId(),
lv.getLimitType().name(), lv.getLimit(), lv.getValue(), (int) lv.getBaseVoltage()));
});
res.addPreContingency(pcr);
Expand Down Expand Up @@ -188,7 +188,7 @@ public WorkflowResult getWorkflowResult(String processId, String workflowId) thr
srMap.put(state, sr);

contViols.forEach(lv -> {
sr.addViolation(new Violation(lv.getCountry().toString(), lv.getSubject().getId(),
sr.addViolation(new Violation(lv.getCountry().toString(), lv.getSubjectId(),
lv.getLimitType().name(), lv.getLimit(), lv.getValue(),
(int) lv.getBaseVoltage()));
});
Expand Down Expand Up @@ -288,7 +288,7 @@ public ProcessSynthesis getSynthesis(String processId) throws Exception {
private void fillViolationSynthesis( DateTime dateTime, List<ViolationSynthesis> violationList , List<LimitViolation> limitList ){
limitList.forEach( lv ->{
LimitViolationType violationType = lv.getLimitType();
String equipment = lv.getSubject().getId();
String equipment = lv.getSubjectId();
float limit = lv.getLimit();
float value = lv.getValue();
ViolationSynthesis synt;
Expand Down
Expand Up @@ -324,8 +324,7 @@ public Map<Integer, List<LimitViolation>> getViolations(String workflowId, Onlin
return null;
Map<Integer, List<LimitViolation>> res = new HashMap<Integer, List<LimitViolation>>();
List<LimitViolation> viols = new ArrayList<>();
LimitViolation lv = new LimitViolation(network.getIdentifiable("sub1"), LimitViolationType.CURRENT, 100,
"HIGH_CURRENT", 0, 110, Country.FR, 240);
LimitViolation lv = new LimitViolation("sub1", LimitViolationType.CURRENT, 100, "HIGH_CURRENT", 0, 110, Country.FR, 240);
viols.add(lv);
res.put(0, viols);
return res;
Expand Down Expand Up @@ -374,8 +373,7 @@ public Map<Integer, Map<String, List<LimitViolation>>> getPostContingencyViolati
Map<Integer, Map<String, List<LimitViolation>>> res = new HashMap<>();
Map<String, List<LimitViolation>> mmap = new HashMap<>();
List<LimitViolation> viols = new ArrayList<>();
LimitViolation lv = new LimitViolation(network.getIdentifiable("sub1"), LimitViolationType.CURRENT, 100,
"HIGH_CURRENT", 0, 200, Country.FR, 120);
LimitViolation lv = new LimitViolation("sub1", LimitViolationType.CURRENT, 100, "HIGH_CURRENT", 0, 200, Country.FR, 120);
viols.add(lv);
mmap.put("test_contingency", viols);
res.put(0, mmap);
Expand Down
Expand Up @@ -455,7 +455,7 @@ private Path getFile(Path folder, String filename) {
private void writeCsvViolations(String basecase, List<LimitViolation> networkViolations, CsvWriter cvsWriter)
throws IOException {
for (LimitViolation violation : networkViolations) {
String[] values = new String[] { basecase, violation.getSubject().getId(), violation.getLimitType().name(),
String[] values = new String[] { basecase, violation.getSubjectId(), violation.getLimitType().name(),
Float.toString(violation.getValue()), Float.toString(violation.getLimit()) };
cvsWriter.writeRecord(values);
}
Expand Down
Expand Up @@ -1286,7 +1286,7 @@ private void storeViolations(String workflowId, String mapName, List<LimitViolat
Map<String, String> metricsMap = wfMVStore.openMap(mapName, mapBuilder);
int violationIndex = 0;
for (LimitViolation limitViolation : violations) {
String violationId = limitViolation.getSubject().getId() + "_" + violationIndex;
String violationId = limitViolation.getSubjectId() + "_" + violationIndex;
metricsMap.put(violationId, OnlineDbMVStoreUtils.limitViolationToJson(limitViolation));
violationIndex++;
}
Expand Down

0 comments on commit 40c8481

Please sign in to comment.