def test_GetBSweepArray_Error(): # Check if getBsweep throws an error when # crazy n dimensional arrays are provided errMsg = "unexpected getB for collection" type_erMsg = "getBsweep did not return a numpy array." from magpylib._lib.classes.magnets import Box from numpy import allclose #Input mag = (2, 3, 5) dim = (2, 2, 2) pos = array([[[2, 2, 2], [2, 2, 2], [2, 2, 3]], [[2, 2, 2], [2, 2, 2], [2, 2, 3]]]) mockResult = [[ [0.24976596, 0.21854521, 0.15610372], [0.24976596, 0.21854521, 0.15610372], [0.12442073, 0.10615358, 0.151319], ], [ [0.24976596, 0.21854521, 0.15610372], [0.24976596, 0.21854521, 0.15610372], [0.12442073, 0.10615358, 0.151319], ]] #Run with pytest.raises(AssertionError): b = Box(mag, dim) b2 = Box(mag, dim) c = Collection(b, b2) result = c.getBsweep(pos, multiprocessing=False) assert isinstance(result, ndarray), type_erMsg assert allclose( result, mockResult ), errMsg #check if the field results are the same as the mock results in the array
def test_GetBSweepList_multiprocessing_many_positions_few_objects(): # Check if getB sweep is performing multipoint # calculations utilizing multiple processes from magpylib._lib.classes.magnets import Box type_erMsg = "getBsweep did not return a numpy array." #Input mag = (2, 3, 5) dim = (2, 2, 2) pos = array([[2, 2, 2], [2, 2, 2], [2, 2, 3], [2, 2, 2], [2, 2, 2], [2, 2, 3]]) mockResult = [ [0.24976596, 0.21854521, 0.15610372], [0.24976596, 0.21854521, 0.15610372], [0.12442073, 0.10615358, 0.151319], [0.24976596, 0.21854521, 0.15610372], [0.24976596, 0.21854521, 0.15610372], [0.12442073, 0.10615358, 0.151319], ] #Run b = Box(mag, dim) b2 = Box(mag, dim) c = Collection(b, b2) result = c.getBsweep(pos, multiprocessing=True) assert isinstance(result, ndarray), type_erMsg rounding = 4 for i in range(len(mockResult)): for j in range(3): assert round(result[i][j], rounding) == round(mockResult[i][j], rounding)
def test_GetBList(): # Check if sole getB throws an error # for Lists. from magpylib._lib.classes.magnets import Box #Input mockList = ( array([0.12488298, 0.10927261, 0.07805186]), array([0.12488298, 0.10927261, 0.07805186]), ) mockResult = [sum(mockList), sum(mockList)] mag = (2, 3, 5) dim = (2, 2, 2) pos = array([[2, 2, 2], [2, 2, 2]]) with pytest.raises(IndexError): #Run b = Box(mag, dim) b2 = Box(mag, dim) c = Collection(b, b2) result = c.getB(pos) rounding = 4 for i in range(len(mockResult)): for j in range(3): assert round(result[i][j], rounding) == round(mockResult[i][j], rounding)
def test_GetBSweepList_multiprocessing(): # Check if getB sweep is performing multipoint # calculations utilizing multiple processes from magpylib._lib.classes.magnets import Box #Input mag=(2,3,5) dim=(2,2,2) pos=array([ [2,2,2], [2,2,2], [2,2,3]]) mockResult = [ [0.24976596, 0.21854521, 0.15610372], [0.24976596, 0.21854521, 0.15610372], [0.12442073, 0.10615358, 0.151319 ],] #Run b = Box(mag,dim) b2 = Box(mag,dim) c = Collection(b,b2) result = c.getBsweep(pos,multiprocessing=True) rounding = 4 for i in range(len(mockResult)): for j in range(3): assert round(result[i][j],rounding) == round(mockResult[i][j],rounding)
def test_GetBSweepList_multiprocessing_many_objects_few_positions(): # Check if getB sweep is performing multipoint # calculations utilizing multiple processes from magpylib._lib.classes.magnets import Box type_erMsg = "getBsweep did not return a numpy array." #Input mag = (2, 3, 5) dim = (2, 2, 2) pos = array([[2, 2, 2], [2, 2, 2], [2, 2, 3]]) numberOfBoxes = 30 mockResult = [ array([0.12488298, 0.10927261, 0.07805186]) * numberOfBoxes, array([0.12488298, 0.10927261, 0.07805186]) * numberOfBoxes, array([0.06221036, 0.05307679, 0.0756595]) * numberOfBoxes, ] #Run c = Collection([Box(mag, dim) for i in range(numberOfBoxes)]) result = c.getBsweep(pos, multiprocessing=True) assert isinstance(result, ndarray), type_erMsg rounding = 3 for i in range(len(mockResult)): for j in range(3): assert round(result[i][j], rounding) == round(mockResult[i][j], rounding)
def test_GetBSweep_displacement_error(): # Check if getBsweep throws an error # if a displacement input is provided. from magpylib._lib.classes.magnets import Box #Input erMsg = "Results from getB are unexpected" type_erMsg = "getBsweep did not return a numpy array." mockResults = array( ([0.00453617, -0.07055326, 0.03153698], [0.00488989, 0.04731373, 0.02416068], [0.0249435, 0.00106315, 0.02894469])) # Input 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 = Box(mag, dim, pos) with pytest.raises(AssertionError): #Run c = Collection(pm) result = c.getBsweep(listOfArgs) assert isinstance(result, ndarray), type_erMsg rounding = 4 for i in range(len(mockResults)): for j in range(3): assert round(result[i][j], rounding) == round(mockResults[i][j], rounding), erMsg
def test_sensorDraw(): # Check if marker inputs are acceptable # TODO: find out a good way to compare plot results. # Have output plot data saved in text maybe # Just test input validity for now. from magpylib._lib.classes.magnets import Box from magpylib._lib.classes.sensor import Sensor b = Box([1, 1, 1], [1, 1, 1], pos=(5, 5, 5)) a = Collection() sensor = Sensor() a.displaySystem(sensors=[sensor], suppress=True) from matplotlib import pyplot
def test_GetB_markerInput(): # Check if marker inputs are acceptable # TODO: find out a good way to compare plot results. # Have output plot data saved in text maybe # Just test input validity for now. from magpylib._lib.classes.magnets import Box b = Box([1,1,1],[1,1,1],pos=(5,5,5)) a = Collection() markers = [[0,0,0], # int [.1,.1,.1], # float b.position] # float64 a.displaySystem(markers,suppress=True)
def test_GetB(): # Check if getB is being called correctly, # getB in collection superimposes (adds) all the fields # generated by the objects in the collection. from magpylib._lib.classes.magnets import Box #Input mockList = (array([0.12488298, 0.10927261, 0.07805186]), array([0.12488298, 0.10927261, 0.07805186])) mockResult = sum(mockList) mag = (2, 3, 5) dim = (2, 2, 2) pos = [2, 2, 2] #Run b = Box(mag, dim) b2 = Box(mag, dim) c = Collection(b, b2) result = c.getB(pos) rounding = 4 for j in range(3): assert round(result[j], rounding) == round(mockResult[j], rounding)
def boxFactory(): mag = (2, 3, 5) dim = (2, 2, 2) return Box(mag, dim)