def kdv(u, t, L): """Differential equations for the KdV equation, discretized in x.""" # Compute the x derivatives using the pseudo-spectral method. ux = psdiff(u, period=L) uxxx = psdiff(u, period=L, order=3) # Compute du/dt. dudt = -6*u*ux - uxxx return dudt
def kdv(u, t, L): """Differential equations for the KdV equation, discretized in x.""" # Compute the x derivatives using the pseudo-spectral method. ux = psdiff(u, period=L) uxxx = psdiff(u, period=L, order=3) # Compute du/dt. dudt = -6 * u * ux - uxxx return dudt
def L_dU_perp(dU_perp): # Function used with fsolve to calculate du_perp_dx at x = x_min du_perp_r = dU_perp[0:nz] du_perp_i = dU_perp[nz:] du_perp = du_perp_r + 1j * du_perp_i L1 = omega ** 2 * rhox(x_min,z) du_perp_zz = psdiff(du_perp, order=2, period=2*Lz) L_du_perp = np.cos(alpha) ** 2 * du_perp_zz + omega ** 2 / vA(x_min,z) ** 2 * du_perp nabla_perp_db_par = 1j * k_perp * db_par_x_min - np.sin(alpha) * psdiff(db_par_x_min, period=2*Lz) res = L_du_perp + L1 * u_perp_x_min - 1j * omega * nabla_perp_db_par return np.concatenate((np.real(res), np.imag(res)))
def residual(x,U,dU): ux_r = U[0:ct.nz] ux_i = U[ct.nz:2*ct.nz] b_par_r = U[2*ct.nz:3*ct.nz] b_par_i = U[3*ct.nz:4*ct.nz] u_perp_r = U[4*ct.nz:5*ct.nz] u_perp_i = U[5*ct.nz:] dux_r = dU[0:ct.nz] dux_i = dU[ct.nz:2*ct.nz] db_par_r = dU[2*ct.nz:3*ct.nz] db_par_i = dU[3*ct.nz:4*ct.nz] du_perp_r = dU[4*ct.nz:5*ct.nz] du_perp_i = dU[5*ct.nz:] ux = ux_r + 1j * ux_i b_par = b_par_r + 1j * b_par_i u_perp = u_perp_r + 1j * u_perp_i dux = dux_r + 1j * dux_i db_par = db_par_r + 1j * db_par_i du_perp = du_perp_r + 1j * du_perp_i nabla_perp_b_par = 1j * ct.k_perp * b_par - np.sin(ct.alpha) * psdiff(b_par, period = 2 * ct.Lz) nabla_perp_u_perp = 1j * ct.k_perp * u_perp - np.sin(ct.alpha) * psdiff(u_perp, period = 2 * ct.Lz) L_ux = np.cos(ct.alpha) ** 2 * psdiff(ux, order = 2, period = 2 * ct.Lz) \ + ct.omega ** 2 / ct.vA(x,ct.z) ** 2 * ux L_u_perp = np.cos(ct.alpha) ** 2 * psdiff(u_perp, order = 2, period = 2 * ct.Lz) \ + ct.omega ** 2 / ct.vA(x,ct.z) ** 2 * u_perp # nabla_perp_b_par = 1j * ct.k_perp * b_par - 1j * np.sin(ct.alpha) * b_par # nabla_perp_u_perp = 1j * ct.k_perp * u_perp - 1j * np.sin(ct.alpha) * u_perp # L_ux = -ct.kz ** 2 * np.cos(ct.alpha) ** 2 * ux \ # + ct.omega ** 2 / ct.vA(x,ct.z) ** 2 * ux # L_u_perp = -ct.kz ** 2 * np.cos(ct.alpha) ** 2 * u_perp \ # + ct.omega ** 2 / ct.vA(x,ct.z) ** 2 * u_perp res = np.zeros(6 * ct.nz) res[0:ct.nz] = np.real(dux + 1j * ct.omega * b_par + nabla_perp_u_perp) res[ct.nz:2*ct.nz] = np.imag(dux + 1j * ct.omega * b_par + nabla_perp_u_perp) res[2*ct.nz:3*ct.nz] = np.real(db_par + 1j / ct.omega * L_ux) res[3*ct.nz:4*ct.nz] = np.imag(db_par + 1j / ct.omega * L_ux) res[4*ct.nz:5*ct.nz] = np.real(L_u_perp - 1j * ct.omega * nabla_perp_b_par) res[5*ct.nz:] = np.imag(L_u_perp - 1j * ct.omega * nabla_perp_b_par) return res
def residual(x, U, dU): ux_r = U[0:nz] ux_i = U[nz:2 * nz] b_par_r = U[2 * nz:3 * nz] b_par_i = U[3 * nz:4 * nz] u_perp_r = U[4 * nz:5 * nz] u_perp_i = U[5 * nz:] dux_r = dU[0:nz] dux_i = dU[nz:2 * nz] db_par_r = dU[2 * nz:3 * nz] db_par_i = dU[3 * nz:4 * nz] du_perp_r = dU[4 * nz:5 * nz] du_perp_i = dU[5 * nz:] ux = ux_r + 1j * ux_i b_par = b_par_r + 1j * b_par_i u_perp = u_perp_r + 1j * u_perp_i dux = dux_r + 1j * dux_i db_par = db_par_r + 1j * db_par_i du_perp = du_perp_r + 1j * du_perp_i nabla_perp_b_par = 1j * k_perp * b_par - np.sin(alpha) * psdiff( b_par, period=2 * Lz) nabla_perp_u_perp = 1j * k_perp * u_perp - np.sin(alpha) * psdiff( u_perp, period=2 * Lz) L_ux = np.cos(alpha) ** 2 * psdiff(ux, order = 2, period = 2 * Lz) \ + omega ** 2 / vA(x,z) ** 2 * ux L_u_perp = np.cos(alpha) ** 2 * psdiff(u_perp, order = 2, period = 2 * Lz) \ + omega ** 2 / vA(x,z) ** 2 * u_perp res = np.zeros(6 * nz) res[0:nz] = np.real(dux + 1j * omega * b_par + nabla_perp_u_perp) res[nz:2 * nz] = np.imag(dux + 1j * omega * b_par + nabla_perp_u_perp) res[2 * nz:3 * nz] = np.real(db_par + 1j / omega * L_ux) res[3 * nz:4 * nz] = np.imag(db_par + 1j / omega * L_ux) res[4 * nz:5 * nz] = np.real(L_u_perp - 1j * omega * nabla_perp_b_par) res[5 * nz:] = np.imag(L_u_perp - 1j * omega * nabla_perp_b_par) return res
def dUdt(U, t): """ Compute dUdt, given U. Solve the algebraic system of equations to find the variables. (Given U, compute F and S :param U: Vectors of u :return: F, S """ ## Compute F #F = np.zeros([5, x.size]) F = np.zeros_like(U) F[0] = U[1] # 'rho * u' # Compute the velocity at every element u = U[1] / U[0] # u = rho * u / rho #print(u) # Compute the energy (temperature) at every element # Validated E = U[2] / U[ 0] - u**2 / 2.0 # 'e = rho * ((e + u**2/2.0))/rho - u**2/2.0' #print(f'E = {E}') # e(p, v, lambd, phi) #F[1] = U[0] * u**2 + p # Compute the porosity at every element PHI = U[3] / U[0] # phi = (rho * phi) / rho #print(f'Phi = {Phi}') # Compute the reaction progress at every element LAMBD = U[4] / U[0] # lambd = (rho * lambd) / rho #print(f'Lambd = {Lambd}') F[3] = F[0] * PHI # 'rho * u * phi' # also U[1] * U[3]/U[0] F[4] = F[0] * LAMBD # 'rho * u * Lambd' # also U[1] * U[4]/U[0] # Compute the pressure # Compute the specific volume from the density V = U[0]**(-1) # specific volume #TODO: Try to vectorize ## P = p_from_e(E, V, LAMBD, PHI) P = np.zeros(np.shape(U)[1]) for ind in range(np.shape(U)[1]): # TODO: Extremely slow P[ind] = p_from_e(E[ind], V[ind], LAMBD[ind], PHI[ind]) #print(f'P = {P}') F[1] = U[0] * u**2 + P # rho * u^2 + p #print(f'F[1] = {F[1]}') F[2] = F[0] * (E + u**2 / 2.0 + P / U[0]) # 'rho * u * (e + u^2/2 + p/rho) ## Compute S #S = np.zeros([5, x.size]) S = np.zeros_like(U) R_phi = r_phi(P, PHI) #print(f'R_phi = {R_phi}') #print(f'r_phi(P[0], Phi[0]) = {r_phi(P[0], Phi[0])}') #print(f'r_phi({P[0]}, {Phi[0]}) = {r_phi(P[0], Phi[0])}') R_lambd = r_lambda(P, LAMBD) #print(f'R_lambd = {R_lambd}') S[3] = U[0] * R_phi # rho * r_phi S[4] = U[0] * R_lambd # rho * r_lam #print(S) # Gradient of F dFdx0 = psdiff(F[0], period=L) dFdx1 = psdiff(F[1], period=L) dFdx2 = psdiff(F[2], period=L) dFdx3 = psdiff(F[3], period=L) dFdx4 = psdiff(F[4], period=L) #print(dFdx4) dFdx = np.array([ dFdx0, dFdx1, dFdx2, dFdx3, dFdx4, ]) d2Fdx0 = psdiff(F[0], order=2, period=L) d2Fdx1 = psdiff(F[1], order=2, period=L) d2Fdx2 = psdiff(F[2], order=2, period=L) d2Fdx3 = psdiff(F[3], order=2, period=L) d2Fdx4 = psdiff(F[4], order=2, period=L) dF2dx = np.array([ d2Fdx0, d2Fdx1, d2Fdx2, d2Fdx3, d2Fdx4, ]) #print(dFdx) #print(S - dFdx) return S - dFdx - 0.1e-3 * dF2dx
nabla_perp0 = 1j * k_perp beta0 = xi # Calculate b_par0 L1 = omega**2 * rhox(0, z) kzn = 2 * np.pi * fftfreq(nz, dz) kzn[0] = 1 # To avoid division by zero, note that fft(f)[0] is approximately equal to zero anyway f = beta0 * L1 / (1j * omega) * sol.sol(z)[0] b_par0 = ifft(fft(f) / 1j / (k_perp - kzn * np.sin(alpha))) ux_x_min = ux_ana(x_min, z) b_par_x_min = b_par_ana(x_min, z) u_perp_x_min = u_perp_ana(x_min, z) nabla_perp_u_perp = 1j * k_perp * u_perp_x_min - np.sin(alpha) * psdiff( u_perp_x_min, period=2 * Lz) ux_zz = psdiff(ux_x_min, order=2, period=2 * Lz) L_ux = np.cos(alpha)**2 * ux_zz + omega**2 / vA(x_min, z)**2 * ux_x_min dux_x_min = -(1j * omega * b_par_x_min + nabla_perp_u_perp) db_par_x_min = -1j / omega * L_ux dU_perp_x_min = fsolve( L_dU_perp, np.concatenate((du_perp_ana(x_min, z).real, du_perp_ana(x_min, z).imag))) U_x_min = np.concatenate((ux_x_min.real, \ ux_x_min.imag, \ b_par_x_min.real, \ b_par_x_min.imag, \ u_perp_x_min.real, \ u_perp_x_min.imag))