Peano
Loading...
Searching...
No Matches
convergence-test.py
Go to the documentation of this file.
1import subprocess
2import os
3import numpy as np
4from datetime import datetime
5from matplotlib import pyplot as plt
6from matrixReader import populateRHS
7
8os.system('export PYTHONPATH=../../../../python/')
9
10meshes = (3, 9, 81, 243)
11
12# Get the current date and time
13current_date_time = datetime.now()
14# Format the date and time as a string
15formatted_date_time = current_date_time.strftime("%Y-%m-%d_%H-%M-%S")
16
17folder_name = f"convergence-test-{formatted_date_time}"
18
19input(f"This will run a convergence test for meshes {meshes}. The output will be saved in {folder_name}/. Press Enter to continue...")
20#os.system(f"git clean -xdf")
21os.system(f"mkdir {folder_name}")
22
23for mesh in meshes:
24 meshsize = 1.1 * 1.0/mesh
25 os.system("python3 dg.py -m release --meshsize " + str(meshsize))
26 os.system("./peano4petsc")
27
28 mesh_folder_name = f"{folder_name}/{mesh}"
29 os.system(f"mkdir {mesh_folder_name}")
30 os.system(f"mv mat.xml {mesh_folder_name}/")
31 os.system(f"mv rhs.xml {mesh_folder_name}/")
32 os.system(f"mv sol.xml {mesh_folder_name}/")
33 os.system(f"mv exactSol.txt {mesh_folder_name}/")
34
35 os.system(f"git clean -xdf -e {folder_name}")
36
37 #input("Press Enter to continue...")
38
39# Compute error
40Err = []
41H = []
42for mesh in meshes:
43 cell_ndof = mesh ** 2 * 4
44 mesh_folder_name = f"{folder_name}/{mesh}"
45 sol = populateRHS(f"{mesh_folder_name}/sol.xml")[:cell_ndof]
46 exact_sol = np.loadtxt(f"{mesh_folder_name}/exactSol.txt")
47 assert(len(sol) == len(exact_sol))
48
49 error = np.max(np.abs(sol - exact_sol))
50 Err.append(error)
51 H.append(1.0/mesh)
52 print(f"for mesh {mesh}, error = {error}")
53
54# Plot convergence data
55X = np.asarray(H)
56Y = np.asarray(Err)
57
58plt.plot(
59 X,
60 Y,
61 linewidth=2,
62 color="blue",
63 markersize=6,
64 marker="o",
65 label=f"$degree=1$",
66)
67
68ax = plt.gca()
69plt.title(r"Interiror penatly method for 2D Poisson equation: " + '\n' + r"$-\Delta u = f(x,y), \quad f(x,y) = 8 \pi^2 \, \sin(2\pi x) \, \sin(2\pi y), \quad u |_{\partial \Omega} = 0$")
70ax.set_xscale("log")
71ax.set_yscale("log")
72ax.set_xlabel("Grid spacing $h$")
73ax.set_ylabel(r"Max error: $||u-u_{\mathrm{exact}}||_\inf$")
74plt.plot(
75 [1e-2, 1e-1],
76 [2.0e-3, 2.0e-3 * 10**2.0],
77 linewidth=2,
78 linestyle="--",
79 color="black",
80 label=r"$\propto h^{-2.0}$",
81)
82
83plt.legend(loc="lower right")
84plt.savefig(f"{folder_name}/error.png", bbox_inches="tight")
85print(f"Error plot saved in {folder_name}/error.png")
#define assert(...)
Definition LinuxAMD.h:28