Buckets:
| """ | |
| Empirical estimation of the truncation error in a scheme | |
| for a problem with one independent variable. | |
| """ | |
| import numpy as np | |
| import scitools.std as plt | |
| def estimate(truncation_error, T, N_0, m, makeplot=True): | |
| """ | |
| Compute the truncation error in a problem with one independent | |
| variable, using m meshes, and estimate the convergence | |
| rate of the truncation error. | |
| The user-supplied function truncation_error(dt, N) computes | |
| the truncation error on a uniform mesh with N intervals of | |
| length dt:: | |
| R, t, R_a = truncation_error(dt, N) | |
| where R holds the truncation error at points in the array t, | |
| and R_a are the corresponding theoretical truncation error | |
| values (None if not available). | |
| The truncation_error function is run on a series of meshes | |
| with 2**i*N_0 intervals, i=0,1,...,m-1. | |
| The values of R and R_a are restricted to the coarsest mesh. | |
| and based on these data, the convergence rate of R (pointwise) | |
| and time-integrated R can be estimated empirically. | |
| """ | |
| N = [2**i*N_0 for i in range(m)] | |
| R_I = np.zeros(m) # time-integrated R values on various meshes | |
| R = [None]*m # time series of R restricted to coarsest mesh | |
| R_a = [None]*m # time series of R_a restricted to coarsest mesh | |
| dt = np.zeros(m) | |
| legends_R = []; legends_R_a = [] # all legends of curves | |
| for i in range(m): | |
| dt[i] = T/float(N[i]) | |
| R[i], t, R_a[i] = truncation_error(dt[i], N[i]) | |
| R_I[i] = np.sqrt(dt[i]*np.sum(R[i]**2)) | |
| if i == 0: | |
| t_coarse = t # the coarsest mesh | |
| stride = N[i]/N_0 | |
| R[i] = R[i][::stride] # restrict to coarsest mesh | |
| R_a[i] = R_a[i][::stride] | |
| if makeplot: | |
| plt.figure(1) | |
| plt.plot(t_coarse, R[i], log='y') | |
| legends_R.append('N=%d' % N[i]) | |
| plt.hold('on') | |
| plt.figure(2) | |
| plt.plot(t_coarse, R_a[i] - R[i], log='y') | |
| plt.hold('on') | |
| legends_R_a.append('N=%d' % N[i]) | |
| if makeplot: | |
| plt.figure(1) | |
| plt.xlabel('time') | |
| plt.ylabel('pointwise truncation error') | |
| plt.legend(legends_R) | |
| plt.savefig('R_series.png') | |
| plt.savefig('R_series.pdf') | |
| plt.figure(2) | |
| plt.xlabel('time') | |
| plt.ylabel('pointwise error in estimated truncation error') | |
| plt.legend(legends_R_a) | |
| plt.savefig('R_error.png') | |
| plt.savefig('R_error.pdf') | |
| # Convergence rates | |
| r_R_I = convergence_rates(dt, R_I) | |
| print 'R integrated in time; r:', | |
| print ' '.join(['%.1f' % r for r in r_R_I]) | |
| R = np.array(R) # two-dim. numpy array | |
| r_R = [convergence_rates(dt, R[:,n])[-1] | |
| for n in range(len(t_coarse))] | |
| # Plot convergence rates | |
| if makeplot: | |
| plt.figure() | |
| plt.plot(t_coarse, r_R) | |
| plt.xlabel('time') | |
| plt.ylabel('r') | |
| plt.axis([t_coarse[0], t_coarse[-1], 0, 2.5]) | |
| plt.title('Pointwise rate $r$ in truncation error $\sim\Delta t^r$') | |
| plt.savefig('R_rate_series.png') | |
| plt.savefig('R_rate_series.pdf') | |
| def convergence_rates(h, E): | |
| """ | |
| Given a sequence of discretization parameters in the list h, | |
| and corresponding errors in the list E, | |
| compute the convergence rate of two successive (h[i], E[i]) | |
| and (h[i+1],E[i+1]) experiments, assuming the model E=C*h^r | |
| (for small enough h). | |
| """ | |
| from math import log | |
| r = [log(E[i]/E[i-1])/log(h[i]/h[i-1]) | |
| for i in range(1, len(h))] | |
| return r | |
Xet Storage Details
- Size:
- 3.55 kB
- Xet hash:
- ba2a1a4d77069c75a5a776ed25b0a272625143a44101bd1b720531cb630d41d3
·
Xet efficiently stores files, intelligently splitting them into unique chunks and accelerating uploads and downloads. More info.