Skip to content

Commit c213c98

Browse files
committed
add backward error calculation documentation
Signed-off-by: Martijn Govers <Martijn.Govers@Alliander.com>
1 parent 64a57df commit c213c98

File tree

1 file changed

+211
-23
lines changed

1 file changed

+211
-23
lines changed

docs/advanced_documentation/algorithms/lu-solver.md

Lines changed: 211 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -167,20 +167,75 @@ slightly different matrix that is then iteratively refined.
167167
#### Pivot perturbation algorithm
168168

169169
The following pivot perturbation algorithm works for both real and complex matrix equations. Let $M$
170-
be the matrix, $\left\||A\right\||_{\infty ,\text{bwod}}$ the
170+
be the matrix, $\left\|M\right\|_{\infty ,\text{bwod}}$ the
171171
[block-wise off-diagonal infinite norm](#block-wise-off-diagonal-infinite-matrix-norm) of the matrix.
172172

173-
1. Set $\epsilon \gets \text{perturbation_threshold} * \left\||A\right\||_{\text{bwod}}$
174-
2. If $|\text{pivot_element}| < \epsilon$, then:
175-
1. Set $\text{phase}\left(\text{pivot_element}\right) \gets \text{pivot_element} / |\text{pivot_element}|$.
176-
2. Set $\text{pivot_element} \gets \text{perturbation_threshold} * \text{phase}\left(\text{pivot_element}\right)$.
173+
1. Set $\epsilon \gets \text{perturbation_threshold} * \left\|M\right\|_{\text{bwod}}$
174+
2. If $|\text{pivot_element}| \lt \epsilon$, then:
175+
1. If $|\text{pivot_element}| = 0$, then:
176+
1. Set $\text{direction} = 1$.
177+
2. Proceed.
178+
2. Else:
179+
1. Set $\text{direction}\left(\text{pivot_element}\right) \gets \text{pivot_element} / |\text{pivot_element}|$.
180+
2. Proceed.
181+
3. Set $\text{pivot_element} \gets \epsilon * \text{direction}\left(\text{pivot_element}\right)$.
177182

178-
<!-- In this equation, $\left\||A\right\||$ denotes the absolute value of the maximum element, as defined in
179-
[Arioli89](https://epubs.siam.org/doi/10.1137/0610013). -->
183+
$\text{direction}$ ensures that the complex phase of the pivot element is preserved, with a fallback
184+
the positive real axis when the pivot element is identically zero.
180185

181186
#### Iterative refinement
182187

183-
TODO
188+
This algorithm is heavily inspired by the GESP algorithm described in
189+
[Li99](https://www.semanticscholar.org/paper/A-Scalable-Sparse-Direct-Solver-Using-Static-Li-Demmel/7ea1c3360826ad3996f387eeb6d70815e1eb3761).
190+
191+
The refinement process improves the solution to the matrix equation $A \cdot x = b$ as follows:
192+
In iteration step $i$, it assumes an existing approximation $x_i$ for $x$. It then defines
193+
the difference between the current best and the actual solution $\Delta x = x - x_i$.
194+
Substiting in the original equation yields $A \cdot (x_i + \Delta x) = b$, so that
195+
$A \cdot \Delta x = b - A \cdot x_i =: r$, where the residual $r$ can be calculated.
196+
An estimation for the left-hand side can be obtained by using the pivot-perturbed matrix
197+
$\tilde{A}$ instead of the original matrix A. Convergence can be reached if $r \to 0$, since then
198+
also $\left\|\Delta x\right\| \to 0$. Solving for $\Delta x$ and substituting back into
199+
$x_{i+1} = x_i + \Delta x$ provides the next best approximation $x_{i+1}$ for $x$.
200+
201+
A measure for the quality of the approximation is given by the $\text{backward_error}$ (see also
202+
[backward error formula](#improved-backward-error-calculation)).
203+
204+
Since the matrix $A$ does not change during this process, the LU decomposition remains valid
205+
throughout the process, so that this iterative refinement can be done at a reasonably low cost.
206+
207+
Given the original matrix equation $A \cdot x = b$ to solve, the pivot perturbated matrix
208+
$\tilde{A}$ with a pre-calculated LU-decomposition, and the convergence threshold $\epsilon$,
209+
the algorithm is as follows:
210+
211+
1. Initialize:
212+
1. Set the initial estimate: $x_{\text{est}} = 0$.
213+
2. Set the initial residual: $r \gets b$.
214+
3. Set the initial backward error: $\text{backward_error} = \infty$.
215+
4. Set the number of iterations to 0.
216+
2. Iteratively refine; loop:
217+
1. Check stop criteria:
218+
1. If $\text{backward_error} \leq \epsilon$, then:
219+
1. Convergence reached: stop the refinement process.
220+
2. Else, if the number of iterations > maximum allowed amount of iterations, then:
221+
1. Convergence not reached; iterative refinement not possible: raise a sparse matrix
222+
error.
223+
3. Else:
224+
1. Increase the number of iterations.
225+
2. Proceed.
226+
2. Solve $\tilde{A} \cdot \Delta x = r$ for $\Delta x$.
227+
3. Calculate the backward error with the original $x$ and $r$ using the [backward error formula](#improved-backward-error-calculation).
228+
4. Set the next estimation of $x$: $x_{\text{est}} \gets x_{\text{est}} + \Delta x$.
229+
5. Set the residual: $r \gets b - A \cdot x$.
230+
231+
Because the backward error is calculated on the $x$ and $r$ from the previous iteration, the
232+
iterative refinement loop will always be executed twice.
233+
234+
The reason a sparse matrix error is raised and not an iteration diverge error, is that it is the
235+
iterative refinement of the matrix equation solution that cannot be solved in the set amount of
236+
iterations - not the set of power system equations. This will only happen when the matrix
237+
equation requires iterative refinement in the first place, which happens only when pivot
238+
perturbation is needed, namely in the case of an ill-conditioned matrix equation.
184239

185240
#### Differences with literature
186241

@@ -196,7 +251,7 @@ They are summarized below.
196251
contains an early-out criterion for the iterative refinement that checks for deminishing returns in
197252
consecutive iterations. It amounts to (in reverse order):
198253

199-
1. If $$\text{backward_error} \gt \frac{1}{2}\text{last_backward_error}$$, then:
254+
1. If $\text{backward_error} \gt \frac{1}{2}\text{last_backward_error}$, then:
200255
1. Stop iterative refinement.
201256
2. Else:
202257
1. Go to next refinement iteration.
@@ -205,28 +260,79 @@ In power systems, however, the fact that the matrix may contain elements
205260
[spanning several orders of magnitude](#element-size-properties-of-power-system-equations) may cause
206261
slow convergence far away from the optimum. The diminishing return criterion would cause the
207262
algorithm to exit before the actual solution is found. Multiple refinement iterations may still
208-
yield better results. The power grid model therefore does not stop on deminishing returns. Instead,
209-
a maximum amount of iterations is used in combination with the error tolerance.
263+
yield better results. The power grid model therefore does not stop on deminishing returns. Instead, a maximum amount of iterations is used in combination with the error tolerance.
264+
265+
##### Improved backward error calculation
266+
267+
In power system equations, the matrix equation $A x = b$ can be very unbalanced: some entries
268+
in the matrix $A$ may be very large while others are zero or very small. The same may be true for
269+
the right-hand side of the equation $b$, as well as its solution $x$. In fact, there may be
270+
certain rows $i$ for which both $\left|b[i]\right|$ and
271+
$\sum_j \left|A[i,j]\right| \left|x[j]\right|$ are small and, therefore, their sum is prone to
272+
rounding errors, which may be several orders larger than machine precision.
273+
274+
[Li99](https://www.semanticscholar.org/paper/A-Scalable-Sparse-Direct-Solver-Using-Static-Li-Demmel/7ea1c3360826ad3996f387eeb6d70815e1eb3761)
275+
uses the following backward error in the [iterative refinement algorithm](#iterative-refinement):
276+
277+
$$
278+
\begin{align*}
279+
\text{backward_error}_{\text{Li}} &= \max_i \frac{\left|r[i]\right|}{\sum_j \left|A[i,j]\right| \left|x[j]\right| + \left|b[i]\right|} \\
280+
&= \max_i \frac{\left|b[i] - \sum_j A[i,j] x[j]\right|}{\sum_j \left|A[i,j]\right| \left|x[j]\right| + \left|b[i]\right|} \\
281+
&= \max_i \frac{\left|r[i]\right|}{\left(\left|A\right| \cdot \left|x\right| + \left|b\right|\right)\left[i\right]}
282+
\end{align*}
283+
$$
284+
285+
In this equation, the symbolic notation
286+
$\left(\left|A\right|\cdot \left|x\right|\right)\left[i\right] := \sum_j |A[i,j]| |x[j]|$, as
287+
defined in [Arioli89](https://epubs.siam.org/doi/10.1137/0610013).
288+
289+
Due to aforementioned, this is prone to rounding errors, and a single row with rounding errors may
290+
cause the entire iterative refinement to fail. The power grid model therefore use a modified
291+
version, in which the denominator is capped to a minimum value, determined by the maximum across all
292+
denominators:
293+
294+
$$
295+
\begin{align*}
296+
D_{\max} &= \max_i {(|A||x| + |b|)_i} \\
297+
berr &= \max_{ i } {\frac{|r|_i}{\max ( (|A||x| + |b|)_i, \epsilon_{berr} D_{\max}) }}
298+
\end{align*}
299+
$$
300+
301+
$\epsilon$ may be chosen. $\epsilon = 0$ means no cut-off, while $\epsilon = 1$ means maximum
302+
cut-off. The former is prone to rounding errors, while the latter may hide issues in rows with small
303+
coefficients by supressing them in the backward error, even if that row's residual is relatively
304+
large, in favor of other rows with larger absolute, but smaller relative, residuals. In conclusion,
305+
$\epsilon$ should be chosen not too large and not too small.
306+
307+
```{note}
308+
$\epsilon = 10^{-4}$ was experimentally determined to be a reasonably good value on a number of
309+
real-world MV grids.
310+
```
210311

211312
##### Block-wise off-diagonal infinite matrix norm
212313

213-
For the pivot perturbation algorithm, a matrix norm is used to determine the relative size of the
214-
current pivot element compared to the rest of the matrix as a measure for the ill-conditioning. The
215-
norm is a variant to the $L_{\infty}$ norm of a matrix, which we call the block-wise off-diagonal
216-
infinite matrix norm ($L_{\infty ,\text{bwod}}$).
314+
For the [pivot perturbation algorithm](#pivot-perturbation-algorithm), a matrix norm is used to
315+
determine the relative size of the current pivot element compared to the rest of the matrix as a
316+
measure for the degree of ill-conditioning. The norm is a variant of the $L_{\infty}$ norm of a
317+
matrix, which we call the block-wise off-diagonal infinite matrix norm
318+
($L_{\infty ,\text{bwod}}$).
217319

218320
Since the power grid model solves the matrix equations using a multi-scale matrix solver (dense
219321
intra-block, block-sparse for the full topological structure of the grid), the norm is also taken on
220-
two levels.
322+
those same levels, so the calculation of the norm is _block-wise_.
221323

222-
In addition, because the diagonal blocks may have much larger elements than the off-diagonal
223-
elements, while the relevant information is contained mostly in in the off-diagonal blocks. As a
224-
result, the block-diagonal elements would undesirably dominate the norm. The power grid model
225-
therefore skips the diagonal blocks when calculating the norm.
324+
In addition, the diagonal blocks may have much larger elements than the off-diagonal elements, while
325+
the relevant information is contained mostly in the off-diagonal blocks. As a result, the
326+
block-diagonal elements would undesirably dominate the norm. The power grid model therefore
327+
restricts the calculation of the norm to _off-diagonal_ blocks.
226328

227-
In short, the $L_{\infty ,\text{bwod}}$ norm it is the $L_{\infty}$ norm of the block-sparse matrix
329+
In short, the $L_{\infty ,\text{bwod}}$-norm it is the $L_{\infty}$ norm of the block-sparse matrix
228330
with the $L_{\infty}$ norm of the individual blocks as elements, where the block-diagonal elements
229-
are skipped at the block-level. The algorithm is as follows:
331+
are skipped at the block-level.
332+
333+
###### Block-wise off-diagonal infinite matrix norm algorithm
334+
335+
The algorithm is as follows:
230336

231337
Let $M\equiv M\left[0:N, 0:N\right]$ be the $N\times N$-matrix with a block-sparse structure and
232338
$M\left[i,j\right]$ its block element at (0-based) indices $(i,j)$, where $i,j = 0..(N-1)$. In turn,
@@ -247,5 +353,87 @@ let $M[i,j] \equiv M_{i,j}\left[0:N_{i,j},0:N_{i,j}\right]$ be the dense block w
247353
1. Set $\text{block_row_norm} \gets \text{block_row_norm} + \left\|M_{i,j}\left[k,l\right]\right\|$.
248354
3. Calculate the new block norm: set
249355
$\text{block_norm} \gets \max\left\{\text{block_norm}, \text{block_row_norm}\right\}$.
356+
4. Continue with the next row of the current block.
250357
4. Set $\text{row_norm} \gets \text{row_norm} + \text{block_norm}$.
251-
3. Calculate the new norm: set $\text{norm} \gets \max\left\{\text{norm}, \text{row_norm}\right\}$.
358+
5. Continue with the next block-column.
359+
3. Calculate the new norm: set
360+
$\text{norm} \gets \max\left\{\text{norm}, \text{row_norm}\right\}$.
361+
4. Continue with the next block-row.
362+
363+
###### Illustration of the block-wise off-diagonal infinite matrix norm calculation
364+
365+
This section aims to illustrate how the $L_{\infty ,\text{bwod}}$-norm differs from a regular
366+
$L_{\infty}$-norm using the following examples.
367+
368+
The first example shows how the taking the block-wise norm affects the calculation of the norm.
369+
370+
$$
371+
\begin{pmatrix}
372+
\begin{pmatrix}
373+
0 && 0 \\
374+
0 && 0
375+
\end{pmatrix} && \begin{pmatrix}
376+
1 && 0 \\
377+
0 && 3
378+
\end{pmatrix} && \begin{pmatrix}
379+
3 && 0 \\
380+
0 && 0
381+
\end{pmatrix} \\
382+
\begin{pmatrix}
383+
5 && 0 \\
384+
0 && 0
385+
\end{pmatrix} &&
386+
\begin{pmatrix}
387+
0 && 0 \\
388+
0 && 0
389+
\end{pmatrix} && \begin{pmatrix}
390+
0 && 0 \\
391+
0 && \frac{1}{2}
392+
\end{pmatrix} \\
393+
\begin{pmatrix}
394+
0 && 0 \\
395+
0 && 0
396+
\end{pmatrix} &&
397+
\begin{pmatrix}
398+
0 && 0 \\
399+
0 && 0
400+
\end{pmatrix} &&
401+
\begin{pmatrix}
402+
1 && 0 \\
403+
0 && 1
404+
\end{pmatrix}
405+
\end{pmatrix}
406+
$$
407+
408+
* The regular $L_{\infty}$-norm is $\max\left\{1+3, 3, 5, \frac{1}{2}, 1, 1\right\} = 5$.
409+
* The block-wise off-diagonal infinity $L_{\infty ,\text{bwod}}$-norm is
410+
$\max\left\{0+\max\left\{1, 3\right\}+\max\left\{3, 0\right\},\max\left\{5, 0\right\} + \max\left\{0, \frac{1}{2}\right\}, 1\right\} = \max\left\{3+3, 5+\frac{1}{2}, 1, 1\right\} = 6$.
411+
412+
The two norms clearly differ and even the elements that contribute most to the norm are different.
413+
414+
The next example shows how keeping only the off-diagonal blocks affects the norm.
415+
416+
$$
417+
\begin{pmatrix}
418+
\begin{pmatrix}
419+
20 && 20 \\
420+
30 && 0
421+
\end{pmatrix} && \begin{pmatrix}
422+
2 && 2 \\
423+
3 && 0
424+
\end{pmatrix} \\
425+
\begin{pmatrix}
426+
0 && 0 \\
427+
0 && 3
428+
\end{pmatrix} && \begin{pmatrix}
429+
100 && 0 \\
430+
0 && 1
431+
\end{pmatrix}
432+
\end{pmatrix}
433+
$$
434+
435+
* The regular $L_{\infty}$-norm is $\max\left\{20+20+2+2,30+3,100,3+1\right\} = \max\left\{44,33,100,4\right\} = 100$.
436+
* The block-wise infinity norm with diagonals would be
437+
$\max\left\{\max\left\{20+20, 30\right\}+\max\left\{2+2, 3\right\},\max\left\{0,3\right\} + \max\left\{100, 1\right\}\right\} = \max\left\{40+4, 3+100\right\} = \max\left\{44, 103\right\} = 103$.
438+
* The $L_{\infty ,\text{bwod}}$-norm is
439+
$\max\left\{\max\left\{2+2, 3\right\},\max\left\{0,3\right\}\right\} = \max\left\{4, 3\right\} = 4$.

0 commit comments

Comments
 (0)