-
Notifications
You must be signed in to change notification settings - Fork 22
Reorganize docs, add info, reduce redundancies #337
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
Open
acostarelli
wants to merge
20
commits into
main
Choose a base branch
from
ac/docs
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
20 commits
Select commit
Hold shift + click to select a range
244cf8d
Convert docs tutorials and how-to guides to Literate.jl
5719f8a
file wasn't included
c4a5cbd
started as manual edits, then claude-generated plan for reworking to …
9f4226e
second claude pass
e52b270
third claude pass
c980007
fourth claude pass; asked it to validate its info for references
78f4fe8
fifth claude pass; thinned out howtos that were mostly references
7ac1887
sixth claude pass; better tutorial, redistribute info between howto a…
732e9a0
seventh claude pass; two tutorials, more in API less in reference, be…
cc0c878
claude update tutorials
3ed54eb
manual explanation cleanups, claude cleanups
dcf3685
manual edits + claude rewrite second tutorial
e207a77
change code output
d0ffba4
remove claude skills
a0113c4
address review comments except copilot's
29dd15d
claude math check
d387ef1
copilot review
51b983b
typo and removed trailing commas
ef5d1c4
copilot review: pass linear_solver to inner ptdf when making lodf fro…
39ac03b
nrel -> nlr; suppress build output in tutorial
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 |
|---|---|---|
| @@ -1,70 +1,60 @@ | ||
| ## Computational Considerations | ||
|
|
||
| ### Matrix Construction | ||
|
|
||
| All matrices in `PowerNetworkMatrices.jl` are derived from the `Ybus` matrix (i.e. building any matrix starts with building the `Ybus`). Additionally, all network reductions are applied to the `Ybus` matrix prior to computing the downstream matrices. This design choice is key for enabling high performance and code maintainability: Looping through the system objects is required only when building the `Ybus` (slow) and subsequent operations are built on fast matrix operations on (often sparse) matrices. In addition, network reductions are only defined for the `Ybus` but can be applied uniformly across all matrices. | ||
|
|
||
| ### Sparsity | ||
|
|
||
| Power networks are sparse; most buses connect to only a few others. This sparsity is exploited for computational efficiency via sparse linear solvers: | ||
|
|
||
| - Incidence and admittance matrices are very sparse. | ||
| - Common sensitivity matrices (e.g. PTDF and LODF) are dense. | ||
|
|
||
| ### Automatic Sparsification Tolerance | ||
|
|
||
| The PTDF and LODF are obtained by solving against the reduced susceptance matrix `ABA = Aᵀ B A`, which is sparse. In theory, however, **the inverse of a sparse matrix is dense**: `ABA` is a grounded graph Laplacian, and `ABA⁻¹` has essentially no zeros even though `ABA` does. So the sensitivity matrices come out dense, and for a large network a single PTDF column has one entry per bus — tens of thousands of numbers, almost all of them negligible (a branch is effectively insensitive to an injection electrically far away). | ||
|
|
||
| To recover sparsity we apply a **tolerance** and drop entries below it (`droptol!`). The size of that cutoff is the question this package answers automatically. Every PTDF/LODF constructor takes a `tol` keyword: | ||
|
|
||
| ```julia | ||
| tol::Union{Float64, AutoTolerance} = AutoTolerance() | ||
| ``` | ||
|
|
||
| - A **`Float64`** is an explicit, *absolute* cutoff: any entry with `|x| ≤ tol` is dropped. Use it to pin an exact result (`tol = eps()`) or to sparsify by a fixed number. This is the backward-compatible path. | ||
|
|
||
| - An **[`AutoTolerance`](@ref)** (the default) chooses the cutoff from the data instead of a hand-tuned guess. It drops an entry of a row only when it is below the precision the input data can justify, *relative to that row's own peak*: | ||
|
|
||
| ``` | ||
| drop entry j of row i when |row_i[j]| < α · max|row_i|, | ||
| α = clamp(safety · δ, 1e-6, 1e-2) | ||
| ``` | ||
|
|
||
| where `δ` is the relative precision of the branch reactances (auto-discovered from their significant figures, or set explicitly via `data_precision`). Because the cutoff is *relative to each row's peak*, the achieved column sparsity is independent of the matrix scale and of how ill-conditioned `ABA` is. The 1-norm condition number of `ABA` is still estimated and logged as a diagnostic, but it never enters the cutoff. | ||
|
|
||
| Sparsification only matters at scale, so `AutoTolerance` acts only where it pays off: | ||
|
|
||
| - **On-demand (virtual) matrices at or above `AUTO_TOLERANCE_BUS_LIMIT` buses** are sparsified per requested row/column — this is what lets a column of a large system come back sparse instead of dense. | ||
| - **Small systems and the dense `PTDF`/`LODF` constructors are returned exactly** (`AutoTolerance` is a no-op there), preserving their dense type and numerical values. Pass a `Float64` `tol` to sparsify those explicitly. | ||
|
|
||
| For very large studies, prefer the [`VirtualPTDF`](@ref)/[`VirtualLODF`](@ref) variants: they compute rows on demand and, with the default `AutoTolerance`, store each one sparsely. | ||
|
|
||
| #### Accuracy and limitations | ||
|
|
||
| Sparsification trades exactness for memory, and the relative per-row rule has consequences worth understanding before you rely on a sparsified matrix for a sensitive calculation. Each dropped entry is below `α · max|row|` (`α ≤ 1e-2`, and typically `α ≈ δ ≈ 5e-4`), so the dominant sensitivities are never touched — but the following hold: | ||
|
|
||
| - **The error is one-signed, not zero-mean.** A dropped entry is set to exactly zero, never rounded, so each row's total mass strictly decreases. When you sum many small entries of a row (for example, aggregating a flow contribution across many buses), the truncation errors accumulate in the same direction instead of cancelling. The bias is bounded by `(number of dropped entries) · α · max|row|`. | ||
|
|
||
| - **The cutoff is per row, so global invariants are not preserved.** Each row is sparsified against *its own* peak, with no coupling between rows or columns, so quantities depending on cross-row or cross-column structure — Kirchhoff's current law, a column sum, or the *difference* of two entries — are not conserved. The sharp case is two buses `j, k` both far from branch `i`: `PTDF[i,j]` and `PTDF[i,k]` may be similar in magnitude yet fall on opposite sides of the cutoff. Each *absolute* error stays below the threshold, but the *relative* error on the (tiny) difference `PTDF[i,j] − PTDF[i,k]` can approach 100%. | ||
| - **Auto-discovered precision assumes a power-of-10 base.** With `data_precision = :auto`, `δ` counts the significant figures of the branch reactances. Decimal significant-figure counts are invariant under multiplication by a power of ten (the conventional 100 MVA base) but **not** under an arbitrary impedance base: data converted by a non-power-of-10 base (e.g. `Z_base = kV²/MVA = 190.44 Ω`) reads more figures than it carries, so `:auto` *over-estimates* precision. The direction is safe — a smaller `α`, hence *less* aggressive dropping — but on such data prefer an explicit `data_precision`. | ||
| - **Contingency (Woodbury) corrections do not amplify the error.** In [`VirtualMODF`](@ref) the cutoff is applied to the *final* post-contingency row, after the exact Woodbury solve; the correction is computed from exact factorization solves, never sparsified rows. The error stays bounded by the cutoff however near-critical (near-islanding) the contingency is, even when the Woodbury update is severely ill-conditioned. Verified directly in the test suite. | ||
|
|
||
| When you need an exact result — to preserve KCL, to difference two small sensitivities, or to validate against a reference — pass a `Float64` `tol`: use `tol = eps()` for an unsparsified matrix, or a deliberate fixed cutoff for a reproducible absolute tolerance. `AutoTolerance` is the memory-versus-accuracy lever; the explicit `Float64` paths remain available for when exactness matters more than size. | ||
|
|
||
| ### Matrix Sizes | ||
|
|
||
| A system with $N_b$ buses and $N_a$ arcs has matrix dimensions: | ||
|
|
||
| - Incidence: $N_a × N_b$ (sparse) | ||
| - Admittance: $N_b × N_b$ (sparse) | ||
| - PTDF: $N_a × N_b$ (dense) | ||
| - LODF: $N_a × N_a$ (dense) | ||
|
|
||
| ### Computational Complexity | ||
|
|
||
| | Operation | Complexity | Notes | | ||
| |:----------------- |:-------------------- |:------------------------------ | | ||
| | Incidence Matrix | O($N_a$) | Simple topology scan | | ||
| | Admittance Matrix | O($N_a$) | Includes electrical parameters | | ||
| | PTDF | O($N_b^3$) | Requires matrix inversion | | ||
| | LODF | O($N_a \cdot N_b^2$) | Derived from PTDF | | ||
| # Computational Considerations | ||
|
|
||
| ## Sparsity | ||
|
|
||
| Power networks are sparse — most buses connect to only a few others — and this is | ||
| exploited via sparse linear solvers, so the connectivity matrices ([`IncidenceMatrix`](@ref) and [`Ybus`](@ref)) | ||
| are very sparse. The sensitivity matrices ([`PTDF`](@ref), [`LODF`](@ref)), however, are obviously dense: e.g., one entry per bus in every | ||
| [`PTDF`](@ref) column, most of them negligible because a branch is nearly insensitive | ||
| to an injection electrically far away. | ||
|
|
||
| ## Sparsification and tolerance | ||
|
|
||
| Those negligible entries can be dropped to recover sparsity. The **tolerance** is the | ||
| cutoff below which an entry is set to zero. The default [`AutoTolerance`](@ref) picks | ||
| it from the data as a *relative per-row* drop — an entry is dropped when | ||
| `|x| < α · max|row|` — which keeps large matrices sparse while leaving small systems | ||
| and the dense constructors exact. The exact rule (including the bus-count gate that | ||
| makes it a no-op on small systems and the `Float64` `tol` alternative) is in the | ||
| [`AutoTolerance`](@ref) docstring. For very large studies prefer the | ||
| [`VirtualPTDF`](@ref)/[`VirtualLODF`](@ref) variants, which compute rows on demand | ||
| and store each one sparsely. | ||
|
|
||
| ### Accuracy and limitations | ||
|
|
||
| Sparsification trades exactness for memory. Because each dropped entry is below | ||
| `α · max|row|` (`α ≤ 1e-2`, typically `α ≈ 5e-4`), the dominant sensitivities are | ||
| never touched — but two properties are worth understanding before relying on a | ||
| sparsified matrix for a sensitive calculation: | ||
|
|
||
| - **The error is one-signed, not zero-mean.** A dropped entry becomes exactly | ||
| zero, so a row's total mass strictly decreases. Summing many small entries of a | ||
| row — e.g. aggregating a transfer's flow contribution across a subsystem's buses — | ||
| accumulates that truncation in one direction instead of cancelling. The bias is | ||
| bounded by `(number of dropped entries) · α · max|row|`. | ||
| - **The cutoff is per row, so cross-row/column invariants are not preserved.** Each | ||
| row is sparsified against its own peak, so quantities that couple different rows or | ||
| columns — Kirchhoff's current law, a column sum, the *difference* of two entries — | ||
| are not conserved. Two buses `j, k` both far from branch `i` can have similar | ||
| `PTDF[i,j]`, `PTDF[i,k]` land on opposite sides of the cutoff: each absolute error | ||
| stays under threshold, yet the relative error on their tiny difference can approach | ||
| 100%. | ||
|
|
||
| Contingency corrections do not compound this: in [`VirtualMODF`](@ref) the cutoff is | ||
| applied to the *final* post-contingency row, after the exact Woodbury solve, so the | ||
| error stays bounded by the cutoff however near-critical the contingency. | ||
|
|
||
| When you need an exact result — to preserve KCL, to difference two small | ||
| sensitivities, or to validate against a reference — pass a `tol::Float64` | ||
| (`tol = eps()` for an unsparsified matrix, or a deliberate fixed cutoff). | ||
|
|
||
| ## Matrix sizes and complexity | ||
|
|
||
| A system with $N_b$ buses and $N_a$ arcs: | ||
|
|
||
| | Operation | Dimensions | Complexity | Notes | | ||
| |:----------------- |:-------------------- |:-------------------- |:------------------------------ | | ||
| | Incidence Matrix | $N_a × N_b$ (sparse) | $O(N_a)$ | Simple topology scan | | ||
| | Admittance Matrix | $N_b × N_b$ (sparse) | $O(N_a)$ | Includes electrical parameters | | ||
| | PTDF | $N_a × N_b$ (dense) | $O(N_b^3)$ | Requires matrix inversion | | ||
| | LODF | $N_a × N_a$ (dense) | $O(N_a \cdot N_b^2)$ | Derived from PTDF | |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.