Skip to content

SavedValues DifferentialEquations.jl #108

@markilg

Description

@markilg

When using ComponentArrays within parameters for differential equations, saving seems to only preserve the last value of the simulation.

Simple Proportional Control Snippet:

      function motor!(D, u, p, t)
            @unpack J, b, Ke, Kt, R, L, V = p
            @unpack Dθ, θ, i = u
 
            D.Dθ = -b/J*Dθ + Kt/J*i
            D.θ = Dθ
            D.i = -R/L*i - Ke/L*Dθ + 1/L*V
            return nothing
      end

      motor_p = ComponentArray(J=0.01,b=0.1,Ke=0.01,Kt=0.01,R=1.0,L=0.5,V=0.0)

      struct ProportionalController{T}
            Gain::T
      end

      # Controller Feedback - Compare the Error between (1.0 - Step Response and Value)
      function (c::ProportionalController)(integrator::OrdinaryDiffEq.ODEIntegrator)   
            error = 1.0 - integrator.u.Dθ   
            integrator.p.V = c.Gain*error      
      end

      model = ODEProblem(motor!,ComponentArray(Dθ=0.0,θ=0.0,i=0.0),(0.0,1.0),motor_p)

      saved_values = SavedValues(Float64,ComponentArray)
      cb = SavingCallback((u,t,integrator)->(integrator.p), saved_values)

      controller = ProportionalController(10.0)
      controller_cb = PeriodicCallback(controller,0.1; initial_affect = false);

      cbs = CallbackSet(cb,controller_cb)
      sol = solve(model,Tsit5(), callback = cbs)

Here saved_values only has values of V that are constant for the simulation time window.

If you change:

      saved_values = SavedValues(Float64,Float64) 
      cb = SavingCallback((u,t,integrator)->(integrator.p.V), saved_values)

*** EDIT : Disregard this issue. Using a copy operation preserves the values. - Oversight in implementation.

    cb = SavingCallback((u,t,integrator)->(copy(integrator.p)), saved_values)

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