def test_monopole_fluxpoints(self): """Tests monopole flux points.""" field = ElectricField([PointCharge(2, [0, 0])]) circle = GaussianCircle([0, 0], 10) fluxpoints = circle.fluxpoints(field, 4) self.assertEqual(len(fluxpoints), 4) self.assertTrue( isclose(fluxpoints, [[10, 0], [0, 10], [-10, 0], [0, -10]]).all()) fluxpoints = circle.fluxpoints(field, 14) self.assertEqual(len(fluxpoints), 14) self.assertTrue(isclose(fluxpoints[0], [10, 0]).all()) self.assertTrue(isclose(fluxpoints[7], [-10, 0]).all()) x1 = fluxpoints[1:7] x2 = fluxpoints[-1:7:-1] x2[:, 1] = fabs(x2[:, 1]) self.assertTrue(isclose(x1, x2).all()) x1 = append(fluxpoints[-3:], fluxpoints[:4], axis=0) x2 = fluxpoints[-4:3:-1] x2[:, 0] = fabs(x2[:, 0]) self.assertEqual(len(x1), len(x2)) self.assertTrue(isclose(x1, x2).all())
def test_dipole_fluxpoints(self): """Tests dipole flux points.""" field = ElectricField( [PointCharge(-2, [0, 0]), PointCharge(2, [2, 0])]) circle = GaussianCircle([0, 0], 0.1) fluxpoints = circle.fluxpoints(field, 4) self.assertEqual(len(fluxpoints), 4) fluxpoints = circle.fluxpoints(field, 14) self.assertEqual(len(fluxpoints), 14) self.assertTrue(isclose(fluxpoints[0], [0.1, 0]).all()) self.assertTrue(isclose(fluxpoints[7], [-0.1, 0]).all()) x1 = fluxpoints[1:7] x2 = fluxpoints[-1:7:-1] x2[:, 1] = fabs(x2[:, 1]) self.assertTrue(isclose(x1, x2).all())
from electrostatics import GaussianCircle, ElectricField from electrostatics import finalize_plot # pylint: disable=invalid-name XMIN, XMAX = -40, 40 YMIN, YMAX = -30, 30 ZOOM = 6 XOFFSET = 0 electrostatics.init(XMIN, XMAX, YMIN, YMAX, ZOOM, XOFFSET) # Set up the charges and electric field a = 2 charges = [LineCharge(1, [0, -a], [0, a])] field = ElectricField(charges) # Set up the Gaussian surface g = GaussianCircle([0, 0], 29) # Create the field lines fieldlines = [] for x in g.fluxpoints(field, 12): fieldlines.append(field.line(x)) # Plotting pyplot.figure(figsize=(6, 4.5)) field.plot() for fieldline in fieldlines: fieldline.plot() for charge in charges:
def draw_E_and_V(charges, locations, background=False, circlesize=True, lines=True): XMIN, XMAX = -40, 40 YMIN, YMAX = -40, 40 ZOOM = 5 XOFFSET = 0 electrostatics.init(XMIN, XMAX, YMIN, YMAX, ZOOM, XOFFSET) # Set up the charges, electric field, and potential charges = [ PointCharge(charges[i] * 1e9, locations[i][:2]) for i in range(len(charges)) ] field = ElectricField(charges) potential = Potential(charges) # Set up the Gaussian surface fieldlines = [] for charge in charges: g = GaussianCircle(charge.x, 0.1) # Create the field lines for x in g.fluxpoints(field, 12): fieldlines.append(field.line(x)) fieldlines.append(field.line([10, 0])) # Create the vector grid x, y = numpy.meshgrid( numpy.linspace(XMIN / ZOOM + XOFFSET, XMAX / ZOOM + XOFFSET, 41), numpy.linspace(YMIN / ZOOM, YMAX / ZOOM, 31)) u, v = numpy.zeros_like(x), numpy.zeros_like(y) n, m = x.shape for i in range(n): for j in range(m): if any( numpy.isclose( electrostatics.norm(charge.x - [x[i, j], y[i, j]]), 0) for charge in charges): u[i, j] = v[i, j] = None else: mag = field.magnitude([x[i, j], y[i, j]])**(1 / 5) a = field.angle([x[i, j], y[i, j]]) u[i, j], v[i, j] = mag * numpy.cos(a), mag * numpy.sin(a) ## Plotting ## # Electric field lines and potential contours fig = pyplot.figure(figsize=(20, 9)) pyplot.subplot(1, 2, 1) #fig = pyplot.figure(figsize=(8, 6)) potential.plot() potential.plot_color() if background: field.plot() if lines: for fieldline in fieldlines: fieldline.plot() for charge in charges: charge.plot(circlesize) finalize_plot() #fig.savefig('dipole-field-lines.pdf', transparent=True) # Field vectors pyplot.subplot(1, 2, 2) #fig = pyplot.figure(figsize=(8,6)) cmap = pyplot.cm.get_cmap('hsv') pyplot.quiver(x, y, u, v, pivot='mid', cmap=cmap, scale=35) for charge in charges: charge.plot() finalize_plot() #fig.savefig('dipole-field-vectors.pdf', transparent=True) pyplot.show()
from electrostatics import finalize_plot # pylint: disable=invalid-name XMIN, XMAX = -40, 40 YMIN, YMAX = -30, 30 ZOOM = 6 XOFFSET = 0 electrostatics.init(XMIN, XMAX, YMIN, YMAX, ZOOM, XOFFSET) # Set up the charges and electric field. The point with charge 0 is a # termination point (0 electric field). charges = [PointChargeFlatland(1, [-1, 0]), PointChargeFlatland(-1, [1, 0])] field = ElectricField(charges) # Set up the Gaussian surface g = GaussianCircle(charges[0].x, 0.1) # Create the field lines fieldlines = [] for x in g.fluxpoints(field, 12): fieldlines.append(field.line(x)) fieldlines.append(field.line([3, 0])) fieldlines.append(field.line([5, 0])) # Plotting fig = pyplot.figure(figsize=(6, 4.5)) field.plot(-1.7, 0.8) for fieldline in fieldlines:
XMIN, XMAX = -200, 200 YMIN, YMAX = -150, 150 ZOOM = 33 XOFFSET = 0 electrostatics.init(XMIN, XMAX, YMIN, YMAX, ZOOM, XOFFSET) # Set up the charges and electric field charges = [ PointCharge(1, [-2, 0]), PointCharge(1, [2, 0]), PointCharge(-1, [0, -2]), PointCharge(-1, [0, 2]), PointCharge(0, [0, 0]) ] field = ElectricField(charges) potential = Potential(charges) # Set up the Gaussian surfaces g = [GaussianCircle(charges[i].x, 0.1) for i in range(len(charges))] g[2].a0 = radians(90) g[3].a0 = radians(-90) # Create the field lines fieldlines = [] for g_ in g[2:4]: for x in g_.fluxpoints(field, 12): fieldlines.append(field.line(x)) fieldlines.append(field.line([-1, 0])) fieldlines.append(field.line([1, 0])) fieldlines.append(field.line([-3, 0]))
from electrostatics import finalize_plot # pylint: disable=invalid-name XMIN, XMAX = -40, 40 YMIN, YMAX = -30, 30 ZOOM = 6 XOFFSET = 0 electrostatics.init(XMIN, XMAX, YMIN, YMAX, ZOOM, XOFFSET) # Set up the charges and electric field charges = [PointCharge(1, [-2, 0]), PointCharge(1, [2, 0]), PointCharge(0, [0, 0])] field = ElectricField(charges) # Set up the Gaussian surfaces g = [GaussianCircle(charges[i].x, 0.1) for i in range(len(charges))] g[0].a0 = radians(-180) # Create the field lines fieldlines = [] for g_ in g[:-1]: for x in g_.fluxpoints(field, 12): fieldlines.append(field.line(x)) # Plotting pyplot.figure(figsize=(6, 4.5)) field.plot() for fieldline in fieldlines:
from electrostatics import PointChargeFlatland, ElectricField, GaussianCircle from electrostatics import finalize_plot # pylint: disable=invalid-name XMIN, XMAX = -40, 40 YMIN, YMAX = -30, 30 ZOOM = 6 XOFFSET = 0 electrostatics.init(XMIN, XMAX, YMIN, YMAX, ZOOM, XOFFSET) # Set up the charges and electric field. The point with charge 0 is a # termination point (0 electric field). charges = [PointChargeFlatland(1, [-1, 0]), PointChargeFlatland(-1, [1, 0])] field = ElectricField(charges) # Set up the Gaussian surface g = GaussianCircle(charges[0].x, 0.1) # Create the field lines fieldlines = [] for x in g.fluxpoints(field, 12): fieldlines.append(field.line(x)) fieldlines.append(field.line([3, 0])) fieldlines.append(field.line([5, 0])) # Plotting fig = pyplot.figure(figsize=(6, 4.5)) field.plot(-1.7, 0.8) for fieldline in fieldlines:
import electrostatics from electrostatics import PointCharge, ElectricField, GaussianCircle from electrostatics import finalize_plot # pylint: disable=invalid-name XMIN, XMAX = -40, 40 YMIN, YMAX = -30, 30 ZOOM = 6 XOFFSET = 0 electrostatics.init(XMIN, XMAX, YMIN, YMAX, ZOOM, XOFFSET) # Set up the charges and electric field charges = [PointCharge(1, [-1, 0]), PointCharge(-1, [1, 0])] field = ElectricField(charges) # Set up the Gaussian surface g = GaussianCircle(charges[0].x, 0.1) # Create the field lines fieldlines = [] for x in g.fluxpoints(field, 12): fieldlines.append(field.line(x)) fieldlines.append(field.line([10, 0])) # Create the vector grid x, y = numpy.meshgrid( numpy.linspace(XMIN / ZOOM + XOFFSET, XMAX / ZOOM + XOFFSET, 41), numpy.linspace(YMIN / ZOOM, YMAX / ZOOM, 31)) u, v = numpy.zeros_like(x), numpy.zeros_like(y)
from electrostatics import PointCharge, LineCharge from electrostatics import ElectricField, GaussianCircle from electrostatics import finalize_plot # pylint: disable=invalid-name XMIN, XMAX = -80, 80 YMIN, YMAX = -60, 60 ZOOM = 15 XOFFSET = 0 electrostatics.init(XMIN, XMAX, YMIN, YMAX, ZOOM, XOFFSET) # Set up the charges and electric field charges = [LineCharge(1, [-1, -2], [-1, 2]), PointCharge(-1, [1, 0])] field = ElectricField(charges) # Set up the Gaussian surfaces g = GaussianCircle(charges[1].x, 0.1) # Create the field lines fieldlines = [] for x in g.fluxpoints(field, 12): fieldlines.append(field.line(x)) fieldlines.append(field.line([-10, 0])) # Plotting pyplot.figure(figsize=(6, 4.5)) field.plot() for fieldline in fieldlines: fieldline.plot()
from electrostatics import PointCharge, ElectricField, GaussianCircle from electrostatics import finalize_plot # pylint: disable=invalid-name XMIN, XMAX = -40, 40 YMIN, YMAX = -30, 30 ZOOM = 6 XOFFSET = 0 electrostatics.init(XMIN, XMAX, YMIN, YMAX, ZOOM, XOFFSET) # Set up the charges and electric field charges = [PointCharge(1, [-1, 0]), PointCharge(-1, [1, 0])] field = ElectricField(charges) # Set up the Gaussian surface g = GaussianCircle(charges[0].x, 0.1) # Create the field lines fieldlines = [] for x in g.fluxpoints(field, 12): fieldlines.append(field.line(x)) fieldlines.append(field.line([10, 0])) # Create the vector grid x, y = numpy.meshgrid(numpy.linspace(XMIN/ZOOM+XOFFSET, XMAX/ZOOM+XOFFSET, 41), numpy.linspace(YMIN/ZOOM, YMAX/ZOOM, 31)) u, v = numpy.zeros_like(x), numpy.zeros_like(y) n, m = x.shape
def setUp(self): """Sets up a dipole centred at (0, 0).""" charges = [PointCharge(-2, [-1, 0]), PointCharge(2, [1, 0])] self.field = ElectricField(charges)
# pylint: disable=invalid-name XMIN, XMAX = -200, 200 YMIN, YMAX = -150, 150 ZOOM = 33 XOFFSET = 0 electrostatics.init(XMIN, XMAX, YMIN, YMAX, ZOOM, XOFFSET) # Set up the charges and electric field charges = [PointCharge(1, [-2, 0]), PointCharge(1, [2, 0]), PointCharge(-1, [0, -2]), PointCharge(-1, [0, 2]), PointCharge(0, [0, 0])] field = ElectricField(charges) # Set up the Gaussian surfaces g = [GaussianCircle(charges[i].x, 0.1) for i in range(len(charges))] g[2].a0 = radians(90) g[3].a0 = radians(-90) # Create the field lines fieldlines = [] for g_ in g[2:4]: for x in g_.fluxpoints(field, 12): fieldlines.append(field.line(x)) fieldlines.append(field.line([-1,0])) fieldlines.append(field.line([1,0])) fieldlines.append(field.line([-3,0])) fieldlines.append(field.line([3,0]))
elements_between_limits=200) charges = [ PointChargeFlatland(2, [0, 0]), PointChargeFlatland(-1, [2, 1]), PointChargeFlatland(1, [4, 0]), LineCharge(1, [-1, -2], [-1, 2]) ] # %% # Original version XMIN, XMAX = -40, 40 YMIN, YMAX = -30, 30 ZOOM = 6 XOFFSET = 2 init(XMIN, XMAX, YMIN, YMAX, ZOOM, XOFFSET) field = ElectricField(charges) f = pyplot.figure(figsize=(6, 4.5)) field.plot(-1.7, 0.8) for charge in charges: charge.plot() finalize_plot() pyplot.savefig('image.png') # %% # Sequential version electric_field = SequentialElectricField(config, charges) electric_field.draw(n_min=-1.7, n_max=0.8, n_step=0.2) # electric_field.time_it() # electric_field.calculate() # %%