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 Jusdl # hide

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

julia> adder = Adder((+,+))
Adder(signs:(+, +), input:Inport(numpins:2, eltype:Inpin{Float64}), output:Outport(numpins:1, eltype:Outpin{Float64}))

Connect the generator and adder.

julia> connect(gen.output, adder.input[1])
1-element Array{Link{Float64},1}:
 Link(state:open, eltype:Float64, isreadable:false, iswritable:false)

We are ready to construct a SubSystem.

julia> sub = SubSystem([gen, adder], [adder.input[2]], adder.output)
SubSystem(input:Inport(numpins:1, eltype:Inpin{Float64}), output:Outport(numpins:1, eltype:Outpin{Float64}), components:AbstractComponent[ConstantGenerator(amp:1.0), Adder(signs:(+, +), input:Inport(numpins:2, eltype:Inpin{Float64}), output:Outport(numpins:1, eltype:Outpin{Float64}))])

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}()
(Outport(numpins:1, eltype:Outpin{Float64}), Inport(numpins:1, eltype:Inpin{Float64}), Outpin(eltype:Float64, isbound:false), Inpin(eltype:Bool, isbound:false))

julia> connect(oport, sub.input)
1-element Array{Link{Float64},1}:
 Link(state:open, eltype:Float64, isreadable:false, iswritable:false)

julia> connect(sub.output, iport)
1-element Array{Link{Float64},1}:
 Link(state:open, eltype:Float64, isreadable:false, iswritable:false)

julia> connect(trg, sub.trigger)
Link(state:open, eltype:Float64, isreadable:false, iswritable:false)

julia> connect(sub.handshake, hnd)
Link(state:open, eltype:Bool, isreadable:false, iswritable:false)

julia> t = launch(sub)
3-element Array{Task,1}:
 Task (runnable) @0x00007fa6731c8c40
 Task (runnable) @0x00007fa6732104f0
 Task (runnable) @0x00007fa6731c84f0

julia> t2 = @async while true
           all(take!(iport) .=== NaN) && break
           end
Task (runnable) @0x00007fa670d72980

sub is ready to be triggered,

julia> put!(trg, 1.)

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

julia> put!(oport, [1.])
1-element Array{Float64,1}:
 1.0

The step needs to be approved.

julia> take!(hnd)
true

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

julia> sub.components[1].output[1].links[1].buffer[1]
1.0

julia> sub.components[2].output[1].links[1].buffer[1]
2.0

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

Jusdl.SubSystemType
SubSystem(components, input, output; callbacks=nothing, name=Symbol())

Constructs a SubSystem consisting of components. input and output determines the input and output of SubSystem. input and output may be nothing, a vector of pins or a port.

Example

julia> adder = Adder((+,-));

julia> gain = Gain();

julia> gen = ConstantGenerator();

julia> connect(gen.output, adder.input);

julia> connect(adder.output, gain.input);

julia> ss = SubSystem([gen, adder, gain], adder.input[1], gain.output)
SubSystem(input:Inport(numpins:1, eltype:Inpin{Float64}), output:Outport(numpins:1, eltype:Outpin{Float64}), components:AbstractComponent[ConstantGenerator(amp:1.0), Adder(signs:(+, -), input:Inport(numpins:2, eltype:Inpin{Float64}), output:Outport(numpins:1, eltype:Outpin{Float64})), Gain(gain:1.0, input:Inport(numpins:1, eltype:Inpin{Float64}), output:Outport(numpins:1, eltype:Outpin{Float64}))])
source