@@ -512,24 +512,8 @@ function make_ReactionSystem_internal(rxs_and_eqs::Vector, iv, us_in, ps_in;
512
512
eqs = Equation[eq for eq in rxs_and_eqs if eq isa Equation]
513
513
514
514
# Loops through all reactions, adding encountered quantities to the unknown and parameter vectors.
515
- # Starts by looping through substrates + products only (so these are added to the vector first).
516
- # Next, the other components of reactions (e.g. rates and stoichiometries) are added.
517
515
for rx in rxs
518
- for reactants in (rx. substrates, rx. products), spec in reactants
519
- MT. isparameter (spec) ? push! (ps, spec) : push! (us, spec)
520
- end
521
- end
522
- for rx in rxs
523
- # Adds all quantities encountered in the reaction's rate.
524
- findvars! (ps, us, rx. rate, ivs, vars)
525
-
526
- # Extracts all quantities encountered within stoichiometries.
527
- for stoichiometry in (rx. substoich, rx. prodstoich), sym in stoichiometry
528
- (sym isa Symbolic) && findvars! (ps, us, sym, ivs, vars)
529
- end
530
-
531
- # Extract all quantities encountered in relevant `Reaction` metadata.
532
- hasnoisescaling (rx) && findvars! (ps, us, getnoisescaling (rx), ivs, vars)
516
+ MT. collect_vars! (us, ps, rx, iv)
533
517
end
534
518
535
519
# Extracts any species, variables, and parameters that occur in (non-reaction) equations.
@@ -543,6 +527,11 @@ function make_ReactionSystem_internal(rxs_and_eqs::Vector, iv, us_in, ps_in;
543
527
fulleqs = rxs
544
528
end
545
529
530
+ # get variables in subsystems with scope at this level
531
+ for ssys in get (kwargs, :systems , [])
532
+ MT. collect_scoped_vars! (us, ps, ssys, iv)
533
+ end
534
+
546
535
# Loops through all events, adding encountered quantities to the unknown and parameter vectors.
547
536
find_event_vars! (ps, us, continuous_events, ivs, vars)
548
537
find_event_vars! (ps, us, discrete_events, ivs, vars)
@@ -1397,6 +1386,42 @@ function MT.flatten(rs::ReactionSystem; name = nameof(rs))
1397
1386
discrete_events = MT. discrete_events (rs))
1398
1387
end
1399
1388
1389
+ """
1390
+ ModelingToolkit.compose(sys::ReactionSystem, systems::AbstractArray; name = nameof(sys))
1391
+
1392
+ Compose the indicated [`ReactionSystem`](@ref) with one or more `AbstractSystem`s.
1393
+
1394
+ Notes:
1395
+ - The `AbstractSystem` being added in must be an `ODESystem`, `NonlinearSystem`,
1396
+ or `ReactionSystem` currently.
1397
+ - Returns a new `ReactionSystem` and does not modify `rs`.
1398
+ - By default, the new `ReactionSystem` will have the same name as `sys`.
1399
+ """
1400
+ function ModelingToolkit. compose (sys:: ReactionSystem , systems:: AbstractArray ; name = nameof (sys))
1401
+ nsys = length (systems)
1402
+ nsys == 0 && return sys
1403
+ @set! sys. name = name
1404
+ @set! sys. systems = [get_systems (sys); systems]
1405
+ newunknowns = OrderedSet {BasicSymbolic{Real}} ()
1406
+ newparams = OrderedSet ()
1407
+ iv = has_iv (sys) ? get_iv (sys) : nothing
1408
+ for ssys in systems
1409
+ MT. collect_scoped_vars! (newunknowns, newparams, ssys, iv)
1410
+ end
1411
+
1412
+ if ! isempty (newunknowns)
1413
+ @set! sys. unknowns = union (get_unknowns (sys), newunknowns)
1414
+ sort! (get_unknowns (sys), by = ! isspecies)
1415
+ @set! sys. species = filter (isspecies, get_unknowns (sys))
1416
+ end
1417
+
1418
+ if ! isempty (newparams)
1419
+ @set! sys. ps = union (get_ps (sys), newparams)
1420
+ end
1421
+
1422
+ return sys
1423
+ end
1424
+
1400
1425
"""
1401
1426
ModelingToolkit.extend(sys::AbstractSystem, rs::ReactionSystem; name::Symbol=nameof(sys))
1402
1427
0 commit comments