Routhian.scm 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430
  1. #| -*-Scheme-*-
  2. Copyright (C) 1986, 1987, 1988, 1989, 1990, 1991, 1992, 1993, 1994,
  3. 1995, 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005,
  4. 2006, 2007, 2008, 2009, 2010, 2011, 2012, 2013 Massachusetts
  5. Institute of Technology
  6. This file is part of MIT/GNU Scheme.
  7. MIT/GNU Scheme is free software; you can redistribute it and/or modify
  8. it under the terms of the GNU General Public License as published by
  9. the Free Software Foundation; either version 2 of the License, or (at
  10. your option) any later version.
  11. MIT/GNU Scheme is distributed in the hope that it will be useful, but
  12. WITHOUT ANY WARRANTY; without even the implied warranty of
  13. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. General Public License for more details.
  15. You should have received a copy of the GNU General Public License
  16. along with MIT/GNU Scheme; if not, write to the Free Software
  17. Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301,
  18. USA.
  19. |#
  20. #|
  21. SICM p.218...
  22. Routhian equations of motion
  23. Assume a Lagrangian of the form L(t; x, y; vx, vy),
  24. where x and y may have substructure.
  25. We perform a Legendre transform on vy to get the Routhian.
  26. The equations of motion are Hamilton's equations for the py, y and
  27. Lagrange's equations for vx, x.
  28. |#
  29. (define* ((Lagrangian->Routhian Lagrangian) R-state)
  30. (let ((t (time R-state))
  31. (q (coordinate R-state))
  32. (vp (ref R-state 2)))
  33. (let ((x (ref q 0))
  34. (y (ref q 1))
  35. (vx (ref vp 0))
  36. (py (ref vp 1)))
  37. (define (L vy)
  38. (Lagrangian (up t q (up vx vy))))
  39. ((Legendre-transform-procedure L) py))))
  40. (define* ((Routh-equations Routhian) x y py)
  41. (lambda (t)
  42. (define (L s)
  43. (let ((tau (time s))
  44. (q (coordinate s))
  45. (v (velocity s)))
  46. (Routhian (up tau (up q (y tau)) (up v (py tau))))))
  47. (define (H s)
  48. (let ((tau (time s))
  49. (q (coordinate s))
  50. (p (momentum s)))
  51. (Routhian (up tau (up (x tau) q) (up ((D x) tau) p)))))
  52. (up
  53. (((Lagrange-equations L) x) t)
  54. (((Hamilton-equations H) y py) t)
  55. )))
  56. (define* ((Routh-equations-bad Routhian) x y py)
  57. (lambda (t)
  58. (define (L s)
  59. (let ((tau (time s))
  60. (q (coordinate s))
  61. (v (velocity s)))
  62. (Routhian (up tau (up q (y t)) (up v (py t))))))
  63. (define (H s)
  64. (let ((tau (time s))
  65. (q (coordinate s))
  66. (p (momentum s)))
  67. (Routhian (up tau (up (x t) q) (up ((D x) t) p)))))
  68. (up
  69. (((Lagrange-equations L) x) t)
  70. (((Hamilton-equations H) y py) t)
  71. )))
  72. #|
  73. (define ((Lag mx kx my ky) s)
  74. (let ((t (time s))
  75. (q (coordinate s))
  76. (v (velocity s)))
  77. (let ((x (ref q 0))
  78. (y (ref q 1))
  79. (vx (ref v 0))
  80. (vy (ref v 1)))
  81. (- (+ (* 1/2 mx (square vx))
  82. (* 1/2 my (square vy)))
  83. (+ (* 1/2 kx (square x))
  84. (* 1/2 ky (square y))
  85. (* x y y))))))
  86. (pe ((Lagrangian->Routhian (Lag 'mx 'kx 'my 'ky))
  87. (up 't (up 'x 'y) (up 'vx 'py))))
  88. (+ (* 1/2 kx (expt x 2))
  89. (* 1/2 ky (expt y 2))
  90. (* -1/2 mx (expt vx 2))
  91. (* x (expt y 2))
  92. (/ (* 1/2 (expt py 2)) my))
  93. ; ok
  94. (pe (((Routh-equations
  95. (Lagrangian->Routhian (Lag 'mx 'kx 'my 'ky)))
  96. (literal-function 'x)
  97. (literal-function 'y)
  98. (literal-function 'py))
  99. 't))
  100. (up
  101. (+ (* -1 kx (x t)) (* -1 mx (((expt D 2) x) t)) (* -1 (expt (y t) 2)))
  102. (up 0
  103. (+ ((D y) t) (/ (* -1 (py t)) my))
  104. (+ (* ky (y t)) (* 2 (y t) (x t)) ((D py) t))))
  105. ;looks good
  106. (define ((Lag2 m k) s)
  107. (let ((t (time s))
  108. (q (coordinate s))
  109. (v (velocity s)))
  110. (let ((x (ref q 0))
  111. (y (ref q 1))
  112. (vx (ref v 0))
  113. (vy (ref v 1)))
  114. (- (+ (* 1/2 m (square vx))
  115. (* 1/2 m (square vy)))
  116. (+ (* 1/2 k (square x))
  117. (* 1/2 k (square y)))))))
  118. (pe (((Routh-equations
  119. (Lagrangian->Routhian (Lag2 'm 'k)))
  120. (up (literal-function 'x0) (literal-function 'x1))
  121. (up (literal-function 'y0) (literal-function 'y1))
  122. (down (literal-function 'py1) (literal-function 'py1)))
  123. 't))
  124. (up
  125. (down (+ (* -1 k (x0 t)) (* -1 m (((expt D 2) x0) t)))
  126. (+ (* -1 k (x1 t)) (* -1 m (((expt D 2) x1) t))))
  127. (up
  128. 0
  129. (up (+ ((D y0) t) (/ (* -1 (py1 t)) m))
  130. (+ ((D y1) t) (/ (* -1 (py1 t)) m)))
  131. (down (+ (* k (y0 t)) ((D py1) t))
  132. (+ (* k (y1 t)) ((D py1) t)))))
  133. ;;; good
  134. |#
  135. (define* ((Routhian->acceleration-bad R) s)
  136. (let ((P ((partial 2 0) R))
  137. (F ((partial 1 0) R)))
  138. (* (s:inverse (ref s 2 0) (((partial 2 0) P) s) (ref s 2 0))
  139. ((- F
  140. (+ ((partial 0) P)
  141. (* ((partial 1 0) P) (component 2 0)))) s))))
  142. #| good except for adding dissipation function
  143. (define ((Routhian->acceleration R) s)
  144. (let ((P ((partial 2 0) R))
  145. (F ((partial 1 0) R)))
  146. (* (s:inverse (ref s 2 0) (((partial 2 0) P) s) (ref s 2 0))
  147. ((- F
  148. (+ ((partial 0) P)
  149. (* ((partial 1 0) P) (component 2 0))
  150. (* ((partial 1 1) P) ((partial 2 1) R))
  151. (* -1 ((partial 2 1) P) ((partial 1 1) R))
  152. ))
  153. s))))
  154. |#
  155. (define* (Routhian->acceleration R #:optional dissipation-function)
  156. (lambda (s)
  157. (if (default-object? dissipation-function)
  158. (let ((minus-P ((partial 2 0) R))
  159. (minus-F (((partial 1 0) R) s))
  160. (vy (((partial 2 1) R) s))
  161. (pyd ((* -1 ((partial 1 1) R)) s)))
  162. (* (s:inverse (ref s 2 0) (((partial 2 0) minus-P) s) (ref s 2 0))
  163. (- minus-F
  164. (+ (((partial 0) minus-P) s)
  165. (* (((partial 1 0) minus-P) s) ((component 2 0) s))
  166. (* (((partial 1 1) minus-P) s) vy)
  167. (* (((partial 2 1) minus-P) s) pyd)
  168. ))))
  169. (let ((minus-P ((partial 2 0) R))
  170. (minus-F (((partial 1 0) R) s))
  171. (vy (((partial 2 1) R) s)))
  172. (let ((minus-F0 (((partial 2 0) dissipation-function)
  173. (up (time s)
  174. (coordinate s)
  175. (up (ref s 2 0) vy))))
  176. (minus-F1 (((partial 2 1) dissipation-function)
  177. (up (time s)
  178. (coordinate s)
  179. (up (ref s 2 0) vy)))))
  180. (let ((pyd (- ((* -1 ((partial 1 1) R)) s) minus-F1)))
  181. (* (s:inverse (ref s 2 0) (((partial 2 0) minus-P) s) (ref s 2 0))
  182. (+ (- minus-F
  183. (+ (((partial 0) minus-P) s)
  184. (* (((partial 1 0) minus-P) s) ((component 2 0) s))
  185. (* (((partial 1 1) minus-P) s) vy)
  186. (* (((partial 2 1) minus-P) s) pyd)
  187. ))
  188. minus-F0))))))))
  189. #|
  190. (pe ((Routhian->acceleration
  191. (Lagrangian->Routhian (Lag2 'm 'k)))
  192. (up 't
  193. (up (up 'x0 'x1) (up 'y0 'y1))
  194. (up (up 'vx0 'vx1) (down 'py0 'py1)))))
  195. (up (/ (* -1 k x0) m) (/ (* -1 k x1) m))
  196. ; good
  197. |#
  198. #| good except for dissipation function
  199. (define (Routhian->state-derivative R)
  200. (let ((acc (Routhian->acceleration R)))
  201. (lambda (s)
  202. (up 1
  203. (up
  204. (ref s 2 0)
  205. (((partial 2 1) R) s))
  206. (up
  207. (acc s)
  208. (- (((partial 1 1) R) s)))))))
  209. ;;; good except for redundant calculation of pyd
  210. (define* (Routhian->state-derivative R #:optional dissipation-function)
  211. (if (default-object? dissipation-function)
  212. (let ((acc (Routhian->acceleration R)))
  213. (lambda (s)
  214. (up 1
  215. (up
  216. (ref s 2 0)
  217. (((partial 2 1) R) s))
  218. (up
  219. (acc s)
  220. (- (((partial 1 1) R) s))))))
  221. (let ((acc (Routhian->acceleration R dissipation-function)))
  222. (lambda (s)
  223. (up 1
  224. (up
  225. (ref s 2 0)
  226. (((partial 2 1) R) s))
  227. (up
  228. (acc s)
  229. (- (- (((partial 1 1) R) s))
  230. (((partial 2 1) dissipation-function)
  231. (up (time s)
  232. (coordinate s)
  233. (up (ref s 2 0) (((partial 2 1) R) s))))
  234. )))))))
  235. |#
  236. (define* (Routhian->state-derivative R #:optional dissipation-function)
  237. (lambda (s)
  238. (let ((minus-P ((partial 2 0) R))
  239. (minus-F (((partial 1 0) R) s))
  240. (vx (ref s 2 0))
  241. (vy (((partial 2 1) R) s)))
  242. (if (default-object? dissipation-function)
  243. (let ((pyd (- (((partial 1 1) R) s))))
  244. (up 1
  245. (up vx vy)
  246. (up
  247. (* (s:inverse vx (((partial 2 0) minus-P) s) vx)
  248. (- minus-F
  249. (+ (((partial 0) minus-P) s)
  250. (* (((partial 1 0) minus-P) s) vx)
  251. (* (((partial 1 1) minus-P) s) vy)
  252. (* (((partial 2 1) minus-P) s) pyd)
  253. )))
  254. pyd)))
  255. (let ((minus-F0 (((partial 2 0) dissipation-function)
  256. (up (time s) (coordinate s) (up vx vy))))
  257. (minus-F1 (((partial 2 1) dissipation-function)
  258. (up (time s) (coordinate s) (up vx vy)))))
  259. (let ((pyd (- ((* -1 ((partial 1 1) R)) s) minus-F1)))
  260. (up 1
  261. (up vx vy)
  262. (up
  263. (* (s:inverse vx (((partial 2 0) minus-P) s) vx)
  264. (+ (- minus-F
  265. (+ (((partial 0) minus-P) s)
  266. (* (((partial 1 0) minus-P) s) vx)
  267. (* (((partial 1 1) minus-P) s) vy)
  268. (* (((partial 2 1) minus-P) s) pyd)
  269. ))
  270. minus-F0))
  271. pyd))))))))
  272. #|
  273. (pe ((Routhian->state-derivative
  274. (Lagrangian->Routhian (Lag2 'm 'k)))
  275. (up 't
  276. (up (up 'x0 'x1) (up 'y0 'y1))
  277. (up (up 'vx0 'vx1) (down 'py0 'py1)))))
  278. (up
  279. 1
  280. (up (up vx0 vx1)
  281. (up (/ py0 m) (/ py1 m)))
  282. (up (up (/ (* -1 k x0) m) (/ (* -1 k x1) m))
  283. (down (* -1 k y0) (* -1 k y1))))
  284. |#
  285. #|
  286. a test of the dissipation function
  287. ;;; in Lagrangian variables
  288. (define ((diss2 delta0 delta1) s)
  289. (+ (* 1/2 delta0 (square (ref s 2 0)))
  290. (* 1/2 delta1 (square (ref s 2 1)))))
  291. (pe ((Routhian->state-derivative
  292. (Lagrangian->Routhian (Lag2 'm 'k))
  293. (diss2 'delta0 'delta1))
  294. (up 't
  295. (up (up 'x0 'x1) (up 'y0 'y1))
  296. (up (up 'vx0 'vx1) (down 'py0 'py1)))))
  297. (up
  298. 1
  299. (up (up vx0 vx1) (up (/ py0 m) (/ py1 m)))
  300. (up
  301. (up (+ (/ (* -1 delta0 vx0) m) (/ (* -1 k x0) m))
  302. (+ (/ (* -1 delta0 vx1) m) (/ (* -1 k x1) m)))
  303. (down (+ (* -1 k y0) (/ (* -1 delta1 py0) m))
  304. (+ (* -1 k y1) (/ (* -1 delta1 py1) m)))))
  305. ;ok
  306. |#
  307. (define* ((Lagrangian-state->Routhian-state L) s)
  308. (let ((t (time s))
  309. (q (coordinate s))
  310. (v (velocity s)))
  311. (let ((vx (ref v 0)))
  312. (let ((py (ref (((partial 2) L) s) 1)))
  313. (up t
  314. q
  315. (up vx py))))))
  316. (define* ((Routhian-state->Lagrangian-state R) s)
  317. (let ((t (time s))
  318. (q (coordinate s))
  319. (v (velocity s)))
  320. (let ((vx (ref v 0)))
  321. (let ((vy (ref (((partial 2) R) s) 1)))
  322. (up t
  323. q
  324. (up vx vy))))))
  325. #|;;; Two 2-dimensional particles
  326. (define ((L m1 m2 V) s)
  327. (let ((t (time s))
  328. (q (coordinate s))
  329. (v (velocity s)))
  330. (let ((xy1 (ref q 0))
  331. (xy2 (ref q 1))
  332. (v1 (ref v 0))
  333. (v2 (ref v 1)))
  334. (- (+ (* 1/2 m1 (square v1))
  335. (* 1/2 m2 (square v2)))
  336. (V xy1 xy2)))))
  337. (pe ((Lagrangian->Routhian
  338. (L 'm1 'm2
  339. (literal-function 'V
  340. (-> (X (UP Real Real) (UP Real Real)) Real))))
  341. (up 't
  342. (up (up 'x1 'y1) (up 'x2 'y2))
  343. (up (up 'v1x 'v1y) (down 'p2x 'p2y)))))
  344. (+ (* -1/2 m1 (expt v1x 2))
  345. (* -1/2 m1 (expt v1y 2))
  346. (V (up x1 y1) (up x2 y2))
  347. (/ (* 1/2 (expt p2x 2)) m2)
  348. (/ (* 1/2 (expt p2y 2)) m2))
  349. (pe (((Routh-equations
  350. (Lagrangian->Routhian
  351. (L 'm1 'm2
  352. (literal-function 'V
  353. (-> (X (UP Real Real) (UP Real Real)) Real)))))
  354. (up (literal-function 'x1) (literal-function 'y1))
  355. (up (literal-function 'x2) (literal-function 'y2))
  356. (down (literal-function 'p2x) (literal-function 'p2y)))
  357. 't))
  358. (up
  359. (down
  360. (+ (* -1 m1 (((expt D 2) x1) t))
  361. (* -1 (((partial 0 0) V) (up (x1 t) (y1 t)) (up (x2 t) (y2 t)))))
  362. (+ (* -1 m1 (((expt D 2) y1) t))
  363. (* -1 (((partial 0 1) V) (up (x1 t) (y1 t)) (up (x2 t) (y2 t))))))
  364. (up
  365. 0
  366. (up (+ ((D x2) t) (/ (* -1 (p2x t)) m2))
  367. (+ ((D y2) t) (/ (* -1 (p2y t)) m2)))
  368. (down
  369. (+ ((D p2x) t) (((partial 1 0) V) (up (x1 t) (y1 t)) (up (x2 t) (y2 t))))
  370. (+ ((D p2y) t) (((partial 1 1) V) (up (x1 t) (y1 t)) (up (x2 t) (y2 t)))))))
  371. |#