sections.scm 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337
  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. (declare (usual-integrations))
  21. ;;; Now to do the Poincare section.
  22. ;;; Map explorer:
  23. ;;; Left button starts a trajectory.
  24. ;;; Middle button continues a trajectory.
  25. ;;; Right button interrogates coordinates.
  26. ;;; FBE: we don't have 'plot-point' -> comment out.
  27. ;; (define* (explore-map window poincare-map #:optional mode-or-n)
  28. ;; (let* ((default-n 1000)
  29. ;; (collector
  30. ;; (cond ((default-object? mode-or-n)
  31. ;; (default-collector (default-monitor window)
  32. ;; poincare-map
  33. ;; default-n))
  34. ;; ((number? mode-or-n)
  35. ;; (default-collector (default-monitor window)
  36. ;; poincare-map
  37. ;; mode-or-n))
  38. ;; (else poincare-map))))
  39. ;; (define (button-loop ox oy)
  40. ;; (pointer-coordinates window
  41. ;; (lambda (x y button)
  42. ;; (case button
  43. ;; ((0)
  44. ;; (display "Started: ")
  45. ;; (write-line (list x y))
  46. ;; (collector x y button-loop map-failed))
  47. ;; ((1)
  48. ;; (if (eq? ox 'ignore)
  49. ;; (button-loop 'ignore oy)
  50. ;; (begin (display "Continued: ")
  51. ;; (write-line (list ox oy))
  52. ;; (collector ox oy button-loop map-failed))))
  53. ;; ((2)
  54. ;; (display "Hit: ")
  55. ;; (write-line (list x y))
  56. ;; (button-loop ox oy))))))
  57. ;; (define (map-failed)
  58. ;; (display "Illegal point \n")
  59. ;; (button-loop 'ignore 'ignore))
  60. ;; (newline)
  61. ;; (display "Left button starts a trajectory.")
  62. ;; (newline)
  63. ;; (display "Middle button continues a trajectory.")
  64. ;; (newline)
  65. ;; (display "Right button interrogates coordinates.")
  66. ;; (newline)
  67. ;; (button-loop 'ignore 'ignore)))
  68. (define* ((default-collector monitor pmap n) x y done fail)
  69. (let lp ((n n) (x x) (y y))
  70. (monitor x y)
  71. (if (fix:> n 0)
  72. (pmap x y
  73. (lambda (nx ny)
  74. (lp (fix:- n 1) nx ny))
  75. fail)
  76. (done x y))))
  77. ;;; FBE: we don't have 'plot-point' -> comment out.
  78. ;; (define* ((default-monitor win) x y)
  79. ;; (plot-point win x y))
  80. ;;; FBE: comment-out
  81. ;; (define (pointer-coordinates window continue)
  82. ;; (beep)
  83. ;; (get-pointer-coordinates window continue))
  84. #| ;;; Test for standard map
  85. (define win (frame 0.0 2pi 0.0 2pi))
  86. (explore-map win (standard-map 1.0))
  87. (explore-map win (standard-map 1.0) 5000)
  88. (graphics-clear win)
  89. (graphics-close win)
  90. |#
  91. #|
  92. ;;; This is used to zero in on crossings in autonomous systems,
  93. ;;; such as Henon-Heiles.
  94. (define (refine-crossing sec-eps advance state)
  95. (let lp ((state state))
  96. (let ((x (g:ref state 1 0))
  97. (xd (g:ref state 2 0)))
  98. (let ((zstate (advance state (- (/ x xd)))))
  99. (if (< (abs (g:ref zstate 1 0))
  100. sec-eps)
  101. zstate
  102. (lp zstate))))))
  103. |#
  104. ;;; For iterating maps of the shape used by explore-map.
  105. (define* ((iterated-map map n) x y continue fail)
  106. (if (fix:< n 0) (error "iterated-map: cannot invert map"))
  107. (let loop ((x x) (y y) (i n))
  108. (if (fix:= i 0)
  109. (continue x y)
  110. (map x y
  111. (lambda (nx ny)
  112. (loop nx ny (fix:- i 1)))
  113. fail))))
  114. ;;; FBE: 'plot-point' -> comment out
  115. ;; (define (display-map window poincare-map x y n)
  116. ;; (plot-point window x y)
  117. ;; (if (fix:> n 0)
  118. ;; (poincare-map
  119. ;; x y
  120. ;; (lambda (nx ny)
  121. ;; (display-map window poincare-map nx ny (fix:- n 1)))
  122. ;; (lambda ()
  123. ;; (newline)
  124. ;; (display "Illegal point: ")
  125. ;; (write (list x y))))))
  126. ;;; For example, first a simple problem.
  127. (define* ((standard-map K) x y continue fail)
  128. (let ((yp (flo:pv (flo:+ y (flo:* K (flo:sin x))))))
  129. (continue (flo:pv (flo:+ x yp)) yp)))
  130. (define* ((standard-map-inverse K) x y continue fail)
  131. (let ((xp (flo:pv (flo:- x y))))
  132. (continue xp (flo:pv (flo:- y (flo:* K (flo:sin xp)))))))
  133. ;;; This is the 0-2pi principal value:
  134. (define (flo:pv x)
  135. (flo:- x (flo:* 2pi (flo:floor (flo:/ x 2pi)))))
  136. #|
  137. (define ((T-pend m l g ys) local)
  138. (let ((t (time local))
  139. (theta (coordinate local))
  140. (thetadot (velocity local)))
  141. (let ((ysdot (D ys)))
  142. (* 1/2 m
  143. (+ (square (* l thetadot))
  144. (square (ysdot t))
  145. (* 2 (ysdot t) l (sin theta) thetadot))))))
  146. (define ((V-pend m l g ys) local)
  147. (let ((t (time local))
  148. (theta (coordinate local)))
  149. (* m g (- (ys t) (* l (cos theta))))))
  150. (define L-pend (- T-pend V-pend))
  151. (define ((periodic-drive amplitude frequency phase) t)
  152. (* amplitude (cos (+ (* frequency t) phase))))
  153. (define (L-periodically-driven-pendulum m l g a omega)
  154. (let ((ys (periodic-drive a omega 0)))
  155. (L-pend m l g ys)))
  156. (define (H-pend-sysder m l g a omega)
  157. (Hamiltonian->state-derivative
  158. (Lagrangian->Hamiltonian
  159. (L-periodically-driven-pendulum m l g a omega))))
  160. ;;; The driven pendulum map is very interesting.
  161. (set! *ode-integration-method* 'bulirsch-stoer)
  162. (define (driven-pendulum-map mass l g a omega)
  163. (let ((map-period (/ 2pi omega)))
  164. (lambda (theta p-theta continue fail)
  165. (let ((ns
  166. ((state-advancer H-pend-sysder
  167. mass l g a omega)
  168. (up ;state to start from
  169. 0.0 ;t0 = phase/drive-freq.
  170. theta
  171. p-theta)
  172. map-period
  173. 1.0e-10)))
  174. (continue
  175. ((principal-value pi) (vector-ref ns 1))
  176. (vector-ref ns 2))))))
  177. ;;; driven at twice the small-oscillation resonance.
  178. ;;; driven-pend-fig6.ps
  179. (define win (frame -pi pi -10.0 10.0))
  180. (let* ((m 1.0) ;m=1kg
  181. (l 1.0) ;l=1m
  182. (g 9.8) ;g=9.8m/s^2
  183. (small-amplitude-frequency (sqrt (/ g l)))
  184. (drive-amplitude 0.1) ;a=1/10 m
  185. (drive-frequency
  186. (* 2.0 small-amplitude-frequency)))
  187. (explore-map
  188. win
  189. (driven-pendulum-map m l g drive-amplitude drive-frequency)
  190. 1000))
  191. (graphics-close win)
  192. ;;; driven off resonance, at 4.2 times the natural frequency.
  193. ;;; driven-pend-fig7.ps
  194. (define win (frame -pi pi -20 20))
  195. (let* ((m 1.0) ;m=1kg
  196. (l 1.0) ;l=1m
  197. (g 9.8) ;g=9.8m/s^2
  198. (small-amplitude-frequency (sqrt (/ g l)))
  199. (drive-amplitude 0.05) ;a=1/20 m
  200. (drive-frequency
  201. (* 4.2 small-amplitude-frequency)))
  202. (explore-map
  203. win
  204. (driven-pendulum-map m l g drive-amplitude drive-frequency)
  205. 1000))
  206. (graphics-close win)
  207. ;;; Driven at high frequency, with larger amplitude.
  208. ;;; This shows the stable inverted pendulum island.
  209. ;;; driven-pend-fig8.ps
  210. (define win (frame -pi pi -20 20))
  211. (let* ((m 1.0) ;m=1kg
  212. (l 1.0) ;l=1m
  213. (g 9.8) ;g=9.8m/s^2
  214. (small-amplitude-frequency (sqrt (/ g l)))
  215. (drive-amplitude 0.2) ;a=1/20 m
  216. (drive-frequency
  217. (* 10.1 small-amplitude-frequency)))
  218. (explore-map
  219. win
  220. (driven-pendulum-map m l g drive-amplitude drive-frequency)
  221. 1000))
  222. (graphics-close win)
  223. |#
  224. #|
  225. ;;; An alternative, using construction and selection rather than
  226. ;;; continuations Here a map returns a new vector of x,y or an object
  227. ;;; describing the reason why it cannot.
  228. (define* (explore-map window poincare-map #:optional n)
  229. (define (iterate-map i x y)
  230. (if (fix:> i 0)
  231. (let ((nxy (poincare-map x y)))
  232. (if (vector? nxy)
  233. (begin (plot-point window x y)
  234. (iterate-map (fix:- i 1)
  235. (vector-ref nxy 0)
  236. (vector-ref nxy 1)))
  237. (begin (newline)
  238. (display "Illegal point: ")
  239. (write (list x y))
  240. (button-loop x y))))
  241. (button-loop x y)))
  242. (define (button-loop ox oy)
  243. (pointer-coordinates
  244. window
  245. (lambda (x y button)
  246. (case button
  247. ((0)
  248. (write-line (list x y))
  249. (display " started.")
  250. (iterate-map n x y))
  251. ((1)
  252. (write-line (list ox oy))
  253. (display " continued.")
  254. (iterate-map n ox oy))
  255. ((2)
  256. (write-line (list x y))
  257. (display " hit.")
  258. (button-loop ox oy))))))
  259. (if (default-object? n) (set! n 1000))
  260. (newline)
  261. (display "Left button starts a trajectory.")
  262. (newline)
  263. (display "Middle button continues a trajectory.")
  264. (newline)
  265. (display "Right button interrogates coordinates.")
  266. (button-loop 9. 9.))
  267. ;;; In this form the map is slightly different, but everything
  268. ;;; else is the same.
  269. (define ((standard-map K) x y)
  270. (let ((yp (flo:pv (flo:+ y (flo:* K (flo:sin x))))))
  271. (vector (flo:pv (flo:+ x yp)) yp)))
  272. (define (driven-pendulum-map mass l g a omega)
  273. (let ((map-period (/ 2pi omega)))
  274. (lambda (theta p-theta)
  275. (let ((ns
  276. ((state-advancer H-pend-sysder
  277. mass l g a omega)
  278. (up ;state to start from
  279. 0.0 ;t0 = phase/drive-freq.
  280. theta
  281. p-theta)
  282. map-period
  283. 1.0e-10)))
  284. (vector
  285. ((principal-value pi) (vector-ref ns 1))
  286. (vector-ref ns 2))))))
  287. |#