Beispiel #1
0
def test_exch_2d_pbc2d():
    """
    Test the exchange field components in a 2D mesh with PBCs
    The mesh sites:

            3     4     5    -->    (0,1,0)  (1,1,0)  (2,1,0)
     y ^    0     1     2           (0,0,0)  (1,0,0)  (2,0,0)
       |
       x -->

    The expected components are in increasing order along x

    """

    mesh = CuboidMesh(nx=3, ny=2, nz=1, periodicity=(True, True, False))
    print mesh.neighbours
    sim = Sim(mesh)
    exch = UniformExchange(1)
    sim.add(exch)

    sim.set_m(init_m, normalise=False)

    field = exch.compute_field()

    expected_x = np.array([3, 4, 5, 3, 4, 5])
    expected_y = np.array([2, 2, 2, 2, 2, 2])

    # Since the field ordering is now: fx1 fy1 fz1 fx2 ...
    # We extract the x components jumping in steps of 3
    assert np.max(abs(field[::3] - expected_x)) == 0
    # For the y component is similar, now we start at the 1th
    # entry and jump in steps of 3
    assert np.max(abs(field[1::3] - expected_y)) == 0
    # Similar fot he z component
    assert np.max(field[2::3]) == 0
Beispiel #2
0
def test_exch_1d():
    """
    Test the x component of the exchange field
    in a 1D mesh, with the spin ordering:

    0 1 2 3 4 5

    """
    mesh = CuboidMesh(nx=5, ny=1, nz=1)
    sim = Sim(mesh)
    exch = Exchange(1.0)
    sim.add(exch)

    sim.set_m(init_m, normalise=False)

    field = exch.compute_field()

    assert field[0] == 1
    assert field[1 * 3] == 2
    assert field[2 * 3] == 4
    assert field[3 * 3] == 6
    assert field[4 * 3] == 3

    assert np.max(field[2::3]) == 0
    assert np.max(field[1::3]) == 0
Beispiel #3
0
def test_exch_3d():
    """
    Test the exchange field of the spins in this 3D mesh:

    bottom layer:
    8  9  10  11
    4  5  6   7       x 2
    0  1  2   3

    The assertions are the mx component
    of the: 0, 1, 2, .. 7 spins

    Remember the new new ordering: fx1, fy1, fz1, fx2, ...

    """
    mesh = CuboidMesh(nx=4, ny=3, nz=2)
    sim = Sim(mesh)
    exch = UniformExchange(1)
    sim.add(exch)

    sim.set_m(init_m, normalise=False)

    field = exch.compute_field()
    # print field
    assert field[0] == 1
    assert field[3] == 0 + 1 + 2 + 1
    assert field[6] == 1 + 2 + 3 + 2
    assert field[9] == 2 + 3 + 3

    assert field[4 * 3] == 1
    assert field[5 * 3] == 5
    assert field[6 * 3] == 10
    assert field[7 * 3] == 11
Beispiel #4
0
def test_sim_init_m_fun():
    mesh = CuboidMesh(nx=3, ny=4, nz=5)
    sim = Sim(mesh)
    sim.set_m(init_m, normalise=False)
    assert(sim.spin_at(1, 2, 3)[0] == 1)
    assert(sim.spin_at(1, 2, 3)[1] == 2)
    assert(sim.spin_at(1, 2, 3)[2] == 3)
Beispiel #5
0
def test_sim_init_m():
    mesh = CuboidMesh(nx=3, ny=4, nz=5)
    sim = Sim(mesh)
    sim.set_m((0, 1, 0))
    sim.spin.shape = (3, -1)
    spin_y = sim.spin[1]
    assert(spin_y.any() == 1)
Beispiel #6
0
def test_exch_energy_1d():
    mesh = CuboidMesh(nx=2, ny=1, nz=1)
    sim = Sim(mesh)
    exch = UniformExchange(1.23)
    sim.add(exch)

    sim.set_m((0, 0, 1))

    energy = exch.compute_energy()
    assert energy == -1.23
def test_dynamic():

    mesh = CuboidMesh(nx=1, ny=1, nz=1)

    sim = Sim(mesh, name='dyn_spin', driver='llg_stt_cpp')
    # sim.set_options(rtol=1e-10,atol=1e-14)
    sim.driver.gamma = 1.0
    sim.mu_s = 1.0

    sim.set_m((0.8,0,-1))

    Kx = Anisotropy(Ku=-0.05, axis=(0, 0, 1), name='Kz')
    sim.add(Kx)

    sim.p = (0,0,1)

    sim.a_J = 0.0052
    sim.alpha = 0.1

    ts = np.linspace(0, 1200, 401)
    for t in ts:
        sim.driver.run_until(t)


    mz = sim.spin[2]
    alpha, K, u = 0.1, 0.05, 0.0052
    print(mz, u/(2*alpha*K))

    #########################################################
    # The system used in this test can be solved analytically, which gives that mz = u/(2*alpha*K),
    # where K represents the easy-plane anisotropy.
    ###
    assert abs(mz - u/(2*alpha*K))/mz< 5e-4
Beispiel #8
0
def test_demag_two_spin_xx():
    mesh = CuboidMesh(nx=2, ny=1, nz=1)
    sim = Sim(mesh)

    demag = Demag()
    sim.add(demag)

    sim.set_m((1, 0, 0))
    field = demag.compute_field()
    print field
    assert(field[0] == 2e-7)
    assert(field[3] == 2e-7)
Beispiel #9
0
def relax_neb(k, maxst, simname, init_im, interp, save_every=10000):
    """
    Execute a simulation with the NEB function of the FIDIMAG code

    The simulations are made for a specific spring constant 'k' (a float),
    number of images 'init_im', interpolations between images 'interp'
    (an array) and a maximum of 'maxst' steps.
    'simname' is the name of the simulation, to distinguish the
    output files.

    --> vtks and npys are saved in files starting with the 'simname' string

    """

    # Prepare simulation
    sim = Sim(mesh, name=simname)
    sim.gamma = const.gamma

    # magnetisation in units of Bohr's magneton
    sim.mu_s = 2. * const.mu_B

    # Exchange constant in Joules: E = Sum J_{ij} S_i S_j
    J = 12. * const.meV
    exch = UniformExchange(J)
    sim.add(exch)

    # DMI constant in Joules: E = Sum D_{ij} S_i x S_j
    D = 2. * const.meV
    dmi = DMI(D, dmi_type='interfacial')
    sim.add(dmi)

    # Anisotropy along +z axis
    ku = Anisotropy(Ku=0.5 * const.meV,
                    axis=[0, 0, 1],
                    name='ku')
    sim.add(ku)

    # Initial images
    init_images = init_im

    # Number of images between each state specified before (here we need only
    # two, one for the states between the initial and intermediate state
    # and another one for the images between the intermediate and final
    # states). Thus, the number of interpolations must always be
    # equal to 'the number of initial states specified', minus one.
    interpolations = interp

    neb = NEB_Sundials(sim,
                       init_images,
                       interpolations=interpolations,
                       spring=k,
                       name=simname)

    neb.relax(max_steps=maxst,
              save_vtk_steps=save_every,
              save_npy_steps=save_every,
              stopping_dmdt=1e-2)
Beispiel #10
0
def test_sim_pin():
    mesh = CuboidMesh(nx=3, ny=2, nz=1)
    sim = Sim(mesh)
    sim.set_m((0, 0.8, 0.6))
    sim.alpha = 0.1
    sim.gamma = 1.0
    sim.pins = pin_fun

    anis = Anisotropy(Ku=1.0, axis=[0, 0, 1], name='Dx')
    sim.add(anis)

    sim.run_until(1.0)
    assert sim.spin[0] == 0
    assert sim.spin[2] != 0
Beispiel #11
0
def test_exch_1d_pbc():
    mesh = CuboidMesh(nx=5, ny=1, nz=1, periodicity=(True, False, False))
    sim = Sim(mesh)
    exch = UniformExchange(1)
    sim.add(exch)

    sim.set_m(init_m, normalise=False)

    field = exch.compute_field()
    assert field[0] == 1 + 4
    assert field[3] == 2
    assert field[6] == 4
    assert field[9] == 6
    assert field[12] == 3 + 0
    assert np.max(field[2::3]) == 0
    assert np.max(field[1::3]) == 0
Beispiel #12
0
def test_exch_2d():
    mesh = CuboidMesh(nx=5, ny=2, nz=1)
    sim = Sim(mesh)
    exch = UniformExchange(1)
    sim.add(exch)

    sim.set_m(init_m, normalise=False)

    field = exch.compute_field()

    assert np.max(field[2::3]) == 0

    assert field[0] == 1
    assert field[3] == 2 + 1
    assert field[6] == 1 + 2 + 3
    assert field[9] == 2 + 3 + 4
    assert field[12] == 3 + 4
Beispiel #13
0
def test_dmi_1d():

    mesh = CuboidMesh(nx=2, ny=1, nz=1)

    sim = Sim(mesh)
    sim.set_m((1, 0, 0))

    dmi = DMI(D=1)
    sim.add(dmi)

    field = dmi.compute_field()

    expected = np.array([0, 0, 0, 0, 0, 0])

    assert (field == expected).all()

    energy = dmi.compute_energy()
    assert energy == 0
Beispiel #14
0
def test_dmi_1d_field():

    mesh = CuboidMesh(nx=2, ny=1, nz=1)

    sim = Sim(mesh)
    sim.set_m(init_m)

    dmi = DMI(D=1.23)
    sim.add(dmi)

    field = dmi.compute_field()

    expected = np.array([0, -1, 0, 0, 0, -1]) * 1.23

    assert np.allclose(field, expected)

    energy = dmi.compute_energy()

    assert energy == 1.23
Beispiel #15
0
def test_exch_1d_spatial():
    """
    Test the x component of the exchange field
    in a 1D mesh, with the spin ordering:

    0 1 2 3 4 5

    """
    mesh = CuboidMesh(nx=12, ny=1, nz=1)
    sim = Sim(mesh)
    exch = Exchange(spatial_J)
    sim.add(exch)

    sim.set_m(init_m, normalise=False)

    field = exch.compute_field()

    assert exch._J[3,3] == -1.0
    assert exch._J[5,1] == 0.3
    assert exch._J[6,0] == 0.3
    assert exch._J[8,5] == 1.0
Beispiel #16
0
def test_zeeman():
    """
    Test the x and y component of the zeeman field
    for the 2nd spin

        6  7  8  9  10 11
        0  1  2  3  4  5
              ^
    """

    mesh = CuboidMesh(nx=5, ny=2, nz=1)

    sim = Sim(mesh)
    sim.set_m((1, 0, 0))

    zeeman = Zeeman(varying_field)
    sim.add(zeeman)

    field = zeeman.compute_field()

    assert field[2 * 3] == 1.2 * (2 + 0.5)
    assert field[2 * 3 + 1] == 2.3 * 0.5
def dynamic(mesh):

    sim = Sim(mesh, name='dyn_spin', driver='slonczewski')
    # sim.set_options(rtol=1e-10,atol=1e-14)
    sim.driver.gamma = 1.0
    sim.mu_s = 1.0

    sim.set_m((0.8,0,-1))

    Kx = Anisotropy(Ku=-0.05, axis=(0, 0, 1), name='Kz')
    sim.add(Kx)

    sim.p = (0,0,1)

    sim.u0 = 0.005
    sim.driver.alpha = 0.1

    ts = np.linspace(0, 1200, 401)
    for t in ts:
        sim.run_until(t)
        #sim.save_vtk()
        print t
Beispiel #18
0
def test_demag_fft_exact():
    mesh = CuboidMesh(nx=5, ny=3, nz=4)
    sim = Sim(mesh)

    demag = Demag()
    sim.add(demag)

    def init_m(pos):
        x = pos[0]
        if x <= 2:
            return (1, 0, 0)
        elif x >= 4:
            return (0, 0, 1)
        else:
            return (0, 1, 0)

    sim.set_m(init_m)
    fft = demag.compute_field()
    exact = demag.compute_exact()
    # print fft,exact
    print np.max(np.abs(fft - exact))

    assert np.max(np.abs(fft - exact)) < 5e-22
Beispiel #19
0
def relax_system(mesh):

    sim = Sim(mesh, name='relax')
    # sim.set_options(rtol=1e-10,atol=1e-14)
    sim.driver.alpha = 1.0
    sim.driver.gamma = 1.0
    sim.mu_s = 1.0

    sim.set_m(init_m)
    # sim.set_m(random_m)
    # sim.set_m(np.load('m_10000.npy'))

    J = 1.0
    exch = UniformExchange(J)
    sim.add(exch)

    Kx = Anisotropy(Ku=0.005, axis=(1, 0, 0), name='Kx')
    sim.add(Kx)

    sim.relax(dt=2.0, stopping_dmdt=1e-6, max_steps=1000,
              save_m_steps=100, save_vtk_steps=50)

    np.save('m0.npy', sim.spin)
def relax_system(mesh):

    sim = Sim(mesh, name='dmi_2d')
    sim.driver.alpha = 0.1
    sim.driver.gamma=1.76e11
    sim.mu_s = 1e-22

    J = 1e-20
    exch = UniformExchange(J)
    sim.add(exch)

    dmi = DMI(0.1 * J)
    sim.add(dmi)

    sim.set_m(init_m)    

    ts = np.linspace(0, 5e-10, 101)
    for t in ts:
        print(t)
        sim.run_until(t)
        #sim.save_vtk()

    return sim.spin
Beispiel #21
0
def create_sim():

    mesh = CuboidMesh(nx=121,ny=121,nz=1)
    sim=Sim(mesh,name='relax')

    sim.driver.alpha = 1.0
    sim.driver.gamma = 0.5
    sim.mu_s = mu_s

    sim.set_m(init_m)

    J = 1.0
    exch = UniformExchange(J)
    sim.add(exch)

    D = 0.08
    dmi = DMI(D)
    sim.add(dmi)

    K = 4e-3
    anis=Anisotropy(K, direction=(0,0,1),name='Ku')
    sim.add(anis)

    return sim
Beispiel #22
0
def disable_test_sim_single_spin_llg_stt(do_plot=False):
    ni = Nickel()
    mesh = CuboidMesh(nx=1, ny=1, nz=1)
    mesh.set_material(ni)

    ni.alpha = 0.1
    sim = Sim(mesh, driver='llg_stt')
    sim.set_m((1, 0, 0))

    H0 = 1
    sim.add(Zeeman((0, 0, H0)))

    dt = 1e-12
    ts = np.linspace(0, 200 * dt, 101)

    precession = ni.gamma / (1 + ni.alpha**2)

    mz_ref = []
    mxyz = []
    real_ts = []

    for t in ts:
        sim.run_until(t)
        real_ts.append(sim.t)
        print(sim.t, abs(sim.spin_length()[0] - 1), sim.spin)
        mz_ref.append(np.tanh(precession * ni.alpha * H0 * sim.t))
        mxyz.append(np.copy(sim.spin))

    mxyz = np.array(mxyz)

    if do_plot:
        ts_ns = np.array(real_ts) * 1e9

        plt.plot(ts_ns, mxyz[:, 0], ".-", label="mx")
        plt.plot(ts_ns, mxyz[:, 1], ".-", label="my")
        plt.plot(ts_ns, mxyz[:, 2], ".-", label="mz")
        plt.plot(ts_ns, mz_ref, "-", label="analytical")
        plt.xlabel("time (ns)")
        plt.ylabel("mz")
        plt.title("integrating a macrospin")
        plt.legend()
        plt.savefig("test_llg_stt.png")

        print(("Deviation = {0}".format(np.max(np.abs(mxyz[:, 2] - mz_ref)))))

    assert np.max(np.abs(mxyz[:, 2] - mz_ref)) < 1e-9
def test_sim_spins(do_plot=False):

    mesh = CuboidMesh(nx=10, ny=5, nz=1)

    sim = Sim(mesh, name='10spin')

    alpha = 0.1
    gamma = 2.21e5
    sim.driver.alpha = alpha
    sim.driver.gamma = gamma
    sim.mu_s = 1.0

    sim.set_m((1, 0, 0))
    print(sim.spin)

    H0 = 1e5
    sim.add(Zeeman((0, 0, H0)))

    ts = np.linspace(0, 1e-9, 101)

    mx = []
    my = []
    mz = []
    real_ts = []

    for t in ts:
        sim.driver.run_until(t)
        real_ts.append(sim.driver.t)
        #print sim.driver.t, abs(sim.spin_length()[0] - 1)
        av = sim.compute_average()
        mx.append(av[0])
        my.append(av[1])
        mz.append(av[2])

        #sim.save_vtk()

    mz = np.array(mz)
    # print mz
    a_mx, a_my, a_mz = single_spin(alpha, gamma, H0, ts)

    print(sim.driver.stat())

    if do_plot:
        plot(real_ts, mx, my, mz, a_mx, a_my, a_mz, name='spins.pdf', title='integrating spins')

    print(("Max Deviation = {0}".format(
        np.max(np.abs(mz - a_mz)))))

    assert np.max(np.abs(mz - a_mz)) < 5e-7
def excite_system(mesh, time=0.1, snaps=11):

    # Specify the stt dynamics in the simulation
    sim = Sim(mesh, name='dyn', driver='llg_stt')

    # Set the simulation parameters
    sim.driver.set_tols(rtol=1e-12, atol=1e-12)
    sim.driver.gamma = 2.211e5 / mu0
    sim.mu_s = 1e-27 / mu0
    sim.alpha = 0.05

    sim.set_m(np.load('m0.npy'))

    # Energies
    exch = UniformExchange(J=2e-20)
    sim.add(exch)

    anis = Anisotropy(0.01*2e-20, axis=(0, 0, 1))
    sim.add(anis)
    # dmi = DMI(D=8e-4)
    # sim.add(dmi)

    # Set the current in the x direction, in A / m
    # beta is the parameter in the STT torque
    sim.driver.jz = -1e12
    sim.driver.beta = 0.1

    # The simulation will run for x ns and save
    # 'snaps' snapshots of the system in the process
    ts = np.linspace(0, time * 1e-9, snaps)

    for t in ts:
        print('time', t)
        sim.driver.run_until(t)
        #sim.save_vtk()
    np.save('m1.npy', sim.spin)

    print(np.load('m1.npy')[:100])
def single_spin(alpha=0.01):

    mat = Material()

    mesh = CuboidMesh(nx=1, ny=1, nz=1)

    sim = Sim(mesh, driver='sllg')
    sim.driver.alpha = alpha
    sim.driver.gamma = mat.gamma
    sim.mu_s = mat.mu_s
    sim.T = 10000

    sim.set_m((1, 1, 1))

    #sim.add(Zeeman(1,(0, 0, 1)))

    anis = Anisotropy(mat.K, direction=(0, 0, 1))
    sim.add(anis)

    dt = 0.5e-12
    ts = np.linspace(0, 1000 * dt, 1001)

    sx = []
    sy = []
    for t in ts:
        sim.run_until(t)
        sx.append(sim.spin[0])
        sy.append(sim.spin[1])
        print(t)

    plt.plot(sx, sy)
    plt.xlabel("$S_x$")
    plt.ylabel("$S_y$")
    plt.grid()
    plt.axis((-0.9, 0.9, -0.9, 0.9))
    plt.axes().set_aspect('equal')

    plt.savefig("macrospin.pdf")
def test_sim_single_spin_sllg(do_plot=False):

    mesh = CuboidMesh(nx=1, ny=1, nz=1)

    sim = Sim(mesh, name='spin', driver='sllg')

    alpha = 0.1
    gamma = 2.21e5

    sim.driver.set_options(dt=5e-15, gamma=gamma)

    sim.driver.alpha = alpha
    sim.mu_s = 1.0

    sim.set_m((1, 0, 0))

    H0 = 1e5
    sim.add(Zeeman((0, 0, H0)))

    ts = np.linspace(0, 1e-10, 101)

    mx = []
    my = []
    mz = []
    real_ts = []
    for t in ts:
        sim.driver.run_until(t)
        real_ts.append(sim.driver.t)
        print(sim.driver.t, abs(sim.spin_length()[0] - 1))
        mx.append(sim.spin[0])
        my.append(sim.spin[1])
        mz.append(sim.spin[2])

    mz = np.array(mz)

    a_mx, a_my, a_mz = single_spin(alpha, gamma, H0, ts)

    if do_plot:
        plot(real_ts, mx, my, mz, a_mx, a_my, a_mz, name='spin_sllg.pdf', title='integrating a spin')

    print(("Max Deviation = {0}".format(
        np.max(np.abs(mz - a_mz)))))

    assert np.max(np.abs(mz - a_mz)) < 1e-8
def relax_system(rtol=1e-10, atol=1e-12):
    """numerical solution"""
    mesh = CuboidMesh(nx=1, ny=1, nz=1)
    sim = Sim(mesh, name='relax')
    sim.driver.set_tols(rtol=rtol, atol=atol)
    sim.driver.alpha = 0.5
    sim.driver.gamma = 2.21e5
    sim.mu_s = 1.0

    sim.set_m((1.0, 0, 0))

    sim.add(Zeeman((0, 0, 1e5)))

    ts = np.linspace(0, 1e-9, 1001)

    for t in ts:
        sim.driver.run_until(t)
Beispiel #28
0
def relax_system(mesh):

    sim=Sim(mesh,name='relax')
    sim.set_options(rtol=1e-12,atol=1e-14)
    sim.do_precession = False
    sim.alpha = 0.5
    sim.gamma = 1.0
    sim.mu_s = 1.0

    sim.set_m(init_m)

    J = 1.0
    exch = UniformExchange(J)
    sim.add(exch)

    D = 0.18
    dmi = DMI(D)
    sim.add(dmi)

    zeeman = Zeeman([0,0e-3,2e-2],name='H')
    sim.add(zeeman)

    sim.relax(dt=2.0, stopping_dmdt=1e-8, max_steps=10000, save_m_steps=None, save_vtk_steps=100)

    np.save('m0.npy',sim.spin)
Beispiel #29
0
def test_m_average():
    mesh = CuboidMesh(nx=3, ny=4, nz=5)
    sim = Sim(mesh)
    sim.set_m((0, 0, 1))
    a = sim.compute_average()
    assert a[2] == 1.0
Beispiel #30
0
def skip_test_sim_T_fun():
    mesh = CuboidMesh(nx=3, ny=4, nz=5)
    sim = Sim(mesh)
    sim.set_T(init_T)
    assert(sim.T[0] == 1.5)
    assert(sim.T[-1] == 10.5)