This repository has been archived by the owner on Oct 1, 2019. It is now read-only.
/
bicgstab.go
141 lines (131 loc) · 3.33 KB
/
bicgstab.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
// Copyright ©2017 The gonum Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package iterative
import (
"errors"
"math"
"github.com/gonum/floats"
)
// BiCGSTAB implements the BiConjugate Gradient STABilized iterative method with
// preconditioning for solving the system of linear equations
// Ax = b,
// where A is a non-symmetric matrix. For symmetric positive definite systems
// use CG.
//
// BiCGSTAB needs MatVec and PSolve matrix operations.
type BiCGSTAB struct {
first bool
resume int
rho, rhoPrev float64
alpha float64
omega float64
rt []float64
p []float64
v []float64
t []float64
phat []float64
s []float64
shat []float64
}
// Init implements the Method interface.
func (b *BiCGSTAB) Init(dim int) {
if dim <= 0 {
panic("BiCGSTAB: dimension not positive")
}
b.rt = reuse(b.rt, dim)
b.p = reuse(b.p, dim)
b.v = reuse(b.v, dim)
b.t = reuse(b.t, dim)
b.phat = reuse(b.phat, dim)
b.s = reuse(b.s, dim)
b.shat = reuse(b.shat, dim)
b.first = true
b.resume = 1
}
// Iterate implements the Method interface.
func (b *BiCGSTAB) Iterate(ctx *Context) (Operation, error) {
switch b.resume {
case 1:
if b.first {
copy(b.rt, ctx.Residual)
}
b.rho = floats.Dot(b.rt, ctx.Residual)
if math.Abs(b.rho) < rhoBreakdownTol {
b.resume = 0 // Calling Iterate again without Init will panic.
return NoOperation, errors.New("BiCGSTAB: rho breakdown")
}
if b.first {
copy(b.p, ctx.Residual)
} else {
beta := (b.rho / b.rhoPrev) * (b.alpha / b.omega)
floats.AddScaled(b.p, -b.omega, b.v) // p_i -= ω * v_i
floats.Scale(beta, b.p) // p_i *= β
floats.Add(b.p, ctx.Residual) // p_i += r_i
}
ctx.Src = b.p
ctx.Dst = b.phat
b.resume = 2
return PSolve, nil
// Solve M p^_i = p_i.
case 2:
ctx.Src = b.phat
ctx.Dst = b.v
b.resume = 3
return MatVec, nil
// Compute Ap^_i -> v_i.
case 3:
b.alpha = b.rho / floats.Dot(b.rt, b.v)
// Early check for tolerance.
floats.AddScaled(ctx.Residual, -b.alpha, b.v)
copy(b.s, ctx.Residual)
ctx.Src = nil
ctx.Dst = nil
ctx.ResidualNorm = floats.Norm(ctx.Residual, 2)
ctx.Converged = false
b.resume = 4
return CheckResidualNorm, nil
case 4:
if ctx.Converged {
floats.AddScaled(ctx.X, b.alpha, b.phat)
b.resume = 0 // Calling Iterate again without Init will panic.
return EndIteration, nil
}
ctx.Src = b.s
ctx.Dst = b.shat
b.resume = 5
return PSolve, nil
// Solve M s^_i = r_i.
case 5:
ctx.Src = b.shat
ctx.Dst = b.t
b.resume = 6
return MatVec, nil
// Compute As^_i -> t_i.
case 6:
b.omega = floats.Dot(b.t, b.s) / floats.Dot(b.t, b.t)
floats.AddScaled(ctx.X, b.alpha, b.phat)
floats.AddScaled(ctx.X, b.omega, b.shat)
floats.AddScaled(ctx.Residual, -b.omega, b.t)
ctx.Src = nil
ctx.Dst = nil
ctx.ResidualNorm = floats.Norm(ctx.Residual, 2)
ctx.Converged = false
b.resume = 7
return CheckResidualNorm, nil
case 7:
if ctx.Converged {
b.resume = 0 // Calling Iterate again without Init will panic.
return EndIteration, nil
}
if math.Abs(b.omega) < omegaBreakdownTol {
return NoOperation, errors.New("BiCGSTAB: omega breakdown")
}
b.rhoPrev = b.rho
b.first = false
b.resume = 1
return EndIteration, nil
default:
panic("BiCGSTAB: Init not called")
}
}