Example #1
0
def test_compute_skyrmion_number_2d_pbc():

    mesh = df.RectangleMesh(df.Point(0, 0), df.Point(100, 100), 40, 40)

    Ms = 8.6e5
    sim = Simulation(mesh, Ms, pbc='2d', unit_length=1e-9)
    sim.set_m(init_skx_down)

    sim.add(Exchange(1.3e-11))
    sim.add(DMI(D=4e-3))
    sim.add(Zeeman((0, 0, 0.45 * Ms)))

    sim.do_precession = False

    sim.relax(stopping_dmdt=1, dt_limit=1e-9)

    #df.plot(sim.m_field.f)
    #df.interactive()
    print np.max(sim.m_field.as_array())

    sky_num = compute_skyrmion_number_2d(sim.m_field.f)

    print 'sky_num = %g' % sky_num

    assert sky_num < -0.95 and sky_num > -1.0
Example #2
0
def test_dmi_pbc2d_1D(plot=False):
    def m_init_fun(p):
        if p[0] < 10:
            return [0.5, 0, 1]
        else:
            return [-0.5, 0, -1]

    mesh = df.RectangleMesh(df.Point(0, 0), df.Point(20, 2), 10, 1)
    m_init = vector_valued_function(m_init_fun, mesh)

    Ms = 8.6e5
    sim = Simulation(mesh, Ms, pbc='2d', unit_length=1e-9)

    sim.set_m(m_init_fun)

    A = 1.3e-11
    D = 5e-3
    sim.add(Exchange(A))
    sim.add(DMI(D))

    sim.relax(stopping_dmdt=0.0001)

    if plot:
        sim.m_field.plot_with_dolfin()

    mx = [sim.m_field.probe([x + 0.5, 1])[0] for x in range(20)]

    assert np.max(np.abs(mx)) < 1e-6
Example #3
0
def test_interaction_accepts_name():
    """
    Check that the interaction accepts a 'name' argument
    and has a 'name' attribute.
    """
    dmi = DMI(1)
    assert hasattr(dmi, 'name')
Example #4
0
def test_dmi_field():
    """
    Simulation 1 is computing H_dmi=dE_dM via assemble.
    Simulation 2 is computing H_dmi=g*M with a suitable pre-computed matrix g.
    Simulation 3 is computing g using a petsc matrix.

    We show that the three methods give equivalent results (this relies
    on H_dmi being linear in M).

    """
    m_initial = df.Expression(
        ('(2*x[0]-L)/L', 'sqrt(1 - ((2*x[0]-L)/L)*((2*x[0]-L)/L))', '0'),
        L=length,
        degree=1)
    m = Field(V)
    m.set(m_initial)
    dmi1 = DMI(D=5e-3, method="box-assemble")
    dmi1.setup(m, Field(df.FunctionSpace(mesh, 'DG', 0), 8.6e5))
    dmi2 = DMI(D=5e-3, method="box-matrix-numpy")
    dmi2.setup(m, Field(df.FunctionSpace(mesh, 'DG', 0), 8.6e5))
    dmi3 = DMI(D=5e-3, method="box-matrix-petsc")
    dmi3.setup(m, Field(df.FunctionSpace(mesh, 'DG', 0), 8.6e5))

    H_dmi1 = dmi1.compute_field()
    H_dmi2 = dmi2.compute_field()
    H_dmi3 = dmi3.compute_field()

    diff12 = np.max(np.abs(H_dmi1 - H_dmi2))
    diff13 = np.max(np.abs(H_dmi1 - H_dmi3))

    print "Difference between H_dmi1 and H_dmi2: max(abs(H_dmi1-H_dmi2))=%g" % diff12
    print "Max value = %g, relative error = %g " % (max(H_dmi1),
                                                    diff12 / max(H_dmi1))
    print "Difference between H_dmi1 and H_dmi3: max(abs(H_dmi1-H_dmi3))=%g" % diff13
    print "Max value = %g, relative error = %g " % (max(H_dmi1),
                                                    diff13 / max(H_dmi1))

    assert diff12 < 5e-8
    assert diff13 < 5e-8
    assert diff12 / max(H_dmi1) < 1e-14
    assert diff13 / max(H_dmi1) < 1e-14
Example #5
0
def test_dmi_uses_unit_length_2dmesh():
    """
    Set up a helical state in two meshes (one expressed in SI units
    the other expressed in nanometers) and compute energies and fields.

    """
    A = 8.78e-12  # J/m
    D = 1.58e-3  # J/m^2
    Ms = 3.84e5  # A/m

    energies = []

    # unit_lengths 1e-9 and 1 are common, let's throw in an intermediate length
    # just to challenge the system a little:
    for unit_length in (1, 1e-4, 1e-9):
        radius = 200e-9 / unit_length
        maxh = 5e-9 / unit_length
        helical_period = (4 * pi * A / D) / unit_length
        k = 2 * pi / helical_period
        # HF 27 April 2014: The next command fails in dolfin 1.3
        # mesh = df.CircleMesh(df.Point(0, 0), radius, maxh)
        # The actual shape of the domain shouldn't matter for the test,
        # so let's use a Rectangular mesh which should work the same:

        nx = ny = int(round(radius / maxh))
        mesh = df.RectangleMesh(df.Point(0, 0), df.Point(radius, radius), nx,
                                ny)

        S3 = df.VectorFunctionSpace(mesh, "CG", 1, dim=3)
        m_expr = df.Expression(("0", "cos(k * x[0])", "sin(k * x[0])"),
                               k=k,
                               degree=1)
        m = Field(S3, m_expr, name='m')
        dmi = DMI(D)
        Ms_dg = Field(df.FunctionSpace(mesh, 'DG', 0), Ms)
        dmi.setup(m, Ms_dg, unit_length=unit_length)
        energies.append(dmi.compute_energy())

        H = df.Function(S3)
        H.vector()[:] = dmi.compute_field()
        print H(0.0, 0.0)

        print "Using unit_length = {}.".format(unit_length)
        print "Helical period {}.".format(helical_period)
        print "Energy {}.".format(dmi.compute_energy())

    rel_diff_energies = abs(energies[0] - energies[1]) / abs(energies[1])
    print "Relative difference of energy {}.".format(rel_diff_energies)
    assert rel_diff_energies < 1e-13

    rel_diff_energies2 = abs(energies[0] - energies[2]) / abs(energies[2])
    print "Relative difference2 of energy {}.".format(rel_diff_energies2)
    assert rel_diff_energies2 < 1e-13
Example #6
0
def test_dmi_pbc2d():
    mesh = df.BoxMesh(df.Point(0, 0, 0), df.Point(1, 1, 0.1), 2, 2, 1)

    pbc = PeriodicBoundary2D(mesh)
    S3 = df.VectorFunctionSpace(mesh, "Lagrange", 1, constrained_domain=pbc)
    m_expr = df.Expression(("0", "0", "1"), degree=1)
    m = Field(S3, m_expr, name='m')

    dmi = DMI(1)
    dmi.setup(m, Field(df.FunctionSpace(mesh, 'DG', 0), 1))
    field = dmi.compute_field()

    assert np.max(field) < 1e-15
Example #7
0
def excite_system():
    Ms = 8.6e5
    sim = Simulation(mesh, Ms, pbc='1d', unit_length=1e-9)

    sim.alpha = 0.0001
    sim.set_m(np.load('relaxed.npy'))

    alpha_expr = AlphaExpression()
    alpha_mult = df.interpolate(alpha_expr, sim.llg.S1)
    sim.spatial_alpha(0.0001, alpha_mult)

    #df.plot(alpha_mult)
    #df.interactive()
    #xs=find_skyrmion_center(sim.llg._m)
    #
    #assert(1==2)

    A = 1.3e-11
    D = 4e-3
    sim.add(Exchange(A))
    sim.add(DMI(D))
    sim.add(Zeeman((0, 0, 0.4 * Ms)))

    GHz = 1e9
    omega = 50 * 2 * np.pi * GHz

    def time_fun(t):
        return np.sinc(omega * (t - 50e-12))

    h0 = 1e3
    kc = 1.0 / 45.0
    H0 = MyExpression(h0, kc)

    sim.add(TimeZeemanPython(H0, time_fun))

    xs = find_skyrmion_center(sim.llg._m)
    ts = np.linspace(0, 8e-9, 4001)

    np.save('xs.npy', xs)

    sim.create_integrator()
    sim.integrator.integrator.set_scalar_tolerances(1e-8, 1e-8)

    index = 0
    for t in ts:

        sim.run_until(t)

        np.save('data/m_%d.npy' % index, sim.llg.m)

        index += 1
Example #8
0
def create_sim(mesh):

    Ms = 3.84e5  # A/m
    A = 8.78e-12  # J/m
    D = 1.58e-3  # J/m**2

    sim = Sim(mesh, Ms, unit_length=1e-9)
    sim.set_m((0, 0, 1))
    sim.set_tol(reltol=1e-10, abstol=1e-10)

    sim.add(Exchange(A))
    sim.add(DMI(D))

    return sim
Example #9
0
def run_finmag():

    sim = Sim(mesh, Ms, unit_length=unit_length)
    sim.alpha = 0.5
    sim.set_m(init_m)

    exchange = Exchange(13.0e-12)
    sim.add(exchange)

    dmi = DMI(4e-3)
    sim.add(dmi)

    dmi_direct = DMI(4e-3, method='direct', name='dmi_direct')
    sim.add(dmi_direct)

    df.plot(sim.m_field.f, title='m')

    fun = df.interpolate(HelperExpression(), sim.S3)
    df.plot(fun, title='exact')

    df.plot(Field(sim.S3, dmi.compute_field()).f, title='dmi_petsc')
    df.plot(Field(sim.S3, dmi_direct.compute_field()).f,
            title='dmi_direct',
            interactive=True)
Example #10
0
def test_DMI_energy_density_3D():
    """Same as above, on a 3D mesh."""
    mesh = df.UnitCubeMesh(4, 4, 4)
    V = df.VectorFunctionSpace(mesh, "CG", 1, dim=3)
    M = Field(V, df.Expression(("-0.5*x[1]", "0.5*x[0]", "1"), degree=1))
    Ms = 10
    D = 1
    dmi = DMI(D)
    dmi.setup(M, Field(df.FunctionSpace(mesh, 'DG', 0), Ms))
    density = dmi.energy_density()
    deviation = np.abs(density - 1.0)

    print "3D energy density (expect array of 1):"
    print density
    print "Max deviation: %g" % np.max(deviation)

    assert np.all(deviation < TOL), \
        "Max deviation %g, should be zero." % np.max(deviation)
Example #11
0
def relax_system(mesh=mesh):

    Ms = 8.6e5
    sim = Simulation(mesh, Ms, pbc='1d', unit_length=1e-9, name='relax')
    sim.set_m(m_init_skyrmion)

    A = 1.3e-11
    D = 4e-3
    sim.add(Exchange(A))
    sim.add(DMI(D))
    sim.add(Zeeman((0, 0, 0.4 * Ms)))

    #sim.run_until(1e-9)
    #sim.schedule('save_vtk', at_end=True)
    #sim.schedule(plot_m, every=1e-10, at_end=True)

    sim.relax()
    df.plot(sim.llg._m)
    np.save('relaxed.npy', sim.m)
Example #12
0
def relax(mesh):

    Ms = 8.6e5
    sim = Simulation(mesh, Ms, pbc='2d',unit_length=1e-9)
    sim.set_m(m_init_fun)

    sim.add(Exchange(1.3e-11))
    sim.add(DMI(D = 4e-3))
    sim.add(Zeeman((0,0,0.45*Ms)))

    sim.alpha = 0.5

    ts = np.linspace(0, 1e-9, 101)
    for t in ts:
        sim.run_until(t)
        p = df.plot(sim.llg._m)
    sim.save_vtk()
        
    df.plot(sim.llg._m).write_png("vortex")
    df.interactive()
Example #13
0
def move_skyrmion(mesh):
    Ms = 8.6e5
    sim = Simulation(mesh, Ms, pbc='2d',unit_length=1e-9, kernel='llg_stt')
    sim.set_m(np.load('m0.npy'))

    sim.add(Exchange(1.3e-11))
    sim.add(DMI(D = 4e-3))
    sim.add(Zeeman((0,0,0.45*Ms)))
    
    sim.alpha=0.01
    
    sim.llg.set_parameters(J_profile=init_J, speedup=50)
    
    ts = np.linspace(0, 5e-9, 101)
    for t in ts:
        sim.run_until(t)
        print t
        #p = df.plot(sim.llg._m)
        sim.save_vtk(filename='nonlocal/v1e7_.pvd')
        
    df.plot(sim.llg._m).write_png("vortex")
Example #14
0
def move_skyrmion(mesh):
    Ms = 8.6e5
    sim = Simulation(mesh, Ms, pbc='2d', unit_length=1e-9)
    sim.set_m(np.load('m0.npy'))

    sim.add(Exchange(1.3e-11))
    sim.add(DMI(D=4e-3))
    sim.add(Zeeman((0, 0, 0.45 * Ms)))

    sim.alpha = 0.01

    sim.set_zhangli(init_J, 1.0, 0.01)

    ts = np.linspace(0, 10e-9, 101)
    for t in ts:
        sim.run_until(t)
        print t
        #p = df.plot(sim.llg._m)
        sim.save_vtk(filename='vtks/v1e7_.pvd')

    df.plot(sim.llg._m).write_png("vortex")
Example #15
0
def compute_skyrmion_number_2d_example():

    mesh = df.CircleMesh(df.Point(0, 0), 20, 4)

    Ms = 3.84e5
    mu0 = 4 * np.pi * 1e-7
    Hz = 0.2

    sim = Simulation(mesh, Ms, unit_length=1e-9, name='sim')
    sim.do_precession = False

    sim.set_m(init_skx_down)

    sim.add(Exchange(8.78e-12))
    sim.add(DMI(-1.58e-3))
    sim.add(Zeeman((0, 0, Hz / mu0)))

    sim.relax(stopping_dmdt=1, dt_limit=1e-9)

    #sim.m_field.plot_with_dolfin(interactive=True)

    print compute_skyrmion_number_2d(sim.m_field.f)
Example #16
0
def test_DMI_energy_density_2D():
    """
    For a vector field (x, y, z) = 0.5 * (-y, x, c),
    the curl is exactly 1.0. (HF)

    """
    mesh = df.UnitSquareMesh(4, 4)
    V = df.VectorFunctionSpace(mesh, "CG", 1, dim=3)
    M = Field(V, value=df.Expression(("-0.5*x[1]", "0.5*x[0]", "1"), degree=1))
    Ms = 1
    D = 1
    dmi = DMI(D)
    dmi.setup(M, Field(df.FunctionSpace(mesh, 'DG', 0), Ms))
    density = dmi.energy_density()
    deviation = np.abs(density - 1.0)

    print "2D energy density (expect array of 1):"
    print density
    print "Max deviation: %g" % np.max(deviation)

    assert np.all(deviation < TOL), \
        "Max deviation %g, should be zero." % np.max(deviation)
Example #17
0
#Gilbert damping for the spin waves recording
sw_alpha = 1e-20

mesh = df.BoxMesh(0, 0, 0, xdim, ydim, zdim, xv, yv, zv)
sim = Sim(mesh, Ms)
sim.set_m((1,1,1))

#exchange energy constant
A = 3.57e-13 #J/m
#DMI constant
D = 2.78e-3 #J/m**2
#external magnetic field
H = [0,0,0] #A/m

sim.add(Exchange(A)) #exchnage interaction
sim.add(DMI(D)) #DMI interaction
sim.add(Zeeman(H)) #Zeeman interaction

############################################################
#time series for the static state simulation
tsim = np.linspace(tstart, tstatic, 101)
#simulation to the ground state
sim.alpha = 1 #dynamics neglected
for t in tsim:
    sim.run_until(t)
    df.plot(sim.llg._m)
############################################################

############################################################
#excite the system with an external sinc field
tsim = np.linspace(tstatic+toffset, tstatic+tpulse, n_sinc_steps)
Example #18
0
import numpy as np
import dolfin as df
from finmag import Simulation
from finmag.energies import Exchange, DMI_Old, DMI

mesh = df.Box(0, 0, 0, 30e-9, 30e-9, 3e-9, 10, 10, 1)
Ms = 8.6e5
sim = Simulation(mesh, Ms)
sim.set_m((Ms, 0, 0))

A = 1.3e-11
D = 4e-3
sim.add(Exchange(A))
sim.add(DMI(D))

series = df.TimeSeries("solution/m")
t = np.linspace(0, 1e-9, 1000)
for i in t:
    sim.run_until(i)
    p = df.plot(sim.llg._m)
    p.write_png("m_{}".format(i))
    series.store(sim.llg._m.vector(), i)
df.interactive()
Example #19
0
from finmag.util.dmi_helper import find_skyrmion_center_2d
from finmag.util.helpers import set_logging_level
from finmag.util import meshes
import finmag

rdir = '../results/relax'
os.chdir(rdir)

h5_file = df.HDF5File(df.mpi_comm_world(), 'relaxed-nanotrack-1e-7.h5', 'r')
mesh = df.Mesh()
h5_file.read(mesh, 'mesh', False)

sim = Sim(mesh=mesh, Ms=5.8e5, unit_length=1e-9, pbc=None)
sim.add(UniaxialAnisotropy(K1=6e5, axis=[0, 0, 1]))
sim.add(Exchange(A=1.5e-11))
sim.add(DMI(D=3e-3))
sim.add(Zeeman((0, 0, 1e5)))
sim.alpha = 0.5

print('Trying to load relaxed state')
m = hlp.load_h5_field(sim, 'relaxed-nanotrack-1e-7.h5')
sim.set_m(m)
sim.do_precession = False

print('Computing eigenmodes!\n\n\n')
n_eigenmodes = 50  # Calculate the first 50 eigenmodes.
complex_freqs = sim.compute_normal_modes(n_values=n_eigenmodes)
f_array = np.real(complex_freqs[0])

np.save('frequencies.npy', f_array)
Example #20
0
# df.plot(mesh, interactive=True)

print finmag.util.meshes.mesh_quality(mesh)
print finmag.util.meshes.mesh_info(mesh)

# Generate simulation object with or without PBCs
if not args.PBC_2D:
    sim = Sim(mesh, args.Ms, unit_length=1e-9, name=args.sim_name)
else:
    sim = Sim(mesh, args.Ms, unit_length=1e-9, pbc='2d', name=args.sim_name)

# Add energies
sim.add(Exchange(args.A))

if args.D != 0:
    sim.add(DMI(args.D * 1e-3, dmi_type='interfacial'))

# Zeeman field in the z direction (in Tesla)
# if it is not zero
if args.B != 0:
    sim.add(Zeeman((0, 0, args.B / finmag.util.consts.mu0)))

if args.k_u:
    sim.add(UniaxialAnisotropy(args.k_u, (0, 0, 1), name='Ku'))

# No Demag in 2D
# sim.add(Demag())

# sim.llg.presession = False

if args.alpha:
# Bulk
A = 13e-12
D = 3e-3
Ms = 0.86e6
Ku = 0.4e6
initial_sk_diam = 10

sim = Sim(mesh, Ms=Ms, unit_length=1e-9, name="1d_Cnv")
sim.set_m((0, 0.1, 0.9))

# -----------------------------------------------------------------------------

# Exchange Energy
sim.add(Exchange(A))
# DMI
sim.add(DMI(D, dmi_type='interfacial'))
# Uniaxial Anisotropy
sim.add(UniaxialAnisotropy(Ku, (0, 0, 1), name='Ku'))

# -----------------------------------------------------------------------------

sim.do_precession = False
sim.alpha = 0.9

if not os.path.exists('vtks'):
    # shutil.rmtree('vtks')
    os.mkdir('vtks')

sim.relax()
sim.save_vtk(filename='vtks/1d_Cnv.pvd', overwrite=True)
sim.save_field('m', '1d_Cnv.npy', overwrite=True)
Example #22
0
def m_init_fun(pos):
    return np.random.random(3)-0.5


pbc=True

Ms = 8.6e5
sim = (mesh, Ms,unit_length=1e-9,pbc2d=pbc)
sim.set_m(m_init_fun)
sim.T=temperature
#A = 3.57e-13
#D = 2.78e-3

A = 1.3e-11
D = 4e-3
sim.add(Exchange(A,pbc2d=pbc))
sim.add(DMI(D,pbc2d=pbc))
sim.add(Zeeman((0,0,0.35*Ms)))
#sim.add(Demag())

def loop(final_time, steps=200):
    t = np.linspace(sim.t + 1e-12, final_time, steps)
    for i in t:
        sim.run_until(i)
        p = df.plot(sim._m)
    df.interactive()

loop(5e-10)

        phi = np.arctan2(y, x) + 0.5 * np.pi
        k = np.pi / initial_sk_diam
        return (np.sin(k * r) * np.cos(phi), np.sin(k * r) * np.sin(phi),
                -np.cos(k * r))
    else:
        return (0, 0, 1)


sim.set_m(m_init)

# -----------------------------------------------------------------------------

# Exchange Energy
sim.add(Exchange(A))
# DMI
sim.add(DMI(D, dmi_type='bulk'))
# Add the corresponding Zeeman field
sim.add(Zeeman((0, 0, B / mu0)))

# -----------------------------------------------------------------------------

sim.do_precession = False
sim.alpha = 0.9

if os.path.exists('vtks'):
    shutil.rmtree('vtks')
os.mkdir('vtks')

sim.save_vtk(filename='vtks/m.pvd'.format(0))
sim.relax()
sim.save_vtk(filename='vtks/m.pvd'.format(1))
Example #24
0
Ms = 3.84e5  # magnetisation saturation (A/m)
A = 8.78e-12  # exchange energy constant (J/m)
D = 1.58e-3  # DMI constant (J/m**2)

# initial magnetisation configuration
m_init = (0, 0, 1)

# We vary the thickness of the top layer.
for ht in np.arange(2, 19, 1):
    # Create mesh and simulation.
    mesh = hlp.disk_with_internal_boundary(d, hb, ht, lmax)
    sim = Sim(mesh, Ms, unit_length=1e-9)

    # Add energies. No Zeeman because the system is in zero field.
    sim.add(Exchange(A))
    sim.add(DMI(hlp.D_init(D)))
    sim.add(Demag())

    # Turn off precession to speed up simulations.
    sim.do_precession = False

    # Initialise the system.
    sim.set_m(m_init)

    # Relax the system with reduced stopping dmdt value for higher
    # precision.
    sim.relax(stopping_dmdt=0.1)

    # Define and create results directory.
    basename = 'd{}hb{}ht{}'.format(d, hb, ht)
    rdir = '../results/stability/{}'.format(basename)