-
Notifications
You must be signed in to change notification settings - Fork 19
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
258 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
/target | ||
/.ipynb_checkpoints |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
# Copyright (c) 2018, 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/. | ||
|
||
# @author Christian Biasuzzi <christian.biasuzzi@techrain.it> | ||
|
||
|
||
from py4j.java_gateway import JavaGateway, GatewayParameters | ||
from py4j.java_gateway import java_import | ||
from py4j.java_gateway import JavaPackage | ||
|
||
#connect to the JVM (running iPST/PowSyBl) | ||
gateway = JavaGateway(gateway_parameters = GatewayParameters(auto_field=True)) | ||
|
||
#other boilerplate imports | ||
java_import(gateway.jvm,'java.nio.file.*') | ||
java_import(gateway.jvm,'java.io.*') | ||
|
||
java_import(gateway.jvm,'com.powsybl.commons.config.ComponentDefaultConfig') | ||
java_import(gateway.jvm,'com.powsybl.computation.local.LocalComputationManager') | ||
java_import(gateway.jvm,'com.powsybl.loadflow.LoadFlowFactory') | ||
java_import(gateway.jvm,'com.powsybl.iidm.import_.Importers') | ||
java_import(gateway.jvm,'com.powsybl.iidm.export.Exporters') | ||
java_import(gateway.jvm,'com.powsybl.iidm.network.test.FictitiousSwitchFactory') | ||
|
||
#needed below to resolve some class-factories, by name | ||
ReflectionUtil = gateway.jvm.py4j.reflection.ReflectionUtil | ||
|
||
#simple dump network flows function | ||
def dumpLinesFlow(network): | ||
print("\nFlow on lines for network: " + network.getId()) | ||
lines = network.getLines().toList() | ||
print("line id;terminal1.I;terminal2.I") | ||
for line in lines: | ||
print(line.getId()+";" + str(line.getTerminal1().getI()) + ";" + str(line.getTerminal2().getI())) | ||
|
||
#load platform config | ||
defaultConfig = gateway.jvm.ComponentDefaultConfig.load() | ||
|
||
#instantiate a computation manager and a LF factory | ||
computationManager = gateway.jvm.LocalComputationManager() | ||
loadflowfactory=defaultConfig.newFactoryImpl(ReflectionUtil.classForName("com.powsybl.loadflow.LoadFlowFactory")) | ||
|
||
#create a demo network | ||
network = gateway.jvm.com.powsybl.iidm.network.test.FictitiousSwitchFactory.create() | ||
|
||
#instantiate a LF | ||
loadFlow = loadflowfactory.create(network, computationManager, 0) | ||
|
||
#dump network's lines flow | ||
dumpLinesFlow(network) | ||
|
||
#run a LF on the network and dump its results metrics | ||
loadflowResult = loadFlow.run() | ||
print("\nLF result: " + str(loadflowResult.isOk()) + "; metrics: " + str(loadflowResult.getMetrics())) | ||
|
||
#dump network's lines flow | ||
dumpLinesFlow(network) | ||
|
||
|
||
#change the network: open a switch | ||
network.getSwitch("BD").setOpen(True) | ||
|
||
#re-run a LF on the network and dump its results metrics | ||
loadflowResult = loadFlow.run() | ||
print("\nLF result: " + str(loadflowResult.isOk()) + "; metrics: " + str(loadflowResult.getMetrics())) | ||
|
||
#dump network's lines flow | ||
dumpLinesFlow(network) | ||
|
||
#export this network to a file, in xiidm format | ||
gateway.jvm.Exporters.export("XIIDM", network, None, gateway.jvm.File('/tmp/export1').toPath()) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
# Py-powsybl: how-to use the powsybl APIs from Python | ||
|
||
py-powsybl module uses [py4j](https://www.py4j.org) to call the Powsybl java APIs, from a Python environment. | ||
|
||
### Runtime requirements: | ||
Py4j enables Python programs running in a Python interpreter to dynamically access Java objects in a Java Virtual Machine. | ||
|
||
Py4j documentation states that it has been tested with Python 2.6, 2.7, 3.4, 3.5, and 3.6 | ||
To install Py4j v0.10.7 on CentOS (detailed information [here](https://www.py4j.org/install.html#id1): | ||
|
||
pip install py4j (Python v2) | ||
|
||
or | ||
|
||
pip3 install py4j (Python v3) | ||
|
||
Note: when it comes to experimenting with multiple Python extensions, it might be a good idea to create first an isolated python environment with [virtualenv](https://virtualenv.pypa.io/en/stable/) | ||
|
||
### Execute py-powsybl | ||
|
||
Start the py-powsybl 'server' in a console. | ||
|
||
itools py-powsybl | ||
|
||
### Run some python code: | ||
|
||
Demo1.py demostrates what can be done (it executes a loadflow on a network, opens a switch, exports a network to a file in xiidm format) | ||
|
||
python Demo1.py | ||
|
||
### Stop py-powsybl | ||
To stop the py-powsybl 'server', CTRL+C in the itools console. | ||
|
||
|
||
## Notes | ||
py-powsybl has been tested on a CentOS 6, with python v2.6, python v3.6 (in a virtualenv sandbox). | ||
|
||
## TODO | ||
* performances/memory with multiple, large networks/data ? | ||
* make the client/server connection configurable (currently, it uses the defaults py4j) | ||
* explore the other way round (calling python from java) | ||
* issues? threads? security? |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<!-- | ||
Copyright (c) 2018, 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/. | ||
--> | ||
<project xmlns="http://maven.apache.org/POM/4.0.0" | ||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | ||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> | ||
<modelVersion>4.0.0</modelVersion> | ||
|
||
|
||
<parent> | ||
<groupId>eu.itesla_project</groupId> | ||
<artifactId>itesla-parent</artifactId> | ||
<version>0.1-SNAPSHOT</version> | ||
</parent> | ||
|
||
<artifactId>py-powsybl</artifactId> | ||
<name>py-powsybl</name> | ||
<description>py-powsybl python-java gateway</description> | ||
|
||
<dependencies> | ||
<dependency> | ||
<groupId>ch.qos.logback</groupId> | ||
<artifactId>logback-classic</artifactId> | ||
<scope>runtime</scope> | ||
</dependency> | ||
|
||
<dependency> | ||
<groupId>org.slf4j</groupId> | ||
<artifactId>log4j-over-slf4j</artifactId> | ||
<scope>runtime</scope> | ||
</dependency> | ||
|
||
<dependency> | ||
<groupId>com.google.auto.service</groupId> | ||
<artifactId>auto-service</artifactId> | ||
</dependency> | ||
|
||
<dependency> | ||
<groupId>${project.groupId}</groupId> | ||
<artifactId>modules</artifactId> | ||
<version>${project.version}</version> | ||
</dependency> | ||
|
||
<dependency> | ||
<groupId>net.sf.py4j</groupId> | ||
<artifactId>py4j</artifactId> | ||
<version>0.10.7</version> | ||
</dependency> | ||
|
||
</dependencies> | ||
|
||
</project> |
75 changes: 75 additions & 0 deletions
75
py-powsybl/src/main/java/com/powsybl/powsybl/PyPowsybl.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
/** | ||
* Copyright (c) 2018, 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 com.powsybl.powsybl; | ||
|
||
import com.google.auto.service.AutoService; | ||
import com.powsybl.tools.Command; | ||
import com.powsybl.tools.Tool; | ||
import com.powsybl.tools.ToolRunningContext; | ||
import org.apache.commons.cli.CommandLine; | ||
import org.apache.commons.cli.Options; | ||
import py4j.GatewayServer; | ||
|
||
/** | ||
* @author Christian Biasuzzi <christian.biasuzzi@techrain.it> | ||
*/ | ||
@AutoService(Tool.class) | ||
public class PyPowsybl implements Tool { | ||
|
||
@Override | ||
public Command getCommand() { | ||
return new Command() { | ||
|
||
@Override | ||
public String getName() { | ||
return "py-powsybl"; | ||
} | ||
|
||
@Override | ||
public String getTheme() { | ||
return "Misc"; | ||
} | ||
|
||
@Override | ||
public String getDescription() { | ||
return "run py-powsybl gateway"; | ||
} | ||
|
||
@Override | ||
public Options getOptions() { | ||
Options options = new Options(); | ||
return options; | ||
} | ||
|
||
@Override | ||
public String getUsageFooter() { | ||
return null; | ||
} | ||
}; | ||
|
||
} | ||
|
||
@Override | ||
public void run(CommandLine line, ToolRunningContext context) throws Exception { | ||
|
||
GatewayServer server = new GatewayServer(); | ||
|
||
Runtime.getRuntime().addShutdownHook(new Thread() { | ||
public void run() { | ||
System.out.println(); | ||
System.out.println("Shutting down py-powsybl server"); | ||
server.shutdown(); | ||
} | ||
}); | ||
|
||
// execute the py4j Gateway service | ||
//TODO customize service parameters: address, port, ..... | ||
server.start(); | ||
System.out.println("py-powsybl server started; CTRL+C to stop it"); | ||
|
||
} | ||
} |