def plotEvolution(): #ky_list = linspace(0.3, pi, 17) ky_list = logspace(-1, log10(pi), 3) results = [] """ for w0 in [ -0.4 + 0.05j , -0.2 + 0.01j, - 0.8 - 0.01j, - 1.2 - 0.04j ]: res = [] for n in range(len(ky_list)): w0 = solveDispersion(ky_list[n], (w0, w0+0.01, w0+0.001j), doPlots=False) res.append(w0) results.append(res) """ w0 = -0.4 + 0.05j for w0_init in [ -0.25 + 0.05j, -0.5 + 0.01j, -0.4 + 0.02 ]: res = [] idx_l = where(ky_list < 2.55) idx_u = where(ky_list >= 2.55) idx = append(idx_u, idx_l[0][::-1]) ky_list_mod = ky_list[idx] for n in range(len(ky_list)): print " ky ---> " , ky_list[idx][n] if (abs(ky_list[idx][n] - 2.55) < 0.1) : w0 = w0_init w0 = solveDispersion(ky_list[idx][n], (w0, w0+0.01, w0+0.001j), doPlots=False) res.append(w0) res = array(res) results.append(res[idx]) results = array(results) #fig = gkcStyle.newFigure(ratio='1.41:1', basesize=9) fig = gkcStyle.newFigure(ratio='1.41:1', basesize=18) subplot(121) for res in results: semilogx(ky_list, imag(res), '-', label='imag') gkcStyle.plotZeroLine(min(ky_list), max(ky_list)) xlim((min(ky_list), max(ky_list))) xlabel("$k_y$") ylabel("Growthrates $\\gamma$") #savefig("Growthrate.pdf", bbox_inches='tight') subplot(122) #clf() for res in results: semilogx(ky_list, real(res), '-', label='imag') gkcStyle.plotZeroLine(min(ky_list), max(ky_list)) xlim((min(ky_list), max(ky_list))) xlabel("$k_y$") ylabel("Frequency $\\gamma$") savefig("Frequency.pdf", bbox_inches='tight') # Save max tendency savetxt("Tendency.txt", array([ky_list, real(results), imag(results)]).T)
def solveDispersion(ky): L = zeros((Nx,Nx), dtype=complex) def setup_L(w): L[:,:] = 0. iCode.setupMatrixPy(species, w, ky, X, kx_list, Ls, Ln, Nx, L, dk*dx, lambda_D2) return L def solveEquation(w): w = complex(w) L = setup_L(w) # Get Minium absolute eigenvalue. # # The non-linear eigenvalue problem obeys # det(L) = 0. This requirement is equal # to an eigenvalue 0 for the linear eigenvalue # problem det(L'- lambda I) = 0, which is identical # to the non-linear requirenement once lambda = 0. # # [ This is less sensitive (and thus numerically less demanding) # than caluculating directly the determinant val = getMinEigenvalue(L) #val = det(A) #(sign, logdet) = np.linalg.slogdet(L) #val = sign * logdet print ky, " w : %.8f+%.8f j" % (real(complex(w)), imag(complex(w))) , " Determinant : %.2e " % abs(val) return val try : #omega = complex(mp.findroot(solveEquation, (w0, w0+0.05, w0-0.005j), solver='muller', tol=1.e-15, ftol=1.e-15, maxsteps=5000)) omega = complex(mp.findroot(solveEquation, (w0, w0-0.01, w0+0.001j), solver='muller', tol=1.e-15, ftol=1.e-15, maxsteps=5000)) except: return float('nan') + 1.j * float('nan') #traceback.print_exc(file=sys.stdout) try: # n = 0 # solution found for w0, get solution vector werr = solveEquation(omega) L = setup_L(omega) # We found our eigenvalue omega, now we use the # inverse iteration to find the closest eigenvector # to the eigenvalue L__lambda_I = L - omega * eye(Nx) # Start with random inital eigenvector b = 1. * rand(Nx) + 1.j * rand(Nx) # Convergence is fast thus large iteration number not required # However, how to best check the error ? # e.g. A dot phi # Tested also, Rayleigh-Quotient Iteration but less successfull. for n in range(128): # rescale b b = b/real(sqrt(vdot(b,b))) # inverse iteration b = solve(L__lambda_I, b) # calculate error r = dot(L,b) residual = real(sqrt(vdot(r,r))) print("I-I Residual : %.2e " % residual ) if (residual < 1.e-9) : break clf() fig = gkcStyle.newFigure(ratio='1.41:1', basesize=9) fig.suptitle("$\omega_0$ = %.4f %.4fi $\pm$ %.2e %.2e i" % (real(omega), imag(omega), real(werr), imag(werr))) ###################### Plot Fourier Modes ##########3 plot(kx_list, real(b), 'r.-', label="real") plot(kx_list, imag(b), '.-', label="imag", color=gkcStyle.color_indigo) xlim((min(kx_list), max(kx_list))) xlabel("$k_x$") ylabel("$\phi(k_x)$") legend(ncol=2).draw_frame(0) savefig(str(ky) + "_Plot1.pdf", bbox_inches='tight') ################### Plot real modes ########3 clf() # We have to transform to FFTW format, which has # form of [ k=0, k=1, ..., k = N/2, k = -(N/2-1), ..., k=-1 ] F = append(append(b[Nx/2], b[Nx/2+1:]), b[:Nx/2]) K = np.fft.ifft(F) K = append(append(K[Nx/2], K[Nx/2+1:]), K[:Nx/2]) # Correct for phase K = K * exp(-1.j*arctan2(imag(sum(K)),real(sum(K)))) #print " Phase : " , arctan2(imag(sum(K)),real(sum(K))) #F_list = append(append(kx_list[Nx/2], kx_list[Nx/2+1:]), kx_list[:Nx/2]) #print "fft kx_list -----------> " , F_list plot(X, real(K), 'r.-', label='real') plot(X, imag(K), '.-', label='imag', color=gkcStyle.color_indigo) plot(X, abs(K), 'g-', label='abs', linewidth=5., alpha=0.5) xlim((min(X), max(X))) xlabel("$x$") ylabel("$\phi(x)$") legend(ncol=2).draw_frame(0) savefig(str(ky) + "_Plot2.pdf", bbox_inches='tight') ################ Plot Contour clf() y = linspace(0., Ly, 512) KxKy = zeros((Nx, 65), dtype=complex) nky = ky * Ly / (2.*pi) KxKy[:,nky] = K #np.fft.ifft(F) XY = np.fft.irfft(KxKy, axis=1, n=512) xlabel("$x$") ylabel("$y$") contourf(X, y, XY.T, 20, vmin=-abs(XY).max(), vmax=abs(XY).max()) #contourf(X, y, XY.T, 20, vmin=-abs(XY).max(), vmax=abs(XY).max()) colorbar() savefig(str(ky) + "_Plot3.pdf", bbox_inches='tight') # append and normalize sol.append(np.fft.ifft(b/abs(b).max())) except: traceback.print_exc(file=sys.stdout) return omega
def solveDispersion(ky): A = zeros((Nx,Nx), dtype=complex) def setupA(w): A[:,:] = 0. iCode.setupMatrixPy(species, w, ky, X, kx_list, Ls, Ln, Nx, A, dk*dx, lambda_D2) return A def solveEquation(w): global D_min, w_min A = setupA(complex(w)) #print A #val = SlepcDet.getMinAbsEigenvalue(A) val = SlepcDet.getMinAbsEigenvaluLA(A) #val = det(A) #(sign, logdet) = np.linalg.slogdet(A) #val = sign * logdet if abs(val) < abs(D_min) : w_min = complex(w) print ky, " w : %.3f+%.3f j" % (real(complex(w)), imag(complex(w))) , " Determinant : %.2e " % abs(val) if val != val: return 0. + 0.j return val try : w0= -0.01 + 0.02j w_damp = complex(mp.findroot(solveEquation, (w0, w0-0.005j, w0+0.005), solver='muller', tol=1.e-8, ftol=1.e-8, maxsteps=5000)) #w_damp = PyPPL.getZero(solveEquation, init=(w0, w0+0.01j, w0+0.02), solver='muller', tol=1.e-9, ftol=1.e-6, maxsteps=5000) except: traceback.print_exc(file=sys.stdout) try: #for n in range(Nx): n = 0 global w_min print "-----------------> ", w_min # solution found for w0, get solution vector werr = solveEquation(w_min) A = setupA(w_min) #print A #S = solve(A, append(1.+0.j,zeros(Nx-1, dtype=complex))) #S = solve(A, append(1.+0.j, append(zeros(Nx-2, dtype=complex), 1.+0.j))) #b = append(0., append(1.+0., zeros(Nx-2, dtype=complex))) #b = zeros(Nx, dtype=complex) #b = ones(Nx, dtype=complex) #b[:] = 0. ; #b[0] = 1. #S, err = solve(A, b), 0. #S, err = sla.lgmres(A,b, tol=1.e-9) # We found our eigenvalue w_min, now we use the # inverse iteration to find the closest eigenvector I = (1.+0.j) * eye(Nx) b = (1.+1.j) * ones(Nx, dtype=complex) for n in range(4096): b_prev = b b = solve(A - w_min * I, b) # RESCALE b = b / sum(abs(b)) if (abs(sum( sqrt(sum(b**2)/sum(b_prev**2)) * b_prev - b )) < 1.e-10) : break #print("Eigv Error : %.2e Abs : %.2e " % (abs(sum( sqrt(sum(b**2)/sum(b_prev**2)) * b_prev - b )), sum(abs(b))) ) #print "Sol : " , b clf() gkcStyle.newFigure(ratio='2.33:1', basesize=12) subplot(131) fig.suptitle("$\omega_0$ = %.4f %.4fi $\pm$ %.2e %.2e i" % (real(w_min), imag(w_min), real(werr), imag(werr))) ###################### Plot Fourier Modes ##########3 b = -real(b) + 1.j * imag(b) plot(kx_list, real(b), 'r.-', label="real") plot(kx_list, imag(b), '.-', label="imag", color=gkcStyle.color_indigo) xlim((min(kx_list), max(kx_list))) xlabel("$k_x$") ylabel("$\phi(k_x)$") legend(ncol=2).draw_frame(0) ################### Plot real modes ########3 subplot(132) # Remove High frequency modes #b[:3] = 0.; #b[-4:] = 0.; # We have to transform to FFTW format F = append(append(b[Nx/2], b[Nx/2+1:]), b[:Nx/2]) print "F--------------->", F plot(X,real(np.fft.ifft(F)), 'r.-', label='real') plot(X,imag(np.fft.ifft(F)), '.-', label='imag', color=gkcStyle.color_indigo) xlim((min(X), max(X))) xlabel("$x$") ylabel("$\phi(x)$") legend(ncol=2).draw_frame(0) ################ Plot Contour subplot(133) y = linspace(0., Ly, 128) KxKy = zeros((Nx, 65), dtype=complex) nky = ky * Ly / (2.*pi) KxKy[:,nky] = np.fft.ifft(F) XY = np.fft.irfft(KxKy, axis=1) xlabel("$x$") ylabel("$y$") contourf(X, y, XY.T, 20, vmin=-abs(XY).max(), vmax=abs(XY).max()) colorbar() savefig("Plot2_" + str(ky) + ".png", bbox_inches='tight') # append and normalize sol.append(np.fft.ifft(b/abs(b).max())) except: #species = [ Species(name= "Adiab"), Species(m=1.,q=1.,T=1.,n=1.,eta=5., name="Ion"), Species(m=0.0025,q=-1.,T=1.,n=1., eta=0., name="Electron") ] traceback.print_exc(file=sys.stdout) return w_min, abs(solveEquation(w_min))
# std-ITG (1) w_0, kx, phi_k, x, phi_0 = disp.solveEquation((w0, w0+0.0005, w0+0.0002j), 0.49, stdSetup_swITG) # sw-ITG (1) w_1, kx, phi_k, x, phi_1 = disp.solveEquation((w0, w0+0.0005, w0+0.0002j), 2.5, stdSetup_swITG) # sw-ITG (2) w_2, kx, phi_k, x, phi_2 = disp.solveEquation((w0, w0+0.0005, w0+0.0002j), 9., stdSetup_swITG) """ # sw-ITG (2) w_2, kx, phi_k, x, phi_2 = disp.solveEquation((w0, w0+0.0005, w0+0.0002j), 0.5, stdSetup_swITG) ########### Plotting #######333 gkcStyle.newFigure(ratio="2.33:1", basesize=10.) subplot(121) plot(x, real(phi_0), label="%.3f %.3f" % (real(w_0), imag(w_0))) plot(x, real(phi_1), label="%.3f %.3f" % (real(w_1), imag(w_1))) plot(x, real(phi_2), label="%.3f %.3f" % (real(w_2), imag(w_2))) gkcStyle.plotZeroLine(min(kx), max(kx)) xlim((-5, 5.)) xlabel("$X$") ylabel("$\phi(x)$") #### subplot(122) plot(x, imag(phi_0), label="%.3f %.3f" % (real(w_0), imag(w_0))) plot(x, imag(phi_1), label="%.3f %.3f" % (real(w_1), imag(w_1)))
from pylab import * import gkcStyle fig = gkcStyle.newFigure(ratio="2.33:1", basesize=12) Nx = array([ 33., 65., 127., 197., 257., 369., 513., 713.]) omega = [ -0.260 - 0.004j , - 0.218 + 0.061j, -0.229 + 0.082j, -0.233 + 0.0882j, - 0.235 + 0.0905j, -0.237 + 0.09267j, -0.238 + 0.094j, -0.239 + 0.0949j] # Frequency subplot(211) plot(1./Nx, real(omega), 'o-') xticks([0., 0.005, 0.01, 0.02, 0.04], [" \n$\\infty$", "200", "100", "50", "25"]) xlabel("Nx") ylabel("Frequency") subplot(212) plot(1./Nx, imag(omega), 'o-') xticks([0., 0.005, 0.01, 0.02, 0.04], [" \n$\\infty$", "200", "100", "50", "25"]) xlabel("Nx") ylabel("Frequency") savefig("Convergence.pdf", bbox_inches='tight')
param = { 'disp' : 'GyroSlab', 'mode' : mode, 'beta' : 1.0e-5, 'tau' : 1., 'lambdaD' : 1.e-5, 'rhoLn' : rhoLn, \ 'rhoLT' : rhoLT, 'kx' : np.sqrt(2.) * 1.e-1 , 'kp' : 2. * np.sqrt(2.) * 1.e-3, 'm_ie' : m_ie, 'adiab' : lambda x : 1 } omega_th = [] for ky in ky_list: omega_th.append(getDispersion(ky, param)) #omega_th.append(getDispersion(sqrt(1837.) * ky, param)) omega_th = np.array(omega_th) * param['kp'] #/sqrt(1837.) fileh5.close() ############################## Plot Figure ################### gkcStyle.newFigure(ratio="1:1.33", basesize=12) # Plot Growthrates pylab.subplot( 211, title="Benchmark of Smolykov et al. (PRL, 2002) using gkc++ (rev. 173)") pylab.semilogx(D['ky'][1:-1], 2. * np.imag(omega_sim[1:-1]), '^', label='gkc++') pylab.semilogx(ky_list, np.imag(omega_th), '-', color='#FF4444', alpha=0.8,
def solveDispersion(ky): L = zeros((Nx, Nx), dtype=complex) def setup_L(w): L[:, :] = 0. iCode.setupMatrixPy(species, w, ky, X, kx_list, Ls, Ln, Nx, L, dk * dx, lambda_D2) return L def solveEquation(w): w = complex(w) L = setup_L(w) # Get Minium absolute eigenvalue. # # The non-linear eigenvalue problem obeys # det(L) = 0. This requirement is equal # to an eigenvalue 0 for the linear eigenvalue # problem det(L'- lambda I) = 0, which is identical # to the non-linear requirenement once lambda = 0. # # [ This is less sensitive (and thus numerically less demanding) # than caluculating directly the determinant val = getMinEigenvalue(L) #val = det(A) #(sign, logdet) = np.linalg.slogdet(L) #val = sign * logdet print ky, " w : %.8f+%.8f j" % (real(complex(w)), imag( complex(w))), " Determinant : %.2e " % abs(val) return val try: #omega = complex(mp.findroot(solveEquation, (w0, w0+0.05, w0-0.005j), solver='muller', tol=1.e-15, ftol=1.e-15, maxsteps=5000)) omega = complex( mp.findroot(solveEquation, (w0, w0 - 0.01, w0 + 0.001j), solver='muller', tol=1.e-15, ftol=1.e-15, maxsteps=5000)) except: return float('nan') + 1.j * float('nan') #traceback.print_exc(file=sys.stdout) try: # n = 0 # solution found for w0, get solution vector werr = solveEquation(omega) L = setup_L(omega) # We found our eigenvalue omega, now we use the # inverse iteration to find the closest eigenvector # to the eigenvalue L__lambda_I = L - omega * eye(Nx) # Start with random inital eigenvector b = 1. * rand(Nx) + 1.j * rand(Nx) # Convergence is fast thus large iteration number not required # However, how to best check the error ? # e.g. A dot phi # Tested also, Rayleigh-Quotient Iteration but less successfull. for n in range(128): # rescale b b = b / real(sqrt(vdot(b, b))) # inverse iteration b = solve(L__lambda_I, b) # calculate error r = dot(L, b) residual = real(sqrt(vdot(r, r))) print("I-I Residual : %.2e " % residual) if (residual < 1.e-9): break clf() fig = gkcStyle.newFigure(ratio='1.41:1', basesize=9) fig.suptitle("$\omega_0$ = %.4f %.4fi $\pm$ %.2e %.2e i" % (real(omega), imag(omega), real(werr), imag(werr))) ###################### Plot Fourier Modes ##########3 plot(kx_list, real(b), 'r.-', label="real") plot(kx_list, imag(b), '.-', label="imag", color=gkcStyle.color_indigo) xlim((min(kx_list), max(kx_list))) xlabel("$k_x$") ylabel("$\phi(k_x)$") legend(ncol=2).draw_frame(0) savefig(str(ky) + "_Plot1.pdf", bbox_inches='tight') ################### Plot real modes ########3 clf() # We have to transform to FFTW format, which has # form of [ k=0, k=1, ..., k = N/2, k = -(N/2-1), ..., k=-1 ] F = append(append(b[Nx / 2], b[Nx / 2 + 1:]), b[:Nx / 2]) K = np.fft.ifft(F) K = append(append(K[Nx / 2], K[Nx / 2 + 1:]), K[:Nx / 2]) # Correct for phase K = K * exp(-1.j * arctan2(imag(sum(K)), real(sum(K)))) #print " Phase : " , arctan2(imag(sum(K)),real(sum(K))) #F_list = append(append(kx_list[Nx/2], kx_list[Nx/2+1:]), kx_list[:Nx/2]) #print "fft kx_list -----------> " , F_list plot(X, real(K), 'r.-', label='real') plot(X, imag(K), '.-', label='imag', color=gkcStyle.color_indigo) plot(X, abs(K), 'g-', label='abs', linewidth=5., alpha=0.5) xlim((min(X), max(X))) xlabel("$x$") ylabel("$\phi(x)$") legend(ncol=2).draw_frame(0) savefig(str(ky) + "_Plot2.pdf", bbox_inches='tight') ################ Plot Contour clf() y = linspace(0., Ly, 512) KxKy = zeros((Nx, 65), dtype=complex) nky = ky * Ly / (2. * pi) KxKy[:, nky] = K #np.fft.ifft(F) XY = np.fft.irfft(KxKy, axis=1, n=512) xlabel("$x$") ylabel("$y$") contourf(X, y, XY.T, 20, vmin=-abs(XY).max(), vmax=abs(XY).max()) #contourf(X, y, XY.T, 20, vmin=-abs(XY).max(), vmax=abs(XY).max()) colorbar() savefig(str(ky) + "_Plot3.pdf", bbox_inches='tight') # append and normalize sol.append(np.fft.ifft(b / abs(b).max())) except: traceback.print_exc(file=sys.stdout) return omega
def solveMode(ky_list, w_init_list, Nx, Setup): print "Nx" disp = solveDispersion(Nx) for (ky, w_init) in zip(ky_list, w_init_list) : w0, kx_list, K, X, R = disp.solveEquation(w_init, ky, Setup) clf() fig = gkcStyle.newFigure(ratio='1.41:1', basesize=9) fig.suptitle("$w0$ = %.4f %.4fi $\pm$ %.2e %.2e i" % (real(w0), imag(w0), 0., 0.)) ###################### Plot Fourier Modes ########## plot(kx_list, real(K), 'r.-', label="real") plot(kx_list, imag(K), '.-', label="imag", color=gkcStyle.color_indigo) xlim((min(kx_list), max(kx_list))) xlabel("$k_x$") ylabel("$\phi(k_x)$") legend(ncol=2).draw_frame(0) savefig(str(ky) + "_Plot1.pdf", bbox_inches='tight') ################### Plot real modes ########3 clf() plot(X, real(R), 'r.-', label='real') plot(X, imag(R), '.-', label='imag', color=gkcStyle.color_indigo) xlim((min(X), max(X))) xlabel("$x$") ylabel("$\phi(x)$") legend(ncol=2).draw_frame(0) savefig(str(ky) + "_Plot2.pdf", bbox_inches='tight') ################ Plot Contour ############### clf() Ly = 2.*pi/ky y = linspace(0., Ly, 256) KxKy = zeros((Nx, 65), dtype=complex) KxKy[:,1] = R XY = np.fft.irfft(KxKy, axis=1, n=256) xlabel("$x$") ylabel("$y$") contourf(X, y, XY.T, 20, vmin=-abs(XY).max(), vmax=abs(XY).max()) colorbar() savefig(str(ky) + "_Plot3.pdf", bbox_inches='tight') # append and normalize #sol.append(np.fft.ifft(b/abs(b).max())) # Write out real mode structure savetxt("IntegralCode_ky_" + str(ky) + ".txt", array([X, real(K), imag(K)]).T )
param = { 'disp' : 'GyroSlab', 'mode' : mode, 'beta' : 1.0e-5, 'tau' : 1., 'lambdaD' : 1.e-5, 'rhoLn' : rhoLn, \ 'rhoLT' : rhoLT, 'kx' : np.sqrt(2.) * 1.e-1 , 'kp' : 2. * np.sqrt(2.) * 1.e-3, 'm_ie' : m_ie, 'adiab' : lambda x : 1 } omega_th = [] for ky in ky_list : omega_th.append(getDispersion(ky, param)) #omega_th.append(getDispersion(sqrt(1837.) * ky, param)) omega_th = np.array(omega_th) * param['kp']#/sqrt(1837.) fileh5.close() ############################## Plot Figure ################### gkcStyle.newFigure(ratio="1:1.33", basesize=12) # Plot Growthrates pylab.subplot(211, title="Benchmark of Smolykov et al. (PRL, 2002) using gkc++ (rev. 173)") pylab.semilogx(D['ky'][1:-1], 2. * np.imag(omega_sim[1:-1]), '^', label='gkc++') pylab.semilogx(ky_list, np.imag(omega_th), '-', color='#FF4444', alpha=0.8, linewidth=5., label='Theory') gkcStyle.plotZeroLine(D['ky'][1], D['ky'][-1], direction='horizontal', color="#666666", lw=1.5) pylab.xlim((D['ky'][1], D['ky'][-1])) if (isKinetic) : pylab.ylim((-0.005, 0.015)) else : pylab.ylim((-0.002, 0.005))