def test_anisotropy_field_supported_methods(fixt): """ Check that all supported methods give the same results as the default method. """ TOLERANCE = 1e-13 fixt["m"].set(df.Constant((1 / np.sqrt(2), 0, 1 / np.sqrt(2)))) H_default = fixt["anis"].compute_field() supported_methods = list(UniaxialAnisotropy._supported_methods) # No need to compare default method with itself. supported_methods.remove(fixt["anis"].method) for method in supported_methods: anis = UniaxialAnisotropy(fixt["K1"], fixt["a"], method=method) anis.setup(fixt["m"], fixt["Ms"]) H = anis.compute_field() print( textwrap.dedent(""" With method '{}', expecting: H = {}, got: H = {}. """.format(method, H_default.reshape((3, -1)).mean(axis=1), H.reshape((3, -1)).mean(axis=1)))) assert np.allclose(H, H_default, atol=0, rtol=TOLERANCE)
def compute_finmag_anis(m_gen, Ms, K1, axis, dolfin_mesh): S3 = df.VectorFunctionSpace(dolfin_mesh, "Lagrange", 1, dim=3) coords = np.array(zip(*dolfin_mesh.coordinates())) m0 = m_gen(coords).flatten() m = Field(S3) m.set_with_numpy_array_debug(m0) anis = UniaxialAnisotropy(K1, axis) anis.setup(m, Field(df.FunctionSpace(dolfin_mesh, 'DG', 0), Ms)) anis_field = df.Function(S3) anis_field.vector()[:] = anis.compute_field() return anis_field
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 setup(K2=K2): print "Running finmag..." mesh = from_geofile(os.path.join(MODULE_DIR, "bar.geo")) coords = np.array(zip(*mesh.coordinates())) S3 = df.VectorFunctionSpace(mesh, "Lagrange", 1, dim=3) m = Field(S3) m.set_with_numpy_array_debug(m_gen(coords).flatten()) S1 = df.FunctionSpace(mesh, "Lagrange", 1) Ms_cg = Field(df.FunctionSpace(mesh, 'CG', 1), Ms) anisotropy = UniaxialAnisotropy(K1, u1, K2=K2) anisotropy.setup(m, Ms_cg, unit_length=1e-9) H_anis = df.Function(S3) H_anis.vector()[:] = anisotropy.compute_field() return dict(m=m, H=H_anis, S3=S3, table=start_table())