Skip to content

Commit

Permalink
Add parsing of 2-winding transformers (#11)
Browse files Browse the repository at this point in the history
* WIP add parsing of 2-winding transformers

* fixup! WIP add parsing of 2-winding transformers

* fixup! WIP add parsing of 2-winding transformers

* Add tests for parsing v29-format transformer data

* Update Manfiest to fix local docs

* Document fields for two-winding transformers

* Add comment about counting rows for Transformers

* Add printing for Transformers

* Fix counting of rows

- since data for each Transformer is spread over
  multiple lines, we can now have a line start with '0'
  and it not indicate the end of the section (since
  it in the middle of the data for a Transformer, not
  the start of a new record).
  • Loading branch information
nickrobinson251 committed Sep 30, 2021
1 parent a57f2fe commit 5214ad5
Show file tree
Hide file tree
Showing 7 changed files with 367 additions and 19 deletions.
20 changes: 13 additions & 7 deletions docs/Manifest.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,19 +16,19 @@ uuid = "2a0f44e3-6c83-55bd-87e4-b1978d98bd5f"

[[Compat]]
deps = ["Base64", "Dates", "DelimitedFiles", "Distributed", "InteractiveUtils", "LibGit2", "Libdl", "LinearAlgebra", "Markdown", "Mmap", "Pkg", "Printf", "REPL", "Random", "SHA", "Serialization", "SharedArrays", "Sockets", "SparseArrays", "Statistics", "Test", "UUIDs", "Unicode"]
git-tree-sha1 = "4866e381721b30fac8dda4c8cb1d9db45c8d2994"
git-tree-sha1 = "1a90210acd935f222ea19657f143004d2c2a1117"
uuid = "34da2185-b29b-5c13-b0c7-acf172513d20"
version = "3.37.0"
version = "3.38.0"

[[Crayons]]
git-tree-sha1 = "3f71217b538d7aaee0b69ab47d9b7724ca8afa0d"
uuid = "a8cc5b0e-0ffa-5ad4-8c14-923d3ee1735f"
version = "4.0.4"

[[DataAPI]]
git-tree-sha1 = "bec2532f8adb82005476c141ec23e921fc20971b"
git-tree-sha1 = "cc70b17275652eb47bc9e5f81635981f13cea5c8"
uuid = "9a962f9c-6df0-11e9-0e5d-c546b8b5ee8a"
version = "1.8.0"
version = "1.9.0"

[[DataFrames]]
deps = ["Compat", "DataAPI", "Future", "InvertedIndices", "IteratorInterfaceExtensions", "LinearAlgebra", "Markdown", "Missings", "PooledArrays", "PrettyTables", "Printf", "REPL", "Reexport", "SortingAlgorithms", "Statistics", "TableTraits", "Tables", "Unicode"]
Expand Down Expand Up @@ -167,9 +167,9 @@ version = "1.4.1"

[[Parsers]]
deps = ["Dates"]
git-tree-sha1 = "438d35d2d95ae2c5e8780b330592b6de8494e779"
git-tree-sha1 = "9d8c00ef7a8d110787ff6f170579846f776133a9"
uuid = "69de0a69-1ddd-5017-9359-2bf0b02dc9f0"
version = "2.0.3"
version = "2.0.4"

[[Pkg]]
deps = ["Artifacts", "Dates", "Downloads", "LibGit2", "Libdl", "Logging", "Markdown", "Printf", "REPL", "Random", "SHA", "Serialization", "TOML", "Tar", "UUIDs", "p7zip_jll"]
Expand All @@ -182,7 +182,7 @@ uuid = "2dfb63ee-cc39-5dd5-95bd-886bf059d720"
version = "1.3.0"

[[PowerFlowData]]
deps = ["DocStringExtensions", "Parsers", "Tables"]
deps = ["DocStringExtensions", "Parsers", "Tables", "WeakRefStrings"]
path = ".."
uuid = "dd99e9e3-7471-40fc-b48d-a10501125371"
version = "1.0.0-DEV"
Expand Down Expand Up @@ -268,6 +268,12 @@ uuid = "cf7118a7-6976-5b1a-9a39-7adc72f591a4"
[[Unicode]]
uuid = "4ec0a83e-493e-50e2-b9ac-8f72acf5a8f5"

[[WeakRefStrings]]
deps = ["DataAPI", "Parsers"]
git-tree-sha1 = "4a4cfb1ae5f26202db4f0320ac9344b3372136b0"
uuid = "ea10d353-3f73-51f8-a26c-33c1cb351aa5"
version = "1.3.0"

[[Zlib_jll]]
deps = ["Libdl"]
uuid = "83775a58-1f1d-513f-b197-d71354ab007a"
Expand Down
1 change: 1 addition & 0 deletions docs/src/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ Buses
Loads
Generators
Branches
TwoWindingTransformers
```

## Alternatives
Expand Down
2 changes: 1 addition & 1 deletion src/PowerFlowData.jl
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ using WeakRefStrings: InlineString3, InlineString15

export parse_network
export Network
export CaseID, Buses, Loads, Generators, Branches
export CaseID, Buses, Loads, Generators, Branches, TwoWindingTransformers

include("types.jl")
include("parsing.jl")
Expand Down
47 changes: 39 additions & 8 deletions src/parsing.jl
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,12 @@ function parse_network(source)
@debug "branches" nrows pos
branches, pos = parse_records!(Branches(nrows), bytes, pos, len, OPTIONS, EOL_OPTIONS)

return Network(caseid, buses, loads, gens, branches)
nrows = count_nrow(bytes, pos, len, OPTIONS) ÷ 4 # Two-winding Transformers data is 4 lines each
@debug "2-winding transformers" nrows pos
two_winding_transformers, pos = parse_records!(
TwoWindingTransformers(nrows), bytes, pos, len, OPTIONS, EOL_OPTIONS
)
return Network(caseid, buses, loads, gens, branches, two_winding_transformers)
end

function parse_caseid(bytes, pos, len, options)
Expand Down Expand Up @@ -88,12 +93,6 @@ function parse_records!(rec::R, bytes, pos, len, options, eol_options)::Tuple{R,
nrows == 0 && return rec, pos
for row in 1:nrows
pos, code = parse_row!(rec, row, bytes, pos, len, options, eol_options)

# Because we're working around end-of-line comments,
# rows with comments won't have hit the newline character yet
if !newline(code)
pos = next_line(bytes, pos, len)
end
end

# Data input is terminated by specifying a bus number of zero.
Expand All @@ -115,7 +114,10 @@ function count_nrow(buf, pos, len, options)
pos += res.tlen
if newline(res.code) || eof(res.code)
nlines += 1
if eof(buf, pos, len) || peekbyte(buf, pos) == UInt8('0')
if eof(buf, pos, len) || (
!eof(buf, pos, len) && peekbyte(buf, pos) == UInt8('0') &&
!eof(buf, pos+1, len) && peekbyte(buf, pos+1) == UInt8(' ')
)
break
end
end
Expand Down Expand Up @@ -155,6 +157,35 @@ function parse_row!(rec::Records, row::Int, bytes, pos, len, options, eol_option

@debug codes(code) row col pos newline=newline(code)
end
# Because we're working around end-of-line comments,
# rows with comments won't have hit the newline character yet
if !newline(code)
pos = next_line(bytes, pos, len)
end
return pos, code
end

function parse_row!(rec::TwoWindingTransformers, row::Int, bytes, pos, len, options, eol_options)
# Each `TwoWindingTransformers` is 4 lines of the file
# cols_per_line = (14, 3, 16, 2)
eol_cols = (14, 17, 33, 35)
ncols = nfields(rec)
@assert ncols == last(eol_cols)
local code::Parsers.ReturnCode
for col in 1:ncols
eltyp = eltype(fieldtype(typeof(rec), col))
opts = ifelse(col in eol_cols, eol_options, options)
val, pos, code = parse_value(eltyp, bytes, pos, len, opts)
@inbounds getfield(rec, col)[row] = val

# Because we're working around end-of-line comments,
# rows with comments won't have hit the newline character yet
if col in eol_cols && !newline(code)
pos = next_line(bytes, pos, len)
end

@debug codes(code) row col pos newline=newline(code)
end
return pos, code
end

Expand Down
Loading

0 comments on commit 5214ad5

Please sign in to comment.