Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Sritchie/geodesic #70

Closed
wants to merge 19 commits into from
13 changes: 13 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,19 @@

## [unreleased]

- #69:

- Adds
`emmy.mathbox.physics.{lagrangian-curve,hamiltonian-curve,routhian-curve,geodesic}`
to the existing `ode-curve` function.

- Adds
`emmy.viewer.physics.{evolve-lagrangian,evolve-hamiltonian,evolve-routhian}`
to the existing `evolve` function.

- Updates the `emmy.viewer.components.physics/Evolve` component to use a
pre-allocated JS output array instead of forcing an allocation on each tick.

## [0.2.0]

- #57:
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -196,7 +196,7 @@ Use `plot/scene` to render multiple plots onto a 3D canvas:

![MathBox plot example](https://github.com/mentat-collective/emmy-viewers/assets/69635/3349cff0-744c-40fa-b996-1d324162b3d9)

> See the [MathBox Guide](https://emmy-viewers.mentat.org/dev/examples/mafs) for 3D plotting examples.
> See the [MathBox Guide](https://emmy-viewers.mentat.org/dev/examples/mathbox) for 3D plotting examples.

Next, try clicking the 'show code' link for the examples at the top of the
page and pasting their source into your namespace. Then visit the ["Specific
Expand Down
2 changes: 1 addition & 1 deletion dev/emmy_viewers/notebook.clj
Original file line number Diff line number Diff line change
Expand Up @@ -222,7 +222,7 @@
:t [-10 10]
:color :green}))

;; > See the [MathBox Guide](/dev/examples/mafs) for 3D plotting examples.
;; > See the [MathBox Guide](/dev/examples/mathbox) for 3D plotting examples.
;;
;; Next, try clicking the 'show code' link for the examples at the top of the
;; page and pasting their source into your namespace. Then visit the ["Specific
Expand Down
2 changes: 0 additions & 2 deletions dev/emmy_viewers/sci_extensions.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@
(:require [emmy.viewer.sci]
[examples.simulation.cylinder-flow]
[examples.simulation.lorenz]
[examples.simulation.phase-portrait]
[examples.simulation.quartic-well]
[nextjournal.clerk.sci-env]))

(emmy.viewer.sci/install!)
71 changes: 49 additions & 22 deletions dev/examples/manifold/klein.clj
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
:exclude [+ - * / zero? compare divide numerator denominator
infinite? abs ref partial =])
(:require [emmy.clerk :as ec]
[emmy.mathbox.physics :as ph]
[emmy.env :as e :refer :all]
[emmy.leva :as leva]
[emmy.mathbox.plot :as p]
Expand All @@ -17,17 +18,10 @@

;; # Fun with Klein Bottles

(defn slider-surface [name {:keys [u v] :as opts}]
(defn slider-surface [name {:keys [u v] :as opts} & children]
(ev/with-let [!opts {:u (peek u) :v (peek v)}]
(p/scene
{:axes
{:x {:divisions 10
:label {:position 3}}
:y {:divisions 10
:ticks {:labels? false}}
:z {:divisions 10
:label-ticks? false}}
:grids []}
(apply
p/scene
(leva/controls
{:folder {:name name}
:schema
Expand All @@ -37,7 +31,8 @@
(p/parametric-surface
(assoc opts
:u [(first u) (ev/get !opts :u)]
:v [(first v) (ev/get !opts :v)])))))
:v [(first v) (ev/get !opts :v)]))
children)))

;; ## Mobius Strip

Expand Down Expand Up @@ -67,18 +62,31 @@

;; Can you find the Emmy logo hidden inside?

(ev/with-let [!r {:r 2}]
(ev/with-let [!r {:r 2 :theta_0 1 :alpha_0 0 :steps 20}]
[:<>
(leva/controls {:atom !r
:folder {:name "Klein Bagel"}
:schema {:r {:min 2 :max 6 :step 0.01}}})
(leva/controls
{:atom !r
:folder {:name "Klein Bagel"}
:schema {:r {:min 2 :max 6 :step 0.01}
:theta_0 {:label (->infix 'theta_0) :min 0 :max Math/PI :step 0.02}
:alpha_0 {:label (->infix 'alpha_0) :min 0 :max Math/PI :step 0.02}
:steps {:min 500 :max 9000 :step 50}}})
(slider-surface
"Klein Bagel"
{:f (ev/with-params {:atom !r :params [:r]}
klein-bagel)
:opacity 0.75
:u [0 (* 2 Math/PI)]
:v [0 (* 2 Math/PI)]})])
:v [0 (* 2 Math/PI)]}

(ph/geodesic
{:x0 [(ev/get !r :theta_0) 0]
:v0 [(list 'Math/cos (ev/get !r :alpha_0))
(list 'Math/sin (ev/get !r :alpha_0))]
:xform (ev/with-params {:atom !r :params [:r]}
klein-bagel)
:steps (ev/get !r :steps)
:width 2
:end? true}))])

;; ## Klein bottle:

Expand All @@ -102,11 +110,30 @@
(* 2/15 (sin v)
(+ 3 (* 5 (cos u) (sin u))))])

(slider-surface
"Klein"
{:f klein-bottle
:u [0 Math/PI]
:v [0 (* 2 Math/PI)]})
(ev/with-let [!r {:theta_0 1 :alpha_0 0 :steps 20}]
[:<>
(leva/controls
{:atom !r
:folder {:name "Klein"}
:schema {:theta_0 {:label (->infix 'theta_0) :min 0 :max Math/PI :step 0.02}
:alpha_0 {:label (->infix 'alpha_0) :min 0 :max Math/PI :step 0.02}
:steps {:min 10 :max 9000 :step 50}}})

(slider-surface
"Klein"
{:f klein-bottle
:opacity 0.2
:u [0 Math/PI]
:v [0 (* 2 Math/PI)]}

(ph/geodesic
{:x0 [(ev/get !r :theta_0) 0]
:v0 [(list 'Math/cos (ev/get !r :alpha_0))
(list 'Math/sin (ev/get !r :alpha_0))]
:xform klein-bottle
:steps (ev/get !r :steps)
:width 2
:end? true}))])

;; ## Plucker's Conoid

Expand Down
35 changes: 35 additions & 0 deletions dev/examples/mathbox/color_test.clj
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
^{:nextjournal.clerk/visibility {:code :hide}}
(ns examples.mathbox.color-test
#:nextjournal.clerk{:toc true}
(:refer-clojure
:exclude [+ - * / = zero? compare numerator denominator ref partial
infinite? abs])
(:require [emmy.clerk :as ec]
[emmy.leva]
[emmy.env :as e :refer :all]
[emmy.mathbox.plot :as plot]
[emmy.viewer :as ev]))

{:nextjournal.clerk/width :wide}

^{:nextjournal.clerk/visibility {:code :hide :result :hide}}
(ec/install!)

;; # Color Testing

;; ## Demo
;;
;; Let's make a function to try!

(ev/with-let [!state {:amplitude 1}]
(plot/scene
(emmy.leva/controls {:atom !state})
(plot/of-xy
{:x-samples 128
:y-samples 128
#_#_:color "LimeGreen"
:z (ev/with-params {:atom !state :params [:amplitude]}
(fn [amplitude]
(fn [[x y]]
(* amplitude
(cos (* x y))))))})))
72 changes: 72 additions & 0 deletions dev/examples/mathbox/feig.clj
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
;; # Feigenbaum

(ns examples.mathbox.feig
{:nextjournal.clerk/toc true}
(:require [emmy.clerk :as ec]
[emmy.mathbox :as mb]
[nextjournal.clerk :as clerk]))

^{::clerk/visibility {:code :hide :result :hide}}
(ec/install!)

(defn f [p x] (* p x (- 1 x)))

(defn fixps [r]
(->> (iterate (partial f r) (rand))
(drop 300)
(take 300)
set
(map (fn [x] [r x]))))

(def tree (into [] (mapcat fixps) (range 2.5 4.0 0.005)))
(def width (count tree))
(def boundary (Math/floor (Math/sqrt width)))

(defn Feigenbaum []
[:<>
(mb/matrix
{:width (quot width boundary)
:height (mod width boundary)
:channels 2
:live false
:expr `(fn [emit# i# j#]
(apply emit# (nth ~tree (+ (* i# ~boundary) j#) nil)))})
(mb/point

{:color "#3090FF"
:size 1})])

(defn axis
([axis-no] (axis axis-no [0 0 0 0]))
([axis-no origin]
[:<>
(mb/axis {:axis axis-no
:width 2
:color "black"
:origin origin})
(mb/scale {:divide 10
:axis axis-no
:origin origin})
(mb/ticks {:width 5
:size 15
:color "black"})
(mb/format {:digits 2
:weight "bold"})
(mb/label {:color "red"
:zIndex 1})]))

^{::clerk/width :wide}
(mb/mathbox
{:container {:style {:height "1000px" :width "100%"}}}
(mb/camera {:position [0 0 2.1]
:proxy true})
(mb/layer
(mb/cartesian
{:range [[2.5 4 0] [0 1 0]]
:scale [0.8 0.8 1]}
(mb/grid {:width 1
:divideX 10
:divideY 10})
(axis 1)
(axis 2 [2.5 0 0 0])
(Feigenbaum))))
24 changes: 22 additions & 2 deletions dev/examples/mathbox/geom.clj
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
(:require [emmy.clerk :as ec]
[emmy.leva]
[emmy.env :as e :refer :all]
[emmy.matrix]
[emmy.viewer :as ev]
[emmy.mathbox.plot :as p]))

Expand All @@ -15,9 +16,28 @@
^{:nextjournal.clerk/visibility {:code :hide :result :hide}}
(ec/install!)

(defn basis [m]
(let [[x y z] (emmy.matrix/transpose m)]
(-> [:<>
(p/vector {:tip x :arrow-size 3 :color "red"})
(p/vector {:tip y :arrow-size 3 :color "green"})
(p/vector {:tip z :arrow-size 3 :color "blue"})]
(ev/fragment p/scene))))

(ev/with-let [!size {:y 1}]
(p/scene
(emmy.leva/controls
{:atom !size :schema {:y {:min 0 :max 10 :step 0.01}}})
(basis
(matrix-by-rows
[1 0 0]
[0 (ev/get !size :y) 0]
[1 0 2]))))

(ev/with-let [!size {:size 20 :pos 1}]
[:<> (emmy.leva/controls
{:atom !size :schema {:size {:min 20 :max 100 :step 1}}})
[:<>
(emmy.leva/controls
{:atom !size :schema {:size {:min 20 :max 100 :step 1}}})
(p/scene
(p/point
{:coords [0.5 -0.5 (ev/get !size :pos)]
Expand Down
7 changes: 3 additions & 4 deletions dev/examples/simulation/double_ellipsoid.clj
Original file line number Diff line number Diff line change
Expand Up @@ -52,12 +52,11 @@
{:threestrap {:plugins ["core" "controls" "cursor" "stats"]}}
(leva/controls {:atom !opts})

(emmy.viewer.physics/evolve
(emmy.viewer.physics/evolve-lagrangian
{:atom !state
:initial-state initial-state
:f' (ev/with-params {:atom !opts :params [:m :k :x0 :a :b :c]}
(comp Lagrangian->state-derivative
L-central-triaxial))})
:L (ev/with-params {:atom !opts :params [:m :k :x0 :a :b :c]}
L-central-triaxial)})

(emmy.mathbox.physics/comet
{:length 16
Expand Down
7 changes: 3 additions & 4 deletions dev/examples/simulation/ellipsoid.clj
Original file line number Diff line number Diff line change
Expand Up @@ -117,12 +117,11 @@
:b {:min 1 :max 5 :step 0.01}
:c {:min 1 :max 5 :step 0.01}}})

(emmy.viewer.physics/evolve
(emmy.viewer.physics/evolve-lagrangian
{:atom !state
:initial-state initial-state
:f' (ev/with-params {:atom !opts :params [:m :g :a :b :c]}
(comp Lagrangian->state-derivative
L-central-triaxial))})
:L (ev/with-params {:atom !opts :params [:m :g :a :b :c]}
L-central-triaxial)})

(plot/parametric-surface
{:opacity 0.2
Expand Down
8 changes: 4 additions & 4 deletions dev/examples/simulation/oscillator.clj
Original file line number Diff line number Diff line change
Expand Up @@ -36,14 +36,14 @@
(plot/scene
(leva/controls {:atom !opts})

(emmy.viewer.physics/evolve
(emmy.viewer.physics/evolve-lagrangian
{:atom !state
:initial-state initial-state
:f' (ev/with-params {:atom !opts :params [:g :m :k]}
(comp e/Lagrangian->state-derivative L-harmonic))})
:L (ev/with-params {:atom !opts :params [:g :m :k]}
L-harmonic)})

(emmy.mathbox.physics/comet
{:length 1
{:length 10
:state->xyz coordinate
:initial-state initial-state
:atom !state}))))
Expand Down
Loading