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
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()
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)
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")
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"
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)
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
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
def test4(): domain = cowpy.DistributedDomain([N, N, N], guard=3) dfield = cowpy.VectorField3d(domain, name="vel4") dfield.dump(fname)