Пример #1
0
    def test_zyx_ordering(self):
        m = mesh.Mesh((3, 1, 1), cellsize=(1, 1, 1))
        indices = [r for r in m.iter_coords_int()]
        expected = [[0, 0, 0], [1, 0, 0], [2, 0, 0]]
        assert np.array_equal(m.mesh_size, [3, 1, 1])
        assert m.array_order == mesh.Mesh.ZYX
        assert np.array_equal(expected, indices)

        m = mesh.Mesh((2, 2, 2), cellsize=(1, 1, 1))
        indices = [r for r in m.iter_coords_int()]
        expected = [[0, 0, 0], [1, 0, 0], [0, 1, 0], [1, 1, 0],
                    [0, 0, 1], [1, 0, 1], [0, 1, 1], [1, 1, 1]]
        assert np.array_equal(m.mesh_size, [2, 2, 2])
        assert m.array_order == mesh.Mesh.ZYX
        assert np.array_equal(expected, indices)
Пример #2
0
def test_against_oommf(finmag=conftest.setup_cubic()):

    REL_TOLERANCE = 7e-2

    oommf_mesh = mesh.Mesh((20, 20, 20),
                           size=(conftest.x1, conftest.y1, conftest.z1))
    # FIXME: why our result is three times of oommf's??
    oommf_anis = oommf_cubic_anisotropy(m0=oommf_m0(conftest.m_gen,
                                                    oommf_mesh),
                                        Ms=conftest.Ms,
                                        K1=conftest.K1,
                                        K2=conftest.K2,
                                        u1=conftest.u1,
                                        u2=conftest.u2).flat
    finmag_anis = finmag_to_oommf(finmag["H"], oommf_mesh, dims=3)

    assert oommf_anis.shape == finmag_anis.shape
    diff = np.abs(oommf_anis - finmag_anis)
    print 'diff', diff
    rel_diff = diff / \
        np.sqrt(
            (np.max(oommf_anis[0] ** 2 + oommf_anis[1] ** 2 + oommf_anis[2] ** 2)))

    print "comparison with oommf, H, relative_difference:"
    print stats(rel_diff)

    finmag["table"] += conftest.table_entry("oommf", REL_TOLERANCE, rel_diff)
    assert np.max(rel_diff) < REL_TOLERANCE
Пример #3
0
def three_dimensional_problem():
    x_max = 100e-9
    y_max = z_max = 1e-9
    x_n = 20
    y_n = z_n = 1

    dolfin_mesh = df.BoxMesh(df.Point(0, 0, 0), df.Point(x_max, y_max, z_max),
                             x_n, y_n, z_n)
    print dolfin_mesh.num_vertices()
    oommf_mesh = mesh.Mesh((x_n, y_n, z_n), size=(x_max, y_max, z_max))

    def m_gen(rs):
        xs = rs[0]
        return np.array([
            xs / x_max,
            np.sqrt(1 - (0.9 * xs / x_max)**2 - 0.01), 0.1 * np.ones(len(xs))
        ])

    from finmag.util.oommf.comparison import compare_anisotropy
    return compare_anisotropy(m_gen,
                              Ms,
                              K1, (0, 0, 1),
                              dolfin_mesh,
                              oommf_mesh,
                              dims=3,
                              name="3D")
Пример #4
0
def test_dmdt_computation_with_oommf():
    # set up finmag
    llg = LLG(S1, S3)
    llg.set_m((-3, -2, 1))

    Ms = llg.Ms.vector().array()[0]
    Ms = float(Ms)
    h = Ms / 2
    H_app = (h / np.sqrt(3), h / np.sqrt(3), h / np.sqrt(3))
    zeeman = Zeeman(H_app)
    zeeman.setup(llg.m_field, llg.Ms, 1)
    llg.effective_field.add(zeeman)

    dmdt_finmag = df.Function(llg.S3)
    dmdt_finmag.vector()[:] = llg.solve(0)

    # set up oommf
    msh = mesh.Mesh((nL, nW, nH), size=(L, W, H))
    m0 = msh.new_field(3)
    m0.flat[0] += -3
    m0.flat[1] += -2
    m0.flat[2] += 1
    m0.flat /= np.sqrt(m0.flat[0] * m0.flat[0] + m0.flat[1] * m0.flat[1] +
                       m0.flat[2] * m0.flat[2])

    dmdt_oommf = oommf_dmdt(m0, Ms, A=0, H=H_app, alpha=0.5,
                            gamma_G=llg.gamma).flat

    # extract finmag data for comparison with oommf
    dmdt_finmag_like_oommf = msh.new_field(3)
    for i, (x, y, z) in enumerate(msh.iter_coords()):
        dmdt_x, dmdt_y, dmdt_z = dmdt_finmag(x, y, z)
        dmdt_finmag_like_oommf.flat[0, i] = dmdt_x
        dmdt_finmag_like_oommf.flat[1, i] = dmdt_y
        dmdt_finmag_like_oommf.flat[2, i] = dmdt_z

    # compare
    difference = np.abs(dmdt_finmag_like_oommf.flat - dmdt_oommf)
    relative_difference = difference / np.max(
        np.sqrt(dmdt_oommf[0]**2 + dmdt_oommf[1]**2 + dmdt_oommf[2]**2))
    print "comparison with oommf, dm/dt, relative difference:"
    print stats(relative_difference)
    assert np.max(relative_difference) < TOLERANCE

    return difference, relative_difference
Пример #5
0
def small_problem():
    # The oommf mesh corresponding to this problem only has a single cell.
    x_max = 1e-9
    y_max = 1e-9
    z_max = 1e-9
    dolfin_mesh = df.BoxMesh(df.Point(0, 0, 0), df.Point(x_max, y_max, z_max),
                             5, 5, 5)
    oommf_mesh = mesh.Mesh((1, 1, 1), size=(x_max, y_max, z_max))

    def m_gen(rs):
        rn = len(rs[0])
        return np.array([np.ones(rn), np.zeros(rn), np.zeros(rn)])

    from finmag.util.oommf.comparison import compare_anisotropy
    return compare_anisotropy(m_gen,
                              Ms,
                              K1, (1, 1, 1),
                              dolfin_mesh,
                              oommf_mesh,
                              name="single")
Пример #6
0
def one_dimensional_problem():
    x_min = 0
    x_max = 100e-9
    x_n = 100000
    dolfin_mesh = df.IntervalMesh(x_n, x_min, x_max)
    oommf_mesh = mesh.Mesh((20, 1, 1), size=(x_max, 1e-12, 1e-12))

    def m_gen(rs):
        xs = rs[0]
        return np.array(
            [xs / x_max,
             np.sqrt(1 - (xs / x_max)**2),
             np.zeros(len(xs))])

    return compare_anisotropy(m_gen,
                              Ms,
                              K1, (1, 1, 1),
                              dolfin_mesh,
                              oommf_mesh,
                              dims=1,
                              name="1D")
Пример #7
0
def test_against_oommf():
    finmag = conftest.setup(K2=0)

    REL_TOLERANCE = 9e-2

    oommf_mesh = mesh.Mesh((20, 20, 20),
                           size=(conftest.x1, conftest.y1, conftest.z1))
    oommf_anis = oommf_uniaxial_anisotropy(
        oommf_m0(conftest.m_gen, oommf_mesh), conftest.Ms, conftest.K1,
        conftest.u1).flat
    finmag_anis = finmag_to_oommf(finmag["H"], oommf_mesh, dims=3)

    assert oommf_anis.shape == finmag_anis.shape
    diff = np.abs(oommf_anis - finmag_anis)
    rel_diff = diff / \
        np.sqrt(
            (np.max(oommf_anis[0] ** 2 + oommf_anis[1] ** 2 + oommf_anis[2] ** 2)))

    print "comparison with oommf, H, relative_difference:"
    print stats(rel_diff)

    finmag["table"] += conftest.table_entry("oommf", REL_TOLERANCE, rel_diff)
    assert np.max(rel_diff) < REL_TOLERANCE
Пример #8
0
def test_1d():
    print "1D problem..."
    for x_n in [1e1, 1e2, 1e3, 1e4, 1e5, 1e6]:
        dolfin_mesh = df.IntervalMesh(int(x_n), 0, x_max)
        oommf_mesh = mesh.Mesh((int(x_n), 1, 1), size=(x_max, 1e-12, 1e-12))
        res = compare_anisotropy(m_gen,
                                 1.0,
                                 K1, (1, 1, 1),
                                 dolfin_mesh,
                                 oommf_mesh,
                                 dims=1,
                                 name="1D")

        vertices[0].append(dolfin_mesh.num_vertices())
        mean_rdiffs[0].append(np.mean(res["rel_diff"]))
        max_rdiffs[0].append(np.max(res["rel_diff"]))

    def linear(x, a, b):
        return a * x + b

    xs = np.log(vertices[0])
    ys = np.log(mean_rdiffs[0])
    popt, pcov = curve_fit(linear, xs, ys)
    assert popt[0] < -1.0
Пример #9
0
def test_against_oommf(finmag):
    REL_TOLERANCE = 8e-2

    from finmag.util.oommf import mesh, oommf_uniform_exchange
    from finmag.util.oommf.comparison import oommf_m0, finmag_to_oommf

    oommf_mesh = mesh.Mesh((xn, 1, 1), size=(x1, 1e-12, 1e-12))
    oommf_exc = oommf_uniform_exchange(oommf_m0(m_gen, oommf_mesh), Ms, A).flat
    finmag_exc = finmag_to_oommf(finmag["H"], oommf_mesh, dims=1)

    assert oommf_exc.shape == finmag_exc.shape
    diff = np.abs(oommf_exc - finmag_exc)
    rel_diff = diff / \
        np.sqrt(
            np.max(oommf_exc[0] ** 2 + oommf_exc[1] ** 2 + oommf_exc[2] ** 2))

    finmag["table"] += table_entries.format("oommf", s(REL_TOLERANCE, 0),
                                            s(np.max(rel_diff)),
                                            s(np.mean(rel_diff)),
                                            s(np.std(rel_diff)))

    print "comparison with oommf, H, relative_difference:"
    print stats(rel_diff)
    assert np.max(rel_diff) < REL_TOLERANCE
Пример #10
0
 def test_zyx_ordering(self):
     m = mesh.Mesh((3, 1, 1), cellsize=(1, 1, 1))
     coords = [r for r in m.iter_coords()]
     expected = [[0.5, 0.5, 0.5], [1.5, 0.5, 0.5], [2.5, 0.5, 0.5]]
     assert np.array_equal(expected, coords)