-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_circuit.py
116 lines (99 loc) · 2.75 KB
/
test_circuit.py
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
import math
import numpy as np
from scipy import linalg
from scipy import sparse
import scipy.sparse.linalg
class circuit:
def __init__(self):
# diode parameters
self.IS = 1e-16
self.VT = 0.0259
# add diffusion capacitance later
self.TF=1e-7
# series resistance
self.RS = 1e3
# bias
self.V = 0
# solution
self.solution = np.array([0.0]*3)
def set_bias(self, v):
#print("set bias %g" % v)
self.V = v
def set_solution(self, sol):
#print("set solution %s" % str(sol))
self.solution = sol
def get_solution(self):
return self.solution
def load_circuit(self):
# solution[0] = I # current in voltage source
# solution[1] = V1 # voltage node attached to voltage source and resistor
# solution[2] = V2 # voltage node attached to resitor and diode
# diode
I = self.solution[0]
V1 = self.solution[1]
V2 = self.solution[2]
IS = self.IS
VT = self.VT
try:
common = IS*(math.exp(V2/VT))
except:
common = IS*(math.exp(20))
idiode = common - IS
idiode_V2 = common/VT
#https://en.wikipedia.org/wiki/Diffusion_capacitance
#qdiode = idiode * self.TF
#qdiode_V2 = idiode_V2 * self.TF
qdiode_V2 = 1e-10
qdiode = qdiode_V2 * V2
#qdiode = qdiode_V2 * V2
# series resistance
grs = 1./self.RS
irs = (V1 - V2)*grs
# voltage source
ibias = V1 - self.V
dibias_dv1 = 1.
dI_dI = 1.0
ivec = np.array([
ibias,
+irs + I,
-irs + idiode
])
gmat = np.array([
[0., 0., 0.],
[0., 0., 0.],
[0., 0., 0.],
])
gmat[0][1] = dibias_dv1
gmat[1][0] = +dI_dI
gmat[1][1] = grs
gmat[1][2] = -grs
gmat[2][1] = -grs
gmat[2][2] = +grs + idiode_V2
qvec = np.array([
0.0,
0.0,
qdiode
])
cmat = np.array([
[0., 0., 0.],
[0., 0., 0.],
[0., 0., 0.],
])
cmat[2][2] = qdiode_V2
return {
'gmat' : sparse.csc_matrix(gmat),
'ivec' : ivec,
'cmat' : sparse.csc_matrix(cmat),
'qvec' : qvec,
}
def dc_solve(self):
for n in range(5):
data = self.load_circuit()
#print(data['gmat'])
jinv = sparse.linalg.factorized(data['gmat'])
update = -jinv(data['ivec'])
print(update)
newsol = self.get_solution() + update
self.set_solution(newsol)
#print(update.shape)
#print(f.shape)