@register multidimensional interpolation in ModelingToolkit

Hello,

I am trying to feed multidimensional interpolating functions into initial conditions to solve a system of multidimensional PDEs using NeuralPDE.jl. However, registering those functions as symbolic functions with @register keep failing.

A minimal reproducible code is:

using ModelingToolkit, Interpolations

@parameters x y z t
@variables P(..)

x_temp = [0,0.01, 30000]
y_temp = [0, 1.5, 1.51, 2.0,2.01, 20]
z_temp = [0, 300]

P_temp = zeros(3,6,2)
P_temp[1:2,3:4,:] .= 6.0*10^8

##following a tutorial in Interpolations.jl
nodes = (x_temp, y_temp, z_temp)
P_interp = interpolate(nodes, P_temp, Gridded(Linear()))

@register P_interp(x,y,z)

Then, the output is

cannot define function P_interp; it already has a value

Stacktrace:
  [1] top-level scope
    @ none:0
  [2] top-level scope
    @ none:1
  [3] eval(m::Module, e::Any)
    @ Core ./boot.jl:373
  [4] top-level scope
    @ ~/.julia/packages/Symbolics/DCeQ3/src/register.jl:79
  [5] eval
    @ ./boot.jl:373 [inlined]
  [6] include_string(mapexpr::typeof(REPL.softscope), mod::Module, code::String, filename::String)
    @ Base ./loading.jl:1196
  [7] #invokelatest#2
    @ ./essentials.jl:716 [inlined]
  [8] invokelatest
    @ ./essentials.jl:714 [inlined]
  [9] (::VSCodeServer.var"#164#165"{VSCodeServer.NotebookRunCellArguments, String})()
    @ VSCodeServer ~/.vscode/extensions/julialang.language-julia-1.6.17/scripts/packages/VSCodeServer/src/serve_notebook.jl:19
 [10] withpath(f::VSCodeServer.var"#164#165"{VSCodeServer.NotebookRunCellArguments, String}, path::String)
    @ VSCodeServer ~/.vscode/extensions/julialang.language-julia-1.6.17/scripts/packages/VSCodeServer/src/repl.jl:184
 [11] notebook_runcell_request(conn::VSCodeServer.JSONRPC.JSONRPCEndpoint{Base.PipeEndpoint, Base.PipeEndpoint}, params::VSCodeServer.NotebookRunCellArguments)
    @ VSCodeServer ~/.vscode/extensions/julialang.language-julia-1.6.17/scripts/packages/VSCodeServer/src/serve_notebook.jl:13
 [12] dispatch_msg(x::VSCodeServer.JSONRPC.JSONRPCEndpoint{Base.PipeEndpoint, Base.PipeEndpoint}, dispatcher::VSCodeServer.JSONRPC.MsgDispatcher, msg::Dict{String, Any})
    @ VSCodeServer.JSONRPC ~/.vscode/extensions/julialang.language-julia-1.6.17/scripts/packages/JSONRPC/src/typed.jl:67
 [13] serve_notebook(pipename::String, outputchannel_logger::Base.CoreLogging.SimpleLogger; crashreporting_pipename::String)
    @ VSCodeServer ~/.vscode/extensions/julialang.language-julia-1.6.17/scripts/packages/VSCodeServer/src/serve_notebook.jl:136
 [14] top-level scope
    @ ~/.vscode/extensions/julialang.language-julia-1.6.17/scripts/notebook/notebook.jl:32
 [15] include(mod::Module, _path::String)
    @ Base ./Base.jl:418
 [16] exec_options(opts::Base.JLOptions)
    @ Base ./client.jl:292
 [17] _start()
    @ Base ./client.jl:495

I searched through similar postings here or at the Issues of MTK github, but, those involve 1-d interpolating functions and don’t seem to be remedied.

In particular, I am using the package Interpolations for multidimensional interpolation.

Could anyone help me out for this?

Thanks a lot in advance!
Kyemyung

Interpolations returns a callable struct, @register only works for functions. I usually solve this by defining an intermediate function:

P_interp = interpolate(x_temp, y_temp, z_temp)
P_func(x, y, z) = P_interp(x, y, z)
@register_symbolic P_func(x, y, s)

And then use P_func in any symbolic expressions.

3 Likes

Thanks a lot! It worked!