forked from jheinen/GR.jl
-
Notifications
You must be signed in to change notification settings - Fork 0
/
spin.jl
77 lines (64 loc) · 1.66 KB
/
spin.jl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
using LinearAlgebra
import GR
const GR3 = GR.gr3
rate = 100
const μ0 = [0.03, 0., 0.09]
const Β = [0., 0., 0.8]
const γ = 2.0
const η = 0.1
const ΔT = 0.001
function calcualate_magnetic_moment(c::Channel)
μ = μ0
while true
put!(c, μ)
P = - γ * μ × Β # precession
G = η * (μ / norm(μ)) × P # Gilbert damping
dμ = (P + G) * ΔT
if norm(dμ) < 1e-6
break
end
μ += dμ
end
put!(c, nothing)
end
function calculate_cone_length(radius, cone_angle)
2 * radius / tand(cone_angle)
end
function drawarrowmesh(start, end_, color, radius, cone_angle=20)
direction = end_ - start
length = norm(direction)
cone_length = calculate_cone_length(radius, cone_angle)
cylinder_length = length - cone_length
direction /= length
cone_start = start + cylinder_length * direction
if cylinder_length > 0
GR3.drawcylindermesh(1, start, direction, color, [radius], [cylinder_length])
end
GR3.drawconemesh(1, cone_start, direction, color, [2 * radius], [cone_length])
end
function main()
c = Channel(calcualate_magnetic_moment)
i = 0
GR.setviewport(0, 1, 0, 1)
GR3.setbackgroundcolor(1, 1, 1, 1)
while isopen(c)
magmom = take!(c)
if magmom == nothing
break
end
i += 1
if i % rate != 0
continue
end
point = 5 * magmom / norm(magmom)
GR.clearws()
GR3.clear()
GR3.drawspheremesh(1, [0, 0, 0], [0, 0.75, 0.75], [1])
drawarrowmesh([0, 0, 0], point, [1, 0, 0], 0.1)
GR3.drawspheremesh(1, point, [0, 1, 0], [0.2])
GR3.cameralookat(10, 0, 10, 0, 0, 0, 0, 0, 1)
GR3.drawimage(0, 1, 0, 1, 500, 500, GR3.DRAWABLE_GKS)
GR.updatews()
end
end
main()