Quantcast
Channel: First steps - JuliaLang
Viewing all articles
Browse latest Browse all 2795

Simulating MOSFET equations with ModelingToolkit

$
0
0

Hello, I’m trying to model a MOSFET with ModelingToolkit using the most basic quadratic model, but having a lot of issues. The equations I came up with so far are as follows:

using ModelingToolkit, OrdinaryDiffEq


function NMOS(Vg, Vd, Vs, Vth, W, L, uCox)
    Vgs = Vg-Vs
    Vds = Vd-Vs
    return ((Vgs > Vth) * (Vds < Vgs-Vth))*uCox*W/L*((Vgs-Vth)*Vds-Vds^2/2) +
           ((Vgs > Vth) * (Vds > Vgs-Vth))*uCox/2*W/L*(Vgs-Vth)^2
end

@parameters t Vth W L KP C R Vcc f
@variables Vg(t) Vc(t) Id(t) Ic(t) Ir(t)
@derivatives D'~t

eqs = [
    Vg ~ 1+sin(2*pi*f*t),
    Id ~ NMOS(Vg, Vc, 0, Vth, W, L, KP),
    Ic ~ C*D(Vc),
    Ir ~ (Vcc-Vc)/R,
    0 ~ Ir - Id - Ic,
]

Basically I tried to generate a sine input to the mosfet gate, which is connected to a resistor to vcc, and a capacitor at the output. The MOSFET model is the most basic quadratic model, where I implemented the different operating regions by multiplying with booleans.

So far, baring any silly mistakes, no problems. But then I try to make a system of equations from this and am not having much luck.

Despite knowing that my MOSFET is nonlinear, I started by just putting it in ODESystem to see what happens. Apparently ERROR: LoadError: type Sym has no field op. Okay, time to move on to Solving Nonlinear Systems with NLsolve · ModelingToolkit.jl

ns = NonlinearSystem(eqs, [Vg, Vc, Id, Ic , Ir], [t, Vth, W, L, KP, C, R, Vcc, f])
nlsys_func = generate_function(ns)[2] # second is the inplace version
fn = eval(nlsys_func)
du = zeros(5); u = ones(5)
params = (0, 0.7, 10e-6, 1e-6, 2.0718e-5, 1e-6, 10e3, 5, 1e3)
fn(du,u,params)
du

Everything seems fine until I call the eval’d function. At that point I get all sorts of type errors that I don’t understand:

ERROR: LoadError: MethodError: objects of type Term{Real} are not callable
ERROR: LoadError: MethodError: Cannot `convert` an object of type Term{Float64} to an object of type Float64

They seem to come from the MOSFET and capacitor line.

1 post - 1 participant

Read full topic


Viewing all articles
Browse latest Browse all 2795