From db60c04ec9cd21d016e8798a3c19774891a0813c Mon Sep 17 00:00:00 2001 From: anand jain Date: Wed, 7 Dec 2022 05:19:01 -0500 Subject: [PATCH 1/3] `@to_kw` macro for easy boundary conditions --- src/MOL_utils.jl | 25 +++++++++++++++++++++++++ src/MethodOfLines.jl | 2 +- test/utils_test.jl | 19 +++++++++++++++++++ 3 files changed, 45 insertions(+), 1 deletion(-) diff --git a/src/MOL_utils.jl b/src/MOL_utils.jl index a4357df81..4f5132895 100644 --- a/src/MOL_utils.jl +++ b/src/MOL_utils.jl @@ -311,3 +311,28 @@ function ex2term(term, v) name = Symbol("⟦" * string(term) * "⟧") return setname(similarterm(exdv, rename(operation(exdv), name), arguments(exdv)), name) end + +function _to_kw(ex) + @assert ex.head == :call + args = ex.args + @assert all(x isa Symbol for x in args) + u = args[1] + xs = args[2:end] + kw_fn_name = Symbol(ex.args[1], "_kw") + kws = [] + for arg in xs + kw = Expr(:kw, arg, esc(arg)) + push!(kws, kw) + end + params = Expr(:parameters, kws...) + + def_call_ex = Expr(:call, kw_fn_name, params) + block_call_ex = Expr(:call, u, xs...) + + body = Expr(:block, block_call_ex) + Expr(:function, def_call_ex, body) +end + +macro to_kw(ex) + _to_kw(ex) +end diff --git a/src/MethodOfLines.jl b/src/MethodOfLines.jl index e1c6dff5e..327b14cea 100644 --- a/src/MethodOfLines.jl +++ b/src/MethodOfLines.jl @@ -76,7 +76,7 @@ include("error_analysis.jl") include("scalar_discretization.jl") include("MOL_discretization.jl") -export MOLFiniteDifference, discretize, symbolic_discretize, ODEFunctionExpr, generate_code, grid_align, edge_align, center_align, get_discrete +export MOLFiniteDifference, discretize, symbolic_discretize, ODEFunctionExpr, generate_code, grid_align, edge_align, center_align, get_discrete, @to_kw export UpwindScheme, WENOScheme end diff --git a/test/utils_test.jl b/test/utils_test.jl index d7be033c4..620222c7a 100644 --- a/test/utils_test.jl +++ b/test/utils_test.jl @@ -131,3 +131,22 @@ end I = CartesianIndex(-1, 2) @test MethodOfLines._wrapperiodic(I, 2, 1, 4) == CartesianIndex(2, 2) end + +@testset "@to_kw" begin + @variables t + N = 2 + x = Symbolics.variables(:x, 1:N) + + for (i, xi) in enumerate(x) + @eval $(Symbol(:x, i)) = $xi + end + + @variables u(..) + + @to_kw u(t, x1, x2) + @test isequal(u_kw(t=0), u(0, x1, x2)) + + @variables foo(..) + @to_kw foo(t, x1, x2) + @test isequal(foo_kw(t=0), foo(0, x1, x2)) +end \ No newline at end of file From b61fc78372aeaa2273015397e07f60789c5050ac Mon Sep 17 00:00:00 2001 From: anand jain Date: Wed, 7 Dec 2022 05:21:25 -0500 Subject: [PATCH 2/3] newline --- test/utils_test.jl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/utils_test.jl b/test/utils_test.jl index 620222c7a..c74154be7 100644 --- a/test/utils_test.jl +++ b/test/utils_test.jl @@ -149,4 +149,4 @@ end @variables foo(..) @to_kw foo(t, x1, x2) @test isequal(foo_kw(t=0), foo(0, x1, x2)) -end \ No newline at end of file +end From ef56f354206a3e23e7dfc8574b9fd39b3743127f Mon Sep 17 00:00:00 2001 From: anand jain Date: Thu, 8 Dec 2022 16:22:22 -0500 Subject: [PATCH 3/3] fix escaping --- src/MOL_utils.jl | 4 ++-- test/utils_test.jl | 1 - 2 files changed, 2 insertions(+), 3 deletions(-) diff --git a/src/MOL_utils.jl b/src/MOL_utils.jl index 4f5132895..972dc4181 100644 --- a/src/MOL_utils.jl +++ b/src/MOL_utils.jl @@ -326,8 +326,8 @@ function _to_kw(ex) end params = Expr(:parameters, kws...) - def_call_ex = Expr(:call, kw_fn_name, params) - block_call_ex = Expr(:call, u, xs...) + def_call_ex = Expr(:call, esc(kw_fn_name), params) + block_call_ex = Expr(:call, esc(u), xs...) body = Expr(:block, block_call_ex) Expr(:function, def_call_ex, body) diff --git a/test/utils_test.jl b/test/utils_test.jl index c74154be7..7c134d2e2 100644 --- a/test/utils_test.jl +++ b/test/utils_test.jl @@ -142,7 +142,6 @@ end end @variables u(..) - @to_kw u(t, x1, x2) @test isequal(u_kw(t=0), u(0, x1, x2))