def test_quadratic():
"""
Check the scalar and vectorized versions work for
a quadratic u(x,t)=x(L-x)(1+t/2) that is exactly reproduced.
"""
# The following function must work for x as array or scalar
exact_solution = lambda x, t: x*(L - x)*(1 + 0.5*t)
I = lambda x: exact_solution(x, 0)
V = lambda x: 0.5*exact_solution(x, 0)
# f is a scalar (zeros_like(x) works for scalar x too)
f = lambda x, t: zeros_like(x) + 2*c**2*(1 + 0.5*t)
L = 2.5
c = 1.5
Nx = 3 # Very coarse mesh
C = 1
T = 18 # Long time integration
def assert_no_error(u, x, t, n):
u_e = exact_solution(x, t[n])
diff = abs(u - u_e).max()
nt.assert_almost_equal(diff, 0, places=13)
solver(I, V, f, c, L, Nx, C, T,
user_action=assert_no_error, version='scalar')
solver(I, V, f, c, L, Nx, C, T,
user_action=assert_no_error, version='vectorized')
Note: