Skip to content

Commit

Permalink
Merge branch 'master' into fix_histodb_forecastdiff
Browse files Browse the repository at this point in the history
  • Loading branch information
mathbagu committed Feb 24, 2017
2 parents 399f351 + e126f27 commit c94d52b
Show file tree
Hide file tree
Showing 18 changed files with 360 additions and 4 deletions.
@@ -1,5 +1,6 @@
/**
* Copyright (c) 2016, All partners of the iTesla project (http://www.itesla-project.eu/consortium)
* Copyright (c) 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/.
Expand All @@ -17,7 +18,7 @@
import eu.itesla_project.eurostag.network.io.EsgWriter;
import eu.itesla_project.eurostag.tools.EurostagNetworkModifier;
import eu.itesla_project.iidm.ddb.eurostag_imp_exp.DynamicDatabaseClient;
import eu.itesla_project.iidm.ddb.eurostag_imp_exp.IIDMDynamicDatabaseFactory;
import eu.itesla_project.iidm.ddb.eurostag_imp_exp.DynamicDatabaseClientFactory;
import eu.itesla_project.iidm.eurostag.export.BranchParallelIndexes;
import eu.itesla_project.iidm.eurostag.export.EurostagDictionary;
import eu.itesla_project.iidm.eurostag.export.EurostagEchExport;
Expand Down Expand Up @@ -57,7 +58,7 @@ public void run(CommandLine line) throws Exception {
if (!Files.isDirectory(outputDir)) {
throw new RuntimeException(outputDir + " is not a directory");
}
DynamicDatabaseClient ddbClient = new IIDMDynamicDatabaseFactory().create(eurostagConfig.isDdbCaching());
DynamicDatabaseClient ddbClient = defaultConfig.newFactoryImpl(DynamicDatabaseClientFactory.class).create(eurostagConfig.isDdbCaching());

System.out.println("loading case...");
// load network
Expand Down
@@ -1,5 +1,6 @@
/**
* Copyright (c) 2016, All partners of the iTesla project (http://www.itesla-project.eu/consortium)
* Copyright (c) 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/.
Expand All @@ -9,13 +10,13 @@
import com.google.common.collect.ImmutableMap;
import com.google.common.io.ByteStreams;
import eu.itesla_project.commons.Version;
import eu.itesla_project.commons.config.ComponentDefaultConfig;
import eu.itesla_project.computation.*;
import eu.itesla_project.eurostag.network.EsgGeneralParameters;
import eu.itesla_project.eurostag.network.EsgNetwork;
import eu.itesla_project.eurostag.network.io.EsgWriter;
import eu.itesla_project.eurostag.tools.EurostagNetworkModifier;
import eu.itesla_project.iidm.datasource.FileDataSource;
import eu.itesla_project.iidm.ddb.eurostag_imp_exp.IIDMDynamicDatabaseFactory;
import eu.itesla_project.iidm.eurostag.export.BranchParallelIndexes;
import eu.itesla_project.iidm.eurostag.export.EurostagDictionary;
import eu.itesla_project.iidm.eurostag.export.EurostagEchExport;
Expand Down Expand Up @@ -113,7 +114,8 @@ public EurostagStabilization(Network network, ComputationManager computationMana
this.network = network;
this.computationManager = computationManager;
this.priority = priority;
this.ddbClient = new IIDMDynamicDatabaseFactory().create(config.isDdbCaching());
ComponentDefaultConfig defaultConfig = ComponentDefaultConfig.load();
this.ddbClient = defaultConfig.newFactoryImpl(DynamicDatabaseClientFactory.class).create(config.isDdbCaching());
this.config = config;

LOGGER.info(config.toString());
Expand Down
17 changes: 17 additions & 0 deletions iidm-ddb/iidm-ddb-eurostag-import-export/pom.xml
Expand Up @@ -80,6 +80,23 @@
<type>jar</type>
</dependency>

<dependency>
<groupId>eu.itesla_project</groupId>
<artifactId>iidm-impl</artifactId>
<version>${ipstcore.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>eu.itesla_project</groupId>
<artifactId>iidm-test</artifactId>
<version>${ipstcore.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.google.jimfs</groupId>
<artifactId>jimfs</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
Expand Down
@@ -0,0 +1,105 @@
/**
* Copyright (c) 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.iidm.ddb.eurostag_imp_exp;

import com.google.common.base.Strings;
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 org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.*;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.Objects;

/**
* @author Christian Biasuzzi <christian.biasuzzi@techrain.it>
*/
class DynamicDatabaseMock implements DynamicDatabaseClient {

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

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

@Override
public void dumpDtaFile(Path workingDir, String fileName, Network network, Map<String, Character> parallelIndexes, String eurostagVersion, Map<String, String> iidm2eurostagId) {
Objects.requireNonNull(workingDir);
Objects.requireNonNull(fileName);
Objects.requireNonNull(network);
Objects.requireNonNull(parallelIndexes);
Objects.requireNonNull(eurostagVersion);
Objects.requireNonNull(iidm2eurostagId);

//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())))
.findFirst()
.orElseThrow(() -> new RuntimeException("could not find a suitable generator in network: " + network + ", to be used in: " + fileName));

Bus bus = generator.getTerminal().getBusBreakerView().getConnectableBus();
if ((bus == null) || (!iidm2eurostagId.containsKey(bus.getId()))) {
throw new RuntimeException("suitable node not found");
}
String mappedGenName = formatString8(iidm2eurostagId.get(generator.getId()));
String mappedNodeName = formatString8(iidm2eurostagId.get(bus.getId()));

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);
}
}

private String formatString8(String string) {
return Strings.padEnd((string.length() > 8) ? string.substring(0, 8) : string, 8, ' ');
}

@Override
public String getName() {
return "mock DDB";
}

@Override
public String getVersion() {
return null;
}

}
@@ -0,0 +1,19 @@
/**
* Copyright (c) 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.iidm.ddb.eurostag_imp_exp;

/**
* @author Christian Biasuzzi <christian.biasuzzi@techrain.it>
*/
public class IIDMDynamicDatabaseMockFactory implements DynamicDatabaseClientFactory {

@Override
public DynamicDatabaseClient create(boolean cache) {
return new DynamicDatabaseMock();
}

}
@@ -0,0 +1,5 @@
HEADER 15/02/17 5.1
70 84
0
0
0
@@ -0,0 +1,25 @@
HEADER 15/02/17 5.1
70 84
1
?
?
?
?
?
?
?
_EFD
?
?
?
?
?
$EFD
1
-805
-522
26
0
1
0
0
@@ -0,0 +1,7 @@
HEADER 15/02/17 5.1

_end_comment_

DUMMEFD 1


Binary file not shown.
Binary file not shown.
@@ -0,0 +1,5 @@
HEADER 15/02/17 5.1
70 84
0
0
0
@@ -0,0 +1,25 @@
HEADER 15/02/17 5.1
70 84
1
?
?
?
?
?
?
?
_CM
?
?
?
?
?
$CM
2
-535
-312
26
0
2
0
0
@@ -0,0 +1,7 @@
HEADER 15/02/17 5.1

_end_comment_

DUMMYCM 1


Binary file not shown.
Binary file not shown.
@@ -0,0 +1,27 @@
HEADER 15/02/17 5.1

M6 U 0. 0.
MINIMALI NODENAME 100000. 1. 1. 1. 0. 4.
0. 0.3 1. 0.5 4.
1. 1 3 3
N 0 0 0 0 0 100000. 100000.


RAMINIMALI
DUMMYCM 1


RVMINIMALI
DUMMEFD 1


LOADP CH

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




CH
CH W

@@ -0,0 +1,84 @@
/**
* Copyright (c) 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/.
*/

import com.google.common.io.CharStreams;
import com.google.common.jimfs.Configuration;
import com.google.common.jimfs.Jimfs;
import eu.itesla_project.iidm.ddb.eurostag_imp_exp.DynamicDatabaseClient;
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 org.junit.After;
import org.junit.Before;
import org.junit.Test;

import java.io.File;
import java.io.InputStreamReader;
import java.nio.file.FileSystem;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;

/**
* @author Christian Biasuzzi <christian.biasuzzi@techrain.it>
*/
public class TestDynamicDatabaseMock {

private FileSystem fileSystem;

private static final String DTA_FILENAME = "sim.dta";
private static final List<String> REGULATORS_FILENAMES = Arrays.asList("dummefd.fri",
"dummefd.frm",
"dummefd.par",
"dummefd.pcp",
"dummefd.rcp",
"dummycm.fri",
"dummycm.frm",
"dummycm.par",
"dummycm.pcp",
"dummycm.rcp");

@Before
public void setUp() throws Exception {
fileSystem = Jimfs.newFileSystem(Configuration.unix());
}

@After
public void tearDown() throws Exception {
fileSystem.close();
}

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

Map<String, String> iidm2eurostag = new HashMap<>();
iidm2eurostag.put("GEN", "GEN");
iidm2eurostag.put("NGEN", "NGEN");

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_test00.dta").toURI());
Path testFile = workingDir.resolve(DTA_FILENAME);

assertEquals(CharStreams.toString(new InputStreamReader(Files.newInputStream(expectedDtaFile.toPath()))),
CharStreams.toString(new InputStreamReader(Files.newInputStream(testFile))));

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

}

0 comments on commit c94d52b

Please sign in to comment.