def test_SortSomeVariables(): x = Variable("x") y = Variable("y") p = Variable("p") assert VariableList([x, y]) == VariableList([y, x]) assert VariableList([x, p]) == VariableList([p, x]) assert VariableList([y, p]) == VariableList([p, y])
def test_KineticEnergy_Random(): Ntests = 1 # 10 x = Variable("x") y = Variable("y") for i in range(Ntests): mass = np.random.rand() vector3D = np.zeros(3, dtype="object") vector3D += x.dt * np.random.rand(3) vector3D += y.dt * np.random.rand(3) E = KineticEnergy(mass, vector3D) assert E == mass * np.dot(vector3D, vector3D) / 2
def test_SortAllVariables(): x = Variable("x") y = Variable("y") p = Variable("p") listgood = VariableList([x, y, p]) assert VariableList([x, y, p]) == listgood assert VariableList([x, p, y]) == listgood assert VariableList([y, x, p]) == listgood assert VariableList([y, p, x]) == listgood assert VariableList([p, x, y]) == listgood assert VariableList([p, y, x]) == listgood
def test_SetMatrixDim5_Random(): Ntests = 1 # 2 dim = 5 X = [] dX = [] for i in range(dim): xi = Variable("x%d" % i) X.append(xi) dX.append(xi.dt) X = np.array(X) dX = np.array(dX) for i in range(Ntests): M = randomSymmetricMatrix(dim) V = np.random.rand(dim, dim) K = randomSymmetricMatrix(dim) A = np.random.rand(dim) B = np.random.rand(dim) C = np.random.rand() totalenergy = C totalenergy += np.dot(dX, np.dot(M, dX)) / 2 totalenergy += np.dot(dX, np.dot(V, X)) totalenergy += np.dot(X, np.dot(K, X)) / 2 totalenergy += np.dot(A, dX) totalenergy += np.dot(B, X) E = Energy.frommatrix(X=X, M=M, V=V, K=K, A=A, B=B, C=C) assert E == totalenergy
def test_QuadPosition(): Ntests = 1 # 10 x = Variable("x") for i in range(Ntests): a = np.random.rand() E = Energy(a * x**2) assert E == a * x**2
def test_VelocityPoint(): x = Variable("x") R0 = FrameReference() R1 = FrameReference(R0, translation=(x, 0, 0)) point = Object(R1) np.testing.assert_almost_equal(point.get(R1, "v"), (0, 0, 0)) np.testing.assert_array_equal(point.get(R0, "v"), (x.dt, 0, 0))
def test_CompareWithMatrixDim5_Random(): Ntests = 1 # 2 dim = 5 X = [] dX = [] for i in range(dim): xi = Variable("x%d" % i) X.append(xi) dX.append(xi.dt) X = np.array(X) dX = np.array(dX) for i in range(Ntests): M = randomSymmetricMatrix(dim) V = np.random.rand(dim, dim) K = randomSymmetricMatrix(dim) A = np.random.rand(dim) B = np.random.rand(dim) C = np.random.rand() totalenergy = C totalenergy += np.dot(dX, np.dot(M, dX)) / 2 totalenergy += np.dot(dX, np.dot(V, X)) totalenergy += np.dot(X, np.dot(K, X)) / 2 totalenergy += np.dot(A, dX) totalenergy += np.dot(B, X) print("z") E = Energy(totalenergy) print("k") assert E == totalenergy
def test_QuadSpeed(): Ntests = 1 # 10 x = Variable("x") dx = x.dt for i in range(Ntests): a = np.random.rand() E = Energy(a * dx**2) assert E == a * dx**2
def test_KineticEnergy_Standard(): x = Variable("x") dx = x.dt mass = 1 vector3D = (dx, 0, 0) E_good = mass * (dx**2) / 2 E_test = KineticEnergy(mass, vector3D) E_good = Energy(E_good) assert E_test == E_good
def test_Linear(): Ntests = 1 # 10 x = Variable("x") dx = x.dt for i in range(Ntests): a, b, c = np.random.rand(3) totalenergy = c totalenergy += b * x totalenergy += a * dx E = Energy(totalenergy) assert E == totalenergy
def test_CompareWithMatrixDim2_Random(): Ntests = 1 # 10 x = Variable("x") y = Variable("y") X = np.array([x, y]) dX = np.array([x.dt, y.dt]) for i in range(Ntests): M = randomSymmetricMatrix(2) V = np.random.rand(2, 2) K = randomSymmetricMatrix(2) A = np.random.rand(2) B = np.random.rand(2) C = np.random.rand() totalenergy = C totalenergy += np.dot(dX, np.dot(M, dX)) / 2 totalenergy += np.dot(dX, np.dot(V, X)) totalenergy += np.dot(X, np.dot(K, X)) / 2 totalenergy += np.dot(A, dX) totalenergy += np.dot(B, X) E = Energy(totalenergy) assert E == totalenergy
def test_CompareWithMatrixDim2_Standard(): x = Variable("x") y = Variable("y") dx = x.dt dy = y.dt X = (x, y) M = np.array([[14, 2], [2, 12]]) V = np.array([[4, 6], [3, 5]]) K = np.array([[-6, -3], [-3, -10]]) A = np.array([1, -7]) B = np.array([-5, 9]) C = 80 ener = 7*(dx**2) + 2*dx*dy + 6*(dy**2) + \ 4*dx*x + 6*dx*y + 3*dy*x + 5*dy*y + \ (-3)*x**2 + (-3)*x*y + (-5)*y**2 + \ 1*dx + (-7)*dy + (-5)*x + 9*y + 80 assert Energy(ener) == ener
def test_SetMatrixDim2_Random(): Ntests = 1 # 2 x = Variable("x") y = Variable("y") X = np.array([x, y]) dX = np.array([x.dt, y.dt]) for i in range(Ntests): M = randomSymmetricMatrix(2) V = np.random.rand(2, 2) K = randomSymmetricMatrix(2) A = np.random.rand(2) B = np.random.rand(2) C = np.random.rand() totalenergy = C totalenergy += np.dot(dX, np.dot(M, dX)) / 2 totalenergy += np.dot(dX, np.dot(V, X)) totalenergy += np.dot(X, np.dot(K, X)) / 2 totalenergy += np.dot(A, dX) totalenergy += np.dot(B, X) E = Energy.frommatrix(X=X, M=M, V=V, K=K, A=A, B=B, C=C) assert E == totalenergy
def test_SetMatrixDim2_Standard(): x = Variable("x") y = Variable("y") dx = x.dt dy = y.dt X = (x, y) M = np.array([[14, 2], [2, 12]]) V = np.array([[4, 6], [3, 5]]) K = np.array([[-6, -3], [-3, -10]]) A = np.array([1, -7]) B = np.array([-5, 9]) C = 80 ener = 7*(dx**2) + 2*dx*dy + 6*(dy**2) + \ 4*dx*x + 6*dx*y + 3*dy*x + 5*dy*y + \ (-3)*x**2 + (-3)*x*y + (-5)*y**2 + \ 1*dx + (-7)*dy + (-5)*x + 9*y + 80 E = Energy.frommatrix(X=X, M=M, V=V, K=K, A=A, B=B, C=C) ener = Energy(ener) assert E == ener
def test_AllQuantity(): Ntests = 1 # 10 x = Variable("x") dx = x.dt for i in range(Ntests): m, v, k, a, b, c = np.random.rand(6) totalenergy = c totalenergy += a * dx totalenergy += b * x totalenergy += k * x**2 / 2 totalenergy += v * x * dx totalenergy += m * dx**2 / 2 E = Energy(totalenergy) assert E == totalenergy
def test_KineticEnergy(): m = 1 x = Variable("x") R0 = FrameReference() R1 = FrameReference(R0, translation=(x, 0, 0)) point = Object(R1) point.mass = m v = point.get(R1, "v") E = point.KineticEnergy(R1) assert E == m * np.dot(v, v) / 2 v = point.get(R0, "v") E = point.KineticEnergy(R0) assert E == m * np.dot(v, v) / 2
def test_Comparation(): x = Variable("x") y = Variable("y") assert x == x assert x != y
def test_GetDerivative(): x = Variable("x") dx = x.dt
def test_RebuildSameVariable(): x = Variable("x") newx = Variable("x") assert x is newx
def test_BuildTwoVariables(): x = Variable("x") y = Variable("y")
def test_Build(): x = Variable("x")