Skip to content

Commit

Permalink
commented out old code for resvec,iters, and relres in bicgstab, retu…
Browse files Browse the repository at this point in the history
…rning None for now. approved by Dr Wright. chagned gmres option to raise a not implemted error for now since gmres will be implemented later
  • Loading branch information
seanbsu committed Mar 13, 2024
1 parent e5dc699 commit c3e0acd
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 28 deletions.
27 changes: 16 additions & 11 deletions src/pymgm_test/mgm/solve.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,20 +24,25 @@ def solve(self,mgmobj, fh, tol=1e-8, accel='none', maxIters=None):
smooths = [mgmobj[8]['preSmooth'], mgmobj[9]['postSmooth']]

if accel.lower() == 'gmres':
A = csc_matrix(matvecOp(levelsData, np.eye(N)))
b = fh
uh, flag = gmres(A, b, tol=tol, maxiter=maxIters)
iters = flag # Note: scipy gmres returns iteration count or convergence flag
relres = np.linalg.norm(b - A @ uh) / np.linalg.norm(b)
if flag != 0:
print(f"GMRES did not converge to a tolerance of {tol} in {maxIters} iterations")
# A = csc_matrix(matvecOp(levelsData, np.eye(N)))
# b = fh
# uh, flag = gmres(A, b, tol=tol, maxiter=maxIters) #python gmres proabaly not what we need
# iters = flag # Note: scipy gmres returns iteration count or convergence flag
# relres = np.linalg.norm(b - A @ uh) / np.linalg.norm(b)
# if flag != 0:
# print(f"GMRES did not converge to a tolerance of {tol} in {maxIters} iterations")
raise NotImplementedError
elif accel.lower() == 'bicgstab':
A_operator = LinearOperator(shape=(N, N), matvec=matvecOp)
b = fh
uh, flag = bicgstab(A_operator, b, tol=tol, maxiter=maxIters)
iters = flag[1] # Note: scipy bicgstab returns iteration count and convergence flag
relres = np.linalg.norm(b - A @ uh) / np.linalg.norm(b)
if flag[1] != 0:
uh, flag= bicgstab(A_operator, b, tol=tol, maxiter=maxIters)
uh= uh.reshape(-1,1)
# resvec = b - A_operator.dot(uh)
# relres = np.linalg.norm(resvec) / np.linalg.norm(b)
resvec = None
relres = None
iters= None
if flag != 0:
print(f"BiCGStab did not converge to a tolerance of {tol} in {maxIters} iterations")
elif accel.lower() == 'none':
uh, flag, relres, iters, resvec = mgmMethod(levelsData, fh, tol, maxIters, uh0, smooths)
Expand Down
29 changes: 12 additions & 17 deletions test/test_mgm_abstract.py
Original file line number Diff line number Diff line change
Expand Up @@ -284,29 +284,24 @@ def test_solve_bicgstab_accel(mgmObj):
# Assert the result dimensions
assert np.allclose(expected_uh, uh)
assert flag == 0
assert np.allclose(expected_relres, relres)
assert iters == 13
assert np.allclose(expected_resvec, resvec)
## bigcstab doesn't return relres, iters and resvec for now uh and the flag are ok
# assert np.allclose(expected_relres, relres)
# assert iters == 13
# assert np.allclose(expected_resvec, resvec)

def test_solve_no_acceleration_large_system(mgmObj):
# Test with a larger system
fh = np.ones(100) # Example right-hand side for a larger system
tol = 1e-8 # Default tolerance
def test_solve_gmres_accel(mgmObj):
file_path = construct_file_path('solve_data', 'fh_before_solve_bicgstab.mat')
fh = sp.io.loadmat(file_path)['fh']
tol = 1e-10 # Default tolerance
maxIters = 100 # Default maximum number of iterations
accel = 'none' # No acceleration
accel = 'gmres'

# Instantiate TestMGMImplementation object
mgm_obj = TestMGMImplementation()

# Call solve method with no acceleration and default parameters for a larger system
uh, flag, relres, iters, resvec = mgm_obj.solve(mgmObj, fh, tol, accel, maxIters)

# Assert the result dimensions
assert uh.shape == fh.shape
assert isinstance(flag, int)
assert isinstance(relres, float)
assert isinstance(iters, int)
assert isinstance(resvec, np.ndarray)
# Call solve method with no acceleration and default parameters
with pytest.raises(NotImplementedError):
uh, flag, relres, iters, resvec = mgm_obj.solve(mgmObj, fh, tol, accel, maxIters)



Expand Down

0 comments on commit c3e0acd

Please sign in to comment.