Skip to content

Commit

Permalink
adds dta mock + reg files for the SVC test network case
Browse files Browse the repository at this point in the history
  • Loading branch information
Christian Biasuzzi committed Mar 20, 2017
1 parent be2e826 commit 348e8f2
Show file tree
Hide file tree
Showing 10 changed files with 253 additions and 27 deletions.
6 changes: 6 additions & 0 deletions iidm-ddb/iidm-ddb-eurostag-import-export/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,12 @@
<groupId>junit</groupId>
<artifactId>junit</artifactId>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-simple</artifactId>
<scope>test</scope>
</dependency>

</dependencies>

<build>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,13 @@
package eu.itesla_project.iidm.ddb.eurostag_imp_exp;

import com.google.common.base.Strings;
import com.google.common.collect.ImmutableMap;
import com.google.common.io.ByteStreams;
import com.google.common.io.CharStreams;
import eu.itesla_project.iidm.network.Bus;
import eu.itesla_project.iidm.network.Generator;
import eu.itesla_project.iidm.network.Network;
import eu.itesla_project.iidm.network.StaticVarCompensator;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

Expand All @@ -29,11 +31,50 @@
class DynamicDatabaseMock implements DynamicDatabaseClient {

private static final String MINIMAL_DTA_TEMPLATE = "/sim_min.dta";
private static final String MINIMAL_DTA_SVC_TEMPLATE = "/sim_min_svc.dta";
private static final List<String> MOCK_REG_FILES_PREFIXES = Arrays.asList("dummefd", "dummycm");
private static final List<String> MOCK_REG_FILES_SVC_PREFIXES = Arrays.asList("dummefd", "dummycm", "interdum");
private static final List<String> REG_EXTENSIONS = Arrays.asList("fri", "frm", "par", "pcp", "rcp");

private static final Logger LOGGER = LoggerFactory.getLogger(DynamicDatabaseMock.class);

private void copyRegulatorsFiles(List<String> regulatorsNames, Path workingDir) {
regulatorsNames.stream()
.flatMap(filePrefix -> REG_EXTENSIONS.stream().map(fileSuffix -> filePrefix + "." + fileSuffix))
.forEach(filename -> {
try (final InputStream reader = getClass().getResourceAsStream("/" + filename)) {
LOGGER.info("copying regulator file: {}", filename);
Files.write(workingDir.resolve(filename), ByteStreams.toByteArray(reader));
} catch (IOException e) {
LOGGER.error(e.getMessage(), e);
throw new RuntimeException(e.getMessage(), e);
}
});
}

private void copyDtaFile(String templateName, Path destFile, Map<String, String> mapping) throws IOException {
try (final Reader reader = new InputStreamReader(getClass().getResourceAsStream(templateName))) {
String dtaContents = CharStreams.toString(reader);
dtaContents = mapping.keySet().stream()
.reduce(dtaContents, (str, key) -> str.replaceAll(key, mapping.get(key)));
try (BufferedWriter writer = Files.newBufferedWriter(destFile)) {
writer.write(dtaContents);
}
}
}

private void copyDynamicFiles(String templateName, Path workingDir, String fileName, Map<String, String> mapping, List<String> regulatorFiles) {
try {
//change IDs (machines/nodes) in a template according to a mapping and write the result to a file
copyDtaFile(templateName, workingDir.resolve(fileName), mapping);
//copy a list of regulators dummy files to a directory
copyRegulatorsFiles(regulatorFiles, workingDir);
} catch (Exception e) {
throw new RuntimeException(e.getMessage(), e);
}
}


@Override
public void dumpDtaFile(Path workingDir, String fileName, Network network, Map<String, Character> parallelIndexes, String eurostagVersion, Map<String, String> iidm2eurostagId) {
Objects.requireNonNull(workingDir);
Expand All @@ -43,6 +84,8 @@ public void dumpDtaFile(Path workingDir, String fileName, Network network, Map<S
Objects.requireNonNull(eurostagVersion);
Objects.requireNonNull(iidm2eurostagId);

LOGGER.info("exporting dynamic data for network: {}", network.getId());

//uses the first connected generator that is available in the iidm2eurostag map
Generator generator = network.getGeneratorStream()
.filter(gen -> ((iidm2eurostagId.containsKey(gen.getId())) && (gen.getTerminal().isConnected())))
Expand All @@ -59,32 +102,30 @@ public void dumpDtaFile(Path workingDir, String fileName, Network network, Map<S
LOGGER.info("generator: iidm {}, eurostag {}", generator.getId(), mappedGenName);
LOGGER.info("node: iidm {}, eurostag {}", generator.getTerminal().getBusBreakerView().getConnectableBus().getId(), mappedNodeName);

try {
try (final Reader reader = new InputStreamReader(getClass().getResourceAsStream(MINIMAL_DTA_TEMPLATE))) {
String dtaContents = CharStreams.toString(reader);

//change the connection node name, according to the current network and the iidm2eurostag mapping
String newDtaContents = dtaContents
.replace("NODENAME", mappedNodeName)
.replace("MINIMALI", mappedGenName);
try (BufferedWriter writer = Files.newBufferedWriter(workingDir.resolve(fileName))) {
writer.write(newDtaContents);
}
}
//copy the regulators dummy files
MOCK_REG_FILES_PREFIXES.stream()
.flatMap(filePrefix -> REG_EXTENSIONS.stream().map(fileSuffix -> filePrefix + "." + fileSuffix))
.forEach(filename -> {
try (final InputStream reader = getClass().getResourceAsStream("/" + filename)) {
LOGGER.info("copying regulator file: {}", filename);
Files.write(workingDir.resolve(filename), ByteStreams.toByteArray(reader));
} catch (IOException e) {
LOGGER.error(e.getMessage(), e);
throw new RuntimeException(e.getMessage(), e);
}
});
} catch (Exception e) {
throw new RuntimeException(e.getMessage(), e);
//Mock DB, driven by network ID
switch (network.getId()) {

// the test SVC network
case "svcTestCase":
//uses the first connected SVC that is available in the iidm2eurostag map
StaticVarCompensator svc1 = network.getStaticVarCompensatorStream()
.filter(svc -> ((iidm2eurostagId.containsKey(svc.getId())) && (svc.getTerminal().isConnected())))
.findFirst()
.orElseThrow(() -> new RuntimeException("could not find a suitable svc in network: " + network + ", to be used in: "));
String mappedSvcName = iidm2eurostagId.get(svc1.getId());
LOGGER.info("svc: iidm {}, eurostag {}", svc1.getId(), mappedSvcName);

copyDynamicFiles(MINIMAL_DTA_SVC_TEMPLATE, workingDir, fileName,
ImmutableMap.of("NODENAME", mappedNodeName, "MINIMALI", mappedGenName, "SVCDUMMY", mappedSvcName),
MOCK_REG_FILES_SVC_PREFIXES);

break;


default:
copyDynamicFiles(MINIMAL_DTA_TEMPLATE, workingDir, fileName,
ImmutableMap.of("NODENAME", mappedNodeName, "MINIMALI", mappedGenName),
MOCK_REG_FILES_PREFIXES);
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
HEADER 06/03/17 5.1
70 84
0
0
0
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
HEADER 06/03/17 5.1
70 84
2
? ?
? ?
? ?
? ?
? ?
? ?
? ?
_B _G
? ?
? ?
? ?
? ?
? ?
$B $G
1 2
7535 7535
8948 9068
26 26
0 0
1 2
0
0
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
HEADER 06/03/17 5.1

_end_comment_

INTERDUM 1


Binary file not shown.
Binary file not shown.
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
HEADER 06/03/17 5.1

M1 S 0. 0.
MINIMALI NODENAME 1150. 24. 1. 1. 0. 6.3
0.004 0.219 2.351 0. 0.01723 0.12825 0.00113 0.24253
2.351 0.0193 0.08921 0.03923 1.78484 3
N 1000. 1100.
0.1 6.025 0.1 6.025

R MINIMALI
DUMMYCM 1



R MINIMALI
DUMMEFD 1




M21 0. 0.
SVCDUMMY C 0 0 250.

R SVCDUMMY
INTERDUM 1




LOADP CH

VOLTA1 1. 1. 2. 2. 0. 0.




CH
CH W


A11
MINIMALI NODENAME 0.7 0.707 0.7 0. 0.
NODENAME 1.2 1.188 3. 0. 0.
FINA11

A12
MINIMALI 47.5 1. 55. 1. 0.
FINA12

Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import eu.itesla_project.iidm.ddb.eurostag_imp_exp.IIDMDynamicDatabaseMockFactory;
import eu.itesla_project.iidm.network.Network;
import eu.itesla_project.iidm.network.test.EurostagTutorialExample1Factory;
import eu.itesla_project.iidm.network.test.SvcTestCaseFactory;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
Expand Down Expand Up @@ -48,6 +49,23 @@ public class TestDynamicDatabaseMock {
"dummycm.pcp",
"dummycm.rcp");

private static final List<String> SVC_REGULATORS_FILENAMES = Arrays.asList("dummefd.fri",
"dummefd.frm",
"dummefd.par",
"dummefd.pcp",
"dummefd.rcp",
"dummycm.fri",
"dummycm.frm",
"dummycm.par",
"dummycm.pcp",
"dummycm.rcp",
"interdum.frm",
"interdum.par",
"interdum.pcp",
"interdum.rcp",
"interdum.fri");


@Before
public void setUp() throws Exception {
fileSystem = Jimfs.newFileSystem(Configuration.unix());
Expand All @@ -59,7 +77,7 @@ public void tearDown() throws Exception {
}

@Test
public void test_00() throws Exception {
public void test_TutorialExample1() throws Exception {
Network network = EurostagTutorialExample1Factory.create();

Map<String, String> iidm2eurostag = new HashMap<>();
Expand All @@ -81,4 +99,30 @@ public void test_00() throws Exception {
.forEach(filename -> assertTrue(Files.isRegularFile(workingDir.resolve(filename))));
}

@Test
public void test_SvcTestCase() throws Exception {
Network network = SvcTestCaseFactory.create();

Map<String, String> iidm2eurostag = new HashMap<>();
iidm2eurostag.put("G1", "G1");
iidm2eurostag.put("B1", "B1");
iidm2eurostag.put("SVC2", "SVC2");
iidm2eurostag.put("B2", "B2");

Path workingDir = Files.createDirectory(fileSystem.getPath("/workingdir"));

DynamicDatabaseClient ddbClient = new IIDMDynamicDatabaseMockFactory().create(false);
ddbClient.dumpDtaFile(workingDir, DTA_FILENAME, network, new HashMap<String, Character>(), "mock", iidm2eurostag);

File expectedDtaFile = new File(getClass().getResource("/sim_test_svc.dta").toURI());
Path testFile = workingDir.resolve(DTA_FILENAME);
assertEquals(CharStreams.toString(new InputStreamReader(Files.newInputStream(expectedDtaFile.toPath()))),
CharStreams.toString(new InputStreamReader(Files.newInputStream(testFile))));

SVC_REGULATORS_FILENAMES.stream()
.forEach(filename -> assertTrue(Files.isRegularFile(workingDir.resolve(filename))));

}


}
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
HEADER 06/03/17 5.1

M1 S 0. 0.
G1 B1 1150. 24. 1. 1. 0. 6.3
0.004 0.219 2.351 0. 0.01723 0.12825 0.00113 0.24253
2.351 0.0193 0.08921 0.03923 1.78484 3
N 1000. 1100.
0.1 6.025 0.1 6.025

R G1
DUMMYCM 1



R G1
DUMMEFD 1




M21 0. 0.
SVC2 C 0 0 250.

R SVC2
INTERDUM 1




LOADP CH

VOLTA1 1. 1. 2. 2. 0. 0.




CH
CH W


A11
G1 B1 0.7 0.707 0.7 0. 0.
B1 1.2 1.188 3. 0. 0.
FINA11

A12
G1 47.5 1. 55. 1. 0.
FINA12

0 comments on commit 348e8f2

Please sign in to comment.