Exemple #1
0
def initialize_particles():
    '''Initializes particle arrays.
    
    INPUT:
        <NONE>
        
    OUTPUT:
        pos    -- Particle position array (3, 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
    '''
    if const.radix_loading == True:
        pos, vel, idx = uniform_config_reverseradix_velocity()
    elif const.gaussian_T == True:
        pos, vel, idx = uniform_config_random_velocity_gaussian_T()
    else:
        pos, vel, idx = uniform_config_random_velocity()

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

    Bp = np.zeros((3, const.N), dtype=np.float64)
    Ep = np.zeros((3, const.N), dtype=np.float64)
    temp_N = np.zeros((const.N), dtype=np.float64)

    particles.assign_weighting_TSC(pos, idx, Ie, W_elec)
    return pos, vel, Ie, W_elec, Ib, W_mag, idx, Ep, Bp, temp_N
Exemple #2
0
def initialize_particles():
    '''Initializes particle arrays.
    
    INPUT:
        <NONE>
        
    OUTPUT:
        pos    -- Particle position array (3, 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
    '''
    if quiet_start == 1:
        print('Initializing quiet distribution')
        pos, vel, idx = uniform_gaussian_distribution_quiet()
    else:
        pos, vel, idx = uniform_gaussian_distribution()
    #pos, vel, idx = CAM_CL_loading()
    
    Ie         = np.zeros(N,      dtype=nb.uint16)
    Ib         = np.zeros(N,      dtype=nb.uint16)
    W_elec     = np.zeros((3, N), dtype=nb.float64)
    W_mag      = np.zeros((3, N), dtype=nb.float64)
    
    Bp      = np.zeros((3, N), dtype=nb.float64)
    Ep      = np.zeros((3, N), dtype=nb.float64)
    temp_N  = np.zeros((N),    dtype=nb.float64)
    
    particles.assign_weighting_TSC(pos, Ie, W_elec)
    return pos, vel, Ie, W_elec, Ib, W_mag, idx, Ep, Bp, temp_N
Exemple #3
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
    '''
    ppc = particles_per_cell()
    pos, vel, idx = uniform_gaussian_distribution_quiet(ppc)

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

    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
Exemple #4
0
def initialize_particles():
    '''Initializes particle arrays.
    
    INPUT:
        <NONE>
        
    OUTPUT:
        pos    -- Particle position array (3, 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
    '''
    if radix_loading == True:
        print('Loading particle velocities using radix-N scrambling sets.')
        pos, vel, idx = uniform_config_reverseradix_velocity()
    elif gaussian_T == True:
        print('Loading particles with spatially gaussian temperature.')
        pos, vel, idx = uniform_config_random_velocity_gaussian_T()
    else:
        print('Loading particle velocties using random distributions.')
        pos, vel, idx = uniform_config_random_velocity()
    
    Ie      = np.zeros(N, dtype=np.uint16)
    W_elec  = np.zeros((3, N), dtype=np.float64)
    
    Bp      = np.zeros((3, N), dtype=np.float64)
    temp_N  = np.zeros((N),    dtype=np.float64)
    
    particles.assign_weighting_TSC(pos, idx, Ie, W_elec)
    return pos, vel, Ie, W_elec, idx, Bp, temp_N
Exemple #5
0
def test_force_interpolation():
    E = np.zeros((const.NX + 3, 3))
    B = np.zeros((const.NX + 3, 3))
    
    E_nodes = (np.arange(const.NX + 3) - 0.5) #* const.dx
    B_nodes = (np.arange(const.NX + 3) - 1.0) #* const.dx
    
    B[:, 0] = np.arange(const.NX + 3) * 5e-9            # Linear
    B[:, 1] = np.sin(0.5*np.arange(const.NX + 3) + 5)   # Sinusoidal
    B[:, 2] = 4e-9                                      # Constant
    
    E[:, 0] = np.arange(const.NX + 3) * 1e-5        # Linear
    E[:, 1] = np.sin(0.5*np.arange(const.NX + 3))   # Sinusoidal
    E[:, 2] = 3e-5                                  # Constant
    
    fig = plt.figure(figsize=(12, 8))
    ax1 = plt.subplot2grid((3, 3), (0,0), colspan=3)
    ax2 = plt.subplot2grid((3, 3), (1,0), colspan=3)
    ax3 = plt.subplot2grid((3, 3), (2,0), colspan=3)
    #plt.tight_layout(pad=1.0, w_pad=1.8)
    fig.subplots_adjust(hspace=0)
    
    which_field = 'B'
    for ii in np.arange(0, const.NX + 2, 0.5):
        position   = np.array([ii]) * const.dx
        
        Ie, W_elec = particles.assign_weighting_TSC(position, E_nodes=True)
        Ib, W_mag  = particles.assign_weighting_TSC(position, E_nodes=False)
    
        Ep, Bp     = particles.interpolate_forces_to_particle(E, B, Ie[0], W_elec[:, 0], Ib[0], W_mag[:, 0])

        for ax, jj in zip([ax1, ax2, ax3], list(range(3))):
            ax.clear()
            ax.set_xlim(-1.5, const.NX + 2)
            
            if which_field == 'E':
                ax1.set_title('Electric field interpolation to Particle')
                ax.plot(E_nodes, E[:, jj])
                ax.scatter(ii, Ep[jj])
            elif which_field == 'B':
                ax1.set_title('Magnetic field interpolation to Particle')
                ax.plot(B_nodes, B[:, jj])
                ax.scatter(ii, Bp[jj])
 
            for kk in range(const.NX + 3):
                ax.axvline(E_nodes[kk], linestyle='--', c='r', alpha=0.2)
                ax.axvline(B_nodes[kk], linestyle='--', c='b', alpha=0.2)
                
                ax.axvline(const.xmin/const.dx, linestyle='-', c='k', alpha=0.2)
                ax.axvline(const.xmax/const.dx, linestyle='-', c='k', alpha=0.2)
    
        plt.pause(0.01)
    
    plt.show()

    return
Exemple #6
0
def test_velocity_deposition():
    E_nodes = (np.arange(const.NX + 3) - 0.5) #* const.dx
    B_nodes = (np.arange(const.NX + 3) - 1.0) #* const.dx
    
    dt       = 0.1
    velocity = np.array([[0.3 * const.dx / dt, 0.0],
                         [ 0., 0.0],
                         [ 0., 0.0]])
    
    position = np.array([16.5, 16.5]) * const.dx
    idx      = np.array([0, 0]) 
    
    left_nodes, weights = particles.assign_weighting_TSC(position)
    n_i, nu_i = sources.deposit_velocity_moments(velocity, left_nodes, weights, idx)

    for jj in range(const.Nj):
        normalized_density = (const.cellpart / const.Nj)*n_i[:, jj] / const.density[jj]
        species_color = const.temp_color[jj]
        plt.plot(E_nodes, normalized_density, marker='o', c=species_color)
        
        print('Normalized total density contribution of species {} is {}'.format(jj, normalized_density.sum()))

    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)
    return
Exemple #7
0
def test_init_collect_moments():
    # E_nodes = (np.arange(const.NX + 3) - 0.5) #* const.dx
    # B_nodes = (np.arange(const.NX + 3) - 1.0) #* const.dx

    dt = 0.1
    velocity = np.array([[0.3 * const.dx / dt, 0.0], [0., 0.0], [0., 0.0]])

    position = np.array([16.5, 16.5]) * const.dx
    idx = np.array([0, 0])

    left_node, weights = particles.assign_weighting_TSC(position, E_nodes=True)

    position, left_node, weights, rho_0, rho, J_plus, J_init, G, L = sources.init_collect_moments(
        position, velocity, left_node, weights, idx, dt)
    return
Exemple #8
0
def initialize_particles():
    '''Initializes particle arrays.
    
    INPUT:
        <NONE>
        
    OUTPUT:
        pos    -- Particle position array (1, N)
        vel    -- Particle velocity array (3, N)
        W_elec -- Initial particle weights on E-grid
        idx    -- Particle type index
    '''
    ppc = particles_per_cell()
    pos, idx = uniform_distribution(ppc)
    vel = gaussian_distribution(ppc)
    Ie, W_elec = assign_weighting_TSC(pos)
    return pos, vel, Ie, W_elec, idx
Exemple #9
0
def test_weight_conservation():
    '''
    Plots the normalized weight for a single particle at 1e5 points along simulation
    domain. Should remain at 1 the whole time.
    '''
    nspace        = 100000
    xmax          = const.NX*const.dx
    
    positions     = np.linspace(0, xmax, nspace)
    normal_weight = np.zeros(nspace)
    
    for ii, x in zip(np.arange(nspace), positions):
        left_nodes, weights = particles.assign_weighting_TSC(np.array([x]))
        normal_weight[ii]   = weights.sum()

    plt.plot(positions/const.dx, normal_weight)
    plt.xlim(0., const.NX)
    return
Exemple #10
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
Exemple #11
0
    )

    DT, max_inc, part_save_iter, field_save_iter = init.set_timestep(vel)

    # Collect initial moments and save initial state
    sources.collect_moments(vel, Ie, W_elec, idx, q_dens, Ji, ni, nu, temp1D)
    fields.calculate_E(B, Ji, q_dens, E_int, Ve, Te, temp3D, temp3D2, temp1D)

    if save_particles == 1:
        save.save_particle_data(DT, part_save_iter, 0, pos, vel)

    if save_fields == 1:
        save.save_field_data(DT, field_save_iter, 0, Ji, E_int, B, Ve, Te,
                             q_dens)

    particles.assign_weighting_TSC(pos, Ib, W_mag, E_nodes=False)
    particles.velocity_update(vel, Ie, W_elec, Ib, W_mag, idx, B, E_int,
                              -0.5 * DT)

    qq = 1
    print('Starting main loop...')
    while qq < max_inc:
        qq, DT, max_inc, part_save_iter, field_save_iter =               \
        aux.main_loop(pos, vel, idx, Ie, W_elec, Ib, W_mag,              \
              B, E_int, E_half, q_dens, q_dens_adv, Ji, ni, nu,          \
              Ve, Te, temp3D, temp3D2, temp1D, old_particles, old_fields,\
              qq, DT, max_inc, part_save_iter, field_save_iter)

        if qq % part_save_iter == 0 and save_particles == 1:
            save.save_particle_data(DT, part_save_iter, qq, pos, vel)
Exemple #12
0
    Ve = np.zeros((size, 3), dtype=np.float64)
    Te = np.zeros((size), dtype=np.float64)

    temp3d = np.zeros((size, 3), dtype=np.float64)

    if False:
        pos, idx = init.uniform_distribution()
        vel = init.gaussian_distribution()
    else:
        pos, vel, idx = init.quiet_start()

    B[:, 0] = Bc[0]  # Set Bx initial
    B[:, 1] = Bc[1]  # Set By initial
    B[:, 2] = Bc[2]  # Set Bz initial

    particles.assign_weighting_TSC(pos, Ie, W_elec)

    DT, max_inc, part_save_iter, field_save_iter, subcycles = aux.set_timestep(
        vel)

    print('Loading initial state...\n')
    sources.init_collect_moments(pos, vel, Ie, W_elec, idx, ni_init, nu_init,
                                 ni, nu_plus, rho_int, rho_half, J, J_plus, L,
                                 G, 0.5 * DT)

    # Put init into qq = 0 and save as usual, qq = 1 will be at t = dt
    qq = 0
    print('Starting loop...')
    while qq < max_inc:
        ############################
        ##### EXAMINE TIMESTEP #####