@@ -9,6 +9,8 @@ using LinearAlgebra
99using MLStyle
1010using OrdinaryDiffEq
1111using Test
12+ Point3D = Point3{Float64}
13+ flatten (vfield:: Function , mesh) = ♭ (mesh, DualVectorField (vfield .(mesh[triangle_center (mesh),:dual_point ])))
1214
1315function test_hodge (k, sd:: HasDeltaSet , hodge)
1416 hodge = ⋆ (k,sd,hodge= hodge)
@@ -56,59 +58,18 @@ function generate(sd, my_symbol)
5658end
5759
5860@testset " Simulation Generation" begin
59-
60- DiffusionExprBody = quote
61- (C, Ċ):: Form0{X}
62- ϕ:: Form1{X}
63-
64- # Fick's first law
65- ϕ == ∘ (k, d₀)(C)
66- # Diffusion equation
67- Ċ == ∘ (⋆ ₁, dual_d₁, ⋆ ₀⁻¹)(ϕ)
68- ∂ₜ (C) == Ċ
69- end
70-
71- diffExpr = parse_decapode (DiffusionExprBody)
72- ddp = SummationDecapode (diffExpr)
73- add_constant! (ddp, :k )
74- @test nparts (ddp, :Var ) == 4
75-
76- DiffusionExprBody = quote
77- (C, Ċ):: Form0{X}
78- ϕ:: Form1{X}
79- k:: Constant{ℝ}
80-
81-
82- # Fick's first law
83- ϕ == k * d₀ (C)
84- # Diffusion equation
85- Ċ == ∘ (⋆ ₁, dual_d₁, ⋆ ₀⁻¹)(ϕ)
86- ∂ₜ (C) == Ċ
87- end
88-
89- diffExpr = parse_decapode (DiffusionExprBody)
90- ddp = SummationDecapode (diffExpr)
91-
92- dec_matrices = Vector {Symbol} ()
93- alloc_vectors = Vector {Decapodes.AllocVecCall} ()
94-
95- @test Decapodes. get_vars_code (ddp, [:k ]). args[2 ] == :(k = p. k)
96- @test infer_state_names (ddp) == [:C , :k ]
97-
61+ # Mesh and ICs to use for these tests:
9862torus = loadmesh (Torus_30x10 ())
9963c_dist = MvNormal ([5 , 5 ], LinearAlgebra. Diagonal (map (abs2, [1.5 , 1.5 ])))
10064c = [pdf (c_dist, [p[1 ], p[2 ]]) for p in torus[:point ]]
101-
10265u₀ = ComponentArray (C= c)
10366du = ComponentArray (C= zero (c))
10467
105- f = eval (gensim (expand_operators (ddp)))
106- fₘₛ = f (torus, generate)
107-
108- DiffusionExprBody = quote
68+ # Three Decapodes variations, with k as constant, parameter, or literal.
69+ DiffusionWithConstant = @decapode begin
10970 (C, Ċ):: Form0{X}
11071 ϕ:: Form1{X}
111- k:: Parameter {ℝ}
72+ k:: Constant {ℝ}
11273
11374 # Fick's first law
11475 ϕ == k * d₀ (C)
@@ -117,80 +78,65 @@ DiffusionExprBody = quote
11778 ∂ₜ (C) == Ċ
11879end
11980
120- diffExpr = parse_decapode (DiffusionExprBody)
121- ddp = SummationDecapode (diffExpr)
122- dec_matrices2 = Vector {Symbol} ()
123- alloc_vectors2 = Vector {Decapodes.AllocVecCall} ()
124-
125- @test infer_state_names (ddp) == [:C , :k ]
126- @test Decapodes. get_vars_code (ddp, [:k ]). args[2 ] == :(k = p. k (t))
127-
128- f = eval (gensim (expand_operators (ddp)))
129- fₘₚ = f (torus, generate)
130-
131- @test norm (fₘₛ (du, u₀, (k= 2.0 ,), 0 ) - fₘₚ (du, u₀, (k= t-> 2.0 ,), 0 )) < 1e-4
132-
133- DiffusionExprBody = quote
134- (C, Ċ):: Form0{X}
135- ϕ:: Form1{X}
81+ DiffusionWithParameter = @decapode begin
82+ (C, Ċ):: Form0{X}
83+ ϕ:: Form1{X}
84+ k:: Parameter{ℝ}
13685
137- # Fick's first law
138- ϕ == 3 * d₀ (C)
139- # Diffusion equation
140- Ċ == ∘ (⋆ ₁, dual_d₁, ⋆ ₀⁻¹)(ϕ)
141- ∂ₜ (C) == Ċ
86+ # Fick's first law
87+ ϕ == k * d₀ (C)
88+ # Diffusion equation
89+ Ċ == ∘ (⋆ ₁, dual_d₁, ⋆ ₀⁻¹)(ϕ)
90+ ∂ₜ (C) == Ċ
14291end
14392
144- diffExpr = parse_decapode (DiffusionExprBody)
145- ddp = SummationDecapode (diffExpr)
146-
147- @test infer_state_names (ddp) == [:C ]
148- # TODO : Fix proper Expr equality, the Float64 does not equate here
149- # @test Decapodes.get_vars_code(ddp, [Symbol("3")]).args[2] == :(var"3"::Float64 = 3.0)
150- @test Decapodes. get_vars_code (ddp, [Symbol (" 3" )]). args[2 ]. args[1 ] == :(var"3" )
151- @test Decapodes. get_vars_code (ddp, [Symbol (" 3" )]). args[2 ]. args[2 ] == 3.0
152-
93+ DiffusionWithLiteral = @decapode begin
94+ (C, Ċ):: Form0{X}
95+ ϕ:: Form1{X}
15396
154- f = eval (gensim (expand_operators (ddp)))
155- fₘₚ = f (torus, generate)
97+ # Fick's first law
98+ ϕ == 3 * d₀ (C)
99+ # Diffusion equation
100+ Ċ == ∘ (⋆ ₁, dual_d₁, ⋆ ₀⁻¹)(ϕ)
101+ ∂ₜ (C) == Ċ
102+ end
156103
157- @test norm (fₘₛ (du, u₀, (k= 2.0 ,), 0 ) - fₘₚ (du, u₀, (k= t-> 2.0 ,), 0 )) < 1e-4
104+ # Verify the variable accessors.
105+ @test Decapodes. get_vars_code (DiffusionWithConstant, [:k ]). args[2 ] == :(k = p. k)
106+ @test infer_state_names (DiffusionWithConstant) == [:C , :k ]
158107
159- DiffusionExprBody = quote
160- (C, Ċ):: Form0{X}
161- ϕ:: Form1{X}
108+ @test infer_state_names (DiffusionWithParameter) == [:C , :k ]
109+ @test Decapodes. get_vars_code (DiffusionWithParameter, [:k ]). args[2 ] == :(k = p. k (t))
162110
163- # Fick's first law
164- ϕ == 3 * d₀ (C)
165- # Diffusion equation
166- Ċ == ∘ (⋆ ₁, dual_d₁, ⋆ ₀⁻¹)(ϕ)
167- ∂ₜ (C) == Ċ
168- end
111+ @test infer_state_names (DiffusionWithLiteral) == [:C ]
112+ # TODO : Fix proper Expr equality, the Float64 does not equate here
113+ # @test Decapodes.get_vars_code(DiffusionWithLiteral, [Symbol("3")]).args[2] == :(var"3"::Float64 = 3.0)
114+ @test Decapodes. get_vars_code (DiffusionWithLiteral, [Symbol (" 3" )]). args[2 ]. args[1 ] == :(var"3" )
115+ @test Decapodes. get_vars_code (DiffusionWithLiteral, [Symbol (" 3" )]). args[2 ]. args[2 ] == 3.0
169116
170- diffExpr = parse_decapode (DiffusionExprBody)
171- ddp = SummationDecapode (diffExpr)
117+ # Test that simulations generated from these return the same result.
118+ f = evalsim (DiffusionWithConstant)
119+ f_with_constant = f (torus, generate)
172120
173- @test infer_state_names (ddp) == [:C ]
174- # TODO : Fix proper Expr equality, the Float64 does not equate here
175- # @test Decapodes.get_vars_code(ddp, [Symbol("3")]).args[2] == :(var"3"::Float64 = 3.0)
176- @test Decapodes. get_vars_code (ddp, [Symbol (" 3" )]). args[2 ]. args[1 ] == :(var"3" )
177- @test Decapodes. get_vars_code (ddp, [Symbol (" 3" )]). args[2 ]. args[2 ] == 3.0
121+ f = evalsim (DiffusionWithParameter)
122+ f_with_parameter = f (torus, generate)
178123
179- f = eval (gensim (expand_operators (ddp )))
180- fₘₚ = f (torus, generate)
124+ f = eval (gensim (expand_operators (DiffusionWithLiteral )))
125+ f_with_literal = f (torus, generate)
181126
182- @test norm (fₘₛ (du, u₀, (k= 2.0 ,), 0 ) - fₘₚ (du, u₀, (k= t-> 2.0 ,), 0 )) < 1e-4
127+ f_with_constant (du, u₀, (k= 3.0 ,), 0 )
128+ fc_res = copy (du. C)
129+ f_with_parameter (du, u₀, (k= t-> 3.0 ,), 0 )
130+ fp_res = copy (du. C)
131+ f_with_literal (du, u₀, NamedTuple (), 0 )
132+ fl_res = copy (du. C)
183133
184- # to solve the ODE over a duration, use the ODEProblem from OrdinaryDiffEq
185- # tₑ = 10
186- # using OrdinaryDiffEq
187- # prob = ODEProblem(fₘₛ,u₀, (0,tₑ), (k=2.0,))
188- # soln = solve(prob, Tsit5())
134+ @test norm (fc_res - fp_res) < 1e-4
135+ @test norm (fc_res - fl_res) < 1e-4
189136end
190137
191138# Testing done based on the original gensim
192- Point3D = Point3{Float64}
193- flatten (vfield:: Function , mesh) = ♭ (mesh, DualVectorField (vfield .(mesh[triangle_center (mesh),:dual_point ])))
139+ # -----------------------------------------
194140
195141# Testing Brusselator
196142@testset " Brusselator Simulation" begin
394340 A = ones (nv (earth))
395341 u = ComponentArray (A= A)
396342 constants_and_parameters = ()
397- result = f (u, u, constants_and_parameters, 0 )
343+ f (u, u, constants_and_parameters, 0 )
398344
399- @test result == - 1 * ones (nv (earth))
345+ @test u . A == - 1 * ones (nv (earth))
400346
401347
402348 # Testing simple contract operations
@@ -799,9 +745,6 @@ _ = @allocated f(du, u₀, p, (0,1.0)) # 3962696
799745# Test that subsequent calls make a reasonable amount.
800746bytes = @allocated f (du, u₀, p, (0 ,1.0 ))
801747nallocs = @allocations f (du, u₀, p, (0 ,1.0 ))
802- @test nallocs == 4
803- @test bytes ==
804- sizeof (u₀. C) +
805- sizeof (p. D) +
806- sizeof (Decapodes. FixedSizeDiffCache (Vector {Float64} (undef , nparts (sd, :V ))))
748+ @test nallocs == 3
749+ @test bytes == 80
807750end
0 commit comments