ModelingToolkit.jl icon indicating copy to clipboard operation
ModelingToolkit.jl copied to clipboard

Warning: : no method matching get_unit for arguments (Pair{Num, Float64},).

Open mlenel opened this issue 3 years ago • 5 comments

When using scalarized vector parameters in PDESystem, get_unit in validation.jl throws a warning. Example code:

using OrdinaryDiffEq, ModelingToolkit, MethodOfLines, DomainSets
using Symbolics: scalarize
@parameters t x 
@parameters Dn[1:2]
@variables u(..)
Dt = Differential(t)
Dxx = Differential(x)^2

function testfun(a)
    return sum(a)
end

eq  = Dt(u(t, x)) ~ testfun(Dn)*Dxx(u(t, x))
bcs = [ u(0, x) ~ cos(x),
        u(t, 0) ~ exp(-t),
        u(t, 1) ~ exp(-t) * cos(1)]

domains = [t ∈ Interval(0.0, 1.0),
           x ∈ Interval(0.0, 1.0)]

@named pdesys = PDESystem(eq, bcs, domains, [t, x], [u(t, x)], scalarize(Dn) .=> [0.5, 0.0])

Warning:

 Warning: : no method matching get_unit for arguments (Pair{Num, Float64},).
└ @ ModelingToolkit ~/.julia/packages/ModelingToolkit/fp90z/src/systems/validation.jl:144
┌ Warning: : no method matching get_unit for arguments (Pair{Num, Float64},).
└ @ ModelingToolkit ~/.julia/packages/ModelingToolkit/fp90z/src/systems/validation.jl:144
PDESystem
Equations: Equation[Differential(t)(u(t, x)) ~ Differential(x)(Differential(x)(u(t, x)))*Symbolics._mapreduce(identity, +, Dn, Colon(), (:init => false,))]
Boundary Conditions: Equation[u(0, x) ~ cos(x), u(t, 0) ~ exp(-t), u(t, 1) ~ 0.5403023058681398exp(-t)]
Domain: Symbolics.VarDomainPairing[Symbolics.VarDomainPairing(t, 0.0..1.0), Symbolics.VarDomainPairing(x, 0.0..1.0)]
Dependent Variables: Num[u(t, x)]
Independent Variables: Num[t, x]
Parameters: Pair{Num, Float64}[Dn[1] => 0.5, Dn[2] => 0.0]
Default Parameter ValuesDict{Any, Any}()

Seems related but distinct from #1253

mlenel avatar Jun 21 '22 13:06 mlenel

That's weird... those warnings should have resulted in the validation throwing an error. I'll have a look.

lamorton avatar Aug 27 '22 16:08 lamorton

I see what's happening now:

  • all_dimensionless should be using get_unit instead of safe_get_unit so that this would have thrown an error rather than just printing a warning.
  • get_unit needs to be specialized for this new representation of the parameter vector ps that comes with parameter values.

Should be a quick fix.

lamorton avatar Aug 27 '22 20:08 lamorton

@YingboMa Is it supported to provide parameter values to a system constructor as a vector of pairs like this? I think the official way to do this one of the options below, but maybe I'm just behind the times.

@parameters Dn[1:2] = [0.5, 0.0]
#...
@named pdesys = PDESystem(eq, bcs, domains, [t, x], [u(t, x)], scalarize(Dn))

or

@named pdesys = PDESystem(eq, bcs, domains, [t, x], [u(t, x)], scalarize(Dn), defaults = Dict( scalarize(Dn) .=> [0.5, 0.0]))

lamorton avatar Aug 27 '22 22:08 lamorton

They should both work.

YingboMa avatar Sep 01 '22 14:09 YingboMa

I tried this as a test:

@parameters P(t) [unit = u"MW"]
@variables t [unit = u"ms"] E(t) [unit = u"kJ"] 
D = Differential(t)
eqs = [D(E) ~ P ,]
ODESystem(eqs, t, [P,], [τ => 1]; name = :sys)

And I got:

ERROR: MethodError: no method matching hasmetadata(::Pair{Num, Int64}, ::Type{Symbolics.VariableDefaultValue})
Closest candidates are:
  hasmetadata(::SymbolicUtils.Symbolic, ::Any) at C:\Users\lucas\.julia\packages\SymbolicUtils\qulQp\src\types.jl:15
  hasmetadata(::Complex{Num}, ::Any) at C:\Users\lucas\.julia\packages\Symbolics\J8IHJ\src\Symbolics.jl:155
  hasmetadata(::Num, ::Any) at C:\Users\lucas\.julia\packages\Symbolics\J8IHJ\src\Symbolics.jl:155
Stacktrace:
 [1] hasdefault(v::Pair{Num, Int64})
   @ ModelingToolkit c:\Users\lucas\Documents\Code\Jdev\ModelingToolkit\src\utils.jl:245
 [2] collect_defaults!
   @ c:\Users\lucas\Documents\Code\Jdev\ModelingToolkit\src\utils.jl:259 [inlined]
 [3] process_variables!(var_to_name::Dict{Any, Any}, defs::Dict{Any, Any}, vars::Vector{Pair{Num, Int64}})
   @ ModelingToolkit c:\Users\lucas\Documents\Code\Jdev\ModelingToolkit\src\utils.jl:252
 [4] ODESystem(deqs::Vector{Equation}, iv::Num, dvs::Vector{Num}, ps::Vector{Pair{Num, Int64}}; controls::Vector{Num}, observed::Vector{Equation}, systems::Vector{ODESystem}, name::Symbol, default_u0::Dict{Any, Any}, default_p::Dict{Any, Any}, defaults::Dict{Any, Any}, connector_type::Nothing, preface::Nothing, continuous_events::Nothing, discrete_events::Nothing, checks::Bool, metadata::Nothing)
   @ ModelingToolkit c:\Users\lucas\Documents\Code\Jdev\ModelingToolkit\src\systems\diffeqs\odesystem.jl:178
 [5] top-level scope
   @ REPL[5]:1

So I don't think what @mlenel is trying to do is generally supported... is it PDE-specific?

lamorton avatar Sep 03 '22 04:09 lamorton