|
| 1 | +import matplotlib.pyplot as plt |
| 2 | +import numpy as np |
| 3 | +import pandas as pd |
| 4 | + |
| 5 | +N = [32, 64, 128, 256, 512, 1024] |
| 6 | +Ord = [1, 3, 5] |
| 7 | + |
| 8 | +errors = np.nan * np.zeros((len(N), len(Ord), 3)) |
| 9 | + |
| 10 | +TEND = 200000 |
| 11 | + |
| 12 | +for i in range(len(N)): |
| 13 | + for j in range(len(Ord)): |
| 14 | + |
| 15 | + sim_a1 = pd.read_csv(f"N{N[i]}_O{Ord[j]}/D/cons.5.00.{TEND}.dat", sep=r"\s+", header=None, names=["x", "y"]) |
| 16 | + sim_a2 = pd.read_csv(f"N{N[i]}_O{Ord[j]}/D/cons.6.00.{TEND}.dat", sep=r"\s+", header=None, names=["x", "y"]) |
| 17 | + |
| 18 | + exact_a1 = pd.read_csv(f"N{N[i]}_O{Ord[j]}/D/cons.5.00.000000.dat", sep=r"\s+", header=None, names=["x", "y"]) |
| 19 | + exact_a2 = pd.read_csv(f"N{N[i]}_O{Ord[j]}/D/cons.6.00.000000.dat", sep=r"\s+", header=None, names=["x", "y"]) |
| 20 | + |
| 21 | + ## 2 norm |
| 22 | + errors[i, j, 0] = 1 / N[i] * np.sum(np.sqrt((sim_a1.y - exact_a1.y) ** 2)) |
| 23 | + errors[i, j, 0] += 1 / N[i] * np.sum(np.sqrt((sim_a2.y - exact_a2.y) ** 2)) |
| 24 | + |
| 25 | + ## 1 norm |
| 26 | + errors[i, j, 1] = 1 / N[i] * np.sum(np.abs(sim_a1.y - exact_a1.y)) |
| 27 | + errors[i, j, 1] += 1 / N[i] * np.sum(np.abs(sim_a2.y - exact_a2.y)) |
| 28 | + |
| 29 | + ## Inf norm |
| 30 | + errors[i, j, 2] = np.nanmax(np.abs(sim_a1.y - exact_a1.y)) |
| 31 | + errors[i, j, 2] += np.nanmax(np.abs(sim_a2.y - exact_a2.y)) |
| 32 | + |
| 33 | +fig, ax = plt.subplots(1, 3, figsize=(12, 8), sharex=True) |
| 34 | + |
| 35 | +colors = ["blue", "green", "red", "purple"] |
| 36 | + |
| 37 | +ref = np.nan * np.zeros((len(N), len(Ord))) |
| 38 | + |
| 39 | +for i in range(3): |
| 40 | + ax[i].plot(N, 30 / np.array(N) ** 1, label="Slope = -1", color=colors[0]) |
| 41 | + ax[i].plot(N, 3000 / np.array(N) ** 3, label="Slope = -3", color=colors[1]) |
| 42 | + ax[i].plot(N, 5000 / np.array(N) ** 5, label="Slope = -5", color=colors[2]) |
| 43 | + |
| 44 | +for j in range(len(Ord)): |
| 45 | + ax[0].plot(N, errors[:, j, 0], "o-", color=colors[j]) |
| 46 | + ax[0].set_xscale("log", base=2) |
| 47 | + ax[0].set_yscale("log") |
| 48 | + ax[0].set_title("||error||_2") |
| 49 | + ax[0].legend() |
| 50 | + |
| 51 | + ax[1].plot(N, errors[:, j, 1], "o-", color=colors[j]) |
| 52 | + ax[1].set_xscale("log", base=2) |
| 53 | + ax[1].set_yscale("log") |
| 54 | + ax[1].set_title("||error||_1") |
| 55 | + |
| 56 | + ax[2].plot(N, errors[:, j, 2], "o-", color=colors[j]) |
| 57 | + ax[2].set_xscale("log", base=2) |
| 58 | + ax[2].set_yscale("log") |
| 59 | + ax[2].set_title("||error||_inf") |
| 60 | + |
| 61 | +plt.tight_layout() |
| 62 | +plt.show() |
| 63 | + |
| 64 | +errors = np.column_stack((N, errors[:, :, 0])) |
| 65 | +errors = np.column_stack((errors, 7 / np.array(N) ** 1)) |
| 66 | +errors = np.column_stack((errors, 700 / np.array(N) ** 3)) |
| 67 | +errors = np.column_stack((errors, 2000 / np.array(N) ** 5)) |
| 68 | + |
| 69 | +df = pd.DataFrame(errors, columns=["N", "1", "3", "5", "R1", "R3", "R5"], index=N) |
| 70 | +df.to_csv("errors.csv", index=False) |
0 commit comments