MTK : equations, unkowns and initial cond different lengths

Hello,

I am playing with a toy problem and I have a query with internal equation generation.

The following will fail


using ModelingToolkit
using ModelingToolkit: t_nounits as t, D_nounits as D


@component function my_func(;name)
    para = @parameters begin
        p(t)
    end
    vars = @variables begin
        x(t),
        y(t)
    end
    eqs = [
        x^2 ~ y^2 - p^2,
    ]
    return System(eqs,t,vars,para,name=name)
end

@named sys = my_func()
sys_ = mtkcompile(sys,fully_determined = false,simplify = true)

u0 = [sys_.y => 1.0,sys_.x => 0]
para = [sys_.p => 0]

prob = NonlinearProblem(sys_,merge(Dict(u0),Dict(para)),guesses = [sys_.y => 1.0,sys_.x => 0])

This is because

ERROR: LoadError: ArgumentError: Equations (1), unknowns (2), and initial conditions (2) are of different lengths.

Now to solve this I provide the same equation again to the system and it works. As follows:

using ModelingToolkit
using ModelingToolkit: t_nounits as t, D_nounits as D


@component function my_func(;name)
    para = @parameters begin
        p(t)
    end
    vars = @variables begin
        x(t),
        y(t)
    end
    eqs = [
        x^2 ~ y^2 - p^2,
        x^2 - y^2 ~ -p^2
    ]
    return System(eqs,t,vars,para,name=name)
end

@named sys = my_func()
sys_ = mtkcompile(sys,fully_determined = false,simplify = true)

u0 = [sys_.y => 1.0,sys_.x => 0]
para = [sys_.p => 0]

prob = NonlinearProblem(sys_,merge(Dict(u0),Dict(para)),guesses = [sys_.y => 1.0,sys_.x => 0])
using NonlinearSolve
sol = solve(prob,NewtonRaphson())

This works returning the expected solution

retcode: Success
u: 2-element Vector{Float64}:
 0.0
 4.76837158203125e-7

Is there a way without giving the second equation? I know there are infinite solutions to this problem but for a fix y there are at most 2 solutions for x.

There will be cases in many applications where there will be way more than 2 unknows so providing the remaining trivial equations seems a bit inefficient.

Thank you

A NonlinearProblem requires a consistent system. You can use NonlinearLeastSquaresProblem instead for over and underdetermined systems, i.e.

prob = NonlinearLeastSquaresProblem(sys_,merge(Dict(u0),Dict(para)),guesses = [sys_.y => 1.0,sys_.x => 0])
using NonlinearSolve
sol = solve(prob)

should work fine.

Upon following the suggestion I get the following error:

ERROR: `NonlinearFunction` requires a time-independent system.


To disable this check, pass `check_compatibility = false`.

It persists even when I pass check_compatibility = false.

I am using the following versions:

  [961ee093] ModelingToolkit v10.1.0
  [8913a72c] NonlinearSolve v4.9.0

Here is the code block for reference:

using ModelingToolkit
using ModelingToolkit: t_nounits as t, D_nounits as D


@component function my_func(;name)
    para = @parameters begin
        p(t)
    end
    vars = @variables begin
        x(t),
        y(t)
    end
    eqs = [
        x^2 ~ y^2 - p^2,

    ]
    return System(eqs,t,vars,para,name=name)
end

@named sys = my_func()
sys_ = mtkcompile(sys,fully_determined = false)

u0 = []
para = [sys_.p => 2]

using NonlinearSolve
prob = NonlinearLeastSquaresProblem(sys_,merge(Dict(u0),Dict(para)),guesses = [sys_.y => 1.0,sys_.x => 0],check_compatibility = false)
sol = solve(prob)

@cryptic.ax issue with the new MTK v10? We should allow this as an alternative steady state definition.

But for now, removing the t-dependence is fine.

using ModelingToolkit
using ModelingToolkit: t_nounits as t, D_nounits as D

@component function my_func(;name)
    para = @parameters begin
        p
    end
    vars = @variables begin
        x,
        y
    end
    eqs = [
        x^2 ~ y^2 - p^2,
    ]
    return System(eqs,vars,para,name=name)
end

@named sys = my_func()
sys_ = mtkcompile(sys,fully_determined = false)

using NonlinearSolve
prob = NonlinearLeastSquaresProblem(sys_,[sys_.y => 1.0,sys_.x => 0, sys_.p => 2])
sol = solve(prob)

We have NonlinearSystem(sys::System) for this reason - it converts the time-dependent sys into the time-independent steady-state version.

Okay that works too

If I understand this correctly, if I have a time dependent system after structural simplification I need to do one more step of using NonlinearSystem and pass that to NonlinearLeastSquaresProblem?

Yes we should document this.