Example #1
0
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
Example #2
0
    def add_instrumentation(self, logmgr):
        from pytools.log import \
                add_simulation_quantities, \
                add_general_quantities, \
                add_run_info, ETA
        from pyrticle.log import add_particle_quantities, add_field_quantities, \
                add_beam_quantities, add_currents

        setup = self.setup

        from pyrticle.log import StateObserver
        self.observer = StateObserver(self.method, self.maxwell_op)
        self.observer.set_fields_and_state(self.fields, self.state)

        add_run_info(logmgr)
        add_general_quantities(logmgr)
        add_simulation_quantities(logmgr)
        add_particle_quantities(logmgr, self.observer)
        add_field_quantities(logmgr, self.observer)

        if setup.beam_axis is not None and setup.beam_diag_axis is not None:
            add_beam_quantities(logmgr, self.observer, 
                    axis=setup.beam_diag_axis, 
                    beam_axis=setup.beam_axis)

        if setup.tube_length is not None:
            from hedge.tools import unit_vector
            add_currents(logmgr, self.observer, 
                    unit_vector(self.method.dimensions_velocity, setup.beam_axis), 
                    setup.tube_length)

        self.method.add_instrumentation(logmgr, self.observer)

        self.f_rhs_calculator.add_instrumentation(logmgr)

        if hasattr(self.stepper, "add_instrumentation"):
            self.stepper.add_instrumentation(logmgr)

        mean_beta = self.method.mean_beta(self.state)
        gamma = self.method.units.gamma_from_beta(mean_beta)

        logmgr.set_constant("dt", self.dt)
        logmgr.set_constant("beta", mean_beta)
        logmgr.set_constant("gamma", gamma)
        logmgr.set_constant("v", mean_beta*self.units.VACUUM_LIGHT_SPEED())
        logmgr.set_constant("Q0", self.total_charge)
        logmgr.set_constant("n_part_0", setup.nparticles)
        logmgr.set_constant("pmass", setup.distribution.mean()[3][0])
        logmgr.set_constant("chi", setup.chi)
        logmgr.set_constant("phi_decay", setup.phi_decay)
        logmgr.set_constant("shape_radius_setup", setup.shape_bandwidth)
        logmgr.set_constant("shape_radius", self.method.depositor.shape_function.radius)
        logmgr.set_constant("shape_exponent", self.method.depositor.shape_function.exponent)

        from pytools.log import IntervalTimer
        self.vis_timer = IntervalTimer("t_vis", "Time spent visualizing")
        logmgr.add_quantity(self.vis_timer)

        logmgr.add_quantity(ETA(self.nsteps))

        logmgr.add_watches(setup.watch_vars)
Example #3
0
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
Example #4
0
    def add_instrumentation(self, logmgr):
        from pytools.log import \
                add_simulation_quantities, \
                add_general_quantities, \
                add_run_info, ETA
        from pyrticle.log import add_particle_quantities, add_field_quantities, \
                add_beam_quantities, add_currents

        setup = self.setup

        from pyrticle.log import StateObserver
        self.observer = StateObserver(self.method, self.maxwell_op)
        self.observer.set_fields_and_state(self.fields, self.state)

        add_run_info(logmgr)
        add_general_quantities(logmgr)
        add_simulation_quantities(logmgr)
        add_particle_quantities(logmgr, self.observer)
        add_field_quantities(logmgr, self.observer)

        if setup.beam_axis is not None and setup.beam_diag_axis is not None:
            add_beam_quantities(logmgr,
                                self.observer,
                                axis=setup.beam_diag_axis,
                                beam_axis=setup.beam_axis)

        if setup.tube_length is not None:
            from hedge.tools import unit_vector
            add_currents(
                logmgr, self.observer,
                unit_vector(self.method.dimensions_velocity, setup.beam_axis),
                setup.tube_length)

        self.method.add_instrumentation(logmgr, self.observer)

        self.f_rhs_calculator.add_instrumentation(logmgr)

        if hasattr(self.stepper, "add_instrumentation"):
            self.stepper.add_instrumentation(logmgr)

        mean_beta = self.method.mean_beta(self.state)
        gamma = self.method.units.gamma_from_beta(mean_beta)

        logmgr.set_constant("dt", self.dt)
        logmgr.set_constant("beta", mean_beta)
        logmgr.set_constant("gamma", gamma)
        logmgr.set_constant("v", mean_beta * self.units.VACUUM_LIGHT_SPEED())
        logmgr.set_constant("Q0", self.total_charge)
        logmgr.set_constant("n_part_0", setup.nparticles)
        logmgr.set_constant("pmass", setup.distribution.mean()[3][0])
        logmgr.set_constant("chi", setup.chi)
        logmgr.set_constant("phi_decay", setup.phi_decay)
        logmgr.set_constant("shape_radius_setup", setup.shape_bandwidth)
        logmgr.set_constant("shape_radius",
                            self.method.depositor.shape_function.radius)
        logmgr.set_constant("shape_exponent",
                            self.method.depositor.shape_function.exponent)

        from pytools.log import IntervalTimer
        self.vis_timer = IntervalTimer("t_vis", "Time spent visualizing")
        logmgr.add_quantity(self.vis_timer)

        logmgr.add_quantity(ETA(self.nsteps))

        logmgr.add_watches(setup.watch_vars)