<jupyter_start><jupyter_text>Particle stepper================An example of PlasmaPy's particle stepper class, currently in need of a rewritefor speed.<jupyter_code>import numpy as np
from astropy import units as u
from plasmapy.classes import Plasma
from plasmapy.simulation import ParticleTracker
from plasmapy.formulary import gyrofrequency<jupyter_output><empty_output><jupyter_text>Initialize a plasma. This will be a source of electric and magneticfields for our particles to move in.<jupyter_code>plasma = Plasma(domain_x=np.linspace(-1, 1, 10) * u.m,
                domain_y=np.linspace(-1, 1, 10) * u.m,
                domain_z=np.linspace(-1, 1, 10) * u.m)<jupyter_output><empty_output><jupyter_text>Initialize the fields. We'll take B in the x directionand E in the y direction, which gets us an E cross B driftin the z direction.<jupyter_code>B0 = 4 * u.T
plasma.magnetic_field[0, :, :, :] = np.ones((10, 10, 10)) * B0

E0 = 2 * u.V / u.m
plasma.electric_field[1, :, :, :] = np.ones((10, 10, 10)) * E0<jupyter_output><empty_output><jupyter_text>Calculate the timestep. We'll take one proton `p`, take its gyrofrequency, invert thatto get to the gyroperiod, and resolve that into 10 steps for higher accuracy.<jupyter_code>freq = gyrofrequency(B0, 'p').to(u.Hz, equivalencies=u.dimensionless_angles())
gyroperiod = (1/freq).to(u.s)
steps_to_gyroperiod = 10
timestep = gyroperiod / steps_to_gyroperiod<jupyter_output><empty_output><jupyter_text>Initialize the trajectory calculation.<jupyter_code>number_steps = steps_to_gyroperiod * int(2 * np.pi)
trajectory = ParticleTracker(plasma, 'p', 1, 1, timestep, number_steps)<jupyter_output><empty_output><jupyter_text>We still have to initialize the particle's velocity. We'll limit ourselves toone in the x direction, parallel to the magnetic field B -that way, it won't turn in the z direction.<jupyter_code>trajectory.v[0][0] = 1 * (u.m / u.s)<jupyter_output><empty_output><jupyter_text>Run the pusher and plot the trajectory versus time.<jupyter_code>trajectory.run()
trajectory.plot_time_trajectories()<jupyter_output><empty_output><jupyter_text>Plot the shape of the trajectory in 3D.<jupyter_code>trajectory.plot_trajectories()<jupyter_output><empty_output><jupyter_text>As a test, we calculate the mean velocity in the z direction from thevelocity and position<jupyter_code>vmean = trajectory.velocity_history[:, :, 2].mean()
print(f"The calculated drift velocity is {vmean:.4f} to compare with the"
      f"theoretical E0/B0 = {E0/B0:.4f}")<jupyter_output><empty_output><jupyter_text>and from position:<jupyter_code>Vdrift = trajectory.position_history[-1, 0, 2] / (trajectory.NT * trajectory.dt)
print(f"The calculated drift velocity from position is {Vdrift:.4f}")<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.11549601737871108, 'fraction_numerical': 0.020275162925416364, 'mean_word_length': 6.1953125, 'pattern_counts': {'":': 0, '<': 41, '<?xml version=': 0, '>': 41, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '10', 'dir': 'jupyter-structured-clean-dedup', 'id': '656513', 'license': 'MIT', 'n_tokens_mistral': 1015, 'n_tokens_neox': 954, 'n_words': 248, 'path': 'docs/notebooks/particle_stepper.ipynb', 'repo_name': 'faryal-khan/PlasmaPy', 'size': '2762'}