def test(): from scipy.linalg import inv from time import perf_counter from ppp.Plots import plot_setup import matplotlib.pyplot as pt M = 100 times = np.empty((2, M)) for i in range(M): A = np.random.randint(10, size=(i + 2, i + 2)) b = np.random.randint(10, size=(i + 2, 1)) start = perf_counter() inv(A) @ b times[0, i] = perf_counter() - start start = perf_counter() LU = alu(A) LU.solve(b) times[1, i] = perf_counter() - start fig, ax = plot_setup('Matrix Size', 'Time [s]') lineObjects = ax.plot(np.arange(M) + 2, times.T) ax.legend(iter(lineObjects), ['LARPACK', 'LU Decomposition'], fontsize=16) pt.show()
def show_mesh2D(self): import matplotlib.pyplot as pt from ppp.Plots import plot_setup fig, ax = plot_setup('$x$', '$y$') ax.triplot(self.P[:self.Np, 0], self.P[:self.Np, 1], self.T[:, :3]) ax.plot(self.P[:, 0], self.P[:, 1], 'o') ax.plot(self.B[:, 0], self.B[:, 1], 'bx') self.draw_boundary(ax) pt.show()
def show_mesh2D(self): import matplotlib.pyplot as pt print('hi there') from ppp.Plots import plot_setup fig, ax = plot_setup('$x$', '$y$') ax.triplot(self.P[:, 0], self.P[:, 1], self.T) ax.plot(self.P[:, 0], self.P[:, 1], 'o') ax.plot(self.B[:, 0], self.B[:, 1], 'bx') pt.show()
def test(func, exact, t0, tN, y0): from ppp.Plots import plot_setup import matplotlib.pyplot as pt from time import perf_counter methods = [ 'Forward Euler', 'Heun', 'Ralston', 'RK3', 'Heun3', 'Ralston3', 'SSPRK3', 'RK4', '3/8 Rule', 'RK5' ] N_vals = 10**np.linspace(0, 5, 11) average_errs = np.empty((len(methods), len(N_vals))) times = np.copy(average_errs) for i, method_input in enumerate(methods): print(method_input) for j, N_input in enumerate(N_vals.astype(int)): start = perf_counter() ode = explicit(function, y_0, t_0, t_N, N=N_input, method=method_input) times[i, j] = perf_counter() - start err = np.abs((exact(t_N) - ode.y_vals[-1])) if type(err) == float: average_errs[i, j] = err #np.abs((exact(t_N) - ode.y_vals[-1])) else: average_errs[i, j] = np.mean(err) fig, ax = plot_setup('$N$', 'Time [s]', x_log=True, y_log=True) lineObjects = ax.plot(N_vals, times.T, 'x-') ax.legend(iter(lineObjects), methods, fontsize=16) pt.show() fig, ax = plot_setup('$N$', 'Absolute Error', x_log=True, y_log=True) lineObjects = ax.plot(N_vals, average_errs.T, 'x-') ax.legend(iter(lineObjects), methods, fontsize=16) pt.show()
def test(f, g, exact, t0, tN, y0): from ppp.Plots import plot_setup import matplotlib.pyplot as pt from time import perf_counter u0, v0 = y0 methods = ['Euler', 'Stromer--Verlet', 'Ruth3'] N_vals = 10**np.linspace(0, 5, 11) average_errs = np.empty((len(methods), len(N_vals))) times = np.copy(average_errs) for i, method_input in enumerate(methods): print(method_input) for j, N_input in enumerate(N_vals.astype(int)): start = perf_counter() ode = symplectic(f, g, u_0, v_0, t_0, t_N, N=N_input, method=method_input) times[i, j] = perf_counter() - start u_exact, v_exact = exact(t_N) u_approx, v_approx = ode.u_vals[-1], ode.v_vals[-1] err = np.array([ np.abs(u_exact - u_approx), np.abs(v_exact - v_approx) ]) if type(err) == float: average_errs[i, j] = err#np.abs((exact(t_N) - ode.y_vals[-1])) else: average_errs[i, j] = np.mean(err) fig, ax = plot_setup('$N$', 'Time [s]', x_log=True, y_log=True) lineObjects = ax.plot(N_vals, times.T, 'x-') ax.legend(iter(lineObjects), methods, fontsize=16) pt.show() fig, ax = plot_setup('$N$', 'Absolute Error', x_log=True, y_log=True) lineObjects = ax.plot(N_vals, average_errs.T, 'x-') ax.legend(iter(lineObjects), methods, fontsize=16) pt.show()
def plot_grid(P, T, seedings=None): from ppp.Plots import plot_setup fig, ax = plot_setup('$x$', '$y$') cells = P[T] for cell in cells: ax.plot(cell[[0, 1], 0], cell[[0, 1], 1], 'k-') ax.plot(cell[[1, 2], 0], cell[[1, 2], 1], 'k-') ax.plot(cell[[2, 0], 0], cell[[2, 0], 1], 'k-') ax.plot(P[:, 0], P[:, 1], 'ro', markersize=3) if seedings is not None: ax.plot(seedings[:, 0], seedings[:, 1], 'bx', markersize=4) return fig, ax