def test_solver_exact_discrete_solution(): def tilde_w(w, dt): return (2. / dt) * asin(w * dt / 2.) def u_numerical_exact(t): return I * cos(tilde_w(w, dt) * t) w = 2.5 I = 1.5 # Estimate period and time step P = 2 * pi / w num_periods = 4 T = num_periods * P N = 5 # time steps per period dt = P / N u, t = solver(I, w, dt, T) u_e = u_numerical_exact(t) error = abs(u_e - u).max() # Make a plot in a file, but not on the screen from scitools.std import plot plot(t, u, 'bo', t, u_e, 'r-', legend=('numerical', 'exact'), show=False, savefig='tmp.png') assert error < 1E-14
def run_simulations(N, dt0, num_periods): """ Run N simulations where the time step is halved in each simulation, starting with dt0. Make subdirectories tmp_case0, tmp_case1, etc with plot files for each simulation (tmp_*.png). """ for i in range(N): dt = dt0 / 2.0**i u, t = solver(I=1, w=2 * pi, dt=dt, T=num_periods) # visualize_front removes all old plot files :) visualize_front(u, t, I=1, w=2 * pi, savefig=True, skip_frames=2**i) # skip_frames is essential: for N=4 we have to store # only each 2**4=16-th file to get as many files # as for the dt0 simulation! # Move all plot files tmp_*.png for movie to a # separate directory. Delete that directory if it # exists and recreate it. dirname = 'tmp_case%d' % i if os.path.isdir(dirname): shutil.rmtree(dirname) # remove directory (tree) os.mkdir(dirname) # make new directory for filename in glob.glob('tmp_*.png'): # Move file to subdirectory dirname os.rename(filename, os.path.join(dirname, filename))
def test_solver_exact_discrete_solution(): def tilde_w(w, dt): return (2.0 / dt) * asin(w * dt / 2.0) def u_numerical_exact(t): return I * cos(tilde_w(w, dt) * t) w = 2.5 I = 1.5 # Estimate period and time step P = 2 * pi / w num_periods = 4 T = num_periods * P N = 5 # time steps per period dt = P / N u, t = solver(I, w, dt, T) u_e = u_numerical_exact(t) error = abs(u_e - u).max() # Make a plot in a file, but not on the screen from scitools.std import plot plot(t, u, "bo", t, u_e, "r-", legend=("numerical", "exact"), show=False, savefig="tmp.png") assert error < 1e-14
def run_simulations(N, dt0, num_periods): """ Run N simulations where the time step is halved in each simulation, starting with dt0. Make subdirectories tmp_case0, tmp_case1, etc with plot files for each simulation (tmp_*.png). """ for i in range(N): dt = dt0/2.0**i u, t = solver(I=1, w=2*pi, dt=dt, T=num_periods) # visualize_front removes all old plot files :) visualize_front(u, t, I=1, w=2*pi, savefig=True, skip_frames=2**i) # skip_frames is essential: for N=4 we have to store # only each 2**4=16-th file to get as many files # as for the dt0 simulation! # Move all plot files tmp_*.png for movie to a # separate directory. Delete that directory if it # exists and recreate it. dirname = 'tmp_case%d' % i if os.path.isdir(dirname): shutil.rmtree(dirname) # remove directory (tree) os.mkdir(dirname) # make new directory for filename in glob.glob('tmp_*.png'): # Move file to subdirectory dirname os.rename(filename, os.path.join(dirname, filename))
def test_solver_memsave(): from vib_undamped import solver _, _ = solver_memsave(I=1, dt=0.1, w=1, T=30) u_expected, _ = solver (I=1, dt=0.1, w=1, T=30) data = np.loadtxt('tmp.dat') u_computed = data[:,1] diff = np.abs(u_expected - u_computed).max() assert diff < 5E-13, diff
def test_solver_memsave(): from vib_undamped import solver _, _ = solver_memsave(I=1, dt=0.1, w=1, T=30) u_expected, _ = solver(I=1, dt=0.1, w=1, T=30) data = np.loadtxt('tmp.dat') u_computed = data[:, 1] diff = np.abs(u_expected - u_computed).max() assert diff < 5E-13, diff
def run_solver_and_plot(solver, timesteps_per_period=20, num_periods=1, I=1, w=2*np.pi): P = 2*np.pi/w # duration of one period dt = P/timesteps_per_period Nt = num_periods*timesteps_per_period T = Nt*dt t_mesh = np.linspace(0, T, Nt+1) solver.set(f_kwargs={'w': w}) solver.set_initial_condition([0, I]) u, t = solver.solve(t_mesh) from vib_undamped import solver u2, t2 = solver(I, w, dt, T) plt.plot(t, u[:,1], 'r-', t2, u2, 'b-') plt.legend(['Euler-Cromer', '2nd-order ODE']) plt.xlabel('t'); plt.ylabel('u') plt.savefig('tmp1.png'); plt.savefig('tmp1.pdf')
def run_solver_and_plot(solver, timesteps_per_period=20, num_periods=1, I=1, w=2 * np.pi): P = 2 * np.pi / w # duration of one period dt = P / timesteps_per_period Nt = num_periods * timesteps_per_period T = Nt * dt t_mesh = np.linspace(0, T, Nt + 1) solver.set(f_kwargs={'w': w}) solver.set_initial_condition([0, I]) u, t = solver.solve(t_mesh) from vib_undamped import solver u2, t2 = solver(I, w, dt, T) plt.plot(t, u[:, 1], 'r-', t2, u2, 'b-') plt.legend(['Euler-Cromer', '2nd-order ODE']) plt.xlabel('t') plt.ylabel('u') plt.savefig('tmp1.png') plt.savefig('tmp1.pdf')
from vib_undamped import solver, plot_empirical_freq_and_amplitude from math import pi dt_values = [0.1, 0.05, 0.01] u_cases = [] t_cases = [] for dt in dt_values: # Simulate scaled problem for 40 periods u, t = solver(I=1, w=2 * pi, dt=dt, T=40) u_cases.append(u) t_cases.append(t) plot_empirical_freq_and_amplitude(u_cases, t_cases, I=1, w=2 * pi) raw_input()
from vib_undamped import solver, plot_empirical_freq_and_amplitude from math import pi dt_values = [0.1, 0.05, 0.01] u_cases = [] t_cases = [] for dt in dt_values: # Simulate scaled problem for 40 periods u, t = solver(I=1, w=2*pi, dt=dt, T=40) u_cases.append(u) t_cases.append(t) plot_empirical_freq_and_amplitude(u_cases, t_cases, I=1, w=2*pi) raw_input()
# -*- coding: utf-8 -*- from vib_undamped import solver, plot_empirical_freq_and_amplitude from math import pi tau_values = [0.1, 0.5, 0.01] u_cases = [] t_cases = [] for tau in tau_values: # Расчитываем безразмерную модель для 40 периодов u, t = solver(U=1, omega=2 * pi, tau=tau, T=40) u_cases.append(u) t_cases.append(t) plot_empirical_freq_and_amplitude(u_cases, t_cases, U=1, omega=2 * pi)