Skip to content

Instantly share code, notes, and snippets.

@sritchie
Created April 7, 2023 17:51
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sritchie/9836d443dbd31a8f82f42814ed3285d9 to your computer and use it in GitHub Desktop.
Save sritchie/9836d443dbd31a8f82f42814ed3285d9 to your computer and use it in GitHub Desktop.
(ns examples.simulation.toroid
(:refer-clojure
:exclude [+ - * / = zero? compare
numerator denominator ref partial
infinite? abs])
(:require [emmy.env :as e :refer :all]
[emmy.expression.compile :as xc]
[nextjournal.clerk :as clerk]
[examples.manifold.pq-knot :as pq]))
;; ## Geodesics of the PQ Torus Knot
(defn torus-knot->rect
[_ p q R r2 r3]
(let [theta->xyz (pq/torus-knot R (+ r2 r3) p q)
M (pq/->TNB theta->xyz)]
(fn [[_ [theta phi]]]
(+ (theta->xyz theta)
(* (M theta)
(pq/circle r3 phi))))))
;; Next, the Lagrangian:
(defn T-free-particle [m]
(fn [[_ _ v]]
(* 1/2 m (square v))))
(defn V-free-particle [_m]
(constantly 0))
((T-free-particle 'm)
(up 't
(up 'theta 'phi)
(up 'v_theta 'v_phi)))
(defn L-torus-knot [m p q r1 r2 r3]
(comp
(- (T-free-particle m)
(V-free-particle m))
(F->C
(torus-knot->rect m p q r1 r2 r3))))
^{::clerk/viewer
{:transform-fn
(comp clerk/mark-presented
(clerk/update-val
(fn [{:keys [L params initial-state state->xyz
keys] :as m}]
(assoc m
:L
(xc/compile-state-fn
(compose e/Lagrangian->state-derivative L)
(mapv params keys)
initial-state
{:mode :js
:calling-convention :primitive
:generic-params? false
:simplify? false})
:state->xyz
(xc/compile-state-fn
state->xyz
(mapv params keys)
initial-state
{:mode :js
:calling-convention :primitive
:generic-params? false
:simplify? false})))))
:render-fn 'demo.mathbox/ToroidViewer}}
{:params
{:m 10000
:p 7
:q 8
:r1 1.791
:r2 0.95
:r3 0.1}
:keys [:mass :p :q :r1 :r2 :r3]
:schema
{:mass {:min 1000 :max 20000 :step 100}
:p {:min 0 :max 32 :step 1}
:q {:min 0 :max 32 :step 1}
:r1 {:min 0 :max 3 :step 0.001}
:r2 {:min 0.0 :max 2.5 :step 0.01}
:r3 {:min 0.0 :max 0.2 :step 0.01}}
:state->xyz torus-knot->rect
:L L-torus-knot
:initial-state [0 [0 0] [6 1]]
:cartesian
{:range [[-1 1] [-1 1] [-1 1]]
:scale [1 1 1]}}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment