def update(context): global im1, im3 c = context params = config.params solver = config.solver X, U, phi = c.X, c.U, c.phi if (params.tstep % params.compute_energy == 0 or params.tstep % params.sample_stats or params.tstep % params.plot_result == 0 and params.plot_result > 0): U = solver.get_velocity(**c) phi = c.phi_hat.backward(c.phi) if params.tstep == 1 and solver.rank == 0 and params.plot_result > 0: # Initialize figures plt.figure(1, figsize=(6, 3)) im1 = plt.quiver(X[1][:, :, 0], X[0][:, :, 0], U[1, :, :, 0], U[0, :, :, 0], pivot='mid', scale=5) #im1.set_array(U[0,:,:,0]) plt.draw() plt.figure(2, figsize=(6, 3)) im3 = plt.contourf(X[1][0, :, 0], X[0][:, 0, 0], phi[:, :, 0], 100) plt.colorbar(im3) plt.draw() plt.pause(1e-6) if params.tstep % params.plot_result == 0 and solver.rank == 0 and params.plot_result > 0: plt.figure(1) im1.set_UVC(U[1, :, :, 0], U[0, :, :, 0]) im1.scale = np.linalg.norm(0.25 * U[1]) plt.pause(1e-6) plt.figure(2) im3.ax.clear() im3.ax.contourf(X[1][0, :, 0], X[0][:, 0, 0], phi[:, :, 0], 100) im3.autoscale() plt.pause(1e-6) if params.tstep % params.compute_energy == 0: e0 = dx(U[0] * U[0], c.FST) e1 = dx(U[1] * U[1], c.FST) e2 = dx(U[2] * U[2], c.FST) e3 = dx(phi * phi, c.FRB) div_u = solver.get_divergence(**c) e4 = dx(div_u * div_u, c.FST) if solver.rank == 0: print("Time %2.5f Energy %2.6e %2.6e %2.6e %2.6e div %2.6e" % (config.params.t, e0, e1, e2, e3, e4)) if params.tstep % params.sample_stats == 0: solver.stats(U, phi)
def initialize(solver, context): global OS, e0 params = config.params OS = OrrSommerfeld(Re=params.Re, N=128) eigvals, eigvectors = OS.solve(False) OS.eigvals, OS.eigvectors = eigvals, eigvectors U = context.U X = context.X FST = context.FST initOS(OS, eigvals, eigvectors, U, X) U_hat = solver.set_velocity(**context) U = solver.get_velocity(**context) # Compute convection from data in context (i.e., context.U_hat and context.g) # This is the convection at t=0 if hasattr(context.FST, 'dx'): e0 = 0.5 * FST.dx(U[0]**2 + (U[1] - (1 - X[0]**2))**2, context.ST.quad) else: e0 = 0.5 * dx(U[0]**2 + (U[1] - (1 - X[0]**2))**2, context.FST) #print(e0) acc[0] = 0.0 if not 'KMMRK3' in params.solver: # Initialize at t = dt context.H_hat1[:] = solver.get_convection(**context) initOS(OS, eigvals, eigvectors, U, X, t=params.dt) U_hat = solver.set_velocity(**context) U = solver.get_velocity(**context) context.U_hat0[:] = U_hat params.t = params.dt params.tstep = 1 if hasattr(context.FST, 'dx'): e1 = 0.5 * FST.dx(U[0]**2 + (U[1] - (1 - X[0]**2))**2, context.ST.quad) else: e1 = 0.5 * dx(U[0]**2 + (U[1] - (1 - X[0]**2))**2, context.FST) if solver.rank == 0: acc[0] += abs(e1 / e0 - exp(2 * imag(OS.eigval) * params.t)) else: params.t = 0 params.tstep = 0 if not "KMM" in params.solver: P_hat = solver.compute_pressure(**context) P = FST.backward(P_hat, context.P, context.SN) else: context.g[:] = 0
def initialize(solver, context): global OS, e0 params = config.params OS = OrrSommerfeld(Re=params.Re, N=128) eigvals, eigvectors = OS.solve(False) OS.eigvals, OS.eigvectors = eigvals, eigvectors U = context.U X = context.X FST = context.FST initOS(OS, eigvals, eigvectors, U, X) U_hat = solver.set_velocity(**context) U = solver.get_velocity(**context) # Compute convection from data in context (i.e., context.U_hat and context.g) # This is the convection at t=0 e0 = 0.5 * dx(U[0]**2 + (U[1] - (1 - X[0]**2))**2, FST) acc[0] = 0.0 if 'RK3' not in params.solver: # Initialize at t = dt context.H_hat1[:] = solver.get_convection(**context) context.U_hat0[:] = U_hat context.U0[:] = U initOS(OS, eigvals, eigvectors, U, X, t=params.dt) U_hat = solver.set_velocity(**context) U = solver.get_velocity(**context) params.t = params.dt params.tstep = 1 e1 = 0.5 * dx(U[0]**2 + (U[1] - (1 - X[0]**2))**2, FST) if solver.rank == 0: acc[0] += abs(e1 / e0 - exp(2 * imag(OS.eigval) * params.t)) * params.dt else: params.t = 0 params.tstep = 0 if not ("KMM" in params.solver or "Coupled" in params.solver): P_hat = solver.compute_pressure(**context) P = P_hat.backward(context.P) if params.convection == 'Vortex': P += 0.5 * sum(U**2, axis=0) P_hat = context.P.forward(P_hat) else: try: context.g[:] = 0 except AttributeError: pass
def update(context): c = context params = config.params solver = config.solver #if params.tstep == 2: reset_profile(profile) if (params.tstep % params.plot_step == 0 or params.tstep % params.compute_energy == 0): U = solver.get_velocity(**context) global im1, im2, im3, OS, e0, acc if not plt is None: if im1 is None and solver.rank == 0 and params.plot_step > 0: plt.figure() im1 = plt.contourf(c.X[1][:, :, 0], c.X[0][:, :, 0], c.U[0, :, :, 0], 100) plt.colorbar(im1) plt.draw() plt.figure() im2 = plt.contourf(c.X[1][:, :, 0], c.X[0][:, :, 0], c.U[1, :, :, 0] - (1 - c.X[0][:, :, 0]**2), 100) plt.colorbar(im2) plt.draw() plt.figure() im3 = plt.quiver(c.X[1][:, :, 0], c.X[0][:, :, 0], c.U[1, :, :, 0] - (1 - c.X[0][:, :, 0]**2), c.U[0, :, :, 0]) plt.draw() plt.pause(1e-6) if params.tstep % params.plot_step == 0 and solver.rank == 0 and params.plot_step > 0: im1.ax.clear() im1.ax.contourf(c.X[1][:, :, 0], c.X[0][:, :, 0], U[0, :, :, 0], 100) im1.autoscale() im2.ax.clear() im2.ax.contourf(c.X[1][:, :, 0], c.X[0][:, :, 0], U[1, :, :, 0] - (1 - c.X[0][:, :, 0]**2), 100) im2.autoscale() im3.set_UVC(U[1, :, :, 0] - (1 - c.X[0][:, :, 0]**2), U[0, :, :, 0]) plt.pause(1e-6) if params.tstep % params.compute_energy == 0: e1, e2, exact = compute_error(c) div_u = solver.get_divergence(**c) if hasattr(c.FST, 'dx'): e3 = 0.5 * c.FST.dx(div_u**2, c.ST.quad) else: e3 = dx(div_u**2, c.FST) if solver.rank == 0 and not config.params.spatial_refinement_test: acc[0] += abs(e1 / e0 - exact) print("Time %2.5f Norms %2.16e %2.16e %2.16e %2.16e %2.16e" % (params.t, e1 / e0, exact, e1 / e0 - exact, sqrt(e2), e3))
def compute_error(context): global OS, e0, acc c = context params = config.params solver = config.solver U = solver.get_velocity(**c) pert = (U[1] - (1 - c.X[0]**2))**2 + U[0]**2 e1 = 0.5 * dx(pert, c.FST) exact = exp(2 * imag(OS.eigval) * params.t) U0 = c.work[(c.U, 0, True)] initOS(OS, OS.eigvals, OS.eigvectors, U0, c.X, t=params.t) pert = (U[0] - U0[0])**2 + (U[1] - U0[1])**2 #pert = (U[1] - U0[1])**2 e2 = 0.5 * dx(pert, c.FST) return e1, e2, exact
def compute_error(context): global OS, e0, acc c = context params = config.params solver = config.solver U = solver.get_velocity(**c) pert = (U[0] - (1-c.X[2]**2))**2 + U[2]**2 e1 = 0.5*dx(pert, c.FST, axis=2) exact = exp(2*imag(OS.eigval)*params.t) U0 = c.work[(c.U, 0, True)] initOS(OS, OS.eigvals, OS.eigvectors, U0, c.X, t=params.t) #pert = (U[0] - U0[0])**2 + (U[1]-U0[1])**2 pert = (U[2] - U0[2])**2 e2 = 0.5*dx(pert, c.FST, axis=2) return e1, e2, exact
def initialize(solver, context): global OS, e0 params = config.params OS = OrrSommerfeld(Re=params.Re, N=128) eigvals, eigvectors = OS.solve(False) OS.eigvals, OS.eigvectors = eigvals, eigvectors U = context.U X = context.X FST = context.FST initOS(OS, eigvals, eigvectors, U, X) U_hat = solver.set_velocity(**context) U = solver.get_velocity(**context) # Compute convection from data in context (i.e., context.U_hat and context.g) # This is the convection at t=0 e0 = 0.5*dx(U[2]**2+(U[0]-(1-X[2]**2))**2, context.FST, axis=2) #print(e0) acc[0] = 0.0 if not 'KMMRK3' in params.solver: # Initialize at t = dt context.H_hat1[:] = solver.get_convection(**context) initOS(OS, eigvals, eigvectors, U, X, t=params.dt) U_hat = solver.set_velocity(**context) U = solver.get_velocity(**context) context.U_hat0[:] = U_hat params.t = params.dt params.tstep = 1 e1 = 0.5*dx(U[2]**2+(U[0]-(1-X[2]**2))**2, context.FST, axis=2) if solver.rank == 0: acc[0] += abs(e1/e0 - exp(2*imag(OS.eigval)*params.t)) else: params.t = 0 params.tstep = 0 if not "KMM" in params.solver: P_hat = solver.compute_pressure(**context) P = FST.backward(P_hat, context.P, context.SN) else: context.g[:] = 0
def update(context): c = context params = config.params solver = config.solver #if params.tstep == 2: reset_profile(profile) if (params.tstep % params.plot_step == 0 or params.tstep % params.compute_energy == 0): U = solver.get_velocity(**context) global im1, im2, im3, OS, e0, acc if not plt is None: if im1 is None and solver.rank == 0 and params.plot_step > 0: plt.figure() im1 = plt.contourf(c.X[0][:, 0, :], c.X[2][:, 0, :], c.U[2, :, 0, :], 100) plt.colorbar(im1) plt.draw() plt.figure() im2 = plt.contourf(c.X[0][:, 0, :], c.X[2][:, 0, :], c.U[0, :, 0, :] - (1-c.X[2][:, 0, :]**2), 100) plt.colorbar(im2) plt.draw() plt.figure() im3 = plt.quiver(c.X[0][:, 0, :], c.X[2][:, 0, :], c.U[0, :, 0, :]-(1-c.X[2][:, 0, :]**2), c.U[2, :, 0, :]) plt.draw() plt.pause(1e-6) if params.tstep % params.plot_step == 0 and solver.rank == 0 and params.plot_step > 0: im1.ax.clear() im1.ax.contourf(c.X[0][:, 0, :], c.X[2][:, 0, :], U[0, :, 0, :], 100) im1.autoscale() im2.ax.clear() im2.ax.contourf(c.X[0][:, 0, :], c.X[2][:, 0, :], U[0, :, 0, :]-(1-c.X[2][:, 0, :]**2), 100) im2.autoscale() im3.set_UVC(U[0, :, 0, :]-(1-c.X[2][:, 0, :]**2), U[2, :, 0, :]) plt.pause(1e-6) if params.tstep % params.compute_energy == 0: e1, e2, exact = compute_error(c) div_u = solver.get_divergence(**c) e3 = dx(div_u**2, c.FST, axis=2) if solver.rank == 0 and not config.params.spatial_refinement_test: acc[0] += abs(e1/e0-exact) print("Time %2.5f Norms %2.16e %2.16e %2.16e %2.16e %2.16e" %(params.t, e1/e0, exact, e1/e0-exact, sqrt(e2), e3))
def update(context): global im1, im2, im3, flux c = context params = config.params solver = config.solver X, U, U_hat = c.X, c.U, c.U_hat #if params.tstep == 1: reset_profile(profile) # Dynamically adjust flux if params.tstep % 1 == 0: U[1] = c.FST.backward(U_hat[1], U[1]) beta[0] = dx(U[1], c.FST) #solver.comm.Bcast(beta) q = (flux[0] - beta[0]) # array(params.L).prod() #U_tmp = c.work[(U[0], 0)] #F_tmp = c.work[(U_hat[0], 0)] #U_tmp[:] = beta[0] #F_tmp = c.FST.forward(U_tmp, F_tmp, c.ST) #U_hat[1] += q/beta[0]*U_hat[1] if solver.rank == 0: #d0 = zeros(U_hat.shape[1], dtype=U_hat.dtype) #d1 = zeros(U_hat.shape[1], dtype=U_hat.dtype) #d0 = c.mat.ADD.matvec(U_hat[1,:,0,0], d0) #d1 = c.mat.BDD.matvec(U_hat[1,:,0,0], d1) #c.Sk[1,0,0,0] -= (flux[0]/beta[0]-1)/params.dt*(-params.nu*params.dt/2.*d0[0] + d1[0])*0.025 c.Sk[1, 0, 0, 0] -= (flux[0]/beta[0]-1)*0.1 #c.Source[1] -= q/array(params.L).prod() #c.Sk[1] = c.FST.scalar_product(c.Source[1], c.Sk[1], c.ST) #if params.tstep % 1 == 0: #U[1] = c.FST.backward(U_hat[1], U[1], c.ST) #beta[0] = c.FST.dx(U[1], c.ST.quad) ##beta[0] = (flux[0] - beta[0])/(array(params.L).prod()) #solver.comm.Bcast(beta) #q = flux[0]/beta[0]-1 ##U[1] += beta[0]*U[1] ##U_hat[1] = c.FST.forward(U[1], U_hat[1], c.ST) #U_hat[1] += q*U_hat[1] #c.Source[1] -= beta[0]*q/(array(params.L).prod())/params.dt/2 #c.Sk[1] = c.FST.scalar_product(c.Source[1], c.Sk[1], c.ST) #utau = config.params.Re_tau * config.params.nu #Source[:] = 0 #Source[1] = -utau**2 #Source[:] += 0.05*random.randn(*U.shape) #for i in range(3): #Sk[i] = FST.scalar_product(Source[i], Sk[i], ST) if (params.tstep % params.compute_energy == 0 or params.tstep % params.plot_result == 0 and params.plot_result > 0 or params.tstep % params.sample_stats == 0): U = solver.get_velocity(**c) if params.tstep % params.print_energy0 == 0 and solver.rank == 0: print((c.U_hat[0].real*c.U_hat[0].real).mean(axis=(0, 2))) print((c.U_hat[0].real*c.U_hat[0].real).mean(axis=(0, 1))) #print(params.tstep, (c.U_hat[0]*c.U_hat[0]).mean()/4096**2, (c.U[0]*c.U[0]).mean()) #print(params.tstep, (c.U_hat[1]*c.U_hat[1]).mean()/4096**2, (c.U[1]*c.U[1]).mean()) #print(params.tstep, (c.U_hat[2]*c.U_hat[2]).mean()/4096**2, (c.U[2]*c.U[2]).mean()) if params.tstep == 1 and solver.rank == 0 and params.plot_result > 0: # Initialize figures plt.figure() im1 = plt.contourf(X[1][:, :, 0], X[0][:, :, 0], U[0, :, :, 0], 100) plt.colorbar(im1) plt.draw() plt.figure() im2 = plt.contourf(X[1][:, :, 0], X[0][:, :, 0], U[1, :, :, 0], 100) plt.colorbar(im2) plt.draw() plt.figure() im3 = plt.contourf(X[2][:, 0, :], X[0][:, 0, :], U[0, :, 0, :], 100) plt.colorbar(im3) plt.draw() plt.pause(1e-6) if params.tstep % params.plot_result == 0 and solver.rank == 0 and params.plot_result > 0: im1.ax.clear() im1.ax.contourf(X[1][:, :, 0], X[0][:, :, 0], U[0, :, :, 0], 100) im1.autoscale() plt.figure(1) plt.pause(1e-6) im2.ax.clear() im2.ax.contourf(X[1][:, :, 0], X[0][:, :, 0], U[1, :, :, 0], 100) im2.autoscale() plt.figure(2) plt.pause(1e-6) im3.ax.clear() #im3.ax.contourf(X[1, :,:,0], X[0, :,:,0], P[:, :, 0], 100) im3.ax.contourf(X[2][:, 0, :], X[0][:, 0, :], U[0, :, 0, :], 100) im3.autoscale() plt.figure(3) plt.pause(1e-6) if params.tstep % params.compute_energy == 0: e0 = dx(U[0]*U[0], c.FST) e1 = dx(U[1]*U[1], c.FST) e2 = dx(U[2]*U[2], c.FST) q = dx(U[1], c.FST) div_u = solver.get_divergence(**c) e3 = dx(div_u**2, c.FST) if solver.rank == 0: print("Time %2.5f Energy %2.8e %2.8e %2.8e Flux %2.6e Q %2.6e %2.6e %2.6e" %(config.params.t, e0, e1, e2, q, e0+e1+e2, e3, flux[0]/beta[0]-1)) if params.tstep % params.sample_stats == 0: solver.stats(U)
def update(context): global im1, im2, im3, flux c = context params = config.params solver = config.solver X, U, U_hat = c.X, c.U, c.U_hat #if params.tstep == 1: reset_profile(profile) # Dynamically adjust flux if params.tstep % 1 == 0: U[1] = c.FST.backward(U_hat[1], U[1]) beta[0] = dx(U[1], c.FST) #solver.comm.Bcast(beta) q = (flux[0] - beta[0]) if solver.rank == 0: #c.Sk[1, 0, 0, 0] -= (flux[0]/beta[0]-1)*0.05 c.U_hat[1, 0, 0, 0] += q / (np.array(params.L).prod() * 4. / 3.) if (params.tstep % params.compute_energy == 0 or params.tstep % params.plot_result == 0 and params.plot_result > 0 or params.tstep % params.sample_stats == 0): U = solver.get_velocity(**c) if params.tstep % params.print_energy0 == 0 and solver.rank == 0: print(abs(c.U_hat[0]).mean(axis=(0, 2))) print(abs(c.U_hat[0]).mean(axis=(0, 1))) if params.tstep == 1 and solver.rank == 0 and params.plot_result > 0: # Initialize figures plt.figure() im1 = plt.contourf(X[1][:, :, 0], X[0][:, :, 0], U[0, :, :, 0], 100) plt.colorbar(im1) plt.draw() plt.figure() im2 = plt.contourf(X[1][:, :, 0], X[0][:, :, 0], U[1, :, :, 0], 100) plt.colorbar(im2) plt.draw() plt.figure() im3 = plt.contourf(X[2][:, 0, :], X[0][:, 0, :], U[0, :, 0, :], 100) plt.colorbar(im3) plt.draw() plt.pause(1e-6) if params.tstep % params.plot_result == 0 and solver.rank == 0 and params.plot_result > 0: im1.ax.clear() im1.ax.contourf(X[1][:, :, 0], X[0][:, :, 0], U[0, :, :, 0], 100) im1.autoscale() plt.figure(1) plt.pause(1e-6) im2.ax.clear() im2.ax.contourf(X[1][:, :, 0], X[0][:, :, 0], U[1, :, :, 0], 100) im2.autoscale() plt.figure(2) plt.pause(1e-6) im3.ax.clear() #im3.ax.contourf(X[1, :,:,0], X[0, :,:,0], P[:, :, 0], 100) im3.ax.contourf(X[2][:, 0, :], X[0][:, 0, :], U[0, :, 0, :], 100) im3.autoscale() plt.figure(3) plt.pause(1e-6) if params.tstep % params.compute_energy == 0: e0 = dx(U[0] * U[0], c.FST) e1 = dx(U[1] * U[1], c.FST) e2 = dx(U[2] * U[2], c.FST) q = dx(U[1], c.FST) div_u = solver.get_divergence(**c) e3 = dx(div_u**2, c.FST) if solver.rank == 0: print( "Time %2.5f Energy %2.8e %2.8e %2.8e Flux %2.6e Q %2.6e %2.6e %2.6e" % (config.params.t, e0, e1, e2, q, e0 + e1 + e2, e3, flux[0] / beta[0] - 1)) if params.tstep % params.sample_stats == 0: solver.stats(U)
def initialize(solver, context): # Initialize with pertubation ala perturbU (https://github.com/wyldckat/perturbU) for openfoam U = context.U X = context.X U_hat = context.U_hat params = config.params Y = np.where(X[0] < 0, 1+X[0], 1-X[0]) utau = params.nu*params.Re_tau Um = 46.9091*utau # For Re_tau=180 #Um = 56.*utau Xplus = Y*params.Re_tau Yplus = X[1]*params.Re_tau Zplus = X[2]*params.Re_tau duplus = Um*0.2/utau #Um*0.25/utau alfaplus = params.L[1]/200. # May have to adjust these two for different Re betaplus = params.L[2]/100. # sigma = 0.00055 # 0.00055 epsilon = Um/200. #Um/200. U[:] = 0 U[1] = Um*(Y-0.5*Y**2) dev = 1+0.0000001*np.random.randn(Y.shape[0], Y.shape[1], Y.shape[2]) #dev = np.fromfile('dev.dat').reshape((64, 64, 64)) dd = utau*duplus/2.0*Xplus/40.*np.exp(-sigma*Xplus**2+0.5)*np.cos(betaplus*Zplus)*dev[:, slice(0, 1), :] U[1] += dd U[2] += epsilon*np.sin(alfaplus*Yplus)*Xplus*np.exp(-sigma*Xplus**2)*dev[:, :, slice(0, 1)] U_hat = U.forward(U_hat) U = U_hat.backward(U) U_hat = U.forward(U_hat) if "KMM" in params.solver: context.g[:] = 1j*context.K[1]*U_hat[2] - 1j*context.K[2]*U_hat[1] # Set the flux #flux[0] = context.FST.dx(U[1], context.ST.quad) #solver.comm.Bcast(flux) # project to zero divergence div_u = solver.get_divergence(**context) print('div0 ', dx(div_u**2, context.FST)) u = TrialFunction(context.FST) v = TestFunction(context.FST) A = inner(v, div(grad(u))) b = inner(v, div(U_hat)) from shenfun.chebyshev.la import Helmholtz sol = Helmholtz(*A) phi = Function(context.FST) phi = sol(phi, b) U_hat -= project(grad(phi), context.VFS) U = U_hat.backward(U) div_u = solver.get_divergence(**context) print('div1 ', dx(div_u**2, context.FST)) if solver.rank == 0: print("Flux {}".format(flux[0])) if not 'KMM' in params.solver: P_hat = solver.compute_pressure(**context) P = P_hat.backward(context.P) if not 'RK3' in params.solver: context.U_hat0[:] = context.U_hat[:] context.H_hat1[:] = solver.get_convection(**context)