11import numpy as np
22import sympy as sp
33from 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
78def 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+
4453def 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+
5767def 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+
7589def 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
269283def minmax (t , u ):
0 commit comments