/
solvemech.py
executable file
·265 lines (227 loc) · 8.5 KB
/
solvemech.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
#!/usr/bin/env python3
from symmech import SymComputer
import sympy
from sympy import Symbol
import scipy
import scipy.integrate
import types
class SolveMech(object):
def __init__(
self,
lagrangian,
coordinates,
velocities,
t=Symbol("t"),
derivative_suffix="_dot",
velocity_suffix="_v",
):
"""
Takes a lagrangian, made from sympy variables. Coordinates and velocities
are lists of sympy variables used in the lagrangian. Make sure that coorinates[i]
matches velocities[i].
"""
self.symComputer = SymComputer(lagrangian, coordinates, velocities, t=t)
if derivative_suffix in velocity_suffix or velocity_suffix in derivative_suffix:
raise Exception(
"derivative_suffix and velocity_suffix can't be substrings of eachother"
)
self.derivative_suffix = derivative_suffix
self.velocity_suffix = velocity_suffix
self.diffeq = None
self.constValsList = None
self.allVarList = None
def __str__(self):
result = ""
result += "****SolveMech**** "
result += str(self.symComputer)
return result
def __call__(self, yarr, t):
# print "call: ",yarr,t
constVals = []
for p in self.constValsList:
try:
constVals.append(float(p))
except TypeError:
constVals.append(self.callConstFunc(p, yarr, t))
allvars = [t] + list(yarr) + constVals
result = scipy.zeros(len(yarr))
for i in range(len(yarr)):
result[i] = self.diffeq[i](*allvars)
return result
def solveEulerLegrange(self, times, initialVals, constantValueDict):
"""
Requires a list of time values to solve for, a list of initial values for
the coordinates concatenated with one for the velocities, and a dictionary of
values to assign to the constant variables (where the keys are sympy
variables).
returns a list of lists for each time point, where for each time
point is a list of values for each coord and each velocity in the
same order as in the initial values.
Order of initial values and results:
[coord1,coord2,...,vel_coord1,vel_coord2,...]
Make sure initial values are in the same order as the coords and velocities
fed to the contructor!
"""
eoms = self.symComputer.getEulerLegrangeEOMs()
print(eoms)
eomDict = self.rearangeForDerivs(eoms, secondOrder=True)
print(self.symComputer.tdm)
print(eomDict)
varList = []
for coord in self.symComputer.coordinates:
varList.append(coord)
for vel in self.symComputer.velocities:
varList.append(vel)
constList = list(constantValueDict.keys())
self.constValsList = [constantValueDict[x] for x in constList]
allVarList = [self.symComputer.t] + varList + constList
self.allVarList = allVarList
## create functions to be called with list [t]+[coord1,coord2,...]+[vel1,vel2,...]+[const1,const2,...]
exprList = []
for var in varList:
var_dot = self.symComputer.tdm[var]
if var_dot in eomDict:
exprList.append(eomDict[var_dot])
else:
exprList.append(var_dot)
print("vars:", varList)
print("diffexps:", exprList)
print("allVarList: ", allVarList)
self.diffeq = []
for expr in exprList:
self.diffeq.append(sympy.lambdify(allVarList, expr))
# print "about to call odeint!"
# print "with ", self, initialVals, times
result = scipy.integrate.odeint(self, initialVals, times)
# print result
self.diffeq = None
self.constValsList = None
self.allVarList = None
return result
def solveHamiltonian(self, times, initialVals, constantValueDict):
"""
Requires a list of time values to solve for, a list of initial values for
the coordinates concatenated with one for the momenta, and a dictionary of
values to assign to the constant variables (where the keys are sympy
variables).
returns a list of lists for each time point, where for each time
point is a list of values for each coord and each momenta in the
same order as in the initial values.
Order of initial values and results:
[coord1,coord2,...,momentum_coord1,momentum_coord2,...]
Make sure initial values are in the same order as the coords and velocities
fed to the contructor!
"""
eoms = self.symComputer.getHamiltonianEOMs()
eomDict = self.rearangeForDerivs(eoms)
varList = []
for coord in self.symComputer.coordinates:
varList.append(coord)
for mom in self.symComputer.momenta:
varList.append(mom)
constList = list(constantValueDict.keys())
self.constValsList = [constantValueDict[x] for x in constList]
allVarList = [self.symComputer.t] + varList + constList
self.allVarList = allVarList
## create functions to be called with list [t]+[coord1,coord2,...]+[mom1,mom2,...]+[const1,const2,...]
exprList = []
for var in varList:
var_dot = self.symComputer.tdm[var]
exprList.append(eomDict[var_dot])
self.diffeq = []
for expr in exprList:
self.diffeq.append(sympy.lambdify(allVarList, expr))
# print "about to call odeint!"
# print "with ", self, initialVals, times
result = scipy.integrate.odeint(self, initialVals, times)
# print result
self.diffeq = None
self.constValsList = None
self.allVarList = None
return result
def rearangeForDerivs(self, eoms, secondOrder=False):
suffix = self.derivative_suffix
if secondOrder:
suffix += suffix
derivSet = set()
for eom in eoms:
for x in sympy.preorder_traversal(eom):
if type(x) == sympy.Symbol and suffix in str(x):
derivSet.add(x)
derivList = list(derivSet)
if len(derivList) == 0:
raise Exception(
"No time derivative was found in eom: '{0}', where derivative has suffix of '{1}'".format(
eom, suffix
)
)
solns = sympy.solve(eoms, derivList)
if len(solns) < len(derivList):
raise Exception(
"Too few solutions, {2}, were found for '{1}'".format(
eom, derivList, solns
)
)
if len(solns) > len(derivList):
raise Exception(
"Too many solutions, {2}, were found for '{1}'".format(
eom, derivList, solns
)
)
for deriv in derivList:
try:
solns[deriv]
except KeyError:
raise Exception(
"Couldn't find solution for {1}, in solutions {2}, in EOMs '{0}'".format(
eom, deriv, solns
)
)
return solns
def callConstFunc(self, func, yarr, t):
allVarNameList = [str(i) for i in self.allVarList]
params = {}
for name, val in zip(allVarNameList, [t] + list(yarr) + self.constValsList):
try:
params[name] = float(val)
except TypeError:
pass
return func(params)
if __name__ == "__main__":
from matplotlib import pyplot as mpl
t = Symbol("t")
x = Symbol("x")
y = Symbol("y")
z = Symbol("z")
x_dot = Symbol("x_dot")
y_dot = Symbol("y_dot")
z_dot = Symbol("z_dot")
m = Symbol("m")
M = Symbol("M")
g = Symbol("g")
constValsDict = {
m: 20.0,
# M:0.,
g: 10.0,
# g: lambda d: 10.-10*d['t'],
}
times = scipy.linspace(0, 1, 20)
initialValsL = [0.0, 10.0]
initialValsH = [0.0, 10.0 * constValsDict[m]]
L = m * x_dot ** 2 / 2 - m * g * x
sm = SolveMech(L, [x], [x_dot])
timeSeriesH = sm.solveHamiltonian(times, initialValsH, constValsDict)
timeSeriesL = sm.solveEulerLegrange(times, initialValsL, constValsDict)
print(sm)
mpl.subplot(2, 1, 1)
mpl.plot(times, timeSeriesH[:, 0], "b-")
mpl.plot(times, timeSeriesL[:, 0], "ro")
mpl.ylabel("Position")
mpl
mpl.subplot(2, 1, 2)
mpl.plot(times, timeSeriesH[:, 1], "b-")
mpl.plot(times, timeSeriesL[:, 1], "ro")
mpl.xlabel("time (s)")
mpl.ylabel("Velocity/Momentum")
mpl
mpl.show()