def test_ECM(self):
        js = JsonRead.readFile("Structures/structure00.json")
        truss = Truss(js)
        ECM = truss.get_ECM()
        ans = np.array([[0, 1],[1, 3],[1, 2]], dtype=int)

        self.assertEqual(ECM.all(), ans.all(), "ECM function is incorrect")
    def test_main(self):
        self.maxDiff = None
        struct_files = ['structure00.json', 'structure01.json']
        out_files = ['output00.json', 'output01.json']

        for x, y in zip(struct_files, out_files):
            structure = readFile('Structures/' + x)
            truss = Truss(structure)
            d, fr = truss.main_func()
            out = truss.gen_output(d, fr)

            deeptest.assertDeepAlmostEqual(self,
                                           readFile('Expected_output/' + y),
                                           out)
    def test_k_local(self):
        files = ['Structures/structure00.json', 'Structures/structure01.json']
        ans = [[[[0.6012389205100935,0.4762626539760776,0.7072913266665941,-0.6012389205100935,-0.4762626539760776,-0.7072913266665941],[0.4762626539760776,0.37726452469160976,0.5602705228841516,-0.4762626539760776,-0.37726452469160976,-0.5602705228841516],[0.7072913266665941,0.5602705228841516,0.8320502943378438,-0.7072913266665941,-0.5602705228841516,-0.8320502943378438],[-0.6012389205100935,-0.4762626539760776,-0.7072913266665941,0.6012389205100935,0.4762626539760776,0.7072913266665941],[-0.4762626539760776,-0.37726452469160976,-0.5602705228841516,0.4762626539760776,0.37726452469160976,0.5602705228841516],[-0.7072913266665941,-0.5602705228841516,-0.8320502943378438,0.7072913266665941,0.5602705228841516,0.8320502943378438]],[[0.6012389205100935,0.4762626539760776,0.7072913266665941,-0.6012389205100935,-0.4762626539760776,-0.7072913266665941],[0.4762626539760776,0.37726452469160976,0.5602705228841516,-0.4762626539760776,-0.37726452469160976,-0.5602705228841516],[0.7072913266665941,0.5602705228841516,0.8320502943378438,-0.7072913266665941,-0.5602705228841516,-0.8320502943378438],[-0.6012389205100935,-0.4762626539760776,-0.7072913266665941,0.6012389205100935,0.4762626539760776,0.7072913266665941],[-0.4762626539760776,-0.37726452469160976,-0.5602705228841516,0.4762626539760776,0.37726452469160976,0.5602705228841516],[-0.7072913266665941,-0.5602705228841516,-0.8320502943378438,0.7072913266665941,0.5602705228841516,0.8320502943378438]],[[0.40462543377395666,0.5828319831073592,0.6464020034283373,-0.40462543377395666,-0.5828319831073592,-0.6464020034283373],[0.5828319831073592,0.8395248844456622,0.9310926355488939,-0.5828319831073592,-0.8395248844456622,-0.9310926355488939],[0.6464020034283373,0.9310926355488939,1.0326477654629869,-0.6464020034283373,-0.9310926355488939,-1.0326477654629869],[-0.40462543377395666,-0.5828319831073592,-0.6464020034283373,0.40462543377395666,0.5828319831073592,0.6464020034283373],[-0.5828319831073592,-0.8395248844456622,-0.9310926355488939,0.5828319831073592,0.8395248844456622,0.9310926355488939],[-0.6464020034283373,-0.9310926355488939,-1.0326477654629869,0.6464020034283373,0.9310926355488939,1.0326477654629869]]],
               [[[44090.85417074018, 34925.92795824568, 51868.030622216895, -44090.85417074018, -34925.92795824568,
                 -51868.030622216895],
                [34925.92795824568, 27666.06514405138, 41086.50501150444, -34925.92795824568, -27666.06514405138,
                 -41086.50501150444],
                [51868.030622216895, 41086.50501150444, 61017.021584775204, -51868.030622216895, -41086.50501150444,
                 -61017.021584775204],
                [-44090.85417074018, -34925.92795824568, -51868.030622216895, 44090.85417074018, 34925.92795824568,
                 51868.030622216895],
                [-34925.92795824568, -27666.06514405138, -41086.50501150444, 34925.92795824568, 27666.06514405138,
                 41086.50501150444],
                [-51868.030622216895, -41086.50501150444, -61017.021584775204, 51868.030622216895, 41086.50501150444,
                 61017.021584775204]
                ],
               [[44090.85417074018, 34925.92795824568, 51868.030622216895, -44090.85417074018, -34925.92795824568,
                 -51868.030622216895],
                [34925.92795824568, 27666.06514405138, 41086.50501150444, -34925.92795824568, -27666.06514405138,
                 -41086.50501150444],
                [51868.030622216895, 41086.50501150444, 61017.021584775204, -51868.030622216895, -41086.50501150444,
                 -61017.021584775204],
                [-44090.85417074018, -34925.92795824568, -51868.030622216895, 44090.85417074018, 34925.92795824568,
                 51868.030622216895],
                [-34925.92795824568, -27666.06514405138, -41086.50501150444, 34925.92795824568, 27666.06514405138,
                 41086.50501150444],
                [-51868.030622216895, -41086.50501150444, -61017.021584775204, 51868.030622216895, 41086.50501150444,
                 61017.021584775204]
                ],
               [[38551.266217366145, 55530.149776297716, 61586.87427327159, -38551.266217366145, -55530.149776297716,
                 -61586.87427327159],
                [55530.149776297716, 79986.93264163118, 88711.18093413537, -55530.149776297716, -79986.93264163118,
                 -88711.18093413537],
                [61586.87427327159, 88711.18093413537, 98386.99100999074, -61586.87427327159, -88711.18093413537,
                 -98386.99100999074],
                [-38551.266217366145, -55530.149776297716, -61586.87427327159, 38551.266217366145, 55530.149776297716,
                 61586.87427327159],
                [-55530.149776297716, -79986.93264163118, -88711.18093413537, 55530.149776297716, 79986.93264163118,
                 88711.18093413537],
                [-61586.87427327159, -88711.18093413537, -98386.99100999074, 61586.87427327159, 88711.18093413537,
                 98386.99100999074]]]]
        for x, y in zip(files, ans):
            structure = JsonRead.readFile(x)
            truss = Truss(structure)
            k_local = truss.get_k_global()

            self.assertAlmostEqual(k_local.all(), np.array(y).all(), "k_local function is incorrect")
    def test_area(self):
        type = ["rectangle", "rectangle", "circle", "circle"]
        dim = [
            {"y": 5.2, "z": 2},
            {"y": 0, "z": -1},
            {"radius": 4},
            {"radius": .2}
        ]

        ans = [10.4, 0, 50.265482457436, 0.125663706143]

        for t, d, a in zip(type, dim, ans):
            self.assertAlmostEqual(Truss.get_area(None, t, d), a)
    def test_k_global(self):
        files = ['Structures/structure00.json', 'Structures/structure01.json']
        k_local = [
            [[[0.6012389205100935,0.4762626539760776,0.7072913266665941,-0.6012389205100935,-0.4762626539760776,-0.7072913266665941],[0.4762626539760776,0.37726452469160976,0.5602705228841516,-0.4762626539760776,-0.37726452469160976,-0.5602705228841516],[0.7072913266665941,0.5602705228841516,0.8320502943378438,-0.7072913266665941,-0.5602705228841516,-0.8320502943378438],[-0.6012389205100935,-0.4762626539760776,-0.7072913266665941,0.6012389205100935,0.4762626539760776,0.7072913266665941],[-0.4762626539760776,-0.37726452469160976,-0.5602705228841516,0.4762626539760776,0.37726452469160976,0.5602705228841516],[-0.7072913266665941,-0.5602705228841516,-0.8320502943378438,0.7072913266665941,0.5602705228841516,0.8320502943378438]],[[0.6012389205100935,0.4762626539760776,0.7072913266665941,-0.6012389205100935,-0.4762626539760776,-0.7072913266665941],[0.4762626539760776,0.37726452469160976,0.5602705228841516,-0.4762626539760776,-0.37726452469160976,-0.5602705228841516],[0.7072913266665941,0.5602705228841516,0.8320502943378438,-0.7072913266665941,-0.5602705228841516,-0.8320502943378438],[-0.6012389205100935,-0.4762626539760776,-0.7072913266665941,0.6012389205100935,0.4762626539760776,0.7072913266665941],[-0.4762626539760776,-0.37726452469160976,-0.5602705228841516,0.4762626539760776,0.37726452469160976,0.5602705228841516],[-0.7072913266665941,-0.5602705228841516,-0.8320502943378438,0.7072913266665941,0.5602705228841516,0.8320502943378438]],[[0.40462543377395666,0.5828319831073592,0.6464020034283373,-0.40462543377395666,-0.5828319831073592,-0.6464020034283373],[0.5828319831073592,0.8395248844456622,0.9310926355488939,-0.5828319831073592,-0.8395248844456622,-0.9310926355488939],[0.6464020034283373,0.9310926355488939,1.0326477654629869,-0.6464020034283373,-0.9310926355488939,-1.0326477654629869],[-0.40462543377395666,-0.5828319831073592,-0.6464020034283373,0.40462543377395666,0.5828319831073592,0.6464020034283373],[-0.5828319831073592,-0.8395248844456622,-0.9310926355488939,0.5828319831073592,0.8395248844456622,0.9310926355488939],[-0.6464020034283373,-0.9310926355488939,-1.0326477654629869,0.6464020034283373,0.9310926355488939,1.0326477654629869]]],
            [[[44090.85417074018, 34925.92795824568, 51868.030622216895, -44090.85417074018, -34925.92795824568, -51868.030622216895],[34925.92795824568, 27666.06514405138, 41086.50501150444, -34925.92795824568, -27666.06514405138, -41086.50501150444],[51868.030622216895, 41086.50501150444, 61017.021584775204, -51868.030622216895, -41086.50501150444, -61017.021584775204], [-44090.85417074018, -34925.92795824568, -51868.030622216895, 44090.85417074018, 34925.92795824568,  51868.030622216895], [-34925.92795824568, -27666.06514405138, -41086.50501150444, 34925.92795824568, 27666.06514405138, 41086.50501150444], [-51868.030622216895, -41086.50501150444, -61017.021584775204, 51868.030622216895, 41086.50501150444, 61017.021584775204]],               [[44090.85417074018, 34925.92795824568, 51868.030622216895, -44090.85417074018, -34925.92795824568,  -51868.030622216895],                [34925.92795824568, 27666.06514405138, 41086.50501150444, -34925.92795824568, -27666.06514405138,  -41086.50501150444],                [51868.030622216895, 41086.50501150444, 61017.021584775204, -51868.030622216895, -41086.50501150444, -61017.021584775204],                [-44090.85417074018, -34925.92795824568, -51868.030622216895, 44090.85417074018, 34925.92795824568, 51868.030622216895],                [-34925.92795824568, -27666.06514405138, -41086.50501150444, 34925.92795824568, 27666.06514405138, 41086.50501150444],                [-51868.030622216895, -41086.50501150444, -61017.021584775204, 51868.030622216895, 41086.50501150444, 61017.021584775204]],               [[38551.266217366145, 55530.149776297716, 61586.87427327159, -38551.266217366145, -55530.149776297716, -61586.87427327159],                [55530.149776297716, 79986.93264163118, 88711.18093413537, -55530.149776297716, -79986.93264163118, -88711.18093413537],                [61586.87427327159, 88711.18093413537, 98386.99100999074, -61586.87427327159, -88711.18093413537, -98386.99100999074],                [-38551.266217366145, -55530.149776297716, -61586.87427327159, 38551.266217366145, 55530.149776297716, 61586.87427327159],                [-55530.149776297716, -79986.93264163118, -88711.18093413537, 55530.149776297716, 79986.93264163118, 88711.18093413537],                [-61586.87427327159, -88711.18093413537, -98386.99100999074, 61586.87427327159, 88711.18093413537, 98386.99100999074]]]
        ]
        ans = [
            [[0.6012389205100935, 0.4762626539760776, 0.7072913266665941, -0.6012389205100935, -0.4762626539760776,
              -0.7072913266665941, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0],
             [0.4762626539760776, 0.37726452469160976, 0.5602705228841516, -0.4762626539760776, -0.37726452469160976,
              -0.5602705228841516, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0],
             [0.7072913266665941, 0.5602705228841516, 0.8320502943378438, -0.7072913266665941, -0.5602705228841516,
              -0.8320502943378438, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0],
             [-0.6012389205100935, -0.4762626539760776, -0.7072913266665941, 1.6071032747941438, 1.5353572910595144,
              2.0609846567615255, -0.40462543377395666, -0.5828319831073592, -0.6464020034283373, -0.6012389205100935,
              -0.4762626539760776, -0.7072913266665941],
             [-0.4762626539760776, -0.37726452469160976, -0.5602705228841516, 1.5353572910595144, 1.5940539338288817,
              2.051633681317197, -0.5828319831073592, -0.8395248844456622, -0.9310926355488939, -0.4762626539760776,
              -0.37726452469160976, -0.5602705228841516],
             [-0.7072913266665941, -0.5602705228841516, -0.8320502943378438, 2.0609846567615255, 2.051633681317197,
              2.6967483541386743, -0.6464020034283373, -0.9310926355488939, -1.0326477654629869, -0.7072913266665941,
              -0.5602705228841516, -0.8320502943378438],
             [0.0, 0.0, 0.0, -0.40462543377395666, -0.5828319831073592, -0.6464020034283373, 0.40462543377395666,
              0.5828319831073592, 0.6464020034283373, 0.0, 0.0, 0.0],
             [0.0, 0.0, 0.0, -0.5828319831073592, -0.8395248844456622, -0.9310926355488939, 0.5828319831073592,
              0.8395248844456622, 0.9310926355488939, 0.0, 0.0, 0.0],
             [0.0, 0.0, 0.0, -0.6464020034283373, -0.9310926355488939, -1.0326477654629869, 0.6464020034283373,
              0.9310926355488939, 1.0326477654629869, 0.0, 0.0, 0.0],
             [0.0, 0.0, 0.0, -0.6012389205100935, -0.4762626539760776, -0.7072913266665941, 0.0, 0.0, 0.0,
              0.6012389205100935, 0.4762626539760776, 0.7072913266665941],
             [0.0, 0.0, 0.0, -0.4762626539760776, -0.37726452469160976, -0.5602705228841516, 0.0, 0.0, 0.0,
              0.4762626539760776, 0.37726452469160976, 0.5602705228841516],
             [0.0, 0.0, 0.0, -0.7072913266665941, -0.5602705228841516, -0.8320502943378438, 0.0, 0.0, 0.0,
              0.7072913266665941, 0.5602705228841516, 0.8320502943378438]],
            [[44090.85417074018, 34925.92795824568, 51868.030622216895, -44090.85417074018, -34925.92795824568,
              -51868.030622216895, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0],
             [34925.92795824568, 27666.06514405138, 41086.50501150444, -34925.92795824568, -27666.06514405138,
              -41086.50501150444, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0],
             [51868.030622216895, 41086.50501150444, 61017.021584775204, -51868.030622216895, -41086.50501150444,
              -61017.021584775204, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0],
             [-44090.85417074018, -34925.92795824568, -51868.030622216895, 126732.9745588465, 125382.00569278907,
              165322.9355177054, -38551.266217366145, -55530.149776297716, -61586.87427327159, -44090.85417074018,
              -34925.92795824568, -51868.030622216895],
             [-34925.92795824568, -27666.06514405138, -41086.50501150444, 125382.00569278907, 135319.06292973395,
              170884.19095714425, -55530.149776297716, -79986.93264163118, -88711.18093413537, -34925.92795824568,
              -27666.06514405138, -41086.50501150444],
             [-51868.030622216895, -41086.50501150444, -61017.021584775204, 165322.9355177054, 170884.19095714425,
              220421.03417954117, -61586.87427327159, -88711.18093413537, -98386.99100999074, -51868.030622216895,
              -41086.50501150444, -61017.021584775204],
             [0.0, 0.0, 0.0, -38551.266217366145, -55530.149776297716, -61586.87427327159, 38551.266217366145,
              55530.149776297716, 61586.87427327159, 0.0, 0.0, 0.0],
             [0.0, 0.0, 0.0, -55530.149776297716, -79986.93264163118, -88711.18093413537, 55530.149776297716,
              79986.93264163118, 88711.18093413537, 0.0, 0.0, 0.0],
             [0.0, 0.0, 0.0, -61586.87427327159, -88711.18093413537, -98386.99100999074, 61586.87427327159,
              88711.18093413537, 98386.99100999074, 0.0, 0.0, 0.0],
             [0.0, 0.0, 0.0, -44090.85417074018, -34925.92795824568, -51868.030622216895, 0.0, 0.0, 0.0,
              44090.85417074018, 34925.92795824568, 51868.030622216895],
             [0.0, 0.0, 0.0, -34925.92795824568, -27666.06514405138, -41086.50501150444, 0.0, 0.0, 0.0,
              34925.92795824568, 27666.06514405138, 41086.50501150444],
             [0.0, 0.0, 0.0, -51868.030622216895, -41086.50501150444, -61017.021584775204, 0.0, 0.0, 0.0,
              51868.030622216895, 41086.50501150444, 61017.021584775204]]
            ]
        for x, y, z in zip(files, ans, k_local):
            structure = JsonRead.readFile(x)
            truss = Truss(structure)
            K = truss.get_K(np.array(z))

            self.assertAlmostEqual(K.all(), np.array(y).all(), "k_global function is incorrect")
    def test_inverse(self):
        for i in range(5):
            arr = np.random.rand(20, 20)

            self.assertAlmostEqual(np.linalg.inv(arr).all(), Truss.inv(None, arr).all(), "Inverse function is incorrect")
from JsonRead import readFile, writeFile
from Truss_class import Truss
import sys

if __name__ == "__main__":
    structure = readFile(sys.argv[1])
    truss = Truss(structure)

    # nodal deformation and Internal Forces
    d, fr = truss.main_func()

    out = truss.gen_output(d, fr)
    writeFile(out, sys.argv[2])