Peano
Loading...
Searching...
No Matches
parseDG.py
Go to the documentation of this file.
1"""
2Plot residual norm vs number of DG iterations for DG solver
3"""
4
5import numpy as np
6import re
7import matplotlib.pyplot as plt
8import mplcursors
9
10res_norm = [] # residual norms after each DG iteration in the formal [|r|_max, |r|_2, |r|_h]
11
12# Open output file
13with open('out.txt', 'r') as file:
14 lines = file.readlines()
15
16# Work out mesh size
17n_mesh = None
18for line in lines:
19 match = re.search(r'h_min=([\d.]+)', line)
20 if match:
21 n_mesh = round(1.0/float(match.group(1))) # n_mesh = 3 for 3x3 mesh
22 print(f"Mesh size detected: {n_mesh}x{n_mesh}")
23 break
24if n_mesh is None:
25 print("Mesh size not detected")
26
27# Check each line in the output file for "DG smoother step, disable CG solver"
28for i in range(len(lines)):
29 if "run Solve" in lines[i]:
30 # Look at the next line that contains the residual norms
31 next_line = lines[i + 1]
32 pattern = r"([-+]?\d*\.?\d+(?:e[-+]?\d+)?)"
33 full_pattern = rf"\|r\|\_max={pattern},\s*\|r\|\_2={pattern},\s*\|r\|\_h={pattern}"
34 norms = re.search(full_pattern, next_line)
35 if norms:
36 r_max, r_2, r_h = map(float, norms.groups()) # Convert to floats
37 res_norm.append([r_max, r_2, r_h])
38
39res_norm_array = np.array(res_norm)
40
41degree = input("Enter polynomial degree: ")
42
43# Plot max-norm vs DG iterations
44plt.plot(res_norm_array[:,0], label='r_max', marker='o')
45plt.plot(res_norm_array[:,1], label='r_2', marker='o')
46plt.yscale('log')
47plt.xlabel('DG iterations')
48plt.ylabel('|r|_max')
49
50
51res_norm_stable = np.load(f"../../../../../PeanoData/res_norm_dg_mesh_{n_mesh}_p_{degree}_STABLE.npy")
52plt.plot(res_norm_stable[:,0], label='r_max stable', marker='x')
53plt.plot(res_norm_stable[:,1], label='r_2 stable', marker='x')
54
55
56plt.show()
57
58dir = '../../../../../PeanoData/'
59file_name = f'res_norm_dg_mesh_{n_mesh}_p_{degree}.npy'
60
61np.save(f"{dir}{file_name}", res_norm_array)
62print(f"Saved residual norms to {dir}{file_name}")