示例#1
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[("gas", "cell_volume")].sum(dtype="float64")),
                        1.0)
    assert_equal(dd[("index", "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[("gas", "cell_volume")].sum(dtype="float64")),
                        1.0)
    assert_equal(dd[("index", "ones")].size, Nx * Ny * Nz)
    assert_almost_equal(dd[("index", "dx")].to_ndarray(), 1.0 / Nx)
    assert_almost_equal(dd[("index", "dy")].to_ndarray(), 1.0 / Ny)
    assert_almost_equal(dd[("index", "dz")].to_ndarray(), 1.0 / Nz)

    s = SlicePlot(ds, "x", "random_field")
    s._setup_plots()
    s.frb[("stream", "random_field")]
示例#2
0
    def __init__(self,
                 ds,
                 image_name="ytSliceImage",
                 axis='z',
                 variable='density',
                 center=[0.0, 0.0, 0.0],
                 width=10.0,
                 units='kpc',
                 color_map='hot',
                 show_annotations=True):
        from yt import SlicePlot
        p = SlicePlot(ds, axis, variable, center=center, width=(width, units))
        p.hide_axes()
        p.hide_colorbar()
        p.set_cmap(variable, color_map)
        p._setup_plots()
        plot = p.plots[variable]
        plot.canvas.draw()
        buff = plot.canvas.tostring_argb()
        ncols, nrows = plot.canvas.get_width_height()
        vv = np.fromstring(buff, dtype=np.uint8).reshape((nrows, ncols, 4),
                                                         order="C")
        if show_annotations:
            p = SlicePlot(ds,
                          axis,
                          variable,
                          center=center,
                          width=(width, units))
            p.set_cmap(variable, color_map)
            p._setup_plots()
            plot = p.plots[variable]
            plot.canvas.draw()
            buff2 = plot.canvas.tostring_argb()
            ncols2, nrows2 = plot.canvas.get_width_height()
            vv2 = np.fromstring(buff2, dtype=np.uint8).reshape(
                (nrows2, ncols2, 4), order="C")

        ncols = vv.shape[1]
        nrows = vv.shape[0]

        # delete if already there
        for im in bpy.data.images:
            if im.name == image_name:
                delete_unused_images(image_name)
                delete_unused_textures(image_name)
                delete_unused_materials(image_name)

        # for annotations
        for im in bpy.data.images:
            if im.name == image_name + '_ann':
                delete_unused_images(image_name + '_ann')
                delete_unused_textures(image_name + '_ann')
                delete_unused_materials(image_name + '_ann')

        # switching a from back to front, and flipping image
        # if also with annotation do a nice one in the domain
        pixels_tmp = np.array(vv)
        for i in range(0, nrows):
            #pixels_tmp[i,:,0] = vv[i,:,0]
            #pixels_tmp[i,:,1] = vv[i,:,1]
            #pixels_tmp[i,:,2] = vv[i,:,2]
            #pixels_tmp[i,:,3] = vv[i,:,3]
            pixels_tmp[i, :, 3] = vv[nrows - 1 - i, :, 0]
            pixels_tmp[i, :, 0] = vv[nrows - 1 - i, :, 1]
            pixels_tmp[i, :, 1] = vv[nrows - 1 - i, :, 2]
            pixels_tmp[i, :, 2] = vv[nrows - 1 - i, :, 3]

        if show_annotations:
            pixels_tmp2 = np.array(vv2)
        if show_annotations:
            for i in range(0, nrows2):
                pixels_tmp2[i, :, 3] = vv2[nrows2 - 1 - i, :, 0]
                pixels_tmp2[i, :, 0] = vv2[nrows2 - 1 - i, :, 1]
                pixels_tmp2[i, :, 1] = vv2[nrows2 - 1 - i, :, 2]
                pixels_tmp2[i, :, 2] = vv2[nrows2 - 1 - i, :, 3]

        # blank image
        image = bpy.data.images.new(image_name, width=ncols, height=nrows)
        if show_annotations:
            image2 = bpy.data.images.new(image_name + '_ann',
                                         width=ncols2,
                                         height=nrows2)

        pixels = pixels_tmp.ravel() / 255.
        #pixels = vv.ravel()/255.
        if show_annotations:
            pixels2 = pixels_tmp2.ravel() / 255.

        image.pixels = pixels
        if show_annotations:
            image2.pixels = pixels2

        # activate the image in the uv editor
        for area in bpy.context.screen.areas:
            if area.type == 'IMAGE_EDITOR':
                if show_annotations:
                    area.spaces.active.image = image2
                else:
                    area.spaces.active.image = image
            elif area.type == 'VIEW_3D':  # make sure material/render view is on
                for space in area.spaces:
                    if space.type == 'VIEW_3D':
                        if (space.viewport_shade != 'RENDERED') and (
                                space.viewport_shade != 'MATERIAL'):
                            space.viewport_shade = 'RENDERED'  # material is too slow

        # also, attach to the projection in the blender 3d window
        # now, set color
        figmat = makeMaterial(image_name, (0, 0, 0), (1, 1, 1), 1.0,
                              0.0)  # no emissivity or transperency for now
        setMaterial(bpy.data.objects[image_name], figmat)
        # also, make shadeless
        bpy.data.materials[image_name].use_shadeless = True

        # Create image texture from image
        cTex = bpy.data.textures.new(image_name, type='IMAGE')
        cTex.image = image
        # Add texture slot for color texture
        mat = bpy.data.materials[image_name]
        mtex = mat.texture_slots.add()
        mtex.texture = cTex
        #mtex.texture_coords = 'UV'
        mtex.texture_coords = 'OBJECT'  # this seems to work better for figures, but certainly needs to be tested
        mtex.use_map_color_diffuse = True
        mtex.use_map_color_emission = True
        mtex.emission_color_factor = 0.5
        mtex.use_map_density = True
        mtex.mapping = 'FLAT'

        # map to object
        mtex.object = bpy.data.objects[image_name]