Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

File Parser Enhancements #311

Merged
merged 7 commits into from
Jun 21, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 13 additions & 4 deletions src/io/common.jl
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,6 @@ function parse_file(file::String; import_all=false)
pm_data = parse_json(file)
end

check_network_data(pm_data)

return pm_data
end

Expand All @@ -29,10 +27,21 @@ end

""
function parse_json(file_string::String)
data_string = readstring(open(file_string))
return JSON.parse(data_string)
open(file_string) do f
parse_json(f)
end
end


""
function parse_json(io::IO)
data_string = readstring(io)
pm_data = JSON.parse(data_string)
check_network_data(pm_data)
return pm_data
end


""
function check_network_data(data::Dict{String,Any})
add_powermodels_version(data)
Expand Down
26 changes: 15 additions & 11 deletions src/io/matpower.jl
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,11 @@
# #
#########################################################################

""
function parse_matpower(file_string::String)
mp_data = parse_matpower_file(file_string)
#display(mp_data)

"Parses the matpwer data from either a filename or an IO object"
function parse_matpower(file::Union{IO, String})
mp_data = parse_matpower_file(file)
pm_data = matpower_to_powermodels(mp_data)

check_network_data(pm_data)
return pm_data
end

Expand Down Expand Up @@ -129,9 +127,18 @@ mp_dcline_columns = [

""
function parse_matpower_file(file_string::String)
io = open(file_string)
mp_data = open(file_string) do io
parse_matpower_file(io)
end

return mp_data
end


""
function parse_matpower_file(io::IO)
data_string = readstring(io)
close(io)

return parse_matpower_string(data_string)
end

Expand Down Expand Up @@ -278,9 +285,6 @@ function parse_matpower_string(data_string::String)
end
end

#println("Case:")
#println(case)

return case
end

Expand Down
18 changes: 15 additions & 3 deletions src/io/psse.jl
Original file line number Diff line number Diff line change
Expand Up @@ -675,13 +675,25 @@ function parse_psse(pti_data::Dict; import_all=false)::Dict
end
end

check_network_data(pm_data)

return pm_data
end


"Parses directly from file"
function parse_psse(file::String; import_all=false)::Dict
pti_data = parse_pti(file)
function parse_psse(filename::String; import_all=false)::Dict
pm_data = open(filename) do f
parse_psse(f; import_all=import_all)
end

return pm_data
end


"Parses directly from iostream"
function parse_psse(io::IO; import_all=false)::Dict
pti_data = parse_pti(io)

return parse_psse(pti_data; import_all=import_all)
end
end
26 changes: 20 additions & 6 deletions src/io/pti.jl
Original file line number Diff line number Diff line change
Expand Up @@ -568,16 +568,30 @@ end


"""
parse_pti(filename)
parse_pti(filename::String)

Open PTI raw file given by `filename`, passing the file contents as a string
to the main PTI parser, returning a `Dict` of all the data parsed into the
proper types.
Open PTI raw file given by `filename`, returning a `Dict` of the data parsed
into the proper types.
"""
function parse_pti(filename::String)::Dict
data_string = readstring(open(filename))
pti_data = open(filename) do f
parse_pti(f)
end

return pti_data
end


"""
parse_pti(io::IO)

Reads PTI data in `io::IO`, returning a `Dict` of the data parsed into the
proper types.
"""
function parse_pti(io::IO)::Dict
data_string = readstring(io)
pti_data = parse_pti_data(data_string, get_pti_sections())
pti_data["CASE IDENTIFICATION"][1]["NAME"] = match(r"[\/\\]*(?:.*[\/\\])*(.*)\.raw", lowercase(filename)).captures[1]
pti_data["CASE IDENTIFICATION"][1]["NAME"] = match(r"[\/\\]*(?:.*[\/\\])*(.*)\.raw", lowercase(io.name)).captures[1]

return pti_data
end
93 changes: 0 additions & 93 deletions test/data.jl
Original file line number Diff line number Diff line change
@@ -1,20 +1,5 @@
# Tests of data checking and transformation code

function test_case(filename::AbstractString)
temp_file = "temp.m"
source_data = PowerModels.parse_file(filename)

io = open(temp_file, "w")
PowerModels.export_matpower(io, source_data)
close(io)
destination_data = PowerModels.parse_file(temp_file)

@test InfrastructureModels.compare_dict(source_data, destination_data)

rm(temp_file)
end


@testset "test data summary" begin

@testset "5-bus summary from dict" begin
Expand Down Expand Up @@ -403,81 +388,3 @@ end
@test isapprox(result["objective"], 5907; atol = 1e0)
end
end

@testset "test idempotent matpower export" begin
@testset "test frankenstein_00" begin
file = "../test/data/matpower/frankenstein_00.m"
test_case(file)
end

@testset "test case14" begin
file = "../test/data/matpower/case14.m"
test_case(file)
end

@testset "test case2" begin
file = "../test/data/matpower/case2.m"
test_case(file)
end

@testset "test case24" begin
file = "../test/data/matpower/case24.m"
test_case(file)
end

@testset "test case3_tnep" begin
file = "../test/data/matpower/case3_tnep.m"
test_case(file)
end

@testset "test case30" begin
file = "../test/data/matpower/case30.m"
test_case(file)
end

@testset "test case5 asym" begin
file = "../test/data/matpower/case5_asym.m"
test_case(file)
end

@testset "test case5 gap" begin
file = "../test/data/matpower/case5_gap.m"
test_case(file)
end

@testset "test case5 pwlc" begin
file = "../test/data/matpower/case5_pwlc.m"
test_case(file)
end

@testset "test case5" begin
file = "../test/data/matpower/case5.m"
test_case(file)
end

@testset "test case6" begin
file = "../test/data/matpower/case6.m"
test_case(file)
end

@testset "test case3" begin
file = "../test/data/matpower/case3.m"
test_case(file)
end

@testset "test case5 dc" begin
file = "../test/data/matpower/case5_dc.m"
test_case(file)
end

@testset "test case5 tnep" begin
file = "../test/data/matpower/case5_tnep.m"
test_case(file)
end

@testset "test case7 tplgy" begin
file = "../test/data/matpower/case7_tplgy.m"
test_case(file)
end

end
128 changes: 127 additions & 1 deletion test/matpower.jl
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ using JSON
@test isapprox(result["objective"], 204.96; atol = 1e-1)
end

@testset "30-bus case matpower data" begin
@testset "30-bus case matpower data (parse_file)" begin
data = PowerModels.parse_file("../test/data/matpower/case30.m")
@test isa(JSON.json(data), String)

Expand All @@ -19,6 +19,28 @@ using JSON
@test isapprox(result["objective"], 204.96; atol = 1e-1)
end

@testset "30-bus case matpower data (parse_matpower)" begin
data = PowerModels.parse_matpower("../test/data/matpower/case30.m")
@test isa(JSON.json(data), String)

result = run_opf(data, ACPPowerModel, ipopt_solver)

@test result["status"] == :LocalOptimal
@test isapprox(result["objective"], 204.96; atol = 1e-1)
end

@testset "30-bus case matpower data (parse_matpower; iostream)" begin
open("../test/data/matpower/case30.m") do f
data = PowerModels.parse_matpower(f)
@test isa(JSON.json(data), String)

result = run_opf(data, ACPPowerModel, ipopt_solver)

@test result["status"] == :LocalOptimal
@test isapprox(result["objective"], 204.96; atol = 1e-1)
end
end

@testset "14-bus case file with bus names" begin
data = PowerModels.parse_file("../test/data/matpower/case14.m")
@test data["bus"]["1"]["bus_name"] == "Bus 1 HV"
Expand Down Expand Up @@ -180,3 +202,107 @@ end
@test data["name"] == ref[:name]
end
end

@testset "test idempotent matpower export" begin

function test_case(filename::AbstractString, parse_file::Function)
temp_file = "temp.m"
source_data = parse_file(filename)

io = PipeBuffer()
PowerModels.export_matpower(io, source_data)
destination_data = PowerModels.parse_matpower(io)

@test InfrastructureModels.compare_dict(source_data, destination_data)
end

@testset "test frankenstein_00" begin
file = "../test/data/matpower/frankenstein_00.m"
test_case(file, PowerModels.parse_file)
test_case(file, PowerModels.parse_matpower)
end

@testset "test case14" begin
file = "../test/data/matpower/case14.m"
test_case(file, PowerModels.parse_file)
test_case(file, PowerModels.parse_matpower)
end

@testset "test case2" begin
file = "../test/data/matpower/case2.m"
test_case(file, PowerModels.parse_file)
test_case(file, PowerModels.parse_matpower)
end

@testset "test case24" begin
file = "../test/data/matpower/case24.m"
test_case(file, PowerModels.parse_file)
test_case(file, PowerModels.parse_matpower)
end

@testset "test case3_tnep" begin
file = "../test/data/matpower/case3_tnep.m"
test_case(file, PowerModels.parse_file)
test_case(file, PowerModels.parse_matpower)
end

@testset "test case30" begin
file = "../test/data/matpower/case30.m"
test_case(file, PowerModels.parse_file)
test_case(file, PowerModels.parse_matpower)
end

@testset "test case5 asym" begin
file = "../test/data/matpower/case5_asym.m"
test_case(file, PowerModels.parse_file)
test_case(file, PowerModels.parse_matpower)
end

@testset "test case5 gap" begin
file = "../test/data/matpower/case5_gap.m"
test_case(file, PowerModels.parse_file)
test_case(file, PowerModels.parse_matpower)
end

@testset "test case5 pwlc" begin
file = "../test/data/matpower/case5_pwlc.m"
test_case(file, PowerModels.parse_file)
test_case(file, PowerModels.parse_matpower)
end

@testset "test case5" begin
file = "../test/data/matpower/case5.m"
test_case(file, PowerModels.parse_file)
test_case(file, PowerModels.parse_matpower)
end

@testset "test case6" begin
file = "../test/data/matpower/case6.m"
test_case(file, PowerModels.parse_file)
test_case(file, PowerModels.parse_matpower)
end

@testset "test case3" begin
file = "../test/data/matpower/case3.m"
test_case(file, PowerModels.parse_file)
test_case(file, PowerModels.parse_matpower)
end

@testset "test case5 dc" begin
file = "../test/data/matpower/case5_dc.m"
test_case(file, PowerModels.parse_file)
test_case(file, PowerModels.parse_matpower)
end

@testset "test case5 tnep" begin
file = "../test/data/matpower/case5_tnep.m"
test_case(file, PowerModels.parse_file)
test_case(file, PowerModels.parse_matpower)
end

@testset "test case7 tplgy" begin
file = "../test/data/matpower/case7_tplgy.m"
test_case(file, PowerModels.parse_file)
test_case(file, PowerModels.parse_matpower)
end
end
Loading