# Ideal Diode and Ideal Switch instability problem

**URL:** <https://discourse.julialang.org/t/ideal-diode-and-ideal-switch-instability-problem/107769>\
**Category:** Modelling & Simulations\
**Tags:** question, modelingtoolkit\
**Created:** [December 18, 2023, 1:33pm UTC](https://discourse.julialang.org/t/ideal-diode-and-ideal-switch-instability-problem/107769 "2023-12-18T13:33:58Z")\
**Posts on this page:** 1\
**Showing post:** 9

<div class="post-metadata">

**Author:** ![langestefan](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/langestefan/32/207923_2.png) [@langestefan](https://discourse.julialang.org/u/langestefan)\
**Post date:** [November 18, 2024, 4:43pm UTC](https://discourse.julialang.org/t/ideal-diode-and-ideal-switch-instability-problem/107769/9 "2024-11-18T16:43:26Z")

</div>

I implemented the diode.

There isn’t a place to put common constants yet so I just put them inside the component. If you want I can also make a separate PR for that?

I also implemented the thermal diode which has an additional thermalport and models the temperature dependent behaviour, I’ll make a separate PR for that once I have added tests.

```julia
"""
        Diode(; name, Is = 1e-12, n = 1)

Ideal diode based on the Shockley diode equation.

# States

    - See [OnePort](@ref)

# Connectors
    
    - `p` Positive pin
    - `n` Negative pin

# Parameters
     
    - `Is`: [`A`] Saturation current
    - `n`: Ideality factor
"""
@mtkmodel Diode begin
    begin
        k = 1.380649e-23 # Boltzmann constant (J/K)
        q = 1.602176634e-19 # Elementary charge (C)
    end

    @extend v, i = oneport = OnePort(; v = 0.0)
    @parameters begin
        Is = 1e-6, [description = "Saturation current (A)"]
        n = 1, [description = "Ideality factor"]
        T = 300.15, [description = "Ambient temperature"]
    end
    @equations begin
        i ~ Is * (exp(v * q / (n * k * T)) - 1)
    end
end

```

Made a small demo with an AC source:

 ![afbeelding](https://global.discourse-cdn.com/julialang/original/3X/6/f/6f4de6df1a63cc8298407f1473d271cca432207a.png)

---

_[View the full topic](https://discourse.julialang.org/t/ideal-diode-and-ideal-switch-instability-problem/107769)._
