Defining an array of MTK variables causes error with ModelingToolkit.discretize()

I am trying to solve a number of ODEs for a set of variables (u_1, u_2, ..., u_6) using MethodOfLines.jl, but I get this error

MethodError: no method matching nameof(::SymbolicUtils.Term{SymbolicUtils.FnType{Tuple, Real}, Nothing})

Closest candidates are:

nameof(!Matched::SymbolicUtils.Sym) at C:\Users\User\.julia\packages\SymbolicUtils\v2ZkM\src\types.jl:144

nameof(!Matched::ModelingToolkit.AbstractSystem) at C:\Users\User\.julia\packages\ModelingToolkit\Uaky4\src\systems\abstractsystem.jl:140

nameof(!Matched::DataType) at C:\Users\User\AppData\Local\Programs\Julia-1.7.0\share\julia\base\reflection.jl:223


(::MethodOfLines.var"#12#27"{Nothing})(::SymbolicUtils.Term{Real, Base.ImmutableDict{DataType, Any}})@discretize_vars.jl:43
_collect(::Vector{Any}, ::Base.Generator{Vector{Any}, MethodOfLines.var"#12#27"{Nothing}}, ::Base.EltypeUnknown, ::Base.HasShape{1})@array.jl:744
collect_similar(::Vector{Any}, ::Base.Generator{Vector{Any}, MethodOfLines.var"#12#27"{Nothing}})@array.jl:653
map(::Function, ::Vector{Any})@abstractarray.jl:2849
MethodOfLines.DiscreteSpace(::Vector{Symbolics.VarDomainPairing}, ::Vector{Any}, ::Vector{SymbolicUtils.Sym{Real, Base.ImmutableDict{DataType, Any}}}, ::MethodOfLines.MOLFiniteDifference{MethodOfLines.CenterAlignedGrid})@discretize_vars.jl:41
symbolic_discretize(::ModelingToolkit.PDESystem, ::MethodOfLines.MOLFiniteDifference{MethodOfLines.CenterAlignedGrid})@MOL_discretization.jl:37
discretize(::ModelingToolkit.PDESystem, ::MethodOfLines.MOLFiniteDifference{MethodOfLines.CenterAlignedGrid})@MOL_discretization.jl:120
top-level scope@Local: 45[inlined]

The error doesn’t show up, if I name the variables individually (@variables u_1, u_2, ..., u_6). But I have a lot of functions that need to be iterated over to define the equations and boundary conditions, and that would not be convenient.

Any idea how I can deal with this?

What does your code look like?

Sorry, I thought I had it there. Here it is

	# Dependencies
	using DiffEqOperators, MethodOfLines, OrdinaryDiffEq, ModelingToolkit, DomainSets, NonlinearSolve

	N = 6 # number of dependent variables
	# Variables, parameters, and derivatives
	@parameters x
	@variables u[1:N](..)
	Dx = Differential(x)
	Dxx = Differential(x)^2
	# Domain edges
	x_min= 0.
	x_max = 1.

	# Discretization parameters
	dx = 0.1
	order = 2
	# Equations
	eqs  = Vector{ModelingToolkit.Equation}(undef, N)
	for i = 1:N
		eqs[i] = Dxx(u[i](x)) ~ u[i](x)

	# Initial and boundary conditions
	bcs = Vector{ModelingToolkit.Equation}(undef, 2*N)
	for i = 1:N
		bcs[i] = Dx(u[i](x_min)) ~ 0.
	for i = 1:N
		bcs[i+N] = u[i](x_max) ~ rand()
	# Space and time domains
	domains = [x ∈ Interval(x_min, x_max)]
	# PDE system
	@named pdesys = PDESystem(eqs, bcs, domains, [x], [u[i](x) for i = 1:N])
	# Method of lines discretization
	discretization = MOLFiniteDifference([x=>dx], nothing, approx_order=order)
	prob = ModelingToolkit.discretize(pdesys,discretization)
	# # Solution of the ODE system
	sol = NonlinearSolve.solve(prob,NewtonRaphson())

Oh I see now. Open an issue on MethodOfLines.jl. This is not related to MTK itself.

Ok. I opened an issue
Defining an array of variables causes error with ModelingToolkit.discretize() · Issue #43 · SciML/MethodOfLines.jl (