plt.title('Time step: %g' % dt) plt.savefig('vib_%d_%d_u.png' % (timesteps_per_period, num_periods)) plt.savefig('vib_%d_%d_u.pdf' % (timesteps_per_period, num_periods)) plt.savefig('vib_%d_%d_u.eps' % (timesteps_per_period, num_periods)) #f = RHS(b=0.4, A_F=1, w_F=2) f = RHS(b=0.4, A_F=0, w_F=2) f = RHS(b=0.4, A_F=1, w_F=np.pi) f = RHS(b=0.4, A_F=20, w_F=2 * np.pi) # qualitatively wrong FE, almost ok BE, smaller T #f = RHS(b=0.4, A_F=20, w_F=0.5*np.pi) # cool, FE almost there, BE good # Define different sets of experiments solvers_theta = [ odespy.ForwardEuler(f), # Implicit methods must use Newton solver to converge odespy.BackwardEuler(f, nonlinear_solver='Newton'), odespy.CrankNicolson(f, nonlinear_solver='Newton'), ] solvers_RK = [odespy.RK2(f), odespy.RK4(f)] solvers_accurate = [ odespy.RK4(f), odespy.CrankNicolson(f, nonlinear_solver='Newton'), odespy.DormandPrince(f, atol=0.001, rtol=0.02) ] solvers_CN = [odespy.CrankNicolson(f, nonlinear_solver='Newton')] if __name__ == '__main__': timesteps_per_period = 20
'Medium damping, medium forcing w/smaller frequency'), (RHS(b=0.4, F=lambda t: 10 * sin(0.5 * pi * t)), 'Medium damping, large forcing w/smaller frequency'), (RHS(b=1.2, F=lambda t: 10 * sin(0.5 * pi * t)), 'Strong damping, large forcing w/smaller frequency'), (RHS(b=0.4, F=lambda t: 1 * sin(2 * pi * t)), 'Medium damping, medium forcing w/larger frequency'), (RHS(b=0.4, F=lambda t: 10 * sin(2 * pi * t)), 'Medium damping, large forcing w/larger frequency'), (RHS(b=1.2, F=lambda t: 10 * sin(2 * pi * t)), 'Strong damping, large forcing w/larger frequency'), ] for rhs, title in ODEs: solvers = [ odespy.ForwardEuler(rhs), # Implicit methods must use Newton solver to converge odespy.BackwardEuler(rhs, nonlinear_solver='Newton'), odespy.CrankNicolson(rhs, nonlinear_solver='Newton'), VibSolverWrapper4Odespy(rhs), ] T = 20 # Period is 1 dt = 0.05 # 20 steps per period filename = 'FEBNCN_' + title.replace(', ', '_').replace('w/', '') title = title + ' (dt=%g)' % dt plt.figure() run_solvers_and_plot(solvers, rhs, T, dt, title=title, filename=filename) plt.show() raw_input()
N = 40 L = 1 x = linspace(0, L, N + 1) f_kwargs = dict(L=L, beta=1, x=x) u = zeros(N + 1) U_0 = zeros(N + 1) U_0[0] = s(0) U_0[1:] = 283 import odespy solvers = { 'FE': odespy.ForwardEuler(rhs, f_kwargs=f_kwargs), 'BE': odespy.BackwardEuler(rhs, f_is_linear=True, jac=K, f_kwargs=f_kwargs, jac_kwargs=f_kwargs), 'B2': odespy.Backward2Step(rhs, f_is_linear=True, jac=K, f_kwargs=f_kwargs, jac_kwargs=f_kwargs), 'theta': odespy.ThetaRule(rhs, f_is_linear=True,
"""The FitzHugh-Nagumo model from biology.""" def f(u, t, s=10, a=0.12, c1=0.175, c2=0.03, b=0.011, d=0.55): v, w = u fv = s * c1 * v * (v - a) * (1 - v) - s * c2 * w fw = s * b * (v - d * w) return [fv, fw] def jac(u, t, s=10, a=0.12, c1=0.175, c2=0.03, b=0.011, d=0.55): v, w = u return [[(1 + a) * 2 * v * c1 - a * c1 - 3 * v**2 * c1, c2], [b, -b * d]] v0 = 0.1 w0 = 0.0 T = 50 import odespy, numpy as np, scitools.std as st a = 0.12 # stable a = -0.12 # unstable solver = odespy.ForwardEuler(f, jac=jac, f_kwargs={'a': a}) solver.set_initial_condition([v0, w0]) time_points = np.linspace(0, T, 401) #time_points = np.linspace(0, T, 21) u, t = solver.solve(time_points) v = u[:, 0] w = u[:, 1] st.plot(t, v, 'r-', t, w, 'b-', legend=('v', 'w'))
t = self.solver.t dW = numpy.random.normal(loc=0, scale=1, size=len(t)-1) dt = t[1:] - t[:-1] self.N = self.sigma*dW/numpy.sqrt(dt) x, v = u N = self.N[self.solver.n] return [v, N -self.b*v -self.c*x] from numpy import pi, linspace from matplotlib.pyplot import * import odespy problem = WhiteNoiseOscillator(b=0.1, c=pi**2, sigma=1) solvers = [odespy.Heun(problem.f), odespy.RK4(problem.f), odespy.ForwardEuler(problem.f)] for solver in solvers: f.connect_solver(solver) solver.set_initial_condition([0,0]) # start from rest T = 60 # with c=pi**2, the period is 1 u, t = solver.solve(linspace(0, T, 10001)) x = u[:,0] plot(t, x) hold(True) legend([str(s) for s in solvers]) savefig('tmppng'); savefig('tmp.pdf') show()