# Miscellaneous¶

## Units¶

The following table contains a list of quantities used in magnum.fe and their units. Note that all quantities are given in SI units.

This convention also applies to the mesh dimension which in general is expected to be given in meters. However, for numerical reasons it is preferable to create a mesh with lateral dimensions with the order of magnitude 1 and add a proper scale parameter in the State class. Usually nanometers are a good choice which corresponds to state.scale = 1e-9.

Quantity Description Unit
A (SAFLayer) coupling constant $$\text{J/m}^2$$
A (AFCoupling) coupling constant $$\text{J/m}^2$$
Aex (Material) exchange constant $$\text{J/m}$$
alpha (Material) damping constant $$1$$
b (Material) coupling constant (Zhang and Li model) $$\text{m}^3/\text{As}$$
beta (Material) polarization parameter $$1$$
beta_prime (Material) polarization parameter $$1$$
c (Material) spin diffusion coupling constant (deprecated) $$\text{N/A}^2$$
C0 (Material) connected to conductivity by $$C_0=\sigma/2$$ $$\text{A/Vm}$$
D0 (Material) diffusion constant $$\text{m}^2\text{/s}$$
D_inter (Material) interface DMI coupling constant $$\text{J/m}^2$$
E() (LLGTerm) energy $$\text{J}$$
h() (LLGTerm) field $$\text{A/m}$$
j (State) electric current $$\text{A/m}^2$$
J (Material) coupling strength $$\text{J}$$
K_uni (Material) anisotropy constant $$\text{J/m}^3$$
lambda_sf (Material) diffusion length (deprecated) $$\text{m}$$
lambda_j (Material) diffusion length (deprecated) $$\text{m}$$
m (State) magnetization $$1$$
ms (Material) saturation magnetization $$\text{A/m}$$
s (State) spin accumulation $$\text{A/m}$$
sigma (Material) electric conductivity $$\text{A/Vm}$$
t (State) time $$\text{s}$$
T (State) temperature $$\text{K}$$
theta (Material) spin Hall angle $$1$$
tau_sf (Material) spin-flip relaxation time $$\text{s}$$
xi (Material) degree of nonadiabacity (Zhang and Li model) $$1$$

## Constants¶

magnum.fe defines some important constants in the Constants class. They can be used to conveniently convert units. For instance the field strength of 2 Tesla can be converted to A/m by using 2.0 / Constants.mu0 in the simulation script.

class magnumfe.Constants
e = -1.602176487e-19

elementary charge e

gamma = 221276.157

gyromagnetic ratio gamma

hbar = 1.0545718e-34

reduced Planck constant

kb = 1.380648813e-23

Boltzmann constant kb

mu0 = 1.2566370614e-06

magnetic constant mu_0

mu_b = 9.2740154e-24

Bohr magneton mu_bohr

## read_vtk()¶

magnumfe.read_vtk(filename, fileformat=None)

Reads mesh and field information from VTU, VTK or INP files. Works for scalar and vector fields.

# read mesh and magnetization from VTU file and create state from it
state = State(m0.function_space().mesh())
state.m = m0

Arguments
filename (str)
The filename
fileformat (str)
The fileformat can be ‘vtu’(default), ‘vtk’, or ‘inp’. If no format is specified the function tries to extract the format from the given filename.
Returns
dolfin.Function
The function object

## TimeInterpolator¶

class magnumfe.TimeInterpolator(points)

Simple interpolation helper for the creation of e.g. hysteresis loops.

Example
# create a current hysteresis loop (-5e12 to 5e12 A/m^2 in z-direction)
# starting from 0 and taking 4 ns for a complete sweak from min to max.
interpolator = TimeInterpolator({
0e-9: (0, 0,  0),
2e-9: (0, 0,  5e12),
6e-9: (0, 0, -5e12),
10e-9: (0, 0,  5e12)
})
state.j = lambda state: Constant(interpolator(state.t))


## Timer¶

class magnumfe.Timer(name)

Utility class that supports basic timing/profiling features. Each timer is initialized with a unique name. Each call to the body is timed and counted. Results can be printed with by calling print_report().

Example
Timer.enable()

x = 0.0
for i in range(10):
with Timer("Outer Function"):
for j in range(10):
with Timer("Inner Function"):
x += 1.0

Timer.print_report()


results in:

==================================================================
TIMER REPORT
==================================================================
Operation           No of calls    Avg time [ms]    Total time [s]
----------------  -------------  ---------------  ----------------
Outer Function               10      0.0245333         0.000245333
Inner Function            100      0.000665188       6.65188e-05
----------------  -------------  ---------------  ----------------
Total                                                  0.00105381
==================================================================

static configure(**kwargs)

Configure the timer.

Arguments
active bool
Flag the activates/deactived all timers.
skip int
Skip the timing of the first x calls to every timer. The calls are still counted, but not considered in the average execution time.
log_mem bool
Log accumulated memory consumption of timed calls
static disable()

Disable all timers.

static enable(**kwargs)

Enable all timers. Takes all options that are accepted by configure.

static print_report()

Print the timing report.

static reset()

Reset all timers.

## Cache¶

class magnumfe.Cache(*keys, **kwargs)

Simple cache class that is aware of changes in the state class.

Arguments
*keys ([str])
Names of state attributes to be monitored.
**kwargs
Any attributes to be attached to the cache object.
Example
# Initialize cache object that tracks changes in the m and t
# attribute of the state class.
cache = Cache("t", "m")

# Check for cache hit. In case of cache miss the following
# block is expected to update the cache. The hash keys of the
# current state are saved.
if cache.requires_update(state):
cache.some_var = expensive_operation(state.t, state.m)

if cache.requires_update(state):
# Won't execute since cache is up to date

flush()

Flushes the cache by resetting the uuid

requires_update(state)

Checks if cache is expired with resepect to the current state and saves the new state. Has to be followed by a block that updates the cache.

Arguments
state (State)
The current simulation state (or an arbitrary object providing
a uuid() function or a uuid variable)
Returns
True if cache is expired, False otherwise
wrap_func(func)

Builds a lambda from a given function that returns the function call along with the caching keys as required by the state class.

Arguments
func
Function to be wrapped
Returns
Wrapped function

## Screen logging¶

magnum.fe uses the Python logging interface to print status information to the screen. The verbosity of the screen logging can be configured for magnum.fe and third party libraries individualy. Availabe log levels are

CRITICAL  = 50 # errors that may lead to data corruption and suchlike
ERROR     = 40 # things that go boom
WARNING   = 30 # things that may go boom later
INFO      = 20 # information of general interest

magnumfe.set_log_level(level)

Set the log level of magnum.fe specific logging messages. Defaults to INFO = 20.

Arguments
level (int)
The log level
magnumfe.set_fenics_log_level(level)

Set the log level of all third party modules (dolfin, bempp, ffc, ...). Defaults to WARNING = 30.

Arguments
level (int)
The log level