ADDA > 線形方程式を解き直してみた (21のrefractive index実部の一次補間) > iterationは37回から1回に

```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)
scipy v0.19.1
geopandas v0.3.0
```

https://qiita.com/7of9/items/99562018229a941f83fd
の続き。

### 概要

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

TensorFlowを使わずに、単純な補間処理ではどうか試した。

### 補間

refractive indexの実部に対して補間する。

データ準備のコードは以下に掲載している。
[ADDA + Matplotlib > InitField-Y for various values of Re{m} > v0.1, v0.2 (with 1d interpolation)](https://qiita.com/7of9/items/80db74708b4908745f09)

### 補間コード

関連: [link > Python で 1 変量データ補間 > SciPy > interpolate.*](https://qiita.com/7of9/items/dbd21f4190fc3c0ee3e6)

```interp_rem_171104.py
import numpy as np
import subprocess as sb
import sys
import glob
from scipy import signal, interpolate

'''
v0.1 Nov. 04, 2017
  - output interpolated values
  - add interpolate_rem()
  - read all the files
  - add read_INTF()
  - add get_real_m()
'''

# on Python 3.5.2

# codingrule:PEP8


def get_real_m(inpfile):
    IDX_REM = 2  # 'refactive index: 1.05+1e-05'
    with open(inpfile) as logfd:
        lines = logfd.readlines()
        for aline in lines:
            if aline.find('refractive') == -1:
                continue
            # print(aline)
            elem = aline.replace('+', ' ').split(' ')
            return elem[IDX_REM]
        return 0.0


def read_INTF(inpfile):
    dat = np.genfromtxt(inpfile, delimiter=' ',
                        skip_header=1)
    ary = []
    for elem in dat:
        ary += [elem]
    return ary


def interpolate_rem(rems, dats, irow, icol, arem):
    xns = np.array(rems, dtype=float)
    ary = []
    for imr in range(lend1):
        adat = dats[imr][irow][icol]
        ary += [adat]

    itpfnc = interpolate.interp1d(xns, ary, kind="cubic")
    res = itpfnc([arem])
    return res

listINTF = glob.glob("../CALC_171028_HW0001/*/IntField-Y")
listLOG = glob.glob("../CALC_171028_HW0001/*/log")

# 1. read files
rems = []
dats = []
for idx in range(len(listINTF)):
    arem = get_real_m(listLOG[idx])
    adat = read_INTF(listINTF[idx])
    rems += [arem]
    dats += [adat]

lend1 = len(dats)  # mr (21)
lend2 = len(dats[0])  # rows (25928)
lend3 = len(dats[0][0])  # columns (10)

# 2. interpolation
print("x y z |E|^2 Ex.r Ex.i Ey.r Ey.i Ez.r Ez.i")  # file header
arem = 1.45
for irow in range(lend2):
    strs = ""
    for icol in range(lend3):
        res = interpolate_rem(rems, dats, irow, icol, arem)
        strs += "%s " % str(*res)
    print(strs)

```

```txt:run
$ python3 interp_rem_171104.py > res_interp_rem_1p45_171104
```

### 補間結果を用いた再計算

```bash
$ ./adda -m 1.45 0.00001 -grid 52 -shape chebyshev 0.7 12 -init_field read CODE_171028/res_interp_rem_1p45_171104 
all data is saved in 'run6112_chebyshev_g52_m1.45'
box dimensions: 52x52x52
lambda: 6.283185307   Dipoles/lambda: 14.5
Required relative residual norm: 1e-05
Total number of occupied dipoles: 25928
Memory usage for MatVec matrices: 40.6 MB
Calculating Green's function (Dmatrix)
Fourier transform of Dmatrix......
Initializing FFTW3
Total memory usage: 50.9 MB

here we go, calc Y

CoupleConstant:0.005319005376+1.896539108e-05i
x_0 = from file CODE_171028/res_interp_rem_1p45_171104
RE_000 = 8.2451071981E-06
Cext	= 853.9841055
Qext	= 4.294268977
Cabs	= 0.05739387475
Qabs	= 0.0002886057647
```

RE_XXXという行が一行だけであり、iterationは1回で終了した。
(初期値解を与えない場合はiterationは37回)。

補間の元データに含まれない実部の値1.43で試すと、iterationは4回になった。
(初期値解を与えない場合はiterationは37回)。