Subsystem

Construction of SubSystems

A SubSystem consists of connected components. Thus, to construct a SubSystem, we first construct components, connect them and specify the input and output of SubSystem.

Basic Operation of SubSystems

The operation of a SubSystem is very similar to that of a StaticSystem. The only difference is that when a SubSystem is triggered through its trigger pin, it distributes the trigger to the trigger pins of its components. Then, each of the components of the SubSystem takes steps individually.

Let us construct a subsystem consisting of a generator and an adder.

julia> using Causal # hide

julia> gen = ConstantGenerator()
ConstantGenerator(amp:1.0)

julia> adder = Adder((+,+))
ERROR: MethodError: no method matching Adder(::Tuple{typeof(+), typeof(+)})
Closest candidates are:
  Adder(::S, !Matched::IP, !Matched::OP, !Matched::RO, !Matched::var"##4004", !Matched::var"##4005", !Matched::var"##4006", !Matched::Symbol, !Matched::var"##4007") where {S, IP, OP, RO, var"##4004", var"##4005", var"##4006", Symbol, var"##4007"} at /juliateam/.julia/packages/Causal/vOCIT/src/components/systems/staticsystems/staticsystems.jl:118
  Adder(; signs, input, output, readout, trigger, handshake, callbacks, name, id) at util.jl:461

Connect the generator and adder.

julia> connect!(gen.output, adder.input[1])
ERROR: UndefVarError: adder not defined

We are ready to construct a SubSystem.

julia> sub = SubSystem([gen, adder], [adder.input[2]], adder.output)
ERROR: UndefVarError: adder not defined

To trigger the sub, we need to launch it. For that purpose, we construct ports and pins for input-output and signaling.

julia> oport, iport, trg, hnd = Outport(length(sub.input)), Inport(length(sub.output)), Outpin(), Inpin{Bool}()
ERROR: UndefVarError: sub not defined

julia> connect!(oport, sub.input)
ERROR: UndefVarError: sub not defined

julia> connect!(sub.output, iport)
ERROR: UndefVarError: sub not defined

julia> connect!(trg, sub.trigger)
ERROR: UndefVarError: sub not defined

julia> connect!(sub.handshake, hnd)
ERROR: UndefVarError: sub not defined

julia> t = launch(sub)
ERROR: UndefVarError: sub not defined

julia> t2 = @async while true
           all(take!(iport) .=== NaN) && break
           end
Task (failed) @0x00007fee17bf1660
UndefVarError: iport not defined
Stacktrace:
 [1] macro expansion
   @ ./none:2 [inlined]
 [2] (::Main.ex-subsystem_ex.var"#1#2")()
   @ Main.ex-subsystem_ex ./task.jl:406

sub is ready to be triggered,

julia> put!(trg, 1.)
ERROR: UndefVarError: trg not defined

Put some data to the input of sub via oport (since oport is connected to sub.input)

julia> put!(oport, [1.])
ERROR: UndefVarError: oport not defined

The step needs to be approved.

julia> take!(hnd)
ERROR: UndefVarError: hnd not defined

Now print the data written to the outputs of the components of sub.

julia> sub.components[1].output[1].links[1].buffer[1]
ERROR: UndefVarError: sub not defined

julia> sub.components[2].output[1].links[1].buffer[1]
ERROR: UndefVarError: sub not defined

Note that when sub is triggered, sub transfer the trigger to all its internal components.