def test_kh_uniform_solution(show=False, verbose=False): """Test eigenvalue solver using FourierGrid""" import numpy as np from psecas import Solver, FourierGrid from psecas.systems.kh_uniform import KelvinHelmholtzUniform grid = FourierGrid(N=64, zmin=0, zmax=2) system = KelvinHelmholtzUniform(grid, beta=1e4, nu=1e-2, kx=3.52615254237) system.u0 = 1. solver = Solver(grid, system) Ns = np.hstack((np.arange(2, 16) * 32, np.arange(2, 12) * 64)) for useOPinv in [True, False]: omega, v, err = solver.iterate_solver(Ns, tol=1e-5, useOPinv=useOPinv, verbose=verbose) np.testing.assert_allclose(1.66548246011, omega, atol=1e-5) if show: from psecas import plot_solution plot_solution(system, smooth=True, num=2) # Check that information converged is set to False when solver does not converge solver.iterate_solver(np.arange(1, 3) * 16, tol=1e-16, useOPinv=useOPinv, verbose=verbose) assert solver.system.result['converged'] is False return err
def test_mti_solution(show=False, verbose=False): """Test eigenvalue solver using ChebyshevExtremaGrid""" import numpy as np from psecas import Solver, ChebyshevExtremaGrid from psecas.systems.mti import MagnetoThermalInstability grid = ChebyshevExtremaGrid(N=64, zmin=0, zmax=1) system = MagnetoThermalInstability(grid, beta=1e5, Kn0=200, kx=4 * np.pi) system.beta = 1e5 system.Kn0 = 200 assert system.Kn0 == 200 assert system.beta == 1e5 solver = Solver(grid, system) Ns = np.hstack(np.arange(1, 10) * 16) mode = 0 omega, vec, err = solver.iterate_solver(Ns, mode=mode, tol=1e-8, verbose=verbose) system.get_bx_and_by() if show: from psecas import plot_solution phi = np.arctan(vec[2].imag / vec[2].real) solver.keep_result(omega, vec * np.exp(-1j * phi), mode=mode) plot_solution(system, smooth=True, num=1) np.testing.assert_allclose(1.7814514515967603, omega, atol=1e-8) return err
Nz=256, Lz=2.0, path="./", name="khi_hydro_delta", ) # Print out some information Lx = 2 * np.pi / system.kx print('') print('Eigenvalue is:', omega) print('Lx should be:', Lx) # Make a plot plt.figure(1) plt.plot(kxmax, omega.real, "+") plot_solution(system, filename='./khi_hydro_delta.pdf') # Write files for loading into Athena s = system c_dic = {} for key in s.variables: c_dic.update({key: s.grid.to_coefficients(s.result[key])}) perturb = [] for key in ['drho', 'dvx', 'dvz', 'dT']: perturb.append(c_dic[key].real) perturb.append(c_dic[key].imag) perturb = np.transpose(perturb) np.savetxt(
for key in system.variables: system.result[key] /= val # Save system pickle object save_system(system, "./khi_nu.p") # Print out some information Lx = 2 * np.pi / system.kx print('') print('Eigenvalue is:', omega) print('Lx should be:', Lx) # Make a plot plt.figure(1) plt.plot(kxmax, omega.real, "+") plot_solution(system, filename='./khi_nu.pdf') # Write files for loading into Athena s = system c_dic = {} for key in s.variables: c_dic.update({key: s.grid.to_coefficients(s.result[key])}) perturb = [] for key in ['drho', 'dvx', 'dvz', 'dT', 'dA']: perturb.append(c_dic[key].real) perturb.append(c_dic[key].imag) perturb = np.transpose(perturb) np.savetxt(
system.add_equation( "-r*r*sigma*bphi = - 1j*kz*r*r*vphi - 1j*kz*q*r**(2-q)*Aphi - eta*(Drbphi) - lh*va*1j*kz*(DrAphi)" ) # The boundary conditions Aphi_bound = 'r**2*dr(dr(Aphi)) + r*dr(Aphi) - Aphi = 0' system.add_boundary('vr', 'Dirichlet', 'Dirichlet') system.add_boundary('vphi', 'Dirichlet', 'Dirichlet') system.add_boundary('vz', 'Neumann', 'Neumann') system.add_boundary('Aphi', Aphi_bound, Aphi_bound) system.add_boundary('bphi', 'Dirichlet', 'Dirichlet') system.add_substitution( 'DrAphi = r*r*dr(dr(Aphi)) + r*dr(Aphi) - Aphi - kz**2*r*r*Aphi') system.add_substitution( 'Drbphi = r*r*dr(dr(bphi)) + r*dr(bphi) - bphi - kz**2*r*r*bphi') solver = Solver(grid, system) mode = 0 Ns = np.arange(1, 32) * 32 - 1 omega, vec, err = solver.iterate_solver(Ns, mode=mode, verbose=True, tol=1e-4) phi = np.arctan(vec[2].imag / vec[2].real) solver.keep_result(omega, vec * np.exp(-1j * phi), mode=mode) if isinstance(grid, ChebyshevTLnGrid): plot_solution(system, limits=[0, 1.5]) plt.xlim(0, 1.5) else: plot_solution(system)
def test_mri_solution(show=False, verbose=False): """Test eigenvalue solver using ChebyshevExtremaGrid""" import numpy as np from psecas import Solver, ChebyshevExtremaGrid, System class HallMRI(System): def __init__(self, grid, kz, variables, eigenvalue): # Set parameters self.q = 1.5 self.eta = 0.003 self.lh = 1 self.h = 0.25 self.va = 0.002 self.kz = kz super().__init__(grid, variables, eigenvalue) # Create a grid grid = ChebyshevExtremaGrid(N=128, zmin=1, zmax=2, z='r') variables = ['rho', 'vr', 'vphi', 'vz', 'Aphi', 'bphi'] kz = 2 * np.pi # Create the system system = HallMRI(grid, kz, variables=variables, eigenvalue='sigma') # The linearized equations system.add_equation("-r*sigma*rho = r*dr(vr) + vr + 1j*kz*r*vz") system.add_equation( "-r*r*sigma*vr = - 2*r**(2-q)*vphi + h**2*r*r*dr(rho) + va**2*(DrAphi)" ) system.add_equation("-sigma*vphi = + (2-q)*r**(-q)*vr - va**2*1j*kz*bphi") system.add_equation("-sigma*vz = h**2*1j*kz*rho") system.add_equation( "-r*r*sigma*Aphi = + r*r*vr - eta*(DrAphi) + lh*va*1j*kz*r*r*bphi") system.add_equation( "-r*r*sigma*bphi = - 1j*kz*r*r*vphi - 1j*kz*q*r**(2-q)*Aphi - eta*(Drbphi) - lh*va*1j*kz*(DrAphi)" ) # The boundary conditions Aphi_bound = 'r**2*dr(dr(Aphi)) + r*dr(Aphi) - Aphi = 0' system.add_boundary('vr', 'Dirichlet', 'Dirichlet') system.add_boundary('vphi', 'Dirichlet', 'Dirichlet') system.add_boundary('vz', 'Neumann', 'Neumann') system.add_boundary('Aphi', Aphi_bound, Aphi_bound) system.add_boundary('bphi', 'Dirichlet', 'Dirichlet') # Short hands for long expressions for derivatives system.add_substitution( 'DrAphi = r*r*dr(dr(Aphi)) + r*dr(Aphi) - Aphi - kz**2*r*r*Aphi') system.add_substitution( 'Drbphi = r*r*dr(dr(bphi)) + r*dr(bphi) - bphi - kz**2*r*r*bphi') solver = Solver(grid, system) mode = 0 Ns = np.hstack(np.arange(1, 10) * 32) omega, vec, err = solver.iterate_solver(Ns, mode=mode, tol=1e-8, verbose=verbose) if show: from psecas import plot_solution phi = np.arctan(vec[2].imag / vec[2].real) solver.keep_result(omega, vec * np.exp(-1j * phi), mode=mode) plot_solution(system, smooth=True, num=1) np.testing.assert_allclose(0.09892641, omega, atol=1e-8) return err
import numpy as np from psecas import Solver, LegendreExtremaGrid import matplotlib.pyplot as plt from psecas import plot_solution grid = LegendreExtremaGrid(N=200, zmin=-0.5, zmax=0.5) system = KelvinHelmholtzMiura(grid, kx=2*np.pi, B=0.2, z1=0, a=1/25.0) solver = Solver(grid, system) Ns = np.arange(1, 8) * 64 omega, v, err = solver.iterate_solver(Ns, verbose=True, tol=1e-8) print('\nB={:1.2f} has gammma*2a = {:1.6f}'.format(system.B, omega.real*system.a*2)) plot_solution(system, num=1, filename='Ryu_and_Frank1996.pdf') # Make figure 4 in Miura & Pritchett (1982) # This calculation takes some time. if True: # The iterative solver is more precise but slow at high kx # Set fast=True to instead run with a fixed grid size and get a figure # much faster. The result looks okay but is imprecise at high k! fast = False plt.figure(2) plt.clf() for B in [1e-8, 0.2, 0.3, 0.4]: system = KelvinHelmholtzMiura(grid, 2*np.pi, B=B, z1=0, a=1/20.0)
beta = 1e5 Kn = 1 / 1500. kx = 250 system = HeatFluxDrivenBuoyancyInstability(grid, beta, Kn, kx) solver = Solver(grid, system) mode = 2 Ns = np.hstack((np.arange(2, 5) * 16, np.arange(3, 12) * 32)) omega, vec, err = solver.iterate_solver(Ns, mode=mode, verbose=True, tol=1e-5) phi = np.arctan(vec[2].imag / vec[2].real) solver.keep_result(omega, vec * np.exp(-1j * phi), mode=mode) plot_solution(system, smooth=True) if True: # Plot 2D maps of the perturbations import matplotlib.pyplot as plt from psecas import get_2Dmap plt.rc("image", origin="lower", cmap="RdBu") plt.figure(2) plt.clf() fig, axes = plt.subplots(num=2, sharex=True, sharey=True, ncols=4) xmin = 0 xmax = grid.zmax/10 Nx = 512 Nz = 1024
drhodz_sym = sym.diff(rho_sym, z) domgdz_sym = sym.diff(Omg_sym, z) zg = self.grid.zg self.rho = np.ones_like(zg) * lambdify(z, rho_sym)(zg) self.Omg = np.ones_like(zg) * lambdify(z, Omg_sym)(zg) self.shr = np.ones_like(zg) * lambdify(z, shr_sym)(zg) self.drhodz = np.ones_like(zg) * lambdify(z, drhodz_sym)(zg) self.domgdz = np.ones_like(zg) * lambdify(z, domgdz_sym)(zg) # Create a grid grid = ChebyshevRationalGrid(N=200, C=0.2) # grid = ChebyshevExtremaGrid(N=199, zmin=-5, zmax=5) # Create the system system = VerticalShearInstability(grid, variables=['rh', 'wx', 'wy', 'wz'], eigenvalue='sigma') # The linearized equations system.add_equation("-sigma*rh = - 1j*kx*wx - 1/h*dz(wz) - 1/h*drhodz/rho*wz") system.add_equation("-sigma*wx = + 2*Omg*wy - 1j*kx*(h/O0)**2*rh") system.add_equation("-sigma*wy = - (2*Omg + shr)*wx - 1/h*domgdz*wz") system.add_equation("-sigma*wz = - h/O0**2*dz(rh)", boundary=True) solver = Solver(grid, system, do_gen_evp=True) omega, vec = solver.solve(mode=0, verbose=True) plot_solution(system)
plt.rc("image", origin="lower", cmap="RdBu", interpolation="None") extent = [xmin, xmax, zmin, zmax] phi = np.arctan(vec[2].imag / vec[2].real) solver.keep_result(omega, vec * np.exp(-1j * phi), mode=0) # Normalize eigenmodes y = np.vstack([ system.result["dvx"].real, system.result["dvx"].imag, system.result["dvz"].real, system.result["dvz"].imag, ]) limits = [-2, 2] plot_solution(system, num=1, limits=limits, smooth=True) plt.xlim(limits[0], limits[1]) maps = { key: get_2Dmap(system, key, xmin, xmax, Nx, Nz, zmin=zmin, zmax=zmax) for key in system.variables } plt.figure(2) plt.clf() fig, axes = plt.subplots(num=2) axes.imshow(maps["dT"] + maps["drho"], extent=extent)
for key in system.variables: system.result[key] /= val # Save system pickle object save_system(system, "./khi_mhd_beta5.p") # Print out some information Lx = 2 * np.pi / system.kx print('') print('Eigenvalue is:', omega) print('Lx should be:', Lx) # Make a plot plt.figure(1) plt.plot(kxmax, omega.real, "+") plot_solution(system, filename='./khi_mhd_beta5.pdf') # Write files for loading into Athena s = system c_dic = {} for key in s.variables: c_dic.update({key: s.grid.to_coefficients(s.result[key])}) perturb = [] for key in ['drho', 'dvx', 'dvz', 'dT', 'dA']: perturb.append(c_dic[key].real) perturb.append(c_dic[key].imag) perturb = np.transpose(perturb) np.savetxt(