Skip to content

Commit

Permalink
refactor: format
Browse files Browse the repository at this point in the history
  • Loading branch information
AayushSabharwal committed Sep 16, 2024
1 parent f79f997 commit d85ed68
Show file tree
Hide file tree
Showing 6 changed files with 24 additions and 15 deletions.
3 changes: 2 additions & 1 deletion src/systems/abstractsystem.jl
Original file line number Diff line number Diff line change
Expand Up @@ -1280,7 +1280,8 @@ end
function is_array_of_symbolics(x)
symbolic_type(x) == ArraySymbolic() && return true
symbolic_type(x) == ScalarSymbolic() && return false
x isa AbstractArray && any(y -> symbolic_type(y) != NotSymbolic() || is_array_of_symbolics(y), x)
x isa AbstractArray &&
any(y -> symbolic_type(y) != NotSymbolic() || is_array_of_symbolics(y), x)
end

function namespace_expr(
Expand Down
10 changes: 6 additions & 4 deletions src/systems/diffeqs/abstractodesystem.jl
Original file line number Diff line number Diff line change
Expand Up @@ -854,13 +854,15 @@ function process_DEProblem(constructor, sys::AbstractODESystem, u0map, parammap;
end
end
defs = defaults(sys)
guesses = merge(ModelingToolkit.guesses(sys), isempty(guesses) ? Dict() : todict(guesses))
guesses = merge(
ModelingToolkit.guesses(sys), isempty(guesses) ? Dict() : todict(guesses))
solvablepars = [p
for p in parameters(sys)
if is_parameter_solvable(p, parammap, defs, guesses)]
for p in parameters(sys)
if is_parameter_solvable(p, parammap, defs, guesses)]
# ModelingToolkit.get_tearing_state(sys) !== nothing => Requires structural_simplify first
if sys isa ODESystem && build_initializeprob &&
(((implicit_dae || !isempty(missingvars) || !isempty(solvablepars) || !isempty(setobserved)) &&
(((implicit_dae || !isempty(missingvars) || !isempty(solvablepars) ||
!isempty(setobserved)) &&
ModelingToolkit.get_tearing_state(sys) !== nothing) ||
!isempty(initialization_equations(sys))) && t !== nothing
if eltype(u0map) <: Number
Expand Down
2 changes: 1 addition & 1 deletion src/systems/diffeqs/odesystem.jl
Original file line number Diff line number Diff line change
Expand Up @@ -237,7 +237,7 @@ function ODESystem(deqs::AbstractVector{<:Equation}, iv, dvs, ps;
"`default_u0` and `default_p` are deprecated. Use `defaults` instead.",
:ODESystem, force = true)
end
defaults = Dict{Any,Any}(todict(defaults))
defaults = Dict{Any, Any}(todict(defaults))
var_to_name = Dict()
process_variables!(var_to_name, defaults, dvs′)
process_variables!(var_to_name, defaults, ps′)
Expand Down
6 changes: 4 additions & 2 deletions src/systems/model_parsing.jl
Original file line number Diff line number Diff line change
Expand Up @@ -157,9 +157,11 @@ function update_kwargs_and_metadata!(dict, kwargs, a, def, indices, type, var,
if !isnothing(meta) && haskey(meta, VariableUnit)
uvar = gensym()
push!(where_types, uvar)
push!(kwargs, Expr(:kw, :($a::Union{Nothing, Missing, $NoValue, $uvar}), NO_VALUE))
push!(kwargs,
Expr(:kw, :($a::Union{Nothing, Missing, $NoValue, $uvar}), NO_VALUE))
else
push!(kwargs, Expr(:kw, :($a::Union{Nothing, Missing, $NoValue, $type}), NO_VALUE))
push!(kwargs,
Expr(:kw, :($a::Union{Nothing, Missing, $NoValue, $type}), NO_VALUE))
end
dict[:kwargs][getname(var)] = Dict(:value => def, :type => type)
else
Expand Down
9 changes: 5 additions & 4 deletions src/systems/nonlinear/initializesystem.jl
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ function generate_initializesystem(sys::ODESystem;
elseif check_defguess
error("Invalid setup: parameter $(p) has no default value, initial value, or guess")
end
# `missing` passed to `ODEProblem`, and (either an equation using default or a guess)
# `missing` passed to `ODEProblem`, and (either an equation using default or a guess)
elseif _val1 === missing
if _val2 !== nothing && _val2 !== missing
push!(eqs_ics, varp ~ _val2)
Expand All @@ -134,8 +134,8 @@ function generate_initializesystem(sys::ODESystem;
elseif check_defguess
error("Invalid setup: parameter $(p) has no default value, initial value, or guess")
end
# No value passed to `ODEProblem`, but a default and a guess are present
# _val2 !== missing is implied by it falling this far in the elseif chain
# No value passed to `ODEProblem`, but a default and a guess are present
# _val2 !== missing is implied by it falling this far in the elseif chain
elseif _val1 === nothing && _val2 !== nothing && _val3 !== nothing
push!(eqs_ics, varp ~ _val2)
push!(u0, varp => _val3)
Expand Down Expand Up @@ -176,7 +176,8 @@ function is_parameter_solvable(p, pmap, defs, guesses)
_val3 = get(guesses, p, nothing)
# either (missing is a default or was passed to the ODEProblem) or (nothing was passed to
# the ODEProblem and it has a default and a guess)
return (_val1 === missing || _val2 === missing) || (_val1 === nothing && _val2 !== nothing && _val3 !== nothing)
return (_val1 === missing || _val2 === missing) ||
(_val1 === nothing && _val2 !== nothing && _val3 !== nothing)
end

function SciMLBase.remake_initializeprob(sys::ODESystem, odefn, u0, t0, p)
Expand Down
9 changes: 6 additions & 3 deletions test/initializationsystem.jl
Original file line number Diff line number Diff line change
Expand Up @@ -562,7 +562,8 @@ sol = solve(oprob_2nd_order_2, Rosenbrock23()) # retcode: Success
@test solve(prob, Tsit5()).ps[sym] val
end
function test_initializesystem(sys, u0map, pmap, p, equation)
isys = ModelingToolkit.generate_initializesystem(sys; u0map, pmap, guesses = ModelingToolkit.guesses(sys))
isys = ModelingToolkit.generate_initializesystem(
sys; u0map, pmap, guesses = ModelingToolkit.guesses(sys))
@test is_variable(isys, p)
@test equation in equations(isys) || (0 ~ -equation.rhs) in equations(isys)
end
Expand Down Expand Up @@ -609,7 +610,8 @@ sol = solve(oprob_2nd_order_2, Rosenbrock23()) # retcode: Success
test_parameter(prob2, p, 2.0)

# No `missing`, default and guess
@mtkbuild sys = ODESystem([D(x) ~ x * q, D(y) ~ y * p], t; defaults = [p => 2q], guesses = [p => 0.0])
@mtkbuild sys = ODESystem(
[D(x) ~ x * q, D(y) ~ y * p], t; defaults = [p => 2q], guesses = [p => 0.0])
delete!(pmap, p)
prob = ODEProblem(sys, u0map, (0.0, 1.0), pmap)
test_parameter(prob, p, 2.0)
Expand All @@ -627,7 +629,8 @@ sol = solve(oprob_2nd_order_2, Rosenbrock23()) # retcode: Success
@test prob.ps[p] 3.0
@test prob.f.initializeprob === nothing
# Default overridden by ODEProblem, guess provided
@mtkbuild sys = ODESystem([D(x) ~ q * x, D(y) ~ y * p], t; defaults = [p => 2q], guesses = [p => 1.0])
@mtkbuild sys = ODESystem(
[D(x) ~ q * x, D(y) ~ y * p], t; defaults = [p => 2q], guesses = [p => 1.0])
prob = ODEProblem(sys, u0map, (0.0, 1.0), _pmap)
@test prob.ps[p] 3.0
@test prob.f.initializeprob === nothing
Expand Down

0 comments on commit d85ed68

Please sign in to comment.