Beispiel #1
0
            bonds.append(bond)

    print("number of bonds: {}".format(M))
    print("number of sites: {}".format(N))
    print("bonds:")
    print(bonds)

    ## task 3.2
    import random
    random.seed(2)
    for i in range(10):
        random.random()

    ## task 3.3
    from latticemaker import shuffleList
    bonds = shuffleList(bonds)
    print("bonds after shuffling")
    print(bonds)

## task 3.4
# sites contain the status of each node
# If node i is the root node of a cluster, then sites[i] contains the number
# of nodes in the cluster. If not, then sites[i] contains the index of the root
# node of the cluster it belongs to.
# Since we keep both sizes and indexes in the same array, we code sizes using
# negative numbers, and indexes using positive
# So the array will initially look like [-1, -1, ...], since all nodes are root
# nodes of clusters with size 1
from sites import Sites

## task 3.8
Beispiel #2
0
def runTests():
    from latticemaker import makeSquareLattice, shuffleList
    import numpy as np

    print("Testing Sites()")

    ## some simple tests of a square 3x3 lattice
    sites = Sites(3, 3)

    sites.activate([0, 1])
    assert sites.sizeOfLargestCluster == 2, "Test failed"
    check = [1, -2, -1, -1, -1, -1, -1, -1, -1]
    assert (set(sites.sites) - set(check)) == set(), "Test failed"

    sites.activate([1, 2])
    assert sites.sizeOfLargestCluster == 3, "Test failed"
    check = [1, 2, -3, -1, -1, -1, -1, -1, -1]
    assert (set(sites.sites) - set(check)) == set(), "Test failed"

    sites.activate([3, 4])
    assert sites.sizeOfLargestCluster == 3, "Test failed"
    check = [1, 2, -3, 4, -2, -1, -1, -1, -1]
    assert (set(sites.sites) - set(check)) == set(), "Test failed"

    sites.activate([4, 5])
    assert sites.sizeOfLargestCluster == 3, "Test failed"
    check = [1, 2, -3, 4, 5, -3, -1, -1, -1]
    assert (set(sites.sites) - set(check)) == set(), "Test failed"

    sites.activate([0, 3])
    assert sites.sizeOfLargestCluster == 6, "Test failed"
    check = [1, 2, 5, 4, 5, -6, -1, -1, -1]
    assert (set(sites.sites) - set(check)) == set(), "Test failed"

    ## test merging two bigger clusters
    sites = Sites(3, 3)
    sites.activate([[0, 1], [1, 2]])  # size 3 cluster
    sites.activate([[3, 4], [4, 5], [4, 7]])  # size 4 cluster
    sites.activate([[0, 3]])
    assert sites.sizeOfLargestCluster == 7, "Test failed"

    sites2 = Sites(3, 3)
    sites2.activate([[0, 1], [1, 2]])  # size 3 cluster
    sites2.activate([[3, 4], [4, 5], [4, 7]])  # size 4 cluster
    sites2.activate([[1, 4]])
    assert sites2.sizeOfLargestCluster == 7, "Test failed"

    sites3 = Sites(3, 3)
    sites3.activate([[0, 1], [1, 2]])  # size 3 cluster
    sites3.activate([[3, 4], [4, 5], [4, 7]])  # size 4 cluster
    sites3.activate([[2, 5]])
    assert sites3.sizeOfLargestCluster == 7, "Test failed"

    # check that the order of merging doesn't matter
    for i in range(len(sites.sites)):
        assert sites.sites[i] == sites2.sites[i], "Test failed"
        assert sites2.sites[i] == sites3.sites[i], "Test failed"

    ## test merging across periodic boundaries
    sites = Sites(3, 3)
    sites.activate([[0, 1], [1, 2]])  # size 3 cluster
    sites.activate([[6, 7], [7, 8], [4, 7]])  # size 4 cluster
    sites.activate([[0, 6]])
    assert sites.sizeOfLargestCluster == 7, "Test failed"

    ## test larger lattice
    sites = Sites(10, 10)
    bonds = makeSquareLattice(10, 10)
    bonds = shuffleList(bonds)
    sites.activate(bonds[0:80])
    assert abs(np.min(
        sites.sites)) == sites.sizeOfLargestCluster, "Test failed"
    assert np.argmin(sites.sites) == sites.largestCluster, "Test failed"
    sites.activate(bonds[80:105])
    assert abs(np.min(
        sites.sites)) == sites.sizeOfLargestCluster, "Test failed"
    assert np.argmin(sites.sites) == sites.largestCluster, "Test failed"
    sites.activate(bonds[105:])
    assert abs(np.min(sites.sites)) == 100, "Test failed"
    assert abs(np.min(
        sites.sites)) == sites.sizeOfLargestCluster, "Test failed"
    assert np.argmin(sites.sites) == sites.largestCluster, "Test failed"

    ## test that the _findRoot works as expected
    sites = Sites(3, 3)
    sites.activate([[1, 0], [1, 2]])  # make a cluster
    sites.activate([[3, 4], [4, 5]])  # make another cluster
    sites.activate(
        [4,
         1])  # merge in a specific order, so upper left becomes the root node
    sites.activate([[6, 7], [7, 8]])  # make a third cluster
    sites.activate([1, 7])  # merge
    assert sites.sites[8] == 7, "Test failed"
    assert sites._findRoot(
        8) == 0, "Test failed"  # this should modify sites.sites[8]
    assert sites.sites[8] == 0, "Test failed"

    ## test larger cluster and many activations
    sites = Sites(100, 100)
    bonds = makeSquareLattice(100, 100)  # don't shuffle

    # sites.activate(bonds[::2][:9])  # activate 10 first nodes
    # assert sites.sizeOfLargestCluster == 10, "Test failed"

    sites.activate(bonds[::2][-10:-1])  # activate 10 last nodes

    # print(sites.sites[:100])
    print(sites.sites[-200:])

    print(bonds[-1])
    print(bonds[-2])
    print(bonds[:105])

    print(" -- All tests passed")