@@ -111,6 +111,125 @@ end
111111 @test maximum (abs .(sol_arr[u (t, x)] .- exact)) < 1.0e-2
112112end
113113
114+ # Parameterized calls keep `θ` whole.
115+ vecfn_scalar (u, θ) = θ[1 ] * u
116+ @register_symbolic vecfn_scalar (u, θ:: AbstractVector )
117+
118+ vecfn_array (u, θ) = [θ[1 ] * u]
119+ @register_array_symbolic vecfn_array (u:: Real , θ:: AbstractVector ) begin
120+ size = (1 ,)
121+ eltype = Real
122+ end
123+ struct VectorFunctionWrapper end
124+ (:: VectorFunctionWrapper )(u:: Number , θ:: AbstractVector ) = [θ[1 ] * u]
125+ (:: VectorFunctionWrapper )(u:: AbstractVector , θ:: AbstractVector ) = [θ[1 ] * u[1 ]]
126+ const vecfn_wrapper = VectorFunctionWrapper ()
127+ @parameters (vecfn_parameter:: typeof (vecfn_wrapper))(.. )[1 : 1 ] =
128+ vecfn_wrapper [tunable = false ]
129+
130+ vecfn_rev (θ, u) = θ[1 ] * u
131+ @register_symbolic vecfn_rev (θ:: AbstractVector , u)
132+
133+ function test_parameterized_heat (source; parameters = Any[], map_name = nothing )
134+ @parameters t x
135+ @parameters θ[1 : 2 ] = [1.0 , 0.0 ]
136+ @variables u (.. )
137+ Dt = Differential (t)
138+ Dxx = Differential (x)^ 2
139+
140+ eq = Dt (u (t, x)) ~ Dxx (u (t, x)) + source (u (t, x), θ)
141+ bcs = [u (0 , x) ~ sinpi (x), u (t, 0 ) ~ 0.0 , u (t, 1 ) ~ 0.0 ]
142+ domains = [t ∈ Interval (0.0 , 0.2 ), x ∈ Interval (0.0 , 1.0 )]
143+ ps = Any[parameters... , θ]
144+ @named pdesys = PDESystem (eq, bcs, domains, [t, x], [u (t, x)], ps)
145+
146+ disc = MOLFiniteDifference ([x => 0.05 ], t)
147+ sys_arr, _ = symbolic_discretize (pdesys, disc)
148+ @test narrayeqs_interior (sys_arr) == 1
149+ if map_name != = nothing
150+ int_eq = only (filter (eq -> isinterioreq (eq) && isarrayeq (eq), get_eqs (sys_arr)))
151+ @test occursin (map_name, string (int_eq))
152+ end
153+ prob = discretize (pdesys, disc)
154+ @test prob isa SciMLBase. DAEProblem
155+ sol_arr = solve (prob)
156+ @test successful_retcode (sol_arr)
157+ xdisc = sol_arr[x]
158+ tdisc = sol_arr[t]
159+ exact = [exp ((1 - pi ^ 2 ) * ti) * sinpi (xi) for ti in tdisc, xi in xdisc]
160+ @test maximum (abs .(sol_arr[u (t, x)] .- exact)) < 1.0e-2
161+ return nothing
162+ end
163+
164+ @testset " 1D diffusion, array parameter as scalar coefficient" begin
165+ test_parameterized_heat ((u, θ) -> θ[1 ] * u)
166+ end
167+
168+ @testset " 1D diffusion with parameterized registered function" begin
169+ test_parameterized_heat (
170+ (u, θ) -> vecfn_scalar (u, θ); map_name = " array_map_callable("
171+ )
172+ end
173+
174+ @testset " 1D diffusion with vector-valued registered function" begin
175+ test_parameterized_heat (
176+ (u, θ) -> vecfn_array (u, θ)[1 ];
177+ map_name = " array_map_callable_getindex("
178+ )
179+ end
180+
181+ @testset " 1D diffusion with callable vector parameter" begin
182+ @test ModelingToolkitBase. isparameter (Symbolics. unwrap (vecfn_parameter))
183+ test_parameterized_heat (
184+ (u, θ) -> vecfn_parameter (u, θ)[1 ];
185+ parameters = [vecfn_parameter],
186+ map_name = " array_map_callable_getindex("
187+ )
188+ end
189+
190+ @testset " Unsupported vector-parameter call falls back" begin
191+ @parameters t x
192+ @parameters θ[1 : 2 ] = [1.0 , 0.0 ]
193+ @variables u (.. )
194+ Dt = Differential (t)
195+ Dxx = Differential (x)^ 2
196+
197+ eq = Dt (u (t, x)) ~ Dxx (u (t, x)) + vecfn_rev (θ, u (t, x))
198+ bcs = [u (0 , x) ~ sinpi (x), u (t, 0 ) ~ 0.0 , u (t, 1 ) ~ 0.0 ]
199+ domains = [t ∈ Interval (0.0 , 0.2 ), x ∈ Interval (0.0 , 1.0 )]
200+ @named pdesys = PDESystem (eq, bcs, domains, [t, x], [u (t, x)], [θ])
201+
202+ disc = MOLFiniteDifference ([x => 0.05 ], t)
203+ sys, _ = symbolic_discretize (pdesys, disc)
204+ @test narrayeqs_interior (sys) == 0
205+ end
206+
207+ @testset " Vector-input callable parameter falls back and solves" begin
208+ @parameters t x
209+ @parameters θ[1 : 2 ] = [1.0 , 0.0 ]
210+ @variables u (.. )
211+ Dt = Differential (t)
212+ Dxx = Differential (x)^ 2
213+
214+ eq = Dt (u (t, x)) ~ Dxx (u (t, x)) + vecfn_parameter ([u (t, x)], θ)[1 ]
215+ bcs = [u (0 , x) ~ sinpi (x), u (t, 0 ) ~ 0.0 , u (t, 1 ) ~ 0.0 ]
216+ domains = [t ∈ Interval (0.0 , 0.2 ), x ∈ Interval (0.0 , 1.0 )]
217+ @named pdesys = PDESystem (
218+ eq, bcs, domains, [t, x], [u (t, x)], [vecfn_parameter, θ]
219+ )
220+
221+ disc = MOLFiniteDifference ([x => 0.05 ], t)
222+ sys, _ = symbolic_discretize (pdesys, disc)
223+ @test narrayeqs_interior (sys) == 0
224+ prob = discretize (pdesys, disc)
225+ sol = solve (prob)
226+ @test successful_retcode (sol)
227+ xdisc = sol[x]
228+ tdisc = sol[t]
229+ exact = [exp ((1 - pi ^ 2 ) * ti) * sinpi (xi) for ti in tdisc, xi in xdisc]
230+ @test maximum (abs .(sol[u (t, x)] .- exact)) < 1.0e-2
231+ end
232+
114233@testset " 1D diffusion, Neumann and Robin BCs" begin
115234 @parameters t x
116235 @variables u (.. )
0 commit comments