Skip to content

Commit

Permalink
refactor: Rename NewtonBCGS to NewtonKrylov (#5)
Browse files Browse the repository at this point in the history
  • Loading branch information
davidkleiven authored Jun 21, 2020
1 parent c9280a9 commit e4b1f63
Show file tree
Hide file tree
Showing 6 changed files with 27 additions and 27 deletions.
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@ import (
"github.com/davidkleiven/gononlin/nonlin"
)

func ExampleNewtonBCGS() {
// This example shows how one can use NewtonBCGS to solve the
func ExampleNewtonKrylov() {
// This example shows how one can use NewtonKrylov to solve the
// system of equations
// (x-1)^2*(x - y) = 0
// (x-2)^3*cos(2*x/y) = 0
Expand All @@ -29,7 +29,7 @@ func ExampleNewtonBCGS() {
},
}

solver := nonlin.NewtonBCGS{
solver := nonlin.NewtonKrylov{
// Maximum number of Newton iterations
Maxiter: 1000,

Expand Down
2 changes: 1 addition & 1 deletion examples/burger/description.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ The implicit Euler discretized form is given by

*n* denotes the time step, and *i* denotes the spatial position. At each time step a non-linear
system of equations needs to be solved for the unknown <i>u<sub>i</sub><sup>n+1</sup></i>.
In the example file *main.go* this is done by using *NewtonBCGS* method. The solution is shown
In the example file *main.go* this is done by using *NewtonKrylov* method. The solution is shown
below.

![solution](fig/velocityProfile.svg)
Expand Down
4 changes: 2 additions & 2 deletions examples/burger/main.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// main shows how one can use NewtonBCGS to solve the Burgers equation using
// main shows how one can use NewtonKrylov to solve the Burgers equation using
// implicit euler method
package main

Expand Down Expand Up @@ -41,7 +41,7 @@ func main() {
},
}

solver := nonlin.NewtonBCGS{
solver := nonlin.NewtonKrylov{
Tol: 1e-7,
StepSize: 1e-3,
Maxiter: 10000,
Expand Down
34 changes: 17 additions & 17 deletions nonlin/newtonBCGS.go → nonlin/newtonKrylov.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import (
"gonum.org/v1/gonum/mat"
)

// NewtonBCGS solve a non-linear system of equation using the conjugate gradient
// NewtonKrylov solve a non-linear system of equation using the conjugate gradient
// Jacobian Free Newton Krylov method. It is Newton based method, but the Jacobian
// is never explicitly constructed, since we only needs its action onto a vector
// (e.g. Jv, where J is the jacobian matrix and v is a vector). This, makes the
Expand All @@ -19,7 +19,7 @@ import (
// one seeks the folusion F(x) = 0. The method will try at most Maxiter Newton steps
// before it terminates. In case, the maximum number of iterations is reached the Converged
// attribute of the Result struct will be false.
type NewtonBCGS struct {
type NewtonKrylov struct {
// Tolerance for convergence. The method terminates when InfNorm(F(x)) + InfNorm(dx)
// is less than this value. Here, dx represents the amound the position changed on the
// current Newton step
Expand All @@ -44,48 +44,48 @@ type NewtonBCGS struct {
// Solve solves the non-linear system of equations. The method terminates when
// the inifinity norm of F + the inifinity norm of dx is less than the tolerance.
// dx is the change in x between two sucessive iterations.
func (bcgs *NewtonBCGS) Solve(p Problem, x []float64) Result {
func (nk *NewtonKrylov) Solve(p Problem, x []float64) Result {
var deriv DerivativeApprox
switch bcgs.Stencil {
switch nk.Stencil {
case 0, 4:
deriv = NewFourPoint(p.F, bcgs.StepSize)
deriv = NewFourPoint(p.F, nk.StepSize)
case 2:
deriv = NewCentral(p.F, bcgs.StepSize)
deriv = NewCentral(p.F, nk.StepSize)
case 6:
deriv = NewSixPoint(p.F, bcgs.StepSize)
deriv = NewSixPoint(p.F, nk.StepSize)
case 8:
deriv = NewEightPoint(p.F, bcgs.StepSize)
deriv = NewEightPoint(p.F, nk.StepSize)
default:
deriv = NewFourPoint(p.F, bcgs.StepSize)
deriv = NewFourPoint(p.F, nk.StepSize)
}
deriv.X = x

if bcgs.InnerMethod == nil {
bcgs.InnerMethod = &linsolve.GMRES{}
if nk.InnerMethod == nil {
nk.InnerMethod = &linsolve.GMRES{}
}

if bcgs.Maxiter == 0 {
bcgs.Maxiter = 10000
if nk.Maxiter == 0 {
nk.Maxiter = 10000
}

work := make([]float64, 2*len(x))
f0 := work[:len(x)]
f1 := work[len(x):]
b := mat.NewVecDense(len(f0), nil)

for iter := 0; iter < bcgs.Maxiter; iter++ {
for iter := 0; iter < nk.Maxiter; iter++ {
p.F(f0, x)
for i := range f0 {
b.SetVec(i, -f0[i])
}
res, err := linsolve.Iterative(&deriv, b, bcgs.InnerMethod, nil)
res, err := linsolve.Iterative(&deriv, b, nk.InnerMethod, nil)
if err != nil {
log.Fatalf("NewtonKrylov: %s\n", err)
}

//dx := bcgs.solveDX(p, x, f0, deriv)
//dx := nk.solveDX(p, x, f0, deriv)

if InfNorm(f0)+mat.Norm(res.X, math.Inf(1)) < bcgs.Tol {
if InfNorm(f0)+mat.Norm(res.X, math.Inf(1)) < nk.Tol {
return Result{
X: x,
Converged: true,
Expand Down
2 changes: 1 addition & 1 deletion nonlin/newtonBCGS_test.go → nonlin/newtonKrylov_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ func TestBicStab(t *testing.T) {
Tol: 1e-4,
},
} {
solver := NewtonBCGS{
solver := NewtonKrylov{
Maxiter: 1000,
StepSize: 1e-3,
Tol: 1e-7,
Expand Down
6 changes: 3 additions & 3 deletions nonlin/nonlin_example_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ import (
"github.com/davidkleiven/gononlin/nonlin"
)

func ExampleNewtonBCGS() {
// This example shows how one can use NewtonBCGS to solve the
func ExampleNewtonKrylov() {
// This example shows how one can use NewtonKrylov to solve the
// system of equations
// (x-1)^2*(x - y) = 0
// (x-2)^3*cos(2*x/y) = 0
Expand All @@ -20,7 +20,7 @@ func ExampleNewtonBCGS() {
},
}

solver := nonlin.NewtonBCGS{
solver := nonlin.NewtonKrylov{
// Maximum number of Newton iterations
Maxiter: 1000,

Expand Down

0 comments on commit e4b1f63

Please sign in to comment.