WCAUtils { public static void applyInjections(Network network, String stateId, Map<String, Float> injections) { Objects.requireNonNull(network); Objects.requireNonNull(stateId); Objects.requireNonNull(injections); String originalStateId = network.getStateManager().getWorkingStateId(); network.getStateManager().setWorkingState(stateId); injections.keySet().forEach(injection -> { Load load = network.getLoad(injection); if (load != null) { double oldP = load.getTerminal().getP(); LOGGER.debug("Network {}, state {}: incrementing P of load {} from {} to {}", network.getId(), network.getStateManager().getWorkingStateId(), injection, oldP, oldP + injections.get(injection)); load.getTerminal().setP(oldP + injections.get(injection)); load.setP0(oldP + injections.get(injection)); } else { Generator generator = network.getGenerator(injection); if (generator != null) { double oldP = generator.getTerminal().getP(); LOGGER.debug("Network {}, state {}: incrementing P of generator {} from {} to {}", network.getId(), network.getStateManager().getWorkingStateId(), injection, oldP, oldP + injections.get(injection)); generator.getTerminal().setP(oldP + injections.get(injection)); generator.setTargetP(-oldP - injections.get(injection)); } else { LOGGER.error("No load or generator with id {} in network {}: cannot apply the injection", injection, network.getId()); } } }); network.getStateManager().setWorkingState(originalStateId); } private  WCAUtils(); static void exportState(Network network, Path folder, int faultNum, int actionNum); static WCADomainsResult readDomainsResult(String domainsPrefix, Path workingDir, String uncertaintiesFile); static WCAClustersResult readClustersResult(String clustersPrefix, Path workingDir, String flowsFile, String uncertaintiesFile); static void writeContingencies(Collection<Contingency> contingencies, DataSource dataSource, StringToIntMapper<AmplSubset> mapper); static void writeActions(Collection<String> actionIds, DataSource dataSource, StringToIntMapper<AmplSubset> mapper,
                                     String title, AmplSubset amplSubset); static void applyInjections(Network network, String stateId, Map<String, Float> injections); static boolean containsViolation(List<LimitViolation> violations, LimitViolation violation);  }
@Test public void testApplyInjections() { Network network = NetworkTest1Factory.create(); network.getGenerator("generator1").getTerminal().setP(-network.getGenerator("generator1").getTargetP()); network.getLoad("load1").getTerminal().setP(network.getLoad("load1").getP0()); double loadP = network.getLoad("load1").getTerminal().getP(); double loadP0 = network.getLoad("load1").getP0(); double generatorP = network.getGenerator("generator1").getTerminal().getP(); double generatorTargetP = network.getGenerator("generator1").getTargetP(); Map<String, Float> injections = new HashMap<String, Float>(); injections.put("load1", 10f); injections.put("generator1", -10f); WCAUtils.applyInjections(network, StateManagerConstants.INITIAL_STATE_ID, injections); assertEquals(loadP + 10, network.getLoad("load1").getTerminal().getP(), 0.0); assertEquals(loadP0 + 10, network.getLoad("load1").getP0(), 0.0); assertEquals(generatorP - 10, network.getGenerator("generator1").getTerminal().getP(), 0.0); assertEquals(generatorTargetP + 10, network.getGenerator("generator1").getTargetP(), 0.0); }