1+ from pySDC .implementations .collocation_classes .gauss_radau_right import CollGaussRadau_Right
2+ from pySDC .core .Sweeper import sweeper
3+ import numpy as np
4+ import matplotlib .pyplot as plt
5+
6+ def iteration_vs_estimate ():
7+
8+ M = 5
9+ K = 10
10+ swee = sweeper ({'collocation_class' : CollGaussRadau_Right , 'num_nodes' : M })
11+ Q = swee .coll .Qmat [1 :, 1 :]
12+ # Qd = swee.get_Qdelta_implicit(swee.coll, 'IE')[1:, 1:]
13+ Qd = swee .get_Qdelta_implicit (swee .coll , 'LU' )[1 :, 1 :]
14+ Qd = swee .get_Qdelta_explicit (swee .coll , 'EE' )[1 :, 1 :]
15+ print (swee .coll .nodes )
16+ exit ()
17+ I = np .eye (M )
18+
19+ lam = - 0.7
20+ print (1 / np .linalg .norm (Qd , np .inf ), np .linalg .norm (np .linalg .inv (Qd ), np .inf ))
21+ C = I - lam * Q
22+ P = I - lam * Qd
23+ Pinv = np .linalg .inv (P )
24+
25+ R = Pinv .dot (lam * (Q - Qd ))
26+ rho = max (abs (np .linalg .eigvals (R )))
27+ infnorm = np .linalg .norm (R , np .inf )
28+ twonorm = np .linalg .norm (R , 2 )
29+
30+ # uex = np.exp(lam)
31+ u0 = np .ones (M , dtype = np .complex128 )
32+ uex = np .linalg .inv (C ).dot (u0 )[- 1 ]
33+ u = u0 .copy ()
34+ # u = np.random.rand(M)
35+ res = u0 - C .dot (u )
36+ err = []
37+ resnorm = []
38+ for k in range (K ):
39+ u += Pinv .dot (res )
40+ res = u0 - C .dot (u )
41+ err .append (abs (u [- 1 ] - uex ))
42+ resnorm .append (np .linalg .norm (res , np .inf ))
43+ print (k , resnorm [- 1 ], err [- 1 ])
44+
45+ plt .figure ()
46+ plt .semilogy (range (K ), err , 'o-' , color = 'red' , label = 'error' )
47+ plt .semilogy (range (K ), resnorm , 'd-' , color = 'orange' , label = 'residual' )
48+ plt .semilogy (range (K ), [err [0 ] * rho ** k for k in range (K )], '--' , color = 'green' , label = 'spectral' )
49+ plt .semilogy (range (K ), [err [0 ] * infnorm ** k for k in range (K )], '--' , color = 'blue' , label = 'infnorm' )
50+ plt .semilogy (range (K ), [err [0 ] * twonorm ** k for k in range (K )], '--' , color = 'cyan' , label = 'twonorm' )
51+ plt .semilogy (range (K ), [err [0 ] * ((- 1 / lam ) ** (1 / M )) ** k for k in range (K )], 'x--' , color = 'black' , label = 'est' )
52+ plt .semilogy (range (K ), [err [0 ] * (abs (lam ) * np .linalg .norm (Q - Qd )) ** k for k in range (K )], 'x-.' , color = 'black' , label = 'est' )
53+ # plt.semilogy(range(K), [err[0] * (1/abs(lam) + np.linalg.norm((I-np.linalg.inv(Qd).dot(Q)) ** k)) for k in range(K)], 'x-.', color='black', label='est')
54+ plt .grid ()
55+ plt .legend ()
56+ plt .show ()
57+
58+
59+ def estimates_over_lambda ():
60+ M = 5
61+ K = 10
62+ swee = sweeper ({'collocation_class' : CollGaussRadau_Right , 'num_nodes' : M })
63+ Q = swee .coll .Qmat [1 :, 1 :]
64+ # Qd = swee.get_Qdelta_implicit(swee.coll, 'IE')[1:, 1:]
65+ Qd = swee .get_Qdelta_implicit (swee .coll , 'LU' )[1 :, 1 :]
66+
67+ Qdinv = np .linalg .inv (Qd )
68+ I = np .eye (M )
69+ # lam = 1/np.linalg.eigvals(Q)[0]
70+ # print(np.linalg.inv(I - lam * Q))
71+ # print(np.linalg.norm(1/lam * Qdinv, np.inf))
72+ # exit()
73+
74+ # print(np.linalg.norm((I-Qdinv.dot(Q)) ** K))
75+ # lam_list = np.linspace(start=20j, stop=0j, num=1000, endpoint=False)
76+ lam_list = np .linspace (start = - 1000 , stop = 0 , num = 100 , endpoint = True )
77+
78+ rho = []
79+ est = []
80+ infnorm = []
81+ twonorm = []
82+ for lam in lam_list :
83+ # C = I - lam * Q
84+ P = I - lam * Qd
85+ Pinv = np .linalg .inv (P )
86+
87+ R = Pinv .dot (lam * (Q - Qd ))
88+ # w, V = np.linalg.eig(R)
89+ # Vinv = np.linalg.inv(V)
90+ # assert np.linalg.norm(V.dot(np.diag(w)).dot(Vinv) - R) < 1E-14, np.linalg.norm(V.dot(np.diag(w)).dot(Vinv) - R)
91+ rho .append (max (abs (np .linalg .eigvals (R ))))
92+ # est.append(0.62*(-1/lam) ** (1/(M-1))) # M = 3
93+ # est.append(0.57*(-1/lam) ** (1/(M-1))) # M = 5
94+ # est.append(0.71*(-1/lam) ** (1/(M-1.5))) # M = 7
95+ # est.append(0.92*(-1/lam) ** (1/(M-2.5))) # M = 9
96+ # est.append((-1/lam) ** (1/(M)))
97+ est .append (1000 * (1 / abs (lam )) ** (1 / (1 )))
98+ # est.append(abs(lam))
99+ # est.append(np.linalg.norm((I-Qdinv.dot(Q)) ** M) + 1/abs(lam))
100+ # est.append(np.linalg.norm(np.linalg.inv(I - lam * Qd), np.inf))
101+ # est.append(np.linalg.norm(np.linalg.inv(I - np.linalg.inv(I - lam * np.diag(Qd)).dot(lam*np.tril(Qd, -1))), np.inf))
102+
103+ infnorm .append (np .linalg .norm (np .linalg .matrix_power (R , M ), np .inf ))
104+ # infnorm.append(np.linalg.norm(Vinv.dot(R).dot(V), np.inf))
105+ twonorm .append (np .linalg .norm (np .linalg .matrix_power (R , M ), 2 ))
106+ # twonorm.append(np.linalg.norm(Vinv.dot(R).dot(V), 2))
107+ plt .figure ()
108+ plt .semilogy (lam_list , rho , '--' , color = 'green' , label = 'spectral' )
109+ plt .semilogy (lam_list , est , '--' , color = 'red' , label = 'est' )
110+ # plt.semilogy(lam_list, infnorm, 'x--', color='blue', label='infnorm')
111+ # plt.semilogy(lam_list, twonorm, 'd--', color='cyan', label='twonorm')
112+ # plt.semilogy(np.imag(lam_list), rho, '--', color='green', label='spectral')
113+ # plt.semilogy(np.imag(lam_list), est, '--', color='red', label='est')
114+ # plt.semilogy(np.imag(lam_list), infnorm, '--', color='blue', label='infnorm')
115+ # plt.semilogy(np.imag(lam_list), twonorm, '--', color='cyan', label='twonorm')
116+ plt .grid ()
117+ plt .legend ()
118+ plt .show ()
119+
120+ if __name__ == '__main__' :
121+ iteration_vs_estimate ()
122+ # estimates_over_lambda()
0 commit comments