-
Notifications
You must be signed in to change notification settings - Fork 14
/
componentarrays.jl
178 lines (147 loc) · 4.9 KB
/
componentarrays.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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
using ComponentArrays
using OrdinaryDiffEq
using GeometryBasics
using JSON
using Distributions
using Artifacts
# using GLMakie
using Catlab
using Catlab.CategoricalAlgebra
using CombinatorialSpaces
using DiagrammaticEquations
using DiagrammaticEquations.Deca
using Decapodes
using Test
using MLStyle
using LinearAlgebra
C = ones(Float64, 10)
V = ones(Float64, 100)
u₀ = ComponentArray(C=C,V=V)
dynamics(du, u, p, t) = begin
du.C .= 0.1 * u.C
return du
end
prob = ODEProblem(dynamics,u₀,(0,1))
soln = solve(prob, Tsit5())
function generate(sd, my_symbol)
op = @match my_symbol begin
:k => x->x/20
_ => default_dec_generate(sd, my_symbol)
end
# return (args...) -> begin println("applying $my_symbol"); println("arg length $(length(args[1]))"); op(args...);end
return (args...) -> op(args...)
end
plot_mesh = loadmesh(Rectangle_30x10())
periodic_mesh = loadmesh(Torus_30x10())
point_map = loadmesh(Point_Map())
# function plotform0(plot_mesh, c)
# fig, ax, ob = mesh(plot_mesh; color=c[point_map]);
# Colorbar(fig)
# ax.aspect = AxisAspect(3.0)
# fig
# end
DiffusionExprBody = quote
(C, Ċ)::Form0{X}
ϕ::Form1{X}
# Fick's first law
ϕ == ∘(d₀, k)(C)
# Diffusion equation
Ċ == ∘(⋆₁, dual_d₁, ⋆₀⁻¹)(ϕ)
∂ₜ(C) == Ċ
end
diffExpr = parse_decapode(DiffusionExprBody)
ddp = SummationDecapode(diffExpr)
gensim(expand_operators(ddp), [:C])
f = eval(gensim(expand_operators(ddp), [:C]))
fₘ = f(periodic_mesh, generate)
c_dist = MvNormal([5, 5], LinearAlgebra.Diagonal(map(abs2, [1.5, 1.5])))
c = [pdf(c_dist, [p[1], p[2]]) for p in periodic_mesh[:point]]
u₀ = ComponentArray(C=c)
tₑ = 10
prob = ODEProblem(fₘ,u₀,(0,tₑ))
soln = solve(prob, Tsit5())
soln(0.9)
# plotform0(plot_mesh, findnode((soln(1)-u₀), :C))
# plotform0(plot_mesh, findnode((soln(0.0000000000001)-u₀), :C))
# times = range(0.0, tₑ, length=150)
# colors = [findnode(soln(t), :C)[point_map] for t in times]
# Initial frame
# fig, ax, ob = mesh(plot_mesh, color=colors[1], colorrange = extrema(vcat(colors...)))
# ax.aspect = AxisAspect(3.0)
# Colorbar(fig[1,2], ob)
# framerate = 30
# Animation
# record(fig, "diff.gif", range(0.0, tₑ; length=150); framerate = 30) do t
# ob.color = findnode(soln(t), :C)[point_map]
# end
function closest_point(p1, p2, dims)
p_res = collect(p2)
for i in 1:length(dims)
if dims[i] != Inf
p = p1[i] - p2[i]
f, n = modf(p / dims[i])
p_res[i] += dims[i] * n
if abs(f) > 0.5
p_res[i] += sign(f) * dims[i]
end
end
end
Point3{Float64}(p_res...)
end
function flat_op(s::AbstractDeltaDualComplex2D, X::AbstractVector; dims=[Inf, Inf, Inf])
# XXX: Creating this lookup table shouldn't be necessary. Of course, we could
# index `tri_center` but that shouldn't be necessary either. Rather, we should
# loop over incident triangles instead of the elementary duals, which just
# happens to be inconvenient.
tri_map = Dict{Int,Int}(triangle_center(s,t) => t for t in triangles(s))
map(edges(s)) do e
p = closest_point(point(s, tgt(s,e)), point(s, src(s,e)), dims)
e_vec = (point(s, tgt(s,e)) - p) * sign(1,s,e)
dual_edges = elementary_duals(1,s,e)
dual_lengths = dual_volume(1, s, dual_edges)
mapreduce(+, dual_edges, dual_lengths) do dual_e, dual_length
X_vec = X[tri_map[s[dual_e, :D_∂v0]]]
dual_length * dot(X_vec, e_vec)
end / sum(dual_lengths)
end
end
AdvDiff = quote
(C, Ċ)::Form0{X}
(V, ϕ, ϕ₁, ϕ₂)::Form1{X}
# Fick's first law
ϕ₁ == (d₀∘k)(C)
ϕ₂ == ∧₀₁(C,V)
ϕ == ϕ₁ + ϕ₂
# Diffusion equation
Ċ == ∘(⋆₁, dual_d₁,⋆₀⁻¹)(ϕ)
∂ₜ(C) == Ċ
end
advdiff = parse_decapode(AdvDiff)
advdiffdp = SummationDecapode(advdiff)
# Decapodes.compile(advdiffdp, [:C, :V])
# Decapodes.compile(expand_operators(advdiffdp), [:C, :V])
# gensim(expand_operators(advdiffdp), [:C, :V])
sim = eval(gensim(expand_operators(advdiffdp), [:C, :V]))
fₘ = sim(periodic_mesh, generate)
velocity(p) = [-0.5, -0.5, 0.0]
v = flat_op(periodic_mesh, DualVectorField(velocity.(periodic_mesh[triangle_center(periodic_mesh),:dual_point])); dims=[30, 10, Inf])
c_dist = MvNormal([7, 5], LinearAlgebra.Diagonal(map(abs2, [1.5, 1.5])))
c = [pdf(c_dist, [p[1], p[2]]) for p in periodic_mesh[:point]]
u₀ = ComponentArray(C=c,V=v)
tₑ = 24
prob = ODEProblem(fₘ,u₀,(0,tₑ))
soln = solve(prob, Tsit5())
@test norm(soln.u[end].C - soln.u[1].C) >= 1e-4
@test norm(soln.u[end].V - soln.u[1].V) <= 1e-8
# Plot the result
# times = range(0.0, tₑ, length=150)
# colors = [findnode(soln(t), :C)[point_map] for t in times]
# Initial frame
# fig, ax, ob = mesh(plot_mesh, color=colors[end], colorrange = extrema(vcat(colors...)))
# ax.aspect = AxisAspect(3.0)
# Colorbar(fig[1,2], ob)
# framerate = 30
# Animation
# record(fig, "diff_adv.gif", range(0.0, tₑ; length=150); framerate = 30) do t
# ob.color = findnode(soln(t), :C)[point_map]
# end