Пример #1
0
class unittest_get_group(TestCase):
    """unittest to extract a group as a Mesh object"""
    def setUp(self):
        self.mesh = Mesh()
        self.mesh.element["Triangle3"] = ElementMat(nb_node_per_element=3)
        self.mesh.node = NodeMat()
        self.mesh.node.add_node(np.array([0, 0]))
        self.mesh.node.add_node(np.array([1, 0]))
        self.mesh.node.add_node(np.array([1, 2]))
        self.mesh.node.add_node(np.array([2, 3]))
        self.mesh.node.add_node(np.array([3, 3]))

        self.mesh.add_element(np.array([0, 1, 2]), "Triangle3", group=int(3))
        self.mesh.add_element(np.array([1, 2, 3]), "Triangle3", group=int(3))
        self.mesh.add_element(np.array([4, 2, 3]), "Triangle3", group=int(2))

    def test_ElementMat_1group(self):
        """unittest for 1 group"""

        elem_grp4 = self.mesh.element["Triangle3"].get_group([3])
        solution = np.array([[0, 1, 2], [1, 2, 3]])
        results = elem_grp4.connectivity
        testA = np.sum(abs(solution - results))
        msg = "Wrong output: returned " + str(results) + ", expected: " + str(
            solution)
        DELTA = 1e-10
        self.assertAlmostEqual(testA, 0, msg=msg, delta=DELTA)
Пример #2
0
class unittest_plot_mesh_field(TestCase):
    """unittest to get elements containing specific node(s)"""
    def setUp(self):

        self.simu = Simulation()
        self.out = Output(simu=self.simu)
        self.mesh = Mesh()
        self.mesh.element["Triangle3"] = ElementMat(nb_node_per_element=3)
        self.mesh.node = NodeMat()
        self.mesh.node.add_node(np.array([0, 0]))
        self.mesh.node.add_node(np.array([1, 0]))
        self.mesh.node.add_node(np.array([0, 1]))
        self.mesh.node.add_node(np.array([1, 1]))
        self.mesh.node.add_node(np.array([2, 0]))

        self.mesh.add_element(np.array([0, 1, 2]), "Triangle3", group=int(3))
        self.mesh.add_element(np.array([1, 2, 3]), "Triangle3", group=int(3))
        self.mesh.add_element(np.array([4, 1, 3]), "Triangle3", group=int(2))

    def test_Mesh_ElementMat_NodeMat_3Tgl(self):

        field = np.array([1, 2, 3])
        self.out.plot_mesh_field(mesh=self.mesh,
                                 title="Permeability",
                                 field=field)
        fig = plt.gcf()
        fig.savefig(join(save_path, "test_plot_mesh_field.png"))
Пример #3
0
class unittest_get_all_node_coord(TestCase):
    """unittest for Mesh get_all_node_coord method. Indirect testing of get_group, get_all_node_tags, get_coord"""
    def setUp(self):
        self.mesh = Mesh()
        self.mesh.element["Triangle3"] = ElementMat(nb_node_per_element=3)
        self.mesh.add_element([2, 1, 0], "Triangle3", group=int(3))
        self.mesh.add_element([1, 2, 3], "Triangle3", group=int(2))
        self.mesh.add_element([3, 1, 4], "Triangle3", group=int(2))

        self.mesh.node = NodeMat()
        self.mesh.node.add_node([0, 0])
        self.mesh.node.add_node([1, 0])
        self.mesh.node.add_node([0, 1])
        self.mesh.node.add_node([1, -1])
        self.mesh.node.add_node([2, -1])

    def test_NodeMat_ElementMat_coord(self):
        """unittest with NodeMat and ElementMat objects"""

        result, res_tags = self.mesh.get_all_node_coord()
        solution = np.array([[0, 0], [1, 0], [0, 1], [1, -1], [2, -1]])
        testA = np.sum(abs(result - solution))
        msg = "Wrong result: returned " + str(result) + ", expected: " + str(
            solution)
        DELTA = 1e-10
        self.assertAlmostEqual(testA, 0, msg=msg, delta=DELTA)

    def test_NodeMat_ElementMat_tags(self):
        """unittest with NodeMat and ElementMat objects"""

        result, res_tags = self.mesh.get_all_node_coord()
        solution = np.array([0, 1, 2, 3, 4])
        testA = np.sum(abs(res_tags - solution))
        msg = "Wrong result: returned " + str(res_tags) + ", expected: " + str(
            solution)
        DELTA = 1e-10
        self.assertAlmostEqual(testA, 0, msg=msg, delta=DELTA)

    def test_NodeMat_ElementMat_group(self):
        """unittest with NodeMat and ElementMat objects"""

        result, res_tags = self.mesh.get_all_node_coord(2)
        solution = np.array([1, 2, 3, 4])
        testA = np.sum(abs(res_tags - solution))
        msg = "Wrong result: returned " + str(res_tags) + ", expected: " + str(
            solution)
        DELTA = 1e-10
        self.assertAlmostEqual(testA, 0, msg=msg, delta=DELTA)
Пример #4
0
class unittest_get_coord(TestCase):
    """unittest for NodeMat get_node methods"""
    def setUp(self):
        self.mesh = Mesh()
        self.mesh.element["Triangle3"] = ElementMat(nb_node_per_element=3)
        self.mesh.node = NodeMat()
        self.mesh.node.add_node(np.array([0, 0]))
        self.mesh.node.add_node(np.array([1, 0]))
        self.mesh.node.add_node(np.array([1, 2]))
        self.mesh.node.add_node(np.array([2, 3]))

        self.mesh.add_element(np.array([0, 1, 2]), "Triangle3")
        self.mesh.add_element(np.array([1, 2, 3]), "Triangle3")

    def test_NodeMat_1node(self):
        """unittest for a node"""
        node_tags = self.mesh.get_connectivity(1)
        # Method test 1
        coord = self.mesh.node.get_coord(node_tags)
        # Check result
        solution = np.array([[1, 0], [1, 2], [2, 3]])
        testA = np.sum(abs(solution - coord))
        msg = "Wrong output: returned " + str(coord) + ", expected: " + str(
            solution)
        DELTA = 1e-10
        self.assertAlmostEqual(testA, 0, msg=msg, delta=DELTA)

    def test_NodeMat_false(self):
        """unittest for a false node tag"""

        coord = self.mesh.node.get_coord(-999)
        solution = None
        testA = coord is None
        msg = "Wrong out: returned " + str(coord) + ", expected: " + str(
            solution)
        self.assertTrue(testA, msg=msg)

    def test_NodeMat_None(self):
        """unittest for a None node tag"""

        coord = self.mesh.node.get_coord(None)
        solution = None
        testA = coord is None
        msg = "Wrong output: returned " + str(coord) + ", expected: " + str(
            solution)
        self.assertTrue(testA, msg=msg)
Пример #5
0
    def test_ElementMat_NodeMat(self):
        # Init 1
        # Init
        mesh = Mesh()
        mesh.element["Triangle3"] = ElementMat(nb_node_per_element=3)
        mesh.node = NodeMat()
        mesh.node.add_node(np.array([0, 0]))
        mesh.node.add_node(np.array([1, 0]))
        mesh.node.add_node(np.array([1, 2]))
        mesh.node.add_node(np.array([2, 3]))
        mesh.node.add_node(np.array([3, 3]))

        mesh.add_element(np.array([0, 1, 2]), "Triangle3", group=int(3))
        mesh.add_element(np.array([1, 2, 3]), "Triangle3", group=int(3))
        mesh.add_element(np.array([4, 2, 3]), "Triangle3", group=int(2))

        # Method test 1
        mesh_grp4 = mesh.set_submesh([3])
        # Check results
        solution = np.array([[0, 1, 2], [1, 2, 3]])
        results = mesh_grp4.element["Triangle3"].connectivity
        testA = np.sum(abs(solution - results))
        msg = "Wrong result: returned " + str(results) + ", expected: " + str(
            solution)
        DELTA = 1e-10
        self.assertAlmostEqual(testA, 0, msg=msg, delta=DELTA)

        # Method test 2
        mesh_grp4 = mesh.set_submesh([3, 2])
        # Check results
        solution = np.array([2, 3])
        results = mesh_grp4.element["Segment2"].connectivity
        testA = np.sum(abs(solution - results))
        msg = "Wrong result: returned " + str(results) + ", expected: " + str(
            solution)
        DELTA = 1e-10
        self.assertAlmostEqual(testA, 0, msg=msg, delta=DELTA)
Пример #6
0
class unittest_get_node2element(TestCase):
    """unittest to get elements containing specific node(s)"""

    def setUp(self):
        self.mesh = Mesh()
        self.mesh.element["Triangle3"] = ElementMat(nb_node_per_element=3)
        self.mesh.add_element(np.array([0, 1, 2]), "Triangle3", group=int(3))
        self.mesh.add_element(np.array([1, 2, 3]), "Triangle3", group=int(3))
        self.mesh.add_element(np.array([4, 2, 3]), "Triangle3", group=int(2))

    def test_ElementMat_1node(self):
        """unittest for one existing node """
        elem_tag = self.mesh.element["Triangle3"].get_node2element(1)
        solution = np.array([0, 1])
        testA = np.sum(abs(solution - elem_tag))
        msg = "Wrong output: returned " + str(elem_tag) + ", expected: " + str(solution)
        DELTA = 1e-10
        self.assertAlmostEqual(testA, 0, msg=msg, delta=DELTA)

    def test_ElementMat_fakenode(self):
        """unittest for one non-existing node """
        elem_tag = self.mesh.element["Triangle3"].get_node2element(-99)
        solution = None
        testA = np.sum(abs(solution - elem_tag))
        msg = "Wrong output: returned " + str(elem_tag) + ", expected: " + str(solution)
        DELTA = 1e-10
        self.assertAlmostEqual(testA, 0, msg=msg, delta=DELTA)

    def test_ElementMat_None(self):
        """unittest for None input """
        elem_tag = self.mesh.element["Triangle3"].get_node2element(None)
        solution = None
        testA = np.sum(abs(solution - elem_tag))
        msg = "Wrong output: returned " + str(elem_tag) + ", expected: " + str(solution)
        DELTA = 1e-10
        self.assertAlmostEqual(testA, 0, msg=msg, delta=DELTA)
Пример #7
0
class unittest_get_vertice(TestCase):
    """unittest for Mesh and Element get_all_connectivity methods"""
    def setUp(self):
        self.mesh = Mesh()
        self.mesh.element["Quad4"] = ElementMat(nb_node_per_element=4)
        self.mesh.element["Triangle3"] = ElementMat(nb_node_per_element=3)
        self.mesh.element["Segment2"] = ElementMat(nb_node_per_element=2)
        self.mesh.node = NodeMat()
        self.mesh.node.add_node(np.array([0, 0]))
        self.mesh.node.add_node(np.array([1, 0]))
        self.mesh.node.add_node(np.array([1, 2]))
        self.mesh.node.add_node(np.array([2, 3]))
        self.mesh.node.add_node(np.array([3, 3]))

        self.mesh.add_element(np.array([0, 1, 2]), "Triangle3", group=int(3))
        self.mesh.add_element(np.array([1, 2, 3]), "Triangle3", group=int(3))
        self.mesh.add_element(np.array([4, 2, 3]), "Triangle3", group=int(2))
        self.mesh.add_element(np.array([4, 3]), "Segment2", group=int(2))

    def test_ElementMat_empty(self):
        """unittest with ElementMat object. Test for empty Mesh"""

        solution = 0
        result = self.mesh.get_vertice("Quad4")
        testA = result.size
        msg = ("Wrong output: returned " + str(result.size) + ", expected: " +
               str(solution))
        DELTA = 1e-10
        self.assertAlmostEqual(testA, solution, msg=msg, delta=DELTA)

    def test_ElementMat_seg(self):
        """unittest with ElementMat object. Test for empty Mesh"""

        solution = 4
        result = self.mesh.get_vertice("Segment2")
        testA = result.size
        msg = ("Wrong output: returned " + str(result.size) + ", expected: " +
               str(solution))
        DELTA = 1e-10
        self.assertAlmostEqual(testA, solution, msg=msg, delta=DELTA)

    def test_ElementMat_tgl(self):
        """unittest with ElementMat object. Test for empty Mesh"""

        solution = 18
        result = self.mesh.get_vertice("Triangle3")
        testA = result.size
        msg = ("Wrong output: returned " + str(result.size) + ", expected: " +
               str(solution))
        DELTA = 1e-10
        self.assertAlmostEqual(testA, solution, msg=msg, delta=DELTA)
Пример #8
0
class unittest_get_all_connectivity(TestCase):
    """unittest for Mesh and Element get_all_connectivity methods. Indirect test add_element """

    def setUp(self):
        self.mesh = Mesh()
        self.mesh.element["Triangle3"] = ElementMat(nb_node_per_element=3)
        self.mesh.element["Segment2"] = ElementMat(nb_node_per_element=2)
        self.mesh.add_element([2, 1, 0], "Triangle3", group=int(3))
        self.mesh.add_element([1, 2, 3], "Triangle3", group=int(2))
        self.mesh.add_element([3, 1, 4], "Triangle3", group=int(2))
        self.mesh.add_element([0, 1], "Segment2", group=int(3))

        self.mesh.node = NodeMat()
        self.mesh.node.add_node([0, 0])
        self.mesh.node.add_node([1, 0])
        self.mesh.node.add_node([0, 1])
        self.mesh.node.add_node([1, -1])
        self.mesh.node.add_node([2, -1])

    def test_ElementMat_NodeMat_1seg(self):
        """unittest with ElementDict and NodeMat objects, get 1 segment"""
        tag_test = self.mesh.add_element([2, 1], "Segment2", group=int(3))
        result = self.mesh.get_connectivity(tag_test)
        solution = np.array([2, 1])
        testA = np.sum(abs(result - solution))
        msg = "Wrong output: returned " + str(result) + ", expected: " + str(solution)
        DELTA = 1e-10
        self.assertAlmostEqual(testA, 0, msg=msg, delta=DELTA)

    def test_ElementMat_NodeMat_1tgl(self):
        """unittest with ElementDict and NodeMat objects, with input None"""
        result = self.mesh.get_connectivity(None)

        testA = result is None
        msg = "Wrong result: returned " + str(result) + ", expected: " + str(None)
        self.assertTrue(testA, msg=msg)

    def test_ElementMat_NodeMat_1seg_stupid(self):
        """unittest with ElementDict and NodeMat objects, with only 1 segment"""
        result = self.mesh.get_connectivity(
            -99999
        )  # We test what happened with stupid entry
        # Check result
        testA = result is None
        msg = "Wrong result: returned " + str(result) + ", expected: " + str(None)
        self.assertTrue(testA, msg=msg)
Пример #9
0
class unittest_get_node_tags(TestCase):
    """unittest for elements and nodes getter methods"""
    def setUp(self):
        self.mesh = Mesh()
        self.mesh.element["Triangle3"] = ElementMat(nb_node_per_element=3)
        self.mesh.element["Segment2"] = ElementMat(nb_node_per_element=2)
        self.mesh.node = NodeMat()
        self.mesh.node.add_node(np.array([0, 0]))
        self.mesh.node.add_node(np.array([1, 0]))
        self.mesh.node.add_node(np.array([1, 2]))
        self.mesh.node.add_node(np.array([2, 3]))
        self.mesh.node.add_node(np.array([3, 3]))

        self.mesh.add_element(np.array([0, 1, 2]), "Triangle3", group=int(3))
        self.mesh.add_element(np.array([1, 2, 3]), "Triangle3", group=int(3))
        self.mesh.add_element(np.array([4, 2, 3]), "Triangle3", group=int(2))
        self.mesh.add_element(np.array([4, 3]), "Segment2", group=int(2))

    def test_ElementMat_NodeMat_Triangle3(self):
        """unittest with ElementMat and NodeMat objects, only Triangle3 elements are defined"""

        node_tags = self.mesh.get_node_tags(
            elem_tag=np.array([1, 2], dtype=int))
        solution = np.array([1, 2, 3, 4])

        testA = np.sum(abs(solution - node_tags))
        msg = ("Wrong projection: returned " + str(node_tags) +
               ", expected: " + str(solution))
        DELTA = 1e-10
        self.assertAlmostEqual(testA, 0, msg=msg, delta=DELTA)

    def test_ElementMat_NodeMat_MixedElement(self):
        """unittest with ElementMat and NodeMat objects, both Triangle3 and Segment2 elements are defined"""
        # Method test 2
        node_tags = self.mesh.get_node_tags(np.array([2, 3]))
        # Check result
        solution = np.array([2, 3, 4])
        testA = np.sum(abs(solution - node_tags))
        msg = ("Wrong projection: returned " + str(node_tags) +
               ", expected: " + str(solution))
        DELTA = 1e-10
        self.assertAlmostEqual(testA, 0, msg=msg, delta=DELTA)
class unittest_get_all_connectivity(TestCase):
    """unittest for Mesh and Element get_all_connectivity methods"""
    def setUp(self):
        self.mesh = Mesh()
        self.mesh.element["Triangle3"] = ElementMat(nb_node_per_element=3)
        self.mesh.element["Segment2"] = ElementMat(nb_node_per_element=2)

    def test_ElementMat_empty(self):
        """unittest with ElementMat object. Test for empty Mesh"""

        solution = np.array([])
        result, tags = self.mesh.get_all_connectivity("Segment2")
        testA = result.size
        msg = "Wrong output: returned " + str(
            result.size) + ", expected: " + str(0)
        DELTA = 1e-10
        self.assertAlmostEqual(testA, 0, msg=msg, delta=DELTA)

    def test_ElementMat_1seg(self):
        """unittest with ElementMat object. Test for 1 segment."""

        node_tags = np.array([0, 1])
        self.mesh.add_element(node_tags, "Segment2")
        result, tags = self.mesh.get_all_connectivity("Segment2")

        testA = np.sum(abs(result - node_tags))
        msg = "Wrong output: returned " + str(result) + ", expected: " + str(
            node_tags)
        DELTA = 1e-10
        self.assertAlmostEqual(testA, 0, msg=msg, delta=DELTA)

    def test_ElementMat_2seg(self):
        """unittest with ElementMat object. Test with 2 segment."""

        self.mesh.add_element([0, 1], "Segment2")
        self.mesh.add_element([1, 2], "Segment2")
        result, tags = self.mesh.get_all_connectivity("Segment2")
        solution = np.array([[0, 1], [1, 2]])
        testA = np.sum(abs(result - solution))
        msg = "Wrong output: returned " + str(result) + ", expected: " + str(
            solution)
        DELTA = 1e-10
        self.assertAlmostEqual(testA, 0, msg=msg, delta=DELTA)

    def test_ElementMat_2seg_1tgl(self):
        """unittest with ElementMat object. Test with 2 segment and 1 tgl."""

        self.mesh.add_element([0, 1], "Segment2")
        self.mesh.add_element([1, 2], "Segment2")
        node_tags = np.array([1, 2, 3])
        self.mesh.add_element(node_tags, "Triangle3")
        result, tags = self.mesh.get_all_connectivity("Triangle3")
        testA = np.sum(abs(result - node_tags))
        msg = "Wrong result: returned " + str(result) + ", expected: " + str(
            node_tags)
        DELTA = 1e-10
        self.assertAlmostEqual(testA, 0, msg=msg, delta=DELTA)

    def test_ElementMat_2seg_2tgl(self):
        """unittest with ElementMat object. Test with 2 segment and 2 tgl."""

        self.mesh.add_element([0, 1], "Segment2")
        self.mesh.add_element([1, 2], "Segment2")
        self.mesh.add_element([1, 2, 3], "Triangle3")
        self.mesh.add_element([2, 3, 0], "Triangle3")
        result, tags = self.mesh.get_all_connectivity("Triangle3")
        solution = np.array([[1, 2, 3], [2, 3, 0]])
        testA = np.sum(abs(result - solution))
        msg = "Wrong result: returned " + str(result) + ", expected: " + str(
            solution)
        DELTA = 1e-10
        self.assertAlmostEqual(testA, 0, msg=msg, delta=DELTA)

    def test_ElementMat_3seg_2tgl_1group(self):
        """unittest with ElementMat object. Test for 1 triangle in a group."""

        self.mesh.add_element([0, 1], "Segment2")
        self.mesh.add_element([1, 2], "Segment2")
        self.mesh.add_element([1, 2, 3], "Triangle3")
        self.mesh.add_element([2, 3, 0], "Triangle3")
        node_tags = np.array([2, 1, 0])
        self.mesh.add_element(node_tags, "Triangle3", group=3)
        result, tags = self.mesh.get_all_connectivity("Triangle3",
                                                      group=np.array(
                                                          [3], dtype=int))
        solution = node_tags
        testA = np.sum(abs(result - solution))
        msg = "Wrong result: returned " + str(result) + ", expected: " + str(
            solution)
        DELTA = 1e-10
        self.assertAlmostEqual(testA, 0, msg=msg, delta=DELTA)

        result, tags = self.mesh.get_all_connectivity("Triangle3", group=3)
        solution = node_tags
        testA = np.sum(abs(result - solution))
        msg = "Wrong result: returned " + str(result) + ", expected: " + str(
            solution)
        DELTA = 1e-10
        self.assertAlmostEqual(testA, 0, msg=msg, delta=DELTA)

    def test_ElementMat_3seg_2tgl_2group(self):
        """unittest with ElementMat object. Test for 2 triangle in a group."""

        self.mesh.add_element([0, 1], "Segment2")
        self.mesh.add_element([1, 2], "Segment2")
        self.mesh.add_element([1, 2, 3], "Triangle3")
        self.mesh.add_element([2, 3, 0], "Triangle3", group=3)
        self.mesh.add_element([2, 1, 0], "Triangle3", group=3)
        result, tags = self.mesh.get_all_connectivity("Triangle3", group=3)
        solution = np.array([[2, 3, 0], [2, 1, 0]])
        testA = np.sum(abs(result - solution))
        msg = "Wrong result: returned " + str(result) + ", expected: " + str(
            solution)
        DELTA = 1e-10
        self.assertAlmostEqual(testA, 0, msg=msg, delta=DELTA)
Пример #11
0
class unittest_add_element(TestCase):
    """unittest for add_element methods in Mesh and Element classes"""

    def setUp(self):
        self.mesh = Mesh()
        self.mesh.element["Triangle3"] = ElementMat(nb_node_per_element=3)
        self.mesh.element["Segment2"] = ElementMat(nb_node_per_element=2)

    def test_Mesh_ElementMat_1seg(self):
        """unittest with ElementMat and only 1 segment element"""

        node_tags = np.array([0, 1])
        self.mesh.add_element(node_tags, "Segment2")
        # Check result
        testA = np.sum(abs(self.mesh.element["Segment2"].connectivity - node_tags))
        msg = (
            "Wrong result: returned "
            + str(self.mesh.element["Segment2"].connectivity)
            + ", expected: "
            + str(node_tags)
        )
        DELTA = 1e-10
        self.assertAlmostEqual(testA, 0, msg=msg, delta=DELTA)

        msg = (
            "Wrong result: returned "
            + str(self.mesh.element["Segment2"].nb_elem)
            + ", expected: "
            + str(1)
        )
        DELTA = 1e-10
        self.assertAlmostEqual(
            self.mesh.element["Segment2"].nb_elem, 1, msg=msg, delta=DELTA
        )

    def test_Mesh_ElementMat_2elem(self):
        """unittest with ElementMat and 2 segment element"""

        node_tags = np.array([0, 1])
        self.mesh.add_element(node_tags, "Segment2")
        node_tags = np.array([1, 2])
        self.mesh.add_element(node_tags, "Segment2")

        # Check result
        solution = np.array([[0, 1], [1, 2]])
        testA = np.sum(abs(self.mesh.element["Segment2"].connectivity - solution))
        msg = (
            "Wrong result: returned "
            + str(self.mesh.element["Segment2"].connectivity)
            + ", expected: "
            + str(solution)
        )
        DELTA = 1e-10
        self.assertAlmostEqual(testA, 0, msg=msg, delta=DELTA)

    def test_Mesh_ElementMat_2elem_1tgl(self):
        """unittest with ElementMat and 2 segment element and 1 triangle"""

        self.mesh.add_element(np.array([0, 1]), "Segment2")
        self.mesh.add_element(np.array([1, 2]), "Segment2")
        solution = np.array([1, 2, 3])
        self.mesh.add_element(solution, "Triangle3")

        testA = np.sum(abs(self.mesh.element["Triangle3"].connectivity - solution))
        msg = (
            "Wrong result: returned "
            + str(self.mesh.element["Triangle3"].connectivity)
            + ", expected: "
            + str(solution)
        )
        DELTA = 1e-10
        self.assertAlmostEqual(testA, 0, msg=msg, delta=DELTA)

    def test_Mesh_ElementMat_2elem_1tgl_add_seg_exist(self):
        """unittest with ElementMat and 2 segment element and 1 triangle, try to add an already existing segment."""

        self.mesh.add_element(np.array([0, 1]), "Segment2")
        self.mesh.add_element(np.array([1, 2]), "Segment2")
        self.mesh.add_element(np.array([1, 2, 3]), "Triangle3")
        self.mesh.add_element(np.array([0, 1]), "Segment2")  # already exist

        solution = np.array([[0, 1], [1, 2]])
        testA = np.sum(abs(self.mesh.element["Segment2"].connectivity - solution))
        msg = (
            "Wrong result: returned "
            + str(self.mesh.element["Segment2"].connectivity)
            + ", expected: "
            + str(solution)
        )
        DELTA = 1e-10
        self.assertAlmostEqual(testA, 0, msg=msg, delta=DELTA)

    def test_Mesh_ElementMat_2elem_1tgl_add_tgl_exist(self):
        """unittest with ElementMat and 2 segment element and 1 triangle, try to add an already existing triangle."""

        self.mesh.add_element(np.array([0, 1]), "Segment2")
        self.mesh.add_element(np.array([1, 2]), "Segment2")
        self.mesh.add_element(np.array([1, 2, 3]), "Triangle3")
        self.mesh.add_element(np.array([2, 3, 1]), "Triangle3")  # already exist

        solution = np.array([1, 2, 3])
        testA = np.sum(abs(self.mesh.element["Triangle3"].connectivity - solution))
        msg = (
            "Wrong result: returned "
            + str(self.mesh.element["Triangle3"].connectivity)
            + ", expected: "
            + str(solution)
        )
        DELTA = 1e-10
        self.assertAlmostEqual(testA, 0, msg=msg, delta=DELTA)

    def test_Mesh_ElementMat_2elem_2tgl(self):
        """unittest with ElementMat and 2 segment element and 1 triangle, add 1 triangle."""

        self.mesh.add_element(np.array([0, 1]), "Segment2")
        self.mesh.add_element(np.array([1, 2]), "Segment2")
        self.mesh.add_element(np.array([1, 2, 3]), "Triangle3")
        self.mesh.add_element(np.array([2, 3, 0]), "Triangle3")  # already exist

        solution = np.array([[1, 2, 3], [2, 3, 0]])
        testA = np.sum(abs(self.mesh.element["Triangle3"].connectivity - solution))
        msg = (
            "Wrong result: returned "
            + str(self.mesh.element["Triangle3"].connectivity)
            + ", expected: "
            + str(solution)
        )
        DELTA = 1e-10
        self.assertAlmostEqual(testA, 0, msg=msg, delta=DELTA)

    def test_Mesh_ElementMat_2elem_2tgl_add_tgl_group(self):
        """unittest with ElementMat and 2 segment element and 1 triangle, add 1 triangle with a group number."""

        self.mesh.add_element(np.array([0, 1]), "Segment2")
        self.mesh.add_element(np.array([1, 2]), "Segment2")
        self.mesh.add_element(np.array([1, 2, 3]), "Triangle3")
        self.mesh.add_element(np.array([2, 3, 0]), "Triangle3")  # already exist
        self.mesh.add_element(np.array([2, 0, 1]), "Triangle3", group=3)

        solution = np.array([-1, -1, 3], dtype=int)
        result = self.mesh.element["Triangle3"].group
        testA = (result == solution).all()
        msg = "Wrong result: returned " + str(result) + ", expected: " + str(solution)
        self.assertTrue(testA, msg=msg)

    def test_Mesh_ElementMat_add_stupid(self):
        """unittest with ElementMat and 2 segment element and 1 triangle, add 1 triangle with a group number."""

        self.mesh.add_element(np.array([0, 1]), "Segment2")
        self.mesh.add_element(None, "Segment2")
        self.mesh.add_element(np.array([0, 1, 2]), "Segment2")
        self.mesh.add_element(np.array([1, 1]), "Segment2")

        solution = np.array([0, 1], dtype=int)
        result = self.mesh.element["Segment2"].connectivity
        testA = np.sum(abs(result - solution))
        msg = "Wrong result: returned " + str(result) + ", expected: " + str(solution)
        DELTA = 1e-10
        self.assertAlmostEqual(testA, 0, msg=msg, delta=DELTA)
Пример #12
0
class unittest_interface(TestCase):
    """unittest for elements and nodes getter methods"""
    def setUp(self):
        self.mesh = Mesh()
        self.mesh.element["Triangle3"] = ElementMat(nb_node_per_element=3)
        self.mesh.element["Segment2"] = ElementMat(nb_node_per_element=2)
        self.mesh.node = NodeMat()

        self.other_mesh = Mesh()
        self.other_mesh.element["Triangle3"] = ElementMat(
            nb_node_per_element=3)
        self.other_mesh.element["Segment2"] = ElementMat(nb_node_per_element=2)
        self.other_mesh.node = self.mesh.node

    def test_ElementMat_NodeMat_flat(self):
        """unittest with ElementDict and NodeMat objects"""

        self.mesh.add_element([0, 1, 2], "Triangle3")
        self.mesh.add_element([2, 3, 4], "Triangle3")

        self.mesh.node.add_node([0, 0])
        self.mesh.node.add_node([0.5, 1])
        self.mesh.node.add_node([1, 0])
        self.mesh.node.add_node([1.5, 1])
        self.mesh.node.add_node([2, 0])
        self.mesh.node.add_node([0.5, -1])
        self.mesh.node.add_node([1.5, -1])

        self.other_mesh.add_element([0, 5, 2], "Triangle3")
        self.other_mesh.add_element([4, 6, 2], "Triangle3")

        new_seg_mesh = self.mesh.interface(self.other_mesh)
        solution = np.array([[0, 2], [2, 4]])
        resultat = new_seg_mesh.element["Segment2"].connectivity
        testA = np.sum(abs(resultat - solution))
        msg = ("Wrong projection: returned " + str(resultat) + ", expected: " +
               str(solution))
        DELTA = 1e-10
        self.assertAlmostEqual(testA, 0, msg=msg, delta=DELTA)

    def test_ElementMat_NodeMat_corner_ext(self):
        """unittest with ElementMat and NodeMat objects, extract interface from the external mesh point of view"""
        self.mesh.add_element([0, 1, 2], "Triangle3")
        self.mesh.add_element([1, 2, 3], "Triangle3")
        self.mesh.add_element([1, 5, 4], "Triangle3")

        self.mesh.node.add_node([2, 0])
        self.mesh.node.add_node([3, 0])
        self.mesh.node.add_node([2.5, 1])
        self.mesh.node.add_node([4, 0])
        self.mesh.node.add_node([3.5, 1])
        self.mesh.node.add_node([3, -1])

        self.other_mesh.add_element([0, 1, 5], "Triangle3")

        # Method test 1
        new_seg_mesh = self.mesh.interface(self.other_mesh)

        # Check result
        solution = np.array([[0, 1], [1, 5]])
        result = new_seg_mesh.element["Segment2"].connectivity
        testA = np.sum(abs(result - solution))
        msg = "Wrong result: returned " + str(result) + ", expected: " + str(
            solution)
        DELTA = 1e-10
        self.assertAlmostEqual(testA, 0, msg=msg, delta=DELTA)

    def test_ElementMat_NodeMat_corner_int(self):
        """unittest with ElementMat and NodeMat objects, extract interface from the internal mesh point of view"""
        self.mesh.add_element([0, 1, 2], "Triangle3")
        self.mesh.add_element([1, 2, 3], "Triangle3")
        self.mesh.add_element([1, 5, 4], "Triangle3")

        self.mesh.node.add_node([2, 0])
        self.mesh.node.add_node([3, 0])
        self.mesh.node.add_node([2.5, 1])
        self.mesh.node.add_node([4, 0])
        self.mesh.node.add_node([3.5, 1])
        self.mesh.node.add_node([3, -1])

        self.other_mesh.add_element([0, 1, 5], "Triangle3")

        # Method test 1
        new_seg_mesh = self.other_mesh.interface(self.mesh)

        # Check result
        solution = np.array([[0, 1], [1, 5]])
        result = new_seg_mesh.element["Segment2"].connectivity
        testA = np.sum(abs(result - solution))
        msg = "Wrong result: returned " + str(result) + ", expected: " + str(
            solution)
        DELTA = 1e-10
        self.assertAlmostEqual(testA, 0, msg=msg, delta=DELTA)

    def test_ElementMat_NodeMat_self(self):
        """unittest with ElementMat and NodeMat objects, extract interface on itself"""
        self.mesh.add_element([0, 1, 2], "Triangle3")
        self.mesh.add_element([0, 2, 3], "Triangle3")
        self.mesh.add_element([0, 3, 4], "Triangle3")
        self.mesh.add_element([0, 4, 1], "Triangle3")

        self.mesh.node.add_node([0, 0])
        self.mesh.node.add_node([0, 1])
        self.mesh.node.add_node([1, 0])
        self.mesh.node.add_node([-1, 0])
        self.mesh.node.add_node([0, -1])

        # Method test 1
        new_seg_mesh = self.mesh.interface(self.mesh)

        # Check result
        solution = np.array([])
        result = new_seg_mesh.element["Segment2"].connectivity
        testA = np.sum(abs(result - solution))
        msg = "Wrong result: returned " + str(result) + ", expected: " + str(
            solution)
        DELTA = 1e-10
        self.assertAlmostEqual(testA, 0, msg=msg, delta=DELTA)