#!/usr/bin/env python # coding: utf-8 # # Multiple energy terms of the same class # # Here we demonstrate how to define multiple energy terms of the same class in the energy equation. For the sample, we choose a one-dimensional chain of magnetic moments. # In[1]: import random import oommfc as mc import discretisedfield as df import micromagneticmodel as mm p1 = (-10e-9, 0, 0) p2 = (10e-9, 1e-9, 1e-9) cell = (1e-9, 1e-9, 1e-9) region = df.Region(p1=p1, p2=p2) mesh = df.Mesh(region=region, cell=cell) # The mesh is # In[2]: mesh.mpl(box_aspect=(10, 4, 4), figsize=(8, 8)) # Let us say that the system has an energy equation, which consists of two Zeeman energy terms. # In[3]: H1 = (0, 0, 1e6) H2 = (1e6, 0, 0) system = mm.System(name="multiple_terms") # Now, if we try to add two Zeeman energy terms, we get an exception raised. # In[4]: system.energy = mm.Zeeman(H=(0, 0, 1e5)) + mm.Zeeman(H=(0, 1e5, 0)) # This is because different energy terms must have different names, so they can be uniquely identified. So, we have to give names to our energy terms: # In[5]: system.energy = mm.Zeeman(H=H1, name="zeeman1") + mm.Zeeman(H=H2, name="zeeman2") # We need to define the system's magnetisation (`system.m`). We are going to make it random with $M_\text{s}=8\times10^{5} \,\text{Am}^{-1}$ # In[6]: random.seed(1) Ms = 8e5 # saturation magnetisation (A/m) def m_fun(pos): return [2 * random.random() - 1 for i in range(3)] system.m = df.Field(mesh, nvdim=3, value=m_fun, norm=Ms) # Now, we can minimise the system's energy: # In[7]: md = mc.MinDriver() md.drive(system) # We expect that now all magnetic moments are aligned parallel or antiparallel to the anisotropy axis (in the $z$-direction). # In[8]: system.m.sel("y").mpl( figsize=(10, 3), scalar_kw={"clim": [-0.1, 0.1], "colorbar_label": "$m_y$"} ) # We can see that magnetisation is aligned with the sum of fields `H1+H2`. Finally, let us have a look at the table: # In[9]: system.table.data # We can see that energy terms are marked with the names we gave to energy terms when we defined the energy equation.