def f(u, t): return -a * u I = 1 a = 2 T = 6 dt = float(sys.argv[1]) if len(sys.argv) >= 2 else 0.75 Nt = int(round(T / dt)) t_mesh = np.linspace(0, Nt * dt, Nt + 1) solvers = [ odespy.RK2(f), odespy.RK3(f), odespy.RK4(f), # BackwardEuler must use Newton solver to converge # (Picard is default and leads to divergence) odespy.BackwardEuler(f, nonlinear_solver='Newton') ] legends = [] for solver in solvers: solver.set_initial_condition(I) u, t = solver.solve(t_mesh) plt.plot(t, u) plt.hold('on') legends.append(solver.__class__.__name__)
k3 = func(z[i, :] + k2 * dt2, t + dt2) # predictor step 3 k4 = func(z[i, :] + k3 * dt, t + dt) # predictor step 4 z[i + 1, :] = z[i, :] + dt / 6.0 * (k1 + 2.0 * k2 + 2.0 * k3 + k4 ) # Corrector step return z # Main program starts here from numpy import linspace T = 10 # end of simulation N = 20 # no of time steps time = linspace(0, T, N + 1) solvers = [] solvers.append(odespy.RK3(f2)) solvers.append(odespy.RK4(f2)) legends = [] z0 = np.zeros(2) z0[0] = 2.0 for i, solver in enumerate(solvers): solver.set_initial_condition(z0) z, t = solver.solve(time) plot(t, z[:, 1]) legends.append(str(solver)) scheme_list = [euler, heun, rk4_own]
plt.hold('on') plt.xlabel('Number of periods') plt.ylabel('Amplitude (absolute value)') plt.legend(loc='upper left') plt.savefig(file_name + '.png') plt.savefig(file_name + '.pdf') plt.show() # Define different sets of experiments solvers_CNB2 = [ odespy.CrankNicolson(f, nonlinear_solver='Newton'), odespy.Backward2Step(f) ] solvers_RK34 = [odespy.RK3(f), odespy.RK4(f)] solvers_AB = [odespy.AdamsBashforth2(f), odespy.AdamsBashforth3(f)] if __name__ == '__main__': # Default values timesteps_per_period = 30 solver_collection = 'CNB2' num_periods = 100 # Override from command line try: # Example: python vib_undamped_odespy.py 30 RK34 50 timesteps_per_period = int(sys.argv[1]) solver_collection = sys.argv[2] num_periods = int(sys.argv[3]) except IndexError: pass # default values are ok
def fblasius(y, x): """ODE-system for the Blasius-equation""" return [y[1],y[2], -y[0]*y[2]] import odespy solvers=[] solvers.append(odespy.RK4(fblasius)) solvers.append(odespy.RK2(fblasius)) solvers.append(odespy.RK3(fblasius)) from numpy import linspace, exp xmin = 0 xmax = 5.75 N = 150 # no x-values xspan = linspace(xmin, xmax, N+1) smin=0.1 smax=0.8 Ns=30 srange = linspace(smin,smax,Ns) from matplotlib.pyplot import * #change some default values to make plots more readable on the screen LNWDT=5; FNT=25 matplotlib.rcParams['lines.linewidth'] = LNWDT; matplotlib.rcParams['font.size'] = FNT figure() legends=[] linet=['r-',':','.','-.','--']
odespy.AdamsBashforth2(problem), odespy.AdamsBashforth3(problem), odespy.AdamsBashforth4(problem), odespy.AdaptiveResidual(problem, solver='Euler'), odespy.Backward2Step(problem), odespy.BackwardEuler(problem), odespy.Dop853(problem, rtol=rtol, atol=atol), odespy.Dopri5(problem, rtol=rtol, atol=atol), odespy.Euler(problem), odespy.Heun(problem), odespy.Leapfrog(problem), odespy.LeapfrogFiltered(problem), odespy.MidpointImplicit(problem), odespy.MidpointIter(problem, max_iter=10, eps_iter=1E-7), odespy.RK2(problem), odespy.RK3(problem), odespy.RK4(problem), odespy.RKFehlberg(problem, rtol=rtol, atol=atol), odespy.SymPy_odefun(problem), odespy.ThetaRule(problem), odespy.Trapezoidal(problem), odespy.Vode(problem, rtol=rtol, atol=atol, adams_or_bdf=adams_or_bdf), ] theta_exact = lambda t: problem.Theta * numpy.cos(sqrt(problem.c) * t) import sys, time try: num_periods = int(sys.argv[1]) except IndexError: num_periods = 30 # default