def test_diffusion_equation(resolution, alpha, spatial_order): grid_x = field.UniformNonPeriodicGrid(resolution, (0, 2 * np.pi)) grid_y = field.UniformPeriodicGrid(resolution, 2 * np.pi) domain = field.Domain([grid_x, grid_y]) x, y = domain.values() c = field.Field(domain) X = field.FieldSystem([c]) D = 1 r = np.sqrt((x - 3 * np.pi / 4)**2 + (y - np.pi / 2)**2) IC = np.exp(-r**2 * 16) c.data[:] = IC diff = equations.DiffusionBC(X, D, spatial_order) dt = alpha * grid_y.dx while diff.t < 3 * np.pi / 4 - 1e-5: diff.step(dt) try: c_target = np.loadtxt('c_%i.dat' % resolution) except: c_target = np.loadtxt('answers3/c_%i.dat' % resolution) error = np.max(np.abs(c.data - c_target)) error_est = error_diffusion[(resolution, alpha, spatial_order)] assert error < error_est
def test_reaction_diffusion(resolution, alpha): grid_x = field.UniformPeriodicGrid(resolution, 20) grid_y = field.UniformPeriodicGrid(resolution, 20) domain = field.Domain((grid_x, grid_y)) x, y = domain.values() IC = np.exp(-(x+(y-10)**2-14)**2/8)*np.exp(-((x-10)**2+(y-10)**2)/10) c = field.Field(domain) X = field.FieldSystem([c]) c.data[:] = IC D = 1e-2 dcdx2 = spatial.FiniteDifferenceUniformGrid(2, 8, c, 0) dcdy2 = spatial.FiniteDifferenceUniformGrid(2, 8, c, 1) rd_problem = equations.ReactionDiffusion2D(X, D, dcdx2, dcdy2) dt = alpha*grid_x.dx while rd_problem.t < 1-1e-5: rd_problem.step(dt) try: solution = np.loadtxt('c_%i.dat' % resolution) except: solution = np.loadtxt('answers2/c_%i.dat' % resolution) error = np.max(np.abs(solution - c.data)) error_est = error_RD[(resolution,alpha)] assert error < error_est
def test_vb(): resolution=200 alpha=0.25 grid_x = field.UniformPeriodicGrid(resolution, 20) grid_y = field.UniformPeriodicGrid(resolution, 20) domain = field.Domain((grid_x, grid_y)) x, y = domain.values() IC = np.exp(-(x+(y-10)**2-14)**2/8)*np.exp(-((x-10)**2+(y-10)**2)/10) u = field.Field(domain) v = field.Field(domain) X = field.FieldSystem([u,v]) u.data[:] = IC v.data[:] = IC nu = 1e-2 error2 = np.max(np.abs(v.data)) vb_problem = equations.ViscousBurgers2D(X,nu,8) dt = alpha*grid_x.dx while vb_problem.t < 1-1e-5: vb_problem.step(dt) error1 = np.max(np.abs(u.data)) assert (error1,error2)==(0,0)
def test_wave(): resolution = 100 spatial_order = 2 grid_x = field.UniformNonPeriodicGrid(resolution, (0, 2 * np.pi)) grid_y = field.UniformPeriodicGrid(resolution, 2 * np.pi) domain = field.Domain([grid_x, grid_y]) x, y = domain.values() xm, ym = domain.plotting_arrays() ux = field.Field(domain) uy = field.Field(domain) p = field.Field(domain) X = field.FieldSystem([ux, uy, p]) r = np.sqrt((x - 3 * np.pi / 4)**2 + (y - np.pi / 2)**2) IC = np.exp(-r**2 * 16) p.data[:] = IC ux.data[:] = 0.01 uy.data[:] = 0.01 wave = equations.Wave2DBC(X, spatial_order) ts = timesteppers.PredictorCorrector(wave) alpha = 0.1 dt = grid_x.dx * alpha fig = plt.figure(figsize=(4, 3)) ax = fig.add_subplot(111) pcm = ax.pcolormesh(xm, ym, p.data) ax.set_aspect(1) fig.colorbar(pcm) ax.set_xlabel('x') ax.set_ylabel('y') fig.canvas.draw() while ts.t < 2 * np.pi: ts.step(dt) if ts.iter % 50 == 0: pcm.set_array(np.ravel(p.data)) fig.canvas.draw() fig.canvas.show() assert ts.t > 1
def test_vb_error(): n=6 k=2 # offset alpha_list=[1/(2**(i-k)) for i in range(n)] big=[] for alpha in alpha_list: resolution=200 grid_x = field.UniformPeriodicGrid(resolution, 20) grid_y = field.UniformPeriodicGrid(resolution, 20) domain = field.Domain((grid_x, grid_y)) x, y = domain.values() IC = np.exp(-(x+(y-10)**2-14)**2/8)*np.exp(-((x-10)**2+(y-10)**2)/10) u = field.Field(domain) v = field.Field(domain) X = field.FieldSystem([u,v]) u.data[:] = IC v.data[:] = IC nu = 1e-2 vb_problem = equations.ViscousBurgers2D(X,nu,8) dt = alpha*grid_x.dx while vb_problem.t < 1-1e-5: vb_problem.step(dt) error1 = np.max(np.abs(u.data)) big.append(error1) error=[np.abs(big[i]-big[-1]) for i in range(n)] errordiv=[error[i]/error[i+1] for i in range(n-2)] print(error) print(errordiv) assert min(errordiv)>4