Пример #1
0
def test_single_moment_analytic():
    import numpy as np

    # create the data
    relax_system()

    # read the data from file
    data = DataReader('relax.txt')
    ts = data['time']

    # compare with analytical result
    mx, my, mz = single_spin(0.5, 2.21e5, 1e5, ts)

    Mx = data['m_x']
    My = data['m_y']
    Mz = data['m_z']

    # check absolute tolerance is < 1e-8
    assert np.allclose(mx, Mx, rtol=0, atol=1e-8)
    assert np.allclose(my, My, rtol=0, atol=1e-8)
    assert np.allclose(mz, Mz, rtol=0, atol=1e-8)

    # check absolute tolerance is < 1e-5 for x and y, 1e-9 for z
    # (I checked the numbers just now, and this is what passes)
    assert np.allclose(mx, Mx, rtol=1e-05, atol=0)
    assert np.allclose(my, My, rtol=1e-05, atol=0)
    assert np.allclose(mz, Mz, rtol=1e-09, atol=0)
Пример #2
0
def deal_plot():
    data = DataReader('dyn.txt')
    ts = data['time'] * 1e9
    mx = data['m_x']
    my = data['m_y']
    mz = data['m_z']

    data2 = np.loadtxt('oommf.txt')

    ts2 = data2[:, 0] * 1e9
    mx2 = data2[:, 1]
    my2 = data2[:, 2]
    mz2 = data2[:, 3]

    plt.plot(ts, mx, '--', label='m_fidimag', dashes=(2, 2))
    plt.plot(ts, my, '--', label='', dashes=(2, 2))
    plt.plot(ts, mz, '--', label='', dashes=(2, 2))
    plt.plot(ts2, mx2, '--', label='m_oommf')
    plt.plot(ts2, my2, '--', label='')
    plt.plot(ts2, mz2, '--', label='')

    plt.title('std4')
    plt.legend()
    #plt.xlim([0, 0.012])
    #plt.ylim([-5, 100])
    plt.xlabel(r'Ts (ns)')
    # plt.ylabel('Susceptibility')
    plt.savefig('cmp.pdf')
Пример #3
0
def test_energy(do_plot=False):
    mesh = CuboidMesh(nx=30)
    relax_system(mesh)

    if do_plot:
        save_plot()

    data = DataReader('test_energy.txt')
    energy = data['E_total']

    for i in range(len(energy) - 1):
        assert energy[i] > energy[i + 1]
Пример #4
0
def deal_plot():
    data = DataReader('dyn.txt')
    ts = data['time'] * 1e9
    mx = data['m_x']
    my = data['m_y']
    mz = data['m_z']

    data2 = np.loadtxt('oommf.txt')

    ts2 = data2[:, 0] * 1e9
    mx2 = data2[:, 1]
    my2 = data2[:, 2]
    mz2 = data2[:, 3]

    plt.plot(ts, mx, '--', label='m_fidimag', dashes=(2, 2))
    plt.plot(ts, my, '--', label='', dashes=(2, 2))
    plt.plot(ts, mz, '--', label='', dashes=(2, 2))
    plt.plot(ts2, mx2, '--', label='m_oommf')
    plt.plot(ts2, my2, '--', label='')
    plt.plot(ts2, mz2, '--', label='')

    plt.title('std4')
    plt.legend()
    #plt.xlim([0, 0.012])
    #plt.ylim([-5, 100])
    plt.xlabel(r'Ts (ns)')
    # plt.ylabel('Susceptibility')
    plt.savefig('cmp.pdf')

    # compute deviation of very last point against reference solution
    # from the OOMMF simulation tool
    dev_mx = abs(mx[-1] - mx2[-1])
    dev_my = abs(my[-1] - my2[-1])
    dev_mz = abs(mz[-1] - mz2[-1])
    print("Deviations are   {}, {} and {} in m_x, m_y and m_z.".format(
        dev_mx, dev_my, dev_mz))

    expected_devs = 1.8811609559e-06, 1.88438380672e-05, 1.05292548867e-06
    print("Expected devs are {}, {} and {} in m_x, m_y and m_z.".format(
        *expected_devs))

    # and use as simple system test
    assert dev_mx < 1.89e-06
    assert dev_my < 1.89e-05
    assert dev_mz < 1.06e-06
Пример #5
0
def save_plot():
    fig = plt.figure()

    data = DataReader('test_energy.txt')
    ts = data['time']

    #plt.plot(ts, data['E_Dp'], label='E_Dp')
    #plt.plot(ts, data['E_Dp'], label='E_Dp')
    #plt.plot(ts, data['E_Dx'], label='E_Dx')
    plt.plot(ts, data['E_exch'], label='E_exch')
    plt.plot(ts, data['E_total'], label='E_total')

    plt.ylabel('Energy (J)')
    plt.xlabel('Time (s)')
    plt.legend(loc=1)
    plt.grid()
    # plt.ylim([-1.2,1.2])
    fig.savefig('energy.pdf')
Пример #6
0
def plot_all():

    font = {
        'family': 'serif',
        'weight': 'normal',
        'size': 12,
    }

    plt.rc('font', **font)

    data = DataReader('relax.txt')
    ts = data['time']

    mx, my, mz = single_spin(0.5, 2.21e5, 1e5, ts)

    ts = ts * 1e9
    fig = plt.figure(figsize=(5, 4))
    plt.plot(ts, mx, '--', label='m_x', dashes=(2.0, 2.0))
    plt.plot(ts, my, '--', label='m_y', dashes=(2.0, 2.0))
    plt.plot(ts, mz, '--', label='m_z', dashes=(2.0, 2.0))

    ts = ts[::10]
    mx = data['m_x'][::10]
    my = data['m_y'][::10]
    mz = data['m_z'][::10]
    plt.plot(ts, mx, '.', color='b')
    plt.plot(ts, my, '.', color='g')
    plt.plot(ts[::2], mz[::2], '.', color='r')
    plt.xlim([0, 1.01])

    l1 = plt.legend(bbox_to_anchor=[0.8, 0.8], shadow=True, frameon=True)
    custom_legend(l1)

    plt.xlabel('Time (ns)')
    plt.ylabel('m')
    plt.tight_layout()
    fig.savefig('m_ts.pdf')
oommf_original = np.loadtxt(OOMMF_DATAFILE)
oommf_data = np.zeros(oommf_original.shape)
oommf_data[1:] = oommf_original[:-1]
# we have zeroes at t = 0 for oommf's data for now

# LaTeX labels
math = lambda txt: "$" + txt + "$"      # concatenation because getting {{{}}}
rm = lambda txt: "\mathrm{" + txt + "}" # right in subsequent formats is hard
olbl = math(rm("oommf"))
flbl = math(rm("fidimag"))
lbli = lambda name, mi: math(rm(rm(name) + "\; m_" + rm(mi)))
olbli = lambda i: lbli("oommf", i)
flbli = lambda i: lbli("fidimag", i)

for fi in FIDIMAG_DATAFILES:
    data = DataReader(fi)
    tol = re.search('dyn_r([0-9]+)_a', fi).groups()[0]  # get exponent from filename
    difference = np.sqrt((oommf_data[:, 2] - data["m_x"]) ** 2 +
                         (oommf_data[:, 3] - data["m_y"]) ** 2 +
                         (oommf_data[:, 4] - data["m_z"]) ** 2)

    fig, axes = plt.subplots(3, figsize=(10, 8), sharex= True)

    # dynamics of average magnetisation plotted
    axes[0].plot(oommf_data[:, 0] * 1e9, oommf_data[:, 2], "b-", label=olbli("x"))
    axes[0].plot(oommf_data[:, 0] * 1e9, oommf_data[:, 3], "g-", label=olbli("y"))
    axes[0].plot(oommf_data[:, 0] * 1e9, oommf_data[:, 4], "r-", label=olbli("z"))
    axes[0].plot(data["time"] * 1e9, data["m_x"], "bx", label=flbli("x"))
    axes[0].plot(data["time"] * 1e9, data["m_y"], "gx", label=flbli("y"))
    axes[0].plot(data["time"] * 1e9, data["m_z"], "rx", label=flbli("z"))
    axes[0].set_ylim((-1.05, 1))
Пример #8
0
import matplotlib
matplotlib.use('Agg')
import matplotlib.pyplot as plt
from fidimag.common.fileio import DataReader

# labels on plot
mathmode = lambda txt: "$" + txt + "$"
rm = lambda txt: "\mathrm{" + txt + "}"
label = lambda name, mi: mathmode(rm(rm(name) + "\; m_" + rm(mi)))
fidi = mathmode(rm("fidimag"))
m = lambda i: label("fidimag", i)

for datafile, descr in (("sim_sundials_J0", "w/o Jacobian"),
                        ("sim_sundials_diag", "Jacobian approx."),
                        ("sim_sundials_J1", "with Jacobian")):
    data = DataReader(datafile + ".txt")
    total_wall_time = (data["real_time"][-1] - data["real_time"][0]) / 60.0

    fig, axes = plt.subplots(2, figsize=(10, 8), sharex=True)

    # magnetisation dynamics
    axes[0].plot(data["time"] * 1e9, data["m_x"], "bx", label=m("x"))
    axes[0].plot(data["time"] * 1e9, data["m_y"], "gx", label=m("y"))
    axes[0].plot(data["time"] * 1e9, data["m_z"], "rx", label=m("z"))
    axes[0].set_ylim((-0.3, 1.1))
    axes[0].set_ylabel("unit magnetisation (1)")
    axes[0].legend()
    axes[0].set_title("{}, wall time {:.2} minutes".format(
        descr, total_wall_time))

    # number of RHS evaluations