Ejemplo n.º 1
0
def test_compute_temperature():
    vx = np.array([0.1, 0.2])
    vy = np.array([10.1, 0.3])
    vz = np.array([23.4, 42.5])
    m = np.array([2.3, 3.4])
    temperature = Ptcls.compute_temperature(vx, vy, vz, m)
    assert temperature > 0
Ejemplo n.º 2
0
def test_compute_kinetic_energies():
    vx = np.array([0.1, 0.2])
    vy = np.array([10.1, 0.3])
    vz = np.array([23.4, 42.5])
    m = np.array([2.3, 3.4])
    kinetic_energies = Ptcls.compute_kinetic_energies(vx, vy, vz, m)
    for i in range(2):
        assert abs(kinetic_energies[i] -
                0.5 * m[i] * (vx[i]**2 + vy[i]**2 + vz[i]**2)) < 1.0e-7
Ejemplo n.º 3
0
def run_simulation(n_wells, dt, tmax, num_dump = 1):
    # initialize particles
    n_ions = n_wells**3
    ptcls = Ptcls.Ptcls()
    ptcls.set_nptcls(2 * n_ions)

    # ions
    lattice_spacing = 1.0e-6
    xmin = -0.5 * (n_wells - 1) * lattice_spacing
    for i in range(n_ions):
        ptcls.x()[i] = xmin + lattice_spacing * ((i / (n_wells**0)) % n_wells)
        ptcls.y()[i] = xmin + lattice_spacing * ((i / (n_wells**1)) % n_wells)
        ptcls.z()[i] = xmin + lattice_spacing * ((i / (n_wells**2)) % n_wells)
    ptcls.ptclList[3:6,:n_ions] = 0
    ptcls.ptclList[6,:n_ions] = fund_charge
    ptcls.ptclList[7,:n_ions] = ion_mass

    #electrons
    ptcls.ptclList[0:3,n_ions:] = np.random.normal(
            0.0, 0.5 * abs(xmin), ptcls.ptclList[0:3,n_ions:].shape)
    electron_temperature = 3.0
    electron_mass = 9.10938291e-31
    kB = 1.3806e-23
    vThermal = np.sqrt(kB * electron_temperature / electron_mass)
    ptcls.ptclList[3:6,n_ions:] = np.random.normal(
            0.0, vThermal, ptcls.ptclList[3:6,n_ions:].shape)
    ptcls.ptclList[6,n_ions:] = -fund_charge
    ptcls.ptclList[7,n_ions:] = electron_mass


    ctx = cl.create_some_context(interactive = True)
    queue = cl.CommandQueue(ctx)
    coulomb_acc = CoulombAcc.CoulombAcc(ctx, queue)
    accelerations = [coulomb_acc]
    updater = BorisUpdater.BorisUpdater(ctx, queue)

    xd = cl_array.to_device(queue, ptcls.x())
    yd = cl_array.to_device(queue, ptcls.y())
    zd = cl_array.to_device(queue, ptcls.z())
    vxd = cl_array.to_device(queue, ptcls.vx())
    vyd = cl_array.to_device(queue, ptcls.vy())
    vzd = cl_array.to_device(queue, ptcls.vz())
    qd = cl_array.to_device(queue, ptcls.q())
    md = cl_array.to_device(queue, ptcls.m())

    t = 0.0
    while t < tmax:
        np.save('ptcls' + str(i) + '.npy',ptcls.ptclList[0:3,:])
        t = updater.update(
                xd, yd, zd, vxd, vyd, vzd, qd, md, accelerations, t, dt,
                num_dump)
        xd.get(queue, ptcls.x())
        yd.get(queue, ptcls.y())
        zd.get(queue, ptcls.z())

    np.savetxt('ptcls' + str(num_dump) + '.txt',ptcls.ptclList[0:3,:])
Ejemplo n.º 4
0
def test_takeSteps():
    numSteps = 3
    ptcls = Ptcls.Ptcls()
    ptcls.numPtcls = 10
    ptcls.init_ptcls()
    # initial mean velocity of particles
    ptcls.ptclList[5] = 20.0 * np.ones_like(ptcls.ptclList[5])
    history = np.ndarray([numSteps, ptcls.numPtcls])
    updater = BendKickUpdater.BendKickUpdater(testCtx, testQueue)
    updater.trapConfiguration.Bz = 1.0e-6
    accelerations = [cla0, cla1]
    for i in range(numSteps):
        history[i] = ptcls.vz().copy()
        take_steps(10, 1.0e-6, ptcls, updater, accelerations)
Ejemplo n.º 5
0
    def __init__(self, ctx=None, queue=None):
        self.accList = []
        self.ptcls = Ptcls.Ptcls()
        self.t = 0.0

        self.ctx = ctx
        self.queue = queue
        if self.ctx == None:
            self.ctx = cl.create_some_context()
        if self.queue == None:
            self.queue = cl.CommandQueue(
                self.ctx,
                properties=cl.command_queue_properties.PROFILING_ENABLE)

        self.trapConfiguration = TrapConfiguration.TrapConfiguration()
        self.updater = BendKickUpdater.BendKickUpdater(self.ctx, self.queue)
        self.updater.trapConfiguration = self.trapConfiguration
Ejemplo n.º 6
0
def test_initPtcls():
    ptcls = Ptcls.Ptcls()
    ptcls.numPtcls = 10
    ptcls.init_ptcls()
    # initial mean velocity of particles
    ptcls.ptclList[5] = 20.0 * np.ones_like(ptcls.ptclList[5])
Ejemplo n.º 7
0
def test_temperature_of_particles():
    ptcls = Ptcls.Ptcls()
    ptcls.vth = 1.0e4
    ptcls.set_nptcls(10)
    ptcls.init_ptcls()
    assert ptcls.temperature() > 0
Ejemplo n.º 8
0
def run_simulation(n_ions, dt, tmax, num_dump=1):
    # initialize particles
    ptcls = Ptcls.Ptcls()
    ptcls.set_nptcls(2 * n_ions)
    density = 1.0e+18
    volume = n_ions / density
    l = (3 * volume / 4 / np.pi)**(1. / 3.)
    a_ws = (3 / (4 * np.pi * density))**(1. / 3.)
    l = l / a_ws
    # electron plasma frequency is used to scale time t -> t * w_e
    w_e = np.sqrt(density * fund_charge_SI**2 / (electron_mass_SI * Epsilon))

    # ions
    i = 0
    while i < n_ions:
        xt = (np.random.random() - 0.5) * l * 2
        yt = (np.random.random() - 0.5) * l * 2
        zt = (np.random.random() - 0.5) * l * 2
        rt = np.sqrt(xt**2 + yt**2 + zt**2)
        if rt <= l:
            ptcls.x()[i] = xt
            ptcls.y()[i] = yt
            ptcls.z()[i] = zt
            i += 1
    ptcls.ptclList[3:6, :n_ions] = 0
    ptcls.ptclList[6, :n_ions] = fund_charge
    ptcls.ptclList[7, :n_ions] = ion_mass

    #electrons
    i = 0
    while i < n_ions:
        xt = (np.random.random() - 0.5) * l * 2
        yt = (np.random.random() - 0.5) * l * 2
        zt = (np.random.random() - 0.5) * l * 2
        rt = np.sqrt(xt**2 + yt**2 + zt**2)
        if rt <= l:
            ptcls.x()[n_ions + i] = xt
            ptcls.y()[n_ions + i] = yt
            ptcls.z()[n_ions + i] = zt
            i += 1

    electron_temperature = 3.0
    vThermal = np.sqrt(kB * electron_temperature / electron_mass_SI)
    vThermal = vThermal / (a_ws * w_e)
    ptcls.ptclList[3:6, n_ions:] = np.random.normal(
        0.0, vThermal, ptcls.ptclList[3:6, n_ions:].shape)
    ptcls.ptclList[6, n_ions:] = -fund_charge
    ptcls.ptclList[7, n_ions:] = electron_mass

    ctx = cl.create_some_context(interactive=True)
    queue = cl.CommandQueue(ctx)
    coulomb_acc = CoulombAccScaled.CoulombAccScaled(ctx, queue)
    accelerations = [coulomb_acc]
    updater = BorisUpdater.BorisUpdater(ctx, queue)

    xd = cl_array.to_device(queue, ptcls.x())
    yd = cl_array.to_device(queue, ptcls.y())
    zd = cl_array.to_device(queue, ptcls.z())
    vxd = cl_array.to_device(queue, ptcls.vx())
    vyd = cl_array.to_device(queue, ptcls.vy())
    vzd = cl_array.to_device(queue, ptcls.vz())
    qd = cl_array.to_device(queue, ptcls.q())
    md = cl_array.to_device(queue, ptcls.m())

    t = 0.0
    while t < tmax:
        #np.save('data\ptcls' + str(t) + '.npy',ptcls.ptclList[0:6,:])
        np.savetxt('data\ptcls' + str(t) + '.txt', ptcls.ptclList[0:6, :])
        t = updater.update(xd, yd, zd, vxd, vyd, vzd, qd, md, accelerations, t,
                           dt, num_dump)
        xd.get(queue, ptcls.x())
        yd.get(queue, ptcls.y())
        zd.get(queue, ptcls.z())
        vxd.get(queue, ptcls.vx())
        vyd.get(queue, ptcls.vy())
        vzd.get(queue, ptcls.vz())

    np.savetxt('data\ptcls' + str(t) + '.txt', ptcls.ptclList[0:6, :])