Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

pass vectors along wires for instantaneous machines #112

Merged
merged 6 commits into from
Aug 17, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
add test for InstantaneousDelayMachine with vectors on wires
  • Loading branch information
slwu89 committed Aug 4, 2022
commit 52009ef10882954fe22ba9dba21609651e405ab2
17 changes: 8 additions & 9 deletions src/dwd_dynam.jl
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,11 @@ end
DirectedVectorInterface{T,N}(ninputs::Int, noutputs::Int) where {T,N} =
DirectedVectorInterface{T,N}(1:ninputs, 1:noutputs)

# Instantaneous interfaces
abstract type AbstractInstantaneousDirectedInterface{T} <: AbstractDirectedInterface{T} end

# InstantaneousDirectedInterface
struct InstantaneousDirectedInterface{T} <: AbstractDirectedInterface{T}
struct InstantaneousDirectedInterface{T} <: AbstractInstantaneousDirectedInterface{T}
input_ports::Vector
output_ports::Vector
dependency::Span # P_in <- R -> P_out
Expand All @@ -65,13 +68,8 @@ InstantaneousDirectedInterface{T}(input_ports::AbstractVector, output_ports::Abs
end...)
)

dependency(interface::InstantaneousDirectedInterface) = interface.dependency
dependency_pairs(interface::InstantaneousDirectedInterface) = map(apex(dependency(interface))) do i
legs(dependency(interface))[2](i) => legs(dependency(interface))[1](i)
end |> sort

# InstantaneousDirectedVectorInterface
struct InstantaneousDirectedVectorInterface{T,N} <: AbstractDirectedInterface{T}
struct InstantaneousDirectedVectorInterface{T,N} <: AbstractInstantaneousDirectedInterface{T}
input_ports::Vector
output_ports::Vector
dependency::Span # P_in <- R -> P_out
Expand All @@ -96,8 +94,9 @@ InstantaneousDirectedVectorInterface{T,N}(input_ports::AbstractVector, output_po
end...)
)

dependency(interface::InstantaneousDirectedVectorInterface) = interface.dependency
dependency_pairs(interface::InstantaneousDirectedVectorInterface) = map(apex(dependency(interface))) do i
# get dependency
dependency(interface::AbstractInstantaneousDirectedInterface) = interface.dependency
dependency_pairs(interface::AbstractInstantaneousDirectedInterface) = map(apex(dependency(interface))) do i
legs(dependency(interface))[2](i) => legs(dependency(interface))[1](i)
end |> sort

Expand Down
35 changes: 32 additions & 3 deletions test/dwd_dynam.jl
Original file line number Diff line number Diff line change
Expand Up @@ -350,7 +350,36 @@ end
@test eval_dynamics(m, vcat(u1, u2, u3), [x1, x2], nothing, 0) == vcat(x1, u2 + x1 + 2*u2 + u3, x2)

end
end



@testset "VectorInterface for InstantaneousDelayMachine" begin
m = InstantaneousDelayMachine{Float64,3}(
1, 3, 1, # ninputs, nstates, noutputs
(u, x, h, p, t) -> -h(p, t-1), # dynamics
(u, x, h, p, t) -> u+x, # readout
[1 => 1] # output -> input dependency
)

x1 = Float64.([1,2,3])
u1 = Float64.([4,5,6])
hist(p,t) = u1

@test readout(m, u1, x1, hist, nothing, 0) == u1+x1
@test eval_dynamics(m, u1, [x1], hist, nothing, 0) == -u1

prob = DDEProblem(m, u1, [x1], hist, (0.0, 3.0), nothing)
sol = solve(prob,MethodOfSteps(Tsit5()),abstol=1e-12,reltol=1e-12)

# DiffEq.jl solution
prob1 = DDEProblem((du,u,h,p,t) -> begin
x_lag = -h(p, t-1)
du[1] = x_lag[1]
du[2] = x_lag[2]
du[3] = x_lag[3]
end, u1, hist, (0.0, 3.0), nothing)

sol1 = solve(prob1,MethodOfSteps(Tsit5()),abstol=1e-12,reltol=1e-12)

@test sol.u[end] ≈ sol1.u[end]

end
end