Skip to content

Commit 8b3cbdd

Browse files
author
Daniel Ruprecht
committed
function that plots convergence rate versus k for different values of c_s
1 parent 4e3d410 commit 8b3cbdd

File tree

1 file changed

+47
-7
lines changed

1 file changed

+47
-7
lines changed

examples/acoustic_1d_imex/runitererror.py

Lines changed: 47 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
from pySDC import CollocationClasses as collclass
33

44
import numpy as np
5+
import scipy.sparse.linalg as LA
56

67
from ProblemClass_conv import acoustic_1d_imex
78
from examples.acoustic_1d_imex.HookClass import plot_solution
@@ -14,10 +15,13 @@
1415
from pySDC.Stats import grep_stats, sort_stats
1516

1617
from matplotlib import pyplot as plt
18+
from pylab import rcParams
1719

1820

1921
if __name__ == "__main__":
2022

23+
fs = 8
24+
2125
# set global logger (remove this if you do not want the output at all)
2226
logger = Log.setup_custom_logger('root')
2327

@@ -29,13 +33,16 @@
2933

3034
sparams = {}
3135

32-
cs_v = [0.5, 1.0, 1.5]
33-
sparams['maxiter'] = 25
34-
nodes_v = [2, 3]
36+
cs_v = [0.5, 1.0, 1.5, 5.0]
37+
sparams['maxiter'] = 15
38+
nodes_v = [3]
3539

3640
residual = np.zeros((np.size(cs_v), np.size(nodes_v), sparams['maxiter']))
37-
38-
for cs_ind in np.arange(np.size(cs_v)):
41+
convrate = np.zeros((np.size(cs_v), np.size(nodes_v), sparams['maxiter']-1))
42+
lastiter = np.zeros(( np.size(cs_v), np.size(nodes_v) )) + sparams['maxiter']
43+
avg_convrate = np.zeros(( np.size(cs_v), np.size(nodes_v) ))
44+
45+
for cs_ind in range(0,np.size(cs_v)):
3946

4047
# This comes as read-in for the problem class
4148
pparams = {}
@@ -83,7 +90,40 @@
8390
for k,v in stats.items():
8491
iter = getattr(k,'iter')
8592
if iter is not -1:
86-
residual[cs_ind, nodes_ind, iter-1] = v
87-
# print residual
93+
residual[cs_ind, nodes_ind, iter-1] = v
94+
95+
# Compute convergence rates
96+
for iter in range(0,sparams['maxiter']-1):
97+
if residual[cs_ind, nodes_ind, iter]<lparams['restol']:
98+
lastiter[cs_ind,nodes_ind] = iter
99+
else:
100+
convrate[cs_ind, nodes_ind, iter] = residual[cs_ind, nodes_ind, iter+1]/residual[cs_ind, nodes_ind, iter]
101+
102+
# Compute estimate
103+
#lambda_fast = LA.eigsh(P.A, k=1, which='LM')
104+
#lambda_slow = LA.eigsh(P.Dx, k=1, which='LM')
105+
avg_convrate[cs_ind, nodes_ind] = np.sum(convrate[cs_ind, nodes_ind, :])/float(lastiter[cs_ind,nodes_ind])
106+
107+
#### end of for loops ####
108+
109+
color = [ 'r', 'b', 'g', 'c' ]
110+
shape = ['o-', 'd-', 's-', '>-']
111+
rcParams['figure.figsize'] = 2.5, 2.5
112+
fig = plt.figure()
113+
for ii in range(0,np.size(cs_v)):
114+
x = np.arange(1,lastiter[ii,0])
115+
y = convrate[ii, 0, 0:lastiter[ii,0]-1]
116+
plt.plot(x, y, shape[ii], markersize=fs-2, color=color[ii], label=r'$c_{s}$=%4.2f' % cs_v[ii])
117+
#plt.plot(x, 0.0*y+avg_convrate[ii,0], '--', color=color[ii])
118+
119+
plt.legend(loc='upper right', fontsize=fs, prop={'size':fs})
120+
plt.xlabel('Iteration', fontsize=fs)
121+
plt.ylabel('Convergence rate', fontsize=fs, labelpad=2)
122+
plt.xlim([0, sparams['maxiter']])
123+
plt.ylim([0, 0.8])
124+
plt.yticks(fontsize=fs)
125+
plt.xticks(fontsize=fs)
126+
plt.show()
127+
fig.savefig('sdc_fwsw_iteration.pdf',bbox_inches='tight')
88128

89129

0 commit comments

Comments
 (0)