def test_4d_approx(n): t, x, y, z = sp.symbols('t x y z') t_domain = np.linspace(-1, 4, n) # Physical x_domain = np.linspace(0, 2, n) y_domain = np.linspace(0, 1, n) z_domain = np.linspace(-2, 0, n) T, X, Y, Z = np.meshgrid(t_domain, x_domain, y_domain, z_domain, indexing='ij') # I will look at 3 points aroung the center pts_indices = [(n / 2 - 1, ) * 4, (n / 2, ) * 4, (n / 2 + 1, ) * 4] # Their physical representations pts = map( lambda (i, j, k, l): [t_domain[i], x_domain[j], y_domain[k], z_domain[k]], pts_indices) symbols = (t, x, y, z) functions = [ sp.sin(sp.pi * t) * sp.exp(-(x**2 + y**3 - z**2)), sp.cos(sp.pi * t) * sp.exp(-(x**2 + 3 * y**2 + x * y * z)) ] # Some deriveatives derivs = [(0, 0, 3, 0), (1, 0, 1, 1), (0, 1, 0, 2), (2, 1, 0, 3), (1, 1, 2, 1)] numeric = np.zeros(len(pts)) errors = defaultdict(list) for foo in functions: foo_values = sp.lambdify((t, x, y, z), foo, 'numpy')(T, X, Y, Z) grid_foo = GridFunction([t_domain, x_domain, y_domain, z_domain], foo_values) for d in derivs: if d != (0, 0, 0, 0): dfoo = foo for var, deg in zip(symbols, d): dfoo = dfoo.diff(var, deg) else: dfoo = foo # True df = sp.lambdify((t, x, y, z), dfoo) exact = np.array([df(*pt) for pt in pts]) dgrid_foo = diff(grid_foo, d) [ dgrid_foo(pi, numeric[i:i + 1]) for i, pi in enumerate(pts_indices) ] error = exact - numeric e = np.linalg.norm(error) errors[foo].append(e) return errors
def test_3d_poly(n): t, x, y = sp.symbols('t x y') t_domain = np.linspace(-1, 4, n) # Physical x_domain = np.linspace(0, 2, n) y_domain = np.linspace(0, 1, n) T, X, Y = np.meshgrid(t_domain, x_domain, y_domain, indexing='ij') # I will look at 3 points aroung the center pts_indices = [(n / 2 - 1, ) * 3, (n / 2, ) * 3, (n / 2 + 1, ) * 3] # Their physical representations pts = map(lambda (i, j, k): [t_domain[i], x_domain[j], y_domain[k]], pts_indices) symbols = (t, x, y) functions = [ 2 * t + 1 * x - 2 * y, 3 * (t**2 - 2 * t + 1) + x**2 + t * x - 4 * t * y + 3 * t * x, y * (t**4 - 2 * t**2 + t - 1) + 2 * t**2 * 4 * x**3 - y * x * 4 * t**4 ] # Some deriveatives derivs = [(0, 0, 0), (1, 0, 1), (0, 1, 0), (2, 1, 0), (1, 2, 0), (3, 1, 1), (1, 3, 1)] status = True numeric = np.zeros(len(pts)) for foo in functions: print foo foo_values = sp.lambdify((t, x, y), foo, 'numpy')(T, X, Y) grid_foo = GridFunction([t_domain, x_domain, y_domain], foo_values) for d in derivs: if d != (0, 0, 0): dfoo = foo for var, deg in zip(symbols, d): dfoo = dfoo.diff(var, deg) else: dfoo = foo print '\t', d, dfoo, # True df = sp.lambdify((t, x, y), dfoo) exact = np.array([df(*pt) for pt in pts]) dgrid_foo = diff(grid_foo, d) [ dgrid_foo(pi, numeric[i:i + 1]) for i, pi in enumerate(pts_indices) ] error = exact - numeric e = np.linalg.norm(error) print 'OK' if e < TOL else (exact, numeric) status = status and e < TOL return status
def test_2d_poly(n): '''Error for some approx at [-1, 4]x[0, 2]''' t, x = sp.symbols('t x') t_domain = np.linspace(-1, 4, n) # Physical x_domain = np.linspace(0, 2, n) # Physical T, X = np.meshgrid(t_domain, x_domain, indexing='ij') # I will look at 3 points aroung the center pts_indices = [(n / 2 - 1, ) * 2, (n / 2, ) * 2, (n / 2 + 1, ) * 2] # Their physical representations pts = map(lambda (i, j): [t_domain[i], x_domain[j]], pts_indices) symbols = (t, x) functions = [ 2 * t + x, 3 * (t**2 - 2 * t + 1) + x**2, (t**4 - 2 * t**2 + t - 1) + 2 * t**2 * 4 * x**3 - x * 3 * t**2 ] # Some deriveatives derivs = [(0, 0), (1, 0), (0, 1), (1, 1), (2, 1), (3, 1), (1, 3), (2, 2)] numeric = np.zeros(len(pts)) status = True for foo in functions: print foo foo_values = sp.lambdify((t, x), foo, 'numpy')(T, X) grid_foo = GridFunction([t_domain, x_domain], foo_values) for d in derivs: if d != (0, 0): dfoo = foo for var, deg in zip(symbols, d): dfoo = dfoo.diff(var, deg) else: dfoo = foo print '\t', d, dfoo, # True df = sp.lambdify((t, x), dfoo) exact = np.array([df(*pt) for pt in pts]) dgrid_foo = diff(grid_foo, d) [ dgrid_foo(pi, numeric[i:i + 1]) for i, pi in enumerate(pts_indices) ] error = exact - numeric e = np.linalg.norm(error) print 'OK' if e < TOL else (exact, numeric) status = status and e < TOL return status
def test_2d_approx(n): '''Error for some approx at [-1, 4]x[0, 2].Just convergence''' t, x = sp.symbols('t x') t_domain = np.linspace(-1, 4, n) # Physical x_domain = np.linspace(0, 2, n) # Physical T, X = np.meshgrid(t_domain, x_domain, indexing='ij') # I will look at 3 points aroung the center pts_indices = [(n / 2 - 1, ) * 2, (n / 2, ) * 2, (n / 2 + 1, ) * 2] # Their physical representations pts = map(lambda (i, j): [t_domain[i], x_domain[j]], pts_indices) symbols = (t, x) functions = [ sp.sin(sp.pi * (t + x)), t * sp.sin(2 * sp.pi * (x - 3 * t * x)), sp.cos(2 * sp.pi * x * t) ] # Some deriveatives derivs = [(1, 0), (0, 1), (1, 1), (2, 1), (3, 1), (1, 3), (2, 2)] errors = {foo: list() for foo in functions} numeric = np.zeros(len(pts)) for foo in functions: foo_values = sp.lambdify((t, x), foo, 'numpy')(T, X) grid_foo = GridFunction([t_domain, x_domain], foo_values) for d in derivs: if d != (0, 0): dfoo = foo for var, deg in zip(symbols, d): dfoo = dfoo.diff(var, deg) else: dfoo = foo # True df = sp.lambdify((t, x), dfoo) exact = np.array([df(*pt) for pt in pts]) dgrid_foo = diff(grid_foo, d) [ dgrid_foo(pi, numeric[i:i + 1]) for i, pi in enumerate(pts_indices) ] error = exact - numeric e = np.linalg.norm(error) errors[foo].append(e) return errors
def test_1d_poly_vec(n): '''Error for some approx at [-1, 4]''' t = sp.Symbol('t') t_domain = np.linspace(-1, 4, n) # Physical t_domain_index = range(len(t_domain)) # I will look at 3 points aroung the center pts_index = [ t_domain_index[len(t_domain) / 2 - 1], t_domain_index[len(t_domain) / 2], t_domain_index[len(t_domain) / 2 + 1] ] pts = t_domain[pts_index] symbols = (t, ) functions = [2 * t, 3 * (t**2 - 2 * t + 1), (t**4 - 2 * t**2 + t - 1)] # We look at up to 3 derivatives derivs = [(0, ), (1, ), (2, ), (3, )] status = True numeric = np.zeros((len(pts), 2)) for foo in functions: print foo foo_values = np.array(map(sp.lambdify(t, foo), t_domain)) grid_foo = GridFunction([t_domain], [foo_values, 2 * foo_values]) for d in derivs: if d != (0, ): dfoo = foo for var, deg in zip(symbols, d): dfoo = dfoo.diff(var, deg) else: dfoo = foo print '\t', d, dfoo, # True df = sp.lambdify(t, dfoo) exact = df(pts) exact = np.c_[exact, 2 * exact] dgrid_foo = diff(grid_foo, d) for row, pi in zip(numeric, pts_index): np.put(row, range(2), dgrid_foo((pi, ))) error = exact - numeric e = np.linalg.norm(error) print 'OK' if e < TOL else (exact, numeric) status = status and e < TOL return status
def test_1d_approx(n, width=7): '''Error for some approx at [-1, 4]. Just convergence''' t = sp.Symbol('t') t_domain = np.linspace(-1, 4, n) # Physical t_domain_index = range(len(t_domain)) # I will look at 3 points aroung the center pts_index = [ t_domain_index[len(t_domain) / 2 - 1], t_domain_index[len(t_domain) / 2], t_domain_index[len(t_domain) / 2 + 1] ] pts = t_domain[pts_index] symbols = (t, ) functions = [2 * sp.sin(sp.pi * t), sp.cos(2 * sp.pi * t**2)] # We look at up to 3 derivatives derivs = [(1, ), (2, ), (3, ), (4, )] numeric = np.zeros(len(pts)) status = {f: list() for f in functions} for foo in functions: foo_values = np.array(map(sp.lambdify(t, foo), t_domain)) grid_foo = GridFunction([t_domain], foo_values) for d in derivs: if d != (0, ): dfoo = foo for var, deg in zip(symbols, d): dfoo = dfoo.diff(var, deg) else: dfoo = foo # True df = sp.lambdify(t, dfoo) exact = np.array(map(df, pts)) dgrid_foo = diff(grid_foo, d, width=width) numeric.ravel()[:] = [dgrid_foo((pi, )) for pi in pts_index] error = exact - numeric e = np.linalg.norm(error) status[foo].append(e) return status