def test_init_invalid_name(self): p1 = (0, 0, 0) p2 = (10, 10, 10) n = (10, 100, 2) for name in ['mesh name', '2name', ' ', 5, '-name', '+mn']: with pytest.raises((TypeError, ValueError)): # Exception is raised by the descriptor for mesh.name. mesh = df.Mesh(p1=p1, p2=p2, n=n, name=name)
def test_centre(self): p1 = (0, 0, 0) p2 = (100, 100, 100) cell = (1, 1, 1) mesh = df.Mesh(p1=p1, p2=p2, cell=cell) assert mesh.centre == (50, 50, 50) p1 = (-18.5, 10, 0) p2 = (10, 5, 10) cell = (0.1, 0.25, 2) mesh = df.Mesh(p1=p1, p2=p2, cell=cell) assert mesh.centre == (-4.25, 7.5, 5) p1 = (500e-9, 125e-9, 3e-9) p2 = (0, 0, 0) cell = (10e-9, 5e-9, 1e-9) mesh = df.Mesh(p1, p2, cell) assert mesh.centre == (250e-9, 62.5e-9, 1.5e-9)
def test_init_with_region_and_points(self): p1 = (0, -4, 16.5) p2 = (15, 10.1, 11) region = df.Region(p1=p1, p2=p2) n = (10, 10, 10) with pytest.raises(ValueError) as excinfo: mesh = df.Mesh(region=region, p1=p1, p2=p2, n=n) assert 'not both.' in str(excinfo.value)
def test_init_with_n_and_cell(self): p1 = (0, -4, 16.5) p2 = (15, 10.1, 11) n = (15, 141, 11) cell = (1, 0.1, 0.5) with pytest.raises(ValueError) as excinfo: mesh = df.Mesh(p1=p1, p2=p2, n=n, cell=cell) assert 'not both.' in str(excinfo.value)
def test_line(self): mesh = df.Mesh((0, 0, 0), (10e-9, 10e-9, 10e-9), (1e-9, 1e-9, 1e-9)) field = df.Field(mesh, value=(1, 2, 3)) pv = list(field.line(p1=(0, 0, 1e-9), p2=(5e-9, 5e-9, 5e-9))) p, v = zip(*pv) assert len(p) == 100 assert len(v) == 100
def test_value(self): mesh = df.Mesh(p1=(0, 0, 0), p2=(10, 10, 10), cell=(1, 1, 1)) f = df.Field(mesh, dim=3) f.value = (1, 1, 1) assert f.value == (1, 1, 1) f.array[0, 0, 0, 0] = 3 assert isinstance(f.value, np.ndarray)
def test_axis_points(self): p1 = (0, 0, 0) p2 = (10, 6, 8) cell = (2, 2, 2) mesh = df.Mesh(region=df.Region(p1=p1, p2=p2), cell=cell) assert list(mesh.axis_points('x')) == [1.0, 3.0, 5.0, 7.0, 9.0] assert list(mesh.axis_points('y')) == [1.0, 3.0, 5.0] assert list(mesh.axis_points('z')) == [1.0, 3.0, 5.0, 7.0]
def test_cell_greater_than_domain(self): p1 = (0, 0, 0) p2 = (1e-9, 1e-9, 1e-9) args = [(2e-9, 1e-9, 1e-9), (1e-9, 2e-9, 1e-9), (1e-9, 1e-9, 2e-9), (1e-9, 5e-9, 0.1e-9)] for cell in args: with pytest.raises(ValueError) as excinfo: mesh = df.Mesh(p1=p1, p2=p2, cell=cell)
def test_repr_no_pbc(self): p1 = (-1, -4, 11) p2 = (15, 10.1, 12.5) cell = (1, 0.1, 0.5) name = 'meshname' mesh = df.Mesh(p1=p1, p2=p2, cell=cell, name=name) rstr = ('Mesh(p1=(-1, -4, 11), p2=(15, 10.1, 12.5), ' 'cell=(1, 0.1, 0.5), pbc=set(), name=\'meshname\')') assert repr(mesh) == rstr
def test_random_point(self): p1 = (-18.5, 5, 0) p2 = (10, -10, 10e-9) n = (100, 5, 10) mesh = df.Mesh(p1=p1, p2=p2, n=n) for _ in range(50): random_point = mesh.random_point() assert isinstance(random_point, tuple) assert random_point in mesh
def test_init_with_n_and_cell(self): p1 = (0, -4, 16.5) p2 = (15, 10.1, 11) n = (15, 141, 11) cell = (1, 0.1, 0.5) pbc = 'x' name = 'test_mesh' with pytest.raises(ValueError) as excinfo: mesh = df.Mesh(p1=p1, p2=p2, n=n, cell=cell, pbc=pbc, name=name) assert 'One and only one' in str(excinfo.value)
def create_meshes(self): p1_list = [(0, 0, 0), (-5e-9, -8e-9, -10e-9), (10, -5, -80)] p2_list = [(5e-9, 8e-9, 10e-9), (11e-9, 4e-9, 4e-9), (15, 10, 85)] cell_list = [(1e-9, 1e-9, 1e-9), (1e-9, 2e-9, 1e-9), (5, 5, 2.5)] meshes = [] for i in range(len(p1_list)): mesh = df.Mesh(p1_list[i], p2_list[i], cell_list[i]) meshes.append(mesh) return meshes
def test_zero_domain_edge_length(self): args = [[(0, 100e-9, 1e-9), (150e-9, 100e-9, 6e-9), (1e-9, 0.01e-9, 1e-9)], [(0, 100e-9, 0), (150e-9, 101e-9, 0), (2e-9, 1e-9, 0.1e-9)], [(10e9, 10e3, 0), (0.01e12, 11e3, 5), (1e9, 1e3, 1)]] for p1, p2, cell in args: with pytest.raises(ValueError) as excinfo: mesh = df.Mesh(p1=p1, p2=p2, cell=cell) assert "edge length is zero" in str(excinfo)
def test_region_not_aggregate_of_cell(self): args = [[(0, 100e-9, 1e-9), (150e-9, 120e-9, 6e-9), (4e-9, 1e-9, 1e-9)], [(0, 100e-9, 0), (150e-9, 104e-9, 1e-9), (2e-9, 1.5e-9, 0.1e-9)], [(10e9, 10e3, 0), (11e9, 11e3, 5), (1e9, 1e3, 1.5)]] for p1, p2, cell in args: with pytest.raises(ValueError) as excinfo: mesh = df.Mesh(p1=p1, p2=p2, cell=cell)
def test_plane(self): p1 = (0, 0, 0) p2 = (10, 10, 10) cell = (1, 1, 1) mesh = df.Mesh(p1, p2, cell) pi = mesh.plane(z=3, n=(2, 2)) for point in pi: assert isinstance(point, tuple) assert len(point) == 3
def setup(self): # Create meshes using valid arguments from TestMesh. tm = dft.TestMesh() tm.setup() self.meshes = [] for p1, p2, n, cell in tm.valid_args: mesh = df.Mesh(p1=p1, p2=p2, n=n, cell=cell) self.meshes.append(mesh) # Create lists of field values. self.consts = [0, -5., np.pi, 1e-15, 1.2e12, random.random()] self.iters = [(0, 0, 1), (0, -5.1, np.pi), [70, 1e15, 2*np.pi], [5, random.random(), np.pi], np.array([4, -1, 3.7]), np.array([2.1, 0.0, -5*random.random()])] self.sfuncs = [lambda c: 1, lambda c: -2.4, lambda c: -6.4e-15, lambda c: c[0] + c[1] + c[2] + 1, lambda c: (c[0]-1)**2 - c[1]+7 + c[2]*0.1, lambda c: np.sin(c[0]) + np.cos(c[1]) - np.sin(2*c[2])] self.vfuncs = [lambda c: (1, 2, 0), lambda c: (-2.4, 1e-3, 9), lambda c: (c[0], c[1], c[2] + 100), lambda c: (c[0]+c[2]+10, c[1], c[2]+1), lambda c: (c[0]-1, c[1]+70, c[2]*0.1), lambda c: (np.sin(c[0]), np.cos(c[1]), -np.sin(2*c[2]))] # Create a field for plotting mesh = df.Mesh(p1=(-5, -5, -5), p2=(5, 5, 5), cell=(1, 1, 1)) self.pf = df.Field(mesh, dim=3, value=(0, 0, 2)) def normfun(pos): x, y, z = pos if x**2 + y**2 <= 5**2: return 1 else: return 0 self.pf.norm = normfun
def test_repr_no_pbc(self): p1 = (-1, -4, 11) p2 = (15, 10.1, 12.5) cell = (1, 0.1, 0.5) name = "meshname" mesh = df.Mesh(p1=p1, p2=p2, cell=cell, name=name) rstr = ("Mesh(p1=(-1, -4, 11), p2=(15, 10.1, 12.5), " "cell=(1, 0.1, 0.5), pbc=set(), name=\"meshname\")") assert repr(mesh) == rstr
def test_norm(self): mesh = df.Mesh(p1=(0, 0, 0), p2=(10, 10, 10), cell=(1, 1, 1)) f = df.Field(mesh, dim=3, value=(2, 2, 2)) f.norm = 1 assert isinstance(f.norm.value, int) assert f.norm.value == 1 f.array[0, 0, 0, 0] = 3 assert isinstance(f.norm.value, np.ndarray)
def test_contains(self): p1 = (0, 0, 0) p2 = (10e-9, 10e-9, 20e-9) cell = (1e-9, 1e-9, 1e-9) mesh = df.Mesh(p1=p1, p2=p2, cell=cell) assert (0, 0, 0) in mesh assert (10e-9, 10e-9, 10e-9) in mesh assert (5e-9, 5e-9, 5e-9) in mesh assert (11e-9, 11e-9, 11e-9) not in mesh assert (-1e-9, -1e-9, -1e-9) not in mesh
def test_domain_not_aggregate_of_cell(self): args = [[(0, 100e-9, 1e-9), (150e-9, 120e-9, 6e-9), None, (4e-9, 1e-9, 1e-9)], [(0, 100e-9, 0), (150e-9, 104e-9, 1e-9), None, (2e-9, 1.5e-9, 0.1e-9)], [(10e9, 10e3, 0), (11e9, 11e3, 5), None, (1e9, 1e3, 1.5)]] for p1, p2, n, cell in args: with pytest.raises(ValueError) as excinfo: mesh = df.Mesh(p1=p1, p2=p2, n=n, cell=cell) assert 'not an aggregate' in str(excinfo.value)
def test_repr_pbc(self): p1 = (-1, -4, 11) p2 = (15, 10, 12.5) n = (16, 140, 3) pbc = 'x' name = 'meshname' mesh = df.Mesh(p1=p1, p2=p2, n=n, pbc=pbc, name=name) rstr = ('Mesh(p1=(-1, -4, 11), p2=(15, 10, 12.5), ' 'cell=(1.0, 0.1, 0.5), pbc={\'x\'}, name=\'meshname\')') assert repr(mesh) == rstr
def set_with_dict(self): p1 = (0, 0, 0) p2 = (10e-9, 10e-9, 10e-9) n = (5, 5, 5) regions = {'r1': df.Region(p1=(0, 0, 0), p2=(5e-9, 10e-9, 10e-9)), 'r2': df.Region(p1=(5e-9, 0, 0), p2=(10e-9, 10e-9, 10e-9))} mesh = df.Mesh(p1=p1, p2=p2, cell=cell, regions=regions) field = df.Field(mesh, dim=2, value={'r1': (0, 0, 1), 'r2': (0, 0, 2)}) assert np.all(field(3e-9, 7e-9, 9e-9) == (0, 0, 1)) assert np.all(field(5.5e-9, 2e-9, 9e-9) == (0, 0, 1))
def test_slider_selector(self): p1 = (0, 0, 0) p2 = (10e-9, 15e-9, 2e-9) n = (10, 15, 2) mesh = df.Mesh(p1=p1, p2=p2, n=n) f = df.Field(mesh, dim=3, value=(1, 1, 1)) line = f.line(p1=(1e-9, 1e-9, 0.1e-9), p2=(4e-9, 5e-9, 1e-9), n=20) assert isinstance(line.slider(), ipywidgets.SelectionRangeSlider) assert isinstance(line.selector(), ipywidgets.SelectMultiple)
def test_repr_pbc(self): p1 = (-1, -4, 11) p2 = (15, 10.1, 12.5) cell = (1, 0.1, 0.5) pbc = "zyx" name = "meshname" mesh = df.Mesh(p1=p1, p2=p2, cell=cell, pbc=pbc, name=name) rstr = ("Mesh(p1=(-1, -4, 11), p2=(15, 10.1, 12.5), " "cell=(1, 0.1, 0.5), pbc=\"xyz\", name=\"meshname\")") assert repr(mesh) == rstr
def test_random_point(self): p1 = (-18.5, 5, 0) p2 = (10, -10, 10e-9) cell = (0.1e-9, 0.25, 2e-9) mesh = df.Mesh(p1, p2, cell) for _ in range(20): p = mesh.random_point() assert isinstance(p, tuple) for i in range(3): assert mesh.pmin[i] <= p[i] <= mesh.pmax[i]
def test_indices(self): p1 = (0, 0, 0) p2 = (10, 10, 10) n = (10, 10, 10) mesh = df.Mesh(p1=p1, p2=p2, n=n) assert len(list(mesh.indices)) == 1000 for index in mesh.indices: assert isinstance(index, tuple) assert len(index) == 3 assert all([0 <= i <= 9 for i in index])
def test_coordinates(self): p1 = (0, 0, 0) p2 = (10, 10, 10) cell = (1, 1, 1) mesh = df.Mesh(p1=p1, p2=p2, cell=cell) assert len(list(mesh.coordinates)) == 1000 for coord in mesh.coordinates: assert isinstance(coord, tuple) assert len(coord) == 3 assert all([0.5 <= i <= 9.5 for i in coord])
def test_iter(self): p1 = (1, 1, 1) p2 = (5, 5, 5) n = (2, 2, 2) mesh = df.Mesh(p1=p1, p2=p2, n=n) assert len(list(mesh)) == 8 for coord in mesh: assert isinstance(coord, tuple) assert len(coord) == 3 assert all([1.5 <= i <= 4.5 for i in coord])
def test_mpl(self): for p1, p2, n, cell in self.valid_args: mesh = df.Mesh(region=df.Region(p1=p1, p2=p2), n=n, cell=cell) mesh.mpl() filename = 'figure.pdf' with tempfile.TemporaryDirectory() as tmpdir: tmpfilename = os.path.join(tmpdir, filename) mesh.mpl(filename=tmpfilename) plt.close('all')
def setup(self): p1 = (0, 0, 0) p2 = (5e-9, 5e-9, 5e-9) n = (5, 5, 5) self.Ms = 1e6 A = 1e-12 H = (0, 0, 1e6) region = df.Region(p1=p1, p2=p2) self.mesh = df.Mesh(region=region, n=n) self.energy = mm.Exchange(A=A) + mm.Zeeman(H=H) self.m = df.Field(self.mesh, dim=3, value=(0, 1, 0), norm=self.Ms)