def run_test(self, backend, method, nsteps=40000): llg = setup_domain_wall_cobalt(node_count=NODE_COUNT) integrator = llg_integrator(llg, llg.m_field, backend, method=method, nsteps=nsteps) t = datetime.now() integrator.advance_time(END_TIME) dt = datetime.now() - t print "backend=%s, method=%s: elapsed time=%s, n_rhs_evals=%s, error=%g" % ( backend, method, dt, integrator.n_rhs_evals, domain_wall_error(llg.m_field.as_array(), NODE_COUNT))
def run_test(backend, method, mode='onego', nsteps=40000): llg = setup_domain_wall_cobalt(node_count=NODE_COUNT) integrator = llg_integrator(llg, llg.m_field, backend, method=method, nsteps=nsteps) t = datetime.now() if mode == 'onego': END_TIME = END_TIME1 + END_TIME2 elif mode == 'twogoes' or mode == 'twogoesreinit': END_TIME = END_TIME1 else: raise ValueError( "Can only understand 'onego', 'twogoes', twogoesreinit'.") integrator.advance_time(END_TIME) dt = datetime.now() - t print "backend=%s, method=%s: elapsed time=%s, n_rhs_evals=%s, error=%g" % ( backend, method, dt, integrator.n_rhs_evals, domain_wall_error(integrator.m, NODE_COUNT)) if mode == 'onego': return integrator if mode == 'twogoesreinit': # check that rhs counter goes back to zero print "re-initialising" integrator.reinit() assert integrator.n_rhs_evals == 0 else: print "Not re-initialising" integrator.advance_time(END_TIME2) print "backend=%s, method=%s: elapsed time=%s, n_rhs_evals=%s, error=%g" % ( backend, method, dt, integrator.n_rhs_evals, domain_wall_error(integrator.m, NODE_COUNT)) print("second call to integrator.n_rhs_evals ={}".format( integrator.n_rhs_evals)) return integrator
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()
# Create mesh mu = 1e-9 mesh = Mesh("coarse_bar.xml.gz") #mesh = Mesh("bar.xml.gz") # Setup LLG llg = LLG(mesh, unit_length=mu) llg.Ms = 0.86e6 llg.A = 13.0e-12 llg.alpha = 0.5 llg.set_m((1, 0, 1)) llg.setup(use_exchange=True, use_dmi=False, use_demag=True, demag_method="FK") # Set up time integrator integrator = llg_integrator(llg, llg.m) dt = 5e-12 ###### # After ten time steps, plot the energy density # from z=0 to z=100 through the center of the body. ###### # Integrate integrator.run_until(dt * 10) exch = llg.exchange.energy_density_function() demag = llg.demag.energy_density_function() finmag_exch, finmag_demag = [], [] R = range(100) for i in R: finmag_exch.append(exch([15, 15, i]))
def compute_domain_wall_cobalt(end_time=1e-9): llg = setup_domain_wall_cobalt() integrator = llg_integrator(llg, llg.m_field) integrator.advance_time(end_time) return np.linspace(0, LENGTH, NODE_COUNT), llg.m.reshape((3, -1))
def test_external_field_depends_on_t(): tfinal = 0.3 * 1e-9 dt = 0.001e-9 simplices = 2 L = 10e-9 mesh = df.IntervalMesh(simplices, 0, L) S1 = df.FunctionSpace(mesh, "Lagrange", 1) S3 = df.VectorFunctionSpace(mesh, "Lagrange", 1, dim=3) GHz = 1e9 omega = 100 * GHz llg = LLG(S1, S3) llg.set_m(df.Constant((1, 0, 0))) #This is the time dependent field H_app_expr = df.Expression(("0.0", "0.0", "H0*sin(omega*t)"), H0=1e5, omega=omega, t=0.0, degree=1) H_app = TimeZeeman(H_app_expr) Ms_field = Field(df.FunctionSpace(mesh, 'DG', 0), 8.6e5) H_app.setup(llg.m_field, Ms=Ms_field) #define function that updates that expression, and the field object def update_H_ext(t): print "update_H_ext being called for t=%g" % t H_app.update(t) llg.effective_field.add(H_app, with_time_update=update_H_ext) #nothing special from here, just setting up time integration integrator = llg_integrator(llg, llg.m_field) #to gather data for later analysis mlist = [] tlist = [] hext = [] #time loop times = np.linspace(0, tfinal, tfinal / dt + 1) for t in times: integrator.advance_time(t) print "Integrating time: %g" % t mlist.append(llg.m_average) tlist.append(t) hext.append(H_app.H((0))) #only plotting and data analysis from here on mx = [tmp[0] for tmp in mlist] my = [tmp[1] for tmp in mlist] mz = [tmp[2] for tmp in mlist] pylab.plot(tlist, mx, label='m_x') pylab.plot(tlist, my, label='m_y') pylab.plot(tlist, mz, label='m_z') pylab.xlabel('time [s]') pylab.legend() pylab.savefig(os.path.join(MODULE_DIR, 'results.png')) pylab.close() #if max_step is not provided, or chosen too large, #the external field appears not smooth in this plot. #What seems to happen is that the ode integrator #returns the solution without calling the rhs side again #if we request very small time steps. #This is only for debugging. pylab.plot(tlist, hext, '-x') pylab.ylabel('external field [A/m]') pylab.xlabel('time [s]') pylab.savefig(os.path.join(MODULE_DIR, 'hext.png')) pylab.close() #Then try to fit sinusoidal curve through results def sinusoidalfit(t, omega, phi, A, B): return A * np.cos(omega * t + phi) + B #if scipy available try: import scipy.optimize except ImportError: print "Couldn't import scipy.optimize, skipping test" else: popt, pcov = scipy.optimize.curve_fit(sinusoidalfit, np.array(tlist), np.array(my), p0=(omega * 1.04, 0., 0.1, 0.2)) #p0 is the set of parameters with which the fitting #routine starts print "popt=", popt fittedomega, fittedphi, fittedA, fittedB = popt f = open(os.path.join(MODULE_DIR, "fittedresults.txt"), "w") print >> f, "Fitted omega : %9g" % (fittedomega) print >> f, "Rel error in omega fit : %9g" % ( (fittedomega - omega) / omega) print >> f, "Fitted phi : %9f" % (fittedphi) print >> f, "Fitted Amplitude (A) : %9f" % (fittedA) print >> f, "Fitted Amp-offset (B) : %9f" % (fittedB) pylab.plot(tlist, my, label='my - simulated') pylab.plot(tlist, sinusoidalfit(np.array(tlist), *popt), '-x', label='m_y - fit') pylab.xlabel('time [s]') pylab.legend() pylab.savefig(os.path.join(MODULE_DIR, 'fit.png')) deviation = np.sqrt( sum((sinusoidalfit(np.array(tlist), *popt) - my)**2)) / len(tlist) print >> f, "stddev=%g" % deviation f.close() assert (fittedomega - omega) / omega < 1e-4 assert deviation < 5e-4