Skip to content

Commit 6f95e5c

Browse files
refactor: rename states to unknowns, and related changes
- also some bug fixes
1 parent 0e04e8a commit 6f95e5c

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

59 files changed

+385
-385
lines changed

docs/src/basics/AbstractSystem.md

+4-4
Original file line numberDiff line numberDiff line change
@@ -38,18 +38,18 @@ same keyword arguments, which are:
3838

3939
Each `AbstractSystem` has lists of variables in context, such as distinguishing
4040
parameters vs states. In addition, an `AbstractSystem` can also hold other
41-
`AbstractSystem` types. Direct accessing of the values, such as `sys.states`,
42-
gives the immediate list, while the accessor functions `states(sys)` gives the
41+
`AbstractSystem` types. Direct accessing of the values, such as `sys.unknowns`,
42+
gives the immediate list, while the accessor functions `unknowns(sys)` gives the
4343
total set, which includes that of all systems held inside.
4444

4545
The values which are common to all `AbstractSystem`s are:
4646

4747
- `equations(sys)`: All equations that define the system and its subsystems.
48-
- `states(sys)`: All the states in the system and its subsystems.
48+
- `unknowns(sys)`: All the unknowns in the system and its subsystems.
4949
- `parameters(sys)`: All parameters of the system and its subsystems.
5050
- `nameof(sys)`: The name of the current-level system.
5151
- `get_eqs(sys)`: Equations that define the current-level system.
52-
- `get_states(sys)`: States that are in the current-level system.
52+
- `get_unknowns(sys)`: States that are in the current-level system.
5353
- `get_ps(sys)`: Parameters that are in the current-level system.
5454
- `get_systems(sys)`: Subsystems of the current-level system.
5555

docs/src/basics/Linearization.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ The named tuple `matrices` contains the matrices of the linear statespace repres
3838

3939
```@example LINEARIZE
4040
using ModelingToolkit: inputs, outputs
41-
[states(simplified_sys); inputs(simplified_sys); outputs(simplified_sys)]
41+
[unknowns(simplified_sys); inputs(simplified_sys); outputs(simplified_sys)]
4242
```
4343

4444
## Operating point

docs/src/examples/modelingtoolkitize_index_reduction.md

+4-4
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ traced_sys = modelingtoolkitize(pendulum_prob)
3232
pendulum_sys = structural_simplify(dae_index_lowering(traced_sys))
3333
prob = ODAEProblem(pendulum_sys, [], tspan)
3434
sol = solve(prob, Tsit5(), abstol = 1e-8, reltol = 1e-8)
35-
plot(sol, idxs = states(traced_sys))
35+
plot(sol, idxs = unknowns(traced_sys))
3636
```
3737

3838
## Explanation
@@ -154,10 +154,10 @@ prob = ODEProblem(pendulum_sys, Pair[], tspan)
154154
sol = solve(prob, Rodas4())
155155
156156
using Plots
157-
plot(sol, idxs = states(traced_sys))
157+
plot(sol, idxs = unknowns(traced_sys))
158158
```
159159

160-
Note that plotting using `states(traced_sys)` is done so that any
160+
Note that plotting using `unknowns(traced_sys)` is done so that any
161161
variables which are symbolically eliminated, or any variable reordering
162162
done for enhanced parallelism/performance, still show up in the resulting
163163
plot and the plot is shown in the same order as the original numerical
@@ -173,7 +173,7 @@ traced_sys = modelingtoolkitize(pendulum_prob)
173173
pendulum_sys = structural_simplify(dae_index_lowering(traced_sys))
174174
prob = ODAEProblem(pendulum_sys, Pair[], tspan)
175175
sol = solve(prob, Tsit5(), abstol = 1e-8, reltol = 1e-8)
176-
plot(sol, idxs = states(traced_sys))
176+
plot(sol, idxs = unknowns(traced_sys))
177177
```
178178

179179
And there you go: this has transformed the model from being too hard to

docs/src/systems/JumpSystem.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ JumpSystem
99
## Composition and Accessor Functions
1010

1111
- `get_eqs(sys)` or `equations(sys)`: The equations that define the jump system.
12-
- `get_states(sys)` or `states(sys)`: The set of states in the jump system.
12+
- `get_unknowns(sys)` or `unknowns(sys)`: The set of unknowns in the jump system.
1313
- `get_ps(sys)` or `parameters(sys)`: The parameters of the jump system.
1414
- `get_iv(sys)`: The independent variable of the jump system.
1515

docs/src/systems/NonlinearSystem.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ NonlinearSystem
99
## Composition and Accessor Functions
1010

1111
- `get_eqs(sys)` or `equations(sys)`: The equations that define the nonlinear system.
12-
- `get_states(sys)` or `states(sys)`: The set of states in the nonlinear system.
12+
- `get_unknowns(sys)` or `unknowns(sys)`: The set of unknowns in the nonlinear system.
1313
- `get_ps(sys)` or `parameters(sys)`: The parameters of the nonlinear system.
1414
- `get_u0_p(sys, u0map, parammap)` Numeric arrays for the initial condition and parameters given `var => value` maps.
1515

docs/src/systems/ODESystem.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ ODESystem
99
## Composition and Accessor Functions
1010

1111
- `get_eqs(sys)` or `equations(sys)`: The equations that define the ODE.
12-
- `get_states(sys)` or `states(sys)`: The set of states in the ODE.
12+
- `get_unknowns(sys)` or `unknowns(sys)`: The set of unknowns in the ODE.
1313
- `get_ps(sys)` or `parameters(sys)`: The parameters of the ODE.
1414
- `get_iv(sys)`: The independent variable of the ODE.
1515
- `get_u0_p(sys, u0map, parammap)` Numeric arrays for the initial condition and parameters given `var => value` maps.

docs/src/systems/OptimizationSystem.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ OptimizationSystem
99
## Composition and Accessor Functions
1010

1111
- `get_op(sys)`: The objective to be minimized.
12-
- `get_states(sys)` or `states(sys)`: The set of states for the optimization.
12+
- `get_unknowns(sys)` or `unknowns(sys)`: The set of unknowns for the optimization.
1313
- `get_ps(sys)` or `parameters(sys)`: The parameters for the optimization.
1414
- `get_constraints(sys)` or `constraints(sys)`: The constraints for the optimization.
1515

docs/src/systems/SDESystem.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ sde = SDESystem(ode, noiseeqs)
1616
## Composition and Accessor Functions
1717

1818
- `get_eqs(sys)` or `equations(sys)`: The equations that define the SDE.
19-
- `get_states(sys)` or `states(sys)`: The set of states in the SDE.
19+
- `get_unknowns(sys)` or `unknowns(sys)`: The set of unknowns in the SDE.
2020
- `get_ps(sys)` or `parameters(sys)`: The parameters of the SDE.
2121
- `get_iv(sys)`: The independent variable of the SDE.
2222

ext/MTKBifurcationKitExt.jl

+6-6
Original file line numberDiff line numberDiff line change
@@ -30,12 +30,12 @@ struct ObservableRecordFromSolution{S, T}
3030
p_vals) where {S, T}
3131
obs_eqs = observed(nsys)
3232
target_obs_idx = findfirst(isequal(plot_var, eq.lhs) for eq in observed(nsys))
33-
state_end_idxs = length(states(nsys))
33+
state_end_idxs = length(unknowns(nsys))
3434
param_end_idxs = state_end_idxs + length(parameters(nsys))
3535

3636
bif_par_idx = state_end_idxs + bif_idx
3737
# Gets the (base) substitution values for states.
38-
subs_vals_states = Pair.(states(nsys), u0_vals)
38+
subs_vals_states = Pair.(unknowns(nsys), u0_vals)
3939
# Gets the (base) substitution values for parameters.
4040
subs_vals_params = Pair.(parameters(nsys), p_vals)
4141
# Gets the (base) substitution values for observables.
@@ -95,16 +95,16 @@ function BifurcationKit.BifurcationProblem(nsys::NonlinearSystem,
9595

9696
# Converts the input state guess.
9797
u0_bif_vals = ModelingToolkit.varmap_to_vars(u0_bif,
98-
states(nsys);
98+
unknowns(nsys);
9999
defaults = nsys.defaults)
100100
p_vals = ModelingToolkit.varmap_to_vars(ps, parameters(nsys); defaults = nsys.defaults)
101101

102102
# Computes bifurcation parameter and the plotting function.
103103
bif_idx = findfirst(isequal(bif_par), parameters(nsys))
104104
if !isnothing(plot_var)
105105
# If the plot var is a normal state.
106-
if any(isequal(plot_var, var) for var in states(nsys))
107-
plot_idx = findfirst(isequal(plot_var), states(nsys))
106+
if any(isequal(plot_var, var) for var in unknowns(nsys))
107+
plot_idx = findfirst(isequal(plot_var), unknowns(nsys))
108108
record_from_solution = (x, p) -> x[plot_idx]
109109

110110
# If the plot var is an observed state.
@@ -134,7 +134,7 @@ end
134134
# When input is a ODESystem.
135135
function BifurcationKit.BifurcationProblem(osys::ODESystem, args...; kwargs...)
136136
nsys = NonlinearSystem([0 ~ eq.rhs for eq in equations(osys)],
137-
states(osys),
137+
unknowns(osys),
138138
parameters(osys);
139139
name = nameof(osys))
140140
return BifurcationKit.BifurcationProblem(nsys, args...; kwargs...)

src/ModelingToolkit.jl

+1-2
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ using PrecompileTools, Reexport
3636
using RecursiveArrayTools
3737

3838
using SymbolicIndexingInterface
39-
export independent_variables, states, parameters
39+
export independent_variables, unknowns, parameters
4040
import SymbolicUtils
4141
import SymbolicUtils: istree, arguments, operation, similarterm, promote_symtype,
4242
Symbolic, isadd, ismul, ispow, issym, FnType,
@@ -196,7 +196,6 @@ export ODEProblem, SDEProblem
196196
export NonlinearFunction, NonlinearFunctionExpr
197197
export NonlinearProblem, BlockNonlinearProblem, NonlinearProblemExpr
198198
export OptimizationProblem, OptimizationProblemExpr, constraints
199-
export AutoModelingToolkit
200199
export SteadyStateProblem, SteadyStateProblemExpr
201200
export JumpProblem, DiscreteProblem
202201
export NonlinearSystem, OptimizationSystem, ConstraintsSystem

src/inputoutput.jl

+4-4
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ using Symbolics: get_variables
55
Return all variables that mare marked as inputs. See also [`unbound_inputs`](@ref)
66
See also [`bound_inputs`](@ref), [`unbound_inputs`](@ref)
77
"""
8-
inputs(sys) = [filter(isinput, states(sys)); filter(isinput, parameters(sys))]
8+
inputs(sys) = [filter(isinput, unknowns(sys)); filter(isinput, parameters(sys))]
99

1010
"""
1111
outputs(sys)
@@ -17,7 +17,7 @@ function outputs(sys)
1717
o = observed(sys)
1818
rhss = [eq.rhs for eq in o]
1919
lhss = [eq.lhs for eq in o]
20-
unique([filter(isoutput, states(sys))
20+
unique([filter(isoutput, unknowns(sys))
2121
filter(isoutput, parameters(sys))
2222
filter(x -> istree(x) && isoutput(x), rhss) # observed can return equations with complicated expressions, we are only looking for single Terms
2323
filter(x -> istree(x) && isoutput(x), lhss)])
@@ -205,7 +205,7 @@ function generate_control_function(sys::AbstractODESystem, inputs = unbound_inpu
205205

206206
sys, _ = io_preprocessing(sys, inputs, []; simplify, kwargs...)
207207

208-
dvs = states(sys)
208+
dvs = unknowns(sys)
209209
ps = parameters(sys)
210210
ps = setdiff(ps, inputs)
211211
if disturbance_inputs !== nothing
@@ -300,7 +300,7 @@ function inputs_to_parameters!(state::TransformationState, io)
300300

301301
@set! sys.eqs = isempty(input_to_parameters) ? equations(sys) :
302302
fast_substitute(equations(sys), input_to_parameters)
303-
@set! sys.states = setdiff(states(sys), keys(input_to_parameters))
303+
@set! sys.unknowns = setdiff(unknowns(sys), keys(input_to_parameters))
304304
ps = parameters(sys)
305305

306306
if io !== nothing

src/structural_transformation/StructuralTransformations.jl

+1-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ using SymbolicUtils: similarterm, istree
1111

1212
using ModelingToolkit
1313
using ModelingToolkit: ODESystem, AbstractSystem, var_from_nested_derivative, Differential,
14-
states, equations, vars, Symbolic, diff2term, value,
14+
unknowns, equations, vars, Symbolic, diff2term, value,
1515
operation, arguments, Sym, Term, simplify, solve_for,
1616
isdiffeq, isdifferential, isirreducible,
1717
empty_substitutions, get_substitutions,

src/structural_transformation/codegen.jl

+4-4
Original file line numberDiff line numberDiff line change
@@ -298,7 +298,7 @@ function build_torn_function(sys;
298298
rhss)
299299

300300
states = Any[fullvars[i] for i in states_idxs]
301-
@set! sys.unknown_states = states
301+
@set! sys.solved_unknowns = states
302302
syms = map(Symbol, states)
303303

304304
pre = get_postprocess_fbody(sys)
@@ -410,9 +410,9 @@ function build_observed_function(state, ts, var_eq_matching, var_sccs,
410410
required_algvars = Set(intersect(algvars, vars))
411411
obs = observed(sys)
412412
observed_idx = Dict(x.lhs => i for (i, x) in enumerate(obs))
413-
namespaced_to_obs = Dict(states(sys, x.lhs) => x.lhs for x in obs)
414-
namespaced_to_sts = Dict(states(sys, x) => x for x in states(sys))
415-
sts = Set(states(sys))
413+
namespaced_to_obs = Dict(unknowns(sys, x.lhs) => x.lhs for x in obs)
414+
namespaced_to_sts = Dict(unknowns(sys, x) => x for x in unknowns(sys))
415+
sts = Set(unknowns(sys))
416416

417417
# FIXME: This is a rather rough estimate of dependencies. We assume
418418
# the expression depends on everything before the `maxidx`.

src/structural_transformation/pantelides.jl

+1-1
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ function pantelides_reassemble(state::TearingState, var_eq_matching)
6565
out_eqs[sort(filter(x -> x !== unassigned, var_eq_matching))]))
6666

6767
@set! sys.eqs = final_eqs
68-
@set! sys.states = final_vars
68+
@set! sys.unknowns = final_vars
6969
return sys
7070
end
7171

src/structural_transformation/symbolics_tearing.jl

+1-1
Original file line numberDiff line numberDiff line change
@@ -540,7 +540,7 @@ function tearing_reassemble(state::TearingState, var_eq_matching;
540540

541541
sys = state.sys
542542
@set! sys.eqs = neweqs
543-
@set! sys.states = Any[v
543+
@set! sys.unknowns = Any[v
544544
for (i, v) in enumerate(fullvars)
545545
if diff_to_var[i] === nothing && ispresent(i)]
546546
@set! sys.substitutions = Substitutions(subeqs, deps)

0 commit comments

Comments
 (0)