Exemple #1
0
    def test_potential_returns_float(self):
        testObj = Charge(self.positionList, self.charge)

        ppos = [1, 0, 0]
        electric_potential = testObj.potential(ppos)

        assert isinstance(electric_potential, float)
Exemple #2
0
    def test_field_returns_numpy_array(self):
        testObj = Charge(self.positionList, self.charge)

        fpos = [1, 0, 0]
        electric_field = testObj.field(fpos)

        assert isinstance(electric_field, np.ndarray)
Exemple #3
0
    def test_field_returns_expected_values(self):
        testObj = Charge(self.positionList, -self.charge)

        fpos = [1, 0, 0]
        electric_field = testObj.field(fpos)

        expected_field = np.array([-62.9, 0, 0])
        np.testing.assert_allclose(electric_field, expected_field, atol=1e-1)
Exemple #4
0
    def test_field_from_potential_returns_near_analytical(self):
        testObj = Charge(self.positionList, -self.charge)

        fpos = [1, 0, 0]
        electric_field = testObj.field(fpos, "potential")

        expected_field = testObj.field(fpos, "analytical")
        np.testing.assert_allclose(electric_field, expected_field, atol=1e-1)
Exemple #5
0
    def test_potential_returns_known_constant_case(self):
        pos = [0, 0, 0]
        charge = 1

        testObj = Charge(pos, charge)

        ppos = [1, 0, 0]
        potential = testObj.potential(ppos)

        expected_potential = (4 * pi * epsilon_0)**-1

        np.testing.assert_equal(potential, expected_potential)
Exemple #6
0
    def test_field_returns_known_constant_case(self):
        pos = [0, 0, 0]
        charge = 1

        testObj = Charge(pos, charge)

        fpos = [1, 0, 0]
        field = testObj.field(fpos)

        field_constant = (4 * pi * epsilon_0)**-1
        expected_field = np.array([field_constant, 0, 0])

        np.testing.assert_equal(field, expected_field)
Exemple #7
0
    def test_potential_volume_at_point_equal_class_potential(self):

        charge = Charge(self.position_1, self.charge)

        potential_volume = volume.potential(
            [charge],
            x_range=[-10, 10],
            y_range=[-10, 10],
            z_range=[-10, 10],
            h=1,
        )

        # Point = [-6, -6, -6]
        potential_at_point = potential_volume[4][4][4]

        expected_potential = charge.potential([-6, -6, -6])

        np.testing.assert_equal(potential_at_point, expected_potential)
Exemple #8
0
    def test_field_volume_at_point_equal_class_field(self):

        charge = Charge(self.position_1, self.charge)

        field_volume = volume.field(
            [charge],
            x_range=[-10, 10],
            y_range=[-10, 10],
            z_range=[-10, 10],
            h=1,
        )

        # Point = [-10, -6, -3]
        field_at_point = field_volume[0][4][7]

        expected_field = charge.field([-10, -6, -3])

        np.testing.assert_equal(field_at_point, expected_field)
Exemple #9
0
    def test_two_charge_field_volume_eq_sum_of_class_field(self):

        charges = [Charge(self.position_1, self.charge)]
        charges.append(Charge(self.position_2, -self.charge))

        field_volume = volume.field(
            charges,
            x_range=[-10, 10],
            y_range=[-10, 10],
            z_range=[-10, 10],
            h=1,
        )

        # Point = [-6, -5, -3]
        field_at_point = field_volume[4][5][7]

        expected_field = np.add(charges[0].field([-6, -5, -3]),
                                charges[1].field([-6, -5, -3]))

        np.testing.assert_equal(field_at_point, expected_field)
Exemple #10
0
    def test_neg_gradient_of_potiental_returns_field(self):
        pos = [0, 0, 0]
        charge = 7e-9

        testObj = Charge(pos, charge)

        ppos = [1, 2, -1]
        h = 0.001
        potential_grid = np.empty([3, 3, 3], dtype=object)
        x = np.linspace(ppos[0] - h, ppos[0] + h, 3)
        y = np.linspace(ppos[1] - h, ppos[1] + h, 3)
        z = np.linspace(ppos[2] - h, ppos[2] + h, 3)

        for (i, j, k), _ in np.ndenumerate(potential_grid):
            potential_grid[i][j][k] = testObj.potential([x[i], y[j], z[k]])

        xgrad, ygrad, zgrad = np.gradient(potential_grid, h)
        grad_potential = np.array(
            [xgrad[1, 1, 1], ygrad[1, 1, 1], zgrad[1, 1, 1]])

        electric_field = testObj.field(ppos)
        np.testing.assert_allclose(-grad_potential, electric_field, atol=1e-1)
Exemple #11
0
    def test_two_charge_potential_volume_eq_sum_of_class_potential(self):

        charges = [Charge(self.position_1, self.charge)]
        charges.append(Charge(self.position_2, -self.charge))

        potential_volume = volume.potential(
            charges,
            x_range=[-10, 10],
            y_range=[-10, 10],
            z_range=[-10, 10],
            h=1,
        )

        # Point = [-6, -5, -3]
        potential_at_point = potential_volume[4][5][7]

        expected_potential = np.add(
            charges[0].potential([-6, -5, -3]),
            charges[1].potential([-6, -5, -3]),
        )

        np.testing.assert_equal(potential_at_point, expected_potential)
Exemple #12
0
    def test_field_returns_singleton_dim_for_single_slice(self):

        charge = Charge(self.position_1, self.charge)

        field_volume = volume.field(
            [charge],
            x_range=[-10, 10],
            y_range=[1, 1],
            z_range=[-10, 10],
            h=0.1,
        )

        expected_shape = (201, 1, 201)
        actual_shape = field_volume.shape

        np.testing.assert_equal(actual_shape, expected_shape)
Exemple #13
0
 def test_q_decorator_equal_to_initialized_charge_value(self):
     testObj = Charge(self.positionList, self.charge)
     self.assertEqual(testObj.q, self.charge)
Exemple #14
0
    def test_potential_returns_nan_at_particle_position(self):
        testObj = Charge(self.positionList, self.charge)

        ppos = self.positionList

        self.assertTrue(np.isnan(testObj.potential(ppos)))
Exemple #15
0
 def test_that_3D_position_list_returns_an_object_of_type_Charge(self):
     assert isinstance(Charge(self.positionList, self.charge), Charge)
Exemple #16
0
 def test_Charge_object_has_parent_type_Particle(self):
     assert isinstance(Charge(self.positionList, self.charge), Particle)
Exemple #17
0
 def test_charge_property_equal_to_initialized_value(self):
     testObj = Charge(self.positionList, self.charge)
     self.assertEqual(testObj.charge, self.charge)
Exemple #18
0
    def test_field_returns_nan_at_particle_position(self):
        testObj = Charge(self.positionList, self.charge)

        fpos = self.positionList

        self.assertTrue(np.isnan(testObj.field(fpos)).all())