Skip to content

Commit

Permalink
Avoid return shared_ptr to julia for network metadata and store them …
Browse files Browse the repository at this point in the history
…in the network handle struct. Remove explicit module NetworkCreationUtils

Signed-off-by: Bertrand Rix <[email protected]>
  • Loading branch information
obrix committed Oct 8, 2024
1 parent c8edab8 commit 2a109e5
Show file tree
Hide file tree
Showing 4 changed files with 78 additions and 72 deletions.
24 changes: 10 additions & 14 deletions cpp/powsybljl-cpp/powsybl_jl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -102,26 +102,22 @@ JLCXX_MODULE define_module_powsybl(jlcxx::Module& mod)
});

mod.add_type<network_metadata>("NetworkMetadata")
.method("id", [](const network_metadata& att) {
return std::string(att.id);
.method("id", [](pypowsybl::JavaHandle handle) {
return std::string(pypowsybl::getNetworkMetadata(handle)->id);
})
.method("name", [](const network_metadata& att) {
return std::string(att.name);
.method("name", [](pypowsybl::JavaHandle handle) {
return std::string(pypowsybl::getNetworkMetadata(handle)->name);
})
.method("source_format", [](const network_metadata& att) {
return std::string(att.source_format);
.method("source_format", [](pypowsybl::JavaHandle handle) {
return std::string(pypowsybl::getNetworkMetadata(handle)->source_format);
})
.method("forecast_distance", [](const network_metadata& att) {
return att.forecast_distance;
.method("forecast_distance", [](pypowsybl::JavaHandle handle) {
return pypowsybl::getNetworkMetadata(handle)->forecast_distance;
})
.method("case_date", [](const network_metadata& att) {
return att.case_date;
.method("case_date", [](pypowsybl::JavaHandle handle) {
return pypowsybl::getNetworkMetadata(handle)->case_date;
});

mod.method("get_network_metadata", [] (pypowsybl::JavaHandle handle) {
return pypowsybl::getNetworkMetadata(handle);
}, "Get network attributes");

mod.add_type<pypowsybl::SeriesArray>("SeriesArray")
.method("as_array", [](pypowsybl::SeriesArray& seriesArray) {
jlcxx::Array<series> data{ };
Expand Down
13 changes: 12 additions & 1 deletion src/Network.jl
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,11 @@ module Network

mutable struct NetworkHandle
handle::Powsybl.JavaHandle
id::String
name::String
source_format::String
forecast_distance::Int32
case_date::Float64
end

function get_network_metadata(network::NetworkHandle)
Expand Down Expand Up @@ -170,7 +175,13 @@ module Network
end

function load(network_file::String)::NetworkHandle
return NetworkHandle(Powsybl.load(network_file))
handle = Powsybl.load(network_file)
return NetworkHandle(Powsybl.load(network_file),
Powsybl.id(handle),
Powsybl.name(handle),
Powsybl.source_format(handle),
Powsybl.forecast_distance(handle),
Powsybl.case_date(handle))
end

include("NetworkCreationUtils.jl")
Expand Down
100 changes: 50 additions & 50 deletions src/NetworkCreationUtils.jl
Original file line number Diff line number Diff line change
@@ -1,65 +1,65 @@
module NetworkCreationUtils
using ..Powsybl
using ..Network
using CxxWrap
function _create_network(name::String, network_id::String = "")
handle = Powsybl.create_network(name, network_id)
return Network.NetworkHandle(handle,
Powsybl.id(handle),
Powsybl.name(handle),
Powsybl.source_format(handle),
Powsybl.forecast_distance(handle),
Powsybl.case_date(handle))
end

function _create_network(name::String, network_id::String = "")
return Network.NetworkHandle(Powsybl.create_network(name, network_id))
end
function create_empty(network_id::String = "")
return _create_network("empty", network_id)
end

function create_empty(network_id::String = "")
return _create_network("empty", network_id)
end
function create_ieee9(network_id::String = "")
return _create_network("ieee9", network_id)
end

function create_ieee9(network_id::String = "")
return _create_network("ieee9", network_id)
end
function create_ieee14(network_id::String = "")
return _create_network("ieee14", network_id)
end

function create_ieee14(network_id::String = "")
return _create_network("ieee14", network_id)
end
function create_ieee30(network_id::String = "")
return _create_network("ieee30", network_id)
end

function create_ieee30(network_id::String = "")
return _create_network("ieee30", network_id)
end
function create_ieee57(network_id::String = "")
return _create_network("ieee57", network_id)
end

function create_ieee57(network_id::String = "")
return _create_network("ieee57", network_id)
end
function create_ieee118(network_id::String = "")
return _create_network("ieee118", network_id)
end

function create_ieee118(network_id::String = "")
return _create_network("ieee118", network_id)
end
function create_ieee300(network_id::String = "")
return _create_network("ieee300", network_id)
end

function create_ieee300(network_id::String = "")
return _create_network("ieee300", network_id)
end
function create_eurostag_tutorial_example1(network_id::String = "")
return _create_network("eurostag_tutorial_example1", network_id)
end

function create_eurostag_tutorial_example1(network_id::String = "")
return _create_network("eurostag_tutorial_example1", network_id)
end
function create_eurostag_tutorial_example1_with_power_limits(network_id::String = "")
return _create_network("eurostag_tutorial_example1_with_power_limits", network_id)
end

function create_eurostag_tutorial_example1_with_power_limits(network_id::String = "")
return _create_network("eurostag_tutorial_example1_with_power_limits", network_id)
end
function create_four_substations_node_breaker(network_id::String = "")
return _create_network("four_substations_node_breaker", network_id)
end

function create_four_substations_node_breaker(network_id::String = "")
return _create_network("four_substations_node_breaker", network_id)
end
function create_four_substations_node_breaker_with_extensions(network_id::String = "")
return _create_network("four_substations_node_breaker_with_extensions", network_id)
end

function create_four_substations_node_breaker_with_extensions(network_id::String = "")
return _create_network("four_substations_node_breaker_with_extensions", network_id)
end
function create_micro_grid_be(network_id::String = "")
return _create_network("micro_grid_be", network_id)
end

function create_micro_grid_be(network_id::String = "")
return _create_network("micro_grid_be", network_id)
end
function create_micro_grid_nl(network_id::String = "")
return _create_network("micro_grid_nl", network_id)
end

function create_micro_grid_nl(network_id::String = "")
return _create_network("micro_grid_nl", network_id)
end

function create_metrix_tutorial_six_buses(network_id::String = "")
return _create_network("metrix_tutorial_six_buses", network_id)
end
function create_metrix_tutorial_six_buses(network_id::String = "")
return _create_network("metrix_tutorial_six_buses", network_id)
end
13 changes: 6 additions & 7 deletions test/print_network.jl
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,13 @@ using Powsybl
using CxxWrap
using Test

network = Powsybl.Network.NetworkCreationUtils.create_ieee9()
network_metadata = Powsybl.Network.get_network_metadata(network)
network = Powsybl.Network.create_ieee9()

@test Powsybl.id(network_metadata[]) == "ieee9cdf"
@test Powsybl.name(network_metadata[]) == "ieee9cdf"
@test Powsybl.source_format(network_metadata[]) == "IEEE-CDF"
@test Powsybl.forecast_distance(network_metadata[]) == 0
@test Powsybl.case_date(network_metadata[]) 1.240704e9
@test network.id == "ieee9cdf"
@test network.name == "ieee9cdf"
@test network.source_format == "IEEE-CDF"
@test network.forecast_distance == 0
@test network.case_date 1.240704e9

lines = Powsybl.Network.get_lines(network)
@test names(lines) == ["id", "name", "r", "x", "g1", "b1", "g2",
Expand Down

0 comments on commit 2a109e5

Please sign in to comment.