Skip to content

Commit f154a0b

Browse files
using print(...) instead of print ..., for python3, added some comments
1 parent eea21fb commit f154a0b

File tree

5 files changed

+84
-46
lines changed

5 files changed

+84
-46
lines changed

coupling_schemes.py

Lines changed: 45 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,13 @@
1515

1616
def estimate_coupling_neumann_BC(left_domain, u_left, right_domain, u_right):
1717
"""
18-
@type left_domain Domain
18+
estimate neumann boundary condition at the coupling interface between left_domain and right_domain from existing nodal data
19+
:param left_domain: left domain
20+
:type left_domain: Domain
21+
:param u_left: solution on left domain
22+
:param left_domain: right domain
23+
:type right_domain: Domain
24+
:param u_right: solution on right domain
1925
@type right_domain Domain
2026
"""
2127
# set neumann BC at coupling interface
@@ -27,7 +33,7 @@ def estimate_coupling_neumann_BC(left_domain, u_left, right_domain, u_right):
2733
u_neumann_coupled__ = finite_difference_schemes.central_FD_at(left_domain.u.shape[0]-1, right_domain.grid.h, u_glued, order=numeric_parameters.neumann_coupling_order)
2834
else: # use modified operator for non-identical mesh size
2935
if numeric_parameters.neumann_coupling_order != 2:
30-
print "Operator of order %d is not implemented!!!" % numeric_parameters.neumann_coupling_order
36+
print("Operator of order %d is not implemented!!!" % numeric_parameters.neumann_coupling_order)
3137
quit()
3238
fraction = left_domain.grid.h / right_domain.grid.h # normalize to right domain's meshwidth
3339
p = np.array([-fraction, 0, 1.0])
@@ -36,13 +42,17 @@ def estimate_coupling_neumann_BC(left_domain, u_left, right_domain, u_right):
3642
u = np.array([u_left[-2], u_right[0], u_right[1]])
3743
u_neumann_coupled__ = 1.0/right_domain.grid.h * (u.dot(c))
3844
else:
39-
print "not implemented schemes for coupling Neumann BC demanded!"
45+
print("not implemented schemes for coupling Neumann BC demanded!")
4046
quit()
4147

4248
return u_neumann_coupled__
4349

4450

4551
class CouplingScheme(object):
52+
"""
53+
abstract class defining a coupling scheme.
54+
"""
55+
4656
__metaclass__ = abc.ABCMeta
4757

4858
name = "Coupling Scheme"
@@ -53,23 +63,28 @@ def __init__(self):
5363

5464
@abc.abstractmethod
5565
def perform(self, t0, tau, left_domain, right_domain):
66+
"""
67+
abstract method to perform one coupled timestep with coupling between equation on left_domain and right_domain
68+
:param t0: current time
69+
:param tau: time step size
70+
:param left_domain: left domain with independent model
71+
:param right_domain: right domain with independent model
72+
:return:
73+
"""
5674
return
5775

5876

5977
class FullyImplicitCoupling(CouplingScheme):
78+
"""
79+
fully implicit coupling with iterative coupling
80+
"""
6081

6182
name = "Fully Implicit Coupling"
6283

6384
def __init__(self):
6485
super(FullyImplicitCoupling, self).__init__()
6586

6687
def perform(self, t0, tau, left_domain, right_domain):
67-
"""
68-
@type left_domain Domain
69-
@type right_domain Domain
70-
:param t0:
71-
"""
72-
7388
residual = np.inf
7489
tol = numeric_parameters.fixed_point_tol
7590
n_steps_max = numeric_parameters.n_max_fixed_point_iterations
@@ -120,7 +135,7 @@ def perform(self, t0, tau, left_domain, right_domain):
120135
n_steps += 1
121136

122137
if n_steps == n_steps_max:
123-
print "maximum number of steps exceeded!"
138+
print("maximum number of steps exceeded!")
124139
return False
125140

126141
# update solution
@@ -133,6 +148,9 @@ def perform(self, t0, tau, left_domain, right_domain):
133148

134149

135150
class FullyExplicitCoupling(CouplingScheme):
151+
"""
152+
fully explicit coupling with staggered approach
153+
"""
136154

137155
name = "Fully Explicit Coupling"
138156

@@ -195,6 +213,9 @@ def perform(self, t0, tau, left_domain, right_domain):
195213

196214

197215
class WaveformCoupling(CouplingScheme):
216+
"""
217+
waveform coupling relying on waveform relaxation
218+
"""
198219

199220
name = "Waveform Coupling"
200221

@@ -295,7 +316,7 @@ def perform(self, t0, tau, left_domain, right_domain):
295316
u_right_new[0] = u_dirichlet_continuous_m1(t0+tau) # we have to set the (known and changing) dirichlet value manually, since this value is not changed by the timestepping
296317

297318
if numeric_parameters.neumann_coupling_order != 2:
298-
print "Operator of order %d is not implemented!!!" % numeric_parameters.neumann_coupling_order
319+
print("Operator of order %d is not implemented!!!" % numeric_parameters.neumann_coupling_order)
299320
quit()
300321

301322
fraction = left_domain.grid.h / right_domain.grid.h # normalize to right domain's meshwidth
@@ -320,6 +341,9 @@ def perform(self, t0, tau, left_domain, right_domain):
320341

321342

322343
class ExplicitPredictorCoupling(CouplingScheme):
344+
"""
345+
predictor coupling using a predictor scheme for the coupled participants
346+
"""
323347

324348
name = "Explicit Predictor Coupling"
325349

@@ -393,6 +417,9 @@ def perform(self, t0, tau, left_domain, right_domain):
393417

394418

395419
class SemiImplicitExplicitCoupling(CouplingScheme):
420+
"""
421+
coupling scheme using a combination of explicit and implicit coupling
422+
"""
396423

397424
name = "Semi Implicit Explicit Coupling"
398425

@@ -470,7 +497,7 @@ def perform(self, t0, tau, left_domain, right_domain):
470497
n_steps += 1
471498

472499
if n_steps == n_steps_max:
473-
print "maximum number of steps exceeded!"
500+
print("maximum number of steps exceeded!")
474501
return False
475502

476503
# update solution
@@ -483,6 +510,9 @@ def perform(self, t0, tau, left_domain, right_domain):
483510

484511

485512
class StrangSplittingCoupling(CouplingScheme):
513+
"""
514+
coupling using Strang splitting
515+
"""
486516

487517
name = "Strang Splitting Coupling"
488518

@@ -565,7 +595,9 @@ def perform(self, t0, tau, left_domain, right_domain):
565595

566596

567597
class MonolithicScheme(CouplingScheme):
568-
# todo this class hierarchy stinks!
598+
"""
599+
monolithic scheme for solving an equation without coupling
600+
"""
569601

570602
name = "Monolithic Approach"
571603

finite_difference_schemes.py

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,11 @@ def central_FD_at(i, h, u, derivative=1, order=2):
1515
if order == 2:
1616
du = 1.0/(2.0 * h) * (-1.0 * u[i-1] + 1.0 * u[i+1])
1717
else:
18-
print "not implemented!"
18+
print("central_FD_at(...) with derivarive == 1 and order != 2 not implemented!")
19+
quit()
1920
else:
20-
print "not implemented!"
21+
print("central_FD_at(...) with derivarive > 1 not implemented!")
22+
quit()
2123

2224
return du
2325

@@ -51,8 +53,10 @@ def one_sided_forward_FD_at(i, h, u, derivative=1, order=1):
5153
elif order == 5:
5254
du = 1.0/h * (-137.0/60.0 * u[i] + 5.0 * u[i+1] - 5.0 * u[i+2] + 10.0/3.0 * u[i+3] - 5.0/4.0 * u[i+4] + 1.0/5.0 * u[i+5])
5355
else:
54-
print "not implemented!"
56+
print("one_sided_forward_FD_at(...) with derivarive == 1 and order > 5 not implemented!")
57+
quit()
5558
else:
56-
print "not implemented!"
59+
print("one_sided_forward_FD_at(...) with derivarive > 1 not implemented!")
60+
quit()
5761

5862
return du

main.py

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -38,36 +38,36 @@ def experimental_series(taus, u_ref_left, u_ref_right, experiment):
3838
"""
3939
@type taus list
4040
"""
41-
print "EXPERIMENTAL SERIES"
41+
print("EXPERIMENTAL SERIES")
4242

4343
errors_left = []
4444
errors_right = []
4545
experiment_counter = 1
4646
n_experiments = taus.__len__()
4747

4848
for tau in taus:
49-
print "----"
50-
print "%i of %i" % (experiment_counter, n_experiments)
51-
print "tau = %f" % tau
49+
print("----")
50+
print("%i of %i" % (experiment_counter, n_experiments))
51+
print("tau = %f" % tau)
5252

5353
u_left, u_right = experiment(tau)
5454

5555
# compute error
5656
e_tot_left = np.sum(np.abs(u_left-u_ref_left)) / u_left.shape[0]
5757
e_tot_right = np.sum(np.abs(u_right-u_ref_right)) / (u_right.shape[0])
5858

59-
print "Error total = %.2e | %.2e" % (e_tot_left, e_tot_right)
59+
print("Error total = %.2e | %.2e" % (e_tot_left, e_tot_right))
6060

6161
experiment_counter += 1
6262
errors_left += [e_tot_left]
6363
errors_right += [e_tot_right]
6464

65-
print "----"
65+
print("----")
6666
np.set_printoptions(formatter={'float': lambda x: format(x, '6.3E')}) # print in scientific notation
67-
print repr(np.array(errors_left))
68-
print repr(np.array(errors_right))
69-
print taus
70-
print
67+
print(repr(np.array(errors_left)))
68+
print(repr(np.array(errors_right)))
69+
print(taus)
70+
print()
7171

7272
return errors_left, errors_right
7373

@@ -145,7 +145,7 @@ def solve_monolithic_problem(tau, time_stepping_scheme, domain):
145145
def compute_reference_solution(domain, tau, time_integration_scheme = time_integration.ImplicitTrapezoidalRule()):
146146
# compute monolithic at constant spatial and very fine temporal resolution
147147

148-
print "REFERENCE SOLUTION for grid "+str(domain.grid.x)
148+
print("REFERENCE SOLUTION for grid "+str(domain.grid.x))
149149

150150
return solve_monolithic_problem(tau, time_integration_scheme, domain)
151151

@@ -166,7 +166,7 @@ def coupling_experiment(coupling_scheme, time_stepping_schemes, left_domain, rig
166166
"""
167167
if type(time_stepping_schemes) is list:
168168
experiment_name = time_stepping_schemes[0].name + " - " + time_stepping_schemes[1].name + " - " + coupling_scheme.name
169-
print experiment_name
169+
print(experiment_name)
170170
experiment_errors_left, experimental_errors_right = experimental_series(experiment_timesteps, u_ref_left, u_ref_right, lambda tau: solve_inhomogeneously_coupled_problem(
171171
tau, time_stepping_schemes, coupling_scheme, [left_domain, right_domain]))
172172
else:
@@ -175,7 +175,7 @@ def coupling_experiment(coupling_scheme, time_stepping_schemes, left_domain, rig
175175
experiment_name += " - "+coupling_scheme.name_suffix
176176
except AttributeError:
177177
pass
178-
print experiment_name
178+
print(experiment_name)
179179
experiment_errors_left, experimental_errors_right = experimental_series(experiment_timesteps, u_ref_left, u_ref_right, lambda tau: solve_coupled_problem(tau, time_stepping_schemes, coupling_scheme, left_domain, right_domain))
180180

181181
return Experiment(experiment_name, [left_domain.grid.h, right_domain.grid.h], experiment_timesteps, experiment_errors_left, experimental_errors_right, additional_numerical_parameters=numeric_parameters.numeric_parameters_dict)
@@ -193,7 +193,7 @@ def monolithic_experiment(time_stepping_scheme, monolithic_domain, u_ref_left, u
193193
:return:
194194
"""
195195
experiment_name = time_stepping_scheme.name + " - " + coupling_schemes.MonolithicScheme().name
196-
print experiment_name
196+
print(experiment_name)
197197
experiment_errors_left, experimental_errors_right = experimental_series(experiment_timesteps, u_ref_left, u_ref_right, lambda tau: solve_monolithic_problem(tau, time_stepping_scheme, monolithic_domain))
198198

199199
if type(monolithic_domain.grid) is IrregularGrid:
@@ -250,7 +250,7 @@ def save_experiments(experimental_series, prefix):
250250
u_ref_left_regular, u_ref_right_regular = compute_reference_solution(monolithic_domain_regular, tau_ref, time_integration_scheme=time_integration.RungeKutta4)
251251
u_ref_left_nonregular, u_ref_right_nonregular = compute_reference_solution(monolithic_domain_nonregular, tau_ref, time_integration_scheme=time_integration.RungeKutta4)
252252

253-
print "### experimental series 1: order degradation with classical schemes on regular domain"
253+
print("### experimental series 1: order degradation with classical schemes on regular domain")
254254

255255
experiments = list()
256256
experiments.append(monolithic_experiment(time_integration.ExplicitHeun, monolithic_domain_regular, u_ref_left_regular, u_ref_right_regular, experiment_timesteps))
@@ -261,7 +261,7 @@ def save_experiments(experimental_series, prefix):
261261
experiments.append(coupling_experiment(coupling_schemes.FullyImplicitCoupling(), time_integration.RungeKutta4, left_domain, right_domain_coarse, u_ref_left_regular, u_ref_right_regular, experiment_timesteps))
262262
save_experiments(experiments, 'series1_Order')
263263

264-
print "### experimental series 2: Customized schemes Semi Implicit-Explicit coupling and Predictor coupling on regular domain"
264+
print("### experimental series 2: Customized schemes Semi Implicit-Explicit coupling and Predictor coupling on regular domain")
265265

266266
experiments = list()
267267
experiments.append(monolithic_experiment(time_integration.ExplicitHeun, monolithic_domain_regular, u_ref_left_regular, u_ref_right_regular, experiment_timesteps))
@@ -272,7 +272,7 @@ def save_experiments(experimental_series, prefix):
272272
experiments.append(coupling_experiment(coupling_schemes.SemiImplicitExplicitCoupling(), time_integration.ImplicitTrapezoidalRule, left_domain, right_domain_coarse, u_ref_left_regular, u_ref_right_regular, experiment_timesteps))
273273
save_experiments(experiments, 'series2_Custom')
274274

275-
print "### experimental series 3: Strang splitting coupling on non-regular domain"
275+
print("### experimental series 3: Strang splitting coupling on non-regular domain")
276276

277277
experiments = list()
278278
experiments.append(monolithic_experiment(time_integration.ExplicitHeun, monolithic_domain_nonregular, u_ref_left_nonregular, u_ref_right_nonregular, experiment_timesteps))
@@ -284,7 +284,7 @@ def save_experiments(experimental_series, prefix):
284284
experiments.append(coupling_experiment(coupling_schemes.StrangSplittingCoupling(), [time_integration.RungeKutta4, time_integration.ImplicitTrapezoidalRule], left_domain, right_domain_fine, u_ref_left_nonregular, u_ref_right_nonregular, experiment_timesteps))
285285
save_experiments(experiments, 'series3_Strang')
286286

287-
print "### experimental series 4: Waveform relaxation coupling on non-regular domain"
287+
print("### experimental series 4: Waveform relaxation coupling on non-regular domain")
288288

289289
experiments = list()
290290
experiments.append(monolithic_experiment(time_integration.ImplicitTrapezoidalRule, monolithic_domain_nonregular, u_ref_left_nonregular, u_ref_right_nonregular, experiment_timesteps))

postproc.py

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,8 @@
1717
name = parser.parse_args().foldername
1818
plottingdir = r'./' + name
1919
filenames = glob.glob(plottingdir+"/*.json")
20-
print filenames
20+
print("reading from")
21+
print(filenames)
2122

2223
marker_collection = ('o','x','d','s','^','<','>','v','.')
2324

@@ -27,9 +28,6 @@
2728
if plot_right:
2829
marker_right = itertools.cycle(marker_collection)
2930

30-
for filename in filenames:
31-
print filename
32-
3331
if plot_right:
3432
f, (ax1, ax2) = plt.subplots(1, 2, sharey=True, figsize=(12,9))
3533
else:
@@ -58,8 +56,8 @@ def concat_w_zero_padding(a,b):
5856

5957

6058
for filename in filenames:
61-
print "---"
62-
print filename
59+
print("---")
60+
print(filename)
6361
with open(filename) as data_file:
6462

6563
data = json.load(data_file)

space_discretization.py

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ def second_derivative_matrix(grid, **kwargs):
4343
elif type(grid) is IrregularGrid:
4444
return irregular_matrix_operator(grid.x, **kwargs)
4545
else:
46-
print "unsupported grid type!"
46+
print("in second_derivative_matrix(...) unsupported grid type!")
4747
quit()
4848

4949

@@ -83,7 +83,8 @@ def irregular_matrix_operator(x, **kwargs):
8383
# manually add it to R
8484
R[1] = 1.0/h**2 * kwargs.get('dirichlet_l')
8585
else:
86-
print "insufficient boundary conditions at left boundary!"
86+
print("in irregular_matrix_operator(...): insufficient boundary conditions at left boundary!")
87+
quit()
8788

8889
if 'neumann_r' in kwargs:
8990
# modify row and column corresponding to neumann BC at u_N
@@ -99,7 +100,8 @@ def irregular_matrix_operator(x, **kwargs):
99100
# manually add it to R
100101
R[-2] = 1.0/h**2 * kwargs.get('dirichlet_r')
101102
else:
102-
print "insufficient boundary conditions at right boundary!"
103+
print("in irregular_matrix_operator(...): insufficient boundary conditions at right boundary!")
104+
quit()
103105

104106
return A, R
105107

@@ -132,7 +134,8 @@ def regular_matrix_operator(h, N, **kwargs):
132134
# manually add it to R
133135
R[1] = 1.0/h**2 * kwargs.get('dirichlet_l')
134136
else:
135-
print "insufficient boundary conditions at left boundary!"
137+
print("in regular_matrix_operator(...): insufficient boundary conditions at left boundary!")
138+
quit()
136139

137140
if 'neumann_r' in kwargs:
138141
# modify row and column corresponding to neumann BC at u_N
@@ -146,6 +149,7 @@ def regular_matrix_operator(h, N, **kwargs):
146149
# manually add it to R
147150
R[-2] = 1.0/h**2 * kwargs.get('dirichlet_r')
148151
else:
149-
print "insufficient boundary conditions at right boundary!"
152+
print("in regular_matrix_operator(...): insufficient boundary conditions at right boundary!")
153+
quit()
150154

151155
return A, R

0 commit comments

Comments
 (0)