-
Notifications
You must be signed in to change notification settings - Fork 46
Sparse matrix error: detect ill conditions on-the-fly #1205
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
mgovers
wants to merge
5
commits into
main
Choose a base branch
from
feature/on-the-fly-sparse-matrix
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
5 commits
Select commit
Hold shift + click to select a range
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -69,6 +69,9 @@ template <rk2_tensor Matrix> class DenseLUFactor { | |
| TranspositionVector col_transpositions{}; | ||
| double max_pivot{}; | ||
|
|
||
| Eigen::Matrix<decltype(cabs(Scalar{})), size, size> accumulated_error{ | ||
| Eigen::Matrix<decltype(cabs(Scalar{})), size, size>::Zero()}; | ||
|
|
||
| // main loop | ||
| for (int8_t pivot = 0; pivot != size; ++pivot) { | ||
| int row_biggest_eigen{}; | ||
|
|
@@ -105,15 +108,50 @@ template <rk2_tensor Matrix> class DenseLUFactor { | |
| col_transpositions[pivot] = col_biggest; | ||
| if (pivot != row_biggest) { | ||
| matrix.row(pivot).swap(matrix.row(row_biggest)); | ||
| accumulated_error.row(pivot).swap(accumulated_error.row(row_biggest)); | ||
| } | ||
| if (pivot != col_biggest) { | ||
| matrix.col(pivot).swap(matrix.col(col_biggest)); | ||
| accumulated_error.row(pivot).swap(accumulated_error.row(col_biggest)); | ||
| } | ||
|
|
||
| // check stability | ||
| Scalar const piv_size = cabs(matrix(pivot, pivot)); | ||
| double const piv_error = accumulated_error(pivot, pivot); | ||
| if (cabs(matrix(pivot, pivot)) <= std::max(accumulated_error.row(pivot).tail(size - pivot).maxCoeff(), | ||
| accumulated_error.col(pivot).tail(size - pivot).maxCoeff())) { | ||
| throw SparseMatrixError{ | ||
| 3, std::format( | ||
| "Accumulated error is equal to or exceeds stability threshold: pivot magnitude = {}, max " | ||
| "accumulated error = {}.", | ||
| cabs(matrix(pivot, pivot)), | ||
| std::max(accumulated_error.row(pivot).tail(size - pivot).maxCoeff(), | ||
| accumulated_error.col(pivot).tail(size - pivot).maxCoeff()))}; | ||
| } | ||
|
|
||
| // use Gaussian elimination to calculate the bottom right corner | ||
| if (pivot < size - 1) { | ||
| // calculate the pivot column | ||
| matrix.col(pivot).tail(size - pivot - 1) /= matrix(pivot, pivot); | ||
| accumulated_error.col(pivot).tail(size - pivot - 1) /= cabs(matrix(pivot, pivot)); | ||
|
|
||
| struct AbsoluteStability { | ||
| typedef double result_type; | ||
| result_type operator()(Matrix::Scalar const& a, Matrix::Scalar const& b) const { | ||
| if (a == Scalar{} || b == Scalar{}) { | ||
| return 0.0; | ||
| } | ||
| return epsilon * (cabs(a) + cabs(b)) / 2; | ||
| } | ||
| }; | ||
|
|
||
| // check for numerical stability by calculating the new diagonal entries and comparing to the old ones | ||
| accumulated_error.bottomRightCorner(size - pivot - 1, size - pivot - 1) += | ||
| matrix.bottomRightCorner(size - pivot - 1, size - pivot - 1) | ||
| .binaryExpr( | ||
| (matrix.col(pivot).tail(size - pivot - 1) * matrix.row(pivot).tail(size - pivot - 1)), | ||
|
Comment on lines
+151
to
+152
Member
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. TODO: also add the contribution from the |
||
| AbsoluteStability{}); | ||
|
|
||
| // calculate the bottom right corner | ||
| matrix.bottomRightCorner(size - pivot - 1, size - pivot - 1).noalias() -= | ||
| matrix.col(pivot).tail(size - pivot - 1) * matrix.row(pivot).tail(size - pivot - 1); | ||
|
|
@@ -134,8 +172,9 @@ template <rk2_tensor Matrix> class DenseLUFactor { | |
| // only check condition number if pivot perturbation is not used | ||
| double const pivot_threshold = has_pivot_perturbation ? 0.0 : epsilon * max_pivot; | ||
| for (int8_t pivot = 0; pivot != size; ++pivot) { | ||
| if (cabs(matrix(pivot, pivot)) < pivot_threshold || !is_normal(matrix(pivot, pivot))) { | ||
| throw SparseMatrixError{}; // can not specify error code | ||
| if (!is_normal(matrix(pivot, pivot))) { | ||
| throw SparseMatrixError{4, std::format("Non-normal dense pivot element {}: value = {}.", pivot, | ||
| cabs(matrix(pivot, pivot)))}; | ||
| } | ||
| } | ||
| capturing::into_the_void(std::move(matrix)); | ||
|
|
@@ -280,7 +319,8 @@ template <class Tensor, class RHSVector, class XVector> class SparseLUSolver { | |
| has_pivot_perturbation_); | ||
| } | ||
| if (!is_normal(lu_matrix[pivot_idx])) { | ||
| throw SparseMatrixError{}; | ||
| throw SparseMatrixError{5, std::format("Non-normal sparse pivot element {}: value = {}.", | ||
| pivot_row_col, cabs(lu_matrix[pivot_idx]))}; | ||
| } | ||
| return {}; | ||
| } | ||
|
|
@@ -432,7 +472,8 @@ template <class Tensor, class RHSVector, class XVector> class SparseLUSolver { | |
| while (backward_error > epsilon_converge) { | ||
| // check maximum iteration, including one initial run | ||
| if (num_iter++ == max_iterative_refinement + 1) { | ||
| throw SparseMatrixError{}; | ||
| throw SparseMatrixError{6, std::format("Iterative refinement did not converge after {} iterations.", | ||
| max_iterative_refinement)}; | ||
| } | ||
| // solve with residual (first time it is the b vector) | ||
| solve_once(data, block_perm_array, residual_.value(), dx_.value()); | ||
|
|
||
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
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
36 changes: 36 additions & 0 deletions
36
tests/data/state_estimation/test-ill-conditioned/input.json
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,36 @@ | ||
| { | ||
| "version": "1.0", | ||
| "type": "input", | ||
| "is_batch": false, | ||
| "attributes": {}, | ||
| "data": { | ||
| "node": [ | ||
| {"id": 0, "u_rated": 100000}, | ||
| {"id": 2, "u_rated": 10000}, | ||
| {"id": 3, "u_rated": 10000}, | ||
| {"id": 4, "u_rated": 10000}, | ||
| {"id": 5, "u_rated": 100000} | ||
| ], | ||
| "line": [ | ||
| {"id": 7, "from_node": 4, "to_node": 2, "from_status": 1, "to_status": 1, "r1": 1, "x1": 0.10000000000000001, "c1": 1.0000000000000001e-05, "tan1": 0, "r0": 10, "x0": 10, "c0": 9.9999999999999995e-07, "tan0": 0, "i_n": 100}, | ||
| {"id": 8, "from_node": 4, "to_node": 3, "from_status": 1, "to_status": 1, "r1": 1, "x1": 0.10000000000000001, "c1": 1.0000000000000001e-05, "tan1": 0, "r0": 10, "x0": 10, "c0": 9.9999999999999995e-07, "tan0": 0, "i_n": 100}, | ||
| {"id": 9, "from_node": 3, "to_node": 2, "from_status": 1, "to_status": 1, "r1": 1, "x1": 0.10000000000000001, "c1": 1.0000000000000001e-05, "tan1": 0, "r0": 10, "x0": 10, "c0": 9.9999999999999995e-07, "tan0": 0, "i_n": 100}, | ||
| {"id": 10, "from_node": 0, "to_node": 5, "from_status": 1, "to_status": 1, "r1": 1, "x1": 0.10000000000000001, "c1": 1.0000000000000001e-05, "tan1": 0, "r0": 10, "x0": 10, "c0": 9.9999999999999995e-07, "tan0": 0, "i_n": 100} | ||
| ], | ||
| "transformer": [ | ||
| {"id": 1177, "from_node": 5, "to_node": 4, "from_status": 1, "to_status": 1, "u1": 100000, "u2": 10000, "sn": 33000000, "uk": 0.1, "pk": 68000, "i0": 0.01, "p0": 16400, "winding_from": 0, "winding_to": 2, "clock": 1, "tap_side": 0, "tap_pos": 2, "tap_min": -10, "tap_max": 10, "tap_nom": 0, "tap_size": 1600, "uk_min": 0.1, "uk_max": 0.1, "pk_min": 68000, "pk_max": 68000} | ||
| ], | ||
| "source": [ | ||
| {"id": 14, "node": 0, "status": 1, "u_ref": 1, "sk": 1000} | ||
| ], | ||
| "sym_load": [ | ||
| {"id": 15, "node": 4, "status": 1, "type": 0} | ||
| ], | ||
| "sym_power_sensor": [ | ||
| {"id": 18, "measured_object": 15, "measured_terminal_type": 4, "power_sigma": 100, "p_measured": 0, "q_measured": 0} | ||
| ], | ||
| "sym_voltage_sensor": [ | ||
| {"id": 19, "measured_object": 0, "u_sigma": 1000, "u_measured": 100000} | ||
| ] | ||
| } | ||
| } |
3 changes: 3 additions & 0 deletions
3
tests/data/state_estimation/test-ill-conditioned/input.json.license
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,3 @@ | ||
| SPDX-FileCopyrightText: Contributors to the Power Grid Model project <[email protected]> | ||
|
|
||
| SPDX-License-Identifier: MPL-2.0 |
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.
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.
this is actually a very weak criterion. by the time your accumulated error even becomes close to your pivot element size, the rest of your calculation may already become garbage. E.g., if you want a precision of
1E-5, odds are that your calculation will not be as precise anymore when your matrix element precision is something like1E-1. This criterion probably should be stricter: