0

I'm trying to do a plot of convergence of this GMRES alghorithm. I managed to create a class that make me print the residual at each iteration but I can't find a way to extract this data into an array so that i can plot it with matplotlib.

Here is my code:

 matrixSize = 25
 A = Atridiag(2, -1, -1, matrixSize)
 A = scipy.sparse.csc_matrix (A)
 b = np.matrix(np.ones((matrixSize, 1)))
 x1 = np.matrix(np.ones((matrixSize, 1)))

 M_i=scipy.sparse.linalg.spilu(A)
 M2=scipy.sparse.linalg.LinearOperator((matrixSize,matrixSize),M_i.solve)

 nmax_iter = 1
 rstart = 1
 tol = 1e-12
 e = np.zeros((nmax_iter + 1, 1))
 rr = 1

 class gmres_counter(object):
    def __init__(self, disp=True):
       self._disp = disp
       self.niter = 0
       self.callbacks = []
    def __call__(self, rk=None):
       self.callbacks.append(str(rk))
       self.niter += 1
       if self._disp:
       print('%s' %(str(rk)))

 counter = gmres_counter()
 x, info = scipy.sparse.linalg.gmres(A, b, x0=x1, tol=tol, restart=rstart, 
                                     M=M2, callback=counter)
Giovanni Frison
  • 628
  • 3
  • 19
  • On a first glance, it looks to me like you already found a way to collect the data. Appending the residuals from each iteration to a persistent container is exactly what I would do. So, what exactly is not working? – MB-F Feb 14 '17 at 15:21
  • I can't find a way to use the callbacks list. How can I plot what should be inside callbacks array? thank you very much for your help – Giovanni Frison Feb 14 '17 at 15:50
  • There are many possible reasons why you cannot find a way. I'm sure you tried something. When you did, what did you expect and what did actually happen or what was the error message? If you can provide this information, I'm sure somebody will be able to help you. For a start, you convert `rk` to a string before appending to `callbacks`. Most likely this is one problem, but there may be more... It is too difficult to help when you do not tell us what you were attempting to do with the callbacks list later on. – MB-F Feb 14 '17 at 16:09
  • Sorry, it is the first time Im posting here. Probably I lack some understanding of classes...The code is running without any warning or error. My aim is to have an array or a list (callbacks here) that contains the residual rk at each iteration performed. After that I want to plot the residual rk ,collected in callbacks, with matplotlib, in ordr to have a graphical understanding of the performance of the GMRES algorithm. Since I'm using Spider IDLE would like to see the callbacks list in the variable explorer, buit there isn't. – Giovanni Frison Feb 14 '17 at 16:17
  • 1
    Ok, so after running your code you should see `counter` in the variable explorer. You can access class members with `.`, i.e. type `counter.callbacks` in the terminal or assign it to a variable with `callbacks = counter.callbacks`. As to how to do the plotting you will have to do some research. Googling for "matplotlib" should get you started. – MB-F Feb 15 '17 at 06:57
  • Thank you very much, this is really what i needed! Have you any suggestion here?? http://stackoverflow.com/questions/42233966/general-minimum-residual-gmres-with-ilu-preconditioner – Giovanni Frison Feb 15 '17 at 07:22

0 Answers0