for i in range(1, n + 1): plt.plot(ss, dV(i, ss)) plt.plot(xx, 0 * xx, '.k') # Assemble matrices: start = time.time() K = np.zeros([n, n]) M = np.zeros([n, n]) for i in range(1, n + 1): for j in range(1, n + 1): if (np.abs(i - j) <= 1): for l in range(n + 1): if (np.abs(l + 1 - min(i, j)) <= 1 and max(i, j) <= l + 1): dom = [xx[l], xx[l + 1]] N = 1000 x = chebpts(N, dom) # quadrature grid w = quadwts(N, dom) K[i - 1, j - 1] += w @ (dV(i, x) * dV(j, x)) M[i - 1, j - 1] += w @ (V(i, x) * V(j, x)) K = csr_matrix(K) M = a * csr_matrix(M) L = K + M # Assemble RHS: F = np.zeros(n) for i in range(1, n + 1): for l in range(n + 1): dom = [xx[l], xx[l + 1]] x = chebpts(N, dom) w = quadwts(N, dom) F[i - 1] += w @ (f(x) * V(i, x))
# a1(x) = -(2*x^2 + 1), # a2(x) = x, # f(x) = 0, # # and u(x0) = c, a <= x0 <= b, # u(x1) = d, a <= x1 <= b. # # Exact solution is a linear combination of scaled Bessel functions J1 and Y1. # Domain: a = 1.1 b = 2.6 # Grid: n = 400 x = chebpts(n, [a, b]) # Boundary conditions: x0 = a x1 = b c = 4.4 d = 1.2 # Variable coefficients: a0 = lambda x: 4 * x**3 * np.exp(x**2) a1 = lambda x: -(2 * x**2 + 1) a2 = lambda x: x # Right-hand side: f = lambda x: 0 * x
import numpy as np import time # Chebpy imports: from chebpy.cheb import chebpts, coeffs2vals, multmat, spconvert, vals2coeffs # %% Multiplication g = f*h on [-1,1]. # Functions: f = lambda x: np.cos(x) * np.exp(x) h = lambda x: np.sin(x) * x**2 gex = lambda x: f(x) * h(x) # Grid: n = 10000 x = chebpts(n) # Compute coeffs of f: F = vals2coeffs(f(x)) # Multiplication by h matrix in coefficient space: start = time.time() M = multmat(n, h) end = time.time() print(f'Time (setup): {end-start:.5f}s') plt.figure() plt.spy(M) # Multiply: start = time.time() G = M @ F
Copyright 2020 by Hadrien Montanelli. """ # %% Imports. # Standard library imports: import numpy as np # Chebpy imports: from chebpy.cheb import chebpts, coeffs2vals, vals2coeffs # %% Transforms (1D) on [-1,1]. f = lambda x: np.exp(-10 * x**2) n = 100 x = chebpts(n) error = coeffs2vals(vals2coeffs(f(x))) - f(x) print(f'Error (1D): {np.max(np.abs(error)):.2e}') # %% Transforms (1D) on [0,2]. f = lambda x: np.exp(-10 * x**2) n = 100 x = chebpts(n, [0, 2]) error = coeffs2vals(vals2coeffs(f(x))) - f(x) print(f'Error (1D): {np.max(np.abs(error)):.2e}') # %% Transforms (2D) on [-1,1]x[-1,1]. f = lambda x, y: np.exp(-10 * (x**2 + y**2)) n = 100
f = lambda r, z: -f0 * r**3 - f0 * R0**2 * r # Exact solution: scl = f0 * a**2 * R0**2 / 2 uex = lambda r, z: scl * (1 - z**2 / a**2 - ((r - R0) / a + (r - R0)**2 / (2 * a * R0))**2) # Boundary condtions: g1 = lambda z: uex(r0, z) # u(r0, z) = g1(z) g2 = lambda z: uex(r1, z) # u(r1, z) = g2(z) h1 = lambda r: uex(r, z0) # u(r, z0) = h1(r) h2 = lambda r: uex(r, z1) # u(r, z1) = h2(r) # Grid points: n = 100 r = chebpts(n, [ra, rb]) z = chebpts(n, [za, zb]) R, Z = np.meshgrid(r, z) # Assemble differentiation matrices: start = time.time() S0 = spconvert(n, 0) S1 = spconvert(n, 1) D1r = diffmat(n, 1, [ra, rb]) D2r = diffmat(n, 2, [ra, rb]) D2z = diffmat(n, 2, [za, zb]) M0 = multmat(n, lambda r: r, [ra, rb], 0) M2 = multmat(n, lambda r: r, [ra, rb], 2) A1 = S1 @ S0 C1 = M2 @ D2r - S1 @ D1r A2 = D2z
f = lambda x, y: 0 * x + 0 * y # Exact solution: w = 14.1 K = np.sqrt(2) * w uex = lambda x, y: np.sin(w * x) * np.sin(w * y) # Boundary condtions: g1 = lambda y: uex(-1, y) # u(-1, y) = g1(y) g2 = lambda y: uex(+1, y) # u(+1, y) = g2(y) h1 = lambda x: uex(x, -1) # u(x, -1) = h1(x) h2 = lambda x: uex(x, +1) # u(x, +1) = h2(x) # Grid points: n = 100 x = chebpts(n) y = chebpts(n) X, Y = np.meshgrid(x, y) # Assemble differentiation matrices: start = time.time() S0 = spconvert(n, 0) S1 = spconvert(n, 1) A1 = S1 @ S0 C1 = diffmat(n, 2) + K**2 * S1 @ S0 A2 = diffmat(n, 2) C2 = S1 @ S0 # Assemble boundary conditions: Bx = np.zeros([2, n]) By = np.zeros([2, n])
import time # Chebpy imports: from chebpy.cheb import chebpts, diffmat, spconvert, vals2coeffs # %% First-order differentiation on [-1, 1]. # Function: w = 100 f = lambda x: np.cos(w * x) dfex = lambda x: -w * np.sin(w * x) # Grid: n = 4 * w dom = [-1, 1] x = chebpts(n, dom) # Compute coeffs of f: F = vals2coeffs(f(x)) # Differentiation matrix in coefficient space: start = time.time() D = diffmat(n, 1, dom) end = time.time() print(f'Time (setup): {end-start:.5f}s') plt.figure() plt.spy(D) # Differentiate: start = time.time() DF = D @ F
# -*- coding: utf-8 -*- """ Created on Fri Dec 4 16:56:16 2020 Copyright 2020 by Hadrien Montanelli. """ # %% Imports. # Standard library imports: import numpy as np # Chebpy imports: from chebpy.cheb import chebpts, feval, vals2coeffs # %% Evaluate f(x) = cos(x)*exp(-x^2). # Function: f = lambda x: np.cos(x) * np.exp(-x**2) # Chebyshev grid: n = 30 x = chebpts(n) F = vals2coeffs(f(x)) # Evaluation grid: xx = np.linspace(-1, 1, 100) vals = feval(F, xx) # Error: error = np.max(np.abs(vals - f(xx))) / np.max(np.abs(f(xx))) print(f'Error: {error:.2e}')