def test_H0_is_indexable_or_callable(): """ Test that an exception is raised if H0 is not indexable, and that an exception is not raised if H0 is indexable. """ # Test for some different accepted types. inputSuccess = ([0., 0., 1.], np.array([0., 0., 1.]), lambda x: x + 0.1) for zS in inputSuccess: Zeeman(zS) # Test for different failing types. Should perhaps use a unittest.TestCase # for testing to make this more elegant, but there's probably a reason why # it's not used elsewhere. inputFailures = [5., -7] for zS in inputFailures: try: Zeeman(zS) except ValueError: pass else: raise Exception("Zeeman argument \"{}\" was expected to raise an " "exception, but did not!." .format(zS))
def test_zeeman_energy(): mu0 = 4 * np.pi * 1e-7 # A system of 8 cells ( not using nm units) mesh = CuboidMesh(dx=2, dy=2, dz=2, nx=2, ny=2, nz=2 ) sim = Sim(mesh) Ms = 1e5 sim.set_Ms(Ms) sim.set_m((0, 0, 1)) H = 0.1 / mu0 zeeman = Zeeman((0, 0, H)) sim.add(zeeman) field = zeeman.compute_field() zf = sim.get_interaction('Zeeman') # -> -> # Expected energy: Int ( -mu0 M * H ) dV # Since we have 8 cells with the same M, we just sum their contrib exp_energy = 8 * (-mu0 * H * Ms * mesh.dx * mesh.dy * mesh.dz) assert np.abs(zf.compute_energy() - exp_energy) < 1e-10
def test_zeeman(): 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[6] == 1.2 * (2 + 0.5) assert field[7] == 2.3 * 0.5
def relax_system(mesh): sim = Sim(mesh, name='relax') sim.set_tols(rtol=1e-10, atol=1e-14) sim.alpha = 0.5 sim.gamma = 2.211e5 sim.Ms = 8.6e5 sim.do_precession = False sim.set_m((1,1,1)) # sim.set_m(np.load('m0.npy')) A = 1.3e-11 exch = UniformExchange(A=A) sim.add(exch) dmi = DMI(D=1e-3) sim.add(dmi) zeeman = Zeeman((0, 0, 2e4)) sim.add(zeeman, save_field=True) sim.relax(dt=1e-13, stopping_dmdt=0.01, max_steps=5000, save_m_steps=None, save_vtk_steps=50) np.save('m0.npy', sim.spin)
def run_dynamics(mesh, initial_state, integrator_settings): if integrator_settings[0] == "sundials": integrator, tol, max_ord, use_jac = integrator_settings jac_label = "_J" if use_jac else "" print "Simulation with sundials integrator, rtol, atol={}, q={}, J={}.".format( tol, max_ord, use_jac) sim = setup_simulation(mesh, initial_state, "dyn_t{}_q{}{}".format(tol, max_ord, use_jac), integrator, use_jac) sim.set_tols(rtol=10**-tol, atol=10**-tol, max_ord=max_ord) else: integrator, stepsize = integrator_settings print "Simulation with {} integrator, stepsize={}.".format( integrator, stepsize) sim = setup_simulation(mesh, initial_state, "dyn_{}_h{}".format(integrator, stepsize), integrator) sim.integrator.h = stepsize sim.add(Zeeman([-35.5 * mT, -6.3 * mT, 0], name='H'), save_field=True) ts = np.linspace(0, 2e-9, 501) for ti, t in enumerate(ts): sim.run_until(t) if ti % 100 == 0: print "it's {} now, simulated {} s.".format( time.strftime("%d/%m/%Y %H:%M:%S"), t)
def relax_system(mesh): sim = Sim(mesh, name='relax') sim.set_tols(rtol=1e-6, atol=1e-6) sim.alpha = 0.5 sim.gamma = 2.211e5 sim.Ms = 8.6e5 sim.do_precession = False sim.set_m(init_m) exch = UniformExchange(A=1.3e-11) sim.add(exch) dmi = DMI(D=-4e-3) sim.add(dmi) zeeman = Zeeman((0, 0, 4e5)) sim.add(zeeman, save_field=True) sim.relax(dt=1e-13, stopping_dmdt=1e-2, save_m_steps=None, save_vtk_steps=50) np.save('m0.npy', sim.spin)
def relax_system(mesh): sim = Sim(mesh, name='relax') sim.set_tols(rtol=1e-6, atol=1e-6) sim.alpha = 0.5 sim.gamma = 2.211e5 sim.Ms = 8.6e5 sim.do_precession = False sim.set_m(init_m) #sim.set_m((0,0.1,1)) #sim.set_m(np.load('m0.npy')) A = 1.3e-11 exch = UniformExchange(A=A) sim.add(exch) dmi = DMI(D=1.3e-3) sim.add(dmi) anis = UniaxialAnisotropy(-3.25e4, axis=(0, 0, 1)) sim.add(anis) zeeman = Zeeman((0, 0, 6.014576e4)) sim.add(zeeman, save_field=True) sim.relax(dt=1e-13, stopping_dmdt=0.5, max_steps=5000, save_m_steps=None, save_vtk_steps=50) np.save('m0.npy', sim.spin)
def relax_system(mesh): sim = Sim(mesh, chi=1e-3, name='relax', driver='llbar_full') sim.set_tols(rtol=1e-7, atol=1e-7) sim.Ms = 8.0e5 sim.alpha = 0.1 sim.beta = 0 sim.gamma = 2.211e5 sim.set_m((1, 0.25, 0.1)) # sim.set_m(np.load('m0.npy')) A = 1.3e-11 exch = UniformExchange(A=A) sim.add(exch) mT = 795.7747154594767 zeeman = Zeeman([-100 * mT, 4.3 * mT, 0], name='H') sim.add(zeeman, save_field=True) demag = Demag() sim.add(demag) ONE_DEGREE_PER_NS = 17453292.52 sim.relax(dt=1e-12, stopping_dmdt=0.01, max_steps=5000, save_m_steps=100, save_vtk_steps=50) np.save('m0.npy', sim.spin)
def apply_field1(mesh): sim = Sim(mesh, name='dyn') sim.driver.set_tols(rtol=1e-10, atol=1e-10) sim.driver.alpha = 0.02 sim.driver.gamma = 2.211e5 sim.Ms = 8.0e5 sim.set_m(np.load('m0.npy')) A = 1.3e-11 exch = UniformExchange(A=A) sim.add(exch) demag = Demag() sim.add(demag) mT = 0.001 / mu0 print("Applied field = {}".format(mT)) zeeman = Zeeman([-24.6 * mT, 4.3 * mT, 0], name='H') sim.add(zeeman, save_field=True) ts = np.linspace(0, 1e-9, 201) for t in ts: sim.driver.run_until(t) print('sim t=%g' % t)
def create_simulation(mesh, simname): # Initiate a simulation object. PBCs are specified in the mesh sim = Sim(mesh, name=simname) # Use default gamma value # sim.gamma = const.gamma # Magnetisation in A/m sim.Ms = 148367 # We could change the parameters using this option # sim.set_options(gamma=const.gamma) # Initial magnetisation profile from the function sim.set_m((0, 0.2, 0.8)) # Exchange constant A = 1.602e-12 exch = UniformExchange(A) sim.add(exch) # DMI constant D = 3.84e-3 dmi = DMI(D, dmi_type='interfacial') sim.add(dmi) # Zeeman field sim.add(Zeeman((0, 0, 25. / c.mu_0))) # Tune the damping for faster convergence sim.driver.alpha = 0.5 # Remove precession sim.driver.do_precession = False sim.driver.set_tols(rtol=1e-12, atol=1e-12) return sim
def test_sim_single_spin(do_plot=False): mesh = CuboidMesh(nx=1, ny=1, nz=1) sim = Sim(mesh, name='spin') alpha = 0.1 gamma = 2.21e5 sim.alpha = alpha sim.gamma = gamma sim.mu_s = 1.0 sim.set_m((1, 0, 0)) 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.run_until(t) real_ts.append(sim.t) print(sim.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) # print mz a_mx, a_my, a_mz = single_spin(alpha, gamma, H0, ts) print(sim.stat()) if do_plot: ts_ns = np.array(real_ts) * 1e9 plt.plot(ts_ns, mx, ".", label="mx", color='DarkGreen') plt.plot(ts_ns, my, ".", label="my", color='darkslateblue') plt.plot(ts_ns, mz, ".", label="mz", color='m') plt.plot(ts_ns, a_mx, "--", label="analytical", color='b') plt.plot(ts_ns, a_my, "--", color='b') plt.plot(ts_ns, a_mz, "--", color='b') plt.xlabel("time (ns)") plt.ylabel("m") plt.title("integrating a macrospin") plt.legend() plt.savefig("single_spin.pdf") print(("Max Deviation = {0}".format( np.max(np.abs(mz - a_mz))))) assert np.max(np.abs(mz - a_mz)) < 5e-7
def run_sim(): mesh = CuboidMesh() sim = Sim(mesh, name='spin') alpha = 0.1 gamma = 2.21e5 sim.alpha = alpha sim.driver.gamma = gamma sim.mu_s = 1.0 sim.set_m((1, 0, 0)) H0 = 1e5 sim.add(Zeeman((0, 0, H0))) sim.driver.run_until(1e-10) sim.driver.run_until(0.5e-10)
def relax_system(): mesh = CuboidMesh(nx=1, ny=1, nz=1) sim = Sim(mesh, name='relax') sim.driver.set_tols(rtol=1e-10, atol=1e-10) sim.driver.alpha = 0.5 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.run_until(t)
def test_add_remove_interaction_simple(): mesh = CuboidMesh(nx=10, ny=10, nz=10, unit_length=1e-9) name = 'test_add_remove_intn_simple' sim = Sim(mesh, name=name) sim.set_m(lambda pos: (0, 0, 1)) sim.set_Ms(5.8e5) exch = UniformExchange(A=1e-11, name='Exchange') zee = Zeeman((0, 0, 0.05 / constant.mu_0), name='Zeeman') sim.add(exch) sim.add(zee) sim.driver.run_until(1e-9) sim.remove('Zeeman') sim.driver.run_until(2e-9) f = open(name + '.txt') lines = f.read().split('\n') headers = lines[0].split() first_data = lines[2].split() last_data = lines[2].split() # Find the position in the data table position = headers.index('E_Zeeman') assert np.abs(float(last_data[position])) < 1e-15
def excite_system(mesh): sim = Sim(mesh, name='dyn', driver='llg_stt') sim.set_tols(rtol=1e-8, atol=1e-10) sim.alpha = 0.5 sim.gamma = 2.211e5 sim.Ms = 8.6e5 sim.set_m(np.load('m0.npy')) exch = UniformExchange(A=1.3e-11) sim.add(exch) dmi = DMI(D=-4e-3) sim.add(dmi) zeeman = Zeeman((0, 0, 4e5)) sim.add(zeeman, save_field=True) sim.jx = -5e12 sim.beta = 0 ts = np.linspace(0, 0.5e-9, 101) for t in ts: print 'time', t sim.run_until(t) sim.save_vtk()
if args.no_precession: sim.do_precession = False # Material parameters ----------------------------------------------------- sim.Ms = args.Ms exch = UniformExchange(A=args.A) sim.add(exch) dmi = DMI(D=(args.D * 1e-3), type='interfacial') sim.add(dmi) if args.B: zeeman = Zeeman((0, 0, args.B / mu0)) sim.add(zeeman, save_field=True) if args.k_u: # Uniaxial anisotropy along + z-axis sim.add(UniaxialAnisotropy(args.k_u, axis=(0, 0, 1))) if args.Demag: print 'Using Demag!' sim.add(Demag()) # ------------------------------------------------------------------------- # Load magnetisation profile --------------------------------------------- # Change the skyrmion initial configuration according to the
def relax_neb(mesh, k, maxst, simname, init_im, interp, save_every=10000, stopping_dYdt=0.01): """ Execute a simulation with the NEBM algorithm of the FIDIMAG code Here we use always the 21x21 Spins Mesh and don't vary the material parameters. This can be changed adding those parameters as variables. We create a new Simulation object every time this function is called since it can be modified in the process k :: NEBM spring constant maxst :: Maximum number of iterations simname :: Simulation name. VTK and NPY files are saved in folders starting with the 'simname' string init_im :: A list with magnetisation states (usually loaded from NPY files or from a function) that will be used as images in the energy band, e.g. for two states: [np.load('skyrmion.npy'), np.load('ferromagnet.npy')] interp :: Array or list with the numbers of interpolations between every pair of the 'init_im' list. The length of this array is: len(__init_im) - 1 save_every :: Save VTK and NPY files every 'save_every' number of steps """ # Initialise a simulation object and set the default gamma for the LLG # equation sim = Sim(mesh, name=simname) # sim.gamma = const.gamma # Magnetisation in A/m sim.Ms = 148367 # Interactions ------------------------------------------------------------ # Exchange constant A = 1.602e-12 exch = UniformExchange(A) sim.add(exch) # DMI constant D = 3.84e-3 dmi = DMI(D, dmi_type='interfacial') sim.add(dmi) # Zeeman field sim.add(Zeeman((0, 0, 25. / c.mu_0))) # ------------------------------------------------------------------------- # Set the initial images from the list init_images = init_im # The number of interpolations must always be # equal to 'the number of initial states specified', minus one. interpolations = interp # Start a NEB simulation passing the Simulation object and all the NEB # parameters neb = NEBM_Geodesic(sim, init_images, interpolations=interpolations, spring_constant=k, name=simname, ) # Finally start the energy band relaxation neb.relax(max_iterations=maxst, save_vtks_every=save_every, save_npys_every=save_every, stopping_dYdt=stopping_dYdt ) # Produce a file with the data from a cubic interpolation for the band interp_data = np.zeros((200, 2)) interp_data[:, 0], interp_data[:, 1] = neb.compute_polynomial_approximation(200) np.savetxt(simname + 'interpolation.dat', interp_data)
A = 13e-12 # J * m**-1 D = 3e-3 # J * m**-2 Ku = 0e6 # J * m**-3 Ms = 0.86e6 # A / m B0 = 0.4 # T sim.Ms = Ms sim.add(UniformExchange(A)) sim.add(Demag()) # sim.add(UniaxialAnisotropy(Ku, (0, 0, 1))) # Periodic DMI sim.add(DMI(D, dmi_type='interfacial')) # External field along the stripe length sim.add(Zeeman((0, B0 / mu0, 0)), save_field=True) # ----------------------------------------------------------------------------- # Relax the system first sim.driver.alpha = 0.9 sim.driver.do_precession = False sim.driver.relax(stopping_dmdt=0.01) np.save('initial_state.npy', sim.spin) # Remove automatically saved files shutil.rmtree('unnamed_npys/') shutil.rmtree('unnamed_vtks/') shutil.move('unnamed.txt', 'relaxation.txt')