Skip to content

Describe interactions with connectors #54

Description

@hersle

It would be convenient to use ModelingToolkit connectors to describe interactions between components (species and gravitational theories), such as total energy density and pressure, but also momentum exchange (e.g. between baryons and photons) for more easily composing arbitrary interactions in the perturbations. Think of the entire Einstein-Boltzmann system as an "electrical circuit of interacting components".

For example:

using ModelingToolkit
@independent_variables t
D = Differential(t)

function Metric(; kwargs...)
    vars = @variables a(t)
    vars = GlobalScope.(vars)
    eqs = Equation[]
    System(eqs, t, vars, []; kwargs...)
end

@connector function Interactor(; kwargs...)
    vars = @variables ρ(t) [connect=Flow] P(t) [connect=Flow]
    eqs = Equation[]
    return System(eqs, t, vars, []; kwargs...)
end

function Species(g, w; kwargs...)
    _w = w
    @named X = Interactor()
    vars = @variables w(t) Ω(t) ρ(t) P(t)
    pars = @parameters Ω₀
    eqs = Equation[
        w ~ _w
        Ω ~ Ω₀ * g.a^(-3(1+_w))
        ρ ~ 3/(8*Num(π)) * Ω
        P ~ w * ρ
        X.ρ ~ ρ
        X.P ~ P
    ]
    System(eqs, t, vars, pars; systems = [X], kwargs...)
end

function Gravity(g; kwargs...)
    @named X = Interactor()
    vars = @variables ρ(t) P(t)
    @unpack a = g
    eqs = Equation[
        ρ ~ -X.ρ
        P ~ -X.P
        D(a) ~ (8*Num(π)/3*ρ) * a^2
    ]
    System(eqs, t, vars, []; systems = [X], kwargs...)
end

@named g = Metric()
@named G = Gravity(g)
@named r = Species(g, 1//3)
@named m = Species(g, 0)
@named Λ = Species(g, -1)

eqs = [
    connect(G.X, r.X, m.X, Λ.X)
]
@named M = System(eqs, t; systems = [g, G, r, m, Λ])
Ms = mtkcompile(M)
  • Flow connections sum to zero.

See the components in ModelingToolkitStandardLibrary for examples and inspiration.

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or request

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions