Hard magnet tutorial – with AI-based property predictions#

Introduction#

  • This notebook demonstrates how to use the mammos-ai package to predict hard magnet properties using pre-trained machine learning models.

  • This is the same workflow as the hard magnet tutorial but uses AI models to predict hysteresis properties instead of micromagnetic simulations.

Requirements:

%config InlineBackend.figure_format = "retina"


from pprint import pprint

import mammos_ai
import mammos_analysis
import mammos_dft
import mammos_entity as me
import mammos_spindynamics
import mammos_units as u
import matplotlib.pyplot as plt
import numpy as np

# Allow convenient conversions between A/m and T
u.set_enabled_equivalencies(u.magnetic_flux_field());

Load data from DFT and spindynamics databases#

material = "Co2Fe2H4"

results_dft = mammos_dft.db.get_micromagnetic_properties(material)
results_spindynamics = mammos_spindynamics.db.get_spontaneous_magnetization(material)

Calculate micromagnetic intrinsic properties using Kuz’min formula#

We use the Kuzmin equations to get intrinsic parameters Ms, A and K as inputs for the AI model. For each temperature we investigate, there is one triple of these parameters.

results_kuzmin = mammos_analysis.kuzmin_properties(
    T=results_spindynamics.T,
    Ms=results_spindynamics.Ms,
    K1_0=results_dft.Ku_0,
)

The Curie temperature Tc, at which ferromagnetism breaks down, is

results_kuzmin.Tc
CurieTemperature(value=1061.2857713503042, unit=K)

We show how the input parameters Ms, A and K vary as a function of temperature:

results_kuzmin.plot();

AI-based prediction of extrinsic properties#

Our aim now is to compute extrinsic properties remanent magnetization Mr, coercive field Hc and maximum energy product BHmax for multiple temperatures.

Rather than running micromagnetic simulations to compute hysteresis loops and derive extrinsic properties from this, we use pre-trained machine learning models from the mammos-ai package.

Details about the model and its training data can be found in the training repository.

This AI-based approach allows for rapid estimates of hysteresis properties based on the intrinsic micromagnetic parameters computed earlier.

The model was trained using a 50 × 50 × 50 nm cubic grain with uniaxial anisotropy, where both the anisotropy axis and the applied magnetic field were oriented along a cube edge.

We prepare a number of temperatures for which we like predictions of the extrinsic parameters:

T = np.linspace(0, 0.95 * results_kuzmin.Tc.q, 16)

We limit the temperature range to stay below Tc, because above Tc we leave the ferromagnetic domain: the saturation magnetization Ms (and thus also Mr, Hc, and BHmax) are all zero. The AI model has not been trained outside the ferromagnetic domain. We show below how the training data range can be inspected.

For each of the temperatures, we get the input parameters Ms, A and K1 from the Kuzmin model, and ask the AI model to provide a prediction of the output parameters coercive field Hc, magnetic remanence Mr and maximum energy product BHMax:

results_ai = mammos_ai.Hc_Mr_BHmax_from_Ms_A_K(
    Ms=results_kuzmin.Ms(T),
    A=results_kuzmin.A(T),
    K1=results_kuzmin.K1(T),
)

This returns an ExtrinsicProperties object containing the predicted hysteresis properties. This is the same object type as returned by running micromagnetic simulations and then processing them with mammos_analysis.hysteresis.extrinsic_properties in the hard magnet tutorial.

results_ai
ExtrinsicProperties(Hc=Entity(ontology_label='CoercivityHcExternal', value=array([2939667.5 , 2871028.  , 2805837.  , 2500236.8 , 2416813.2 ,
       2232524.  , 2015582.8 , 1921863.2 , 1756569.4 , 1865400.4 ,
       1725963.6 , 1271442.1 ,  953029.94, 1116855.1 , 1159318.4 ,
        829845.4 ], dtype=float32), unit='A / m'), Mr=Entity(ontology_label='Remanence', value=array([1136178.6 , 1129380.2 , 1116109.8 , 1106587.  , 1080187.2 ,
       1074002.5 , 1045093.8 , 1004585.44,  957804.4 ,  856274.1 ,
        797031.5 ,  759612.9 ,  740686.5 ,  618404.6 ,  507746.78,
        375777.38], dtype=float32), unit='A / m'), BHmax=Entity(ontology_label='MaximumEnergyProduct', value=array([405474.25 , 400628.78 , 391265.   , 384600.66 , 366463.16 ,
       362253.8  , 342995.25 , 316899.97 , 288047.66 , 230243.3  ,
       199506.02 , 181179.36 , 172032.86 , 119876.34 ,  80758.94 ,
        44292.496], dtype=float32), unit='J / m3'))

We can visualize the predicted extrinsic properties as a function of temperature; starting with the remanence Mr:

plt.plot(T, results_ai.Mr.value, linestyle="-", marker="o")
plt.xlabel(me.T().axis_label)
plt.ylabel(me.Mr().axis_label);

Comparison with detailed micromagnetic simulations#

To benchmark the predicted values of extrinsic properties, we compare the AI model output with the extrinsic properties obtained through mammos-mumag (FEM micromagnetic simulation tool) hysteresis loop calculations. These values have been computed before (using these scripts) because the execution of the runs providing the data (one hysteresis loop per temperature) takes several hours (using a V100 GPU).

properties_mumag = me.io.entities_from_file("data/hard-magnet-ai-surrogate-model/hystloop_extrinsic_properties.csv")
properties_mumag
EntityCollection(
    T=Entity(ontology_label='ThermodynamicTemperature', value=array([   0.   ,   67.215,  134.43 ,  201.644,  268.859,  336.074,
        403.289,  470.503,  537.718,  604.933,  672.148,  739.362,
        806.577,  873.792,  941.007, 1008.221]), unit='K'),
    Hc=Entity(ontology_label='CoercivityHcExternal', value=array([3063643.78279397, 3010589.35534184, 2904488.80610078,
       2771861.69519724, 2639223.87342486, 2480065.70733037,
       2294391.09244712, 2108708.99907407, 1923020.76053178,
       1710817.87481574, 1498609.73191757, 1286396.02986627,
       1074171.526717  ,  835435.54425978,  596670.60795321,
        304893.99290198]), unit='A / m'),
    Mr=Entity(ontology_label='Remanence', value=array([1137190.81662674, 1127399.79422518, 1109779.84929895,
       1087154.66804918, 1060406.95389496, 1029933.49594663,
        995883.83207295,  958233.97285901,  916797.62840432,
        871204.78336661,  820829.95512118,  764645.7089647 ,
        700898.2368173 ,  626326.06713436,  533749.14702107,
        400506.53310329]), unit='A / m'),
    BHmax=Entity(ontology_label='MaximumEnergyProduct', value=array([406204.20720868, 399250.01489222, 386870.04394259, 371227.48143407,
       353103.26522207, 333002.05146174, 311466.01963998, 288406.23835736,
       263941.70858614, 238190.29322162, 211581.68007052, 183574.27704874,
       154119.94565109, 123148.09889135,  89303.00698477,  50060.61812712]), unit='J / m3'),
)
plt.plot(T, results_ai.Mr.value, linestyle="-", marker="o", label="Mr from AI prediction")
plt.plot(
    properties_mumag.T.value,
    properties_mumag.Mr.value,
    linestyle="--",
    marker="x",
    label="Mr from micromagnetic simulations",
)
plt.xlabel(me.T().axis_label)
plt.ylabel(me.Mr().axis_label)
plt.legend();

We can also see how the coercive field changes with temperature:

plt.plot(T, results_ai.Hc.value, linestyle="-", marker="o", label="Hc from AI prediction")
plt.plot(
    properties_mumag.T.value,
    properties_mumag.Hc.value,
    linestyle="--",
    marker="x",
    label="Hc from micromagnetic simulations",
)
plt.xlabel(me.T().axis_label)
plt.ylabel(me.Hc().axis_label)
plt.legend();

Finally we look at the maximum energy product BHmax.

plt.plot(T, results_ai.BHmax.value, linestyle="-", marker="o", label="BHmax from AI prediction")
plt.plot(
    properties_mumag.T.value,
    properties_mumag.BHmax.value,
    linestyle="--",
    marker="x",
    label="BHmax from micromagnetic simulations",
)
plt.xlabel(me.T().axis_label)
plt.ylabel(me.BHmax().axis_label)
plt.legend();

The AI models show good agreement with micromagnetic simulations for the remanent magnetization (Mr), the coercive field (Hc), and the maximum energy product (BHmax).

In this notebook, we have shown how the mammos-ai package can serve as a fast alternative to full micromagnetic simulations for predicting the hysteresis properties of hard magnets. By using intrinsic micromagnetic parameters as inputs, the model provides rapid estimates that avoid the long runtime of micromagnetic simulations.

It is important to not use the AI model outside its training range (see next section below).

Ranges of validity#

Asking for a coercivity field for a temperature above Tc is a somewhat unphysical question: Ms is zero, and thus Hc will be zero. This is reflected in the AI model not being trained for Ms=0.

We show how the training data range can be queried.

Let us pretend that we want to extend the range of temperatures T2 that we want to study to values above Tc:

T2 = np.linspace(0.95 * results_kuzmin.Tc.q, 1.1 * results_kuzmin.Tc.q, 4)
T2
\[[1008.2215,~1061.2858,~1114.3501,~1167.4143] \; \mathrm{K}\]

We can compute the corresponding values for the magnetization from the Kuzmin model:

Ms2 = results_kuzmin.Ms(T2).value
Ms2
array([402215.74629948,      0.        ,      0.        ,      0.        ])

What is the range of the training data for Ms for the Hc_Mr_BHmax_from_Ms_A_K model? We can find out with the mammos_ai.Hc_Mr_BHmax_from_Ms_A_K_metadata() function:

pprint(mammos_ai.Hc_Mr_BHmax_from_Ms_A_K_metadata()["training_data_range"])
{'A': (Entity(ontology_label='ExchangeStiffnessConstant', value=1e-13, unit='J / m'),
       Entity(ontology_label='ExchangeStiffnessConstant', value=1e-11, unit='J / m')),
 'K': (Entity(ontology_label='UniaxialAnisotropyConstant', value=10000.0, unit='J / m3'),
       Entity(ontology_label='UniaxialAnisotropyConstant', value=10000000.0, unit='J / m3')),
 'Ms': (Entity(ontology_label='SpontaneousMagnetization', value=79580.0, unit='A / m'),
        Entity(ontology_label='SpontaneousMagnetization', value=3980000.0, unit='A / m'))}

From this, we can see Ms=0 A/m is outside the training range, and thus no sensible prediction are possible.

If we, nevertheless, attempt to use the model outside its valid range, it produces unpredictable results. For example:

results_ai2 = mammos_ai.Hc_Mr_BHmax_from_Ms_A_K(
    Ms=results_kuzmin.Ms(T2),
    A=results_kuzmin.A(T2),
    K1=results_kuzmin.K1(T2),
)
results_ai2.BHmax
MaximumEnergyProduct(value=[ 44292.582 350828.28  350828.28  350828.28 ], unit=J / m3)