magpylib.source.magnet package

This subpackage provides the permanent magnet classes that are used for field computation. They include Box (cuboid shape), Cylinder (cylindrical shape) Sphere (spherical shape) and Facet (triangular surface of magnet body).

class magpylib.source.magnet.Box(mag=(0.0, 0.0, 0.0), dim=(0.0, 0.0, 0.0), pos=(0.0, 0.0, 0.0), angle=0.0, axis=(0.0, 0.0, 1.0))[source]

Bases: magpylib._lib.classes.base.HomoMag

A homogeneously magnetized cuboid magnet. In the canonical basis (position=[0,0,0], angle=0.0, axis=[0,0,1]) the magnet has the origin at its geometric center and the sides of the box are parallel to the basis vectors. Scalar input is either integer or float. Vector input format can be either list, tuple or array of any data type (float, int).

Parameters:
  • mag (vec3 [mT]) – Set magnetization vector of magnet in units of [mT].
  • dim (vec3 [mm]) – Set the size of the box. dim=[a,b,c] which anchorresponds to the three side lenghts of the box in units of [mm].
  • pos=[0,0,0] (vec3 [mm]) – Set position of the center of the magnet in units of [mm].
  • angle=0.0 (scalar [deg]) – Set angle of orientation of magnet in units of [deg].
  • axis=[0,0,1] (vec3 []) – Set axis of orientation of the magnet.
magnetization

Magnetization vector of box in units of [mT].

Type:arr3 [mT]
dimension

Magnet dimension=[a,b,c] which anchorrespond to the three side lenghts of the box in units of [mm] in x-,y- and z-direction respectively in the canonical basis.

Type:arr3 [mm]
position

Position of the center of the magnet in units of [mm].

Type:arr3 [mm]
angle

Angle of orientation of the magnet in units of [deg].

Type:float [deg]
axis

Axis of orientation of the magnet.

Type:arr3 []

Example

>>> from magpylib import source
>>> pm = source.magnet.Box(mag=[0,0,1000],dim=[1,1,1])
>>> B = pm.getB([1,0,1])
>>> print(B)
  [4.29223532e+01 1.76697482e-14 1.37461635e+01]

Note

The following Methods are available to all sources objects.

getB(pos)[source]

This method returns the magnetic field vector generated by the source at the argument position pos in units of [mT]

Parameters:pos (vec3 [mm] Position or list of Positions where magnetic field) – should be determined.
Returns:magnetic field vector – position pos generated by the source in units of [mT].
Return type:arr3 [mT] Magnetic field at the argument
move(displacement)

This method moves the source by the argument vector displacement. Vector input format can be either list, tuple or array of any data type (float, int).

Parameters:displacement (vec3 [mm]) – Set displacement vector
Returns:
Return type:None

Example

>>> from magpylib import source
>>> pm = source.magnet.Sphere(mag=[0,0,1000],dim=1,pos=[1,2,3])
>>> print(pm.position)
    [1. 2. 3.]
>>> pm.move([3,2,1])
>>> print(pm.position)
    [4. 4. 4.]
rotate(angle, axis, anchor='self.position')

This method rotates the source about axis by angle. The axis passes through the center of rotation anchor. Scalar input is either integer or float. Vector input format can be either list, tuple or array of any data type (float, int).

Parameters:
  • angle (scalar [deg]) – Set angle of rotation in units of [deg]
  • axis (vec3 []) – Set axis of rotation
  • anchor (vec3 [mm]) – Specify the Center of rotation which defines the position of the axis of rotation. If not specified the source will rotate about its own center.
Returns:

Return type:

None

Example

>>> from magpylib import source
>>> pm = source.magnet.Sphere(mag=[0,0,1000], dim=1)
>>> print(pm.position, pm.angle, pm.axis)
  [0. 0. 0.] 0.0 [0. 0. 1.]
>>> pm.rotate(90, [0,1,0], anchor=[1,0,0])
>>> print(pm.position, pm.angle, pm.axis)
  [1., 0., 1.] 90.0 [0., 1., 0.]
setOrientation(angle, axis)

This method sets a new source orientation given by angle and axis. Scalar input is either integer or float. Vector input format can be either list, tuple or array of any data type (float, int).

Parameters:
  • angle (scalar [deg]) – Set new angle of source orientation.
  • axis (vec3 []) – Set new axis of source orientation.
Returns:

Return type:

None

Example

>>> from magpylib import source
>>> pm = source.magnet.Sphere(mag=[0,0,1000],dim=1)
>>> print([pm.angle,pm.axis])
    [0.0, array([0., 0., 1.])]
>>> pm.setOrientation(45,[0,1,0])
>>> print([pm.angle,pm.axis])
    [45.0, array([0., 1., 0.])]
setPosition(newPos)

This method moves the source to the position given by the argument vector newPos. Vector input format can be either list, tuple or array of any data type (float, int)

Parameters:newPos (vec3 [mm]) – Set new position of the source.
Returns:
Return type:None

Example

>>> from magpylib import source
>>> pm = source.magnet.Sphere(mag=[0,0,1000],dim=1)
>>> print(pm.position)
    [0. 0. 0.]
>>> pm.setPosition([5,5,5])
>>> print(pm.position)
    [5. 5. 5.]
class magpylib.source.magnet.Cylinder(mag=(0.0, 0.0, 0.0), dim=(0.0, 0.0), pos=(0.0, 0.0, 0.0), angle=0.0, axis=(0.0, 0.0, 1.0), iterDia=50)[source]

Bases: magpylib._lib.classes.base.HomoMag

A homogeneously magnetized cylindrical magnet. The magnet is initialized in the canonical basis (position=[0,0,0], angle=0.0, axis=[0,0,1]) with the geometric center at the origin and the central symmetry axis pointing in z-direction so that the circular bottom lies in a plane parallel to the xy-plane. Scalar input is either integer or float and reflects a round bottom. Vector input format can be either list, tuple or array of any data type (float, int).

Parameters:
  • mag (vec3 [mT]) – Set magnetization vector of magnet in units of [mT].
  • dim (vec2 [mm]) – Set the size of the cylinder. dim=[D,H] which are diameter and height of the cylinder in units of [mm] respectively.
  • pos=[0,0,0] (vec3 [mm]) – Set position of the center of the magnet in units of [mm].
  • angle=0.0 (scalar [deg]) – Set angle of orientation of magnet in units of [deg].
  • axis=[0,0,1] (vec3 []) – Set axis of orientation of the magnet.
  • iterDia=50 (int []) – Set number of iterations for calculation of B-field from non-axial magnetization. Lower values will make the calculation faster but less precise.
magnetization

Magnetization vector of magnet in units of [mT].

Type:arr3 [mT]
dimension

Magnet dimension=[d,h] which anchorrespond to diameter and height of the cylinder in units of [mm].

Type:arr2 [mm]
position

Position of the center of the magnet in units of [mm].

Type:arr3 [mm]
angle

Angle of orientation of the magnet in units of [deg].

Type:float [deg]
axis

Axis of orientation of the magnet.

Type:arr3 []
iterDia

Number of iterations for calculation of B-field from non-axial magnetization. Lower values will make the calculation faster but less precise.

Type:int []

Example

>>> from magpylib import source
>>> pm = source.magnet.Cylinder(mag=[0,0,1000],dim=[1,1])
>>> B = pm.getB([1,0,1])
>>> print(B)
  [34.31662243  0.         10.16090915]

Note

The following Methods are available to all sources objects.

getB(pos)[source]

This method returns the magnetic field vector generated by the source at the argument position pos in units of [mT]

Parameters:pos (vec3 [mm] Position or list of Positions where magnetic field) – should be determined.
Returns:magnetic field vector – position pos generated by the source in units of [mT].
Return type:arr3 [mT] Magnetic field at the argument
move(displacement)

This method moves the source by the argument vector displacement. Vector input format can be either list, tuple or array of any data type (float, int).

Parameters:displacement (vec3 [mm]) – Set displacement vector
Returns:
Return type:None

Example

>>> from magpylib import source
>>> pm = source.magnet.Sphere(mag=[0,0,1000],dim=1,pos=[1,2,3])
>>> print(pm.position)
    [1. 2. 3.]
>>> pm.move([3,2,1])
>>> print(pm.position)
    [4. 4. 4.]
rotate(angle, axis, anchor='self.position')

This method rotates the source about axis by angle. The axis passes through the center of rotation anchor. Scalar input is either integer or float. Vector input format can be either list, tuple or array of any data type (float, int).

Parameters:
  • angle (scalar [deg]) – Set angle of rotation in units of [deg]
  • axis (vec3 []) – Set axis of rotation
  • anchor (vec3 [mm]) – Specify the Center of rotation which defines the position of the axis of rotation. If not specified the source will rotate about its own center.
Returns:

Return type:

None

Example

>>> from magpylib import source
>>> pm = source.magnet.Sphere(mag=[0,0,1000], dim=1)
>>> print(pm.position, pm.angle, pm.axis)
  [0. 0. 0.] 0.0 [0. 0. 1.]
>>> pm.rotate(90, [0,1,0], anchor=[1,0,0])
>>> print(pm.position, pm.angle, pm.axis)
  [1., 0., 1.] 90.0 [0., 1., 0.]
setOrientation(angle, axis)

This method sets a new source orientation given by angle and axis. Scalar input is either integer or float. Vector input format can be either list, tuple or array of any data type (float, int).

Parameters:
  • angle (scalar [deg]) – Set new angle of source orientation.
  • axis (vec3 []) – Set new axis of source orientation.
Returns:

Return type:

None

Example

>>> from magpylib import source
>>> pm = source.magnet.Sphere(mag=[0,0,1000],dim=1)
>>> print([pm.angle,pm.axis])
    [0.0, array([0., 0., 1.])]
>>> pm.setOrientation(45,[0,1,0])
>>> print([pm.angle,pm.axis])
    [45.0, array([0., 1., 0.])]
setPosition(newPos)

This method moves the source to the position given by the argument vector newPos. Vector input format can be either list, tuple or array of any data type (float, int)

Parameters:newPos (vec3 [mm]) – Set new position of the source.
Returns:
Return type:None

Example

>>> from magpylib import source
>>> pm = source.magnet.Sphere(mag=[0,0,1000],dim=1)
>>> print(pm.position)
    [0. 0. 0.]
>>> pm.setPosition([5,5,5])
>>> print(pm.position)
    [5. 5. 5.]
class magpylib.source.magnet.Sphere(mag=(0.0, 0.0, 0.0), dim=0.0, pos=(0.0, 0.0, 0.0), angle=0.0, axis=(0.0, 0.0, 1.0))[source]

Bases: magpylib._lib.classes.base.HomoMag

A homogeneously magnetized sphere. The magnet is initialized in the canonical basis (position=[0,0,0], angle=0.0, axis=[0,0,1]) with the center at the origin. Scalar input is either integer or float. Vector input format can be either list, tuple or array of any data type (float, int).

Parameters:
  • mag (vec3 [mT]) – Set magnetization vector of magnet in units of [mT].
  • dim (float [mm]) – Set diameter of the sphere in units of [mm].
  • pos=[0,0,0] (vec3 [mm]) – Set position of the center of the magnet in units of [mm].
  • angle=0.0 (scalar [deg]) – Set angle of orientation of magnet in units of [deg].
  • axis=[0,0,1] (vec3 []) – Set axis of orientation of the magnet.
magnetization

Magnetization vector of magnet in units of [mT].

Type:arr3 [mT]
dimension

Sphere diameter in units of [mm].

Type:float [mm]
position

Position of the center of the magnet in units of [mm].

Type:arr3 [mm]
angle

Angle of orientation of the magnet in units of [deg].

Type:float [deg]
axis

Axis of orientation of the magnet.

Type:arr3 []

Example

>>> from magpylib import source
>>> pm = source.magnet.Sphere(mag=[0,0,1000],dim=1)
>>> B = pm.getB([1,0,1])
>>> print(B)
  [22.09708691  0.          7.36569564]

Note

The following Methods are available to all sources objects.

getB(pos)[source]

This method returns the magnetic field vector generated by the source at the argument position pos in units of [mT]

Parameters:pos (vec3 [mm] Position or list of Positions where magnetic field) – should be determined.
Returns:magnetic field vector – position pos generated by the source in units of [mT].
Return type:arr3 [mT] Magnetic field at the argument
move(displacement)

This method moves the source by the argument vector displacement. Vector input format can be either list, tuple or array of any data type (float, int).

Parameters:displacement (vec3 [mm]) – Set displacement vector
Returns:
Return type:None

Example

>>> from magpylib import source
>>> pm = source.magnet.Sphere(mag=[0,0,1000],dim=1,pos=[1,2,3])
>>> print(pm.position)
    [1. 2. 3.]
>>> pm.move([3,2,1])
>>> print(pm.position)
    [4. 4. 4.]
rotate(angle, axis, anchor='self.position')

This method rotates the source about axis by angle. The axis passes through the center of rotation anchor. Scalar input is either integer or float. Vector input format can be either list, tuple or array of any data type (float, int).

Parameters:
  • angle (scalar [deg]) – Set angle of rotation in units of [deg]
  • axis (vec3 []) – Set axis of rotation
  • anchor (vec3 [mm]) – Specify the Center of rotation which defines the position of the axis of rotation. If not specified the source will rotate about its own center.
Returns:

Return type:

None

Example

>>> from magpylib import source
>>> pm = source.magnet.Sphere(mag=[0,0,1000], dim=1)
>>> print(pm.position, pm.angle, pm.axis)
  [0. 0. 0.] 0.0 [0. 0. 1.]
>>> pm.rotate(90, [0,1,0], anchor=[1,0,0])
>>> print(pm.position, pm.angle, pm.axis)
  [1., 0., 1.] 90.0 [0., 1., 0.]
setOrientation(angle, axis)

This method sets a new source orientation given by angle and axis. Scalar input is either integer or float. Vector input format can be either list, tuple or array of any data type (float, int).

Parameters:
  • angle (scalar [deg]) – Set new angle of source orientation.
  • axis (vec3 []) – Set new axis of source orientation.
Returns:

Return type:

None

Example

>>> from magpylib import source
>>> pm = source.magnet.Sphere(mag=[0,0,1000],dim=1)
>>> print([pm.angle,pm.axis])
    [0.0, array([0., 0., 1.])]
>>> pm.setOrientation(45,[0,1,0])
>>> print([pm.angle,pm.axis])
    [45.0, array([0., 1., 0.])]
setPosition(newPos)

This method moves the source to the position given by the argument vector newPos. Vector input format can be either list, tuple or array of any data type (float, int)

Parameters:newPos (vec3 [mm]) – Set new position of the source.
Returns:
Return type:None

Example

>>> from magpylib import source
>>> pm = source.magnet.Sphere(mag=[0,0,1000],dim=1)
>>> print(pm.position)
    [0. 0. 0.]
>>> pm.setPosition([5,5,5])
>>> print(pm.position)
    [5. 5. 5.]
class magpylib.source.magnet.Facet[source]

Bases: magpylib._lib.classes.base.HomoMag

WIP

getB(pos)

This method returns the magnetic field vector generated by the source at the argument position pos in units of [mT]

Parameters:pos (vec3 [mm] Position or list of Positions where magnetic field) – should be determined.
Returns:magnetic field vector – position pos generated by the source in units of [mT].
Return type:arr3 [mT] Magnetic field at the argument
move(displacement)

This method moves the source by the argument vector displacement. Vector input format can be either list, tuple or array of any data type (float, int).

Parameters:displacement (vec3 [mm]) – Set displacement vector
Returns:
Return type:None

Example

>>> from magpylib import source
>>> pm = source.magnet.Sphere(mag=[0,0,1000],dim=1,pos=[1,2,3])
>>> print(pm.position)
    [1. 2. 3.]
>>> pm.move([3,2,1])
>>> print(pm.position)
    [4. 4. 4.]
rotate(angle, axis, anchor='self.position')

This method rotates the source about axis by angle. The axis passes through the center of rotation anchor. Scalar input is either integer or float. Vector input format can be either list, tuple or array of any data type (float, int).

Parameters:
  • angle (scalar [deg]) – Set angle of rotation in units of [deg]
  • axis (vec3 []) – Set axis of rotation
  • anchor (vec3 [mm]) – Specify the Center of rotation which defines the position of the axis of rotation. If not specified the source will rotate about its own center.
Returns:

Return type:

None

Example

>>> from magpylib import source
>>> pm = source.magnet.Sphere(mag=[0,0,1000], dim=1)
>>> print(pm.position, pm.angle, pm.axis)
  [0. 0. 0.] 0.0 [0. 0. 1.]
>>> pm.rotate(90, [0,1,0], anchor=[1,0,0])
>>> print(pm.position, pm.angle, pm.axis)
  [1., 0., 1.] 90.0 [0., 1., 0.]
setOrientation(angle, axis)

This method sets a new source orientation given by angle and axis. Scalar input is either integer or float. Vector input format can be either list, tuple or array of any data type (float, int).

Parameters:
  • angle (scalar [deg]) – Set new angle of source orientation.
  • axis (vec3 []) – Set new axis of source orientation.
Returns:

Return type:

None

Example

>>> from magpylib import source
>>> pm = source.magnet.Sphere(mag=[0,0,1000],dim=1)
>>> print([pm.angle,pm.axis])
    [0.0, array([0., 0., 1.])]
>>> pm.setOrientation(45,[0,1,0])
>>> print([pm.angle,pm.axis])
    [45.0, array([0., 1., 0.])]
setPosition(newPos)

This method moves the source to the position given by the argument vector newPos. Vector input format can be either list, tuple or array of any data type (float, int)

Parameters:newPos (vec3 [mm]) – Set new position of the source.
Returns:
Return type:None

Example

>>> from magpylib import source
>>> pm = source.magnet.Sphere(mag=[0,0,1000],dim=1)
>>> print(pm.position)
    [0. 0. 0.]
>>> pm.setPosition([5,5,5])
>>> print(pm.position)
    [5. 5. 5.]