Skip to content

Commit

Permalink
clean up jacobi bicg
Browse files Browse the repository at this point in the history
  • Loading branch information
rowanc1 committed Jan 8, 2017
1 parent d2e2023 commit b4f93d5
Showing 1 changed file with 13 additions and 19 deletions.
32 changes: 13 additions & 19 deletions pymatsolver/iterative.py
Expand Up @@ -9,41 +9,34 @@
from pymatsolver.solvers import Base


def _jacobi_operator(A):
nSize = A.shape[0]
Ainv = sp.spdiags(1./A.diagonal(), 0, nSize, nSize)
return sp.linalg.interface.aslinearoperator(Ainv)


class BicgJacobi(Base):
"""Bicg Solver with Jacobi preconditioner"""

isfactored = None
_factored = False
solver = None
M = None
maxIter = 1000
TOL = 1E-6
maxiter = 1000
tol = 1E-6

def __init__(self, A, symmetric=True):
self.A = A
self.symmetric = symmetric
self.dtype = A.dtype
self.solver = bicgstab
# Jacobi Preconditioner
self.M = _jacobi_operator(A)
self.isfactored = True

def factor(self):
if self.isfactored is not True:
self.M = _jacobi_operator(self.A)
self.isfactored = True
if self._factored:
return
nSize = self.A.shape[0]
Ainv = sp.spdiags(1./self.A.diagonal(), 0, nSize, nSize)
self.M = sp.linalg.interface.aslinearoperator(Ainv)
self._factored = True

def _solve1(self, rhs):
self.factor()
sol, info = self.solver(
self.A, rhs,
tol=self.TOL,
maxiter=self.maxIter,
tol=self.tol,
maxiter=self.maxiter,
M=self.M
)
return sol
Expand All @@ -53,11 +46,12 @@ def _solveM(self, rhs):
sol = []
for icol in range(rhs.shape[1]):
sol.append(self.solver(self.A, rhs[:, icol].flatten(),
tol=self.TOL, maxiter=self.maxIter, M=self.M)[0])
tol=self.tol, maxiter=self.maxiter, M=self.M)[0])
out = np.hstack(sol)
out.shape
return out

def clean(self):
self.M = None
self.A = None
self._factored = False

0 comments on commit b4f93d5

Please sign in to comment.