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_efield_vs_gauss_law(): from hedge.mesh.generator import \ make_box_mesh, \ make_cylinder_mesh from math import sqrt, pi from pytools.arithmetic_container import \ ArithmeticList, join_fields from random import seed from pytools.stopwatch import Job from pyrticle.units import SIUnitsWithNaturalConstants units = SIUnitsWithNaturalConstants() seed(0) nparticles = 10000 beam_radius = 2.5 * units.MM emittance = 5 * units.MM * units.MRAD final_time = 0.1 * units.M / units.VACUUM_LIGHT_SPEED() field_dump_interval = 1 tube_length = 20 * units.MM # discretization setup ---------------------------------------------------- from pyrticle.geometry import make_cylinder_with_fine_core mesh = make_cylinder_with_fine_core( r=10 * beam_radius, inner_r=1 * beam_radius, min_z=0, max_z=tube_length, max_volume_inner=10 * units.MM**3, max_volume_outer=100 * units.MM**3, radial_subdiv=10, ) from hedge.backends import guess_run_context rcon = guess_run_context([]) discr = rcon.make_discretization(mesh, order=3) from hedge.models.em import MaxwellOperator max_op = MaxwellOperator(epsilon=units.EPSILON0, mu=units.MU0, flux_type=1) from hedge.models.nd_calculus import DivergenceOperator div_op = DivergenceOperator(discr.dimensions) # 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) # particle ic --------------------------------------------------------- cloud_charge = -1e-9 * units.C electrons_per_particle = abs(cloud_charge / nparticles / units.EL_CHARGE) el_energy = 10 * units.EL_REST_ENERGY() el_lorentz_gamma = el_energy / units.EL_REST_ENERGY() beta = (1 - 1 / el_lorentz_gamma**2)**0.5 gamma = 1 / sqrt(1 - beta**2) from pyrticle.distribution import KVZIntervalBeam beam = KVZIntervalBeam(units, total_charge=cloud_charge, p_charge=cloud_charge / nparticles, p_mass=electrons_per_particle * units.EL_MASS, radii=2 * [beam_radius], emittances=2 * [5 * units.MM * units.MRAD], z_length=tube_length, z_pos=tube_length / 2, beta=beta) state = method.make_state() method.add_particles(state, beam.generate_particles(), nparticles) # field ic ---------------------------------------------------------------- from pyrticle.cloud import guess_shape_bandwidth guess_shape_bandwidth(method, state, 2) from pyrticle.cloud import compute_initial_condition from hedge.data import ConstantGivenFunction fields = compute_initial_condition(rcon, discr, method, state, maxwell_op=max_op, potential_bc=ConstantGivenFunction()) # check against theory ---------------------------------------------------- q_per_unit_z = cloud_charge / beam.z_length class TheoreticalEField: shape = (3, ) def __call__(self, x, el): r = la.norm(x[:2]) if r >= max(beam.radii): xy_unit = x / r xy_unit[2] = 0 return xy_unit * ((q_per_unit_z) / (2 * pi * r * max_op.epsilon)) else: return numpy.zeros((3, )) def theory_indicator(x, el): r = la.norm(x[:2]) if r >= max(beam.radii): return 1 else: return 0 from hedge.tools import join_fields, to_obj_array e_theory = to_obj_array( discr.interpolate_volume_function(TheoreticalEField())) theory_ind = discr.interpolate_volume_function(theory_indicator) e_field, h_field = max_op.split_eh(fields) restricted_e = join_fields(*[e_i * theory_ind for e_i in e_field]) def l2_error(field, true): return discr.norm(field - true) / discr.norm(true) outer_l2 = l2_error(restricted_e, e_theory) assert outer_l2 < 0.08 if False: visf = vis.make_file("e_comparison") mesh_scalars, mesh_vectors = \ method.add_to_vis(vis, visf) vis.add_data(visf, [ ("e", restricted_e), ("e_theory", e_theory), ] + mesh_vectors + mesh_scalars) visf.close()
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_efield_vs_gauss_law(): from hedge.mesh.generator import \ make_box_mesh, \ make_cylinder_mesh from math import sqrt, pi from pytools.arithmetic_container import \ ArithmeticList, join_fields from random import seed from pytools.stopwatch import Job from pyrticle.units import SIUnitsWithNaturalConstants units = SIUnitsWithNaturalConstants() seed(0) nparticles = 10000 beam_radius = 2.5 * units.MM emittance = 5 * units.MM * units.MRAD final_time = 0.1*units.M/units.VACUUM_LIGHT_SPEED() field_dump_interval = 1 tube_length = 20*units.MM # discretization setup ---------------------------------------------------- from pyrticle.geometry import make_cylinder_with_fine_core mesh = make_cylinder_with_fine_core( r=10*beam_radius, inner_r=1*beam_radius, min_z=0, max_z=tube_length, max_volume_inner=10*units.MM**3, max_volume_outer=100*units.MM**3, radial_subdiv=10, ) from hedge.backends import guess_run_context rcon = guess_run_context([]) discr = rcon.make_discretization(mesh, order=3) from hedge.models.em import MaxwellOperator max_op = MaxwellOperator( epsilon=units.EPSILON0, mu=units.MU0, flux_type=1) from hedge.models.nd_calculus import DivergenceOperator div_op = DivergenceOperator(discr.dimensions) # 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) # particle ic --------------------------------------------------------- cloud_charge = -1e-9 * units.C electrons_per_particle = abs(cloud_charge/nparticles/units.EL_CHARGE) el_energy = 10*units.EL_REST_ENERGY() el_lorentz_gamma = el_energy/units.EL_REST_ENERGY() beta = (1-1/el_lorentz_gamma**2)**0.5 gamma = 1/sqrt(1-beta**2) from pyrticle.distribution import KVZIntervalBeam beam = KVZIntervalBeam(units, total_charge=cloud_charge, p_charge=cloud_charge/nparticles, p_mass=electrons_per_particle*units.EL_MASS, radii=2*[beam_radius], emittances=2*[5 * units.MM * units.MRAD], z_length=tube_length, z_pos=tube_length/2, beta=beta) state = method.make_state() method.add_particles(state, beam.generate_particles(), nparticles) # field ic ---------------------------------------------------------------- from pyrticle.cloud import guess_shape_bandwidth guess_shape_bandwidth(method, state, 2) from pyrticle.cloud import compute_initial_condition from hedge.data import ConstantGivenFunction fields = compute_initial_condition( rcon, discr, method, state, maxwell_op=max_op, potential_bc=ConstantGivenFunction()) # check against theory ---------------------------------------------------- q_per_unit_z = cloud_charge/beam.z_length class TheoreticalEField: shape = (3,) def __call__(self, x, el): r = la.norm(x[:2]) if r >= max(beam.radii): xy_unit = x/r xy_unit[2] = 0 return xy_unit*((q_per_unit_z) / (2*pi*r*max_op.epsilon)) else: return numpy.zeros((3,)) def theory_indicator(x, el): r = la.norm(x[:2]) if r >= max(beam.radii): return 1 else: return 0 from hedge.tools import join_fields, to_obj_array e_theory = to_obj_array(discr.interpolate_volume_function(TheoreticalEField())) theory_ind = discr.interpolate_volume_function(theory_indicator) e_field, h_field = max_op.split_eh(fields) restricted_e = join_fields(*[e_i * theory_ind for e_i in e_field]) def l2_error(field, true): return discr.norm(field-true)/discr.norm(true) outer_l2 = l2_error(restricted_e, e_theory) assert outer_l2 < 0.08 if False: visf = vis.make_file("e_comparison") mesh_scalars, mesh_vectors = \ method.add_to_vis(vis, visf) vis.add_data(visf, [ ("e", restricted_e), ("e_theory", e_theory), ] + mesh_vectors + mesh_scalars ) visf.close()
def run_setup(units, casename, setup, discr, pusher, visualize=False): from hedge.timestep.runge_kutta import LSRK4TimeStepper from hedge.visualization import SiloVisualizer from hedge.models.em import MaxwellOperator vis = SiloVisualizer(discr) from pyrticle.cloud import PicMethod from pyrticle.deposition.shape import ShapeFunctionDepositor method = PicMethod(discr, units, ShapeFunctionDepositor(), pusher(), 3, 3, debug=set(["verbose_vis"])) e, h = setup.fields(discr) b = units.MU0 * h init_positions = setup.positions(0) init_velocities = setup.velocities(0) nparticles = len(init_positions) state = method.make_state() method.add_particles( state, zip( init_positions, init_velocities, nparticles * [setup.charge], nparticles * [units.EL_MASS], ), nparticles) final_time = setup.final_time() nsteps = setup.nsteps() dt = final_time / nsteps # timestepping ------------------------------------------------------------ from hedge.models.em import MaxwellOperator max_op = MaxwellOperator(epsilon=units.EPSILON0, mu=units.MU0, flux_type=1) fields = max_op.assemble_eh(e, h) from pyrticle.cloud import \ FieldToParticleRhsCalculator, \ ParticleRhsCalculator p_rhs_calculator = ParticleRhsCalculator(method, max_op) f2p_rhs_calculator = FieldToParticleRhsCalculator(method, max_op) def rhs(t, ts_state): return (p_rhs_calculator(t, lambda: fields, lambda: ts_state.state) + f2p_rhs_calculator(t, lambda: fields, lambda: ts_state.state)) stepper = LSRK4TimeStepper() t = 0 bbox = discr.mesh.bounding_box() z_period = bbox[1][2] - bbox[0][2] def check_result(): from hedge.tools import cross deriv_dt = 1e-12 dim = discr.dimensions true_x = setup.positions(t) true_v = setup.velocities(t) true_f = [(p2 - p1) / (2 * deriv_dt) for p1, p2 in zip( setup.momenta(t - deriv_dt), setup.momenta(t + deriv_dt))] state = ts_state.state from pyrticle.tools import NumberShiftableVector vis_info = state.vis_listener.particle_vis_map sim_x = state.positions sim_v = method.velocities(state) sim_f = NumberShiftableVector.unwrap(vis_info["mag_force"] + vis_info["el_force"]) sim_el_f = NumberShiftableVector.unwrap(vis_info["el_force"]) sim_mag_f = NumberShiftableVector.unwrap(vis_info["mag_force"]) local_e = setup.e() local_b = units.MU0 * setup.h() x_err = 0 v_err = 0 f_err = 0 for i in range(len(state)): #real_f = numpy.array(cross(sim_v, setup.charge*local_b)) + setup.charge*local_e my_true_x = true_x[i] my_true_x[2] = my_true_x[2] % z_period if False and i == 0: #print "particle %d" % i print "pos:", la.norm(true_x[i] - sim_x[i]) / la.norm( true_x[i]) #print "vel:", la.norm(true_v[i]-sim_v[i])/la.norm(true_v[i]) #print "force:", la.norm(true_f[i]-sim_f[i])/la.norm(true_f[i]) print "pos:", true_x[i], sim_x[i] #print "vel:", true_v[i], sim_v[i] #print "force:", true_f[i], sim_f[i] #print "forces%d:..." % i, sim_el_f[i], sim_mag_f[i] #print "acc%d:" % i, la.norm(a-sim_a) #u = numpy.vstack((v, sim_v, f, sim_f, real_f)) #print "acc%d:\n%s" % (i, u) #raw_input() def rel_err(sim, true): return la.norm(true - sim) / la.norm(true) x_err = max(x_err, rel_err(sim_x[i], my_true_x)) v_err = max(v_err, rel_err(sim_v[i], true_v[i])) f_err = max(f_err, rel_err(sim_f[i], true_f[i])) return x_err, v_err, f_err # make sure verbose-vis fields are filled from pyrticle.cloud import TimesteppablePicState ts_state = TimesteppablePicState(method, state) del state rhs(t, ts_state) errors = (0, 0, 0) for step in xrange(nsteps): if step % int(setup.nsteps() / 300) == 0: errors = tuple( max(old_err, new_err) for old_err, new_err in zip(errors, check_result())) if visualize: visf = vis.make_file("%s-%04d" % (casename, step)) method.add_to_vis(vis, visf, ts_state.state, time=t, step=step) if True: vis.add_data(visf, [ ("e", e), ("h", h), ], time=t, step=step) else: vis.add_data(visf, [], time=t, step=step) visf.close() method.upkeep(ts_state.state) ts_state = stepper(ts_state, t, dt, rhs) t += dt assert errors[0] < 2e-12, casename + "-pos" assert errors[1] < 2e-13, casename + "-vel" assert errors[2] < 2e-4, casename + "-acc" vis.close()
def run_setup(units, casename, setup, discr, pusher, visualize=False): from hedge.timestep.runge_kutta import LSRK4TimeStepper from hedge.visualization import SiloVisualizer from hedge.models.em import MaxwellOperator vis = SiloVisualizer(discr) from pyrticle.cloud import PicMethod from pyrticle.deposition.shape import ShapeFunctionDepositor method = PicMethod(discr, units, ShapeFunctionDepositor(), pusher(), 3, 3, debug=set(["verbose_vis"])) e, h = setup.fields(discr) b = units.MU0 * h init_positions = setup.positions(0) init_velocities = setup.velocities(0) nparticles = len(init_positions) state = method.make_state() method.add_particles(state, zip(init_positions, init_velocities, nparticles * [setup.charge], nparticles * [units.EL_MASS], ), nparticles) final_time = setup.final_time() nsteps = setup.nsteps() dt = final_time/nsteps # timestepping ------------------------------------------------------------ from hedge.models.em import MaxwellOperator max_op = MaxwellOperator( epsilon=units.EPSILON0, mu=units.MU0, flux_type=1) fields = max_op.assemble_eh(e, h) from pyrticle.cloud import \ FieldToParticleRhsCalculator, \ ParticleRhsCalculator p_rhs_calculator = ParticleRhsCalculator(method, max_op) f2p_rhs_calculator = FieldToParticleRhsCalculator(method, max_op) def rhs(t, ts_state): return (p_rhs_calculator(t, lambda: fields, lambda: ts_state.state) + f2p_rhs_calculator(t, lambda: fields, lambda: ts_state.state)) stepper = LSRK4TimeStepper() t = 0 bbox = discr.mesh.bounding_box() z_period = bbox[1][2] - bbox[0][2] def check_result(): from hedge.tools import cross deriv_dt = 1e-12 dim = discr.dimensions true_x = setup.positions(t) true_v = setup.velocities(t) true_f = [(p2-p1)/(2*deriv_dt) for p1, p2 in zip(setup.momenta(t-deriv_dt), setup.momenta(t+deriv_dt))] state = ts_state.state from pyrticle.tools import NumberShiftableVector vis_info = state.vis_listener.particle_vis_map sim_x = state.positions sim_v = method.velocities(state) sim_f = NumberShiftableVector.unwrap( vis_info["mag_force"] + vis_info["el_force"]) sim_el_f = NumberShiftableVector.unwrap(vis_info["el_force"]) sim_mag_f = NumberShiftableVector.unwrap(vis_info["mag_force"]) local_e = setup.e() local_b = units.MU0 * setup.h() x_err = 0 v_err = 0 f_err = 0 for i in range(len(state)): #real_f = numpy.array(cross(sim_v, setup.charge*local_b)) + setup.charge*local_e my_true_x = true_x[i] my_true_x[2] = my_true_x[2] % z_period if False and i == 0: #print "particle %d" % i print "pos:", la.norm(true_x[i]-sim_x[i])/la.norm(true_x[i]) #print "vel:", la.norm(true_v[i]-sim_v[i])/la.norm(true_v[i]) #print "force:", la.norm(true_f[i]-sim_f[i])/la.norm(true_f[i]) print "pos:", true_x[i], sim_x[i] #print "vel:", true_v[i], sim_v[i] #print "force:", true_f[i], sim_f[i] #print "forces%d:..." % i, sim_el_f[i], sim_mag_f[i] #print "acc%d:" % i, la.norm(a-sim_a) #u = numpy.vstack((v, sim_v, f, sim_f, real_f)) #print "acc%d:\n%s" % (i, u) #raw_input() def rel_err(sim, true): return la.norm(true-sim)/la.norm(true) x_err = max(x_err, rel_err(sim_x[i], my_true_x)) v_err = max(v_err, rel_err(sim_v[i], true_v[i])) f_err = max(f_err, rel_err(sim_f[i], true_f[i])) return x_err, v_err, f_err # make sure verbose-vis fields are filled from pyrticle.cloud import TimesteppablePicState ts_state = TimesteppablePicState(method, state) del state rhs(t, ts_state) errors = (0, 0, 0) for step in xrange(nsteps): if step % int(setup.nsteps()/300) == 0: errors = tuple( max(old_err, new_err) for old_err, new_err in zip(errors, check_result())) if visualize: visf = vis.make_file("%s-%04d" % (casename, step)) method.add_to_vis(vis, visf, ts_state.state, time=t, step=step) if True: vis.add_data(visf, [ ("e", e), ("h", h), ], time=t, step=step) else: vis.add_data(visf, [], time=t, step=step) visf.close() method.upkeep(ts_state.state) ts_state = stepper(ts_state, t, dt, rhs) t += dt assert errors[0] < 2e-12, casename+"-pos" assert errors[1] < 2e-13, casename+"-vel" assert errors[2] < 2e-4, casename+"-acc" vis.close()