def solve_newton_with_dt(func, u0, args, kargs, dt, max_iter, abs_tol, rel_tol, verbose): u = adarray(value(u0).copy()) _DEBUG_perturb_new(u) for i_Newton in range(max_iter): if dt == np.inf: res = func(u, *args, **kargs) else: res = (u - u0) / dt + func(u, *args, **kargs) res_norm = np.linalg.norm(res._value.reshape(res.size), np.inf) if verbose: print(' ', i_Newton, res_norm) if i_Newton == 0: res_norm0 = res_norm if res_norm < max(abs_tol, rel_tol * res_norm0): return adsolution(u, res, i_Newton + 1) if not np.isfinite(res_norm) or res_norm > res_norm0 * 1E6: break # Newton update J = res.diff(u).tocsr() if J.shape[0] > 1: minus_du = splinalg.spsolve(J, np.ravel(res._value), use_umfpack=False) else: minus_du = res._value / J.toarray()[0,0] u._value -= minus_du.reshape(u.shape) u = adarray(u._value) # unlink operation history if any _DEBUG_perturb_new(u) # not converged u = adarray(value(u0).copy()) res = func(u, *args, **kargs) return adsolution(u, res, np.inf)
def solve(func, u0, args=(), kargs={}, max_iter=10, abs_tol=1E-6, rel_tol=1E-6, verbose=True): u = adarray(value(u0).copy()) _DEBUG_perturb_new(u) func = replace__globals__(func) for i_Newton in range(max_iter): res = func(u, *args, **kargs) res_norm = np.linalg.norm(res._value, np.inf) if verbose: print(' ', i_Newton, res_norm) if not np.isfinite(res_norm): break if i_Newton == 0: res_norm0 = res_norm if res_norm < max(abs_tol, rel_tol * res_norm0): return adsolution(u, res, i_Newton + 1) # Newton update J = res.diff(u).tocsr() if J.shape[0] > 1: minus_du = splinalg.spsolve(J, np.ravel(res._value), use_umfpack=False) else: minus_du = res._value / J.toarray()[0,0] u._value -= minus_du.reshape(u.shape) u = adarray(u._value) # unlink operation history if any _DEBUG_perturb_new(u) # not converged return adsolution(u, res, np.inf)
def __init__(self, solution, residual, n_Newton): assert isinstance(solution, adarray) assert isinstance(residual, adarray) residual._current_state = ResidualState(residual._current_state) adarray.__init__(self, solution._value) self._current_state = SolutionState(self, residual._current_state, residual.diff(solution)) self._n_Newton = n_Newton self._res_norm = np.linalg.norm(residual._value.reshape(residual.size)) _DEBUG_perturb_new(self)
def solve(func, u0, args=(), kargs={}, max_iter=10, abs_tol=1E-6, rel_tol=1E-6, verbose=True): u = adarray(base(u0).copy()) _DEBUG_perturb_new(u) for i_Newton in range(max_iter): start = time.time() res = func(u, *args, **kargs) # TODO: how to put into adarray context? res_norm = np.linalg.norm(res._base, np.inf) if verbose: print(' ', i_Newton, res_norm) if not np.isfinite(res_norm): break if i_Newton == 0: res_norm0 = res_norm if res_norm < max(abs_tol, rel_tol * res_norm0): return adsolution(u, res, i_Newton + 1) # Newton update J = res.diff(u).tocsr() start2 = time.time() minus_du = splinalg.spsolve(J, np.ravel(res._base)) print time.time()-start2 # P = splinalg.spilu(J, drop_tol=1e-5) # M_x = lambda x: P.solve(x) # M = splinalg.LinearOperator((n * m, n * m), M_x) # minus_du = splinalg.gmres(J, np.ravel(res._base), M=M,tol=1e-6) u._base -= minus_du.reshape(u.shape) u = adarray(u._base) # unlink operation history if any _DEBUG_perturb_new(u) print time.time()-start # not converged return adsolution(u, res, np.inf)