-
Notifications
You must be signed in to change notification settings - Fork 22
add fixed point diagnostics #350
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
Draft
luke-kiernan
wants to merge
14
commits into
main
Choose a base branch
from
lk/fixed-point-spectral-radius
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.
Draft
Changes from all commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
248d764
add fixed point diagnostics
luke-kiernan cbb56f5
address copilot's comments
luke-kiernan 3884b4c
robust homotopy: respect override x0
luke-kiernan 5ce1dad
post-rebase fixes
luke-kiernan 8f433c4
test RH w dist slack; remove PF from test env.
luke-kiernan a5ae37d
override x0 kwarg eliminated
luke-kiernan 474e0fa
add an adaptive method
luke-kiernan bd2c1e6
add back log diagnostics helper; make iteration not reset when switch…
luke-kiernan 1166f9c
second pass at adaptive method
luke-kiernan cd1be0c
small test fixes
luke-kiernan 7ce686d
format
luke-kiernan 9601bf5
Make solver diagnostics formulation-aware
luke-kiernan b7eab22
Add smallest-magnitude Jacobian eigenvalue routine
luke-kiernan 29ac70c
Wire min-eigenvalue diagnostic to its own flag
luke-kiernan 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
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 |
|---|---|---|
| @@ -0,0 +1,242 @@ | ||
| """ | ||
| acpf_hvvp(data, time_step, v, u) -> Vector{Float64} | ||
|
|
||
| Compute the bilinear form of every component Hessian of the AC power flow residual | ||
| with two vectors `v` and `u`: | ||
|
|
||
| ```math | ||
| w[k] = v^\\top \\left(\\frac{\\partial^2 F_k}{\\partial x \\partial x^\\top}\\right) u | ||
| ``` | ||
|
|
||
| where ``F_k`` is the ``k``-th component of the AC power flow residual at the current | ||
| state stored in `data` (i.e., `data.bus_magnitude` and `data.bus_angles`). | ||
|
|
||
| This is matrix-free: the residual's 3-tensor of second derivatives is never | ||
| materialized. See [`_create_jacobian_matrix_structure`](@ref) for the | ||
| bus-type-dependent state vector convention. | ||
|
|
||
| LCC state variables are not yet supported; the returned vector covers only the | ||
| `2 * num_buses` bus state entries. | ||
| """ | ||
| function acpf_hvvp( | ||
| data::ACPowerFlowData, | ||
| time_step::Int, | ||
| v::AbstractVector{Float64}, | ||
| u::AbstractVector{Float64}, | ||
| ) | ||
| Yb = data.power_network_matrix.data | ||
| Vm = view(data.bus_magnitude, :, time_step) | ||
| θ = view(data.bus_angles, :, time_step) | ||
| num_buses = first(size(data.bus_type)) | ||
| n = 2 * num_buses | ||
| @assert length(v) >= n && length(u) >= n | ||
| w = zeros(Float64, n) | ||
|
|
||
| for i in 1:num_buses | ||
| bt_i = data.bus_type[i, time_step] | ||
| has_θi = (bt_i == PSY.ACBusTypes.PQ) || (bt_i == PSY.ACBusTypes.PV) | ||
| Pi_θiθi, Qi_θiθi = 0.0, 0.0 | ||
| Pi_Viθi, Qi_Viθi = 0.0, 0.0 | ||
|
|
||
| for k in data.neighbors[i] | ||
| k == i && continue | ||
| bt_k = data.bus_type[k, time_step] | ||
| has_θk = (bt_k == PSY.ACBusTypes.PQ) || (bt_k == PSY.ACBusTypes.PV) | ||
| Gik, Bik = real(Yb[i, k]), imag(Yb[i, k]) | ||
| θik = θ[i] - θ[k] | ||
| s, c = sin(θik), cos(θik) | ||
|
|
||
| if has_θi | ||
| d2P_θkθi = Vm[i] * Vm[k] * (Gik * c + Bik * s) | ||
| d2Q_θkθi = Vm[i] * Vm[k] * (Gik * s - Bik * c) | ||
| # contribution towards sum in ∂²Δ{Pᵢ,Qᵢ}/∂θᵢ∂θᵢ | ||
| Pi_θiθi -= d2P_θkθi | ||
| Qi_θiθi -= d2Q_θkθi | ||
|
|
||
| if has_θk | ||
| # ∂²Δ{Pᵢ,Qᵢ}/∂θₖ∂θᵢ | ||
| # really ∂²ΔPᵢ/∂θₖ∂θᵢ * v[θₖ] * u[θᵢ] + ∂²ΔQᵢ/∂θᵢ∂θₖ * v[θᵢ] * u[θₖ] | ||
| # but ∂²ΔPᵢ/∂θₖ∂θᵢ equals ∂²ΔPᵢ/∂θᵢ∂θₖ. | ||
| w[2 * i - 1] += d2P_θkθi * (v[2 * k] * u[2 * i] + v[2 * i] * u[2 * k]) | ||
| w[2 * i] += d2Q_θkθi * (v[2 * k] * u[2 * i] + v[2 * i] * u[2 * k]) | ||
| end | ||
| end | ||
|
|
||
| if bt_i == PSY.ACBusTypes.PQ | ||
| d2P_θkVi = Vm[k] * (Gik * s - Bik * c) | ||
| d2Q_θkVi = Vm[k] * (-Gik * c - Bik * s) | ||
| # contribution towards sum in ∂²Δ{Pᵢ,Qᵢ}/∂θᵢ∂Vᵢ | ||
| Pi_Viθi -= d2P_θkVi | ||
| Qi_Viθi -= d2Q_θkVi | ||
|
|
||
| if has_θk | ||
| # ∂²Δ{Pᵢ,Qᵢ}/∂θₖ∂Vᵢ | ||
| w[2 * i - 1] += | ||
| d2P_θkVi * (v[2 * k] * u[2 * i - 1] + v[2 * i - 1] * u[2 * k]) | ||
| w[2 * i] += | ||
| d2Q_θkVi * (v[2 * k] * u[2 * i - 1] + v[2 * i - 1] * u[2 * k]) | ||
| end | ||
| end | ||
|
|
||
| if has_θk | ||
| # ∂²Δ{Pᵢ,Qᵢ}/∂²θₖ | ||
| d2P_θkθk = Vm[i] * Vm[k] * (-Gik * c - Bik * s) | ||
| d2Q_θkθk = Vm[i] * Vm[k] * (-Gik * s + Bik * c) | ||
| w[2 * i - 1] += d2P_θkθk * v[2 * k] * u[2 * k] | ||
| w[2 * i] += d2Q_θkθk * v[2 * k] * u[2 * k] | ||
| end | ||
|
|
||
| if bt_k == PSY.ACBusTypes.PQ | ||
| # ∂²Δ{Pᵢ,Qᵢ}/∂θₖ∂Vₖ | ||
| d2P_θkVk = Vm[i] * (Gik * s - Bik * c) | ||
| d2Q_θkVk = Vm[i] * (-Gik * c - Bik * s) | ||
| w[2 * i - 1] += | ||
| d2P_θkVk * (v[2 * k] * u[2 * k - 1] + v[2 * k - 1] * u[2 * k]) | ||
| w[2 * i] += | ||
| d2Q_θkVk * (v[2 * k] * u[2 * k - 1] + v[2 * k - 1] * u[2 * k]) | ||
|
|
||
| if bt_i == PSY.ACBusTypes.PQ | ||
| # ∂²Δ{Pᵢ,Qᵢ}/∂Vₖ∂Vᵢ | ||
| d2P_VkVi = Gik * c + Bik * s | ||
| d2Q_VkVi = Gik * s - Bik * c | ||
| w[2 * i - 1] += | ||
| d2P_VkVi * | ||
| (v[2 * k - 1] * u[2 * i - 1] + v[2 * i - 1] * u[2 * k - 1]) | ||
| w[2 * i] += | ||
| d2Q_VkVi * | ||
| (v[2 * k - 1] * u[2 * i - 1] + v[2 * i - 1] * u[2 * k - 1]) | ||
| end | ||
|
|
||
| if has_θi | ||
| # ∂²Δ{Pᵢ,Qᵢ}/∂Vₖ∂θᵢ | ||
| d2P_Vkθi = -Vm[i] * (Gik * s - Bik * c) | ||
| d2Q_Vkθi = -Vm[i] * (-Gik * c - Bik * s) | ||
| w[2 * i - 1] += | ||
| d2P_Vkθi * (v[2 * k - 1] * u[2 * i] + v[2 * i] * u[2 * k - 1]) | ||
| w[2 * i] += | ||
| d2Q_Vkθi * (v[2 * k - 1] * u[2 * i] + v[2 * i] * u[2 * k - 1]) | ||
| end | ||
| end | ||
| end | ||
|
|
||
| # diagonal terms in i: accumulated sums [except ∂²Vᵢ] | ||
| if has_θi | ||
| # ∂²Δ{Pᵢ,Qᵢ}/∂²θᵢ | ||
| w[2 * i - 1] += Pi_θiθi * v[2 * i] * u[2 * i] | ||
| w[2 * i] += Qi_θiθi * v[2 * i] * u[2 * i] | ||
| end | ||
|
|
||
| if bt_i == PSY.ACBusTypes.PQ | ||
| # ∂²Δ{Pᵢ,Qᵢ}/∂Vᵢ∂θᵢ | ||
| w[2 * i - 1] += | ||
| Pi_Viθi * (v[2 * i - 1] * u[2 * i] + v[2 * i] * u[2 * i - 1]) | ||
| w[2 * i] += | ||
| Qi_Viθi * (v[2 * i - 1] * u[2 * i] + v[2 * i] * u[2 * i - 1]) | ||
|
|
||
| # ∂²Δ{Pᵢ,Qᵢ}/∂²Vᵢ | ||
| d2P_ViVi = 2.0 * real(Yb[i, i]) | ||
| d2Q_ViVi = -2.0 * imag(Yb[i, i]) | ||
| w[2 * i - 1] += d2P_ViVi * v[2 * i - 1] * u[2 * i - 1] | ||
| w[2 * i] += d2Q_ViVi * v[2 * i - 1] * u[2 * i - 1] | ||
| end | ||
| end | ||
|
|
||
| return w | ||
| end | ||
|
|
||
| """ | ||
| compute_fixed_point_spectral_radius(data, time_step; x0, tol, maxiter, krylovdim) | ||
| -> (ρ::Float64, info, condest::Float64) | ||
|
|
||
| Estimate the spectral radius of the Jacobian of the Newton fixed-point map | ||
|
|
||
| ```math | ||
| g(x) = x - J(x)^{-1} F(x) | ||
| ``` | ||
|
|
||
| at the state `x0` (default: the result of `calculate_x0(data, time_step)`). The | ||
| spectral radius ``\\rho(\\partial g / \\partial x)`` is a local convergence | ||
| diagnostic for Newton-Raphson: ``\\rho < 1`` is necessary (and locally sufficient) | ||
| for the fixed-point iteration to converge from a small enough neighborhood of | ||
| ``x``. Values close to or above 1 indicate that NR is at risk of stalling or | ||
| diverging from this starting point. | ||
|
|
||
| Differentiating ``g`` and using ``\\partial F / \\partial x = J`` gives the | ||
| identity | ||
|
|
||
| ```math | ||
| (G v)_k = \\bigl(J^{-1} w\\bigr)_k, | ||
| \\qquad w_k = v^\\top H_k u, | ||
| \\qquad u = J^{-1} F(x) | ||
| ``` | ||
|
|
||
| where ``H_k`` is the Hessian of the ``k``-th residual component. ``G`` is never | ||
| materialized: the matvec ``G v`` is computed as one [`acpf_hvvp`](@ref) call | ||
| (producing ``w``) followed by two `KLU` back-solves (one for ``u``, one for | ||
| ``J^{-1} w``). KrylovKit's | ||
| matrix-free Lanczos / Arnoldi solver then extracts the eigenvalue of largest | ||
| magnitude. | ||
|
|
||
| # Notes | ||
|
|
||
| - At a true power-flow solution, ``F(x) = 0`` so ``u = 0`` and ``\\rho(G) = 0`` | ||
| trivially. The diagnostic is meaningful at *non-solution* iterates such as the | ||
| flat-start `x0`. | ||
| - LCC state variables are not yet supported. | ||
| """ | ||
| function compute_fixed_point_spectral_radius( | ||
| data::ACPowerFlowData, | ||
| time_step::Int; | ||
| x0::Union{Vector{Float64}, Nothing} = nothing, | ||
| tol::Float64 = 1e-6, | ||
| maxiter::Int = 200, | ||
| krylovdim::Int = 30, | ||
| ) | ||
| if size(data.lcc.p_set, 1) > 0 | ||
| throw( | ||
| ArgumentError( | ||
| "compute_fixed_point_spectral_radius does not yet support LCC HVDC systems", | ||
| ), | ||
| ) | ||
| end | ||
|
|
||
| residual = ACPowerFlowResidual(data, time_step) | ||
| jac = ACPowerFlowJacobian(residual, time_step) | ||
| x = isnothing(x0) ? calculate_x0(data, time_step) : copy(x0) | ||
| residual(x, time_step) | ||
| jac(time_step) | ||
| return _fixed_point_spectral_radius!( | ||
| data, residual, jac, time_step; | ||
| tol = tol, maxiter = maxiter, krylovdim = krylovdim, | ||
| ) | ||
| end | ||
|
|
||
| """In-place spectral radius computation that reuses an already-evaluated | ||
| `residual` and `jac` (both must have been called at the current state). Returns | ||
| `(ρ, info, condest)`, where `condest` is a Hager 1-norm estimate of the | ||
| condition number of `jac.Jv` computed from the same KLU factor used for the | ||
| spectral radius matvecs. Used by the per-iteration monitor inside | ||
| `_run_power_flow_method`.""" | ||
| function _fixed_point_spectral_radius!( | ||
| data::ACPowerFlowData, | ||
| residual::ACPowerFlowResidual, | ||
| jac::ACPowerFlowJacobian, | ||
| time_step::Int; | ||
| tol::Float64 = 1e-6, | ||
| maxiter::Int = 200, | ||
| krylovdim::Int = 30, | ||
| ) | ||
| n = 2 * size(data.bus_type, 1) | ||
| F = KLU.klu(jac.Jv) | ||
|
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Creating a new factorization each time here is less-than-efficient. Might be not worth fixing, though--if I recall correctly, the KrylovKit Also, I'm using closures and boxing here: |
||
| u = F \ copy(residual.Rv) | ||
| matvec(v::AbstractVector) = F \ acpf_hvvp(data, time_step, v, u) | ||
| # Deterministic init for reproducibility across runs / CI logs. | ||
| v_init = ones(Float64, n) ./ sqrt(n) | ||
| vals, _, info = KrylovKit.eigsolve( | ||
| matvec, v_init, 1, :LM; | ||
|
luke-kiernan marked this conversation as resolved.
|
||
| tol = tol, maxiter = maxiter, krylovdim = krylovdim, | ||
| ) | ||
| ρ = isempty(vals) ? NaN : abs(vals[1]) | ||
| condest = KLU.condest(F) | ||
| return ρ, info, condest | ||
| end | ||
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.
Uh oh!
There was an error while loading. Please reload this page.