def ne_single_gaussian(r, z, *p): n1, a1, b1, rm = p br, bz = rt1.bvec(r, z, separatrix) bb = np.sqrt(br**2 + bz**2) if r == 0.0: return n1 * np.exp(-a1 * abs((rt1.psi(r, 0.0, separatrix)-psix)/psi0)**2) if rt1.check_coilcase(r, z): return 0.0 else: return n1 * np.exp(-a1*abs((rt1.psi(r, z, separatrix) - psix)/psi0)**2) * (bb/rt1.b0(r, z, separatrix))**(-b1)
def psi_term(r, z, *p): n1, a1, b1 = p if rt1.check_coilcase(r, z): return 0.0 else: return np.exp(-a1 * abs((rt1.psi(r, z, separatrix) - psix) / psi0)**2)
def err_single_gaussian(p, disp_flg): n1, a1, b1, rm = p psix = rt1.psi(rm, 0.0, separatrix) # line integral along horizontal y=45 chord nl_y450 = 0.0 for i, x in enumerate(x450): nl_y450 = nl_y450 + np.exp(-a1*abs((psi_x450[i] - psix)/psi0)**2)*n1*dx450 nl_y450 = 2.0*nl_y450 error_y450 = (nl_y450 - nl_y450_mes)**2/(nl_y450_mes)**2 # line integral along vertical y=60, 70 chord nl_z620 = 0.0 for j, z in enumerate(z620): nl_z620 = nl_z620 + n1*np.exp(-a1*abs((psi_z620[j] - psix)/psi0)**2)*(bb620[j]/b0620[j])**(-b1)*dz620 error_z620 = (nl_z620 - nl_z620_mes)**2/(nl_z620_mes)**2 nl_z700 = 0.0 for j, z in enumerate(z700): nl_z700 = nl_z700 + n1*np.exp(-a1*abs((psi_z700[j] - psix)/psi0)**2)*(bb700[j]/b0700[j])**(-b1)*dz700 error_z700 = (nl_z700 - nl_z700_mes)**2/(nl_z700_mes)**2 error = [error_y450, error_z620, error_z700] # print ( 'n1, a1, b1 =' , p) # print ( 'y400: ', nl_y400, '/', nl_y400_mes) # print ( 'y450: ', nl_y450, '/', nl_y450_mes) # print ( 'y500: ', nl_y500, '/', nl_y500_mes) # print ( 'y550: ', nl_y550, '/', nl_y550_mes) # print ( 'y620: ', nl_y620, '/', nl_y620_mes) # print ( 'z620: ', nl_z620, '/', nl_z620_mes) # print ( 'z700: ', nl_z700, '/', nl_z700_mes) # print ( 'z840: ', nl_z840, '/', nl_z840_mes) # print (' err = ', sum(error[4:7])) return sum(error[0:3])
def view_profile(rm, p_opt): psix = rt1.psi(rm, 0.0, separatrix) # psi上のBが最小となる直線上の密度最大値 n1, a1, b1 = p_opt nl_y450 = 0.0 nl_z620 = 0.0 nl_z700 = 0.0 nl_y450 = 0.0 for i, x in enumerate(x450): nl_y450 = nl_y450 + np.exp(-a1 * abs( (psi_x450[i] - psix) / psi0)**2) * n1 * dx450 nl_y450 = 2.0 * nl_y450 error_y450 = (nl_y450 - nl_y450_mes)**2 / (nl_y450_mes)**2 # line integral along vertical y=60, 70 chord nl_z620 = 0.0 for j, z in enumerate(z620): nl_z620 = nl_z620 + n1 * np.exp(-a1 * abs( (psi_z620[j] - psix) / psi0)**2) * (bb620[j] / b0620[j])**(-b1) * dz620 error_z620 = (nl_z620 - nl_z620_mes)**2 / (nl_z620_mes)**2 nl_z700 = 0.0 for j, z in enumerate(z700): nl_z700 = nl_z700 + n1 * np.exp(-a1 * abs( (psi_z700[j] - psix) / psi0)**2) * (bb700[j] / b0700[j])**(-b1) * dz700 error_z700 = (nl_z700 - nl_z700_mes)**2 / (nl_z700_mes)**2 print('y450: ', nl_y450, '/', nl_y450_mes) print('z620: ', nl_z620, '/', nl_z620_mes) print('z700: ', nl_z700, '/', nl_z700_mes) print('error_y450: ', error_y450) print('error_z620: ', error_z620) print('error_z700: ', error_z700) # Export figure rs = np.linspace(0.1, 1.001, 200) zs = np.linspace(-0.4, 0.401, 200) r_mesh, z_mesh = np.meshgrid(rs, zs) ne_profile = np.array([ list( map(lambda r, z: ne_single_gaussian(r, z, *p_opt_best), r_mesh.ravel(), z_mesh.ravel())) ]).ravel().reshape(len(rs), len(zs)) psi_term_profile = np.array([ list( map(lambda r, z: psi_term(r, z, *p_opt_best), r_mesh.ravel(), z_mesh.ravel())) ]).ravel().reshape(len(rs), len(zs)) B_term_profile = np.array([ list( map(lambda r, z: B_term(r, z, *p_opt_best), r_mesh.ravel(), z_mesh.ravel())) ]).ravel().reshape(len(rs), len(zs)) psi = np.array([ list(map(lambda r, z: rt1.psi(r, z), r_mesh.ravel(), z_mesh.ravel())) ]).ravel().reshape(len(rs), len(zs)) coilcase_truth_table = np.array([ list( map(lambda r, z: rt1.check_coilcase(r, z), r_mesh.ravel(), z_mesh.ravel())) ]).ravel().reshape(len(rs), len(zs)) psi[coilcase_truth_table == True] = 0 np.save('ne2D_35_t20_r1', ne_profile) # density profileの表示 levels = [ 0.005, 0.006, 0.007, 0.008, 0.009, 0.01, 0.011, 0.012, 0.013, 0.014 ] plt.figure(figsize=(8, 5)) plt.subplot(111) img = plt.imshow(ne_profile, origin='lower', cmap='jet', extent=(rs.min(), rs.max(), zs.min(), zs.max())) plt.contour(r_mesh, z_mesh, ne_profile, colors=['k']) plt.contour(r_mesh, z_mesh, psi, colors=['white'], levels=levels) plt.title(r'$n_\mathrm{e}$') plt.xlabel(r'$r\mathrm{\ [m]}$') plt.ylabel(r'$z\mathrm{\ [m]}$') # plt.gca():現在のAxesオブジェクトを返す divider = make_axes_locatable(plt.gca()) # カラーバーの位置 cax = divider.append_axes("right", "5%", pad="3%") cb = plt.colorbar(img, cax=cax) #cb.set_clim(0,6.4) cb.set_label(r'$\mathrm{[10^{16}\,m^{-3}]}$') plt.tight_layout(pad=1.0, w_pad=2.0, h_pad=1.0) plt.show() # psi term profileの表示 plt.figure(figsize=(8, 5)) plt.subplot(111) img = plt.imshow(psi_term_profile, origin='lower', cmap='plasma', extent=(rs.min(), rs.max(), zs.min(), zs.max())) plt.contour(r_mesh, z_mesh, psi_term_profile, colors=['k']) plt.contour(r_mesh, z_mesh, psi, colors=['white'], levels=levels) plt.title(r'$\psi term$') plt.xlabel(r'$r\mathrm{\ [m]}$') plt.ylabel(r'$z\mathrm{\ [m]}$') # plt.gca():現在のAxesオブジェクトを返す divider = make_axes_locatable(plt.gca()) # カラーバーの位置 cax = divider.append_axes("right", "5%", pad="3%") cb = plt.colorbar(img, cax=cax) cb.set_clim(0, 6.4) cb.set_label(r'$\mathrm{[10^{16}\,m^{-3}]}$') plt.tight_layout(pad=1.0, w_pad=2.0, h_pad=1.0) plt.show() # B term profileの表示 plt.figure(figsize=(8, 5)) plt.subplot(111) img = plt.imshow(B_term_profile, origin='lower', cmap='plasma', extent=(rs.min(), rs.max(), zs.min(), zs.max())) plt.contour(r_mesh, z_mesh, B_term_profile, colors=['k']) plt.contour(r_mesh, z_mesh, psi, colors=['white'], levels=levels) plt.title(r'$B term$') plt.xlabel(r'$r\mathrm{\ [m]}$') plt.ylabel(r'$z\mathrm{\ [m]}$') # plt.gca():現在のAxesオブジェクトを返す divider = make_axes_locatable(plt.gca()) # カラーバーの位置 cax = divider.append_axes("right", "5%", pad="3%") cb = plt.colorbar(img, cax=cax) cb.set_clim(0, 6.4) cb.set_label(r'$\mathrm{[10^{16}\,m^{-3}]}$') plt.tight_layout(pad=1.0, w_pad=2.0, h_pad=1.0) plt.show() # profile_zでのdensity profileの表示 profile_z = 0.0 # プロファイルが見たい任意のz [m] profile_z_index = np.searchsorted(zs, profile_z) ne_profile_z0 = ne_profile[:][profile_z_index] fig, ax = plt.subplots(1) ax.plot(rs, ne_profile_z0) plt.draw() plt.show() #error at rms fig, ax = plt.subplots(1) ax.plot(rms, error_at_rms[:, 0]) ax.plot(rms, error_at_rms[:, 1]) plt.yscale('log') plt.xlabel('r [m]') plt.ylabel('Error at rm') plt.show()
if view_mode is True: print('Running with view mode') rm_best = 0.5424 p_opt_best = [33.984, 8.652, 0.431] t_start = time.time() print('') print(time.ctime()) print('') dx450 = x450[1] - x450[0] dz620 = z620[1] - z620[0] dz700 = z700[1] - z700[0] # calculate psi, bb, b0 along the line of sights beforehand psi0 = rt1.psi(1.0, 0.0, separatrix) # psi at the vacuum chamber psi_x450 = np.zeros(len(x450)) psi_z620 = np.zeros(len(z620)) psi_z700 = np.zeros(len(z700)) bb620 = np.zeros(len(z620)) bb700 = np.zeros(len(z700)) b0620 = np.zeros(len(z620)) b0700 = np.zeros(len(z700)) for i, x in enumerate(x450): rx = np.sqrt(x**2 + r450_para**2) psi_x450[i] = rt1.psi(rx, 0.0, separatrix) for j, z in enumerate(z620): psi_z620[j] = rt1.psi(r620_perp, z, separatrix) br, bz = rt1.bvec(r620_perp, z, separatrix)
#p_opt_best = [35.389, 6.629, 1.800, 0.549] #p_opt_best = [29.241, 4.295, 1.698, 0.550] p_opt_best = [28.227, 4.897, 1.903, 0.542] t_start = time.time() print('') print(time.ctime()) print('') dx450 = x450[1] - x450[0] dz620 = z620[1] - z620[0] dz700 = z700[1] - z700[0] # calculate psi, bb, b0 along the line of sights beforehand psi0 = rt1.psi(1.0, 0.0, separatrix) # psi at the vacuum chamber psi_x450 = np.zeros(len(x450)) psi_z620 = np.zeros(len(z620)) psi_z700 = np.zeros(len(z700)) bb620 = np.zeros(len(z620)) bb700 = np.zeros(len(z700)) b0620 = np.zeros(len(z620)) b0700 = np.zeros(len(z700)) for i, x in enumerate(x450): rx = np.sqrt(x**2 + r450_para**2) psi_x450[i] = rt1.psi(rx, 0.0, separatrix) for j, z in enumerate(z620): psi_z620[j] = rt1.psi(r620_perp, z, separatrix) br, bz = rt1.bvec(r620_perp, z, separatrix)