-
Notifications
You must be signed in to change notification settings - Fork 0
/
Diode.java
executable file
·147 lines (132 loc) · 3.79 KB
/
Diode.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
class Diode {
int nodes[];
CirSim sim;
Diode(CirSim s) {
sim = s;
nodes = new int[2];
}
void setup(double fw, double zv) {
fwdrop = fw;
zvoltage = zv;
vdcoef = Math.log(1/leakage + 1)/fwdrop;
vt = 1/vdcoef;
// critical voltage for limiting; current is vt/sqrt(2) at
// this voltage
vcrit = vt * Math.log(vt/(Math.sqrt(2)*leakage));
if (zvoltage == 0)
zoffset = 0;
else {
// calculate offset which will give us 5mA at zvoltage
double i = -.005;
zoffset = zvoltage-Math.log(-(1+i/leakage))/vdcoef;
}
}
void reset() {
lastvoltdiff = 0;
}
public double leakage = 1e-14; // was 1e-9;
double vt, vdcoef, fwdrop, zvoltage, zoffset;
double lastvoltdiff;
double vcrit;
double limitStep(double vnew, double vold) {
double arg;
double oo = vnew;
// check new voltage; has current changed by factor of e^2?
if (vnew > vcrit && Math.abs(vnew - vold) > (vt + vt)) {
if(vold > 0) {
arg = 1 + (vnew - vold) / vt;
if(arg > 0) {
// adjust vnew so that the current is the same
// as in linearized model from previous iteration.
// current at vnew = old current * arg
vnew = vold + vt * Math.log(arg);
// current at v0 = 1uA
double v0 = Math.log(1e-6/leakage)*vt;
vnew = Math.max(v0, vnew);
} else {
vnew = vcrit;
}
} else {
// adjust vnew so that the current is the same
// as in linearized model from previous iteration.
// (1/vt = slope of load line)
vnew = vt *Math.log(vnew/vt);
}
sim.converged = false;
//System.out.println(vnew + " " + oo + " " + vold);
} else if (vnew < 0 && zoffset != 0) {
// for Zener breakdown, use the same logic but translate the values
vnew = -vnew - zoffset;
vold = -vold - zoffset;
if (vnew > vcrit && Math.abs(vnew - vold) > (vt + vt)) {
if(vold > 0) {
arg = 1 + (vnew - vold) / vt;
if(arg > 0) {
vnew = vold + vt * Math.log(arg);
double v0 = Math.log(1e-6/leakage)*vt;
vnew = Math.max(v0, vnew);
//System.out.println(oo + " " + vnew);
} else {
vnew = vcrit;
}
} else {
vnew = vt *Math.log(vnew/vt);
}
sim.converged = false;
}
vnew = -(vnew+zoffset);
}
return vnew;
}
void stamp(int n0, int n1) {
nodes[0] = n0;
nodes[1] = n1;
sim.stampNonLinear(nodes[0]);
sim.stampNonLinear(nodes[1]);
}
void doStep(double voltdiff) {
// used to have .1 here, but needed .01 for peak detector
if (Math.abs(voltdiff-lastvoltdiff) > .01)
sim.converged = false;
voltdiff = limitStep(voltdiff, lastvoltdiff);
lastvoltdiff = voltdiff;
if (voltdiff >= 0 || zvoltage == 0) {
// regular diode or forward-biased zener
double eval = Math.exp(voltdiff*vdcoef);
// make diode linear with negative voltages; aids convergence
if (voltdiff < 0)
eval = 1;
double geq = vdcoef*leakage*eval;
double nc = (eval-1)*leakage - geq*voltdiff;
sim.stampConductance(nodes[0], nodes[1], geq);
sim.stampCurrentSource(nodes[0], nodes[1], nc);
} else {
// Zener diode
/*
* I(Vd) = Is * (exp[Vd*C] - exp[(-Vd-Vz)*C] - 1 )
*
* geq is I'(Vd)
* nc is I(Vd) + I'(Vd)*(-Vd)
*/
double geq = leakage*vdcoef* (
Math.exp(voltdiff*vdcoef) + Math.exp((-voltdiff-zoffset)*vdcoef)
);
double nc = leakage* (
Math.exp(voltdiff*vdcoef)
- Math.exp((-voltdiff-zoffset)*vdcoef)
- 1
) + geq*(-voltdiff);
sim.stampConductance(nodes[0], nodes[1], geq);
sim.stampCurrentSource(nodes[0], nodes[1], nc);
}
}
double calculateCurrent(double voltdiff) {
if (voltdiff >= 0 || zvoltage == 0)
return leakage*(Math.exp(voltdiff*vdcoef)-1);
return leakage* (
Math.exp(voltdiff*vdcoef)
- Math.exp((-voltdiff-zoffset)*vdcoef)
- 1
);
}
}