Ejemplo n.º 1
0
 def test_constant_vector_and_scalar_fields(self):
     VField1 = ConstantVectorConservativeField(
         'My field', 'My type', np.array([1.0, 0.0, 0.0], dtype=np.double))
     CField1 = ConstantScalarConservativeField('My field 1', 'My type',
                                               np.pi)
     SField = SuperposedField('My super Field', [VField1, CField1])
     xyz = np.ones((100, 3), dtype=np.double)
     result = SField.vector_field(xyz)
     check = np.zeros((100, 3), dtype=np.double)
     check[:, 0] += np.ones(100, dtype=np.double)
     np.testing.assert_allclose(result, check)
     result = SField.scalar_field(xyz)
     np.testing.assert_allclose(
         result, np.pi + np.asarray(VField1.scalar_field(xyz)))
Ejemplo n.º 2
0
 def test_constant_vector_fields(self):
     VField1 = ConstantVectorConservativeField(
         'My field', 'My type', np.array([1.0, 0.0, 0.0], dtype=np.double))
     VField2 = ConstantVectorConservativeField(
         'My field', 'My type', np.array([0.0, 1.0, 0.0], dtype=np.double))
     VField3 = ConstantVectorConservativeField(
         'My field', 'My type', np.array([0.0, 0.0, 1.0], dtype=np.double))
     SField = SuperposedField('My super Field', [VField1, VField2])
     xyz = np.ones((100, 3), dtype=np.double)
     result = SField.vector_field(xyz)
     check = np.zeros((100, 3), dtype=np.double)
     check[:, 0] += np.ones(100, dtype=np.double)
     check[:, 1] += np.ones(100, dtype=np.double)
     np.testing.assert_allclose(result, check)
     SField.fields += [VField3]
     result = SField.vector_field(xyz)
     check[:, 2] += np.ones(100, dtype=np.double)
     np.testing.assert_allclose(result, check)
Ejemplo n.º 3
0
 def test_constant_scalar_fields(self):
     CField1 = ConstantScalarConservativeField('My field 1', 'My type',
                                               np.pi)
     CField2 = ConstantScalarConservativeField('My field 2', 'My type',
                                               -np.pi)
     CField3 = ConstantScalarConservativeField('My field 3', 'My type',
                                               np.pi)
     SField = SuperposedField('My super Field', [CField1, CField2])
     xyz = np.ones((100, 3), dtype=np.double)
     result = SField.scalar_field(xyz)
     np.testing.assert_allclose(result, np.zeros(100, dtype=np.double))
     SField.fields += [CField3]
     result = SField.scalar_field(xyz)
     np.testing.assert_allclose(result,
                                np.ones(100, dtype=np.double) * np.pi)
     xyz = np.ones((100, 3), dtype=np.double)
     result = SField.vector_field(xyz)
     check = np.zeros((100, 3), dtype=np.double)
     np.testing.assert_allclose(result, check)
Ejemplo n.º 4
0
 def setUp(self):
     self.Field = Field('My Field', 'The Type of My Field')
     self.SField = SuperposedField('My super Field', [])
Ejemplo n.º 5
0
class TestField(unittest.TestCase):
    def setUp(self):
        self.Field = Field('My Field', 'The Type of My Field')
        self.SField = SuperposedField('My super Field', [])

    def test_name(self):
        self.assertEqual(self.SField.name, 'My super Field')
        self.SField.name = 'Another name for My super Field'
        self.assertEqual(self.SField.name, 'Another name for My super Field')

    def test_type(self):
        self.assertIsNone(self.SField.type)
        self.SField.type = 'Another type for My super Field'
        self.assertEqual(self.SField.type, 'Another type for My super Field')
        with self.assertRaises(TypeError):
            self.SField.type = 3.14

    def test_elements(self):
        self.assertFalse(
            self.SField.add_element(
                Field('My another Field', 'The Type of My Field')))
        self.assertFalse(
            self.SField.remove_element(
                Field('My another Field', 'The Type of My Field')))

    def test_scalar_field(self):
        xyz = np.ones((100, 3), dtype=np.double)
        result = self.SField.scalar_field(xyz)
        np.testing.assert_allclose(result, np.zeros(100, dtype=np.double))

    def test_vector_field(self):
        xyz = np.ones((100, 3), dtype=np.double)
        result = self.SField.vector_field(xyz)
        np.testing.assert_allclose(result, np.zeros((100, 3), dtype=np.double))

    def test_constant_scalar_fields(self):
        CField1 = ConstantScalarConservativeField('My field 1', 'My type',
                                                  np.pi)
        CField2 = ConstantScalarConservativeField('My field 2', 'My type',
                                                  -np.pi)
        CField3 = ConstantScalarConservativeField('My field 3', 'My type',
                                                  np.pi)
        SField = SuperposedField('My super Field', [CField1, CField2])
        xyz = np.ones((100, 3), dtype=np.double)
        result = SField.scalar_field(xyz)
        np.testing.assert_allclose(result, np.zeros(100, dtype=np.double))
        SField.fields += [CField3]
        result = SField.scalar_field(xyz)
        np.testing.assert_allclose(result,
                                   np.ones(100, dtype=np.double) * np.pi)
        xyz = np.ones((100, 3), dtype=np.double)
        result = SField.vector_field(xyz)
        check = np.zeros((100, 3), dtype=np.double)
        np.testing.assert_allclose(result, check)

    def test_constant_vector_fields(self):
        VField1 = ConstantVectorConservativeField(
            'My field', 'My type', np.array([1.0, 0.0, 0.0], dtype=np.double))
        VField2 = ConstantVectorConservativeField(
            'My field', 'My type', np.array([0.0, 1.0, 0.0], dtype=np.double))
        VField3 = ConstantVectorConservativeField(
            'My field', 'My type', np.array([0.0, 0.0, 1.0], dtype=np.double))
        SField = SuperposedField('My super Field', [VField1, VField2])
        xyz = np.ones((100, 3), dtype=np.double)
        result = SField.vector_field(xyz)
        check = np.zeros((100, 3), dtype=np.double)
        check[:, 0] += np.ones(100, dtype=np.double)
        check[:, 1] += np.ones(100, dtype=np.double)
        np.testing.assert_allclose(result, check)
        SField.fields += [VField3]
        result = SField.vector_field(xyz)
        check[:, 2] += np.ones(100, dtype=np.double)
        np.testing.assert_allclose(result, check)

    def test_constant_vector_and_scalar_fields(self):
        VField1 = ConstantVectorConservativeField(
            'My field', 'My type', np.array([1.0, 0.0, 0.0], dtype=np.double))
        CField1 = ConstantScalarConservativeField('My field 1', 'My type',
                                                  np.pi)
        SField = SuperposedField('My super Field', [VField1, CField1])
        xyz = np.ones((100, 3), dtype=np.double)
        result = SField.vector_field(xyz)
        check = np.zeros((100, 3), dtype=np.double)
        check[:, 0] += np.ones(100, dtype=np.double)
        np.testing.assert_allclose(result, check)
        result = SField.scalar_field(xyz)
        np.testing.assert_allclose(
            result, np.pi + np.asarray(VField1.scalar_field(xyz)))
Ejemplo n.º 6
0
neg_charged_ball.coordinate_system.origin = neg_ball_position

space.add_element(pos_charged_ball)
space.add_element(neg_charged_ball)

pos_electrostatic_field = HyperbolicPotentialSphericalConservativeField(name='Pos Charged ball field',
                                                                        field_type='electrostatic',
                                                                        a=pos_ball_charge, r=pos_ball_radius)
pos_charged_ball.add_element(pos_electrostatic_field)

neg_electrostatic_field = HyperbolicPotentialSphericalConservativeField(name='Neg Charged ball field',
                                                                        field_type='electrostatic',
                                                                        a=neg_ball_charge, r=neg_ball_radius)
neg_charged_ball.add_element(neg_electrostatic_field)

fields_superposition = SuperposedField('Field of two charged balls', [pos_electrostatic_field,
                                                                      neg_electrostatic_field])

space.add_element(fields_superposition)

fig = mlab.figure('CS demo', bgcolor=(0.0, 0.0, 0.0))  # Create the mayavi figure

pos_charged_ball_vis = Visual.FigureView(fig, pos_charged_ball)
pos_charged_ball_vis.set_color((1, 0, 0))
pos_charged_ball_vis.draw()

neg_charged_ball_vis = Visual.FigureView(fig, neg_charged_ball)
neg_charged_ball_vis.set_color((0, 0, 1))
neg_charged_ball_vis.draw()

fields_superposition_vis = Visual.FieldView(fig, fields_superposition, scalar_field_visible=False)