def test_repr(self): # 'empty' system object system = mm.System() r = repr(system) assert 'hamiltonian' in r assert 'dynamics' in r assert 'System' in r # system object with name system = mm.System(name='testname') r = repr(system) assert 'testname' in r
def test_init_invalid_args(self): with pytest.raises(TypeError): system = mm.System(energy=5) with pytest.raises(TypeError): system = mm.System(dynamics=5) with pytest.raises(TypeError): system = mm.System(name=152) with pytest.raises(ValueError): system = mm.System(T=-0.1) with pytest.raises(TypeError): system = mm.System(m=-0.1)
def test_demag_pbc(self): name = 'demag_pbc' Ms = 1e6 system = mm.System(name=name) system.energy = mm.Demag() md = self.calculator.MinDriver() # 1D pbc mesh = df.Mesh(region=self.region, cell=self.cell, bc='x') system.m = df.Field(mesh, dim=3, value=(0, 0, 1), norm=Ms) md.drive(system) # 2D pbc mesh = df.Mesh(region=self.region, cell=self.cell, bc='xy') system.m = df.Field(mesh, dim=3, value=(0, 0, 1), norm=Ms) with pytest.raises(ValueError): md.drive(system) # 3D pbc mesh = df.Mesh(region=self.region, cell=self.cell, bc='xyz') system.m = df.Field(mesh, dim=3, value=(0, 0, 1), norm=Ms) with pytest.raises(ValueError): md.drive(system) self.calculator.delete(system)
def test_exchange_dmi_zeeman(self): name = 'exchange_dmi_zeeman' mesh = df.Mesh(region=self.region, cell=self.cell, subregions=self.subregions) # Very weak DMI and strong Zeeman to make the final # magnetisation uniform. A = {'r1': 1e-12, 'r2': 3e-12, 'r1:r2': 2e-12} D = {'r1': 1e-9, 'r2': 0, 'r1:r2': 5e-9} H = df.Field(mesh, dim=3, value=(1e10, 0, 0)) Ms = 1e6 system = mm.System(name=name) system.energy = mm.Exchange(A=A) + \ mm.DMI(D=D, crystalclass='Cnv') + \ mm.Zeeman(H=H) system.m = df.Field(mesh, dim=3, value=(1, 0.3, 0), norm=Ms) md = self.calculator.MinDriver() md.drive(system) value = system.m(mesh.region.random_point()) assert np.linalg.norm(np.subtract(value, (Ms, 0, 0))) < 1 self.calculator.delete(system)
def test_exchange_cubicanisotropy(self): name = 'exchange_cubicanisotropy' A = {'r1': 1e-12, 'r2': 0} K = 1e5 u1 = (1, 0, 0) u2 = (0, 1, 0) Ms = 1e6 mesh = df.Mesh(region=self.region, cell=self.cell, subregions=self.subregions) system = mm.System(name=name) system.energy = mm.Exchange(A=A) + \ mm.CubicAnisotropy(K=K, u1=u1, u2=u2) system.m = df.Field(mesh, dim=3, value=(1, 0.3, 0), norm=Ms) md = self.calculator.MinDriver() md.drive(system) value = system.m(mesh.region.random_point()) assert np.linalg.norm(np.subtract(value, (Ms, 0, 0))) < 1e-3 self.calculator.delete(system)
def test_field_vector_vector(self): name = 'cubicanisotropy_field_vector_vector' mesh = df.Mesh(region=self.region, cell=self.cell) def K_fun(pos): x, y, z = pos if x <= 0: return 0 else: return 1e5 K = df.Field(mesh, dim=1, value=K_fun) u1 = (0, 0, 1) u2 = (0, 1, 0) Ms = 1e6 system = mm.System(name=name) system.energy = mm.CubicAnisotropy(K=K, u1=u1, u2=u2) system.m = df.Field(mesh, dim=3, value=(0, 0.3, 1), norm=Ms) md = self.calculator.MinDriver() md.drive(system) value = system.m((-2e-9, 1e-9, 1e-9)) assert np.linalg.norm(np.cross(value, (0, 0.3 * Ms, Ms))) < 1e-3 value = system.m((2e-9, 2e-9, 2e-9)) assert np.linalg.norm(np.subtract(value, (0, 0, Ms))) < 1e-3 self.calculator.delete(system)
def test_scalar_vector_vector(self): name = 'cubicanisotropy_scalar_vector_vector' mesh = df.Mesh(region=self.region, cell=self.cell) K = 1e5 u1 = (0, 0, 1) u2 = (0, 1, 0) Ms = 1e6 system = mm.System(name=name) system.energy = mm.CubicAnisotropy(K=K, u1=u1, u2=u2) def m_fun(pos): x, y, z = pos if x <= 0: return (0, 0.2, 1) else: return (0, 1, 0.2) system.m = df.Field(mesh, dim=3, value=m_fun, norm=Ms) md = self.calculator.MinDriver() md.drive(system) value = system.m((-1e-9, 2e-9, 2e-9)) assert np.linalg.norm(np.subtract(value, (0, 0, Ms))) < 1e-3 value = system.m((1e-9, 2e-9, 2e-9)) assert np.linalg.norm(np.subtract(value, (0, Ms, 0))) < 1e-3 self.calculator.delete(system)
def test_dict_vector_vector(self): name = 'cubicanisotropy_dict_vector_vector' mesh = df.Mesh(region=self.region, cell=self.cell, subregions=self.subregions) K = {'r1': 0, 'r2': 1e5} u1 = (0, 0, 1) u2 = (0, 1, 0) Ms = 1e6 system = mm.System(name=name) system.energy = mm.CubicAnisotropy(K=K, u1=u1, u2=u2) system.m = df.Field(mesh, dim=3, value=(0, 0.3, 1), norm=Ms) md = self.calculator.MinDriver() md.drive(system) value = system.m((-2e-9, 1e-9, 1e-9)) assert np.linalg.norm(np.cross(value, (0, 0.3 * Ms, Ms))) < 1e-3 value = system.m((2e-9, 2e-9, 2e-9)) assert np.linalg.norm(np.subtract(value, (0, 0, Ms))) < 1e-3 self.calculator.delete(system)
def test_field(self): name = 'exchange_field' def A_fun(pos): x, y, z = pos if x <= 0: return 1e-10 # for 0, OOMMF gives nan else: return 1e-12 mesh = df.Mesh(region=self.region, n=self.n) A = df.Field(mesh, dim=1, value=A_fun) Ms = 1e6 system = mm.System(name=name) system.energy = mm.Exchange(A=A) system.m = df.Field(mesh, dim=3, value=self.m_init, norm=Ms) md = self.calculator.MinDriver() md.drive(system) assert abs(np.linalg.norm(system.m.average) - Ms) < 1 self.calculator.delete(system)
def test_dict(self): name = 'exchange_dict' A = {'r1': 3e-12, 'r2': 1e-12, 'r1:r2': -1e-12} Ms = 1e6 system = mm.System(name=name) system.energy = mm.Exchange(A=A) mesh = df.Mesh(region=self.region, n=self.n, subregions=self.subregions) system.m = df.Field(mesh, dim=3, value=self.m_init, norm=Ms) md = self.calculator.MinDriver() md.drive(system) # r1 assert abs(np.linalg.norm(system.m['r1'].average) - Ms) < 1 # r2 assert abs(np.linalg.norm(system.m['r2'].average) - Ms) < 1 assert abs(np.dot(system.m['r1'].orientation.average, system.m['r2'].orientation.average) - (-1)) < 1e-3 self.calculator.delete(system)
def test_dirname(calculator): name = 'specifying_dirname' mydirname = './my_dirname' p1 = (0, 0, 0) p2 = (5e-9, 5e-9, 5e-9) n = (2, 2, 2) Ms = 1e6 A = 1e-12 H = (0, 0, 1e6) region = df.Region(p1=p1, p2=p2) mesh = df.Mesh(region=region, n=n) system = mm.System(name=name) system.energy = mm.Exchange(A=A) + mm.Zeeman(H=H) system.dynamics = (mm.Precession(gamma0=mm.consts.gamma0) + mm.Damping(alpha=1)) system.m = df.Field(mesh, dim=3, value=(0, 0.1, 1), norm=Ms) md = calculator.MinDriver() md.drive(system, dirname=mydirname) dirname = os.path.join(mydirname, name, 'drive-0') assert os.path.exists(dirname) system.energy.zeeman.H = (1e6, 0, 0) td = calculator.TimeDriver() td.drive(system, t=100e-12, n=10, dirname=mydirname) dirname = os.path.join(mydirname, name, 'drive-1') assert os.path.exists(dirname) shutil.rmtree(mydirname)
def minimise_system_energy(L, m_init): N = 16 # discretisation in one dimension cubesize = 100e-9 # cube edge length (m) cellsize = cubesize/N # discretisation in all three dimensions. lex = cubesize/L # exchange length. Km = 1e6 # magnetostatic energy density (J/m**3) Ms = np.sqrt(2*Km/mm.consts.mu0) # magnetisation saturation (A/m) A = 0.5 * mm.consts.mu0 * Ms**2 * lex**2 # exchange energy constant K = 0.1*Km # Uniaxial anisotropy constant u = (0, 0, 1) # Uniaxial anisotropy easy-axis p1 = (0, 0, 0) # Minimum sample coordinate. p2 = (cubesize, cubesize, cubesize) # Maximum sample coordinate. cell = (cellsize, cellsize, cellsize) # Discretisation. region = df.Region(p1=p1, p2=p2) mesh = df.Mesh(region=region, cell=cell) system = mm.System(name=name) system.energy = (mm.Exchange(A=A) + mm.UniaxialAnisotropy(K=K, u=u) + mm.Demag()) system.m = df.Field(mesh, dim=3, value=m_init, norm=Ms) md = calculator.MinDriver() md.drive(system) calculator.delete(system) return system
def test_dict_scalar(self): name = 'zhangli_dict_scalar' H = (0, 0, 1e6) u = {'r1': 0, 'r2': 1} beta = 0.5 Ms = 1e6 mesh = df.Mesh(region=self.region, cell=self.cell, subregions=self.subregions) system = mm.System(name=name) system.energy = mm.Zeeman(H=H) system.dynamics = mm.ZhangLi(u=u, beta=beta) system.m = df.Field(mesh, dim=3, value=(0, 0.1, 1), norm=Ms) td = self.calculator.TimeDriver() td.drive(system, t=0.2e-9, n=50) # u=0 region value = system.m((1e-9, -4e-9, 3e-9)) assert np.linalg.norm(np.cross(value, (0, 0.1 * Ms, Ms))) < 1e-3 # u!=0 region value = system.m((1e-9, 4e-9, 3e-9)) assert np.linalg.norm(np.subtract(value, (0, 0, Ms))) > 1 self.calculator.delete(system)
def test_field_scalar(self): name = 'zhangli_field_scalar' mesh = df.Mesh(region=self.region, cell=self.cell) def u_fun(pos): x, y, z = pos if y <= 0: return 0 else: return 1 H = (0, 0, 1e6) u = df.Field(mesh, dim=1, value=u_fun) beta = 0.5 Ms = 1e6 system = mm.System(name=name) system.energy = mm.Zeeman(H=H) system.dynamics = mm.ZhangLi(u=u, beta=beta) system.m = df.Field(mesh, dim=3, value=(0, 0.1, 1), norm=Ms) td = self.calculator.TimeDriver() td.drive(system, t=0.2e-9, n=50) # u=0 region value = system.m((1e-9, -4e-9, 3e-9)) assert np.linalg.norm(np.cross(value, (0, 0.1 * Ms, Ms))) < 1e-3 # u!=0 region value = system.m((1e-9, 4e-9, 3e-9)) assert np.linalg.norm(np.subtract(value, (0, 0, Ms))) > 1 self.calculator.delete(system)
def test_scalar_dict(self): name = 'dynamics_scalar_dict' H = (0, 0, 1e6) gamma0 = 2.211e5 alpha = {'r1': 0, 'r2': 1} Ms = 1e6 mesh = df.Mesh(region=self.region, n=self.n, subregions=self.subregions) system = mm.System(name=name) system.energy = mm.Zeeman(H=H) system.dynamics = (mm.Precession(gamma0=gamma0) + mm.Damping(alpha=alpha)) system.m = df.Field(mesh, dim=3, value=(0, 0.1, 1), norm=Ms) td = self.calculator.TimeDriver() td.drive(system, t=0.2e-9, n=50) # alpha=0 region value = system.m((1e-9, -4e-9, 3e-9)) assert np.linalg.norm(np.cross(value, (0, 0, Ms))) > 1 # alpha!=0 region value = system.m((1e-9, 4e-9, 3e-9)) assert np.linalg.norm(np.subtract(value, (0, 0, Ms))) < 1e-3 self.calculator.delete(system)
def test_field_values(self): name = 'slonczewski_scalar_values' mesh = df.Mesh(region=self.region, n=self.n) J = df.Field(mesh, dim=1, value=0.5e12) mp = df.Field(mesh, dim=3, value=(1, 0, 0)) P = df.Field(mesh, dim=1, value=0.5) Lambda = df.Field(mesh, dim=1, value=2) eps_prime = df.Field(mesh, dim=1, value=1) H = (0, 0, 1e6) Ms = 1e6 system = mm.System(name=name) system.energy = mm.Zeeman(H=H) system.dynamics = mm.Slonczewski(J=J, mp=mp, P=P, Lambda=Lambda, eps_prime=eps_prime) system.m = df.Field(mesh, dim=3, value=(0, 0.1, 1), norm=Ms) td = self.calculator.TimeDriver() td.drive(system, t=0.2e-9, n=20) # Check if it runs. self.calculator.delete(system)
def test_field(self): name = 'damping_field' mesh = df.Mesh(region=self.region, n=self.n) def value_fun(pos): x, y, z = pos if y <= 0: return 0 else: return 1 H = (0, 0, 1e6) alpha = df.Field(mesh, dim=1, value=value_fun) Ms = 1e6 system = mm.System(name=name) system.energy = mm.Zeeman(H=H) system.dynamics = mm.Damping(alpha=alpha) system.m = df.Field(mesh, dim=3, value=(0, 0.1, 1), norm=Ms) td = self.calculator.TimeDriver() td.drive(system, t=0.2e-9, n=50) # alpha=0 region value = system.m((1e-9, -4e-9, 3e-9)) assert np.linalg.norm(np.cross(value, (0, 0.1 * Ms, Ms))) < 1e-3 # alpha!=0 region value = system.m((1e-9, 4e-9, 3e-9)) assert np.linalg.norm(np.subtract(value, (0, 0, Ms))) < 1e-3 self.calculator.delete(system)
def test_dict(self): name = 'precession_dict' H = (0, 0, 1e6) gamma0 = {'r1': 0, 'r2': 2.211e5} Ms = 1e6 mesh = df.Mesh(region=self.region, n=self.n, subregions=self.subregions) system = mm.System(name=name) system.energy = mm.Zeeman(H=H) system.dynamics = mm.Precession(gamma0=gamma0) system.m = df.Field(mesh, dim=3, value=(0, 0.1, 1), norm=Ms) td = self.calculator.TimeDriver() td.drive(system, t=0.2e-9, n=50) # gamma=0 region value = system.m((1e-9, -4e-9, 3e-9)) assert np.linalg.norm(np.cross(value, (0, 0.1*Ms, Ms))) < 1e-3 # gamma!=0 region value = system.m((1e-9, 4e-9, 3e-9)) assert np.linalg.norm(np.cross(value, (0, 0.1*Ms, Ms))) > 1 self.calculator.delete(system)
def test_dict_values(self): name = 'slonczewski_scalar_values' J = {'r1': 1e12, 'r2': 5e12} mp = {'r1': (0, 0, 1), 'r2': (0, 1, 0)} P = {'r1': 0.4, 'r2': 0.35} Lambda = {'r1': 2, 'r2': 1.5} eps_prime = {'r1': 0, 'r2': 1} H = (0, 0, 1e6) Ms = 1e6 mesh = df.Mesh(region=self.region, n=self.n, subregions=self.subregions) system = mm.System(name=name) system.energy = mm.Zeeman(H=H) system.dynamics = mm.Slonczewski(J=J, mp=mp, P=P, Lambda=Lambda, eps_prime=eps_prime) system.m = df.Field(mesh, dim=3, value=(0, 0.1, 1), norm=Ms) td = self.calculator.TimeDriver() td.drive(system, t=0.2e-9, n=20) # Check if it runs. self.calculator.delete(system)
def test_field(self): name = 'precession_field' mesh = df.Mesh(region=self.region, n=self.n) def value_fun(pos): x, y, z = pos if y <= 0: return 0 else: return 2.211e5 H = (0, 0, 1e6) gamma0 = df.Field(mesh, dim=1, value=value_fun) Ms = 1e6 system = mm.System(name=name) system.energy = mm.Zeeman(H=H) system.dynamics = mm.Precession(gamma0=gamma0) system.m = df.Field(mesh, dim=3, value=(0, 0.1, 1), norm=Ms) td = self.calculator.TimeDriver() td.drive(system, t=0.2e-9, n=50) # gamma=0 region value = system.m((1e-9, -4e-9, 3e-9)) assert np.linalg.norm(np.cross(value, (0, 0.1*Ms, Ms))) < 1e-3 # gamma!=0 region value = system.m((1e-9, 4e-9, 3e-9)) assert np.linalg.norm(np.cross(value, (0, 0.1*Ms, Ms))) > 1 self.calculator.delete(system)
def test_set_m(self): system = mm.System(name="test_sim") system.m = df.Field(self.mesh, dim=3, value=(0, 0, 1)) assert isinstance(system.m, df.Field) assert len(system.m.average) == 3
def test_outputstep(calculator): name = 'output_step' p1 = (0, 0, 0) p2 = (5e-9, 5e-9, 5e-9) n = (2, 2, 2) Ms = 1e6 A = 1e-12 H = (0, 0, 1e6) region = df.Region(p1=p1, p2=p2) mesh = df.Mesh(region=region, n=n) system = mm.System(name=name) system.energy = mm.Exchange(A=A) + mm.Zeeman(H=H) system.m = df.Field(mesh, dim=3, value=(0, 0.1, 1), norm=Ms) md = calculator.MinDriver() md.drive(system, output_step=True) dirname = os.path.join(name, 'drive-0') assert os.path.exists(dirname) assert len(system.table.data.index) > 1 calculator.delete(system)
def test_scalar_field(self): name = 'uniaxialanisotropy_scalar_field' def value_fun(pos): x, y, z = pos if x <= 0: return (1, 0, 0) else: return (0, 1, 0) mesh = df.Mesh(region=self.region, cell=self.cell) K = 1e5 u = df.Field(mesh, dim=3, value=value_fun) Ms = 1e6 system = mm.System(name=name) system.energy = mm.UniaxialAnisotropy(K=K, u=u) system.m = df.Field(mesh, dim=3, value=(1, 1, 0), norm=Ms) md = self.calculator.MinDriver() md.drive(system) value = system.m((-2e-9, -2e-9, -2e-9)) assert np.linalg.norm(np.subtract(value, (Ms, 0, 0))) < 1e-3 value = system.m((2e-9, 2e-9, 2e-9)) assert np.linalg.norm(np.subtract(value, (0, Ms, 0))) < 1e-3 self.calculator.delete(system)
def test_field_vector(self): name = 'uniaxialanisotropy_field_vector' def value_fun(pos): x, y, z = pos if x <= 0: return 0 else: return 1e5 mesh = df.Mesh(region=self.region, cell=self.cell) K = df.Field(mesh, dim=1, value=value_fun) u = (0, 0, 1) Ms = 1e6 system = mm.System(name=name) system.energy = mm.UniaxialAnisotropy(K=K, u=u) system.m = df.Field(mesh, dim=3, value=(0, 0.3, 1), norm=Ms) md = self.calculator.MinDriver() md.drive(system) value = system.m((-2e-9, -2e-9, -2e-9)) assert np.linalg.norm(np.cross(value, (0, 0.3*Ms, Ms))) < 1e-3 value = system.m((2e-9, 2e-9, 2e-9)) assert np.linalg.norm(np.subtract(value, (0, 0, Ms))) < 1e-3 self.calculator.delete(system)
def test_set_dynamics_with_zero(self): system = mm.System() system.dynamics = mm.Precession(2e5) + mm.Damping(0.1) assert isinstance(system.dynamics, mm.Dynamics) assert len(system.dynamics.terms) == 2 system.dynamics = 0 assert isinstance(system.dynamics, mm.Dynamics) assert len(system.dynamics.terms) == 0
def test_set_hamiltonian_with_zero(self): system = mm.System() system.hamiltonian = mm.Exchange(1e-12) + mm.Demag() assert isinstance(system.hamiltonian, mm.Hamiltonian) assert len(system.hamiltonian.terms) == 2 system.hamiltonian = 0 assert isinstance(system.hamiltonian, mm.Hamiltonian) assert len(system.hamiltonian.terms) == 0
def test_drive_exception(self): name = 'timedriver_exception' system = mm.System(name=name) system.energy = self.energy system.dynamics = self.precession + self.damping system.m = self.m td = self.calculator.TimeDriver() with pytest.raises(ValueError): td.drive(system, t=-0.1e-9, n=10) with pytest.raises(ValueError): td.drive(system, t=0.1e-9, n=-10)
def test_init(self): system = mm.System(name="test_sim") assert isinstance(system.hamiltonian, mm.Hamiltonian) assert system.hamiltonian.terms == [] system.hamiltonian = mm.Exchange(1e-12) + mm.Demag() assert len(system.hamiltonian.terms) assert isinstance(system.dynamics, mm.Dynamics) assert system.dynamics.terms == [] assert system.name == "test_sim"
def test_multiple_drives(calculator): name = 'multiple_drives' p1 = (0, 0, 0) p2 = (5e-9, 5e-9, 5e-9) n = (2, 2, 2) Ms = 1e6 A = 1e-12 H = (0, 0, 1e6) region = df.Region(p1=p1, p2=p2) mesh = df.Mesh(region=region, n=n) system = mm.System(name=name) system.energy = mm.Exchange(A=A) + mm.Zeeman(H=H) system.dynamics = (mm.Precession(gamma0=mm.consts.gamma0) + mm.Damping(alpha=1)) system.m = df.Field(mesh, dim=3, value=(0, 0.1, 1), norm=Ms) md = calculator.MinDriver() md.drive(system) dirname = os.path.join(name, 'drive-0') assert os.path.exists(dirname) system.energy.zeeman.H = (1e6, 0, 0) td = calculator.TimeDriver() td.drive(system, t=100e-12, n=10) dirname = os.path.join(name, 'drive-1') assert os.path.exists(dirname) E = calculator.compute(system.energy.zeeman.energy, system) dirname = os.path.join(name, 'compute-0') assert os.path.exists(dirname) td.drive(system, t=100e-12, n=10) dirname = os.path.join(name, 'drive-2') assert os.path.exists(dirname) Heff = calculator.compute(system.energy.zeeman.effective_field, system) dirname = os.path.join(name, 'compute-1') assert os.path.exists(dirname) assert len(os.listdir(name)) == 5 calculator.delete(system)
def test_noevolver_driver(self): name = 'mindriver_noevolver_driver' system = mm.System(name=name) system.energy = self.energy system.m = self.m md = self.calculator.MinDriver(stopping_mxHxm=0.1) md.drive(system) value = system.m(self.mesh.region.random_point()) assert np.linalg.norm(np.subtract(value, (0, 0, self.Ms))) < 1e-3 self.calculator.delete(system)