import multigrid from ntiling import TilingBuilder from drawing.pil_draw_simple import Draw palette = [ "#7fb414", "#fac70b", "#127681", "#df0e62", "#21174a", ] draw = Draw(scale=150, width=3 * 1280, height=3 * 1280, bg_color=palette[-1]) draw.line_color = None index_range = (-2, 2) grid = multigrid.Multigrid(9) tiling_builder = TilingBuilder(grid) tiling_builder.prepare_grid(index_range) tiling_builder.generate_rhomb_list() for rhomb in tiling_builder._rhombs.values(): col = (rhomb.type() in (4, 5, 3, 6, 2, 7)) + (rhomb.type() in ( 3, 6, 2, 7)) + (rhomb.type() in (2, 7)) if col != 0: new = [] for a, b in rhomb.get_edges(): new.append((a.get_xy(form="xy1") + b.get_xy(form="xy1")) / 2) # coords = rhomb.center(form="xy") draw.polygon(new, color=palette[col]) else: draw.polygon(rhomb.xy(form="xy1"), color=palette[col])
# the analytic solution return -np.sin(x) + x * np.sin(1.0) def f(x): # the righthand side return np.sin(x) # test the multigrid solver nx = 128 # create the multigrid object a = multigrid.Multigrid(nx, bc_left_type="dirichlet", bc_right_type="dirichlet", verbose=1, true_function=true) # initialize the solution to 0 a.init_solution() # initialize the RHS using the function f a.init_rhs(f(a.x)) # solve to a relative tolerance of 1.e-11 elist, rlist = a.solve(rtol=1.e-11) ncycle = np.arange(len(elist)) + 1 # get the solution
import multigrid from ntiling import TilingBuilder from drawing.pil_draw_simple import Draw palette = [ "#293462", "#216583", "#00818a", "#f7be16", ] draw = Draw(scale=90, width=3 * 1280, height=3 * 1280, bg_color=palette[-2]) draw.line_color = None index_range = (-1, 1) grid = multigrid.Multigrid(23) tiling_builder = TilingBuilder(grid) tiling_builder.prepare_grid(index_range) tiling_builder.generate_rhomb_list() for rhomb in tiling_builder._rhombs.values(): for a, b in rhomb.get_edges(): draw.edge(a.get_xy(homogenous=False), b.get_xy(homogenous=False), color=palette[-1], width=4) draw.show()
import multigrid from ntiling import TilingBuilder from drawing.pil_draw_simple import Draw palette = [ "#ffee7d", "#b767ff", "#44fadd", "#ffe0f7", "#fe91ca", ] draw = Draw(scale=110, width=3 * 1280, height=3 * 1280, bg_color=palette[-2]) draw.line_color = None index_range = (-3, 3) grid = multigrid.Multigrid(17) tiling_builder = TilingBuilder(grid) tiling_builder.prepare_grid(index_range) tiling_builder.generate_rhomb_list() for rhomb in tiling_builder._rhombs.values(): new = [] for a, b in rhomb.get_edges(): new.append((a.get_xy() + b.get_xy()) / 2) p1 = new[-1] for p2 in new: draw.edge(p2[:2], p1[:2], color=palette[-1], width=8) p1 = p2 draw.show()
# palette = [ # "#7fb414", # "#df0e62", # "#127681", # "#fac70b", # "#092a35", # ] draw = Draw(scale=90, width=4 * 1280, height=4 * 1280, bg_color=palette[-1]) draw.line_color = None index_range = (-4, 4) offsets = list( map( float, "0.04885131 0.38705046 0.15540683 0.37524718 0.09360688 0.04554864 0.0424169" .split(" "))) grid = multigrid.Multigrid(7, offsets) # grid = multigrid.Multigrid(7, [0.11071195, 0.40178219, 0.38167641, 0.05840904, 0.26593674, 0.30876262, 0.40169052]) tiling_builder = TilingBuilder(grid) tiling_builder.prepare_grid(index_range) tiling_builder.generate_rhomb_list() for rhomb in tiling_builder._rhombs.values(): c = (rhomb.type() in (1, 6, 2, 5)) + (rhomb.type() in (1, 6)) # c = (rhomb.type() in (1, 2, 5, 6)) + (rhomb.type() in (1, 6)) draw.polygon(rhomb.xy(), color=palette[c]) for a, b in rhomb.get_edges(): draw.edge(a.get_xy(homogenous=False), b.get_xy(homogenous=False), color=palette[-2], width=6)