Skip to content

Commit cdb8ce9

Browse files
committed
Misc tweaks for test passing. Solve still problematic
1 parent 8c59d2d commit cdb8ce9

File tree

2 files changed

+31
-10
lines changed

2 files changed

+31
-10
lines changed

fdm-devito-notebooks/01_vib/vib_gen.ipynb

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -543,8 +543,15 @@
543543
" # fd_order set as backward derivative used is 1st order\n",
544544
" eqn = m*u.dt2 + b*u.dt*sp.Abs(u.dtl(fd_order=1)) + s(u) - F(u)\n",
545545
" stencil = Eq(u.forward, solve(eqn, u.forward))\n",
546+
"\n",
546547
" # First timestep needs to have the backward timestep substituted\n",
547-
" stencil_init = stencil.subs(u.backward, u.forward-2*t.spacing*V)\n",
548+
" # Has to be done to the equation otherwise the stencil will have\n",
549+
" # forward timestep on both sides\n",
550+
" # FIXME: Doesn't look like you can do subs or solve on anything inside an Abs\n",
551+
" eqn_init = eqn.subs(u.backward, u.forward-2*t.spacing*V)\n",
552+
" stencil_init = Eq(u.forward, solve(eqn_init, u.forward))\n",
553+
" # stencil_init = stencil.subs(u.backward, u.forward-2*t.spacing*V)\n",
554+
"\n",
548555
" op_init = Operator(stencil_init, name='first_timestep')\n",
549556
" op = Operator(stencil, name='main_loop')\n",
550557
" op_init.apply(h_t=dt, t_M=1)\n",

src/vib/vib.py

Lines changed: 23 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
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
6+
67

78
def solver(I, V, m, b, s, F, dt, T, damping='linear'):
89
"""
@@ -32,15 +33,23 @@ def solver(I, V, m, b, s, F, dt, T, damping='linear'):
3233
# fd_order set as backward derivative used is 1st order
3334
eqn = m*u.dt2 + b*u.dt*sp.Abs(u.dtl(fd_order=1)) + s(u) - F(u)
3435
stencil = Eq(u.forward, solve(eqn, u.forward))
36+
3537
# First timestep needs to have the backward timestep substituted
36-
stencil_init = stencil.subs(u.backward, u.forward-2*t.spacing*V)
38+
# Has to be done to the equation otherwise the stencil will have
39+
# forward timestep on both sides
40+
# FIXME: Doesn't look like you can do subs or solve on anything inside an Abs
41+
eqn_init = eqn.subs(u.backward, u.forward-2*t.spacing*V)
42+
stencil_init = Eq(u.forward, solve(eqn_init, u.forward))
43+
# stencil_init = stencil.subs(u.backward, u.forward-2*t.spacing*V)
44+
3745
op_init = Operator(stencil_init, name='first_timestep')
3846
op = Operator(stencil, name='main_loop')
3947
op_init.apply(h_t=dt, t_M=1)
4048
op.apply(h_t=dt, t_m=1, t_M=Nt-1)
4149

4250
return u.data, np.linspace(0, Nt*dt, Nt+1)
4351

52+
4453
def visualize(u, t, title='', filename='tmp'):
4554
plt.plot(t, u, 'b-')
4655
plt.xlabel('t')
@@ -54,10 +63,14 @@ def visualize(u, t, title='', filename='tmp'):
5463
plt.savefig(filename + '.pdf')
5564
plt.show()
5665

66+
5767
def test_constant():
5868
"""Verify a constant solution."""
5969
u_exact = lambda t: I
60-
I = 1.2; V = 0; m = 2; b = 0.9
70+
I = 1.2
71+
V = 0
72+
m = 2
73+
b = 0.9
6174
w = 1.5
6275
s = lambda u: w**2*u
6376
F = lambda t: w**2*u_exact(t)
@@ -72,6 +85,7 @@ def test_constant():
7285
difference = np.abs(u_exact(t) - u).max()
7386
assert difference < tol
7487

88+
7589
def lhs_eq(t, m, b, s, u, damping='linear'):
7690
"""Return lhs of differential equation as sympy expression."""
7791
v = sp.diff(u, t)
@@ -211,10 +225,10 @@ def plot_empirical_freq_and_amplitude(u, t):
211225
a = amplitudes(minima, maxima)
212226
plt.figure()
213227
from math import pi
214-
w = 2*pi/p
215-
plt.plot(range(len(p)), w, 'r-')
228+
w = 2*pi/p
229+
plt.plot(list(range(len(p))), w, 'r-')
216230
plt.hold('on')
217-
plt.plot(range(len(a)), a, 'b-')
231+
plt.plot(list(range(len(a))), a, 'b-')
218232
ymax = 1.1*max(w.max(), a.max())
219233
ymin = 0.9*min(w.min(), a.min())
220234
plt.axis([0, max(len(p), len(a)), ymin, ymax])
@@ -247,7 +261,7 @@ def visualize_front(u, t, window_width, savefig=False):
247261
axis=plot_manager.axis(),
248262
show=not savefig) # drop window if savefig
249263
if savefig:
250-
print 't=%g' % t[n]
264+
print('t=%g' % t[n])
251265
st.savefig('tmp_vib%04d.png' % n)
252266
plot_manager.update(n)
253267

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

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

269283
def minmax(t, u):

0 commit comments

Comments
 (0)