def getBsweep(self, pos=numpyArray, multiprocessing=False, processes=0): Btotal = [] assert all( isPosVector(item) for item in pos), "Non-position vector in Collection getBsweep" if cpu_count() < len( numpyArray ) or multiprocessing is False: # If there are too few positions to parallelize in calcFields = [ s.getBsweep(pos, multiprocessing=multiprocessing, processes=processes) for s in self.sources ] else: calcFields = self._getBsweepObj(pos, processes) for p in range( len(pos)): # For each position, calculate and sum all fields px = py = pz = 0 for src in range(len(self.sources)): px += calcFields[src][p][0] # x coord val of this position py += calcFields[src][p][1] # y coord val of this position pz += calcFields[src][p][2] # z coord val of this position Btotal.append([px, py, pz]) return array(Btotal)
def test_isPosVectorArgs(): from numpy import array listOfArgs = [ [ [1,2,3], #pos [0,0,1], #MPos (180,(0,1,0)),],#Morientation [ [1,2,3], [0,1,0], (90,(1,0,0)),], [ [1,2,3], [1,0,0], (255,(0,1,0)),],] assert any(isPosVector(a)==False for a in listOfArgs)
def getBsweep(self, pos=numpyArray, multiprocessing=False, processes=0): Btotal = [] assert all( isPosVector(item) for item in pos), "Non-position vector in Collection getBsweep" calcFields = [ s.getBsweep(pos, multiprocessing=multiprocessing, processes=processes) for s in self.sources ] for p in range( len(pos)): # For each position, calculate and sum all fields px = py = pz = 0 for src in range(len(self.sources)): px += calcFields[src][p][0] # x coord val of this position py += calcFields[src][p][1] # y coord val of this position pz += calcFields[src][p][2] # z coord val of this position Btotal.append([px, py, pz]) return Btotal
def test_isPosVectorArgs(): from numpy import array errMsg = "isPosVector returned unexpected False value" position = array([1, 4, -24.242]) listOfArgs = [ [ [1, 2, 3], #pos [0, 0, 1], #MPos (180, (0, 1, 0)), ], #Morientation [ [1, 2, 3], [0, 1, 0], (90, (1, 0, 0)), ], [ [1, 2, 3], [1, 0, 0], (255, (0, 1, 0)), ], ] assert any(isPosVector(a) == False for a in listOfArgs)
def getBsweep(self, INPUT, multiprocessing=False, processes=Auto): """ This method can be used to determine the field for a given set of sensor positions, or for different magnet positions and orientations. While this can manually be achieved by looping getB, this getBsweep implements the possibility of parallelization. The input can have two different formats (ONLY THE FIRST ONE CAN BE USED FOR COLLECTIONS!): Warning ------- Multiprocessing enabled calculations will drastically reduce performance if done for small sets, i.e. under a few hundred. Parameters ---------- INPUT : TYPE [type 1 or type 2 input] INPUT TYPE 1 is a list of N sensor positions. In this case the magnetic field of the source is determined for all N sensor positions and returned in an Nx3 array. INPUT TYPE 2 is a list of the following format [(sensorPos1, sourcePos1, sourceOrient1),…]. Here for each case of sensor position and source state the field is evaluated and returned in an Nx3 array. This corresponds to a system where sensor and magnet move simultaneously. TYPE 2 DOES NOT WORK FOR COLLECTIONS ! multiprocessing : bool [bool] Default = False Enable/disable parallel multiprocessing; This requires some additional code on Windows, please refer to example below. processes : cores [int] Define the number of allowed processes when multiprocessing is enabled. Example ------- For INPUT of type 1: >>> from multiprocessing import freeze_support >>> if __name__ == "__main__": >>> freeze_support() >>> # Input >>> from magpylib.source import magnet >>> mag=[6,7,8] >>> dim=[10,10,10] >>> pos=[2,2,2] >>> listOfPos = [[.5,.5,5],[.5,.5,5],[.5,.5,5]] >>> # Run >>> pm = magnet.Box(mag,dim,pos) >>> result = pm.getBsweep(listOfPos) >>> print(result) ( [3.99074612, 4.67238469, 4.22419432], [3.99074612, 4.67238469, 4.22419432], [3.99074612, 4.67238469, 4.22419432],) For INPUT of type 2: >>> from multiprocessing import freeze_support >>> if __name__ == "__main__": >>> freeze_support() >>> # Input >>> from magpylib.source import magnet >>> mag=[1,2,3] >>> dim=[1,2,3] >>> pos=[0,0,0] >>> listOfArgs = [ [ [1,2,3], #pos ... [0,0,1], #MPos ... (180,(0,1,0)),],#Morientation ... [ [1,2,3], ... [0,1,0], ... (90,(1,0,0)),], ... [ [1,2,3], ... [1,0,0], ... (255,(0,1,0)),],] >>> # Run >>> pm = magnet.Box(mag,dim,pos) >>> result = pm.getBsweep(listOfArgs) >>> print(result) ( [ 0.00453617, -0.07055326, 0.03153698], [0.00488989, 0.04731373, 0.02416068], [0.0249435, 0.00106315, 0.02894469]) """ if multiprocessing is True: if all(isPosVector(item) for item in INPUT): return self._getBmultiList(INPUT, processes=processes) else: return self._getBDisplacement(INPUT, processes=processes) else: if all(isPosVector(item) for item in INPUT): return array(list(map(self.getB, INPUT))) else: return array( list( map(recoordinateAndGetB, repeat(self, times=len(INPUT)), INPUT)))
def test_isPosVectorArray2(): from numpy import array errMsg = "isPosVector returned unexpected False value" position = array([1, 4, -24.242]) assert isPosVector(position), errMsg
def test_isPosVector(): errMsg = "isPosVector returned unexpected False value" position = [1, 2, 3] assert isPosVector(position), errMsg