sor.jl 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. """
  2. x,flag,err,iter,resvec = sor(A::SparseMatrixCSC, b::Vector; omega::Real=2/3, tol::Real=1e-2, maxIter::Int=1,out::Int=0)
  3. Symmetric successive over-relaxation applied to the linear system A*x = b.
  4. !! Note that upper/lower triangular matrix are never build to save memory !!
  5. Input:
  6. A - sparse matrix CSC
  7. b - right hand side vector
  8. omega - relaxation parameter (omega=1.0: Gauss Seidel)
  9. tol - error tolerance (default=1e-2)
  10. maxIter - maximum number of iterations (default=1)
  11. out - flag for output (-1 : no output, 0 : only errors, 1 : final status, 2: error at each iteration)
  12. Output:
  13. x - solution
  14. flag - exit flag ( 0 : desired tolerance achieved,
  15. -1 : maxIter reached without converging)
  16. err - error, i.e., norm(A*x-b)/norm(b)
  17. iter - number of iterations
  18. resvec - error at each iteration
  19. """
  20. function sor(A::SparseMatrixCSC,b::Vector;tol::Real=1e-2,maxIter::Int=1,omega::Real=1.0,out::Int=0,storeInterm::Bool=false;)
  21. n = length(b)
  22. OmInvD = omega./diag(A) #D矩阵 并乘以omega
  23. x = zeros(eltype(A),n)
  24. r = copy(b)
  25. rnorm0 = norm(b)
  26. res = 0
  27. value = 0.0
  28. flag = -1
  29. iter = 0
  30. dx = 0
  31. for iter = 1:maxIter
  32. println(iter)
  33. #第一次迭代 按照列来
  34. for j = 1:n
  35. dx = r[j] * OmInvD[j]
  36. for gidx = A.colptr[j] : A.colptr[j+1]-1
  37. r[A.rowval[gidx]] -= A.nzval[gidx] * dx
  38. end
  39. x[j] += dx
  40. end
  41. res = norm(A*x-b) / rnorm0
  42. println(res)
  43. if res < tol
  44. flag = 0; break
  45. end
  46. end
  47. return x,flag,iter
  48. end