Skip to content

Commit 06a92d1

Browse files
committed
convergence test fisher
1 parent 3a30507 commit 06a92d1

File tree

3 files changed

+119
-1
lines changed

3 files changed

+119
-1
lines changed

playgrounds/order_test/Fisher.py

Lines changed: 114 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,114 @@
1+
# import pySDC.helpers.plot_helper as plt_helper
2+
#
3+
# import pickle
4+
# import os
5+
import numpy as np
6+
7+
from pySDC.implementations.datatype_classes.mesh import mesh
8+
from pySDC.implementations.sweeper_classes.generic_implicit import generic_implicit
9+
from pySDC.implementations.collocation_classes.gauss_radau_right import CollGaussRadau_Right
10+
from pySDC.implementations.controller_classes.allinclusive_multigrid_nonMPI import allinclusive_multigrid_nonMPI
11+
from pySDC.implementations.problem_classes.GeneralizedFisher_1D_FD_implicit import generalized_fisher
12+
13+
from pySDC.helpers.stats_helper import filter_stats, sort_stats
14+
15+
16+
def main():
17+
# initialize level parameters
18+
level_params = dict()
19+
level_params['restol'] = 1E-10
20+
21+
# This comes as read-in for the step class (this is optional!)
22+
step_params = dict()
23+
step_params['maxiter'] = 50
24+
25+
# This comes as read-in for the problem class
26+
problem_params = dict()
27+
problem_params['nu'] = 1
28+
problem_params['nvars'] = 2047
29+
problem_params['lambda0'] = 1.0
30+
problem_params['newton_maxiter'] = 50
31+
problem_params['newton_tol'] = 1E-10
32+
problem_params['interval'] = (-1, 1)
33+
34+
# This comes as read-in for the sweeper class
35+
sweeper_params = dict()
36+
sweeper_params['collocation_class'] = CollGaussRadau_Right
37+
sweeper_params['num_nodes'] = 4
38+
sweeper_params['QI'] = 'LU'
39+
sweeper_params['spread'] = False
40+
sweeper_params['do_coll_update'] = False
41+
42+
# initialize controller parameters
43+
controller_params = dict()
44+
controller_params['logger_level'] = 30
45+
46+
# Fill description dictionary for easy hierarchy creation
47+
description = dict()
48+
description['problem_class'] = generalized_fisher
49+
description['problem_params'] = problem_params
50+
description['dtype_u'] = mesh
51+
description['dtype_f'] = mesh
52+
description['sweeper_class'] = generic_implicit
53+
description['sweeper_params'] = sweeper_params
54+
description['step_params'] = step_params
55+
56+
# setup parameters "in time"
57+
t0 = 0
58+
Tend = 1.0
59+
dt_list = [Tend / 2 ** i for i in range(0, 4)]
60+
61+
err = 0
62+
for dt in dt_list:
63+
print('Working with dt = %s...' % dt)
64+
65+
level_params['dt'] = dt
66+
description['level_params'] = level_params
67+
68+
# instantiate the controller
69+
controller = allinclusive_multigrid_nonMPI(num_procs=1, controller_params=controller_params,
70+
description=description)
71+
72+
# get initial values on finest level
73+
P = controller.MS[0].levels[0].prob
74+
uinit = P.u_exact(t0)
75+
76+
# call main function to get things done...
77+
uend, stats = controller.run(u0=uinit, t0=t0, Tend=Tend)
78+
79+
# compute exact solution and compare
80+
uex = P.u_exact(Tend)
81+
err_new = abs(uex - uend)
82+
83+
print('error at time %s: %s' % (Tend, err_new))
84+
if err > 0:
85+
print('order of accuracy: %6.4f' % (np.log(err / err_new) / np.log(2)))
86+
87+
err = err_new
88+
89+
# filter statistics by type (number of iterations)
90+
filtered_stats = filter_stats(stats, type='niter')
91+
92+
# convert filtered statistics to list of iterations count, sorted by process
93+
iter_counts = sort_stats(filtered_stats, sortby='time')
94+
95+
# compute and print statistics
96+
niters = np.array([item[1] for item in iter_counts])
97+
out = ' Mean number of iterations: %4.2f' % np.mean(niters)
98+
# f.write(out + '\n')
99+
print(out)
100+
out = ' Range of values for number of iterations: %2i ' % np.ptp(niters)
101+
# f.write(out + '\n')
102+
print(out)
103+
out = ' Position of max/min number of iterations: %2i -- %2i' % \
104+
(int(np.argmax(niters)), int(np.argmin(niters)))
105+
# f.write(out + '\n')
106+
print(out)
107+
out = ' Std and var for number of iterations: %4.2f -- %4.2f' % \
108+
(float(np.std(niters)), float(np.var(niters)))
109+
# f.write(out + '\n')
110+
# f.write(out + '\n')
111+
print(out)
112+
113+
if __name__ == "__main__":
114+
main()

pySDC/implementations/controller_classes/allinclusive_multigrid_nonMPI.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -136,7 +136,7 @@ def restart_block(self, active_slots, time, u0):
136136
# determine whether I am the first and/or last in line
137137
self.MS[p].status.first = active_slots.index(p) == 0
138138
self.MS[p].status.last = active_slots.index(p) == len(active_slots) - 1
139-
# intialize step with u0
139+
# initialize step with u0
140140
self.MS[p].init_step(u0)
141141
# reset some values
142142
self.MS[p].status.done = False

pySDC/implementations/problem_classes/GeneralizedFisher_1D_FD_implicit.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,7 @@ def solve_system(self, rhs, factor, u0, t):
9595

9696
# start newton iteration
9797
n = 0
98+
res = 99
9899
while n < self.params.newton_maxiter:
99100

100101
# form the function g with g(u) = 0
@@ -118,6 +119,9 @@ def solve_system(self, rhs, factor, u0, t):
118119
# increase iteration count
119120
n += 1
120121

122+
if n == self.params.newton_maxiter:
123+
raise ProblemError('Newton did not converge after %i iterations, error is %s' % (n, res))
124+
121125
return u
122126

123127
def eval_f(self, u, t):

0 commit comments

Comments
 (0)