Skip to content

Allow passing kwargs to connect #1094

Closed
@ValentinKaisermayer

Description

@ValentinKaisermayer

function connect(syss...)

Since the connect method is essentially user defined it would be nice to allow for additional keyword arguments.

function connect(syss...; kwargs...)
    connect(promote_connect_type(map(get_connection_type, syss)...), syss...; kwargs...)
end

The reason is that if I compose a new component e.g. a RealCapacitor out of already existing components (Resistor, Capacitor and Pin) I need the connection to respect that the flow (i.e. current) from the pin of the RealCapacitor and the flow from internal components have different sign conventions. This can be also found in the Modelica specification here and here.

The way I solved this is to use a simple flag to indicate if the connector is inside or outside.

function connect(::Type{Pin}, connectors...; is_inside=fill(true, length(connectors))) # Which connector are inside connectors?
    eqs = [ 
        0 ~ sum([(i_ ? 1 : -1) * c.i for (c, i_) in zip(connectors, is_inside)]) # Flow balance
    ]
    for c in connectors[2:end] 
        push!(eqs, connectors[1].v ~ c.v) # Effort equality
    end
    return eqs
end

and in the definition of the RealCapacitor this can be used as follows:

function RealCapacitor(; name, C=1.0, R_esr=0.1, v0=0.0)
    C_val, R_esr_val = C, R_esr

    @named p = Pin()
    @named n = Pin()
    @named R_esr = Resistor(; R=R_esr_val)
    @named C = Capacitor(; C=C_val, v0=v0)
    connections = [
        connect(p, R_esr.p; is_inside=[false, true])
        connect(R_esr.n, C.p; is_inside=[true, true])
        connect(C.n, n; is_inside=[false, true])
    ]
    return ODESystem(connections, t, [], []; systems=[p, n, R_esr, C], name)
end

It is not really nice and a user is likely to forget about this but the results will be wrong otherwise.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions