This jupyter-notebook has be created with ‘jupytext’.
Use this Link to directly open the Notebook in Google Colab.
Install magnum.np and fetch reference data (required for Colab)
[1]:
!pip install -q triton magnumnp
from os import path
if not path.isdir("ref"):
!mkdir ref
!wget -P ref https://gitlab.com/magnum.np/magnum.np/raw/main/demos/slonczewski2/ref/m.dat &> /dev/null
WARNING: Running pip as the 'root' user can result in broken permissions and conflicting behaviour with the system package manager, possibly rendering your system unusable. It is recommended to use a virtual environment instead: https://pip.pypa.io/warnings/venv. Use the --root-user-action option if you know what you are doing and want to suppress this warning.
Slonczewski STT Demo 2
Example 2 taken from the Mumax3 Website (https://mumax.github.io/examples.html) Flipping the magnetization of a elliptical nanodisk with Slonczewski STT. Implemented by Jed Cheng (jed.cheng@mag.ed.kyushu-u.ac.jp)
Run Simulation
[2]:
from magnumnp import *
import torch
import numpy as np
import pathlib
from tqdm import tqdm
set_log_level(25) # show info_green, but hide info_blue
Timer.enable()
try:
this_dir = pathlib.Path(__file__).resolve().parent
except:
this_dir = pathlib.Path().resolve()
# initialize state
dt = 5e-12
n = (64, 32, 1)
dx = (2.5e-9, 2.5e-9, 5e-9)
L = (n[0]*dx[0], n[1]*dx[1], n[2]*dx[2])
origin = (-L[0]/2., -L[1]/2., -L[2]/2.)
mesh = Mesh(n, dx, origin = origin)
state = State(mesh)
state.material = {
"Ms": 8e5,
"A": 1.3e-11,
"alpha": 0.01,
"P": 0.5669,
"Lambda": 1,
"epsilon_prime": 0,
"mp": [np.cos(np.radians(20)), np.sin(np.radians(20)), 0],
"d": L[2],
"J": -8e11,
}
x, y, z = mesh.SpatialCoordinate()
disk = (x/80e-9)**2 + (y/40e-9)**2 < 1
write_vti(disk, "data/domain.vti")
state.m = state.Constant([0.,0.,0.])
state.m[disk] = torch.tensor([1.,0.,0.])
# initialize field terms
demag = DemagField()
exchange = ExchangeField()
torque = SpinTorqueSlonczewski()
# perform integration with spin torque
m_disk = lambda state: state.m[disk] # only consider disk region
llg = LLGSolver([demag, exchange, torque])
logger = Logger(this_dir / "data", ['t', 'm', m_disk], ["m"], fields_every = 10)
for i in tqdm(torch.arange(0, 1e-9, dt)):
llg.step(state, dt)
logger << state
Timer.print_report()
2025-04-02 11:30:45 magnum.np:INFO magnum.np 2.0.2 (/usr/local/lib/python3.10/site-packages/ipykernel_launcher.py -f /tmp/tmp8wew4dz_.json --HistoryManager.hist_file=:memory:)
2025-04-02 11:30:45 magnum.np:INFO [State] running on device: cpu (dtype = float64)
2025-04-02 11:30:45 magnum.np:INFO [Mesh] 64x32x1 (dx= 2.5e-09 x 2.5e-09 x 5e-09)
2025-04-02 11:30:45 magnum.np:INFO [LLGSolver] using RKF45 solver (atol = 1e-05)
100%|██████████| 201/201 [00:32<00:00, 6.11it/s]
2025-04-02 11:31:18 magnum.np: =============================================================================
2025-04-02 11:31:18 magnum.np: TIMER REPORT
2025-04-02 11:31:18 magnum.np: =============================================================================
2025-04-02 11:31:18 magnum.np: Operation No of calls Avg time [ms] Total time [s]
2025-04-02 11:31:18 magnum.np: --------------------------- ------------- --------------- ----------------
2025-04-02 11:31:18 magnum.np: LLGSolver.step 201 159.352 32.0298
2025-04-02 11:31:18 magnum.np: DemagField.h 16884 0.960114 16.2106
2025-04-02 11:31:18 magnum.np: ExchangeField.h 16884 0.454757 7.67812
2025-04-02 11:31:18 magnum.np: SpinTorqueSlonczewski.h 16884 0.124672 2.10496
2025-04-02 11:31:18 magnum.np: --------------------------- ------------- --------------- ----------------
2025-04-02 11:31:18 magnum.np: Total 32.914
2025-04-02 11:31:18 magnum.np: Missing 0.884183
2025-04-02 11:31:18 magnum.np: =============================================================================
Plot Results
[3]:
import numpy as np
import matplotlib.pyplot as plt
data = np.loadtxt("data/log.dat")
ref = np.load("ref/M_mx3_2.npy")
#ref = np.load("ref/M_oommf_2.npy") # OOMMF and mumax3 perfectly agree
fig, ax = plt.subplots(figsize=(15, 5))
cycle = plt.rcParams['axes.prop_cycle'].by_key()['color']
ax.plot(data[:,0]*1e9, data[:,4], '-', color = cycle[0], label = "magnum.np - x")
ax.plot(ref[0]*1e9, ref[1], '-', color = cycle[0], linewidth = 6, alpha = 0.4, label = "reference - x")
ax.plot(data[:,0]*1e9, data[:,5], '-', color = cycle[1], label = "magnum.np - y")
ax.plot(ref[0]*1e9, ref[2], '-', color = cycle[1], linewidth = 6, alpha = 0.4, label = "reference - y")
ax.plot(data[:,0]*1e9, data[:,6], '-', color = cycle[2], label = "magnum.np - z")
ax.plot(ref[0]*1e9, ref[3], '-', color = cycle[2], linewidth = 6, alpha = 0.4, label = "reference - z")
ax.set_xlabel("Time t[ns]")
ax.set_ylabel("Magnetization $m$")
ax.set_xlim(0,1)
ax.legend(ncol=3)
ax.grid()
fig.savefig("data/results.png")
