Test of convergence in mesh spacing

The same prob­lem is solved with de­creased mesh spac­ing. Con­ver­gence to true so­lu­tion is shown.

%matplotlib inline
import matplotlib.pylab as plt
from oedes import *
from oedes.fvm import mesh1d
params = {
    'T': 300.,
    'electrode0.voltage': 1.,
    'electrode1.voltage': 0,
    'electrode0.workfunction': 0.,
    'electrode1.workfunction': 0,
    'hole.mu': 1e-9,
    'hole.electrode0': 1e27,
    'hole.electrode1': 1e27,
    'epsilon_r': 3,
    'hole.energy': 0,
    'hole.N0': 1e27
}
def test_scl(mesh, params, plot=False, plot_args=(), plot_kwargs={}):
    b = models.BaseModel()
    models.electronic_device(b, mesh, 'p')
    b.species[0].bc = [models.DirichletFromParams(
        boundary) for boundary in mesh.boundaries]
    b.setUp()
    x = solve(b, b.X, params, maxiter=30)
    out = b.output(0., x, 0. * x, params)
    if plot:
        plt.plot(mesh.cells['center'], out['hole.c'],
                 *plot_args, **plot_kwargs)
        plt.yscale('log')
    return out['J']
L = 100e-9
lgn = np.linspace(1, 16, 12)
result = []
test_scl(
    mesh1d(L),
    params,
    plot=True,
    plot_args=('o'),
    plot_kwargs=dict(
        label='default meshing'))
for n in 2**lgn:
    j = test_scl(
        mesh1d(np.linspace(0, L, int(n + 1))),
        params,
        plot=True,
        plot_args=('-', ))
    result.append((n, j))
    print(n, j)
plt.legend(loc='upper center')
data = np.asarray(result)
testing.nb_store_array(data)
plt.plot(data[:, 0], data[:, 1])
plt.xlabel('Number of mesh points')
plt.ylabel('J estimate')
plt.xscale('log')
h = L / (data[:, 0] + 1)
j = data[:, 1]
jtrue = j[-1]
relative_error = ((j - jtrue)[:-1]) / jtrue
h = h[:-1]
ideal_error = h**2.
# match at the smallest point
ideal_error *= relative_error[-1] / ideal_error[-1]
plt.plot(h, relative_error, label='actual')
plt.plot(h, ideal_error, '--', label='second order $O(h^2)$')
plt.xscale('log')
plt.yscale('log')
plt.xlabel('Mesh spacing h [m]')
plt.ylabel('Relative error $|\Delta j|/j$')
plt.legend(loc=0, frameon=False)