This script performs passive magnetic shimming to improve the homogeneity of a magnetic field. Below is a breakdown of the code:
- The script loads magnetic field data from a
.npyfile. - Converts the field and position data into meters and filters the data within a defined spherical region (DSV - Diameter Spherical Volume).
- The magnetic field data is visualized in 3D scatter plots.
- Specifies the geometry of the shim array, including magnet dimensions, diameter, and magnetization.
- Creates shim ring templates using the
make_shim_ring_templatefunction. - Saves the initial shim tray arrangement as an STL file for visualization.
- Computes the magnetic field generated by the shim tray template.
- Visualizes the computed field and its combination with the measured field.
- Defines an optimization problem using the
shimming_problemfunction to minimize field inhomogeneity. - Uses the
MixedVariableGAalgorithm from thepymoolibrary to solve the optimization problem. - Outputs the optimized shim tray configuration.
- Creates an optimized shim tray based on the optimization results.
- Saves the optimized shim tray as an STL file and visualizes the shimmed magnetic field.
- Saves the optimized shim tray configuration to a
.pklfile. - Demonstrates how to reload and visualize the saved shim tray.
magpylib: Used for magnetic field computations and visualization.pymoo: Provides optimization algorithms for solving the shimming problem.- Custom Functions:
get_field_pos,filter_dsv: Process and filter magnetic field data.make_shim_ring_template: Creates shim tray templates.cost_fn: Evaluates the field homogeneity.write2stl: Exports shim tray configurations to STL files.
- Visualizations of the magnetic field before and after shimming.
- STL files for initial and optimized shim tray configurations.
- A
.pklfile containing the optimized shim tray for future use.
This script automates the process of designing and optimizing passive shim trays for magnetic field correction.