@@ -395,16 +395,43 @@ function generate_connection_set(
395
395
connectionsets = ConnectionSet[]
396
396
domain_csets = ConnectionSet[]
397
397
sys = generate_connection_set! (
398
- connectionsets, domain_csets, sys, find, replace, scalarize)
398
+ connectionsets, domain_csets, sys, find, replace, scalarize, nothing ,
399
+ # include systems to be ignored
400
+ ignored_connections (sys)[1 ])
399
401
csets = merge (connectionsets)
400
402
domain_csets = merge ([csets; domain_csets], true )
401
403
402
404
sys, (csets, domain_csets)
403
405
end
404
406
407
+ """
408
+ $(TYPEDSIGNATURES)
409
+
410
+ Generate connection sets from `connect` equations.
411
+
412
+ # Arguments
413
+
414
+ - `connectionsets` is the list of connection sets to be populated by recursively
415
+ descending `sys`.
416
+ - `domain_csets` is the list of connection sets for domain connections.
417
+ - `sys` is the system whose equations are to be searched.
418
+ - `namespace` is a system representing the namespace in which `sys` exists, or `nothing`
419
+ for no namespace (if `sys` is top-level).
420
+ - `ignored_systems` is a list of systems (in the format returned by `as_hierarchy`) to
421
+ be ignored when generating connections. This is typically because the connections
422
+ they are used in were removed by analysis point transformations.
423
+ """
405
424
function generate_connection_set! (connectionsets, domain_csets,
406
- sys:: AbstractSystem , find, replace, scalarize, namespace = nothing )
425
+ sys:: AbstractSystem , find, replace, scalarize, namespace = nothing , ignored_systems = [] )
407
426
subsys = get_systems (sys)
427
+ # turn hierarchies into namespaced systems
428
+ namespaced_ignored = from_hierarchy .(ignored_systems)
429
+ # filter the subsystems of `sys` to exclude ignored ones
430
+ filtered_subsys = filter (subsys) do ss
431
+ all (namespaced_ignored) do igsys
432
+ nameof (igsys) != nameof (ss)
433
+ end
434
+ end
408
435
409
436
isouter = generate_isouter (sys)
410
437
eqs′ = get_eqs (sys)
@@ -430,9 +457,21 @@ function generate_connection_set!(connectionsets, domain_csets,
430
457
neweq isa AbstractArray ? append! (eqs, neweq) : push! (eqs, neweq)
431
458
else
432
459
if lhs isa Connection && get_systems (lhs) === :domain
433
- connection2set! (domain_csets, namespace, get_systems (rhs), isouter)
460
+ # don't consider systems that should be ignored
461
+ systems_to_connect = filter (get_systems (rhs)) do ss
462
+ all (namespaced_ignored) do igsys
463
+ nameof (igsys) != nameof (ss)
464
+ end
465
+ end
466
+ connection2set! (domain_csets, namespace, systems_to_connect, isouter)
434
467
elseif isconnection (rhs)
435
- push! (cts, get_systems (rhs))
468
+ # ignore required systems
469
+ systems_to_connect = filter (get_systems (rhs)) do ss
470
+ all (namespaced_ignored) do igsys
471
+ nameof (igsys) != nameof (ss)
472
+ end
473
+ end
474
+ push! (cts, systems_to_connect)
436
475
else
437
476
# split connections and equations
438
477
if eq. lhs isa AbstractArray || eq. rhs isa AbstractArray
@@ -446,7 +485,8 @@ function generate_connection_set!(connectionsets, domain_csets,
446
485
447
486
# all connectors are eventually inside connectors.
448
487
T = ConnectionElement
449
- for s in subsys
488
+ # only generate connection sets for systems that are not ignored
489
+ for s in filtered_subsys
450
490
isconnector (s) || continue
451
491
is_domain_connector (s) && continue
452
492
for v in unknowns (s)
@@ -465,12 +505,32 @@ function generate_connection_set!(connectionsets, domain_csets,
465
505
end
466
506
@set! sys. systems = map (
467
507
s -> generate_connection_set! (connectionsets, domain_csets, s,
468
- find, replace, scalarize,
469
- renamespace (namespace, s )),
508
+ find, replace, scalarize, renamespace (namespace, s),
509
+ ignored_systems_for_subsystem (s, ignored_systems )),
470
510
subsys)
471
511
@set! sys. eqs = eqs
472
512
end
473
513
514
+ """
515
+ $(TYPEDSIGNATURES)
516
+
517
+ Given a subsystem `subsys` of a parent system and a list of systems (variables) to be
518
+ ignored by `generate_connection_set!` (`expand_variable_connections`), filter
519
+ `ignored_systems` to only include those present in the subtree of `subsys` and update
520
+ their hierarchy to not include `subsys`.
521
+ """
522
+ function ignored_systems_for_subsystem (
523
+ subsys:: AbstractSystem , ignored_systems:: Vector{<:HierarchyT} )
524
+ result = eltype (ignored_systems)[]
525
+ for igsys in ignored_systems
526
+ if igsys[end ] == nameof (subsys)
527
+ push! (result, copy (igsys))
528
+ pop! (result[end ])
529
+ end
530
+ end
531
+ return result
532
+ end
533
+
474
534
function Base. merge (csets:: AbstractVector{<:ConnectionSet} , allouter = false )
475
535
ele2idx = Dict {ConnectionElement, Int} ()
476
536
idx2ele = ConnectionElement[]
@@ -576,16 +636,25 @@ end
576
636
Recursively descend through the hierarchy of `sys` and expand all connection equations
577
637
of causal variables. Return the modified system.
578
638
"""
579
- function expand_variable_connections (sys:: AbstractSystem )
639
+ function expand_variable_connections (sys:: AbstractSystem ; ignored_variables = nothing )
640
+ if ignored_variables === nothing
641
+ ignored_variables = ignored_connections (sys)[2 ]
642
+ end
643
+ namespaced_ignored = from_hierarchy .(ignored_variables)
580
644
eqs = copy (get_eqs (sys))
581
645
valid_idxs = trues (length (eqs))
582
646
additional_eqs = Equation[]
583
647
584
648
for (i, eq) in enumerate (eqs)
585
649
eq. lhs isa Connection || continue
586
650
connection = eq. rhs
587
- elements = connection. systems
651
+ elements = get_systems ( connection)
588
652
is_causal_variable_connection (connection) || continue
653
+ elements = filter (elements) do el
654
+ all (namespaced_ignored) do var
655
+ getname (var) != getname (el. var)
656
+ end
657
+ end
589
658
590
659
valid_idxs[i] = false
591
660
elements = map (x -> x. var, elements)
@@ -595,7 +664,10 @@ function expand_variable_connections(sys::AbstractSystem)
595
664
end
596
665
end
597
666
eqs = [eqs[valid_idxs]; additional_eqs]
598
- subsystems = map (expand_variable_connections, get_systems (sys))
667
+ subsystems = map (get_systems (sys)) do subsys
668
+ expand_variable_connections (subsys;
669
+ ignored_variables = ignored_systems_for_subsystem (subsys, ignored_variables))
670
+ end
599
671
@set! sys. eqs = eqs
600
672
@set! sys. systems = subsystems
601
673
return sys
0 commit comments