I should also mention the ideal-diode code (comments added below) comes directly from Ken's Chapter 3, section 6.3.
This topic is a few months old, but I just saw it today when I needed to model a current-limited voltage source. My solution isn't original to me, I extracted it from
http://www.beigebag.com/case_logical_9.htm "Logical Expressions - NO Smooth Switches and a Current Limited Voltage source".
But it's a nice solution, so here it is in easy to use form:
// Current limited voltage source
// imax
// -----
// vint ---| --> |------> vp
// | ----- |
// | |
// | \
// | / Rnil
// | | /| \
// |_____|/ |___|
// | |\ | va
// | | \|
// -
// / + \
// | |
// \ - /
// -
// |
// ----------------> vn
//
//
`include "disciplines.vams"
`include "constants.vams"
module vdc_i_limited(vp, vn);
electrical vp, vn;
electrical vint, va;
parameter real imax = 0.017; // current limit
parameter real vdd = 3.4; // V set
parameter real Rnil = 1e-9;
analog begin
V(vint,vn) <+ vdd;
I(vint,vp) <+ imax;
I(vp,va) <+ V(vp,va) / Rnil;
// Ideal Diode
@(cross((V(va,vint) + I(va,vint)),0))
;
if((V(va,vint) + I(va,vint)) > 0)
V(va,vint) <+ 0;
else
I(va,vint) <+ 0;
end
// End of ideal diode
endmodule
And a test circuit that sweeps a resistive load:
// Test vdc_i_limited model
//
// (n) ----------
// | |
// | |
// - |
// / + \ /
// vdc_i_limited | | \ Rsweep
// \ - / /
// - |
// | |
// | |
// (0) ----------
//
simulator lang=spectre
ahdl_include "vdc_i_limited.vams"
V1 (n 0) vdc_i_limited
Rsweep (n 0) resistor R = 400
doDCop dc print=yes
doDCswp dc start=400 stop=100 step=1 dev=Rsweep
The drawings got messed up, but you get the idea... Maybe attaching an ascii file...