Exemplo n.º 1
0
def main():
    # Make printing a bit nicer for visualizing
    np.set_printoptions(threshold=sys.maxsize, linewidth=sys.maxsize)

    layout = np.array([[-2, -2, -2, -2, -2, -2, -2, -2, -2],
                        [-2, -2, -2, -2, -2, -2, -2, -2, -2],
                        [-2, -2, -2, -2, -2, -2, -2, -2, -2],
                        [-2, -2, -2, -1, -1, -1, -2, -2, -2],
                        [-2, -2, -2, -1, -1, -1, -2, -2, -2],
                        [-2, -2, -2, -1, -1, -1, -2, -2, -2],
                        [-2, -2, -2, -2, -2, -2, -2, -2, -2],
                        [-2, -2, -2, -2, -2, -2, -2, -2, -2],
                        [-2, -2, -2, -2, -2, -2, -2, -2, -2]])

    plt.imshow(layout)
    plt.show()

    coordinates = shared.index_layout(layout)
    print(layout)
    adjacency_list = get_adjacency_list(layout, coordinates)
    print(adjacency_list)
    laplacian = shared.compute_laplacian(adjacency_list)
    potentials = compute_harmonic_function(laplacian, 3, 3, 1)
    #print(potentials)
    harmonic_function = shared.display_harmonic_function(potentials, coordinates, 9, display_type='grid')
    print('resistance', 1/shared.get_energy(adjacency_list, potentials, 1))
Exemplo n.º 2
0
def left_to_right_potentials(b, level, coordinates, laplacian):
    ''' Generates the harmonic function which is entirely 1 on the left side,
    and 0 on the right side.  In order to do this, the associated indices of the
    boundary are computed.  The potentials computed are returned'''

    grid_size = get_grid_size(b, level)
    edge_length = get_edge_length(b, level)

    # Get Boundary Indices
    boundary_indices = []
    boundary_indices.extend(range(edge_length))
    boundary_indices.extend(range(2 * edge_length - 2, 3 * edge_length - 2))

    # Dirichlet Boundary Conditions
    boundary = np.zeros((2 * edge_length))
    boundary[:edge_length] = 1

    potentials = shared.compute_harmonic_function(laplacian, boundary_indices,
                                                  boundary)
    harmonic_function = shared.display_harmonic_function(potentials,
                                                         coordinates,
                                                         grid_size,
                                                         display_type='grid')

    return potentials
Exemplo n.º 3
0
def top_to_bottom_potentials(b, level, resolution, laplacian):
    edge_length = get_edge_length(b, level, resolution)

    # Calculate Boundary Indices
    boundary_indices = []
    boundary_indices.extend(range(edge_length))
    boundary_indices.extend(range(laplacian.shape[0]-edge_length,laplacian.shape[0]))

    # Set Dirichlet Boundary
    boundary = np.zeros((2*edge_length))
    boundary[edge_length:] = 1

    # Compute Harmonic Function
    potentials = shared.compute_harmonic_function(laplacian, boundary_indices, boundary)
    harmonic_function = shared.display_harmonic_function(potentials, coordinates, edge_length, display_type='grid')
Exemplo n.º 4
0
def left_to_bottom_potentials(b, crosswires, level):
    edge_length = crosswires * b**level

    # Calculate Boundary Indices
    boundary_indices = []
    boundary_indices.extend(range(2 * edge_length))

    # Set Dirichlet Boundary
    boundary = np.zeros((2 * edge_length))
    boundary[edge_length:] = 1

    potentials = shared.compute_harmonic_function(laplacian, boundary_indices,
                                                  boundary)
    harmonic_function = shared.display_harmonic_function(potentials,
                                                         coordinates,
                                                         grid_size,
                                                         display_type='grid')

    return potentials, harmonic_function, boundary_indices
Exemplo n.º 5
0
def exit_distribution_potentials(b, crosswires, level, l):
    edge_length = crosswires * b**level

    # Set Dirichlet Boundary Indices
    boundary_indices = []
    boundary_indices.extend(range(4 * edge_length))
    #print(plus.get_grid_size(b, crosswires, level-1)//2)
    boundary_indices.append(
        len(coordinates) - l * crosswires * b**(level - 1) // 2 - 1)

    # Set Dirichlet Boundary
    boundary = np.full((4 * edge_length + 1), 0)
    boundary[-1] = 1

    potentials = shared.compute_harmonic_function(laplacian, boundary_indices,
                                                  boundary)
    harmonic_function = shared.display_harmonic_function(potentials,
                                                         coordinates,
                                                         grid_size,
                                                         display_type='grid')

    return potentials, harmonic_function, boundary_indices
Exemplo n.º 6
0
def main():
    print('sdf')
    # Make printing a bit nicer for visualizing
    np.set_printoptions(threshold=sys.maxsize, linewidth=sys.maxsize)
    #print('asdfsadf')

    base_layout = np.array([[-1, -2, -2, -2, -1], [-2, -2, -2, -2, -2],
                            [-2, -2, -2, -2, -2], [-2, -2, -2, -2, -2],
                            [-1, -2, -2, -2, -1]])

    blank_layout = np.full((5, 5), -1)

    x = np.hstack((base_layout, base_layout, base_layout))
    y = np.hstack((base_layout, blank_layout, base_layout))
    z = np.hstack((base_layout, base_layout, base_layout))

    layout = np.vstack((x, y, z))
    print(layout)
    plt.imshow(layout)
    plt.show()

    coordinates = shared.index_layout(layout)
    print(layout)

    adjacency_list = get_adjacency_list(layout, coordinates, 3)
    print(adjacency_list)
    laplacian = shared.compute_laplacian(adjacency_list)
    #plt.imshow(laplacian.todense())
    #print(laplacian.todense())
    #plt.show()
    potentials = compute_harmonic_function(laplacian, 3, 3, 1)
    #print(potentials)
    harmonic_function = shared.display_harmonic_function(potentials,
                                                         coordinates,
                                                         15,
                                                         display_type='grid')
    print('resistance', 1 / shared.get_energy(adjacency_list, potentials, 1))
Exemplo n.º 7
0
def main():
    # Make printing a bit nicer for visualizing
    np.set_printoptions(threshold=sys.maxsize, linewidth=sys.maxsize)

    # Algorithm Parameters (Type -h for usage)
    parser = argparse.ArgumentParser(
        description=
        'Generates the + Graph Approximations for the Sierpinski Carpet')
    parser.add_argument(
        '-b',
        default=3,
        type=int,
        help='The number of sections to divide the carpet into')
    parser.add_argument(
        '-l',
        default=1,
        type=int,
        help='The number of sections to remove from the carpet center')
    parser.add_argument('-c',
                        '--crosswires',
                        type=int,
                        default=1,
                        help='The number of crosswires')
    parser.add_argument('-a',
                        '--level',
                        type=int,
                        default=3,
                        help='Number of pre-carpet contraction iterations')
    args = parser.parse_args()

    # Begin Computation of Harmonic Function
    print(
        'Generating + Graph Approximation for b=%d, l=%d, crosswires=%d, level=%d ...'
        % (args.b, args.l, args.crosswires, args.level))
    grid_size = get_grid_size(args.b, args.crosswires, args.level)
    layout = get_grid_layout(args.b, args.l, args.crosswires, args.level)

    # Visualization of Fractal
    shared.display_grid_layout(layout, display_type='matplotlib')

    # Possibly need to clear some memory, insert `del layout` at some point
    coordinates = shared.index_layout(layout)
    adjacency_list = get_adjacency_list(layout, coordinates, args.crosswires)
    laplacian = shared.compute_laplacian(adjacency_list)

    # 0 -> 1 Harmonic Function
    # Set Dirichlet Boundary Indices
    edge_length = args.crosswires * args.b**args.level
    boundary_indices = []
    boundary_indices.extend(range(edge_length))
    boundary_indices.extend(range(2 * edge_length, 3 * edge_length))

    # Set Dirichlet Boundary
    boundary = np.zeros((2 * edge_length))
    boundary[edge_length:] = 1

    potentials = shared.compute_harmonic_function(laplacian, boundary_indices,
                                                  boundary)
    harmonic_function = shared.display_harmonic_function(potentials,
                                                         coordinates,
                                                         grid_size,
                                                         display_type='grid')

    # Energy Calculation
    #print('resistance', 1/shared.get_energy(adjacency_list, potentials, 1))

    # Max Edge Portion
    '''max_edges = shared.max_edges(adjacency_list, potentials, coordinates, grid_size)
    print(max_edges)
    min_coordinate = (-1, -1)
    for edge in max_edges:
        print('------')
        print('left edge', coordinates[edge[0], 0], coordinates[edge[0], 1])
        print('right edge', coordinates[edge[1], 0], coordinates[edge[1], 1])
        print('left potential', potentials[edge[0]])
        print('right potential', potentials[edge[1]])
        print('------')'''

    # Exit Distribution
    # Set Dirichlet Boundary Indices
    #boundary_indices = []
    #boundary_indices.extend(range(4*edge_length))
    #boundary_indices.append(random.randint(4*edge_length, len(coordinates)-1))

    # Set Dirichlet Boundary
    #boundary = np.full((4*edge_length+1), 0)
    #boundary[-1] = 1

    #potentials2 = shared.compute_harmonic_function(laplacian, boundary_indices, boundary)
    #harmonic_function2 = shared.display_harmonic_function(potentials2, coordinates, grid_size, display_type='grid')
    #print(coordinates)
    #print(potentials2)

    #edge_diff_distribution = np
    #for i, row in enumerate(adjacency_list)

    #print()

    ## INTERPOLATION PORTION
    '''print('------------------------------------------------')
    print('Beginning Interpolation of cell for ...')
    interpolation_level = 4
    # Finding maximum edge
    max_edges = shared.max_edges(adjacency_list, potentials, coordinates, grid_size)
    #print(max_edges)
    print(max_edges)

    # Fetching max_cell
    sublevel = 1
    subcell_size = get_grid_size(args.b, args.crosswires, sublevel)
    print(max_edges[0,0])
    print(coordinates[max_edges[0,0],0])

    subcoordinate = (coordinates[max_edges[0,0],0]//(subcell_size-1), coordinates[max_edges[0,0],1]//(subcell_size-1))
    print(subcoordinate)
    #print(subcoordinate)
    cell = get_max_subcell(harmonic_function, args.b, args.crosswires, args.level, subcoordinate, sublevel=sublevel)
    #generate_interpolation(cell, args.b, args.crosswires, interpolation_level)

    # Begin Computation of Harmonic Function

    interpolation_grid_size = get_grid_size(args.b, args.crosswires, interpolation_level)
    interpolation_layout = get_grid_layout(args.b, args.l, args.crosswires, interpolation_level)

    # Visualization of Fractal
    shared.display_grid_layout(interpolation_layout, display_type='matplotlib')

    # Possibly need to clear some memory, insert `del layout` at some point
    interpolation_coordinates = shared.index_layout(interpolation_layout)
    interpolation_adjacency_list = get_adjacency_list(interpolation_layout, interpolation_coordinates, args.crosswires)
    interpolation_laplacian = shared.compute_laplacian(interpolation_adjacency_list)
    dirichlet = generate_interpolation(cell, args.b, args.crosswires, interpolation_level, interpolation_layout)
    interpolation_potentials = compute_interpolation_harmonic_function(interpolation_laplacian, args.b, args.crosswires, interpolation_level, dirichlet)
    interpolation_harmonic_function = shared.display_harmonic_function(interpolation_potentials, interpolation_coordinates, interpolation_grid_size, display_type='grid')
    ''' '''potentials = compute_harmonic_function(laplacian, args.b, args.crosswires, args.level)''' '''