def test_heat_1d_constructor(): """ Test constructor """ x_start = 0 x_end = 1 nx = 11 a = 1 heat_1d = Heat1D(a=a, x_start=x_start, x_end=x_end, nx=nx, t_start=0, t_stop=1, nt=11) np.testing.assert_equal(heat_1d.x_start, x_start) np.testing.assert_equal(heat_1d.x_end, x_end) np.testing.assert_equal(heat_1d.nx, nx - 2) np.testing.assert_almost_equal(heat_1d.dx, 0.1) np.testing.assert_equal(heat_1d.x, np.linspace(x_start, x_end, nx)[1:-1]) np.testing.assert_equal(True, isinstance(heat_1d.vector_template, VectorHeat1D)) np.testing.assert_equal(True, isinstance(heat_1d.vector_t_start, VectorHeat1D)) np.testing.assert_equal(heat_1d.vector_t_start.get_values(), np.zeros(9))
def test_heat_1d_step(): """ Test step() """ x_start = 0 x_end = 1 nx = 6 a = 1 heat_1d = Heat1D(a=a, init_cond=lambda x: 2 * x, x_start=x_start, x_end=x_end, nx=nx, t_start=0, t_stop=1, nt=11) heat_1d_res = heat_1d.step(u_start=heat_1d.vector_t_start, t_start=0, t_stop=0.1) np.testing.assert_almost_equal(heat_1d_res.get_values(), np.array([0.28164, 0.51593599, 0.63660638, 0.53191933]))
def test_heat_equation_run(): """ Test one run for the heat equation """ heat0 = Heat1D(x_start=0, x_end=2, nx=5, a=1, rhs=rhs, init_cond=init_cond, t_start=0, t_stop=2, nt=65) heat1 = Heat1D(x_start=0, x_end=2, nx=5, a=1, rhs=rhs, init_cond=init_cond, t_start=0, t_stop=2, nt=17) heat2 = Heat1D(x_start=0, x_end=2, nx=5, a=1, rhs=rhs, init_cond=init_cond, t_start=0, t_stop=2, nt=5) problem = [heat0, heat1, heat2] mgrit = Mgrit(problem=problem, cf_iter=1, nested_iteration=True, max_iter=2, random_init_guess=False) res = mgrit.solve() result_conv = np.array([0.00267692, 0.00018053]) np.testing.assert_almost_equal(result_conv, res['conv'])
def test_split_into(): """ Test the function split_into """ heat0 = Heat1D(x_start=0, x_end=2, nx=5, a=1, rhs=rhs, init_cond=init_cond, t_start=0, t_stop=2, nt=2**2 + 1) result = np.array([4, 3, 3]) mgrit = Mgrit(problem=[heat0], transfer=[], nested_iteration=False) np.testing.assert_equal(result, mgrit.split_into(10, 3))
def test_time_stepping(): heat0 = Heat1D(x_start=0, x_end=2, nx=5, a=1, rhs=rhs, init_cond=init_cond, t_start=0, t_stop=2, nt=65) mgrit = Mgrit(problem=[heat0], cf_iter=1, nested_iteration=True, max_iter=2, random_init_guess=False) res = mgrit.solve() result_conv = np.array([]) np.testing.assert_almost_equal(result_conv, res['conv'])
def test_split_points(): """ Test the function split points """ heat0 = Heat1D(x_start=0, x_end=2, nx=5, a=1, rhs=rhs, init_cond=init_cond, t_start=0, t_stop=2, nt=2**2 + 1) result_proc0 = (4, 0) result_proc1 = (3, 4) result_proc2 = (3, 7) mgrit = Mgrit(problem=[heat0], nested_iteration=False) np.testing.assert_equal(result_proc0, mgrit.split_points(10, 3, 0)) np.testing.assert_equal(result_proc1, mgrit.split_points(10, 3, 1)) np.testing.assert_equal(result_proc2, mgrit.split_points(10, 3, 2))
def main(): def rhs(x, t): """ Right-hand side of 1D heat equation example problem at a given space-time point (x,t), -sin(pi*x)(sin(t) - a*pi^2*cos(t)), a = 1 Note: exact solution is np.sin(np.pi * x) * np.cos(t) :param x: spatial grid point :param t: time point :return: right-hand side of 1D heat equation example problem at point (x,t) """ return -np.sin(np.pi * x) * (np.sin(t) - 1 * np.pi**2 * np.cos(t)) def init_cond(x): """ Initial condition of 1D heat equation example, u(x,0) = sin(pi*x) :param x: spatial grid point :return: initial condition of 1D heat equation example problem """ return np.sin(np.pi * x) # Parameters t_start = 0 t_stop = 2 x_start = 0 x_end = 1 nx0 = 2**10 + 1 a = 1 nt = 2**10 + 1 # number of time points iterations = 1 # Set up multigrid hierarchy heat0 = Heat1D(x_start=x_start, x_end=x_end, nx=nx0, a=a, init_cond=init_cond, rhs=rhs, t_start=t_start, t_stop=t_stop, nt=nt) heat1 = Heat1D(x_start=x_start, x_end=x_end, nx=nx0, a=a, init_cond=init_cond, rhs=rhs, t_interval=heat0.t[::4]) heat2 = Heat1D(x_start=x_start, x_end=x_end, nx=nx0, a=a, init_cond=init_cond, rhs=rhs, t_interval=heat1.t[::4]) heat3 = Heat1D(x_start=x_start, x_end=x_end, nx=nx0, a=a, init_cond=init_cond, rhs=rhs, t_interval=heat2.t[::4]) heat4 = Heat1D(x_start=x_start, x_end=x_end, nx=nx0, a=a, init_cond=init_cond, rhs=rhs, t_interval=heat3.t[::4]) problem = [heat0, heat1, heat2, heat3, heat4] # Plots mgrit_plots = MgritWithPlots(problem=problem, cf_iter=1, cycle_type='V', random_init_guess=True, nested_iteration=False) mgrit_plots.plot_parallel_distribution(time_procs=4, text_size=13) mgrit_plots.plot_cycle(iterations=2, text_size=13) mgrit_plots.solve() mgrit_plots.plot_convergence(text_size=14) # Start timings problem = [heat0, heat1, heat2, heat3, heat4] runtime = np.zeros(6) iters = np.zeros(6) # V-cycle, FCF-relaxation for i in range(iterations): mgrit = Mgrit(problem=problem, cf_iter=1, cycle_type='V', random_init_guess=True, nested_iteration=False).solve() runtime[1] += mgrit['time_setup'] + mgrit['time_solve'] iters[1] += len(mgrit['conv']) # # V-cycle, FCFCF-relaxation, BDF1 for i in range(iterations): mgrit = Mgrit(problem=problem, cf_iter=2, cycle_type='V', random_init_guess=True, nested_iteration=False).solve() runtime[2] += mgrit['time_setup'] + mgrit['time_solve'] iters[2] += len(mgrit['conv']) # F-cycle, F-relaxation for i in range(iterations): mgrit = Mgrit(problem=problem, cf_iter=0, cycle_type='F', random_init_guess=True, nested_iteration=False).solve() runtime[3] += mgrit['time_setup'] + mgrit['time_solve'] iters[3] += len(mgrit['conv']) # F-cycle, FCF-relaxation for i in range(iterations): mgrit = Mgrit(problem=problem, cf_iter=1, cycle_type='F', random_init_guess=True, nested_iteration=False).solve() runtime[4] += mgrit['time_setup'] + mgrit['time_solve'] iters[4] += len(mgrit['conv']) # F-cycle, FCFCF-relaxation for i in range(iterations): mgrit = Mgrit(problem=problem, cf_iter=2, cycle_type='F', random_init_guess=True, nested_iteration=False).solve() runtime[5] += mgrit['time_setup'] + mgrit['time_solve'] iters[5] += len(mgrit['conv']) #Save and print results save_runtime = runtime / iterations save_iters = iters / iterations print('V-cycle with FCF-relaxation:', save_iters[1], save_runtime[1]) print('V-cycle with FCFCF-relaxation:', save_iters[2], save_runtime[2]) print('F-cycle with F-relaxation:', save_iters[3], save_runtime[3]) print('F-cycle with FCF-relaxation:', save_iters[4], save_runtime[4]) print('F-cycle with FCFCF-relaxation:', save_iters[5], save_runtime[5]) print(save_iters)
def main(): def rhs(x, t): """ Right-hand side of 1D heat equation example problem at a given space-time point (x,t), -sin(pi*x)(sin(t) - a*pi^2*cos(t)), a = 1 Note: exact solution is np.sin(np.pi * x) * np.cos(t) :param x: spatial grid point :param t: time point :return: right-hand side of 1D heat equation example problem at point (x,t) """ return -np.sin(np.pi * x) * (np.sin(t) - 1 * np.pi**2 * np.cos(t)) def init_cond(x): """ Initial condition of 1D heat equation example, u(x,0) = sin(pi*x) :param x: spatial grid point :return: initial condition of 1D heat equation example problem """ return np.sin(np.pi * x) def exact_sol(x, t): return np.sin(np.pi * x) * np.cos(t) def plot_error(mgrit, ts): fonts = 18 lw = 2 ms = 10 plt.rcParams["font.weight"] = "bold" plt.rcParams["axes.labelweight"] = "bold" fig1 = plt.figure(figsize=(15, 8)) ax1 = fig1.add_subplot(1, 1, 1) labels = [ 'V-cycle, FCF', 'V-cycle, FCFCF', 'F-cycle, F', 'F-cycle, FCF', 'F-cycle, FCFCF', ] colors = ['green', 'black', 'orange', 'yellow', 'purple'] mfc = ['green', 'black', 'white', 'white', 'white'] marker = ['s', 'D', 'o', 's', 'D'] linetype = ['-', '-', '--', '--', '--'] count = 1 save_vecs = [] for j in range(len(mgrit)): sol = mgrit[j].u[0] diffs_vector = np.zeros(len(sol)) for i in range(len(sol)): u_e = exact_sol(x=mgrit[j].problem[0].x, t=mgrit[j].problem[0].t[i]) diffs_vector[i] = np.linalg.norm(sol[i].get_values() - u_e, np.inf) save_vecs.append(diffs_vector.copy()) ax1.plot( heat0.t, diffs_vector, linetype[j], label=labels[j], color=colors[j], lw=lw, markevery=[], markeredgewidth=3, markerfacecolor=mfc[j], marker=marker[j], markersize=ms, ) count += 1 diffs_vector = np.zeros(len(sol)) for i in range(len(sol)): u_e = exact_sol(x=heat0.x, t=heat0.t[i]) diffs_vector[i] += abs(ts[i].get_values() - u_e).max() ax1.plot(heat0.t, diffs_vector, label='time-stepping', color='blue', lw=lw, markevery=[], markeredgewidth=3, markerfacecolor='blue', marker='x', markersize=ms) val = len(heat0.t) / 7 for i in range(5): ax1.plot(heat0.t, save_vecs[i], color=[0, 0, 0, 0], lw=lw, markevery=[int((i + 1) * val)], markeredgewidth=3, markerfacecolor=mfc[i], marker=marker[i], markeredgecolor=colors[i], markersize=ms) ax1.plot(heat0.t, diffs_vector, color=[0, 0, 0, 0], lw=lw, markevery=[int(6 * val)], markeredgewidth=3, markerfacecolor='blue', marker='x', markeredgecolor='blue', markersize=ms) ax1.set_xlabel('time', fontsize=fonts, weight='bold') ax1.set_ylabel('L-infinity norm of error', fontsize=fonts, weight='bold') ax1.tick_params(axis='both', which='major', labelsize=fonts) # , weight='bold') ax1.legend(loc='upper right', prop={'size': fonts, 'weight': 'bold'}) plt.savefig("example_1_error.png", bbox_inches='tight') plt.show() # Parameters t_start = 0 t_stop = 2 x_start = 0 x_end = 1 nx0 = 2**10 + 1 a = 1 nt = 2**10 + 1 # number of time points iterations = 1 # Set up multigrid hierarchy heat0 = Heat1D(x_start=x_start, x_end=x_end, nx=nx0, a=a, init_cond=init_cond, rhs=rhs, t_start=t_start, t_stop=t_stop, nt=nt) heat1 = Heat1D(x_start=x_start, x_end=x_end, nx=nx0, a=a, init_cond=init_cond, rhs=rhs, t_interval=heat0.t[::4]) heat2 = Heat1D(x_start=x_start, x_end=x_end, nx=nx0, a=a, init_cond=init_cond, rhs=rhs, t_interval=heat1.t[::4]) heat3 = Heat1D(x_start=x_start, x_end=x_end, nx=nx0, a=a, init_cond=init_cond, rhs=rhs, t_interval=heat2.t[::4]) heat4 = Heat1D(x_start=x_start, x_end=x_end, nx=nx0, a=a, init_cond=init_cond, rhs=rhs, t_interval=heat3.t[::4]) problem = [heat0, heat1, heat2, heat3, heat4] # Plots mgrit_plots = MgritWithPlots(problem=problem, cf_iter=1, cycle_type='V', random_init_guess=True, nested_iteration=False) mgrit_plots.plot_parallel_distribution(time_procs=4, text_size=13) mgrit_plots.plot_cycle(iterations=2, text_size=13) mgrit_plots.solve() mgrit_plots.plot_convergence(text_size=14) # Start timings runtime = np.zeros(5) iters = np.zeros(5) # V-cycle, FCF-relaxation for i in range(iterations): mgrit_V_FCF = Mgrit(problem=problem, cf_iter=1, cycle_type='V', random_init_guess=True, nested_iteration=False) res = mgrit_V_FCF.solve() runtime[0] += res['time_setup'] + res['time_solve'] iters[0] += len(res['conv']) # # V-cycle, FCFCF-relaxation, BDF1 for i in range(iterations): mgrit_V_FCFCF = Mgrit(problem=problem, cf_iter=2, cycle_type='V', random_init_guess=True, nested_iteration=False) res = mgrit_V_FCFCF.solve() runtime[1] += res['time_setup'] + res['time_solve'] iters[1] += len(res['conv']) # F-cycle, F-relaxation for i in range(iterations): mgrit_F_F = Mgrit(problem=problem, cf_iter=0, cycle_type='F', random_init_guess=True, nested_iteration=False) res = mgrit_F_F.solve() runtime[2] += res['time_setup'] + res['time_solve'] iters[2] += len(res['conv']) # F-cycle, FCF-relaxation for i in range(iterations): mgrit_F_FCF = Mgrit(problem=problem, cf_iter=1, cycle_type='F', random_init_guess=True, nested_iteration=False) res = mgrit_F_FCF.solve() runtime[3] += res['time_setup'] + res['time_solve'] iters[3] += len(res['conv']) # F-cycle, FCFCF-relaxation for i in range(iterations): mgrit_F_FCFCF = Mgrit(problem=problem, cf_iter=2, cycle_type='F', random_init_guess=True, nested_iteration=False) res = mgrit_F_FCFCF.solve() runtime[4] += res['time_setup'] + res['time_solve'] iters[4] += len(res['conv']) # Save and print results if MPI.COMM_WORLD.Get_rank() == 0: save_runtime = runtime / iterations save_iters = iters / iterations print('V-cycle with FCF-relaxation:', save_iters[0], save_runtime[0]) print('V-cycle with FCFCF-relaxation:', save_iters[1], save_runtime[1]) print('F-cycle with F-relaxation:', save_iters[2], save_runtime[2]) print('F-cycle with FCF-relaxation:', save_iters[3], save_runtime[3]) print('F-cycle with FCFCF-relaxation:', save_iters[4], save_runtime[4]) print(save_iters) if MPI.COMM_WORLD.Get_size() == 1: ts = [heat0.vector_t_start.clone()] for i in range(1, len(heat0.t)): ts.append( heat0.step(u_start=ts[-1], t_start=heat0.t[i - 1], t_stop=heat0.t[i])) plot_error(mgrit=[ mgrit_V_FCF, mgrit_V_FCFCF, mgrit_F_F, mgrit_F_FCF, mgrit_F_FCFCF ], ts=ts)
def main(): def rhs(x, t): """ Right-hand side of 1D heat equation example problem at a given space-time point (x,t), -sin(pi*x)(sin(t) - a*pi^2*cos(t)), a = 1 Note: exact solution is np.sin(np.pi * x) * np.cos(t) :param x: spatial grid point :param t: time point :return: right-hand side of 1D heat equation example problem at point (x,t) """ return -np.sin(np.pi * x) * (np.sin(t) - 1 * np.pi**2 * np.cos(t)) def init_cond(x): """ Initial condition of 1D heat equation example, u(x,0) = sin(pi*x) :param x: spatial grid point :return: initial condition of 1D heat equation example problem """ return np.sin(np.pi * x) heat0 = Heat1D(x_start=0, x_end=1, nx=1001, a=1, init_cond=init_cond, rhs=rhs, t_start=0, t_stop=2, nt=65) heat1 = Heat1D(x_start=0, x_end=1, nx=1001, a=1, init_cond=init_cond, rhs=rhs, t_start=0, t_stop=2, nt=33) heat2 = Heat1D(x_start=0, x_end=1, nx=1001, a=1, init_cond=init_cond, rhs=rhs, t_start=0, t_stop=2, nt=17) heat3 = Heat1D(x_start=0, x_end=1, nx=1001, a=1, init_cond=init_cond, rhs=rhs, t_start=0, t_stop=2, nt=9) heat4 = Heat1D(x_start=0, x_end=1, nx=1001, a=1, init_cond=init_cond, rhs=rhs, t_start=0, t_stop=2, nt=5) # Setup five-level MGRIT solver and solve the problem problem = [heat0, heat1, heat2, heat3, heat4] # Unitary C-relaxation weight mgrit = Mgrit(problem=problem, tol=1e-8, cf_iter=1, cycle_type='F', nested_iteration=False, max_iter=10, logging_lvl=20, random_init_guess=False) info = mgrit.solve() # Non-unitary C-relaxation weight mgrit2 = Mgrit(problem=problem, weight_c=1.3, tol=1e-8, cf_iter=1, cycle_type='F', nested_iteration=False, max_iter=10, logging_lvl=20, random_init_guess=False) info = mgrit2.solve()
def test_setup_points_and_comm_info(): """ Test for the function setup_points_and_comm_info """ heat0 = Heat1D(x_start=0, x_end=2, nx=5, a=1, rhs=rhs, init_cond=init_cond, t_start=0, t_stop=2, nt=65) heat1 = Heat1D(x_start=0, x_end=2, nx=5, a=1, rhs=rhs, init_cond=init_cond, t_start=0, t_stop=2, nt=17) heat2 = Heat1D(x_start=0, x_end=2, nx=5, a=1, rhs=rhs, init_cond=init_cond, t_start=0, t_stop=2, nt=5) problem = [heat0, heat1, heat2] mgrit = Mgrit(problem=problem, cf_iter=1, nested_iteration=True, max_iter=2) size = 7 cpts = [] comm_front = [] comm_back = [] block_size_this_lvl = [] index_local = [] index_local_c = [] index_local_f = [] first_is_c_point = [] first_is_f_point = [] last_is_c_point = [] last_is_f_point = [] send_to = [] get_from = [] for i in range(size): mgrit.comm_time_size = size mgrit.comm_time_rank = i mgrit.int_start = 0 # First time points of process interval mgrit.int_stop = 0 # Last time points of process interval mgrit.cpts = [ ] # C-points per process and level corresponding to complete time interval mgrit.comm_front = [] # Communication inside F-relax per MGRIT level mgrit.comm_back = [] # Communication inside F-relax per MGRIT level mgrit.block_size_this_lvl = [ ] # Block size per process and level with ghost point mgrit.index_local_c = [] # Local indices of C-Points mgrit.index_local_f = [] # Local indices of F-Points mgrit.index_local = [] # Local indices of all points mgrit.first_is_f_point = [] # Communication after C-relax mgrit.first_is_c_point = [] # Communication after F-relax mgrit.last_is_f_point = [] # Communication after F-relax mgrit.last_is_c_point = [] # Communication after C-relax mgrit.send_to = [] mgrit.get_from = [] for lvl in range(mgrit.lvl_max): mgrit.t.append(np.copy(mgrit.problem[lvl].t)) mgrit.setup_points_and_comm_info(lvl=lvl) cpts.append(mgrit.cpts) comm_front.append(mgrit.comm_front) comm_back.append(mgrit.comm_back) block_size_this_lvl.append(mgrit.block_size_this_lvl) index_local.append(mgrit.index_local) index_local_c.append(mgrit.index_local_c) index_local_f.append(mgrit.index_local_f) first_is_c_point.append(mgrit.first_is_c_point) first_is_f_point.append(mgrit.first_is_f_point) last_is_c_point.append(mgrit.last_is_c_point) last_is_f_point.append(mgrit.last_is_f_point) send_to.append(mgrit.send_to) get_from.append(mgrit.get_from) test_cpts = [[np.array([0, 4, 8]), np.array([0]), np.array([0])], [np.array([12, 16]), np.array([4]), np.array([1])], [ np.array([20, 24, 28]), np.array([], dtype=int), np.array([], dtype=int) ], [np.array([32, 36]), np.array([8]), np.array([2])], [ np.array([40, 44]), np.array([], dtype=int), np.array([], dtype=int) ], [np.array([48, 52]), np.array([12]), np.array([3])], [np.array([56, 60, 64]), np.array([16]), np.array([4])]] test_comm_front = [[False, False, False], [True, True, False], [False, False, False], [False, False, False], [True, True, False], [True, False, False], [False, True, False]] test_comm_back = [[True, True, False], [False, False, False], [False, False, False], [True, True, False], [True, False, False], [False, True, False], [False, False, False]] test_block_size_this_lvl = [[10, 3, 1], [11, 3, 2], [10, 4, 0], [10, 3, 2], [10, 3, 0], [10, 3, 2], [10, 4, 2]] test_index_local = [[ np.array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9]), np.array([0, 1, 2]), np.array([0]) ], [ np.array([1, 2, 3, 4, 5, 6, 7, 8, 9, 10]), np.array([1, 2]), np.array([1]) ], [ np.array([1, 2, 3, 4, 5, 6, 7, 8, 9]), np.array([1, 2, 3]), np.array([], dtype=int) ], [ np.array([1, 2, 3, 4, 5, 6, 7, 8, 9]), np.array([1, 2]), np.array([1]) ], [ np.array([1, 2, 3, 4, 5, 6, 7, 8, 9]), np.array([1, 2]), np.array([], dtype=int) ], [ np.array([1, 2, 3, 4, 5, 6, 7, 8, 9]), np.array([1, 2]), np.array([1]) ], [ np.array([1, 2, 3, 4, 5, 6, 7, 8, 9]), np.array([1, 2, 3]), np.array([1]) ]] test_index_local_f = [[ np.array([9, 5, 6, 7, 1, 2, 3]), np.array([1, 2]), np.array([], dtype=float) ], [ np.array([8, 9, 10, 4, 5, 6, 1, 2]), np.array([1]), np.array([], dtype=float) ], [ np.array([6, 7, 8, 2, 3, 4]), np.array([1, 2, 3]), np.array([], dtype=float) ], [ np.array([1, 2, 3, 9, 5, 6, 7]), np.array([2]), np.array([], dtype=float) ], [ np.array([8, 9, 4, 5, 6, 1, 2]), np.array([1, 2]), np.array([], dtype=float) ], [ np.array([7, 8, 9, 3, 4, 5, 1]), np.array([2]), np.array([], dtype=float) ], [ np.array([6, 7, 8, 2, 3, 4]), np.array([1, 2]), np.array([], dtype=float) ]] test_index_local_c = [[np.array([0, 4, 8]), np.array([0]), np.array([0])], [np.array([3, 7]), np.array([2]), np.array([1])], [ np.array([1, 5, 9]), np.array([], dtype=int), np.array([], dtype=int) ], [np.array([4, 8]), np.array([1]), np.array([1])], [ np.array([3, 7]), np.array([], dtype=int), np.array([], dtype=int) ], [np.array([2, 6]), np.array([1]), np.array([1])], [np.array([1, 5, 9]), np.array([3]), np.array([1])]] test_first_is_c_point = [[False, False, False], [False, False, False], [True, False, False], [False, True, False], [False, False, False], [False, True, False], [True, False, False]] test_first_is_f_point = [[False, False, False], [False, False, False], [False, True, False], [True, False, False], [False, False, False], [False, False, False], [False, False, False]] test_last_is_f_point = [[False, False, False], [True, False, False], [False, True, False], [False, False, False], [False, True, False], [True, False, False], [False, False, False]] test_last_is_c_point = [[False, False, False], [False, True, False], [True, False, False], [False, False, False], [False, False, False], [False, False, False], [False, False, False]] test_send_to = [[1, 1, 1], [2, 2, 3], [3, 3, -99], [4, 4, 5], [5, 5, -99], [6, 6, 6], [-99, -99, -99]] test_get_from = [[-99, -99, -99], [0, 0, 0], [1, 1, -99], [2, 2, 1], [3, 3, -99], [4, 4, 3], [5, 5, 5]] for i in range(size): assert all([ a == b for a, b in zip(first_is_c_point[i], test_first_is_c_point[i]) ]) assert all([ a == b for a, b in zip(first_is_f_point[i], test_first_is_f_point[i]) ]) assert all([ a == b for a, b in zip(last_is_f_point[i], test_last_is_f_point[i]) ]) assert all([ a == b for a, b in zip(last_is_c_point[i], test_last_is_c_point[i]) ]) assert all([a == b for a, b in zip(comm_front[i], test_comm_front[i])]) assert all([a == b for a, b in zip(comm_back[i], test_comm_back[i])]) assert all([ a == b for a, b in zip(block_size_this_lvl[i], test_block_size_this_lvl[i]) ]) assert all([a == b for a, b in zip(send_to[i], test_send_to[i])]) assert all([a == b for a, b in zip(get_from[i], test_get_from[i])]) [np.testing.assert_equal(a, b) for a, b in zip(cpts[i], test_cpts[i])] [ np.testing.assert_equal(a, b) for a, b in zip(index_local[i], test_index_local[i]) ] [ np.testing.assert_equal(a, b) for a, b in zip(index_local_c[i], test_index_local_c[i]) ] [ np.testing.assert_equal(a, b) for a, b in zip(index_local_f[i], test_index_local_f[i]) ]
def main(): def rhs(x, t): """ Right-hand side of 1D heat equation example problem at a given space-time point (x,t), -sin(pi*x)(sin(t) - a*pi^2*cos(t)), a = 1 Note: exact solution is np.sin(np.pi * x) * np.cos(t) :param x: spatial grid point :param t: time point :return: right-hand side of 1D heat equation example problem at point (x,t) """ return -np.sin(np.pi * x) * (np.sin(t) - 1 * np.pi**2 * np.cos(t)) def init_cond(x): """ Initial condition of 1D heat equation example, u(x,0) = sin(pi*x) :param x: spatial grid point :return: initial condition of 1D heat equation example problem """ return np.sin(np.pi * x) # Construct a four-level multigrid hierarchy for the 1d heat example # * use a coarsening factor of 2 in time on all levels # * apply spatial coarsening by a factor of 2 on the first two levels heat0 = Heat1D(x_start=0, x_end=2, nx=2**4 + 1, a=1, rhs=rhs, init_cond=init_cond, t_start=0, t_stop=2, nt=2**7 + 1) heat1 = Heat1D(x_start=0, x_end=2, nx=2**3 + 1, a=1, rhs=rhs, init_cond=init_cond, t_interval=heat0.t[::2]) heat2 = Heat1D(x_start=0, x_end=2, nx=2**2 + 1, a=1, rhs=rhs, init_cond=init_cond, t_interval=heat1.t[::2]) heat3 = Heat1D(x_start=0, x_end=2, nx=2**2 + 1, a=1, rhs=rhs, init_cond=init_cond, t_interval=heat2.t[::2]) problem = [heat0, heat1, heat2, heat3] # Specify a list of grid transfer operators of length (#levels - 1) for the transfer between two consecutive levels # * Use the new class GridTransferHeat to apply spatial coarsening for transfers between the first three levels # * Use PyMGRIT's core class GridTransferCopy for the transfer between the last two levels (no spatial coarsening) transfer = [GridTransferHeat(), GridTransferHeat(), GridTransferCopy()] # Setup four-level MGRIT solver and solve the problem mgrit = Mgrit(problem=problem, transfer=transfer) info = mgrit.solve()