Quickstart#

This page gives a whirlwind introduction to flatspin. Make sure you have installed flatspin before you continue.

The best way to get familiar with flatspin, is to play with the simulator in an interactive Python environment. This guide is written as a Jupyter notebook, which you can download and run yourself. Just click the download link from the top of this page.

Let’s get started!

The flatspin model#

The main object in flatspin is the model. Each model class defines a spin ice geometry, which specifies the positions and angles of the spins. The SquareSpinIceClosed class, for instance, creates a square spin ice geometry (with “closed” edges):

from flatspin.model import SquareSpinIceClosed

model = SquareSpinIceClosed()
model.plot();
_images/b4bfa03c135e5ad1f4766479efb7bbc6bf7d2f7ee238440ab58df2c81813c5c6.svg

A list of available model classes can be found in the user guide. Custom geometries may be created by extending flatspin.

Model parameters#

Properties of the model can be changed through parameters, which are passed as keyword arguments to the model class. For instance, we may change the size parameter to create a larger spin ice:

model = SquareSpinIceClosed(size=(10,5))
print(f"10x5 square ASI has", model.spin_count, "spins")
model.plot();
10x5 square ASI has 115 spins
_images/25de196956aa48e5f4881b36155bf675b01c01ef988c5217f2c77cc97d05a8f5.svg

Note that size is geometry-specific: for SquareSpinIceClosed, the size specifies the number of columns (rows) of horizontal (vertical) magnets. For KagomeSpinIce, the size denotes the number of hexagonal units:

from flatspin.model import KagomeSpinIce
model = KagomeSpinIce(size=(10,5))
print(f"10x5 kagome ASI has", model.spin_count, "spins")
model.plot();
10x5 kagome ASI has 179 spins
_images/4764ad00e24b02b8ff90537c18b4bf67abffddfbfd5871b781dad6fc53746ef0.svg

Other important parameters include alpha (the coupling strength), hc (the coercive field), disorder (random variations in the coercive fields) and temperature (absolute temperature in Kelvin). For a list of available parameters, see SpinIce.

Running the model#

The primary method for interacting with the model is the external field. We set the external field with set_h_ext().

model = SquareSpinIceClosed()
model.set_h_ext([-0.058, -0.058])

Next we run the model by calling relax(), which will flip all spins until equilibrium is reached, i.e., until there are no more flippable spins. relax() returns the number of spins that were flipped.

flips = model.relax()
print(flips, "spins flipped")
model.plot();
23 spins flipped
_images/77cd77055e074473da01437c2155eaf3e68ed46b2caf080732a84f3a8d4b5cf6.svg

We may also flip only a single spin at a time by calling step(). For each call to step(), the spin with the highest “switching energy” will be flipped, and the method returns True. If there are no flippable spins, step() returns False. Below we use step() to create an animation of the relaxation process.

from matplotlib.animation import FuncAnimation
from IPython.display import HTML

# Reset system back to the polarized state
model.polarize()

fig, ax = plt.subplots()

def do_steps():
    yield model
    while model.step():
        yield model

def animate(model):
    model.plot(ax=ax, replace=True)
        
anim = FuncAnimation(fig, animate, frames=do_steps(), interval=200, blit=False)
plt.close() # Only show the animation
HTML(anim.to_jshtml())
/tmp/ipykernel_7958/2208795677.py:17: UserWarning: frames=<generator object do_steps at 0x7ff3eaec86d0> which we can infer the length of, did not pass an explicit *save_count* and passed cache_frame_data=True.  To avoid a possibly unbounded cache, frame data caching has been disabled. To suppress this warning either pass `cache_frame_data=False` or `save_count=MAX_FRAMES`.
  anim = FuncAnimation(fig, animate, frames=do_steps(), interval=200, blit=False)

Hysteresis loops#

Often we are interested in some average quantity of the ensemble, such as the total magnetization.

A hysteresis loop is a plot of the total magnetization, projected along the direction of the external field. Below we gradually increase the strength H of the external field, applied at some angle phi, and record the total magnetization for each field value H. We also enable GPU acceleration to speed up the simulation with use_opencl=True. If your system is not set up to use opencl, replace with use_opencl=False (this will slow down your simulations significantly).

model = SquareSpinIceClosed(size=(10,10), use_opencl=True)

# H decreases linearly from 0.1 to -0.1, then back to 0.1
H = np.linspace(0.1, -0.1, 500)
H = np.concatenate([H, -H])

# Angle of the external field
phi = np.deg2rad(45)
h_dir = np.array([np.cos(phi), np.sin(phi)])

M_H = []

for h in H:
    model.set_h_ext(h * h_dir)
    model.relax()
    # Magnetization projected along field direction
    m = model.total_magnetization().dot(h_dir)
    M_H.append(m)

plt.plot(H, M_H)
plt.xlabel("H [T]")
plt.ylabel(r"M_H [a.u.]");
_images/b49599ba4390c67233fb6649d552b447667978f3898685b5db32110dfc266a8a.svg

And here is an animation of the hysteresis loop, where only changes to the state are shown:

# Reset system back to the polarized state
model.polarize()

fig, ax = plt.subplots()

def do_hysteresis():
    yield model
    for h in H:
        model.set_h_ext(h * h_dir)
        if model.relax():
            # Only show frames where any spins flipped
            yield model

def animate(model):
    h = model.h_ext[0].dot(h_dir)
    ax.set_title(f'H = {h:g}')
    model.plot(ax=ax, replace=True)
        
anim = FuncAnimation(fig, animate, frames=do_hysteresis(), interval=200, blit=False)
plt.close() # Only show the animation
HTML(anim.to_jshtml())
/tmp/ipykernel_7958/912287197.py:19: UserWarning: frames=<generator object do_hysteresis at 0x7ff3e8184970> which we can infer the length of, did not pass an explicit *save_count* and passed cache_frame_data=True.  To avoid a possibly unbounded cache, frame data caching has been disabled. To suppress this warning either pass `cache_frame_data=False` or `save_count=MAX_FRAMES`.
  anim = FuncAnimation(fig, animate, frames=do_hysteresis(), interval=200, blit=False)

Temperature#

If we increase the temperature, spins flip more easily due to additional thermal energy. Below we can see how temperature affects the hysteresis loop from earlier. Notice how the hysteresis loop becomes narrower at higher temperatures, as the additional thermal energy causes spins to flip for weaker fields.

model = SquareSpinIceClosed(size=(10,10), m_therm=1e-17, use_opencl=True)

# H, phi and h_dir as before

temperatures = [0, 200, 400, 600]

for T in temperatures:
    M_H = []

    model.polarize()
    model.set_temperature(T)
    for h in H:
        model.set_h_ext(h * h_dir)
        model.relax()
        m = model.total_magnetization().dot(h_dir)
        M_H.append(m)

    plt.plot(H, M_H, label=f"{T} K")

plt.xlabel("H [T]")
plt.ylabel(r"M_H [a.u.]");
plt.legend();
_images/265162d3f800d16012161c2b9f0d44753f7bc2bbadaed4f0851d9eb5dd739fff.svg

Vertices#

Spin ice systems are frequently analyzed in terms of its vertices. A vertex is defined by the points in the geometry where spins point either in or out. In square spin ice, a vertex is surrounded by four spins. In the example below, the blue dots denote the vertices.

model = SquareSpinIceClosed()
model.plot()
model.plot_vertices();
_images/4d1496679b34b5efda9254cedbc9702efe372346a2b1e3758643cc09b7a08357.svg

The vertex type is defined by the dipolar energy between the spins in the vertex. Below we randomize the spin states of the model, and count the number of different vertex types with vertex_count(). In the plot, the colors of the dots indicate the vertex type: green for type 1 (lowest energy), blue for type 2, red for type 3 and gray for type 4 (highest energy).

model.randomize(seed=0x9876)
print("Vertex counts:", model.vertex_count())
model.plot()
model.plot_vertices();
Vertex counts: ((1, 2, 3, 4), (1, 2, 5, 1))
_images/49c1d3769c0a0ea0fad7014faf52537cc4bb4908c5ddbc1063e4dafa9410cd02.svg

Another useful visualization is the vertex magnetization, which is the net magnetic moment of the spins in each vertex. Type 1 vertices have zero net moment, and depicted as white (empty) regions in the plot below.

model.plot_vertex_mag();
_images/220d1da1675b7a267ce2b048b60544756bc32f1642b6f678947e1105075295c4.svg

Large scale patterns#

Spin-spin interactions can give rise to large scale patterns in the ensemble. The emergent behavior is a function of the spin ice geometry. Square spin ice exhibits antiferromagnetic domains with zero net magnetization. Large scale patterns are easily visible if we plot the vertex magnetization.

Below, we initialize a square spin ice to a random configuration, and “anneal” it to a low energy state. We cheat a bit and use a very high coupling strength alpha to anneal the system without any external field. For a more physically realistic field-based annealing protocol, see Field based annealing.

model = SquareSpinIceClosed(size=(25,25), alpha=1.0, init='random', use_opencl=True)
model.relax()
model.plot_vertex_mag();
_images/c79b096e71a61251aa50b9e9d80e2be037ec1d958615cf94a7df36e6c9fb5be1.svg

Pinwheel spin ice, on the other hand, favors ferromagnetic domains with a positive net magnetization.

from flatspin.model import PinwheelSpinIceDiamond
model = PinwheelSpinIceDiamond(size=(25,25), alpha=1.0, init='random', use_opencl=True)
model.relax()
model.plot_vertex_mag();
_images/d422902b8a6e62767269635244bbc0f514acf0c97ee18ce2bb846837c5a92796.svg

Energy#

The energy of each spin is derived from the total fields acting on each spin, i.e., the dipolar fields, external fields and thermal fields. Below we obtain the energy of each spin by calling energy(). Next we use the energy to color the arrows of each spin in the relaxed pinwheel system from above. Notice how the spins that are part of the domain walls (the boundaries of the ferromagnetic domains) have higher energy than the spins that are well inside the domains.

E = model.energy()
print("Total energy:", np.sum(E))
quiv = model.plot(C=E, cmap='plasma')
plt.colorbar(quiv);
Total energy: -4582.397431374655
_images/34f5918e6367a253c2201aebddfcd476b66be7bdee8b32e71438111b70273d31.svg

Ready to learn more?#

This has been a quick whirlwind introduction to flatspin. If you haven’t already done so, you are encouraged to download this notebook and play with the code examples yourself. Just click the download button from the top of this page!

In this short time, we have covered only a small part of the flatspin simulator. To learn more, head over to the User guide and explore the topics covered.