def example3(Ms): x0 = y0 = z0 = 0 x1 = 500 y1 = 10 z1 = 500 nx = 50 ny = 1 nz = 1 mesh = df.Box(x0, y0, z0, x1, y1, z1, nx, ny, nz) sim = Sim(mesh, Ms, unit_length=1e-9) sim.alpha = 0.01 sim.set_m((1, 0, 0.1)) H_app = Zeeman((0, 0, 5e5)) sim.add(H_app) exch = fe.Exchange(13.0e-12) sim.add(exch) demag = Demag(solver="FK") sim.add(demag) llg = sim.llg max_time = 1 * np.pi / (llg.gamma * 1e5) ts = np.linspace(0, max_time, num=100) for t in ts: print t sim.run_until(t) df.plot(llg._m) df.interactive()
def ring_down(m0=(1, 0.01, 0), pbc=None): n = 49 assert n >= 1 and n % 2 == 1 Ms = 8.6e5 sim = Simulation(mesh, Ms, unit_length=1e-9, name='dy', pbc='1d') sim.alpha = 1e-3 sim.set_m(m0) sim.set_tol(1e-8, 1e-8) parameters = { 'absolute_tolerance': 1e-10, 'relative_tolerance': 1e-10, 'maximum_iterations': int(1e5) } Ts = [] for i in range(-n / 2 + 1, n / 2 + 1): Ts.append((10. * i, 0, 0)) demag = Demag(Ts=Ts) demag.parameters['phi_1'] = parameters demag.parameters['phi_2'] = parameters sim.add(demag) sim.add(Exchange(1.3e-11)) sim.schedule('save_ndt', every=2e-12) sim.run_until(4e-9)
def test_zhangli(): #mesh = df.BoxMesh(df.Point(0, 0, 0), df.Point(100, 1, 1), 50, 1, 1) mesh = df.IntervalMesh(50, 0, 100) sim = Sim(mesh, Ms=8.6e5, unit_length=1e-9, kernel='llg_stt') sim.set_m(init_m) sim.add(UniaxialAnisotropy(K1=520e3, axis=[1, 0, 0])) sim.add(Exchange(A=13e-12)) sim.alpha = 0.01 sim.llg.set_parameters(J_profile=init_J, speedup=50) p0 = sim.m_average sim.run_until(5e-12) p1 = sim.m_average print sim.integrator.stats() print p0, p1 sim.run_until(1e-11) p1 = sim.m_average print sim.integrator.stats() print p0, p1 assert p1[0] < p0[0] assert abs(p0[0]) < 1e-15 assert abs(p1[0]) > 1e-3
def run_finmag(demagsolver): """Run the finmag simulation and store data in (demagsolvertype)averages.txt.""" sim = Sim(mesh, Ms, unit_length=unit_length) sim.alpha = 0.5 sim.set_m((1, 0, 1)) exchange = Exchange(13.0e-12) sim.add(exchange) demag = Demag(solver=demagsolver) sim.add(demag) fh = open(os.path.join(MODULE_DIR, demagsolver + "averages.txt"), "w") fe = open(os.path.join(MODULE_DIR, demagsolver + "energies.txt"), "w") # Progressbar bar = pb.ProgressBar(maxval=60, \ widgets=[pb.ETA(), pb.Bar('=', '[', ']'), ' ', pb.Percentage()]) logger.info("Time integration") times = np.linspace(0, 3.0e-10, 61) #times = np.linspace(0, 3.0e-10, 100000) for counter, t in enumerate(times): bar.update(counter) # Integrate sim.run_until(t) print counter print("press return to continue") _ = raw_input() # Save averages to file mx, my, mz = sim.m_average fh.write(str(t) + " " + str(mx) + " " + str(my) + " " + str(mz) + "\n") # Energies E_e = exchange.compute_energy() E_d = demag.compute_energy() fe.write(str(E_e) + " " + str(E_d) + "\n") # Energy densities if counter == 10: exch_energy = exchange.energy_density_function() demag_energy = demag.demag.energy_density_function() finmag_exch, finmag_demag = [], [] R = range(100) for i in R: finmag_exch.append(exch_energy([15, 15, i])) finmag_demag.append(demag_energy([15, 15, i])) # Store data np.save( os.path.join(MODULE_DIR, "finmag%s_exch_density.npy" % demagsolver), np.array(finmag_exch)) np.save( os.path.join(MODULE_DIR, "finmag%s_demag_density.npy" % demagsolver), np.array(finmag_demag)) fh.close() fe.close()
def run_simulation(): L = 3e-8 W = 1e-8 H = 1e-8 mesh = df.BoxMesh(df.Point(0, 0, 0), df.Point(L, W, H), 10, 4, 4) Ms = 0.86e6 # A/m A = 1.3e-11 # J/m sim = Sim(mesh, Ms) sim.set_m(("2*x[0]/L - 1", "2*x[1]/W - 1", "1"), L=3e-8, H=1e-8, W=1e-8) sim.alpha = 0.1 sim.add(Zeeman((Ms / 2, 0, 0))) sim.add(Exchange(A)) t = 0 dt = 1e-11 tmax = 1e-9 # s fh = open(os.path.join(MODULE_DIR, "averages.txt"), "w") while t <= tmax: mx, my, mz = sim.m_average fh.write(str(t) + " " + str(mx) + " " + str(my) + " " + str(mz) + "\n") t += dt sim.run_until(t) fh.close()
def run_sim_with_stt(): sim = Sim(mesh, Ms=8.6e5) sim.set_m((1, 0.01, 0.01)) sim.alpha = 0.014 sim.gamma = 221017 H_app_mT = np.array([0.2, 0.2, 10.0]) H_app_SI = H_app_mT / (1000 * mu0) sim.add(Zeeman(tuple(H_app_SI))) sim.add(Exchange(1.3e-11)) sim.add(UniaxialAnisotropy(1e5, (0, 0, 1))) I = 5e-5 # current in A J = I / (L * W) # current density in A/m^2 theta = 40.0 * pi / 180 phi = pi / 2 # polarisation direction p = (sin(theta) * cos(phi), sin(theta) * sin(phi), cos(theta)) sim.llg.use_slonczewski(J=J, P=0.4, d=5e-9, p=(0, 1, 0)) with open(averages_with, "w") as f: dt = 5e-12 t_max = 10e-9 for t in np.arange(0, t_max, dt): sim.run_until(t) f.write("{} {} {} {}\n".format(t, *sim.m_average))
def relax_system(): Ms = 8.6e5 sim = Simulation(mesh, Ms, unit_length=1e-9, name = 'dy') sim.alpha = 0.001 sim.set_m(np.load('m_000088.npy')) #sim.set_tol(1e-6, 1e-6) A = 1.3e-11 sim.add(Exchange(A)) parameters = { 'absolute_tolerance': 1e-10, 'relative_tolerance': 1e-10, 'maximum_iterations': int(1e5) } demag = Demag() demag.parameters['phi_1'] = parameters demag.parameters['phi_2'] = parameters print demag.parameters sim.add(demag) sim.schedule('save_ndt', every=2e-12) sim.schedule('save_vtk', every=2e-12, filename='vtks/m.pvd') sim.schedule('save_m', every=2e-12, filename='npys/m.pvd') sim.run_until(1e-9)
def run_simulation(): L = W = 12.5e-9 H = 2.5e-9 sim = Sim(mesh, Ms=8.6e5, unit_length=1e-9) sim.set_m((1, 0.01, 0.01)) sim.alpha = 0.014 sim.gamma = 221017 H_app_mT = np.array([0.0, 0.0, 10.0]) H_app_SI = H_app_mT / (1000 * mu0) sim.add(Zeeman(tuple(H_app_SI))) sim.add(Exchange(1.3e-11)) I = 5e-5 # current in A J = I / (L * W) # current density in A/m^2 theta = 40.0 * pi / 180 phi = pi / 2 # polarisation direction p = (sin(theta) * cos(phi), sin(theta) * sin(phi), cos(theta)) sim.llg.use_slonczewski(J=J, P=0.4, d=H, p=p) with open(averages_file, "w") as f: dt = 10e-12 t_max = 10e-9 for t in np.arange(0, t_max, dt): sim.run_until(t) f.write("{} {} {} {}\n".format(t, *sim.m_average))
def macrospin(Ms=0.86e6, m_init=(1, 0, 0), H_ext=(0, 0, 1e6), alpha=0.1, name='macrospin'): """ Minimal mesh with two vertices (1 nm apart). No anisotropy, exchange coupling or demag is present so that magnetic moments at the two vertices behave identical under the influence of the external field. (Ideally, we would only have a single vertex but Dolfin doesn't support this.) Default values for the arguments: Ms = 0.86e6 (saturation magnetisation in A/m) m_init = (1, 0, 0) (initial magnetisation pointing along the x-axis) H_ext = (0, 0, 1e6) (external field in A/m) alpha = 0.1 (Gilbert damping coefficient) """ mesh = df.UnitIntervalMesh(1) sim = Simulation(mesh, Ms=Ms, unit_length=1e-9, name=name) sim.alpha = alpha sim.set_m(m_init) sim.add(Zeeman(H_ext)) return sim
def setup_module(module=None): # define the mesh x_max = 20e-9 # m simplexes = 10 mesh = IntervalMesh(simplexes, 0, x_max) def m_gen(coords): x = coords[0] mx = min(1.0, 2.0 * x / x_max - 1.0) my = np.sqrt(1.0 - mx**2) mz = 0.0 return np.array([mx, my, mz]) Ms = 0.86e6 A = 1.3e-11 global sim sim = Sim(mesh, Ms) sim.alpha = 0.2 sim.set_m(m_gen) sim.pins = [0, 10] exchange = Exchange(A) sim.add(exchange) # Save H_exc and m at t0 for comparison with nmag global H_exc_t0, m_t0 H_exc_t0 = exchange.compute_field() m_t0 = sim.m t = 0 t1 = 5e-10 dt = 1e-11 # s av_f = open(os.path.join(MODULE_DIR, "averages.txt"), "w") tn_f = open(os.path.join(MODULE_DIR, "third_node.txt"), "w") global averages averages = [] global third_node third_node = [] while t <= t1: mx, my, mz = sim.m_average averages.append([t, mx, my, mz]) av_f.write( str(t) + " " + str(mx) + " " + str(my) + " " + str(mz) + "\n") mx, my, mz = h.components(sim.m) m2x, m2y, m2z = mx[2], my[2], mz[2] third_node.append([t, m2x, m2y, m2z]) tn_f.write( str(t) + " " + str(m2x) + " " + str(m2y) + " " + str(m2z) + "\n") t += dt sim.run_until(t) av_f.close() tn_f.close()
def create_initial_state(): print "Creating initial relaxed state." sim = Sim(mesh, Ms=Ms, unit_length=1e-9) sim.set_m(m_gen) sim.alpha = 0.5 sim.add(Exchange(1.3e-11)) sim.add(Demag()) sim.relax() np.savetxt(initial_m_file, sim.m)
def test_deviations_over_alpha_and_tol(number_of_alphas=5, do_plot=False): alphas = numpy.linspace(0.01, 1.00, number_of_alphas) max_deviationss = [] for rtol_power_of_ten in rtols_powers_of_ten: rtol = pow(10, rtol_power_of_ten) print "#### New series for rtol={0}. ####".format(rtol) # One entry in this array corresponds to the maximum deviation between # the analytical solution and the computed solution for one value of alpha. max_deviations = [] for alpha in alphas: print "Solving for alpha={0}.".format(alpha) sim = Simulation(mesh, 1, unit_length=1e-9) sim.alpha = alpha sim.set_m((1, 0, 0)) sim.add(Zeeman((0, 0, 1e5))) ts = numpy.linspace(0, 1e-9, num=50) ys = odeint(sim.llg.solve_for, sim.llg._m_field.get_numpy_array_debug(), ts, rtol=rtol, atol=rtol) # One entry in this array corresponds to the deviation between the two # solutions for one particular moment during the simulation. deviations = [] M_analytical = make_analytic_solution(1e5, alpha, sim.gamma) for i in range(len(ts)): M_computed = numpy.mean(ys[i].reshape((3, -1)), 1) M_ref = M_analytical(ts[i]) # The difference of the two vectors has 3 components. The # deviation is the average over these components. deviation = numpy.mean(numpy.abs(M_computed - M_ref)) assert deviation < TOLERANCE deviations.append(deviation) # This represents the addition of one point to the graph. max_deviations.append(numpy.max(deviations)) # This represents one additional series in the graph. max_deviationss.append(max_deviations) if do_plot: for i in range(len(rtols_powers_of_ten)): label = r"$rtol=1\cdot 10^{" + str(rtols_powers_of_ten[i]) + r"}$" plt.plot(alphas, max_deviationss[i], ".", label=label) plt.legend() plt.title(r"Influence of $\alpha$ and rtol on the Deviation") plt.ylabel("deviation") plt.xlabel(r"$\alpha$") plt.ylim((0, 1e-6)) plt.savefig(os.path.join(MODULE_DIR, "deviation_over_alpha_rtols.pdf"))
def run_simulation(stop_when_mx_eq_zero): """ Runs the simulation using field #1 from the problem description. Stores the average magnetisation components regularly, as well as the magnetisation when the x-component of the average magnetisation crosses the value 0 for the first time. """ mesh = from_geofile(os.path.join(MODULE_DIR, "bar.geo")) sim = Simulation(mesh, Ms, name="dynamics", unit_length=1e-9) sim.alpha = alpha sim.gamma = gamma sim.set_m(np.load(m_0_file)) sim.add(Demag()) sim.add(Exchange(A)) """ Conversion between mu0 * H in mT and H in A/m. mu0 * H = 1 mT = 1 * 1e-3 T = 1 * 1e-3 Vs/m^2 divide by mu0 with mu0 = 4pi * 1e-7 Vs/Am gives H = 1 / 4pi * 1e4 A/m with the unit A/m which is indeed what we want. Consequence: Just divide the value of mu0 * H in Tesla by mu0 to get the value of H in A/m. """ Hx = -24.6e-3 / mu0 Hy = 4.3e-3 / mu0 Hz = 0 sim.add(Zeeman((Hx, Hy, Hz))) def check_if_crossed(sim): mx, _, _ = sim.m_average if mx <= 0: print "The x-component of the spatially averaged magnetisation first crossed zero at t = {}.".format( sim.t) np.save(m_at_crossing_file, sim.m) # When this function returns True, it means this event is done # and doesn't need to be triggered anymore. # When we return False, it means we want to stop the simulation. return not stop_when_mx_eq_zero sim.schedule(check_if_crossed, every=1e-12) sim.schedule('save_averages', every=10e-12, at_end=True) sim.schedule('save_vtk', every=10e-12, at_end=True, overwrite=True) sim.run_until(2.0e-9) return sim.t
def excite_system(): Ms = 8.6e5 sim = Simulation(mesh, Ms, pbc='1d', unit_length=1e-9) sim.alpha = 0.0001 sim.set_m(np.load('relaxed.npy')) alpha_expr = AlphaExpression() alpha_mult = df.interpolate(alpha_expr, sim.llg.S1) sim.spatial_alpha(0.0001, alpha_mult) #df.plot(alpha_mult) #df.interactive() #xs=find_skyrmion_center(sim.llg._m) # #assert(1==2) A = 1.3e-11 D = 4e-3 sim.add(Exchange(A)) sim.add(DMI(D)) sim.add(Zeeman((0, 0, 0.4 * Ms))) GHz = 1e9 omega = 50 * 2 * np.pi * GHz def time_fun(t): return np.sinc(omega * (t - 50e-12)) h0 = 1e3 kc = 1.0 / 45.0 H0 = MyExpression(h0, kc) sim.add(TimeZeemanPython(H0, time_fun)) xs = find_skyrmion_center(sim.llg._m) ts = np.linspace(0, 8e-9, 4001) np.save('xs.npy', xs) sim.create_integrator() sim.integrator.integrator.set_scalar_tolerances(1e-8, 1e-8) index = 0 for t in ts: sim.run_until(t) np.save('data/m_%d.npy' % index, sim.llg.m) index += 1
def test_spatially_varying_alpha_using_Simulation_class(): """ test that I can change the value of alpha through the property sim.alpha and that I get an df.Function back. """ length = 20 simplices = 10 mesh = df.IntervalMesh(simplices, 0, length) sim = Simulation(mesh, Ms=1, unit_length=1e-9) sim.alpha = 1 expected_alpha = np.ones(simplices + 1) assert np.array_equal(sim.alpha.vector().array(), expected_alpha)
def run_simulation(): print "Running simulation of STT dynamics." sim = Sim(mesh, Ms=Ms, unit_length=1e-9) sim.set_m(np.loadtxt(initial_m_file)) sim.alpha = 0.01 sim.add(Exchange(1.3e-11)) sim.add(Demag()) sim.llg.use_slonczewski(J=0.1e12, P=0.4, d=10e-9, p=(0, 1, 0)) with open(averages_file, "w") as f: dt = 5e-12 t_max = 10e-9 for t in np.arange(0, t_max, dt): sim.run_until(t) f.write("{} {} {} {}\n".format(t, *sim.m_average))
def relax_system(mesh): sim = Sim(mesh, Ms=8.0e5, unit_length=1e-9) sim.gamma = 2.211e5 sim.alpha = 1 sim.add(Exchange(A=13e-12)) sim.add(Demag()) sim.set_m(init_m) sim.relax(stopping_dmdt=0.01) np.save('m0.npy', sim.m) df.plot(sim._m)
def test_uniform_external_field(): TOLERANCE = 3.5e-10 mesh = df.UnitCubeMesh(2, 2, 2) sim = Sim(mesh, Ms) sim.set_m((1, 0, 0)) sim.add(Zeeman((0, Ms, 0))) sim.alpha = 1.0 sim.run_until(1e-9) m = sim.m.reshape((3, -1)).mean(-1) expected_m = np.array([0, 1, 0]) diff = np.abs(m - expected_m) assert np.max(diff) < TOLERANCE
def setup_module(module=None): x_max = 100e-9 # m simplexes = 50 mesh = dolfin.IntervalMesh(simplexes, 0, x_max) def m_gen(coords): x = coords[0] mx = min(1.0, x / x_max) mz = 0.1 my = np.sqrt(1.0 - (0.99 * mx**2 + mz**2)) return np.array([mx, my, mz]) K1 = 520e3 # J/m^3 Ms = 0.86e6 sim = Sim(mesh, Ms) sim.alpha = 0.2 sim.set_m(m_gen) anis = UniaxialAnisotropy(K1, (0, 0, 1)) sim.add(anis) # Save H_anis and m at t0 for comparison with nmag global H_anis_t0, m_t0 H_anis_t0 = anis.compute_field() m_t0 = sim.m av_f = open(os.path.join(MODULE_DIR, "averages.txt"), "w") tn_f = open(os.path.join(MODULE_DIR, "third_node.txt"), "w") t = 0 t_max = 3e-10 dt = 5e-12 # s while t <= t_max: mx, my, mz = sim.m_average averages.append([t, mx, my, mz]) av_f.write( str(t) + " " + str(mx) + " " + str(my) + " " + str(mz) + "\n") mx, my, mz = h.components(sim.m) m2x, m2y, m2z = mx[2], my[2], mz[2] third_node.append([t, m2x, m2y, m2z]) tn_f.write( str(t) + " " + str(m2x) + " " + str(m2y) + " " + str(m2z) + "\n") t += dt sim.run_until(t) av_f.close() tn_f.close()
def excite_system(): Ms = 8.0e5 sim = Simulation(mesh, Ms, unit_length=1e-9, name='dy') sim.alpha = 0.001 sim.set_m((1, 0, 0)) sim.set_tol(1e-6, 1e-6) A = 1.3e-11 sim.add(Exchange(A)) sim.add(Zeeman(varying_field)) sim.schedule('save_ndt', every=2e-12) sim.run_until(0.5e-9)
def run_finmag(): """Run the finmag simulation and store data in averages.txt.""" sim = Sim(mesh, Ms, unit_length=unit_length) sim.alpha = 0.5 sim.set_m((1, 0, 1)) exchange = Exchange(13.0e-12) sim.add(exchange) demag = Demag(solver="FK") sim.add(demag) fh = open(os.path.join(MODULE_DIR, "averages.txt"), "w") fe = open(os.path.join(MODULE_DIR, "energies.txt"), "w") logger.info("Time integration") times = np.linspace(0, 3.0e-10, 61) for counter, t in enumerate(times): # Integrate sim.run_until(t) # Save averages to file mx, my, mz = sim.m_average fh.write(str(t) + " " + str(mx) + " " + str(my) + " " + str(mz) + "\n") # Energies E_e = exchange.compute_energy() E_d = demag.compute_energy() fe.write(str(E_e) + " " + str(E_d) + "\n") # Energy densities if counter == 10: exch_energy = exchange.energy_density_function() demag_energy = demag.energy_density_function() finmag_exch, finmag_demag = [], [] R = range(100) for i in R: finmag_exch.append(exch_energy([15, 15, i])) finmag_demag.append(demag_energy([15, 15, i])) # Store data np.save(os.path.join(MODULE_DIR, "finmag_exch_density.npy"), np.array(finmag_exch)) np.save(os.path.join(MODULE_DIR, "finmag_demag_density.npy"), np.array(finmag_demag)) fh.close() fe.close()
def setup_sim(self, m0): Hz = [8e4, 0, 0] # A/m A = 1.3e-11 # J/m Ms = 800e3 # A/m alpha = 1. mesh = df.BoxMesh(0, 0, 0, *(self.box_size + self.box_divisions)) sim = Simulation(mesh, Ms) sim.alpha = alpha sim.set_m(m0) sim.add(Demag()) sim.add(Exchange(A)) sim.add(Zeeman(Hz)) return sim
def test_negative_uniform_external_field(): TOLERANCE = 1e-10 mesh = df.UnitCubeMesh(2, 2, 2) sim = Sim(mesh, Ms) sim.set_m((1, 0.1, 0)) # slightly misaligned sim.add(Zeeman((-1.0 * Ms, 0, 0))) sim.alpha = 1.0 sim.run_until(1e-9) m = sim.m.reshape((3, -1)).mean(-1) print "Average magnetisation ({:.2g}, {:.2g}, {:.2g}).".format(*m) expected_m = np.array([-1, 0, 0]) diff = np.abs(m - expected_m) TOLERANCE = 1e-5 assert np.max(diff) < TOLERANCE
def compare_with_analytic_solution(alpha=0.5, max_t=1e-9): """ Compares the C/dolfin/odeint solution to the analytical one. """ print "Running comparison with alpha={0}.".format(alpha) # define 3d mesh x0 = y0 = z0 = 0 x1 = y1 = z1 = 10e-9 nx = ny = nz = 1 mesh = dolfin.BoxMesh(dolfin.Point(x0, x1, y0), dolfin.Point(y1, z0, z1), nx, ny, nz) sim = Simulation(mesh, Ms=1) sim.alpha = alpha sim.set_m((1, 0, 0)) sim.add(Zeeman((0, 0, 1e6))) # plug in an integrator with lower tolerances sim.set_tol(abstol=1e-12, reltol=1e-12) ts = numpy.linspace(0, max_t, num=100) ys = numpy.array([(sim.advance_time(t), sim.m.copy())[1] for t in ts]) tsfine = numpy.linspace(0, max_t, num=1000) m_analytical = make_analytic_solution(1e6, alpha, sim.gamma) save_plot(ts, ys, tsfine, m_analytical, alpha) TOLERANCE = 1e-6 # tolerance on Ubuntu 11.10, VM Hans, 25/02/2012 rel_diff_maxs = list() for i in range(len(ts)): m = numpy.mean(ys[i].reshape((3, -1)), axis=1) m_ref = m_analytical(ts[i]) diff = numpy.abs(m - m_ref) diff_max = numpy.max(diff) rel_diff_max = numpy.max(diff / numpy.max(m_ref)) rel_diff_maxs.append(rel_diff_max) print "t= {0:.3g}, diff_max= {1:.3g}.".format(ts[i], diff_max) msg = "Diff at t= {0:.3g} too large.\nAllowed {1:.3g}. Got {2:.3g}." assert diff_max < TOLERANCE, msg.format(ts[i], TOLERANCE, diff_max) print "Maximal relative difference: " print numpy.max(numpy.array(rel_diff_maxs))
def with_current(mesh): sim = Sim(mesh, Ms=8.0e5, unit_length=1e-9, name='stt') sim.gamma = 2.211e5 sim.alpha = 0.1 sim.set_m(np.load('m0.npy')) sim.add(Exchange(A=13e-12)) sim.add(Demag()) sim.set_zhangli(init_J, 1.0, 0.05) sim.schedule('save_ndt', every=5e-11) #sim.schedule('save_vtk', every=5e-11) sim.schedule(spin_length, every=5e-11) sim.run_until(8e-9) df.plot(sim._m)
def t_test_sim_ode_parallel(do_plot=False): mesh = df.BoxMesh(df.Point(0, 0, 0), df.Point(2, 2, 2), 1, 1, 1) sim = Sim(mesh, 8.6e5, unit_length=1e-9, pbc='2d', parallel=True) sim.alpha = alpha sim.set_m((1, 0, 0)) sim.set_tol(1e-10, 1e-14) H0 = 1e5 sim.add(Zeeman((0, 0, H0))) # sim.add(Exchange(1.3e-11)) dt = 1e-12 ts = np.linspace(0, 500 * dt, 100) precession_coeff = sim.gamma / (1 + alpha**2) mz_ref = np.tanh(precession_coeff * alpha * H0 * ts) mzs = [] length_error = [] for t in ts: sim.run_until(t) mm = sim.m.copy() # mm.shape=(3,-1) # mx,my,mz = mm[:,0] # same as m_average for this macrospin problem mzs.append(mm[-1]) #length = np.sqrt(mx**2+my**2+mz**2) # length_error.append(abs(length-1.0)) if do_plot: ts_ns = ts * 1e9 plt.plot(ts_ns, mzs, "b.", label="computed") plt.plot(ts_ns, mz_ref, "r-", label="analytical") plt.xlabel("time (ns)") plt.ylabel("mz") plt.title("integrating a macrospin") plt.legend() plt.savefig(os.path.join(MODULE_DIR, "test_sim_ode.png")) print("Deviation = {}, total value={}".format(np.max(np.abs(mzs - mz_ref)), mz_ref)) assert np.max(np.abs(mzs - mz_ref)) < 1e-9
def test_non_uniform_external_field(): TOLERANCE = 1e-9 length = 10e-9 vertices = 5 mesh = df.IntervalMesh(vertices, 0, length) sim = Sim(mesh, Ms) sim.set_m((1, 0, 0)) # applied field # (0, -H, 0) for 0 <= x <= a # (0, +H, 0) for a < x <= length H_expr = df.Expression(("0", "H*(x[0]-a)/fabs(x[0]-a)", "0"), a=length / 2, H=Ms / 2, degree=1) sim.add(Zeeman(H_expr)) sim.alpha = 1.0 sim.run_until(1e-9) m = sim.m.reshape((3, -1)).mean(-1) expected_m = np.array([0, 0, 0]) diff = np.abs(m - expected_m) assert np.max(diff) < TOLERANCE
def run_sim_without_stt(): sim = Sim(mesh, Ms=8.6e5) sim.set_m((1, 0.01, 0.01)) sim.alpha = 0.014 sim.gamma = 221017 H_app_mT = np.array([0.2, 0.2, 10.0]) H_app_SI = H_app_mT / (1000 * mu0) sim.add(Zeeman(tuple(H_app_SI))) sim.add(Exchange(1.3e-11)) sim.add(UniaxialAnisotropy(1e5, (0, 0, 1))) with open(averages_without, "w") as f: dt = 5e-12 t_max = 10e-9 for t in np.arange(0, t_max, dt): sim.run_until(t) f.write("{} {} {} {}\n".format(t, *sim.m_average))
def run_simulation(verbose=False): mesh = from_geofile('bar.geo') sim = Simulation(mesh, Ms=0.86e6, unit_length=1e-9, name="finmag_bar") sim.set_m((1, 0, 1)) sim.set_tol(1e-6, 1e-6) sim.alpha = 0.5 sim.add(Exchange(13.0e-12)) sim.add(Demag()) sim.schedule('save_averages', every=5e-12) sim.schedule("eta", every=10e-12) sim.run_until(3e-10) print timer if verbose: print "The RHS was evaluated {} times, while the Jacobian was computed {} times.".format( sim.integrator.stats()['nfevals'], timer.get("sundials_jtimes", "LLG").calls)
def relax(mesh): Ms = 8.6e5 sim = Simulation(mesh, Ms, pbc='2d',unit_length=1e-9) sim.set_m(m_init_fun) sim.add(Exchange(1.3e-11)) sim.add(DMI(D = 4e-3)) sim.add(Zeeman((0,0,0.45*Ms))) sim.alpha = 0.5 ts = np.linspace(0, 1e-9, 101) for t in ts: sim.run_until(t) p = df.plot(sim.llg._m) sim.save_vtk() df.plot(sim.llg._m).write_png("vortex") df.interactive()