def fd_d1_o4_uneven(var, grid, mat=False, return_new_grid=False): """Centered finite difference, first derivative, 4th order. Evenly spaced grid is created and var is interpolated onto this grid. Derivative is interpolated back onto original grid. var: quantity to be differentiated. grid: grid for var mat: matrix for the finite-differencing operator. if mat=False then it is created""" N = 2.0 * len(grid) grid0 = np.linspace(grid[0], grid[-1], N) var0 = interp(grid, var, grid0) if not mat: mat = get_mat_fd_d1_o4(len(var0), grid0[1] - grid0[0]) dvar0 = -np.dot(mat, var0) dvar0[0] = 0.0 dvar0[1] = 0.0 dvar0[-1] = 0.0 dvar0[-2] = 0.0 if return_new_grid: return grid0, -dvar0 else: dvar = np.zeros(len(grid)) dvar[2:-2] = interp(grid0[2:-2], dvar0[2:-2], grid[2:-2]) return -dvar
def totalE(EFITdict, ITERDBdict, OUT_file_name, ntheta): psipn_vec = EFITdict['psipn'] rhotn_vec = EFITdict['rhotn'] ne = interp(ITERDBdict['rhot_ne'], ITERDBdict['ne'], rhotn_vec) ni = interp(ITERDBdict['rhot_ni'], ITERDBdict['ni'], rhotn_vec) nz = interp(ITERDBdict['rhot_nz'], ITERDBdict['nz'], rhotn_vec) te = interp(ITERDBdict['rhot_te'], ITERDBdict['te'], rhotn_vec) ti = interp(ITERDBdict['rhot_ti'], ITERDBdict['ti'], rhotn_vec) f = open(OUT_file_name, 'w') sys.stdout = f dV = [] de = [] for i in range(len(psipn_vec) - 2): #print(i) R_in, Z_in, B_pol_in, B_tor_in, B_tot_in = BfieldsFS( EFITdict, psipn_vec[i], ntheta) R_out, Z_out, B_pol_out, B_tor_out, B_tot_out = BfieldsFS( EFITdict, psipn_vec[i + 1], ntheta) this_dV = dVolume(R_in, Z_in, R_out, Z_out, False) this_de = this_dV * ((ni[i] + nz[i]) * ti[i] + ne[i] * te[i]) * 1.6E-19 dV.append(this_dV) de.append(this_de) print('{:.8f} {:.8f} {:.8f}'.format(psipn_vec[i], this_dV, this_de)) return sum(dV), sum(de)
def magneticShear(EFITdict, show_plots=False): rhotn = EFITdict['rhotn'] q = EFITdict['qpsi'] #uni_rhot = np.linspace(rhotn[0], rhotn[-1], len(rhotn) * 10) uni_rhot = np.linspace(rhotn[0], rhotn[-1], len(rhotn)) q_unirhot = interp(rhotn, q, uni_rhot) shat_unirhot = uni_rhot / q_unirhot * first_derivative(q_unirhot, uni_rhot) shat = interp(uni_rhot, shat_unirhot, rhotn) R_unirhot = interp(rhotn, EFITdict['R'], uni_rhot) Ls_unirhot = q_unirhot * R_unirhot / shat_unirhot Ls = interp(uni_rhot, Ls_unirhot, rhotn) if show_plots: plt.plot(uni_rhot, shat_unirhot) plt.ylabel('shat') plt.xlabel('rhot') plt.axis([0.8, 1., 0., 10.]) plt.show() plt.plot(uni_rhot, Ls_unirhot) plt.ylabel('Ls') plt.xlabel('rhot') plt.axis([0.8, 1., 0., 2.]) plt.show() return uni_rhot, shat_unirhot, Ls_unirhot
def dPressuredR(ITERDBdict, EFITdict, rhotLeft, rhotRight): rhot0 = EFITdict['rhotn'] R0 = EFITdict['R'] Pres = EFITdict['Pres'] print(len(rhot0), len(R0), len(Pres)) leftInd = np.argmin(abs(rhot0 - rhotLeft)) rightInd = np.argmin(abs(rhot0 - rhotRight)) rhotProf = rhot0[leftInd: rightInd + 1] prof = Pres[leftInd: rightInd + 1] R = R0[leftInd: rightInd + 1] uniR = np.linspace(R[0], R[-1], len(R)) rhot_uniR = interp(R, rhotProf, uniR) prof_uniR = interp(R, prof, uniR) if 1 == 0: plt.plot(R0, rhot0, '+-', label = 'rhot0') plt.plot(uniR, rhot_uniR, '+-', label = 'rhot_uniR') plt.xlabel('R') plt.ylabel('rhot') plt.legend() plt.show() plt.plot(R0, Pres, '+-', label = 'Pres') plt.plot(uniR, prof_uniR, '+-', label = 'prof_uniR') plt.xlabel('R') plt.ylabel('pressure') plt.legend() plt.show() profPrime_uniR = - first_derivative(prof_uniR, uniR) / prof_uniR return rhot_uniR, uniR, prof_uniR, profPrime_uniR
def fd_d1_o4_uneven(var,grid,mat=False,return_new_grid = False): """Centered finite difference, first derivative, 4th order. Evenly spaced grid is created and var is interpolated onto this grid. Derivative is interpolated back onto original grid. var: quantity to be differentiated. grid: grid for var mat: matrix for the finite-differencing operator. if mat=False then it is created""" N = 2.0*len(grid) grid0 = np.linspace(grid[0],grid[-1],N) var0 = interp(grid,var,grid0) if not mat: mat=get_mat_fd_d1_o4(len(var0),grid0[1]-grid0[0]) dvar0=-np.dot(mat,var0) dvar0[0]=0.0 dvar0[1]=0.0 dvar0[-1]=0.0 dvar0[-2]=0.0 if return_new_grid: return grid0,-dvar0 else: dvar = np.zeros(len(grid)) dvar[2:-2] = interp(grid0[2:-2],dvar0[2:-2],grid[2:-2]) return -dvar
def remove_edge_opt_complex(array, edge_opt): nz = len(array) dz = 2.0 * np.pi / nz zgrid_even = np.arange(nz) / float(nz - 1) * (2.0 * np.pi - dz) - np.pi N = np.arcsinh(edge_opt * zgrid_even[-1]) / zgrid_even[-1] zprime_even = zgrid_even z_of_zprime_even = 1.0 / edge_opt * np.sinh(N * zprime_even) Rarray = interp(z_of_zprime_even, np.real(array), zgrid_even) Iarray = interp(z_of_zprime_even, np.imag(array), zgrid_even) array_out = Rarray + 1.0J * Iarray return array_out
def remove_edge_opt_complex(array,edge_opt): nz = len(array) dz = 2.0*np.pi/nz zgrid_even = np.arange(nz)/float(nz-1)*(2.0*np.pi-dz)-np.pi N = np.arcsinh(edge_opt*zgrid_even[-1])/zgrid_even[-1] zprime_even = zgrid_even z_of_zprime_even = 1.0/edge_opt*np.sinh(N*zprime_even) Rarray = interp(z_of_zprime_even,np.real(array),zgrid_even) Iarray = interp(z_of_zprime_even,np.imag(array),zgrid_even) array_out = Rarray + 1.0J*Iarray return array_out
def calc_gammaE_gene(R, rhot, te, B_pol, Er, q, a): # assume ti, B_tor, B_pol, Er are on uniform grid of R M = 3.3 * 10**(-27) uni_rhot = np.linspace(rhot[0], rhot[-1], 1000) te_unirhot = interp(rhot, te, uni_rhot) Bp_unirhot = interp(rhot, B_pol, uni_rhot) Er_unirhot = interp(rhot, Er, uni_rhot) R_unirhot = interp(rhot, R, uni_rhot) q_unirhot = interp(rhot, q, uni_rhot) omega_tor = Er_unirhot / (R_unirhot * Bp_unirhot) cs = np.sqrt(te_unirhot / M) gammaE = (uni_rhot / q_unirhot) * fd_d1_o4(omega_tor, uni_rhot) / (cs / a) return uni_rhot, gammaE, omega_tor
def calc_gammaE_gene(R,rhot,te,B_pol,Er,q,a): # assume ti, B_tor, B_pol, Er are on uniform grid of R M = 3.3*10**(-27) uni_rhot = np.linspace(rhot[0],rhot[-1],1000) te_unirhot = interp(rhot,te,uni_rhot) Bp_unirhot = interp(rhot,B_pol,uni_rhot) Er_unirhot = interp(rhot,Er,uni_rhot) R_unirhot = interp(rhot,R,uni_rhot) q_unirhot = interp(rhot,q,uni_rhot) omega_tor = Er_unirhot/(R_unirhot*Bp_unirhot) cs = np.sqrt(te_unirhot/M) gammaE = (uni_rhot/q_unirhot)*fd_d1_o4(omega_tor,uni_rhot)/(cs/a) return uni_rhot, gammaE, omega_tor
def get_geom_pars(efit_file_name, rhot0): psip_n, Rgrid, Zgrid, F, p, ffprime, pprime, psirz, qpsi, rmag, zmag, nw, psiax, psisep = read_EFIT_file( efit_file_name) R_major = rmag dummy, rhot_n, phi_edge = calc_rho_tor(psip_n, psiax, psisep, qpsi, nw, psip_n_max=0.999) psip_n_obmp, R_obmp, B_pol, B_tor = calc_B_fields(Rgrid, rmag, Zgrid, zmag, psirz, psiax, psisep, F, nw, psip_n) Bref = abs(B_tor[0]) Lref = np.sqrt(2.0 * abs(phi_edge / Bref)) irhot_n = np.argmin(abs(rhot_n - rhot0)) q0 = qpsi[irhot_n] rhot_new = np.linspace(rhot_n[0], rhot_n[-1], 4 * len(rhot_n)) qpsi_new = interp(rhot_n, qpsi, rhot_new) shat = rhot_new / qpsi_new * fd_d1_o4(qpsi_new, rhot_new) irhot_new = np.argmin(abs(rhot_new - rhot0)) shat0 = shat[irhot_new] return Lref, Bref, R_major, q0, shat0
def calc_shat(q, rhot): rhot0 = np.arange(10000.0) / 9999.0 q0 = interp(rhot, q, rhot0) qprime = fd_d1_o4(q0, rhot0) shat = rhot0 / q0 * qprime return rhot0, q0, shat
def calc_B_fields(Rgrid, rmag, Zgrid, zmag, psirz, psiax, psisep, F, nw, psip_n): Z0_ind = np.argmin(abs(Zgrid-zmag)) psi_pol_mp = psirz[Z0_ind,:].copy() Rmag_ind = np.argmin(abs(Rgrid - rmag)) psi_pol_ob = psi_pol_mp[Rmag_ind:].copy() Sep_ind = np.argmin(abs(psi_pol_ob-psisep)) psi_pol_obmp = psi_pol_ob[:Sep_ind+3].copy() R_obmp = Rgrid[Rmag_ind:Rmag_ind+Sep_ind+3].copy() #plt.plot(Rgrid,psi_pol_mp,label='mp') #print len(R_obmp),len(psi_pol_obmp) #plt.plot(R_obmp,psi_pol_obmp,label='obmp') #plt.legend(loc=2) #plt.show() # B_pol is d psi_pol/ d R * (1/R) B_pol = fd_d1_o4(psi_pol_obmp,R_obmp)/R_obmp # convert F(on even psi_pol grid) to F(on even R grid) print psiax print psisep psip_n_obmp = (psi_pol_obmp-psiax)/(psisep-psiax) #plt.plot(R_obmp,psip_n_obmp,label='psip_n_obmp') #plt.legend() #plt.show() F_obmp = interp(psip_n, F, psip_n_obmp) # B_tor = F/R B_tor = F_obmp/R_obmp # psip_n_obmp is normalized psi_pol at outboard midplane on uniform unif_R # B_tor and B_pol are on uniform unif_R as well # psip_n_obmp is unlike psip_n ([0,1]), it goes from 0 to 1.06 here return psip_n_obmp, R_obmp, B_pol, B_tor
def totalN(EFITdict, ITERDBdict, OUT_file_name, ntheta): psipn_vec = EFITdict['psipn'] rhotn_vec = EFITdict['rhotn'] nz = interp(ITERDBdict['rhot_nz'], ITERDBdict['nz'], rhotn_vec) f = open(OUT_file_name, 'w') sys.stdout = f dV = [] dn = [] for i in range(len(psipn_vec) - 2): #print(i) R_in, Z_in, B_pol_in, B_tor_in, B_tot_in = BfieldsFS( EFITdict, psipn_vec[i], ntheta) R_out, Z_out, B_pol_out, B_tor_out, B_tot_out = BfieldsFS( EFITdict, psipn_vec[i + 1], ntheta) this_dV = dVolume(R_in, Z_in, R_out, Z_out, False) this_dn = this_dV * nz[i] dV.append(this_dV) dn.append(this_dn) print('{:.8f} {:.8f} {:.8f}'.format(psipn_vec[i], this_dV, this_dn)) return sum(dV), sum(dn)
def calc_shat(q,rhot): rhot0 = np.arange(10000.0)/9999.0 q0 = interp(rhot,q,rhot0) qprime = fd_d1_o4(q0,rhot0) shat = rhot0/q0*qprime return rhot0,q0,shat
def readProfiles(speciesName, suffix, subtract_convection = True): profilesFilename = 'profiles_'+speciesName+'_'+ suffix fluxprofFilename = 'fluxprof'+speciesName+'_'+ suffix+'.dat' profiles = np.genfromtxt(profilesFilename) rho = profiles[:,0] tempProf = profiles[:,2] densProf = profiles[:,3] omtProf = profiles[:,4] omnProf = profiles[:,5] fluxprof = np.genfromtxt(fluxprofFilename,skip_footer=3) fluxrho = fluxprof[:,0] Gamma_es = fluxprof[:,1] Qheat_es = fluxprof[:,2] Gamma_em = fluxprof[:,3] Qheat_em = fluxprof[:,4] Gamma_tot = Gamma_es + Gamma_em Qheat_tot = Qheat_es + Qheat_em suffix = '_'+suffix #pars = init_read_parameters_file(suffix) par = Parameters() par.Read_Pars('parameters'+suffix) pars = par.pardict tempProf = tempProf / pars['Tref'] densProf = densProf / pars['nref'] temp_fluxrho = interp(rho, tempProf, fluxrho) dens_fluxrho = interp(rho, densProf, fluxrho) omt_fluxrho = interp(rho, omtProf, fluxrho) omn_fluxrho = interp(rho, omnProf, fluxrho) if subtract_convection: Qheat_tot = Qheat_tot - 3./2.*Gamma_tot*temp_fluxrho if 1 == 0: plt.plot(fluxrho, Gamma_tot, label='Gamma') plt.plot(fluxrho, Qheat_tot, label='Q') plt.legend() plt.title(speciesName) plt.show() return rho, temp_fluxrho, dens_fluxrho, omt_fluxrho, omn_fluxrho, fluxrho, Gamma_tot, Qheat_tot
def calc_pedestal_average_gHB(rbs): rmin = float(raw_input("Enter xmin for pedestal average:\n")) rmax = float(raw_input("Enter xmax for pedestal average:\n")) rhot = np.arange(2000) / 1999.0 ghb = interp(rbs[:, 0], rbs[:, 9], rhot) imin = np.argmin(abs(rhot - rmin)) imax = np.argmin(abs(rhot - rmax)) ghb_avg = np.sum(abs(ghb[imin:imax])) / float(imax - imin) return ghb_avg, rmin, rmax
def remove_edge_opt(array,edge_opt): nz = len(array) dz = 2.0*np.pi/nz zgrid_even = np.arange(nz)/float(nz-1)*(2.0*np.pi-dz)-np.pi N = np.arcsinh(edge_opt*zgrid_even[-1])/zgrid_even[-1] #zprime = 1/N*np.arcsinh(edge_opt*zgrid_even) zprime_even = zgrid_even z_of_zprime_even = 1.0/edge_opt*np.sinh(N*zprime_even) array_out = interp(z_of_zprime_even,array,zgrid_even) return array_out
def remove_edge_opt(array, edge_opt): nz = len(array) dz = 2.0 * np.pi / nz zgrid_even = np.arange(nz) / float(nz - 1) * (2.0 * np.pi - dz) - np.pi N = np.arcsinh(edge_opt * zgrid_even[-1]) / zgrid_even[-1] #zprime = 1/N*np.arcsinh(edge_opt*zgrid_even) zprime_even = zgrid_even z_of_zprime_even = 1.0 / edge_opt * np.sinh(N * zprime_even) array_out = interp(z_of_zprime_even, array, zgrid_even) return array_out
def calc_B_fields(Rgrid, rmag, Zgrid, zmag, psirz, psiax, psisep, F, nw, psip_n): # Z0_ind is the index of Zgrid of midplane Z0_ind = np.argmin(np.abs(Zgrid - zmag)) # psi_midplane is psi_pol at midplane on even Rgrid psi_pol_mp = psirz[Z0_ind, :] # Rmag_ind is index of unif_R at rmag Rmag_ind = np.argmin(np.abs(Rgrid - rmag)) ###print "rmag",rmag ###print "Rmag_ind",Rmag_ind ###print "Rgrid[Rmag_ind]~rmag", Rgrid[Rmag_ind] ###print "psi_pol_mp[Rmag_ind]~psiax", psi_pol_mp[Rmag_ind] psi_pol_obmp = psi_pol_mp[Rmag_ind:].copy() #normalize psi_pol_obmp to psip_n_temp psip_n_temp = np.empty(len(psi_pol_obmp)) for i in range(len(psi_pol_obmp)): psip_n_temp[i] = (psi_pol_obmp[i] - psiax) / (psisep - psiax) unif_R = np.linspace(Rgrid[Rmag_ind], Rgrid[-1], nw * 10) # unif_R = np.linspace(Rgrid[Rmag_ind],Rgrid[-1],nw) psip_n_unifR = interp(Rgrid[Rmag_ind:], psip_n_temp, unif_R) psisep_ind = np.argmin(abs(psip_n_unifR - 1.02)) ###print "psisep_ind", psisep_ind ###print "psip_n_temp[psisep_ind]~1", psip_n_unifR[psisep_ind] #print "we have a problem here because uniform R grid doesn't have enough resolution near separatrix" psip_n_obmp = psip_n_unifR[:psisep_ind].copy() ###print "psip_n_obmp[0]~0", psip_n_obmp[0] ###print "psip_n_obmp[-1]~1", psip_n_obmp[-1] #plt.plot(psi_pol_obmp) #plt.show() R_obmp = unif_R[:psisep_ind].copy() # B_pol is d psi_pol/ d R * (1/R) #B_pol = fd_d1_o4(psi_pol_obmp, unif_R[Rmag_ind:Rmag_ind+psisep_ind])/unif_R[Rmag_ind:Rmag_ind+psisep_ind] B_pol = fd_d1_o4(psip_n_obmp * (psisep - psiax) + psiax, R_obmp) / R_obmp # convert F(on even psi_pol grid) to F(on even R grid) F_obmp = interp(psip_n, F, psip_n_obmp) # B_tor = F/R B_tor = F_obmp / R_obmp # psip_n_obmp is normalized psi_pol at outboard midplane on uniform unif_R # B_tor and B_pol are on uniform unif_R as well # psip_n_obmp is unlike psip_n ([0,1]), it goes from 0 to 1.06 here return psip_n_obmp, R_obmp, B_pol, B_tor
def calc_B_fields(Rgrid, rmag, Zgrid, zmag, psirz, psiax, psisep, F, nw, psip_n): # Z0_ind is the index of Zgrid of midplane Z0_ind = np.argmin(np.abs(Zgrid-zmag)) # psi_midplane is psi_pol at midplane on even Rgrid psi_pol_mp = psirz[Z0_ind,:] # Rmag_ind is index of unif_R at rmag Rmag_ind = np.argmin(np.abs(Rgrid - rmag)) ###print "rmag",rmag ###print "Rmag_ind",Rmag_ind ###print "Rgrid[Rmag_ind]~rmag", Rgrid[Rmag_ind] ###print "psi_pol_mp[Rmag_ind]~psiax", psi_pol_mp[Rmag_ind] psi_pol_obmp = psi_pol_mp[Rmag_ind:].copy() #normalize psi_pol_obmp to psip_n_temp psip_n_temp = np.empty(len(psi_pol_obmp)) for i in range(len(psi_pol_obmp)): psip_n_temp[i] = (psi_pol_obmp[i]-psiax)/(psisep-psiax) unif_R = np.linspace(Rgrid[Rmag_ind],Rgrid[-1],nw*10) # unif_R = np.linspace(Rgrid[Rmag_ind],Rgrid[-1],nw) psip_n_unifR = interp(Rgrid[Rmag_ind:],psip_n_temp,unif_R) psisep_ind = np.argmin(abs(psip_n_unifR-1.02)) ###print "psisep_ind", psisep_ind ###print "psip_n_temp[psisep_ind]~1", psip_n_unifR[psisep_ind] #print "we have a problem here because uniform R grid doesn't have enough resolution near separatrix" psip_n_obmp = psip_n_unifR[:psisep_ind].copy() ###print "psip_n_obmp[0]~0", psip_n_obmp[0] ###print "psip_n_obmp[-1]~1", psip_n_obmp[-1] #plt.plot(psi_pol_obmp) #plt.show() R_obmp = unif_R[:psisep_ind].copy() # B_pol is d psi_pol/ d R * (1/R) #B_pol = fd_d1_o4(psi_pol_obmp, unif_R[Rmag_ind:Rmag_ind+psisep_ind])/unif_R[Rmag_ind:Rmag_ind+psisep_ind] B_pol = fd_d1_o4(psip_n_obmp*(psisep-psiax)+psiax,R_obmp)/R_obmp # convert F(on even psi_pol grid) to F(on even R grid) F_obmp = interp(psip_n, F, psip_n_obmp) # B_tor = F/R B_tor = F_obmp/R_obmp # psip_n_obmp is normalized psi_pol at outboard midplane on uniform unif_R # B_tor and B_pol are on uniform unif_R as well # psip_n_obmp is unlike psip_n ([0,1]), it goes from 0 to 1.06 here return psip_n_obmp, R_obmp, B_pol, B_tor
def dProfdR(ITERDBdict, EFITdict, profile, rhotLeft, rhotRight): rhot0 = EFITdict['rhotn'] R0 = EFITdict['R'] rhotn = ITERDBdict['rhot_'+profile] Prof = ITERDBdict[profile] leftInd = np.argmin(abs(rhotn - rhotLeft)) rightInd = np.argmin(abs(rhotn - rhotRight)) rhotProf = rhotn[leftInd: rightInd + 1] prof = Prof[leftInd: rightInd + 1] R = interp(rhot0, R0, rhotProf) uniR = np.linspace(R[0], R[-1], len(R)) rhot_uniR = interp(R, rhotProf, uniR) prof_uniR = interp(R, prof, uniR) profPrime_uniR = - first_derivative(prof_uniR, uniR) / prof_uniR return rhot_uniR, prof_uniR, profPrime_uniR
def fd_d1_o4_smoothend(var, grid, mat=False): """Centered finite difference, first derivative, 4th order using extrapolation to get boundary points var: quantity to be differentiated. grid: grid for var mat: matrix for the finite-differencing operator. if mat=False then it is created""" dx = grid[1] - grid[0] grid0 = np.linspace(grid[0] - 2 * dx, grid[-1] + 2 * dx, len(grid) + 4) var0 = interp(grid, var, grid0) if not mat: mat = get_mat_fd_d1_o4(len(var0), grid0[1] - grid0[0]) dvar0 = -np.dot(mat, var0) dvar_out = dvar0[2:-2] return -dvar_out
def fd_d1_o4_smoothend(var,grid,mat=False): """Centered finite difference, first derivative, 4th order using extrapolation to get boundary points var: quantity to be differentiated. grid: grid for var mat: matrix for the finite-differencing operator. if mat=False then it is created""" dx = grid[1]-grid[0] grid0 = np.linspace(grid[0]-2*dx,grid[-1]+2*dx,len(grid)+4) var0 = interp(grid,var,grid0) if not mat: mat=get_mat_fd_d1_o4(len(var0),grid0[1]-grid0[0]) dvar0=-np.dot(mat,var0) dvar_out=dvar0[2:-2] return -dvar_out
def get_geom_pars(efit_file_name,rhot0): psip_n, Rgrid, Zgrid, F, p, ffprime, pprime, psirz, qpsi, rmag, zmag, nw,psiax,psisep = read_EFIT_file(efit_file_name) R_major = rmag dummy, rhot_n, phi_edge = calc_rho_tor(psip_n, psiax, psisep, qpsi, nw,psip_n_max=0.999) psip_n_obmp, R_obmp, B_pol, B_tor = calc_B_fields(Rgrid, rmag, Zgrid, zmag, psirz, psiax, psisep, F, nw, psip_n) Bref = abs(B_tor[0]) Lref = np.sqrt(2.0*abs(phi_edge/Bref)) irhot_n = np.argmin(abs(rhot_n-rhot0)) q0 = qpsi[irhot_n] rhot_new = np.linspace(rhot_n[0],rhot_n[-1],4.0*len(rhot_n)) qpsi_new = interp(rhot_n,qpsi,rhot_new) shat = rhot_new/qpsi_new*fd_d1_o4(qpsi_new,rhot_new) irhot_new = np.argmin(abs(rhot_new-rhot0)) shat0 = shat[irhot_new] return Lref, Bref, R_major, q0, shat0
def dProfdrhot(ITERDBdict, EFITdict, profile, rhotLeft, rhotRight): rhot0 = EFITdict['rhotn'] R0 = EFITdict['R'] rhotn = ITERDBdict['rhot_'+profile] Prof = ITERDBdict[profile] leftInd = np.argmin(abs(rhotn - rhotLeft)) rightInd = np.argmin(abs(rhotn - rhotRight)) rhotProf = rhotn[leftInd: rightInd + 1] prof = Prof[leftInd: rightInd + 1] uni_rhot = np.linspace(rhotProf[0], rhotProf[-1], len(rhotProf)) prof_unirhot = interp(rhotProf, prof, uni_rhot) profPrime_unirhot = - first_derivative(prof_unirhot, uni_rhot) / prof_unirhot return uni_rhot, prof_unirhot, profPrime_unirhot
def calc_gammaE_hb(R, psi_pol, ti, B_tor, B_pol, Er, rho_tor, a): # assume R,Er,B_tor,B_pol,ti,rho_tor are on psi_pol(not normalized) M = 3.3 * 10**(-27) #a = 2.6863997038399379E-01 uni_psip = np.linspace(psi_pol[0], psi_pol[-1], 500) R_unipsip = interp(psi_pol, R, uni_psip) Er_unipsip = interp(psi_pol, Er, uni_psip) Bt_unipsip = interp(psi_pol, B_tor, uni_psip) Bp_unipsip = interp(psi_pol, B_pol, uni_psip) ti_unipsip = interp(psi_pol, ti, uni_psip) v_thi = np.sqrt(ti_unipsip / M) B_tot = np.sqrt(Bt_unipsip**2 + Bp_unipsip**2) omega_t = Er_unipsip / R_unipsip / Bp_unipsip gammaE = (R_unipsip * Bp_unipsip)**2 * fd_d1_o4(omega_t, uni_psip) / B_tot gammaE = gammaE / (v_thi / a) rhot_unipsip = interp(psi_pol, rho_tor, uni_psip) return gammaE, omega_t, rhot_unipsip
def calc_gammaE_hb(R, psi_pol, te, B_tor, B_pol, Er, rho_tor, a): # assume R,Er,B_tor,B_pol,ti,rho_tor are on psi_pol(not normalized) M = 3.3 * 10**(-27) uni_psip = np.linspace(psi_pol[0], psi_pol[-1], len(psi_pol)) #uni_psip = np.linspace(psi_pol[0],psi_pol[-1],500) R_unipsip = interp(psi_pol, R, uni_psip) Er_unipsip = interp(psi_pol, Er, uni_psip) Bt_unipsip = interp(psi_pol, B_tor, uni_psip) Bp_unipsip = interp(psi_pol, B_pol, uni_psip) te_unipsip = interp(psi_pol, te, uni_psip) c_s = np.sqrt(te_unipsip / M) B_tot = np.sqrt(Bt_unipsip**2 + Bp_unipsip**2) omega_t = Er_unipsip / R_unipsip / Bp_unipsip gammaE = (R_unipsip * Bp_unipsip)**2 * fd_d1_o4(omega_t, uni_psip) / B_tot gammaE = gammaE / (c_s / a) rhot_unipsip = interp(psi_pol, rho_tor, uni_psip) return gammaE, omega_t, rhot_unipsip
def calc_gammaE_hb(R,psi_pol,te,B_tor,B_pol,Er,rho_tor,a): # assume R,Er,B_tor,B_pol,ti,rho_tor are on psi_pol(not normalized) M = 3.3*10**(-27) uni_psip = np.linspace(psi_pol[0],psi_pol[-1],len(psi_pol)) #uni_psip = np.linspace(psi_pol[0],psi_pol[-1],500) R_unipsip = interp(psi_pol,R,uni_psip) Er_unipsip = interp(psi_pol,Er,uni_psip) Bt_unipsip = interp(psi_pol,B_tor,uni_psip) Bp_unipsip = interp(psi_pol,B_pol,uni_psip) te_unipsip = interp(psi_pol,te,uni_psip) c_s = np.sqrt(te_unipsip/M) B_tot = np.sqrt(Bt_unipsip**2+Bp_unipsip**2) omega_t = Er_unipsip/R_unipsip/Bp_unipsip gammaE = (R_unipsip*Bp_unipsip)**2*fd_d1_o4(omega_t,uni_psip)/B_tot gammaE = gammaE/(c_s/a) rhot_unipsip = interp(psi_pol,rho_tor,uni_psip) return gammaE, omega_t, rhot_unipsip
def calc_gammaE_hb(R,psi_pol,ti,B_tor,B_pol,Er,rho_tor,a): # assume R,Er,B_tor,B_pol,ti,rho_tor are on psi_pol(not normalized) M = 3.3*10**(-27) #a = 2.6863997038399379E-01 uni_psip = np.linspace(psi_pol[0],psi_pol[-1],500) R_unipsip = interp(psi_pol,R,uni_psip) Er_unipsip = interp(psi_pol,Er,uni_psip) Bt_unipsip = interp(psi_pol,B_tor,uni_psip) Bp_unipsip = interp(psi_pol,B_pol,uni_psip) ti_unipsip = interp(psi_pol,ti,uni_psip) v_thi = np.sqrt(ti_unipsip/M) B_tot = np.sqrt(Bt_unipsip**2+Bp_unipsip**2) omega_t = Er_unipsip/R_unipsip/Bp_unipsip gammaE = (R_unipsip*Bp_unipsip)**2*fd_d1_o4(omega_t,uni_psip)/B_tot gammaE = gammaE/(v_thi/a) rhot_unipsip = interp(psi_pol,rho_tor,uni_psip) return gammaE, omega_t, rhot_unipsip
psi0, ne, te, ni, ti, nz = read_cmod_pfile(p_file_name) ne = ne * 1E20 te = te * e * 1E03 ni = ni * 1E20 ti = ti * e * 1E03 nz = nz * 1E20 #plt.plot(psi0,te,'x',label='electron temp') #plt.plot(psi0,ti,'r.',label='ion temp') #plt.legend() #plt.show() rhot0 = rho_tor_spl(psi0 * (psisep - psiax) + psiax) ni_obmp = interp(psi0, ni, psip_n_obmp) ti_obmp = interp(psi0, ti, psip_n_obmp) ne_obmp = interp(psi0, ne, psip_n_obmp) te_obmp = interp(psi0, te, psip_n_obmp) Er_file = 'Er1120907032.01010_v20140623' shift_Er = True #Shift Er profile in poloidal flux coord Er_shift = 0.005 #Shift in poloidal flux coord psi0_Er, Er, Er_error = read_Er(Er_file, shift_Er, Er_shift) Er = Er * 1E03 # psi0_Er starts from 0.9 to 1.055, psip_obmp goes from 0 to 1.06 # high end of psip_n_obmp has to be outside of high end of psi0_Er # conversion makes Er outside the original range not useful psi_Er_f = np.argmin(abs(psip_n_obmp - psi0_Er[0])) psi_Er_l = np.argmin(abs(psip_n_obmp - psi0_Er[-1]))
plt.ylabel('Pressure (kPa)') #plt.legend() plt.title(efit_file_name) plt.show() ### plot q vs rhot_n plt.plot(rhot_n,qpsi,'x',label='EFIT') plt.xlabel('rhot_n') plt.ylabel('q') #plt.legend() plt.title(efit_file_name) plt.show() ### calculate shat and plot it vs rhot_n uni_rhot = np.linspace(rhot_n[0],rhot_n[-1],1000) q_unirhot = interp(rhot_n,qpsi,uni_rhot) shat = fd_d1_o4(q_unirhot,uni_rhot)*uni_rhot/q_unirhot ### output shat vs rhot_n #f = open('shat_profile.dat','w') #f.write('#1.rhot_n 2.shat\n') #np.savetxt(f,np.column_stack((uni_rhot,shat))) #f.close() #if plot_EFIT: # plot shat vs uni_rhot # plt.plot(uni_rhot,shat,'r.',label='EFIT') # plt.xlabel('rhot_n') # plt.ylabel('shat') # #plt.legend() # plt.title(efit_file_name)
def get_abs_psi_prime(geomfile,rbsfile,x0,show_plots = False, do_tests = True, remove_edge_opt = False, edge_opt = 0.0): print "Calculating absolution value of psi_prime (i.e. Bpol R)." print "x0 (rhot):",x0 #Note edge_opt is not removed--must include jacobian in integrals rbs = np.genfromtxt(rbsfile) parameters, geom = read_geometry_local(geomfile) psi0 = abs(get_psi0(rbsfile)) nz0 = float(parameters['gridpoints']) dz = 2.0/nz0 zgrid_even = np.arange(nz0)/float(nz0-1)*(2.0-dz)-1.0 # zgrid is even zgrid of length nz0 # gl_dxdR = drhot / dR on zgrid un-normalized # gl_dxdZ = drhot / dZ on zgrid un-normalized # gl_R = R on zgrid # gB = Total B on zgrid un-normalized if remove_edge_opt: if edge_opt <= 0.0: edge_opt = float(raw_input("Enter edge_opt:\n")) print "Removing edge_opt." gl_dxdR = remove_edge_opt(geom['gl_dxdR'],edge_opt) gl_dxdZ = remove_edge_opt(geom['gl_dxdZ'],edge_opt) gl_R = remove_edge_opt(geom['gl_R'],edge_opt) gl_z = remove_edge_opt(geom['gl_z'],edge_opt) gB = remove_edge_opt(geom['gBfield'],edge_opt) else: gl_dxdR = geom['gl_dxdR'] gl_dxdZ = geom['gl_dxdZ'] gl_R = geom['gl_R'] gl_z = geom['gl_z'] gB = geom['gBfield'] zgrid = zgrid_even gB = gB*parameters['Bref'] gl_dxdZ = gl_dxdZ/parameters['Lref'] gl_dxdR = gl_dxdR/parameters['Lref'] #drhot_dr = drhot / dr (where dr is radial perpendicular to flux surface) on zgrid drhot_dr = (gl_dxdR**2+gl_dxdZ**2)**0.5 rhot_even = np.arange(1000)/999.0 psi_rhot = interp(rbs[:,0],rbs[:,1],rhot_even) #dpsi_drhot = dpsi_drhot on even rhot grid dpsi_drhot = fd_d1_o4(psi_rhot,rhot_even) #ind = rhot index of radial position (on even rhot grid) ind = np.argmin(abs(rhot_even - x0)) #dpsi_drhot0 = dpsi_drhot at x0 dpsi_drhot0 = dpsi_drhot[ind] #dpsi_dr = R B_theta on zgrid dpsi_dr = drhot_dr*dpsi_drhot0*psi0 if show_plots: plt.plot(zgrid,dpsi_dr,'x-') plt.xlabel('z(rad)') plt.ylabel(r'$\frac{d \psi}{ d r}$',size=18) plt.show() np.savetxt('dpsidr.dat',np.column_stack((zgrid,dpsi_dr))) if do_tests: #####Tests##### #####Tests##### #####Tests##### #ind0 = index of outboard midplane on zgrid ind0 = np.argmin(abs(zgrid-0)) #ind_gexb = index of x0 on rbs grid x0_ind_rbs = np.argmin(abs(rbs[:,0]-x0)) #prefactor = should be B_theta**2 R**2 / B prefactor = dpsi_dr**2/gB #prefactor_norm = prefactor normalized to outboard midplane value prefactor_norm = prefactor/prefactor[ind0] #Test 1: #Calculate R**2 B_theta**2 / B #Compare outboard midplane vs rbs #print "\n\n" #print "gB(geom) at outboard",gB[ind0] #print "B(rbs) at x0",(rbs[x0_ind_rbs,25]**2+rbs[x0_ind_rbs,26]**2)**0.5 error = abs(gB[ind0]-(rbs[x0_ind_rbs,25]**2+rbs[x0_ind_rbs,26]**2)**0.5)/abs(gB[ind0]) if error>0.02: print "Bfield error=",error print "Error=",error dummy = raw_input("Press any key to continue") #print "error",error #print "\n\n" #print "R(geom) at outboard",gl_R[ind0] #print "R(rbs) at x0",rbs[x0_ind_rbs,24] error = abs(gl_R[ind0]-rbs[x0_ind_rbs,24])/abs(gl_R[ind0]) if error>0.02: print "R error=",error dummy = raw_input("Press any key to continue") #print "error",error #print "\n\n" Rrbs = rbs[:,24] R_even = np.arange(1000)/999.0*(Rrbs[-1]-Rrbs[0])+Rrbs[0] rhot_R = interp(Rrbs,rbs[:,0],R_even) psi_R = interp(Rrbs,rbs[:,1],R_even) ind_x0_R_even = np.argmin(abs(rhot_R-x0)) drhot_dR_R = fd_d1_o4(rhot_R,R_even) #print "drhot_dr (geom) at outboard", drhot_dr[ind0] #print "drhot_dr (rbs) at x0", drhot_dR_R[ind_x0_R_even] error = abs(drhot_dr[ind0]-drhot_dR_R[ind_x0_R_even])/drhot_dr[ind0] if error>0.02: print "drhot_dr error=",error dummy = raw_input("Press any key to continue") #print "error",error #print "\n\n" dpsi_dR_R = fd_d1_o4(psi_R,R_even)*psi0 #print "dpsi_dr (geom) at outboard", dpsi_dr[ind0] #print "dpsi_dr (rbs) at x0", dpsi_dR_R[ind_x0_R_even] #print "Btheta*R (rbs) at x0", rbs[x0_ind_rbs,25]*rbs[x0_ind_rbs,24] error = abs(dpsi_dr[ind0]-dpsi_dR_R[ind_x0_R_even])/dpsi_dr[ind0] if error>0.02: print "dpsi_dr error=",error dummy = raw_input("Press any key to continue") #print "error",error #print "\n\n" #print "prefactor[ind0]",prefactor[ind0] prefactor_rbs = rbs[:,25]**2*rbs[:,24]**2/(rbs[:,25]**2+rbs[:,26]**2)**0.5 #print "B_theta**2 R**2 / B", prefactor_rbs[x0_ind_rbs] error = abs(prefactor[ind0]-prefactor_rbs[x0_ind_rbs])/abs(prefactor[ind0]) if error>0.02: print "prefactor error=",error dummy = raw_input("Press any key to continue") #print "error",error #print "\n\n" #####Tests##### #####Tests##### #####Tests##### return zgrid, dpsi_dr, prefactor
""") plot_gammaE = options.plot_gammaE shift_Ti = options.shift_Ti plot_lingr = options.plot_lingr test_calc = options.test_calc psip_n, Rgrid, Zgrid, F, p, ffprime, pprime, psirz, qpsi, rmag, zmag, nw, psiax,psisep = read_EFIT_file(efit_file_name) rho_tor_spl, rhot_n = calc_rho_tor(psip_n, psiax, psisep, qpsi, nw) psip_n_obmp, R_obmp, B_pol, B_tor = calc_B_fields(Rgrid, rmag, Zgrid, zmag, psirz, psiax, psisep, F,nw,psip_n) psip_obmp = psip_n_obmp*(psisep-psiax)+psiax rhot_n_obmp = rho_tor_spl(psip_obmp) p_obmp = interp(psip_n,p,psip_n_obmp) if shift_Ti : psi0, ne, te, ni, ti, nz=read_cmod_pfile(p_file_name) else: psi0, ne, te, ni, ti, nz=read_cmod_pfile(p_file_name,shift_Ti=False,shift_Te=False) ne = ne*1E20 te = te*e*1E03 ni = ni*1E20 ti = ti*e*1E03 nz = nz*1E20 rhot0=rho_tor_spl(psi0*(psisep-psiax)+psiax) ni_obmp = interp(psi0,ni,psip_n_obmp)
Qql[nky - 1] = Qql0[ i] #Taking maximum over ballooning angle / eigenvalue #gkp = np.genfromtxt('scanfiles'+sfsuffix+'/gamma_kperp2_ratio_kxcenter0') #kperp2 = gkp[:,2] #Test kperp^3 #Qql2 = Qql*kperp2/kperp2**1.5 #if include_qn2: # Qql = Qql*Qn2 kygrid = np.linspace(pars['kymin'], (pars['nky0'] - 1) * pars['kymin'], num=pars['nky0'] - 1) #print "kygrid",kygrid #print "ky1",ky1 #Cubic spline interpolation Qql_interp1 = interp(ky2, Qql, kygrid) #Linear interpolation Qql_interp2 = np.interp(kygrid, ky2, Qql) #Qql2_interp2 = np.interp(kygrid,ky2,Qql2) Qql_tot1 = np.sum(Qql_interp1) Qql_tot2 = np.sum(Qql_interp2) Qnl_tot = np.sum(Qes) #Qql2_tot = np.sum(Qql2_interp2) print("Sum Qql interp1:", np.sum(Qql_interp1)) print("Sum Qql linear interp:", np.sum(Qql_interp2)) #print "Sum Qql (kp3) linear interp:",np.sum(Qql2_interp2) print("Sum Qnl:", np.sum(Qes)) C0 = np.sum(Qes) / np.sum(Qql_interp2) print("C0:", C0) ikpeak = np.argmax(Qql_interp2)
### read from p-file ### ne, ni are in the unit of 10^20 m^(-3) ### te, ti are in the unit of KeV psi0, ne, te, ni, ti_pfile, nz_pfile=read_cmod_pfile(p_file_name,shift_Ti=False,shift_Te=False,add_impurity=True) # convert into SI units ne = ne*1E20 te = te*e*1E03 ni = ni*1E20 ti_pfile = ti_pfile*e*1E03 nz_pfile = nz_pfile*1E20 # find ni,ti,ne,te,q on the grid of uniform psip_n at outboard midplane rhot0=rho_tor_spl(psi0*(psisep-psiax)+psiax) ni_obmp = interp(psi0,ni,psip_n_obmp) #ti_obmp = interp(psi0,ti,psip_n_obmp) ne_obmp = interp(psi0,ne,psip_n_obmp) te_obmp = interp(psi0,te,psip_n_obmp) rhotp_obmp = interp(psi0,rhot0,psip_n_obmp) q_obmp = interp(psip_n, qpsi, psip_n_obmp) # shift cxrs measurements out by 0.005 psip_n cxrs_shift = 0.005 #Shift in poloidal flux coord psip_tb,tb,psip_nb,nb,psip_Er,Er = read_cxrs_file(cxrs_file_name) psip_tb = psip_tb+cxrs_shift psip_nb = psip_nb+cxrs_shift psip_Er = psip_Er+cxrs_shift # convert into SI units tb = tb*e*1E03
n0_min = 1 #minmum mode number (include) that finder will cover n0_max = 100 #maximum mode number (include) that finder will cover q_scale = 1. #set the q to q*q_scale mref = 2. # mass of ion in proton mass #**************End of Setting up********************************************* #**************End of Block for user****************************************** #*************Loading the data****************************************** rhot0, te0, ti0, ne0, ni0, nz0, vrot0 = read_iterdb_file(iterdb_file_name) EFITdict = read_EFIT(geomfile) xgrid = EFITdict['psipn'] q = EFITdict['qpsi'] * q_scale uni_rhot = np.linspace(min(rhot0), max(rhot0), len(rhot0) * 10.) te_u = interp(rhot0, te0, uni_rhot) ne_u = interp(rhot0, ne0, uni_rhot) vrot_u = interp(rhot0, vrot0, uni_rhot) q = interp(xgrid, q, uni_rhot) tprime_e = -fd_d1_o4(te_u, uni_rhot) / te_u nprime_e = -fd_d1_o4(ne_u, uni_rhot) / ne_u midped, topped = find_pedestal(file_name=geomfile, path_name='', plot=False) x0_center = midped print('mid pedestal is at r/a = ' + str(x0_center)) Lref, Bref, R_major, q0, shat0 = get_geom_pars(geomfile, x0_center) index_begin = np.argmin(abs(uni_rhot - x0_center + 2 * (1 - x0_center)))
iterdb_file_name = 'newDIIID153764.iterdb' aGENE_m = 0.76 Bref_Gauss = 19545. n0_global = 14 kymin = 0.1044 x0 = 0.975 e = 1.6*10**(-19) mref = 2. M_kg = 3.3*10**(-27) rhot0, te0, ti0, ne0, ni0, nz0, vrot0 = read_iterdb_file(iterdb_file_name) uni_rhot = np.linspace(min(rhot0),max(rhot0),len(rhot0)*10.) te_u = interp(rhot0,te0,uni_rhot) ne_u = interp(rhot0,ne0,uni_rhot) vrot_u = interp(rhot0,vrot0,uni_rhot) tprime_e = -fd_d1_o4(te_u,uni_rhot)/te_u nprime_e = -fd_d1_o4(ne_u,uni_rhot)/ne_u x0Ind = np.argmin(abs(uni_rhot - x0)) te_mid = te_u[x0Ind] kyGENE = kymin * np.sqrt(te_u/te_mid) omMTM = kyGENE*(tprime_e+nprime_e) gyroFreq = 9.79E3/np.sqrt(mref)*np.sqrt(te_u)/aGENE_m mtmFreq = omMTM*gyroFreq/2./np.pi/1000. omegaDoppler = vrot_u*n0_global/2./np.pi/1E3
a = 2.6863997038399379E-01 parser = op.OptionParser() options,args = parser.parse_args() lores_efit_file_name = args[0] hires_efit_file_name = args[1] ### read EFIT file psip_n_lores, Rgrid_lores, Zgrid_lores, F_lores, p_lores, ffprime_lores, pprime_lores, psirz_lores, qpsi_lores, rmag_lores, zmag_lores, nw_lores, psiax_lores, psisep_lores, Bctr_lores = read_EFIT_file(lores_efit_file_name) psip_n_hires, Rgrid_hires, Zgrid_hires, F_hires, p_hires, ffprime_hires, pprime_hires, psirz_hires, qpsi_hires, rmag_hires, zmag_hires, nw_hires, psiax_hires, psisep_hires, Bctr_hires = read_EFIT_file(hires_efit_file_name) rho_tor_spl_lores, rhot_n_lores = calc_rho_tor(psip_n_lores, psiax_lores, psisep_lores, qpsi_lores, nw_lores) uni_rhot_lores = np.linspace(rhot_n_lores[0],rhot_n_lores[-1],1000) q_unirhot_lores = interp(rhot_n_lores,qpsi_lores,uni_rhot_lores) shat_lores = fd_d1_o4(q_unirhot_lores,uni_rhot_lores)*uni_rhot_lores/q_unirhot_lores rho_tor_spl_hires, rhot_n_hires = calc_rho_tor(psip_n_hires, psiax_hires, psisep_hires, qpsi_hires, nw_hires) uni_rhot_hires = np.linspace(rhot_n_hires[0],rhot_n_hires[-1],1000) q_unirhot_hires = interp(rhot_n_hires,qpsi_hires,uni_rhot_hires) shat_hires = fd_d1_o4(q_unirhot_hires,uni_rhot_hires)*uni_rhot_hires/q_unirhot_hires plt.plot(uni_rhot_lores,shat_lores,'x',label='low res') plt.plot(uni_rhot_hires,shat_hires,'r.',label='high res') plt.xlabel('rho_tor_n') plt.ylabel('shat') plt.legend() plt.show()
a = 2.6863997038399379E-01 parser = op.OptionParser() options,args = parser.parse_args() efit_file_name = 'g1120907032.01012' p_file_name = 'p1120907032.01012' iterdb_filename = args[0] ### read EFIT file psip_n, Rgrid, Zgrid, F, p, ffprime, pprime, psirz, qpsi, rmag, zmag, nw, psiax, psisep = read_EFIT_file(efit_file_name) ### find conversion relation from psi_pol to rhot_n rho_tor_spl, rhot_n = calc_rho_tor(psip_n, psiax, psisep, qpsi, nw) uni_rhot = np.linspace(rhot_n[0],rhot_n[-1],1000) q_unirhot = interp(rhot_n,qpsi,uni_rhot) shat = fd_d1_o4(q_unirhot,uni_rhot)*uni_rhot/q_unirhot #plt.plot(rhot_n,qpsi,'x') #plt.plot(uni_rhot,q_unirhot,'r.') #plt.show() #plt.plot(uni_rhot,shat) #plt.xlabel('rho_tor_n') #plt.ylabel('shat') #plt.show() ### calculate B_pol and B_tor at the outboard midplane (obmp) uniform R grid psip_n_obmp, R_obmp, B_pol, B_tor = calc_B_fields(Rgrid, rmag, Zgrid, zmag, psirz, psiax, psisep, F,nw,psip_n) psip_obmp = psip_n_obmp*(psisep-psiax)+psiax
plt.contourf(xgrid,zgrid_ext,np.real(phi_bnd[:,0,:]),70) plt.colorbar() plt.subplot(3,1,3) plt.title(r'$Im[\phi]$') plt.ylabel(r'$z/\pi$',fontsize=13) plt.xlabel(r'$\rho_{tor}$',fontsize=13) plt.contourf(xgrid,zgrid_ext,np.imag(phi_bnd[:,0,:]),70) plt.colorbar() plt.show() #Extract m's #phi_theta = np.zeros((field.nz+4,field.ny,field.nx),dtype = 'complex128') for i in range(len(xgrid)): print "m",pars['n0_global']*geometry['q'][i] #phi_theta[:,0,i] = interp(zgrid_ext,phi_bnd[:,0,i]*np.e**(-1J*pars['n0_global']*geometry['q'][i]*np.pi*zgrid_ext),zgridm) phi_theta[:,0,i] = interp(zgrid_ext,phi_bnd[:,0,i],zgridm) phi_theta[:,0,i] = phi_theta[:,0,i]*np.e**(1J*pars['n0_global']*geometry['q'][i]*np.pi*zgridm) #phi_theta[:,0,i] = interp(zgrid_ext,np.e**(-1J*pars['n0_global']*geometry['q'][i]*np.pi*zgrid_ext),zgridm) for i in range(len(xgrid)): phi_m[:,0,i] = np.fft.fft(phi_theta[:,0,i]) #for i in range(len(nm/2)): # phi_m0[i,0,:] = phi_m[i,0,:]+ if plot_theta: for i in range(int(mmin),int(mmax)+1): plt.plot(xgrid,np.abs(phi_m[i,0,:])) for i in range(len(qrats)): ix = np.argmin(abs(geometry['q']-qrats[i])) plt.axvline(xgrid[ix],color='black') plt.xlabel(r'$\rho_{tor}$',fontsize=13) plt.ylabel(r'$\phi_{m}$',fontsize=13) plt.title(r'$\phi_m$')
#from read_iterdb_file import * from read_iterdb_x import * import sys from interp import * from finite_differences_x import * from read_EFIT import * from calc_volume_from_EFIT import * from write_iterdb import * import numpy as np ITERDBdict = read_iterdb_x(sys.argv[1]) EFITdict = read_EFIT(sys.argv[2]) vrot0 = ITERDBdict['vrot'] rhot_vrot0 = ITERDBdict['rhot_vrot'] shift = 0.005 vrot_shifted = interp(rhot_vrot0 - shift, vrot0, rhot_vrot0) plt.plot(rhot_vrot0, vrot0, label='before') plt.plot(ITERDBdict['rhot_te'], vrot_shifted, label='new') plt.legend() plt.show() file_out_base = 'efit_DIIID153764_' + 'off7Zp2bsx11_120_exp_vrot_left' base_number = '153764' time_str = '0.0' psipn = interp(EFITdict['rhotn'], EFITdict['psipn'], ITERDBdict['rhot_te']) rhop = np.sqrt(abs(psipn)) output_iterdb(ITERDBdict['rhot_te'], \ rhop, \ ITERDBdict['ne']*1.E-19, \ ITERDBdict['te']*1.E-3, \ ITERDBdict['ni']*1.E-19, \ ITERDBdict['ti']*1.E-3, \
def read_cmod_pfile(p_file_name,shift_Te=False,shift_Ti=True,add_impurity=True): impurity_charge = 5.0 f=open(p_file_name,'r') data = f.read() f.close() sdata = data.split('\n') nr = int(sdata[0].split()[0]) print "p-file resolution: nr = ", nr # ne is electron density profile, dne is gradient of ne ne = np.empty(0) dne = np.empty(0) ni = np.empty(0) dni = np.empty(0) te = np.empty(0) dte = np.empty(0) ti = np.empty(0) dti = np.empty(0) # psipne is the grid of psi_pol on which ne&dne above is recorded psipne = np.empty(0) psipni = np.empty(0) psipte = np.empty(0) psipti = np.empty(0) for i in np.array(range(nr)): temp = sdata[i+1].split() psipne = np.append(psipne,float(temp[0])) ne = np.append(ne,float(temp[1])) dne = np.append(dne,float(temp[2])) temp = sdata[nr+i+2].split() psipte = np.append(psipte,float(temp[0])) te = np.append(te,float(temp[1])) dte = np.append(dte,float(temp[2])) temp = sdata[2*nr+i+3].split() psipni = np.append(psipni,float(temp[0])) ni = np.append(ni,float(temp[1])) dni = np.append(dni,float(temp[2])) temp = sdata[3*nr+i+4].split() psipti = np.append(psipti,float(temp[0])) ti = np.append(ti,float(temp[1])) dti = np.append(dti,float(temp[2])) if shift_Te: psipte = psipte + 0.005 if shift_Ti: psipti = psipti + 0.005 #plt.plot(psipne,'x') #plt.plot(psipni,'r.') #plt.show() #psi0 is normalized psi_pol in [0,1] with 1000 points #quantities with _out are interpolated on psi0 grid #print "length of arrays",len(ne),len(ni),len(te),len(ti) psi0 = np.arange(1000)/999.0 #psi0 = np.linspace(0.0,1.0,len(ne)) ne_out = interp(psipne,ne,psi0) te_out = interp(psipte,te,psi0) ni_out = interp(psipni,ni,psi0) ti_out = interp(psipti,ti,psi0) dne_out = interp(psipne,dne,psi0) dte_out = interp(psipte,dte,psi0) dni_out = interp(psipni,dni,psi0) dti_out = interp(psipti,dti,psi0) if add_impurity: nz_out = np.empty(len(ne_out)) for i in range(len(ne_out)): nz_out[i] = (ne_out[i]-ni_out[i])/impurity_charge # output quantities are psi_pol, ne, te, ni, ti # grid: even psi_pol_norm # resolution: 1000 return psi0, ne_out, te_out, ni_out, ti_out, nz_out
plt.contourf(xgrid,zgrid_ext,np.real(phi_bnd[:,0,:]),70) plt.colorbar() plt.subplot(3,1,3) plt.title(r'$Im[\phi]$') plt.ylabel(r'$z/\pi$',fontsize=13) plt.xlabel(r'$\rho_{tor}$',fontsize=13) plt.contourf(xgrid,zgrid_ext,np.imag(phi_bnd[:,0,:]),70) plt.colorbar() plt.show() #Extract m's #phi_theta = np.zeros((field.nz+4,field.ny,field.nx),dtype = 'complex128') for i in range(len(xgrid)): #print "m",pars['n0_global']*geometry['q'][i] #phi_theta[:,0,i] = interp(zgrid_ext,phi_bnd[:,0,i]*np.e**(-1J*pars['n0_global']*geometry['q'][i]*np.pi*zgrid_ext),zgridm) phi_theta[:,0,i] = interp(zgrid_ext,phi_bnd[:,0,i],zgridm) phi_theta[:,0,i] = phi_theta[:,0,i]*np.e**(1J*pars['n0_global']*geometry['q'][i]*np.pi*zgridm) #phi_theta[:,0,i] = interp(zgrid_ext,np.e**(-1J*pars['n0_global']*geometry['q'][i]*np.pi*zgrid_ext),zgridm) for i in range(len(xgrid)): phi_m[:,0,i] = np.fft.fft(phi_theta[:,0,i]) #for i in range(len(nm/2)): # phi_m0[i,0,:] = phi_m[i,0,:]+ if plot_theta: for i in range(int(mmin),int(mmax)+1): plt.plot(xgrid,np.abs(phi_m[i,0,:])) for i in range(len(qrats)): ix = np.argmin(abs(geometry['q']-qrats[i])) plt.axvline(xgrid[ix],color='black') plt.xlabel(r'$\rho_{tor}$',fontsize=13) plt.ylabel(r'$\phi_{m}$',fontsize=13) plt.title(r'$\phi_m$')
omega_tor = data[:,16]/(data[:,25]*data[:,24]) if show_plots: plt.plot(rhot,omega_tor) plt.plot(data[:,0],data[:,27]) plt.xlabel('rho_tor') plt.ylabel('omega_tor (rad/s)') plt.show() #Calculte Hahm-Burrell psi_new = np.arange(1000)/999.0 #omega_tor_new = interp(data[:,1],omega_tor,psi_new) #### Hahm-Burrell #### #### Hahm-Burrell #### #### Hahm-Burrell #### R_new = interp(data[:,1],data[:,24],psi_new) Bpol_new = interp(data[:,1],data[:,25],psi_new) Btor_new = interp(data[:,1],data[:,26],psi_new) rhot_new = interp(data[:,1],data[:,0],psi_new) Er_new = interp(data[:,1],data[:,16],psi_new) omega_tor_new = Er_new/(Bpol_new*R_new) omega_tor_prime = fd_d1_o4(omega_tor_new,psi_new) gamma_HB = omega_tor_prime*(Bpol_new*R_new)**2/((Bpol_new**2+Btor_new**2)**0.5) cs = np.sqrt(te*1000*1.6e-19/(2.0*1.67e-27)) cs_new = interp(data[:,1],cs,psi_new) gamma_HB_norm = gamma_HB/(cs_new/a) #np.savetxt('gamma_HB_'+base_name,np.column_stack((rhot_new,gamma_HB_norm,omega_tor_new))) #plt.plot(rhot_new,gamma_HB_norm,label='gamma_HB') #plt.plot(rhot,-data[:,9],label='-gamma_HB(Prashant)') #plt.title('gamma_HB/(cs/a)') #plt.legend(loc = 'upper left')
f_gi = args[1] rhot0 = float(args[2]) gene_e = np.genfromtxt(f_ge) gene_i = np.genfromtxt(f_gi) rhote = gene_e[:, 0] te = gene_e[:, 2] ne = gene_e[:, 3] rhoti = gene_i[:, 0] ti = gene_i[:, 2] ni = gene_i[:, 3] rhot1 = np.arange(1000) / 999.0 * (rhote[-1] - rhote[0]) + rhote[0] n1 = interp(rhote, ne, rhot1) t1 = interp(rhote, te, rhot1) rhot2 = np.arange(1000) / 999.0 * (rhoti[-1] - rhoti[0]) + rhoti[0] n2 = interp(rhoti, ni, rhot2) t2 = interp(rhoti, ti, rhot2) omt1 = abs(1.0 / t1 * fd_d1_o4(t1, rhot1)) omn1 = abs(1.0 / n1 * fd_d1_o4(n1, rhot1)) omt2 = abs(1.0 / t2 * fd_d1_o4(t2, rhot2)) omn2 = abs(1.0 / n2 * fd_d1_o4(n2, rhot2)) irhot1 = np.argmin(abs(rhot1[:] - rhot0)) irhot2 = np.argmin(abs(rhot2[:] - rhot0)) Te0 = t1[irhot1] Ti0 = t2[irhot2]
else: rhot_te, te, ti, ne, ni, vrot = read_iterdb_file(iterdb_file) te = te/1000.0 ti = ti/1000.0 ne = ne/1.0e19 ni = ni/1.0e19 if impurity: nb = nb/1.0e19 erhot = np.linspace(rhotor_min,erhotor_max,enx0) #itemp = np.argmin(abs(erhot-ex0)) #ex0 = erhot[itemp] #print "New x0:", ex0 ete = interp(rhot_te,te,erhot) eti = interp(rhot_te,ti,erhot) ene = interp(rhot_te,ne,erhot) eni = interp(rhot_te,ni,erhot) if impurity: enb = interp(rhot_te,nb,erhot) evrot = interp(rhot_te,vrot,erhot) show_plots = False if show_plots: plt.plot(rhot_te,te) plt.plot(erhot,ete) plt.show() plt.plot(rhot_te,ti) plt.plot(erhot,eti) plt.show()
plt.ylabel('P') #plt.legend() plt.title(efit_file_name) plt.show() ### plot q vs rhot_n plt.plot(rhot_n, qpsi, 'x', label='EFIT') plt.xlabel('rhot_n') plt.ylabel('q') #plt.legend() plt.title(efit_file_name) plt.show() ### calculate shat and plot it vs rhot_n uni_rhot = np.linspace(rhot_n[0], rhot_n[-1], 1000) q_unirhot = interp(rhot_n, qpsi, uni_rhot) shat = fd_d1_o4(q_unirhot, uni_rhot) * uni_rhot / q_unirhot f = open('shat_profile.dat', 'w') f.write('#1.rhot_n 2.shat\n') np.savetxt(f, np.column_stack((uni_rhot, shat))) f.close() if plot_EFIT: plt.plot(uni_rhot, shat, 'r.', label='EFIT') plt.xlabel('rho_tor_n') plt.ylabel('shat') #plt.legend() plt.title(efit_file_name) plt.show()
Lref, Bref, R_major, q0 = get_dimpar_pars(efit_file_name,0.9) psip_n, Rgrid, Zgrid, F, p, ffprime, pprime, psirz, qpsi, rmag, zmag, nw,psiax,psisep = read_EFIT_file(efit_file_name) print "R_major",R_major print "Bref",Bref print "psisep",psisep print "psiax",psiax psisep0 = psisep-psiax print "psisep0",psisep0 #Get everything on the same even psi grid psi0 = np.linspace(0.0,1.0,num=3000) #interopolate rho_tor, n, T, qpsi ni0 = interp(psi,ni,psi0) Ti0 = interp(psi,Ti,psi0) Ti0J = Ti0*1000.0*ee ni00 = ni0*1.0e19 pi0 = Ti0J*ni00 rhot0 = interp(psi,rhot,psi0) qpsi0 = interp(psip_n,qpsi,psi0) R0 = interp(psip_n,Rgrid,psi0) trpeps = rhot0*Lref/R_major vti = (0.5*Ti0*1000.0*ee/mref)**0.5 #nuii = 2.3031E-5*Lref*(ne)/(Te)**2*(24.0-log(sqrt(ne*1.0E13)/Te*0.001)) coll = 2.3031E-5*Lref*(ni0)/(Ti0)**2*(24.0-log(sqrt(ni0*1.0E13)/Ti0*0.001)) nustar_i=8.0/3.0/pi**0.5*qpsi0/trpeps**1.5*(R_major/Lref)*Z**4*coll #plt.plot(psi0,nustar_i)
label='nx0/4') plt.plot(zgrid_ext, np.abs(phi_bnd[:, 0, int(pars['nx0'] / 2)]), label='nx0/2') plt.plot(zgrid_ext, np.abs(phi_bnd[:, 0, 3 * int(pars['nx0'] / 4)]), label='3/4*nx0') plt.legend() plt.show() #Extract m's #phi_theta = np.zeros((field.nz+4,field.ny,field.nx),dtype = 'complex128') for i in range(len(xgrid)): #print "m",pars['n0_global']*geometry['q'][i] #phi_theta[:,0,i] = interp(zgrid_ext,phi_bnd[:,0,i]*np.e**(-1J*pars['n0_global']*geometry['q'][i]*np.pi*zgrid_ext),zgridm) phi_theta[:, 0, i] = interp(zgrid_ext, phi_bnd[:, 0, i], zgridm) phi_theta[:, 0, i] = phi_theta[:, 0, i] * np.e**( 1J * pars['n0_global'] * geometry['q'][i] * np.pi * zgridm) #phi_theta[:,0,i] = interp(zgrid_ext,np.e**(-1J*pars['n0_global']*geometry['q'][i]*np.pi*zgrid_ext),zgridm) for i in range(len(xgrid)): phi_m[:, 0, i] = np.fft.fft(phi_theta[:, 0, i]) #for i in range(len(nm/2)): # phi_m0[i,0,:] = phi_m[i,0,:]+ if plot_theta: for i in range(int(mmin), int(mmax) + 1): plt.plot(xgrid, np.abs(phi_m[i, 0, :])) for i in range(len(qrats)): ix = np.argmin(abs(geometry['q'] - qrats[i])) plt.axvline(xgrid[ix], color='black') plt.xlabel(r'$\rho_{tor}$', fontsize=13) plt.ylabel(r'$\phi_{m}$', fontsize=13)
def read_EFIT(EFIT_file_name): EFITdict = {} f = open(EFIT_file_name, 'r') eqdsk = f.readlines() line1 = eqdsk[0].split() if len(line1) == 2: nwnh = eqdsk[0].split()[1] nw = int(nwnh[0:3]) nh = int(nwnh[3:7]) else: nw = int(line1[-2]) nh = int(line1[-1]) EFITdict['nw'] = nw #number of grid for Rgrid EFITdict['nh'] = nh #number of grid for Zgrid entrylength = 16 #note: here rmin is rleft from EFIT #print(len(eqdsk[1])/entrylength) is not integer try: rdim,zdim,rcentr,rleft,zmid = \ [float(eqdsk[1][j*entrylength:(j+1)*entrylength]) \ for j in range(len(eqdsk[1])//entrylength)] except: entrylength = 15 try: rdim,zdim,rcentr,rleft,zmid = \ [float(eqdsk[1][j*entrylength:(j+1)*entrylength]) \ for j in range(len(eqdsk[1])//entrylength)] except: exit('Error reading EQDSK file, please check format!') rmaxis,zmaxis,simag,sibry,bcentr = \ [float(eqdsk[2][j*entrylength:(j+1)*entrylength]) \ for j in range(len(eqdsk[2])//entrylength)] current,simag2,xdum,rmaxis2,xdum = \ [float(eqdsk[3][j*entrylength:(j+1)*entrylength]) \ for j in range(len(eqdsk[3])//entrylength)] zmaxis2,xdum,sibry2,xdum,xdum = \ [float(eqdsk[4][j*entrylength:(j+1)*entrylength]) \ for j in range(len(eqdsk[4])//entrylength)] EFITdict['rdim'] = rdim EFITdict['zdim'] = zdim EFITdict['rcentr'] = rcentr EFITdict['rleft'] = rleft EFITdict['zmid'] = zmid EFITdict['rmaxis'] = rmaxis # R of magnetic axis (m) EFITdict['zmaxis'] = zmaxis # Z of magnetic axis (m) EFITdict['simag'] = simag # poloidal flux at magnetic axis EFITdict['sibry'] = sibry # poloidal flux at plasma boundary EFITdict['bcentr'] = bcentr # vacuum toroidal magnetic field in Telsa EFITdict['current'] = current # plasma current in Ampere print('EFIT file Resolution: %d x %d' % (EFITdict['nw'], EFITdict['nh'])) print('Horizontal dimension(m): %10.4f' % EFITdict['rdim']) print('Vertical dimension(m): %10.4f' % EFITdict['zdim']) print('Minimum R of rectangular grid: %10.4f' % EFITdict['rleft']) print('(R, Z) of magnetic axis: (%10.4f, %10.4f)' % (EFITdict['rmaxis'], EFITdict['zmaxis'])) print('poloidal flux at magnetic axis in Weber/rad: %10.4f' % EFITdict['simag']) print('poloidal flux at the plasma boundary in Weber/rad: %10.4f' % EFITdict['sibry']) print('Vacuum toroidal magnetic field at R = %10.4f: %10.4f Tesla' % (EFITdict['rcentr'], EFITdict['bcentr'])) print('Z of center of rectangular grid: %10.4f' % EFITdict['zmid']) print('Plasma current: %10.4f Ampere' % EFITdict['current']) Rgrid = np.linspace(0, 1, nw, endpoint=True) * rdim + rleft Zgrid = np.linspace(0, 1, nh, endpoint=True) * zdim + (zmid - zdim / 2.) EFITdict['Rgrid'] = Rgrid # Rgrid of psi(Z, R) EFITdict['Zgrid'] = Zgrid # Zgrid of psi(Z, R) Fpol = empty(nw, dtype=float) Pres = empty(nw, dtype=float) FFprime = empty(nw, dtype=float) Pprime = empty(nw, dtype=float) qpsi = empty(nw, dtype=float) jtor = empty(nw, dtype=float) # psi_pol is written on uniform (R,Z) grid (res=nw(R)*nh(Z)) psirz_1d = empty(nw * nh, dtype=float) start_line = 5 wordsInLine = 5 lines = range(nw // wordsInLine) if nw % wordsInLine != 0: lines = range(nw // wordsInLine + 1) for i in lines: n_entries = len(eqdsk[i + start_line]) // entrylength Fpol[i*wordsInLine:i*wordsInLine+n_entries] = \ [float(eqdsk[i+start_line][j*entrylength:(j+1)*entrylength]) \ for j in range(n_entries)] start_line = i + start_line + 1 EFITdict[ 'Fpol'] = Fpol # poloidal current function F = R * Btor on psipn grid for i in lines: n_entries = len(eqdsk[i + start_line]) // entrylength Pres[i*wordsInLine:i*wordsInLine+n_entries] = \ [float(eqdsk[i+start_line][j*entrylength:(j+1)*entrylength]) \ for j in range(n_entries)] start_line = i + start_line + 1 EFITdict['Pres'] = Pres # plasma pressure in N / m^2 on psipn grid for i in lines: n_entries = len(eqdsk[i + start_line]) // entrylength FFprime[i*wordsInLine:i*wordsInLine+n_entries] = \ [float(eqdsk[i+start_line][j*entrylength:(j+1)*entrylength]) \ for j in range(n_entries)] start_line = i + start_line + 1 EFITdict['FFprime'] = FFprime # FFprime on psipn grid for i in lines: n_entries = len(eqdsk[i + start_line]) // entrylength Pprime[i*wordsInLine:i*wordsInLine+n_entries] = \ [float(eqdsk[i+start_line][j*entrylength:(j+1)*entrylength]) \ for j in range(n_entries)] start_line = i + start_line + 1 EFITdict['Pprime'] = Pprime # Pprime on psipn grid lines_twod = range(nw * nh // wordsInLine) if nw * nh % wordsInLine != 0: lines_twod = range(nw * nh // wordsInLine + 1) for i in lines_twod: n_entries = len(eqdsk[i + start_line]) // entrylength psirz_1d[i*wordsInLine:i*wordsInLine+n_entries] = \ [float(eqdsk[i+start_line][j*entrylength:(j+1)*entrylength]) \ for j in range(n_entries)] start_line = i + start_line + 1 psirz = psirz_1d.reshape(nh, nw) EFITdict[ 'psirz'] = psirz # poloidal flux on the rectangular grid (Rgrid, Zgrid) for i in lines: n_entries = len(eqdsk[i + start_line]) // entrylength qpsi[i*wordsInLine:i*wordsInLine+n_entries] = \ [float(eqdsk[i+start_line][j*entrylength:(j+1)*entrylength]) \ for j in range(n_entries)] start_line = i + start_line + 1 EFITdict['qpsi'] = qpsi # safety factor on psipn grid # even grid of psi_pol, on which all 1D fields are defined psipn = np.linspace(0., 1., nw) EFITdict['psipn'] = psipn # uniform psipn grid interpol_order = 3 psip = psipn * (sibry - simag) + simag q_spl_psi = UnivariateSpline(psip, qpsi, k=interpol_order, s=1e-5) psi_pol_fine = linspace(psip[0], psip[-1], nw * 10) psi_tor_fine = empty((nw * 10), dtype=float) psi_tor_fine[0] = 0. for i in range(1, nw * 10): x = psi_pol_fine[:i + 1] y = q_spl_psi(x) psi_tor_fine[i] = np.trapz(y, x) rhot_n_fine = np.sqrt(psi_tor_fine / (psi_tor_fine[-1] - psi_tor_fine[0])) rho_tor_spl = UnivariateSpline(psi_pol_fine, rhot_n_fine, k=interpol_order, s=1e-5) rhotn = rho_tor_spl(psip) EFITdict['rhotn'] = rhotn # square root of toroidal flux on psipn grid Z0_ind = np.argmin(abs(Zgrid - zmaxis)) R0_ind = np.argmin(abs(Rgrid - rmaxis - 0.02)) R_obmp = Rgrid[R0_ind:] psirz_obmp = psirz[Z0_ind, R0_ind:] psipn_obmp = (psirz_obmp - simag) / (sibry - simag) sepInd = np.argmin(abs(psipn_obmp - 1.)) psipn_obmp = psipn_obmp[:sepInd + 1] R_obmp = list(R_obmp[:sepInd + 1]) R = interp(psipn_obmp, R_obmp, psipn) if 1 == 0: plt.plot(psipn_obmp, R_obmp, label='before') plt.plot(psipn, R, label='after') plt.xlabel('psipn') plt.ylabel('R') plt.legend(loc=2) plt.show() EFITdict['R'] = R # major radius (m) on psipn grid #jtor = rmaxis * Pprime + FFprime / rmaxis jtor = R * Pprime + FFprime / R EFITdict['jtor'] = jtor # toroidal current density on psipn grid #psirz_spl = interpolate.RectBivariateSpline(Zgrid, Rgrid, psirz) Bp_Z_grid = np.empty(np.shape(psirz)) for i in range(nh): Bp_Z_grid_this = first_derivative(psirz[i, :].flatten(), Rgrid) / Rgrid Bp_Z_grid[i, :] = Bp_Z_grid_this.copy() Bp_R_grid = np.empty(np.shape(psirz)) for i in range(nw): Bp_R_grid_this = -first_derivative(psirz[:, i].flatten(), Zgrid) / Rgrid[i] Bp_R_grid[:, i] = Bp_R_grid_this.copy() #Bp_R_spl = interpolate.RectBivariateSpline(Zgrid, Rgrid, Bp_R_grid) #Bp_Z_spl = interpolate.RectBivariateSpline(Zgrid, Rgrid, Bp_Z_grid) Bp_tot_grid = np.sqrt(Bp_R_grid**2 + Bp_Z_grid**2) Bp_obmp = Bp_tot_grid[Z0_ind, R0_ind:R0_ind + sepInd + 1] Bpol = interp(psipn_obmp, Bp_obmp, psipn) EFITdict['Bpol'] = Bpol # B_pol on psipn grid F_spl = interpolate.UnivariateSpline(psipn, Fpol) Btor = F_spl(psipn) / R EFITdict['Btor'] = abs(Btor) #B_tor on psipn grid return EFITdict
rt0=float(args[0]) lx_a=float(args[1]) rin = rt0 - lx_a/2.0 rout = rt0 + lx_a/2.0 print "Calculating average gene shear rate centered at ",rt0," with rhot_min = ",rin," and rhot_max = ",rout data = np.genfromtxt('rbsProfs') rho_tor = np.arange(4000)/3999.0 irbs = np.argmin(abs(data[:,0]-rt0)) i0 = np.argmin(abs(rho_tor-rt0)) Er0 = interp(data[:,0],data[:,16],rho_tor) Bp0 = interp(data[:,0],data[:,25],rho_tor) R0 = interp(data[:,0],data[:,24],rho_tor) q0 = interp(data[:,0],data[:,23],rho_tor) Te0 = interp(data[:,0],data[:,5],rho_tor) omt0 = Er0/(Bp0*R0) domt0 = fd_d1_o4(omt0,rho_tor) gamma_ExB = rho_tor/q0*domt0 cs = np.sqrt(1000.0*Te0*e0/mi) a = calc_a() gamma_ExB_norm = gamma_ExB*a/cs #plt.plot(rho_tor,gamma_ExB_norm) #plt.show()
plt.contourf(xgrid, zgrid_ext, np.real(phi_bnd[:, 0, :]), 70) plt.colorbar() plt.subplot(3, 1, 3) plt.title(r'$Im[\phi]$') plt.ylabel(r'$z/\pi$', fontsize=13) plt.xlabel(r'$\rho_{tor}$', fontsize=13) plt.contourf(xgrid, zgrid_ext, np.imag(phi_bnd[:, 0, :]), 70) plt.colorbar() plt.show() #Extract m's #phi_theta = np.zeros((field.nz+4,field.ny,field.nx),dtype = 'complex128') for i in range(len(xgrid)): print "m", pars['n0_global'] * geometry['q'][i] #phi_theta[:,0,i] = interp(zgrid_ext,phi_bnd[:,0,i]*np.e**(-1J*pars['n0_global']*geometry['q'][i]*np.pi*zgrid_ext),zgridm) phi_theta[:, 0, i] = interp(zgrid_ext, phi_bnd[:, 0, i], zgridm) phi_theta[:, 0, i] = phi_theta[:, 0, i] * np.e**( 1J * pars['n0_global'] * geometry['q'][i] * np.pi * zgridm) #phi_theta[:,0,i] = interp(zgrid_ext,np.e**(-1J*pars['n0_global']*geometry['q'][i]*np.pi*zgrid_ext),zgridm) for i in range(len(xgrid)): phi_m[:, 0, i] = np.fft.fft(phi_theta[:, 0, i]) #for i in range(len(nm/2)): # phi_m0[i,0,:] = phi_m[i,0,:]+ if plot_theta: for i in range(int(mmin), int(mmax) + 1): plt.plot(xgrid, np.abs(phi_m[i, 0, :])) for i in range(len(qrats)): ix = np.argmin(abs(geometry['q'] - qrats[i])) plt.axvline(xgrid[ix], color='black') plt.xlabel(r'$\rho_{tor}$', fontsize=13) plt.ylabel(r'$\phi_{m}$', fontsize=13)
psip_n_rbs = rbs[:,1] p_rbs = rbs[:,2] n = rbs[:,3]*10**20 Ti = rbs[:,4]*e*10**3 Te = rbs[:,5]*e*10**3 gamE_N = rbs[:,9] gamE0_N = rbs[:,10] Er = rbs[:,16] Er0 = rbs[:,17] R_rbs = rbs[:,24] omega_rbs = rbs[:,27] Bp_rbs = rbs[:,25] Bt_rbs = rbs[:,26] unif_R = linspace(R_rbs[0],R_rbs[-1],len(R_rbs)) rhot_unifR = interp(R_rbs,rhot_n_rbs,unif_R) p_unifR_rbs = interp(rhot_n_rbs,p_rbs,rhot_unifR) n_unifR = interp(rhot_n_rbs,n,rhot_unifR) Ti_unifR = interp(rhot_n_rbs,Ti,rhot_unifR) Te_unifR = interp(rhot_n_rbs,Te,rhot_unifR) Er_unifR = interp(rhot_n_rbs,Er,rhot_unifR) Er0_unifR = interp(rhot_n_rbs,Er0,rhot_unifR) Bp_unifR = interp(rhot_n_rbs,Bp_rbs,rhot_unifR) Bt_unifR = interp(rhot_n_rbs,Bt_rbs,rhot_unifR) psip_n_unifR = interp(R_rbs,psip_n_rbs,unif_R) if test_calc_gammaE: Er_gradp_rbs,gammaE_rbs_niti = calc_gammaE_niti(unif_R,n_unifR,Ti_unifR,Te_unifR,Bt_unifR,Bp_unifR,minor_a) gammaE_rbs_Er = calc_gammaE_Er(unif_R,Te_unifR,Bt_unifR,Bp_unifR,Er_unifR,minor_a) gammaE_rbs_Er0 = calc_gammaE_Er(unif_R,Te_unifR,Bt_unifR,Bp_unifR,Er0_unifR,minor_a) gammaE_rbs_hb,omega_t,rhot_unifpsip = calc_gammaE_hb(R_rbs,psip_n_rbs*abs(psip_sep_rbs),Te,Bt_rbs,Bp_rbs,Er,rhot_n_rbs,minor_a)
rhot_n_obmp = rho_tor_spl(psip_obmp) ### read from p-file ### ne, ni are in the unit of 10^20 m^(-3) ### te, ti are in the unit of KeV #psi0, ne, te, ni, ti, nz=read_cmod_pfile(p_file_name,shift=False) psi0, ne, te, ni, ti, nz=read_cmod_pfile(p_file_name,shift_Ti=True,shift_Te=True) ne = ne*1E20 te = te*e*1E03 ni = ni*1E20 ti = ti*e*1E03 nz = nz*1E20 rhot0=rho_tor_spl(psi0*(psisep-psiax)+psiax) ni_obmp = interp(psi0,ni,psip_n_obmp) ti_obmp = interp(psi0,ti,psip_n_obmp) ne_obmp = interp(psi0,ne,psip_n_obmp) te_obmp = interp(psi0,te,psip_n_obmp) rhotp_obmp = interp(psi0,rhot0,psip_n_obmp) q_obmp = interp(psip_n, qpsi, psip_n_obmp) ### read from Er file ### Er is in the unit of kV/m Er_file = 'Er1120907032.01010_v20140623' shift_Er = True #Shift Er profile in poloidal flux coord Er_shift = 0.005 #Shift in poloidal flux coord psi0_Er, Er, Er_error = read_Er(Er_file,shift_Er,Er_shift) Er = Er*1E03
def Parameter_reader(profile_name, geomfile, q_scale, manual_ped, mid_ped0, plot, output_csv): n0 = 1. mref = 2. # mass of ion in proton mass rhot0, rhop0, te0, ti0, ne0, ni0, vrot0 = read_profile_file( profile_type, profile_name, geomfile_name) if geomfile_type == "gfile": xgrid, q = read_geom_file(geomfile_type, geomfile_name, suffix) elif geomfile_type == "GENE_tracor": xgrid, q, Lref, Bref, x0_from_para = read_geom_file( geomfile_type, geomfile_name, suffix) q = q * q_scale if geomfile_type == "GENE_tracor": rhot0_range_min = np.argmin(abs(rhot0 - xgrid[0])) rhot0_range_max = np.argmin(abs(rhot0 - xgrid[-1])) rhot0 = rhot0[rhot0_range_min:rhot0_range_max] rhop0 = rhop0[rhot0_range_min:rhot0_range_max] te0 = te0[rhot0_range_min:rhot0_range_max] ti0 = ti0[rhot0_range_min:rhot0_range_max] ne0 = ne0[rhot0_range_min:rhot0_range_max] ni0 = ni0[rhot0_range_min:rhot0_range_max] vrot0 = vrot0[rhot0_range_min:rhot0_range_max] uni_rhot = np.linspace(min(rhot0), max(rhot0), len(rhot0) * 10.) te_u = interp(rhot0, te0, uni_rhot) ne_u = interp(rhot0, ne0, uni_rhot) ni_u = interp(rhot0, ni0, uni_rhot) vrot_u = interp(rhot0, vrot0, uni_rhot) q = interp(xgrid, q, uni_rhot) tprime_e = -fd_d1_o4(te_u, uni_rhot) / te_u nprime_e = -fd_d1_o4(ne_u, uni_rhot) / ne_u qprime = fd_d1_o4(q, uni_rhot) / q #center_index = np.argmax((tprime_e*te_u+nprime_e*ne_u)[0:int(len(tprime_e)*0.99)]) if manual_ped == 1: x0_center = mid_ped0 else: if geomfile_type == "gfile": midped, topped = find_pedestal(file_name=geomfile_name, path_name='', plot=False) elif geomfile_type == "GENE_tracor": midped = x0_from_para x0_center = midped print('mid pedestal is at r/a = ' + str(x0_center)) if geomfile_type == "gfile": Lref, Bref, R_major, q0, shat0 = get_geom_pars(geomfile_name, x0_center) print("Lref=" + str(Lref)) print("x0_center=" + str(x0_center)) index_begin = np.argmin(abs(uni_rhot - x0_center + 2 * (1. - x0_center))) te_u = te_u[index_begin:len(uni_rhot) - 1] ne_u = ne_u[index_begin:len(uni_rhot) - 1] ni_u = ni_u[index_begin:len(uni_rhot) - 1] vrot_u = vrot_u[index_begin:len(uni_rhot) - 1] q = q[index_begin:len(uni_rhot) - 1] tprime_e = tprime_e[index_begin:len(uni_rhot) - 1] nprime_e = nprime_e[index_begin:len(uni_rhot) - 1] qprime = qprime[index_begin:len(uni_rhot) - 1] uni_rhot = uni_rhot[index_begin:len(uni_rhot) - 1] Lt = 1. / tprime_e Ln = 1. / nprime_e Lq = 1. / qprime center_index = np.argmin(abs(uni_rhot - x0_center)) q0 = q[center_index] ne = ne_u / (10.**19.) # in 10^19 /m^3 ni = ni_u / (10.**19.) # in 10^19 /m^3 te = te_u / 1000. #in keV m_SI = mref * 1.6726 * 10**(-27) me_SI = 9.11 * 10**(-31) c = 1. qref = 1.6 * 10**(-19) #refes to GENE manual coll_c = 2.3031 * 10**(-5) * Lref * ne / (te)**2 * ( 24 - np.log(np.sqrt(ne * 10**13) / (te * 1000))) coll_ei = 4 * (ni / ne) * coll_c * np.sqrt( te * 1000. * qref / me_SI) / Lref nuei = coll_ei beta = 403. * 10**(-5) * ne * te / Bref**2. nref = ne_u[center_index] te_mid = te_u[center_index] Tref = te_u[center_index] * qref cref = np.sqrt(Tref / m_SI) Omegaref = qref * Bref / m_SI / c rhoref = cref / Omegaref kymin = n0 * q0 * rhoref / (Lref * x0_center) kyGENE = kymin * (q / q0) * np.sqrt(te_u / te_mid) * ( x0_center / uni_rhot) #Add the effect of the q varying #from mtm_doppler omMTM = kyGENE * (tprime_e + nprime_e) gyroFreq = 9.79E3 / np.sqrt(mref) * np.sqrt(te_u) / Lref mtmFreq = omMTM * gyroFreq / (2. * np.pi * 1000.) omegaDoppler = abs(vrot_u * n0 / (2. * np.pi * 1000.)) omega = mtmFreq + omegaDoppler omega_n_GENE = kyGENE * (nprime_e) #in cs/a omega_n = omega_n_GENE * gyroFreq / (2. * np.pi * 1000.) #in kHz coll_ei = coll_ei / (1000.) #in kHz shat = Ln / Lq eta = Ln / Lt ky = kyGENE nu = (coll_ei) / (omega_n) mean_rho, xstar = omega_gaussian_fit(uni_rhot, mtmFreq, rhoref, Lref, plot) if plot == True: plt.clf() plt.xlabel('r/a') plt.ylabel('eta') plt.plot(uni_rhot, eta, label='eta') plt.show() plt.clf() #plt.title('mode number finder') plt.xlabel('r/a') plt.ylabel('omega*(Lab), kHz') plt.plot(uni_rhot, omega, label='omega*(Lab)') plt.show() plt.clf() plt.xlabel('r/a') plt.ylabel('eta') plt.plot(uni_rhot, eta, label='eta') plt.show() plt.clf() plt.xlabel('r/a') plt.ylabel('shat') plt.plot(uni_rhot, shat, label='shat') plt.show() plt.clf() plt.xlabel('r/a') plt.ylabel('beta') plt.plot(uni_rhot, beta, label='beta') plt.show() plt.clf() plt.xlabel('r/a') plt.ylabel('ky rhoi') plt.plot(uni_rhot, ky, label='ky') plt.show() if output_csv == True: with open('profile_output.csv', 'w') as csvfile: data = csv.writer(csvfile, delimiter=',') data.writerow([ 'x/a', 'nu_ei(kHz)', 'omega*n(kHz)', 'omega* plasma(kHz)', 'Doppler shift(kHz)', 'nu/omega*n', 'eta', 'shat', 'beta', 'ky rhoi(for n=1)' ]) for i in range(len(uni_rhot)): data.writerow([ uni_rhot[i], coll_ei[i], omega_n[i], mtmFreq[i], omegaDoppler[i], nu[i], eta[i], shat[i], beta[i], ky[i] ]) csvfile.close() return uni_rhot, nu, eta, shat, beta, ky, q, mtmFreq, omegaDoppler, omega_n, omega_n_GENE, xstar, Lref, rhoref
parser = op.OptionParser() options,args = parser.parse_args() c_buffer_x = args[0] buffer_size = args[1] l_buffer_x = float(c_buffer_x) - 0.4*float(buffer_size) r_buffer_x = float(c_buffer_x) + 0.4*float(buffer_size) pdata=np.genfromtxt('p_info.dat') rhot=pdata[:,0] te=pdata[:,2] e = 1.6*10**(-19) rhot_fine = linspace(rhot[0],rhot[-1],10*len(rhot)) te_fine = interp(rhot,te,rhot_fine) l_ind = np.argmin(abs(rhot_fine - float(l_buffer_x))) c_ind = np.argmin(abs(rhot_fine - float(c_buffer_x))) r_ind = np.argmin(abs(rhot_fine - float(r_buffer_x))) te_l = te_fine[l_ind] te_c = te_fine[c_ind] te_r = te_fine[r_ind] lv = 3*np.sqrt(te_l/te_c) lw = lv**2 print 'lv = ', lv print 'te_l = ', te_l*0.001/e print 'te_c = ', te_c*0.001/e
### ne, ni are in the unit of 10^20 m^(-3) ### te, ti are in the unit of KeV psi0, ne, te, ni, ti_pfile, nz_pfile=read_cmod_pfile(p_file_name,shift_Ti=False,shift_Te=False,add_impurity=True) # convert into SI units ne = ne*1E20 te = te*e*1E03 #ni = ni*1E20 #ti_pfile = ti_pfile*e*1E03 #nz_pfile = nz_pfile*1E20 # find ni,ti,ne,te,q on the grid of uniform psip_n at outboard midplane rhot0=rho_tor_spl(psi0*(psisep-psiax)+psiax) #ni_obmp = interp(psi0,ni,psip_n_obmp) #ti_obmp = interp(psi0,ti,psip_n_obmp) ne_obmp = interp(psi0,ne,psip_n_obmp) te_obmp = interp(psi0,te,psip_n_obmp) rhotp_obmp = interp(psi0,rhot0,psip_n_obmp) q_obmp = interp(psip_n, qpsi, psip_n_obmp) # shift cxrs measurements out by 0.005 psip_n cxrs_shift = 0.005 #Shift in poloidal flux coord psip_tb,tb,psip_nb,nb,psip_Er,Er = read_cxrs_file(cxrs_file_name) psip_tb = psip_tb+cxrs_shift #psip_nb = psip_nb+cxrs_shift psip_Er = psip_Er+cxrs_shift # convert into SI units tb = tb*e*1E03 #nb = nb*1E18 Er = Er*1E03
a = rbsProfs_data[-1,22] rhot_data = rbsProfs_data[:,0] fs_ind = np.argmin(abs(rhot_data-fs)) rhot = rhot_data[fs_ind-10:fs_ind+10] psip = rbsProfs_data[fs_ind-10:fs_ind+10,1] n = rbsProfs_data[fs_ind-10:fs_ind+10,3] Ti = rbsProfs_data[fs_ind-10:fs_ind+10,4] Te = rbsProfs_data[fs_ind-10:fs_ind+10,5] R = rbsProfs_data[fs_ind-10:fs_ind+10,24] B_pol = rbsProfs_data[fs_ind-10:fs_ind+10,25] B_tor = rbsProfs_data[fs_ind-10:fs_ind+10,26] gamE = rbsProfs_data[fs_ind-10:fs_ind+10,9] #print R[10],B_pol[10],B_tor[10] unif_R = np.linspace(R[0],R[-1],100) Ti_unifR = interp(R,Ti,unif_R) tprime = a*fd_d1_o4(Ti_unifR,unif_R)/Ti_unifR n_unifR = interp(R,n,unif_R) fprime = a*fd_d1_o4(n_unifR,unif_R)/n_unifR gamE_unifR = interp(R,gamE,unif_R) R_fs_ind = np.argmin(abs(unif_R-R[10])) #print tprime[R_fs_ind],fprime[R_fs_ind],gamE_prime[R_fs_ind] tprime_obmp = abs(tprime[R_fs_ind]) fprime_obmp = abs(fprime[R_fs_ind]) gamE_obmp = abs(gamE_unifR[R_fs_ind]) R_obmp = R[10] B_pol_obmp = abs(B_pol[10]) B_tor_obmp = abs(B_tor[10]) B_tot_obmp = np.sqrt(B_pol_obmp**2+B_tor_obmp**2)