Beispiel #1
0
def test_weight_conservation():
    '''
    Plots the normalized weight for a single particle at 1e6 points along simulation
    domain. Should remain at 1 the whole time.
    '''
    nspace_x = 100
    nspace_y = 100
    xmax = const.NX * const.dx
    ymax = const.NY * const.dy
    pos_x = np.linspace(0, xmax, nspace_x)
    pos_y = np.linspace(0, ymax, nspace_y)
    positions = np.zeros((2, nspace_x * nspace_y))  # x and y
    weights = np.zeros((2, nspace_x * nspace_y))  # elec and mag

    xx = 0
    for ii in range(nspace_x):
        for jj in range(nspace_y):
            positions[0, xx] = pos_x[ii]
            positions[1, xx] = pos_y[jj]

            pos = np.array([pos_x[ii], pos_y[jj]]).reshape((2, 1))

            for grid in [0, 1]:
                I = np.zeros(pos.shape)
                W = np.zeros((2, 1, 3))
                particles.assign_weighting_TSC(pos, I, W, E_nodes=(grid == 0))

                for mm in range(3):
                    for nn in range(3):
                        weights[grid, xx] += W[0, 0, mm] * W[1, 0, nn]

                print(weights[grid, xx])
            xx += 1
    return
Beispiel #2
0
def initialize_particles():
    '''Initializes particle arrays.
    
    INPUT:
        <NONE>
        
    OUTPUT:
        pos    -- Particle position array (1, N)
        vel    -- Particle velocity array (3, N)
        Ie     -- Initial particle positions by leftmost E-field node
        W_elec -- Initial particle weights on E-grid
        Ib     -- Initial particle positions by leftmost B-field node
        W_mag  -- Initial particle weights on B-grid
        idx    -- Particle type index
    '''
    print('Initializing particles')
    np.random.seed(seed)  # Pretty sure it works here. Just check?
    N = N_species.sum()

    ppc = particles_per_cell()
    pos, idx = initialize_configuration_space(ppc)
    vel = initialize_velocity_space(ppc)

    Ie = np.zeros((2, N), dtype=np.uint16)
    Ib = np.zeros((2, N), dtype=np.uint16)
    W_elec = np.zeros((2, N, 3), dtype=np.float64)
    W_mag = np.zeros((2, N, 3), dtype=np.float64)

    print('Assigning initial weights to particles')
    assign_weighting_TSC(pos, Ie, W_elec)
    assign_weighting_TSC(pos, Ib, W_mag, E_nodes=False)
    return pos, vel, Ie, W_elec, Ib, W_mag, idx
Beispiel #3
0
def test_weight_shape():
    plt.ion()

    E_nodes = (np.arange(const.NX + 3) - 0.5)  #* const.dx
    B_nodes = (np.arange(const.NX + 3) - 1.0)  #* const.dx
    dns_test = np.zeros(const.NX + 3)

    positions = np.array([0.0]) * const.dx
    left_nodes, weights = particles.assign_weighting_TSC(positions)

    for jj in range(3):
        dns_test[left_nodes + jj] = weights[jj]

    plt.plot(E_nodes, dns_test, marker='o')

    for ii in range(const.NX + 3):
        plt.axvline(E_nodes[ii], linestyle='--', c='r', alpha=0.2)
        plt.axvline(B_nodes[ii], linestyle='--', c='b', alpha=0.2)

    plt.axvline(const.xmin / const.dx, linestyle='-', c='k', alpha=0.2)
    plt.axvline(const.xmax / const.dx, linestyle='-', c='k', alpha=0.2)

    plt.xlim(-1.5, const.NX + 2)
    return
Beispiel #4
0
def test_density_and_velocity_deposition():
    '''
    Moment deposition seems weighed fine.
    Boundary cell thing seems reversed: x boundary stuff goes to y boundary? WTF?
    Weights and nodes themselves are calculated right
    '''
    NX = const.NX
    NY = const.NY
    xmax = const.xmax
    ymax = const.ymax
    dx = const.dx
    dy = const.dy

    cells_x = np.arange(NX + 3) * dx
    cells_y = np.arange(NY + 3) * dy
    EX, EY = np.meshgrid((cells_x - 0.5 * dx), (cells_y - 0.5 * dy))

    q_dens = np.zeros((NX + 3, NY + 3), dtype=np.float64)
    Ji = np.zeros((NX + 3, NY + 3, 3), dtype=np.float64)
    ni = np.zeros((NX + 3, NY + 3, 1), dtype=np.float64)
    nu = np.zeros((NX + 3, NY + 3, 1, 3), dtype=np.float64)

    pos = np.array([5.0 * xmax / NX, 1.0 * ymax / NY]).reshape((2, 1))
    vel = np.array([1, 2, 4]).reshape((3, 1))

    Ie = np.zeros(pos.shape, dtype=int)
    We = np.zeros((2, 1, 3))
    idx = np.array([0])

    particles.assign_weighting_TSC(pos, Ie, We)

    #sources.deposit_moments_to_grid(vel, Ie, We, idx, ni, nu)
    sources.collect_moments(vel, Ie, We, idx, q_dens, Ji, ni, nu)

    # Normalize contribution (since we just care about positions/weights)
    q_dens /= (const.q * const.n_contr[idx[0]])
    Ji /= (const.q * const.n_contr[idx[0]])

    if True:
        fig = plt.figure(figsize=(15, 15))
        ax = fig.add_subplot(111)
        ################
        ## GRID SPACE ##
        ################
        ax.scatter(EX, EY, c='r', marker='x', s=20)  # E node locations

        ax.set_xlabel('x direction')
        ax.set_ylabel('y direction')
        ax.set_xlim(-1.5 * dx, xmax + 1.5 * dx)
        ax.set_ylim(-1.5 * dy, ymax + 1.5 * dy)

        border = np.array([[0, 0, 0], [0, ymax, 0], [xmax, ymax, 0],
                           [xmax, 0, 0], [0, 0, 0]])
        ax.plot(border[:, 0],
                border[:, 1],
                border[:, 2],
                c='k',
                linestyle='--')

        for mm in range(const.NX):
            plt.vlines(mm * dx, 0, ymax, linestyles='--', alpha=0.20)

        for nn in range(const.NY):
            plt.hlines(nn * dy, 0, xmax, linestyles='--', alpha=0.20)

        #############
        #############
        #im = ax.pcolormesh(cells_x - 1.0*dx, cells_y - 1.0*dy, q_dens[:, :].T, alpha=0.9, cmap='Greens')
        im = ax.pcolormesh(cells_x - 1.0 * dx,
                           cells_y - 1.0 * dy,
                           Ji[:, :, 2].T,
                           alpha=0.9,
                           cmap='Greens')
        ax.scatter(pos[0], pos[1], c='k', marker='o', s=40)

        plt.colorbar(im)
    return
Beispiel #5
0
def test_weight_correctness():
    '''
    Nodes being referenced correctly as of 19/07/19
    Weights being assigned properly?
    '''
    dx = const.dx
    dy = const.dy

    xmax = const.NX * dx
    ymax = const.NY * dy
    cells_x = np.arange(const.NX + 3) * dx
    cells_y = np.arange(const.NX + 3) * dy

    EX, EY = np.meshgrid(cells_x - 0.5 * dx, cells_y - 0.5 * dy)
    BX, BY = np.meshgrid(cells_x - 1.0 * dx, cells_y - 1.0 * dy)

    pos = np.array([0.5 * xmax, 0.5 * ymax]).reshape((2, 1))

    Ie = np.zeros(pos.shape)
    We = np.zeros((2, 1, 3))

    Ib = np.zeros(pos.shape)
    Wb = np.zeros((2, 1, 3))

    dt = 0.1

    pos = np.array([np.random.uniform(0, xmax),
                    np.random.uniform(0, ymax)]).reshape((2, 1))
    plt.figure()

    for ii in range(1):
        particles.assign_weighting_TSC(pos, Ie, We, E_nodes=True)
        particles.assign_weighting_TSC(pos, Ib, Wb, E_nodes=False)

        Ie[0] -= 0.5
        Ie[0] *= dx
        Ie[1] -= 0.5
        Ie[1] *= dy

        Ib[0] -= 1.0
        Ib[0] *= dx
        Ib[1] -= 1.0
        Ib[1] *= dy

        plt.gca().clear()
        ## GRID SPACE ##
        plt.scatter(EX, EY, c='r', marker='x', s=20)  # E node locations
        plt.scatter(BX, BY, c='b', marker='x', s=20)  # B node locations

        plt.xlim(-1.5 * dx, xmax + 1.5 * dx)
        plt.ylim(-1.5 * dy, ymax + 1.5 * dy)

        for mm in range(const.NX):
            plt.vlines(mm * dx, 0, ymax, linestyles='--', alpha=0.20)

        for nn in range(const.NY):
            plt.hlines(nn * dy, 0, xmax, linestyles='--', alpha=0.20)

        plt.hlines(0, 0, xmax, color='k')
        plt.hlines(ymax, 0, xmax, color='k')
        plt.vlines(0, 0, ymax, color='k')
        plt.vlines(xmax, 0, ymax, color='k')
        ################

        ## PARTICLE STUFF ##
        particle_shape = patches.Rectangle((pos[0] - dx, pos[1] - dy),
                                           2 * dx,
                                           2 * dy,
                                           alpha=0.2,
                                           color='k')

        wec = 0
        wbc = 0
        for ii in range(3):
            for jj in range(3):
                W_elec = We[0, 0, ii] * We[1, 0, jj]
                W_mag = Wb[0, 0, ii] * Wb[1, 0, jj]

                plt.scatter(Ie[0] + ii * dx,
                            Ie[1] + jj * dy,
                            s=20,
                            marker='o',
                            c='r')
                plt.gca().annotate(W_elec, (Ie[0] + ii * dx, Ie[1] + jj * dy))

                plt.scatter(Ib[0] + ii * dx,
                            Ib[1] + jj * dy,
                            s=20,
                            marker='o',
                            c='b')
                plt.gca().annotate(W_mag, (Ib[0] + ii * dx, Ib[1] + jj * dy))

                wec += W_elec
                wbc += W_mag

        print(wec, wbc)

        plt.scatter(pos[0], pos[1], c='k', marker='o', s=20)
        plt.gca().add_patch(particle_shape)

        plt.pause(0.2)

        vx = const.dx * 0.5  #np.random.uniform(-0.5, 0.5)
        vy = const.dy * 0.2  #np.random.uniform(-0.5, 0.5)

        vel = np.array([[vx], [vy], [0.]]) / dt

        position_update_single(pos, vel, dt, [0, xmax, 0, ymax])


# =============================================================================
#         figManager = plt.get_current_fig_manager()
#         figManager.window.showMaximized()
# =============================================================================
    return