# Modelling constant phase elements with ModellingToolkit

**URL:** <https://discourse.julialang.org/t/modelling-constant-phase-elements-with-modellingtoolkit/78754>\
**Category:** New to Julia\
**Created:** [March 30, 2022, 4:19pm UTC](https://discourse.julialang.org/t/modelling-constant-phase-elements-with-modellingtoolkit/78754 "2022-03-30T16:19:07Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![FTACV](https://avatars.discourse-cdn.com/v4/letter/f/74df32/32.png) [@FTACV](https://discourse.julialang.org/u/FTACV)\
**Post date:** [March 30, 2022, 4:19pm UTC](https://discourse.julialang.org/t/modelling-constant-phase-elements-with-modellingtoolkit/78754/1 "2022-03-30T16:19:07Z")

</div>

Hi all,

I’m attempting to very slightly extend the example given `[here](https://mtk.sciml.ai/dev/tutorials/acausal_components/)` to include a constant phase element. The constant phase element simulation in the time domain can be modelled as a current dependent voltage source, with the voltage gain/drop given by the convolution integral

V\_{CPE}(t)=\frac{1}{\Gamma(\alpha)Q}\int\_0^t (t-u)^{\alpha-1}I(u)du.

This can be approximated (with constant dt) using

V\_{CPE}(t\_n)=\frac{\Delta t}{\Gamma(\alpha)Q}\sum\_{k=1}^{n-1}(t\_n-t\_k)^{\alpha-1}I(t\_k)

To model this using the framework in the link above, I need to find some way of tracking the history of both the time and current state variables for each CPE modelled. If I was working in Python, the function I’d pass to the ODE solver would be wrapped in a class that could record the state variables passed to it. However, this does not seem to be compatible with the Julia philosophy, so I was wondering what the preferred solution would be?

---

<div class="post-metadata">

**Author:** ![baggepinnen](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/baggepinnen/32/693_2.png) [@baggepinnen](https://discourse.julialang.org/u/baggepinnen)\
**Post date:** [March 30, 2022, 4:25pm UTC](https://discourse.julialang.org/t/modelling-constant-phase-elements-with-modellingtoolkit/78754/2 "2022-03-30T16:25:33Z")

</div>

There’s [https://github.com/SciML/DelayDiffEq.jl](https://github.com/SciML/DelayDiffEq.jl)  
that handles the state history for you.

---

<div class="post-metadata">

**Author:** ![ChrisRackauckas](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/chrisrackauckas/32/77_2.png) [@ChrisRackauckas](https://discourse.julialang.org/u/ChrisRackauckas)\
**Post date:** [March 30, 2022, 4:27pm UTC](https://discourse.julialang.org/t/modelling-constant-phase-elements-with-modellingtoolkit/78754/3 "2022-03-30T16:27:14Z")

</div>

And ModelingToolkit hasn’t set this up at the symbolic level at this point, but it’s planned to use DelayDiffEq.jl to do so.

---

<div class="post-metadata">

**Author:** ![FTACV](https://avatars.discourse-cdn.com/v4/letter/f/74df32/32.png) [@FTACV](https://discourse.julialang.org/u/FTACV)\
**Post date:** [March 30, 2022, 4:34pm UTC](https://discourse.julialang.org/t/modelling-constant-phase-elements-with-modellingtoolkit/78754/4 "2022-03-30T16:34:20Z")

</div>

Ah ok - does that mean it’s not possible to use a DDE with ModellingToolkit as present?

---

<div class="post-metadata">

**Author:** ![ChrisRackauckas](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/chrisrackauckas/32/77_2.png) [@ChrisRackauckas](https://discourse.julialang.org/u/ChrisRackauckas)\
**Post date:** [March 30, 2022, 4:41pm UTC](https://discourse.julialang.org/t/modelling-constant-phase-elements-with-modellingtoolkit/78754/5 "2022-03-30T16:41:26Z")

</div>

Yes, it’s not possible at present. Right now, directly use DelayDiffEq.jl

---

<div class="post-metadata">

**Author:** ![FTACV](https://avatars.discourse-cdn.com/v4/letter/f/74df32/32.png) [@FTACV](https://discourse.julialang.org/u/FTACV)\
**Post date:** [March 30, 2022, 4:48pm UTC](https://discourse.julialang.org/t/modelling-constant-phase-elements-with-modellingtoolkit/78754/6 "2022-03-30T16:48:34Z")

</div>

Apologies for being dense, but does that mean I won’t be able to use modelling toolkit to construct and reduce a DAE system?

---

<div class="post-metadata">

**Author:** ![ChrisRackauckas](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/chrisrackauckas/32/77_2.png) [@ChrisRackauckas](https://discourse.julialang.org/u/ChrisRackauckas)\
**Post date:** [March 30, 2022, 6:35pm UTC](https://discourse.julialang.org/t/modelling-constant-phase-elements-with-modellingtoolkit/78754/7 "2022-03-30T18:35:06Z")

</div>

Yes, the analysis would need to be adjusted to handle delays anyways.

---

<div class="post-metadata">

**Author:** ![FTACV](https://avatars.discourse-cdn.com/v4/letter/f/74df32/32.png) [@FTACV](https://discourse.julialang.org/u/FTACV)\
**Post date:** [March 31, 2022, 5:19pm UTC](https://discourse.julialang.org/t/modelling-constant-phase-elements-with-modellingtoolkit/78754/8 "2022-03-31T17:19:40Z")

</div>

Ok, after some thought, in the meantime I would like to try and implement this as a current-controlled voltage source. Is it possible to provide an equation to the symbolic toolkit (such as

eqs =[  
v~ v+CCVS\_gain()  
])  
and then provide the function to calculate CCVS\_gain() from the current and time state variable history to ODAEProblem()?

Additionally, apologies if this is a foolish question, but if there are multiple @named elements with the same component (e.g. multiple capacitors with a C parameter), would those multiple instances of the C parameter be treated as the same when calling structural\_simplify()?

---

<div class="post-metadata">

**Author:** ![ChrisRackauckas](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/chrisrackauckas/32/77_2.png) [@ChrisRackauckas](https://discourse.julialang.org/u/ChrisRackauckas)\
**Post date:** [March 31, 2022, 11:14pm UTC](https://discourse.julialang.org/t/modelling-constant-phase-elements-with-modellingtoolkit/78754/9 "2022-03-31T23:14:26Z")

</div>

> [@FTACV](#):
>
> eqs =[  
> v~ v+CCVS\_gain()  
> ])  
> and then provide the function to calculate CCVS\_gain() from the current and time state variable history to ODAEProblem()?

You mean like `v~ v+CCVS_gain(v(t-tau),t)`? That’s exactly what’s missing in the symbolic form right now.

> [@FTACV](#):
>
> Additionally, apologies if this is a foolish question, but if there are multiple @named elements with the same component (e.g. multiple capacitors with a C parameter), would those multiple instances of the C parameter be treated as the same when calling structural\_simplify()?

No, they are namedspaced. See the component-based modeling example: [https://mtk.sciml.ai/dev/tutorials/acausal\_components/](https://mtk.sciml.ai/dev/tutorials/acausal_components/)
