def sdc_regression_test(self): M = 2 P = 5 tstart = 0.0 tend = 0.25 nsteps = 5 dt = (tend - tstart) / float(nsteps) prob = problem_model(a=1.0, nu=1.0, alpha=2.0, v0=0.25) K_iter = 12 u0 = [2.0, 0.0, 0.0, 0.0, 0.0] for n in range(nsteps): tstart = float(n) * dt tend = float(n + 1) * dt sdc = sdc_step(M, P, tstart, tend, prob) # reset buffers to zero u = np.zeros((M, prob.dim)) usub = np.zeros((M, P, prob.dim)) fu = np.zeros((M, prob.dim)) fu_sub = np.zeros((M, P, prob.dim)) sdc.predict(u0, u, usub, fu, fu_sub) for k in range(K_iter): sdc.sweep(u0, u, usub, fu, fu_sub) u0 = u[M - 1] ### file = open('sdc-model-regression.txt') val = [] for line in file: val = np.append(val, float(line)) defect = np.linalg.norm(val - u0, np.inf) assert defect < 1e-15, ( "Regression test failed, different from previous version. Defect: %5.3e" % defect)
def sdc_regression_test(self): M = 2 P = 5 tstart = 0.0 tend = 0.25 nsteps = 5 dt = (tend - tstart)/float(nsteps) prob = problem_model(a=1.0, nu=1.0, alpha=2.0, v0=0.25) K_iter = 12 u0 = [2.0, 0.0, 0.0, 0.0, 0.0] for n in range(nsteps): tstart = float(n)*dt tend = float(n+1)*dt sdc = sdc_step(M, P, tstart, tend, prob) # reset buffers to zero u = np.zeros((M,prob.dim)) usub = np.zeros((M,P,prob.dim)) fu = np.zeros((M,prob.dim)) fu_sub = np.zeros((M,P,prob.dim)) sdc.predict(u0, u, usub, fu, fu_sub) for k in range(K_iter): sdc.sweep(u0, u, usub, fu, fu_sub) u0 = u[M-1] ### file = open('sdc-model-regression.txt') val = [] for line in file: val = np.append(val, float(line)) defect = np.linalg.norm(val - u0, np.inf) assert defect < 1e-15, ("Regression test failed, different from previous version. Defect: %5.3e" % defect)
def test_converge_to_fixpoint(self): self.prob = problem_model(a=-0.1, nu=-1.0, alpha=1.0, v0=1.0) self.M = 3 self.P = 4 tstart = 0.0 tend = 0.2 self.sdc = sdc_step(self.M, self.P, tstart, tend, self.prob) u0 = np.reshape([2.0, 1.0, 0.0, 1.0, 0.0], (self.prob.dim, )) u = np.zeros((self.M, self.prob.dim)) usub = np.zeros((self.M, self.P, self.prob.dim)) fu = np.zeros((self.M, self.prob.dim)) fu_sub = np.zeros((self.M, self.P, self.prob.dim)) # run predictor self.sdc.predict(u0, u, usub, fu, fu_sub) for k in range(25): u_ = copy.deepcopy(u) usub_ = copy.deepcopy(usub) # run standard node sweep... self.sdc.sweep(u0, u, usub, fu, fu_sub) update_standard = np.linalg.norm((u - u_).flatten(), np.inf) update_embedded = np.linalg.norm((usub - usub_).flatten(), np.inf) res_standard = self.sdc.residual(u0, u) res_embedded = self.sdc.sub_residual(u0, usub) fu_sub_ = copy.deepcopy(fu_sub) assert update_standard < 1e-12, ( "Standard update failed to converge to zero. Value: %5.3e" % update_standard) assert update_embedded < 1e-12, ( "Embedded update failed to converge to zero. Value: %5.3e" % update_embedded) assert res_standard < 1e-12, ( "Standard residual failed to converge to zero. Value: %5.3e" % res_standard) assert res_embedded < 1e-12, ( "Embedded residual failed to converge to zero. Value: %5.3e" % res_embedded) # Continue test to validate collocation update formula u0_sub = u0 for m in range(self.M): u0_coll = self.sdc.collocation_update(u0_sub, u, usub, m) u0_sub = usub[m, -1] err = np.linalg.norm(u0_coll - u0_sub) assert err < 1e-12, ( "For collocation solution, update formula failed to reproduce last stage. Error: %5.3e" % err)
def test_converge_to_fixpoint(self): self.prob = problem_model(a = -0.1, nu = -1.0, alpha = 1.0, v0 = 1.0) self.M = 3 self.P = 4 tstart = 0.0 tend = 0.2 self.sdc = sdc_step(self.M, self.P, tstart, tend, self.prob) u0 = np.reshape([2.0, 1.0, 0.0, 1.0, 0.0], (self.prob.dim,)) u = np.zeros((self.M,self.prob.dim)) usub = np.zeros((self.M,self.P,self.prob.dim)) fu = np.zeros((self.M,self.prob.dim)) fu_sub = np.zeros((self.M,self.P,self.prob.dim)) # run predictor self.sdc.predict(u0, u, usub, fu, fu_sub) for k in range(25): u_ = copy.deepcopy(u) usub_ = copy.deepcopy(usub) # run standard node sweep... self.sdc.sweep(u0, u, usub, fu, fu_sub) update_standard = np.linalg.norm( (u-u_).flatten(), np.inf) update_embedded = np.linalg.norm( (usub-usub_).flatten(), np.inf) res_standard = self.sdc.residual(u0, u) res_embedded = self.sdc.sub_residual(u0, usub) fu_sub_ = copy.deepcopy(fu_sub) assert update_standard<1e-12, ("Standard update failed to converge to zero. Value: %5.3e" % update_standard) assert update_embedded<1e-12, ("Embedded update failed to converge to zero. Value: %5.3e" % update_embedded) assert res_standard<1e-12, ("Standard residual failed to converge to zero. Value: %5.3e" % res_standard) assert res_embedded<1e-12, ("Embedded residual failed to converge to zero. Value: %5.3e" % res_embedded) # Continue test to validate collocation update formula u0_sub = u0 for m in range(self.M): u0_coll = self.sdc.collocation_update(u0_sub, u, usub, m) u0_sub = usub[m,-1] err = np.linalg.norm(u0_coll - u0_sub) assert err<1e-12, ("For collocation solution, update formula failed to reproduce last stage. Error: %5.3e" % err)
tend = 1.0 nue=500 a = 1.0 alpha_vec = np.linspace(0.0, 60.0, 120) nu_vec = np.linspace(0.0, nue, 50) P_vec = [8] #range(M,M+11) stab = np.zeros((np.size(alpha_vec), np.size(nu_vec))) theta=0.25 K_iter = 2 for P in P_vec: for kk in range(np.size(alpha_vec)): for ll in range(np.size(nu_vec)): prob = problem_model(a=a, nu=nu_vec[ll], alpha=alpha_vec[kk], v0=0.0) sdc = sdc_step(M, P, 0.0, 1.0, prob, theta) R = np.zeros((prob.dim,prob.dim)) u0 = np.eye(prob.dim) for mm in range(prob.dim): #### run multirate SDC u = np.zeros((M,prob.dim)) usub = np.zeros((M,P,prob.dim)) u_ = np.zeros((M,prob.dim)) usub_ = np.zeros((M,P,prob.dim)) fu = np.zeros((M,prob.dim)) fu_sub = np.zeros((M,P,prob.dim))
tend = 2.5 nsteps = [5, 10, 15, 20, 25, 30, 35, 40, 45, 50, 55, 60, 65, 70] K_iter = [2, 3, 4] err = np.zeros((np.size(K_iter),np.size(nsteps))) err_ros2 = np.zeros(np.size(nsteps)) err_std = np.zeros((np.size(K_iter),np.size(nsteps))) order = np.zeros((np.size(K_iter),np.size(nsteps))) fs = 8 a = 1.0 nu = 1.1 alpha = 1.0e-2 v0 = 0.25 prob = problem_model(a, nu, alpha, v0) u_ex = np.zeros(prob.dim) u0 = np.zeros(prob.dim) u0[0] = 1.0 sol = odeint(prob.f, u0, [0.0, tend], rtol=1e-12, atol=1e-12) u_ex = sol[-1] for kk in range(np.size(K_iter)): order_p = np.min([K_iter[kk], 2*M-1, 2*P-1]) for ll in range(np.size(nsteps)): dt = (tend - tstart)/float(nsteps[ll]) u0 = np.zeros(prob.dim)
a = 1.0 alpha_vec = np.linspace(0.0, 35.0, 10) nu_vec = np.linspace(0.0, nue, 10) stab = np.zeros((np.size(alpha_vec), np.size(nu_vec))) stab_ros = np.zeros((np.size(alpha_vec), np.size(nu_vec))) stab_std = np.zeros((np.size(alpha_vec), np.size(nu_vec))) stab_c = np.zeros((np.size(alpha_vec), np.size(nu_vec))) K_iter = 2 for kk in range(np.size(alpha_vec)): for ll in range(np.size(nu_vec)): prob = problem_model(a=a, nu=nu_vec[ll], alpha=alpha_vec[kk], v0=0.0) eigv, eigvec = np.linalg.eig(prob.S + prob.get_mat1(0.1)) stab_c[kk, ll] = np.max(eigv) sdc = sdc_step(M, P, 0.0, 1.0, prob, 0.0) sdc_std = sdc_standard_step(M, 0.0, 1.0, prob) ros = ros2_step(0.0, 1.0, prob) R = np.zeros((prob.dim, prob.dim)) R_ros = np.zeros((prob.dim, prob.dim)) R_std = np.zeros((prob.dim, prob.dim)) u0 = np.eye(prob.dim) u0_ros = np.eye(prob.dim) u0_std = np.eye(prob.dim)
tend = 2.5 nsteps = [5, 10, 15, 20, 25, 30, 35, 40, 45, 50, 55, 60, 65, 70] K_iter = [2, 3, 4] err = np.zeros((np.size(K_iter), np.size(nsteps))) err_ros2 = np.zeros(np.size(nsteps)) err_std = np.zeros((np.size(K_iter), np.size(nsteps))) order = np.zeros((np.size(K_iter), np.size(nsteps))) fs = 8 a = 1.0 nu = 1.1 alpha = 1.0e-2 v0 = 0.25 prob = problem_model(a, nu, alpha, v0) u_ex = np.zeros(prob.dim) u0 = np.zeros(prob.dim) u0[0] = 1.0 sol = odeint(prob.f, u0, [0.0, tend], rtol=1e-12, atol=1e-12) u_ex = sol[-1] for kk in range(np.size(K_iter)): order_p = np.min([K_iter[kk], 2 * M - 1, 2 * P - 1]) for ll in range(np.size(nsteps)): dt = (tend - tstart) / float(nsteps[ll]) u0 = np.zeros(prob.dim)
u+= 1.0/np.pi*( np.cos(float(k)*a*t)*np.cos(float(k)*x) + np.sin(float(k)*a*t)*np.sin(float(k)*x) ) return u M = 3 P = 5 tstart = 0.0 tend = 0.25 nsteps = 5 dt = (tend - tstart)/float(nsteps) Nx = 50 xaxis = np.linspace(0, 2*np.pi, Nx+1) xaxis = xaxis[0:Nx] prob = problem_model(a=1.0, nu=1.0, alpha=2.0, v0=0.25) K_iter = 12 u0 = [2.0, 0.0, 0.0, 0.0, 0.0] u_plot = np.zeros((nsteps+1,prob.dim)) u_euler = np.zeros((nsteps+1, prob.dim)) t_axis = np.zeros(nsteps+1) t_axis[0] = tstart u_plot[0] = u0 u_euler[0] = u0 fig = plt.figure() for n in range(nsteps): tstart = float(n)*dt tend = float(n+1)*dt