Skip to content

Constantify a parameter - New feature request  #1989

@H-Sax

Description

@H-Sax

Following a discussion on slack I am putting this on here as a feature request.

From the MTK docs of the Acausal modelling of the RC circuit.

using ModelingToolkit, Plots, DifferentialEquations
@variables t
@connector function Pin(;name)
    sts = @variables v(t)=1.0 i(t)=1.0 [connect = Flow]
    ODESystem(Equation[], t, sts, []; name=name)
end
function Ground(;name)
    @named g = Pin()
    eqs = [g.v ~ 0]
    compose(ODESystem(eqs, t, [], []; name=name), g)
end
function OnePort(;name)
    @named p = Pin()
    @named n = Pin()
    sts = @variables v(t)=1.0 i(t)=1.0
    eqs = [
           v ~ p.v - n.v
           0 ~ p.i + n.i
           i ~ p.i
          ]
    compose(ODESystem(eqs, t, sts, []; name=name), p, n)
end
function Resistor(;name, R = 1.0)
    @named oneport = OnePort()
    @unpack v, i = oneport
    ps = @parameters R=R
    eqs = [
           v ~ i * R
          ]
    extend(ODESystem(eqs, t, [], ps; name=name), oneport)
end
function Capacitor(;name, C = 1.0)
    @named oneport = OnePort()
    @unpack v, i = oneport
    ps = @parameters C=C
    D = Differential(t)
    eqs = [
           D(v) ~ i / C
          ]
    extend(ODESystem(eqs, t, [], ps; name=name), oneport)
end
function ConstantVoltage(;name, V = 1.0)
    @named oneport = OnePort()
    @unpack v = oneport
    ps = @parameters V=V
    eqs = [
           V ~ v
          ]
    extend(ODESystem(eqs, t, [], ps; name=name), oneport)
end
R = 1.0
C = 1.0
V = 1.0
@named resistor = Resistor(R=R)
@named capacitor = Capacitor(C=C)
@named source = ConstantVoltage(V=V)
@named ground = Ground()
rc_eqs = [
          connect(source.p, resistor.p)
          connect(resistor.n, capacitor.p)
          connect(capacitor.n, source.n)
          connect(capacitor.n, ground.g)
         ]
@named _rc_model = ODESystem(rc_eqs, t)
@named rc_model = compose(_rc_model,
                          [resistor, capacitor, source, ground])
sys = structural_simplify(rc_model)
u0 = [
      capacitor.v => 0.0
     ]
prob = ODAEProblem(sys, u0, (0, 10.0))
sol = solve(prob, Tsit5())
plot(sol)

Given I want to then remake the problem and perform optimisation on only C and V in system. It would be a nice feature if we could hold R as a global constant and remake with only 2 parameters doing something like

function reparameterize(prob, parameter_map, u0)
    resistor.R = 2.0
   remake(prob; p=parameter_map, u0=u0)
end
   
p = [ 
    capacitor.C => 2.0
    source.V => 2.0
]
new_prob = reparameterize(prob, p, prob.u0)

What this means is that the dimensionalIty of the parameter space has decreased in size from 3 to 2 as R would now be considered a global constant, given this feature could be worked in, Performing reparameterize would allow us to reduced the parameter space size making it easier for inverse problem solves. optimisation etc.

By thinking stems from a much larger case where I have a model with 80 parameters and would only like to optimise 5, this feature would work nicely for this.

In the DifferentialEquations.jl framework one would use a closure to keep this parameter constant as seen in this example on discourse here https://discourse.julialang.org/t/ode-parameter-estimation-on-a-subset-of-the-parameters/28013

Hope that is clear :)

Thanks,
Harry

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