Exemple #1
0
    def getB(self, *sources, dupWarning=True):
        """Extract the magnetic field based on the Sensor orientation

        Parameters
        ----------
        dupWarning : Check if there are any duplicate sources, optional.
            This will prevent duplicates and throw a warning, by default True.

        Returns
        -------
        [vec3]
            B-Field as perceived by the sensor

        Example
        -------
        >>> from magpylib import source, Sensor
        >>> sensor = Sensor([0,0,0],90,(0,0,1)) # This sensor is rotated in respect to space
        >>> cyl = source.magnet.Cylinder([1,2,300],[1,2])
        >>> absoluteReading = cyl.getB([0,0,0])
        >>> print(absoluteReading)
            [  0.552   1.105  268.328 ]
        >>> relativeReading = sensor.getB(cyl)
        >>> print(relativeReading)
            [  1.105  -0.552  268.328 ]
        """
        # Check input, add Collection list
        sourcesList = []
        for s in sources:
            try:
                addListToCollection(sourcesList, s.sources, dupWarning)
            except AttributeError:
                if isinstance(s, list) or isinstance(s, tuple):
                    addListToCollection(sourcesList, s, dupWarning)
                else:
                    assert isSource(s), "Argument " + str(s) + \
                        " in addSource is not a valid source for Collection"
                    if dupWarning is True:
                        addUniqueSource(s, sourcesList)
                    else:
                        sourcesList += [s]

        # Read the field from all nominated sources
        Btotal = sum([s.getB(self.position) for s in sources])
        return angleAxisRotation(
            Btotal,
            -self.angle,  # Rotate in the opposite direction
            self.axis,
            [0, 0, 0])
Exemple #2
0
    def addSources(self, *sources, dupWarning=True):
        """
        This method adds the argument source objects to the collection.
        May also include other collections.

        Parameters
        ----------
        source : source object
            adds the source object `source` to the collection.

        dupWarning : bool
            Warn and prevent if there is an attempt to add a 
            duplicate source into the collection. Set to false to disable
            check and increase performance.

        Returns
        -------
        None

        Example
        -------
        >>> from magpylib import source, Collection
        >>> pm1 = source.magnet.Box(mag=[0,0,1000],dim=[1,1,1])
        >>> pm2 = source.magnet.Cylinder(mag=[0,0,1000],dim=[1,1])
        >>> pm3 = source.magnet.Sphere(mag=[0,0,1000],dim=1)
        >>> col = Collection(pm1)
        >>> print(col.getB([1,0,1]))
          [4.29223532e+01 1.76697482e-14 1.37461635e+01]
        >>> col.addSource(pm2)
        >>> print(col.getB([1,0,1]))
          [7.72389756e+01 1.76697482e-14 2.39070726e+01]
        >>> col.addSource(pm3)
        >>> print(
          [9.93360625e+01 1.76697482e-14 3.12727683e+01]
        """
        for s in sources:
            if type(s) == Collection:
                addListToCollection(self.sources, s.sources, dupWarning)
            elif isinstance(s, list) or isinstance(s, tuple):
                addListToCollection(self.sources, s, dupWarning)
            else:
                assert isSource(s), "Argument " + str(s) + \
                    " in addSource is not a valid source for Collection"
                if dupWarning is True:
                    addUniqueSource(s, self.sources)
                else:
                    self.sources += [s]
Exemple #3
0
    def __init__(self, *sources, dupWarning=True):

        self.sources = []

        # The following will add Sources to the Collection sources list,
        # The code is the same as the addsource method.
        # addSource() is not cast here because it will
        # put a tuple inside a tuple.
        # Iterating for this would compromise performance.
        for s in sources:
            if type(s) == Collection:
                addListToCollection(self.sources, s.sources, dupWarning)
            elif isinstance(s, list) or isinstance(s, tuple):
                addListToCollection(self.sources, s, dupWarning)
            else:
                assert isSource(s), "Argument " + str(s) + \
                    " in addSource is not a valid source for Collection"
                if dupWarning is True:
                    addUniqueSource(s, self.sources)
                else:
                    self.sources += [s]
Exemple #4
0
    def removeSource(self, source_ref=-1):
        """
        Remove a source from the sources list. 

        Parameters
        ----------

        source_ref : source object or int
            [Optional] Remove the inputted source from the list
            [Optional] If given an int, remove a source at the given index position. Default: Last position.

        Return
        ------

        Popped source object.

        Raises
        ------

        ValueError
            Will be thrown if you attempt to remove a source that is not in the Collection.

        AssertionError
            Will be thrown if inputted index kwarg type is not type int

        Example
        -------

            >>> from magpylib import Collection, source
            >>> s = source.magnet.Sphere(mag=[1,2,3],dim=1,pos=[3,3,3])
            >>> s2 = source.magnet.Sphere(mag=[1,2,3],dim=2,pos=[-3,-3,-3])
            >>> m = source.moment.Dipole(moment=[1,2,3],pos=(0,0,0))
            >>> c = Collection(s,s2,m)
            >>> print(c.sources)
            [<magpylib._lib.classes.magnets.Sphere object at 0xa31eafcc>, 
            <magpylib._lib.classes.magnets.Sphere object at 0xa31ea1cc>, 
            <magpylib._lib.classes.moments.Dipole object at 0xa31ea06c>]
            >>> c.removeSource(s)
            >>> print(c.sources)
            [<magpylib._lib.classes.magnets.Sphere object at 0xa31ea1cc>, 
            <magpylib._lib.classes.moments.Dipole object at 0xa31ea06c>]
            >>> c.removeSource(s2)
            >>> print(c.sources)
            [<magpylib._lib.classes.moments.Dipole object at 0xa31ea06c>]
            >>> c.removeSource()
            >>> print(c.sources)
            []



        """
        assert type(source_ref) == int or isSource(
            source_ref), "Reference in removeSource is not an int nor a source"
        if type(source_ref) == int:
            try:
                return self.sources.pop(source_ref)
            except IndexError as e:  # Give a more helpful error message.
                raise type(e)(
                    str(e) + ' - Index ' + str(source_ref) +
                    ' in collection source is not accessible for removeSource')
        else:
            try:
                self.sources.remove(source_ref)
            except ValueError as e:  # Give a more helpful error message.
                raise type(e)(str(e) + ' - ' + str(type(source_ref)) +
                              ' not in list for removeSource')
            return source_ref