def test_with_static_fields(): from pyrticle.units import SIUnitsWithNaturalConstants units = SIUnitsWithNaturalConstants() from hedge.discretization.local import TetrahedronDiscretization from hedge.mesh.generator import \ make_box_mesh, \ make_cylinder_mesh from hedge.discretization import Discretization # discretization setup ---------------------------------------------------- radius = 1*units.M full_mesh = make_cylinder_mesh(radius=radius, height=2*radius, periodic=True, radial_subdivisions=30) from hedge.backends import guess_run_context pcon = guess_run_context([]) if pcon.is_head_rank: mesh = pcon.distribute_mesh(full_mesh) else: mesh = pcon.receive_mesh() discr = pcon.make_discretization(mesh, order=1) # particles setup --------------------------------------------------------- def get_setup(case): c = units.VACUUM_LIGHT_SPEED() from static_field import LarmorScrew, EBParallel if case == "screw": return LarmorScrew(units, mass=units.EL_MASS, charge=units.EL_CHARGE, c=c, vpar=c*0.8, vperp=c*0.1, bz=1e-3, nparticles=4) elif case == "epb": return EBParallel(units, mass=units.EL_MASS, charge=units.EL_CHARGE, c=c, ez=1e+5, bz=1e-3, radius=0.5*radius, nparticles=1) else: raise ValueError, "invalid test case" from pyrticle.pusher import \ MonomialParticlePusher, \ AverageParticlePusher from static_field import run_setup for pusher in [MonomialParticlePusher, AverageParticlePusher]: for case in ["screw", "epb"]: casename = "%s-%s" % (case, pusher.__class__.__name__.lower()) run_setup(units, casename, get_setup(case), discr, pusher)
def test_from_to_gpu(): import pycuda.driver as cuda from hedge.mesh import make_cylinder_mesh, make_ball_mesh, make_box_mesh mesh = make_cylinder_mesh(max_volume=0.004, periodic=False, radial_subdivisions=32) from hedge.backends.cuda import Discretization discr = Discretization(mesh, order=4, init_cuda=False, debug=["cuda_no_plan"]) a = numpy.arange(len(discr), dtype=numpy.float32) a_gpu = discr.convert_volume(a, discr.compute_kind) a_copy = discr.convert_volume(a_gpu, "numpy") diff = a - a_copy assert la.norm(diff) < 1e-10 * la.norm(a)
def test_kv_with_no_charge(): from random import seed seed(0) from pyrticle.units import SIUnitsWithNaturalConstants units = SIUnitsWithNaturalConstants() # discretization setup ---------------------------------------------------- from hedge.mesh import make_cylinder_mesh from hedge.backends import guess_run_context rcon = guess_run_context([]) tube_length = 100 * units.MM mesh = make_cylinder_mesh(radius=25 * units.MM, height=tube_length, periodic=True) discr = rcon.make_discretization(mesh, order=3) dt = discr.dt_factor(units.VACUUM_LIGHT_SPEED()) / 2 final_time = 1 * units.M / units.VACUUM_LIGHT_SPEED() nsteps = int(final_time / dt) + 1 dt = final_time / nsteps # particles setup --------------------------------------------------------- from pyrticle.cloud import PicMethod from pyrticle.deposition.shape import ShapeFunctionDepositor from pyrticle.pusher import MonomialParticlePusher method = PicMethod(discr, units, ShapeFunctionDepositor(), MonomialParticlePusher(), 3, 3) nparticles = 10000 cloud_charge = 1e-9 * units.C electrons_per_particle = cloud_charge / nparticles / units.EL_CHARGE el_energy = 5.2e6 * units.EV gamma = el_energy / units.EL_REST_ENERGY() beta = (1 - 1 / gamma**2)**0.5 from pyrticle.distribution import KVZIntervalBeam beam = KVZIntervalBeam(units, total_charge=0, p_charge=0, p_mass=electrons_per_particle * units.EL_MASS, radii=2 * [2.5 * units.MM], emittances=2 * [5 * units.MM * units.MRAD], z_length=5 * units.MM, z_pos=10 * units.MM, beta=beta) state = method.make_state() method.add_particles(state, beam.generate_particles(), nparticles) # diagnostics setup ------------------------------------------------------- from pytools.log import LogManager from pyrticle.log import add_beam_quantities, StateObserver observer = StateObserver(method, None) logmgr = LogManager(mode="w") add_beam_quantities(logmgr, observer, axis=0, beam_axis=2) from pyrticle.distribution import KVPredictedRadius logmgr.add_quantity( KVPredictedRadius(dt, beam_v=beta * units.VACUUM_LIGHT_SPEED(), predictor=beam.get_rms_predictor(axis=0), suffix="x_rms")) logmgr.add_quantity( KVPredictedRadius(dt, beam_v=beta * units.VACUUM_LIGHT_SPEED(), predictor=beam.get_total_predictor(axis=0), suffix="x_total")) # timestep loop ----------------------------------------------------------- vel = method.velocities(state) from hedge.tools import join_fields def rhs(t, y): return join_fields([ vel, 0 * vel, 0, # drecon ]) from hedge.timestep.runge_kutta import LSRK4TimeStepper stepper = LSRK4TimeStepper() t = 0 from pyrticle.cloud import TimesteppablePicState ts_state = TimesteppablePicState(method, state) for step in xrange(nsteps): observer.set_fields_and_state(None, ts_state.state) logmgr.tick() ts_state = stepper(ts_state, t, dt, rhs) method.upkeep(ts_state.state) t += dt logmgr.tick() _, _, err_table = logmgr.get_expr_dataset( "(rx_rms-rx_rms_theory)/rx_rms_theory") rel_max_rms_error = max(err for step, err in err_table) assert rel_max_rms_error < 0.01
def test_with_static_fields(): from pyrticle.units import SIUnitsWithNaturalConstants units = SIUnitsWithNaturalConstants() from hedge.discretization.local import TetrahedronDiscretization from hedge.mesh.generator import \ make_box_mesh, \ make_cylinder_mesh from hedge.discretization import Discretization # discretization setup ---------------------------------------------------- radius = 1 * units.M full_mesh = make_cylinder_mesh(radius=radius, height=2 * radius, periodic=True, radial_subdivisions=30) from hedge.backends import guess_run_context pcon = guess_run_context([]) if pcon.is_head_rank: mesh = pcon.distribute_mesh(full_mesh) else: mesh = pcon.receive_mesh() discr = pcon.make_discretization(mesh, order=1) # particles setup --------------------------------------------------------- def get_setup(case): c = units.VACUUM_LIGHT_SPEED() from static_field import LarmorScrew, EBParallel if case == "screw": return LarmorScrew(units, mass=units.EL_MASS, charge=units.EL_CHARGE, c=c, vpar=c * 0.8, vperp=c * 0.1, bz=1e-3, nparticles=4) elif case == "epb": return EBParallel(units, mass=units.EL_MASS, charge=units.EL_CHARGE, c=c, ez=1e+5, bz=1e-3, radius=0.5 * radius, nparticles=1) else: raise ValueError, "invalid test case" from pyrticle.pusher import \ MonomialParticlePusher, \ AverageParticlePusher from static_field import run_setup for pusher in [MonomialParticlePusher, AverageParticlePusher]: for case in ["screw", "epb"]: casename = "%s-%s" % (case, pusher.__class__.__name__.lower()) run_setup(units, casename, get_setup(case), discr, pusher)
def test_kv_with_no_charge(): from random import seed seed(0) from pyrticle.units import SIUnitsWithNaturalConstants units = SIUnitsWithNaturalConstants() # discretization setup ---------------------------------------------------- from hedge.mesh import make_cylinder_mesh from hedge.backends import guess_run_context rcon = guess_run_context([]) tube_length = 100*units.MM mesh = make_cylinder_mesh(radius=25*units.MM, height=tube_length, periodic=True) discr = rcon.make_discretization(mesh, order=3) dt = discr.dt_factor(units.VACUUM_LIGHT_SPEED()) / 2 final_time = 1*units.M/units.VACUUM_LIGHT_SPEED() nsteps = int(final_time/dt)+1 dt = final_time/nsteps # particles setup --------------------------------------------------------- from pyrticle.cloud import PicMethod from pyrticle.deposition.shape import ShapeFunctionDepositor from pyrticle.pusher import MonomialParticlePusher method = PicMethod(discr, units, ShapeFunctionDepositor(), MonomialParticlePusher(), 3, 3) nparticles = 10000 cloud_charge = 1e-9 * units.C electrons_per_particle = cloud_charge/nparticles/units.EL_CHARGE el_energy = 5.2e6 * units.EV gamma = el_energy/units.EL_REST_ENERGY() beta = (1-1/gamma**2)**0.5 from pyrticle.distribution import KVZIntervalBeam beam = KVZIntervalBeam(units, total_charge=0, p_charge=0, p_mass=electrons_per_particle*units.EL_MASS, radii=2*[2.5*units.MM], emittances=2*[5 * units.MM * units.MRAD], z_length=5*units.MM, z_pos=10*units.MM, beta=beta) state = method.make_state() method.add_particles(state, beam.generate_particles(), nparticles) # diagnostics setup ------------------------------------------------------- from pytools.log import LogManager from pyrticle.log import add_beam_quantities, StateObserver observer = StateObserver(method, None) logmgr = LogManager(mode="w") add_beam_quantities(logmgr, observer, axis=0, beam_axis=2) from pyrticle.distribution import KVPredictedRadius logmgr.add_quantity(KVPredictedRadius(dt, beam_v=beta*units.VACUUM_LIGHT_SPEED(), predictor=beam.get_rms_predictor(axis=0), suffix="x_rms")) logmgr.add_quantity(KVPredictedRadius(dt, beam_v=beta*units.VACUUM_LIGHT_SPEED(), predictor=beam.get_total_predictor(axis=0), suffix="x_total")) # timestep loop ----------------------------------------------------------- vel = method.velocities(state) from hedge.tools import join_fields def rhs(t, y): return join_fields([ vel, 0*vel, 0, # drecon ]) from hedge.timestep.runge_kutta import LSRK4TimeStepper stepper = LSRK4TimeStepper() t = 0 from pyrticle.cloud import TimesteppablePicState ts_state = TimesteppablePicState(method, state) for step in xrange(nsteps): observer.set_fields_and_state(None, ts_state.state) logmgr.tick() ts_state = stepper(ts_state, t, dt, rhs) method.upkeep(ts_state.state) t += dt logmgr.tick() _, _, err_table = logmgr.get_expr_dataset("(rx_rms-rx_rms_theory)/rx_rms_theory") rel_max_rms_error = max(err for step, err in err_table) assert rel_max_rms_error < 0.01