SciML / MethodOfLines.jl

Automatic Finite Difference PDE solving with Julia SciML
https://docs.sciml.ai/MethodOfLines/stable/
MIT License
167 stars 31 forks source link

Remake with different parameter values broke in 0.11.6 #433

Closed kkakosim closed 3 days ago

kkakosim commented 3 days ago

Describe the example the example of Remake with different parameter values broke in version 0.11.6 with neither the new or old methods failing

Minimal Reproducible Example 👇 copied from the tutorial for reference

using ModelingToolkit, MethodOfLines, OrdinaryDiffEq, DomainSets
using SciMLStructures: replace!, Tunable

@parameters t x
@parameters Dn, Dp
@variables u(..) v(..)
Dt = Differential(t)
Dx = Differential(x)
Dxx = Differential(x)^2

eqs = [Dt(u(t, x)) ~ Dn * Dxx(u(t, x)) + u(t, x) * v(t, x),
    Dt(v(t, x)) ~ Dp * Dxx(v(t, x)) - u(t, x) * v(t, x)]
bcs = [u(0, x) ~ sin(pi * x / 2),
    v(0, x) ~ sin(pi * x / 2),
    u(t, 0) ~ 0.0, Dx(u(t, 1)) ~ 0.0,
    v(t, 0) ~ 0.0, Dx(v(t, 1)) ~ 0.0]

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

@named pdesys = PDESystem(
    eqs, bcs, domains, [t, x], [u(t, x), v(t, x)],
    [Dn, Dp]; defaults = Dict(Dn => 0.5, Dp => 2.0))

discretization = MOLFiniteDifference([x => 0.1], t)

prob = discretize(pdesys, discretization) # This gives an ODEProblem since it's time-dependent

sols = []
for (Dnval, Dpval) in zip(rand(10), rand(10))
    newprob = remake(prob)
    replace!(Tunable(), newprob.p, [Dnval, Dpval])
    push!(sols, solve(newprob, Tsit5()))
end

using Plots
for (j, sol) in enumerate(sols)
    discrete_x = sol[x]
    discrete_t = sol[t]
    solu = sol[u(t, x)]
    solv = sol[v(t, x)]
    anim = @animate for i in 1:length(discrete_t)
        p1 = plot(discrete_x, solu[i, :], label = "u, t=$(discrete_t[i])";
            legend = false, xlabel = "x", ylabel = "u", ylim = [0, 1])
        p2 = plot(discrete_x, solv[i, :], label = "v, t=$(discrete_t[i])";
            legend = false, xlabel = "x", ylabel = "v", ylim = [0, 1])
        plot(p1, p2)
    end
    gif(anim, "plot_$j.gif", fps = 10)
end

Error & Stacktrace ⚠️

ERROR: MethodError: no method matching replace!(::Tunable, ::MTKParameters{Vector{Float64}, Tuple{}, Tuple{}, Tuple{}}, ::Vector{Float64})
The function `replace!` exists, but no method is defined for this combination of argument types.

Closest candidates are:
  replace!(::Any, ::Pair...; count)
   @ Base set.jl:735
  replace!(::Union{Function, Type}, ::Any; count)
   @ Base set.jl:777
  replace!(::SentinelArrays.ChainedVector, ::Pair...; count)
   @ SentinelArrays C:\Users\kkako\.julia\packages\SentinelArrays\ob2QK\src\chainedvector.jl:985

Not Working Environment (please complete the following information):

  [6e4b80f9] BenchmarkTools v1.5.0
  [336ed68f] CSV v0.10.15
  [a93c6f00] DataFrames v1.7.0
  [0c46a032] DifferentialEquations v7.15.0
  [5b8099bc] DomainSets v0.7.14
  [f6369f11] ForwardDiff v0.10.38
  [a98d9a8b] Interpolations v0.15.1
  [4345ca2d] Loess v0.6.4
  [30fc2ffe] LossFunctions v0.11.2
  [2fda8390] LsqFit v0.15.0
  [94925ecb] MethodOfLines v0.11.6
  [961ee093] ModelingToolkit v9.50.0
⌅ [8913a72c] NonlinearSolve v3.15.1
  [429524aa] Optim v1.10.0
  [7f7a1694] Optimization v4.0.5
  [fd9f6733] OptimizationMOI v0.5.1
  [4e6fcdb7] OptimizationNLopt v0.3.1
  [2cab0595] OptimizationNOMAD v0.3.0
  [36348300] OptimizationOptimJL v0.4.1
  [1dea7af3] OrdinaryDiffEq v6.90.1
  [58dd65bb] Plotly v0.4.1
  [a03496cd] PlotlyBase v0.8.19
  [f2990250] PlotlyKaleido v2.2.5
  [91a5bcdd] Plots v1.40.9
  [274fc56d] PythonPlot v1.0.5
  [53ae85a6] SciMLStructures v1.5.0
  [0c5d862f] Symbolics v6.19.0
  [fdbf4ff8] XLSX v0.10.4
Julia Version 1.11.1
Commit 8f5b7ca12a (2024-10-16 10:53 UTC)
Build Info:
  Official https://julialang.org/ release
Platform Info:
  OS: Windows (x86_64-w64-mingw32)
  CPU: 24 × 12th Gen Intel(R) Core(TM) i9-12900HX
  WORD_SIZE: 64
  LLVM: libLLVM-16.0.6 (ORCJIT, alderlake)
Threads: 24 default, 0 interactive, 12 GC (on 24 virtual cores)
Environment:
  JULIA_EDITOR = code
  JULIA_NUM_THREADS = 24

Additional context

the previous approach of remake 👇 doesn't work as well

newprob = remake(prob, p = [Dnval, Dpval])

returning this error

ERROR: BoundsError: attempt to access Float64 at index [2]
Stacktrace:
  [1] getindex
    @ .\number.jl:98 [inlined]
  [2] macro expansion
    @ C:\Users\kkako\.julia\packages\SymbolicUtils\jf8aQ\src\code.jl:387 [inlined]
  [3] macro expansion
    @ C:\Users\kkako\.julia\packages\RuntimeGeneratedFunctions\M9ZX8\src\RuntimeGeneratedFunctions.jl:163 [inlined]
  [4] macro expansion
    @ .\none:0 [inlined]
  [5] generated_callfunc
    @ .\none:0 [inlined]
  [6] (::RuntimeGeneratedFunctions.RuntimeGeneratedFunction{…})(::Vector{…}, ::Vector{…}, ::Float64)
    @ RuntimeGeneratedFunctions C:\Users\kkako\.julia\packages\RuntimeGeneratedFunctions\M9ZX8\src\RuntimeGeneratedFunctions.jl:150
  [7] (::ModelingToolkit.ObservedFunctionCache{…})(::SymbolicUtils.BasicSymbolic{…}, ::Vector{…}, ::Vararg{…})
    @ ModelingToolkit C:\Users\kkako\.julia\packages\ModelingToolkit\zfOUk\src\systems\abstractsystem.jl:1724
  [8] _broadcast_getindex_evalf
    @ .\broadcast.jl:673 [inlined]
  [9] _broadcast_getindex
    @ .\broadcast.jl:646 [inlined]
 [10] getindex
    @ .\broadcast.jl:605 [inlined]
 [11] copy
    @ .\broadcast.jl:906 [inlined]
 [12] materialize
    @ .\broadcast.jl:867 [inlined]
 [13] observed(A::ODESolution{…}, sym::SymbolicUtils.BasicSymbolic{…}, i::Colon)
    @ SciMLBase C:\Users\kkako\.julia\packages\SciMLBase\CMjVZ\src\solutions\solution_interface.jl:148
 [14] (::MethodOfLines.var"#142#144"{Vector{…}, ODESolution{…}, Vector{…}})(I::CartesianIndex{1})
    @ MethodOfLines C:\Users\kkako\.julia\packages\MethodOfLines\NV0bv\src\interface\solution\timedep.jl:37
 [15] iterate
    @ .\generator.jl:48 [inlined]
 [16] _collect(c::CartesianIndices{…}, itr::Base.Generator{…}, ::Base.EltypeUnknown, isz::Base.HasShape{…})
    @ Base .\array.jl:800
 [17] collect_similar(cont::CartesianIndices{…}, itr::Base.Generator{…})
    @ Base .\array.jl:709
 [18] map(f::Function, A::CartesianIndices{1, Tuple{Base.OneTo{Int64}}})
    @ Base .\abstractarray.jl:3371
 [19] (::MethodOfLines.var"#141#143"{…})(u::SymbolicUtils.BasicSymbolic{…})
    @ MethodOfLines C:\Users\kkako\.julia\packages\MethodOfLines\NV0bv\src\interface\solution\timedep.jl:31
 [20] _mapreduce(f::MethodOfLines.var"#141#143"{…}, op::typeof(vcat), ::IndexLinear, A::Vector{…})
    @ Base .\reduce.jl:437
 [21] _mapreduce_dim(f::Function, op::Function, ::Base._InitialValue, A::Vector{Any}, ::Colon)
    @ Base .\reducedim.jl:337
 [22] mapreduce(f::Function, op::Function, A::Vector{Any})
    @ Base .\reducedim.jl:329
 [23] SciMLBase.PDETimeSeriesSolution(sol::ODESolution{…}, metadata::MethodOfLines.MOLMetadata{…})
    @ MethodOfLines C:\Users\kkako\.julia\packages\MethodOfLines\NV0bv\src\interface\solution\timedep.jl:29
 [24] wrap_sol
    @ C:\Users\kkako\.julia\packages\SciMLBase\CMjVZ\src\solutions\pde_solutions.jl:115 [inlined]
 [25] wrap_sol
    @ C:\Users\kkako\.julia\packages\SciMLBase\CMjVZ\src\solutions\basic_solutions.jl:95 [inlined]
 [26] #solve#51
    @ C:\Users\kkako\.julia\packages\DiffEqBase\HW4ge\src\solve.jl:1036 [inlined]
 [27] solve(prob::ODEProblem{…}, args::Tsit5{…})
    @ DiffEqBase C:\Users\kkako\.julia\packages\DiffEqBase\HW4ge\src\solve.jl:1026
 [28] top-level scope
    @ e:\github\tamuq-chen-secarelab-receiver\aysha\remake_example.jl:37
Some type information was truncated. Use `show(err)` to see complete types.
kkakosim commented 3 days ago

it seems that v0.11.7 gets this fixed. I will let anyone from the contributors to close this issue in case this was just a coincidence.

ChrisRackauckas commented 3 days ago

It's not a coincidence.