Skip to content

Commit dde5b83

Browse files
Merge pull request #2631 from AayushSabharwal/as/var-discovery
feat: allow creating NonlinearSystem without specifying unknowns/parameters
2 parents 971359a + 326b2ca commit dde5b83

File tree

2 files changed

+35
-0
lines changed

2 files changed

+35
-0
lines changed

src/systems/nonlinear/nonlinearsystem.jl

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -153,6 +153,32 @@ function NonlinearSystem(eqs, unknowns, ps;
153153
connector_type, metadata, gui_metadata, checks = checks)
154154
end
155155

156+
function NonlinearSystem(eqs; kwargs...)
157+
eqs = collect(eqs)
158+
allunknowns = OrderedSet()
159+
ps = OrderedSet()
160+
for eq in eqs
161+
collect_vars!(allunknowns, ps, eq.lhs, nothing)
162+
collect_vars!(allunknowns, ps, eq.rhs, nothing)
163+
end
164+
new_ps = OrderedSet()
165+
for p in ps
166+
if istree(p) && operation(p) === getindex
167+
par = arguments(p)[begin]
168+
if Symbolics.shape(Symbolics.unwrap(par)) !== Symbolics.Unknown() &&
169+
all(par[i] in ps for i in eachindex(par))
170+
push!(new_ps, par)
171+
else
172+
push!(new_ps, p)
173+
end
174+
else
175+
push!(new_ps, p)
176+
end
177+
end
178+
179+
return NonlinearSystem(eqs, collect(allunknowns), collect(new_ps); kwargs...)
180+
end
181+
156182
function calculate_jacobian(sys::NonlinearSystem; sparse = false, simplify = false)
157183
cache = get_jac(sys)[]
158184
if cache isa Tuple && cache[2] == (sparse, simplify)

test/nonlinearsystem.jl

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -257,3 +257,12 @@ end
257257
sol = solve(prob)
258258
@test_nowarn sol[unknowns(ns)]
259259
end
260+
261+
# Issue#2625
262+
@parameters p d
263+
@variables X(t)
264+
alg_eqs = [0 ~ p - d * X]
265+
266+
sys = @test_nowarn NonlinearSystem(alg_eqs; name = :name)
267+
@test isequal(only(unknowns(sys)), X)
268+
@test all(isequal.(parameters(sys), [p, d]))

0 commit comments

Comments
 (0)