def test_with_only_point_charges_should_be_equal_to_original_results(self): charges = [ PointCharge(2, [0, 0]), PointCharge(-1, [2, 1]), PointCharge(1, [4, 0]) ] original_electric_field = ElectricFieldWrapper(self._config, charges) original_result, _, __ = original_electric_field.calculate() parallel_electric_field = ParallelElectricField( self._config, charges, 16) parallel_result, _, __ = parallel_electric_field.calculate() assert_array_almost_equal(original_result, parallel_result, decimal=5)
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())
def test_with_multiple_charge_types_should_be_equal_to_original_results( self): charges = [ PointChargeFlatland(2, [0, 0]), PointCharge(-1, [2, 1]), LineCharge(1, [-1, -2], [-1, 2]) ] original_electric_field = ElectricFieldWrapper(self._config, charges) original_result, _, __ = original_electric_field.calculate() sequential_electric_field = SequentialElectricField( self._config, charges) sequential_result, _, __ = sequential_electric_field.calculate() assert_array_almost_equal(original_result, sequential_result, decimal=5)
def test_with_multiple_charge_types_should_be_equal_to_sequential_results( self): charges = [ PointChargeFlatland(2, [0, 0]), PointCharge(-1, [2, 1]), LineCharge(1, [-1, -2], [-1, 2]) ] sequential_electric_field = SequentialElectricField( self._config, charges) sequential_result, _, __ = sequential_electric_field.calculate() parallel_electric_field = ParallelElectricField( self._config, charges, 16) parallel_result, _, __ = parallel_electric_field.calculate() assert_array_almost_equal(sequential_result, parallel_result, decimal=5)
from electrostatics import PointCharge from electrostatics import ElectricField, Potential, GaussianCircle from electrostatics import finalize_plot # 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) 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 = []
import electrostatics from electrostatics import PointCharge, ElectricField, Potential, 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(2, [-2, 0]), PointCharge(-4, [0, 0]), PointCharge(2, [2, 0]) ] field = ElectricField(charges) potential = Potential(charges) # Set up the Gaussian surfaces g1 = GaussianCircle(charges[0].x, 0.1) g2 = GaussianCircle(charges[-1].x, 0.1) # Create the field lines fieldlines = [] for g in [g1, g2]: for x in g.fluxpoints(field, 12): fieldlines.append(field.line(x))
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(2, [-2, 0]), PointCharge(-1, [-2/3, 0]), PointCharge(-1, [0, -2/3]), PointCharge(-1, [0, 2/3]), PointCharge(-1, [2/3, 0]), PointCharge(2, [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[2].a0 = radians(90) g[3].a0 = radians(-90) # Create the field lines 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))
import electrostatics from electrostatics import PointCharge from electrostatics import ElectricField, Potential, 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, [-2, 0]), PointCharge(1, [2, 0]), 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[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))
def setUp(self): """Creates a q=2 point charge at (0, 0).""" self.charge = PointCharge(2, [0, 0])
def setUp(self): """Sets up a dipole centred at (0, 0).""" charges = [PointCharge(-2, [-1, 0]), PointCharge(2, [1, 0])] self.field = ElectricField(charges)
import electrostatics from electrostatics import PointCharge from electrostatics import ElectricField, Potential, GaussianCircle from electrostatics import finalize_plot # pylint: disable=invalid-name XMIN, XMAX = -200, 200 YMIN, YMAX = -150, 150 ZOOM = 30 XOFFSET = 0 electrostatics.init(XMIN, XMAX, YMIN, YMAX, ZOOM, XOFFSET) # Set up the charges, electric field, and potential charges = [PointCharge(1, [-2, 0]), PointCharge(1, [2, 0]), PointCharge(1, [0, -2]), PointCharge(1, [0, 2]), PointCharge(-4, [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[:-1]:
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 = 2 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 = [ PointCharge(2, [0, 0]), PointCharge(-1, [2, 0]), PointCharge(0, [6.82842712474619, 0]) ] field = ElectricField(charges) # Set up the Gaussian surfaces g1 = GaussianCircle(charges[0].x, 29) g2 = GaussianCircle(charges[1].x, 0.1) # Create the field lines fieldlines = [] for x in g1.fluxpoints(field, 12): fieldlines.append(field.line(x)) for x in g2.fluxpoints(field, 12): fieldlines.append(field.line(x))
# pylint: disable=invalid-name XMIN, XMAX = -200, 200 YMIN, YMAX = -150, 150 ZOOM = 30 XOFFSET = 0 electrostatics.init(XMIN, XMAX, YMIN, YMAX, ZOOM, XOFFSET) # Set up the charges and electric field charges = [ LineCharge(0.2, [-2, -0.5], [-2, 0.5]), LineCharge(0.4, [-2, -0.5], [0, -0.5]), LineCharge(0.4, [-2, 0.5], [0, 0.5]), 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, 15): fieldlines.append(field.line(x)) fieldlines.append(field.line([-3, 0])) # Plotting pyplot.figure(figsize=(6, 4.5)) field.plot()
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()
import electrostatics 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: