vector-calculus.scm 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269
  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. ;;;; Traditional vector calculus operators
  21. (define (gradient metric basis)
  22. (compose (raise metric basis) d))
  23. (define (curl metric orthonormal-basis)
  24. (let ((star (Hodge-star metric orthonormal-basis))
  25. (sharp (raise metric orthonormal-basis))
  26. (flat (lower metric)))
  27. (compose sharp star d flat)))
  28. (define (divergence metric orthonormal-basis)
  29. (let ((star (Hodge-star metric orthonormal-basis))
  30. (flat (lower metric)))
  31. (compose star d star flat)))
  32. (define (Laplacian metric orthonormal-basis)
  33. (compose (divergence metric orthonormal-basis)
  34. (gradient metric orthonormal-basis)))
  35. #|
  36. ;;; Alternative divergence
  37. (define (((divergence1 Cartan) v) point)
  38. (let ((basis (Cartan->basis Cartan))
  39. (nabla (covariant-derivative Cartan)))
  40. (contract
  41. (lambda (ei wi)
  42. ((wi ((nabla ei) v)) point))
  43. basis)))
  44. |#
  45. ;;; Testing these requires orthonormal bases
  46. (define (coordinate-system->Lame-coefficients coordinate-system)
  47. (let ((gij (coordinate-system->metric-components coordinate-system)))
  48. ; (assert (diagonal? gij))
  49. (s:generate (coordinate-system 'dimension) 'down
  50. (lambda (i)
  51. (sqrt (ref gij i i))))))
  52. (define (coordinate-system->orthonormal-vector-basis coordsys)
  53. (s:generate (coordsys 'dimension) 'down
  54. (lambda (i)
  55. (* (ref (coordinate-system->vector-basis coordsys) i)
  56. (/ 1
  57. (compose
  58. (ref (coordinate-system->Lame-coefficients coordsys) i)
  59. (chart coordsys)))))))
  60. #|
  61. ;;; Test setup for spherical system
  62. (define spherical R3-rect)
  63. (define-coordinates (up r theta phi) spherical)
  64. (define spherical-point
  65. ((point spherical) (up 'r 'theta 'phi)))
  66. (define spherical-basis
  67. (coordinate-system->basis spherical))
  68. (define (spherical-metric v1 v2)
  69. (+ (* (dr v1) (dr v2))
  70. (* (square r)
  71. (+ (* (dtheta v1) (dtheta v2))
  72. (* (expt (sin theta) 2)
  73. (dphi v1) (dphi v2))))))
  74. (define spherical-Gamma
  75. (make-Christoffel
  76. (let ((O (lambda x 0)))
  77. (down
  78. (down (up O O O)
  79. (up O (/ 1 r) O)
  80. (up O O (/ 1 r)))
  81. (down (up O (/ 1 r) O)
  82. (up (* -1 r) O O)
  83. (up O O (/ (cos theta) (sin theta))))
  84. (down (up O O (/ 1 r))
  85. (up O O (/ (cos theta) (sin theta)))
  86. (up (* -1 r (expt (sin theta) 2))
  87. (* -1 (sin theta) (cos theta))
  88. O))))
  89. (coordinate-system->basis spherical)))
  90. (define spherical-Cartan
  91. (Christoffel->Cartan spherical-Gamma))
  92. ;;; normalized spherical basis
  93. (define e_0 d/dr)
  94. (define e_1 (* (/ 1 r) d/dtheta))
  95. (define e_2 (* (/ 1 (* r (sin theta))) d/dphi))
  96. ;;; ((spherical-metric e_0 e_0) spherical-point)
  97. ;;; ((spherical-metric e_1 e_1) spherical-point)
  98. ;;; ((spherical-metric e_2 e_2) spherical-point)
  99. ;;; all 1
  100. ;;; ((spherical-metric e_0 e_1) spherical-point)
  101. ;;; ((spherical-metric e_0 e_2) spherical-point)
  102. ;;; ((spherical-metric e_1 e_2) spherical-point)
  103. ;;; all 0
  104. (define orthonormal-spherical-vector-basis
  105. (down e_0 e_1 e_2))
  106. (define orthonormal-spherical-1form-basis
  107. (vector-basis->dual orthonormal-spherical-vector-basis
  108. spherical))
  109. (define orthonormal-spherical-basis
  110. (make-basis orthonormal-spherical-vector-basis
  111. orthonormal-spherical-1form-basis))
  112. (define v
  113. (+ (* (literal-manifold-function 'v^0 spherical) e_0)
  114. (* (literal-manifold-function 'v^1 spherical) e_1)
  115. (* (literal-manifold-function 'v^2 spherical) e_2)))
  116. ;;; Test of Gradient
  117. ((orthonormal-spherical-1form-basis
  118. ((gradient spherical-metric spherical-basis)
  119. (literal-manifold-function 'f spherical)))
  120. spherical-point)
  121. #|
  122. (up (((partial 0) f) (up r theta phi))
  123. (/ (((partial 1) f) (up r theta phi)) r)
  124. (/ (((partial 2) f) (up r theta phi)) (* r (sin theta))))
  125. |#
  126. ;;; Test of Curl
  127. ((orthonormal-spherical-1form-basis
  128. ((curl spherical-metric orthonormal-spherical-basis) v))
  129. spherical-point)
  130. #|
  131. (up
  132. (/ (+ (* (sin theta) (((partial 1) v^2) (up r theta phi)))
  133. (* (cos theta) (v^2 (up r theta phi)))
  134. (* -1 (((partial 2) v^1) (up r theta phi))))
  135. (* r (sin theta)))
  136. (/ (+ (* -1 r (sin theta) (((partial 0) v^2) (up r theta phi)))
  137. (* -1 (sin theta) (v^2 (up r theta phi)))
  138. (((partial 2) v^0) (up r theta phi)))
  139. (* r (sin theta)))
  140. (/ (+ (* r (((partial 0) v^1) (up r theta phi)))
  141. (v^1 (up r theta phi))
  142. (* -1 (((partial 1) v^0) (up r theta phi))))
  143. r))
  144. |#
  145. ;;; Test of Divergence
  146. (((divergence spherical-metric orthonormal-spherical-basis) v)
  147. spherical-point)
  148. #|
  149. (+ (((partial 0) v^0) (up r theta phi))
  150. (/ (* 2 (v^0 (up r theta phi))) r)
  151. (/ (((partial 1) v^1) (up r theta phi)) r)
  152. (/ (* (v^1 (up r theta phi)) (cos theta)) (* r (sin theta)))
  153. (/ (((partial 2) v^2) (up r theta phi)) (* r (sin theta))))
  154. |#
  155. (define phi
  156. (literal-manifold-function 'phi spherical))
  157. #| phi |#
  158. (((Laplacian spherical-metric orthonormal-spherical-basis)
  159. phi)
  160. spherical-point)
  161. #|
  162. (+ (((partial 0) ((partial 0) phi)) (up r theta phi))
  163. (/ (* 2 (((partial 0) phi) (up r theta phi)))
  164. r)
  165. (/ (((partial 1) ((partial 1) phi)) (up r theta phi))
  166. (expt r 2))
  167. (/ (* (cos theta) (((partial 1) phi) (up r theta phi)))
  168. (* (expt r 2) (sin theta)))
  169. (/ (((partial 2) ((partial 2) phi)) (up r theta phi))
  170. (* (expt r 2) (expt (sin theta) 2))))
  171. |#
  172. ;;; Obtaining the wave equation.
  173. (define SR R4-rect)
  174. (define-coordinates (up t x y z) SR)
  175. (define an-event ((point SR) (up 't0 'x0 'y0 'z0)))
  176. (define c 'c) ; We like units.
  177. (define (g-Minkowski u v)
  178. (+ (* -1 (square c) (dt u) (dt v))
  179. (* (dx u) (dx v))
  180. (* (dy u) (dy v))
  181. (* (dz u) (dz v))))
  182. (define SR-vector-basis
  183. (down (* (/ 1 c) d/dt) d/dx d/dy d/dz))
  184. (define SR-1form-basis
  185. (up (* c dt) dx dy dz))
  186. (define SR-basis
  187. (make-basis SR-vector-basis
  188. SR-1form-basis))
  189. (s:map/r
  190. (lambda (u)
  191. (s:map/r (lambda (v)
  192. ((g-Minkowski u v) an-event))
  193. SR-vector-basis))
  194. SR-vector-basis)
  195. #|
  196. (down (down -1 0 0 0)
  197. (down 0 1 0 0)
  198. (down 0 0 1 0)
  199. (down 0 0 0 1))
  200. |#
  201. (define phi
  202. (literal-manifold-function 'phi SR))
  203. (((Laplacian g-Minkowski SR-basis) phi) an-event)
  204. #|
  205. (+ (* -1 (((partial 1) ((partial 1) phi)) (up t0 x0 y0 z0)))
  206. (* -1 (((partial 2) ((partial 2) phi)) (up t0 x0 y0 z0)))
  207. (* -1 (((partial 3) ((partial 3) phi)) (up t0 x0 y0 z0)))
  208. (/ (((partial 0) ((partial 0) phi)) (up t0 x0 y0 z0)) (expt c 2)))
  209. |#
  210. |#