def gaussian(T=12.2, version="scalar"): print "running gaussian" Lx = 10 Ly = 10 dt = 0.08 dx = 0.25 dy = 0.25 def I(x, y): """Gaussian peak at (Lx/2, Ly/2).""" return np.exp(-0.5 * (x - Lx / 2.0)**2 - 0.5 * (y - Ly / 2.0)**2) def V(x, y): return 0 def q(x, y): return 1.0 def f(x, y, t): return 0.0 b = 0 gridprm = wave2D.GridPa0rameters(Lx=Lx, Ly=Ly, dt=dt, dx=dx, dy=dy) physprm = wave2D.PhysicalParameters(b=b, I=I, V=V, q=q, f=f, gridprm=gridprm) solver = wave2D.Solver(physprm=physprm) solver.solve(T=T, version=version)
def test_constant_solution(T=440.2, value=2.33, version="vectorized"): def constant_solution(value=2.33): """ Defines an array filled with constant values. The array is build upon default mesh size defined in wave2D.GridParameters(). """ gridprm = wave2D.GridParameters() Nx = gridprm.Nx Ny = gridprm.Ny u = value * np.ones([Nx + 3, Ny + 3]) # Include ghost values return u I = lambda x, y: value V = lambda x, y: 0 q = lambda x, y: 1.0 f = lambda x, y, t: 0 b = 0 physprm = wave2D.PhysicalParameters(b=b, I=I, V=V, q=q, f=f) solver = wave2D.Solver(physprm=physprm) u = solver.solve(T=T, save_figs=False, version=version) u_e = constant_solution(value) diff = (u[1:-1, 1:-1] - u_e[1:-1, 1:-1]).max() # maximum value between the differences nt.assert_almost_equal(diff, 0, delta=1e-14) print "The constant solution passed!"
def plug_x(T=2.18, version="scalar"): print "running plug in x direction" Lx = 1 Ly = 1 dt = 0.025 dx = 0.025 dy = 0.025 I = lambda x, y: 0 if np.fabs(x - Lx / 2.0) > 0.025 else 1.0 V = lambda x, y: 0 q = lambda x, y: 1.0 f = lambda x, y, t: 0 b = 0 gridprm = wave2D.GridParameters(Lx=Lx, Ly=Ly, dt=dt, dx=dx, dy=dy) physprm = wave2D.PhysicalParameters(b=b, I=I, V=V, q=q, f=f, gridprm=gridprm) solver = wave2D.Solver(physprm=physprm) solver.bypass_stability = True solver.solve( T=T, version=version) #,save_figs=True,destination="fig",filename="plug")
def test_cubic_solution(version="vectorized"): # Lx and Ly determine stability !? Lx = 0.0008 Ly = 0.0008 dx = 0.00015 dy = 0.00015 dt = 0.00005 gridprm = wave2D.GridParameters(Lx=Lx, Ly=Ly, dx=dx, dy=dy, dt=dt) Nx = gridprm.Nx Ny = gridprm.Ny q = lambda x, y: 1.0 def cubic_solution(x, y, t): return (1 - t + 0.5 * t * t - (1 / 6.0) * t * t * t) * x * x * (x - Lx) * y * y * (Ly - y) def I(x, y): return cubic_solution(x, y, 0) def V(x, y): return -1 * I(x, y) def f(x, y, t): return (1-t)*I(x,y) - q(x,y)*((1-t+0.5*t*t-(1/6.0)*t*t*t)*(6*x-2*Lx)*y*y*(Ly-y) -\ (1-t+0.5*t*t-(1/6.0)*t*t*t)*(2*Ly-6*y)*x*x*(x-Lx)) b = 0 physprm = wave2D.PhysicalParameters(b=b, I=I, V=V, q=q, f=f, gridprm=gridprm) solver = wave2D.Solver(physprm=physprm) u_e = np.zeros([Nx + 3, Ny + 3]) x = np.linspace(0, Lx, Nx + 3) y = np.linspace(0, Lx, Ny + 3) #xv = x[:,np.newaxis] #yv = y[np.newaxis,:] T = 0.1 #u_e[:,:] = cubic_solution(xv,yv,T) u_e[1:-1, 1:-1] = cubic_solution(x[:-2], y[:-2], T) u = solver.solve(T=T, save_figs=False, version=version) u[1, 1] = u_e[1, 1] u[Nx + 1, 1] = u_e[Nx + 1, 1] u[1, Ny + 1] = u_e[1, Ny + 1] u[Nx + 1, Ny + 1] = u_e[Nx + 1, Ny + 1] diff = (u[1:-1, 1:-1] - u_e[1:-1, 1:-1]).max() # maximum value between the differences nt.assert_almost_equal(diff, 0, delta=1e-14) print "The cubic solution passed!"
def test_outletBC(): def I(x, y): return np.exp(-0.5 * (x - gridprm.Lx / 2.0)**2) gridprm = wave2D.GridParameters(Lx=20, Ly=20, dt=0.25, dx=0.55, dy=0.55) physprm = wave2D.PhysicalParameters(I=I, gridprm=gridprm) solver = wave2D.Solver(BC="openoutlet", physprm=physprm) u = solver.solve( version="scalar", T=47.2) #,save_figs=True,destination="src/fig",filename="gaussian") u_e = np.zeros([u.shape[0], u.shape[1]]) diff = (u_e[1:-1] - u[1:-1]).max() nt.assert_almost_equal(diff, 0, delta=4e-2) print "The outlet boundary condition passed! ....by our standards."
def test_manufactured_solution(): A = 1 b = 0.4 mx = 2 my = 2 Lx = np.pi Ly = np.pi dx = 0.05 dy = 0.05 dt = 0.01 kx = mx * np.pi / Lx ky = my * np.pi / Ly q = lambda x, y: 1.0 if np.fabs(x - Lx / 2.0) > 0.25 else 0.35 def w(x, y): return q(x, y) * (kx**2 + ky**2) c = lambda x, y: 1.0 if np.fabs(x - Lx / 2.0) > 0.25 else np.sqrt(0.35) def B(x, y): return c(x, y) * A / w(x, y) def f(x, y, t): # generated with sympy (with some modification) return (-A*c(x,y)**2*np.cos(kx*x)*np.cos(t*w(x,y)) +\ A*kx**2*x*np.cos(kx*x)*np.cos(t*w(x,y)) +\ A*kx*np.sin(kx*x)*np.cos(t*w(x,y)) +\ A*ky**2*x*np.cos(kx*x)*np.cos(t*w(x,y))-\ A*w(x,y)**2*np.cos(kx*x)*np.cos(t*w(x,y)) -\ B(x,y)*c(x,y)**2*np.sin(t*w(x,y))*np.cos(kx*x) +\ B(x,y)*kx**2*x*np.sin(t*w(x,y))*np.cos(kx*x) +\ B(x,y)*kx*np.sin(kx*x)*np.sin(t*w(x,y)) +\ B(x,y)*ky**2*x*np.sin(t*w(x,y))*np.cos(kx*x) -\ B(x,y)*w(x,y)**2*np.sin(t*w(x,y))*np.cos(kx*x))*\ np.exp(-c(x,y)*t)*np.cos(ky*y) -\ A*c(x,y)*np.cos(kx*x)*np.cos(ky*y) +\ B(x,y)*w(x,y)*np.cos(kx*x)*np.cos(ky*y) def I(x, y): return A * np.cos(kx * x) * np.cos(ky * y) def V(x, y): return (A * c(x, y) - B(x, y) * w(x, y)) * np.cos(kx * x) * np.cos( ky * y) def damped_standing_wave(x, y, t): return A*np.cos(kx*x)*np.cos(ky*y)*np.exp(-c(x,y)*t)*\ (np.cos(w(x,y)*t) + (c(x,y)/w(x,y))*np.sin(w(x,y)*t)) T = 12 Nx = int(round(Lx / float(dx))) Ny = int(round(Ly / float(dy))) Nt = int(round(T / float(dt))) u_e = np.zeros([Nx + 3, Ny + 3]) x = np.linspace(0, Lx, Nx + 3) y = np.linspace(0, Lx, Ny + 3) t = np.linspace(0, T, Nt + 1) u_e[1:-1, 1:-1] = damped_standing_wave(x[1:-1], y[1:-1], T) gridprm = wave2D.GridParameters(Lx=Lx, Ly=Ly, dx=dx, dy=dy, dt=dt) physprm = wave2D.PhysicalParameters(b=b, I=I, V=V, f=f, q=q, gridprm=gridprm) solver = wave2D.Solver(physprm=physprm) u = solver.solve(T=T, display=True) diff = (u_e[1:-1] - u[1:-1]).max() nt.assert_almost_equal(diff, 1, delta=1e-14) # unfinished print "this test is under construction"
def test_convergence_damped_standing_wave(display_result=False): """ For the convergence tests, we use the damped standing waves described by u_e(x,y,t) = A*np.cos(kx*x)*np.cos(ky*y)*(cos(omega*t) + (c/omega)*sin(omega*t)) where kx = mx*pi/Lx, ky = my*pi/Ly. """ A = 1 b = 0.4 mx = 2 my = 2 Lx = np.pi Ly = np.pi kx = mx * np.pi / Lx ky = my * np.pi / Ly def c(x, y): return b / 2.0 def q(x, y): return np.sqrt(c(x, y)) def omega(x, y): return q(x, y) * (kx**2 + ky**2) def damped_standing_wave(x, y, t): return A*np.cos(kx*x)*np.cos(ky*y)*np.exp(-c(x,y)*t)*\ (np.cos(omega(x,y)*t) + (c(x,y)/omega(x,y))*np.sin(omega(x,y)*t)) def I(x, y): return damped_standing_wave(x, y, 0) def V(x, y): return A * c(x, y) * (omega(x, y) - 1) * np.cos(kx * x) * np.cos( ky * y) def f(x, y, t): return 0 r = [] # will contain the convergence rates E = [] # will contain the error between numerical and exact Eh = [] T = 5.5 dt = [0.25, 0.125, 0.0625, 0.03125, 0.015625, 0.0078125] dx = [0.5, 0.25, 0.125, 0.0625, 0.03125, 0.015625] dy = [0.5, 0.25, 0.125, 0.0625, 0.03125, 0.015625] l = 0 for dt_val in dt: gridprm = wave2D.GridParameters(Lx=Lx, Ly=Ly, dx=dx[l], dy=dy[l], dt=dt_val) Nx = gridprm.Nx Ny = gridprm.Ny physprm = wave2D.PhysicalParameters(b=b, I=I, V=V, q=q, f=f, gridprm=gridprm) solver = wave2D.Solver(physprm=physprm) x = np.linspace(0, Lx, Nx + 3) y = np.linspace(0, Ly, Ny + 3) u_e = np.zeros([Nx + 3, Ny + 3]) u_e[1:-1, 1:-1] = damped_standing_wave(x[1:-1], y[1:-1], T) save_figs = False #if l==3: # save_figs = True u = solver.solve(T=T, version="vectorized") #,\ #save_figs=save_figs,destination="src/fig",filename="standingwave") E.append((dt_val**2 * dx[l] * dy[l] * np.sqrt( (u_e[1:-1, 1:-1] - solver.u[1:-1, 1:-1])**2)).sum()) Eh.append(E[l] / (dx[l] * dy[l])) # E/h^2 = C l = l + 1 for i in range(len(E) - 1): r.append(np.log(E[i] / E[i + 1]) / np.log(dt[i] / dt[i + 1])) nt.assert_almost_equal(r[-1], 2, delta=1e-1) if display_result: print "h = ", dx print "E = ", E print "E/h^2 = ", Eh print "r = ", r print "The convergence rate is %.2f." % r[-1] print "The damped standing wave passed!"