Skip to content

Commit c530b9a

Browse files
author
Daniel Ruprecht
committed
add script that plots the absolute value of the stability function for fixed M and fixed lambda ratio versus the number of iterations
1 parent d1a7609 commit c530b9a

File tree

1 file changed

+83
-0
lines changed

1 file changed

+83
-0
lines changed

examples/fwsw/plot_stablimits.py

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
from pySDC import Level as lvl
2+
from pySDC import Hooks as hookclass
3+
from pySDC import CollocationClasses as collclass
4+
from pySDC import Step as stepclass
5+
6+
from pySDC.datatype_classes.complex_mesh import mesh, rhs_imex_mesh
7+
from pySDC.sweeper_classes.imex_1st_order import imex_1st_order as imex
8+
from examples.fwsw.ProblemClass import swfw_scalar
9+
import numpy as np
10+
11+
from pylab import rcParams
12+
import matplotlib.pyplot as plt
13+
from matplotlib.patches import Polygon
14+
from subprocess import call
15+
16+
def get_stab_function(LHS, RHS, Kmax, lambd, do_coll_update):
17+
Pinv = np.linalg.inv(LHS)
18+
Mat_sweep = np.linalg.matrix_power(Pinv.dot(RHS), Kmax)
19+
for l in range(0,Kmax):
20+
Mat_sweep = Mat_sweep + np.linalg.matrix_power(Pinv.dot(RHS),k).dot(Pinv)
21+
if do_coll_update:
22+
stab_fh = 1.0 + lambd*level.sweep.coll.weights.dot(Mat_sweep.dot(np.ones(nnodes)))
23+
else:
24+
q = np.zeros(nnodes)
25+
q[nnodes-1] = 1.0
26+
stab_fh = q.dot(Mat_sweep.dot(np.ones(nnodes)))
27+
return stab_fh
28+
29+
if __name__ == "__main__":
30+
mvals = [2, 4]
31+
kvals = np.arange(1,15)
32+
lambdaratio = [2, 10, 50]
33+
stabval = np.zeros((np.size(mvals), np.size(lambdaratio), np.size(kvals)))
34+
35+
for i in range(0,np.size(mvals)):
36+
pparams = {}
37+
# the following are not used in the computation
38+
pparams['lambda_s'] = np.array([0.0])
39+
pparams['lambda_f'] = np.array([0.0])
40+
pparams['u0'] = 1.0
41+
swparams = {}
42+
swparams['collocation_class'] = collclass.CollGaussRadau_Right
43+
swparams['num_nodes'] = mvals[i]
44+
do_coll_update = True
45+
46+
#
47+
# ...this is functionality copied from test_imexsweeper. Ideally, it should be available in one place.
48+
#
49+
step = stepclass.step(params={})
50+
L = lvl.level(problem_class=swfw_scalar, problem_params=pparams, dtype_u=mesh, dtype_f=rhs_imex_mesh, sweeper_class=imex, sweeper_params=swparams, level_params={}, hook_class=hookclass.hooks, id="stability")
51+
step.register_level(L)
52+
step.status.dt = 1.0
53+
step.status.time = 0.0
54+
u0 = step.levels[0].prob.u_exact(step.status.time)
55+
step.init_step(u0)
56+
nnodes = step.levels[0].sweep.coll.num_nodes
57+
level = step.levels[0]
58+
problem = level.prob
59+
60+
QE = level.sweep.QE[1:,1:]
61+
QI = level.sweep.QI[1:,1:]
62+
Q = level.sweep.coll.Qmat[1:,1:]
63+
64+
for j in range(0,np.size(lambdaratio)):
65+
lambda_slow = 1j
66+
lambda_fast = lambdaratio[j]*lambda_slow
67+
68+
LHS = np.eye(nnodes) - step.status.dt*( lambda_fast*QI + lambda_slow*QE )
69+
RHS = step.status.dt*( (lambda_fast+lambda_slow)*Q - (lambda_fast*QI + lambda_slow*QE) )
70+
71+
for k in range(0, np.size(kvals)):
72+
Kmax = kvals[k]
73+
stab_fh = get_stab_function(LHS, RHS, kvals[k], lambda_fast+lambda_slow, do_coll_update)
74+
stabval[i,j,k] = np.absolute(stab_fh)
75+
76+
fig = plt.figure()
77+
plt.plot(kvals, stabval[0,0,:], '-', color='b')
78+
plt.plot(kvals, stabval[0,1,:], '-', color='r')
79+
plt.plot(kvals, stabval[0,2,:], '-', color='g')
80+
#plt.plot(kvals, stabval[1,0,:], '-', color='r')
81+
#plt.plot(kvals, stabval[1,1,:], '--', color='r')
82+
#plt.plot(kvals, stabval[1,2,:], '-.', color='r')
83+
plt.show()

0 commit comments

Comments
 (0)