def test_euler_interface(): x = Function('x') y = Symbol('y') t = Symbol('t') raises(TypeError, lambda: euler_equations()) raises(TypeError, lambda: euler_equations(x(t).diff(t)*y(t), [x(t), y])) raises(ValueError, lambda: euler_equations(x(t).diff(t)*x(y), [x(t), x(y)])) raises(TypeError, lambda: euler_equations(x(t).diff(t)**2, x(0)))
def test_euler_interface(): x = Function('x') y = Symbol('y') t = Symbol('t') raises(TypeError, lambda: euler_equations()) raises(TypeError, lambda: euler_equations(x(t).diff(t) * y(t), [x(t), y])) raises(ValueError, lambda: euler_equations(x(t).diff(t) * x(y), [x(t), x(y)])) raises(TypeError, lambda: euler_equations(x(t).diff(t)**2, x(0)))
def _calc(self): # DEPRECIATED self.L_multipliers = [ sp.Symbol("lambda_" + str(j) + "_mult") for j in range(len(self.constraints)) ] L_prime = self.L for j in range(len(self.constraints)): L_prime += self.L_multipliers[j] * self.constraints[j] eq = euler_equations(L_prime, self.coords, t) # extract euler lagrange equations eq += [sp.Eq(j.diff(t, 2), 0) for j in self.constraints] eq = [sp.simplify(j) for j in eq] eq = [sp.trigsimp(j) for j in eq] eq = [sp.factor(j) for j in eq] if False in eq: raise Exception("Lagrangian appears to be inconsistent") results = sp.linsolve( eq, [i.diff(t, 2) for i in self.coords] + self.L_multipliers ) # find solutions in terms of second derivatives # add validation / ability for non-linear systems result = [] # extract result if only one exists for num, expr in enumerate(results.args[0]): result.append( sp.Eq(([i.diff(t, 2) for i in self.coords] + self.L_multipliers)[num], sp.simplify(expr))) self.motion = result
def test_euler_sineg(): psi = Function('psi') t = Symbol('t') x = Symbol('x') L = (psi(t, x).diff(t))**2/2 - (psi(t, x).diff(x))**2/2 + cos(psi(t, x)) assert euler_equations(L, psi(t, x), [t, x]) == \ set([Eq(-sin(psi(t, x)) - Derivative(psi(t, x), t, t) + \ Derivative(psi(t, x), x, x), 0)])
def test_euler_sineg(): psi = Function('psi') t = Symbol('t') x = Symbol('x') L = (psi(t, x).diff(t))**2 / 2 - (psi(t, x).diff(x))**2 / 2 + cos(psi( t, x)) assert euler_equations(L, psi(t, x), [t, x]) == \ set([Eq(-sin(psi(t, x)) - Derivative(psi(t, x), t, t) + \ Derivative(psi(t, x), x, x), 0)])
def test_euler_henonheiles(): x = Function('x') y = Function('y') t = Symbol('t') L = sum((z(t).diff(t))**2 / 2 - z(t)**2 / 2 for z in [x, y]) L += -x(t)**2 * y(t) + y(t)**3 / 3 assert euler_equations(L, [x(t), y(t)], t) == \ set([Eq(-x(t)**2 + y(t)**2 - y(t) - Derivative(y(t), t, t), 0), Eq(-2*x(t)*y(t) - x(t) - Derivative(x(t), t, t), 0)])
def test_euler_henonheiles(): x = Function('x') y = Function('y') t = Symbol('t') L = sum((z(t).diff(t))**2/2 - z(t)**2/2 for z in [x, y]) L += -x(t)**2*y(t) + y(t)**3/3 assert euler_equations(L, [x(t), y(t)], t) == \ set([Eq(-x(t)**2 + y(t)**2 - y(t) - Derivative(y(t), t, t), 0), Eq(-2*x(t)*y(t) - x(t) - Derivative(x(t), t, t), 0)])
def test_euler_high_order(): # an example from hep-th/0309038 m = Symbol('m') k = Symbol('k') x = Function('x') y = Function('y') t = Symbol('t') L = m*Derivative(x(t), t)**2/2 + m*Derivative(y(t), t)**2/2 - \ k*Derivative(x(t), t)*Derivative(y(t), t, t) + \ k*Derivative(y(t), t)*Derivative(x(t), t, t) assert euler_equations(L, [x(t), y(t)]) == \ set([Eq(-2*k*Derivative(x(t), t, t, t) - \ m*Derivative(y(t), t, t), 0), Eq(2*k*Derivative(y(t), t, t, t) - \ m*Derivative(x(t), t, t), 0)]) w = Symbol('w') L = x(t, w).diff(t, w)**2 / 2 assert euler_equations(L) == \ set([Eq(Derivative(x(t, w), t, t, w, w), 0)])
def test_euler_high_order(): # an example from hep-th/0309038 m = Symbol('m') k = Symbol('k') x = Function('x') y = Function('y') t = Symbol('t') L = m*Derivative(x(t), t)**2/2 + m*Derivative(y(t), t)**2/2 - \ k*Derivative(x(t), t)*Derivative(y(t), t, t) + \ k*Derivative(y(t), t)*Derivative(x(t), t, t) assert euler_equations(L, [x(t), y(t)]) == \ set([Eq(-2*k*Derivative(x(t), t, t, t) - \ m*Derivative(y(t), t, t), 0), Eq(2*k*Derivative(y(t), t, t, t) - \ m*Derivative(x(t), t, t), 0)]) w = Symbol('w') L = x(t, w).diff(t, w)**2/2 assert euler_equations(L) == \ set([Eq(Derivative(x(t, w), t, t, w, w), 0)])
def _calcLU(self, diagnostic, diag_data): #create enough lagrange multipliers for constraints self.L_multipliers = [ sp.Symbol("lambda_" + str(j) + "_mult") for j in range(len(self.constraints)) ] L_prime = self.L # stores lagrangian with constraints for j in range(len(self.constraints)): L_prime += self.L_multipliers[j] * self.constraints[j] eq = euler_equations(L_prime, self.coords, t) # extract euler lagrange equations eq += [sp.Eq(j.diff(t, 2), 0) for j in self.constraints ] # add constraint equations in second t deriv. form if diagnostic: diag_data.append(perf_counter() - self.start) print("Euler-Lagrange equations calculated: " + str(diag_data[-1])) eq = [sp.simplify(j) for j in eq] # simplify equations eq = [sp.trigsimp(j) for j in eq] eq = [sp.factor(j) for j in eq] if diagnostic: diag_data.append(perf_counter() - self.start) print("Euler-Lagrange equations simplified: " + str(diag_data[-1])) if False in eq: raise Exception("Lagrangian appears to be inconsistent") placeholders = [] seconds = [] self.coords1 = copy.copy(self.coords) # set up list of coords self.motion1 = [] for j in self.coords: # convert derivatives into placeholder variables new = sp.symbols(j.name + "_temp", real=True) new_o = dynamicsymbols("omega_" + j.name) self.coords1.append(new_o) placeholders += [(j.diff(t, 2), new), (j.diff(t), new_o)] seconds.append(new) eq = self.subConstants(placeholders, eq) if diagnostic: diag_data.append(perf_counter() - self.start) print("derivatives substituted: " + str(diag_data[-1])) matrixA, matrixB = sp.linear_eq_to_matrix( eq, seconds + self.L_multipliers) # set up equations as a matrix equation if diagnostic: diag_data.append(perf_counter() - self.start) print("Matrix equation created: " + str(diag_data[-1])) solution = matrixA.LUsolve(matrixB) # solve matrix equation if diagnostic: diag_data.append(perf_counter() - self.start) print("matrix equation solved: " + str(diag_data[-1])) # add validation / ability for non-linear systems result = [] for num, expr in enumerate(solution[:]): # recreate equations result.append( sp.Eq( ([ self.coords1[len(self.coords) + j].diff(t) for j in range(len(self.coords)) ] + self.L_multipliers)[num], expr)) # simplify(expr) breaks for double conical??? TODO self.motion = result self.motion1 = [ sp.Eq(self.coords[j].diff(t), self.coords1[len(self.coords) + j]) for j in range(len(self.coords)) ] + self.motion if diagnostic: diag_data.append(perf_counter() - self.start) print("results appended and derivatives reapplied: " + str(diag_data[-1]))
def test_euler_pendulum(): x = Function('x') t = Symbol('t') L = (x(t).diff(t))**2 / 2 + cos(x(t)) assert euler_equations(L, x(t), t) == \ set([Eq(-sin(x(t)) - Derivative(x(t), t, t), 0)])
def test_euler_pendulum(): x = Function('x') t = Symbol('t') L = (x(t).diff(t))**2/2 + cos(x(t)) assert euler_equations(L, x(t), t) == \ set([Eq(-sin(x(t)) - Derivative(x(t), t, t), 0)])