ADDA + Matplotlib > scatterplot of [the number of iterations] for various values of Re{m} and Im{m}, for different iterative solvers (e.g., bicgstab2, bicg, QMR, etc)

```txt:動作環境
GeForce GTX 1070 (8GB)
ASRock Z170M Pro4S [Intel Z170chipset]
Ubuntu 16.04 LTS desktop amd64
TensorFlow v1.2.1
cuDNN v5.1 for Linux
CUDA v8.0
Python 3.5.2
IPython 6.0.0 -- An enhanced Interactive Python.
gcc (Ubuntu 5.4.0-6ubuntu1~16.04.4) 5.4.0 20160609
GNU bash, version 4.3.48(1)-release (x86_64-pc-linux-gnu)
ADDA v.1.3b6
```

### Background

This article is related to [ADDA (light scattering simulator based on the discrete dipole approximation)](https://github.com/adda-team/adda/).

I have found that the number of iterations for a specific combination of Re{m} and Im{m} become large when using QMR (default iterative solver for ADDA), compared with those for different sets of Re{m} and Im{m}.

The numbers of iterations are shown below.

### ADDA execution code 

Python3 script.

```py:group_run_adda_170819_170916.py
import subprocess as sb
import numpy as np
import sys

# on Python 3.5.2

# codingrule: PEP8

RUN_PARAM = "-store_int_field -grid 26"
# RUN_PARAM = "-store_int_field"  # for test

# real part of refractive index (linear scale)
R_RANGE = 0.05  # linear
R_NUM = 21
R_CENTER = 1.45
mrs = np.linspace(R_CENTER - R_RANGE, R_CENTER + R_RANGE, R_NUM)
# imaginary part of refractive index (logarithmic scale)
I_RANGE = 1  # linear
I_NUM = 21
I_CENTER = -4
mis = 10**np.linspace(I_CENTER - I_RANGE, I_CENTER + I_RANGE, I_NUM)

# debug
#print(mrs)
#print(mis)
#sys.exit()

for amr in mrs:
    for ami in mis:
        print(amr, ami)
        # cmd = "./adda -m %f %f %s" % (amr, ami, RUN_PARAM)
        #cmd = "./adda -iter bcgs2 -m %f %f %s" % (amr, ami, RUN_PARAM)
        #cmd = "./adda -iter bicgstab -m %f %f %s" % (amr, ami, RUN_PARAM)
        #cmd = "./adda -iter cgnr -m %f %f %s" % (amr, ami, RUN_PARAM)
        #cmd = "./adda -iter bicg -m %f %f %s" % (amr, ami, RUN_PARAM)
        #cmd = "./adda -iter csym -m %f %f %s" % (amr, ami, RUN_PARAM)
        #cmd = "./adda -iter qmr -m %f %f %s" % (amr, ami, RUN_PARAM)
        cmd = "./adda -iter qmr2 -m %f %f %s" % (amr, ami, RUN_PARAM)
        print(cmd)
        sb.run(cmd.split(), stdout=sb.DEVNULL)
```

By executing the above, you will obtain the runXXX/ directories.

### Number of iterations table creation

I have used the v0.2 of the bash script at [ADDA > bash script > read_numIter_170916_exec > v0.1, v0.2 > to extract the number of iterations from log files](http://qiita.com/7of9/items/8d7d76f5ced659e795cd).


```
bash read_numIter_170916_exec > ReIm_170916_qmr2.tbl
```


### Scatterplot display 

Matplotlib code

v0.3 modified from [ADDA + Matplotlib > 2dplot_ReIm_170916.ipynb > v0.1, v0.2 > show scatterplot of [the number of iterations] for various values of Re{m} and Im{m}](http://qiita.com/7of9/items/e00d8e911ca3ce266c12)

```py:2dplot_ReIm_170916.ipynb
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.cm as cm
import matplotlib as mpl
import sys
from pylab import rcParams

"""
v0.3 WIP
   - use [ReIm_170916_bicgstab.tbl] and results using different iterative solvers
v0.2 Sep. 16, 2017
   - use different INPFILE
v0.1 Sep. 16, 2017
   - show scatterplot of [number of iterations] of ADDA for various Re{m} and Im{m}
"""

# coding rule: PEP8

%matplotlib inline


rcParams['figure.figsize'] = 5, 5  # adjusted 
rcParams['figure.dpi'] = 150  # adjusted

# obtained using [read_numIter_170916_exec]
#INPFILE = "ReIm_170916.tbl"   # ../CALC_170819
#INPFILE = "ReIm_run170916.Tbl"  # ../CALC_170916

#comparison
INPFILE = "ReIm_170916_bicgstab.tbl"  # code from 170819 but for different iterative solver
INPFILE = "ReIm_170916_bicgstab2.tbl"  # same as above
INPFILE = "ReIm_170916_cgnr.tbl"  # same as above
INPFILE = "ReIm_170916_bicg.tbl"  # same as above
INPFILE = "ReIm_170916_csym.tbl"  # same as above
INPFILE = "ReIm_170916_qmr.tbl"  # same as above
INPFILE = "ReIm_170916_qmr2.tbl"  # same as above

# 1. get data
dat = np.genfromtxt(INPFILE, delimiter=',')

xs, ys, nums = [], [], []
for elem in dat:
    ax, ay, anum = elem
    xs += [ax]
    ys += [ay]
    nums += [anum]

color = np.log(nums)  # taking log to enhance the contrast 
#color = nums

# 2. draw data
size = 60  # arbitrary
#plt.scatter(xs, ys, size, color, cmap=cm.jet, vmax=5)
plt.scatter(xs, ys, size, color, cmap=cm.jet)
#plt.scatter(xs, ys, size, color, cmap=cm.jet, norm=mpl.colors.LogNorm(), vmax=50)  # used with [color = nums]
plt.xlabel('Re{m}')
plt.ylabel('Im{m}')
plt.ylim(0.0, 0.001)
cbar = plt.colorbar()
cbar.ax.set_ylabel('log(RE_XXX)')

```

## Result

The number of iterations for different iterative solvers are shown below.

### Bi-CGStab(2)

![qiita.png](https://qiita-image-store.s3.amazonaws.com/0/32870/c68c1326-b11e-e9f8-b70d-5f8102b46875.png)

### Bi-CG

![qiita.png](https://qiita-image-store.s3.amazonaws.com/0/32870/0af0f24e-d187-2efb-55d5-40421f387ed0.png)

### Bi-CGStab

![qiita.png](https://qiita-image-store.s3.amazonaws.com/0/32870/bf626d65-7523-6e82-483a-479e13aa348f.png)

### CGNR

![qiita.png](https://qiita-image-store.s3.amazonaws.com/0/32870/c5adb905-a3a5-b40c-48b3-83592bd3ea58.png)

### CSYM

![qiita.png](https://qiita-image-store.s3.amazonaws.com/0/32870/32727115-c869-1767-0f88-1536e6ea0ba1.png)

### QMR

![qiita.png](https://qiita-image-store.s3.amazonaws.com/0/32870/d15da76c-56b0-f1ce-f79e-42579e1f4214.png)

### QMR2

![qiita.png](https://qiita-image-store.s3.amazonaws.com/0/32870/7fa01bab-cd7b-e51a-f434-47edcb6872c4.png)


## Breakdown?

@ [manual.pdf](https://github.com/adda-team/adda/blob/master/doc/manual.pdf) p39

> All iterative solvers except CGNR and CSYM are susceptible to breakdown. ... For instance, simulation of light scattering by cube with edge length that is exactply multiple of the wavelength results in breakdowns of QMR, QMR2, and Bi-CG. ...