def test_construction_and_query(self): """ Test we can construct 2D arrow correctly. """ node1 = Node2D([1.0, 1.0], color="#595959", width=1) node2 = Node2D([0.5, 0.5], color="#595959", width=1) arrow = Arrow2D(node1, node2) ref_x = [1.0, 0.5] self.assertListEqual(arrow.x.tolist(), ref_x) self.assertListEqual(arrow.y.tolist(), ref_x) # Check other attributes. self.assertEqual(arrow.color, "#595959") self.assertEqual(arrow.edgecolor, "#595959") self.assertEqual(arrow.dx, -0.5) self.assertEqual(arrow.dy, -0.5) self.assertEqual(arrow.alpha, 1) self.assertListEqual(arrow.start.tolist(), [1.0, 1.0]) self.assertListEqual(arrow.end.tolist(), [0.5, 0.5]) self.assertEqual(arrow.head_length, 0.06) self.assertEqual(arrow.head_width, 0.03) self.assertEqual(arrow.shape, "full") self.assertEqual(arrow.style, "solid") self.assertEqual(arrow.width, 1) self.assertEqual(arrow.zorder, 0)
def test_move(self): """ Test the edge can be moved correctly. """ node1 = Node2D([1.0, 1.0], color="#595959", width=1) node2 = Node2D([0.5, 0.5], color="#595959", width=1) edge = Edge2D(node1, node2, n=10) supercell = SuperCell2D([node1, node2], [edge]) supercell.move([0.5, 0.5]) self.assertListEqual(node1.coordinate.tolist(), [1.5, 1.5]) self.assertListEqual(node2.coordinate.tolist(), [1.0, 1.0]) ref_x = [1.5, 1.4545454545454546, 1.4090909090909092, 1.3636363636363638, 1.3181818181818181, 1.2727272727272727, 1.2272727272727273, 1.1818181818181819, 1.1363636363636362, 1.0909090909090908, 1.0454545454545454, 1.0] self.assertListEqual(edge.x.tolist(), ref_x) self.assertListEqual(edge.y.tolist(), ref_x)
def test_construction_and_query(self): """ Test we can construct SuperCell2D correctly. """ node1 = Node2D([1.0, 1.0], color="#595959", width=1) node2 = Node2D([0.5, 0.5], color="#595959", width=1) edge = Edge2D(node1, node2, n=10) supercell = SuperCell2D([node1, node2], [edge])
def test_add_node(self): """ Make sure we can add node to canvas correctly. """ canvas = Grid2DCanvas() n1 = Node2D([0.5, 0.5]) n2 = Node2D([1.0, 1.0]) canvas.add_node(n1) canvas.add_node(n2) # Check nodes in canvas. self.assertTrue(canvas.nodes) for node in canvas.nodes: self.assertTrue(isinstance(node, Node2D)) # Check colors. for c in canvas.node_edgecolors: self.assertEqual(c, "#000000") for c in canvas.node_colors: self.assertEqual(c, "#000000") # Check cooridnates. ref_coordinates = [[0.5, 0.5], [1.0, 1.0]] self.assertListEqual(ref_coordinates, canvas.node_coordinates.tolist()) plt.close(canvas.figure)
def test_clone(self): """ Make sure we can clone a new supercell. """ node1 = Node2D([1.0, 1.0], color="#595959", width=1) node2 = Node2D([0.5, 0.5], color="#595959", width=1) edge = Edge2D(node1, node2, n=10) supercell = SuperCell2D([node1, node2], [edge]) supercell_clone = supercell.clone([0.5, 0.5]) self.assertListEqual(supercell_clone.nodes[0].coordinate.tolist(), [1.5, 1.5]) self.assertListEqual(supercell_clone.nodes[1].coordinate.tolist(), [1.0, 1.0]) ref_x = [1.5, 1.4545454545454546, 1.4090909090909092, 1.3636363636363638, 1.3181818181818181, 1.2727272727272727, 1.2272727272727273, 1.1818181818181819, 1.1363636363636362, 1.0909090909090908, 1.0454545454545454, 1.0] self.assertListEqual(supercell_clone.edges[0].x.tolist(), ref_x) self.assertListEqual(supercell_clone.edges[0].y.tolist(), ref_x)
def test_construction_from2d(self): """ Make sure we can construct 3D edge from a 2D edge. """ node1 = Node2D([1.0, 1.0]) node2 = Node2D([1.0, 2.0]) edge2d = Edge2D(node1, node2) edge3d = Edge3D.from2d(edge2d) self.assertTrue(isinstance(edge3d, Edge3D))
def test_add(self): node1 = Node2D([1.0, 1.0], color="#595959", width=1) node2 = Node2D([0.5, 0.5], color="#595959", width=1) edge = Edge2D(node1, node2, n=10) s1 = SuperCell2D([node1, node2], [edge]) s2 = SuperCell2D([node1, node2], [edge]) s = s1 + s2 self.assertListEqual(s.nodes, [node1, node2, node1, node2]) self.assertListEqual(s.edges, [edge, edge])
def to3d(self): """ Make sure we can map 2D supercell to 3D space. """ node1 = Node2D([1.0, 1.0], color="#595959", width=1) node2 = Node2D([0.5, 0.5], color="#595959", width=1) edge = Edge2D(node1, node2, n=10) supercell = SuperCell2D([node1, node2], [edge]) supercell3d = supercell.to3d() self.assertTrue(isinstance(supercell3d, SuperCell3D))
def test_construction_from2d(self): """ Make sure we can construct 3D supercell from 2D suercell. """ node1 = Node2D([1.0, 1.0]) node2 = Node2D([1.0, 2.0]) edge = Edge2D(node1, node2, n=10) supercell = SuperCell2D([node1, node2], [edge]) supercell3d = SuperCell3D.from2d(supercell) self.assertTrue(isinstance(supercell3d, SuperCell3D))
def test_to3d(self): """ Make sure we can map 2D edge to 3D space correctly. """ node1 = Node2D([1.0, 1.0], color="#595959", width=1) node2 = Node2D([0.5, 0.5], color="#595959", width=1) edge = Edge2D(node1, node2, n=10) edge3d = edge.to3d() self.assertTrue(isinstance(edge3d, Edge3D)) self.assertEqual(edge3d.color, edge.color) self.assertEqual(edge3d.width, edge.width)
def to3d(self, **kwargs): """ Map a 2D edge to 3D space. """ # Map endpoints. plane = kwargs["plane"] node1 = Node2D(self.start).to3d(plane=plane) node2 = Node2D(self.end).to3d(plane=plane) edge3d = Edge3D(node1, node2, n=self.n, color=self.color, width=self.width, style=self.style, alpha=self.alpha, zorder=self.zorder, **kwargs) return edge3d
def test_remove(self): """ Make sure we can remove a component correctly. """ canvas = Grid2DCanvas() n1 = Node2D([0.5, 0.5]) n2 = Node2D([1.0, 1.0]) canvas.add_node(n1) canvas.add_node(n2) self.assertTrue(n1 in canvas.nodes) canvas.remove(n1) self.assertFalse(n1 in canvas.nodes)
def test_draw(self): """ Make sure we can draw in grid canvas without exception raised. """ canvas = Grid2DCanvas() n1 = Node2D([0.5, 0.5]) n2 = Node2D([1.0, 1.0]) canvas.add_node(n1) canvas.add_node(n2) canvas.draw() plt.close(canvas.figure)
def test_construction_and_query(self): """ Test we can construct Grid2DNode correctly. """ node1 = Node2D([1.0, 1.0], color="#595959", width=1) node2 = Node2D([0.5, 0.5], color="#595959", width=1) edge = Edge2D(node1, node2, n=10) ref_x = [ 1.0, 0.9545454545454546, 0.9090909090909091, 0.8636363636363636, 0.8181818181818181, 0.7727272727272727, 0.7272727272727273, 0.6818181818181819, 0.6363636363636364, 0.5909090909090908, 0.5454545454545454, 0.5 ] self.assertListEqual(edge.x.tolist(), ref_x) self.assertListEqual(edge.y.tolist(), ref_x)
def test_to3d(self): """ Make sure we can map all components in 2D canvas to 3D canvas. """ canvas2d = Grid2DCanvas() n1 = Node2D([0.5, 0.5]) n2 = Node2D([1.0, 1.0]) canvas2d.add_node(n1) canvas2d.add_node(n2) canvas3d = Grid3DCanvas() canvas2d.to3d(canvas3d) self.assertListEqual(canvas3d.nodes[0].coordinate.tolist(), [0.5, 0.5, 0.0]) self.assertListEqual(canvas3d.nodes[1].coordinate.tolist(), [1.0, 1.0, 0.0])
def test_extract_node(self): """ Make sure we can extract node from canvas correctly. """ canvas2d = Grid2DCanvas() n1 = Node2D([0.5, 0.5], label=True) n2 = Node2D([1.0, 1.0]) canvas2d.add_node(n1) canvas2d.add_node(n2) canvas3d = Grid3DCanvas() canvas2d.to3d(canvas3d) n_extracted = canvas3d.extract_node(n1.label) self.assertTrue(n_extracted) self.assertEqual(n_extracted[0].label, n1.label)
def test_clone(self): """ Make sure we can clone a node correctly. """ node = Node2D([0.5, 0.5], color="#595959", line_width=1) node_clone = node.clone(relative_position=[0.5, 0.5]) self.assertListEqual(node_clone.coordinate.tolist(), [1.0, 1.0]) self.assertFalse(node is node_clone)
def test_move(self): """ Test the arrow can be moved correctly. """ node1 = Node2D([1.0, 1.0], color="#595959", width=1) node2 = Node2D([0.5, 0.5], color="#595959", width=1) arrow = Arrow2D(node1, node2) arrow.move([0.5, 0.5]) ref_x = [1.5, 1.0] self.assertListEqual(arrow.x.tolist(), ref_x) self.assertListEqual(arrow.y.tolist(), ref_x) # The delta x and y should be unchanged. self.assertEqual(arrow.dx, -0.5) self.assertEqual(arrow.dy, -0.5)
def test_construction_from_2d(self): """ Make sure we can construct a 3D node from 2D node. """ node2d = Node2D([0.5, 0.5], color="#595959") node3d = Node3D.from2d(node2d) self.assertTrue(node3d, Node3D) self.assertListEqual(node3d.coordinate.tolist(), [0.5, 0.5, 0.0]) self.assertEqual(node3d.color, node2d.color)
def test_to3d(self): """ Make sure we can convert 2D node to corresponding 3D node. """ node2d = Node2D([0.5, 0.5], color="#595959", line_width=1) node3d = node2d.to3d() self.assertTrue(isinstance(node3d, Node3D)) self.assertListEqual(node3d.coordinate.tolist(), [0.5, 0.5, 0.0]) self.assertEqual(node2d.color, node3d.color) self.assertEqual(node2d.line_width, node3d.line_width)
def test_move(self): """ Make sure we can move the node correctly. """ node = Node2D([1.0, 1.0], color="#595959", line_width=1) node.move([1.0, 1.0]) self.assertListEqual(node.coordinate.tolist(), [2.0, 2.0]) # Check chain operation. node.move([ -1.0, -1.0, ]).move([-1.0, -1.0]) self.assertListEqual(node.coordinate.tolist(), [0.0, 0.0])
def test_construction_and_query(self): """ Test we can construct Grid2DNode correctly. """ node = Node2D([1.0, 1.0], color="#595959", line_width=1) self.assertListEqual(node.coordinate.tolist(), [1.0, 1.0]) self.assertEqual(node.color, "#595959") self.assertEqual(node.edgecolor, "#595959") self.assertEqual(node.size, 400) self.assertEqual(node.style, "o") self.assertEqual(node.line_width, 1) # Exception is expected when invalid cooridnate passed in. self.assertRaises(ValueError, Node2D, [1.0, 1.0, 0.0])