def test_compute_scalar_potential_fk(self): m1 = df.Constant([1, 0, 0]) m2 = df.Expression(["x[0]*x[1]+3", "x[2]+5", "x[1]+7"], degree=1) expressions = [m1, m2] for exp in expressions: for k in xrange(1, 5 + 1): self.run_demag_computation_test( df.UnitCubeMesh(k, k, k), exp, compute_scalar_potential_native_fk, "native, FK", k=k) self.run_demag_computation_test( sphere(1., 1. / k), exp, compute_scalar_potential_native_fk, "native, FK", k=k) self.run_demag_computation_test(MagSphereBase(0.25, 1).mesh, exp, compute_scalar_potential_native_fk, "native, FK", k=k)
def __init__(self, maxh, radius=10): self.mesh = sphere(radius, maxh, directory=MODULE_DIR) self.Ms = 1.0 self.m = (str(self.Ms), "0.0", "0.0") self.M = self.m # do we need this? self.V = df.VectorFunctionSpace(self.mesh, "CG", 1) self.m = df.interpolate(df.Expression(self.m, degree=1), self.V) self.r = radius self.maxh = maxh
def setup_demag_sphere(Ms): mesh = sphere(r=radius, maxh=maxh) Ms_field = Field(df.FunctionSpace(mesh, 'DG', 0), Ms) S3 = df.VectorFunctionSpace(mesh, "Lagrange", 1) m_function = df.Function(S3) m_function.assign(df.Constant((1, 0, 0))) m = Field(S3, m_function) demag = FKDemag() demag.setup(m, Ms_field, unit_length) return demag
def test_regression_Ms_numpy_type(): mesh = sphere(r=radius, maxh=maxh) S3 = df.VectorFunctionSpace(mesh, "Lagrange", 1) m_function = df.Function(S3) m_function.assign(df.Constant((1, 0, 0))) m = Field(S3, m_function) Ms = np.sqrt(6.0 / mu0) # math.sqrt(6.0 / mu0) would work demag = FKDemag() Ms_field = Field(df.FunctionSpace(mesh, 'DG', 0), Ms) demag.setup(m, Ms_field, unit_length) # this used to fail
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 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
def test_demag_energy_density(): """ With a sphere mesh, unit magnetisation in x-direction, we expect the demag energy to be E = 1/6 * mu0 * Ms^2 * V. (See section about demag solvers in the documentation for how this is found.) To make it simple, we define Ms = sqrt(6/mu0). Energy density is then E/V = 1. """ TOL = 5e-2 mesh = sphere(r=1.0, maxh=0.2) S3 = df.VectorFunctionSpace(mesh, "Lagrange", 1) mu0 = 4 * np.pi * 1e-7 demag = Demag() m = Field(S3, value=(1, 0, 0)) Ms = np.sqrt(6.0 / mu0) demag.setup(m, Field(df.FunctionSpace(mesh, 'DG', 0), Ms), 1) density = demag.energy_density() deviation = np.abs(density - 1.0) print "Demag energy density (expect array of 1s):" print density print "Max deviation:", np.max(deviation) assert np.max(deviation) < TOL, \ "Max deviation is %g, should be zero." % np.max(deviation)
import dolfin as df from finmag.energies.demag.fk_demag import FKDemag from finmag.util.meshes import sphere from simple_timer import SimpleTimer # nmag, dict ksp_tolerances in Simulation class constructor # apparently gets passed to petsc http://www.mcs.anl.gov/petsc/petsc-current/docs/manualpages/KSP/KSPSetTolerances.html # dolfin # managed to pass tolerances to KrylovSolver, may want to simply use "solve" instead # https://answers.launchpad.net/dolfin/+question/193119 # check /usr/share/dolfin/demo # check help(df.solve) mesh = sphere(r=10.0, maxh=0.4) S3 = df.VectorFunctionSpace(mesh, "CG", 1) m = df.Function(S3) m.assign(df.Constant((1, 0, 0))) Ms = 1 unit_length = 1e-9 demag = FKDemag() demag.setup(S3, m, Ms, unit_length) benchmark = SimpleTimer() print "Default df.KrylovSolver tolerances (abs, rel): Poisson ({}, {}), Laplace ({}, {}).".format( demag._poisson_solver.parameters["absolute_tolerance"], demag._poisson_solver.parameters["relative_tolerance"], demag._laplace_solver.parameters["absolute_tolerance"], demag._laplace_solver.parameters["relative_tolerance"]) with benchmark: for i in xrange(10):
radius = 5.0 maxhs = [0.2, 0.4, 0.5, 0.6, 0.7, 0.8, 0.85, 0.9, 0.95, 1.0] unit_length = 1e-9 m_0 = (1, 0, 0) Ms = 1 H_ref = np.array((-Ms / 3.0, 0, 0)) vertices = [] solvers = ["FK", "FK", "GCR", "Treecode"] solvers_label = ["FK", "FK opt", "GCR", "Treecode"] timings = [[], [], [], []] errors = [[], [], [], []] for maxh in maxhs: mesh = sphere(r=radius, maxh=maxh, directory="meshes") vertices.append(mesh.num_vertices()) S3 = df.VectorFunctionSpace(mesh, "Lagrange", 1) m_function = df.Function(S3) m_function.assign(df.Constant(m_0)) m = Field(S3, m_function) for i, solver in enumerate(solvers): demag = Demag(solver) 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:
print "List of Krylov subspace methods.\n" for name, description in df.krylov_solver_methods(): print "{:<20} {}".format(name, description) print "\nList of preconditioners.\n" for name, description in df.krylov_solver_preconditioners(): print "{:<20} {}".format(name, description) m = len(df.krylov_solver_methods()) n = len(df.krylov_solver_preconditioners()) print "\nThere are {} solvers, {} preconditioners and thus {} diferent combinations of solver and preconditioner.".format( m, n, m * n) print "\nSolving first system.\n" ball = sphere(20.0, 1.2, directory="meshes") m_ball = df.Constant((1, 0, 0)) unit_length = 1e-9 print "The used mesh has {} vertices.".format(ball.num_vertices()) H_expected_ball = df.Constant((-1.0 / 3.0, 0.0, 0.0)) tol = 0.002 repetitions = 10 solvers, preconditioners, b1, b2 = run_demag_benchmark(m_ball, ball, unit_length, tol, repetitions, "ball", H_expected_ball) print "\nSolving second system.\n" film = box(0, 0, 0, 500, 50, 1, maxh=2.0, directory="meshes") m_film = df.Constant((1, 0, 0))
from test_bem_computation import compute_scalar_potential_native_fk from finmag.util.meshes import sphere import numpy as np from mayavi import mlab # Create the mesh and compute the scalar potential mesh = sphere(r=1.0, maxh=0.1) phi = compute_scalar_potential_native_fk(mesh) x, y, z = mesh.coordinates().T values = phi.vector().array() # Set up the visualisation figure = mlab.figure(bgcolor=(1, 1, 1), fgcolor=(0, 0, 0)) # Use unconnected datapoints and scalar_scatter and interpolate using a # delaunay mesh src = mlab.pipeline.scalar_scatter(x, y, z, values) field = mlab.pipeline.delaunay3d(src) # Make the contour plot contours = np.linspace(np.min(values), np.max(values), 10) mlab.pipeline.iso_surface(field, contours=list(contours)) mlab.show()
def test_bem_computation(self): self.run_bem_computation_test(sphere(1., 0.8)) self.run_bem_computation_test(sphere(1., 0.4)) self.run_bem_computation_test(sphere(1., 0.3)) self.run_bem_computation_test(sphere(1., 0.2)) self.run_bem_computation_test(df.UnitCubeMesh(3, 3, 3))
if __name__ == "__main__": n = 4 #mesh = UnitCubeMesh(n, n, n) #mesh = BoxMesh(df.Point(-1, 0, 0), df.Point(1, 1, 1), 10, 2, 2) # mesh=sphere(3.0,0.3) # mesh=df.Mesh('tet.xml') # #expr = df.Expression(('4.0*sin(x[0])', '4*cos(x[0])','0'), degree=1) from finmag.util.meshes import elliptic_cylinder, sphere mesh = elliptic_cylinder(100, 150, 5, 4.5, directory='meshes') # mesh=box(0,0,0,5,5,100,5,directory='meshes') #mesh = df.BoxMesh(df.Point(0, 0, 0), df.Point(100, 2, 2), 400, 2, 2) mesh = sphere(15, 1, directory='meshes') Vv = df.VectorFunctionSpace(mesh, "Lagrange", 1) Ms = 8.6e5 expr = df.Expression(('cos(x[0])', 'sin(x[0])', '0'), degree=1) m = Field(Vv, value=expr) #m = df.interpolate(df.Constant((1, 0, 0)), Vv) from finmag.energies.demag.fk_demag import FKDemag import time demag = TreecodeBEM(mac=0.4, p=5, num_limit=1, correct_factor=10,