SciML / ModelingToolkit.jl

An acausal modeling framework for automatically parallelized scientific machine learning (SciML) in Julia. A computer algebra system for integrated symbolics for physics-informed machine learning and automated transformations of differential equations
https://mtk.sciml.ai/dev/
Other
1.43k stars 209 forks source link

The nonlinear solver failed with the return code MAXITERS_EXCEED... #1351

Closed jake484 closed 2 years ago

jake484 commented 2 years ago

image

this is my system, I think I built system with no errors.

include("components.jl")

system = []

@named pumpA = Pump(w=1.0E3);push!(system,pumpA)
@named pumpB = Pump(w=0.5E3);push!(system,pumpB)
@named pumpC = Pump(w=0.2E3);push!(system,pumpC)

@named pipe1 = SimplePipe(L=1.0);push!(system,pipe1)
@named pipe2 = SimplePipe(L=10.0);push!(system,pipe2)
@named pipe3 = SimplePipe(L=1.0);push!(system,pipe3)
@named pipe4 = SimplePipe(L=10.0);push!(system,pipe4)
@named pipe5 = SimplePipe(L=5.0);push!(system,pipe5)
@named pipe6 = SimplePipe(L=1.0);push!(system,pipe6)
@named pipe7 = SimplePipe(L=5.0);push!(system,pipe7)
@named pipe8 = SimplePipe(L=1.0);push!(system,pipe8)
@named pipe9 = SimplePipe(L=10.0);push!(system,pipe9)
@named pipe10 = SimplePipe(L=1.0);push!(system,pipe10)

@named sink1 = Presure(p=1.0E3);push!(system,sink1)
@named sink2 = Presure(p=1.0E3);push!(system,sink2)

eqs = [
        connect(pumpA.port, pipe1.input)
        connect(pipe1.output, pipe2.input,pipe4.input)
        connect(pipe2.output, pipe3.output,pipe5.input)
        connect(pumpB.port, pipe3.input)
        connect(pipe5.output, pipe6.input,pipe7.input)
        connect(pipe6.output, sink1.port)
        connect(pipe4.output, pipe9.output,pipe8.input)
        connect(pipe7.output, pipe10.output,pipe9.input)
        connect(pipe10.input, pumpC.port)
        connect(pipe8.output, sink2.port)
      ]

@named model = ODESystem(eqs, t, systems=system)
sys = structural_simplify(model)
prob = ODAEProblem(sys, [], (0, 1.0),[])
sol = solve(prob,Tsit5())
display(sol)

when solving,error comes.

ERROR: LoadError: The nonlinear solver failed with the return code MAXITERS_EXCEED.

it's something wrong with solver? or I chose an unsuitable solver?

YingboMa commented 2 years ago

The inlined nonlinear solver failed to converge, so you should try to change the default values.

Also, please provide a MWE with a full stack trace. There is not enough information to say anything here, so I am going to close this issue for now.

jake484 commented 2 years ago

oh, thanks for your quick reply.

it's like this. To be honest, it's too complicated for me. When I saw those traces, I lost, hahaha.

Stacktrace:
  [1] error(s::String)  
    @ Base .\error.jl:33
  [2] nlsolve_failure(rc::Symbol)
    @ ModelingToolkit.StructuralTransformations C:\Users\DELL\.julia\packages\ModelingToolkit\iEq0Q\src\structural_transformation\utils.jl:343
  [3] numerical_nlsolve
    @ C:\Users\DELL\.julia\packages\ModelingToolkit\iEq0Q\src\structural_transformation\utils.jl:349 [inlined]
  [4] macro expansion
    @ C:\Users\DELL\.julia\packages\ModelingToolkit\iEq0Q\src\structural_transformation\codegen.jl:154 [inlined]
  [5] macro expansion
    @ C:\Users\DELL\.julia\packages\SymbolicUtils\2UXNG\src\code.jl:283 [inlined]
  [6] macro expansion
    @ C:\Users\DELL\.julia\packages\RuntimeGeneratedFunctions\KrkGo\src\RuntimeGeneratedFunctions.jl:129 [inlined]
  [7] macro expansion
    @ .\none:0 [inlined]
  [8] generated_callfunc
    @ .\none:0 [inlined]
  [9] (::RuntimeGeneratedFunctions.RuntimeGeneratedFunction{(Symbol("##out#311"), Symbol("##arg#312"), Symbol("##arg#313"), :t), ModelingToolkit.StructuralTransformations.var"#_RGF_ModTag", ModelingToolkit.StructuralTransformations.var"#_RGF_ModTag", (0xbf50b90b, 0xedd7555e, 0x6c31a0c5, 0x31b1ef29, 0xf6b5f9f1)})(::Vector{Float64}, ::Vector{Float64}, ::Vector{Float64}, ::Float64)
    @ RuntimeGeneratedFunctions C:\Users\DELL\.julia\packages\RuntimeGeneratedFunctions\KrkGo\src\RuntimeGeneratedFunctions.jl:117
 [10] ODEFunction
    @ C:\Users\DELL\.julia\packages\SciMLBase\x3z0g\src\scimlfunctions.jl:334 [inlined]
 [11] initialize!(integrator::OrdinaryDiffEq.ODEIntegrator{Tsit5{typeof(OrdinaryDiffEq.trivial_limiter!), typeof(OrdinaryDiffEq.trivial_limiter!), Static.False}, true, Vector{Float64}, Nothing, Float64, Vector{Float64}, Float64, Float64, Float64, Float64, Vector{Vector{Float64}}, ODESolution{Float64, 2, Vector{Vector{Float64}}, Nothing, Nothing, Vector{Float64}, Vector{Vector{Vector{Float64}}}, ODEProblem{Vector{Float64}, Tuple{Float64, Float64}, true, Vector{Float64}, ODEFunction{true, RuntimeGeneratedFunctions.RuntimeGeneratedFunction{(Symbol("##out#311"), Symbol("##arg#312"), Symbol("##arg#313"), :t), ModelingToolkit.StructuralTransformations.var"#_RGF_ModTag", ModelingToolkit.StructuralTransformations.var"#_RGF_ModTag", (0xbf50b90b, 0xedd7555e, 0x6c31a0c5, 0x31b1ef29, 0xf6b5f9f1)}, LinearAlgebra.UniformScaling{Bool}, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, SparseArrays.SparseMatrixCSC{Bool, Int64}, Nothing, Nothing, Nothing, Vector{Symbol}, Nothing, ModelingToolkit.StructuralTransformations.var"#generated_observed#55"{Bool, ODESystem, Dict{Any, Any}}, Nothing}, Base.Iterators.Pairs{Union{}, Union{}, Tuple{}, NamedTuple{(), Tuple{}}}, SciMLBase.StandardODEProblem}, Tsit5{typeof(OrdinaryDiffEq.trivial_limiter!), typeof(OrdinaryDiffEq.trivial_limiter!), Static.False}, OrdinaryDiffEq.InterpolationData{ODEFunction{true, RuntimeGeneratedFunctions.RuntimeGeneratedFunction{(Symbol("##out#311"), Symbol("##arg#312"), Symbol("##arg#313"), :t), ModelingToolkit.StructuralTransformations.var"#_RGF_ModTag", ModelingToolkit.StructuralTransformations.var"#_RGF_ModTag", (0xbf50b90b, 0xedd7555e, 0x6c31a0c5, 0x31b1ef29, 0xf6b5f9f1)}, LinearAlgebra.UniformScaling{Bool}, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, SparseArrays.SparseMatrixCSC{Bool, Int64}, Nothing, Nothing, Nothing, Vector{Symbol}, Nothing, ModelingToolkit.StructuralTransformations.var"#generated_observed#55"{Bool, ODESystem, Dict{Any, Any}}, Nothing}, Vector{Vector{Float64}}, Vector{Float64}, Vector{Vector{Vector{Float64}}}, OrdinaryDiffEq.Tsit5Cache{Vector{Float64}, Vector{Float64}, Vector{Float64}, OrdinaryDiffEq.Tsit5ConstantCache{Float64, Float64}, typeof(OrdinaryDiffEq.trivial_limiter!), typeof(OrdinaryDiffEq.trivial_limiter!), Static.False}}, DiffEqBase.DEStats}, ODEFunction{true, RuntimeGeneratedFunctions.RuntimeGeneratedFunction{(Symbol("##out#311"), Symbol("##arg#312"), Symbol("##arg#313"), :t), ModelingToolkit.StructuralTransformations.var"#_RGF_ModTag", ModelingToolkit.StructuralTransformations.var"#_RGF_ModTag", (0xbf50b90b, 0xedd7555e, 0x6c31a0c5, 0x31b1ef29, 0xf6b5f9f1)}, LinearAlgebra.UniformScaling{Bool}, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, SparseArrays.SparseMatrixCSC{Bool, Int64}, Nothing, Nothing, Nothing, Vector{Symbol}, Nothing, ModelingToolkit.StructuralTransformations.var"#generated_observed#55"{Bool, ODESystem, Dict{Any, Any}}, Nothing}, OrdinaryDiffEq.Tsit5Cache{Vector{Float64}, Vector{Float64}, Vector{Float64}, OrdinaryDiffEq.Tsit5ConstantCache{Float64, Float64}, typeof(OrdinaryDiffEq.trivial_limiter!), typeof(OrdinaryDiffEq.trivial_limiter!), Static.False}, OrdinaryDiffEq.DEOptions{Float64, Float64, Float64, Float64, PIController{Rational{Int64}}, typeof(DiffEqBase.ODE_DEFAULT_NORM), typeof(LinearAlgebra.opnorm), Nothing, CallbackSet{Tuple{}, Tuple{}}, typeof(DiffEqBase.ODE_DEFAULT_ISOUTOFDOMAIN), typeof(DiffEqBase.ODE_DEFAULT_PROG_MESSAGE), typeof(DiffEqBase.ODE_DEFAULT_UNSTABLE_CHECK), DataStructures.BinaryHeap{Float64, DataStructures.FasterForward}, DataStructures.BinaryHeap{Float64, DataStructures.FasterForward}, Nothing, Nothing, Int64, Tuple{}, Tuple{}, Tuple{}}, Vector{Float64}, Float64, Nothing, OrdinaryDiffEq.DefaultInit}, 
cache::OrdinaryDiffEq.Tsit5Cache{Vector{Float64}, Vector{Float64}, Vector{Float64}, OrdinaryDiffEq.Tsit5ConstantCache{Float64, Float64}, typeof(OrdinaryDiffEq.trivial_limiter!), typeof(OrdinaryDiffEq.trivial_limiter!), Static.False})
    @ OrdinaryDiffEq C:\Users\DELL\.julia\packages\OrdinaryDiffEq\JsAS0\src\perform_step\low_order_rk_perform_step.jl:627
 [12] __init(prob::ODEProblem{Vector{Float64}, Tuple{Float64, Float64}, true, Vector{Float64}, ODEFunction{true, RuntimeGeneratedFunctions.RuntimeGeneratedFunction{(Symbol("##out#311"), Symbol("##arg#312"), Symbol("##arg#313"), :t), ModelingToolkit.StructuralTransformations.var"#_RGF_ModTag", ModelingToolkit.StructuralTransformations.var"#_RGF_ModTag", (0xbf50b90b, 0xedd7555e, 0x6c31a0c5, 0x31b1ef29, 0xf6b5f9f1)}, LinearAlgebra.UniformScaling{Bool}, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, SparseArrays.SparseMatrixCSC{Bool, Int64}, Nothing, Nothing, Nothing, Vector{Symbol}, Nothing, ModelingToolkit.StructuralTransformations.var"#generated_observed#55"{Bool, ODESystem, Dict{Any, Any}}, Nothing}, Base.Iterators.Pairs{Union{}, Union{}, Tuple{}, NamedTuple{(), Tuple{}}}, SciMLBase.StandardODEProblem}, alg::Tsit5{typeof(OrdinaryDiffEq.trivial_limiter!), typeof(OrdinaryDiffEq.trivial_limiter!), Static.False}, timeseries_init::Tuple{}, ts_init::Tuple{}, ks_init::Tuple{}, recompile::Type{Val{true}}; saveat::Tuple{}, tstops::Tuple{}, d_discontinuities::Tuple{}, save_idxs::Nothing, save_everystep::Bool, save_on::Bool, save_start::Bool, save_end::Nothing, callback::Nothing, dense::Bool, calck::Bool, dt::Float64, dtmin::Nothing, dtmax::Float64, force_dtmin::Bool, adaptive::Bool, gamma::Rational{Int64}, abstol::Nothing, reltol::Nothing, qmin::Rational{Int64}, qmax::Int64, qsteady_min::Int64, qsteady_max::Int64, beta1::Nothing, beta2::Nothing, qoldinit::Rational{Int64}, controller::Nothing, fullnormalize::Bool, failfactor::Int64, maxiters::Int64, internalnorm::typeof(DiffEqBase.ODE_DEFAULT_NORM), internalopnorm::typeof(LinearAlgebra.opnorm), isoutofdomain::typeof(DiffEqBase.ODE_DEFAULT_ISOUTOFDOMAIN), unstable_check::typeof(DiffEqBase.ODE_DEFAULT_UNSTABLE_CHECK), verbose::Bool, timeseries_errors::Bool, dense_errors::Bool, advance_to_tstop::Bool, stop_at_next_tstop::Bool, initialize_save::Bool, progress::Bool, progress_steps::Int64, progress_name::String, progress_message::typeof(DiffEqBase.ODE_DEFAULT_PROG_MESSAGE), userdata::Nothing, allow_extrapolation::Bool, initialize_integrator::Bool, alias_u0::Bool, alias_du0::Bool, initializealg::OrdinaryDiffEq.DefaultInit, kwargs::Base.Iterators.Pairs{Union{}, Union{}, Tuple{}, NamedTuple{(), Tuple{}}})
    @ OrdinaryDiffEq C:\Users\DELL\.julia\packages\OrdinaryDiffEq\JsAS0\src\solve.jl:456
 [13] __init(prob::ODEProblem{Vector{Float64}, Tuple{Float64, Float64}, true, Vector{Float64}, ODEFunction{true, RuntimeGeneratedFunctions.RuntimeGeneratedFunction{(Symbol("##out#311"), Symbol("##arg#312"), Symbol("##arg#313"), :t), ModelingToolkit.StructuralTransformations.var"#_RGF_ModTag", ModelingToolkit.StructuralTransformations.var"#_RGF_ModTag", (0xbf50b90b, 0xedd7555e, 0x6c31a0c5, 0x31b1ef29, 0xf6b5f9f1)}, LinearAlgebra.UniformScaling{Bool}, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, SparseArrays.SparseMatrixCSC{Bool, Int64}, Nothing, Nothing, Nothing, Vector{Symbol}, Nothing, ModelingToolkit.StructuralTransformations.var"#generated_observed#55"{Bool, ODESystem, Dict{Any, Any}}, Nothing}, Base.Iterators.Pairs{Union{}, Union{}, Tuple{}, NamedTuple{(), Tuple{}}}, SciMLBase.StandardODEProblem}, alg::Tsit5{typeof(OrdinaryDiffEq.trivial_limiter!), typeof(OrdinaryDiffEq.trivial_limiter!), Static.False}, timeseries_init::Tuple{}, ts_init::Tuple{}, ks_init::Tuple{}, recompile::Type{Val{true}}) (repeats 5 times)
    @ OrdinaryDiffEq C:\Users\DELL\.julia\packages\OrdinaryDiffEq\JsAS0\src\solve.jl:67
 [14] #__solve#493
    @ C:\Users\DELL\.julia\packages\OrdinaryDiffEq\JsAS0\src\solve.jl:4 [inlined]
 [15] __solve
    @ C:\Users\DELL\.julia\packages\OrdinaryDiffEq\JsAS0\src\solve.jl:4 [inlined]
 [16] #solve_call#42
    @ C:\Users\DELL\.julia\packages\DiffEqBase\b1nST\src\solve.jl:61 [inlined]
 [17] solve_call
    @ C:\Users\DELL\.julia\packages\DiffEqBase\b1nST\src\solve.jl:48 [inlined]
 [18] #solve_up#44
    @ C:\Users\DELL\.julia\packages\DiffEqBase\b1nST\src\solve.jl:87 [inlined]
 [19] solve_up
    @ C:\Users\DELL\.julia\packages\DiffEqBase\b1nST\src\solve.jl:78 [inlined]
 [20] #solve#43
    @ C:\Users\DELL\.julia\packages\DiffEqBase\b1nST\src\solve.jl:73 [inlined]
 [21] solve(prob::ODEProblem{Vector{Float64}, Tuple{Float64, Float64}, true, Vector{Float64}, ODEFunction{true, RuntimeGeneratedFunctions.RuntimeGeneratedFunction{(Symbol("##out#311"), Symbol("##arg#312"), Symbol("##arg#313"), :t), ModelingToolkit.StructuralTransformations.var"#_RGF_ModTag", ModelingToolkit.StructuralTransformations.var"#_RGF_ModTag", (0xbf50b90b, 0xedd7555e, 0x6c31a0c5, 0x31b1ef29, 0xf6b5f9f1)}, LinearAlgebra.UniformScaling{Bool}, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, 
SparseArrays.SparseMatrixCSC{Bool, Int64}, Nothing, Nothing, Nothing, Vector{Symbol}, Nothing, ModelingToolkit.StructuralTransformations.var"#generated_observed#55"{Bool, ODESystem, Dict{Any, Any}}, Nothing}, Base.Iterators.Pairs{Union{}, Union{}, Tuple{}, NamedTuple{(), Tuple{}}}, SciMLBase.StandardODEProblem}, args::Tsit5{typeof(OrdinaryDiffEq.trivial_limiter!), typeof(OrdinaryDiffEq.trivial_limiter!), Static.False})
    @ DiffEqBase C:\Users\DELL\.julia\packages\DiffEqBase\b1nST\src\solve.jl:68
 [22] top-level scope

and components

using ModelingToolkit, DifferentialEquations
using Random
# Basic  components
@parameters t
@connector function Pin(;name)
    @variables Pressure(t),Flow(t)
    ODESystem(Equation[], t, [Pressure, Flow], [], name=name, defaults=[Pressure=>1.0, Flow=>1.0])
end

function ModelingToolkit.connect(::Type{Pin}, ps...)
    eqs = [
           0 ~ sum(p->p.Flow, ps) # KCL
          ]
    # KVL
    for i in 1:length(ps)-1
        push!(eqs, ps[i].Pressure ~ ps[i+1].Pressure)
    end
    return eqs
end

# 管道长 L ~ m
# 管道内径 D ~ m
# 管道摩擦因数f 
# 压降ΔP
# 流体密度 ρ ~kg/m³
# SimplePipe为固定摩擦因数管道组件
function SimplePipe(;name, L = 100.0, D = 25E-3, f = 0.01, ρ = 1.0E3)
    val =  f*L*ρ*8/(D^5*π^2)
    @named input = Pin()
    @named output = Pin()
    @variables ΔP(t)
    @parameters R
    eqs = [
           ΔP ~ input.Pressure - output.Pressure
           0 ~ input.Flow + output.Flow
           ΔP ~ input.Flow^2 * R
          ]
    ODESystem(eqs, t, [ΔP], [R], systems=[input, output],defaults=Dict(R =>val), name=name)
end

function Presure(;name,p=10E6,dp=0)
    @named port = Pin()
    @variables P(t)   
    D = Differential(t)
    eqs = [
        port.Pressure ~ p + dp * P
        D(P) ~ sin(t)
    ]
    ODESystem(eqs, t, [P], [], systems=[port],defaults=Dict(P =>0),name=name)
end

"""
Pump(;name,w=1.0E3,η=1.0,k_disturbance=0)

"""
function Pump(;name,w=1.0E3,η=0.8,k_disturbance=0)
    @named port = Pin()
    @variables Disturbance(t)   
    D = Differential(t)
    eqs = [
        port.Pressure * port.Flow ~ w * (η + Disturbance*rand())
        D(Disturbance) ~ k_disturbance * cos(t)  
    ]
    ODESystem(eqs, t, [Disturbance], [], systems=[port],defaults=Dict(Disturbance =>0.5),name=name)
end