예제 #1
0
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()
예제 #2
0
 def get_true_field():
     return discr.convert_volume(
         to_obj_array(mode(discr).real.astype(discr.default_scalar_type).copy()), kind=discr.compute_kind
     )
예제 #3
0
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()
예제 #4
0
 def get_true_field():
     return discr.convert_volume(to_obj_array(
         mode(discr).real.astype(discr.default_scalar_type).copy()),
                                 kind=discr.compute_kind)