def test_init_maxw(): epc = ParticleCollection() ipc = ParticleCollection() epc, ipc = init_maxw(xmin=1.0, xmax=9.0, n0=1, T0=0.01, V0=0.0, NPIC=100, MMI=100, swidth=0.1) assert epc.particles.size == 100 assert ipc.particles.size == 100 assert epc.particles[0].get_mass() == 1 assert epc.particles[0].get_charge() == -1 assert epc.particles[0].position >= 1.0 assert epc.particles[0].position <= 9.0 assert epc.particles[0].type == "mobile" assert ipc.particles[1].get_mass() == 100 assert ipc.particles[1].get_charge() == 1 assert ipc.particles[1].position >= 1.0 assert ipc.particles[1].position <= 9.0 assert ipc.particles[1].momentum == 0.0 assert ipc.particles[1].type == "immobile"
def simulate(): # Check if output directory exists # If not, create one if not os.path.exists(datadir): os.mkdir(datadir) #initialize simulation Nsteps = int(Tfinish / dt) #calculating number of timesteps Nx = np.array([Grid_Size]) #creating an array with grid size dx = np.array([(xmax - xmin) / Nx.item(0)]) #grid cell size, in de0 #creating a plasma distribution epc = ParticleCollection() ipc = ParticleCollection() epc, ipc = init_maxw(xmin=xmin, xmax=xmax, n0=n0, T0=T0, V0=V0, NPIC=NPIC, MMI=MMI, swidth=dx.item(0)) #creating a grid&field object grid = Grid1DCartesian(dx, Nx, True) grid.set_grid() field = Field1D("Fourier", "Linear", dt, grid) pusher, velfixer = field.get_updaters("LeapFrog") generator = SourceGenerator1DES() i = 0 full_output(field, epc, ipc, datadir, i) while (i < Nsteps): currtime = time.time() print("Nstep=", str(i + 1), ", time=", str(int((i + 1) * dt * 1000) / 1000), "wpe0^-1") pusher(epc, field) pusher(ipc, field) erho = generator.get_source(epc, grid.get_grid()) irho = generator.get_source(ipc, grid.get_grid()) rho = erho + irho field.solve(rho) velfixer(epc, field) velfixer(ipc, field) i += 1 # Check if we are at an output time step if (i % Nout == 0): full_output(field, epc, ipc, datadir, i) print('Timestep took ', str(int(1000 * (time.time() - currtime)) / 1000), ' seconds') return
def test_particle_collection_add_particles(): s = Shape1DTriangle(1) pc = ParticleCollection() p1 = Particle(1, -1, "mobile", np.array([1.2, 0.2, -0.2]), np.array([1, 1, 1]), s) p2 = Particle(4, 1, "immobile", np.array([0.4, 0.2, 0.2]), np.array([1, 1, 1]), s) pc.add_particles(p1) pc.add_particles(p2) compare_particles(pc.particles[0], p1) compare_particles(pc.particles[1], p2)
def test_source_generator(): dx = np.array([1.0]) Nx = np.array([10]) gg = Grid1DCartesian(dx,Nx,False) gg.set_grid() grid = gg.get_grid() gridhalf = gg.get_grid_shifted() s = Shape1DTriangle(1) pc = ParticleCollection() p1 = Particle(1,-1, "mobile", np.array([2.0]), np.array([0.0]), s) p2 = Particle(4, 1, "immobile", np.array([1.0]), np.array([0.0]), s) pc.add_particles(p1) pc.add_particles(p2) generator = SourceGenerator1DES() source = generator.get_source(pc,grid) assert source.size == 10 assert source.item(0) == 0.0 assert source.item(1) == 2.5 assert source.item(2) == -2.5 p1 = Particle(1,-1, "mobile", np.array([2.2]), np.array([0.0]), s) p2 = Particle(4, 1, "immobile", np.array([8.9]), np.array([0.0]), s) pc2 = ParticleCollection() pc2.add_particles(p1) pc2.add_particles(p2) source = generator.get_source(pc2,grid) assert source.size == 10 assert source.item(1) == 0.0 assert np.abs(source.item(2)+2.0) < PRECISION assert np.abs(source.item(9)-2.25) < PRECISION
def test_temperature(): epc = ParticleCollection() ipc = ParticleCollection() epc, ipc = init_maxw(xmin=1.0, xmax=9.0, n0=1, T0=0.01, V0=0.0, NPIC=1000, MMI=100, swidth=0.1) pex = [] for part in epc.particles: pex.append(part.momentum) mu, std = norm.fit(pex) ElectronTemp = std * std / 4.0 assert np.abs((ElectronTemp - 0.01) / ElectronTemp) < TEMP_PRECISION
def test_source_generator_adv(): dx = np.array([1.0]) Nx = np.array([10]) gg = Grid1DCartesian(dx,Nx,False) gg.set_grid() grid = gg.get_grid() gridhalf = gg.get_grid_shifted() s = Shape1DTriangle(1) pc3 = ParticleCollection() p1 = Particle(1,-1, "mobile", np.array([2.2]), np.array([0.0]), s) p2 = Particle(4, 1, "immobile", np.array([1.7]), np.array([0.0]), s) pc3.add_particles(p1) pc3.add_particles(p2) generator = SourceGenerator1DES() source = generator.get_source(pc3,grid) assert source.size == 10 assert np.abs(source.item(2) + 0.25) < PRECISION
def test_particle_collection_set_particles(): s = Shape1DTriangle(1) p1 = Particle(1, -1, "mobile", np.array([1.2, 0.2, -0.2]), np.array([1, 1, 1]), s) particle_array = np.array([p1, p1, p1, p1]) pc = ParticleCollection() assert pc.particles.shape[-1] == 0 # test whether set_particles works properly with empty collections pc.set_particle_collection(particle_array) assert pc.particles.shape[-1] == 4 for i in range(4): compare_particles(pc.particles[i], p1) # test whether set_particles works properly with non-empty collections p2 = Particle(4, 1, "immobile", np.array([0.4, 0.2, 0.2]), np.array([1, 2, 3]), s) pc.set_particle_collection(np.array([p2])) assert pc.particles.shape[-1] == 1 compare_particles(pc.particles[0], p2)
def init_maxw(xmin=1.0, xmax=9.0, n0=1, T0=0.01, V0=0.0, NPIC=1000, MMI=100, swidth=0.1): """ c=e=em=mu=1, de0=1, we0=1 function to initialize uniform plasma slab of particles with density n0, temperature T0, flow velocity V0, number of mobile particles NPIC, mass ratio MMI """ i = 0 xe = [] pex = [] pey = [] pez = [] xi = [] pix = [] piy = [] piz = [] while (i < NPIC): xe.append(xmin + (xmax - xmin) * random.random()) while True: x = 5 * (random.random() - 0.5) y = random.random() if (np.exp(-x * x) >= y): pex.append(2 * np.sqrt(2 * T0) * x + V0) break while True: x = 5 * (random.random() - 0.5) y = random.random() if (np.exp(-x * x) >= y): pey.append(2 * np.sqrt(2 * T0) * x) break while True: x = 5 * (random.random() - 0.5) y = random.random() if (np.exp(-x * x) >= y): pez.append(2 * np.sqrt(2 * T0) * x) break while True: xion = 5 * (random.random() - 0.5) y = random.random() if (np.exp(-x * x) >= y): pix.append(0.0) break while True: x = 5 * (random.random() - 0.5) y = random.random() if (np.exp(-x * x) >= y): piy.append(2 * np.sqrt(2 * T0 / MMI) * x) break while True: x = 5 * (random.random() - 0.5) y = random.random() if (np.exp(-x * x) >= y): piz.append(2 * np.sqrt(2 * T0 / MMI) * x) break i += 1 xi = [x for x in xe] s = Shape1DTriangle(swidth) epc = ParticleCollection() ipc = ParticleCollection() i = 0 while (i < NPIC): p1 = Particle(1, -1, "mobile", np.array([xe[i]]), np.array([pex[i]]), s) p2 = Particle(MMI, 1, "immobile", np.array([xi[i]]), np.array([0.0]), s) epc.add_particles(p1) ipc.add_particles(p2) i += 1 return epc, ipc