diff --git a/src/ModelingToolkit.jl b/src/ModelingToolkit.jl index bf6dfc3af2..e94c8bb5af 100644 --- a/src/ModelingToolkit.jl +++ b/src/ModelingToolkit.jl @@ -214,6 +214,7 @@ include("structural_transformation/StructuralTransformations.jl") include("inputoutput.jl") include("adjoints.jl") +include("deprecations.jl") const t_nounits = let only(@independent_variables t) @@ -233,7 +234,7 @@ PrecompileTools.@compile_workload begin using ModelingToolkit @variables x(ModelingToolkit.t_nounits) @named sys = System([ModelingToolkit.D_nounits(x) ~ -x], ModelingToolkit.t_nounits) - prob = ODEProblem(structural_simplify(sys), [x => 30.0], (0, 100), [], jac = true) + prob = ODEProblem(mtkcompile(sys), [x => 30.0], (0, 100), [], jac = true) @mtkmodel __testmod__ begin @constants begin c = 1.0 @@ -288,7 +289,7 @@ export alias_elimination, flatten export connect, domain_connect, @connector, Connection, AnalysisPoint, Flow, Stream, instream export initial_state, transition, activeState, entry, ticksInState, timeInState -export @component, @mtkmodel, @mtkbuild +export @component, @mtkmodel, @mtkcompile, @mtkbuild export isinput, isoutput, getbounds, hasbounds, getguess, hasguess, isdisturbance, istunable, getdist, hasdist, tunable_parameters, isirreducible, getdescription, hasdescription, @@ -304,8 +305,8 @@ export SymScope, LocalScope, ParentScope, GlobalScope export independent_variable, equations, controls, observed, full_equations, jumps, cost, brownians export initialization_equations, guesses, defaults, parameter_dependencies, hierarchy -export structural_simplify, expand_connections, linearize, linearization_function, - LinearizationProblem +export mtkcompile, expand_connections, linearize, linearization_function, + LinearizationProblem, structural_simplify export solve export Pre diff --git a/src/deprecations.jl b/src/deprecations.jl new file mode 100644 index 0000000000..c8aee32253 --- /dev/null +++ b/src/deprecations.jl @@ -0,0 +1,11 @@ +@deprecate structural_simplify(sys; kwargs...) mtkcompile(sys; kwargs...) +@deprecate structural_simplify(sys, io; kwargs...) mtkcompile( + sys; inputs = io[1], outputs = io[2], kwargs...) + +macro mtkbuild(exprs...) + return quote + Base.depwarn("`@mtkbuild` is deprecated. Use `@mtkcompile` instead.", :mtkbuild) + @mtkcompile $(exprs...) + end |> esc +end + diff --git a/src/inputoutput.jl b/src/inputoutput.jl index 97376a2a44..b4a6bfd998 100644 --- a/src/inputoutput.jl +++ b/src/inputoutput.jl @@ -202,7 +202,7 @@ function generate_control_function(sys::AbstractSystem, inputs = unbound_inputs( kwargs...) # Remove this when the ControlFunction gets merged. if check_simplified && !iscomplete(sys) - error("A completed `ODESystem` is required. Call `complete` or `structural_simplify` on the system before creating the control function.") + error("A completed `ODESystem` is required. Call `complete` or `mtkcompile` on the system before creating the control function.") end isempty(inputs) && @warn("No unbound inputs were found in system.") if disturbance_inputs !== nothing @@ -417,7 +417,7 @@ function add_input_disturbance(sys, dist::DisturbanceModel, inputs = Any[]; kwar dist.input ~ u + dsys.output.u[1]] augmented_sys = System(eqs, t, systems = [dsys], name = gensym(:outer)) augmented_sys = extend(augmented_sys, sys) - ssys = structural_simplify(augmented_sys, inputs = all_inputs, disturbance_inputs = [d]) + ssys = mtkcompile(augmented_sys, inputs = all_inputs, disturbance_inputs = [d]) f, dvs, p, io_sys = generate_control_function(ssys, all_inputs, [d]; kwargs...) diff --git a/src/linearization.jl b/src/linearization.jl index 25b42dffb3..757d6eece5 100644 --- a/src/linearization.jl +++ b/src/linearization.jl @@ -15,7 +15,7 @@ y &= h(x, z, u) where `x` are differential unknown variables, `z` algebraic variables, `u` inputs and `y` outputs. To obtain a linear statespace representation, see [`linearize`](@ref). The input argument `variables` is a vector defining the operating point, corresponding to `unknowns(simplified_sys)` and `p` is a vector corresponding to the parameters of `simplified_sys`. Note: all variables in `inputs` have been converted to parameters in `simplified_sys`. -The `simplified_sys` has undergone [`structural_simplify`](@ref) and had any occurring input or output variables replaced with the variables provided in arguments `inputs` and `outputs`. The unknowns of this system also indicate the order of the unknowns that holds for the linearized matrices. +The `simplified_sys` has undergone [`mtkcompile`](@ref) and had any occurring input or output variables replaced with the variables provided in arguments `inputs` and `outputs`. The unknowns of this system also indicate the order of the unknowns that holds for the linearized matrices. # Arguments: @@ -58,7 +58,7 @@ function linearization_function(sys::AbstractSystem, inputs, outputs = mapreduce(vcat, outputs; init = []) do var symbolic_type(var) == ArraySymbolic() ? collect(var) : [var] end - ssys = structural_simplify(sys; inputs, outputs, simplify, kwargs...) + ssys = mtkcompile(sys; inputs, outputs, simplify, kwargs...) diff_idxs, alge_idxs = eq_idxs(ssys) if zero_dummy_der dummyder = setdiff(unknowns(ssys), unknowns(sys)) @@ -498,7 +498,7 @@ function linearize_symbolic(sys::AbstractSystem, inputs, outputs; simplify = false, allow_input_derivatives = false, eval_expression = false, eval_module = @__MODULE__, kwargs...) - sys = structural_simplify(sys; inputs, outputs, simplify, kwargs...) + sys = mtkcompile(sys; inputs, outputs, simplify, kwargs...) diff_idxs, alge_idxs = eq_idxs(sys) sts = unknowns(sys) t = get_iv(sys) diff --git a/src/problems/bvproblem.jl b/src/problems/bvproblem.jl index b0ad28a842..82783077c1 100644 --- a/src/problems/bvproblem.jl +++ b/src/problems/bvproblem.jl @@ -30,7 +30,7 @@ If a `System` without `constraints` is specified, it will be treated as an initi D(D(y)) ~ λ * y - g x(t)^2 + y^2 ~ 1] cstr = [x(0.5) ~ 1] - @mtkbuild pend = System(eqs, t; constraints = cstrs) + @mtkcompile pend = System(eqs, t; constraints = cstrs) tspan = (0.0, 1.5) u0map = [x(t) => 0.6, y => 0.8] diff --git a/src/problems/compatibility.jl b/src/problems/compatibility.jl index 84c2eefc3f..36302a9e74 100644 --- a/src/problems/compatibility.jl +++ b/src/problems/compatibility.jl @@ -120,7 +120,7 @@ function check_has_noise(sys::System, T) if !isempty(brownians(sys)) msg = """ Systems constructed by defining Brownian variables with `@brownian` must be \ - simplified by calling `structural_simplify` before a `$T` can be constructed. + simplified by calling `mtkcompile` before a `$T` can be constructed. """ end throw(SystemCompatibilityError(msg)) @@ -131,7 +131,7 @@ function check_is_discrete(sys::System, T) if !is_discrete_system(sys) throw(SystemCompatibilityError(""" `$T` expects a discrete system. Consider an `ODEProblem` instead. If your system \ - is discrete, ensure `structural_simplify` has been run on it. + is discrete, ensure `mtkcompile` has been run on it. """)) end end diff --git a/src/problems/initializationproblem.jl b/src/problems/initializationproblem.jl index 132376e3e6..be535da3e6 100644 --- a/src/problems/initializationproblem.jl +++ b/src/problems/initializationproblem.jl @@ -35,7 +35,7 @@ initial conditions for the given DAE. time_dependent_init = is_time_dependent(sys), kwargs...) where {iip, specialize} if !iscomplete(sys) - error("A completed system is required. Call `complete` or `structural_simplify` on the system before creating an `ODEProblem`") + error("A completed system is required. Call `complete` or `mtkcompile` on the system before creating an `ODEProblem`") end if isempty(u0map) && get_initializesystem(sys) !== nothing isys = get_initializesystem(sys; initialization_eqs, check_units) @@ -60,7 +60,7 @@ initial conditions for the given DAE. end if simplify_system - isys = structural_simplify(isys; fully_determined, split = is_split(sys)) + isys = mtkcompile(isys; fully_determined, split = is_split(sys)) end ts = get_tearing_state(isys) diff --git a/src/problems/sccnonlinearproblem.jl b/src/problems/sccnonlinearproblem.jl index 36330e339f..63058a08f3 100644 --- a/src/problems/sccnonlinearproblem.jl +++ b/src/problems/sccnonlinearproblem.jl @@ -73,11 +73,11 @@ function SciMLBase.SCCNonlinearProblem{iip}(sys::System, u0map, parammap = SciMLBase.NullParameters(); eval_expression = false, eval_module = @__MODULE__, cse = true, kwargs...) where {iip} if !iscomplete(sys) || get_tearing_state(sys) === nothing - error("A simplified `System` is required. Call `structural_simplify` on the system before creating an `SCCNonlinearProblem`.") + error("A simplified `System` is required. Call `mtkcompile` on the system before creating an `SCCNonlinearProblem`.") end if !is_split(sys) - error("The system has been simplified with `split = false`. `SCCNonlinearProblem` is not compatible with this system. Pass `split = true` to `structural_simplify` to use `SCCNonlinearProblem`.") + error("The system has been simplified with `split = false`. `SCCNonlinearProblem` is not compatible with this system. Pass `split = true` to `mtkcompile` to use `SCCNonlinearProblem`.") end ts = get_tearing_state(sys) diff --git a/src/structural_transformation/StructuralTransformations.jl b/src/structural_transformation/StructuralTransformations.jl index 15f1f7d2db..0365d50a84 100644 --- a/src/structural_transformation/StructuralTransformations.jl +++ b/src/structural_transformation/StructuralTransformations.jl @@ -35,7 +35,7 @@ import ModelingToolkit: var_derivative!, var_derivative_graph! using Graphs using ModelingToolkit: algeqs, EquationsView, SystemStructure, TransformationState, TearingState, - structural_simplify!, + mtkcompile!, isdiffvar, isdervar, isalgvar, isdiffeq, algeqs, is_only_discrete, dervars_range, diffvars_range, algvars_range, DiffGraph, complete!, diff --git a/src/structural_transformation/pantelides.jl b/src/structural_transformation/pantelides.jl index 53c790863f..871bd99ef4 100644 --- a/src/structural_transformation/pantelides.jl +++ b/src/structural_transformation/pantelides.jl @@ -210,7 +210,7 @@ end dae_index_lowering(sys::System; kwargs...) -> System Perform the Pantelides algorithm to transform a higher index DAE to an index 1 -DAE. `kwargs` are forwarded to [`pantelides!`](@ref). End users are encouraged to call [`structural_simplify`](@ref) +DAE. `kwargs` are forwarded to [`pantelides!`](@ref). End users are encouraged to call [`mtkcompile`](@ref) instead, which calls this function internally. """ function dae_index_lowering(sys::System; kwargs...) diff --git a/src/structural_transformation/symbolics_tearing.jl b/src/structural_transformation/symbolics_tearing.jl index 43c3e78e32..c3499abebc 100644 --- a/src/structural_transformation/symbolics_tearing.jl +++ b/src/structural_transformation/symbolics_tearing.jl @@ -989,7 +989,7 @@ end tearing(sys; simplify=false) Tear the nonlinear equations in system. When `simplify=true`, we simplify the -new residual equations after tearing. End users are encouraged to call [`structural_simplify`](@ref) +new residual equations after tearing. End users are encouraged to call [`mtkcompile`](@ref) instead, which calls this function internally. """ function tearing(sys::AbstractSystem, state = TearingState(sys); mm = nothing, diff --git a/src/systems/abstractsystem.jl b/src/systems/abstractsystem.jl index 6ede839e4a..e4bc47a4b7 100644 --- a/src/systems/abstractsystem.jl +++ b/src/systems/abstractsystem.jl @@ -146,7 +146,7 @@ may be subsetted using `dvs` and `ps`. All `kwargs` are passed to the internal [`build_function`](@ref) call. The returned function can be called as `f(u, p, t)` or `f(du, u, p, t)` for time-dependent systems and `f(u, p)` or `f(du, u, p)` for time-independent systems. If `split=true` (the default) was passed to [`complete`](@ref), -[`structural_simplify`](@ref) or [`@mtkbuild`](@ref), `p` is expected to be an `MTKParameters` +[`mtkcompile`](@ref) or [`@mtkcompile`](@ref), `p` is expected to be an `MTKParameters` object. """ function generate_custom_function(sys::AbstractSystem, exprs, dvs = unknowns(sys), @@ -154,7 +154,7 @@ function generate_custom_function(sys::AbstractSystem, exprs, dvs = unknowns(sys expression = Val{true}, eval_expression = false, eval_module = @__MODULE__, cachesyms::Tuple = (), kwargs...) if !iscomplete(sys) - error("A completed system is required. Call `complete` or `structural_simplify` on the system.") + error("A completed system is required. Call `complete` or `mtkcompile` on the system.") end p = (reorder_parameters(sys, unwrap.(ps))..., cachesyms...) isscalar = !(exprs isa AbstractArray) @@ -751,7 +751,7 @@ function complete( newparams = OrderedSet() iv = has_iv(sys) ? get_iv(sys) : nothing collect_scoped_vars!(newunknowns, newparams, sys, iv; depth = -1) - # don't update unknowns to not disturb `structural_simplify` order + # don't update unknowns to not disturb `mtkcompile` order # `GlobalScope`d unknowns will be picked up and added there @set! sys.ps = unique!(vcat(get_ps(sys), collect(newparams))) @@ -1178,7 +1178,7 @@ end Denotes that a variable belongs to the root system in the hierarchy, regardless of which equations of subsystems in the hierarchy it is involved in. Variables with this scope are never namespaced and only added to the unknowns/parameters of a system when calling -`complete` or `structural_simplify`. +`complete` or `mtkcompile`. """ struct GlobalScope <: SymScope end @@ -2470,7 +2470,7 @@ macro component(expr) esc(component_post_processing(expr, false)) end -macro mtkbuild(exprs...) +macro mtkcompile(exprs...) expr = exprs[1] named_expr = ModelingToolkit.named_expr(expr) name = named_expr.args[1] @@ -2484,7 +2484,7 @@ macro mtkbuild(exprs...) else kwargs = (Expr(:parameters, kwargs...),) end - call_expr = Expr(:call, structural_simplify, kwargs..., name) + call_expr = Expr(:call, mtkcompile, kwargs..., name) esc(quote $named_expr $name = $call_expr @@ -2523,7 +2523,7 @@ function debug_system( functions = Set(functions) # more efficient "in" lookup end if has_systems(sys) && !isempty(get_systems(sys)) - error("debug_system(sys) only works on systems with no sub-systems! Consider flattening it with flatten(sys) or structural_simplify(sys) first.") + error("debug_system(sys) only works on systems with no sub-systems! Consider flattening it with flatten(sys) or mtkcompile(sys) first.") end if has_eqs(sys) eqs = debug_sub.(equations(sys), Ref(functions); kw...) @@ -2608,10 +2608,10 @@ end function check_array_equations_unknowns(eqs, dvs) if any(eq -> eq isa Equation && Symbolics.isarraysymbolic(eq.lhs), eqs) - throw(ArgumentError("The system has array equations. Call `structural_simplify` to handle such equations or scalarize them manually.")) + throw(ArgumentError("The system has array equations. Call `mtkcompile` to handle such equations or scalarize them manually.")) end if any(x -> Symbolics.isarraysymbolic(x), dvs) - throw(ArgumentError("The system has array unknowns. Call `structural_simplify` to handle this or scalarize them manually.")) + throw(ArgumentError("The system has array unknowns. Call `mtkcompile` to handle this or scalarize them manually.")) end end diff --git a/src/systems/callbacks.jl b/src/systems/callbacks.jl index 7be1397922..f70669eca3 100644 --- a/src/systems/callbacks.jl +++ b/src/systems/callbacks.jl @@ -267,7 +267,7 @@ function make_affect(affect::Vector{Equation}; discrete_parameters = Any[], @named affectsys = System( vcat(affect, alg_eqs), iv, collect(union(_dvs, discretes)), collect(union(pre_params, sys_params))) - affectsys = structural_simplify(affectsys; fully_determined = nothing) + affectsys = mtkcompile(affectsys; fully_determined = nothing) # get accessed parameters p from Pre(p) in the callback parameters accessed_params = filter(isparameter, map(unPre, collect(pre_params))) union!(accessed_params, sys_params) diff --git a/src/systems/diffeqs/basic_transformations.jl b/src/systems/diffeqs/basic_transformations.jl index 6046e5d4be..6243ca2405 100644 --- a/src/systems/diffeqs/basic_transformations.jl +++ b/src/systems/diffeqs/basic_transformations.jl @@ -74,7 +74,7 @@ Any extra equations `eqs` involving the new and old independent variables will b # Usage before structural simplification The variable change must take place before structural simplification. -In following calls to `structural_simplify`, consider passing `allow_symbolic = true` to avoid undesired constraint equations between between dummy variables. +In following calls to `mtkcompile`, consider passing `allow_symbolic = true` to avoid undesired constraint equations between between dummy variables. # Usage with non-autonomous systems @@ -99,7 +99,7 @@ julia> @named M = System([D(D(y)) ~ -9.81, D(D(x)) ~ 0.0], t); julia> M = change_independent_variable(M, x); -julia> M = structural_simplify(M; allow_symbolic = true); +julia> M = mtkcompile(M; allow_symbolic = true); julia> unknowns(M) 3-element Vector{SymbolicUtils.BasicSymbolic{Real}}: @@ -248,7 +248,7 @@ function stochastic_integral_transform(sys::System, correction_factor) throw(ArgumentError(""" `$stochastic_integral_transform` expects a system with noise_eqs. If your \ noise is specified using brownian variables, consider calling \ - `structural_simplify`. + `mtkcompile`. """)) end name = nameof(sys) diff --git a/src/systems/index_cache.jl b/src/systems/index_cache.jl index 5ea5fa16a7..bdb69ae8c6 100644 --- a/src/systems/index_cache.jl +++ b/src/systems/index_cache.jl @@ -653,10 +653,10 @@ See also: [`MTKParameters`](@ref), [`tunable_parameters`](@ref), [`reorder_dimen function reorder_dimension_by_tunables!( dest::AbstractArray, sys::AbstractSystem, arr::AbstractArray, syms; dim = 1) if !iscomplete(sys) - throw(ArgumentError("A completed system is required. Call `complete` or `structural_simplify` on the system.")) + throw(ArgumentError("A completed system is required. Call `complete` or `mtkcompile` on the system.")) end if !has_index_cache(sys) || (ic = get_index_cache(sys)) === nothing - throw(ArgumentError("The system does not have an index cache. Call `complete` or `structural_simplify` on the system with `split = true`.")) + throw(ArgumentError("The system does not have an index cache. Call `complete` or `mtkcompile` on the system with `split = true`.")) end if size(dest) != size(arr) throw(ArgumentError("Source and destination arrays must have the same size. Got source array with size $(size(arr)) and destination with size $(size(dest)).")) diff --git a/src/systems/nonlinear/homotopy_continuation.jl b/src/systems/nonlinear/homotopy_continuation.jl index 103780cb21..ddc0b72aed 100644 --- a/src/systems/nonlinear/homotopy_continuation.jl +++ b/src/systems/nonlinear/homotopy_continuation.jl @@ -491,7 +491,7 @@ function SciMLBase.HomotopyNonlinearFunction{iip, specialize}( p = nothing, fraction_cancel_fn = SymbolicUtils.simplify_fractions, cse = true, kwargs...) where {iip, specialize} if !iscomplete(sys) - error("A completed `System` is required. Call `complete` or `structural_simplify` on the system before creating a `HomotopyContinuationFunction`") + error("A completed `System` is required. Call `complete` or `mtkcompile` on the system before creating a `HomotopyContinuationFunction`") end transformation = PolynomialTransformation(sys) if transformation isa NotPolynomialError @@ -552,7 +552,7 @@ function HomotopyContinuationProblem{iip, spec}( sys::System, u0map, pmap = SciMLBase.NullParameters(); kwargs...) where {iip, spec} if !iscomplete(sys) - error("A completed `System` is required. Call `complete` or `structural_simplify` on the system before creating a `HomotopyContinuationProblem`") + error("A completed `System` is required. Call `complete` or `mtkcompile` on the system before creating a `HomotopyContinuationProblem`") end f, u0, p = process_SciMLProblem( HomotopyNonlinearFunction{iip, spec}, sys, u0map, pmap; kwargs...) diff --git a/src/systems/parameter_buffer.jl b/src/systems/parameter_buffer.jl index 83d165eefc..9448f1930e 100644 --- a/src/systems/parameter_buffer.jl +++ b/src/systems/parameter_buffer.jl @@ -23,7 +23,7 @@ dependent systems. It is only required if the symbolic expressions also use the variable of the system. This requires that `complete` has been called on the system (usually via -`structural_simplify` or `@mtkbuild`) and the keyword `split = true` was passed (which is +`mtkcompile` or `@mtkcompile`) and the keyword `split = true` was passed (which is the default behavior). """ function MTKParameters( diff --git a/src/systems/system.jl b/src/systems/system.jl index 1587f85b75..5da5d9db79 100644 --- a/src/systems/system.jl +++ b/src/systems/system.jl @@ -739,7 +739,7 @@ end function Base.showerror(io::IO, err::SystemNotCompleteError) print(io, """ - A completed system is required. Call `complete` or `structural_simplify` on the \ + A completed system is required. Call `complete` or `mtkcompile` on the \ system before creating a `$(err.obj)`. """) end diff --git a/src/systems/systems.jl b/src/systems/systems.jl index 13ee0767cf..13a62f7915 100644 --- a/src/systems/systems.jl +++ b/src/systems/systems.jl @@ -19,14 +19,14 @@ topological sort of the observed equations in `sys`. + `inputs`, `outputs` and `disturbance_inputs` are passed as keyword arguments.` All inputs` get converted to parameters and are allowed to be unconnected, allowing models where `n_unknowns = n_equations - n_inputs`. + `sort_eqs=true` controls whether equations are sorted lexicographically before simplification or not. """ -function structural_simplify( +function mtkcompile( sys::AbstractSystem; additional_passes = [], simplify = false, split = true, allow_symbolic = false, allow_parameter = true, conservative = false, fully_determined = true, inputs = Any[], outputs = Any[], disturbance_inputs = Any[], kwargs...) isscheduled(sys) && throw(RepeatedStructuralSimplificationError()) - newsys′ = __structural_simplify(sys; simplify, + newsys′ = __mtkcompile(sys; simplify, allow_symbolic, allow_parameter, conservative, fully_determined, inputs, outputs, disturbance_inputs, kwargs...) @@ -51,7 +51,7 @@ function structural_simplify( end end -function __structural_simplify(sys::AbstractSystem; simplify = false, +function __mtkcompile(sys::AbstractSystem; simplify = false, inputs = Any[], outputs = Any[], disturbance_inputs = Any[], sort_eqs = true, @@ -84,7 +84,7 @@ function __structural_simplify(sys::AbstractSystem; simplify = false, end end if isempty(brown_vars) - return structural_simplify!( + return mtkcompile!( state; simplify, inputs, outputs, disturbance_inputs, kwargs...) else Is = Int[] @@ -117,7 +117,7 @@ function __structural_simplify(sys::AbstractSystem; simplify = false, for (i, v) in enumerate(fullvars) if !iszero(new_idxs[i]) && invview(var_to_diff)[i] === nothing] - ode_sys = structural_simplify( + ode_sys = mtkcompile( sys; simplify, inputs, outputs, disturbance_inputs, kwargs...) eqs = equations(ode_sys) sorted_g_rows = zeros(Num, length(eqs), size(g, 2)) @@ -184,7 +184,7 @@ function simplify_optimization_system(sys::System; split = true, kwargs...) end econs = fast_substitute.(econs, (irreducible_subs,)) nlsys = System(econs, dvs, parameters(sys); name = :___tmp_nlsystem) - snlsys = structural_simplify(nlsys; kwargs..., fully_determined = false) + snlsys = mtkcompile(nlsys; kwargs..., fully_determined = false) obs = observed(snlsys) subs = Dict(eq.lhs => eq.rhs for eq in observed(snlsys)) seqs = equations(snlsys) @@ -231,7 +231,7 @@ end """ $(TYPEDSIGNATURES) -Given a system that has been simplified via `structural_simplify`, return a `Dict` mapping +Given a system that has been simplified via `mtkcompile`, return a `Dict` mapping variables of the system to equations that are used to solve for them. This includes observed variables. @@ -247,7 +247,7 @@ function map_variables_to_equations(sys::AbstractSystem; rename_dummy_derivative end ts = get_tearing_state(sys) if ts === nothing - throw(ArgumentError("`map_variables_to_equations` requires a simplified system. Call `structural_simplify` on the system before calling this function.")) + throw(ArgumentError("`map_variables_to_equations` requires a simplified system. Call `mtkcompile` on the system before calling this function.")) end dummy_sub = Dict() diff --git a/src/systems/systemstructure.jl b/src/systems/systemstructure.jl index e8d81e0820..16551008e9 100644 --- a/src/systems/systemstructure.jl +++ b/src/systems/systemstructure.jl @@ -24,7 +24,7 @@ function quick_cancel_expr(expr) kws...))(expr) end -export SystemStructure, TransformationState, TearingState, structural_simplify! +export SystemStructure, TransformationState, TearingState, mtkcompile! export isdiffvar, isdervar, isalgvar, isdiffeq, algeqs, is_only_discrete export dervars_range, diffvars_range, algvars_range export DiffGraph, complete! @@ -695,7 +695,7 @@ function Base.show(io::IO, mime::MIME"text/plain", ms::MatchedSystemStructure) printstyled(io, " SelectedState") end -function structural_simplify!(state::TearingState; simplify = false, +function mtkcompile!(state::TearingState; simplify = false, check_consistency = true, fully_determined = true, warn_initialize_determined = true, inputs = Any[], outputs = Any[], disturbance_inputs = Any[], @@ -723,13 +723,13 @@ function structural_simplify!(state::TearingState; simplify = false, state.structure.only_discrete = true end - sys = _structural_simplify!(state; simplify, check_consistency, + sys = _mtkcompile!(state; simplify, check_consistency, inputs, outputs, disturbance_inputs, fully_determined, kwargs...) return sys end -function _structural_simplify!(state::TearingState; simplify = false, +function _mtkcompile!(state::TearingState; simplify = false, check_consistency = true, fully_determined = true, warn_initialize_determined = false, dummy_derivative = true, inputs = Any[], outputs = Any[], diff --git a/src/utils.jl b/src/utils.jl index 13ebb28b6a..4a183fd83f 100644 --- a/src/utils.jl +++ b/src/utils.jl @@ -158,7 +158,7 @@ function check_lhs(eq::Equation, op, dvs::Set) v = unwrap(eq.lhs) _iszero(v) && return (operation(v) isa op && only(arguments(v)) in dvs) && return - error("$v is not a valid LHS. Please run structural_simplify before simulation.") + error("$v is not a valid LHS. Please run mtkcompile before simulation.") end check_lhs(eqs, op, dvs::Set) = for eq in eqs @@ -322,7 +322,7 @@ Throw error when difference/derivative operation occurs in the R.H.S. optext = "derivative" end msg = "The $optext variable must be isolated to the left-hand " * - "side of the equation like `$opvar ~ ...`. You may want to use `structural_simplify` or the DAE form.\nGot $eq." + "side of the equation like `$opvar ~ ...`. You may want to use `mtkcompile` or the DAE form.\nGot $eq." throw(InvalidSystemException(msg)) end @@ -358,10 +358,10 @@ function check_operator_variables(eqs, op::T) where {T} is_tmp_fine = iszero(nd) end is_tmp_fine || - error("The LHS cannot contain nondifferentiated variables. Please run `structural_simplify` or use the DAE form.\nGot $eq") + error("The LHS cannot contain nondifferentiated variables. Please run `mtkcompile` or use the DAE form.\nGot $eq") for v in tmp v in ops && - error("The LHS operator must be unique. Please run `structural_simplify` or use the DAE form. $v appears in LHS more than once.") + error("The LHS operator must be unique. Please run `mtkcompile` or use the DAE form. $v appears in LHS more than once.") push!(ops, v) end empty!(tmp) diff --git a/test/accessor_functions.jl b/test/accessor_functions.jl index 707b928e75..ba6aae27a0 100644 --- a/test/accessor_functions.jl +++ b/test/accessor_functions.jl @@ -68,10 +68,10 @@ let sys_mid2_comp = complete(sys_mid2) sys_mid1_comp = complete(sys_mid1) sys_top_comp = complete(sys_top) - sys_bot_ss = structural_simplify(sys_bot) - sys_mid2_ss = structural_simplify(sys_mid2) - sys_mid1_ss = structural_simplify(sys_mid1) - sys_top_ss = structural_simplify(sys_top) + sys_bot_ss = mtkcompile(sys_bot) + sys_mid2_ss = mtkcompile(sys_mid2) + sys_mid1_ss = mtkcompile(sys_mid1) + sys_top_ss = mtkcompile(sys_top) # Checks `parameters1. @test all_sets_equal(parameters.([sys_bot, sys_bot_comp, sys_bot_ss])..., [d, p_bot]) @@ -98,7 +98,7 @@ let @test all(sym_issubset(parameters_toplevel(sys), get_ps(sys)) for sys in [sys_bot, sys_mid2, sys_mid1, sys_top]) - # Checks `unknowns`. O(t) is eliminated by `structural_simplify` and + # Checks `unknowns`. O(t) is eliminated by `mtkcompile` and # must be considered separately. @test all_sets_equal(unknowns.([sys_bot, sys_bot_comp])..., [O, Y, X_bot]) @test all_sets_equal(unknowns.([sys_bot_ss])..., [Y, X_bot]) diff --git a/test/analysis_points.jl b/test/analysis_points.jl index 54cf0004c6..bb233db5e8 100644 --- a/test/analysis_points.jl +++ b/test/analysis_points.jl @@ -67,7 +67,7 @@ sys = System(eqs, t, systems = [P, C], name = :hej) nonamespace_sys = toggle_namespacing(nested_sys, false) @testset "simplifies and solves" begin - ssys = structural_simplify(sys) + ssys = mtkcompile(sys) prob = ODEProblem(ssys, [P.x => 1], (0, 10)) sol = solve(prob, Rodas5()) @test norm(sol.u[1]) >= 1 diff --git a/test/basic_transformations.jl b/test/basic_transformations.jl index 50f4c0feaf..661b508861 100644 --- a/test/basic_transformations.jl +++ b/test/basic_transformations.jl @@ -51,8 +51,8 @@ end M1 = System(eqs, t; initialization_eqs, name = :M) M2 = change_independent_variable(M1, s) - M1 = structural_simplify(M1; allow_symbolic = true) - M2 = structural_simplify(M2; allow_symbolic = true) + M1 = mtkcompile(M1; allow_symbolic = true) + M2 = mtkcompile(M2; allow_symbolic = true) prob1 = ODEProblem(M1, [M1.s => 1.0], (1.0, 4.0), []) prob2 = ODEProblem(M2, [], (1.0, 2.0), []) sol1 = solve(prob1, Tsit5(); reltol = 1e-10, abstol = 1e-10) @@ -101,9 +101,9 @@ end extraeqs = [Differential(M2.a)(b) ~ exp(-b), M2.a ~ exp(b)] M3 = change_independent_variable(M2, b, extraeqs) - M1 = structural_simplify(M1) - M2 = structural_simplify(M2; allow_symbolic = true) - M3 = structural_simplify(M3; allow_symbolic = true) + M1 = mtkcompile(M1) + M2 = mtkcompile(M2; allow_symbolic = true) + M3 = mtkcompile(M3; allow_symbolic = true) @test length(unknowns(M3)) == length(unknowns(M2)) == length(unknowns(M1)) - 1 end @@ -121,7 +121,7 @@ end @parameters g=9.81 v # gravitational acceleration and constant horizontal velocity Mt = System([D(D(y)) ~ -g, D(x) ~ v], t; name = :M) # gives (x, y) as function of t, ... Mx = change_independent_variable(Mt, x; add_old_diff = true) # ... but we want y as a function of x - Mx = structural_simplify(Mx; allow_symbolic = true) + Mx = mtkcompile(Mx; allow_symbolic = true) Dx = Differential(Mx.x) u0 = [Mx.y => 0.0, Dx(Mx.y) => 1.0, Mx.t => 0.0] p = [v => 10.0] @@ -135,7 +135,7 @@ end @parameters g = 9.81 # gravitational acceleration Mt = System([D(D(y)) ~ -g, D(D(x)) ~ 0], t; name = :M) # gives (x, y) as function of t, ... Mx = change_independent_variable(Mt, x; add_old_diff = true) # ... but we want y as a function of x - Mx = structural_simplify(Mx; allow_symbolic = true) + Mx = mtkcompile(Mx; allow_symbolic = true) Dx = Differential(Mx.x) u0 = [Mx.y => 0.0, Dx(Mx.y) => 1.0, Mx.t => 0.0, Mx.xˍt => 10.0] prob = ODEProblem(Mx, u0, (0.0, 20.0), []) # 1 = dy/dx = (dy/dt)/(dx/dt) means equal initial horizontal and vertical velocities @@ -153,7 +153,7 @@ end ] M1 = System(eqs, t; name = :M) M2 = change_independent_variable(M1, x; add_old_diff = true) - @test_nowarn structural_simplify(M2) + @test_nowarn mtkcompile(M2) # Compare to pen-and-paper result @variables x xˍt(x) xˍt(x) y(x) t(x) @@ -199,7 +199,7 @@ end ]) _f = LinearInterpolation([1.0, 1.0], [-100.0, +100.0]) # constant value 1 - M2s = structural_simplify(M2; allow_symbolic = true) + M2s = mtkcompile(M2; allow_symbolic = true) prob = ODEProblem(M2s, [M2s.y => 0.0], (1.0, 4.0), [fc => _f, f => _f]) sol = solve(prob, Tsit5(); abstol = 1e-5) @test isapprox(sol(4.0, idxs = M2.y), 12.0; atol = 1e-5) # Anal solution is D(y) ~ 12 => y(t) ~ 12*t + C => y(x) ~ 12*√(x) + C. With y(x=1)=0 => 12*(√(x)-1), so y(x=4) ~ 12 @@ -208,7 +208,7 @@ end @testset "Change independent variable (errors)" begin @variables x(t) y z(y) w(t) v(t) M = System([D(x) ~ 1, v ~ x], t; name = :M) - Ms = structural_simplify(M) + Ms = mtkcompile(M) @test_throws "structurally simplified" change_independent_variable(Ms, y) @test_throws "not a function of" change_independent_variable(M, y) @test_throws "not a function of" change_independent_variable(M, z) @@ -224,7 +224,7 @@ end @parameters g=9.81 [unit = u"m * s^-2"] # gravitational acceleration Mt = System([D_units(D_units(y)) ~ -g, D_units(D_units(x)) ~ 0], t_units; name = :M) # gives (x, y) as function of t, ... Mx = change_independent_variable(Mt, x; add_old_diff = true) # ... but we want y as a function of x - Mx = structural_simplify(Mx; allow_symbolic = true) + Mx = mtkcompile(Mx; allow_symbolic = true) Dx = Differential(Mx.x) u0 = [Mx.y => 0.0, Dx(Mx.y) => 1.0, Mx.t_units => 0.0, Mx.xˍt_units => 10.0] prob = ODEProblem(Mx, u0, (0.0, 20.0), []) # 1 = dy/dx = (dy/dt)/(dx/dt) means equal initial horizontal and vertical velocities @@ -281,7 +281,7 @@ end @named sys = ConnectSys() sys = complete(sys; flatten = false) new_sys = change_independent_variable(sys, sys.y; add_old_diff = true) - ss = structural_simplify(new_sys; allow_symbolic = true) + ss = mtkcompile(new_sys; allow_symbolic = true) prob = ODEProblem(ss, [ss.t => 0.0, ss.x => 1.0], (0.0, 1.0)) sol = solve(prob, Tsit5(); reltol = 1e-5) @test all(isapprox.(sol[ss.t], sol[ss.y]; atol = 1e-10)) @@ -298,7 +298,7 @@ end @named sys = System(eqs, t) sys = complete(sys) new_sys = change_independent_variable(sys, sys.x; add_old_diff = true) - ss_new_sys = structural_simplify(new_sys; allow_symbolic = true) + ss_new_sys = mtkcompile(new_sys; allow_symbolic = true) u0 = [new_sys.y => 0.5, new_sys.t => 0.0] prob = ODEProblem(ss_new_sys, u0, (0.0, 0.5), []) sol = solve(prob, Tsit5(); reltol = 1e-5) diff --git a/test/bvproblem.jl b/test/bvproblem.jl index 0931a0ab6d..bafb3e9674 100644 --- a/test/bvproblem.jl +++ b/test/bvproblem.jl @@ -20,7 +20,7 @@ daesolvers = [Ascher2, Ascher4, Ascher6] parammap = [α => 7.5, β => 4, γ => 8.0, δ => 5.0] tspan = (0.0, 10.0) - @mtkbuild lotkavolterra = System(eqs, t) + @mtkcompile lotkavolterra = System(eqs, t) op = ODEProblem(lotkavolterra, u0map, tspan, parammap) osol = solve(op, Vern9()) @@ -52,7 +52,7 @@ end eqs = [D(θ) ~ θ_t D(θ_t) ~ -(g / L) * sin(θ)] - @mtkbuild pend = System(eqs, t) + @mtkcompile pend = System(eqs, t) u0map = [θ => π / 2, θ_t => π / 2] parammap = [:L => 1.0, :g => 9.81] @@ -91,7 +91,7 @@ end D(y(t)) ~ -γ * y(t) + δ * x(t) * y(t)] tspan = (0.0, 1.0) - @mtkbuild lksys = System(eqs, t) + @mtkcompile lksys = System(eqs, t) function lotkavolterra!(du, u, p, t) du[1] = p[1] * u[1] - p[2] * u[1] * u[2] @@ -104,7 +104,7 @@ end # Test with a constraint. constr = [y(0.5) ~ 2.0] - @mtkbuild lksys = System(eqs, t; constraints = constr) + @mtkcompile lksys = System(eqs, t; constraints = constr) function bc!(resid, u, p, t) resid[1] = u(0.0)[1] - 1.0 @@ -177,7 +177,7 @@ end tspan = (0.0, 1.0) guess = [x(t) => 4.0, y(t) => 2.0] constr = [x(0.6) ~ 3.5, x(0.3) ~ 7.0] - @mtkbuild lksys = System(eqs, t; constraints = constr) + @mtkcompile lksys = System(eqs, t; constraints = constr) bvp = SciMLBase.BVProblem{true, SciMLBase.AutoSpecialize}( lksys, u0map, tspan; guesses = guess) @@ -185,13 +185,13 @@ end # Testing that more complicated constraints give correct solutions. constr = [y(0.2) + x(0.8) ~ 3.0, y(0.3) ~ 2.0] - @mtkbuild lksys = System(eqs, t; constraints = constr) + @mtkcompile lksys = System(eqs, t; constraints = constr) bvp = SciMLBase.BVProblem{false, SciMLBase.FullSpecialize}( lksys, u0map, tspan; guesses = guess) test_solvers(solvers, bvp, u0map, constr; dt = 0.05) constr = [α * β - x(0.6) ~ 0.0, y(0.2) ~ 3.0] - @mtkbuild lksys = System(eqs, t; constraints = constr) + @mtkcompile lksys = System(eqs, t; constraints = constr) bvp = SciMLBase.BVProblem{true, SciMLBase.AutoSpecialize}( lksys, u0map, tspan; guesses = guess) test_solvers(solvers, bvp, u0map, constr) @@ -205,7 +205,7 @@ end # eqs = [D(D(x)) ~ λ * x # D(D(y)) ~ λ * y - g # x^2 + y^2 ~ 1] -# @mtkbuild pend = System(eqs, t) +# @mtkcompile pend = System(eqs, t) # # tspan = (0.0, 1.5) # u0map = [x => 1, y => 0] @@ -243,7 +243,7 @@ end # D(D(y)) ~ λ * y - g # x(t)^2 + y^2 ~ 1] # constr = [x(0.5) ~ 1] -# @mtkbuild pend = System(eqs, t; constr) +# @mtkcompile pend = System(eqs, t; constr) # # tspan = (0.0, 1.5) # u0map = [x(t) => 0.6, y => 0.8] @@ -262,13 +262,13 @@ end # # constr = [x(0.5) ~ 1, # x(0.3)^3 + y(0.6)^2 ~ 0.5] -# @mtkbuild pend = System(eqs, t; constr) +# @mtkcompile pend = System(eqs, t; constr) # bvp = SciMLBase.BVProblem{true, SciMLBase.AutoSpecialize}(pend, u0map, tspan, parammap; guesses, check_length = false) # test_solvers(daesolvers, bvp, u0map, constr, get_alg_eqs(pend)) # # constr = [x(0.4) * g ~ y(0.2), # y(0.7) ~ 0.3] -# @mtkbuild pend = System(eqs, t; constr) +# @mtkcompile pend = System(eqs, t; constr) # bvp = SciMLBase.BVProblem{true, SciMLBase.AutoSpecialize}(pend, u0map, tspan, parammap; guesses, check_length = false) # test_solvers(daesolvers, bvp, u0map, constr, get_alg_eqs(pend)) # end @@ -286,7 +286,7 @@ end parammap = [α => 7.5, β => 4, γ => 8.0, δ => 5.0] costs = [x(0.6), x(0.3)^2] consolidate(u, sub) = (u[1] + 3)^2 + u[2] + sum(sub; init = 0) - @mtkbuild lksys = System(eqs, t; costs, consolidate) + @mtkcompile lksys = System(eqs, t; costs, consolidate) @test_throws ModelingToolkit.SystemCompatibilityError ODEProblem( lksys, u0map, tspan, parammap) @@ -301,7 +301,7 @@ end @parameters t_c costs = [y(t_c) + x(0.0), x(0.4)^2] consolidate(u, sub) = log(u[1]) - u[2] + sum(sub; init = 0) - @mtkbuild lksys = System(eqs, t; costs, consolidate) + @mtkcompile lksys = System(eqs, t; costs, consolidate) @test t_c ∈ Set(parameters(lksys)) push!(parammap, t_c => 0.56) prob = ODEProblem(lksys, u0map, tspan, parammap; check_compatibility = false) diff --git a/test/clock.jl b/test/clock.jl index 3c5a32c1ce..7fc9e7ddd2 100644 --- a/test/clock.jl +++ b/test/clock.jl @@ -65,10 +65,10 @@ By inference: ci, varmap = infer_clocks(sys) eqmap = ci.eq_domain tss, inputs, continuous_id = ModelingToolkit.split_system(deepcopy(ci)) -sss = ModelingToolkit._structural_simplify!( +sss = ModelingToolkit._mtkcompile!( deepcopy(tss[continuous_id]), inputs = inputs[continuous_id], outputs = []) @test equations(sss) == [D(x) ~ u - x] -sss = ModelingToolkit._structural_simplify!( +sss = ModelingToolkit._mtkcompile!( deepcopy(tss[1]), inputs = inputs[1], outputs = []) @test isempty(equations(sss)) d = Clock(dt) @@ -116,7 +116,7 @@ eqs = [yd ~ Sample(dt)(y) D(x) ~ -x + u y ~ x] @named sys = System(eqs, t) -@test_throws ModelingToolkit.HybridSystemNotSupportedException ss=structural_simplify(sys); +@test_throws ModelingToolkit.HybridSystemNotSupportedException ss=mtkcompile(sys); @test_skip begin Tf = 1.0 @@ -129,7 +129,7 @@ eqs = [yd ~ Sample(dt)(y) [kp => 1.0; ud(k - 1) => 2.1; ud(k - 2) => 2.0]) # recreate problem to empty saved values sol = solve(prob, Tsit5(), kwargshandle = KeywordArgSilent) - ss_nosplit = structural_simplify(sys; split = false) + ss_nosplit = mtkcompile(sys; split = false) prob_nosplit = ODEProblem(ss_nosplit, [x => 0.1], (0.0, Tf), [kp => 1.0; ud(k - 1) => 2.1; ud(k - 2) => 2.0]) int = init(prob_nosplit, Tsit5(); kwargshandle = KeywordArgSilent) @@ -295,8 +295,8 @@ eqs = [yd ~ Sample(dt)(y) @test varmap[y] == ContinuousClock() @test varmap[u] == ContinuousClock() - ss = structural_simplify(cl) - ss_nosplit = structural_simplify(cl; split = false) + ss = mtkcompile(cl) + ss_nosplit = mtkcompile(cl; split = false) if VERSION >= v"1.7" prob = ODEProblem(ss, [x => 0.0], (0.0, 1.0), [kp => 1.0]) @@ -427,7 +427,7 @@ eqs = [yd ~ Sample(dt)(y) @test varmap[_model.feedback.output.u] == d @test varmap[_model.feedback.input2.u] == d - ssys = structural_simplify(model) + ssys = mtkcompile(model) Tf = 0.2 timevec = 0:(d.dt):Tf @@ -518,7 +518,7 @@ eqs = [yd ~ Sample(dt)(y) end end - @mtkbuild model = FirstOrderWithStepCounter() + @mtkcompile model = FirstOrderWithStepCounter() prob = ODEProblem(model, [], (0.0, 10.0)) sol = solve(prob, Tsit5(), kwargshandle = KeywordArgSilent) @@ -529,7 +529,7 @@ eqs = [yd ~ Sample(dt)(y) @variables x(t)=1.0 y(t)=1.0 eqs = [D(y) ~ Hold(x) x ~ x(k - 1) + x(k - 2)] - @mtkbuild sys = System(eqs, t) + @mtkcompile sys = System(eqs, t) prob = ODEProblem(sys, [], (0.0, 10.0)) int = init(prob, Tsit5(); kwargshandle = KeywordArgSilent) @test int.ps[x] == 2.0 diff --git a/test/code_generation.jl b/test/code_generation.jl index 0dc86871c5..9a3805ce21 100644 --- a/test/code_generation.jl +++ b/test/code_generation.jl @@ -58,7 +58,7 @@ end @testset "Non-standard array variables" begin @variables x(t) @parameters p[0:2] (f::Function)(..) - @mtkbuild sys = System(D(x) ~ p[0] * x + p[1] * t + p[2] + f(p), t) + @mtkcompile sys = System(D(x) ~ p[0] * x + p[1] * t + p[2] + f(p), t) prob = ODEProblem(sys, [x => 1.0], (0.0, 1.0), [p => [1.0, 2.0, 3.0], f => sum]) @test prob.ps[p] == [1.0, 2.0, 3.0] @test prob.ps[p[0]] == 1.0 @@ -70,7 +70,7 @@ end @parameters p[1:2] (f::Function)(..) @named sys = System( [D(x[0]) ~ p[1] * x[0] + x[2], D(x[1]) ~ p[2] * f(x) + x[2]], t) - sys = structural_simplify(sys, inputs = [x[2]], outputs = []) + sys = mtkcompile(sys, inputs = [x[2]], outputs = []) @test is_parameter(sys, x[2]) prob = ODEProblem(sys, [x[0] => 1.0, x[1] => 1.0], (0.0, 1.0), [p => ones(2), f => sum, x[2] => 2.0]) diff --git a/test/components.jl b/test/components.jl index 19b601a0be..689173d61f 100644 --- a/test/components.jl +++ b/test/components.jl @@ -49,9 +49,9 @@ include("common/rc_model.jl") completed_rc_model = complete(rc_model) @test isequal(completed_rc_model.resistor.n.i, resistor.n.i) @test ModelingToolkit.n_expanded_connection_equations(capacitor) == 2 - @test length(equations(structural_simplify(rc_model, allow_parameter = false))) == 2 - sys = structural_simplify(rc_model) - @test_throws ModelingToolkit.RepeatedStructuralSimplificationError structural_simplify(sys) + @test length(equations(mtkcompile(rc_model, allow_parameter = false))) == 2 + sys = mtkcompile(rc_model) + @test_throws ModelingToolkit.RepeatedStructuralSimplificationError mtkcompile(sys) @test length(equations(sys)) == 1 check_contract(sys) @test !isempty(ModelingToolkit.defaults(sys)) @@ -62,7 +62,7 @@ include("common/rc_model.jl") end @testset "Outer/inner connections" begin - sys = structural_simplify(rc_model) + sys = mtkcompile(rc_model) prob = ODEProblem(sys, [sys.capacitor.v => 0.0], (0.0, 10.0)) sol = solve(prob, Rodas4()) @@ -92,7 +92,7 @@ end @named sys_inner_outer = compose(sys′, [ground, shape, source, rc_comp]) @test_nowarn show(IOBuffer(), MIME"text/plain"(), sys_inner_outer) expand_connections(sys_inner_outer, debug = true) - sys_inner_outer = structural_simplify(sys_inner_outer) + sys_inner_outer = mtkcompile(sys_inner_outer) @test !isempty(ModelingToolkit.defaults(sys_inner_outer)) u0 = [rc_comp.capacitor.v => 0.0] prob = ODEProblem(sys_inner_outer, u0, (0, 10.0), sparse = true) @@ -114,7 +114,7 @@ end include("common/serial_inductor.jl") @testset "Serial inductor" begin - sys = structural_simplify(ll_model) + sys = mtkcompile(ll_model) @test length(equations(sys)) == 2 u0 = unknowns(sys) .=> 0 @test_nowarn ODEProblem( @@ -123,7 +123,7 @@ include("common/serial_inductor.jl") sol = solve(prob, DFBDF()) @test sol.retcode == SciMLBase.ReturnCode.Success - sys2 = structural_simplify(ll2_model) + sys2 = mtkcompile(ll2_model) @test length(equations(sys2)) == 3 u0 = unknowns(sys) .=> 0 prob = ODEProblem(sys, u0, (0, 10.0)) @@ -182,7 +182,7 @@ function Circuit(; name) end @named foo = Circuit() -@test structural_simplify(foo) isa ModelingToolkit.AbstractSystem +@test mtkcompile(foo) isa ModelingToolkit.AbstractSystem # BLT tests @testset "BLT ordering" begin @@ -243,7 +243,7 @@ end @named _rc_model = System(rc_eqs, t) @named rc_model = compose(_rc_model, [resistor, capacitor, ground]) - sys = structural_simplify(rc_model) + sys = mtkcompile(rc_model) prob = ODEProblem(sys, [sys.c1.v => 0.0], (0, 10.0)) sol = solve(prob, Tsit5()) end @@ -289,7 +289,7 @@ end end @named outer = Outer() - simp = structural_simplify(outer) + simp = mtkcompile(outer) @test sort(propertynames(outer)) == [:inner, :t, :x] @test propertynames(simp) == propertynames(outer) @@ -306,7 +306,7 @@ end @test_throws ArgumentError outer.inner₊p end -@testset "`getproperty` on `structural_simplify(complete(sys))`" begin +@testset "`getproperty` on `mtkcompile(complete(sys))`" begin @mtkmodel Foo begin @variables begin x(t) @@ -322,7 +322,7 @@ end end @named bar = Bar() cbar = complete(bar) - ss = structural_simplify(cbar) + ss = mtkcompile(cbar) @test isequal(cbar.foo.x, ss.foo.x) end diff --git a/test/constants.jl b/test/constants.jl index bd28517ae6..ec98dab9b4 100644 --- a/test/constants.jl +++ b/test/constants.jl @@ -15,12 +15,12 @@ eqs = [D(x) ~ a] prob = ODEProblem(complete(sys), [0], [0.0, 1.0], []) sol = solve(prob, Tsit5()) -# Test structural_simplify substitutions & observed values +# Test mtkcompile substitutions & observed values eqs = [D(x) ~ 1, w ~ a] @named sys = System(eqs, t) # Now eliminate the constants first -simp = structural_simplify(sys) +simp = mtkcompile(sys) @test equations(simp) == [D(x) ~ 1.0] #Constant with units @@ -33,7 +33,7 @@ UMT.get_unit(β) D = Differential(t) eqs = [D(x) ~ β] @named sys = System(eqs, t) -simp = structural_simplify(sys) +simp = mtkcompile(sys) @testset "Issue#3044" begin @constants h @@ -41,7 +41,7 @@ simp = structural_simplify(sys) @variables x(MT.t_nounits) = h eqs = [MT.D_nounits(x) ~ (h - x) / τ] - @mtkbuild fol_model = System(eqs, MT.t_nounits) + @mtkcompile fol_model = System(eqs, MT.t_nounits) prob = ODEProblem(fol_model, [], (0.0, 10.0), [h => 1]) @test prob[x] ≈ 1 diff --git a/test/dde.jl b/test/dde.jl index cac207caa3..211b59075c 100644 --- a/test/dde.jl +++ b/test/dde.jl @@ -38,7 +38,7 @@ eqs = [D(x₀) ~ (v0 / (1 + beta0 * (x₂(t - tau)^2))) * (p0 - q0) * x₀ - d0 D(x₁) ~ (v0 / (1 + beta0 * (x₂(t - tau)^2))) * (1 - p0 + q0) * x₀ + (v1 / (1 + beta1 * (x₂(t - tau)^2))) * (p1 - q1) * x₁ - d1 * x₁ D(x₂(t)) ~ (v1 / (1 + beta1 * (x₂(t - tau)^2))) * (1 - p1 + q1) * x₁ - d2 * x₂(t)] -@mtkbuild sys = System(eqs, t) +@mtkcompile sys = System(eqs, t) @test ModelingToolkit.is_dde(sys) @test !is_markovian(sys) prob = DDEProblem(sys, @@ -81,7 +81,7 @@ sol = solve(prob, RKMil(), seed = 100) @brownian η τ = 1.0 eqs = [D(x(t)) ~ a * x(t) + b * x(t - τ) + c + (α * x(t) + γ) * η, delx ~ x(t - τ)] -@mtkbuild sys = System(eqs, t) +@mtkcompile sys = System(eqs, t) @test ModelingToolkit.has_observed_with_lhs(sys, delx) @test ModelingToolkit.is_dde(sys) @test !is_markovian(sys) @@ -119,11 +119,11 @@ eqs = [osc1.jcn ~ osc2.delx, @test ModelingToolkit.is_dde(coupledOsc2) @test !is_markovian(coupledOsc2) for coupledOsc in [coupledOsc, coupledOsc2] - local sys = structural_simplify(coupledOsc) + local sys = mtkcompile(coupledOsc) @test length(equations(sys)) == 4 @test length(unknowns(sys)) == 4 end -sys = structural_simplify(coupledOsc) +sys = mtkcompile(coupledOsc) prob = DDEProblem(sys, [], (0.0, 10.0); constant_lags = [sys.osc1.τ, sys.osc2.τ]) sol = solve(prob, MethodOfSteps(Tsit5())) obsfn = ModelingToolkit.build_explicit_observed_function( @@ -162,7 +162,7 @@ prob_sa = DDEProblem(sys, [], (0.0, 10.0); constant_lags = [sys.osc1.τ, sys.osc return System([D(x) ~ dx], t; name = name) end - @mtkbuild ssys = System( + @mtkcompile ssys = System( Equation[], t; systems = [valve(name = :valve), veccy(name = :vvecs)]) prob = DDEProblem(ssys, [ssys.valve.opening => 1.0], (0.0, 1.0)) sol = solve(prob, MethodOfSteps(Tsit5())) @@ -178,7 +178,7 @@ end eqs = [D(x(t)) ~ -w * x(t - τ)] @named sys = System(eqs, t) - sys = structural_simplify(sys) + sys = mtkcompile(sys) prob = DDEProblem(sys, [], @@ -191,7 +191,7 @@ end @brownian r eqs = [D(x(t)) ~ -w * x(t - τ) + r] @named sys = System(eqs, t) - sys = structural_simplify(sys) + sys = mtkcompile(sys) prob = SDDEProblem(sys, [], (0.0, 10.0), diff --git a/test/debugging.jl b/test/debugging.jl index 615b452b38..0cf80fd494 100644 --- a/test/debugging.jl +++ b/test/debugging.jl @@ -6,8 +6,8 @@ using ModelingToolkit: t_nounits as t, D_nounits as D, ASSERTION_LOG_VARIABLE @brownian a @named inner_ode = System(D(x) ~ -sqrt(x), t; assertions = [(x > 0) => "ohno"]) @named inner_sde = System([D(x) ~ -10sqrt(x) + 0.01a], t; assertions = [(x > 0) => "ohno"]) -sys_ode = structural_simplify(inner_ode) -sys_sde = structural_simplify(inner_sde) +sys_ode = mtkcompile(inner_ode) +sys_sde = mtkcompile(inner_sde) SEED = 42 @testset "assertions are present in generated `f`" begin @@ -42,7 +42,7 @@ end (System, ODEProblem, inner_ode, Tsit5()), (System, SDEProblem, inner_sde, ImplicitEM())] kwargs = Problem == SDEProblem ? (; seed = SEED) : (;) - @mtkbuild outer = ctor(Equation[], t; systems = [inner]) + @mtkcompile outer = ctor(Equation[], t; systems = [inner]) dsys = debug_system(outer; functions = []) @test is_parameter(dsys, ASSERTION_LOG_VARIABLE) prob = Problem(dsys, [inner.x => 0.1], (0.0, 5.0); kwargs...) diff --git a/test/discrete_system.jl b/test/discrete_system.jl index 2d2682b07a..2db2aab8d3 100644 --- a/test/discrete_system.jl +++ b/test/discrete_system.jl @@ -9,7 +9,7 @@ using ModelingToolkit: t_nounits as t # Make sure positive shifts error @variables x(t) k = ShiftIndex(t) -@test_throws ErrorException @mtkbuild sys = System([x(k + 1) ~ x + x(k - 1)], t) +@test_throws ErrorException @mtkcompile sys = System([x(k + 1) ~ x + x(k - 1)], t) @inline function rate_to_proportion(r, t) 1 - exp(-r * t) @@ -30,7 +30,7 @@ eqs = [S ~ S(k - 1) - infection * h, # System @named sys = System(eqs, t, [S, I, R], [c, nsteps, δt, β, γ, h]) -syss = structural_simplify(sys) +syss = mtkcompile(sys) @test syss == syss df = DiscreteFunction(syss) @@ -72,7 +72,7 @@ eqs2 = [S ~ S(k - 1) - infection2, R ~ R(k - 1) + recovery2, R2 ~ R] -@mtkbuild sys = System( +@mtkcompile sys = System( eqs2, t, [S, I, R, R2], [c, nsteps, δt, β, γ]; controls = [β, γ], tspan) @test ModelingToolkit.defaults(sys) != Dict() @@ -198,7 +198,7 @@ RHS2 = RHS # Δ(us[i]) ~ dummy_identity(buffer[i], us[i]) # end -# @mtkbuild sys = System(eqs, t, us, ps; defaults = defs, preface = preface) +# @mtkcompile sys = System(eqs, t, us, ps; defaults = defs, preface = preface) # prob = DiscreteProblem(sys, [], (0.0, 1.0)) # sol = solve(prob, FunctionMap(); dt = dt) # @test c[1] + 1 == length(sol) @@ -208,7 +208,7 @@ RHS2 = RHS eqs = [u ~ 1 x ~ x(k - 1) + u y ~ x + u] -@mtkbuild de = System(eqs, t) +@mtkcompile de = System(eqs, t) prob = DiscreteProblem(de, [x(k - 1) => 0.0], (0, 10)) sol = solve(prob, FunctionMap()) @@ -243,7 +243,7 @@ function System(; name, buffer) System(eqs, t, vars, pars; systems = [y_sys], name = name) end -@test_nowarn @mtkbuild sys = System(; buffer = ones(10)) +@test_nowarn @mtkcompile sys = System(; buffer = ones(10)) # Ensure discrete systems with algebraic equations throw @variables x(t) y(t) @@ -253,7 +253,7 @@ k = ShiftIndex(t) @testset "Passing `nothing` to `u0`" begin @variables x(t) = 1 k = ShiftIndex() - @mtkbuild sys = System([x(k) ~ x(k - 1) + 1], t) + @mtkcompile sys = System([x(k) ~ x(k - 1) + 1], t) prob = @test_nowarn DiscreteProblem(sys, nothing, (0.0, 1.0)) @test_nowarn solve(prob, FunctionMap()) end @@ -261,7 +261,7 @@ end @testset "Initialization" begin # test that default values apply to the entire history @variables x(t) = 1.0 - @mtkbuild de = System([x ~ x(k - 1) + x(k - 2)], t) + @mtkcompile de = System([x ~ x(k - 1) + x(k - 2)], t) prob = DiscreteProblem(de, [], (0, 10)) @test prob[x] == 2.0 @test prob[x(k - 1)] == 1.0 @@ -284,14 +284,14 @@ end # Test missing initial throws error @variables x(t) - @mtkbuild de = System([x ~ x(k - 1) + x(k - 2) * x(k - 3)], t) + @mtkcompile de = System([x ~ x(k - 1) + x(k - 2) * x(k - 3)], t) @test_throws ErrorException prob=DiscreteProblem(de, [x(k - 3) => 2.0], (0, 10)) @test_throws ErrorException prob=DiscreteProblem( de, [x(k - 3) => 2.0, x(k - 1) => 3.0], (0, 10)) # Test non-assigned initials are given default value @variables x(t) = 2.0 - @mtkbuild de = System([x ~ x(k - 1) + x(k - 2) * x(k - 3)], t) + @mtkcompile de = System([x ~ x(k - 1) + x(k - 2) * x(k - 3)], t) prob = DiscreteProblem(de, [x(k - 3) => 12.0], (0, 10)) @test prob[x] == 26.0 @test prob[x(k - 1)] == 2.0 @@ -301,7 +301,7 @@ end @variables xₜ₋₂(t) zₜ₋₁(t) z(t) eqs = [x ~ x(k - 1) + z(k - 2), z ~ x(k - 2) * x(k - 3) - z(k - 1)^2] - @mtkbuild de = System(eqs, t) + @mtkcompile de = System(eqs, t) u0 = [x(k - 1) => 3, xₜ₋₂(k - 1) => 4, x(k - 2) => 1, @@ -328,7 +328,7 @@ end y[1](k) ~ y[1](k - 1) + y[1](k - 2), y[2](k) ~ y[2](k - 1) + y[2](k - 2) ] - @mtkbuild sys = System(eqs, t) + @mtkcompile sys = System(eqs, t) prob = DiscreteProblem(sys, [x(k - 1) => ones(2), x(k - 2) => zeros(2), y[1](k - 1) => 1.0, y[1](k - 2) => 0.0, y[2](k - 1) => 1.0, y[2](k - 2) => 0.0], diff --git a/test/domain_connectors.jl b/test/domain_connectors.jl index e35cee8f30..485796d585 100644 --- a/test/domain_connectors.jl +++ b/test/domain_connectors.jl @@ -148,7 +148,7 @@ esys = ModelingToolkit.expand_connections(odesys) csys = complete(odesys) -sys = structural_simplify(odesys) +sys = mtkcompile(odesys) @test length(equations(sys)) == length(unknowns(sys)) sys_defs = ModelingToolkit.defaults(sys) diff --git a/test/downstream/analysis_points.jl b/test/downstream/analysis_points.jl index 5dc0026138..21c33b2068 100644 --- a/test/downstream/analysis_points.jl +++ b/test/downstream/analysis_points.jl @@ -60,7 +60,7 @@ import ControlSystemsBase as CS filt.xd => 0.0 ]) - sys = structural_simplify(closed_loop) + sys = mtkcompile(closed_loop) prob = ODEProblem(sys, unknowns(sys) .=> 0.0, (0.0, 4.0)) sol = solve(prob, Rodas5P(), reltol = 1e-6, abstol = 1e-9) @@ -100,8 +100,8 @@ end connect(F.output, sys_inner.add.input1)] sys_outer = System(eqs, t, systems = [F, sys_inner, r], name = :outer) - # test first that the structural_simplify works correctly - ssys = structural_simplify(sys_outer) + # test first that the mtkcompile works correctly + ssys = mtkcompile(sys_outer) prob = ODEProblem(ssys, Pair[], (0, 10)) @test_nowarn solve(prob, Rodas5()) @@ -136,8 +136,8 @@ end connect(F.output, sys_inner.add.input1)] sys_outer = System(eqs, t, systems = [F, sys_inner, r], name = :outer) - # test first that the structural_simplify works correctly - ssys = structural_simplify(sys_outer) + # test first that the mtkcompile works correctly + ssys = mtkcompile(sys_outer) prob = ODEProblem(ssys, Pair[], (0, 10)) @test_nowarn solve(prob, Rodas5()) @@ -172,8 +172,8 @@ end connect(F.output, sys_inner.add.input1)] sys_outer = System(eqs, t, systems = [F, sys_inner, r], name = :outer) - # test first that the structural_simplify works correctly - ssys = structural_simplify(sys_outer) + # test first that the mtkcompile works correctly + ssys = mtkcompile(sys_outer) prob = ODEProblem(ssys, Pair[], (0, 10)) @test_nowarn solve(prob, Rodas5()) @@ -363,7 +363,7 @@ end sys_normal = normal_test_system() -prob = ODEProblem(structural_simplify(sys_normal), [], (0.0, 10.0)) +prob = ODEProblem(mtkcompile(sys_normal), [], (0.0, 10.0)) @test SciMLBase.successful_retcode(solve(prob, Rodas5P())) matrices_normal, _ = get_sensitivity(sys_normal, sys_normal.normal_inner.ap) @@ -384,7 +384,7 @@ matrices_normal, _ = get_sensitivity(sys_normal, sys_normal.normal_inner.ap) connect(inner.back.output, :ap, inner.F1.input)] @named sys = System(eqs2, t; systems = [inner, step]) - prob = ODEProblem(structural_simplify(sys), [], (0.0, 10.0)) + prob = ODEProblem(mtkcompile(sys), [], (0.0, 10.0)) @test SciMLBase.successful_retcode(solve(prob, Rodas5P())) matrices, _ = get_sensitivity(sys, sys.ap) @@ -408,7 +408,7 @@ end connect(inner.back.output.u, :ap, inner.F1.input.u)] @named sys = System(eqs2, t; systems = [inner, step]) - prob = ODEProblem(structural_simplify(sys), [], (0.0, 10.0)) + prob = ODEProblem(mtkcompile(sys), [], (0.0, 10.0)) @test SciMLBase.successful_retcode(solve(prob, Rodas5P())) matrices, _ = get_sensitivity(sys, sys.ap) @@ -432,7 +432,7 @@ end connect(inner.back.output.u, :ap, inner.F1.input.u)] @named sys = System(eqs2, t; systems = [inner, step]) - prob = ODEProblem(structural_simplify(sys), [], (0.0, 10.0)) + prob = ODEProblem(mtkcompile(sys), [], (0.0, 10.0)) @test SciMLBase.successful_retcode(solve(prob, Rodas5P())) matrices, _ = get_sensitivity(sys, sys.ap) diff --git a/test/downstream/inversemodel.jl b/test/downstream/inversemodel.jl index 0e4b1ea5eb..66da2d2ea9 100644 --- a/test/downstream/inversemodel.jl +++ b/test/downstream/inversemodel.jl @@ -111,7 +111,7 @@ end end end; @named model = InverseControlledTank() -ssys = structural_simplify(model) +ssys = mtkcompile(model) cm = complete(model) op = Dict( diff --git a/test/downstream/test_disturbance_model.jl b/test/downstream/test_disturbance_model.jl index 6da6c249a1..5f5672dc74 100644 --- a/test/downstream/test_disturbance_model.jl +++ b/test/downstream/test_disturbance_model.jl @@ -55,7 +55,7 @@ end end @named model = ModelWithInputs() # Model with load disturbance -ssys = structural_simplify(model) +ssys = mtkcompile(model) prob = ODEProblem(ssys, [], (0.0, 10.0)) sol = solve(prob, Tsit5()) # plot(sol) @@ -94,10 +94,10 @@ dist(; name) = System(1 / s; name) end @named model_with_disturbance = SystemModelWithDisturbanceModel() -# ssys = structural_simplify(open_loop(model_with_disturbance, :d)) # Open loop worked, but it's a bit awkward that we have to use it here +# ssys = mtkcompile(open_loop(model_with_disturbance, :d)) # Open loop worked, but it's a bit awkward that we have to use it here # lsys2 = named_ss(model_with_disturbance, [:u, :d1], # [P.inertia1.phi, P.inertia2.phi, P.inertia1.w, P.inertia2.w]) -ssys = structural_simplify(model_with_disturbance) +ssys = mtkcompile(model_with_disturbance) prob = ODEProblem(ssys, [], (0.0, 10.0)) sol = solve(prob, Tsit5()) @test SciMLBase.successful_retcode(sol) @@ -137,10 +137,10 @@ dist3(; name) = System(ss(1 + 10 / s, balance = false); name) end @named model_with_disturbance = SystemModelWithDisturbanceModel() -# ssys = structural_simplify(open_loop(model_with_disturbance, :d)) # Open loop worked, but it's a bit awkward that we have to use it here +# ssys = mtkcompile(open_loop(model_with_disturbance, :d)) # Open loop worked, but it's a bit awkward that we have to use it here # lsys3 = named_ss(model_with_disturbance, [:u, :d1], # [P.inertia1.phi, P.inertia2.phi, P.inertia1.w, P.inertia2.w]) -ssys = structural_simplify(model_with_disturbance) +ssys = mtkcompile(model_with_disturbance) prob = ODEProblem(ssys, [], (0.0, 10.0)) sol = solve(prob, Tsit5()) @test SciMLBase.successful_retcode(sol) diff --git a/test/dq_units.jl b/test/dq_units.jl index 5a635144f5..74209c3584 100644 --- a/test/dq_units.jl +++ b/test/dq_units.jl @@ -113,24 +113,24 @@ noiseeqs = [0.1us"W" 0.1us"W" eqs = [D(L) ~ v, V ~ L^3] @named sys = System(eqs, t) -sys_simple = structural_simplify(sys) +sys_simple = mtkcompile(sys) eqs = [D(V) ~ r, V ~ L^3] @named sys = System(eqs, t) -sys_simple = structural_simplify(sys) +sys_simple = mtkcompile(sys) @variables V [unit = u"m"^3] L [unit = u"m"] @parameters v [unit = u"m/s"] r [unit = u"m"^3 / u"s"] eqs = [V ~ r * t, V ~ L^3] @named sys = System(eqs, [V, L], [t, r]) -sys_simple = structural_simplify(sys) +sys_simple = mtkcompile(sys) eqs = [L ~ v * t, V ~ L^3] @named sys = System(eqs, [V, L], [t, r, v]) -sys_simple = structural_simplify(sys) +sys_simple = mtkcompile(sys) #Jump System @parameters β [unit = u"(mol^2*s)^-1"] γ [unit = u"(mol*s)^-1"] jumpmol [ @@ -211,7 +211,7 @@ end x^2 + y^2 ~ L^2 end end - @mtkbuild pend = PendulumUnits() + @mtkcompile pend = PendulumUnits() u0 = [pend.x => 1.0, pend.y => 0.0] p = [pend.g => 1.0, pend.L => 1.0] guess = [pend.λ => 0.0] @@ -279,7 +279,7 @@ using DynamicQuantities end end -@mtkbuild pend = UnitsExample() +@mtkcompile pend = UnitsExample() @test ModelingToolkit.get_unit.(filter(x -> occursin("ˍt", string(x)), unknowns(pend))) == [u"m/s", u"m/s"] end diff --git a/test/error_handling.jl b/test/error_handling.jl index 6b416d9a6e..6a552ae063 100644 --- a/test/error_handling.jl +++ b/test/error_handling.jl @@ -43,7 +43,7 @@ rc_eqs = [connect(source.p, resistor.p) connect(capacitor.n, source.n)] @named rc_model = System(rc_eqs, t, systems = [resistor, capacitor, source]) -@test_throws ModelingToolkit.ExtraVariablesSystemException structural_simplify(rc_model) +@test_throws ModelingToolkit.ExtraVariablesSystemException mtkcompile(rc_model) @named source2 = OverdefinedConstantVoltage(V = V, I = V / R) rc_eqs2 = [connect(source2.p, resistor.p) @@ -51,4 +51,4 @@ rc_eqs2 = [connect(source2.p, resistor.p) connect(capacitor.n, source2.n)] @named rc_model2 = System(rc_eqs2, t, systems = [resistor, capacitor, source2]) -@test_throws ModelingToolkit.ExtraEquationsSystemException structural_simplify(rc_model2) +@test_throws ModelingToolkit.ExtraEquationsSystemException mtkcompile(rc_model2) diff --git a/test/extensions/ad.jl b/test/extensions/ad.jl index 8764200fac..d6b0cd67e3 100644 --- a/test/extensions/ad.jl +++ b/test/extensions/ad.jl @@ -21,7 +21,7 @@ u0 = [x => zeros(3), ps = [p => zeros(3, 3), q => 1.0] tspan = (0.0, 10.0) -@mtkbuild sys = System(eqs, t) +@mtkcompile sys = System(eqs, t) prob = ODEProblem(sys, u0, tspan, ps) sol = solve(prob, Tsit5()) @@ -47,7 +47,7 @@ end Th0 => (4 / 11)^(1 / 3) * Tγ0, Tγ0 => (15 / π^2 * ργ0 * (2 * h)^2 / 7)^(1 / 4) / 5 ]) - sys = structural_simplify(sys) + sys = mtkcompile(sys) function x_at_0(θ) prob = ODEProblem(sys, [sys.x => 1.0], (0.0, 1.0), [sys.ργ0 => θ[1], sys.h => θ[2]]) @@ -113,7 +113,7 @@ fwd, back = ChainRulesCore.rrule(remake_buffer, sys, ps, idxs, vals) eqs = [D(D(y)) ~ -9.81] initialization_eqs = [y^2 ~ 0] # initialize y = 0 in a way that builds an initialization problem @named sys = System(eqs, t; initialization_eqs) - sys = structural_simplify(sys) + sys = mtkcompile(sys) # Find initial throw velocity that reaches exactly 10 m after 1 s dprob0 = ODEProblem(sys, [D(y) => NaN], (0.0, 1.0), []; guesses = [y => 0.0]) @@ -129,7 +129,7 @@ end @testset "`sys.var` is non-differentiable" begin @variables x(t) - @mtkbuild sys = System(D(x) ~ x, t) + @mtkcompile sys = System(D(x) ~ x, t) prob = ODEProblem(sys, [x => 1.0], (0.0, 1.0)) grad = Zygote.gradient(prob) do prob diff --git a/test/extensions/bifurcationkit.jl b/test/extensions/bifurcationkit.jl index 7b95ffd82e..65c3f2eb37 100644 --- a/test/extensions/bifurcationkit.jl +++ b/test/extensions/bifurcationkit.jl @@ -97,14 +97,14 @@ end # Checks that default parameter values are accounted for. # Checks that observables (that depend on other observables, as in this case) are accounted for. let - # Creates model, and uses `structural_simplify` to generate observables. + # Creates model, and uses `mtkcompile` to generate observables. @parameters μ p=2 @variables x(t) y(t) z(t) eqs = [0 ~ μ - x^3 + 2x^2, 0 ~ p * μ - y, 0 ~ y - z] @named nsys = System(eqs, [x, y, z], [μ, p]) - nsys = structural_simplify(nsys) + nsys = mtkcompile(nsys) # Creates BifurcationProblem. bif_par = μ @@ -149,7 +149,7 @@ let end end - @mtkbuild fol = FOL() + @mtkcompile fol = FOL() par = [fol.τ => 0.0] u0 = [fol.x => -1.0] diff --git a/test/extensions/dynamic_optimization.jl b/test/extensions/dynamic_optimization.jl index 0d4e9d94e2..b9a512707c 100644 --- a/test/extensions/dynamic_optimization.jl +++ b/test/extensions/dynamic_optimization.jl @@ -20,7 +20,7 @@ const M = ModelingToolkit eqs = [D(x(t)) ~ α * x(t) - β * x(t) * y(t), D(y(t)) ~ -γ * y(t) + δ * x(t) * y(t)] - @mtkbuild sys = System(eqs, t) + @mtkcompile sys = System(eqs, t) tspan = (0.0, 1.0) u0map = [x(t) => 4.0, y(t) => 2.0] parammap = [α => 1.5, β => 1.0, γ => 3.0, δ => 1.0] @@ -53,7 +53,7 @@ const M = ModelingToolkit u0map = Pair[] guess = [x(t) => 4.0, y(t) => 2.0] constr = [x(0.6) ~ 3.5, x(0.3) ~ 7.0] - @mtkbuild lksys = System(eqs, t; constraints = constr) + @mtkcompile lksys = System(eqs, t; constraints = constr) jprob = JuMPDynamicOptProblem(lksys, u0map, tspan, parammap; guesses = guess, dt = 0.01) @test JuMP.num_constraints(jprob.model) == 2 @@ -77,7 +77,7 @@ const M = ModelingToolkit # Test whole-interval constraints constr = [x(t) ≳ 1, y(t) ≳ 1] - @mtkbuild lksys = System(eqs, t; constraints = constr) + @mtkcompile lksys = System(eqs, t; constraints = constr) iprob = InfiniteOptDynamicOptProblem( lksys, u0map, tspan, parammap; guesses = guess, dt = 0.01) isol = solve(iprob, Ipopt.Optimizer, @@ -118,7 +118,7 @@ end cost = [-x(1.0)] # Maximize the final distance. @named block = System( [D(x(t)) ~ v(t), D(v(t)) ~ u(t)], t; costs = cost, constraints = constr) - block = structural_simplify(block; inputs = [u(t)]) + block = mtkcompile(block; inputs = [u(t)]) u0map = [x(t) => 0.0, v(t) => 0.0] tspan = (0.0, 1.0) @@ -139,7 +139,7 @@ end # Test dynamics @parameters (u_interp::ConstantInterpolation)(..) - @mtkbuild block_ode = System([D(x(t)) ~ v(t), D(v(t)) ~ u_interp(t)], t) + @mtkcompile block_ode = System([D(x(t)) ~ v(t), D(v(t)) ~ u_interp(t)], t) spline = ctrl_to_spline(jsol.input_sol, ConstantInterpolation) oprob = ODEProblem(block_ode, u0map, tspan, [u_interp => spline]) osol = solve(oprob, Vern8(), dt = 0.01, adaptive = false) @@ -166,7 +166,7 @@ end costs = [-q(tspan[2])] @named beesys = System(eqs, t; costs) - beesys = structural_simplify(beesys; inputs = [α]) + beesys = mtkcompile(beesys; inputs = [α]) u0map = [w(t) => 40, q(t) => 2] pmap = [b => 1, c => 1, μ => 1, s => 1, ν => 1, α => 1] @@ -183,7 +183,7 @@ end @parameters (α_interp::LinearInterpolation)(..) eqs = [D(w(t)) ~ -μ * w(t) + b * s * α_interp(t) * w(t), D(q(t)) ~ -ν * q(t) + c * (1 - α_interp(t)) * s * w(t)] - @mtkbuild beesys_ode = System(eqs, t) + @mtkcompile beesys_ode = System(eqs, t) oprob = ODEProblem(beesys_ode, u0map, tspan, @@ -213,7 +213,7 @@ end costs = [-h(te)] cons = [T(te) ~ 0, m(te) ~ m_c] @named rocket = System(eqs, t; costs, constraints = cons) - rocket = structural_simplify(rocket; inputs = [T(t)]) + rocket = mtkcompile(rocket; inputs = [T(t)]) u0map = [h(t) => h₀, m(t) => m₀, v(t) => 0] pmap = [ @@ -237,7 +237,7 @@ end eqs = [D(h(t)) ~ v(t), D(v(t)) ~ (T_interp(t) - drag(h(t), v(t))) / m(t) - gravity(h(t)), D(m(t)) ~ -T_interp(t) / c] - @mtkbuild rocket_ode = System(eqs, t) + @mtkcompile rocket_ode = System(eqs, t) interpmap = Dict(T_interp => ctrl_to_spline(jsol.input_sol, CubicSpline)) oprob = ODEProblem(rocket_ode, u0map, (ts, te), merge(Dict(pmap), interpmap)) osol = solve(oprob, RadauIIA5(); adaptive = false, dt = 0.001) @@ -261,7 +261,7 @@ end costs = [-Symbolics.Integral(t in (0, tf))(x(t) - u(t)), -x(tf)] consolidate(u, sub) = u[1] + u[2] + sum(sub) @named rocket = System(eqs, t; costs, consolidate) - rocket = structural_simplify(rocket; inputs = [u(t)]) + rocket = mtkcompile(rocket; inputs = [u(t)]) u0map = [x(t) => 17.5] pmap = [u(t) => 0.0, tf => 8] @@ -285,7 +285,7 @@ end @named block = System( [D(x(t)) ~ v(t), D(v(t)) ~ u(t)], t; costs = cost, constraints = constr) - block = structural_simplify(block, inputs = [u(t)]) + block = mtkcompile(block, inputs = [u(t)]) u0map = [x(t) => 1.0, v(t) => 0.0] tspan = (0.0, tf) @@ -328,7 +328,7 @@ end tspan = (0, tf) @named cartpole = System(eqs, t; costs, constraints = cons) - cartpole = structural_simplify(cartpole; inputs = [u]) + cartpole = mtkcompile(cartpole; inputs = [u]) u0map = [D(x(t)) => 0.0, D(θ(t)) => 0.0, θ(t) => 0.0, x(t) => 0.0] pmap = [mₖ => 1.0, mₚ => 0.2, l => 0.5, g => 9.81, u => 0] diff --git a/test/extensions/homotopy_continuation.jl b/test/extensions/homotopy_continuation.jl index 607c27346c..f9abeaa449 100644 --- a/test/extensions/homotopy_continuation.jl +++ b/test/extensions/homotopy_continuation.jl @@ -33,7 +33,7 @@ end eqs = [0 ~ x^2 + y^2 + 2x * y 0 ~ x^2 + 4x + 4 0 ~ y * z + 4x^2] - @mtkbuild sys = System(eqs) + @mtkcompile sys = System(eqs) u0 = [x => 1.0, y => 1.0, z => 1.0] prob = HomotopyContinuationProblem(sys, u0) @test prob isa NonlinearProblem @@ -60,7 +60,7 @@ end 0 ~ x^2 + 4x + q 0 ~ y * z + 4x^2 + wrapper(r)] - @mtkbuild sys = System(eqs) + @mtkcompile sys = System(eqs) prob = HomotopyContinuationProblem(sys, [x => 1.0, y => 1.0, z => 1.0], [p => 2.0, q => 4, r => Wrapper([1.0 1.0; 0.0 0.0])]) @test prob.ps[p] == 2.0 @@ -78,7 +78,7 @@ end @parameters p[1:3] _x = collect(x) eqs = collect(0 .~ vec(sum(_x * _x'; dims = 2)) + collect(p)) - @mtkbuild sys = System(eqs) + @mtkcompile sys = System(eqs) prob = HomotopyContinuationProblem(sys, [x => ones(3)], [p => 1:3]) @test prob[x] == ones(3) @test prob[p + x] == [2, 3, 4] @@ -93,7 +93,7 @@ end @testset "Parametric exponents" begin @variables x = 1.0 @parameters n::Integer = 4 - @mtkbuild sys = System([x^n + x^2 - 1 ~ 0]) + @mtkcompile sys = System([x^n + x^2 - 1 ~ 0]) prob = HomotopyContinuationProblem(sys, []) sol = solve(prob, singlerootalg) test_single_root(sol) @@ -103,34 +103,34 @@ end @testset "Polynomial check and warnings" begin @variables x = 1.0 - @mtkbuild sys = System([x^1.5 + x^2 - 1 ~ 0]) + @mtkcompile sys = System([x^1.5 + x^2 - 1 ~ 0]) @test_throws ["Cannot convert", "Unable", "symbolically solve", "Exponent", "not an integer", "not a polynomial"] HomotopyContinuationProblem( sys, []) - @mtkbuild sys = System([x^x - x ~ 0]) + @mtkcompile sys = System([x^x - x ~ 0]) @test_throws ["Cannot convert", "Unable", "symbolically solve", "Exponent", "unknowns", "not a polynomial"] HomotopyContinuationProblem( sys, []) - @mtkbuild sys = System([((x^2) / sin(x))^2 + x ~ 0]) + @mtkcompile sys = System([((x^2) / sin(x))^2 + x ~ 0]) @test_throws ["Cannot convert", "both polynomial", "non-polynomial", "recognized", "sin", "not a polynomial"] HomotopyContinuationProblem( sys, []) @variables y = 2.0 - @mtkbuild sys = System([x^2 + y^2 + 2 ~ 0, y ~ sin(x)]) + @mtkcompile sys = System([x^2 + y^2 + 2 ~ 0, y ~ sin(x)]) @test_throws ["Cannot convert", "recognized", "sin", "not a polynomial"] HomotopyContinuationProblem( sys, []) - @mtkbuild sys = System([x^2 + y^2 - 2 ~ 0, sin(x + y) ~ 0]) + @mtkcompile sys = System([x^2 + y^2 - 2 ~ 0, sin(x + y) ~ 0]) @test_throws ["Cannot convert", "function of multiple unknowns"] HomotopyContinuationProblem( sys, []) - @mtkbuild sys = System([sin(x)^2 + 1 ~ 0, cos(y) - cos(x) - 1 ~ 0]) + @mtkcompile sys = System([sin(x)^2 + 1 ~ 0, cos(y) - cos(x) - 1 ~ 0]) @test_throws ["Cannot convert", "multiple non-polynomial terms", "same unknown"] HomotopyContinuationProblem( sys, []) - @mtkbuild sys = System([sin(x^2)^2 + sin(x^2) - 1 ~ 0]) + @mtkcompile sys = System([sin(x^2)^2 + sin(x^2) - 1 ~ 0]) @test_throws ["import Nemo"] HomotopyContinuationProblem(sys, []) end @@ -138,7 +138,7 @@ import Nemo @testset "With Nemo" begin @variables x = 2.0 - @mtkbuild sys = System([sin(x^2)^2 + sin(x^2) - 1 ~ 0]) + @mtkcompile sys = System([sin(x^2)^2 + sin(x^2) - 1 ~ 0]) prob = HomotopyContinuationProblem(sys, []) @test prob[1] ≈ 2.0 # singlerootalg doesn't converge @@ -151,8 +151,8 @@ end @variables x=0.25 y=0.125 a = sin(x^2 - 4x + 1) b = cos(3log(y) + 4) - @mtkbuild sys = System([(a^2 - 5a * b + 6b^2) / (a - 0.25) ~ 0 - (a^2 - 0.75a + 0.125) ~ 0]) + @mtkcompile sys = System([(a^2 - 5a * b + 6b^2) / (a - 0.25) ~ 0 + (a^2 - 0.75a + 0.125) ~ 0]) prob = HomotopyContinuationProblem(sys, []) @test prob[x] ≈ 0.25 @test prob[y] ≈ 0.125 @@ -165,7 +165,7 @@ end @testset "Rational functions" begin @variables x=2.0 y=2.0 @parameters n = 5 - @mtkbuild sys = System([ + @mtkcompile sys = System([ 0 ~ (x^2 - n * x + 6) * (x - 1) / (x - 2) / (x - 3) ]) prob = HomotopyContinuationProblem(sys, []) @@ -209,7 +209,7 @@ end @testset "Rational function in observed" begin @variables x=1 y=1 - @mtkbuild sys = System([x^2 + y^2 - 2x - 2 ~ 0, y ~ (x - 1) / (x - 2)]) + @mtkcompile sys = System([x^2 + y^2 - 2x - 2 ~ 0, y ~ (x - 1) / (x - 2)]) prob = HomotopyContinuationProblem(sys, []) @test any(prob.f.denominator([2.0], parameter_values(prob)) .≈ 0.0) @test SciMLBase.successful_retcode(solve(prob, singlerootalg)) @@ -217,7 +217,7 @@ end @testset "Rational function forced to common denominators" begin @variables x = 1 - @mtkbuild sys = System([0 ~ 1 / (1 + x) - x]) + @mtkcompile sys = System([0 ~ 1 / (1 + x) - x]) prob = HomotopyContinuationProblem(sys, []) @test any(prob.f.denominator([-1.0], parameter_values(prob)) .≈ 0.0) sol = solve(prob, singlerootalg) @@ -228,7 +228,7 @@ end @testset "Non-polynomial observed not used in equations" begin @variables x=1 y - @mtkbuild sys = System([x^2 - 2 ~ 0, y ~ sin(x)]) + @mtkcompile sys = System([x^2 - 2 ~ 0, y ~ sin(x)]) prob = HomotopyContinuationProblem(sys, []) sol = @test_nowarn solve(prob, singlerootalg) @test sol[x] ≈ √2.0 diff --git a/test/extensions/test_infiniteopt.jl b/test/extensions/test_infiniteopt.jl index 1a811359d3..6b371a79ae 100644 --- a/test/extensions/test_infiniteopt.jl +++ b/test/extensions/test_infiniteopt.jl @@ -24,7 +24,7 @@ end model = complete(model) inputs = [model.τ] outputs = [model.y] -model = structural_simplify(model; inputs, outputs) +model = mtkcompile(model; inputs, outputs) f, dvs, psym, io_sys = ModelingToolkit.generate_control_function( model, split = false) diff --git a/test/fmi/fmi.jl b/test/fmi/fmi.jl index b2684b4524..29b2dc1224 100644 --- a/test/fmi/fmi.jl +++ b/test/fmi/fmi.jl @@ -17,7 +17,7 @@ const FMU_DIR = joinpath(@__DIR__, "fmus") end @testset "v2, ME" begin fmu = loadFMU("SpringPendulum1D", "Dymola", "2022x"; type = :ME) - @mtkbuild sys = MTK.FMIComponent(Val(2); fmu, type = :ME) + @mtkcompile sys = MTK.FMIComponent(Val(2); fmu, type = :ME) test_no_inputs_outputs(sys) prob = ODEProblem{true, SciMLBase.FullSpecialize}( sys, [sys.mass__s => 0.5, sys.mass__v => 0.0], (0.0, 8.0)) @@ -34,7 +34,7 @@ const FMU_DIR = joinpath(@__DIR__, "fmus") @named inner = MTK.FMIComponent( Val(2); fmu, communication_step_size = 1e-5, type = :CS) @variables x(t) = 1.0 - @mtkbuild sys = System([D(x) ~ x], t; systems = [inner]) + @mtkcompile sys = System([D(x) ~ x], t; systems = [inner]) test_no_inputs_outputs(sys) prob = ODEProblem{true, SciMLBase.FullSpecialize}( @@ -51,7 +51,7 @@ const FMU_DIR = joinpath(@__DIR__, "fmus") fmu, (0.0, 8.0); saveat = 0.0:0.1:8.0, recordValues = ["mass.s", "mass.v"]) @testset "v3, ME" begin fmu = loadFMU("SpringPendulum1D", "Dymola", "2023x", "3.0"; type = :ME) - @mtkbuild sys = MTK.FMIComponent(Val(3); fmu, type = :ME) + @mtkcompile sys = MTK.FMIComponent(Val(3); fmu, type = :ME) test_no_inputs_outputs(sys) prob = ODEProblem{true, SciMLBase.FullSpecialize}( sys, [sys.mass__s => 0.5, sys.mass__v => 0.0], (0.0, 8.0)) @@ -68,7 +68,7 @@ const FMU_DIR = joinpath(@__DIR__, "fmus") @named inner = MTK.FMIComponent( Val(3); fmu, communication_step_size = 1e-5, type = :CS) @variables x(t) = 1.0 - @mtkbuild sys = System([D(x) ~ x], t; systems = [inner]) + @mtkcompile sys = System([D(x) ~ x], t; systems = [inner]) test_no_inputs_outputs(sys) prob = ODEProblem{true, SciMLBase.FullSpecialize}( @@ -120,7 +120,7 @@ end @testset "IO Model" begin function build_simple_adder(adder) @variables a(t) b(t) c(t) [guess = 1.0] - @mtkbuild sys = System( + @mtkcompile sys = System( [adder.a ~ a, adder.b ~ b, D(a) ~ t, D(b) ~ adder.out + adder.c, c^2 ~ adder.out + adder.value], t; @@ -176,7 +176,7 @@ end function build_sspace_model(sspace) @variables u(t)=1.0 x(t)=1.0 y(t) [guess = 1.0] - @mtkbuild sys = System( + @mtkcompile sys = System( [sspace.u ~ u, D(u) ~ t, D(x) ~ sspace.x + sspace.y, y^2 ~ sspace.y + sspace.x], t; systems = [sspace] ) @@ -229,7 +229,7 @@ end @testset "FMUs in a loop" begin function build_looped_adders(adder1, adder2) @variables x(t) = 1 - @mtkbuild sys = System( + @mtkcompile sys = System( [D(x) ~ x, adder1.a ~ adder2.out2, adder2.a ~ adder1.out2, adder1.b ~ 1.0, adder2.b ~ 2.0], t; @@ -274,7 +274,7 @@ end function build_looped_sspace(sspace1, sspace2) @variables x(t) = 1 - @mtkbuild sys = System([D(x) ~ x, sspace1.u ~ sspace2.x, sspace2.u ~ sspace1.y], + @mtkcompile sys = System([D(x) ~ x, sspace1.u ~ sspace2.x, sspace2.u ~ sspace1.y], t; systems = [sspace1, sspace2]) prob = ODEProblem(sys, [sspace1.x => 1.0, sspace2.x => 1.0], (0.0, 1.0)) return sys, prob diff --git a/test/guess_propagation.jl b/test/guess_propagation.jl index f2131e28cb..0be9506bb9 100644 --- a/test/guess_propagation.jl +++ b/test/guess_propagation.jl @@ -11,7 +11,7 @@ eqs = [D(x) ~ 1 initialization_eqs = [1 ~ exp(1 + x)] @named sys = System(eqs, t; initialization_eqs) -sys = complete(structural_simplify(sys)) +sys = complete(mtkcompile(sys)) tspan = (0.0, 0.2) prob = ODEProblem(sys, [], tspan, []) @@ -28,7 +28,7 @@ eqs = [D(x) ~ 1 initialization_eqs = [1 ~ exp(1 + x)] @named sys = System(eqs, t; initialization_eqs) -sys = complete(structural_simplify(sys)) +sys = complete(mtkcompile(sys)) tspan = (0.0, 0.2) prob = ODEProblem(sys, [], tspan, []) @@ -46,7 +46,7 @@ eqs = [D(x) ~ a] initialization_eqs = [1 ~ exp(1 + x)] @named sys = System(eqs, t; initialization_eqs) -sys = complete(structural_simplify(sys)) +sys = complete(mtkcompile(sys)) tspan = (0.0, 0.2) prob = ODEProblem(sys, [], tspan, []) @@ -66,7 +66,7 @@ eqs = [D(x) ~ a, initialization_eqs = [1 ~ exp(1 + x)] @named sys = System(eqs, t; initialization_eqs) -sys = complete(structural_simplify(sys)) +sys = complete(mtkcompile(sys)) tspan = (0.0, 0.2) prob = ODEProblem(sys, [], tspan, []) @@ -80,7 +80,7 @@ sol = solve(prob.f.initializeprob; show_trace = Val(true)) @parameters x0 @variables x(t) @variables y(t) = x -@mtkbuild sys = System([x ~ x0, D(y) ~ x], t) +@mtkcompile sys = System([x ~ x0, D(y) ~ x], t) prob = ODEProblem(sys, [], (0.0, 1.0), [x0 => 1.0]) @test prob[x] == 1.0 @test prob[y] == 1.0 @@ -88,7 +88,7 @@ prob = ODEProblem(sys, [], (0.0, 1.0), [x0 => 1.0]) @parameters x0 @variables x(t) @variables y(t) = x0 -@mtkbuild sys = System([x ~ x0, D(y) ~ x], t) +@mtkcompile sys = System([x ~ x0, D(y) ~ x], t) prob = ODEProblem(sys, [], (0.0, 1.0), [x0 => 1.0]) @test prob[x] == 1.0 @test prob[y] == 1.0 @@ -96,7 +96,7 @@ prob = ODEProblem(sys, [], (0.0, 1.0), [x0 => 1.0]) @parameters x0 @variables x(t) @variables y(t) = x0 -@mtkbuild sys = System([x ~ y, D(y) ~ x], t) +@mtkcompile sys = System([x ~ y, D(y) ~ x], t) prob = ODEProblem(sys, [], (0.0, 1.0), [x0 => 1.0]) @test prob[x] == 1.0 @test prob[y] == 1.0 @@ -104,7 +104,7 @@ prob = ODEProblem(sys, [], (0.0, 1.0), [x0 => 1.0]) @parameters x0 @variables x(t) = x0 @variables y(t) = x -@mtkbuild sys = System([x ~ y, D(y) ~ x], t) +@mtkcompile sys = System([x ~ y, D(y) ~ x], t) prob = ODEProblem(sys, [], (0.0, 1.0), [x0 => 1.0]) @test prob[x] == 1.0 @test prob[y] == 1.0 diff --git a/test/hierarchical_initialization_eqs.jl b/test/hierarchical_initialization_eqs.jl index e9ea36947e..fef9953438 100644 --- a/test/hierarchical_initialization_eqs.jl +++ b/test/hierarchical_initialization_eqs.jl @@ -122,7 +122,7 @@ HTML as well. end """Run model RLCModel from 0 to 10""" function simple() - @mtkbuild model = RLCModel() + @mtkcompile model = RLCModel() u0 = [] prob = ODEProblem(model, u0, (0.0, 10.0)) sol = solve(prob) @@ -140,7 +140,7 @@ syslist = ModelingToolkit.get_systems(model) @test length(ModelingToolkit.initialization_equations(model)) == 2 u0 = [] -prob = ODEProblem(structural_simplify(model), u0, (0.0, 10.0)) +prob = ODEProblem(mtkcompile(model), u0, (0.0, 10.0)) sol = solve(prob, Rodas5P()) @test length(sol.u[end]) == 2 @test length(equations(prob.f.initializeprob.f.sys)) == 0 diff --git a/test/if_lifting.jl b/test/if_lifting.jl index 085dc600c8..1fcb5947e4 100644 --- a/test/if_lifting.jl +++ b/test/if_lifting.jl @@ -13,9 +13,9 @@ using ModelingToolkit: t_nounits as t, D_nounits as D, IfLifting, no_if_lift end end @named sys = SimpleAbs() - ss1 = structural_simplify(sys) + ss1 = mtkcompile(sys) @test length(equations(ss1)) == 1 - ss2 = structural_simplify(sys, additional_passes = [IfLifting]) + ss2 = mtkcompile(sys, additional_passes = [IfLifting]) @test length(equations(ss2)) == 1 @test length(parameters(ss2)) == 1 @test operation(only(equations(ss2)).rhs) === ifelse @@ -71,7 +71,7 @@ end end @named sys = BigModel() - ss = structural_simplify(sys, additional_passes = [IfLifting]) + ss = mtkcompile(sys, additional_passes = [IfLifting]) ps = parameters(ss) @test length(ps) == 9 @@ -111,7 +111,7 @@ end end end -@testset "`@mtkbuild` macro accepts `additional_passes`" begin +@testset "`@mtkcompile` macro accepts `additional_passes`" begin @mtkmodel SimpleAbs begin @variables begin x(t) @@ -122,7 +122,7 @@ end y ~ sin(t) end end - @test_nowarn @mtkbuild sys=SimpleAbs() additional_passes=[IfLifting] + @test_nowarn @mtkcompile sys=SimpleAbs() additional_passes=[IfLifting] end @testset "Nested conditions are handled properly" begin @@ -144,8 +144,8 @@ end D(x) ~ y end end - @mtkbuild sys = RampModel() - @mtkbuild sys2=RampModel() additional_passes=[IfLifting] + @mtkcompile sys = RampModel() + @mtkcompile sys2=RampModel() additional_passes=[IfLifting] prob = ODEProblem(sys, [sys.x => 1.0], (0.0, 3.0)) prob2 = ODEProblem(sys2, [sys.x => 1.0], (0.0, 3.0)) sol = solve(prob) diff --git a/test/implicit_discrete_system.jl b/test/implicit_discrete_system.jl index 49ab2a5921..59e4a94977 100644 --- a/test/implicit_discrete_system.jl +++ b/test/implicit_discrete_system.jl @@ -7,7 +7,7 @@ rng = StableRNG(22525) @testset "Correct ImplicitDiscreteFunction" begin @variables x(t) = 1 - @mtkbuild sys = System([x(k) ~ x(k) * x(k - 1) - 3], t) + @mtkcompile sys = System([x(k) ~ x(k) * x(k - 1) - 3], t) tspan = (0, 10) # u[2] - u_next[1] @@ -27,7 +27,7 @@ rng = StableRNG(22525) prob = ImplicitDiscreteProblem(sys, [], tspan) @test prob.u0 == [1.0, 1.0] @variables x(t) - @mtkbuild sys = System([x(k) ~ x(k) * x(k - 1) - 3], t) + @mtkcompile sys = System([x(k) ~ x(k) * x(k - 1) - 3], t) @test_throws ModelingToolkit.MissingVariablesError prob=ImplicitDiscreteProblem( sys, [], tspan) end @@ -36,7 +36,7 @@ end @variables x(t) y(t) eqs = [x(k) ~ x(k - 1) + x(k - 2), x^2 ~ 1 - y^2] - @mtkbuild sys = System(eqs, t) + @mtkcompile sys = System(eqs, t) f = ImplicitDiscreteFunction(sys) function correct_f(u_next, u, p, t) @@ -63,7 +63,7 @@ end eqs = [x(k) ~ x(k - 1) + x(k - 2), y(k) ~ x(k) + x(k - 2) * z(k - 1), x + y + z ~ 2] - @mtkbuild sys = System(eqs, t) + @mtkcompile sys = System(eqs, t) @test length(unknowns(sys)) == length(equations(sys)) == 3 @test occursin( "var\"y(t)\"", string(ImplicitDiscreteFunction(sys; expression = Val{true}))) @@ -72,7 +72,7 @@ end eqs = [z(k) ~ x(k) + sin(x(k)), y(k) ~ x(k - 1) + x(k - 2), z(k) * x(k) ~ 3] - @mtkbuild sys = System(eqs, t) + @mtkcompile sys = System(eqs, t) @test occursin("var\"Shift(t, 1)(z(t))\"", string(ImplicitDiscreteFunction(sys; expression = Val{true}))) end diff --git a/test/initial_values.jl b/test/initial_values.jl index cc75c2e41e..347f794e8d 100644 --- a/test/initial_values.jl +++ b/test/initial_values.jl @@ -7,13 +7,13 @@ using SymbolicIndexingInterface @variables x(t)[1:3]=[1.0, 2.0, 3.0] y(t) z(t)[1:2] -@mtkbuild sys=System([D(x) ~ t * x], t) simplify=false +@mtkcompile sys=System([D(x) ~ t * x], t) simplify=false @test get_u0(sys, [])[1] == [1.0, 2.0, 3.0] @test get_u0(sys, [x => [2.0, 3.0, 4.0]])[1] == [2.0, 3.0, 4.0] @test get_u0(sys, [x[1] => 2.0, x[2] => 3.0, x[3] => 4.0])[1] == [2.0, 3.0, 4.0] @test get_u0(sys, [2.0, 3.0, 4.0])[1] == [2.0, 3.0, 4.0] -@mtkbuild sys=System([ +@mtkcompile sys=System([ D(x) ~ 3x, D(y) ~ t, D(z[1]) ~ z[2] + t, @@ -56,7 +56,7 @@ vals = ModelingToolkit.varmap_to_vars(var_vals, desired_values; defaults = defau @parameters k1 k2 Γ[1:1]=X1 + X2 eq = D(X1) ~ -k1 * X1 + k2 * (-X1 + Γ[1]) obs = X2 ~ Γ[1] - X1 -@mtkbuild osys_m = System([eq], t, [X1], [k1, k2, Γ[1]]; observed = [X2 ~ Γ[1] - X1]) +@mtkcompile osys_m = System([eq], t, [X1], [k1, k2, Γ[1]]; observed = [X2 ~ Γ[1] - X1]) # Creates ODEProblem. u0 = [X1 => 1.0, X2 => 2.0] @@ -77,14 +77,14 @@ target_varmap = Dict(p => ones(3), q => 2ones(3), q[1] => 2.0, q[2] => 2.0, q[3] # Issue#1283 @variables z(t)[1:2, 1:2] eqs = [D(D(z)) ~ ones(2, 2)] -@mtkbuild sys = System(eqs, t) +@mtkcompile sys = System(eqs, t) @test_nowarn ODEProblem(sys, [z => zeros(2, 2), D(z) => ones(2, 2)], (0.0, 10.0)) # Initialization with defaults involving parameters that are not part of the system # Issue#2817 @parameters A1 A2 B1 B2 @variables x1(t) x2(t) -@mtkbuild sys = System( +@mtkcompile sys = System( [ x1 ~ B1, x2 ~ B2 @@ -108,14 +108,14 @@ end # Issue#2799 @variables x(t) @parameters p -@mtkbuild sys = System([D(x) ~ p], t; defaults = [x => t, p => 2t]) +@mtkcompile sys = System([D(x) ~ p], t; defaults = [x => t, p => 2t]) prob = ODEProblem(sys, [], (1.0, 2.0), []) @test prob[x] == 1.0 @test prob.ps[p] == 2.0 @testset "Array of symbolics is unwrapped" begin @variables x(t)[1:2] y(t) - @mtkbuild sys = System([D(x) ~ x, D(y) ~ t], t; defaults = [x => [y, 3.0]]) + @mtkcompile sys = System([D(x) ~ x, D(y) ~ t], t; defaults = [x => [y, 3.0]]) prob = ODEProblem(sys, [y => 1.0], (0.0, 1.0)) @test eltype(prob.u0) <: Float64 prob = ODEProblem(sys, [x => [y, 4.0], y => 2.0], (0.0, 1.0)) @@ -125,7 +125,7 @@ end @testset "split=false systems with all parameter defaults" begin @variables x(t) = 1.0 @parameters p=1.0 q=2.0 r=3.0 - @mtkbuild sys=System(D(x) ~ p * x + q * t + r, t) split=false + @mtkcompile sys=System(D(x) ~ p * x + q * t + r, t) split=false prob = @test_nowarn ODEProblem(sys, [], (0.0, 1.0)) @test prob.p isa Vector{Float64} end @@ -137,7 +137,7 @@ end y ~ ifelse(t < c1, 0.0, (-c1 + t)^(c3))] sps = [x, y] ps = [c1, c2, c3] - @mtkbuild osys = System(eqs, t, sps, ps) + @mtkcompile osys = System(eqs, t, sps, ps) u0map = [x => 1.0] pmap = [c1 => 5.0, c2 => 1.0, c3 => 1.2] oprob = ODEProblem(osys, u0map, (0.0, 10.0), pmap) @@ -145,7 +145,7 @@ end @testset "Cyclic dependency checking and substitution limits" begin @variables x(t) y(t) - @mtkbuild sys = System( + @mtkcompile sys = System( [D(x) ~ x, D(y) ~ y], t; initialization_eqs = [x ~ 2y + 3, y ~ 2x], guesses = [x => 2y, y => 2x]) @test_warn ["Cycle", "unknowns", "x", "y"] try @@ -156,7 +156,7 @@ end sys, [x => 2y + 1, y => 2x], (0.0, 1.0); build_initializeprob = false) @parameters p q - @mtkbuild sys = System( + @mtkcompile sys = System( [D(x) ~ x * p, D(y) ~ y * q], t; guesses = [p => 1.0, q => 2.0]) # "unknowns" because they are initialization unknowns @test_warn ["Cycle", "unknowns", "p", "q"] try @@ -171,7 +171,7 @@ end @testset "`add_fallbacks!` checks scalarized array parameters correctly" begin @variables x(t)[1:2] @parameters p[1:2, 1:2] - @mtkbuild sys = System(D(x) ~ p * x, t) + @mtkcompile sys = System(D(x) ~ p * x, t) # used to throw a `MethodError` complaining about `getindex(::Nothing, ::CartesianIndex{2})` @test_throws ModelingToolkit.MissingParametersError ODEProblem( sys, [x => ones(2)], (0.0, 1.0)) @@ -185,7 +185,7 @@ end y[1] ~ x[3], y[2] ~ x[4] ] - @mtkbuild sys = System(eqs, t; defaults = [x => vcat(ones(2), y), y => x[1:2] ./ 2]) + @mtkcompile sys = System(eqs, t; defaults = [x => vcat(ones(2), y), y => x[1:2] ./ 2]) prob = ODEProblem(sys, [], (0.0, 1.0)) sol = solve(prob) @test SciMLBase.successful_retcode(sol) @@ -198,7 +198,7 @@ end eqs = [D(D(x)) ~ λ * x D(D(y)) ~ λ * y - g x^2 + y^2 ~ 1] - @mtkbuild pend = System(eqs, t) + @mtkcompile pend = System(eqs, t) @test_throws ModelingToolkit.MissingGuessError ODEProblem( pend, [x => 1], (0, 1), [g => 1], guesses = [y => λ, λ => y + 1]) @@ -207,7 +207,7 @@ end # Throw multiple if multiple are missing @variables a(t) b(t) c(t) d(t) e(t) eqs = [D(a) ~ b, D(b) ~ c, D(c) ~ d, D(d) ~ e, D(e) ~ 1] - @mtkbuild sys = System(eqs, t) + @mtkcompile sys = System(eqs, t) @test_throws ["a(t)", "c(t)"] ODEProblem( sys, [e => 2, a => b, b => a + 1, c => d, d => c + 1], (0, 1)) end @@ -219,7 +219,7 @@ end @variables x(t) @parameters (interp::Tspline)(..) - @mtkbuild sys = System(D(x) ~ interp(t), t) + @mtkcompile sys = System(D(x) ~ interp(t), t) prob = ODEProblem(sys, [x => 0.0], (0.0, 1.0), [interp => spline]) spline2 = LinearInterpolation(ts .^ 2, ts .^ 2) @@ -248,7 +248,7 @@ end @parameters p d @variables X(t) eqs = [D(X) ~ p - d * X] - @mtkbuild osys = System(eqs, t) + @mtkcompile osys = System(eqs, t) u0 = [X => 1.0f0] ps = [p => 1.0f0, d => 2.0f0] oprob = ODEProblem(osys, u0, (0.0f0, 1.0f0), ps) @@ -260,7 +260,7 @@ end @testset "Array initials and scalar parameters with `split = false`" begin @variables x(t)[1:2] @parameters p - @mtkbuild sys=System([D(x[1]) ~ x[1], D(x[2]) ~ x[2] + p], t) split=false + @mtkcompile sys=System([D(x[1]) ~ x[1], D(x[2]) ~ x[2] + p], t) split=false ps = Set(parameters(sys; initial_parameters = true)) @test length(ps) == 5 for i in 1:2 @@ -284,7 +284,7 @@ end 0 ~ x^2 + y^2 - w2^2 ] - @mtkbuild sys = System(eqs, t) + @mtkcompile sys = System(eqs, t) u0 = [D(x) => 2.0, x => 1.0, @@ -309,7 +309,7 @@ end D(y) ~ x * (ρ - z) - y, D(z) ~ x * y - β * z] - @mtkbuild sys = System(eqs, t) + @mtkcompile sys = System(eqs, t) u0 = SA[D(x) => 2.0f0, x => 1.0f0, @@ -332,7 +332,7 @@ end eqs = [D(D(x)) ~ λ * x D(D(y)) ~ λ * y - g x^2 + y^2 ~ 1] - @mtkbuild pend = System(eqs, t) + @mtkcompile pend = System(eqs, t) u0 = [x => 1.0, D(x) => 0.0] u0_constructor = p_constructor = vals -> SVector{length(vals)}(vals...) @@ -345,7 +345,7 @@ end @test state_values(initdata.initializeprob) isa SVector @test parameter_values(initdata.initializeprob).tunable isa SVector - @mtkbuild pend=System(eqs, t) split=false + @mtkcompile pend=System(eqs, t) split=false prob = ODEProblem(pend, u0, tspan; u0_constructor, p_constructor) @test prob.p isa SVector initdata = prob.f.initialization_data diff --git a/test/initializationsystem.jl b/test/initializationsystem.jl index cca4be1f76..8c1634e4e4 100644 --- a/test/initializationsystem.jl +++ b/test/initializationsystem.jl @@ -11,7 +11,7 @@ using DynamicQuantities eqs = [D(D(x)) ~ λ * x D(D(y)) ~ λ * y - g x^2 + y^2 ~ 1] -@mtkbuild pend = System(eqs, t) +@mtkcompile pend = System(eqs, t) initprob = ModelingToolkit.InitializationProblem(pend, 0.0, [], [g => 1]; guesses = [ModelingToolkit.missing_variable_defaults(pend); x => 1; y => 0.2]) @@ -235,7 +235,7 @@ end end end -@mtkbuild sys = HydraulicSystem() +@mtkcompile sys = HydraulicSystem() initprob = ModelingToolkit.InitializationProblem(sys, 0.0) conditions = getfield.(equations(initprob.f.sys), :rhs) @@ -323,7 +323,7 @@ end end end -@mtkbuild sys = MassDamperSystem() +@mtkcompile sys = MassDamperSystem() initprob = ModelingToolkit.InitializationProblem(sys, 0.0) @test initprob isa NonlinearProblem initsol = solve(initprob, reltol = 1e-12, abstol = 1e-12) @@ -348,7 +348,7 @@ eqs = [D(D(x)) ~ σ * (y - x), D(y) ~ x * (ρ - z) - y, D(z) ~ x * y - β * z] -@mtkbuild sys = System(eqs, t) +@mtkcompile sys = System(eqs, t) u0 = [D(x) => 2.0, y => 0.0, @@ -378,7 +378,7 @@ function System2(; name) return System(eqs, t, vars, []; name) end -@mtkbuild sys = System2() +@mtkcompile sys = System2() prob = ODEProblem(sys, [sys.dx => 1], (0, 1)) # OK prob = ODEProblem(sys, [sys.ddx => -2], (0, 1), guesses = [sys.dx => 1]) sol = solve(prob, Tsit5()) @@ -400,7 +400,7 @@ function System3(; name) return System(eqs, t, vars, []; name, initialization_eqs) end -@mtkbuild sys = System3() +@mtkcompile sys = System3() prob = ODEProblem(sys, [], (0, 1), guesses = [sys.dx => 1]) sol = solve(prob, Tsit5()) @test SciMLBase.successful_retcode(sol) @@ -416,7 +416,7 @@ sol = solve(prob, Tsit5()) D(z) ~ x * y - β * z] @named sys = System(eqs, t) - sys = structural_simplify(sys) + sys = mtkcompile(sys) u0 = [D(x) => 2.0, x => 1.0, @@ -445,7 +445,7 @@ eqs = [D(x) ~ α * x - β * x * y z ~ x + y] @named sys = System(eqs, t) -simpsys = structural_simplify(sys) +simpsys = mtkcompile(sys) tspan = (0.0, 10.0) prob = ODEProblem(simpsys, [D(x) => 0.0, y => 0.0], tspan, guesses = [x => 0.0]) @@ -472,13 +472,13 @@ sol = solve(prob, Tsit5()) eqs = [D(D(x)) ~ λ * x D(D(y)) ~ λ * y - g x^2 + y^2 ~ 1] -@mtkbuild pend = System(eqs, t) +@mtkcompile pend = System(eqs, t) prob = ODEProblem(pend, [x => 1], (0.0, 1.5), [g => 1], guesses = [λ => 0, y => 1], initialization_eqs = [y ~ 1]) unsimp = generate_initializesystem(pend; u0map = [x => 1], initialization_eqs = [y ~ 1]) -sys = structural_simplify(unsimp; fully_determined = false) +sys = mtkcompile(unsimp; fully_determined = false) @test length(equations(sys)) in (3, 4) # could be either depending on tearing # Extend two systems with initialization equations and guesses @@ -494,7 +494,7 @@ sys = extend(sysx, sysy) @testset "Error on missing defaults" begin @variables x(t) y(t) @named sys = System([x^2 + y^2 ~ 25, D(x) ~ 1], t) - ssys = structural_simplify(sys) + ssys = mtkcompile(sys) @test_throws ModelingToolkit.MissingVariablesError ODEProblem( ssys, [x => 3], (0, 1), []) # y should have a guess end @@ -505,7 +505,7 @@ end # system 1 should solve to x = 1 ics1 = [x => 1] - sys1 = System([D(x) ~ 0], t; defaults = ics1, name = :sys1) |> structural_simplify + sys1 = System([D(x) ~ 0], t; defaults = ics1, name = :sys1) |> mtkcompile prob1 = ODEProblem(sys1, [], (0.0, 1.0), []) sol1 = solve(prob1, Tsit5()) @test all(sol1[x] .== 1) @@ -514,7 +514,7 @@ end sys2 = extend( sys1, System([D(y) ~ 0], t; initialization_eqs = [y ~ 2], name = :sys2) - ) |> structural_simplify + ) |> mtkcompile ics2 = unknowns(sys1) .=> 2 # should be equivalent to "ics2 = [x => 2]" prob2 = ODEProblem(sys2, ics2, (0.0, 1.0), []; fully_determined = true) sol2 = solve(prob2, Tsit5()) @@ -526,12 +526,12 @@ end @variables x(t) sys = System( [D(D(x)) ~ 0], t; initialization_eqs = [x ~ 0, D(x) ~ 1], name = :sys) |> - structural_simplify + mtkcompile @test_nowarn ODEProblem(sys, [], (0.0, 1.0), []) sys = System( [D(D(x)) ~ 0], t; initialization_eqs = [x ~ 0, D(D(x)) ~ 0], name = :sys) |> - structural_simplify + mtkcompile @test_nowarn ODEProblem(sys, [D(x) => 1.0], (0.0, 1.0), []) end @@ -542,7 +542,7 @@ end sys = System( [D(D(x)) ~ 0], t; initialization_eqs = [D(x)^2 ~ 1, x ~ 0], guesses = [D(x) => sign], name = :sys - ) |> structural_simplify + ) |> mtkcompile prob = ODEProblem(sys, [], (0.0, 1.0), []) sol = solve(prob, Tsit5()) @test sol(1.0, idxs = sys.x) ≈ sign # system with D(x(0)) = ±1 should solve to x(1) = ±1 @@ -556,8 +556,8 @@ eqs_1st_order = [D(Y) + Y - ω ~ 0, X + k1 ~ Y + k2] eqs_2nd_order = [D(D(Y)) + 2ω * D(Y) + (ω^2) * Y ~ 0, X + k1 ~ Y + k2] -@mtkbuild sys_1st_order = System(eqs_1st_order, t) -@mtkbuild sys_2nd_order = System(eqs_2nd_order, t) +@mtkcompile sys_1st_order = System(eqs_1st_order, t) +@mtkcompile sys_2nd_order = System(eqs_2nd_order, t) u0_1st_order_1 = [X => 1.0, Y => 2.0] u0_1st_order_2 = [Y => 2.0] @@ -583,7 +583,7 @@ sol = solve(oprob_2nd_order_2, Rosenbrock23()) # retcode: Success @testset "Vector in initial conditions" begin @variables x(t)[1:5] y(t)[1:5] @named sys = System([D(x) ~ x, D(y) ~ y], t; initialization_eqs = [y ~ -x]) - sys = structural_simplify(sys) + sys = mtkcompile(sys) prob = ODEProblem(sys, [sys.x => ones(5)], (0.0, 1.0), []) sol = solve(prob, Tsit5(), reltol = 1e-4) @test all(sol(1.0, idxs = sys.x) .≈ +exp(1)) && all(sol(1.0, idxs = sys.y) .≈ -exp(1)) @@ -595,7 +595,7 @@ end @brownian a b x = _x(t) sarray_ctor = splat(SVector) - # `System` constructor creates appropriate type with mtkbuild + # `System` constructor creates appropriate type with mtkcompile # `Problem` and `alg` create the problem to test and allow calling `init` with # the correct solver. # `rhss` allows adding terms to the end of equations (only 2 equations allowed) to influence @@ -635,7 +635,7 @@ end pmap = Dict() pmap[q] = 1.0 # `missing` default, equation from Problem - @mtkbuild sys = System( + @mtkcompile sys = System( [D(x) ~ x * q + rhss[1], D(y) ~ y * p + rhss[2]], t; defaults = [p => missing], guesses = [p => 1.0]) pmap[p] = 2q prob = Problem(sys, u0map, (0.0, 1.0), pmap; u0_constructor, p_constructor) @@ -644,7 +644,7 @@ end prob2 = remake(prob2; p = setp_oop(prob2, p)(prob2, 0.0)) test_parameter(prob2, p, 2.0) # `missing` default, provided guess - @mtkbuild sys = System( + @mtkcompile sys = System( [D(x) ~ x + rhss[1], p ~ x + y + rhss[2]], t; defaults = [p => missing], guesses = [p => 0.0]) prob = Problem(sys, u0map, (0.0, 1.0); u0_constructor, p_constructor) test_parameter(prob, p, 2.0) @@ -654,7 +654,7 @@ end test_parameter(prob2, p, 2.0) # `missing` to Problem, equation from default - @mtkbuild sys = System( + @mtkcompile sys = System( [D(x) ~ x * q + rhss[1], D(y) ~ y * p + rhss[2]], t; defaults = [p => 2q], guesses = [p => 1.0]) pmap[p] = missing prob = Problem(sys, u0map, (0.0, 1.0), pmap; u0_constructor, p_constructor) @@ -664,7 +664,7 @@ end prob2 = remake(prob2; p = setp_oop(prob2, p)(prob2, 0.0)) test_parameter(prob2, p, 2.0) # `missing` to Problem, provided guess - @mtkbuild sys = System( + @mtkcompile sys = System( [D(x) ~ x + rhss[1], p ~ x + y + rhss[2]], t; guesses = [p => 0.0]) prob = Problem(sys, u0map, (0.0, 1.0), pmap; u0_constructor, p_constructor) test_parameter(prob, p, 2.0) @@ -674,7 +674,7 @@ end test_parameter(prob2, p, 2.0) # No `missing`, default and guess - @mtkbuild sys = System( + @mtkcompile sys = System( [D(x) ~ x * q + rhss[1], D(y) ~ y * p + rhss[2]], t; defaults = [p => 2q], guesses = [p => 0.0]) delete!(pmap, p) prob = Problem(sys, u0map, (0.0, 1.0), pmap; u0_constructor, p_constructor) @@ -685,14 +685,14 @@ end test_parameter(prob2, p, 2.0) # Default overridden by Problem, guess provided - @mtkbuild sys = System( + @mtkcompile sys = System( [D(x) ~ q * x + rhss[1], D(y) ~ y * p + rhss[2]], t; defaults = [p => 2q], guesses = [p => 1.0]) _pmap = merge(pmap, Dict(p => q)) prob = Problem(sys, u0map, (0.0, 1.0), _pmap; u0_constructor, p_constructor) test_parameter(prob, p, _pmap[q]) test_initializesystem(prob, p, p ~ q) # Problem dependent value with guess, no `missing` - @mtkbuild sys = System( + @mtkcompile sys = System( [D(x) ~ y * q + p + rhss[1], D(y) ~ x * p + q + rhss[2]], t; guesses = [p => 0.0]) _pmap = merge(pmap, Dict(p => 3q)) prob = Problem(sys, u0map, (0.0, 1.0), _pmap; u0_constructor, p_constructor) @@ -700,7 +700,7 @@ end # Should not be solved for: # Override dependent default with direct value - @mtkbuild sys = System( + @mtkcompile sys = System( [D(x) ~ q * x + rhss[1], D(y) ~ y * p + rhss[2]], t; defaults = [p => 2q], guesses = [p => 1.0]) _pmap = merge(pmap, Dict(p => 1.0)) prob = Problem(sys, u0map, (0.0, 1.0), _pmap; u0_constructor, p_constructor) @@ -710,7 +710,7 @@ end # Non-floating point @parameters r::Int s::Int - @mtkbuild sys = System( + @mtkcompile sys = System( [D(x) ~ s * x + rhss[1], D(y) ~ y * r + rhss[2]], t; defaults = [s => 2r], guesses = [s => 1.0]) prob = Problem(sys, u0map, (0.0, 1.0), [r => 1]; u0_constructor, p_constructor) @test prob.ps[r] == 1 @@ -719,7 +719,7 @@ end @test is_parameter(initsys, r) @test is_parameter(initsys, s) - @mtkbuild sys = System( + @mtkcompile sys = System( [D(x) ~ x + rhss[1], p ~ x + y + rhss[2]], t; guesses = [p => 0.0]) @test_throws ModelingToolkit.MissingParametersError Problem( sys, [x => 1.0, y => 1.0], (0.0, 1.0)) @@ -736,7 +736,7 @@ end @testset "Null system" begin @variables x(t) y(t) s(t) @parameters x0 y0 - @mtkbuild sys = System([x ~ x0, y ~ y0, s ~ x + y], t; guesses = [y0 => 0.0]) + @mtkcompile sys = System([x ~ x0, y ~ y0, s ~ x + y], t; guesses = [y0 => 0.0]) prob = ODEProblem(sys, [s => 1.0], (0.0, 1.0), [x0 => 0.3, y0 => missing]) # trivial initialization run immediately @test prob.ps[y0] ≈ 0.7 @@ -756,7 +756,7 @@ end @named gravity = Force() @named constant = Constant(; k = 9.81) @named damper = TM.Damper(; d = 0.1) - @mtkbuild sys = System( + @mtkcompile sys = System( [connect(fixed.flange, spring.flange_a), connect(spring.flange_b, mass.flange_a), connect(mass.flange_a, gravity.flange), connect(constant.output, gravity.f), connect(fixed.flange, damper.flange_a), connect(damper.flange_b, mass.flange_a)], @@ -782,7 +782,7 @@ end eqs = [0 ~ σ * (y - x), 0 ~ x * (ρ - z) - y, 0 ~ x * y - β * z] - @mtkbuild ns = System(eqs, [x, y, z], [σ, ρ, β]) + @mtkcompile ns = System(eqs, [x, y, z], [σ, ρ, β]) prob = NonlinearProblem(ns, []) @test prob.f.initialization_data.update_initializeprob! === nothing @@ -834,7 +834,7 @@ end 0 ~ p * (-x + (q - z) * x)] @named sys = System(eqs; initialization_eqs = [p^2 + q^2 + 2p * q ~ 0]) sys = complete(sys) - # @mtkbuild sys = NonlinearSystem( + # @mtkcompile sys = NonlinearSystem( # [p * x^2 + q * y^3 ~ 0, x - q ~ 0]; defaults = [q => missing], # guesses = [q => 1.0], initialization_eqs = [p^2 + q^2 + 2p * q ~ 0]) @@ -889,7 +889,7 @@ end (ModelingToolkit.System, DDEProblem, MethodOfSteps(Tsit5()), [_x(t - 0.1), 0.0]), (ModelingToolkit.System, SDDEProblem, ImplicitEM(), [_x(t - 0.1) + a, b]) ] - @mtkbuild sys = System( + @mtkcompile sys = System( [D(x) ~ x + rhss[1], p ~ x + y + rhss[2]], t; guesses = [x => 0.0, p => 0.0]) prob = Problem(sys, [y => 1.0], (0.0, 1.0), [p => 3.0]) @test prob.f.initialization_data.initializeprob.ps[p] ≈ 3.0 @@ -918,7 +918,7 @@ end (ModelingToolkit.System, DDEProblem, MethodOfSteps(Tsit5()), _x(t - 0.1)), (ModelingToolkit.System, SDDEProblem, ImplicitEM(), _x(t - 0.1) + a) ] - @mtkbuild sys = System( + @mtkcompile sys = System( [D(x) ~ 2x + r + rhss], t; parameter_dependencies = [r ~ p + 2q, q ~ p + 3], guesses = [p => 1.0]) prob = Problem(sys, [x => 1.0], (0.0, 1.0), [p => missing]) @@ -941,7 +941,7 @@ end (DDEProblem, MethodOfSteps(Tsit5()), [_x(t - 0.1), 0.0]), (SDDEProblem, ImplicitEM(), [_x(t - 0.1) + a, b]) ] - @mtkbuild sys = System( + @mtkcompile sys = System( [D(x) ~ x + rhss[1], p ~ x + y + rhss[2]], t; defaults = [p => missing], guesses = [ x => 0.0, p => 0.0]) prob = Problem(sys, [x => 1.0, y => 1.0], (0.0, 1.0)) @@ -975,7 +975,7 @@ end ] alge_eqs = [y^2 * q + q^2 * x ~ 0, z * p - p^2 * x * z ~ 0] - @mtkbuild sys = System( + @mtkcompile sys = System( [D(x) ~ x * p + y^2 * q + rhss; alge_eqs], t; guesses = [x => 0.0, y => 0.0, z => 0.0, p => 0.0, q => 0.0]) prob = Problem(sys, [x => 1.0], (0.0, 1.0), [p => 1.0, q => missing]) @@ -1031,7 +1031,7 @@ end (ModelingToolkit.System, SDDEProblem, ImplicitEM(), _x(t - 0.1) + a) ] alge_eqs = [y^2 + 4y * p^2 ~ x^3] - @mtkbuild sys = System( + @mtkcompile sys = System( [D(x) ~ x + p * y^2 + rhss; alge_eqs], t; guesses = [ y => 1.0, p => 1.0]) prob = Problem(sys, [x => 1.0], (0.0, 1.0), [p => 1.0]) @@ -1060,7 +1060,7 @@ end @testset "Nonnumeric parameter dependencies are retained" begin @variables x(t) y(t) @parameters foo(::Real, ::Real) p - @mtkbuild sys = System([D(x) ~ t, 0 ~ foo(x, y)], t; + @mtkcompile sys = System([D(x) ~ t, 0 ~ foo(x, y)], t; parameter_dependencies = [foo ~ Multiplier(p, 2p)], guesses = [y => -1.0]) prob = ODEProblem(sys, [x => 1.0], (0.0, 1.0), [p => 1.0]) integ = init(prob, Rosenbrock23()) @@ -1069,7 +1069,7 @@ end @testset "Use observed equations for guesses of observed variables" begin @variables x(t) y(t) [state_priority = 100] - @mtkbuild sys = System( + @mtkcompile sys = System( [D(x) ~ x + t, y ~ 2x + 1], t; initialization_eqs = [x^3 + y^3 ~ 1]) isys = ModelingToolkit.generate_initializesystem(sys) @test isequal(defaults(isys)[y], 2x + 1) @@ -1077,14 +1077,14 @@ end @testset "Create initializeprob when unknown has dependent value" begin @variables x(t) y(t) - @mtkbuild sys = System([D(x) ~ x, D(y) ~ t * y], t; defaults = [x => 2y]) + @mtkcompile sys = System([D(x) ~ x, D(y) ~ t * y], t; defaults = [x => 2y]) prob = ODEProblem(sys, [y => 1.0], (0.0, 1.0)) @test prob.f.initializeprob !== nothing integ = init(prob) @test integ[x] ≈ 2.0 @variables x(t)[1:2] y(t) - @mtkbuild sys = System([D(x) ~ x, D(y) ~ t], t; defaults = [x => [y, 3.0]]) + @mtkcompile sys = System([D(x) ~ x, D(y) ~ t], t; defaults = [x => [y, 3.0]]) prob = ODEProblem(sys, [y => 1.0], (0.0, 1.0)) @test prob.f.initializeprob !== nothing integ = init(prob) @@ -1099,7 +1099,7 @@ end eqs = [D(D(x)) ~ λ * x D(D(y)) ~ λ * y - g x^2 + y^2 ~ L] - @mtkbuild pend = System(eqs, t) + @mtkcompile pend = System(eqs, t) prob = ODEProblem(pend, [x => 1, y => 0], (0.0, 1.5), [g => 1], guesses = ModelingToolkit.missing_variable_defaults(pend)) @@ -1170,7 +1170,7 @@ end end model = dc_motor() - sys = structural_simplify(model) + sys = mtkcompile(model) prob = ODEProblem(sys, [sys.L1.i => 0.0], (0, 6.0)) @@ -1183,7 +1183,7 @@ end eqs = [D(D(x)) ~ λ * x D(D(y)) ~ λ * y - g x^2 + y^2 ~ 1] - @mtkbuild pend = System(eqs, t) + @mtkcompile pend = System(eqs, t) @test_warn ["structurally singular", "initialization", "Guess", "heuristic"] ODEProblem( pend, [x => 1, y => 0], (0.0, 1.5), [g => 1], guesses = [λ => 1]) end @@ -1191,7 +1191,7 @@ end @testset "DAEProblem initialization" begin @variables x(t) [guess = 1.0] y(t) [guess = 1.0] @parameters p=missing [guess = 1.0] q=missing [guess = 1.0] - @mtkbuild sys = System( + @mtkcompile sys = System( [D(x) ~ p * y + q * t, x^3 + y^3 ~ 5], t; initialization_eqs = [p^2 + q^3 ~ 3]) # FIXME: solve for du0 @@ -1208,7 +1208,7 @@ end @testset "Guesses provided to `ODEProblem` are used in `remake`" begin @variables x(t) y(t)=2x @parameters p q=3x - @mtkbuild sys = System([D(x) ~ x * p + q, x^3 + y^3 ~ 3], t) + @mtkcompile sys = System([D(x) ~ x * p + q, x^3 + y^3 ~ 3], t) prob = ODEProblem( sys, [], (0.0, 1.0), [p => 1.0]; guesses = [x => 1.0, y => 1.0, q => 1.0]) @test prob[x] == 1.0 @@ -1238,7 +1238,7 @@ end @testset "Remake problem with no initializeprob" begin @variables x(t) [guess = 1.0] y(t) [guess = 1.0] @parameters p [guess = 1.0] q [guess = 1.0] - @mtkbuild sys = System( + @mtkcompile sys = System( [D(x) ~ p * x + q * y, y ~ 2x], t; parameter_dependencies = [q ~ 2p]) prob = ODEProblem(sys, [x => 1.0], (0.0, 1.0), [p => 1.0]) test_dummy_initialization_equation(prob, x) @@ -1259,7 +1259,7 @@ end @testset "Variables provided as symbols" begin @variables x(t) [guess = 1.0] y(t) [guess = 1.0] @parameters p [guess = 1.0] q [guess = 1.0] - @mtkbuild sys = System( + @mtkcompile sys = System( [D(x) ~ p * x + q * y, y ~ 2x], t; parameter_dependencies = [q ~ 2p]) prob = ODEProblem(sys, [:x => 1.0], (0.0, 1.0), [p => 1.0]) test_dummy_initialization_equation(prob, x) @@ -1275,7 +1275,7 @@ end @parameters a = 1 @named sys = System([D(x) ~ 0, D(y) ~ x + a], t; initialization_eqs = [y ~ a]) - ssys = structural_simplify(sys) + ssys = mtkcompile(sys) prob = ODEProblem(ssys, [], (0, 1), []) @test SciMLBase.successful_retcode(solve(prob)) @@ -1294,7 +1294,7 @@ end D(X) ~ p - d * X, D(Y) ~ p - d * Y ] - @mtkbuild osys = System(eqs, t) + @mtkcompile osys = System(eqs, t) # Make problem. u0_vals = [X => 4, Y => 5.0] @@ -1321,7 +1321,7 @@ end j₁ = ConstantRateJump(rate₁, affect₁) j₂ = ConstantRateJump(rate₂, affect₂) j₃ = MassActionJump(2 * β + γ, [R => 1], [S => 1, R => -1]) - @mtkbuild js = JumpSystem([j₁, j₂, j₃], t, [S, I, R], [β, γ, S0]) + @mtkcompile js = JumpSystem([j₁, j₂, j₃], t, [S, I, R], [β, γ, S0]) u0s = [I => 1, R => 0] ps = [S0 => 999, β => 0.01, γ => 0.001] @@ -1335,7 +1335,7 @@ end @testset "Solvable array parameters with scalarized guesses" begin @variables x(t) @parameters p[1:2] q - @mtkbuild sys = System( + @mtkcompile sys = System( D(x) ~ p[1] + p[2] + q, t; defaults = [p[1] => q, p[2] => 2q], guesses = [p[1] => q, p[2] => 2q]) @test ModelingToolkit.is_parameter_solvable(p, Dict(), defaults(sys), guesses(sys)) @@ -1349,7 +1349,7 @@ end @testset "Issue#3318: Mutating `Initial` parameters works" begin @variables x(t) y(t)[1:2] [guess = ones(2)] @parameters p[1:2, 1:2] - @mtkbuild sys = System( + @mtkcompile sys = System( [D(x) ~ x, D(y) ~ p * y], t; initialization_eqs = [x^2 + y[1]^2 + y[2]^2 ~ 4]) prob = ODEProblem(sys, [x => 1.0, y[1] => 1], (0.0, 1.0), [p => 2ones(2, 2)]) integ = init(prob, Tsit5()) @@ -1375,7 +1375,7 @@ end continuous_events = [ [y ~ 0.5] => (; f = stop!) ]) - sys = structural_simplify(sys) + sys = mtkcompile(sys) prob0 = ODEProblem(sys, [x => NaN], (0.0, 1.0), []) # final_x(x0) is equivalent to x0 + 0.5 @@ -1390,7 +1390,7 @@ end @testset "Issue#3330: Initialization for unsimplified systems" begin @variables x(t) [guess = 1.0] - @mtkbuild sys = System(D(x) ~ x, t; initialization_eqs = [x^2 ~ 4]) + @mtkcompile sys = System(D(x) ~ x, t; initialization_eqs = [x^2 ~ 4]) prob = ODEProblem(sys, [], (0.0, 1.0)) @test prob.f.initialization_data !== nothing end @@ -1398,7 +1398,7 @@ end @testset "`ReconstructInitializeprob` with `nothing` state" begin @parameters p @variables x(t) - @mtkbuild sys = System(x ~ p * t, t) + @mtkcompile sys = System(x ~ p * t, t) prob = @test_nowarn ODEProblem(sys, [], (0.0, 1.0), [p => 1.0]) @test_nowarn remake(prob, p = [p => 1.0]) @test_nowarn remake(prob, p = [p => ForwardDiff.Dual(1.0)]) @@ -1412,7 +1412,7 @@ end D(X1) ~ k1 * (Γ[1] - X1) - k2 * X1 ] obs = [X2 ~ Γ[1] - X1] - @mtkbuild osys = System(eqs, t, [X1, X2], [k1, k2, Γ]; observed = obs) + @mtkcompile osys = System(eqs, t, [X1, X2], [k1, k2, Γ]; observed = obs) u0 = [X1 => 1.0, X2 => 2.0] ps = [k1 => 0.1, k2 => 0.2] @@ -1433,14 +1433,14 @@ end (DDEProblem, D, _x(t - 0.1)), (SDDEProblem, D, _x(t - 0.1) + a) ] - @mtkbuild sys = ModelingToolkit.System([lhs(x) ~ x + rhs, x + y ~ tot], t; + @mtkcompile sys = ModelingToolkit.System([lhs(x) ~ x + rhs, x + y ~ tot], t; guesses = [tot => 1.0], defaults = [tot => missing]) prob = Problem(sys, [x => 1.0, y => 1.0], (0.0, 1.0)) @test prob.ps[tot] ≈ 2.0 end @testset "$Problem" for Problem in [NonlinearProblem, NonlinearLeastSquaresProblem] @parameters p1 p2 - @mtkbuild sys = System([x^2 + y^2 ~ p1, (x - 1)^2 + (y - 1)^2 ~ p2]; + @mtkcompile sys = System([x^2 + y^2 ~ p1, (x - 1)^2 + (y - 1)^2 ~ p2]; parameter_dependencies = [p2 ~ 2p1], guesses = [p1 => 0.0], defaults = [p1 => missing]) prob = Problem(sys, [x => 1.0, y => 1.0], [p2 => 6.0]) @@ -1459,7 +1459,7 @@ end initialization_eqs = [ X2 ~ Γ[1] - X1 ] - @mtkbuild nlsys = System(eqs, [X1, X2], [k1, k2, Γ]; initialization_eqs) + @mtkcompile nlsys = System(eqs, [X1, X2], [k1, k2, Γ]; initialization_eqs) @testset "throws if initialization_eqs contain unknowns" begin u0 = [X1 => 1.0, X2 => 2.0] @@ -1472,7 +1472,7 @@ end initialization_eqs = [ Initial(X2) ~ Γ[1] - Initial(X1) ] - @mtkbuild nlsys = System(eqs, [X1, X2], [k1, k2, Γ]; initialization_eqs) + @mtkcompile nlsys = System(eqs, [X1, X2], [k1, k2, Γ]; initialization_eqs) @testset "solves initialization" begin u0 = [X1 => 1.0, X2 => 2.0] @@ -1510,7 +1510,7 @@ end @testset "Issue#3504: Update initials when `remake` called with non-symbolic `u0`" begin @variables x(t) y(t) @parameters c1 c2 - @mtkbuild sys = System([D(x) ~ -c1 * x + c2 * y, D(y) ~ c1 * x - c2 * y], t) + @mtkcompile sys = System([D(x) ~ -c1 * x + c2 * y, D(y) ~ c1 * x - c2 * y], t) prob1 = ODEProblem(sys, [1.0, 2.0], (0.0, 1.0), [c1 => 1.0, c2 => 2.0]) prob2 = remake(prob1, u0 = [2.0, 3.0]) prob3 = remake(prob1, u0 = [2.0, 3.0], p = [c1 => 2.0]) @@ -1539,7 +1539,7 @@ end eqs = [D(D(x)) ~ λ * x D(D(y)) ~ λ * y - g x^2 + y^2 ~ 1] - @mtkbuild pend = System(eqs, t) + @mtkcompile pend = System(eqs, t) prob = ODEProblem( pend, [x => (√2 / 2), D(x) => 0.0], (0.0, 1.5), @@ -1594,7 +1594,7 @@ end 0 ~ x^2 + y^2 - w2^2 ] - @mtkbuild sys = System(eqs, t) + @mtkcompile sys = System(eqs, t) u0 = [D(x) => 2.0, x => 1.0, @@ -1625,7 +1625,7 @@ end eqs = [D(D(x)) ~ λ * x D(D(y)) ~ λ * y - g x^2 + y^2 ~ 1] - @mtkbuild pend=System(eqs, t) split=false + @mtkcompile pend=System(eqs, t) split=false prob = ODEProblem(pend, [x => 1.0, D(x) => 0.0], (0.0, 1.0), [g => 1.0]; guesses = [y => 1.0, λ => 1.0]) @test !ModelingToolkit.is_split(prob.f.initialization_data.initializeprob.f.sys) @@ -1637,7 +1637,7 @@ end eqs = [D(D(x)) ~ λ * x D(D(y)) ~ λ * y - g x^2 + y^2 ~ 1] - @mtkbuild pend = System(eqs, t) + @mtkcompile pend = System(eqs, t) prob = ODEProblem(pend, SA[x => 1.0, D(x) => 0.0], (0.0, 1.0), SA[g => 1.0]; guesses = [y => 1.0, λ => 1.0]) @test !SciMLBase.isinplace(prob) diff --git a/test/input_output_handling.jl b/test/input_output_handling.jl index 1f14bc3814..c295546248 100644 --- a/test/input_output_handling.jl +++ b/test/input_output_handling.jl @@ -7,10 +7,10 @@ using ModelingToolkit: t_nounits as t, D_nounits as D @variables xx(t) some_input(t) [input = true] eqs = [D(xx) ~ some_input] @named model = System(eqs, t) -@test_throws ExtraVariablesSystemException structural_simplify(model) +@test_throws ExtraVariablesSystemException mtkcompile(model) if VERSION >= v"1.8" err = "In particular, the unset input(s) are:\n some_input(t)" - @test_throws err structural_simplify(model) + @test_throws err mtkcompile(model) end # Test input handling @@ -50,7 +50,7 @@ end @test !is_bound(sys31, sys1.v[2]) # simplification turns input variables into parameters -ssys = structural_simplify(sys, inputs = [u], outputs = []) +ssys = mtkcompile(sys, inputs = [u], outputs = []) @test ModelingToolkit.isparameter(unbound_inputs(ssys)[]) @test !is_bound(ssys, u) @test u ∈ Set(unbound_inputs(ssys)) @@ -88,7 +88,7 @@ fsys4 = flatten(sys4) @variables x(t) y(t) [output = true] @test isoutput(y) @named sys = System([D(x) ~ -x, y ~ x], t) # both y and x are unbound -syss = structural_simplify(sys, outputs = [y]) # This makes y an observed variable +syss = mtkcompile(sys, outputs = [y]) # This makes y an observed variable @named sys2 = System([D(x) ~ -sys.x, y ~ sys.y], t, systems = [sys]) @@ -106,7 +106,7 @@ syss = structural_simplify(sys, outputs = [y]) # This makes y an observed variab @test isequal(unbound_outputs(sys2), [y]) @test isequal(bound_outputs(sys2), [sys.y]) -syss = structural_simplify(sys2, outputs = [sys.y]) +syss = mtkcompile(sys2, outputs = [sys.y]) @test !is_bound(syss, y) @test !is_bound(syss, x) @@ -165,7 +165,7 @@ end ] @named sys = System(eqs, t) - sys = structural_simplify(sys, inputs = [u]) + sys = mtkcompile(sys, inputs = [u]) f, dvs, ps, io_sys = ModelingToolkit.generate_control_function(sys; simplify, split) @test isequal(dvs[], x) @@ -183,7 +183,7 @@ end ] @named sys = System(eqs, t) - sys = structural_simplify(sys, inputs = [u], disturbance_inputs = [d]) + sys = mtkcompile(sys, inputs = [u], disturbance_inputs = [d]) f, dvs, ps, io_sys = ModelingToolkit.generate_control_function(sys; simplify, split) @test isequal(dvs[], x) @@ -201,7 +201,7 @@ end ] @named sys = System(eqs, t) - sys = structural_simplify(sys, inputs = [u], disturbance_inputs = [d]) + sys = mtkcompile(sys, inputs = [u], disturbance_inputs = [d]) f, dvs, ps, io_sys = ModelingToolkit.generate_control_function( sys; simplify, split, disturbance_argument = true) @@ -267,7 +267,7 @@ eqs = [connect_sd(sd, mass1, mass2) @named _model = System(eqs, t) @named model = compose(_model, mass1, mass2, sd); -model = structural_simplify(model, inputs = [u]) +model = mtkcompile(model, inputs = [u]) f, dvs, ps, io_sys = ModelingToolkit.generate_control_function(model, simplify = true) @test length(dvs) == 4 p = MTKParameters(io_sys, [io_sys.u => NaN]) @@ -283,7 +283,7 @@ i = findfirst(isequal(u[1]), out) @variables x(t) u(t) [input = true] eqs = [D(x) ~ u] @named sys = System(eqs, t) -@test_nowarn structural_simplify(sys, inputs = [u], outputs = []) +@test_nowarn mtkcompile(sys, inputs = [u], outputs = []) #= ## Disturbance input handling @@ -368,7 +368,7 @@ eqs = [D(y₁) ~ -k₁ * y₁ + k₃ * y₂ * y₃ + u1 @named sys = System(eqs, t) m_inputs = [u[1], u[2]] m_outputs = [y₂] -sys_simp = structural_simplify(sys, inputs = m_inputs, outputs = m_outputs) +sys_simp = mtkcompile(sys, inputs = m_inputs, outputs = m_outputs) @test isequal(unknowns(sys_simp), collect(x[1:2])) @test length(inputs(sys_simp)) == 2 @@ -386,7 +386,7 @@ sys_simp = structural_simplify(sys, inputs = m_inputs, outputs = m_outputs) ], t, systems = [int, gain, c, fb]) -sys = structural_simplify(model) +sys = mtkcompile(model) @test length(unknowns(sys)) == length(equations(sys)) == 1 ## Disturbance models when plant has multiple inputs @@ -435,7 +435,7 @@ matrices = ModelingToolkit.reorder_unknowns( ] @named sys = System(eqs, t) - sys = structural_simplify(sys, inputs = [u]) + sys = mtkcompile(sys, inputs = [u]) (; io_sys,) = ModelingToolkit.generate_control_function(sys, simplify = true) obsfn = ModelingToolkit.build_explicit_observed_function( io_sys, [x + u * t]; inputs = [u]) @@ -447,7 +447,7 @@ end @constants c = 2.0 @variables x(t) eqs = [D(x) ~ c * x] - @mtkbuild sys = System(eqs, t, [x], [c]) + @mtkcompile sys = System(eqs, t, [x], [c]) f, dvs, ps, io_sys = ModelingToolkit.generate_control_function(sys) @test f[1]([0.5], nothing, MTKParameters(io_sys, []), 0.0) ≈ [1.0] @@ -458,7 +458,7 @@ end @parameters p(::Real) = (x -> 2x) eqs = [D(x) ~ -x + p(u)] @named sys = System(eqs, t) - sys = structural_simplify(sys, inputs = [u]) + sys = mtkcompile(sys, inputs = [u]) f, dvs, ps, io_sys = ModelingToolkit.generate_control_function(sys) p = MTKParameters(io_sys, []) u = [1.0] diff --git a/test/jacobiansparsity.jl b/test/jacobiansparsity.jl index d10ec66c44..56e8edc183 100644 --- a/test/jacobiansparsity.jl +++ b/test/jacobiansparsity.jl @@ -91,7 +91,7 @@ prob = ODEProblem(sys, u0, (0, 11.5), sparse = true, jac = true) eqs = [D(D(x)) ~ λ * x D(D(y)) ~ λ * y - g x^2 + y^2 ~ 1] - @mtkbuild pend = System(eqs, t) + @mtkcompile pend = System(eqs, t) u0 = [x => 1, y => 0] prob = ODEProblem( @@ -125,7 +125,7 @@ end eqs = [D(D(x)) ~ λ * x D(D(y)) ~ λ * y - g x^2 + y^2 ~ 1] - @mtkbuild pend = System(eqs, t) + @mtkcompile pend = System(eqs, t) prob = ODEProblem(pend, [x => 0.0, D(x) => 1.0], (0.0, 1.0), [g => 1.0]; guesses = [y => 1.0, λ => 1.0], jac = true, sparse = true) J = deepcopy(prob.f.jac_prototype) diff --git a/test/jumpsystem.jl b/test/jumpsystem.jl index b48b17ce60..95cc29e7b0 100644 --- a/test/jumpsystem.jl +++ b/test/jumpsystem.jl @@ -279,7 +279,7 @@ ps = [k => 1.0] @test_nowarn jp3 = JumpProblem(js3, u0, tspan, ps; aggregator = Direct()) @test_nowarn jp4 = JumpProblem(js4, u0, tspan; aggregator = Direct()) -# Ensure `structural_simplify` (and `@mtkbuild`) works on JumpSystem (by doing nothing) +# Ensure `mtkcompile` (and `@mtkcompile`) works on JumpSystem (by doing nothing) # Issue#2558 @parameters k @variables X(t) @@ -287,7 +287,7 @@ rate = k affect = [X ~ Pre(X) - 1] j1 = ConstantRateJump(k, [X ~ Pre(X) - 1]) -@test_nowarn @mtkbuild js1 = JumpSystem([j1], t, [X], [k]) +@test_nowarn @mtkcompile js1 = JumpSystem([j1], t, [X], [k]) # test correct autosolver is selected, which implies appropriate dep graphs are available @testset "Autosolver test" begin @@ -550,7 +550,7 @@ end j2 = ConstantRateJump(rate2, affect2) # Works. - @mtkbuild js = JumpSystem([j1, j2], t, [X], [p, d]) + @mtkcompile js = JumpSystem([j1, j2], t, [X], [p, d]) jprob = JumpProblem( js, [X => 15], (0.0, 10.0), [p => 2.0, d => 0.5]; aggregator = Direct()) sol = solve(jprob, SSAStepper()) diff --git a/test/model_parsing.jl b/test/model_parsing.jl index 88fa7faac7..1da6ac6928 100644 --- a/test/model_parsing.jl +++ b/test/model_parsing.jl @@ -152,7 +152,7 @@ end C_val = 20u"F" R_val = 20u"Ω" res__R = 100u"Ω" -@mtkbuild rc = RC(; C_val, R_val, resistor.R = res__R) +@mtkcompile rc = RC(; C_val, R_val, resistor.R = res__R) prob = ODEProblem(rc, [], (0, 1e9)) sol = solve(prob) defs = ModelingToolkit.defaults(rc) @@ -488,7 +488,7 @@ using ModelingToolkit: D_nounits end end - @mtkbuild model = M() + @mtkcompile model = M() u0 = [model.x => 10, model.y => 0, model.z => 0] prob = ODEProblem(model, u0, (0, 5.0)) diff --git a/test/modelingtoolkitize.jl b/test/modelingtoolkitize.jl index 0157a50acb..f6b4dc8a8d 100644 --- a/test/modelingtoolkitize.jl +++ b/test/modelingtoolkitize.jl @@ -373,7 +373,7 @@ sys = modelingtoolkitize(prob) @testset "ODE" begin @variables x(t)=1.0 y(t)=2.0 @parameters p=3.0 q=4.0 - @mtkbuild sys = System([D(x) ~ p * y, D(y) ~ q * x], t) + @mtkcompile sys = System([D(x) ~ p * y, D(y) ~ q * x], t) prob1 = ODEProblem(sys, [], (0.0, 5.0)) newsys = complete(modelingtoolkitize(prob1)) @test is_variable(newsys, newsys.x) @@ -389,7 +389,7 @@ sys = modelingtoolkitize(prob) @testset "Nonlinear" begin @variables x=1.0 y=2.0 @parameters p=3.0 q=4.0 - @mtkbuild nlsys = System([0 ~ p * y^2 + x, 0 ~ x + exp(x) * q]) + @mtkcompile nlsys = System([0 ~ p * y^2 + x, 0 ~ x + exp(x) * q]) prob1 = NonlinearProblem(nlsys, []) newsys = complete(modelingtoolkitize(prob1)) @test is_variable(newsys, newsys.x) @@ -409,7 +409,7 @@ sys = modelingtoolkitize(prob) end @parameters p=3.0 q=4.0 loss = (p - x)^2 + q * (y - x^2)^2 - @mtkbuild optsys = OptimizationSystem(loss, [x, y], [p, q]) + @mtkcompile optsys = OptimizationSystem(loss, [x, y], [p, q]) prob1 = OptimizationProblem(optsys, [], grad = true, hess = true) newsys = complete(modelingtoolkitize(prob1)) @test is_variable(newsys, newsys.x) @@ -438,7 +438,7 @@ prob = NonlinearLeastSquaresProblem( NonlinearFunction(nlls!, resid_prototype = zeros(3)), u0) sys = modelingtoolkitize(prob) @test length(equations(sys)) == 3 -@test length(equations(structural_simplify(sys; fully_determined = false))) == 0 +@test length(equations(mtkcompile(sys; fully_determined = false))) == 0 @testset "`modelingtoolkitize(::SDEProblem)` sets defaults" begin function sdeg!(du, u, p, t) diff --git a/test/mtkparameters.jl b/test/mtkparameters.jl index 347043d437..afd97a5984 100644 --- a/test/mtkparameters.jl +++ b/test/mtkparameters.jl @@ -140,7 +140,7 @@ end @parameters p::Vector{Float64} @variables X(t) eq = D(X) ~ p[1] - p[2] * X -@mtkbuild osys = System([eq], t) +@mtkcompile osys = System([eq], t) u0 = [X => 1.0] ps = [p => [2.0, 0.1]] @@ -160,7 +160,7 @@ newps = remake_buffer(sys, ps, (p,), (1.0f0,)) @parameters p d @variables X(t) eqs = [D(X) ~ p - d * X] -@mtkbuild sys = System(eqs, t) +@mtkcompile sys = System(eqs, t) u0 = [X => 1.0] tspan = (0.0, 100.0) @@ -180,7 +180,7 @@ function level1() eqs = [D(x) ~ p1 * x - p2 * x * y D(y) ~ -p3 * y + p4 * x * y] - sys = structural_simplify(complete(System( + sys = mtkcompile(complete(System( eqs, t, name = :sys, parameter_dependencies = [y0 => 2p4]))) prob = ODEProblem{true, SciMLBase.FullSpecialize}(sys, [], (0.0, 3.0)) end @@ -194,7 +194,7 @@ function level2() eqs = [D(x) ~ p1 * x - p23[1] * x * y D(y) ~ -p23[2] * y + p4 * x * y] - sys = structural_simplify(complete(System( + sys = mtkcompile(complete(System( eqs, t, name = :sys, parameter_dependencies = [y0 => 2p4]))) prob = ODEProblem{true, SciMLBase.FullSpecialize}(sys, [], (0.0, 3.0)) end @@ -208,7 +208,7 @@ function level3() eqs = [D(x) ~ p1 * x - p23[1] * x * y D(y) ~ -p23[2] * y + p4 * x * y] - sys = structural_simplify(complete(System( + sys = mtkcompile(complete(System( eqs, t, name = :sys, parameter_dependencies = [y0 => 2p4]))) prob = ODEProblem{true, SciMLBase.FullSpecialize}(sys, [], (0.0, 3.0)) end @@ -248,7 +248,7 @@ end @variables x(t) y(t) eqs = [D(x) ~ (α - β * y) * x D(y) ~ (δ * x - γ) * y] -@mtkbuild odesys = System(eqs, t) +@mtkcompile odesys = System(eqs, t) odeprob = ODEProblem( odesys, [x => 1.0, y => 1.0], (0.0, 10.0), [α => 1.5, β => 1.0, γ => 3.0, δ => 1.0]) tunables, _... = canonicalize(Tunable(), odeprob.p) @@ -323,7 +323,7 @@ end D(V[1]) ~ k[1] - k[2] * V[1], D(V[2]) ~ k[3] - k[4] * V[2] ] - @mtkbuild osys_scal = System(eqs, t, [V[1], V[2]], [k[1], k[2], k[3], k[4]]) + @mtkcompile osys_scal = System(eqs, t, [V[1], V[2]], [k[1], k[2], k[3], k[4]]) u0 = [V => [10.0, 20.0]] ps_vec = [k => [2.0, 3.0, 4.0, 5.0]] diff --git a/test/nonlinearsystem.jl b/test/nonlinearsystem.jl index d7c8c15f6c..8575fbc36a 100644 --- a/test/nonlinearsystem.jl +++ b/test/nonlinearsystem.jl @@ -117,7 +117,7 @@ using OrdinaryDiffEq D = Differential(t) @named subsys = convert_system_indepvar(lorenz1, t) @named sys = System([D(subsys.x) ~ subsys.x + subsys.x], t, systems = [subsys]) -sys = structural_simplify(sys) +sys = mtkcompile(sys) u0 = [subsys.x => 1, subsys.z => 2.0, subsys.y => 1.0] prob = ODEProblem(sys, u0, (0, 1.0), [subsys.σ => 1, subsys.ρ => 2, subsys.β => 3]) sol = solve(prob, FBDF(), reltol = 1e-7, abstol = 1e-7) @@ -195,7 +195,7 @@ eq = [v1 ~ sin(2pi * t * h) v2 ~ i2 i1 ~ i2] @named sys = System(eq, t) -@test length(equations(structural_simplify(sys))) == 0 +@test length(equations(mtkcompile(sys))) == 0 @testset "Remake" begin @parameters a=1.0 b=1.0 c=1.0 @@ -229,7 +229,7 @@ end @named ns = System(eqs, [x, y, z], []) ns = complete(ns) vs = [unknowns(ns); parameters(ns)] - ss_mtk = structural_simplify(ns) + ss_mtk = mtkcompile(ns) prob = NonlinearProblem(ss_mtk, vs .=> 1.0) sol = solve(prob) @test_nowarn sol[unknowns(ns)] @@ -249,16 +249,16 @@ sys = @test_nowarn System(alg_eqs; name = :name) @parameters u3 u4 eqs = [u3 ~ u1 + u2, u4 ~ 2 * (u1 + u2), u3 + u4 ~ 3 * (u1 + u2)] @named ns = System(eqs, [u1, u2], [u3, u4]) -sys = structural_simplify(ns; fully_determined = false) +sys = mtkcompile(ns; fully_determined = false) @test length(unknowns(sys)) == 1 # Conservative @variables X(t) alg_eqs = [1 ~ 2X] @named ns = System(alg_eqs) -sys = structural_simplify(ns) +sys = mtkcompile(ns) @test length(equations(sys)) == 0 -sys = structural_simplify(ns; conservative = true) +sys = mtkcompile(ns; conservative = true) @test length(equations(sys)) == 1 # https://github.com/SciML/ModelingToolkit.jl/issues/2858 @@ -270,7 +270,7 @@ sys = structural_simplify(ns; conservative = true) 0 ~ x * y - β * z] guesses = [x => 1.0, z => 0.0] ps = [σ => 10.0, ρ => 26.0, β => 8 / 3] - @mtkbuild ns = System(eqs) + @mtkcompile ns = System(eqs) @test isequal(calculate_jacobian(ns), [(-1 - z + ρ)*σ -x*σ 2x*(-z + ρ) -β-(x^2)]) @@ -287,7 +287,7 @@ sys = structural_simplify(ns; conservative = true) # system that contains a chain of observed variables when simplified @variables x y z eqs = [0 ~ x^2 + 2z + y, z ~ y, y ~ x] # analytical solution x = y = z = 0 or -3 - @mtkbuild ns = System(eqs) # solve for y with observed chain z -> y -> x + @mtkcompile ns = System(eqs) # solve for y with observed chain z -> y -> x @test isequal(expand.(calculate_jacobian(ns)), [-3 // 2 - x;;]) @test isequal(calculate_hessian(ns), [[-1;;]]) prob = NonlinearProblem(ns, unknowns(ns) .=> -4.0) # give guess < -3 to reach -3 @@ -297,7 +297,7 @@ end @testset "Passing `nothing` to `u0`" begin @variables x = 1 - @mtkbuild sys = System([0 ~ x^2 - x^3 + 3]) + @mtkcompile sys = System([0 ~ x^2 - x^3 + 3]) prob = @test_nowarn NonlinearProblem(sys, nothing) @test_nowarn solve(prob) end @@ -310,7 +310,7 @@ end -1 1/2 -1] b = [1, -2, 0] @named sys = System(A * x ~ b, [x], []) - sys = structural_simplify(sys) + sys = mtkcompile(sys) prob = NonlinearProblem(sys, unknowns(sys) .=> 0.0) sol = solve(prob) @test all(sol[x] .≈ A \ b) @@ -321,8 +321,8 @@ end @parameters p @named sys = System([x ~ 1, x^2 - p ~ 0]) for sys in [ - structural_simplify(sys, fully_determined = false), - structural_simplify(sys, fully_determined = false, split = false) + mtkcompile(sys, fully_determined = false), + mtkcompile(sys, fully_determined = false, split = false) ] @test length(equations(sys)) == 1 @test length(unknowns(sys)) == 0 @@ -348,7 +348,7 @@ end end @variables y - @mtkbuild sys = System([0 ~ x * x - p * x + p, 0 ~ x * y + p]) + @mtkcompile sys = System([0 ~ x * x - p * x + p, 0 ~ x * y + p]) @test_throws ["single unknown"] IntervalNonlinearProblem(sys, (0.0, 1.0)) @test_throws ["single unknown"] IntervalNonlinearFunction(sys) @test_throws ["single unknown"] IntervalNonlinearProblem( @@ -361,7 +361,7 @@ end @variables x y @parameters p[1:2] (f::Function)(..) - @mtkbuild sys = System([x^2 - p[1]^2 ~ 0, y^2 ~ f(p)]) + @mtkcompile sys = System([x^2 - p[1]^2 ~ 0, y^2 ~ f(p)]) @test !any(isequal(p[1]), parameters(sys)) @test is_parameter(sys, p) end @@ -397,7 +397,7 @@ end @test ModelingToolkit.iscomplete(nlsys) @test ModelingToolkit.is_split(nlsys) - sys3 = structural_simplify(sys) + sys3 = mtkcompile(sys) nlsys = NonlinearSystem(sys3) @test length(equations(nlsys)) == length(ModelingToolkit.observed(nlsys)) == 1 diff --git a/test/odesystem.jl b/test/odesystem.jl index ec3e52fe3a..463f459df9 100644 --- a/test/odesystem.jl +++ b/test/odesystem.jl @@ -391,7 +391,7 @@ sys = complete(sys) @testset "Issue#1109" begin @variables x(t)[1:3, 1:3] @named sys = System(D(x) ~ x, t) - @test_nowarn structural_simplify(sys) + @test_nowarn mtkcompile(sys) end # Array vars @@ -402,7 +402,7 @@ ps = @parameters p[1:3] = [1, 2, 3] eqs = [collect(D.(x) .~ x) D(y) ~ norm(collect(x)) * y - x[1]] @named sys = System(eqs, t, sts, ps) -sys = structural_simplify(sys) +sys = mtkcompile(sys) @test isequal(@nonamespace(sys.x), x) @test isequal(@nonamespace(sys.y), y) @test isequal(@nonamespace(sys.p), p) @@ -453,7 +453,7 @@ foo(a, ms::AbstractVector) = a + sum(ms) eqs = [D(x) ~ foo(x, ms); D(ms) ~ ones(3)] @named sys = System(eqs, t, [x; ms], []) @named emptysys = System(Equation[], t) -@mtkbuild outersys = compose(emptysys, sys) +@mtkcompile outersys = compose(emptysys, sys) prob = ODEProblem( outersys, [outersys.sys.x => 1.0; collect(outersys.sys.ms .=> 1:3)], (0, 1.0)) @test_nowarn solve(prob, Tsit5()) @@ -468,7 +468,7 @@ end eqs = [D(x) ~ foo(x, ms); D(ms) ~ bar(ms, p)] @named sys = System(eqs, t) @named emptysys = System(Equation[], t) -@mtkbuild outersys = compose(emptysys, sys) +@mtkcompile outersys = compose(emptysys, sys) prob = ODEProblem( outersys, [sys.x => 1.0, sys.ms => 1:3], (0.0, 1.0), [sys.p => ones(3, 3)]) @test_nowarn solve(prob, Tsit5()) @@ -574,7 +574,7 @@ let D(x[2]) ~ -x[1] - 0.5 * x[2] + k y ~ 0.9 * x[1] + x[2]] @named sys = System(eqs, t, vcat(x, [y]), [k], defaults = Dict(x .=> 0)) - sys = structural_simplify(sys) + sys = mtkcompile(sys) u0 = [0.5, 0] du0 = 0 .* copy(u0) @@ -656,7 +656,7 @@ let 0 ~ q / C - R * F] @named sys = System(eqs, t) - @test length(equations(structural_simplify(sys))) == 2 + @test length(equations(mtkcompile(sys))) == 2 end let @@ -669,7 +669,7 @@ let spm ~ 0 sph ~ a] @named sys = System(eqs, t, vars, pars) - @test_throws ModelingToolkit.ExtraEquationsSystemException structural_simplify(sys) + @test_throws ModelingToolkit.ExtraEquationsSystemException mtkcompile(sys) end # 1561 @@ -693,9 +693,9 @@ let ps = [] @named sys = System(eqs, t, u, ps) - @test_nowarn simpsys = structural_simplify(sys) + @test_nowarn simpsys = mtkcompile(sys) - sys = structural_simplify(sys) + sys = mtkcompile(sys) u0 = ModelingToolkit.missing_variable_defaults(sys) u0_expected = Pair[s => 0.0 for s in unknowns(sys)] @@ -781,7 +781,7 @@ let @named connected = System(connections, t) @named sys_con = compose(connected, sys, ctrl) - sys_simp = structural_simplify(sys_con) + sys_simp = mtkcompile(sys_con) true_eqs = [D(sys.x) ~ sys.v D(sys.v) ~ ctrl.kv * sys.v + ctrl.kx * sys.x] @test issetequal(full_equations(sys_simp), true_eqs) @@ -792,7 +792,7 @@ let @variables y(t) = 1 @parameters pp = -1 @named sys4 = System([D(x) ~ -y; D(y) ~ 1 + pp * y + x], t) - sys4s = structural_simplify(sys4) + sys4s = mtkcompile(sys4) prob = ODEProblem(sys4s, [x => 1.0, D(x) => 1.0], (0, 1.0)) @test string.(unknowns(prob.f.sys)) == ["x(t)", "y(t)"] @test string.(parameters(prob.f.sys)) == ["pp"] @@ -813,7 +813,7 @@ let @parameters pp = -1 der = Differential(t) @named sys4 = System([der(x) ~ -y; der(y) ~ 1 + pp * y + x], t) - sys4s = structural_simplify(sys4) + sys4s = mtkcompile(sys4) prob = ODEProblem(sys4s, [x => 1.0, D(x) => 1.0], (0, 1.0)) @test !isnothing(prob.f.sys) end @@ -849,7 +849,7 @@ let # Issue https://github.com/SciML/ModelingToolkit.jl/issues/2322 sys = System(eqs, t; name = :kjshdf) - sys_simp = structural_simplify(sys) + sys_simp = mtkcompile(sys) @test a ∈ keys(ModelingToolkit.defaults(sys_simp)) @@ -862,7 +862,7 @@ end # Issue#2599 @variables x(t) y(t) eqs = [D(x) ~ x * t, y ~ 2x] -@mtkbuild sys = System(eqs, t; continuous_events = [[y ~ 3] => [x ~ 2]]) +@mtkcompile sys = System(eqs, t; continuous_events = [[y ~ 3] => [x ~ 2]]) prob = ODEProblem(sys, [x => 1.0], (0.0, 10.0)) @test_nowarn solve(prob, Tsit5()) @@ -873,14 +873,14 @@ prob = ODEProblem(sys, [x => 1.0], (0.0, 10.0)) eqs = [ D(x) ~ p * x ] - @mtkbuild sys = System( + @mtkcompile sys = System( eqs, t; continuous_events = [[norm(x) ~ 3.0] => [x ~ ones(3)]]) # array affect equations used to not work prob1 = @test_nowarn ODEProblem(sys, [x => ones(3)], (0.0, 10.0), [p => ones(3, 3)]) sol1 = @test_nowarn solve(prob1, Tsit5()) # array condition equations also used to not work - @mtkbuild sys = System( + @mtkcompile sys = System( eqs, t; continuous_events = [[x ~ sqrt(3) * ones(3)] => [x ~ ones(3)]]) # array affect equations used to not work prob2 = @test_nowarn ODEProblem(sys, [x => ones(3)], (0.0, 10.0), [p => ones(3, 3)]) @@ -893,7 +893,7 @@ end @test_skip begin @variables x(t)[1:3] y(t) @parameters p[1:3, 1:3] - @test_nowarn @mtkbuild sys = System([D(x) ~ p * x, D(y) ~ x' * p * x], t) + @test_nowarn @mtkcompile sys = System([D(x) ~ p * x, D(y) ~ x' * p * x], t) @test_nowarn ODEProblem(sys, [x => ones(3), y => 2], (0.0, 10.0), [p => ones(3, 3)]) end @@ -917,7 +917,7 @@ eqs = [D(D(x)) ~ σ * (y - x), D(y) ~ x * (ρ - z) - y, D(z) ~ x * y - β * z] -@mtkbuild sys = System(eqs, t) +@mtkcompile sys = System(eqs, t) u0 = [D(x) => 2.0, x => 1.0, @@ -952,7 +952,7 @@ function FML2(; name) System(eqs, t; systems, name) end -@mtkbuild model = FML2() +@mtkcompile model = FML2() @test isequal(ModelingToolkit.defaults(model)[model.constant.k], model.k2[1]) @test_nowarn ODEProblem(model, [], (0.0, 10.0)) @@ -993,7 +993,7 @@ orig_vars = unknowns(sys) @named outer = System( [D(y) ~ sys.x + t, 0 ~ t + y - sys.x * y], t, [y], []; systems = [sys]) @test ModelingToolkit.guesses(outer)[sys.x] == 1.0 -outer = structural_simplify(outer) +outer = mtkcompile(outer) @test ModelingToolkit.get_guesses(outer)[sys.x] == 1.0 prob = ODEProblem(outer, [outer.y => 2.0], (0.0, 10.0)) int = init(prob, Rodas4()) @@ -1029,7 +1029,7 @@ end @testset "Non-1-indexed variable array (issue #2670)" begin @variables x(t)[0:1] # 0-indexed variable array @named sys = System([x[0] ~ 0.0, D(x[1]) ~ x[0]], t, [x], []) - @test_nowarn sys = structural_simplify(sys) + @test_nowarn sys = mtkcompile(sys) @test equations(sys) == [D(x[1]) ~ 0.0] end @@ -1045,7 +1045,7 @@ end @testset "ForwardDiff through ODEProblem constructor" begin @parameters P @variables x(t) - sys = structural_simplify(System([D(x) ~ P], t, [x], [P]; name = :sys)) + sys = mtkcompile(System([D(x) ~ P], t, [x], [P]; name = :sys)) function x_at_1(P) prob = ODEProblem(sys, [x => P], (0.0, 1.0), [sys.P => P]) @@ -1058,7 +1058,7 @@ end @testset "Inplace observed functions" begin @parameters P @variables x(t) - sys = structural_simplify(System([D(x) ~ P], t, [x], [P]; name = :sys)) + sys = mtkcompile(System([D(x) ~ P], t, [x], [P]; name = :sys)) obsfn = ModelingToolkit.build_explicit_observed_function( sys, [x + 1, x + P, x + t], return_inplace = true)[2] ps = ModelingToolkit.MTKParameters(sys, [P => 2.0]) @@ -1083,7 +1083,7 @@ end y ~ 0 end end - @test_nowarn @mtkbuild sys = MyModel() + @test_nowarn @mtkcompile sys = MyModel() @variables x y(x) @test_logs (:warn,) @named sys = System([y ~ 0], x) @@ -1095,7 +1095,7 @@ end initialization_eqs = [x ~ T] guesses = [x => 0.0] @named sys2 = System(eqs, T; initialization_eqs, guesses) - prob2 = ODEProblem(structural_simplify(sys2), [], (1.0, 2.0), []) + prob2 = ODEProblem(mtkcompile(sys2), [], (1.0, 2.0), []) sol2 = solve(prob2) @test all(sol2[x] .== 1.0) end @@ -1127,7 +1127,7 @@ end eqs = [D(x) ~ 0, y ~ x, D(z) ~ 0] defaults = [x => 1, z => y] @named sys = System(eqs, t; defaults) - ssys = structural_simplify(sys) + ssys = mtkcompile(sys) prob = ODEProblem(ssys, [], (0.0, 1.0), []) @test prob[x] == prob[y] == prob[z] == 1.0 @@ -1136,7 +1136,7 @@ end eqs = [D(x) ~ 0, y ~ y0 / x, D(z) ~ y] defaults = [y0 => 1, x => 1, z => y] @named sys = System(eqs, t; defaults) - ssys = structural_simplify(sys) + ssys = mtkcompile(sys) prob = ODEProblem(ssys, [], (0.0, 1.0), []) @test prob[x] == prob[y] == prob[z] == 1.0 end @@ -1147,11 +1147,11 @@ end @named sys = System( [D(u) ~ (sum(u) + sum(x) + sum(p) + sum(o)) * x, o ~ prod(u) * x], t, [u..., x..., o...], [p...]) - sys1 = structural_simplify(sys, inputs = [x...], outputs = []) + sys1 = mtkcompile(sys, inputs = [x...], outputs = []) fn1, = ModelingToolkit.generate_rhs(sys1; expression = Val{false}) ps = MTKParameters(sys1, [x => 2ones(2), p => 3ones(2, 2)]) @test_nowarn fn1(ones(4), ps, 4.0) - sys2 = structural_simplify(sys, inputs = [x...], outputs = [], split = false) + sys2 = mtkcompile(sys, inputs = [x...], outputs = [], split = false) fn2, = ModelingToolkit.generate_rhs(sys2; expression = Val{false}) ps = zeros(8) setp(sys2, x)(ps, 2ones(2)) @@ -1236,10 +1236,10 @@ end @named outersys = System( [D(innersys.y) ~ innersys.y + p4], t; parameter_dependencies = [p4 ~ 3p3], defaults = [p3 => 3.0, p4 => 9.0], guesses = [p4 => 10.0], systems = [innersys]) - @test_nowarn structural_simplify(outersys) + @test_nowarn mtkcompile(outersys) @parameters p5 sys2 = substitute(outersys, [p4 => p5]) - @test_nowarn structural_simplify(sys2) + @test_nowarn mtkcompile(sys2) @test length(equations(sys2)) == 2 @test length(parameters(sys2)) == 2 @test length(full_parameters(sys2)) == 4 @@ -1261,7 +1261,7 @@ end o[2] ~ sum(p) * sum(x)] @named sys = System(eqs, t, [u..., x..., o], [p...]) - sys1 = structural_simplify(sys, inputs = [x...], outputs = [o...], split = false) + sys1 = mtkcompile(sys, inputs = [x...], outputs = [o...], split = false) @test_nowarn ModelingToolkit.build_explicit_observed_function(sys1, u; inputs = [x...]) @@ -1273,7 +1273,7 @@ end @testset "Passing `nothing` to `u0`" begin @variables x(t) = 1 - @mtkbuild sys = System(D(x) ~ t, t) + @mtkcompile sys = System(D(x) ~ t, t) prob = @test_nowarn ODEProblem(sys, nothing, (0.0, 1.0)) @test_nowarn solve(prob) end @@ -1299,7 +1299,7 @@ end @test_nowarn ODEProblem(sys, [], (0.0, 1.0)) else @test_throws [ - r"array (equations|unknowns)", "structural_simplify", "scalarize"] ODEProblem( + r"array (equations|unknowns)", "mtkcompile", "scalarize"] ODEProblem( sys, [], (0.0, 1.0)) end end @@ -1312,7 +1312,7 @@ end @test_nowarn ODEProblem(sys, [], (0.0, 1.0)) else @test_throws [ - r"array (equations|unknowns)", "structural_simplify", "scalarize"] ODEProblem( + r"array (equations|unknowns)", "mtkcompile", "scalarize"] ODEProblem( sys, [], (0.0, 1.0)) end end @@ -1335,7 +1335,7 @@ end @testset "Inplace observed" begin @variables x(t) @parameters p[1:2] q - @mtkbuild sys = System(D(x) ~ sum(p) * x + q * t, t) + @mtkcompile sys = System(D(x) ~ sum(p) * x + q * t, t) prob = ODEProblem(sys, [x => 1.0], (0.0, 1.0), [p => ones(2), q => 2]) obsfn = ModelingToolkit.build_explicit_observed_function( sys, [p..., q], return_inplace = true)[2] @@ -1375,7 +1375,7 @@ end @testset "`complete` with `split = false` removes the index cache" begin @variables x(t) @parameters p - @mtkbuild sys = System(D(x) ~ p * t, t) + @mtkcompile sys = System(D(x) ~ p * t, t) @test ModelingToolkit.get_index_cache(sys) !== nothing sys2 = complete(sys; split = false) @test ModelingToolkit.get_index_cache(sys2) === nothing @@ -1385,7 +1385,7 @@ end @testset "Observed variables dependent on discrete parameters" begin @variables x(t) obs(t) @parameters c(t) - @mtkbuild sys = System([D(x) ~ c * cos(x), obs ~ c], + @mtkcompile sys = System([D(x) ~ c * cos(x), obs ~ c], t, [x, obs], [c]; @@ -1399,7 +1399,7 @@ end @testset "DAEProblem with array parameters" begin @variables x(t)=1.0 y(t) [guess = 1.0] @parameters p[1:2] = [1.0, 2.0] - @mtkbuild sys = System([D(x) ~ x, y^2 ~ x + sum(p)], t) + @mtkcompile sys = System([D(x) ~ x, y^2 ~ x + sum(p)], t) prob = DAEProblem(sys, [D(x) => x, D(y) => D(x) / 2y], [], (0.0, 1.0)) sol = solve(prob, DFBDF(), abstol = 1e-8, reltol = 1e-8) @test sol[x]≈sol[y^2 - sum(p)] atol=1e-5 @@ -1408,7 +1408,7 @@ end @testset "Symbolic tstops" begin @variables x(t) = 1.0 @parameters p=0.15 q=0.25 r[1:2]=[0.35, 0.45] - @mtkbuild sys = System( + @mtkcompile sys = System( [D(x) ~ p * x + q * t + sum(r)], t; tstops = [0.5p, [0.1, 0.2], [p + 2q], r]) prob = ODEProblem(sys, [], (0.0, 5.0)) sol = solve(prob) @@ -1420,7 +1420,7 @@ end @test all(x -> any(isapprox(x, atol = 1e-6), sol2.t), expected_tstops) @variables y(t) [guess = 1.0] - @mtkbuild sys = System([D(x) ~ p * x + q * t + sum(r), y^3 ~ 2x + 1], + @mtkcompile sys = System([D(x) ~ p * x + q * t + sum(r), y^3 ~ 2x + 1], t; tstops = [0.5p, [0.1, 0.2], [p + 2q], r]) prob = DAEProblem( sys, [D(y) => 2D(x) / 3y^2, D(x) => p * x + q * t + sum(r)], [], (0.0, 5.0)) @@ -1437,11 +1437,11 @@ end @parameters p d @variables X(t)::Int64 eq = D(X) ~ p - d * X - @test_throws ModelingToolkit.ContinuousOperatorDiscreteArgumentError @mtkbuild osys = System( + @test_throws ModelingToolkit.ContinuousOperatorDiscreteArgumentError @mtkcompile osys = System( [eq], t) @variables Y(t)[1:3]::String eq = D(Y) ~ [p, p, p] - @test_throws ModelingToolkit.ContinuousOperatorDiscreteArgumentError @mtkbuild osys = System( + @test_throws ModelingToolkit.ContinuousOperatorDiscreteArgumentError @mtkcompile osys = System( [eq], t) @variables X(t)::Complex @@ -1483,32 +1483,32 @@ end cons = [x(0.3) ~ c * d, y(0.7) ~ 3] # Test variables + parameters infer correctly. - @mtkbuild sys = System(eqs, t; constraints = cons) + @mtkcompile sys = System(eqs, t; constraints = cons) @test issetequal(parameters(sys), [a, c, d, e]) @test issetequal(unknowns(sys), [x(t), y(t), z(t)]) @parameters t_c cons = [x(t_c) ~ 3] - @mtkbuild sys = System(eqs, t; constraints = cons) + @mtkcompile sys = System(eqs, t; constraints = cons) @test issetequal(parameters(sys), [a, e, t_c]) @parameters g(..) h i cons = [g(h, i) * x(3) ~ c] - @mtkbuild sys = System(eqs, t; constraints = cons) + @mtkcompile sys = System(eqs, t; constraints = cons) @test issetequal(parameters(sys), [g, h, i, a, e, c]) # Test that bad constraints throw errors. cons = [x(3, 4) ~ 3] # unknowns cannot have multiple args. - @test_throws ArgumentError @mtkbuild sys = System(eqs, t; constraints = cons) + @test_throws ArgumentError @mtkcompile sys = System(eqs, t; constraints = cons) cons = [x(y(t)) ~ 2] # unknown arg must be parameter, value, or t - @test_throws ArgumentError @mtkbuild sys = System(eqs, t; constraints = cons) + @test_throws ArgumentError @mtkcompile sys = System(eqs, t; constraints = cons) @variables u(t) v cons = [x(t) * u ~ 3] - @test_throws ArgumentError @mtkbuild sys = System(eqs, t; constraints = cons) + @test_throws ArgumentError @mtkcompile sys = System(eqs, t; constraints = cons) cons = [x(t) * v ~ 3] - @test_throws ArgumentError @mtkbuild sys = System(eqs, t; constraints = cons) # Need time argument. + @test_throws ArgumentError @mtkcompile sys = System(eqs, t; constraints = cons) # Need time argument. # Test array variables @variables x(..)[1:5] @@ -1519,13 +1519,13 @@ end 0 0 2 0 5] eqs = D(x(t)) ~ mat * x(t) cons = [x(3) ~ [2, 3, 3, 5, 4]] - @mtkbuild ode = System(D(x(t)) ~ mat * x(t), t; constraints = cons) + @mtkcompile ode = System(D(x(t)) ~ mat * x(t), t; constraints = cons) @test length(constraints(ode)) == 1 end @testset "`build_explicit_observed_function` with `expression = true` returns `Expr`" begin @variables x(t) - @mtkbuild sys = System(D(x) ~ 2x, t) + @mtkcompile sys = System(D(x) ~ 2x, t) obsfn_expr = ModelingToolkit.build_explicit_observed_function( sys, 2x + 1, expression = true) @test obsfn_expr isa Expr @@ -1543,7 +1543,7 @@ end D(y) ~ x * (ρ - z) - y, D(z) ~ x * y - β * z] - @mtkbuild sys=System(eqs, t) split=false + @mtkcompile sys=System(eqs, t) split=false u0 = SA[D(x) => 2.0f0, x => 1.0f0, @@ -1579,6 +1579,6 @@ end @named subsys = SysB(; var1 = x) return System(D(x) ~ x, t; systems = [subsys], name) end - @mtkbuild sys = SysC() + @mtkcompile sys = SysC() @test length(unknowns(sys)) == 3 end diff --git a/test/optimizationsystem.jl b/test/optimizationsystem.jl index 099d8346ba..b586d47d4e 100644 --- a/test/optimizationsystem.jl +++ b/test/optimizationsystem.jl @@ -84,7 +84,7 @@ end z ~ y - x^2 z^2 + y^2 ≲ 1.0] @named sys = OptimizationSystem(loss, [x, y, z], [a, b], constraints = cons) - sys = structural_simplify(sys) + sys = mtkcompile(sys) prob = OptimizationProblem(sys, [x => 0.0, y => 0.0, z => 0.0], [a => 1.0, b => 1.0], grad = true, hess = true, cons_j = true, cons_h = true) sol = solve(prob, IPNewton()) @@ -323,7 +323,7 @@ end @testset "Passing `nothing` to `u0`" begin @variables x = 1.0 - @mtkbuild sys = OptimizationSystem((x - 3)^2, [x], []) + @mtkcompile sys = OptimizationSystem((x - 3)^2, [x], []) prob = @test_nowarn OptimizationProblem(sys, nothing) @test_nowarn solve(prob, NelderMead()) end @@ -335,14 +335,14 @@ end obj = x^2 + y^2 + z^2 cons = [y ~ 2x z ~ 2y] - @mtkbuild sys = OptimizationSystem(obj, [x, y, z], []; constraints = cons) + @mtkcompile sys = OptimizationSystem(obj, [x, y, z], []; constraints = cons) @test is_variable(sys, z) @test !is_variable(sys, y) @variables x[1:3] [bounds = ([-Inf, -1.0, -2.0], [Inf, 1.0, 2.0])] obj = x[1]^2 + x[2]^2 + x[3]^2 cons = [x[2] ~ 2x[1] + 3, x[3] ~ x[1] + x[2]] - @mtkbuild sys = OptimizationSystem(obj, [x], []; constraints = cons) + @mtkcompile sys = OptimizationSystem(obj, [x], []; constraints = cons) @test length(unknowns(sys)) == 2 @test !is_variable(sys, x[1]) @test is_variable(sys, x[2]) @@ -352,7 +352,7 @@ end @testset "Constraints work with nonnumeric parameters" begin @variables x @parameters p f(::Real) - @mtkbuild sys = OptimizationSystem( + @mtkcompile sys = OptimizationSystem( x^2 + f(x) * p, [x], [f, p]; constraints = [2.0 ≲ f(x) + p]) prob = OptimizationProblem(sys, [x => 1.0], [p => 1.0, f => (x -> 2x)]) @test abs(prob.f.cons(prob.u0, prob.p)[1]) ≈ 1.0 diff --git a/test/parameter_dependencies.jl b/test/parameter_dependencies.jl index 1ea796507d..97937b8811 100644 --- a/test/parameter_dependencies.jl +++ b/test/parameter_dependencies.jl @@ -20,7 +20,7 @@ using NonlinearSolve cb2 = [x ~ 4.0] => (f = affect1!, observed = (; p2), modified = (; p1)) # triggers at t=-2+√7 cb3 = SymbolicDiscreteCallback([1.0] => [p1 ~ 5.0], discrete_parameters = [p1]) - @mtkbuild sys = System( + @mtkcompile sys = System( [D(x) ~ p1 * t + p2], t; parameter_dependencies = [p2 => 2p1], @@ -73,7 +73,7 @@ end @parameters p1=1.0 p2=1.0 @variables x(t) = 0 - @mtkbuild sys1 = System( + @mtkcompile sys1 = System( [D(x) ~ p1 * t + p2], t ) @@ -171,7 +171,7 @@ end # (https://github.com/SciML/ModelingToolkit.jl/pull/2978) @inferred ModelingToolkit.parameter_dependencies(sys1) - sys = structural_simplify(sys1) + sys = mtkcompile(sys1) prob = ODEProblem(sys, [], (0.0, 1.0)) sol = solve(prob) @@ -193,7 +193,7 @@ end eqs = [D(y) ~ i(t) + p] @named model = System(eqs, t, [y], [p, i]; parameter_dependencies = [i ~ CallableFoo(p)]) - sys = structural_simplify(model) + sys = mtkcompile(model) prob = ODEProblem(sys, [], (0.0, 1.0)) sol = solve(prob, Tsit5()) @@ -215,7 +215,7 @@ end D(x) ~ -x + u y ~ x z(k) ~ z(k - 2) + yd(k - 2)] - @test_throws ModelingToolkit.HybridSystemNotSupportedException @mtkbuild sys = System( + @test_throws ModelingToolkit.HybridSystemNotSupportedException @mtkcompile sys = System( eqs, t; parameter_dependencies = [kq => 2kp]) @test_skip begin @@ -225,7 +225,7 @@ end yd(k - 2) => 2.0]) @test_nowarn solve(prob, Tsit5()) - @mtkbuild sys = System(eqs, t; parameter_dependencies = [kq => 2kp], + @mtkcompile sys = System(eqs, t; parameter_dependencies = [kq => 2kp], discrete_events = [SymbolicDiscreteCallback( [0.5] => [kp ~ 2.0], discrete_parameters = [kp])]) prob = ODEProblem(sys, [x => 0.0, y => 0.0], (0.0, Tf), @@ -328,7 +328,7 @@ end @parameters p1=1.0 p2=1.0 @variables x(t) eqs = [0 ~ p1 * x * exp(x) + p2] - @mtkbuild sys = System(eqs; parameter_dependencies = [p2 => 2p1]) + @mtkcompile sys = System(eqs; parameter_dependencies = [p2 => 2p1]) @test isequal(only(parameters(sys)), p1) @test Set(full_parameters(sys)) == Set([p1, p2, Initial(p2), Initial(x)]) prob = NonlinearProblem(sys, [x => 1.0]) @@ -350,7 +350,7 @@ end cb2 = [x ~ 4.0] => (affect1!, [], [p1, p2], [p1]) # triggers at t=-2+√7 cb3 = [1.0] => [p1 ~ 5.0] - @mtkbuild sys = System( + @mtkcompile sys = System( [D(x) ~ p1 * t + p2], t; parameter_dependencies = [p2 => 2p1] diff --git a/test/problem_validation.jl b/test/problem_validation.jl index 3db151bda5..e6e7022684 100644 --- a/test/problem_validation.jl +++ b/test/problem_validation.jl @@ -6,7 +6,7 @@ using ModelingToolkit: t_nounits as t, D_nounits as D @variables X(t) @parameters p d eqs = [D(X) ~ p - d * X] - @mtkbuild osys = System(eqs, t) + @mtkcompile osys = System(eqs, t) p = "I accidentally renamed p" u0 = [X => 1.0] @@ -23,7 +23,7 @@ using ModelingToolkit: t_nounits as t, D_nounits as D @variables x(t) y(t) z(t) @parameters a b c d eqs = [D(x) ~ x * a, D(y) ~ y * c, D(z) ~ b + d] - @mtkbuild sys = System(eqs, t) + @mtkcompile sys = System(eqs, t) pmap = [a => 1, b => 2, c => 3, d => 4, "b" => 2] u0map = [x => 1, y => 2, z => 3] @test_throws InvalidKeyError ODEProblem(sys, u0map, (0.0, 1.0), pmap) diff --git a/test/reduction.jl b/test/reduction.jl index e75b2afdee..af9e510646 100644 --- a/test/reduction.jl +++ b/test/reduction.jl @@ -30,7 +30,7 @@ eqs = [D(x) ~ σ * (y - x) lorenz1 = System(eqs, t, name = :lorenz1) -lorenz1_aliased = structural_simplify(lorenz1) +lorenz1_aliased = mtkcompile(lorenz1) io = IOBuffer(); show(io, MIME("text/plain"), lorenz1_aliased); str = String(take!(io)); @@ -74,8 +74,8 @@ __x = x # Reduced Flattened System -reduced_system = structural_simplify(connected) -reduced_system2 = structural_simplify(tearing_substitution(structural_simplify(tearing_substitution(structural_simplify(connected))))) +reduced_system = mtkcompile(connected) +reduced_system2 = mtkcompile(tearing_substitution(mtkcompile(tearing_substitution(mtkcompile(connected))))) @test isempty(setdiff(unknowns(reduced_system), unknowns(reduced_system2))) @test isequal(equations(tearing_substitution(reduced_system)), equations(reduced_system2)) @@ -133,7 +133,7 @@ let pc.y_c ~ ol.y] @named connected = System(connections, t, systems = [ol, pc]) @test equations(connected) isa Vector{Equation} - reduced_sys = structural_simplify(connected) + reduced_sys = mtkcompile(connected) ref_eqs = [D(ol.x) ~ ol.a * ol.x + ol.b * ol.u 0 ~ pc.k_P * ol.y - ol.u] #@test ref_eqs == equations(reduced_sys) @@ -144,7 +144,7 @@ let @variables x(t) @named sys = System([0 ~ D(x) + x], t, [x], []) #@test_throws ModelingToolkit.InvalidSystemException ODEProblem(sys, [1.0], (0, 10.0)) - sys = structural_simplify(sys) + sys = mtkcompile(sys) #@test_nowarn ODEProblem(sys, [1.0], (0, 10.0)) end @@ -155,7 +155,7 @@ eqs = [u1 ~ u2 u3 ~ u1 + u2 + p u3 ~ hypot(u1, u2) * p] @named sys = System(eqs, [u1, u2, u3], [p]) -reducedsys = structural_simplify(sys) +reducedsys = mtkcompile(sys) @test length(observed(reducedsys)) == 2 u0 = [u2 => 1] @@ -175,7 +175,7 @@ N = 5 A = reshape(1:(N^2), N, N) eqs = xs ~ A * xs @named sys′ = System(eqs, [xs], []) -sys = structural_simplify(sys′) +sys = mtkcompile(sys′) @test length(equations(sys)) == 3 && length(observed(sys)) == 3 # issue 958 @@ -189,7 +189,7 @@ eqs = [D(E) ~ k₋₁ * C - k₁ * E * S E₀ ~ E + C] @named sys = System(eqs, t, [E, C, S, P], [k₁, k₂, k₋₁, E₀]) -@test_throws ModelingToolkit.ExtraEquationsSystemException structural_simplify(sys) +@test_throws ModelingToolkit.ExtraEquationsSystemException mtkcompile(sys) # Example 5 from Pantelides' original paper params = collect(@parameters y1(t) y2(t)) @@ -198,7 +198,7 @@ eqs = [0 ~ x + sin(u1 + u2) D(x) ~ x + y1 cos(x) ~ sin(y2)] @named sys = System(eqs, t, sts, params) -@test_throws ModelingToolkit.InvalidSystemException structural_simplify(sys) +@test_throws ModelingToolkit.InvalidSystemException mtkcompile(sys) # issue #963 @variables v47(t) v57(t) v66(t) v25(t) i74(t) i75(t) i64(t) i71(t) v1(t) v2(t) @@ -215,7 +215,7 @@ eq = [v47 ~ v1 0 ~ i64 + i71] @named sys0 = System(eq, t) -sys = structural_simplify(sys0) +sys = mtkcompile(sys0) @test length(equations(sys)) == 1 eq = equations(tearing_substitution(sys))[1] vv = only(unknowns(sys)) @@ -233,7 +233,7 @@ eqs = [D(x) ~ σ * (y - x) u ~ z + a] lorenz1 = System(eqs, t, name = :lorenz1) -lorenz1_reduced = structural_simplify(lorenz1, inputs = [z], outputs = []) +lorenz1_reduced = mtkcompile(lorenz1, inputs = [z], outputs = []) @test z in Set(parameters(lorenz1_reduced)) # #2064 @@ -242,7 +242,7 @@ eqs = [D(x) ~ x D(y) ~ y D(z) ~ t] @named model = System(eqs, t) -sys = structural_simplify(model) +sys = mtkcompile(model) Js = ModelingToolkit.jacobian_sparsity(sys) @test size(Js) == (3, 3) @test Js == Diagonal([1, 1, 0]) @@ -275,7 +275,7 @@ new_sys = alias_elimination(sys) eqs = [x ~ 0 D(x) ~ x + y] @named sys = System(eqs, t, [x, y], []) -ss = structural_simplify(sys) +ss = mtkcompile(sys) @test isempty(equations(ss)) @test sort(string.(observed(ss))) == ["x(t) ~ 0.0" "xˍt(t) ~ 0.0" @@ -285,5 +285,5 @@ eqs = [D(D(x)) ~ -x] @named sys = System(eqs, t, [x], []) ss = alias_elimination(sys) @test length(equations(ss)) == length(unknowns(ss)) == 1 -ss = structural_simplify(sys) +ss = mtkcompile(sys) @test length(equations(ss)) == length(unknowns(ss)) == 2 diff --git a/test/scc_nonlinear_problem.jl b/test/scc_nonlinear_problem.jl index 3759508c0d..278b1a90d1 100644 --- a/test/scc_nonlinear_problem.jl +++ b/test/scc_nonlinear_problem.jl @@ -23,9 +23,9 @@ using ModelingToolkit: t_nounits as t, D_nounits as D eqs = 0 .~ eqs @named model = System(eqs) @test_throws ["simplified", "required"] SCCNonlinearProblem(model, []) - _model = structural_simplify(model; split = false) + _model = mtkcompile(model; split = false) @test_throws ["not compatible"] SCCNonlinearProblem(_model, []) - model = structural_simplify(model) + model = mtkcompile(model) prob = NonlinearProblem(model, [u => zeros(8)]) sccprob = SCCNonlinearProblem(model, [u => zeros(8)]) sol1 = solve(prob, NewtonRaphson()) @@ -85,7 +85,7 @@ end @variables u[1:5] [irreducible = true] @parameters p1[1:6] p2 eqs = 0 .~ collect(nlf(u, (u0, (p1, p2)))) - @mtkbuild sys = System(eqs, [u], [p1, p2]) + @mtkcompile sys = System(eqs, [u], [p1, p2]) sccprob = SCCNonlinearProblem(sys, [u => u0], [p1 => p[1], p2 => p[2][]]) sccsol = solve(sccprob, SimpleNewtonRaphson(); abstol = 1e-9) @test SciMLBase.successful_retcode(sccsol) @@ -141,7 +141,7 @@ end eqs = 0 .~ eqs subrules = Dict(Symbolics.unwrap(D(y[i])) => ((y[i] - u0[i]) / dt) for i in 1:8) eqs = substitute.(eqs, (subrules,)) - @mtkbuild sys = System(eqs) + @mtkcompile sys = System(eqs) prob = NonlinearProblem(sys, [y => u0], [t => t0]) sol = solve(prob, NewtonRaphson(); abstol = 1e-12) @@ -159,10 +159,10 @@ end x + y end @register_symbolic func(x, y) - @mtkbuild sys = System([0 ~ x[1]^3 + x[2]^3 - 5 - 0 ~ sin(x[1] - x[2]) - 0.5 - 0 ~ func(x[1], x[2]) * exp(x[3]) - x[4]^3 - 5 - 0 ~ func(x[1], x[2]) * exp(x[4]) - x[3]^3 - 4]) + @mtkcompile sys = System([0 ~ x[1]^3 + x[2]^3 - 5 + 0 ~ sin(x[1] - x[2]) - 0.5 + 0 ~ func(x[1], x[2]) * exp(x[3]) - x[4]^3 - 5 + 0 ~ func(x[1], x[2]) * exp(x[4]) - x[3]^3 - 4]) sccprob = SCCNonlinearProblem(sys, []) sccsol = solve(sccprob, NewtonRaphson()) @test SciMLBase.successful_retcode(sccsol) @@ -255,7 +255,7 @@ import ModelingToolkitStandardLibrary.Hydraulic.IsothermalCompressible as IC initialization_eqs = [mass.s ~ 0.0 mass.v ~ 0.0] - @mtkbuild sys = System(eqs, t, [], []; systems, initialization_eqs) + @mtkcompile sys = System(eqs, t, [], []; systems, initialization_eqs) prob = ODEProblem(sys, [], (0, 5)) sol = solve(prob) @test SciMLBase.successful_retcode(sol) @@ -264,7 +264,7 @@ end @testset "Array variables split across SCCs" begin @variables x[1:3] @parameters (f::Function)(..) - @mtkbuild sys = System([ + @mtkcompile sys = System([ 0 ~ x[1]^2 - 9, x[2] ~ 2x[1], 0 ~ x[3]^2 - x[1]^2 + f(x)]) prob = SCCNonlinearProblem(sys, [x => ones(3)], [f => sum]) sol = solve(prob, NewtonRaphson()) @@ -274,7 +274,7 @@ end @testset "SCCNonlinearProblem retains parameter order" begin @variables x y z @parameters σ β ρ - @mtkbuild fullsys = System( + @mtkcompile fullsys = System( [0 ~ x^3 * β + y^3 * ρ - σ, 0 ~ x^2 + 2x * y + y^2, 0 ~ z^2 - 4z + 4], [x, y, z], [σ, β, ρ]) @@ -294,7 +294,7 @@ end @variables x y @parameters p[1:2] (f::Function)(..) - @mtkbuild sys = System([x^2 - p[1]^2 ~ 0, y^2 ~ f(p)]) + @mtkcompile sys = System([x^2 - p[1]^2 ~ 0, y^2 ~ f(p)]) prob = SCCNonlinearProblem(sys, [x => 1.0, y => 1.0], [p => ones(2), f => sum]) @test_nowarn solve(prob, NewtonRaphson()) end diff --git a/test/sciml_problem_inputs.jl b/test/sciml_problem_inputs.jl index a1b9d1e416..249991b631 100644 --- a/test/sciml_problem_inputs.jl +++ b/test/sciml_problem_inputs.jl @@ -37,7 +37,7 @@ begin MassActionJump(k2, [Z => 1], [Y => 1, Z => -1]) ] - # Create systems (without structural_simplify, since that might modify systems to affect intended tests). + # Create systems (without mtkcompile, since that might modify systems to affect intended tests). osys = complete(System(diff_eqs, t; name = :osys)) ssys = complete(SDESystem( diff_eqs, noise_eqs, t, [X, Y, Z], [kp, kd, k1, k2]; name = :ssys)) diff --git a/test/sdesystem.jl b/test/sdesystem.jl index a3e0ff00d0..bcec551faf 100644 --- a/test/sdesystem.jl +++ b/test/sdesystem.jl @@ -597,7 +597,7 @@ eqs = [D(x) ~ σ * (y - x) + x * β, D(y) ~ x * (ρ - z) - y + y * β + x * η, D(z) ~ x * y - β * z + (x * z) * β] @named sys1 = System(eqs, tt) -sys1 = structural_simplify(sys1) +sys1 = mtkcompile(sys1) drift_eqs = [D(x) ~ σ * (y - x), D(y) ~ x * (ρ - z) - y, @@ -636,7 +636,7 @@ ssys = SDESystem(eqs, noise_eqs, t, [X], [p, d]; name = :ssys) @variables x(tt) @brownian a eqs = [D(x) ~ p - d * x + a * sqrt(p)] -@mtkbuild sys = System(eqs, tt) +@mtkcompile sys = System(eqs, tt) u0 = @SVector[x => 10.0] tspan = (0.0, 10.0) ps = @SVector[p => 5.0, d => 0.5] @@ -650,7 +650,7 @@ sprob = SDEProblem(sys, u0, tspan, ps) @brownian b eqs = [D(x) ~ p - d * x + a * sqrt(p) D(y) ~ p - d * y + b * sqrt(d)] -@mtkbuild sys = System(eqs, tt) +@mtkcompile sys = System(eqs, tt) u0 = @SVector[x => 10.0, y => 20.0] tspan = (0.0, 10.0) ps = @SVector[p => 5.0, d => 0.5] @@ -666,7 +666,7 @@ let D(y) ~ x * (ρ - z) - y + 0.1a * y, D(z) ~ x * y - β * z + 0.1a * z] - @mtkbuild de = System(eqs, t) + @mtkcompile de = System(eqs, t) u0map = [ x => 1.0, @@ -690,7 +690,7 @@ let # test to make sure that scalar noise always receive the same kicks eqs = [D(x) ~ a, D(y) ~ a] - @mtkbuild de = System(eqs, t) + @mtkcompile de = System(eqs, t) prob = SDEProblem(de, [x => 0, y => 0], (0.0, 10.0), []) sol = solve(prob, SOSRI()) @test sol.u[end][1] == sol.u[end][2] @@ -704,7 +704,7 @@ let # test that diagonal noise is correctly handled D(y) ~ x * (ρ - z) - y + 0.1b * y, D(z) ~ x * y - β * z + 0.1c * z] - @mtkbuild de = System(eqs, t) + @mtkcompile de = System(eqs, t) u0map = [ x => 1.0, @@ -730,7 +730,7 @@ end eqs = [D(x) ~ σ * (y - x) + 0.1a * x + d, D(y) ~ x * (ρ - z) - y + 0.1b * y + e, D(z) ~ x * y - β * z + 0.1c * z + f] - @mtkbuild de = System(eqs, tt) + @mtkcompile de = System(eqs, tt) u0map = [ x => 1.0, @@ -759,7 +759,7 @@ end eqs = [D(x) ~ σ * (y - x) + 0.1a * x, # One brownian D(y) ~ x * (ρ - z) - y + 0.1b * y, # Another brownian D(z) ~ x * y - β * z] # no brownians -- still diagonal - @mtkbuild de = System(eqs, tt) + @mtkcompile de = System(eqs, tt) u0map = [ x => 1.0, @@ -780,7 +780,7 @@ end @testset "Passing `nothing` to `u0`" begin @variables x(t) = 1 @brownian b - @mtkbuild sys = System([D(x) ~ x + b], t) + @mtkcompile sys = System([D(x) ~ x + b], t) prob = @test_nowarn SDEProblem(sys, nothing, (0.0, 1.0)) @test_nowarn solve(prob, ImplicitEM()) end @@ -798,16 +798,16 @@ end input ~ 0.0] sys = System(eqs, t, sts, ps, browns; name = :name) - sys = structural_simplify(sys) + sys = mtkcompile(sys) @test ModelingToolkit.get_noise_eqs(sys) ≈ [1.0] prob = SDEProblem(sys, [], (0.0, 1.0), []) @test_nowarn solve(prob, RKMil()) end -@testset "Observed variables retained after `structural_simplify`" begin +@testset "Observed variables retained after `mtkcompile`" begin @variables x(t) y(t) z(t) @brownian a - @mtkbuild sys = System([D(x) ~ x + a, D(y) ~ y + a, z ~ x + y], t) + @mtkcompile sys = System([D(x) ~ x + a, D(y) ~ y + a, z ~ x + y], t) @test length(observed(sys)) == 1 prob = SDEProblem(sys, [x => 1.0, y => 1.0], (0.0, 1.0)) @test prob[z] ≈ 2.0 @@ -860,9 +860,9 @@ end end end -@testset "`structural_simplify(::SDESystem)`" begin +@testset "`mtkcompile(::SDESystem)`" begin @variables x(t) y(t) - @mtkbuild sys = SDESystem( + @mtkcompile sys = SDESystem( [D(x) ~ x, y ~ 2x], [x, 0], t, [x, y], []) @test length(equations(sys)) == 1 @test length(ModelingToolkit.get_noise_eqs(sys)) == 1 @@ -875,7 +875,7 @@ end @variables X(t)::Int64 @brownian z eq2 = D(X) ~ p - d * X + z - @test_throws ModelingToolkit.ContinuousOperatorDiscreteArgumentError @mtkbuild ssys = System( + @test_throws ModelingToolkit.ContinuousOperatorDiscreteArgumentError @mtkcompile ssys = System( [eq2], t) noiseeq = [1] @test_throws ModelingToolkit.ContinuousOperatorDiscreteArgumentError @named ssys = SDESystem( @@ -929,27 +929,27 @@ end @parameters p d @brownian a seq = D(X) ~ p - d * X + a - @mtkbuild ssys1 = System([seq], t; name = :ssys) - @mtkbuild ssys2 = System([seq], t; name = :ssys) + @mtkcompile ssys1 = System([seq], t; name = :ssys) + @mtkcompile ssys2 = System([seq], t; name = :ssys) @test ssys1 == ssys2 # true continuous_events = [[X ~ 1.0] => [X ~ X + 5.0]] discrete_events = [5.0 => [d ~ d / 2.0]] - @mtkbuild ssys1 = System([seq], t; name = :ssys, continuous_events) - @mtkbuild ssys2 = System([seq], t; name = :ssys) + @mtkcompile ssys1 = System([seq], t; name = :ssys, continuous_events) + @mtkcompile ssys2 = System([seq], t; name = :ssys) @test ssys1 !== ssys2 - @mtkbuild ssys1 = System([seq], t; name = :ssys, discrete_events) - @mtkbuild ssys2 = System([seq], t; name = :ssys) + @mtkcompile ssys1 = System([seq], t; name = :ssys, discrete_events) + @mtkcompile ssys2 = System([seq], t; name = :ssys) @test ssys1 !== ssys2 - @mtkbuild ssys1 = System([seq], t; name = :ssys, continuous_events) - @mtkbuild ssys2 = System([seq], t; name = :ssys, discrete_events) + @mtkcompile ssys1 = System([seq], t; name = :ssys, continuous_events) + @mtkcompile ssys2 = System([seq], t; name = :ssys, discrete_events) @test ssys1 !== ssys2 end -@testset "Error when constructing SDEProblem without `structural_simplify`" begin +@testset "Error when constructing SDEProblem without `mtkcompile`" begin @parameters σ ρ β @variables x(tt) y(tt) z(tt) @brownian a @@ -963,8 +963,8 @@ end u0map = [x => 1.0, y => 0.0, z => 0.0] parammap = [σ => 10.0, β => 26.0, ρ => 2.33] - @test_throws ["Brownian", "structural_simplify"] SDEProblem( + @test_throws ["Brownian", "mtkcompile"] SDEProblem( de, u0map, (0.0, 100.0), parammap) - de = structural_simplify(de) + de = mtkcompile(de) @test SDEProblem(de, u0map, (0.0, 100.0), parammap) isa SDEProblem end diff --git a/test/serialization.jl b/test/serialization.jl index 1a0105d155..83e68f5770 100644 --- a/test/serialization.jl +++ b/test/serialization.jl @@ -32,7 +32,7 @@ sys = include_string(@__MODULE__, str) @test sys == expand_connections(rc_model) # this actually kind of works, but the variables would have different identities. # check answer -ss = structural_simplify(rc_model) +ss = mtkcompile(rc_model) all_obs = observables(ss) prob = ODEProblem(ss, [capacitor.v => 0.0], (0, 0.1)) sol = solve(prob, ImplicitEuler()) diff --git a/test/split_parameters.jl b/test/split_parameters.jl index 39b242db08..ac819351b5 100644 --- a/test/split_parameters.jl +++ b/test/split_parameters.jl @@ -82,7 +82,7 @@ eqs = [y ~ src.output.u @named sys = System(eqs, t, vars, []; systems = [int, src]) s = complete(sys) -sys = structural_simplify(sys) +sys = mtkcompile(sys) prob = ODEProblem( sys, [], (0.0, t_end), [s.src.interpolator => Interpolator(x, dt)]; tofloat = false) @@ -108,7 +108,7 @@ eqs = [D(y) ~ dy * a ddy ~ sin(t) * c] @named model = System(eqs, t, vars, pars) -sys = structural_simplify(model; split = false) +sys = mtkcompile(model; split = false) tspan = (0.0, t_end) prob = ODEProblem(sys, [], tspan, []; build_initializeprob = false) @@ -236,7 +236,7 @@ end (::Foo)(x) = 3x @variables x(t) @parameters fn(::Real) = _f1 - @mtkbuild sys = System(D(x) ~ fn(t), t) + @mtkcompile sys = System(D(x) ~ fn(t), t) @test is_parameter(sys, fn) @test ModelingToolkit.defaults(sys)[fn] == _f1 @@ -260,7 +260,7 @@ end interp = LinearInterpolation(ts .^ 2, ts; extrapolate = true) @variables x(t) @parameters (fn::typeof(interp))(..) - @mtkbuild sys = System(D(x) ~ fn(x), t) + @mtkcompile sys = System(D(x) ~ fn(x), t) @test is_parameter(sys, fn) getter = getp(sys, fn) prob = ODEProblem(sys, [x => 1.0], (0.0, 1.0), [fn => interp]) diff --git a/test/state_selection.jl b/test/state_selection.jl index ba74cc04a2..f3971bdbe3 100644 --- a/test/state_selection.jl +++ b/test/state_selection.jl @@ -117,7 +117,7 @@ let @named system = HydraulicSystem(L = 10) @unpack supply_pipe, return_pipe = system - sys = structural_simplify(system) + sys = mtkcompile(system) u0 = [ sys.supply_pipe.v => 0.1, sys.return_pipe.v => 0.1, D(supply_pipe.v) => 0.0, D(return_pipe.fluid_port_a.m) => 0.0, @@ -168,7 +168,7 @@ let @named trans = System(eqs, t) - sys = structural_simplify(trans) + sys = mtkcompile(trans) n = 3 u = 0 * ones(n) @@ -273,7 +273,7 @@ let # solution ------------------------------------------------------------------- @named catapult = System(eqs, t, vars, params, defaults = defs) - sys = structural_simplify(catapult) + sys = mtkcompile(catapult) prob = ODEProblem(sys, [], (0.0, 0.1), [l_2f => 0.55, damp => 1e7]; jac = true) @test solve(prob, Rodas4()).retcode == ReturnCode.Success end diff --git a/test/static_arrays.jl b/test/static_arrays.jl index 61b9bb6b25..edb6eeff7d 100644 --- a/test/static_arrays.jl +++ b/test/static_arrays.jl @@ -9,7 +9,7 @@ eqs = [D(D(x)) ~ σ * (y - x), D(z) ~ x * y - β * z] @named sys = System(eqs, t) -sys = structural_simplify(sys) +sys = mtkcompile(sys) u0 = @SVector [D(x) => 2.0, x => 1.0, diff --git a/test/stream_connectors.jl b/test/stream_connectors.jl index 23c648d9e1..6e49cd1fb3 100644 --- a/test/stream_connectors.jl +++ b/test/stream_connectors.jl @@ -134,7 +134,7 @@ eqns = [domain_connect(fluid, n1m1.port_a) @named n1m1Test = System(eqns, t, [], []; systems = [fluid, n1m1, pipe, sink]) -@test_nowarn structural_simplify(n1m1Test) +@test_nowarn mtkcompile(n1m1Test) @unpack source, port_a = n1m1 ssort(eqs) = sort(eqs, by = string) @test ssort(equations(expand_connections(n1m1))) == ssort([0 ~ port_a.m_flow @@ -205,7 +205,7 @@ eqns = [connect(n1m2.port_a, sink1.port) @named sys = System(eqns, t) @named n1m2Test = compose(sys, n1m2, sink1, sink2) -@test_nowarn structural_simplify(n1m2Test) +@test_nowarn mtkcompile(n1m2Test) @named n1m2 = N1M2() @named pipe1 = AdiabaticStraightPipe() @@ -220,7 +220,7 @@ eqns = [connect(n1m2.port_a, pipe1.port_a) @named sys = System(eqns, t) @named n1m2AltTest = compose(sys, n1m2, pipe1, pipe2, sink1, sink2) -@test_nowarn structural_simplify(n1m2AltTest) +@test_nowarn mtkcompile(n1m2AltTest) # N2M2 model and test code. function N2M2(; name, @@ -249,7 +249,7 @@ eqns = [connect(source.port, n2m2.port_a) @named sys = System(eqns, t) @named n2m2Test = compose(sys, n2m2, source, sink) -@test_nowarn structural_simplify(n2m2Test) +@test_nowarn mtkcompile(n2m2Test) # stream var @named sp1 = TwoPhaseFluidPort() @@ -472,7 +472,7 @@ csys = complete(two_fluid_system) @test Symbol(sys_defs[csys.volume_a.H.rho]) == Symbol(csys.fluid_a.rho) @test Symbol(sys_defs[csys.volume_b.H.rho]) == Symbol(csys.fluid_b.rho) -@test_nowarn structural_simplify(two_fluid_system) +@test_nowarn mtkcompile(two_fluid_system) function OneFluidSystem(; name) pars = [] @@ -510,4 +510,4 @@ csys = complete(one_fluid_system) @test Symbol(sys_defs[csys.volume_a.H.rho]) == Symbol(csys.fluid.rho) @test Symbol(sys_defs[csys.volume_b.H.rho]) == Symbol(csys.fluid.rho) -@test_nowarn structural_simplify(one_fluid_system) +@test_nowarn mtkcompile(one_fluid_system) diff --git a/test/structural_transformation/index_reduction.jl b/test/structural_transformation/index_reduction.jl index 0703221418..6ab8dde338 100644 --- a/test/structural_transformation/index_reduction.jl +++ b/test/structural_transformation/index_reduction.jl @@ -49,7 +49,7 @@ let pss_pendulum = partial_state_selection(pendulum) @test_broken length(equations(pss_pendulum)) == 3 end -let sys = structural_simplify(pendulum2) +let sys = mtkcompile(pendulum2) @test length(equations(sys)) == 5 @test length(unknowns(sys)) == 5 @@ -76,7 +76,7 @@ let D(D(y)) ~ λ * y - g x^2 + y^2 ~ 1] @named pend = System(eqs, t) - sys = complete(structural_simplify(pend; dummy_derivative = false)) + sys = complete(mtkcompile(pend; dummy_derivative = false)) prob = ODEProblem( sys, [x => 1, y => 0, D(x) => 0.0], (0.0, 10.0), [g => 1], guesses = [λ => 0.0]) sol = solve(prob, Rodas5P()) diff --git a/test/structural_transformation/tearing.jl b/test/structural_transformation/tearing.jl index 5bac18a253..e9b9909871 100644 --- a/test/structural_transformation/tearing.jl +++ b/test/structural_transformation/tearing.jl @@ -146,7 +146,7 @@ eqs = [D(x) ~ z * h 0 ~ x - y 0 ~ sin(z) + y - p * t] @named daesys = System(eqs, t) -newdaesys = structural_simplify(daesys) +newdaesys = mtkcompile(daesys) @test equations(newdaesys) == [D(x) ~ h * z; 0 ~ y + sin(z) - p * t] @test equations(tearing_substitution(newdaesys)) == [D(x) ~ h * z; 0 ~ x + sin(z) - p * t] @test isequal(unknowns(newdaesys), [x, z]) @@ -161,7 +161,7 @@ prob.f(du, u, pr, tt) # test the initial guess is respected @named sys = System(eqs, t, defaults = Dict(z => NaN)) -infprob = ODEProblem(structural_simplify(sys), [x => 1.0], (0, 1.0), [p => 0.2]) +infprob = ODEProblem(mtkcompile(sys), [x => 1.0], (0, 1.0), [p => 0.2]) infprob.f(du, infprob.u0, pr, tt) @test any(isnan, du) @@ -192,7 +192,7 @@ calculate_tgrad(ms_model) u0 = [mass.s => 0.0 mass.v => 1.0] -sys = structural_simplify(ms_model) +sys = mtkcompile(ms_model) # @test ModelingToolkit.get_jac(sys)[] === ModelingToolkit.EMPTY_JAC # @test ModelingToolkit.get_tgrad(sys)[] === ModelingToolkit.EMPTY_TGRAD prob_complex = ODEProblem(sys, u0, (0, 1.0)) diff --git a/test/structural_transformation/utils.jl b/test/structural_transformation/utils.jl index b228cdbbf7..02298eb480 100644 --- a/test/structural_transformation/utils.jl +++ b/test/structural_transformation/utils.jl @@ -49,7 +49,7 @@ end @variables x(t) y(t)[1:2] z(t)[1:2] @parameters foo(::AbstractVector)[1:2] _tmp_fn(x) = 2x - @mtkbuild sys = System( + @mtkcompile sys = System( [D(x) ~ z[1] + z[2] + foo(z)[1], y[1] ~ 2t, y[2] ~ 3t, z ~ foo(y)], t) @test length(equations(sys)) == 1 @test length(observed(sys)) == 7 @@ -75,7 +75,7 @@ end val[] += 1 return [x, 2x] end - @mtkbuild sys = System([D(x) ~ y[1] + y[2], y ~ foo(x)], t) + @mtkcompile sys = System([D(x) ~ y[1] + y[2], y ~ foo(x)], t) @test length(equations(sys)) == 1 @test length(observed(sys)) == 4 prob = ODEProblem(sys, [x => 1.0], (0.0, 1.0), [foo => _tmp_fn2]) @@ -94,7 +94,7 @@ end @testset "CSE hack in equations(sys)" begin val[] = 0 @variables z(t)[1:2] - @mtkbuild sys = System( + @mtkcompile sys = System( [D(y) ~ foo(x), D(x) ~ sum(y), zeros(2) ~ foo(prod(z))], t) @test length(equations(sys)) == 5 @test length(observed(sys)) == 2 @@ -123,14 +123,14 @@ end @named sys = System( [D(x) ~ z[1] + z[2] + foo(z)[1], y[1] ~ 2t, y[2] ~ 3t, z ~ foo(y)], t) - sys1 = structural_simplify(sys; cse_hack = false) + sys1 = mtkcompile(sys; cse_hack = false) @test length(observed(sys1)) == 6 @test !any(observed(sys1)) do eq iscall(eq.rhs) && operation(eq.rhs) == StructuralTransformations.getindex_wrapper end - sys2 = structural_simplify(sys; array_hack = false) + sys2 = mtkcompile(sys; array_hack = false) @test length(observed(sys2)) == 5 @test !any(observed(sys2)) do eq iscall(eq.rhs) && operation(eq.rhs) == StructuralTransformations.change_origin @@ -144,14 +144,14 @@ end @named sys = System( [D(x) ~ z[1] + z[2] + foo(z)[1] + w, y[1] ~ 2t, y[2] ~ 3t, z ~ foo(y)], t) - sys1 = structural_simplify(sys; cse_hack = false, fully_determined = false) + sys1 = mtkcompile(sys; cse_hack = false, fully_determined = false) @test length(observed(sys1)) == 6 @test !any(observed(sys1)) do eq iscall(eq.rhs) && operation(eq.rhs) == StructuralTransformations.getindex_wrapper end - sys2 = structural_simplify(sys; array_hack = false, fully_determined = false) + sys2 = mtkcompile(sys; array_hack = false, fully_determined = false) @test length(observed(sys2)) == 5 @test !any(observed(sys2)) do eq iscall(eq.rhs) && operation(eq.rhs) == StructuralTransformations.change_origin @@ -164,7 +164,7 @@ end @named sys = System([D(x) ~ x, y ~ x + t], t) value = Ref(0) pass(sys; kwargs...) = (value[] += 1; return sys) - structural_simplify(sys; additional_passes = [pass]) + mtkcompile(sys; additional_passes = [pass]) @test value[] == 1 end @@ -200,7 +200,7 @@ end end @testset "`ODESystem`" begin @variables x(t) y(t) z(t) - @mtkbuild sys = System([D(x) ~ 2x + y, y ~ x + z, z^3 + x^3 ~ 12], t) + @mtkcompile sys = System([D(x) ~ 2x + y, y ~ x + z, z^3 + x^3 ~ 12], t) mapping = map_variables_to_equations(sys) @test mapping[x] == (D(x) ~ 2x + y) @test mapping[y] == (y ~ x + z) @@ -213,7 +213,7 @@ end eqs = [D(D(x)) ~ λ * x D(D(y)) ~ λ * y - g x^2 + y^2 ~ 1] - @mtkbuild sys = System(eqs, t) + @mtkcompile sys = System(eqs, t) mapping = map_variables_to_equations(sys) yt = default_toterm(unwrap(D(y))) @@ -254,7 +254,7 @@ end eqs = [osc1.jcn ~ osc2.delx, osc2.jcn ~ osc1.delx] @named coupledOsc = System(eqs, t) - @mtkbuild sys = compose(coupledOsc, systems) + @mtkcompile sys = compose(coupledOsc, systems) mapping = map_variables_to_equations(sys) x1 = operation(unwrap(osc1.x)) x2 = operation(unwrap(osc2.x)) @@ -271,7 +271,7 @@ end end @testset "`NonlinearSystem`" begin @variables x y z - @mtkbuild sys = System([x^2 ~ 2y^2 + 1, sin(z) ~ y, z^3 + 4z + 1 ~ 0]) + @mtkcompile sys = System([x^2 ~ 2y^2 + 1, sin(z) ~ y, z^3 + 4z + 1 ~ 0]) mapping = map_variables_to_equations(sys) @test mapping[x] == (0 ~ 2y^2 + 1 - x^2) @test mapping[y] == (y ~ sin(z)) @@ -335,9 +335,9 @@ end end @named sys = FilteredInputErr() - @test_throws ["derivative of discrete variable", "k(t)"] structural_simplify(sys) + @test_throws ["derivative of discrete variable", "k(t)"] mtkcompile(sys) - @mtkbuild sys = FilteredInput() + @mtkcompile sys = FilteredInput() vs = Set() for eq in equations(sys) ModelingToolkit.vars!(vs, eq) @@ -348,7 +348,7 @@ end @test !(D(sys.k) in vs) - @mtkbuild sys = FilteredInputExplicit() + @mtkcompile sys = FilteredInputExplicit() obsfn1 = ModelingToolkit.build_explicit_observed_function(sys, sys.ddx) obsfn2 = ModelingToolkit.build_explicit_observed_function(sys, sys.dx) u = [1.0] @@ -375,7 +375,7 @@ end return System(eqs, t, vars, params; systems, name) end - @mtkbuild sys = FilteredInput2() + @mtkcompile sys = FilteredInput2() vs = Set() for eq in equations(sys) ModelingToolkit.vars!(vs, eq) @@ -392,7 +392,7 @@ end @testset "ODESystem" begin @variables x(t) p @parameters y(t) q - @mtkbuild sys = System([D(x) ~ x * q, x^2 + y^2 ~ p], t, [x, y], + @mtkcompile sys = System([D(x) ~ x * q, x^2 + y^2 ~ p], t, [x, y], [p, q]; initialization_eqs = [p + q ~ 3], defaults = [p => missing], guesses = [p => 1.0, y => 1.0]) @test length(equations(sys)) == 2 @@ -406,7 +406,7 @@ end @testset "NonlinearSystem" begin @variables x p @parameters y q - @mtkbuild sys = System([0 ~ p * x + y, x^3 + y^3 ~ q], [x, y], + @mtkcompile sys = System([0 ~ p * x + y, x^3 + y^3 ~ q], [x, y], [p, q]; initialization_eqs = [p ~ q + 1], guesses = [p => 1.0], defaults = [p => missing]) @test length(equations(sys)) == length(unknowns(sys)) == 1 @@ -422,7 +422,7 @@ end @variables x(t) p a @parameters y(t) q b @brownian c - @mtkbuild sys = System([D(x) ~ x + q * a, D(y) ~ y + p * b + c], t, [x, y], + @mtkcompile sys = System([D(x) ~ x + q * a, D(y) ~ y + p * b + c], t, [x, y], [p, q], [a, b, c]; initialization_eqs = [p + q ~ 4], guesses = [p => 1.0], defaults = [p => missing]) @test length(equations(sys)) == 2 @@ -436,3 +436,10 @@ end @test integ.ps[p] ≈ 3.0 end end + +@testset "Deprecated `structural_simplify` and `@mtkbuild`" begin + @variables x(t) + @test_deprecated @mtkbuild sys = System([D(x) ~ x], t) + @named sys = System([D(x) ~ x], t) + @test_deprecated structural_simplify(sys) +end diff --git a/test/substitute_component.jl b/test/substitute_component.jl index ad20dbcbc1..e598d86a06 100644 --- a/test/substitute_component.jl +++ b/test/substitute_component.jl @@ -59,8 +59,8 @@ end @named reference = RC() - sys1 = structural_simplify(rcsys) - sys2 = structural_simplify(reference) + sys1 = mtkcompile(rcsys) + sys2 = mtkcompile(reference) @test isequal(unknowns(sys1), unknowns(sys2)) @test isequal(equations(sys1), equations(sys2)) diff --git a/test/symbolic_events.jl b/test/symbolic_events.jl index d3c767ee0d..94a443fc4d 100644 --- a/test/symbolic_events.jl +++ b/test/symbolic_events.jl @@ -312,7 +312,7 @@ end @test only(continuous_events(ball)) == SymbolicContinuousCallback(Equation[x ~ 0], Equation[v ~ -Pre(v)]) - ball = structural_simplify(ball) + ball = mtkcompile(ball) @test length(ModelingToolkit.continuous_events(ball)) == 1 @@ -334,8 +334,8 @@ end D(vy) ~ -0.01vy], t; continuous_events = events) _ball = ball - ball = structural_simplify(_ball) - ball_nosplit = structural_simplify(_ball; split = false) + ball = mtkcompile(_ball) + ball_nosplit = mtkcompile(_ball; split = false) tspan = (0.0, 5.0) prob = ODEProblem(ball, Pair[], tspan) @@ -373,8 +373,8 @@ end D(vx) ~ -1 D(vy) ~ 0], t; continuous_events = events) - ball_nosplit = structural_simplify(ball) - ball = structural_simplify(ball) + ball_nosplit = mtkcompile(ball) + ball = mtkcompile(ball) tspan = (0.0, 5.0) prob = ODEProblem(ball, Pair[], tspan) @@ -396,7 +396,7 @@ end D(vmeasured) ~ 0.0] ev = [sin(20pi * t) ~ 0.0] => [vmeasured ~ Pre(v)] @named sys = System(eq, t, continuous_events = ev) - sys = structural_simplify(sys) + sys = mtkcompile(sys) prob = ODEProblem(sys, zeros(2), (0.0, 5.1)) sol = solve(prob, Tsit5()) @test all(minimum((0:0.1:5) .- sol.t', dims = 2) .< 0.0001) # test that the solver stepped every 0.1s as dictated by event @@ -446,7 +446,7 @@ end @named model = compose(_model, mass1, mass2, sd) end model = Model(sin(30t)) - sys = structural_simplify(model) + sys = mtkcompile(model) @test isempty(ModelingToolkit.continuous_events(sys)) end @@ -632,7 +632,7 @@ end eqs = [oscce.F ~ 0] @named eqs_sys = System(eqs, t) @named oneosc_ce = compose(eqs_sys, oscce) - oneosc_ce_simpl = structural_simplify(oneosc_ce) + oneosc_ce_simpl = mtkcompile(oneosc_ce) prob = ODEProblem(oneosc_ce_simpl, [], (0.0, 2.0), []) sol = solve(prob, Tsit5(), saveat = 0.1) @@ -657,7 +657,7 @@ end evt2 = ModelingToolkit.SymbolicContinuousCallback( [c2 ~ 0], (f = record_crossings, observed = (; v = c2), ctx = cr2)) @named trigsys = System(eqs, t; continuous_events = [evt1, evt2]) - trigsys_ss = structural_simplify(trigsys) + trigsys_ss = mtkcompile(trigsys) prob = ODEProblem(trigsys_ss, [], (0.0, 2π)) sol = solve(prob, Tsit5()) required_crossings_c1 = [π / 2, 3 * π / 2] @@ -679,7 +679,7 @@ end [c2 ~ 0], (f = record_crossings, observed = (; v = c2), ctx = cr2p); affect_neg = (f = record_crossings, observed = (; v = c2), ctx = cr2n)) @named trigsys = System(eqs, t; continuous_events = [evt1, evt2]) - trigsys_ss = structural_simplify(trigsys) + trigsys_ss = mtkcompile(trigsys) prob = ODEProblem(trigsys_ss, [], (0.0, 2π)) sol = solve(prob, Tsit5(); dtmax = 0.01) c1_pc = filter((<=)(0) ∘ sin, required_crossings_c1) @@ -703,7 +703,7 @@ end evt2 = ModelingToolkit.SymbolicContinuousCallback( [c2 ~ 0], (f = record_crossings, observed = (; v = c2), ctx = cr2p); affect_neg = nothing) @named trigsys = System(eqs, t; continuous_events = [evt1, evt2]) - trigsys_ss = structural_simplify(trigsys) + trigsys_ss = mtkcompile(trigsys) prob = ODEProblem(trigsys_ss, [], (0.0, 2π)) sol = solve(prob, Tsit5(); dtmax = 0.01) @test maximum(abs.(c1_pc .- first.(cr1p))) < 1e-5 @@ -722,7 +722,7 @@ end [c2 ~ 0], (f = record_crossings, observed = (; v = c2), ctx = cr2p); affect_neg = (f = record_crossings, observed = (; v = c2), ctx = cr2n)) @named trigsys = System(eqs, t; continuous_events = [evt1, evt2]) - trigsys_ss = structural_simplify(trigsys) + trigsys_ss = mtkcompile(trigsys) prob = ODEProblem(trigsys_ss, [], (0.0, 2π)) sol = solve(prob, Tsit5(); dtmax = 0.01) c1_pc = filter((<=)(0) ∘ sin, required_crossings_c1) @@ -746,7 +746,7 @@ end [c2 ~ 0], (f = record_crossings, observed = (; v = c2), ctx = cr2); rootfind = SciMLBase.RightRootFind) @named trigsys = System(eqs, t; continuous_events = [evt1, evt2]) - trigsys_ss = structural_simplify(trigsys) + trigsys_ss = mtkcompile(trigsys) prob = ODEProblem(trigsys_ss, [], (0.0, 2π)) sol = solve(prob, Tsit5(); dtmax = 0.01) required_crossings_c1 = [π / 2, 3 * π / 2] @@ -766,7 +766,7 @@ end [c2 ~ 0], (f = record_crossings, observed = (; v = c2), ctx = cr2); rootfind = SciMLBase.RightRootFind) @named trigsys = System(eqs, t; continuous_events = [evt1, evt2]) - trigsys_ss = structural_simplify(trigsys) + trigsys_ss = mtkcompile(trigsys) prob = ODEProblem(trigsys_ss, [], (0.0, 2π)) sol = solve(prob, Tsit5()) @test maximum(abs.(first.(cr1) .- required_crossings_c1)) < 1e-4 @@ -784,7 +784,7 @@ end [c2 ~ 0], (f = record_crossings, observed = (; v = c2), ctx = cr2); rootfind = SciMLBase.RightRootFind) @named trigsys = System(eqs, t; continuous_events = [evt2, evt1]) - trigsys_ss = structural_simplify(trigsys) + trigsys_ss = mtkcompile(trigsys) prob = ODEProblem(trigsys_ss, [], (0.0, 2π)) sol = solve(prob, Tsit5()) @test maximum(abs.(first.(cr1) .- required_crossings_c1)) < 1e-4 @@ -867,7 +867,7 @@ end end # Test Simulation - @mtkbuild sys = TestSystem() + @mtkcompile sys = TestSystem() # Test Simulation prob = ODEProblem(sys, [], (0.0, 150.0)) @@ -885,7 +885,7 @@ end cb2 = [x ~ 0.5] => (f = save_affect!, modified = (; b)) cb3 = SymbolicDiscreteCallback(1.0 => [c ~ t], discrete_parameters = [c]) - @mtkbuild sys = System(D(x) ~ cos(t), t, [x], [a, b, c]; + @mtkcompile sys = System(D(x) ~ cos(t), t, [x], [a, b, c]; continuous_events = [cb1, cb2], discrete_events = [cb3]) prob = ODEProblem(sys, [x => 1.0], (0.0, 2pi), [a => 1.0, b => 2.0, c => 0.0]) @test sort(canonicalize(Discrete(), prob.p)[1]) == [0.0, 1.0, 2.0] @@ -915,7 +915,7 @@ end end) @named sys = System( eqs, t, [temp], params; continuous_events = [furnace_off, furnace_enable]) - ss = structural_simplify(sys) + ss = mtkcompile(sys) prob = ODEProblem(ss, [temp => 0.0, furnace_on => true], (0.0, 100.0)) sol = solve(prob, Tsit5(); dtmax = 0.01) @test all(sol[temp][sol.t .> 1.0] .<= 0.79) && all(sol[temp][sol.t .> 1.0] .>= 0.49) @@ -935,7 +935,7 @@ end end) @named sys = System( eqs, t, [temp], params; continuous_events = [furnace_off, furnace_enable]) - ss = structural_simplify(sys) + ss = mtkcompile(sys) prob = ODEProblem(ss, [temp => 0.0, furnace_on => true], (0.0, 100.0)) sol = solve(prob, Tsit5(); dtmax = 0.01) @test all(sol[temp][sol.t .> 1.0] .<= 0.79) && all(sol[temp][sol.t .> 1.0] .>= 0.49) @@ -956,7 +956,7 @@ end @set! x.furnace_on = false end) @named sys = System(eqs, t, [temp], params; continuous_events = [furnace_off]) - ss = structural_simplify(sys) + ss = mtkcompile(sys) @test_logs (:warn, "The symbols Any[:furnace_on] are declared as both observed and modified; this is a code smell because it becomes easy to confuse them and assign/not assign a value.") prob=ODEProblem( ss, [temp => 0.0, furnace_on => true], (0.0, 100.0)) @@ -973,7 +973,7 @@ end end) @named sys = System( eqs, t, [temp, tempsq], params; continuous_events = [furnace_off]) - ss = structural_simplify(sys) + ss = mtkcompile(sys) @test_throws "refers to missing variable(s)" prob=ODEProblem( ss, [temp => 0.0, furnace_on => true], (0.0, 100.0)) @@ -986,7 +986,7 @@ end end) @named sys = System( eqs, t, [temp, tempsq], params; continuous_events = [furnace_off]) - ss = structural_simplify(sys) + ss = mtkcompile(sys) @test_throws "refers to missing variable(s)" prob=ODEProblem( ss, [temp => 0.0, furnace_on => true], (0.0, 100.0)) @@ -998,7 +998,7 @@ end end) @named sys = System( eqs, t, [temp, tempsq], params; continuous_events = [furnace_off]) - ss = structural_simplify(sys) + ss = mtkcompile(sys) prob = ODEProblem( ss, [temp => 0.0, furnace_on => true], (0.0, 100.0)) @test_throws "Tried to write back to" solve(prob, Tsit5()) @@ -1058,7 +1058,7 @@ end end; rootfind = SciMLBase.RightRootFind) @named sys = System( eqs, t, [theta, omega], params; continuous_events = [qAevt, qBevt]) - ss = structural_simplify(sys) + ss = mtkcompile(sys) prob = ODEProblem(ss, [theta => 1e-5], (0.0, pi)) sol = solve(prob, Tsit5(); dtmax = 0.01) @test getp(sol, cnt)(sol) == 198 # we get 2 pulses per phase cycle (cos 0 crossing) and we go to 100 cycles; we miss a few due to the initial state @@ -1070,7 +1070,7 @@ end f = ModelingToolkit.ImperativeAffect(f = (m, o, ctx, int) -> (seen = true; return (;))) cb1 = ModelingToolkit.SymbolicContinuousCallback( [x ~ 0], nothing, initialize = [x ~ 1.5], finalize = f) - @mtkbuild sys = System(D(x) ~ -1, t, [x], []; continuous_events = [cb1]) + @mtkcompile sys = System(D(x) ~ -1, t, [x], []; continuous_events = [cb1]) prob = ODEProblem(sys, [x => 1.0], (0.0, 2), []) sol = solve(prob, Tsit5(); dtmax = 0.01) @test sol[x][1] ≈ 1.0 @@ -1088,7 +1088,7 @@ end b = ModelingToolkit.ImperativeAffect(f = (m, o, ctx, int) -> (finaled = true; return (;))) cb2 = ModelingToolkit.SymbolicContinuousCallback( [x ~ 0.1], nothing, initialize = a, finalize = b) - @mtkbuild sys = System(D(x) ~ -1, t, [x], []; continuous_events = [cb1, cb2]) + @mtkcompile sys = System(D(x) ~ -1, t, [x], []; continuous_events = [cb1, cb2]) prob = ODEProblem(sys, [x => 1.0], (0.0, 2), []) sol = solve(prob, Tsit5()) @test sol[x][1] ≈ 1.0 @@ -1102,7 +1102,7 @@ end finaled = false cb3 = ModelingToolkit.SymbolicDiscreteCallback( 1.0, [x ~ 2], initialize = a, finalize = b) - @mtkbuild sys = System(D(x) ~ -1, t, [x], []; discrete_events = [cb3]) + @mtkcompile sys = System(D(x) ~ -1, t, [x], []; discrete_events = [cb3]) prob = ODEProblem(sys, [x => 1.0], (0.0, 2), []) sol = solve(prob, Tsit5()) @test inited == true @@ -1115,7 +1115,7 @@ end inited = false finaled = false cb3 = ModelingToolkit.SymbolicDiscreteCallback(1.0, f, initialize = a, finalize = b) - @mtkbuild sys = System(D(x) ~ -1, t, [x], []; discrete_events = [cb3]) + @mtkcompile sys = System(D(x) ~ -1, t, [x], []; discrete_events = [cb3]) prob = ODEProblem(sys, [x => 1.0], (0.0, 2), []) sol = solve(prob, Tsit5()) @test seen == true @@ -1126,7 +1126,7 @@ end inited = false finaled = false cb3 = ModelingToolkit.SymbolicDiscreteCallback([1.0], f, initialize = a, finalize = b) - @mtkbuild sys = System(D(x) ~ -1, t, [x], []; discrete_events = [cb3]) + @mtkcompile sys = System(D(x) ~ -1, t, [x], []; discrete_events = [cb3]) prob = ODEProblem(sys, [x => 1.0], (0.0, 2), []) sol = solve(prob, Tsit5()) @test seen == true @@ -1139,7 +1139,7 @@ end finaled = false cb3 = ModelingToolkit.SymbolicDiscreteCallback( t == 1.0, f, initialize = a, finalize = b) - @mtkbuild sys = System(D(x) ~ -1, t, [x], []; discrete_events = [cb3]) + @mtkcompile sys = System(D(x) ~ -1, t, [x], []; discrete_events = [cb3]) prob = ODEProblem(sys, [x => 1.0], (0.0, 2), []) sol = solve(prob, Tsit5(); tstops = 1.0) @test seen == true @@ -1151,17 +1151,17 @@ end @variables x(t) [irreducible = true] y(t) [irreducible = true] eqs = [x ~ y, D(x) ~ -1] cb = [x ~ 0.0] => [x ~ 0, y ~ 1] - @mtkbuild pend = System(eqs, t; continuous_events = [cb]) + @mtkcompile pend = System(eqs, t; continuous_events = [cb]) prob = ODEProblem(pend, [x => 1], (0.0, 3.0), guesses = [y => x]) @test_broken !SciMLBase.successful_retcode(solve(prob, Rodas5())) cb = [x ~ 0.0] => [y ~ 1] - @mtkbuild pend = System(eqs, t; continuous_events = [cb]) + @mtkcompile pend = System(eqs, t; continuous_events = [cb]) prob = ODEProblem(pend, [x => 1], (0.0, 3.0), guesses = [y => x]) @test_broken !SciMLBase.successful_retcode(solve(prob, Rodas5())) cb = [x ~ 0.0] => [x ~ 1, y ~ 1] - @mtkbuild pend = System(eqs, t; continuous_events = [cb]) + @mtkcompile pend = System(eqs, t; continuous_events = [cb]) prob = ODEProblem(pend, [x => 1], (0.0, 3.0), guesses = [y => x]) @test all(≈(0.0; atol = 1e-9), solve(prob, Rodas5())[[x, y]][end]) end @@ -1182,7 +1182,7 @@ end (t == 1.0) => [k ~ 1.0], [discrete_parameters = k] end end - @mtkbuild decay = DECAY() + @mtkcompile decay = DECAY() prob = ODEProblem(decay, [], (0.0, 10.0), []) @test_nowarn solve(prob, Tsit5(), tstops = [1.0]) end @@ -1224,13 +1224,13 @@ end @named wd1 = weird1(0.021) @named wd2 = weird2(0.021) - sys1 = structural_simplify(System(Equation[], t; name = :parent, + sys1 = mtkcompile(System(Equation[], t; name = :parent, discrete_events = [0.01 => ModelingToolkit.ImperativeAffect( modified = (; θs = reduce(vcat, [[wd1.θ]])), ctx = [1]) do m, o, c, i @set! m.θs[1] = c[] += 1 end], systems = [wd1])) - sys2 = structural_simplify(System(Equation[], t; name = :parent, + sys2 = mtkcompile(System(Equation[], t; name = :parent, discrete_events = [0.01 => ModelingToolkit.ImperativeAffect( modified = (; θs = reduce(vcat, [[wd2.θ]])), ctx = [1]) do m, o, c, i @set! m.θs[1] = c[] += 1 @@ -1252,7 +1252,7 @@ end D(D(y)) ~ λ * y - g x^2 + y^2 ~ 1] c_evt = [t ~ 5.0] => [x ~ Pre(x) + 0.1] - @mtkbuild pend = System(eqs, t, continuous_events = c_evt) + @mtkcompile pend = System(eqs, t, continuous_events = c_evt) prob = ODEProblem(pend, [x => -1, y => 0], (0.0, 10.0), [g => 1], guesses = [λ => 1]) sol = solve(prob, FBDF()) @test ≈(sol(5.000001, idxs = x) - sol(4.999999, idxs = x), 0.1, rtol = 1e-4) @@ -1260,7 +1260,7 @@ end # Implicit affect with Pre c_evt = [t ~ 5.0] => [x ~ Pre(x) + y^2] - @mtkbuild pend = System(eqs, t, continuous_events = c_evt) + @mtkcompile pend = System(eqs, t, continuous_events = c_evt) prob = ODEProblem(pend, [x => 1, y => 0], (0.0, 10.0), [g => 1], guesses = [λ => 1]) sol = solve(prob, FBDF()) @test ≈(sol(5.000001, idxs = y)^2 + sol(4.999999, idxs = x), @@ -1269,7 +1269,7 @@ end # Impossible affect errors c_evt = [t ~ 5.0] => [x ~ Pre(x) + 2] - @mtkbuild pend = System(eqs, t, continuous_events = c_evt) + @mtkcompile pend = System(eqs, t, continuous_events = c_evt) prob = ODEProblem(pend, [x => 1, y => 0], (0.0, 10.0), [g => 1], guesses = [λ => 1]) @test_throws UnsolvableCallbackError sol=solve(prob, FBDF()) @@ -1280,7 +1280,7 @@ end x^2 + y^2 ~ 1] c_evt = SymbolicContinuousCallback( [t ~ 5.0], [x ~ Pre(x) + 0.1, g ~ Pre(g) + 1], discrete_parameters = [g], iv = t) - @mtkbuild pend = System(eqs, t, continuous_events = c_evt) + @mtkcompile pend = System(eqs, t, continuous_events = c_evt) prob = ODEProblem(pend, [x => 1, y => 0], (0.0, 10.0), [g => 1], guesses = [λ => 1]) sol = solve(prob, FBDF()) @test sol.ps[g] ≈ [1, 2] @@ -1290,7 +1290,7 @@ end eqs = [y ~ g^2, D(x) ~ x] c_evt = SymbolicContinuousCallback( [t ~ 5.0], [x ~ Pre(x) + 1, g ~ Pre(g) + 1], discrete_parameters = [g], iv = t) - @mtkbuild sys = System(eqs, t, continuous_events = c_evt) + @mtkcompile sys = System(eqs, t, continuous_events = c_evt) prob = ODEProblem(sys, [x => 1.0], (0.0, 10.0), [g => 2]) sol = solve(prob, FBDF()) @test sol.ps[g] ≈ [2.0, 3.0] @@ -1299,7 +1299,7 @@ end # Parameters that don't appear in affects should not be mutated. c_evt = [t ~ 5.0] => [x ~ Pre(x) + 1] - @mtkbuild sys = System(eqs, t, continuous_events = c_evt) + @mtkcompile sys = System(eqs, t, continuous_events = c_evt) prob = ODEProblem(sys, [x => 0.5], (0.0, 10.0), [g => 2], guesses = [y => 0]) sol = solve(prob, FBDF()) @test prob.ps[g] == sol.ps[g] @@ -1328,6 +1328,6 @@ end @named sys = System(Equation[], t, [], Symbolics.scalarize(vals); systems = [child(vals; name = :child)]) - sys = structural_simplify(sys) + sys = mtkcompile(sys) sol = solve(ODEProblem(sys, [], (0.0, 1.0)), Tsit5()) end diff --git a/test/symbolic_indexing_interface.jl b/test/symbolic_indexing_interface.jl index 14cf0c10a7..f71fe93842 100644 --- a/test/symbolic_indexing_interface.jl +++ b/test/symbolic_indexing_interface.jl @@ -78,7 +78,7 @@ end # D(x) ~ -x + u # y ~ x] -# @mtkbuild cl = System(eqs, t) +# @mtkcompile cl = System(eqs, t) # partition1_params = [Hold(ud1), Sample(t, dt)(y), ud1, yd1] # partition2_params = [Hold(ud2), Sample(t, dt2)(y), ud2, yd2] # @test all( @@ -195,7 +195,7 @@ end @testset "Observed functions with variables as `Symbol`s" begin @variables x(t) y(t) z(t)[1:2] @parameters p1 p2[1:2, 1:2] - @mtkbuild sys = System([D(x) ~ x * t + p1, y ~ 2x, D(z) ~ p2 * z], t) + @mtkcompile sys = System([D(x) ~ x * t + p1, y ~ 2x, D(z) ~ p2 * z], t) prob = ODEProblem( sys, [x => 1.0, z => ones(2)], (0.0, 1.0), [p1 => 2.0, p2 => ones(2, 2)]) @test getu(prob, x)(prob) == getu(prob, :x)(prob) @@ -218,7 +218,7 @@ end @variables x(t) y(t) z(t) @parameters a @named sys = System([D(x) ~ a * x, y ~ 2x, z ~ 0.0], t) - sys = structural_simplify(sys, split = false) + sys = mtkcompile(sys, split = false) for sym in [x, y, z, x + y, x + a, y / x] @test only(get_all_timeseries_indexes(sys, sym)) == ContinuousTimeseries() end @@ -230,7 +230,7 @@ end @parameters p(t)[1:2, 1:2] ev = SymbolicContinuousCallback( [x[1] ~ 2.0] => [p ~ -ones(2, 2)], discrete_parameters = [p]) - @mtkbuild sys = System(D(x) ~ p * x, t; continuous_events = [ev]) + @mtkcompile sys = System(D(x) ~ p * x, t; continuous_events = [ev]) p = ModelingToolkit.unwrap(p) @test timeseries_parameter_index(sys, p) === ParameterTimeseriesIndex(1, (1, 1)) @test timeseries_parameter_index(sys, p[1, 1]) === diff --git a/test/units.jl b/test/units.jl index f15145ffa9..77f35877e9 100644 --- a/test/units.jl +++ b/test/units.jl @@ -140,24 +140,24 @@ D = Differential(t) eqs = [D(L) ~ v, V ~ L^3] @named sys = System(eqs, t) -sys_simple = structural_simplify(sys) +sys_simple = mtkcompile(sys) eqs = [D(V) ~ r, V ~ L^3] @named sys = System(eqs, t) -sys_simple = structural_simplify(sys) +sys_simple = mtkcompile(sys) @variables V [unit = u"m"^3] L [unit = u"m"] @parameters v [unit = u"m/s"] r [unit = u"m"^3 / u"s"] t [unit = u"s"] eqs = [V ~ r * t, V ~ L^3] @named sys = System(eqs, [V, L], [t, r]) -sys_simple = structural_simplify(sys) +sys_simple = mtkcompile(sys) eqs = [L ~ v * t, V ~ L^3] @named sys = System(eqs, [V, L], [v, t, r]) -sys_simple = structural_simplify(sys) +sys_simple = mtkcompile(sys) #Jump System @parameters β [unit = u"(mol^2*s)^-1"] γ [unit = u"(mol*s)^-1"] t [unit = u"s"] jumpmol [ diff --git a/test/variable_scope.jl b/test/variable_scope.jl index 2ecd62ec1f..080cbcc7c4 100644 --- a/test/variable_scope.jl +++ b/test/variable_scope.jl @@ -125,7 +125,7 @@ defs = ModelingToolkit.defaults(bar) sys4 = complete(sys3) @test length(unknowns(sys4)) == 3 @test length(parameters(sys4)) == 4 - sys5 = structural_simplify(sys3) + sys5 = mtkcompile(sys3) @test length(unknowns(sys5)) == 4 @test any(isequal(x4), unknowns(sys5)) @test length(parameters(sys5)) == 4 diff --git a/test/variable_utils.jl b/test/variable_utils.jl index 36b80a21dd..c088481925 100644 --- a/test/variable_utils.jl +++ b/test/variable_utils.jl @@ -76,7 +76,7 @@ end sys = System(Equation[], iv; name, systems = [😄, arr]) end - @mtkbuild sys = Outer() + @mtkcompile sys = Outer() for (str, var) in [ # unicode system, scalar variable ("😄.x", sys.😄.x),