Skip to content

Commit 45a0f66

Browse files
committed
more cleanup
1 parent b0e228c commit 45a0f66

File tree

5 files changed

+52
-16
lines changed

5 files changed

+52
-16
lines changed

pySDC/implementations/problem_classes/HenonHeiles.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,12 +50,12 @@ def eval_f(self, u, t):
5050

5151
def u_exact(self, t):
5252
"""
53-
Routine to compute the exact trajectory at time t
53+
Routine to compute the exact/initial trajectory at time t
5454
5555
Args:
5656
t (float): current time
5757
Returns:
58-
dtype_u: exact position and velocity
58+
dtype_u: exact/initial position and velocity
5959
"""
6060
assert t == 0.0, 'error, u_exact only works for the initial time t0=0'
6161
me = particles(2)

pySDC/implementations/problem_classes/OuterSolarSystem.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@
33
import numpy as np
44

55
from pySDC.core.Problem import ptype
6-
from pySDC.implementations.datatype_classes.particles import particles, fields, acceleration
7-
from pySDC.core.Errors import ParameterError, ProblemError
6+
from pySDC.implementations.datatype_classes.particles import particles, acceleration
7+
from pySDC.core.Errors import ParameterError
88

99

1010
# noinspection PyUnusedLocal

pySDC/implementations/sweeper_classes/verlet.py

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,10 @@ class verlet(sweeper):
1212
Second-order sweeper using velocity-Verlet as base integrator
1313
1414
Attributes:
15-
S: node-to-node collocation matrix (first order)
16-
SQ: node-to-node collocation matrix (second order)
17-
ST: node-to-node trapezoidal matrix
18-
Sx: node-to-node Euler half-step for position update
15+
QQ: 0-to-node collocation matrix (second order)
16+
QT: 0-to-node trapezoidal matrix
17+
Qx: 0-to-node Euler half-step for position update
18+
qQ: update rule for final value (if needed)
1919
"""
2020

2121
def __init__(self, params):
@@ -26,17 +26,15 @@ def __init__(self, params):
2626
params: parameters for the sweeper
2727
"""
2828

29-
# call parent's initialization routine
30-
3129
if 'QI' not in params:
3230
params['QI'] = 'IE'
3331
if 'QE' not in params:
3432
params['QE'] = 'EE'
3533

34+
# call parent's initialization routine
3635
super(verlet, self).__init__(params)
3736

38-
# S- and SQ-matrices (derived from Q) and Sx- and ST-matrices for the integrator
39-
# [self.S, self.ST, self.SQ, self.Sx, self.QQ] = self.__get_Qd()
37+
# Trapezoidal rule, Qx and Double-Q as in the Boris-paper
4038
[self.QT, self.Qx, self.QQ] = self.__get_Qd()
4139

4240
self.qQ = np.dot(self.coll.weights, self.coll.Qmat[1:, 1:])
@@ -55,8 +53,6 @@ def __get_Qd(self):
5553
# set implicit and explicit Euler matrices
5654
QI = self.get_Qdelta_implicit(self.coll, self.params.QI)
5755
QE = self.get_Qdelta_explicit(self.coll, self.params.QE)
58-
# QI = np.zeros(self.coll.Qmat.shape)
59-
# QE = np.zeros(self.coll.Qmat.shape)
6056

6157
# trapezoidal rule
6258
QT = 0.5 * (QI + QE)
@@ -66,6 +62,8 @@ def __get_Qd(self):
6662

6763
QQ = np.zeros(np.shape(self.coll.Qmat))
6864

65+
# if we have Gauss-Lobatto nodes, we can do a magic trick from the Book
66+
# this takes Gauss-Lobatto IIIB and create IIIA out of this
6967
if isinstance(self.coll, CollGaussLobatto):
7068

7169
for m in range(self.coll.num_nodes):
@@ -74,6 +72,7 @@ def __get_Qd(self):
7472
self.coll.weights[m])
7573
QQ = np.dot(self.coll.Qmat, QQ)
7674

75+
# if we do not have Gauss-Lobatto, just multiply Q (will not get a symplectic method, they say)
7776
else:
7877

7978
QQ = np.dot(self.coll.Qmat, self.coll.Qmat)
@@ -154,6 +153,7 @@ def integrate(self):
154153
for j in range(1, self.coll.num_nodes + 1):
155154
p[-1].pos += L.dt * (L.dt * self.QQ[m, j] * L.f[j]) + L.dt * self.coll.Qmat[m, j] * L.u[0].vel
156155
p[-1].vel += L.dt * self.coll.Qmat[m, j] * L.f[j]
156+
# we need to set mass and charge here, too, since the code uses the integral to create new particles
157157
p[-1].m = L.u[0].m
158158
p[-1].q = L.u[0].q
159159

@@ -182,6 +182,7 @@ def compute_end_point(self):
182182
for m in range(self.coll.num_nodes):
183183
L.uend.pos += L.dt * (L.dt * self.qQ[m] * L.f[m + 1]) + L.dt * self.coll.weights[m] * L.u[0].vel
184184
L.uend.vel += L.dt * self.coll.weights[m] * L.f[m + 1]
185+
# remember to set mass and charge here, too
185186
L.uend.m = L.u[0].m
186187
L.uend.q = L.u[0].q
187188
# add up tau correction of the full interval (last entry)

pySDC/projects/Hamiltonian/simple_problems.py

Lines changed: 33 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
from collections import defaultdict
33
import os
44
import dill
5+
import numpy as np
56

67
import pySDC.helpers.plot_helper as plt_helper
78

@@ -13,7 +14,7 @@
1314
from pySDC.implementations.problem_classes.HarmonicOscillator import harmonic_oscillator
1415
from pySDC.implementations.transfer_classes.TransferParticles_NoCoarse import particles_to_particles
1516

16-
from pySDC.helpers.stats_helper import filter_stats
17+
from pySDC.helpers.stats_helper import filter_stats, sort_stats
1718
from pySDC.projects.Hamiltonian.hamiltonian_output import hamiltonian_output
1819

1920

@@ -125,18 +126,21 @@ def run_simulation(prob=None):
125126
126127
"""
127128

129+
# check what problem type we have and set up corresponding description and variables
128130
if prob == 'harmonic':
129131
description, controller_params = setup_harmonic()
130132
# set time parameters
131133
t0 = 0.0
132134
Tend = 50.0
133135
num_procs = 100
136+
maxmeaniter = 5.6
134137
elif prob == 'henonheiles':
135138
description, controller_params = setup_henonheiles()
136139
# set time parameters
137140
t0 = 0.0
138141
Tend = 25.0
139142
num_procs = 100
143+
maxmeaniter = 3.9
140144
else:
141145
raise NotImplemented('Problem type not implemented, got %s' % prob)
142146

@@ -151,6 +155,30 @@ def run_simulation(prob=None):
151155
# call main function to get things done...
152156
uend, stats = controller.run(u0=uinit, t0=t0, Tend=Tend)
153157

158+
# filter statistics by type (number of iterations)
159+
filtered_stats = filter_stats(stats, type='niter')
160+
161+
# convert filtered statistics to list of iterations count, sorted by process
162+
iter_counts = sort_stats(filtered_stats, sortby='time')
163+
164+
# compute and print statistics
165+
for item in iter_counts:
166+
out = 'Number of iterations for time %4.2f: %2i' % item
167+
print(out)
168+
169+
niters = np.array([item[1] for item in iter_counts])
170+
out = ' Mean number of iterations: %4.2f' % np.mean(niters)
171+
print(out)
172+
out = ' Range of values for number of iterations: %2i ' % np.ptp(niters)
173+
print(out)
174+
out = ' Position of max/min number of iterations: %2i -- %2i' % \
175+
(int(np.argmax(niters)), int(np.argmin(niters)))
176+
print(out)
177+
out = ' Std and var for number of iterations: %4.2f -- %4.2f' % (float(np.std(niters)), float(np.var(niters)))
178+
print(out)
179+
180+
assert np.mean(niters) <= maxmeaniter, 'Mean number of iterations is too high, got %s' % np.mean(niters)
181+
154182
fname = 'data/' + prob + '.dat'
155183
f = open(fname, 'wb')
156184
dill.dump(stats, f)
@@ -167,22 +195,25 @@ def show_results(prob=None, cwd=''):
167195
prob (str): name of the problem
168196
cwd (str): current working directory
169197
"""
198+
199+
# read in the dill data
170200
f = open(cwd + 'data/' + prob + '.dat', 'rb')
171201
stats = dill.load(f)
172202
f.close()
173203

204+
# extract error in hamiltonian and prepare for plotting
174205
extract_stats = filter_stats(stats, type='err_hamiltonian')
175206
result = defaultdict(list)
176207
for k, v in extract_stats.items():
177208
result[k.iter].append((k.time, v))
178209
for k, v in result.items():
179-
assert k <= 6, 'Number of iterations is too high for %s, got %s' % (prob, k)
180210
result[k] = sorted(result[k], key=lambda x: x[0])
181211

182212
plt_helper.mpl.style.use('classic')
183213
plt_helper.setup_mpl()
184214
plt_helper.newfig(textwidth=238.96, scale=0.89)
185215

216+
# Rearrange data for easy plotting
186217
err_ham = 1
187218
for k, v in result.items():
188219
time = [item[0] for item in v]

pySDC/projects/Hamiltonian/solar_system.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -135,10 +135,13 @@ def show_results(prob=None, cwd=''):
135135
prob (str): name of the problem
136136
cwd (str): current working directory
137137
"""
138+
139+
# read in the dill data
138140
f = open(cwd + 'data/' + prob + '.dat', 'rb')
139141
stats = dill.load(f)
140142
f.close()
141143

144+
# extract error in hamiltonian and prepare for plotting
142145
extract_stats = filter_stats(stats, type='err_hamiltonian')
143146
result = defaultdict(list)
144147
for k, v in extract_stats.items():
@@ -150,6 +153,7 @@ def show_results(prob=None, cwd=''):
150153
plt_helper.setup_mpl()
151154
plt_helper.newfig(textwidth=238.96, scale=0.89)
152155

156+
# Rearrange data for easy plotting
153157
err_ham = 1
154158
for k, v in result.items():
155159
time = [item[0] for item in v]

0 commit comments

Comments
 (0)