Skip to content

Commit 92496e5

Browse files
committed
add tests
1 parent 88a09c9 commit 92496e5

File tree

1 file changed

+171
-0
lines changed

1 file changed

+171
-0
lines changed

test/Electrical/analog.jl

Lines changed: 171 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -540,3 +540,174 @@ end
540540
# title = "RC Circuit Test with VariableResistor")
541541
# savefig(plt, "rc_circuit_test_variable_resistor")
542542
end
543+
544+
545+
@testset "NPN Tests" begin
546+
547+
@mtkmodel SimpleNPNCircuit begin
548+
@components begin
549+
Q1 = NPN()
550+
Vcc = Voltage()
551+
Vb = Voltage()
552+
ground = Ground()
553+
554+
Vcc_const = Constant(k = V_cc)
555+
Vb_const = Constant(k = V_b)
556+
end
557+
558+
@parameters begin
559+
V_cc = 0.0
560+
V_b = 0.0
561+
end
562+
@equations begin
563+
#voltage sources
564+
connect(Vcc_const.output, Vcc.V)
565+
connect(Vb_const.output, Vb.V)
566+
567+
#ground connections
568+
connect(Vcc.n, Vb.n, ground.g, Q1.e)
569+
570+
#other stuff
571+
connect(Vcc.p, Q1.c)
572+
connect(Vb.p, Q1.b)
573+
end
574+
end
575+
576+
@mtkbuild sys = SimpleNPNCircuit(V_cc = 3.0, V_b = 0.70)
577+
578+
prob = ODEProblem(sys, Pair[], (0.0, 10.0))
579+
sol = solve(prob)
580+
581+
# make sure KCL is true
582+
@test sol[sys.Q1.b.i][1] + sol[sys.Q1.e.i][1] + sol[sys.Q1.c.i][1] 0.0
583+
584+
# test NPN with substrate
585+
@mtkmodel SimpleNPNCircuitSubstrate begin
586+
@components begin
587+
Q1 = NPN(use_substrate = true)
588+
Vcc = Voltage()
589+
Vb = Voltage()
590+
ground = Ground()
591+
R1 = Resistor(R = 1000)
592+
593+
Vcc_sine = Sine(frequency = 0.5)
594+
Vb_const = Constant(k = V_b)
595+
end
596+
597+
@parameters begin
598+
V_cc = 0.0
599+
V_b = 0.0
600+
end
601+
@equations begin
602+
#voltage sources
603+
connect(Vcc_sine.output, Vcc.V)
604+
connect(Vb_const.output, Vb.V)
605+
606+
#ground connections
607+
connect(Vcc.n, Vb.n, ground.g, Q1.e, Q1.s)
608+
609+
#other stuff
610+
connect(Vcc.p, R1.p)
611+
connect(R1.n, Q1.c)
612+
connect(Vb.p, Q1.b)
613+
end
614+
end
615+
616+
@mtkbuild sys = SimpleNPNCircuitSubstrate(V_b = 0.70)
617+
618+
prob = ODEProblem(sys, [sys.Q1.c.i => 0.0], (0.0, 10.0))
619+
sol = solve(prob)
620+
621+
@test isapprox(
622+
sol[sys.Q1.b.i][15] +
623+
sol[sys.Q1.e.i][15] +
624+
sol[sys.Q1.c.i][15] +
625+
sol[sys.Q1.s.i][15],
626+
0.0, atol = 1e-16)
627+
628+
end
629+
630+
631+
@testset "PNP Tests" begin
632+
@mtkmodel SimplePNPCircuit begin
633+
@components begin
634+
Q1 = PNP()
635+
Vcc = Voltage()
636+
Vb = Voltage()
637+
ground = Ground()
638+
639+
Vcc_const = Constant(k = V_cc)
640+
Vb_const = Constant(k = V_b)
641+
end
642+
643+
@parameters begin
644+
V_cc = 0.0
645+
V_b = 0.0
646+
end
647+
@equations begin
648+
#voltage sources
649+
connect(Vcc_const.output, Vcc.V)
650+
connect(Vb_const.output, Vb.V)
651+
652+
#ground connections
653+
connect(Vcc.n, Vb.n, ground.g, Q1.e)
654+
655+
#other stuff
656+
connect(Vcc.p, Q1.c)
657+
connect(Vb.p, Q1.b)
658+
end
659+
end
660+
661+
@mtkbuild sys = SimplePNPCircuit(V_cc = 3.0, V_b = 0.70)
662+
663+
prob = ODEProblem(sys, Pair[], (0.0, 10.0))
664+
sol = solve(prob)
665+
666+
# make sure KCL is true
667+
@test sol[sys.Q1.b.i][1] + sol[sys.Q1.e.i][1] + sol[sys.Q1.c.i][1] 0.0
668+
669+
# test PNP with substrate
670+
@mtkmodel SimplePNPCircuitSubstrate begin
671+
@components begin
672+
Q1 = PNP(use_substrate = true)
673+
Vcc = Voltage()
674+
Vb = Voltage()
675+
ground = Ground()
676+
R1 = Resistor(R = 1000)
677+
678+
Vcc_sine = Sine(frequency = 0.5)
679+
Vb_const = Constant(k = V_b)
680+
end
681+
682+
@parameters begin
683+
V_cc = 0.0
684+
V_b = 0.0
685+
end
686+
@equations begin
687+
#voltage sources
688+
connect(Vcc_sine.output, Vcc.V)
689+
connect(Vb_const.output, Vb.V)
690+
691+
#ground connections
692+
connect(Vcc.n, Vb.n, ground.g, Q1.e, Q1.s)
693+
694+
#other stuff
695+
connect(Vcc.p, R1.p)
696+
connect(R1.n, Q1.c)
697+
connect(Vb.p, Q1.b)
698+
end
699+
end
700+
701+
@mtkbuild sys = SimplePNPCircuitSubstrate(V_b = 0.70)
702+
703+
prob = ODEProblem(sys, [sys.Q1.c.i => 0.0], (0.0, 10.0))
704+
sol = solve(prob)
705+
706+
@test isapprox(
707+
sol[sys.Q1.b.i][15] +
708+
sol[sys.Q1.e.i][15] +
709+
sol[sys.Q1.c.i][15] +
710+
sol[sys.Q1.s.i][15],
711+
0.0,
712+
atol = 1e-16)
713+
end

0 commit comments

Comments
 (0)