def test_thin_film_demag_against_real_demag(): sim = Sim( df.BoxMesh(df.Point(0, 0, 0), df.Point(500e-9, 500e-9, 1e-9), 50, 50, 1), Ms) sim.set_m((0, 0, 1)) tfdemag = ThinFilmDemag() sim.add(tfdemag) H_tfdemag = tfdemag.compute_field().view().reshape((3, -1)).mean(1) demag = Demag() sim.add(demag) H_demag = demag.compute_field().view().reshape((3, -1)).mean(1) diff = np.abs(H_tfdemag - H_demag) / Ms print "Standard Demag:\n", H_demag print "ThinFilmDemag:\n", H_tfdemag print "Difference relative to Ms:\n", diff assert np.allclose(H_tfdemag, H_demag, atol=0.05 * Ms) # 5% of Ms sim.set_m((1, 0, 0)) H_tfdemag = tfdemag.compute_field().view().reshape((3, -1)).mean(1) H_demag = demag.compute_field().view().reshape((3, -1)).mean(1) print "Running again, changed m in the meantime." diff = np.abs(H_tfdemag - H_demag) / Ms print "Standard Demag:\n", H_demag print "ThinFilmDemag:\n", H_tfdemag print "Difference relative to Ms:\n", diff assert np.allclose(H_tfdemag, H_demag, atol=0.005 * Ms) # 0.5% of Ms
def run_demag(repetitions, params, m, Ms, unit_length, bem=None, b2g_map=None): demag = Demag("FK") demag.parameters["phi_1_solver"] = params[0] demag.parameters["phi_1_preconditioner"] = params[1] demag.parameters["phi_2_solver"] = params[2] demag.parameters["phi_2_preconditioner"] = params[3] if bem is not None: demag.precomputed_bem(bem, b2g_map) demag.setup(m, Ms, unit_length) start = now() for j in xrange(repetitions): H = demag.compute_field() runtime = (now() - start) / repetitions return H, runtime
def relax_system(): Ms = 8.6e5 sim = Simulation(mesh, Ms, unit_length=1e-9, name='dy', pbc='1d') sim.alpha = 0.01 sim.set_m((0.8, 0.6, 1)) sim.set_tol(1e-8, 1e-8) A = 1.3e-11 sim.add(Exchange(A)) parameters = { 'absolute_tolerance': 1e-10, 'relative_tolerance': 1e-10, 'maximum_iterations': int(1e5) } Ts = [] for i in range(-9, 10): Ts.append((10 * i, 0, 0)) demag = Demag(solver='Treecode') demag.parameters['phi_1'] = parameters demag.parameters['phi_2'] = parameters sim.add(demag) demag.compute_field() 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(0.2e-9)
def setup_finmag(): mesh = from_geofile(os.path.join(MODULE_DIR, "sphere.geo")) S3 = df.VectorFunctionSpace(mesh, "Lagrange", 1) m = Field(S3) m.set(df.Constant((1, 0, 0))) Ms = 1 demag = Demag() demag.setup(m, Field(df.FunctionSpace(mesh, 'DG', 0), Ms), unit_length=1e-9) H = demag.compute_field() return dict(m=m, H=H, Ms=Ms, S3=S3, table=start_table())
def uniformly_magnetised_sphere(): Ms = 1 mesh = sphere(r=1, maxh=0.25) S3 = df.VectorFunctionSpace(mesh, "CG", 1) m = Field(S3) m.set(df.Constant((1, 0, 0))) solutions = [] for solver in solvers: demag = Demag(solver) demag.setup(m, Field(df.FunctionSpace(mesh, 'DG', 0), Ms), unit_length=1e-9) demag.H = demag.compute_field() solutions.append(demag) return solutions
def test_field(): """ Test the demag field. H_demag should be equal to -1/3 M, and with m = (1, 0 ,0) and Ms = 1,this should give H_demag = (-1/3, 0, 0). """ # Using mesh with radius 10 nm (nmag ex. 1) mesh = from_geofile(os.path.join(MODULE_DIR, "sphere1.geo")) S3 = df.VectorFunctionSpace(mesh, "Lagrange", 1) m_function = df.interpolate(df.Constant((1, 0, 0)), S3) m = Field(S3, m_function) demag = Demag() demag.setup(m, Field(df.FunctionSpace(mesh, 'DG', 0), Ms), unit_length=1e-9) # Compute demag field H_demag = demag.compute_field() H_demag.shape = (3, -1) x, y, z = H_demag[0], H_demag[1], H_demag[2] print "Max values in direction:" print "x: %g, y: %g, z: %g" % (max(x), max(y), max(z)) print "Min values in direction:" print "x: %g, y: %g, z: %g" % (min(x), min(y), min(z)) x, y, z = average(x), average(y), average(z) print "Average values in direction" print "x: %g, y: %g, z: %g" % (x, y, z) # Compute relative erros x = abs((x + 1. / 3 * Ms) / Ms) y = abs(y / Ms) z = abs(z / Ms) print "Relative error:" print "x: %g, y: %g, z: %g" % (x, y, z) assert x < TOL, "x-average is %g, should be -1/3." % x assert y < TOL, "y-average is %g, should be zero." % y assert z < TOL, "z-average is %g, should be zero." % z
def run_benchmark(solver): radius = 10.0 maxh = 0.5 unit_length = 1e-9 mesh = sphere(r=radius, maxh=maxh, directory="meshes") S3 = df.VectorFunctionSpace(mesh, "Lagrange", 1) Ms = 1 m_0 = (1, 0, 0) m = df.Function(S3) m.assign(df.Constant(m_0)) H_ref = np.array((-Ms / 3.0, 0, 0)) if solver == "krylov": parameters = {} parameters["phi_1_solver"] = "default" parameters["phi_1_preconditioner"] = "default" parameters["phi_2_solver"] = "default" parameters["phi_2_preconditioner"] = "default" demag = Demag("FK", solver_type="Krylov", parameters=parameters) elif solver == "lu": demag = Demag("FK", solver_type="LU") else: import sys print "Unknown solver {}.".format(solver) sys.exit(1) demag.setup(S3, m, Ms, unit_length) REPETITIONS = 10 start = time.time() for j in xrange(REPETITIONS): H = demag.compute_field() elapsed = (time.time() - start) / REPETITIONS H = H.reshape((3, -1)) spread = abs(H[0].max() - H[0].min()) error = abs(H.mean(axis=1)[0] - H_ref[0]) / abs(H_ref[0]) print elapsed, error, spread
if solver == "FK": if i == 0: demag.parameters["phi_1_solver"] = "default" demag.parameters["phi_1_preconditioner"] = "default" demag.parameters["phi_2_solver"] = "default" demag.parameters["phi_2_preconditioner"] = "default" if i == 1: demag.parameters["phi_1_solver"] = "cg" demag.parameters["phi_1_preconditioner"] = "ilu" demag.parameters["phi_2_solver"] = "cg" demag.parameters["phi_2_preconditioner"] = "ilu" demag.setup(m, Ms, unit_length) start = time.time() for j in xrange(10): H = demag.compute_field() elapsed = (time.time() - start) / 10.0 H = H.reshape((3, -1)).mean(axis=1) error = abs(H[0] - H_ref[0]) / abs(H_ref[0]) timings[i].append(elapsed) errors[i].append(error) fig = plt.figure() ax = fig.add_subplot(211) ax.set_title("Runtime") for i, solver in enumerate(solvers): ax.plot(vertices, timings[i], label=solvers_label[i]) ax.legend(loc=2) ax.set_xlabel("vertices") ax.set_ylabel("time (s)")