Skip to content

Commit

Permalink
fix: stop divide by zero warning in LU solvers
Browse files Browse the repository at this point in the history
Stop the divide by zero warning in LU solvers by rearranging the
tolerance check.
  • Loading branch information
wd15 committed Mar 22, 2021
1 parent 15d7668 commit f813825
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 12 deletions.
19 changes: 9 additions & 10 deletions fipy/solvers/petsc/linearLUSolver.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,13 @@ class LinearLUSolver(PETScSolver):
A direct solve is performed.
"""

def __init__(self, tolerance=1e-10, iterations=10, precon="lu"):
"""
:Parameters:
- `tolerance`: The required error tolerance.
- `iterations`: The maximum number of iterative steps to perform.
- `precon`: *Ignored*.
- `precon`: *Ignored*.
"""
PETScSolver.__init__(self, tolerance=tolerance,
Expand All @@ -38,26 +38,26 @@ def _solve_(self, L, x, b):
# TODO: SuperLU invoked with PCFactorSetMatSolverType(pc, MATSOLVERSUPERLU)
# see: http://www.mcs.anl.gov/petsc/petsc-dev/src/ksp/ksp/examples/tutorials/ex52.c.html
# PETSc.PC().setFactorSolverType("superlu")

L.assemble()
ksp.setOperators(L)
ksp.setFromOptions()

for iteration in range(self.iterations):
errorVector = L * x - b
tol = errorVector.norm()

if iteration == 0:
tol0 = tol
if (tol / tol0) <= self.tolerance:

if tol <= self.tolerance * tol0:
break

xError = x.copy()

ksp.solve(errorVector, xError)
x -= xError

if 'FIPY_VERBOSE_SOLVER' in os.environ:
from fipy.tools.debug import PRINT
# L.view()
Expand All @@ -66,4 +66,3 @@ def _solve_(self, L, x, b):
PRINT('precon:', ksp.getPC().type)
PRINT('iterations: %d / %d' % (iteration+1, self.iterations))
PRINT('residual:', errorVector.norm(1))

2 changes: 1 addition & 1 deletion fipy/solvers/pysparse/linearLUSolver.py
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ def _solve_(self, L, x, b):
for iteration in range(self.iterations):
errorVector = L * x - b

if (numerix.sqrt(numerix.sum(errorVector**2)) / error0) <= self.tolerance:
if numerix.sqrt(numerix.sum(errorVector**2)) <= self.tolerance * error0:
break

xError = numerix.zeros(len(b), 'd')
Expand Down
2 changes: 1 addition & 1 deletion fipy/solvers/scipy/linearLUSolver.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ def _solve_(self, L, x, b):
for iteration in range(min(self.iterations, 10)):
errorVector = L * x - b

if (numerix.sqrt(numerix.sum(errorVector**2)) / error0) <= self.tolerance:
if numerix.sqrt(numerix.sum(errorVector**2)) <= self.tolerance * error0:
break

xError = LU.solve(errorVector)
Expand Down

0 comments on commit f813825

Please sign in to comment.