Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion ext/MTKFMIExt.jl
Original file line number Diff line number Diff line change
Expand Up @@ -799,7 +799,7 @@ function (wrapper::Union{FMI2InstanceWrapper, FMI3InstanceWrapper})(
y = outputs_buffer, y_refs = wrapper.output_value_references
)
wrapper.res_buffer[1:length(states_buffer)] .= states_buffer
wrapper.res_buffer[length(states_buffer)+1:end] .= outputs_buffer
wrapper.res_buffer[(length(states_buffer) + 1):end] .= outputs_buffer
return wrapper.res_buffer
end

Expand Down
11 changes: 8 additions & 3 deletions ext/MTKOrdinaryDiffEqBDFExt.jl
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,14 @@ using ModelingToolkit: t_nounits, D_nounits
@parameters a = 1.0 b = 1.0
@variables x(t_nounits) y(t_nounits)
prob = ODEProblem(
mtkcompile(System([D_nounits(x) ~ a * y, D_nounits(y) ~ -b * x],
t_nounits; name = :precompile_bdf)),
[x => 1.0, y => 0.0], (0.0, 1.0))
mtkcompile(
System(
[D_nounits(x) ~ a * y, D_nounits(y) ~ -b * x],
t_nounits; name = :precompile_bdf
)
),
[x => 1.0, y => 0.0], (0.0, 1.0)
)
@compile_workload begin
solve(prob, FBDF())
end
Expand Down
11 changes: 8 additions & 3 deletions ext/MTKOrdinaryDiffEqDefaultExt.jl
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,14 @@ using ModelingToolkit: t_nounits, D_nounits
@parameters a = 1.0 b = 1.0
@variables x(t_nounits) y(t_nounits)
prob = ODEProblem(
mtkcompile(System([D_nounits(x) ~ a * y, D_nounits(y) ~ -b * x],
t_nounits; name = :precompile_default)),
[x => 1.0, y => 0.0], (0.0, 1.0))
mtkcompile(
System(
[D_nounits(x) ~ a * y, D_nounits(y) ~ -b * x],
t_nounits; name = :precompile_default
)
),
[x => 1.0, y => 0.0], (0.0, 1.0)
)
@compile_workload begin
solve(prob)
end
Expand Down
11 changes: 8 additions & 3 deletions ext/MTKOrdinaryDiffEqRosenbrockExt.jl
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,14 @@ using ModelingToolkit: t_nounits, D_nounits
@parameters a = 1.0 b = 1.0
@variables x(t_nounits) y(t_nounits)
prob = ODEProblem(
mtkcompile(System([D_nounits(x) ~ a * y, D_nounits(y) ~ -b * x],
t_nounits; name = :precompile_rosenbrock)),
[x => 1.0, y => 0.0], (0.0, 1.0))
mtkcompile(
System(
[D_nounits(x) ~ a * y, D_nounits(y) ~ -b * x],
t_nounits; name = :precompile_rosenbrock
)
),
[x => 1.0, y => 0.0], (0.0, 1.0)
)
@compile_workload begin
solve(prob, Rodas5P())
end
Expand Down
1 change: 1 addition & 0 deletions lib/ModelingToolkitBase/ext/MTKCasADiDynamicOptExt.jl
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,7 @@ function MTK.set_variable_bounds!(m::CasADiModel, sys, pmap, tf, tunable_params,
subject_to!(m.model, m.tₛ >= tf_bounds[1])
subject_to!(m.model, m.tₛ <= tf_bounds[2])
end
return nothing
end

function MTK.add_initial_constraints!(m::CasADiModel, u0, u0_idxs, args...)
Expand Down
1 change: 1 addition & 0 deletions lib/ModelingToolkitBase/ext/MTKInfiniteOptExt.jl
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,7 @@ function MTK.set_variable_bounds!(m::InfiniteOptModel, sys, pmap, tf, tunable_pa
set_lower_bound(m.tₛ, tf_bounds[1])
set_upper_bound(m.tₛ, tf_bounds[2])
end
return nothing
end

function MTK.JuMPDynamicOptProblem(
Expand Down
1 change: 1 addition & 0 deletions lib/ModelingToolkitBase/ext/MTKPyomoDynamicOptExt.jl
Original file line number Diff line number Diff line change
Expand Up @@ -199,6 +199,7 @@ function MTK.set_variable_bounds!(m::PyomoDynamicOptModel, sys, pmap, tf, tunabl
MTK.add_constraint!(m, tₛ_sym ≳ tf_bounds[1])
MTK.add_constraint!(m, tₛ_sym ≲ tf_bounds[2])
end
return nothing
end

function MTK.set_objective!(pmodel::PyomoDynamicOptModel, expr)
Expand Down
2 changes: 1 addition & 1 deletion lib/ModelingToolkitBase/ext/MTKTrackerExt.jl
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ function MTKBase.promote_type_with_nothing(::Type{Tracker.TrackedReal{T}}, x::Tr
end

function MTKBase.promote_with_nothing(::Type{Tracker.TrackedReal{T}}, x::Tracker.TrackedArray{T}) where {T}
return x
return x
end

end
9 changes: 6 additions & 3 deletions lib/ModelingToolkitBase/src/problems/jumpproblem.jl
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,12 @@
check_complete(sys, JumpProblem)
check_compatibility && check_compatible_system(JumpProblem, sys)
if haskey(kwargs, :tstops)
throw(ArgumentError(
"Passing `tstops` directly to `JumpProblem(::System, ...)` is not supported. " *
"Define tstops on the `System` via the `tstops` keyword instead."))
throw(
ArgumentError(
"Passing `tstops` directly to `JumpProblem(::System, ...)` is not supported. " *
"Define tstops on the `System` via the `tstops` keyword instead."
)
)
end

has_vrjs = any(x -> x isa VariableRateJump, jumps(sys))
Expand Down
6 changes: 3 additions & 3 deletions lib/ModelingToolkitBase/src/systems/abstractsystem.jl
Original file line number Diff line number Diff line change
Expand Up @@ -631,7 +631,7 @@ Returns true if the parameter `p` is of the form `Initial(x)`.
"""
function isinitial(p::SymbolicT)
p, _ = split_indexed_var(p)
Moshi.Match.@match p begin
return Moshi.Match.@match p begin
BSImpl.Term(; f) => f isa Initial
_ => false
end
Expand Down Expand Up @@ -2961,7 +2961,7 @@ sys = mtkcompile(sys)
```
"""
macro mtkcompile(exprs...)
_named_and_call(mtkcompile, exprs)
return _named_and_call(mtkcompile, exprs)
end

"""
Expand All @@ -2981,7 +2981,7 @@ sys = complete(sys)
```
"""
macro mtkcomplete(exprs...)
_named_and_call(complete, exprs)
return _named_and_call(complete, exprs)
end

"""
Expand Down
2 changes: 1 addition & 1 deletion lib/ModelingToolkitBase/src/systems/analysis_points.jl
Original file line number Diff line number Diff line change
Expand Up @@ -558,7 +558,7 @@ function apply_transformation(tf::Break, sys::AbstractSystem)
if tf.outputs_to_params
namespace = namespace_hierarchy(getname(out_var))
insert!(namespace, 1, nameof(breaksys))
breaksys, _ = modify_nested_subsystem(breaksys, @view(namespace[1:end-1])) do apsys
breaksys, _ = modify_nested_subsystem(breaksys, @view(namespace[1:(end - 1)])) do apsys
new_dvs = copy(get_unknowns(apsys))
var_in_apsys = getvar(apsys, namespace[end]; namespace = false)
deleteat!(new_dvs, findfirst(isequal(var_in_apsys), new_dvs)::Int)
Expand Down
22 changes: 11 additions & 11 deletions lib/ModelingToolkitBase/src/systems/callbacks.jl
Original file line number Diff line number Diff line change
Expand Up @@ -1125,10 +1125,10 @@ function generate_callback(cbs::Vector{SymbolicContinuousCallback}, sys; kwargs.
eqs = reduce(vcat, eqs)

@static if pkgversion(SciMLBase) < v"3"
affect = VectorAffect(eq2affect, compiled.affects)
affect = VectorAffect(eq2affect, compiled.affects)
affect_neg = VectorAffect(eq2affect, compiled.affect_negs)
initialize = wrap_vector_optional_affect(compiled.inits, SciMLBase.INITIALIZE_DEFAULT)
finalize = wrap_vector_optional_affect(compiled.finals, SciMLBase.FINALIZE_DEFAULT)
finalize = wrap_vector_optional_affect(compiled.finals, SciMLBase.FINALIZE_DEFAULT)

return VectorContinuousCallback(
trigger, affect, affect_neg, length(eqs); initialize, finalize,
Expand All @@ -1137,9 +1137,9 @@ function generate_callback(cbs::Vector{SymbolicContinuousCallback}, sys; kwargs.
initialize_save_discretes = cbs[1].initialize_save_discretes
)
else
affect = VectorAffect(eq2affect, compiled.affects, compiled.affect_negs)
affect = VectorAffect(eq2affect, compiled.affects, compiled.affect_negs)
initialize = wrap_vector_optional_affect(compiled.inits, SciMLBase.INITIALIZE_DEFAULT)
finalize = wrap_vector_optional_affect(compiled.finals, SciMLBase.FINALIZE_DEFAULT)
finalize = wrap_vector_optional_affect(compiled.finals, SciMLBase.FINALIZE_DEFAULT)

return VectorContinuousCallback(
trigger, affect, length(eqs); initialize, finalize,
Expand Down Expand Up @@ -1234,7 +1234,7 @@ function generate_callback(cb, sys; tspan = nothing, kwargs...)
)

initialize = isnothing(cb.initialize) ? init : InitFinalizeWrapper(init)
finalize = isnothing(cb.finalize) ? final : InitFinalizeWrapper(final)
finalize = isnothing(cb.finalize) ? final : InitFinalizeWrapper(final)

saved_clock_partitions = if is_split(sys)
get(get_index_cache(sys).callback_to_clocks, cb, ())
Expand Down Expand Up @@ -1493,14 +1493,14 @@ Base.@nospecializeinfer function compile_implicit_affect(
dvs_to_access = unknowns(affsys)
ps_to_access = [unPre(p) for p in parameters(affsys)]

affu_getter = getsym(sys, dvs_to_access)
affp_getter = getsym(sys, ps_to_access)
affu_getter = getsym(sys, dvs_to_access)
affp_getter = getsym(sys, ps_to_access)
affu_setter! = setsym(affsys, unknowns(affsys))
affp_setter! = setsym(affsys, parameters(affsys))
u_setter! = setsym(sys, dvs_to_update)
p_setter! = setsym(sys, ps_to_update)
u_getter = getsym(affsys, dvs_to_update)
p_getter = getsym(affsys, ps_to_update)
u_setter! = setsym(sys, dvs_to_update)
p_setter! = setsym(sys, ps_to_update)
u_getter = getsym(affsys, dvs_to_update)
p_getter = getsym(affsys, ps_to_update)

affprob = ImplicitDiscreteProblem(
affsys, op, (0, 0);
Expand Down
12 changes: 7 additions & 5 deletions lib/ModelingToolkitBase/src/systems/codegen.jl
Original file line number Diff line number Diff line change
Expand Up @@ -272,7 +272,7 @@ function generate_jacobian(
end

function assert_jac_length(arr::SparseMatrixCSC, I::Vector{<:Integer}, J::Vector{<:Integer})
@assert findnz(arr)[1:2] == (I, J)
return @assert findnz(arr)[1:2] == (I, J)
end

function assert_jac_length_header(sys)
Expand Down Expand Up @@ -1098,8 +1098,10 @@ end
function assemble_maj(majv::Vector{U}, unknowntoid, pmapper) where {U <: MassActionJump}
rs = [numericrstoich(maj.reactant_stoch, unknowntoid) for maj in majv]
ns = [numericnstoich(maj.net_stoch, unknowntoid) for maj in majv]
return MassActionJump(rs, ns; param_mapper = pmapper, nocopy = true,
scale_rates = false, rescale_rates_on_update = false)
return MassActionJump(
rs, ns; param_mapper = pmapper, nocopy = true,
scale_rates = false, rescale_rates_on_update = false
)
end

function numericrstoich(mtrs::Vector{Pair{V, W}}, unknowntoid) where {V, W}
Expand Down Expand Up @@ -1383,8 +1385,8 @@ Base.@nospecializeinfer function build_explicit_observed_function(
p_start + wrap_delays, length(args) - length(rps) + 1 + wrap_delays, is_split(sys),
),
}(
oop, iip
)
oop, iip
)
return (return_inplace isa Val{true} || return_inplace isa Bool && return_inplace) ? (f, f) : f
end

Expand Down
4 changes: 2 additions & 2 deletions lib/ModelingToolkitBase/src/systems/codegen_utils.jl
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ function generated_argument_name(i::Int)
end

function compute_array_variable_buffer_idxs(@nospecialize(args); ignore_vars = Set{SymbolicT}())
_compute_array_variable_buffer_idxs(args isa Vector ? args : collect(args), ignore_vars)
return _compute_array_variable_buffer_idxs(args isa Vector ? args : collect(args), ignore_vars)
end

function _compute_array_variable_buffer_idxs(args::Vector, ignore_vars)
Expand Down Expand Up @@ -244,7 +244,7 @@ function should_invalidate_mutable_cache_entry(::Type{ParameterArrayAssignments}
end

function find_arrvars_is_atomic(ex::SymbolicT)
SU.default_is_atomic(ex) && Symbolics.isarraysymbolic(ex)
return SU.default_is_atomic(ex) && Symbolics.isarraysymbolic(ex)
end

"""
Expand Down
1 change: 1 addition & 0 deletions lib/ModelingToolkitBase/src/systems/connectors.jl
Original file line number Diff line number Diff line change
Expand Up @@ -871,6 +871,7 @@ function Base.showerror(io::IO, err::CausalConnectionSetNoSourceError)
for cvar in err.cset
println(io, " ", cvar, " ", cvar.type === InputVar ? "(Input)" : "(Output)")
end
return
end

"""
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1170,7 +1170,7 @@ end
DiffCacheAllocatorAPIWrapper{T}(dcapiw::DiffCacheAllocatorAPIWrapper{T}) where {T} = dcapiw

function DiffCacheAllocatorAPIWrapper{T}(dcapiw::DiffCacheAllocatorAPIWrapper) where {T}
convert(DiffCacheAllocatorAPIWrapper{T}, dcapiw)
return convert(DiffCacheAllocatorAPIWrapper{T}, dcapiw)
end

function (dcapiw::DiffCacheAllocatorAPIWrapper)(reference, sz::NTuple{N, Int}) where {N}
Expand Down Expand Up @@ -1255,14 +1255,14 @@ Does nothing to integers or other number types. Does affect Complex numbers.
"""
struct LiteralRewriter{FloatType <: AbstractFloat} end

(::LiteralRewriter{FT})(x::AbstractFloat) where FT = convert(FT, x)
(::LiteralRewriter{FT})(x::AbstractFloat) where {FT} = convert(FT, x)
function (rw::LiteralRewriter{FT})(x::Complex{F}) where {FT, F <: AbstractFloat}
return convert(Complex{typeof(rw(one(F)))}, x)
end
function (rw::LiteralRewriter{FT})(x::AbstractArray{F}) where {FT, F <: Union{AbstractFloat, Complex{<:AbstractFloat}}}
return map(rw, x)
end
(::LiteralRewriter{FT})(x) where FT = x
(::LiteralRewriter{FT})(x) where {FT} = x

function (rw::LiteralRewriter)(x::SymbolicT)
return Moshi.Match.@match x begin
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -421,8 +421,10 @@ function extract_variable_bounds(sys, pmap, tf, tunable_params, user_bounds = Di
end
tf_bounds = if symbolic_type(tf) === ScalarSymbolic() && hasbounds(tf)
lo, hi = getbounds(tf)
(SymbolicUtils.unwrap_const(unwrap(Symbolics.fixpoint_sub(lo, pmap))),
SymbolicUtils.unwrap_const(unwrap(Symbolics.fixpoint_sub(hi, pmap))))
(
SymbolicUtils.unwrap_const(unwrap(Symbolics.fixpoint_sub(lo, pmap))),
SymbolicUtils.unwrap_const(unwrap(Symbolics.fixpoint_sub(hi, pmap))),
)
else
nothing
end
Expand Down
9 changes: 5 additions & 4 deletions lib/ModelingToolkitBase/src/systems/problem_utils.jl
Original file line number Diff line number Diff line change
Expand Up @@ -775,7 +775,7 @@ function __apply_copy_template(valp, template)
end

function (cp::CopyParamsByTemplate{IsRoot})(src) where {IsRoot}
if IsRoot
return if IsRoot
reshape(mapreduce(Base.Fix1(__apply_copy_template, src), vcat, cp.template), cp.size)
else
buffers = map(Base.Fix1(__apply_copy_template, src), cp.template)
Expand Down Expand Up @@ -1376,7 +1376,7 @@ function (p::PromoteToTunableEltype{F, floatT})(nlsol) where {F, floatT}
raw isa AbstractArray || return raw
isempty(raw) && return raw
T = promote_type(eltype(raw), _tunable_eltype(parameter_values(nlsol)), floatT)
T === eltype(raw) ? raw : convert(AbstractArray{T}, raw)
return T === eltype(raw) ? raw : convert(AbstractArray{T}, raw)
end

_tunable_eltype(p::MTKParameters) = isempty(p.tunable) ? Bool : eltype(p.tunable)
Expand Down Expand Up @@ -1422,7 +1422,7 @@ function maybe_build_initialization_problem(
initsys = initializeprob.f.sys::System
needs_remake = false
_u0 = state_values(initializeprob)
if _u0 !== nothing
if _u0 !== nothing
if ArrayInterface.ismutable(_u0)
__u0 = floatT.(_u0)
else
Expand Down Expand Up @@ -1489,7 +1489,8 @@ function maybe_build_initialization_problem(
initializeprobmap = nothing
else
initializeprobmap = u0_constructor ∘ PromoteToTunableEltype(
getu(initializeprob, solved_unknowns), floatT)
getu(initializeprob, solved_unknowns), floatT
)
end
else
initializeprobmap = nothing
Expand Down
33 changes: 21 additions & 12 deletions lib/ModelingToolkitBase/src/systems/system.jl
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ function check_symbolic_ad_allowed(sys::AbstractSystem)
end
throw(ArgumentError(msg))
end
return nothing
end

"""
Expand Down Expand Up @@ -661,11 +662,14 @@ function System(
# JumpProcesses must not re-apply factorial scaling on parameter updates.
for j in jumps
if j isa MassActionJump && j.rescale_rates_on_update
throw(ArgumentError(
"MassActionJump with rescale_rates_on_update = true is not supported " *
"in Systems with jumps or JumpSystems. Rate expressions must be pre-scaled (e.g. " *
"k/factorial(n) for n-th order reactions). Use SymbolicMassActionJump " *
"or pass scale_rates = false when constructing the MassActionJump."))
throw(
ArgumentError(
"MassActionJump with rescale_rates_on_update = true is not supported " *
"in Systems with jumps or JumpSystems. Rate expressions must be pre-scaled (e.g. " *
"k/factorial(n) for n-th order reactions). Use SymbolicMassActionJump " *
"or pass scale_rates = false when constructing the MassActionJump."
)
)
end
end
return System(
Expand Down Expand Up @@ -1313,15 +1317,20 @@ Construct a `MassActionJump` with `scale_rates = false`, suitable for use in a

Returns a `MassActionJump` — this is a convenience constructor, not a new type.
"""
function SymbolicMassActionJump(rate, reactant_stoch, net_stoch; scale_rates = false,
kwargs...)
function SymbolicMassActionJump(
rate, reactant_stoch, net_stoch; scale_rates = false,
kwargs...
)
if scale_rates
throw(ArgumentError(
"SymbolicMassActionJump requires pre-scaled rate expressions " *
"(scale_rates = false). scale_rates = true is not supported in " *
"ModelingToolkitBase."))
throw(
ArgumentError(
"SymbolicMassActionJump requires pre-scaled rate expressions " *
"(scale_rates = false). scale_rates = true is not supported in " *
"ModelingToolkitBase."
)
)
end
MassActionJump(rate, reactant_stoch, net_stoch; scale_rates = false, kwargs...)
return MassActionJump(rate, reactant_stoch, net_stoch; scale_rates = false, kwargs...)
end

"""
Expand Down
Loading
Loading