```python
import magpylib as magpy
from scipy.spatial.transform import Rotation as R
import pyvista as pv
import numpy as np

# Creation of our magnets including place and orientation in the space (all dimensions guessed, need to put in correct ones)
# magnetic polarization of 1.5 T pointing in x-direction 
# Dimensions assumed are 0.5, 1.5 and 3 cm (x,y,z) 


# Question: Is there a more elegant way to do this? Should I make a for-loop for creating our magnet array?

magnet1 = magpy.magnet.Cuboid(polarization=(1.5, 0, 0), dimension=(0.005, 0.015, 0.03)) 
magnet1.position = (0, 0, 0)
magnet1.orientation = R.from_euler("y", 90, degrees=True)

magnet2 = magpy.magnet.Cuboid(polarization=(1.5, 0, 0), dimension=(0.005, 0.015, 0.03)) 
magnet2.position = (0.04, 0, 0)
magnet2.orientation = R.from_euler("y", 90, degrees=True)

magnet3 = magpy.magnet.Cuboid(polarization=(1.5, 0, 0), dimension=(0.005, 0.015, 0.03)) 
magnet3.position = (0, 0.04, 0)
magnet3.orientation = R.from_euler("y", 90, degrees=True)

magnet4 = magpy.magnet.Cuboid(polarization=(1.5, 0, 0), dimension=(0.005, 0.015, 0.03)) 
magnet4.position = (0.04, 0.04, 0)
magnet4.orientation = R.from_euler("y", 90, degrees=True)

magnet5 = magpy.magnet.Cuboid(polarization=(1.5, 0, 0), dimension=(0.005, 0.015, 0.03)) 
magnet5.position = (0, 0.08, 0)
magnet5.orientation = R.from_euler("y", 90, degrees=True)

magnet6 = magpy.magnet.Cuboid(polarization=(1.5, 0, 0), dimension=(0.005, 0.015, 0.03)) 
magnet6.position = (0.04, 0.08, 0)
magnet6.orientation = R.from_euler("y", 90, degrees=True)

magnet7 = magpy.magnet.Cuboid(polarization=(1.5, 0, 0), dimension=(0.005, 0.015, 0.03)) 
magnet7.position = (0, 0.12, 0)
magnet7.orientation = R.from_euler("y", 90, degrees=True)

magnet8 = magpy.magnet.Cuboid(polarization=(1.5, 0, 0), dimension=(0.005, 0.015, 0.03)) 
magnet8.position = (0.04, 0.12, 0)
magnet8.orientation = R.from_euler("y", 90, degrees=True)


# Grouping the magnets to collection
coll = magpy.Collection(magnet1, magnet2, magnet3, magnet4, magnet5, magnet6, magnet7, magnet8)

# Plotting the magnet array
magpy.show(coll, backend="plotly")
```




```python
# Just for visualization: Showing the magnetic field vectors in 3D

spacing = np.array([0.003, 0.003, 0.003]) # defines the grid where the magnetic field is calculated

# The following is for getting the right dimensions and position of the grid
magnets = [magnet1, magnet2, magnet3, magnet4, magnet5, magnet6, magnet7, magnet8]
all_mins = []
all_maxs = []

for m in magnets:
    pos = np.array(m.position)
    dim = np.array(m.dimension) / 2
    all_mins.append(pos - dim)
    all_maxs.append(pos + dim)

global_min = np.min(all_mins, axis=0) - 0.02  # add 2cm as buffer
global_max = np.max(all_maxs, axis=0) + 0.02

dimensions = ((global_max - global_min) / spacing).astype(int)

grid = pv.ImageData(
    spacing=tuple(spacing),
    dimensions=tuple(dimensions),
    origin=(-0.02, -0.02, -0.02),
)

# Calculation of the B-field  in mT
grid["B"] = coll.getB(grid.points) * 1000  
pl = pv.Plotter()
pl.add_mesh(grid.outline(), color="blue", line_width=1)
pl.add_points(grid.points, render_points_as_spheres=True, point_size=2, color='black')

# Add magnetic field vectors as arrows (glyphs)
pl.add_mesh(
    grid.glyph(orient="B", scale=True, factor=0.00001),  # use "B" vectors, scaling according to field strength, factor to make arrows smaller for visibility
    color="blue",
    label="Magnetic field vectors"
)

magpy.show(coll, canvas=pl, units_length="m", backend="pyvista")



pl.show()


```


    Widget(value='<iframe src="http://localhost:40551/index.html?ui=P_0x7d5c280f7fa0_15&reconnect=auto" class="pyv…



```python
# Introducing the aluminium plate

z_position=0.01 # z_position = distance to the magnets (put in real number here!)
plate_center = [0, 0, z_position]  
plate_size = [0.15, 0.40, 0.005]  # [b,l, h]

plate = pv.Box(bounds=(
    plate_center[0] - plate_size[0]/2, plate_center[0] + plate_size[0]/2,
    plate_center[1] - plate_size[1]/2, plate_center[1] + plate_size[1]/2,
    plate_center[2] - plate_size[2]/2, plate_center[2] + plate_size[2]/2
))
pl.add_mesh(plate, color="silver", opacity=0.5, label="Aluminum plate")

pl.show()
```

    A view with name (P_0x7d5c280f7fa0_15) is already registered
     => returning previous one



    Widget(value='<iframe src="http://localhost:40551/index.html?ui=P_0x7d5c280f7fa0_15&reconnect=auto" class="pyv…



```python

```
