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])
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]
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]