Skip to content

Commit ec1ce76

Browse files
committed
Formatting & docstring updates.
1 parent 803c6a7 commit ec1ce76

File tree

6 files changed

+28
-23
lines changed

6 files changed

+28
-23
lines changed

src/constants.jl

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,14 @@ import SymbolicUtils: symtype, term, hasmetadata, issym
22
struct MTKConstantCtx end
33

44
isconstant(x::Num) = isconstant(unwrap(x))
5+
""" Test whether `x` is a constant-type Sym. """
56
function isconstant(x)
67
x = unwrap(x)
78
x isa Symbolic && getmetadata(x, MTKConstantCtx, false)
89
end
910

1011
"""
11-
toconst(s::Sym)
12+
toconstant(s::Sym)
1213
1314
Maps the parameter to a constant. The parameter must have a default.
1415
"""
@@ -23,7 +24,7 @@ toconstant(s::Num) = wrap(toconstant(value(s)))
2324
"""
2425
$(SIGNATURES)
2526
26-
Define one or more known variables.
27+
Define one or more constants.
2728
"""
2829
macro constants(xs...)
2930
Symbolics._parse_vars(:constants,

src/structural_transformation/codegen.jl

Lines changed: 15 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -193,9 +193,9 @@ function gen_nlsolve!(is_not_prepended_assignment, eqs, vars, u0map::AbstractDic
193193
f = Func([DestructuredArgs(vars, inbounds = !checkbounds)
194194
DestructuredArgs(params, inbounds = !checkbounds)],
195195
[],
196-
pre(Let(needed_assignments[inner_idxs],
197-
funex,
198-
false))) |> SymbolicUtils.Code.toexpr
196+
pre(Let(needed_assignments[inner_idxs],
197+
funex,
198+
false))) |> SymbolicUtils.Code.toexpr
199199

200200
# solver call contains code to call the root-finding solver on the function f
201201
solver_call = LiteralExpr(quote
@@ -302,15 +302,15 @@ function build_torn_function(sys;
302302

303303
expr = SymbolicUtils.Code.toexpr(Func([out
304304
DestructuredArgs(states,
305-
inbounds = !checkbounds)
305+
inbounds = !checkbounds)
306306
DestructuredArgs(parameters(sys),
307-
inbounds = !checkbounds)
307+
inbounds = !checkbounds)
308308
independent_variables(sys)],
309309
[],
310310
pre2(Let([torn_expr;
311-
assignments[is_not_prepended_assignment]],
312-
funbody,
313-
false))),
311+
assignments[is_not_prepended_assignment]],
312+
funbody,
313+
false))),
314314
sol_states)
315315
if expression
316316
expr, states
@@ -474,18 +474,19 @@ function build_observed_function(state, ts, var_eq_matching, var_sccs,
474474
push!(subs, sym obs[eqidx].rhs)
475475
end
476476
pre = get_postprocess_fbody(sys)
477-
cpre = get_preprocess_constants([obs[1:maxidx]; isscalar ? ts[1] : MakeArray(ts, output_type) ])
477+
cpre = get_preprocess_constants([obs[1:maxidx];
478+
isscalar ? ts[1] : MakeArray(ts, output_type)])
478479
pre2 = x -> pre(cpre(x))
479480
ex = Code.toexpr(Func([DestructuredArgs(solver_states, inbounds = !checkbounds)
480481
DestructuredArgs(parameters(sys), inbounds = !checkbounds)
481482
independent_variables(sys)],
482483
[],
483484
pre2(Let([collect(Iterators.flatten(solves))
484-
assignments[is_not_prepended_assignment]
485-
map(eq -> eq.lhs eq.rhs, obs[1:maxidx])
486-
subs],
487-
isscalar ? ts[1] : MakeArray(ts, output_type),
488-
false))), sol_states)
485+
assignments[is_not_prepended_assignment]
486+
map(eq -> eq.lhs eq.rhs, obs[1:maxidx])
487+
subs],
488+
isscalar ? ts[1] : MakeArray(ts, output_type),
489+
false))), sol_states)
489490

490491
expression ? ex : @RuntimeGeneratedFunction(ex)
491492
end

src/systems/abstractsystem.jl

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -961,7 +961,8 @@ The optional argument `io` may take a tuple `(inputs, outputs)`.
961961
This will convert all `inputs` to parameters and allow them to be unconnected, i.e.,
962962
simplification will allow models where `n_states = n_equations - n_inputs`.
963963
"""
964-
function structural_simplify(sys::AbstractSystem, io = nothing; simplify = false, simplify_constants = true, kwargs...)
964+
function structural_simplify(sys::AbstractSystem, io = nothing; simplify = false,
965+
simplify_constants = true, kwargs...)
965966
sys = expand_connections(sys)
966967
if simplify_constants
967968
sys = eliminate_constants(sys)
@@ -983,8 +984,6 @@ function structural_simplify(sys::AbstractSystem, io = nothing; simplify = false
983984
return has_io ? (sys, input_idxs) : sys
984985
end
985986

986-
987-
""" Replace constants in dynamical equations with their literal values."""
988987
function eliminate_constants(sys::AbstractSystem)
989988
if has_eqs(sys)
990989
eqs = get_eqs(sys)

src/utils.jl

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -504,6 +504,7 @@ function collect_var!(states, parameters, var, iv)
504504
return nothing
505505
end
506506

507+
""" Find all the symbolic constants of some equations or terms and return them as a vector. """
507508
function collect_constants(x)
508509
constants = Symbolics.Sym[]
509510
collect_constants!(constants, x)
@@ -546,6 +547,7 @@ function eliminate_constants(eqs::AbstractArray{<:Union{Equation, Symbolic}}, cs
546547
return substitute(eqs, cmap)
547548
end
548549

550+
""" Create a function preface containing assignments of default values to constants. """
549551
function get_preprocess_constants(eqs)
550552
cs = collect_constants(eqs)
551553
pre = ex -> Let(Assignment[Assignment(x, getdefault(x)) for x in cs],

test/components.jl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -269,7 +269,7 @@ function FixedResistor(; name, R = 1.0)
269269
]
270270
extend(ODESystem(eqs, t, [], []; name = name), oneport)
271271
end
272-
capacitor = Capacitor(;name = :c1)
272+
capacitor = Capacitor(; name = :c1)
273273
resistor = FixedResistor(; name = :r1)
274274
ground = Ground(; name = :ground)
275275
rc_eqs = [connect(capacitor.n, resistor.p)
@@ -278,7 +278,7 @@ rc_eqs = [connect(capacitor.n, resistor.p)
278278

279279
@named _rc_model = ODESystem(rc_eqs, t)
280280
@named rc_model = compose(_rc_model,
281-
[resistor, capacitor, ground])
281+
[resistor, capacitor, ground])
282282
sys = structural_simplify(rc_model)
283283
prob = ODAEProblem(sys, u0, (0, 10.0))
284284
sol = solve(prob, Tsit5())

test/funcaffect.jl

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
using ModelingToolkit, Test, OrdinaryDiffEq
22

33
@parameters t
4-
@constants h = 1 zr = 0
4+
@constants h=1 zr=0
55
@variables u(t)
66
D = Differential(t)
77

@@ -269,7 +269,9 @@ function bb_affect!(integ, u, p, ctx)
269269
end
270270

271271
@named bb_model = ODESystem(bb_eqs, t, sts, par,
272-
continuous_events = [[y ~ zr] => (bb_affect!, [v], [], nothing)])
272+
continuous_events = [
273+
[y ~ zr] => (bb_affect!, [v], [], nothing),
274+
])
273275

274276
bb_sys = structural_simplify(bb_model)
275277
@test ModelingToolkit.affects(ModelingToolkit.continuous_events(bb_sys)) isa

0 commit comments

Comments
 (0)