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
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
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)
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)
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)), ])
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)
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)), ])