예제 #1
0
파일: sample.py 프로젝트: geoffryan/cow
def test1():
    ng = 2
    domain = cowpy.DistributedDomain([16,16,16], guard=ng)
    dfield = cowpy.VectorField3d(domain)
    ishape = dfield.interior.shape
    oshape = dfield.value.shape
    
    i0, j0, k0 = domain.global_start

    # domain.coordinate returns the position at the requested index- which is
    # relative to the local subgrid, and has guard zones added to it, i.e. i=ng
    # means x=dx/2.
    for i,j,k in itertools.product(*(range(n) for n in oshape[0:3])):
        dfield.value[i,j,k] = domain.coordinate([i,j,k])
    dfield.sync_guard()

    errors = 0
    for i,j,k in itertools.product(*(range(n) for n in ishape[0:3])):
        x = np.array(domain.coordinate([i+ng, j+ng, k+ng]))
        y = dfield.index_global([i+i0, j+j0, k+k0])
        try:
            assert (x == y).all()
        except AssertionError:
            print domain.cart_rank, x, y
            errors += 1
    assert errors == 0
예제 #2
0
파일: sample.py 프로젝트: geoffryan/cow
def test2():
    """
    Demonstrates that the samples coorindates that go out are the same ones that
    come back, as long as they are re-sorted.
    """
    ng = 3
    nx = 32
    domain = cowpy.DistributedDomain([nx,nx,nx], guard=ng)
    dfield = cowpy.VectorField3d(domain)
    ishape = dfield.interior.shape
    oshape = dfield.value.shape
    np.random.seed(domain.cart_rank)

    for i,j,k in itertools.product(*(range(n) for n in oshape[0:3])):
        dfield.value[i,j,k] = domain.coordinate([i,j,k])
    dfield.sync_guard()

    # Syncing guard zones will throw off the sampling if they're near the
    # boundary, so choose them suffieciently far away. This is not a problem
    # when the data is periodic.
    nsamp = 100
    sampx = 0.125 + 0.25*np.random.rand(nsamp, 3)

    # Indexing trick to sort array by first index:
    # http://stackoverflow.com/questions/2828059/sorting-arrays-in-numpy-by-column
    x, P = dfield.sample_global(sampx)
    xi = sampx[sampx[:,0].argsort(),:] # x in
    xo = x[x[:,0].argsort(),:] # x out
    sP = P[P[:,0].argsort(),:]

    # The samples that go out are the same that come back
    assert (abs(xo - xi) < 1e-14).all()

    # The samples are correctly matched with their coordinates
    assert (abs(x - P) < 1e-14).all()
예제 #3
0
def test5():
    domain = cowpy.DistributedDomain([N, N, N], guard=3)
    dfield = cowpy.VectorField3d(domain, name="vel4")
    div = dfield.divergence(name="div")
    rot = dfield.curl(name="rot")
    dfield.dump(fname)
    div.dump(fname)
    rot.dump(fname)
예제 #4
0
def testglobind():
    domain = cowpy.DistributedDomain([10, 10, 10], guard=3)
    dfield = cowpy.DataField(domain, ["vx", "vy", "vz"])
    dfield.interior[:, :, :, :] = np.mgrid[0:10, 0:10,
                                           0:10].transpose([1, 2, 3, 0])
    x, y, z = dfield.index_global([2, 3, 4])
    assert abs(x - 2) < 1e-16
    assert abs(y - 3) < 1e-16
    assert abs(z - 4) < 1e-16
예제 #5
0
def testhelm():
    domain = cowpy.DistributedDomain([10, 10, 10], guard=2)
    A = cowpy.VectorField3d(domain, name="B")
    Asol = A.solenoidal()
    Adil = A.dilatational()
    pspec = Adil.power_spectrum(bins=100, spacing="log")
    Asol.dump("test.h5")
    Adil.dump("test.h5")
    pspec.dump("test.h5")
예제 #6
0
def testcb():
    domain = cowpy.DistributedDomain([10, 10, 10], guard=2)
    B = cowpy.VectorField3d(domain, name="B")
    B[0] = 1.0
    B[1] = 1.1
    B[2] = 1.2
    J = B.curl()
    M1 = B.divergence(stencil="corner")
    M2 = B.divergence(stencil="5point")
    assert J.name == "del_cross_B"
    assert M1.name == "del_dot_B"
예제 #7
0
def testverify():
    domain = cowpy.DistributedDomain([4, 4], guard=1)
    dfield = cowpy.DataField(domain, ["vx", "vy", "vz"], name="vel")
    dfield["vx"] = 2.1
    dfield["vy"] = 2.2
    dfield["vz"] = 2.3
    dfield["vx"][3, 3] = np.nan
    dfield["vx"][3, 4] = np.inf
    dfield.flags = 0
    dfield.setflags_infnan()
    assert (dfield.flags[3, 3] == 4)
    assert (dfield.flags[3, 4] == 8)
예제 #8
0
def testio():
    domain = cowpy.DistributedDomain([12, 12, 16], guard=3)
    dfield = cowpy.DataField(domain, ["vx", "vy", "vz"], name="vel")
    dfield["vx"] = 2.1
    dfield["vy"] = 2.2
    dfield["vz"] = 2.3
    dfield.dump("test.h5")
    dfield.value[:] = 0.0
    dfield.read("test.h5")
    assert abs(dfield["vx"] - 2.1).all() < 1e-16
    assert abs(dfield["vy"] - 2.2).all() < 1e-16
    assert abs(dfield["vz"] - 2.3).all() < 1e-16
예제 #9
0
def testsamp():
    domain = cowpy.DistributedDomain([10, 10, 10], guard=3)
    dfield = cowpy.DataField(domain, ["vx", "vy", "vz"])
    np.random.seed(domain.cart_rank)
    sampx = np.random.rand(10, 3)
    dfield["vx"] = 2.1
    dfield["vy"] = 2.2
    dfield["vz"] = 2.3
    x, P = dfield.sample_global(sampx)
    assert abs(P[:, 0] - 2.1).all() < 1e-16
    assert abs(P[:, 1] - 2.2).all() < 1e-16
    assert abs(P[:, 2] - 2.3).all() < 1e-16
    assert len(x) == len(sampx)
예제 #10
0
def testbadsetup():
    try:
        e = None
        domain = cowpy.DistributedDomain([6, 6, 6, 5], guard=2)
    except ValueError as e:
        print e
    assert isinstance(e, ValueError)

    try:
        e = None
        domain = cowpy.DistributedDomain([6, 6, 6], guard=2)
        vfield = cowpy.VectorField3d(domain, name=3)
    except TypeError as e:
        print e
    assert isinstance(e, TypeError)

    try:
        e = None
        domain = cowpy.DistributedDomain([6, 6, 6], guard=2)
        vfield = cowpy.VectorField3d(domain, members=["gx", "gy"])
    except ValueError as e:
        print e
    assert isinstance(e, ValueError)
예제 #11
0
def testhist():
    domain = cowpy.DistributedDomain([12, 12, 16], guard=3)
    hist = cowpy.Histogram1d(0,
                             1,
                             bins=10,
                             binmode="counts",
                             name="myhist",
                             domain=domain)
    for n in range(1000):
        hist.add_sample(np.random.rand())
    hist.seal()
    assert abs(hist.binval.sum() - 1000) < 1e-16
    hist.name = "myrealhist"
    hist.dump("hist.dat")
    hist.dump("test.h5", group="G1/G2")
예제 #12
0
def testreduce():
    domain = cowpy.DistributedDomain([10, 10, 10], guard=2)
    J = cowpy.VectorField3d(domain, name="J")
    J[0] = 0.0
    J[0][2, 0, 2] = 20.0  # guard cells should not be included in the reduction
    J[0][2, 12, 2] = -20.0
    J[0][2, 2, 2] = 10.0
    J[0][2, 2, 3] = -10.0
    min, max, sum = J.reduce_component("fx")
    assert abs(min + 10.0) < 1e-16
    assert abs(max - 10.0) < 1e-16
    assert abs(sum - 0.0) < 1e-16
    min, max, sum = J.reduce_magnitude()
    assert abs(max - 10.0) < 1e-16
    assert abs(min - 0.0) < 1e-16
예제 #13
0
def testmaghist():
    domain = cowpy.DistributedDomain([12, 12, 16], guard=3)
    dfield = cowpy.VectorField3d(domain)
    dfield.value[...] = np.random.rand(*dfield.value.shape)
    min, max, sum = dfield.reduce_magnitude()
    maghist = cowpy.Histogram1d(min,
                                max,
                                bins=100,
                                binmode="counts",
                                name="myhist",
                                domain=domain)
    vx, vy, vz = dfield[0], dfield[1], dfield[2]
    mag = (vx**2 + vy**2 + vz**2)**0.5
    for a in mag.flatten():
        maghist.add_sample(a)
    maghist.seal()
    print maghist.binval
예제 #14
0
def test1():
    domain = cowpy.DistributedDomain([N, N, N], guard=3)
    dfield = cowpy.DataField(domain, ["vx"], name="vel1")
    dfield.dump(fname)
예제 #15
0
파일: reduce.py 프로젝트: geoffryan/cow
 def setUp(self):
     self.domain = cowpy.DistributedDomain([16, 16])
     print "running test on %d processes" % self.domain.cart_size
예제 #16
0
def test4():
    domain = cowpy.DistributedDomain([N, N, N], guard=3)
    dfield = cowpy.VectorField3d(domain, name="vel4")
    dfield.dump(fname)
예제 #17
0
def testuserbuffer():
    buffer = np.zeros([10, 10, 10, 1], order='C')
    domain = cowpy.DistributedDomain([6, 6, 6], guard=2)
    dfield = cowpy.DataField(domain, members=['f'], buffer=buffer)