Ejemplo n.º 1
0
    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
Ejemplo n.º 2
0
    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
Ejemplo n.º 3
0
    def __add__(self, update):
        from hedge.tools import is_zero
        if is_zero(update):
            return self

        from pyrticle.tools import NumberShiftableVector

        #from pytools import typedump
        dx, dp, ddep = update
        dx = NumberShiftableVector.unwrap(dx)
        dp = NumberShiftableVector.unwrap(dp)

        new_state = self.method.advance_state(self.state, dx, dp, ddep)

        return TimesteppablePicState(self.method, new_state)
Ejemplo n.º 4
0
    def __add__(self, update):
        from hedge.tools import is_zero
        if is_zero(update):
            return self

        from pyrticle.tools import NumberShiftableVector

        #from pytools import typedump
        dx, dp, ddep = update
        dx = NumberShiftableVector.unwrap(dx)
        dp = NumberShiftableVector.unwrap(dp)

        new_state = self.method.advance_state(self.state, dx, dp, ddep)

        return TimesteppablePicState(self.method, new_state)
Ejemplo n.º 5
0
    def __add__(self, other):
        #if len(self.vector) != len(self.unwrap(other)):
        #print len(self.vector), len(self.unwrap(other)), type(other)
        #print self.signaller, other.signaller
        #print other in other.signaller.subscribers
        #print "---------------------------------------------"

        return NumberShiftableVector(self.vector + self.unwrap(other),
                                     self.signaller)
Ejemplo n.º 6
0
    def advance_state(self, state, rhs):
        # depositor states tend to linger on for a bit--ours here are
        # big, so they need to go.
        from gc import collect
        collect()

        from pyrticle.tools import NumberShiftableVector
        return self.backend.apply_advective_particle_rhs(
            state.depositor_state, state.particle_state,
            NumberShiftableVector.unwrap(rhs),
            state.depositor_state.rho_dof_shift_listener)
Ejemplo n.º 7
0
    def advance_state(self, state, rhs):
        # depositor states tend to linger on for a bit--ours here are
        # big, so they need to go.
        from gc import collect
        collect()

        from pyrticle.tools import NumberShiftableVector
        return self.backend.apply_advective_particle_rhs(
                state.depositor_state,
                state.particle_state,
                NumberShiftableVector.unwrap(rhs),
                state.depositor_state.rho_dof_shift_listener)
Ejemplo n.º 8
0
    def _add_to_silo(self, visualizer, db, state, time, step, beamaxis, vis_listener=None):
        from pylo import DBOPT_DTIME, DBOPT_CYCLE
        optlist = {}
        if time is not None:
            optlist[DBOPT_DTIME] = time
        if step is not None:
            optlist[DBOPT_CYCLE] = step

        pcount = len(state)

        if pcount:
            # real-space ------------------------------------------------------
            db.put_pointmesh("particles",
                    numpy.asarray(state.positions.T, order="C"), optlist)
            db.put_pointvar1("charge", "particles", state.charges)
            db.put_pointvar1("mass", "particles", state.masses)
            db.put_pointvar("momentum", "particles",
                    numpy.asarray(state.momenta.T, order="C"))
            db.put_pointvar("velocity", "particles",
                    numpy.asarray(self.velocities(state).T, order="C"))

            if vis_listener is not None:
                for name, value in self.vis_listener.particle_vis_map.iteritems():
                    from pyrticle.tools import NumberShiftableVector
                    value = NumberShiftableVector.unwrap(value)
                    dim, remainder = divmod(len(value), pcount)
                    assert remainder == 0, (
                            "particle vis value '%s' had invalid number of entries: "
                            "%d (#particles=%d)" % (name, len(value), pcount))
                    if dim == 1:
                        db.put_pointvar1(name, "particles", value)
                    else:
                        db.put_pointvar(name, "particles", [value[i::dim] for i in range(dim)])

            # phase-space -----------------------------------------------------
            axes_names = ["x", "y", "z"]

            if beamaxis is not None:
                for axis in range(min(self.dimensions_pos, self.dimensions_velocity)):
                    if axis == beamaxis:
                        continue

                    axname = axes_names[axis]

                    db.put_defvars("phasespace_%s" % axname,
                            [
                            ("part_%s" % axname, "coord(particles)[%d]" % axis),
                            ("part_%s_prime" % axname,
                                "momentum[%d]/momentum[%d]" % (axis, beamaxis)),
                            ("part_%s_momentum" % axname,
                                "momentum[%d]" % (axis)),
                            ])
Ejemplo n.º 9
0
    def __call__(self):
        vis_info = self.observer.state.vis_listener.particle_vis_map

        if self.vis_field not in vis_info:
            return None
        sd = vis_info[self.vis_field]
        if not sd:
            return None

        from pyrticle.tools import NumberShiftableVector
        sd = NumberShiftableVector.unwrap(sd)

        return sd.sum()/len(sd)
Ejemplo n.º 10
0
    def __call__(self, t, fields_f, state_f):
        state = state_f()

        velocities = self.method.velocities(state)

        from pyrticle.tools import NumberShiftableVector
        from hedge.tools import make_obj_array
        result = make_obj_array([
            NumberShiftableVector(
                velocities, signaller=state.particle_number_shift_signaller),
            0,
            self.method.depositor.rhs(state)
        ])
        return result
Ejemplo n.º 11
0
    def rhs(self, state):
        from pyrticle.tools import NumberShiftableVector
        sub_timer = self.advective_rhs_timer.start_sub_timer()
        result = NumberShiftableVector(
            self.backend.get_advective_particle_rhs(
                state.depositor_state, state.particle_state,
                self.method.velocities(state)),
            signaller=state.depositor_state.rho_dof_shift_listener)
        sub_timer.stop()
        self.element_activation_counter.transfer(
            state.depositor_state.element_activation_counter)
        self.element_kill_counter.transfer(
            state.depositor_state.element_kill_counter)

        return result
Ejemplo n.º 12
0
    def __call__(self, t, fields_f, state_f):
        from pyrticle._internal import ZeroVector

        fields = fields_f()
        state = state_f()

        e, h = self.maxwell_op.split_eh(fields)

        # assemble field_args of the form [ex,ey,ez] and [bx,by,bz],
        # inserting ZeroVectors where necessary.
        idx = 0
        e_arg = []
        for use_component in self.maxwell_op.get_eh_subset()[0:3]:
            if use_component:
                e_arg.append(e[idx])
                idx += 1
            else:
                e_arg.append(ZeroVector())

        idx = 0
        b_arg = []
        for use_component in self.maxwell_op.get_eh_subset()[3:6]:
            if use_component:
                b_arg.append(self.maxwell_op.mu * h[idx])
                idx += 1
            else:
                b_arg.append(ZeroVector())

        field_args = tuple(e_arg) + tuple(b_arg)

        velocities = self.method.velocities(state)

        # compute forces
        forces = self.method.pusher.forces(state, velocities, *field_args)

        from pyrticle.tools import NumberShiftableVector
        from hedge.tools import make_obj_array
        result = make_obj_array([
            0,
            NumberShiftableVector(
                forces, signaller=state.particle_number_shift_signaller), 0
        ])
        return result
Ejemplo n.º 13
0
    def _add_to_silo(self,
                     visualizer,
                     db,
                     state,
                     time,
                     step,
                     beamaxis,
                     vis_listener=None):
        from pylo import DBOPT_DTIME, DBOPT_CYCLE
        optlist = {}
        if time is not None:
            optlist[DBOPT_DTIME] = time
        if step is not None:
            optlist[DBOPT_CYCLE] = step

        pcount = len(state)

        if pcount:
            # real-space ------------------------------------------------------
            db.put_pointmesh("particles",
                             numpy.asarray(state.positions.T, order="C"),
                             optlist)
            db.put_pointvar1("charge", "particles", state.charges)
            db.put_pointvar1("mass", "particles", state.masses)
            db.put_pointvar("momentum", "particles",
                            numpy.asarray(state.momenta.T, order="C"))
            db.put_pointvar("velocity", "particles",
                            numpy.asarray(self.velocities(state).T, order="C"))

            if vis_listener is not None:
                for name, value in self.vis_listener.particle_vis_map.iteritems(
                ):
                    from pyrticle.tools import NumberShiftableVector
                    value = NumberShiftableVector.unwrap(value)
                    dim, remainder = divmod(len(value), pcount)
                    assert remainder == 0, (
                        "particle vis value '%s' had invalid number of entries: "
                        "%d (#particles=%d)" % (name, len(value), pcount))
                    if dim == 1:
                        db.put_pointvar1(name, "particles", value)
                    else:
                        db.put_pointvar(name, "particles",
                                        [value[i::dim] for i in range(dim)])

            # phase-space -----------------------------------------------------
            axes_names = ["x", "y", "z"]

            if beamaxis is not None:
                for axis in range(
                        min(self.dimensions_pos, self.dimensions_velocity)):
                    if axis == beamaxis:
                        continue

                    axname = axes_names[axis]

                    db.put_defvars("phasespace_%s" % axname, [
                        ("part_%s" % axname, "coord(particles)[%d]" % axis),
                        ("part_%s_prime" % axname,
                         "momentum[%d]/momentum[%d]" % (axis, beamaxis)),
                        ("part_%s_momentum" % axname, "momentum[%d]" % (axis)),
                    ])
Ejemplo n.º 14
0
 def store_particle_vis_vector(self, name, vec):
     from pyrticle.tools import NumberShiftableVector
     self.particle_vis_map[name] = NumberShiftableVector(
         vec, signaller=self.particle_number_shift_signaller)
Ejemplo n.º 15
0
 def __mul__(self, other):
     result = NumberShiftableVector(self.vector * self.unwrap(other),
                                    self.signaller)
     return result