Skip to content

Commit d9dcb14

Browse files
committed
First step in convertion to python 3
1 parent c1200a6 commit d9dcb14

32 files changed

+235
-245
lines changed

odespy/PyDSTool.py

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
# Author: Liwei Wang
22
"""
33
"""
4-
from solvers import *
4+
from .solvers import *
55
import numpy as np
66

77
class Pyds(Solver):
@@ -24,10 +24,10 @@ class Pyds(Solver):
2424

2525
def initialize(self):
2626
try:
27-
import PyDSTool
27+
from . import PyDSTool
2828
except ImportError:
29-
raise ImportError,'''
30-
PyDSTool is not installed - required for solvers from PyDSTool'''
29+
raise ImportError('''
30+
PyDSTool is not installed - required for solvers from PyDSTool''')
3131

3232
def solve(self, time_points, terminate=None):
3333
# Common parts as superclass
@@ -45,7 +45,7 @@ def solve(self, time_points, terminate=None):
4545
# through Python dictionaries with string keys.
4646

4747
# Start setting for PyDSTool
48-
import PyDSTool
48+
from . import PyDSTool
4949
neq, f, u0 = self.neq, self.f, self.U0
5050

5151
# Initialize variables as trajectories in PyDSTOOL
@@ -139,7 +139,7 @@ def initialize_for_solve(self):
139139
if not hasattr(self,'init_step'):
140140
self.init_step = self.t[1] - self.t[0]
141141
self.params_pydstool = dict(\
142-
(key,value) for key,value in self.__dict__.items() \
142+
(key,value) for key,value in list(self.__dict__.items()) \
143143
if key in self._params_pydstool)
144144

145145
if __name__ == '__main__':
@@ -148,7 +148,7 @@ def initialize_for_solve(self):
148148
method = Vode_pyds(f)
149149
method.set_initial_condition([0.,1.])
150150
u,t = method.solve(np.linspace(0.,10.,50))
151-
print u
151+
print(u)
152152
import scitools.std as st
153153
st.plot(t,u[:,0])
154-
print max(u[:,0]-np.sin(t))
154+
print(max(u[:,0]-np.sin(t)))

odespy/RungeKutta.py

Lines changed: 20 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
from solvers import Solver, Adaptive
1+
from .solvers import Solver, Adaptive
22
import numpy as np
33

44
def _calculate_order_1_level(coefficients):
@@ -134,7 +134,7 @@ def middle(x,y,z): # Auxilary function
134134
k = np.zeros((k_len, self.neq), self.dtype) # intern stages
135135

136136
if self.verbose > 0:
137-
print 'advance solution in [%s, %s], h=%g' % (t_n, t_next, h)
137+
print('advance solution in [%s, %s], h=%g' % (t_n, t_next, h))
138138

139139
# Loop until next time point is reached
140140
while (abs(t - t_n) < abs(t_next - t_n)):
@@ -150,7 +150,7 @@ def middle(x,y,z): # Auxilary function
150150

151151
self.info['rejected'] += 1 # reduced below if accepted
152152
if self.verbose > 0:
153-
print ' u(t=%g)=%g: ' % (t+h, u_new),
153+
print(' u(t=%g)=%g: ' % (t+h, u_new), end=' ')
154154

155155
# local error between 2 levels
156156
error = h*np.abs(np.dot(factors_error, k))
@@ -171,18 +171,18 @@ def middle(x,y,z): # Auxilary function
171171
self.info['rejected'] -= 1
172172

173173
if self.verbose > 0:
174-
print 'accepted, ',
174+
print('accepted, ', end=' ')
175175
else:
176176
if self.verbose > 0:
177-
print 'rejected, ',
177+
print('rejected, ', end=' ')
178178

179179
if self.verbose > 0:
180-
print 'err=%s, ' % str(error),
180+
print('err=%s, ' % str(error), end=' ')
181181
if hasattr(self, 'u_exact') and callable(self.u_exact):
182-
print 'exact-err=%s, ' % \
183-
(np.asarray(self.u_exact(t+h))-u_new),
182+
print('exact-err=%s, ' % \
183+
(np.asarray(self.u_exact(t+h))-u_new), end=' ')
184184
if h <= self.min_step:
185-
print 'h=min_step!! ',
185+
print('h=min_step!! ', end=' ')
186186

187187

188188
# Replace 0 values by 1e-16 since we will divide by error
@@ -209,7 +209,7 @@ def middle(x,y,z): # Auxilary function
209209
h = min(h, t_next - t_intermediate[-1])
210210

211211
if self.verbose > 0:
212-
print 'new h=%g' % h
212+
print('new h=%g' % h)
213213

214214
if h == 0:
215215
break
@@ -367,16 +367,16 @@ def validate_data(self):
367367
# Check for dimension of user-defined butcher table.
368368
array_shape = self.butcher_tableau.shape
369369
if len(array_shape) is not 2:
370-
raise ValueError,'''
371-
Illegal input! Your input butcher_tableau should be a 2d-array!'''
370+
raise ValueError('''
371+
Illegal input! Your input butcher_tableau should be a 2d-array!''')
372372
else:
373373
m,n = array_shape
374374
if m not in (n, n + 1):
375-
raise ValueError, '''\
375+
raise ValueError('''\
376376
The dimension of 2d-array <method_yours_array> should be:
377377
1. Either (n, n), --> For 1-level RungeKutta methods
378378
2. Or (n+1, n), --> For 2-levels RungeKutta methods
379-
The shape of your input array is (%d, %d).''' % (m,n)
379+
The shape of your input array is (%d, %d).''' % (m,n))
380380
self._butcher_tableau = self.butcher_tableau
381381

382382
# Check for user-defined order,
@@ -393,19 +393,19 @@ def validate_data(self):
393393
if array_shape[0] == array_shape[1] + 1:
394394
# 2-level RungeKutta methods
395395
if type(self.method_order) is int:
396-
raise ValueError, error_2level
396+
raise ValueError(error_2level)
397397
try:
398398
order1, order2 = self.method_order
399399
if abs(order1-order2) != 1 or \
400400
order1 < 1 or order2 < 1:
401-
raise ValueError, error_2level
401+
raise ValueError(error_2level)
402402
except:
403-
raise ValueError,error_2level
403+
raise ValueError(error_2level)
404404
else:
405405
# 1-level RungeKutta methods
406406
if type(self.method_order) is not int or \
407407
self.method_order < 1:
408-
raise ValueError,error_1level
408+
raise ValueError(error_1level)
409409
self._method_order = self.method_order
410410

411411
else: # method_order is not specified
@@ -418,14 +418,14 @@ def validate_data(self):
418418
for i in range(1,array_shape[1] - 1):
419419
if not np.allclose(self.butcher_tableau[i][0],\
420420
sum(self.butcher_tableau[i][1:])):
421-
raise ValueError, '''
421+
raise ValueError('''
422422
Inconsistent data in Butcher_Tableau!
423423
In each lines of stage-coefficients, first number should be
424424
equal to the sum of other numbers.
425425
That is, for a butcher_table with K columns,
426426
a[i][0] == a[i][1] + a[i][2] + ... + a[i][K - 1]
427427
where 1 <= i <= K - 1
428428
Your input for line %d is :%s
429-
''' % (i,str(self.butcher_tableau[i]))
429+
''' % (i,str(self.butcher_tableau[i])))
430430

431431
return True

odespy/__init__.py

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1255,17 +1255,17 @@ def f(u, t):
12551255
with :math:`f(u,t)` implemented in Fortran.
12561256
'''
12571257

1258-
from solvers import *
1259-
from RungeKutta import *
1260-
from rkc import *
1261-
from rkf45 import *
1262-
from odepack import *
1263-
from radau5 import *
1264-
import problems
1258+
from .solvers import *
1259+
from .RungeKutta import *
1260+
from .rkc import *
1261+
from .rkf45 import *
1262+
from .odepack import *
1263+
from .radau5 import *
1264+
from . import problems
12651265

12661266
# Update doc strings with common info
12671267
class_, doc_str, classname = None, None, None
1268-
classnames = [name for name, obj in locals().items() \
1268+
classnames = [name for name, obj in list(locals().items()) \
12691269
if inspect.isclass(obj)]
12701270

12711271
toc = []

odespy/demos/demo_Lsodi_1.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ def jac(u, t, s):
5252
u,t = m.solve(time_points)
5353
st.plot(t, u[:,0], 'b-', title="Lsodi with Python functions",
5454
legend="with res, adda, ydoti & jac", hold="on")
55-
print 'Max error for test case 1 is %g' % max(u[-1] - exact_final)
55+
print('Max error for test case 1 is %g' % max(u[-1] - exact_final))
5656

5757
# Test case 2: Lsodi, with res, ydoti & adda
5858
m = method(res=res, rtol=rtol, atol=atol, ydoti=ydoti,
@@ -61,6 +61,6 @@ def jac(u, t, s):
6161
u,t = m.solve(time_points)
6262
st.plot(t, u[:,0], 'r*', title="Lsodi with Python functions",
6363
legend="with res, adda & ydoti", hold="on")
64-
print 'Max error for test case 1 is %g' % max(u[-1] - exact_final)
64+
print('Max error for test case 1 is %g' % max(u[-1] - exact_final))
6565

6666

odespy/demos/demo_Lsodi_1_fortran.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@
8383
u,t = m.solve(time_points)
8484
st.plot(t, u[:,0], 'r-', title="Lsodi with Fortran subroutines",
8585
legend="with res, adda, ydoti & jac", hold="on")
86-
print 'Max error for test case 1 is %g' % max(u[-1] - exact_final)
86+
print('Max error for test case 1 is %g' % max(u[-1] - exact_final))
8787

8888
# Test case 2: Lsodi, with res, ydoti & adda
8989
m = method(res_f77=res_f77, rtol=rtol, atol=atol, ydoti=ydoti,
@@ -92,6 +92,6 @@
9292
u,t = m.solve(time_points)
9393
st.plot(t, u[:,0], 'g*', title="Lsodi with Fortran subroutines",
9494
legend="with res, adda & ydoti", hold="on")
95-
print 'Max error for test case 2 is %g' % max(u[-1] - exact_final)
95+
print('Max error for test case 2 is %g' % max(u[-1] - exact_final))
9696

9797
os.remove('callback.so')

odespy/demos/demo_Lsodi_2.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,7 @@ def jac_banded(u, t, s, ml, mu): # Banded jacobian
113113
u,t = m.solve(time_points)
114114
st.plot(t, u[:,0], 'r-', title="Lsodi with Python functions",
115115
legend="with res, adda_full & jac_full", hold="on")
116-
print 'Max error with test case 1 is %g' % max(u[-1] - exact_final)
116+
print('Max error with test case 1 is %g' % max(u[-1] - exact_final))
117117

118118

119119
# Test case 2: Lsodi, with res & adda_full
@@ -122,7 +122,7 @@ def jac_banded(u, t, s, ml, mu): # Banded jacobian
122122
u,t = m.solve(time_points)
123123
st.plot(t, u[:,0], 'b*', title="Lsodi with Python functions",
124124
legend="with res & adda_full", hold="on")
125-
print 'Max error with test case 2 is %g' % max(u[-1] - exact_final)
125+
print('Max error with test case 2 is %g' % max(u[-1] - exact_final))
126126

127127
# Test case 3: Lsodi, with res, adda_banded, ml, mu, jac_banded
128128
m = method(res=res, rtol=rtol, atol=atol,
@@ -133,7 +133,7 @@ def jac_banded(u, t, s, ml, mu): # Banded jacobian
133133
u,t = m.solve(time_points)
134134
st.plot(t, u[:,0], 'go', title="Lsodi with Python functions",
135135
legend="with res, adda_banded, jac_banded, ml, mu", hold="on")
136-
print 'Max error with test case 3 is %g' % max(u[-1] - exact_final)
136+
print('Max error with test case 3 is %g' % max(u[-1] - exact_final))
137137

138138
# Test case 4: Lsodi, with res, adda_banded, ml, mu
139139
m = method(res=res, rtol=rtol, atol=atol,
@@ -143,7 +143,7 @@ def jac_banded(u, t, s, ml, mu): # Banded jacobian
143143
u,t = m.solve(time_points)
144144
st.plot(t, u[:,0], 'y-', title="Lsodi with Python functions",
145145
legend="with res, adda_banded, ml, mu", hold="on")
146-
print 'Max error with test case 4 is %g' % max(u[-1] - exact_final)
146+
print('Max error with test case 4 is %g' % max(u[-1] - exact_final))
147147

148148

149149

odespy/demos/demo_Lsodis_1.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -73,11 +73,11 @@ def jac(y, t, s, j, ia, ja):
7373
m = method(res=res, adda_lsodis=adda, atol=atol, rtol=rtol, jac_lsodis=jac)
7474
m.set_initial_condition(u0)
7575
y, t = m.solve(time_points)
76-
print 'Max error for test case 1 is %g' % max(y[-1] - exact_final)
76+
print('Max error for test case 1 is %g' % max(y[-1] - exact_final))
7777

7878
# Test case 2: With res & adda
7979
m = method(res=res, adda_lsodis=adda, atol=atol, rtol=rtol,lrw=4000,liw=100)
8080
m.set_initial_condition(u0)
8181
y, t = m.solve(time_points)
82-
print 'Max error for test case 2 is %g' % max(y[-1] - exact_final)
82+
print('Max error for test case 2 is %g' % max(y[-1] - exact_final))
8383

odespy/demos/demo_Lsodis_2.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -69,25 +69,25 @@ def jac(y, t, s, j, ia, ja):
6969
ia=ia, ja=ja, ic=ic, jc=jc)
7070
m.set_initial_condition(u0)
7171
y, t = m.solve(time_points)
72-
print 'Max error for test case 1 is %g' % max(y[-1] - exact_final)
72+
print('Max error for test case 1 is %g' % max(y[-1] - exact_final))
7373

7474
# Test case 2: with res, adda, ia, ja & jac
7575
m = method(res=res, adda_lsodis=adda, atol=atol, rtol=rtol, jac_lsodis=jac,
7676
ia=ia, ja=ja)
7777
m.set_initial_condition(u0)
7878
y, t = m.solve(time_points)
79-
print 'Max error for test case 2 is %g' % max(y[-1] - exact_final)
79+
print('Max error for test case 2 is %g' % max(y[-1] - exact_final))
8080

8181
# Test case 3: with res, adda & jac
8282
m = method(res=res, adda_lsodis=adda, atol=atol, rtol=rtol, jac_lsodis=jac)
8383
m.set_initial_condition(u0)
8484
y, t = m.solve(time_points)
85-
print 'Max error for test case 3 is %g' % max(y[-1] - exact_final)
85+
print('Max error for test case 3 is %g' % max(y[-1] - exact_final))
8686

8787
# Test case 4: With res & adda
8888
m = method(res=res, adda_lsodis=adda, atol=atol, rtol=rtol,lrw=4000,liw=100)
8989
m.set_initial_condition(u0)
9090
y, t = m.solve(time_points)
91-
print 'Max error for test case 4 is %g' % max(y[-1] - exact_final)
91+
print('Max error for test case 4 is %g' % max(y[-1] - exact_final))
9292

9393

odespy/demos/demo_Lsoibt_2.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -209,7 +209,7 @@ def jac(y, t, s):
209209
u_final = u[-1].reshape(99,3)
210210
u1, u2, u3 = u_final[:, 0], u_final[:, 1], u_final[:, 2]
211211
max_error = max(max(u1 - u1_exact), max(u2 - u2_exact), max(u3 - u3_exact))
212-
print 'Max error with Test case 1 is %g' % max_error
212+
print('Max error with Test case 1 is %g' % max_error)
213213

214214
# Test case 2: Lsoibt, with res, adda, mb, nb
215215
m = method(rtol=rtol, atol=atol, res=res, adda_lsoibt=adda,
@@ -222,4 +222,4 @@ def jac(y, t, s):
222222
u_final = u[-1].reshape(99,3)
223223
u1, u2, u3 = u_final[:, 0], u_final[:, 1], u_final[:, 2]
224224
max_error = max(max(u1 - u1_exact), max(u2 - u2_exact), max(u3 - u3_exact))
225-
print 'Max error with Test case 2 is %g' % max_error
225+
print('Max error with Test case 2 is %g' % max_error)

odespy/demos/demo_MyRungeKutta.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,14 +38,14 @@ def f(u,t):
3838
time_points = np.linspace(t0, tn, n_points)
3939

4040
orders = [] # list for calculated orders
41-
for m in bt.keys():
41+
for m in list(bt.keys()):
4242
# user-defined method, without order suplied
4343
method = MyRungeKutta(f, butcher_tableau=bt[m]['table'])
4444
orders += [method.get_order()]
4545
method.set_initial_condition(u0)
4646
u,t = method.solve(time_points)
4747
error = abs((u[-1] - np.exp(-1.))/np.exp(-1.))
48-
print 'Error is %g with solver %s' % (error, m)
48+
print('Error is %g with solver %s' % (error, m))
4949

5050

5151

0 commit comments

Comments
 (0)