def check_energy_for_m(m, E_expected): """ Helper function to compare the computed energy for a given magnetisation with an expected analytical value. """ m_field = Field(S3) m_field.set(df.Constant(m)) H_ext = Zeeman(H * np.array([1, 0, 0])) H_ext.setup(m_field, Ms, unit_length=unit_length) E_computed = H_ext.compute_energy() assert np.allclose(E_computed, E_expected, atol=0, rtol=1e-12)
def check_energies(m=None, theta=None): """ Helper function to compare the computed energy for a given magnetisation with an expected analytical value. The argument theta is the angle between the magnetisation vector and the x-axis. """ # Exactly one of m, theta must be given assert ((m is None or theta is None) and not (m is None and theta is None)) if m is None: if theta is None: raise ValueError("Exactly one of m, theta must be given.") theta_rad = theta * pi / 180. m = (cos(theta_rad), sin(theta_rad), 0) else: if theta != None: raise ValueError("Exactly one of m, theta must be given.") m = m / np.linalg.norm(m) m_field = Field(S3) m_field.set(df.Constant(m)) H_ext = Zeeman(H * np.array([1, 0, 0])) H_ext.setup(m_field, Field(df.FunctionSpace(mesh, 'DG', 0), Ms), unit_length=unit_length) #E_analytical_1 = -mu0 * Ms * H * volume_1 * cos(theta_rad) E_analytical_1 = -mu0 * Ms * H * volume_1 * np.dot(m, [1, 0, 0]) E_analytical_2 = -mu0 * Ms * H * volume_2 * np.dot(m, [1, 0, 0]) E_analytical_total = E_analytical_1 + E_analytical_2 E_computed_1 = H_ext.compute_energy(dx=dx_disk_1) E_computed_2 = H_ext.compute_energy(dx=dx_disk_2) E_computed_total = H_ext.compute_energy(dx=df.dx) # Check that the sum of the computed energies for disk #1 and #2 equals # the total computed energy assert np.allclose(E_computed_1 + E_computed_2, E_computed_total, atol=0, rtol=1e-12) # Check that the computed energies are within the tolerance of the # analytical expressions assert np.allclose(E_computed_1, E_analytical_1, atol=0, rtol=RTOL) assert np.allclose(E_computed_2, E_analytical_2, atol=0, rtol=RTOL) assert np.allclose(E_computed_total, E_analytical_total, atol=0, rtol=RTOL)
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 example2(Ms): x0 = y0 = z0 = 0 x1 = 500 y1 = 10 z1 = 100 nx = 50 ny = 1 nz = 1 mesh = df.Box(x0, y0, z0, x1, y1, z1, nx, ny, nz) S1 = df.FunctionSpace(mesh, "Lagrange", 1) S3 = df.VectorFunctionSpace(mesh, "Lagrange", 1, dim=3) llb = LLB(S1, S3) llb.alpha = 0.01 llb.beta = 0.0 llb.M0 = Ms llb.set_M((Ms, 0, 0)) llb.set_up_solver(jacobian=False) llb.chi = 1e-4 H_app = Zeeman((0, 0, 5e5)) H_app.setup(S3, llb._M, Ms=1) llb.interactions.append(H_app) exchange = Exchange(13.0e-12, 1e-2) exchange.chi = 1e-4 exchange.setup(S3, llb._M, Ms, unit_length=1e-9) llb.interactions.append(exchange) demag = Demag("FK") demag.setup(S3, llb._M, Ms=1) llb.interactions.append(demag) max_time = 1 * np.pi / (llb.gamma * 1e5) ts = np.linspace(0, max_time, num=100) for t in ts: print t llb.run_until(t) df.plot(llb._M) df.interactive()
def example1(Ms=8.6e5): x0 = y0 = z0 = 0 x1 = y1 = z1 = 10 nx = ny = nz = 1 mesh = df.Box(x0, x1, y0, y1, z0, z1, nx, ny, nz) S1 = df.FunctionSpace(mesh, "Lagrange", 1) S3 = df.VectorFunctionSpace(mesh, "Lagrange", 1, dim=3) vis = df.Function(S3) llb = LLB(S1, S3) llb.alpha = 0.01 llb.beta = 0.0 llb.M0 = Ms llb.set_M((Ms, 0, 0)) llb.set_up_solver(jacobian=False) llb.chi = 1e-4 H_app = Zeeman((0, 0, 1e5)) H_app.setup(S3, llb._M, Ms=1) llb.interactions.append(H_app) exchange = Exchange(13.0e-12, 1e-2) exchange.chi = 1e-4 exchange.setup(S3, llb._M, Ms, unit_length=1e-9) llb.interactions.append(exchange) max_time = 2 * np.pi / (llb.gamma * 1e5) ts = np.linspace(0, max_time, num=100) mlist = [] Ms_average = [] for t in ts: llb.run_until(t) mlist.append(llb.M) vis.vector()[:] = mlist[-1] Ms_average.append(llb.M_average) df.plot(vis) time.sleep(0.00) print 'llb times', llb.call_field_times save_plot(ts, Ms_average, 'Ms_%g-time.png' % Ms) df.interactive()
def example1_sundials(Ms): x0 = y0 = z0 = 0 x1 = y1 = z1 = 10 nx = ny = nz = 1 mesh = df.Box(x0, x1, y0, y1, z0, z1, nx, ny, nz) S1 = df.FunctionSpace(mesh, "Lagrange", 1) S3 = df.VectorFunctionSpace(mesh, "Lagrange", 1, dim=3) vis = df.Function(S3) llb = LLB(S1, S3) llb.alpha = 0.00 llb.set_m((1, 1, 1)) llb.Ms = Ms H_app = Zeeman((0, 0, 1e5)) H_app.setup(S3, llb._m, Ms=Ms) llb.interactions.append(H_app) exchange = BaryakhtarExchange(13.0e-12, 1e-2) exchange.setup(S3, llb._m, llb._Ms) llb.interactions.append(exchange) integrator = llg_integrator(llb, llb.M, abstol=1e-10, reltol=1e-6) max_time = 2 * np.pi / (llb.gamma * 1e5) ts = np.linspace(0, max_time, num=50) mlist = [] Ms_average = [] for t in ts: integrator.advance_time(t) mlist.append(integrator.m.copy()) llb.M = mlist[-1] vis.vector()[:] = mlist[-1] Ms_average.append(llb.M_average) df.plot(vis) time.sleep(0.0) print llb.count save_plot(ts, Ms_average, 'Ms_%g-time-sundials.png' % Ms) df.interactive()
def example1(Ms=8.6e5): x0 = y0 = z0 = 0 x1 = y1 = z1 = 10 nx = ny = nz = 1 mesh = df.Box(x0, x1, y0, y1, z0, z1, nx, ny, nz) S1 = df.FunctionSpace(mesh, "Lagrange", 1) S3 = df.VectorFunctionSpace(mesh, "Lagrange", 1, dim=3) vis = df.Function(S3) llb = LLB(S1, S3, rtol=1e-6, atol=1e-10) llb.Ms = Ms llb.alpha = 0.0 llb.set_m((1, 1, 1)) H_app = Zeeman((0, 0, 1e5)) H_app.setup(S3, llb._m, Ms=Ms) llb.interactions.append(H_app) exchange = BaryakhtarExchange(13.0e-12, 1e-5) exchange.setup(S3, llb._m, llb._Ms) llb.interactions.append(exchange) max_time = 2 * np.pi / (llb.gamma * 1e5) ts = np.linspace(0, max_time, num=100) mlist = [] Ms_average = [] for t in ts: llb.run_until(t) mlist.append(llb.M) vis.vector()[:] = mlist[-1] Ms_average.append(llb.M_average) df.plot(vis) time.sleep(0.00) print llb.count save_plot(ts, Ms_average, 'Ms_%g-time.png' % Ms) df.interactive()