Peano
Loading...
Searching...
No Matches
parseTwoGrid.py
Go to the documentation of this file.
1"""
2Plot residual norm vs number of DG iterations and two-grid cycles
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# Check each line in the output file for "DG smoother step, disable CG solver"
17for i in range(len(lines)):
18 if "DG smoother step, disable CG solver" in lines[i]:
19 # Look at the next line that contains the residual norms
20 next_line = lines[i + 1]
21 pattern = r"([-+]?\d*\.?\d+(?:e[-+]?\d+)?)"
22 full_pattern = rf"\|r\|\_max={pattern},\s*\|r\|\_2={pattern},\s*\|r\|\_h={pattern}"
23 norms = re.search(full_pattern, next_line)
24 if norms:
25 r_max, r_2, r_h = map(float, norms.groups()) # Convert to floats
26 res_norm.append([r_max, r_2, r_h])
27
28res_norm_array = np.array(res_norm)
29
30# Plot max-norm vs DG iterations
31plt.plot(res_norm_array[:,0], label='r_max', marker='o')
32plt.yscale('log')
33#mplcursors.cursor(hover=True)
34
35# Second axis for cycles
36steps = 4 # Number of pre-smoothing steps
37max_id = len(res_norm_array[:,0])
38cycle_ticks = np.arange(3, max_id, steps)
39
40# Labels for the second axis: 0, 1, 2, ...
41cycle_labels = np.arange(len(cycle_ticks))
42
43# Add second axis
44cycles = plt.gca().secondary_xaxis('top')
45cycles.set_xlabel('Two-grid cycles')
46
47# Set ticks and labels
48cycles.set_xticks(cycle_ticks)
49cycles.set_xticklabels(cycle_labels)
50
51# Mark V-cycles with vertical lines
52for i in range(3, max_id, steps):
53 plt.axvline(x=i, color='gray', linestyle='--', linewidth=0.5)
54
55plt.show()
56
57#np.save('../../../../../data***/***.npy', res_norm_array)