private int getBusNum(Bus bus) { return bus == null ? -1 : mapper.getInt(AmplSubset.BUS, bus.getId()); }
private static void createLimitsIds(StringToIntMapper<AmplSubset> mapper, CurrentLimits limits, String branchId, String sideId) { for (CurrentLimits.TemporaryLimit tl : limits.getTemporaryLimits()) { String limitId = branchId + "_" + sideId + "_" + tl.getAcceptableDuration(); mapper.newInt(AmplSubset.TEMPORARY_CURRENT_LIMIT, limitId); } }
@Test public void test() throws IOException { StringToIntMapper<TestSubset> mapper = new StringToIntMapper<>(TestSubset.class); testAddMapping(mapper); mapper.reset(TestSubset.TYPE); testAddMapping(mapper); try { mapper.reset(null); fail(); } catch (IllegalArgumentException ignored) { } }
private void testAddMapping(StringToIntMapper<TestSubset> mapper) throws IOException { String value = "value1"; assertFalse(mapper.isMapped(TestSubset.TYPE, value)); try { mapper.getId(TestSubset.TYPE, 1); fail(); } catch (IllegalArgumentException ignored) { mapper.getInt(TestSubset.TYPE, value); fail(); } catch (IllegalStateException ignored) { int num = mapper.newInt(TestSubset.TYPE, value); assertTrue(mapper.isMapped(TestSubset.TYPE, value)); assertEquals(1, num); assertEquals(value, mapper.getId(TestSubset.TYPE, 1)); assertEquals(1, mapper.getInt(TestSubset.TYPE, value)); num = mapper.newInt(TestSubset.TYPE, value); assertTrue(mapper.isMapped(TestSubset.TYPE, value)); assertEquals(2, num); assertEquals(value, mapper.getId(TestSubset.TYPE, 2)); assertEquals(2, mapper.getInt(TestSubset.TYPE, value)); mapper.dump(writer); assertEquals(content, writer.toString().trim()); mapper.newInt(null, value); fail(); } catch (IllegalArgumentException ignored) {
private Void readPhaseTapChanger(String[] tokens) { int num = Integer.parseInt(tokens[1]); int tap = Integer.parseInt(tokens[2]); String id = mapper.getId(AmplSubset.PHASE_TAP_CHANGER, num); TwoWindingsTransformer twt = network.getTwoWindingsTransformer(id); if (twt == null) { throw new AmplException("Invalid two windings transformer id '" + id + "'"); } PhaseTapChanger ptc = twt.getPhaseTapChanger(); ptc.setTapPosition(ptc.getLowTapPosition() + tap - 1); return null; }
public static void resetNetworkMapping(StringToIntMapper<AmplSubset> mapper) { mapper.reset(AmplSubset.BUS); mapper.reset(AmplSubset.VOLTAGE_LEVEL); mapper.reset(AmplSubset.BRANCH); mapper.reset(AmplSubset.RATIO_TAP_CHANGER); mapper.reset(AmplSubset.PHASE_TAP_CHANGER); mapper.reset(AmplSubset.TAP_CHANGER_TABLE); mapper.reset(AmplSubset.LOAD); mapper.reset(AmplSubset.SHUNT); mapper.reset(AmplSubset.GENERATOR); mapper.reset(AmplSubset.TEMPORARY_CURRENT_LIMIT); mapper.reset(AmplSubset.THREE_WINDINGS_TRANSFO); mapper.reset(AmplSubset.STATIC_VAR_COMPENSATOR); mapper.reset(AmplSubset.HVDC_LINE); }
public static StringToIntMapper<AmplSubset> createMapper(Network network) { StringToIntMapper<AmplSubset> mapper = new StringToIntMapper<>(AmplSubset.class); fillMapper(mapper, network); return mapper; }
@Override public List<CommandExecution> before(Path workingDir) throws IOException { network.getStateManager().setWorkingState(workingStateId); // copy AMPL model for (String amplModelFileName : AMPL_MODEL_FILE_NAMES) { Files.copy(getClass().getResourceAsStream("/ampl/projector/" + amplModelFileName), workingDir.resolve(amplModelFileName)); } // copy the generators domains file Files.copy(config.getGeneratorsDomainsFile(), workingDir.resolve(AMPL_GENERATORS_DOMAINS_FILE_NAME)); // write input data mapper = AmplUtil.createMapper(network); mapper.dump(workingDir.resolve("mapper.csv")); new AmplNetworkWriter(network, new FileDataSource(workingDir, "ampl"), // "ampl_network_" mapper, new AmplExportConfig(AmplExportConfig.ExportScope.ALL, true, AmplExportConfig.ExportActionType.CURATIVE)) .write(); Command command = new SimpleCommandBuilder() .id("projector") .program(config.getAmplHomeDir().resolve("ampl").toString()) .args("projector.run") .build(); return Collections.singletonList(new CommandExecution(command, 1, 0)); }
private void busConnection(Terminal t, int busNum) { if (busNum == -1) { t.disconnect(); } else { String busId = mapper.getId(AmplSubset.BUS, busNum); Bus connectable = AmplUtil.getConnectableBus(t); if (connectable != null && connectable.getId().equals(busId)) { t.connect(); } } }
private static void fillDanglingLines(StringToIntMapper<AmplSubset> mapper, Network network) { for (DanglingLine dl : network.getDanglingLines()) { mapper.newInt(AmplSubset.VOLTAGE_LEVEL, dl.getId()); mapper.newInt(AmplSubset.BUS, dl.getId()); mapper.newInt(AmplSubset.BRANCH, dl.getId()); mapper.newInt(AmplSubset.LOAD, dl.getId()); // limits if (dl.getCurrentLimits() != null) { createLimitsIds(mapper, dl.getCurrentLimits(), dl.getId(), ""); } } }
private Void readBus(String[] tokens) { int num = Integer.parseInt(tokens[1]); double v = readDouble(tokens[2]); double theta = readDouble(tokens[3]); String id = mapper.getId(AmplSubset.BUS, num); Bus bus = buses.get(id); if (bus != null) { bus.setV(v * bus.getVoltageLevel().getNominalV()); bus.setAngle(Math.toDegrees(theta)); } else { throw new AmplException("Invalid bus id '" + id + "'"); } return null; }
public static int getConnectableBusNum(StringToIntMapper<AmplSubset> mapper, Terminal t) { Bus bus = getConnectableBus(t); return bus == null ? -1 : mapper.getInt(AmplSubset.BUS, bus.getId()); }
public static void fillMapper(StringToIntMapper<AmplSubset> mapper, Network network) { // Voltage levels network.getVoltageLevelStream().forEach(vl -> mapper.newInt(AmplSubset.VOLTAGE_LEVEL, vl.getId())); // Buses getBuses(network).forEach(b -> mapper.newInt(AmplSubset.BUS, b.getId())); // Lines fillLines(mapper, network); // Two windings transformers fillTwoWindingsTransformers(mapper, network); // Three windings transformers fillThreeWindingsTransformers(mapper, network); // Dangling lines fillDanglingLines(mapper, network); // loads network.getLoadStream().forEach(l -> mapper.newInt(AmplSubset.LOAD, l.getId())); // shunts network.getShuntCompensatorStream().forEach(sc -> mapper.newInt(AmplSubset.SHUNT, sc.getId())); // generators network.getGeneratorStream().forEach(g -> mapper.newInt(AmplSubset.GENERATOR, g.getId())); // static var compensators network.getStaticVarCompensatorStream().forEach(svc -> mapper.newInt(AmplSubset.STATIC_VAR_COMPENSATOR, svc.getId())); // HVDC lines network.getHvdcLineStream().forEach(hvdc -> mapper.newInt(AmplSubset.HVDC_LINE, hvdc.getId())); // HvdcConverterStations network.getHvdcConverterStations().forEach(conv -> mapper.newInt(conv.getHvdcType().equals(HvdcType.VSC) ? AmplSubset.VSC_CONVERTER_STATION : AmplSubset.LCC_CONVERTER_STATION, conv.getId())); }
private Void readHvdcLine(String[] tokens) { int num = Integer.parseInt(tokens[1]); String converterMode = tokens[2].replace("\"", ""); double targetP = readDouble(tokens[3]); String id = mapper.getId(AmplSubset.HVDC_LINE, num); HvdcLine hl = network.getHvdcLine(id); if (hl == null) { throw new AmplException("Invalid HvdcLine id '" + id + "'"); } hl.setConvertersMode(ConvertersMode.valueOf(converterMode)); hl.setActivePowerSetpoint(targetP); return null; }
public static int getBusNum(StringToIntMapper<AmplSubset> mapper, Terminal t) { Bus bus = getBus(t); return bus == null ? -1 : mapper.getInt(AmplSubset.BUS, bus.getId()); }
private static void fillLines(StringToIntMapper<AmplSubset> mapper, Network network) { for (Line l : network.getLines()) { mapper.newInt(AmplSubset.BRANCH, l.getId()); if (l.isTieLine()) { TieLine tl = (TieLine) l; mapper.newInt(AmplSubset.VOLTAGE_LEVEL, AmplUtil.getXnodeVoltageLevelId(tl)); mapper.newInt(AmplSubset.BUS, AmplUtil.getXnodeBusId(tl)); mapper.newInt(AmplSubset.BRANCH, tl.getHalf1().getId()); mapper.newInt(AmplSubset.BRANCH, tl.getHalf2().getId()); } // limits if (l.getCurrentLimits1() != null) { createLimitsIds(mapper, l.getCurrentLimits1(), l.getId(), "_1_"); } if (l.getCurrentLimits2() != null) { createLimitsIds(mapper, l.getCurrentLimits2(), l.getId(), "_2_"); } } }
private Void readShunt(String[] tokens) { int num = Integer.parseInt(tokens[1]); int busNum = Integer.parseInt(tokens[2]); double q = readDouble(tokens[4]); int sections = Integer.parseInt(tokens[5]); String id = mapper.getId(AmplSubset.SHUNT, num); ShuntCompensator sc = network.getShuntCompensator(id); if (sc == null) { throw new AmplException("Invalid shunt compensator id '" + id + "'"); } sc.setCurrentSectionCount(Math.max(0, Math.min(sc.getMaximumSectionCount(), sections))); Terminal t = sc.getTerminal(); t.setQ(q); busConnection(t, busNum); return null; }
public static void writeActions(Collection<String> actionIds, DataSource dataSource, StringToIntMapper<AmplSubset> mapper, String title, AmplSubset amplSubset) { Objects.requireNonNull(actionIds); Objects.requireNonNull(dataSource); Objects.requireNonNull(mapper); Objects.requireNonNull(title); Objects.requireNonNull(amplSubset); try (Writer writer = new OutputStreamWriter(dataSource.newOutputStream(WCAConstants.ACTIONS_FILE_SUFFIX, WCAConstants.TXT_EXT, false), StandardCharsets.UTF_8); TableFormatter formatter = new AmplDatTableFormatter( writer, title, AmplConstants.INVALID_FLOAT_VALUE, true, AmplConstants.LOCALE, new Column("num"), new Column("id"))) { for (String actionId : actionIds) { int actionNum = mapper.getInt(amplSubset, actionId); formatter.writeCell(actionNum) .writeCell(actionId); } } catch (IOException e) { throw new RuntimeException(e); } }
private static void fillTwoWindingsTransformers(StringToIntMapper<AmplSubset> mapper, Network network) { for (TwoWindingsTransformer twt : network.getTwoWindingsTransformers()) { mapper.newInt(AmplSubset.BRANCH, twt.getId()); if (twt.getRatioTapChanger() != null) { mapper.newInt(AmplSubset.TAP_CHANGER_TABLE, twt.getId() + "_ratio_table"); mapper.newInt(AmplSubset.RATIO_TAP_CHANGER, twt.getId()); } if (twt.getPhaseTapChanger() != null) { mapper.newInt(AmplSubset.TAP_CHANGER_TABLE, twt.getId() + "_phase_table"); mapper.newInt(AmplSubset.PHASE_TAP_CHANGER, twt.getId()); } // limits if (twt.getCurrentLimits1() != null) { createLimitsIds(mapper, twt.getCurrentLimits1(), twt.getId(), "_1_"); } if (twt.getCurrentLimits2() != null) { createLimitsIds(mapper, twt.getCurrentLimits2(), twt.getId(), "_2_"); } } }