def simulate(which, boundary, delta_t): ''' wrapper function for one plot. plots the different times in the so-called array for the scheme ´which´ and boundary ´boundary´. The time step has the size ´delta_t´. ''' times = np.array([0.01, 0.04, 0.07]) how_many_times = np.asarray(times / delta_t, dtype=int) xnum = 51 CFL = delta_t * (xnum + 1)**2 fig = plt.figure(figsize=(8, 5)) for i in range(3): ival = get_ival(how_many_times[i], delta_t, xnum, boundary) obj = simulation(ival, diffusionTable, which) obj.simulate(ival.DiffFunc, 1.0) plt.plot(ival.xvec, obj.result, '-o', fillstyle='none', label='$t=%.2f$, numerical' % times[i]) plt.plot(ival.xvec, diffusionExact[boundary](ival.xvec, ival.N_T * ival.DELTA_T, 1.0), '-', label='$t=%.2f$, exact' % times[i]) plt.xlabel('$x$') plt.ylabel('$u / [\\mathrm{a.u.}]$') plt.title('%s, %s boundary, $\\mathsf{CFL} = %.3f$' % \ (translationTitle[which],boundary,CFL)) plt.grid() plt.legend() plt.tight_layout() return fig
def error_plot(dtvec, dxvec, which, tmax): ''' helper function to create the 3d-plot that is made for the error analysis. ''' dt, dx = np.meshgrid(dtvec, dxvec) res = np.zeros_like(dt) for i, DT in enumerate(dtvec): for j, DX in enumerate(dxvec): ival = initialValues( [None, 1.0, int(1 / DX), int(tmax / DT), DT, 'periodic']) obj = simulation(ival, hopfTable, which) obj.simulate(10 * ival.HopfFunc, 1.0) res[i,j] = np.sqrt(np.sum(( 10*advection_exact_periodic(ival.xvec,ival.N_T*ival.DELTA_T,1.0)- \ obj.result)**2)) return dt, dx, res
def error_plot(dtvec, dxvec, which, tmax): ''' helper function to create the 3d-plot that is made for the error analysis. Logarithmic scaling of all axes. ''' dt, dx = np.meshgrid(dtvec, dxvec) res = np.zeros_like(dt) for i, DT in enumerate(dtvec): for j, DX in enumerate(dxvec): ival = initialValues( [None, 1.0, int(1 / DX), int(tmax / DT), DT, 'reflecting']) obj = simulation(ival, diffusionTable, which) obj.simulate(ival.DiffFunc, 1.0) res[i,j] = np.sqrt(np.sum(( diffusionExact['reflecting'](ival.xvec,ival.N_T*ival.DELTA_T,1.0)- \ obj.result)**2)) return np.log10(dt), np.log10(dx), np.log(res)
def make_sim(nmax): ival.DiffStep = fourier_rect(ival.xvec,h=0.5,N=nmax) + \ ran_profile(ival.xvec,h=1.0,N=nmax) + 0.5 nfive = simulation(ival, diffusionTable, 'cr_n_D') nfive.simulate(u_init, 1.0) return nfive.result
ax.set_ylabel('$\\mathrm{log}(\\mathsf{TE})$') ax.set_title( 'Truncation Error ($\\mathsf{TE}$) for the explicit {\\scshape Euler}-scheme; $\\mathsf{CLF} = 1/6$' ) plt.grid() plt.tight_layout() fig_eu_e_16.savefig(prefix + 'eu_e_ERRORS16.pdf') T = 1000 eps = 5e-3 DT = 1e-6 ival = initialValues([None, 1.0, 701, T, DT, 'periodic'], eps=eps) u_init = eps / np.pi / ((ival.xvec - 0.5)**2 + eps**2) # general check for the cr_n_Dfun-simulation. if False: cnsd = simulation(ival, diffusionTable, 'cr_n_D') cnsd.simulate(eps / np.pi / ((ival.xvec - 0.5)**2 + eps**2), 1.0) cnsd.plot_init(plot_default=False) plt.plot(ival.xvec, cnsd.result, label='simulated') plt.plot(ival.xvec, exact_dfun(ival.xvec, T * DT, ival.DiffStep, x0=0.5), label='exact') plt.grid() plt.xlabel('$x$') plt.ylabel('$u(x,t)/\\mathrm{[a.u.]}$') plt.title('Step profile, mathematical comparison') plt.legend() plt.tight_layout() cnsd.fig.savefig(prefix + 'cr_n_D_COMPARE.pdf') from helpers import fourier_rect, many_steps, ran_profile
'lax_w': lax_wendroff, 'lax_w_h': lax_wendroff_hopfeq } if False: # dummy if, only used to not execute this part which was used to generate # the plots for the hopf equation a = 0.1 dt = 0.001 maxslope_anti = np.sqrt(np.exp(1)) * a my_T = (int(maxslope_anti / dt) - 1) * dt ival = initialValues( [None, 1.0, 301, int(maxslope_anti / dt) - 1, dt, 'periodic']) up = simulation(ival, hopfTable, 'lax_w_h') u_i = +np.exp(-(ival.xvec - 0.5)**2 / 2 / a**2) + 1 up.simulate(u_i, 1.0) up.plot_init('$t=0$') up.plot_final('$t=%.3f$' % my_T, 'Breakdown: $a = %.3f$' % a) #up.fig.savefig('hopfBreakdown2.pdf') plt.show() # some human-readable output definitely is nice. schemeNames = { 'up': 'upwind scheme', 'down': 'downwind scheme', 'ex_c': 'explicit centered scheme', 'im_c': 'implicit centered scheme', 'lax_f': '{\scshape Lax Friedrichs} scheme', 'lax_w': '{\scshape Lax Wendroff} scheme'