Example #1
0
def method(Lx=1.,
           Ly=1.,
           scale=0.75,
           dx=0.02,
           show=False,
           polygon="flipper",
           center=(0.5, 0.5),
           **kwargs):
    edges = np.loadtxt(os.path.join(MESHES_DIR, polygon + ".edges"),
                       dtype=int).tolist()
    nodes = np.loadtxt(os.path.join(MESHES_DIR, polygon + ".nodes"))

    nodes[:, 0] -= 0.5 * np.max(nodes[:, 0])
    nodes[:, 1] -= 0.5 * np.max(nodes[:, 1])
    nodes[:, :] *= scale
    nodes[:, 0] += center[0] * Lx
    nodes[:, 1] += center[1] * Ly

    nodes = nodes.tolist()

    x_min, x_max = 0., Lx
    y_min, y_max = 0., Ly

    corner_pts = [(x_min, y_min), (x_max, y_min), (x_max, y_max),
                  (x_min, y_max)]

    outer_nodes, outer_edges = make_polygon(corner_pts, dx, len(nodes))
    nodes.extend(outer_nodes)
    edges.extend(outer_edges)

    if show:
        plot_edges(nodes, edges)

    mi = tri.MeshInfo()
    mi.set_points(nodes)
    mi.set_facets(edges)
    mi.set_holes([(center[0] * Lx, center[1] * Ly)])

    max_area = 0.5 * dx**2

    mesh = tri.build(mi,
                     max_volume=max_area,
                     min_angle=25,
                     allow_boundary_steiner=False)

    coords = np.array(mesh.points)
    faces = np.array(mesh.elements)

    if show:
        plot_faces(coords, faces)

    mesh = numpy_to_dolfin(coords, faces)

    if show:
        df.plot(mesh)
        plt.show()

    mesh_path = os.path.join(MESHES_DIR,
                             "{}_dx{}_Lx{}_Ly{}".format(polygon, dx, Lx, Ly))
    store_mesh_HDF5(mesh, mesh_path)
Example #2
0
def method(ts, show=True, save_fig=False, latex=False, **kwargs):
    """ Mesh info and plot. """
    info_cyan("Mesh info and plot.")
    f = df.Function(ts.function_space)
    f.vector()[:] = 1.
    area = df.assemble(f*df.dx)

    info("Number of nodes:    {}".format(len(ts.nodes)))
    info("Number of elements: {}".format(len(ts.elems)))
    info("Total mesh area:    {}".format(area))
    info("Mean element area:  {}".format(area/len(ts.elems)))

    if rank == 0:
        save_fig_file = None
        if save_fig:
            save_fig_file = os.path.join(ts.plots_folder,
                                         "mesh.png")

        plot_faces(ts.nodes, ts.elems, title="Mesh",
                   save=save_fig_file, show=show, latex=latex)
def method(Lx=6.,
           Ly=4.,
           Lx_inner=4.,
           num_obstacles=32,
           rad=0.2,
           R=0.3,
           dx=0.05,
           seed=121,
           do_plot=True,
           **kwargs):
    N = int(np.ceil(Lx / dx))

    x_min, x_max = -Lx / 2, Lx / 2
    y_min, y_max = -Ly / 2, Ly / 2

    y = np.linspace(y_min, y_max, N).flatten()

    pts = np.zeros((num_obstacles, 2))
    diam2 = 4 * R**2

    np.random.seed(seed)

    for i in range(num_obstacles):
        while True:
            pt = (np.random.rand(2) - 0.5) * np.array([Lx_inner, Ly])
            if i == 0:
                break
            dist = pts[:i, :] - np.outer(np.ones(i), pt)
            for j in range(len(dist)):
                if abs(dist[j, 1]) > Ly / 2:
                    dist[j, 1] = abs(dist[j, 1]) - Ly
            dist2 = dist[:, 0]**2 + dist[:, 1]**2
            if all(dist2 > diam2):
                break
        pts[i, :] = pt

    pts = pts[pts[:, 0].argsort(), :]

    obstacles = [tuple(row) for row in pts]

    line_segments_top = []
    line_segments_btm = []

    x_prev = x_min

    curve_segments_top = []
    curve_segments_btm = []

    interior_obstacles = []
    exterior_obstacles = []

    for x_c in obstacles:
        # Close to the top of the domain
        if x_c[1] > y_max - rad:
            # identify intersection
            theta = np.arcsin((y_max - x_c[1]) / rad)
            rx = rad * np.cos(theta)
            x_left = x_c[0] - rx
            x_right = x_c[0] + rx

            line_segments_top.append(
                line_points((x_prev, y_max), (x_left, y_max), dx))
            line_segments_btm.append(
                line_points((x_prev, y_min), (x_left, y_min), dx))
            curve_btm = rad_points((x_c[0], x_c[1] - Ly),
                                   rad,
                                   dx,
                                   theta_start=np.pi - theta,
                                   theta_stop=theta)[1:-1]
            curve_top = rad_points(x_c,
                                   rad,
                                   dx,
                                   theta_start=np.pi - theta,
                                   theta_stop=2 * np.pi + theta)[1:-1]
            curve_segments_btm.append(curve_btm)
            curve_segments_top.append(curve_top)

            x_prev = x_right

            exterior_obstacles.append(x_c)
            exterior_obstacles.append((x_c[0], x_c[1] - Ly))
        # Close to the bottom of the domain
        elif x_c[1] < y_min + rad:
            # identify intersection
            theta = np.arcsin((-y_min + x_c[1]) / rad)
            rx = rad * np.cos(theta)
            x_left = x_c[0] - rx
            x_right = x_c[0] + rx

            line_segments_top.append(
                line_points((x_prev, y_max), (x_left, y_max), dx))
            line_segments_btm.append(
                line_points((x_prev, y_min), (x_left, y_min), dx))
            curve_btm = rad_points(x_c,
                                   rad,
                                   dx,
                                   theta_start=np.pi + theta,
                                   theta_stop=-theta)[1:-1]
            curve_top = rad_points((x_c[0], x_c[1] + Ly),
                                   rad,
                                   dx,
                                   theta_start=np.pi + theta,
                                   theta_stop=2 * np.pi - theta)[1:-1]
            curve_segments_btm.append(curve_btm)
            curve_segments_top.append(curve_top)

            x_prev = x_right

            exterior_obstacles.append(x_c)
            exterior_obstacles.append((x_c[0], x_c[1] + Ly))
        else:
            interior_obstacles.append(x_c)

    line_segments_top.append(line_points((x_prev, y_max), (x_max, y_max), dx))
    line_segments_btm.append(line_points((x_prev, y_min), (x_max, y_min), dx))

    assert (len(line_segments_top) == len(curve_segments_top) + 1)
    assert (len(line_segments_btm) == len(curve_segments_btm) + 1)

    pts_top = line_segments_top[0]
    for i in range(len(curve_segments_top)):
        pts_top.extend(curve_segments_top[i])
        pts_top.extend(line_segments_top[i + 1])
    pts_top = pts_top[::-1]

    pts_btm = line_segments_btm[0]
    for i in range(len(curve_segments_btm)):
        pts_btm.extend(curve_segments_btm[i])
        pts_btm.extend(line_segments_btm[i + 1])

    y_side = y[1:-1]
    pts_right = zip(x_max * np.ones(N - 2), y_side)
    pts_left = zip(x_min * np.ones(N - 2), y_side[::-1])

    pts = pts_btm + pts_right + pts_top + pts_left
    edges = round_trip_connect(0, len(pts) - 1)

    for interior_obstacle in interior_obstacles:
        pts_obstacle = rad_points(interior_obstacle, rad, dx)[1:]
        edges_obstacle = round_trip_connect(len(pts),
                                            len(pts) + len(pts_obstacle) - 1)

        pts.extend(pts_obstacle)
        edges.extend(edges_obstacle)

    if do_plot:
        plot_edges(pts, edges)

    mi = tri.MeshInfo()
    mi.set_points(pts)
    mi.set_facets(edges)
    mi.set_holes(interior_obstacles)

    max_area = 0.5 * dx**2

    mesh = tri.build(mi,
                     max_volume=max_area,
                     min_angle=25,
                     allow_boundary_steiner=False)

    coords = np.array(mesh.points)
    faces = np.array(mesh.elements)

    # pp = [tuple(point) for point in mesh.points]
    # print "Number of points:", len(pp)
    # print "Number unique points:", len(set(pp))

    if do_plot:
        plot_faces(coords, faces)

    msh = numpy_to_dolfin(coords, faces)

    mesh_path = os.path.join(
        MESHES_DIR, "periodic_porous_Lx{}_Ly{}_rad{}_N{}_dx{}".format(
            Lx, Ly, rad, num_obstacles, dx))
    store_mesh_HDF5(msh, mesh_path)

    obstacles_path = os.path.join(
        MESHES_DIR, "periodic_porous_Lx{}_Ly{}_rad{}_N{}_dx{}.dat".format(
            Lx, Ly, rad, num_obstacles, dx))

    all_obstacles = np.vstack(
        (np.array(exterior_obstacles), np.array(interior_obstacles)))
    np.savetxt(
        obstacles_path,
        np.hstack((all_obstacles, np.ones((len(all_obstacles), 1)) * rad)))

    if do_plot:
        df.plot(msh)
        df.interactive()
Example #4
0
def method(Lx=4.,
           Ly=4.,
           num_obstacles=25,
           rad=0.25,
           R=0.3,
           dx=0.05,
           seed=123,
           show=False,
           **kwargs):
    x_min, x_max = -Lx / 2, Lx / 2
    y_min, y_max = -Ly / 2, Ly / 2

    np.random.seed(seed)
    obstacles = place_obstacles(num_obstacles, Lx, Ly, R)
    obstacles = correct_obstacles(obstacles, rad, x_min, x_max, y_min, y_max)

    interior_obstacles, exterior_obstacles, obst = classify_obstacles(
        obstacles, rad, x_min, x_max, y_min, y_max)

    theta_low, theta_high = compute_intersections(obst, rad, x_min, x_max,
                                                  y_min, y_max)

    curves = draw_curves(obst, theta_low, theta_high, rad, dx)
    if len(curves) > 0:
        curve_start, curve_stop = get_curve_intersection_points(
            curves, x_min, x_max, y_min, y_max)

        segments = construct_segments(curve_start, curve_stop, x_min, x_max,
                                      y_min, y_max)
        pt_start = curves[0][0]
    else:
        curve_start = []
        curve_stop = []
        segments = [((x_min, y_min), (x_max, y_min)),
                    ((x_max, y_min), (x_max, y_max)),
                    ((x_max, y_max), (x_min, y_max)),
                    ((x_min, y_max), (x_min, y_min))]
        segments = dict(segments)
        pt_start = (x_min, y_min)

    if show:
        for x_a, x_b in segments.items():
            x = np.array([x_a[0], x_b[0]])
            y = np.array([x_a[1], x_b[1]])
            plt.quiver(x[:-1],
                       y[:-1],
                       x[1:] - x[:-1],
                       y[1:] - y[:-1],
                       scale_units='xy',
                       angles='xy',
                       scale=1)
        for curve in curves:
            x = np.array(curve)
            plt.quiver(x[:-1, 0],
                       x[:-1, 1],
                       x[1:, 0] - x[:-1, 0],
                       x[1:, 1] - x[:-1, 1],
                       scale_units='xy',
                       angles='xy',
                       scale=1)

    pts = discretize_loop(pt_start, curve_start, curves, segments, dx)[::-1]

    plt.show()

    edges = round_trip_connect(0, len(pts) - 1)

    for interior_obstacle in interior_obstacles:
        pts_obstacle = rad_points(interior_obstacle, rad, dx)[1:]
        edges_obstacle = round_trip_connect(len(pts),
                                            len(pts) + len(pts_obstacle) - 1)

        pts.extend(pts_obstacle)
        edges.extend(edges_obstacle)

    if show:
        plot_edges(pts, edges)

    mi = tri.MeshInfo()
    mi.set_points(pts)
    mi.set_facets(edges)
    mi.set_holes(interior_obstacles)

    max_area = 0.5 * dx**2

    mesh = tri.build(mi,
                     max_volume=max_area,
                     min_angle=25,
                     allow_boundary_steiner=False)

    coords = np.array(mesh.points)
    faces = np.array(mesh.elements)

    pp = [tuple(point) for point in mesh.points]
    info("Number of points:     {}".format(len(pp)))
    info("Number unique points: {}".format(len(set(pp))))

    if show:
        plot_faces(coords, faces)

    msh = numpy_to_dolfin(coords, faces)

    mesh_path = os.path.join(
        MESHES_DIR, "periodic_porous_Lx{}_Ly{}_rad{}_N{}_dx{}".format(
            Lx, Ly, rad, num_obstacles, dx))
    store_mesh_HDF5(msh, mesh_path)

    obstacles_path = os.path.join(
        MESHES_DIR, "periodic_porous_Lx{}_Ly{}_rad{}_N{}_dx{}.dat".format(
            Lx, Ly, rad, num_obstacles, dx))

    if len(obst) and len(interior_obstacles):
        all_obstacles = np.vstack(
            (np.array(obst), np.array(interior_obstacles)))
    elif len(interior_obstacles):
        all_obstacles = interior_obstacles
    else:
        all_obstacles = []

    if len(all_obstacles):
        np.savetxt(
            obstacles_path,
            np.hstack((all_obstacles, np.ones((len(all_obstacles), 1)) * rad)))