Skip to content

Commit 0c9df05

Browse files
committed
Removed scitools imports
1 parent 4c7e185 commit 0c9df05

File tree

1 file changed

+22
-28
lines changed

1 file changed

+22
-28
lines changed

src/vib/vib.py

Lines changed: 22 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
import numpy as np
22
import sympy as sp
33
from devito import Dimension, Constant, TimeFunction, Eq, solve, Operator
4-
#import matplotlib.pyplot as plt
5-
import scitools.std as plt
4+
import matplotlib.pyplot as plt
5+
# import scitools.std as plt
66

77
def solver(I, V, m, b, s, F, dt, T, damping='linear'):
88
"""
@@ -13,33 +13,27 @@ def solver(I, V, m, b, s, F, dt, T, damping='linear'):
1313
'quadratic', f(u')=b*u'*abs(u').
1414
F(t) and s(u) are Python functions.
1515
"""
16-
dt = float(dt)
17-
b = float(b)
18-
m = float(m)
16+
dt = float(dt); b = float(b); m = float(m) # avoid integer div.
1917
Nt = int(round(T/dt))
20-
t = Dimension('t', spacing=Constant('h_t'))
21-
22-
u = TimeFunction(name='u', dimensions=(t,),
23-
shape=(Nt+1,), space_order=2)
24-
25-
u.data[0] = I
18+
u = np.zeros(Nt+1)
19+
t = np.linspace(0, Nt*dt, Nt+1)
2620

21+
u[0] = I
2722
if damping == 'linear':
28-
# dtc for central difference (default for time is forward, 1st order)
29-
eqn = m*u.dt2 + b*u.dtc + s(u) - F(u)
30-
stencil = Eq(u.forward, solve(eqn, u.forward))
23+
u[1] = u[0] + dt*V + dt**2/(2*m)*(-b*V - s(u[0]) + F(t[0]))
3124
elif damping == 'quadratic':
32-
# fd_order set as backward derivative used is 1st order
33-
eqn = m*u.dt2 + b*u.dt*sp.Abs(u.dtl(fd_order=1)) + s(u) - F(u)
34-
stencil = Eq(u.forward, solve(eqn, u.forward))
35-
# First timestep needs to have the backward timestep substituted
36-
stencil_init = stencil.subs(u.backward, u.forward-2*t.spacing*V)
37-
op_init = Operator(stencil_init, name='first_timestep')
38-
op = Operator(stencil, name='main_loop')
39-
op_init.apply(h_t=dt, t_M=1)
40-
op.apply(h_t=dt, t_m=1, t_M=Nt-1)
25+
u[1] = u[0] + dt*V + \
26+
dt**2/(2*m)*(-b*V*abs(V) - s(u[0]) + F(t[0]))
4127

42-
return u.data, np.linspace(0, Nt*dt, Nt+1)
28+
for n in range(1, Nt):
29+
if damping == 'linear':
30+
u[n+1] = (2*m*u[n] + (b*dt/2 - m)*u[n-1] +
31+
dt**2*(F(t[n]) - s(u[n])))/(m + b*dt/2)
32+
elif damping == 'quadratic':
33+
u[n+1] = (2*m*u[n] - m*u[n-1] + b*u[n]*abs(u[n] - u[n-1])
34+
+ dt**2*(F(t[n]) - s(u[n])))/\
35+
(m + b*abs(u[n] - u[n-1]))
36+
return u, t
4337

4438
def visualize(u, t, title='', filename='tmp'):
4539
plt.plot(t, u, 'b-')
@@ -212,9 +206,9 @@ def plot_empirical_freq_and_amplitude(u, t):
212206
plt.figure()
213207
from math import pi
214208
w = 2*pi/p
215-
plt.plot(range(len(p)), w, 'r-')
209+
plt.plot(list(range(len(p))), w, 'r-')
216210
plt.hold('on')
217-
plt.plot(range(len(a)), a, 'b-')
211+
plt.plot(list(range(len(a))), a, 'b-')
218212
ymax = 1.1*max(w.max(), a.max())
219213
ymin = 0.9*min(w.min(), a.min())
220214
plt.axis([0, max(len(p), len(a)), ymin, ymax])
@@ -247,7 +241,7 @@ def visualize_front(u, t, window_width, savefig=False):
247241
axis=plot_manager.axis(),
248242
show=not savefig) # drop window if savefig
249243
if savefig:
250-
print 't=%g' % t[n]
244+
print('t=%g' % t[n])
251245
st.savefig('tmp_vib%04d.png' % n)
252246
plot_manager.update(n)
253247

@@ -263,7 +257,7 @@ def visualize_front_ascii(u, t, fps=10):
263257

264258
p = Plotter(ymin=umin, ymax=umax, width=60, symbols='+o')
265259
for n in range(len(u)):
266-
print p.plot(t[n], u[n]), '%.2f' % (t[n])
260+
print(p.plot(t[n], u[n]), '%.2f' % (t[n]))
267261
time.sleep(1/float(fps))
268262

269263
def minmax(t, u):

0 commit comments

Comments
 (0)