def test_decomposition_3d():
    array = np.ones((33, 35, 37))
    bbox = np.array([[0., 1.0], [-1.5, 1.5], [1.0, 2.5]])

    ledge, redge, shapes, slices = dec.decompose_array(array.shape,
                                                       np.array([3, 2, 2]), bbox)
    data = [array[slice] for slice in slices]

    assert_array_equal(data[0].shape, np.array([11, 17, 18]))

    gold_le = np.array(
        [[0.00000, -1.50000, 1.00000], [0.00000, -1.50000, 1.72973],
         [0.00000, -0.04286, 1.00000], [0.00000, -0.04286, 1.72973],
         [0.33333, -1.50000, 1.00000], [0.33333, -1.50000, 1.72973],
         [0.33333, -0.04286, 1.00000], [0.33333, -0.04286, 1.72973],
         [0.66667, -1.50000, 1.00000], [0.66667, -1.50000, 1.72973],
         [0.66667, -0.04286, 1.00000], [0.66667, -0.04286, 1.72973]]
    )
    assert_almost_equal(ledge, gold_le, 5)

    gold_re = np.array(
        [[0.33333, -0.04286, 1.72973], [0.33333, -0.04286, 2.50000],
         [0.33333, 1.50000, 1.72973], [0.33333, 1.50000, 2.50000],
         [0.66667, -0.04286, 1.72973], [0.66667, -0.04286, 2.50000],
         [0.66667, 1.50000, 1.72973], [0.66667, 1.50000, 2.50000],
         [1.00000, -0.04286, 1.72973], [1.00000, -0.04286, 2.50000],
         [1.00000, 1.50000, 1.72973], [1.00000, 1.50000, 2.50000]]
    )
    assert_almost_equal(redge, gold_re, 5)
Example #2
0
def test_stream_hexahedral():
    np.random.seed(0x4D3D3D3)
    Nx, Ny, Nz = 32, 18, 24
    # Note what we're doing here -- we are creating a randomly spaced mesh, but
    # because of how the accumulate operation works, we also reset the leftmost
    # cell boundary to 0.0.
    cell_x = np.random.random(Nx + 1)
    cell_x /= cell_x.sum()
    cell_x = np.add.accumulate(cell_x)
    cell_x[0] = 0.0

    cell_y = np.random.random(Ny + 1)
    cell_y /= cell_y.sum()
    cell_y = np.add.accumulate(cell_y)
    cell_y[0] = 0.0

    cell_z = np.random.random(Nz + 1)
    cell_z /= cell_z.sum()
    cell_z = np.add.accumulate(cell_z)
    cell_z[0] = 0.0

    coords, conn = hexahedral_connectivity(cell_x, cell_y, cell_z)
    data = {"random_field": np.random.random((Nx, Ny, Nz))}
    bbox = np.array([[0.0, 1.0], [0.0, 1.0], [0.0, 1.0]])
    ds = load_hexahedral_mesh(data, conn, coords, bbox=bbox)
    dd = ds.all_data()
    # raise RuntimeError
    assert_almost_equal(float(dd["cell_volume"].sum(dtype="float64")), 1.0)
    assert_equal(dd["ones"].size, Nx * Ny * Nz)
    # Now we try it with a standard mesh
    cell_x = np.linspace(0.0, 1.0, Nx + 1)
    cell_y = np.linspace(0.0, 1.0, Ny + 1)
    cell_z = np.linspace(0.0, 1.0, Nz + 1)
    coords, conn = hexahedral_connectivity(cell_x, cell_y, cell_z)
    data = {"random_field": np.random.random((Nx, Ny, Nz))}
    bbox = np.array([[0.0, 1.0], [0.0, 1.0], [0.0, 1.0]])
    ds = load_hexahedral_mesh(data, conn, coords, bbox=bbox)
    dd = ds.all_data()
    assert_almost_equal(float(dd["cell_volume"].sum(dtype="float64")), 1.0)
    assert_equal(dd["ones"].size, Nx * Ny * Nz)
    assert_almost_equal(dd["dx"].to_ndarray(), 1.0 / Nx)
    assert_almost_equal(dd["dy"].to_ndarray(), 1.0 / Ny)
    assert_almost_equal(dd["dz"].to_ndarray(), 1.0 / Nz)

    s = SlicePlot(ds, "x", "random_field")
    s._setup_plots()
    s.frb["random_field"]
Example #3
0
def test_equivalent_width_conserved():
    """
    This tests that the equivalent width of the optical depth is conserved 
    regardless of the bin width employed in wavelength space.
    Unresolved lines should still deposit optical depth into the spectrum.
    """

    # Set up in a temp dir
    tmpdir = tempfile.mkdtemp()
    curdir = os.getcwd()
    os.chdir(tmpdir)

    lr = LightRay(COSMO_PLUS_SINGLE)

    ray_start = [0,0,0]
    ray_end = [1,1,1]
    lr.make_light_ray(start_position=ray_start, end_position=ray_end,
                      fields=['temperature', 'density', 'H_number_density'],
                      data_filename='lightray.h5')

    my_label = 'HI Lya'
    field = 'H_number_density'
    wave = 1215.6700  # Angstromss
    f_value = 4.164E-01
    gamma = 6.265e+08
    mass = 1.00794

    lambda_min= 1200
    lambda_max= 1300
    lambda_bin_widths = [1e-3, 1e-2, 1e-1, 1e0, 1e1]
    total_tau = []

    for lambda_bin_width in lambda_bin_widths:
        n_lambda = ((lambda_max - lambda_min)/ lambda_bin_width) + 1
        sp = AbsorptionSpectrum(lambda_min=lambda_min, lambda_max=lambda_max, 
                                n_lambda=n_lambda)
        sp.add_line(my_label, field, wave, f_value, gamma, mass)
        wavelength, flux = sp.make_spectrum('lightray.h5')
        total_tau.append((lambda_bin_width * sp.tau_field).sum())
        
    # assure that the total tau values are all within 1e-3 of each other
    for tau in total_tau:
        assert_almost_equal(tau, total_tau[0], 3)

    # clean up
    os.chdir(curdir)
    shutil.rmtree(tmpdir)
Example #4
0
def test_arbitrary_grid():
    for ncells in [32, 64]:
        for px in [0.125, 0.25, 0.55519]:

            particle_data = {
                "particle_position_x": np.array([px]),
                "particle_position_y": np.array([0.5]),
                "particle_position_z": np.array([0.5]),
                "particle_mass": np.array([1.0]),
            }

            ds = load_particles(particle_data)

            for dims in ([ncells] * 3, [ncells, ncells / 2, ncells / 4]):
                LE = np.array([0.05, 0.05, 0.05])
                RE = np.array([0.95, 0.95, 0.95])
                dims = np.array(dims)

                dds = (RE - LE) / dims
                volume = ds.quan(np.product(dds), "cm**3")

                obj = ds.arbitrary_grid(LE, RE, dims)
                deposited_mass = obj[("deposit", "all_density")].sum() * volume

                assert_equal(deposited_mass, ds.quan(1.0, "g"))

                LE = np.array([0.00, 0.00, 0.00])
                RE = np.array([0.05, 0.05, 0.05])

                obj = ds.arbitrary_grid(LE, RE, dims)

                deposited_mass = obj[("deposit", "all_density")].sum()

                assert_equal(deposited_mass, 0)

    # Test that we get identical results to the covering grid for unigrid data.
    # Testing AMR data is much harder.
    for nprocs in [1, 2, 4, 8]:
        ds = fake_random_ds(32, nprocs=nprocs)
        for ref_level in [0, 1, 2]:
            cg = ds.covering_grid(
                ref_level, [0.0, 0.0, 0.0], 2 ** ref_level * ds.domain_dimensions
            )
            ag = ds.arbitrary_grid(
                [0.0, 0.0, 0.0], [1.0, 1.0, 1.0], 2 ** ref_level * ds.domain_dimensions
            )
            assert_almost_equal(cg[("gas", "density")], ag[("gas", "density")])
Example #5
0
def test_sph_interpolation_scatter():
    '''
    Just generate an octree, perform some SPH interpolation and check with some
    answer testing
    '''

    ds = fake_sph_grid_ds(hsml_factor=26.0)
    ds.use_sph_normalization = False
    ds._sph_ptypes = ('io',)
    octree = ds.octree(n_ref=5, over_refine_factor=0)
    density = octree[('io', 'density')]
    answers = np.array([1.00434706, 1.00434706, 1.00434706, 1.00434706,
                         1.00434706, 1.00434706, 1.00434706, 0.7762907,
                         0.89250848, 0.89250848, 0.97039088, 0.89250848,
                         0.97039088, 0.97039088, 1.01156175])

    assert_almost_equal(density.d, answers)
Example #6
0
def test_fields_diff_systems_etc():
    ytcfg["yt","skip_dataset_cache"] = "True"

    ds_cgs = load(etc)
    dd_cgs = ds_cgs.sphere("max", (500., "kpc"))

    for us in test_units:
        ds = load(etc, unit_system=us)
        dd = ds.sphere("max", (500., "kpc"))
        for field in test_fields:
            if us == "code":
                v1 = dd_cgs[field].in_units(test_units["code"][field])
            else:
                v1 = dd_cgs[field].in_base(us)
            v2 = dd[field]
            assert_almost_equal(v1.v, v2.v)
            assert str(v2.units) == test_units[us][field]
Example #7
0
 def compare(self, new_result, old_result):
     assert_equal(
         len(new_result),
         len(old_result),
         err_msg="Number of outputs not equal.",
         verbose=True,
     )
     for k in new_result:
         if hasattr(new_result[k], "d"):
             new_result[k] = new_result[k].d
         if hasattr(old_result[k], "d"):
             old_result[k] = old_result[k].d
         if self.decimals is None:
             assert_almost_equal(new_result[k], old_result[k])
         else:
             assert_allclose_units(new_result[k], old_result[k],
                                   10**(-self.decimals))
Example #8
0
def test_decomposition_2d():
    array = np.ones((7, 5, 1))
    bbox = np.array([[-0.7, 0.0], [1.5, 2.0], [0.0, 0.7]])
    ledge, redge, shapes, slices = dec.decompose_array(array.shape,
                                                       np.array([2, 3, 1]),
                                                       bbox)

    data = [array[slice] for slice in slices]
    assert_array_equal(data[1].shape, np.array([3, 2, 1]))

    gold_le = np.array([[-0.7, 1.5, 0.0], [-0.7, 1.6, 0.0], [-0.7, 1.8, 0.0],
                        [-0.4, 1.5, 0.0], [-0.4, 1.6, 0.0], [-0.4, 1.8, 0.0]])
    assert_almost_equal(ledge, gold_le, 8)

    gold_re = np.array([[-0.4, 1.6, 0.7], [-0.4, 1.8, 0.7], [-0.4, 2.0, 0.7],
                        [0.0, 1.6, 0.7], [0.0, 1.8, 0.7], [0.0, 2.0, 0.7]])
    assert_almost_equal(redge, gold_re, 8)
Example #9
0
def test_arbitrary_grid():
    for ncells in [64, 128, 256]:
        for px in [0.125, 0.25, 0.55519]:

            particle_data = {
                'particle_position_x': np.array([px]),
                'particle_position_y': np.array([0.5]),
                'particle_position_z': np.array([0.5]),
                'particle_mass': np.array([1.0])
            }

            ds = load_particles(particle_data)

            LE = np.array([0.05, 0.05, 0.05])
            RE = np.array([0.95, 0.95, 0.95])
            dims = np.array([ncells, ncells, ncells])

            dds = (RE - LE) / dims
            volume = ds.quan(np.product(dds), 'cm**3')

            obj = ds.arbitrary_grid(LE, RE, dims)
            deposited_mass = obj["deposit", "all_density"].sum() * volume

            assert_equal(deposited_mass, ds.quan(1.0, 'g'))

            LE = np.array([0.00, 0.00, 0.00])
            RE = np.array([0.05, 0.05, 0.05])
            dims = np.array([ncells, ncells, ncells])

            obj = ds.arbitrary_grid(LE, RE, dims)

            deposited_mass = obj["deposit", "all_density"].sum()

            assert_equal(deposited_mass, 0)

    # Test that we get identical results to the covering grid for unigrid data.
    # Testing AMR data is much harder.
    for nprocs in [1, 2, 4, 8]:
        ds = fake_random_ds(32, nprocs=nprocs)
        for ref_level in [0, 1, 2]:
            cg = ds.covering_grid(ref_level, [0.0, 0.0, 0.0],
                                  2**ref_level * ds.domain_dimensions)
            ag = ds.arbitrary_grid([0.0, 0.0, 0.0], [1.0, 1.0, 1.0],
                                   2**ref_level * ds.domain_dimensions)
            assert_almost_equal(cg["density"], ag["density"])
Example #10
0
def test_3d_out():
    ds = data_dir_load(threeD)
    particle_types = ["all", "io", "nbody"]
    field_list = list(product(particle_types, particle_fields))
    field_list += list(product(("openPMD",), ("E_x", "E_y", "E_z", "rho")))
    domain_dimensions = [26, 26, 201] * np.ones_like(ds.domain_dimensions)
    domain_width = [2.08e-05, 2.08e-05, 2.01e-05] * np.ones_like(ds.domain_left_edge)

    assert isinstance(ds, OpenPMDDataset)
    assert_equal(str(ds), "data00000100.h5")
    assert_equal(ds.dimensionality, 3)
    assert_equal(ds.particle_types_raw, ("io",))
    assert "all" in ds.particle_unions
    assert_array_equal(ds.field_list, field_list)
    assert_array_equal(ds.domain_dimensions, domain_dimensions)
    assert_almost_equal(
        ds.current_time, 3.28471214521e-14 * np.ones_like(ds.current_time)
    )
    assert_almost_equal(ds.domain_right_edge - ds.domain_left_edge, domain_width)
Example #11
0
def test_cut_region():
    # We decompose in different ways
    for nprocs in [1, 2, 4, 8]:
        ds = fake_random_ds(
            64, nprocs=nprocs, fields=("density", "temperature", "velocity_x")
        )
        # We'll test two objects
        dd = ds.all_data()
        r = dd.cut_region(
            [
                "obj['temperature'] > 0.5",
                "obj['density'] < 0.75",
                "obj['velocity_x'] > 0.25",
            ]
        )
        t = (
            (dd["temperature"] > 0.5)
            & (dd["density"] < 0.75)
            & (dd["velocity_x"] > 0.25)
        )
        assert_equal(np.all(r["temperature"] > 0.5), True)
        assert_equal(np.all(r["density"] < 0.75), True)
        assert_equal(np.all(r["velocity_x"] > 0.25), True)
        assert_equal(np.sort(dd["density"][t]), np.sort(r["density"]))
        assert_equal(np.sort(dd["x"][t]), np.sort(r["x"]))
        r2 = r.cut_region(["obj['temperature'] < 0.75"])
        t2 = r["temperature"] < 0.75
        assert_equal(np.sort(r2["temperature"]), np.sort(r["temperature"][t2]))
        assert_equal(np.all(r2["temperature"] < 0.75), True)

        # Now we can test some projections
        dd = ds.all_data()
        cr = dd.cut_region(["obj['ones'] > 0"])
        for weight in [None, "density"]:
            p1 = ds.proj("density", 0, data_source=dd, weight_field=weight)
            p2 = ds.proj("density", 0, data_source=cr, weight_field=weight)
            for f in p1.field_data:
                assert_almost_equal(p1[f], p2[f])
        cr = dd.cut_region(["obj['density'] > 0.25"])
        p2 = ds.proj("density", 2, data_source=cr)
        assert_equal(p2["density"].max() > 0.25, True)
        p2 = ds.proj("density", 2, data_source=cr, weight_field="density")
        assert_equal(p2["density"].max() > 0.25, True)
Example #12
0
def test_no_fields_out():
    ds = data_dir_load(noFields)
    particle_types = ("all", "io", "nbody")
    no_fields_pfields = sorted(particle_fields + ["particle_id"])
    field_list = list(product(particle_types, no_fields_pfields))
    domain_dimensions = [1, 1, 1] * np.ones_like(ds.domain_dimensions)
    domain_width = [1, 1, 1] * np.ones_like(ds.domain_left_edge)

    assert isinstance(ds, OpenPMDDataset)
    assert_equal(str(ds), "data00000400.h5")
    assert_equal(ds.dimensionality, 3)
    assert_equal(ds.particle_types_raw, ("io", ))
    assert "all" in ds.particle_unions
    assert_array_equal(ds.field_list, field_list)
    assert_array_equal(ds.domain_dimensions, domain_dimensions)
    assert_almost_equal(ds.current_time,
                        1.3161023868481013e-13 * np.ones_like(ds.current_time))
    assert_almost_equal(ds.domain_right_edge - ds.domain_left_edge,
                        domain_width)
Example #13
0
def test_sph_interpolation_gather():
    '''
    Just generate an octree, perform some SPH interpolation and check with some
    answer testing
    '''
    ds = fake_sph_grid_ds(hsml_factor=26.0)
    ds.index
    ds.sph_smoothing_style = 'gather'
    ds.num_neighbors = 5
    ds.use_sph_normalization = False
    ds._sph_ptypes = ('io',)
    octree = ds.octree(n_ref=5, over_refine_factor=0)
    density = octree[('io', 'density')]
    answers = np.array([0.59240874, 0.59240874, 0.59240874, 0.59240874,
                        0.59240874, 0.59240874, 0.59240874, 0.10026846,
                        0.77014968, 0.77014968, 0.96127825, 0.77014968,
                        0.96127825, 0.96127825, 1.21183996])

    assert_almost_equal(density.d, answers)
Example #14
0
def test_no_particles_out():
    ds = data_dir_load(noParticles)
    field_list = [('openPMD', 'E_x'), ('openPMD', 'E_y'), ('openPMD', 'E_z'),
                  ('openPMD', 'rho')]
    domain_dimensions = [51, 201, 1] * np.ones_like(ds.domain_dimensions)
    domain_width = [3.06e-05, 2.01e-05, 1e+0] * np.ones_like(
        ds.domain_left_edge)

    assert isinstance(ds, OpenPMDDataset)
    assert_equal(str(ds), "data00000400.h5")
    assert_equal(ds.dimensionality, 2)
    assert_equal(ds.particle_types_raw, ('io', ))
    assert "all" not in ds.particle_unions
    assert_array_equal(ds.field_list, field_list)
    assert_array_equal(ds.domain_dimensions, domain_dimensions)
    assert_almost_equal(ds.current_time,
                        1.3161023868481013e-13 * np.ones_like(ds.current_time))
    assert_almost_equal(ds.domain_right_edge - ds.domain_left_edge,
                        domain_width)
Example #15
0
def test_amr_kdtree_set_fields():
    ds = fake_amr_ds(fields=["density", "pressure"])
    dd = ds.all_data()

    fields = ds.field_list
    dd.tiles.set_fields(fields, [True, True], False)
    gold = {}
    for i, block in enumerate(dd.tiles.traverse()):
        gold[i] = [data.copy() for data in block.my_data]

    for log_fields in itertools.product([True, False], [True, False]):
        dd.tiles.set_fields(fields, log_fields, False)
        for iblock, block in enumerate(dd.tiles.traverse()):
            for i in range(len(fields)):
                if log_fields[i]:
                    data = block.my_data[i]
                else:
                    data = np.log10(block.my_data[i])
                assert_almost_equal(gold[iblock][i], data)
Example #16
0
def test_cosmo_dataset():
    ds = load(EAGLE_6)
    assert (type(ds) == SwiftDataset)

    field = ('gas', 'density')
    ad = ds.all_data()
    yt_density = ad[field]
    yt_coords = ad[(field[0], 'position')]

    # load some data the old fashioned way
    fh = h5py.File(ds.parameter_filename, "r")
    part_data = fh['PartType0']

    # set up a conversion factor by loading the unit mas and unit length in cm,
    # and then converting to proper coordinates
    units = fh["Units"]
    units = dict(units.attrs)
    density_factor = float(units["Unit mass in cgs (U_M)"])
    density_factor /= float(units["Unit length in cgs (U_L)"])**3

    # add the redshift factor
    header = fh["Header"]
    header = dict(header.attrs)
    density_factor *= (1.0 + float(header["Redshift"]))**3

    # now load the raw density and coordinates
    raw_density = part_data["Density"][:].astype("float64") * density_factor
    raw_coords = part_data["Coordinates"][:].astype("float64")
    fh.close()

    # sort by the positions - yt often loads in a different order
    ind_raw = np.lexsort((raw_coords[:, 2], raw_coords[:, 1], raw_coords[:,
                                                                         0]))
    ind_yt = np.lexsort((yt_coords[:, 2], yt_coords[:, 1], yt_coords[:, 0]))
    raw_density = raw_density[ind_raw]
    yt_density = yt_density[ind_yt]

    # make sure we are comparing fair units
    assert (str(yt_density.units) == 'g/cm**3')

    # make sure the actual values are the same
    assert_almost_equal(yt_density.d, raw_density)
Example #17
0
def test_decomposition_3d():
    array = np.ones((33, 35, 37))
    bbox = np.array([[0.0, 1.0], [-1.5, 1.5], [1.0, 2.5]])

    ledge, redge, shapes, slices = dec.decompose_array(array.shape,
                                                       np.array([3, 2, 2]),
                                                       bbox)
    data = [array[slice] for slice in slices]

    assert_array_equal(data[0].shape, np.array([11, 17, 18]))

    gold_le = np.array([
        [0.00000, -1.50000, 1.00000],
        [0.00000, -1.50000, 1.72973],
        [0.00000, -0.04286, 1.00000],
        [0.00000, -0.04286, 1.72973],
        [0.33333, -1.50000, 1.00000],
        [0.33333, -1.50000, 1.72973],
        [0.33333, -0.04286, 1.00000],
        [0.33333, -0.04286, 1.72973],
        [0.66667, -1.50000, 1.00000],
        [0.66667, -1.50000, 1.72973],
        [0.66667, -0.04286, 1.00000],
        [0.66667, -0.04286, 1.72973],
    ])
    assert_almost_equal(ledge, gold_le, 5)

    gold_re = np.array([
        [0.33333, -0.04286, 1.72973],
        [0.33333, -0.04286, 2.50000],
        [0.33333, 1.50000, 1.72973],
        [0.33333, 1.50000, 2.50000],
        [0.66667, -0.04286, 1.72973],
        [0.66667, -0.04286, 2.50000],
        [0.66667, 1.50000, 1.72973],
        [0.66667, 1.50000, 2.50000],
        [1.00000, -0.04286, 1.72973],
        [1.00000, -0.04286, 2.50000],
        [1.00000, 1.50000, 1.72973],
        [1.00000, 1.50000, 2.50000],
    ])
    assert_almost_equal(redge, gold_re, 5)
def test_cylindrical_coordinates():
    # We're going to load up a simple AMR grid and check its volume
    # calculations and path length calculations.
    ds = fake_amr_ds(geometry="cylindrical")
    axes = ["r", "z", "theta"]
    for i, axis in enumerate(axes):
        dd = ds.all_data()
        fi = ("index", axis)
        fd = ("index", "d%s" % axis)
        ma = np.argmax(dd[fi])
        assert_equal(dd[fi][ma] + dd[fd][ma] / 2.0, ds.domain_right_edge[i].d)
        mi = np.argmin(dd[fi])
        assert_equal(dd[fi][mi] - dd[fd][mi] / 2.0, ds.domain_left_edge[i].d)
        assert_equal(dd[fd].max(), (ds.domain_width/ds.domain_dimensions)[i].d)
    assert_almost_equal(dd["cell_volume"].sum(dtype="float64"),
                        np.pi*ds.domain_width[0]**2 * ds.domain_width[1])
    assert_equal(dd["index", "path_element_r"], dd["index", "dr"])
    assert_equal(dd["index", "path_element_z"], dd["index", "dz"])
    assert_equal(dd["index", "path_element_theta"],
                 dd["index", "r"] * dd["index", "dtheta"])
Example #19
0
    def test_nonrectangular_add(self):
        rgba1 = np.ones((64, 1, 4))
        z1 = np.expand_dims(np.arange(64.0), 1)

        rgba2 = np.zeros((64, 1, 4))
        z2 = np.expand_dims(np.arange(63.0, -1.0, -1.0), 1)

        exact_rgba = np.concatenate((np.ones(32), np.zeros(32)))
        exact_rgba = np.expand_dims(exact_rgba, 1)
        exact_rgba = np.dstack((exact_rgba, exact_rgba, exact_rgba, exact_rgba))

        exact_z = np.concatenate((np.arange(32.0), np.arange(31.0, -1.0, -1.0)))
        exact_z = np.expand_dims(exact_z, 1)

        buff1 = ZBuffer(rgba1, z1)
        buff2 = ZBuffer(rgba2, z2)

        buff = buff1 + buff2

        assert_almost_equal(buff.rgba, exact_rgba)
        assert_almost_equal(buff.z, exact_z)
Example #20
0
def test_cosmology_calculator_answers():
    """
    Test cosmology calculator functions against previously calculated values.
    """

    fn = os.path.join(local_dir, "cosmology_answers.yml")
    with open(fn) as fh:
        data = yaml.load(fh, Loader=yaml.FullLoader)

    cosmologies = data["cosmologies"]
    functions = data["functions"]

    for cname, copars in cosmologies.items():
        omega_curvature = (
            1
            - copars["omega_matter"]
            - copars["omega_lambda"]
            - copars["omega_radiation"]
        )

        cosmology = Cosmology(omega_curvature=omega_curvature, **copars)

        for fname, finfo in functions.items():
            func = getattr(cosmology, fname)
            args = finfo.get("args", [])
            val = func(*args)
            units = finfo.get("units")
            if units is not None:
                val.convert_to_units(units)
            val = float(val)

            err_msg = (
                "{} answer has changed for {} cosmology, old: {:f}, new: {:f}.".format(
                    fname,
                    cname,
                    finfo["answers"][cname],
                    val,
                )
            )
            assert_almost_equal(val, finfo["answers"][cname], 10, err_msg=err_msg)
Example #21
0
def test_groupBased_out():
    dss = load(groupBased)
    field_list = [('all', 'particle_charge'), ('all', 'particle_mass'),
                  ('all', 'particle_momentum_x'),
                  ('all', 'particle_momentum_y'),
                  ('all', 'particle_momentum_z'),
                  ('all', 'particle_positionCoarse_x'),
                  ('all', 'particle_positionCoarse_y'),
                  ('all', 'particle_positionCoarse_z'),
                  ('all', 'particle_positionOffset_x'),
                  ('all', 'particle_positionOffset_y'),
                  ('all', 'particle_positionOffset_z'),
                  ('all', 'particle_weighting'), ('io', 'particle_charge'),
                  ('io', 'particle_mass'), ('io', 'particle_momentum_x'),
                  ('io', 'particle_momentum_y'), ('io', 'particle_momentum_z'),
                  ('io', 'particle_positionCoarse_x'),
                  ('io', 'particle_positionCoarse_y'),
                  ('io', 'particle_positionCoarse_z'),
                  ('io', 'particle_positionOffset_x'),
                  ('io', 'particle_positionOffset_y'),
                  ('io', 'particle_positionOffset_z'),
                  ('io', 'particle_weighting'), ('openPMD', 'J_x'),
                  ('openPMD', 'J_y'), ('openPMD', 'J_z'),
                  ('openPMD', 'e-chargeDensity')]
    domain_dimensions = [32, 64, 64] * np.ones_like(dss[0].domain_dimensions)
    domain_width = [0.0002752, 0.0005504, 0.0005504] * np.ones_like(
        dss[0].domain_left_edge)

    assert_equal(len(dss), 101)
    for ds in dss:
        assert_equal(str(ds), "simData.h5")
        assert_equal(ds.dimensionality, 3)
        assert_equal(ds.particle_types_raw, ('io', ))
        assert_array_equal(ds.field_list, field_list)
        assert_array_equal(ds.domain_dimensions, domain_dimensions)
        assert ds.current_time >= np.zeros_like(ds.current_time)
        assert ds.current_time <= 1.6499999999999998e-12 * np.ones_like(
            ds.current_time)
        assert_almost_equal(ds.domain_right_edge - ds.domain_left_edge,
                            domain_width)
Example #22
0
def test_octree_properties():
    """
    Generate an octree, and test the refinement, depth and sizes of the cells.
    """
    ds = fake_sph_grid_ds()
    octree = ds.octree(n_ref=n_ref)

    depth = octree[("index", "depth")]
    depth_ans = np.array([0] + [1] * 8 + [2] * 8, dtype=np.int64)
    assert_equal(depth, depth_ans)

    size_ans = np.zeros((depth.shape[0], 3), dtype=np.float64)
    for i in range(size_ans.shape[0]):
        size_ans[i, :] = (ds.domain_right_edge -
                          ds.domain_left_edge) / 2.0**depth[i]

    dx = octree[("index", "dx")].d
    assert_almost_equal(dx, size_ans[:, 0])

    dy = octree[("index", "dy")].d
    assert_almost_equal(dy, size_ans[:, 1])

    dz = octree[("index", "dz")].d
    assert_almost_equal(dz, size_ans[:, 2])

    refined = octree[("index", "refined")]
    refined_ans = np.array([True] + [False] * 7 + [True] + [False] * 8,
                           dtype=np.bool_)
    assert_equal(refined, refined_ans)
def test_decomposition_2d():
    array = np.ones((7, 5, 1))
    bbox = np.array([[-0.7, 0.0], [1.5, 2.0], [0.0, 0.7]])
    ledge, redge, shapes, slices = dec.decompose_array(array.shape,
                                                       np.array([2, 3, 1]), bbox)

    data = [array[slice] for slice in slices]
    assert_array_equal(data[1].shape, np.array([3, 2, 1]))

    gold_le = np.array([
                       [-0.7, 1.5, 0.0], [-0.7, 1.6, 0.0],
                       [-0.7, 1.8, 0.0], [-0.4, 1.5, 0.0],
                       [-0.4, 1.6, 0.0], [-0.4, 1.8, 0.0]
                       ])
    assert_almost_equal(ledge, gold_le, 8)

    gold_re = np.array(
        [[-0.4, 1.6, 0.7], [-0.4, 1.8, 0.7],
         [-0.4, 2.0, 0.7], [0.0, 1.6, 0.7],
         [0.0, 1.8, 0.7], [0.0, 2.0, 0.7]]
    )
    assert_almost_equal(redge, gold_re, 8)
Example #24
0
def test_3d_out():
    ds = data_dir_load(threeD)
    field_list = [('all', 'particle_charge'), ('all', 'particle_mass'),
                  ('all', 'particle_momentum_x'),
                  ('all', 'particle_momentum_y'),
                  ('all', 'particle_momentum_z'),
                  ('all', 'particle_positionCoarse_x'),
                  ('all', 'particle_positionCoarse_y'),
                  ('all', 'particle_positionCoarse_z'),
                  ('all', 'particle_positionOffset_x'),
                  ('all', 'particle_positionOffset_y'),
                  ('all', 'particle_positionOffset_z'),
                  ('all', 'particle_weighting'), ('io', 'particle_charge'),
                  ('io', 'particle_mass'), ('io', 'particle_momentum_x'),
                  ('io', 'particle_momentum_y'), ('io', 'particle_momentum_z'),
                  ('io', 'particle_positionCoarse_x'),
                  ('io', 'particle_positionCoarse_y'),
                  ('io', 'particle_positionCoarse_z'),
                  ('io', 'particle_positionOffset_x'),
                  ('io', 'particle_positionOffset_y'),
                  ('io', 'particle_positionOffset_z'),
                  ('io', 'particle_weighting'), ('openPMD', 'E_x'),
                  ('openPMD', 'E_y'), ('openPMD', 'E_z'), ('openPMD', 'rho')]
    domain_dimensions = [26, 26, 201] * np.ones_like(ds.domain_dimensions)
    domain_width = [2.08e-05, 2.08e-05, 2.01e-05] * np.ones_like(
        ds.domain_left_edge)

    assert isinstance(ds, OpenPMDDataset)
    assert_equal(str(ds), "data00000100.h5")
    assert_equal(ds.dimensionality, 3)
    assert_equal(ds.particle_types_raw, ('io', ))
    assert "all" in ds.particle_unions
    assert_array_equal(ds.field_list, field_list)
    assert_array_equal(ds.domain_dimensions, domain_dimensions)
    assert_almost_equal(ds.current_time,
                        3.28471214521e-14 * np.ones_like(ds.current_time))
    assert_almost_equal(ds.domain_right_edge - ds.domain_left_edge,
                        domain_width)
Example #25
0
    def test_rectangular_add(self):
        rgba1 = np.ones((8, 8, 4))
        z1 = np.arange(64.0)
        z1 = z1.reshape((8, 8))
        buff1 = ZBuffer(rgba1, z1)

        rgba2 = np.zeros((8, 8, 4))
        z2 = np.arange(63.0, -1.0, -1.0)
        z2 = z2.reshape((8, 8))
        buff2 = ZBuffer(rgba2, z2)

        buff = buff1 + buff2

        exact_rgba = np.empty((8, 8, 4), dtype=np.float64)
        exact_rgba[0:4, 0:8, :] = 1.0
        exact_rgba[4:8, 0:8, :] = 0.0

        exact_z = np.concatenate((np.arange(32.0), np.arange(31.0, -1.0, -1.0)))
        exact_z = np.expand_dims(exact_z, 1)
        exact_z = exact_z.reshape(8, 8)

        assert_almost_equal(buff.rgba, exact_rgba)
        assert_almost_equal(buff.z, exact_z)
Example #26
0
def test_add_deposited_particle_field():
    # NOT tested: "std", "mesh_id", "nearest" and "simple_smooth"
    base_ds = get_base_ds(1)
    ad = base_ds.all_data()

    # Test "count", "sum" and "cic" method
    for method in ["count", "sum", "cic"]:
        fn = base_ds.add_deposited_particle_field(('io', 'particle_mass'), method)
        expected_fn = 'io_%s' if method == "count" else 'io_%s_mass'
        assert_equal(fn, ('deposit', expected_fn % method))
        ret = ad[fn]
        if method == "count":
            assert_equal(ret.sum(), ad['particle_ones'].sum())
        else:
            assert_almost_equal(ret.sum(), ad['particle_mass'].sum())

    # Test "weighted_mean" method
    fn = base_ds.add_deposited_particle_field(('io', 'particle_ones'), 'weighted_mean',
                                              weight_field='particle_ones')
    assert_equal(fn, ('deposit', 'io_avg_ones'))
    ret = ad[fn]
    # The sum should equal the number of cells that have particles
    assert_equal(ret.sum(), np.count_nonzero(ad[("deposit", "io_count")]))
Example #27
0
def test_non_cosmo_dataset():
    ds = load(keplerian_ring)
    assert type(ds) is SwiftDataset

    field = ("gas", "density")
    ad = ds.all_data()
    yt_density = ad[field]
    yt_coords = ad[(field[0], "position")]

    # load some data the old fashioned way
    fh = h5py.File(ds.parameter_filename, "r")
    part_data = fh["PartType0"]

    # set up a conversion factor by loading the unit mas and unit length in cm,
    # and then converting to proper coordinates
    units = fh["Units"]
    units = dict(units.attrs)
    density_factor = float(units["Unit mass in cgs (U_M)"])
    density_factor /= float(units["Unit length in cgs (U_L)"])**3

    # now load the raw density and coordinates
    raw_density = part_data["Density"][:].astype("float64") * density_factor
    raw_coords = part_data["Coordinates"][:].astype("float64")
    fh.close()

    # sort by the positions - yt often loads in a different order
    ind_raw = np.lexsort((raw_coords[:, 2], raw_coords[:, 1], raw_coords[:,
                                                                         0]))
    ind_yt = np.lexsort((yt_coords[:, 2], yt_coords[:, 1], yt_coords[:, 0]))
    raw_density = raw_density[ind_raw]
    yt_density = yt_density[ind_yt]

    # make sure we are comparing fair units
    assert str(yt_density.units) == "g/cm**3"

    # make sure the actual values are the same
    assert_almost_equal(yt_density.d, raw_density)
Example #28
0
def test_no_fields_out():
    ds = data_dir_load(noFields)
    field_list = [('all', 'particle_charge'), ('all', 'particle_id'),
                  ('all', 'particle_mass'), ('all', 'particle_momentum_x'),
                  ('all', 'particle_momentum_y'),
                  ('all', 'particle_momentum_z'),
                  ('all', 'particle_positionCoarse_x'),
                  ('all', 'particle_positionCoarse_y'),
                  ('all', 'particle_positionCoarse_z'),
                  ('all', 'particle_positionOffset_x'),
                  ('all', 'particle_positionOffset_y'),
                  ('all', 'particle_positionOffset_z'),
                  ('all', 'particle_weighting'), ('io', 'particle_charge'),
                  ('io', 'particle_id'), ('io', 'particle_mass'),
                  ('io', 'particle_momentum_x'), ('io', 'particle_momentum_y'),
                  ('io', 'particle_momentum_z'),
                  ('io', 'particle_positionCoarse_x'),
                  ('io', 'particle_positionCoarse_y'),
                  ('io', 'particle_positionCoarse_z'),
                  ('io', 'particle_positionOffset_x'),
                  ('io', 'particle_positionOffset_y'),
                  ('io', 'particle_positionOffset_z'),
                  ('io', 'particle_weighting')]
    domain_dimensions = [1, 1, 1] * np.ones_like(ds.domain_dimensions)
    domain_width = [1, 1, 1] * np.ones_like(ds.domain_left_edge)

    assert isinstance(ds, OpenPMDDataset)
    assert_equal(str(ds), "data00000400.h5")
    assert_equal(ds.dimensionality, 3)
    assert_equal(ds.particle_types_raw, ('io', ))
    assert "all" in ds.particle_unions
    assert_array_equal(ds.field_list, field_list)
    assert_array_equal(ds.domain_dimensions, domain_dimensions)
    assert_almost_equal(ds.current_time,
                        1.3161023868481013e-13 * np.ones_like(ds.current_time))
    assert_almost_equal(ds.domain_right_edge - ds.domain_left_edge,
                        domain_width)
Example #29
0
    def test_make_colormap(self):
        make_colormap([([0, 0, 1], 10), ([1, 1, 1], 10), ([1, 0, 0], 10)],
                      name='french_flag', interpolate=False)
        show_colormaps('french_flag')

        cmap = make_colormap([('dred', 5), ('blue', 2.0), ('orange', 0)],
                             name='my_cmap')
        assert_almost_equal(cmap["red"][1],
                           np.array([0.00392157, 0.62400345, 0.62400345]))

        assert_almost_equal(cmap["blue"][2],
                           np.array([0.00784314, 0.01098901, 0.01098901]))

        assert_almost_equal(cmap["green"][3], np.array([0.01176471, 0.0, 0.0]))
Example #30
0
def test_radius_surface():
    # see #1407
    ds = load(ISOGAL)
    reg = ds.all_data()
    sp = ds.sphere(ds.domain_center, (0.5, 'code_length'))
    for obj in [reg, sp]:
        for rad in [0.05, .1, .4]:
            surface = ds.surface(obj, 'radius', (rad, 'code_length'))
            assert_almost_equal(surface.surface_area.v,
                                4 * np.pi * rad**2,
                                decimal=2)
            verts = surface.vertices
            for i in range(3):
                assert_almost_equal(verts[i, :].min().v, 0.5 - rad, decimal=2)
                assert_almost_equal(verts[i, :].max().v, 0.5 + rad, decimal=2)
Example #31
0
def test_radius_surface():
    # see #1407
    ds = fake_random_ds(64, nprocs=4, particles=16**3, length_unit=10.0)
    reg = ds.all_data()
    sp = ds.sphere(ds.domain_center, (0.5, "code_length"))
    for obj in [reg, sp]:
        for rad in [0.05, 0.1, 0.4]:
            surface = ds.surface(obj, "radius", (rad, "code_length"))
            assert_almost_equal(surface.surface_area.v,
                                4 * np.pi * rad**2,
                                decimal=2)
            verts = surface.vertices
            for i in range(3):
                assert_almost_equal(verts[i, :].min().v, 0.5 - rad, decimal=2)
                assert_almost_equal(verts[i, :].max().v, 0.5 + rad, decimal=2)
Example #32
0
    def test_make_colormap(self):
        make_colormap(
            [([0, 0, 1], 10), ([1, 1, 1], 10), ([1, 0, 0], 10)],
            name="french_flag",
            interpolate=False,
        )
        show_colormaps("french_flag")

        cmap = make_colormap([("dred", 5), ("blue", 2.0), ("orange", 0)],
                             name="my_cmap")
        assert_almost_equal(cmap["red"][1],
                            np.array([0.00392157, 0.62400345, 0.62400345]))

        assert_almost_equal(cmap["blue"][2],
                            np.array([0.00784314, 0.01098901, 0.01098901]))

        assert_almost_equal(cmap["green"][3], np.array([0.01176471, 0.0, 0.0]))