def run_integrator(): '''Tests the integrator by running a single step and checking coordinates''' # create fields, particles, integrator particles = make_particles(_PD) fields = make_fields(_PD) dt = .1 / np.amax(fields.omega) my_integrator = integrator.integrator(dt, fields.omega) # take a single step my_integrator.single_step(particles, fields)
def vary_rz_modes(mode_pair, PD, ns=1e2): ''' Simulate ns number of steps of size ss using npart particles and a number of modes specified by mode_pair. Arguments: mode_pair (tuple): [n_r,n_z] PD (dict): dictionary of other fixed parameters ns (Optional[int]) : number of steps to run. Defaults to 100. Returns: timing (float): number of seconds/macroparticle/mode ''' num_steps = ns PD['n_r'] = mode_pair[0] PD['n_z'] = mode_pair[1] num_modes = mode_pair[0] + mode_pair[1] PD['num_p'] = PD['np_mode'] * num_modes num_particles = PD['num_p'] # create fields, particles, integrator particles = make_particles(PD) fields = make_fields(PD) my_integrator = integrator.integrator(PD['dt'], fields) step = 0 print "Running {} total modes and {} particles".format( num_modes, num_particles) t0 = time.time() while step < num_steps: my_integrator.single_step_fields(fields) my_integrator.single_step_ptcl(particles, fields) step = step + 1 t1 = time.time() #print "Performed {:e} steps with {} particles and {} modes in {}s".format(num_steps, num_particles, num_modes, time1-time0) s_per_step = (t1 - t0) / num_steps s_combined = s_per_step / (num_modes * num_particles) return s_combined
def vary_rz_modes(mode_pair, PD, ns=1e2): ''' Simulate ns number of steps of size ss using npart particles and a number of modes specified by mode_pair. Arguments: mode_pair (tuple): [n_r,n_z] PD (dict): dictionary of other fixed parameters ns (Optional[int]) : number of steps to run. Defaults to 100. Returns: timing (float): number of seconds/macroparticle/mode ''' num_steps = ns PD['n_r'] = mode_pair[0] PD['n_z'] = mode_pair[1] num_modes = mode_pair[0] + mode_pair[1] PD['n_macro'] = PD['np_mode'] * num_modes num_particles = PD['n_macro'] PD['weight'] = PD['n_e'] / PD['n_macro'] # create fields, particles, integrator particles = particle_data.particle_data(PD['n_macro'], PD['charge'], PD['mass'], PD['weight']) fields = field_data.field_data(PD['Z'], PD['R'], PD['n_z'], PD['n_r']) create_init_conds(particles, fields, PD) my_integrator = integrator.integrator(PD['dt'], fields) step = 0 #print "Running {} total modes and {} particles".format(num_modes, num_particles) #t0 = time.time() while step < num_steps: my_integrator.second_order_step(particles, fields) step = step + 1 return
def test_integrator(): '''Tests the integrator by running a single step and checking coordinates''' # create fields, particles, integrator particles = make_particles(_PD) fields = make_fields(_PD, particles) dt = .1 / np.amax(fields.omega) my_integrator = integrator.integrator(dt, fields) # take a single step my_integrator.second_order_step(particles, fields) #assertions _assert_array(_PD['expected_dc_coords'], fields.dc_coords) _assert_array(_PD['expected_omega_coords'], fields.omega_coords) _assert_array(_PD['expected_r'], particles.r) _assert_array(_PD['expected_pr'], particles.pr) _assert_array(_PD['expected_z'], particles.z) _assert_array(_PD['expected_pz'], particles.pz) _assert_array(_PD['expected_ell'], particles.ell)
fld_data = field_data.field_data(5., 2., n_r, n_z) fld_data.mode_coords = np.ones((n_r, n_z, 2)) ptcl_data.r = np.ones(n_ptcls) for idx in range(0, n_ptcls): ptcl_data.r[idx] *= idx*(4.)/n_ptcls + .01 ptcl_data.pr = ptcl_data.mc*np.arange(0.,.5,.5/n_ptcls) ptcl_data.ell = ptcl_data.r*ptcl_data.pr*.1 ptcl_data.z = np.zeros(n_ptcls) ptcl_data.pz = ptcl_data.mc*np.arange(0.,10.,10./n_ptcls) dt = .1/np.amax(fld_data.omega) my_integrator = integrator.integrator(dt, fld_data) particle_energies = ptcl_data.compute_ptcl_energy(fld_data) field_energies = fld_data.compute_energy() tot_energy = np.sum(particle_energies) + np.sum(field_energies) E0 = tot_energy E = [] t = [] E.append(tot_energy) t.append(0.) n_steps = 100 step = 0
macro_weight = n_electrons/n_macro_ptcls # Create simulation objects ptcl_data = particle_data.particle_data(n_macro_ptcls, charge, mass, macro_weight) fld_data = field_data.field_data(l_z, l_r, n_z_modes, n_r_modes) # Ten steps per fastest frequency dt = 0.1*2*np.pi/np.amax(fld_data.omega) # run for ten plasma periods run_time = 50*dt #4.*np.pi/k_p # Set the diagnostics compute_energy = False my_integrator = integrator.integrator(dt, fld_data.omega) # Initial conditions for ptcl_idx in range(0, n_macro_ptcls): # Uniform distribution in space ptcl_data.r[ptcl_idx] = np.random.random()*l_r ptcl_data.z[ptcl_idx] = np.random.normal(-0.1*l_z, 1./k_p) ptcl_data.pz[ptcl_idx] = 100.*mass*speed_of_light*macro_weight # Create a thermal boundary radial_boundary = radial_reflecting.radial_reflecting() longitudinal_boundary = longitudinal_absorb.longitudinal_absorb() if compute_energy: # Energy diagnostics
from rssympim.sympim_rz.data import field_data, particle_data from rssympim.sympim_rz.integrators import integrator import numpy as np import timeit n_ptcls = 40000 n_r = 40 n_z = 100 my_fields = field_data.field_data(1., 1., n_r, n_z) my_ptcls = particle_data.particle_data(n_ptcls, 1., 1., 1.) my_ptcls.r = np.ones(n_ptcls) for idx in range(0, n_ptcls): my_ptcls.r[idx] *= idx * (4.) / n_ptcls + .01 my_ptcls.pr = -my_ptcls.mc * np.arange(0., .5, .5 / n_ptcls) my_ptcls.ell = my_ptcls.r * my_ptcls.pr my_ptcls.z = np.zeros(n_ptcls) my_ptcls.pz = my_ptcls.mc * np.arange(0., 10., 10. / n_ptcls) my_integrator = integrator.integrator(.01, my_fields.omega) my_time = timeit.timeit('my_integrator.single_step(my_ptcls, my_fields)') print my_time
t_setup = 0. t_overhead = 0. t0 = time.time() while step < n_steps: # Generate the initial conditions create_init_conds(ptcl_data, fld_data) # Span dt over decades dt = dt0 / ((1.1)**step) # Create the new integrator w/ Yoshida coefficients t_oi = time.time() integrator_2nd = integrator.integrator(dt, fld_data) integrator_4th = integrator_y4(dt, fld_data) integrator_6th = integrator_yn(dt, fld_data, 6) integrator_8th = integrator_yn(dt, fld_data, 8) t_of = time.time() t_overhead += t_of - t_oi # Integrate a single step w/ 2nd order integrator_2nd.update(ptcl_data, fld_data) particle_energies = ptcl_data.compute_ptcl_hamiltonian(fld_data) field_energies = fld_data.compute_energy() tot_energy = np.sum(particle_energies) + np.sum(field_energies) E2.append(np.abs(tot_energy - E0) / np.abs(E0))
fields = field_data.field_data(L, R, n_modes_z, n_modes_r) field_data.omega_coords[:, :, 0] = P_omega[:, :] field_data.omega_coords[:, :, 1] = Q_omega[:, :] field_data.dc_coords[:, :, 0] = P_dc[:, :] field_data.dc_coords[:, :, 1] = Q_dc[:, :] # # Create the integrator # # eight steps per fastest frequency dt = (1. / 8.) * (2. * np.pi / np.max(fields.omega)) my_integrator = integrator.integrator(dt, fields) # # Set up the main loop # t_final = n_modes_z * 2. * np.pi / k_p t = 0. step = 0 my_integrator.half_field_forward(field_data) while t < t_final: # Integrate a single step, first particles, then fields
print '|---------------------' print ' domain length', length, 'cm' print ' domain radius', radius, 'cm' print ' k_p ', k_p, 'cm^-1' print ' dtau ', dtau, 'cm' print ' nsteps ', nsteps print ' macroptcls ', n_macro_ptcls print ' ptcls/core ', n_ptcls_per_core print ' n modes, r ', n_modes_r print ' n modes, z ', n_modes_z step_num = 0 print "Rank {} processor says np is {}".format(rank, ptcl_data.np) update_sequence = integrator.integrator(dtau, fld_data) t0 = time.time() radial_boundary = radial_thermal.radial_thermal(plasma_temperature) longitudinal_boundary = longitudinal_thermal.longitudinal_thermal( plasma_temperature) # Instantiate the I/O objects diag_period = 10 field_dumper = field_io.field_io('wave_flds', diag_period) ptcl_dumper = particle_io.particle_io('wave_ptcls', diag_period, parallel_hdf5=True) # Dump the particles and fields to set up an initial condition field_dumper.dump_field(fld_data, 0)