Skip to content

Commit

Permalink
Use ports instead of pins for Element definition
Browse files Browse the repository at this point in the history
  • Loading branch information
martinholters committed Mar 4, 2019
1 parent 46aba98 commit 2f29d01
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 29 deletions.
29 changes: 15 additions & 14 deletions src/ACME.jl
Expand Up @@ -52,17 +52,6 @@ mutable struct Element
end
end

function make_pin_dict(syms)
dict = Dict{Symbol,Vector{Tuple{Int, Int}}}()
for i in 1:length(syms)
branch = (i+1) ÷ 2
polarity = 2(i % 2) - 1
push!(get!(dict, Symbol(syms[i]), []), (branch, polarity))
end
dict
end
make_pin_dict(dict::Dict) = dict

mat_dims =
Dict( :mv => (:nl,:nb), :mi => (:nl,:nb), :mx => (:nl,:nx),
:mxd => (:nl,:nx), :mq => (:nl,:nq), :mu => (:nl,:nu),
Expand All @@ -75,8 +64,20 @@ mutable struct Element
if haskey(mat_dims, key)
val = convert(SparseMatrixCSC{Real,Int}, hcat(val)) # turn val into a sparse matrix whatever it is
update_sizes(val, mat_dims[key])
elseif key == :pins
val = make_pin_dict(val)
else
if key === :pins && !(val isa Dict)
val = ports_from_old_pins(val)
key = :ports
end
if key === :ports
pins = Dict{Symbol,Vector{Tuple{Int, Int}}}()
for branch in 1:length(val)
push!(get!(pins, Symbol(val[branch][1]), []), (branch, 1))
push!(get!(pins, Symbol(val[branch][2]), []), (branch, -1))
end
val = pins
key = :pins
end
end
setfield!(elem, key, val)
end
Expand All @@ -92,7 +93,7 @@ mutable struct Element
elem.nonlinear_eq = wrap_nleq_expr(nn, get(sizes, :nq, 0), elem.nonlinear_eq)
end
if !isdefined(elem, :pins)
elem.pins = make_pin_dict(1:2nb(elem))
elem.pins = Dict(Symbol(i) => [((i+1) ÷ 2, 2(i % 2) - 1)] for i in 1:2nb(elem))
end
elem
end
Expand Down
2 changes: 1 addition & 1 deletion src/circuit.jl
Expand Up @@ -498,7 +498,7 @@ ports.
return Element(mv = M̃ᵥ, mi = -M̃ᵢ, mx = M̃ₓ, mxd = M̃ₓ´, mq = M̃q,
mu = M̃u, u0 = ũ0,
nonlinear_eq=nonlinear_eq_func(circ),
pins=vcat(collect.(ports)...))
ports=ports)
end

function ports_from_pinmap(pinmap)
Expand Down
6 changes: 6 additions & 0 deletions src/deprecated.jl
Expand Up @@ -35,5 +35,11 @@ function wrap_nleq_expr(nn, nq, expr)
end)
end

function ports_from_old_pins(pins)
ports = [pins[2i-1] => pins[2i] for i in 1:length(pins)÷2]
Base.depwarn("`Element(..., pins=$(repr(pins)))` is depreated, use `Element(..., ports=$(repr(ports, context=:typeinfo=>typeof(ports))))` instead", :Element)
return ports
end

Base.@deprecate(composite_element(circ::Circuit, pins::Vector{<:Pair}),
composite_element(circ, pinmap=Dict(pins...)))
28 changes: 14 additions & 14 deletions src/elements.jl
Expand Up @@ -16,7 +16,7 @@ Pins: `1`, `2`
resistor(r) = Element(mv=-1, mi=r)

potentiometer(r, pos) = Element(mv=Matrix{Int}(-I, 2, 2), mi=[r*pos 0; 0 r*(1-pos)],
pins=[1, 2, 2, 3])
ports=[1 => 2, 2 => 3])
potentiometer(r) =
Element(mv=[Matrix{Int}(I, 2, 2); zeros(3, 2)],
mi=[zeros(2, 2); Matrix{Int}(I, 2, 2); zeros(1, 2)],
Expand All @@ -28,7 +28,7 @@ potentiometer(r) =
J = @SMatrix [1 0 -r*pos 0 -r*i1; 0 1 0 -r*(1-pos) -r*i2]
return (res, J)
end),
pins=[1, 2, 2, 3])
ports=[1 => 2, 2 => 3])

"""
capacitor(c)
Expand Down Expand Up @@ -64,7 +64,7 @@ transformer(l1, l2; coupling_coefficient=1,
Element(mv=[1 0; 0 1; 0 0; 0 0],
mi=[0 0; 0 0; l1 mutual_coupling; mutual_coupling l2],
mx=[0 0; 0 0; -1 0; 0 -1], mxd=[-1 0; 0 -1; 0 0; 0 0],
pins=[:primary1; :primary2; :secondary1; :secondary2])
ports=[:primary1 => :primary2, :secondary1 => :secondary2])

"""
transformer(Val{:JA}; D, A, ns, a, α, c, k, Ms)
Expand Down Expand Up @@ -173,8 +173,8 @@ internal series resistance `rs` (in Ohm) can be given which defaults to zero.
Pins: `+` and `-` with `v` being measured from `+` to `-`
"""
function voltagesource end
voltagesource(v; rs=0) = Element(mv=1, mi=-rs, u0=v, pins=[:+; :-])
voltagesource(; rs=0) = Element(mv=1, mi=-rs, mu=1, pins=[:+; :-])
voltagesource(v; rs=0) = Element(mv=1, mi=-rs, u0=v, ports=[:+ => :-])
voltagesource(; rs=0) = Element(mv=1, mi=-rs, mu=1, ports=[:+ => :-])

"""
currentsource(; gp=0)
Expand All @@ -188,8 +188,8 @@ zero.
Pins: `+` and `-` where `i` measures the current leaving source at the `+` pin
"""
function currentsource end
currentsource(i; gp=0) = Element(mv=gp, mi=-1, u0=i, pins=[:+; :-])
currentsource(; gp=0) = Element(mv=gp, mi=-1, mu=1, pins=[:+; :-])
currentsource(i; gp=0) = Element(mv=gp, mi=-1, u0=i, ports=[:+ => :-])
currentsource(; gp=0) = Element(mv=gp, mi=-1, mu=1, ports=[:+ => :-])

"""
voltageprobe()
Expand All @@ -200,7 +200,7 @@ defaults to zero.
Pins: `+` and `-` with the output voltage being measured from `+` to `-`
"""
voltageprobe(;gp=0) = Element(mv=-gp, mi=1, pv=1, pins=[:+; :-])
voltageprobe(;gp=0) = Element(mv=-gp, mi=1, pv=1, ports=[:+ => :-])

"""
currentprobe()
Expand All @@ -212,7 +212,7 @@ defaults to zero.
Pins: `+` and `-` with the output current being the current entering the probe
at `+`
"""
currentprobe(;rs=0) = Element(mv=1, mi=-rs, pi=1, pins=[:+; :-])
currentprobe(;rs=0) = Element(mv=1, mi=-rs, pi=1, ports=[:+ => :-])

@doc doc"""
diode(;is=1e-12, η = 1)
Expand All @@ -224,7 +224,7 @@ coefficient `η` is unitless.
Pins: `+` (anode) and `-` (cathode)
""" diode(;is::Real=1e-12, η::Real = 1) =
Element(mv=[1;0], mi=[0;1], mq=[-1 0; 0 -1], pins=[:+; :-], nonlinear_eq =
Element(mv=[1;0], mi=[0;1], mq=[-1 0; 0 -1], ports=[:+ => :-], nonlinear_eq =
@inline function(q)
v, i = q
ex = exp(v*(1 / (25e-3 * η)))
Expand Down Expand Up @@ -390,7 +390,7 @@ Pins: `base`, `emitter`, `collector`
return Element(mv=[1 0; 0 1; 0 0; 0 0],
mi = [-(re+rb) -rb; -rb -(rc+rb); 1 0; 0 1],
mq = Matrix{Int}(-polarity*I, 4, 4), nonlinear_eq = nonlinear_eq,
pins = [:base; :emitter; :base; :collector])
ports = [:base => :emitter, :base => :collector])
end

@doc doc"""
Expand Down Expand Up @@ -422,7 +422,7 @@ Pins: `gate`, `source`, `drain`
mi=[0 0; 0 0; 0 -1; 1 0],
mq=polarity*[1 0 0; 0 1 0; 0 0 1; 0 0 0],
u0=polarity*[-vt; 0; 0; 0],
pins=[:gate, :source, :drain, :source],
ports=[:gate => :source, :drain => :source],
nonlinear_eq = @inline function (q)
vg, vds, id=q # vg = vgs-vt
if vg <= 0
Expand Down Expand Up @@ -453,7 +453,7 @@ output pin.
Pins: `in+` and `in-` for input, `out+` and `out-` for output
"""
opamp() = Element(mv=[0 0; 1 0], mi=[1 0; 0 0],
pins=["in+", "in-", "out+", "out-"])
ports=["in+" => "in-", "out+" => "out-"])

@doc doc"""
opamp(Val{:macak}, gain, vomin, vomax)
Expand Down Expand Up @@ -485,5 +485,5 @@ Pins: `in+` and `in-` for input, `out+` and `out-` for output
return Element(mv=[0 0; 1 0; 0 1], mi=[1 0; 0 0; 0 0], mq=[0 0; -1 0; 0 -1],
u0=[0; 0; offset],
nonlinear_eq = nonlinear_eq,
pins=["in+", "in-", "out+", "out-"])
ports=["in+" => "in-", "out+" => "out-"])
end

0 comments on commit 2f29d01

Please sign in to comment.