Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions docs/src/connectors/connections.md
Original file line number Diff line number Diff line change
Expand Up @@ -191,7 +191,7 @@ nothing # hide
As can be seen, we get exactly the same result. The only difference here is that we are solving an extra equation, which allows us to plot the body position as well.

```@example connections
prob = ODEProblem(sys, [], (0, 10.0), fully_determined=true)
prob = ODEProblem(sys, [], (0, 10.0), fully_determined = true)
sol_p = solve(prob)

p1 = plot(sol_p, idxs = [body.v])
Expand Down Expand Up @@ -281,7 +281,7 @@ function simplify_and_solve(damping, spring, body, ground; initialization_eqs =

println.(full_equations(sys))

prob = ODEProblem(sys, [], (0, 10.0); initialization_eqs, fully_determined=true)
prob = ODEProblem(sys, [], (0, 10.0); initialization_eqs, fully_determined = true)
sol = solve(prob; abstol = 1e-9, reltol = 1e-9)

return sol
Expand Down
23 changes: 14 additions & 9 deletions docs/src/tutorials/MOSFET_calibration.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
# MOSFET I-V Curves
In this example, first we'll demonstrate the I-V curves of the NMOS transistor model.
# MOSFET I-V Curves

In this example, first we'll demonstrate the I-V curves of the NMOS transistor model.
First of all, we construct a circuit using the NMOS transistor. We'll need to import ModelingToolkit and the Electrical standard library that holds the transistor models.

```@example NMOS
Expand All @@ -12,6 +13,7 @@ using Plots
```

Here we just connect the source pin to ground, the drain pin to a voltage source named `Vcc`, and the gate pin to a voltage source named `Vb`.

```@example NMOS
@mtkmodel SimpleNMOSCircuit begin
@components begin
Expand All @@ -20,8 +22,8 @@ Here we just connect the source pin to ground, the drain pin to a voltage source
Vb = Voltage()
ground = Ground()

Vcc_const = Constant(k=V_cc)
Vb_const = Constant(k=V_b)
Vcc_const = Constant(k = V_cc)
Vb_const = Constant(k = V_b)
end

@parameters begin
Expand All @@ -48,7 +50,8 @@ prob = ODEProblem(sys, Pair[], (0.0, 10.0))
sol = solve(prob)
```

Now to make sure that the transistor model is working like it's supposed to, we can examine the plots of the drain-source voltage vs. the drain current, otherwise knowns as the I-V curve of the transistor.
Now to make sure that the transistor model is working like it's supposed to, we can examine the plots of the drain-source voltage vs. the drain current, otherwise knowns as the I-V curve of the transistor.

```@example NMOS
v_cc_list = collect(0.05:0.1:10.0)

Expand All @@ -58,7 +61,7 @@ I_D_lists = []
for V_b in [1.0, 1.4, 1.8, 2.2, 2.6]
I_D_list = []
for V_cc in v_cc_list
@mtkbuild sys = SimpleNMOSCircuit(V_cc=V_cc, V_b=V_b)
@mtkbuild sys = SimpleNMOSCircuit(V_cc = V_cc, V_b = V_b)
prob = ODEProblem(sys, Pair[], (0.0, 10.0))
sol = solve(prob)
push!(I_D_list, sol[sys.Q1.d.i][1])
Expand All @@ -67,8 +70,10 @@ for V_b in [1.0, 1.4, 1.8, 2.2, 2.6]
end

reduce(hcat, I_D_lists)
plot(v_cc_list, I_D_lists, title="NMOS IV Curves", label=["V_GS: 1.0 V" "V_GS: 1.4 V" "V_GS: 1.8 V" "V_GS: 2.2 V" "V_GS: 2.6 V"], xlabel = "Drain-Source Voltage (V)", ylabel = "Drain Current (A)")
plot(v_cc_list, I_D_lists, title = "NMOS IV Curves",
label = ["V_GS: 1.0 V" "V_GS: 1.4 V" "V_GS: 1.8 V" "V_GS: 2.2 V" "V_GS: 2.6 V"],
xlabel = "Drain-Source Voltage (V)", ylabel = "Drain Current (A)")
```

We can see that we get exactly what we would expect: as the drain-source voltage increases, the drain current increases, until the the transistor gets in to the saturation region of operation.
Then the only increase in drain current is due to the channel-length modulation effect. Additionally, we can see that the maximum current reached increases as the gate voltage increases.
We can see that we get exactly what we would expect: as the drain-source voltage increases, the drain current increases, until the the transistor gets in to the saturation region of operation.
Then the only increase in drain current is due to the channel-length modulation effect. Additionally, we can see that the maximum current reached increases as the gate voltage increases.
4 changes: 2 additions & 2 deletions docs/src/tutorials/input_component.md
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ my_interpolation = LinearInterpolation(df.data, df.time)

@mtkmodel MassSpringDamperSystem2 begin
@components begin
src = Interpolation(itp=my_interpolation)
src = Interpolation(itp = my_interpolation)
clk = ContinuousClock()
model = MassSpringDamper()
end
Expand Down Expand Up @@ -174,7 +174,7 @@ plot(sol2)
```

!!! note

Note that when changing the data, the length of the new data must be the same as the length of the original data.

## Custom Component with External Data
Expand Down
3 changes: 2 additions & 1 deletion src/Blocks/sources.jl
Original file line number Diff line number Diff line change
Expand Up @@ -871,6 +871,7 @@ function ParametrizedInterpolation(
name)
end

function ParametrizedInterpolation(; interp_type, u::AbstractVector, x::AbstractVector, name)
function ParametrizedInterpolation(;
interp_type, u::AbstractVector, x::AbstractVector, name)
ParametrizedInterpolation(interp_type, u, x; name)
end
16 changes: 7 additions & 9 deletions src/Electrical/Analog/mosfets.jl
Original file line number Diff line number Diff line change
Expand Up @@ -71,18 +71,16 @@ Based on the MOSFET models in (Sedra, A. S., Smith, K. C., Carusone, T. C., & Ga
d.i ~ ifelse(d.v < s.v, -1, 1) * ifelse(V_GS < V_tn,
V_DS / R_DS,
ifelse(V_DS < V_OV,
k_n * (1 + lambda * V_DS) * (V_OV - V_DS / 2) * V_DS + V_DS / R_DS,
((k_n * V_OV^2) / 2) * (1 + lambda * V_DS) + V_DS / R_DS
)
k_n * (1 + lambda * V_DS) * (V_OV - V_DS / 2) * V_DS + V_DS / R_DS,
((k_n * V_OV^2) / 2) * (1 + lambda * V_DS) + V_DS / R_DS
)
)

g.i ~ 0
s.i ~ -d.i
end
end



"""
PMOS(;name, V_tp, R_DS, lambda)

Expand Down Expand Up @@ -154,13 +152,13 @@ Based on the MOSFET models in (Sedra, A. S., Smith, K. C., Carusone, T. C., & Ga
d.i ~ -ifelse(d.v > s.v, -1.0, 1.0) * ifelse(V_GS > V_tp,
V_DS / R_DS,
ifelse(V_DS > (V_GS - V_tp),
k_p * (1 + lambda * V_DS) * ((V_GS - V_tp) - V_DS / 2) * V_DS +
V_DS / R_DS,
((k_p * (V_GS - V_tp)^2) / 2) * (1 + lambda * V_DS) + V_DS / R_DS,
k_p * (1 + lambda * V_DS) * ((V_GS - V_tp) - V_DS / 2) * V_DS +
V_DS / R_DS,
((k_p * (V_GS - V_tp)^2) / 2) * (1 + lambda * V_DS) + V_DS / R_DS
)
)

g.i ~ 0
s.i ~ -d.i
end
end
end
3 changes: 1 addition & 2 deletions test/Blocks/sources.jl
Original file line number Diff line number Diff line change
Expand Up @@ -499,7 +499,6 @@ end
end

@testset "Interpolation in model macro" begin

function MassSpringDamper(; name)
@named input = RealInput()
@variables f(t) x(t)=0 dx(t)=0 ddx(t)
Expand Down Expand Up @@ -527,7 +526,7 @@ end
connect(src.input, clk.output)
connect(src.output, model.input)
end
end;
end
@mtkcompile sys = model_with_lut()

prob = ODEProblem(sys, [], (0.0, 1))
Expand Down
44 changes: 20 additions & 24 deletions test/Electrical/analog.jl
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,8 @@ end
connect(R2.n, voltage.n, ground.g)]

@named model = System(connections, t,
systems = [R0, R1, R2, source, short, voltage, ground]; guesses = [R2.v => 0.0, R1.v => 0.0])
systems = [R0, R1, R2, source, short, voltage, ground]; guesses = [
R2.v => 0.0, R1.v => 0.0])
sys = mtkcompile(model)
prob = ODEProblem(sys, [], (0, 2.0))
sol = solve(prob, Rodas4()) # has no state; does not work with Tsit5
Expand Down Expand Up @@ -541,16 +542,15 @@ end
# savefig(plt, "rc_circuit_test_variable_resistor")
end
@testset "NMOS Transistor" begin

@mtkmodel SimpleNMOSCircuit begin
@components begin
Q1 = NMOS()
Vcc = Voltage()
Vb = Voltage()
ground = Ground()

Vcc_const = Constant(k=V_cc)
Vb_const = Constant(k=V_b)
Vcc_const = Constant(k = V_cc)
Vb_const = Constant(k = V_b)
end

@parameters begin
Expand Down Expand Up @@ -589,8 +589,8 @@ end
Vb = Voltage()
ground = Ground()

Vcc_const = Constant(k=V_cc)
Vb_const = Constant(k=V_b)
Vcc_const = Constant(k = V_cc)
Vb_const = Constant(k = V_b)
end

@parameters begin
Expand All @@ -611,19 +611,16 @@ end
end
end

@mtkbuild flipped_sys = FlippedNMOSCircuit(V_cc=5.0, V_b=3.5)
@mtkbuild flipped_sys = FlippedNMOSCircuit(V_cc = 5.0, V_b = 3.5)

flipped_prob = ODEProblem(flipped_sys, Pair[], (0.0, 10.0))
flipped_sol = solve(flipped_prob)
@test flipped_sol[flipped_sys.Q1.d.i][1] < 0
@test flipped_sol[flipped_sys.Q1.s.i][1] > 0
@test flipped_sol[flipped_sys.Q1.s.i][1] > 0
@test flipped_sol[flipped_sys.Q1.s.v] > flipped_sol[flipped_sys.Q1.d.v]

end


@testset "PMOS Transistor" begin

@mtkmodel SimplePMOSCircuit begin
@components begin
Q1 = PMOS()
Expand All @@ -632,9 +629,9 @@ end
Vd = Voltage()
ground = Ground()

Vs_const = Constant(k=V_s)
Vb_const = Constant(k=V_b)
Vd_const = Constant(k=V_d)
Vs_const = Constant(k = V_s)
Vb_const = Constant(k = V_b)
Vd_const = Constant(k = V_d)
end

@parameters begin
Expand All @@ -646,7 +643,7 @@ end
#voltage sources
connect(Vs_const.output, Vs.V)
connect(Vb_const.output, Vb.V)
connect(Vd_const.output, Vd.V )
connect(Vd_const.output, Vd.V)

#ground connections
connect(Vs.n, Vb.n, ground.g, Vd.n)
Expand All @@ -658,7 +655,7 @@ end
end
end

@mtkbuild sys = SimplePMOSCircuit(V_s=5.0, V_b=2.5, V_d = 3)
@mtkbuild sys = SimplePMOSCircuit(V_s = 5.0, V_b = 2.5, V_d = 3)

prob = ODEProblem(sys, Pair[], (0.0, 10.0))
sol = solve(prob)
Expand All @@ -675,9 +672,9 @@ end
Vd = Voltage()
ground = Ground()

Vs_const = Constant(k=V_s)
Vb_const = Constant(k=V_b)
Vd_const = Constant(k=V_d)
Vs_const = Constant(k = V_s)
Vb_const = Constant(k = V_b)
Vd_const = Constant(k = V_d)
end

@parameters begin
Expand All @@ -689,7 +686,7 @@ end
#voltage sources
connect(Vs_const.output, Vs.V)
connect(Vb_const.output, Vb.V)
connect(Vd_const.output, Vd.V )
connect(Vd_const.output, Vd.V)

#ground connections
connect(Vs.n, Vb.n, ground.g, Vd.n)
Expand All @@ -701,15 +698,13 @@ end
end
end

@mtkbuild flipped_sys = FlippedPMOSCircuit(V_s=5.0, V_b=2.5, V_d = 3)
@mtkbuild flipped_sys = FlippedPMOSCircuit(V_s = 5.0, V_b = 2.5, V_d = 3)

flipped_prob = ODEProblem(flipped_sys, Pair[], (0.0, 10.0))
flipped_sol = solve(flipped_prob)

@test flipped_sol[flipped_sys.Q1.d.i][1] > 0.0
@test flipped_sol[flipped_sys.Q1.s.i][1] < 0.0


end

@testset "NPN Tests" begin
Expand Down Expand Up @@ -867,7 +862,8 @@ end

@mtkcompile sys = SimplePNPCircuitSubstrate(V_b = 0.70)

prob = ODEProblem(sys, [sys.Q1.c.i => 0.0], (0.0, 10.0); guesses = [sys.Q1.I_sub => 1.0])
prob = ODEProblem(
sys, [sys.Q1.c.i => 0.0], (0.0, 10.0); guesses = [sys.Q1.I_sub => 1.0])
sol = solve(prob)

@test isapprox(
Expand Down
3 changes: 2 additions & 1 deletion test/Hydraulic/isothermal_compressible.jl
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,8 @@ end
end

@testset "DynamicVolume and minimum_volume feature" begin # Need help here
function TestSystem(; name, area = 0.01, length = 0.1, damping_volume = length * area * 0.1)
function TestSystem(;
name, area = 0.01, length = 0.1, damping_volume = length * area * 0.1)
pars = []

# DynamicVolume values
Expand Down
12 changes: 8 additions & 4 deletions test/Mechanical/rotational.jl
Original file line number Diff line number Diff line change
Expand Up @@ -89,8 +89,10 @@ end

@mtkcompile sys = TwoInertiasWithDrivingTorque()
deqs = [eq.lhs => eq.rhs for eq in equations(sys)]
prob = DAEProblem(sys, [deqs;
D(D(sys.inertia2.phi)) => 1.0; sys.spring.flange_b.phi => 0.0], (0, 10.0))
prob = DAEProblem(
sys, [deqs;
D(D(sys.inertia2.phi)) => 1.0; sys.spring.flange_b.phi => 0.0],
(0, 10.0))
sol = solve(prob, DFBDF())
@test SciMLBase.successful_retcode(sol)

Expand Down Expand Up @@ -216,8 +218,10 @@ end
end

@mtkcompile sys = StickSlip()
prob = DAEProblem(sys, [D.(unknowns(sys)) .=> 0.0;
[sys.inertia.flange_b.tau => 0.0; unknowns(sys) .=> 0.0...]], (0, 10.0))
prob = DAEProblem(sys,
[D.(unknowns(sys)) .=> 0.0;
[sys.inertia.flange_b.tau => 0.0; unknowns(sys) .=> 0.0...]],
(0, 10.0))

sol = solve(prob, DFBDF())
@test SciMLBase.successful_retcode(sol)
Expand Down
Loading