def excite_system(mesh): sim = Sim(mesh, name='dyn', driver='sllg') sim.set_options(dt=1e-14, gamma=const.gamma, k_B=const.k_B) sim.alpha = 0.1 sim.mu_s = const.mu_s_1 sim.T = temperature_gradient sim.set_m(np.load("m0.npy")) J = 50.0 * const.k_B exch = UniformExchange(J) sim.add(exch) D = 0.5 * J dmi = DMI(D) sim.add(dmi) Hz = 0.2 * J / const.mu_s_1 zeeman = Zeeman([0, 0, Hz]) sim.add(zeeman) dt = 2e-14 * 50 # 1e-12 ts = np.linspace(0, 1000 * dt, 501) for t in ts: sim.run_until(t) sim.save_vtk() sim.save_m() print 'sim t=%g' % t
def excite_system(mesh): sim = Sim(mesh, name='dyn') # sim.set_options(rtol=1e-10,atol=1e-14) sim.alpha = 0.04 sim.gamma = 1.0 sim.mu_s = 1.0 sim.set_m(np.load('m0.npy')) J = 1.0 exch = UniformExchange(J) sim.add(exch) D = 0.09 dmi = DMI(D) sim.add(dmi) zeeman = Zeeman([0, 0, 3.75e-3], name='H') sim.add(zeeman) w0 = 0.02 def time_fun(t): return np.exp(-w0 * t) hx = TimeZeeman([0, 0, 1e-5], sinc_fun, name='h') sim.add(hx, save_field=True) ts = np.linspace(0, 20000, 5001) for t in ts: sim.run_until(t) print 'sim t=%g' % t
def excite_system(T=0.1, H=0.15): mesh = CuboidMesh(nx=28 * 3, ny=16 * 5, nz=1, pbc='2d') sim = Sim(mesh, name='dyn', driver='sllg') sim.set_options(dt=1e-14, gamma=const.gamma, k_B=const.k_B) sim.alpha = 0.1 sim.mu_s = const.mu_s_1 sim.set_m(random_m) J = 50 * const.k_B exch = UniformExchange(J) sim.add(exch) D = 0.5 * J dmi = DMI(D) sim.add(dmi) Hz = H * J / const.mu_s_1 zeeman = Zeeman([0, 0, Hz]) sim.add(zeeman) sim.T = J / const.k_B * T ts = np.linspace(0, 5e-11, 51) for t in ts: sim.run_until(t) # sim.save_vtk() np.save('m.npy', sim.spin) plot_m(mesh, 'm.npy', comp='z')
def relax_system_stage1(): mesh = CuboidMesh(nx=140 , ny=140, nz=1) sim = Sim(mesh, name='relax', driver='llg') #sim.set_options(dt=1e-14, gamma=const.gamma, k_B=const.k_B) sim.alpha = 0.5 sim.do_precession = False sim.gamma = const.gamma sim.mu_s = spatial_mu sim.set_m(init_m) J = 50 * const.k_B exch = UniformExchange(J) sim.add(exch) D = 0.27 * J dmi = DMI(D) sim.add(dmi) zeeman = Zeeman(spatial_H) sim.add(zeeman) sim.relax(dt=1e-14, stopping_dmdt=1e10, max_steps=1000, save_m_steps=100, save_vtk_steps=10) np.save('skx.npy', sim.spin) plot_m(mesh, 'skx.npy', comp='z')
def relax_system(mesh): sim = Sim(mesh, name='relax') # sim.set_options(rtol=1e-10,atol=1e-14) sim.driver.alpha = 1.0 sim.driver.gamma = 1.0 sim.mu_s = 1.0 sim.set_m(init_m) # sim.set_m(random_m) # sim.set_m(np.load('m_10000.npy')) J = 1.0 exch = UniformExchange(J) sim.add(exch) D = 0.09 dmi = DMI(D) sim.add(dmi) zeeman = Zeeman([0, 0, 3.75e-3]) sim.add(zeeman) sim.relax(dt=2.0, stopping_dmdt=1e-6, max_steps=1000, save_m_steps=100, save_vtk_steps=50) np.save('m0.npy', sim.spin)
def relax_system(mesh): sim = Sim(mesh, name='relax') sim.set_options(rtol=1e-12, atol=1e-14) sim.do_procession = False sim.alpha = 0.5 sim.gamma = 1.0 sim.mu_s = 1.0 sim.set_m(init_m) J = 1.0 exch = UniformExchange(J) sim.add(exch) D = 0.18 dmi = DMI(D) sim.add(dmi) zeeman = Zeeman([0, 0e-3, 2e-2], name='H') sim.add(zeeman) sim.relax(dt=2.0, stopping_dmdt=1e-8, max_steps=10000, save_m_steps=None, save_vtk_steps=100) np.save('m0.npy', sim.spin)
def relax_system(mesh, Hy=0): sim = Sim(mesh, name='relax') sim.driver.set_tols(rtol=1e-10, atol=1e-12) sim.driver.alpha = 0.5 sim.driver.gamma = 1.0 sim.mu_s = 1.0 sim.do_precession = False sim.set_m(init_m) #sim.set_m(random_m) #sim.set_m(np.load('m_10000.npy')) J = 1.0 exch = UniformExchange(J) sim.add(exch) D = 0.18 dmi = DMI(D) sim.add(dmi) zeeman = Zeeman([0, Hy, 2e-2], name='H') sim.add(zeeman) sim.relax(dt=2.0, stopping_dmdt=1e-7, max_steps=10000, save_m_steps=100, save_vtk_steps=50) np.save('m0.npy', sim.spin)
def excite_system(mesh, Hy=0): sim = Sim(mesh, name='dyn') sim.driver.set_tols(rtol=1e-10, atol=1e-12) sim.driver.alpha = 0.04 sim.driver.gamma = 1.0 sim.mu_s = 1.0 sim.set_m(np.load('m0.npy')) J = 1.0 exch = UniformExchange(J) sim.add(exch) D = 0.18 dmi = DMI(D) sim.add(dmi) zeeman = Zeeman([0, Hy, 2e-2], name='H') sim.add(zeeman) hx = TimeZeeman([0, 0, 1e-5], sinc_fun, name='h') sim.add(hx, save_field=True) dt = 5 steps = 2001 for i in range(steps): sim.run_until(i * dt) sim.save_m() print("step {}/{}".format(i, steps))
def relax_system_stage2(): mesh = CuboidMesh(nx=140 , ny=140, nz=1) sim = Sim(mesh, name='dyn', driver='llg') sim.alpha = 0.1 sim.do_precession = True sim.gamma = const.gamma sim.mu_s = spatial_mu sim.set_m(np.load('skx.npy')) J = 50 * const.k_B exch = UniformExchange(J) sim.add(exch) D = 0.27 * J dmi = DMI(D) sim.add(dmi) zeeman = Zeeman(spatial_H) sim.add(zeeman) ts = np.linspace(0, 2e-9, 201) for t in ts: sim.run_until(t) sim.save_vtk() sim.save_m() print(t)
def test_skx_num(): mesh = CuboidMesh(nx=120, ny=120, nz=1, periodicity=(True, True, False)) sim = Sim(mesh, name='skx_num') sim.set_tols(rtol=1e-6, atol=1e-6) sim.alpha = 1.0 sim.gamma = 1.0 sim.mu_s = 1.0 sim.set_m(init_m) sim.do_procession = False J = 1.0 exch = UniformExchange(J) sim.add(exch) D = 0.09 dmi = DMI(D) sim.add(dmi) zeeman = Zeeman([0, 0, 5e-3]) sim.add(zeeman) sim.relax(dt=2.0, stopping_dmdt=1e-2, max_steps=1000, save_m_steps=None, save_vtk_steps=None) skn = sim.skyrmion_number() print 'skx_number', skn assert skn > -1 and skn < -0.99
def relax_system(mesh): sim = Sim(mesh, name='relax') sim.set_default_options(gamma=const.gamma) sim.alpha = 0.5 sim.mu_s = const.mu_s_1 sim.set_m(init_m) J = 50.0 * const.k_B exch = UniformExchange(J) sim.add(exch) D = 0.5 * J dmi = DMI(D) sim.add(dmi) Hz = 0.2 * J / const.mu_s_1 zeeman = Zeeman([0, 0, Hz]) sim.add(zeeman) ONE_DEGREE_PER_NS = 17453292.52 sim.relax(dt=1e-13, stopping_dmdt=0.01 * ONE_DEGREE_PER_NS, max_steps=1000, save_m_steps=100, save_vtk_steps=50) np.save('m0.npy', sim.spin)
def test_dmi_1d(): mesh = CuboidMesh(nx=2, ny=1, nz=1) sim = Sim(mesh) sim.set_m((1, 0, 0)) dmi = DMI(D=1) sim.add(dmi) field = dmi.compute_field() expected = np.array([0, 0, 0, 0, 0, 0]) assert (field == expected).all() energy = dmi.compute_energy() assert energy == 0
def test_dmi_1d_field(): mesh = CuboidMesh(nx=2, ny=1, nz=1) sim = Sim(mesh) sim.set_m(init_m) dmi = DMI(D=1.23) sim.add(dmi) field = dmi.compute_field() expected = np.array([0, -1, 0, 0, 0, -1]) * 1.23 assert np.allclose(field, expected) energy = dmi.compute_energy() assert energy == 1.23
def relax_neb(k, maxst, simname, init_im, interp, save_every=10000): """ Execute a simulation with the NEB function of the FIDIMAG code The simulations are made for a specific spring constant 'k' (a float), number of images 'init_im', interpolations between images 'interp' (an array) and a maximum of 'maxst' steps. 'simname' is the name of the simulation, to distinguish the output files. --> vtks and npys are saved in files starting with the 'simname' string """ # Prepare simulation sim = Sim(mesh, name=simname) sim.driver.gamma = const.gamma # magnetisation in units of Bohr's magneton sim.mu_s = 2. * const.mu_B # Exchange constant in Joules: E = Sum J_{ij} S_i S_j J = 12. * const.meV exch = UniformExchange(J) sim.add(exch) # DMI constant in Joules: E = Sum D_{ij} S_i x S_j D = 2. * const.meV dmi = DMI(D, dmi_type='interfacial') sim.add(dmi) # Anisotropy along +z axis ku = Anisotropy(Ku=0.5 * const.meV, axis=[0, 0, 1], name='ku') sim.add(ku) # Initial images init_images = init_im # Number of images between each state specified before (here we need only # two, one for the states between the initial and intermediate state # and another one for the images between the intermediate and final # states). Thus, the number of interpolations must always be # equal to 'the number of initial states specified', minus one. interpolations = interp neb = NEB_Sundials(sim, init_images, interpolations=interpolations, spring=k, name=simname) neb.relax(max_steps=maxst, save_vtk_steps=save_every, save_npy_steps=save_every, stopping_dmdt=1e-2)
def test_skx_num_atomistic(): """ Test the *finite spin chirality* or skyrmion number for a discrete spins simulation in a two dimensional lattice The expression is (PRL 108, 017601 (2012)) : Q = S_i \dot ( S_{i+1} X S_{j+1} ) + S_i \dot ( S_{i-1} X S_{j-1} ) which measures the chirality taking two triangles of spins per lattice site i: S_{i} , S_{i + x} , S_{i + y} and S_{i} , S_{i - x} , S_{i - y} The area of the two triangles cover a unit cell, thus the sum cover the whole area of the atomic lattice This test generate a skyrmion pointing down with unrealistic paremeters. """ mesh = CuboidMesh(nx=120, ny=120, nz=1, periodicity=(True, True, False)) sim = Sim(mesh, name='skx_num') sim.set_tols(rtol=1e-6, atol=1e-6) sim.alpha = 1.0 sim.gamma = 1.0 sim.mu_s = 1.0 sim.set_m(lambda pos: init_m(pos, 60, 60, 20)) sim.do_precession = False J = 1.0 exch = UniformExchange(J) sim.add(exch) D = 0.09 dmi = DMI(D) sim.add(dmi) zeeman = Zeeman([0, 0, 5e-3]) sim.add(zeeman) sim.relax(dt=2.0, stopping_dmdt=1e-2, max_steps=1000, save_m_steps=None, save_vtk_steps=None) skn = sim.skyrmion_number() print('skx_number', skn) assert skn > -1 and skn < -0.99
def relax_system(): # 1D chain of 50 spins with a lattice constant of 0.27 A mesh = CuboidMesh( nx=nx, dx=dx, unit_length=1e-9, # pbc='1d' ) # Initiate the simulation sim = Sim(mesh, name=sim_name) sim.gamma = const.gamma # magnetisation in units of Bohr's magneton sim.mu_s = 2 * const.mu_B # sim.set_options(gamma=const.gamma, k_B=const.k_B) # Initial magnetisation profile sim.set_m(init_m) # Exchange constant in Joules: E = Sum J_{ij} S_i S_j J = 12. * const.meV exch = UniformExchange(J) sim.add(exch) # DMI constant in Joules: E = Sum D_{ij} S_i x S_j D = 2. * const.meV dmi = DMI(D, dmi_type='interfacial') sim.add(dmi) # Anisotropy along +z axis ku = Anisotropy(Ku=0.5 * const.meV, axis=[0, 0, 1], name='ku') sim.add(ku) # Faster convergence sim.alpha = 0.5 sim.do_precession = False sim.relax(dt=1e-13, stopping_dmdt=0.05, max_steps=700, save_m_steps=1000, save_vtk_steps=1000) # Save the last relaxed state np.save(sim_name + '.npy', sim.spin)
def test_dw_dmi_atomistic(do_plot=False): mesh = CuboidMesh(nx=300, ny=1, nz=1) sim = Sim(mesh, name='relax') sim.set_default_options(gamma=const.gamma) sim.alpha = 0.5 sim.mu_s = const.mu_s_1 sim.do_precession = False sim.set_m(m_init_dw) J = 50.0 * const.k_B exch = UniformExchange(J) sim.add(exch) D = 0.01 * J dmi = DMI(D) sim.add(dmi) K = 0.005 * J anis = Anisotropy(K, axis=[1, 0, 0]) sim.add(anis) ONE_DEGREE_PER_NS = 17453292.52 sim.relax(dt=1e-13, stopping_dmdt=0.01 * ONE_DEGREE_PER_NS, max_steps=1000, save_m_steps=100, save_vtk_steps=50) np.save('m0.npy', sim.spin) xs = np.array([p[0] for p in mesh.coordinates]) - 150 mx, my, mz = analytical(xs, A=J / 2.0, D=-D, K=K) mxyz = sim.spin.copy() mxyz = mxyz.reshape(-1, 3).T assert max(abs(mxyz[0, :] - mx)) < 0.001 assert max(abs(mxyz[1, :] - my)) < 0.001 assert max(abs(mxyz[2, :] - mz)) < 0.0006 if do_plot: save_plot(xs, mxyz, mx, my, mz)
def create_simulation(R, B): mu_s = 3 sim_hexagon = HexagonSim( R, # R 0.2715, # a mu_s, # mu_s name='unnamed') sim = sim_hexagon.sim # mask = (sim.mu_s / C.mu_B) > 1e-5 exch = UniformExchange(5.881 * C.meV) sim.add(exch) dmi = DMI(D=1.557 * C.meV, dmi_type='interfacial') sim.add(dmi) sim.add(Zeeman([0., 0., B])) sim.add(Anisotropy(0.406 * C.meV, axis=[0, 0, 1])) return sim
def relax_system(mesh): sim = Sim(mesh, name='relax') sim.set_default_options(gamma=const.gamma) sim.alpha = 0.5 sim.mu_s = const.mu_s_1 sim.do_procession = False sim.set_m(m_init_dw) J = 50.0 * const.k_B exch = UniformExchange(J) sim.add(exch) D = 0.1 * J dmi = DMI(D, dmi_type='interfacial') sim.add(dmi) K = 0.02 * J anis = Anisotropy(K, axis=[0, 0, 1]) sim.add(anis) ONE_DEGREE_PER_NS = 17453292.52 sim.relax(dt=1e-13, stopping_dmdt=0.01 * ONE_DEGREE_PER_NS, max_steps=1000, save_m_steps=100, save_vtk_steps=50) np.save('m0.npy', sim.spin) xs = np.array([p[0] for p in mesh.pos]) - 150 mx, my, mz = analytical(xs, A=J / 2.0, D=-D, K=K) mxyz = sim.spin.copy() mxyz.shape = (3, -1) save_plot(xs, mxyz, mx, my, mz)
def relax_system(mesh): sim = Sim(mesh, name='relax') sim.alpha = 0.1 sim.set_m(init_m) J = 1 exch = UniformExchange(J) sim.add(exch) dmi = DMI(0.05 * J) sim.add(dmi) ts = np.linspace(0, 1, 11) for t in ts: print t, sim.spin_length() - 1 sim.run_until(t) sim.save_vtk() return sim.spin
def relax_system(mesh): sim = Sim(mesh, name='dmi_2d') sim.driver.alpha = 0.1 sim.driver.gamma=1.76e11 sim.mu_s = 1e-22 J = 1e-20 exch = UniformExchange(J) sim.add(exch) dmi = DMI(0.1 * J) sim.add(dmi) sim.set_m(init_m) ts = np.linspace(0, 5e-10, 101) for t in ts: print(t) sim.run_until(t) #sim.save_vtk() return sim.spin
def create_sim(): mesh = CuboidMesh(nx=121, ny=121, nz=1) sim = Sim(mesh, name='relax') sim.alpha = 1.0 sim.gamma = 0.5 sim.mu_s = mu_s sim.set_m(init_m) J = 1.0 exch = UniformExchange(J) sim.add(exch) D = 0.08 dmi = DMI(D) sim.add(dmi) K = 4e-3 anis = Anisotropy(K, direction=(0, 0, 1), name='Ku') sim.add(anis) return sim
def simulation(nx, ny, nz, dx, dy, dz, j, d, kc, bz, mu_s, sim_name, add_image=None, add_interpolations=None, add_images_folder=None, stopping_dydt=None, max_steps=None, save_m_every=None, integrator='sundials', integrator_stepsize=1e-3, interpolation_method='rotation', variable_spring_forces=None, interpolation_energy='polynomial'): if add_interpolations: if (len(add_image) - 1) != len(add_interpolations): raise Exception('(N interpolations) != (N images - 1)') mesh = CuboidMesh(nx=nx, ny=ny, nz=nz, dx=dx, dy=dy, dz=dz, x0=-nx * 0.5, y0=-ny * 0.5, z0=-nz * 0.5, unit_length=1., periodicity=(True, True, False)) # Simulation sim = Sim(mesh, name=sim_name) sim.mu_s = mu_s sim.add(Exchange(j)) sim.add(DMI(d, dmi_type='bulk')) sim.add(Zeeman((0.0, 0.0, bz * 1e-3))) if np.abs(kc) > 0.0: sim.add(CubicAnisotropy(kc)) # GNEBM simulation ........................................................ if add_interpolations: # Set the initial images from the list init_images = [np.load(image) for image in add_image] interpolations = [i for i in add_interpolations] elif add_images_folder: if add_images_folder.endswith('_LAST'): dir_list = glob.glob(add_images_folder[:-5] + '*') dir_list = sorted( dir_list, key=lambda f: int(re.search(r'(?<=_)[0-9]+$', f).group(0))) add_images_folder = dir_list[-1] flist = sorted(os.listdir(add_images_folder)) init_images = [ np.load(os.path.join(add_images_folder, image)) for image in flist ] interpolations = [] else: raise Exception('Specify an option to add images') # Start a NEB simulation passing the Simulation object and all the NEB # parameters string = StringMethod(sim, init_images, interpolations=interpolations, name=sim_name, openmp=True, integrator=integrator, interpolation_method=interpolation_method) if integrator == 'verlet': string.integrator.mass = 1 string.integrator.stepsize = integrator_stepsize dt = integrator_stepsize * 10 else: dt = integrator_stepsize # ......................................................................... for fdir in ['interpolations', 'ndts']: if not os.path.exists(fdir): os.makedirs(fdir) # Finally start the energy band relaxation string.relax(max_iterations=max_steps, save_vtks_every=save_m_every, save_npys_every=save_m_every, stopping_dYdt=stopping_dydt, dt=dt) # Produce a file with the data from a cubic interpolation for the band interp_data = np.zeros((200, 2)) if interpolation_energy == 'polynomial': string.compute_polynomial_factors() (interp_data[:, 0], interp_data[:, 1]) = string.compute_polynomial_approximation_energy(200) elif interpolation_energy == 'Bernstein': string.compute_Bernstein_polynomials() (interp_data[:, 0], interp_data[:, 1]) = string.compute_Bernstein_approximation_energy(200) else: raise Exception('No valid interpolation method') np.savetxt('interpolations/{}_interpolation.dat'.format(sim_name), interp_data) # Clean files shutil.move(sim_name + '_energy.ndt', 'ndts/{}_energy.ndt'.format(sim_name)) shutil.move(sim_name + '_dYs.ndt', 'ndts/{}_dYs.ndt'.format(sim_name))
# Initiate Fidimag simulation ------------------------------------------------- sim = Sim(mesh, name=args.sim_name) # sim.set_tols(rtol=1e-10, atol=1e-14) if args.alpha: sim.driver.alpha = args.alpha # sim.gamma = 2.211e5 # Material parameters --------------------------------------------------------- sim.mu_s = args.mu_s * const.mu_B exch = UniformExchange(args.J * const.meV) sim.add(exch) dmi = DMI(D=(args.D * const.meV), dmi_type='interfacial') sim.add(dmi) if args.B: zeeman = Zeeman((0, 0, args.B)) sim.add(zeeman, save_field=True) if args.k_u: # Uniaxial anisotropy along + z-axis sim.add(Anisotropy(args.k_u * const.meV, axis=[0, 0, 1])) if args.Demag: sim.add(DemagHexagonal()) # -----------------------------------------------------------------------------
def test_skx_num_atomistic(): """ Test the *finite spin chirality* or skyrmion number for a discrete spins simulation in a two dimensional lattice The expression is (PRL 108, 017601 (2012)) : Q = S_i \dot ( S_{i+1} X S_{j+1} ) + S_i \dot ( S_{i-1} X S_{j-1} ) which measures the chirality taking two triangles of spins per lattice site i: S_{i} , S_{i + x} , S_{i + y} and S_{i} , S_{i - x} , S_{i - y} The area of the two triangles cover a unit cell, thus the sum cover the whole area of the atomic lattice We also test the Berg and Luscher definition for a topological charge (see the hexagonal mesh test for details) in a square lattice. This test generate a skyrmion pointing down with unrealistic paremeters. """ mesh = CuboidMesh(nx=120, ny=120, nz=1, periodicity=(True, True, False)) sim = Sim(mesh, name='skx_num') sim.driver.set_tols(rtol=1e-6, atol=1e-6) sim.driver.alpha = 1.0 sim.driver.gamma = 1.0 sim.mu_s = 1.0 sim.set_m(lambda pos: init_m(pos, 60, 60, 20)) sim.do_precession = False J = 1.0 exch = UniformExchange(J) sim.add(exch) D = 0.09 dmi = DMI(D) sim.add(dmi) zeeman = Zeeman([0, 0, 5e-3]) sim.add(zeeman) sim.relax(dt=2.0, stopping_dmdt=1e-2, max_steps=1000, save_m_steps=None, save_vtk_steps=None) skn = sim.skyrmion_number() print('skx_number', skn) skn_BL = sim.skyrmion_number(method='BergLuscher') print('skx_number_BergLuscher', skn_BL) # Test the finite chirality method assert skn > -1 and skn < -0.99 # Test the Berg-Luscher method assert np.abs(skn_BL - (-1)) < 1e-4 and np.sign(skn_BL) < 0 # Test guiding center Rx, Ry = compute_RxRy(mesh, sim.spin) print('Rx=%g, Ry=%g' % (Rx, Ry)) assert Rx < 60 and Rx > 58 assert Ry < 60 and Ry > 58
def test_skx_num_atomistic_hexagonal(): """ Test the topological charge or skyrmion number for a discrete spins simulation in a two dimensional hexagonal lattice, using Berg and Luscher definition in [Nucl Phys B 190, 412 (1981)] and simplified in [PRB 93, 174403 (2016)], which maps a triangulated lattice (using triangles of neighbouring spins) area into a unit sphere. The areas of two triangles per lattice site cover a unit cell, thus the sum cover the whole area of the atomic lattice This test generates a skyrmion pointing down and two skyrmions pointing up in a PdFe sample using magnetic parameters from: PRL 114, 177203 (2015) """ mesh = HexagonalMesh(0.2715, 41, 41, periodicity=(True, True)) sim = Sim(mesh, name='skx_number_hexagonal') sim.driver.set_tols(rtol=1e-6, atol=1e-6) sim.driver.alpha = 1.0 sim.driver.gamma = 1.0 sim.mu_s = 3 * const.mu_B sim.set_m(lambda pos: init_m(pos, 16.1, 10, 2)) sim.driver.do_precession = False J = 5.881 * const.meV exch = UniformExchange(J) sim.add(exch) D = 1.557 * const.meV dmi = DMI(D, dmi_type='interfacial') sim.add(dmi) sim.add(Anisotropy(0.406 * const.meV, axis=[0, 0, 1])) zeeman = Zeeman([0, 0, 2.5]) sim.add(zeeman) sim.relax(dt=1e-13, stopping_dmdt=1e-2, max_steps=2000, save_m_steps=None, save_vtk_steps=100) skn_single = sim.skyrmion_number(method='BergLuscher') print('skx_number_hexagonal', skn_single) # Now we generate two skyrmions pointing up sim.driver.reset_integrator() sim.set_m( lambda pos: init_m_multiple_sks(pos, 1, sk_pos=[(9, 6), (18, 12)])) sim.get_interaction('Zeeman').update_field([0, 0, -2.5]) sim.relax(dt=1e-13, stopping_dmdt=1e-2, max_steps=2000, save_m_steps=None, save_vtk_steps=None) skn_two = sim.skyrmion_number(method='BergLuscher') print('skx_number_hexagonal_two', skn_two) # Check that we get a right sk number assert np.abs(skn_single - (-1)) < 1e-4 and np.sign(skn_single) < 0 assert np.abs(skn_two - (2)) < 1e-4 and np.sign(skn_two) > 0
nz=nz, dx=dx, dy=dy, dz=dz, x0=-nx * 0.5, y0=-ny * 0.5, z0=-nz * 0.5, unit_length=1., periodicity=(True, True, False)) sim_name = 'sk_helix' sim = Sim(mesh, name=sim_name, integrator='sundials_openmp') sim.mu_s = 1 sim.add(Exchange(1)) sim.add(DMI(0.727, dmi_type='bulk')) bz_min = FIELD sim.add(Zeeman((0.0, 0.0, bz_min * 1e-3)), save_field=True) kc = -0.05 if np.abs(kc) > 0.0: sim.add(CubicAnisotropy(kc)) # ......................................................................... sim.driver.alpha = 0.9 sim.driver.do_precession = False # Zeeman_int = sim.get_interaction('Zeeman') # Zeeman_int.update_field((0.0, 0.0, B_sweep * 1e-3)) H_BG = np.load(
def generate_simulation(self, do_precession=False, gamma=1.76e11, load_mu_s=False): """ Generates a simulation according to the self.simulation option gamma :: Default is the free electron gyromagnetic ratio load_mu_s :: A file path to a NPY file with the mu_s information (only for the experimental_sample option) """ # Mesh ---------------------------------------------------------------- if self.simulation == 'experimental_sample': self.sim_from_image = sfi.sim_from_image(sfi.default_image) if not load_mu_s: self.sim_from_image.generate_magnetic_moments(mu_s=self.mu_s) else: self.sim_from_image.generate_magnetic_moments( load_file=load_mu_s) self.sim = self.sim_from_image.sim elif self.simulation == '2D_square': # A square sized hexagonal mesh mesh = HexagonalMesh( self.mesh_a * 0.5, self.mesh_nx, self.mesh_ny, # periodicity=(True, True), alignment='square', unit_length=1e-9) self.sim = Sim(mesh) # If we use polygon mesh tools, we can use a hexagon shaped mesh # self.sim.mu_s = self.mu_s_in_hexagon self.sim.mu_s = self.mu_s elif self.simulation == '1D_chain': # A 1D chain using a cuboid mesh mesh = CuboidMesh( dx=self.mesh_a, nx=self.mesh_nx, ny=1, nz=1, # periodicity=(True, True), unit_length=1e-9) self.sim = Sim(mesh) # If we use polygon mesh tools, we can use a hexagon shaped mesh # self.sim.mu_s = self.mu_s_in_hexagon self.sim.mu_s = self.mu_s self.sim.driver.do_precession = do_precession self.sim.driver.gamma = gamma # Interactions -------------------------------------------------------- exch = UniformExchange(self.J) self.sim.add(exch) dmi = DMI(D=(self.D), dmi_type='interfacial') self.sim.add(dmi) zeeman = Zeeman((self.B[0], self.B[1], self.B[2])) self.sim.add(zeeman, save_field=True) if self.ku: # Uniaxial anisotropy along + z-axis self.sim.add(Anisotropy(self.ku, axis=[0, 0, 1])) if self.Demag: print('Using Demag!') self.sim.add(DemagHexagonal()) # --------------------------------------------------------------------- self.hls = np.ones_like(self.sim.spin.reshape(-1, 3)) self.rgbs = np.ones((self.sim.spin.reshape(-1, 3).shape[0], 4))
def simulation(nx, ny, nz, dx, dy, dz, j, d, kc, bz, mu_s, sim_name, add_image=None, add_interpolations=None, add_images_folder=None, spring_constant=None, stopping_dydt=None, max_steps=None, keep_sim_climbing_image_steps=None, save_m_every=None, climbing_image=None, keep_sim_climbing_image=None, keep_sim_climbing_image_again=None, integrator='sundials', integrator_stepsize=1e-3, interpolation_method='rotation', variable_spring_forces=None, interpolation_energy='polynomial'): if add_interpolations: if (len(add_image) - 1) != len(add_interpolations): raise Exception('(N interpolations) != (N images - 1)') mesh = CuboidMesh(nx=nx, ny=ny, nz=nz, dx=dx, dy=dy, dz=dz, x0=-nx * 0.5, y0=-ny * 0.5, z0=-nz * 0.5, unit_length=1., periodicity=(True, True, False)) # Simulation sim = Sim(mesh, name=sim_name) sim.mu_s = mu_s sim.add(Exchange(j)) sim.add(DMI(d, dmi_type='bulk')) sim.add(Zeeman((0.0, 0.0, bz * 1e-3))) if np.abs(kc) > 0.0: sim.add(CubicAnisotropy(kc)) # GNEBM simulation ........................................................ if add_interpolations: # Set the initial images from the list init_images = [np.load(image) for image in add_image] interpolations = [i for i in add_interpolations] elif add_images_folder: if add_images_folder.endswith('_LAST'): dir_list = glob.glob(add_images_folder[:-5] + '*') dir_list = sorted( dir_list, key=lambda f: int(re.search(r'(?<=_)[0-9]+$', f).group(0))) add_images_folder = dir_list[-1] flist = sorted(os.listdir(add_images_folder)) init_images = [ np.load(os.path.join(add_images_folder, image)) for image in flist ] interpolations = [] else: raise Exception('Specify an option to add images') if climbing_image: if climbing_image.isdigit(): climbing_image = int(climbing_image) else: try: energies = np.loadtxt(climbing_image)[-1][1:] energies = energies - energies[0] climbing_image = np.where(energies == np.max(energies))[0][0] except OSError: raise Exception('Err trying to compute climb image from file') print('Using climbing image: {}'.format(climbing_image)) # Start a NEB simulation passing the Simulation object and all the NEB # parameters neb = NEBM_Geodesic(sim, init_images, interpolations=interpolations, spring_constant=spring_constant, name=sim_name, openmp=True, climbing_image=climbing_image, integrator=integrator, interpolation_method=interpolation_method) if integrator == 'verlet': neb.integrator.mass = 1 neb.integrator.stepsize = integrator_stepsize dt = integrator_stepsize * 10 else: dt = integrator_stepsize # ......................................................................... for fdir in ['interpolations', 'ndts']: if not os.path.exists(fdir): os.makedirs(fdir) # Finally start the energy band relaxation neb.relax(max_iterations=max_steps, save_vtks_every=save_m_every, save_npys_every=save_m_every, stopping_dYdt=stopping_dydt, dt=dt) if variable_spring_forces: neb.variable_k = True neb.dk = spring_constant * 0.9 neb.relax(max_iterations=max_steps, save_vtks_every=save_m_every, save_npys_every=save_m_every, stopping_dYdt=stopping_dydt, dt=dt, save_initial_state=False) # Continue with climbing image if specified if keep_sim_climbing_image: if not keep_sim_climbing_image_steps: keep_sim_climbing_image_steps = max_steps # Find all local maxima largest_E_im_idx = [] for i in range(2, neb.n_images - 2): if (neb.energies[i] > neb.energies[i - 1] and neb.energies[i - 1] > neb.energies[i - 2] and neb.energies[i] > neb.energies[i + 1] and neb.energies[i + 1] > neb.energies[i + 2]): largest_E_im_idx.append(i) elif (neb.energies[i] < neb.energies[i - 1] and neb.energies[i - 1] < neb.energies[i - 2] and neb.energies[i] < neb.energies[i + 1] and neb.energies[i + 1] < neb.energies[i + 2]): largest_E_im_idx.append(-i) neb.climbing_image = largest_E_im_idx print('Continuing simulation with CI = {}'.format(largest_E_im_idx)) neb.name += '_CI' # Data will be appended to the initial NDT file, at least we create a # new one with the CI appended to the sim name: # neb.create_tablewriter() # last_step_relax = neb.iterations # Variable dk for better resolution around saddle points # neb.variable_k = True # neb.dk = spring_constant neb.relax(max_iterations=keep_sim_climbing_image_steps, save_vtks_every=save_m_every, save_npys_every=save_m_every, stopping_dYdt=keep_sim_climbing_image, dt=dt, save_initial_state=False) # Remove initial state (same as final state from the prev relaxation) # shutil.rmtree('vtks/{}_{}'.format(neb.name, last_step_relax)) # Continue with climbing image if specified if keep_sim_climbing_image_again: if not keep_sim_climbing_image_steps: keep_sim_climbing_image_steps = max_steps # Find all local maxima largest_E_im_idx = [] for i in range(2, neb.n_images - 2): if (neb.energies[i] > neb.energies[i - 1] and neb.energies[i - 1] > neb.energies[i - 2] and neb.energies[i] > neb.energies[i + 1] and neb.energies[i + 1] > neb.energies[i + 2]): largest_E_im_idx.append(i) elif (neb.energies[i] < neb.energies[i - 1] and neb.energies[i - 1] < neb.energies[i - 2] and neb.energies[i] < neb.energies[i + 1] and neb.energies[i + 1] < neb.energies[i + 2]): largest_E_im_idx.append(-i) neb.climbing_image = largest_E_im_idx print('Continuing simulation with CI = {}'.format(largest_E_im_idx)) neb.name += '_CI' neb.relax(max_iterations=keep_sim_climbing_image_steps, save_vtks_every=save_m_every, save_npys_every=save_m_every, stopping_dYdt=keep_sim_climbing_image_again, dt=dt, save_initial_state=False) # Produce a file with the data from a cubic interpolation for the band interp_data = np.zeros((200, 2)) if interpolation_energy == 'polynomial': neb.compute_polynomial_factors() (interp_data[:, 0], interp_data[:, 1]) = neb.compute_polynomial_approximation_energy(200) elif interpolation_energy == 'Bernstein': neb.compute_Bernstein_polynomials() (interp_data[:, 0], interp_data[:, 1]) = neb.compute_Bernstein_approximation_energy(200) else: raise Exception('No valid interpolation method') np.savetxt('interpolations/{}_interpolation.dat'.format(sim_name), interp_data) # Clean files shutil.move(sim_name + '_energy.ndt', 'ndts/{}_energy.ndt'.format(sim_name)) shutil.move(sim_name + '_dYs.ndt', 'ndts/{}_dYs.ndt'.format(sim_name))
def simulation(nx, ny, nz, dx, dy, dz, j, d, kc, mu_s, sim_name, bz_min, bz_max, bz_steps, bz_hysteresis=False, initial_state_one_bobber=None, initial_state_two_bobbers=None, initial_state_two_bobbers_asymm=None, initial_state_sk_tube=None, initial_state_one_dim_mod=None, initial_state_helix_angle_x=(None, None), stopping_dmdt=1e-5, max_steps=4000, save_initial_state=None): mesh = CuboidMesh(nx=nx, ny=ny, nz=nz, dx=dx, dy=dy, dz=dz, x0=-nx * 0.5, y0=-ny * 0.5, z0=-nz * 0.5, unit_length=1., periodicity=(True, True, False)) # J = 1. # D = 0.628 # L_D = 2 PI a J / D = 10 * a => D / J = 2 PI / 10. # Bz = 0.2 # B_z == (B_z mu_s / J) # mu_s = 1. sim = Sim(mesh, name=sim_name, integrator='sundials_openmp') sim.mu_s = mu_s sim.add(Exchange(j)) sim.add(DMI(d, dmi_type='bulk')) sim.add(Zeeman((0.0, 0.0, bz_min * 1e-3)), save_field=True) if np.abs(kc) > 0.0: sim.add(CubicAnisotropy(kc)) # ......................................................................... sim.driver.alpha = 0.9 sim.driver.do_precession = False if not os.path.exists('npys/{}'.format(sim_name)): os.makedirs('npys/{}'.format(sim_name)) if not os.path.exists('txts'): os.makedirs('txts') for i, B_sweep in enumerate(np.linspace(bz_min, bz_max, bz_steps)): print('Bz = {:.0f} mT '.format(B_sweep).ljust(80, '-')) Zeeman_int = sim.get_interaction('Zeeman') Zeeman_int.update_field((0.0, 0.0, B_sweep * 1e-3)) if (not bz_hysteresis) or (bz_hysteresis and i < 1): if initial_state_one_dim_mod: sim.set_m(lambda r: one_dim_mod(r, B_sweep * 1e-3, d)) elif initial_state_helix_angle_x[0]: angle = initial_state_helix_angle_x[0] * np.pi / 180. periodic = initial_state_helix_angle_x[1] sim.set_m(lambda r: helix_angle_x(r, angle, periodic)) elif initial_state_sk_tube: sk_rad = initial_state_sk_tube sim.set_m( lambda r: sk_tube(r, B_sweep * 1e-3, d, sk_rad=sk_rad)) elif initial_state_two_bobbers: bobber_length = initial_state_two_bobbers sim.set_m(lambda r: two_bobbers(r, B_sweep * 1e-3, d, mesh.Lz, bobber_rad=3., bobber_length=bobber_length)) elif initial_state_two_bobbers_asymm: bobber_length = initial_state_two_bobbers_asymm sim.set_m( lambda r: two_bobbers_asymm(r, B_sweep * 1e-3, d, mesh.Lz, bobber_rad=3., bobber_length=bobber_length)) elif initial_state_one_bobber: bobber_length = initial_state_one_bobber sim.set_m(lambda r: one_bobber(r, B_sweep * 1e-3, d, mesh.Lz, bobber_rad=3., bobber_length=bobber_length)) else: raise Exception('Not a valid initial state') if save_initial_state: save_vtk(sim, sim_name + '_INITIAL', field_mT=B_sweep) name = 'npys/{}/m_{}_INITIAL_Bz_{:06d}.npy'.format( sim.driver.name, sim_name, int(B_sweep)) np.save(name, sim.spin) # ..................................................................... sim.relax(stopping_dmdt=stopping_dmdt, max_steps=max_steps, save_m_steps=None, save_vtk_steps=None) # ..................................................................... save_vtk(sim, sim_name, field_mT=B_sweep) name = 'npys/{}/m_{}_Bz_{:06d}.npy'.format(sim.driver.name, sim_name, int(B_sweep)) np.save(name, sim.spin) sim.driver.reset_integrator() shutil.move(sim_name + '.txt', 'txts/{}.txt'.format(sim_name))