Пример #1
0
def test_interengine_multiple_axes(init_pyDive):
    for size in sizes:
        for dtype in dtypes:
            ref = (np.random.rand(*size) * 100.0).astype(dtype)

            for distaxesA in [range(i+1) for i in range(len(size))]:
                for distaxesB in [range(i,len(size)) for i in range(len(size))]:
                    test_arrayA = pyDive.empty(size, dtype, distaxesA)
                    test_arrayB = pyDive.empty(size, dtype, distaxesB)

                    test_arrayA[:] = ref
                    test_arrayB[:] = ref

                    for distaxis in range(len(size)):
                        if size[distaxis] < 5: continue

                        slicesA = [slice(None)] * len(size)
                        slicesB = list(slicesA)
                        slicesA[distaxis] = slice(0, 5)
                        slicesB[distaxis] = slice(-5, None)

                        ref_sum = ref[slicesA] + ref[slicesB]
                        test_array_sum = test_arrayA[slicesA] + test_arrayB[slicesB]

                        assert np.array_equal(ref_sum, test_array_sum.gather())
Пример #2
0
def test_interengine(init_pyDive):
    for size in sizes:
        for dtype in dtypes:
            ref = (np.random.rand(*size) * 100.0).astype(dtype)

            for distaxis in range(len(size)):
                if size[distaxis] < 5: continue

                test_array = pyDive.empty(size, dtype, distaxis)
                test_array[:] = ref

                slicesA = [slice(None)] * len(size)
                slicesB = list(slicesA)
                slicesA[distaxis] = slice(0, 5)
                slicesB[distaxis] = slice(-5, None)

                test_array[slicesA] = test_array[slicesB]
                ref[slicesA] = ref[slicesB]

                assert np.array_equal(test_array.gather(), ref)

                slicesA = [s/2 for s in size]
                slicesB = list(slicesA)
                slicesA[distaxis] = slice(0, 5)
                slicesB[distaxis] = slice(-5, None)

                test_array[slicesA] = test_array[slicesB]
                ref[slicesA] = ref[slicesB]

                assert np.array_equal(test_array.gather(), ref)
Пример #3
0
def test_reduce(init_pyDive):
    input_array = pyDive.h5.open(input_file, "fields").load()

    energy_array = pyDive.empty(input_array.shape, dtype=input_array.dtype["fieldE"]["x"])

    def energy(out, fields):
        out[:] = fields["fieldE/x"]**2 + fields["fieldE/y"]**2 + fields["fieldB/z"]**2

    pyDive.map(energy, energy_array, input_array)

    test_total = pyDive.reduce(energy_array, np.add)
    ref_total = np.add.reduce(energy_array.gather(), axis=None)

    diff = abs(ref_total - test_total)
    assert diff / ref_total < 1.0e-5
Пример #4
0
def test_map(init_pyDive):
    input_array = pyDive.h5.open(input_file, "fields")

    ref_array = input_array["fieldE/x"].load().gather()**2 \
              + input_array["fieldE/y"].load().gather()**2 \
              + input_array["fieldB/z"].load().gather()**2

    test_array = pyDive.empty(input_array.shape, dtype=input_array.dtype["fieldE"]["x"])

    def energy(out, h5fields):
        fields = h5fields.load()
        out[:] = fields["fieldE/x"]**2 + fields["fieldE/y"]**2 + fields["fieldB/z"]**2

    pyDive.map(energy, test_array, input_array)

    assert np.array_equal(ref_array, test_array.gather())
Пример #5
0
def test_reduce(init_pyDive):
    input_array = pyDive.h5.open(input_file, "fields").load()

    energy_array = pyDive.empty(input_array.shape,
                                dtype=input_array.dtype["fieldE"]["x"])

    def energy(out, fields):
        out[:] = fields["fieldE/x"]**2 + fields["fieldE/y"]**2 + fields[
            "fieldB/z"]**2

    pyDive.map(energy, energy_array, input_array)

    test_total = pyDive.reduce(energy_array, np.add)
    ref_total = np.add.reduce(energy_array.gather(), axis=None)

    diff = abs(ref_total - test_total)
    assert diff / ref_total < 1.0e-5
Пример #6
0
def test_map(init_pyDive):
    input_array = pyDive.h5.open(input_file, "fields")

    ref_array = input_array["fieldE/x"].load().gather()**2 \
              + input_array["fieldE/y"].load().gather()**2 \
              + input_array["fieldB/z"].load().gather()**2

    test_array = pyDive.empty(input_array.shape,
                              dtype=input_array.dtype["fieldE"]["x"])

    def energy(out, h5fields):
        fields = h5fields.load()
        out[:] = fields["fieldE/x"]**2 + fields["fieldE/y"]**2 + fields[
            "fieldB/z"]**2

    pyDive.map(energy, test_array, input_array)

    assert np.array_equal(ref_array, test_array.gather())
Пример #7
0
def test_slicing(init_pyDive):
    for size in sizes:
        for dtype in dtypes:
            ref = (np.random.rand(*size) * 100.0).astype(dtype)

            for distaxis in range(len(size)):
                test_array = pyDive.empty(size, dtype, distaxis)
                test_array[:] = ref

                slices = []
                for i in range(len(size)):
                    start = size[i] / 3
                    stop = size[i] - size[i] / 5
                    step = 2
                    slices.append(slice(start, stop, step))

                assert np.array_equal(ref[slices], test_array[slices].gather())

                slices = []
                for i in range(len(size)):
                    slices.append(slice(-5, None, None))

                assert np.array_equal(ref[slices], test_array[slices].gather())

                slices = []
                for i in range(len(size)):
                    slices.append(slice(0, 5, None))

                assert np.array_equal(ref[slices], test_array[slices].gather())

                # bitmask indexing
                bitmask = pyDive.array(np.random.rand(*size) > 0.5, distaxes=test_array.distaxes)
                assert ref[bitmask.gather()].shape == test_array[bitmask].shape
                # ordering can be distinct, thus merely check if sets are equal
                assert set(ref[bitmask.gather()]) == set(test_array[bitmask].gather())

                ref2 = ref.copy()
                test_array2 = test_array.copy()
                ref2[bitmask.gather()] = 1
                test_array2[bitmask] = 1
                assert np.array_equal(ref2, test_array2.gather())
Пример #8
0
def test_mesh2particles(init_pyDive):
    particles = pyDive.h5.open(input_file, "/particles").load()
    field = pyDive.h5.open(input_file, "/fields/fieldB/z").load().gather()

    field_strengths = pyDive.empty(particles.shape)

    @pyDive.map
    def mesh2particles(field_strengths, particles, field):
        total_pos = particles["cellidx"].astype(np.float32) + particles["pos"]

        # convert total_pos to an (N, 2) shaped array
        total_pos = np.hstack((total_pos["x"][:,np.newaxis],
                               total_pos["y"][:,np.newaxis]))

        import pyDive.mappings
        field_strengths[:] = pyDive.mappings.mesh2particles(field, total_pos, pyDive.mappings.CIC)

    mesh2particles(field_strengths, particles, field=field)

    ref_field_strengths = np.load(os.path.join(dirname, "m2p_CIC.npy"))

    assert np.array_equal(ref_field_strengths, field_strengths.gather())
Пример #9
0
def test_mesh2particles(init_pyDive):
    particles = pyDive.h5.open(input_file, "/particles").load()
    field = pyDive.h5.open(input_file, "/fields/fieldB/z").load().gather()

    field_strengths = pyDive.empty(particles.shape)

    @pyDive.map
    def mesh2particles(field_strengths, particles, field):
        total_pos = particles["cellidx"].astype(np.float32) + particles["pos"]

        # convert total_pos to an (N, 2) shaped array
        total_pos = np.hstack(
            (total_pos["x"][:, np.newaxis], total_pos["y"][:, np.newaxis]))

        import pyDive.mappings
        field_strengths[:] = pyDive.mappings.mesh2particles(
            field, total_pos, pyDive.mappings.CIC)

    mesh2particles(field_strengths, particles, field=field)

    ref_field_strengths = np.load(os.path.join(dirname, "m2p_CIC.npy"))

    assert np.array_equal(ref_field_strengths, field_strengths.gather())