Exemplo n.º 1
0
    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())
Exemplo n.º 2
0
    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())
Exemplo n.º 3
0
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:
Exemplo n.º 4
0
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()
Exemplo n.º 5
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)