Calculating a stray field using an airbox method#

In order to calculate the stray field outside the sample, we have to define an “airbox” which is going to contain our sample. In this example we define a box with 100 nm edgle length as a mesh which then contains a magnetic sample which is a cube with 50 nm dimensions. We achieve this by implementing a Python function for defining the Ms (norm_fun). Outside our sample the value of saturation magnetisation is zero.

import discretisedfield as df
import micromagneticmodel as mm
import oommfc as oc

region = df.Region(p1=(-100e-9, -100e-9, -100e-9), p2=(100e-9, 100e-9, 100e-9))
mesh = df.Mesh(region=region, cell=(5e-9, 5e-9, 5e-9))

def norm_fun(pos):
    x, y, z = pos
    if -50e-9 <= x <= 50e-9 and -50e-9 <= y <= 50e-9 and -50e-9 <= z <= 50e-9:
        return 8e5
        return 0

system = mm.System(name='airbox_method') = mm.Exchange(A=1e-12) + mm.Demag()
system.dynamics = mm.Precession(gamma0=mm.consts.gamma0) + mm.Damping(alpha=1)
system.m = df.Field(mesh, nvdim=3, value=(0, 0, 1), norm=norm_fun, valid="norm")

We can now plot the norm to confirm our definition.


In the next step, we can relax the system and show its magnetisation.

md = oc.MinDriver()

system.m.sel('z').mpl(figsize=(10, 10))
Running OOMMF (ExeOOMMFRunner)[2023/11/10 15:52]... (2.6 s)

Stray field can now be calculated as an effective field for the demagnetisation energy.

stray_field = oc.compute(, system)
Running OOMMF (ExeOOMMFRunner)[2023/11/10 15:52]... (0.6 s)

stray_field is a df.Field and all operations characteristic to vector fields can be performed.

stray_field.sel('z').mpl(figsize=(8, 8), vector_kw={'scale': 1e6})