def test_against_nmag(finmag): REL_TOLERANCE = 2e-14 m_ref = np.genfromtxt(os.path.join(MODULE_DIR, "m0_nmag.txt")) m_computed = vectors(finmag["m"].get_numpy_array_debug()) assert m_ref.shape == m_computed.shape H_ref = np.genfromtxt(os.path.join(MODULE_DIR, "H_exc_nmag.txt")) H_computed = vectors(finmag["H"].vector().array()) assert H_ref.shape == H_computed.shape assert m_ref.shape == H_ref.shape m_cross_H_ref = np.cross(m_ref, H_ref) m_cross_H_computed = np.cross(m_computed, H_computed) diff = np.abs(m_cross_H_ref - m_cross_H_computed) rel_diff = diff / max([norm(v) for v in m_cross_H_ref]) finmag["table"] += table_entries.format("nmag", s(REL_TOLERANCE, 0), s(np.max(rel_diff)), s(np.mean(rel_diff)), s(np.std(rel_diff))) print "comparison with nmag, m x H, relative difference:" print stats(rel_diff) assert np.max(rel_diff) < REL_TOLERANCE
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
def test_cubic_against_nmag(finmag=conftest.setup_cubic()): REL_TOLERANCE = 1e-6 m_ref = np.genfromtxt(os.path.join(conftest.MODULE_DIR, "m0_nmag.txt")) m_computed = vectors(finmag["m"].get_numpy_array_debug()) assert m_ref.shape == m_computed.shape H_ref = np.genfromtxt( os.path.join(conftest.MODULE_DIR, "H_cubic_anis_nmag.txt")) H_computed = vectors(finmag["H"].vector().array()) assert H_ref.shape == H_computed.shape assert m_ref.shape == H_ref.shape mxH_ref = np.cross(m_ref, H_ref) mxH_computed = np.cross(m_computed, H_computed) print mxH_ref print mxH_computed diff = np.abs(mxH_computed - mxH_ref) rel_diff = diff / \ np.sqrt(np.max(mxH_ref[0] ** 2 + mxH_ref[1] ** 2 + mxH_ref[2] ** 2)) print "comparison with nmag, m x H, difference:" print stats(diff) print "comparison with nmag, m x H, relative difference:" print stats(rel_diff) finmag["table"] += conftest.table_entry("nmag", REL_TOLERANCE, rel_diff) assert np.max(rel_diff) < REL_TOLERANCE
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
def test_using_analytical_solution(finmag): """ Expecting (-1/3, 0, 0) as a result. """ REL_TOLERANCE = 2e-2 H = finmag["H"].reshape((3, -1)) H_ref = np.zeros(H.shape) H_ref[0] -= 1.0 / 3.0 diff = np.abs(H - H_ref) rel_diff = diff / \ np.sqrt(np.max(H_ref[0] ** 2 + H_ref[1] ** 2 + H_ref[2] ** 2)) finmag["table"] += table_entries.format("analytical", s(REL_TOLERANCE, 0), s(np.max(rel_diff)), s(np.mean(rel_diff)), s(np.std(rel_diff))) print "comparison with analytical results, H, relative_difference:" print stats(rel_diff) assert np.max(rel_diff) < REL_TOLERANCE
def test_against_magpar(): finmag = conftest.setup(K2=0) REL_TOLERANCE = 5e-7 magpar_result = os.path.join(MODULE_DIR, 'magpar_result', 'test_anis') magpar_nodes, magpar_anis = magpar.get_field(magpar_result, 'anis') ## Uncomment the lines below to invoke magpar to compute the results, ## rather than using our previously saved results. # magpar_nodes, magpar_anis = magpar.compute_anis_magpar(finmag["m"], # K1=conftest.K1, a=conftest.u1, Ms=conftest.Ms) _, _, diff, rel_diff = magpar.compare_field( finmag["S3"].mesh().coordinates(), finmag["H"].vector().array(), magpar_nodes, magpar_anis) print "comparison with magpar, H, relative_difference:" print stats(rel_diff) finmag["table"] += conftest.table_entry("magpar", REL_TOLERANCE, rel_diff) assert np.max(rel_diff) < REL_TOLERANCE
def test_using_magpar(finmag): REL_TOLERANCE = 10.0 magpar_result = os.path.join(MODULE_DIR, 'magpar_result', 'test_demag') magpar_nodes, magpar_H = magpar.get_field(magpar_result, 'demag') ## Uncomment the line below to invoke magpar to compute the results, ## rather than using our previously saved results. # magpar_nodes, magpar_H = magpar.compute_demag_magpar(finmag["m"], Ms=finmag["Ms"]) _, _, diff, rel_diff = compare_field_directly( finmag["S3"].mesh().coordinates(), finmag["H"], magpar_nodes, magpar_H) finmag["table"] += table_entries.format("magpar", s(REL_TOLERANCE, 0), s(np.max(rel_diff)), s(np.mean(rel_diff)), s(np.std(rel_diff))) print "comparison with magpar, H, relative_difference:" print stats(rel_diff) # Compare magpar with analytical solution H_magpar = magpar_H.reshape((3, -1)) H_ref = np.zeros(H_magpar.shape) H_ref[0] -= 1.0 / 3.0 magpar_diff = np.abs(H_magpar - H_ref) magpar_rel_diff = magpar_diff / \ np.sqrt(np.max(H_ref[0] ** 2 + H_ref[1] ** 2 + H_ref[2] ** 2)) finmag["table"] += table_entries.format("magpar/an.", "", s(np.max(magpar_rel_diff)), s(np.mean(magpar_rel_diff)), s(np.std(magpar_rel_diff))) print "comparison beetween magpar and analytical solution, H, relative_difference:" print stats(magpar_rel_diff) # rel_diff beetween finmag and magpar assert np.max(rel_diff) < REL_TOLERANCE
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
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
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") if __name__ == '__main__': res0 = small_problem() print "0D problem, relative difference:\n", stats(res0["rel_diff"]) res1 = one_dimensional_problem() print "1D problem, relative difference:\n", stats(res1["rel_diff"]) res3 = three_dimensional_problem() print "3D problem, relative difference:\n", stats(res3["rel_diff"])
H_ref[0] -= 1.0 / 3.0 magpar_diff = np.abs(H_magpar - H_ref) magpar_rel_diff = magpar_diff / \ np.sqrt(np.max(H_ref[0] ** 2 + H_ref[1] ** 2 + H_ref[2] ** 2)) finmag["table"] += table_entries.format("magpar/an.", "", s(np.max(magpar_rel_diff)), s(np.mean(magpar_rel_diff)), s(np.std(magpar_rel_diff))) print "comparison beetween magpar and analytical solution, H, relative_difference:" print stats(magpar_rel_diff) # rel_diff beetween finmag and magpar assert np.max(rel_diff) < REL_TOLERANCE if __name__ == "__main__": f = setup_finmag() Hx, Hy, Hz = f["H"].reshape((3, -1)) print "Expecting (Hx, Hy, Hz) = (-1/3, 0, 0)." print "demag field x-component:\n", stats(Hx) print "demag field y-component:\n", stats(Hy) print "demag field z-component:\n", stats(Hz) # test_using_analytical_solution(f) # test_using_nmag(f) test_using_magpar(f) teardown_finmag(f)