{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 26,
   "id": "88f4bdb3",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "application/vnd.plotly.v1+json": {
       "config": {
        "plotlyServerURL": "https://plot.ly"
       },
       "data": [
        {
         "color": "#2e91e5",
         "colorscale": [
          [
           0,
           "#00b050"
          ],
          [
           0.16,
           "#00b050"
          ],
          [
           0.26,
           "#dddddd"
          ],
          [
           0.74,
           "#dddddd"
          ],
          [
           0.8400000000000001,
           "#e71111"
          ],
          [
           1,
           "#e71111"
          ]
         ],
         "i": {
          "bdata": "BwAAAAQEAgYEAAMHDwgICAwMCg4MCAsPFxAQEBQUEhYUEBMXHxgYGBwcGh4cGBsfJyAgICQkIiYkICMnLygoKCwsKi4sKCsvNzAwMDQ0MjY0MDM3Pzg4ODw8Oj48ODs/",
          "dtype": "i1"
         },
         "intensity": {
          "bdata": "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAPA/AAAAAAAA8D8AAAAAAAAAAAAAAAAAAAAAAAAAAAAA8D8AAAAAAADwPwAAAAAAAAAAAAAAAAAAAAAAAAAAAADwPwAAAAAAAPA/AAAAAAAAAAAAAAAAAAAAAAAAAAAAAPA/AAAAAAAA8D8AAAAAAAAAAAAAAAAAAAAAAAAAAAAA8D8AAAAAAADwPwAAAAAAAAAAAAAAAAAAAAAAAAAAAADwPwAAAAAAAPA/AAAAAAAAAAAAAAAAAAAAAAAAAAAAAPA/AAAAAAAA8D8AAAAAAAAAAAAAAAAAAAAAAAAAAAAA8D8AAAAAAADwPwAAAAAAAAAAAAAAAAAAAAAAAAAAAADwPwAAAAAAAPA/AAAAAAAAAAAAAAAAAAAAAAAAAAAAAPA/AAAAAAAA8D8AAAAAAAAAAAAAAAAAAAAAAAAAAAAA8D8AAAAAAADwPwAAAAAAAAAAAAAAAAAAAAAAAAAAAADwPwAAAAAAAPA/AAAAAAAAAAAAAAAAAAAAAAAAAAAAAPA/AAAAAAAA8D8AAAAAAAAAAAAAAAAAAAAAAAAAAAAA8D8AAAAAAADwPwAAAAAAAAAAAAAAAAAAAAAAAAAAAADwPwAAAAAAAPA/AAAAAAAAAAAAAAAAAAAAAAAAAAAAAPA/AAAAAAAA8D8=",
          "dtype": "f8"
         },
         "j": {
          "bdata": "AAcBAgYHAQIFBQICCA8JCg4PCQoNDQoKEBcREhYXERIVFRISGB8ZGh4fGRodHRoaICchIiYnISIlJSIiKC8pKi4vKSotLSoqMDcxMjY3MTI1NTIyOD85Oj4/OTo9PTo6",
          "dtype": "i1"
         },
         "k": {
          "bdata": "AwQCAwUGBQUAAQcGCwwKCw0ODQ0ICQ8OExQSExUWFRUQERcWGxwaGx0eHR0YGR8eIyQiIyUmJSUgIScmKywqKy0uLS0oKS8uMzQyMzU2NTUwMTc2Ozw6Oz0+PT04OT8+",
          "dtype": "i1"
         },
         "legendgroup": "Collection(id=137832074478496)",
         "name": "Collection (8 sources)",
         "opacity": 1,
         "showlegend": true,
         "showscale": false,
         "type": "mesh3d",
         "x": {
          "bdata": "uB6F61G4jr+4HoXrUbiOv7gehetRuI6/uB6F61G4jr+4HoXrUbiOP7gehetRuI4/uB6F61G4jj+4HoXrUbiOP5qZmZmZmZk/mpmZmZmZmT+amZmZmZmZP5qZmZmZmZk/KVyPwvUorD8pXI/C9SisPylcj8L1KKw/KVyPwvUorD+4HoXrUbiOv7gehetRuI6/uB6F61G4jr+4HoXrUbiOv7gehetRuI4/uB6F61G4jj+4HoXrUbiOP7gehetRuI4/mpmZmZmZmT+amZmZmZmZP5qZmZmZmZk/mpmZmZmZmT8pXI/C9SisPylcj8L1KKw/KVyPwvUorD8pXI/C9SisP7gehetRuI6/uB6F61G4jr+4HoXrUbiOv7gehetRuI6/uB6F61G4jj+4HoXrUbiOP7gehetRuI4/uB6F61G4jj+amZmZmZmZP5qZmZmZmZk/mpmZmZmZmT+amZmZmZmZPylcj8L1KKw/KVyPwvUorD8pXI/C9SisPylcj8L1KKw/uB6F61G4jr+4HoXrUbiOv7gehetRuI6/uB6F61G4jr+4HoXrUbiOP7gehetRuI4/uB6F61G4jj+4HoXrUbiOP5qZmZmZmZk/mpmZmZmZmT+amZmZmZmZP5qZmZmZmZk/KVyPwvUorD8pXI/C9SisPylcj8L1KKw/KVyPwvUorD8=",
          "dtype": "f8"
         },
         "y": {
          "bdata": "uB6F61G4fr+4HoXrUbh+P7gehetRuH4/uB6F61G4fr+4HoXrUbh+v7gehetRuH4/uB6F61G4fj+4HoXrUbh+v7gehetRuH6/uB6F61G4fj+4HoXrUbh+P7gehetRuH6/uB6F61G4fr+4HoXrUbh+P7gehetRuH4/uB6F61G4fr+kcD0K16OgP1K4HoXrUag/UrgehetRqD+kcD0K16OgP6RwPQrXo6A/UrgehetRqD9SuB6F61GoP6RwPQrXo6A/pHA9CtejoD9SuB6F61GoP1K4HoXrUag/pHA9CtejoD+kcD0K16OgP1K4HoXrUag/UrgehetRqD+kcD0K16OgP5DC9Shcj7I/ZmZmZmZmtj9mZmZmZma2P5DC9Shcj7I/kML1KFyPsj9mZmZmZma2P2ZmZmZmZrY/kML1KFyPsj+QwvUoXI+yP2ZmZmZmZrY/ZmZmZmZmtj+QwvUoXI+yP5DC9Shcj7I/ZmZmZmZmtj9mZmZmZma2P5DC9Shcj7I/zMzMzMzMvD9SuB6F61HAP1K4HoXrUcA/zMzMzMzMvD/MzMzMzMy8P1K4HoXrUcA/UrgehetRwD/MzMzMzMy8P8zMzMzMzLw/UrgehetRwD9SuB6F61HAP8zMzMzMzLw/zMzMzMzMvD9SuB6F61HAP1K4HoXrUcA/zMzMzMzMvD8=",
          "dtype": "f8"
         },
         "z": {
          "bdata": "cxSuR+F6ZD9zFK5H4XpkP4MUrkfhemS/gxSuR+F6ZL+DFK5H4XpkP4MUrkfhemQ/cxSuR+F6ZL9zFK5H4Xpkv3MUrkfhemQ/cxSuR+F6ZD+DFK5H4Xpkv4MUrkfhemS/gxSuR+F6ZD+DFK5H4XpkP3MUrkfhemS/cxSuR+F6ZL9zFK5H4XpkP3MUrkfhemQ/gxSuR+F6ZL+DFK5H4Xpkv4MUrkfhemQ/gxSuR+F6ZD9zFK5H4Xpkv3MUrkfhemS/cxSuR+F6ZD9zFK5H4XpkP4MUrkfhemS/gxSuR+F6ZL+DFK5H4XpkP4MUrkfhemQ/cxSuR+F6ZL9zFK5H4Xpkv3MUrkfhemQ/cxSuR+F6ZD+DFK5H4Xpkv4MUrkfhemS/gxSuR+F6ZD+DFK5H4XpkP3MUrkfhemS/cxSuR+F6ZL9zFK5H4XpkP3MUrkfhemQ/gxSuR+F6ZL+DFK5H4Xpkv4MUrkfhemQ/gxSuR+F6ZD9zFK5H4Xpkv3MUrkfhemS/cxSuR+F6ZD9zFK5H4XpkP4MUrkfhemS/gxSuR+F6ZL+DFK5H4XpkP4MUrkfhemQ/cxSuR+F6ZL9zFK5H4Xpkv3MUrkfhemQ/cxSuR+F6ZD+DFK5H4Xpkv4MUrkfhemS/gxSuR+F6ZD+DFK5H4XpkP3MUrkfhemS/cxSuR+F6ZL8=",
          "dtype": "f8"
         }
        }
       ],
       "layout": {
        "legend": {
         "itemsizing": "constant"
        },
        "scene": {
         "aspectmode": "manual",
         "aspectratio": {
          "x": 1,
          "y": 1,
          "z": 1
         },
         "camera": {
          "eye": {
           "x": 1,
           "y": -1.5,
           "z": 1.4
          }
         },
         "xaxis": {
          "autorange": false,
          "range": [
           -0.0475,
           0.08750000000000001
          ],
          "title": {
           "text": "x (m)"
          }
         },
         "yaxis": {
          "autorange": false,
          "range": [
           -0.007500000000000007,
           0.1275
          ],
          "title": {
           "text": "y (m)"
          }
         },
         "zaxis": {
          "autorange": false,
          "range": [
           -0.0675,
           0.0675
          ],
          "title": {
           "text": "z (m)"
          }
         }
        },
        "template": {
         "data": {
          "bar": [
           {
            "error_x": {
             "color": "#2a3f5f"
            },
            "error_y": {
             "color": "#2a3f5f"
            },
            "marker": {
             "line": {
              "color": "#E5ECF6",
              "width": 0.5
             },
             "pattern": {
              "fillmode": "overlay",
              "size": 10,
              "solidity": 0.2
             }
            },
            "type": "bar"
           }
          ],
          "barpolar": [
           {
            "marker": {
             "line": {
              "color": "#E5ECF6",
              "width": 0.5
             },
             "pattern": {
              "fillmode": "overlay",
              "size": 10,
              "solidity": 0.2
             }
            },
            "type": "barpolar"
           }
          ],
          "carpet": [
           {
            "aaxis": {
             "endlinecolor": "#2a3f5f",
             "gridcolor": "white",
             "linecolor": "white",
             "minorgridcolor": "white",
             "startlinecolor": "#2a3f5f"
            },
            "baxis": {
             "endlinecolor": "#2a3f5f",
             "gridcolor": "white",
             "linecolor": "white",
             "minorgridcolor": "white",
             "startlinecolor": "#2a3f5f"
            },
            "type": "carpet"
           }
          ],
          "choropleth": [
           {
            "colorbar": {
             "outlinewidth": 0,
             "ticks": ""
            },
            "type": "choropleth"
           }
          ],
          "contour": [
           {
            "colorbar": {
             "outlinewidth": 0,
             "ticks": ""
            },
            "colorscale": [
             [
              0,
              "#0d0887"
             ],
             [
              0.1111111111111111,
              "#46039f"
             ],
             [
              0.2222222222222222,
              "#7201a8"
             ],
             [
              0.3333333333333333,
              "#9c179e"
             ],
             [
              0.4444444444444444,
              "#bd3786"
             ],
             [
              0.5555555555555556,
              "#d8576b"
             ],
             [
              0.6666666666666666,
              "#ed7953"
             ],
             [
              0.7777777777777778,
              "#fb9f3a"
             ],
             [
              0.8888888888888888,
              "#fdca26"
             ],
             [
              1,
              "#f0f921"
             ]
            ],
            "type": "contour"
           }
          ],
          "contourcarpet": [
           {
            "colorbar": {
             "outlinewidth": 0,
             "ticks": ""
            },
            "type": "contourcarpet"
           }
          ],
          "heatmap": [
           {
            "colorbar": {
             "outlinewidth": 0,
             "ticks": ""
            },
            "colorscale": [
             [
              0,
              "#0d0887"
             ],
             [
              0.1111111111111111,
              "#46039f"
             ],
             [
              0.2222222222222222,
              "#7201a8"
             ],
             [
              0.3333333333333333,
              "#9c179e"
             ],
             [
              0.4444444444444444,
              "#bd3786"
             ],
             [
              0.5555555555555556,
              "#d8576b"
             ],
             [
              0.6666666666666666,
              "#ed7953"
             ],
             [
              0.7777777777777778,
              "#fb9f3a"
             ],
             [
              0.8888888888888888,
              "#fdca26"
             ],
             [
              1,
              "#f0f921"
             ]
            ],
            "type": "heatmap"
           }
          ],
          "histogram": [
           {
            "marker": {
             "pattern": {
              "fillmode": "overlay",
              "size": 10,
              "solidity": 0.2
             }
            },
            "type": "histogram"
           }
          ],
          "histogram2d": [
           {
            "colorbar": {
             "outlinewidth": 0,
             "ticks": ""
            },
            "colorscale": [
             [
              0,
              "#0d0887"
             ],
             [
              0.1111111111111111,
              "#46039f"
             ],
             [
              0.2222222222222222,
              "#7201a8"
             ],
             [
              0.3333333333333333,
              "#9c179e"
             ],
             [
              0.4444444444444444,
              "#bd3786"
             ],
             [
              0.5555555555555556,
              "#d8576b"
             ],
             [
              0.6666666666666666,
              "#ed7953"
             ],
             [
              0.7777777777777778,
              "#fb9f3a"
             ],
             [
              0.8888888888888888,
              "#fdca26"
             ],
             [
              1,
              "#f0f921"
             ]
            ],
            "type": "histogram2d"
           }
          ],
          "histogram2dcontour": [
           {
            "colorbar": {
             "outlinewidth": 0,
             "ticks": ""
            },
            "colorscale": [
             [
              0,
              "#0d0887"
             ],
             [
              0.1111111111111111,
              "#46039f"
             ],
             [
              0.2222222222222222,
              "#7201a8"
             ],
             [
              0.3333333333333333,
              "#9c179e"
             ],
             [
              0.4444444444444444,
              "#bd3786"
             ],
             [
              0.5555555555555556,
              "#d8576b"
             ],
             [
              0.6666666666666666,
              "#ed7953"
             ],
             [
              0.7777777777777778,
              "#fb9f3a"
             ],
             [
              0.8888888888888888,
              "#fdca26"
             ],
             [
              1,
              "#f0f921"
             ]
            ],
            "type": "histogram2dcontour"
           }
          ],
          "mesh3d": [
           {
            "colorbar": {
             "outlinewidth": 0,
             "ticks": ""
            },
            "type": "mesh3d"
           }
          ],
          "parcoords": [
           {
            "line": {
             "colorbar": {
              "outlinewidth": 0,
              "ticks": ""
             }
            },
            "type": "parcoords"
           }
          ],
          "pie": [
           {
            "automargin": true,
            "type": "pie"
           }
          ],
          "scatter": [
           {
            "fillpattern": {
             "fillmode": "overlay",
             "size": 10,
             "solidity": 0.2
            },
            "type": "scatter"
           }
          ],
          "scatter3d": [
           {
            "line": {
             "colorbar": {
              "outlinewidth": 0,
              "ticks": ""
             }
            },
            "marker": {
             "colorbar": {
              "outlinewidth": 0,
              "ticks": ""
             }
            },
            "type": "scatter3d"
           }
          ],
          "scattercarpet": [
           {
            "marker": {
             "colorbar": {
              "outlinewidth": 0,
              "ticks": ""
             }
            },
            "type": "scattercarpet"
           }
          ],
          "scattergeo": [
           {
            "marker": {
             "colorbar": {
              "outlinewidth": 0,
              "ticks": ""
             }
            },
            "type": "scattergeo"
           }
          ],
          "scattergl": [
           {
            "marker": {
             "colorbar": {
              "outlinewidth": 0,
              "ticks": ""
             }
            },
            "type": "scattergl"
           }
          ],
          "scattermap": [
           {
            "marker": {
             "colorbar": {
              "outlinewidth": 0,
              "ticks": ""
             }
            },
            "type": "scattermap"
           }
          ],
          "scattermapbox": [
           {
            "marker": {
             "colorbar": {
              "outlinewidth": 0,
              "ticks": ""
             }
            },
            "type": "scattermapbox"
           }
          ],
          "scatterpolar": [
           {
            "marker": {
             "colorbar": {
              "outlinewidth": 0,
              "ticks": ""
             }
            },
            "type": "scatterpolar"
           }
          ],
          "scatterpolargl": [
           {
            "marker": {
             "colorbar": {
              "outlinewidth": 0,
              "ticks": ""
             }
            },
            "type": "scatterpolargl"
           }
          ],
          "scatterternary": [
           {
            "marker": {
             "colorbar": {
              "outlinewidth": 0,
              "ticks": ""
             }
            },
            "type": "scatterternary"
           }
          ],
          "surface": [
           {
            "colorbar": {
             "outlinewidth": 0,
             "ticks": ""
            },
            "colorscale": [
             [
              0,
              "#0d0887"
             ],
             [
              0.1111111111111111,
              "#46039f"
             ],
             [
              0.2222222222222222,
              "#7201a8"
             ],
             [
              0.3333333333333333,
              "#9c179e"
             ],
             [
              0.4444444444444444,
              "#bd3786"
             ],
             [
              0.5555555555555556,
              "#d8576b"
             ],
             [
              0.6666666666666666,
              "#ed7953"
             ],
             [
              0.7777777777777778,
              "#fb9f3a"
             ],
             [
              0.8888888888888888,
              "#fdca26"
             ],
             [
              1,
              "#f0f921"
             ]
            ],
            "type": "surface"
           }
          ],
          "table": [
           {
            "cells": {
             "fill": {
              "color": "#EBF0F8"
             },
             "line": {
              "color": "white"
             }
            },
            "header": {
             "fill": {
              "color": "#C8D4E3"
             },
             "line": {
              "color": "white"
             }
            },
            "type": "table"
           }
          ]
         },
         "layout": {
          "annotationdefaults": {
           "arrowcolor": "#2a3f5f",
           "arrowhead": 0,
           "arrowwidth": 1
          },
          "autotypenumbers": "strict",
          "coloraxis": {
           "colorbar": {
            "outlinewidth": 0,
            "ticks": ""
           }
          },
          "colorscale": {
           "diverging": [
            [
             0,
             "#8e0152"
            ],
            [
             0.1,
             "#c51b7d"
            ],
            [
             0.2,
             "#de77ae"
            ],
            [
             0.3,
             "#f1b6da"
            ],
            [
             0.4,
             "#fde0ef"
            ],
            [
             0.5,
             "#f7f7f7"
            ],
            [
             0.6,
             "#e6f5d0"
            ],
            [
             0.7,
             "#b8e186"
            ],
            [
             0.8,
             "#7fbc41"
            ],
            [
             0.9,
             "#4d9221"
            ],
            [
             1,
             "#276419"
            ]
           ],
           "sequential": [
            [
             0,
             "#0d0887"
            ],
            [
             0.1111111111111111,
             "#46039f"
            ],
            [
             0.2222222222222222,
             "#7201a8"
            ],
            [
             0.3333333333333333,
             "#9c179e"
            ],
            [
             0.4444444444444444,
             "#bd3786"
            ],
            [
             0.5555555555555556,
             "#d8576b"
            ],
            [
             0.6666666666666666,
             "#ed7953"
            ],
            [
             0.7777777777777778,
             "#fb9f3a"
            ],
            [
             0.8888888888888888,
             "#fdca26"
            ],
            [
             1,
             "#f0f921"
            ]
           ],
           "sequentialminus": [
            [
             0,
             "#0d0887"
            ],
            [
             0.1111111111111111,
             "#46039f"
            ],
            [
             0.2222222222222222,
             "#7201a8"
            ],
            [
             0.3333333333333333,
             "#9c179e"
            ],
            [
             0.4444444444444444,
             "#bd3786"
            ],
            [
             0.5555555555555556,
             "#d8576b"
            ],
            [
             0.6666666666666666,
             "#ed7953"
            ],
            [
             0.7777777777777778,
             "#fb9f3a"
            ],
            [
             0.8888888888888888,
             "#fdca26"
            ],
            [
             1,
             "#f0f921"
            ]
           ]
          },
          "colorway": [
           "#636efa",
           "#EF553B",
           "#00cc96",
           "#ab63fa",
           "#FFA15A",
           "#19d3f3",
           "#FF6692",
           "#B6E880",
           "#FF97FF",
           "#FECB52"
          ],
          "font": {
           "color": "#2a3f5f"
          },
          "geo": {
           "bgcolor": "white",
           "lakecolor": "white",
           "landcolor": "#E5ECF6",
           "showlakes": true,
           "showland": true,
           "subunitcolor": "white"
          },
          "hoverlabel": {
           "align": "left"
          },
          "hovermode": "closest",
          "mapbox": {
           "style": "light"
          },
          "paper_bgcolor": "white",
          "plot_bgcolor": "#E5ECF6",
          "polar": {
           "angularaxis": {
            "gridcolor": "white",
            "linecolor": "white",
            "ticks": ""
           },
           "bgcolor": "#E5ECF6",
           "radialaxis": {
            "gridcolor": "white",
            "linecolor": "white",
            "ticks": ""
           }
          },
          "scene": {
           "xaxis": {
            "backgroundcolor": "#E5ECF6",
            "gridcolor": "white",
            "gridwidth": 2,
            "linecolor": "white",
            "showbackground": true,
            "ticks": "",
            "zerolinecolor": "white"
           },
           "yaxis": {
            "backgroundcolor": "#E5ECF6",
            "gridcolor": "white",
            "gridwidth": 2,
            "linecolor": "white",
            "showbackground": true,
            "ticks": "",
            "zerolinecolor": "white"
           },
           "zaxis": {
            "backgroundcolor": "#E5ECF6",
            "gridcolor": "white",
            "gridwidth": 2,
            "linecolor": "white",
            "showbackground": true,
            "ticks": "",
            "zerolinecolor": "white"
           }
          },
          "shapedefaults": {
           "line": {
            "color": "#2a3f5f"
           }
          },
          "ternary": {
           "aaxis": {
            "gridcolor": "white",
            "linecolor": "white",
            "ticks": ""
           },
           "baxis": {
            "gridcolor": "white",
            "linecolor": "white",
            "ticks": ""
           },
           "bgcolor": "#E5ECF6",
           "caxis": {
            "gridcolor": "white",
            "linecolor": "white",
            "ticks": ""
           }
          },
          "title": {
           "x": 0.05
          },
          "xaxis": {
           "automargin": true,
           "gridcolor": "white",
           "linecolor": "white",
           "ticks": "",
           "title": {
            "standoff": 15
           },
           "zerolinecolor": "white",
           "zerolinewidth": 2
          },
          "yaxis": {
           "automargin": true,
           "gridcolor": "white",
           "linecolor": "white",
           "ticks": "",
           "title": {
            "standoff": 15
           },
           "zerolinecolor": "white",
           "zerolinewidth": 2
          }
         }
        }
       }
      }
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "import magpylib as magpy\n",
    "from scipy.spatial.transform import Rotation as R\n",
    "import pyvista as pv\n",
    "import numpy as np\n",
    "\n",
    "# Creation of our magnets including place and orientation in the space (all dimensions guessed, need to put in correct ones)\n",
    "# magnetic polarization of 1.5 T pointing in x-direction \n",
    "# Dimensions assumed are 0.5, 1.5 and 3 cm (x,y,z) \n",
    "\n",
    "\n",
    "# Question: Is there a more elegant way to do this? Should I make a for-loop for creating our magnet array?\n",
    "\n",
    "magnet1 = magpy.magnet.Cuboid(polarization=(1.5, 0, 0), dimension=(0.005, 0.015, 0.03)) \n",
    "magnet1.position = (0, 0, 0)\n",
    "magnet1.orientation = R.from_euler(\"y\", 90, degrees=True)\n",
    "\n",
    "magnet2 = magpy.magnet.Cuboid(polarization=(1.5, 0, 0), dimension=(0.005, 0.015, 0.03)) \n",
    "magnet2.position = (0.04, 0, 0)\n",
    "magnet2.orientation = R.from_euler(\"y\", 90, degrees=True)\n",
    "\n",
    "magnet3 = magpy.magnet.Cuboid(polarization=(1.5, 0, 0), dimension=(0.005, 0.015, 0.03)) \n",
    "magnet3.position = (0, 0.04, 0)\n",
    "magnet3.orientation = R.from_euler(\"y\", 90, degrees=True)\n",
    "\n",
    "magnet4 = magpy.magnet.Cuboid(polarization=(1.5, 0, 0), dimension=(0.005, 0.015, 0.03)) \n",
    "magnet4.position = (0.04, 0.04, 0)\n",
    "magnet4.orientation = R.from_euler(\"y\", 90, degrees=True)\n",
    "\n",
    "magnet5 = magpy.magnet.Cuboid(polarization=(1.5, 0, 0), dimension=(0.005, 0.015, 0.03)) \n",
    "magnet5.position = (0, 0.08, 0)\n",
    "magnet5.orientation = R.from_euler(\"y\", 90, degrees=True)\n",
    "\n",
    "magnet6 = magpy.magnet.Cuboid(polarization=(1.5, 0, 0), dimension=(0.005, 0.015, 0.03)) \n",
    "magnet6.position = (0.04, 0.08, 0)\n",
    "magnet6.orientation = R.from_euler(\"y\", 90, degrees=True)\n",
    "\n",
    "magnet7 = magpy.magnet.Cuboid(polarization=(1.5, 0, 0), dimension=(0.005, 0.015, 0.03)) \n",
    "magnet7.position = (0, 0.12, 0)\n",
    "magnet7.orientation = R.from_euler(\"y\", 90, degrees=True)\n",
    "\n",
    "magnet8 = magpy.magnet.Cuboid(polarization=(1.5, 0, 0), dimension=(0.005, 0.015, 0.03)) \n",
    "magnet8.position = (0.04, 0.12, 0)\n",
    "magnet8.orientation = R.from_euler(\"y\", 90, degrees=True)\n",
    "\n",
    "\n",
    "# Grouping the magnets to collection\n",
    "coll = magpy.Collection(magnet1, magnet2, magnet3, magnet4, magnet5, magnet6, magnet7, magnet8)\n",
    "\n",
    "# Plotting the magnet array\n",
    "magpy.show(coll, backend=\"plotly\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "70a4312d",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "application/vnd.jupyter.widget-view+json": {
       "model_id": "31021d1f168d4c86a26e512b68ff75d1",
       "version_major": 2,
       "version_minor": 0
      },
      "text/plain": [
       "Widget(value='<iframe src=\"http://localhost:40551/index.html?ui=P_0x7d5c280f7fa0_15&reconnect=auto\" class=\"pyv…"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "# Just for visualization: Showing the magnetic field vectors in 3D\n",
    "\n",
    "spacing = np.array([0.003, 0.003, 0.003]) # defines the grid where the magnetic field is calculated\n",
    "\n",
    "# The following is for getting the right dimensions and position of the grid\n",
    "magnets = [magnet1, magnet2, magnet3, magnet4, magnet5, magnet6, magnet7, magnet8]\n",
    "all_mins = []\n",
    "all_maxs = []\n",
    "\n",
    "for m in magnets:\n",
    "    pos = np.array(m.position)\n",
    "    dim = np.array(m.dimension) / 2\n",
    "    all_mins.append(pos - dim)\n",
    "    all_maxs.append(pos + dim)\n",
    "\n",
    "global_min = np.min(all_mins, axis=0) - 0.02  # add 2cm as buffer\n",
    "global_max = np.max(all_maxs, axis=0) + 0.02\n",
    "\n",
    "dimensions = ((global_max - global_min) / spacing).astype(int)\n",
    "\n",
    "grid = pv.ImageData(\n",
    "    spacing=tuple(spacing),\n",
    "    dimensions=tuple(dimensions),\n",
    "    origin=(-0.02, -0.02, -0.02),\n",
    ")\n",
    "\n",
    "# Calculation of the B-field  in mT\n",
    "grid[\"B\"] = coll.getB(grid.points) * 1000  \n",
    "pl = pv.Plotter()\n",
    "pl.add_mesh(grid.outline(), color=\"blue\", line_width=1)\n",
    "pl.add_points(grid.points, render_points_as_spheres=True, point_size=2, color='black')\n",
    "\n",
    "# Add magnetic field vectors as arrows (glyphs)\n",
    "pl.add_mesh(\n",
    "    grid.glyph(orient=\"B\", scale=True, factor=0.00001),  # use \"B\" vectors, scaling according to field strength, factor to make arrows smaller for visibility\n",
    "    color=\"blue\",\n",
    "    label=\"Magnetic field vectors\"\n",
    ")\n",
    "\n",
    "magpy.show(coll, canvas=pl, units_length=\"m\", backend=\"pyvista\")\n",
    "\n",
    "\n",
    "\n",
    "pl.show()\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 30,
   "id": "656cf75f",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "A view with name (P_0x7d5c280f7fa0_15) is already registered\n",
      " => returning previous one\n"
     ]
    },
    {
     "data": {
      "application/vnd.jupyter.widget-view+json": {
       "model_id": "5126e1a148de4768b6db9efe2411d7f1",
       "version_major": 2,
       "version_minor": 0
      },
      "text/plain": [
       "Widget(value='<iframe src=\"http://localhost:40551/index.html?ui=P_0x7d5c280f7fa0_15&reconnect=auto\" class=\"pyv…"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "# Introducing the aluminium plate\n",
    "\n",
    "z_position=0.01 # z_position = distance to the magnets (put in real number here!)\n",
    "plate_center = [0, 0, z_position]  \n",
    "plate_size = [0.15, 0.40, 0.005]  # [b,l, h]\n",
    "\n",
    "plate = pv.Box(bounds=(\n",
    "    plate_center[0] - plate_size[0]/2, plate_center[0] + plate_size[0]/2,\n",
    "    plate_center[1] - plate_size[1]/2, plate_center[1] + plate_size[1]/2,\n",
    "    plate_center[2] - plate_size[2]/2, plate_center[2] + plate_size[2]/2\n",
    "))\n",
    "pl.add_mesh(plate, color=\"silver\", opacity=0.5, label=\"Aluminum plate\")\n",
    "\n",
    "pl.show()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "9b52c4ba",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": ".venv",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.10.12"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
