-
Notifications
You must be signed in to change notification settings - Fork 0
/
electro_utils.py
274 lines (210 loc) · 9.3 KB
/
electro_utils.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
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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
from PySpice import *
from PySpice.Spice.Netlist import Circuit
import PySpice.Logging.Logging as Logging
import numpy as np
import torch
# +------------------------------------------------+
# | Main Functions |
# +------------------------------------------------+
def get_heat(circuit, r_mat, dv_mat, scale):
# set up variables:
simulator = circuit.simulator()
analysis = simulator.operating_point()
volt_matrix = torch.zeros(r_mat.shape, dtype=torch.float64)
for node in analysis.nodes.values():
if str(node) == 'vin':
continue
currP = str(node).split('/')
volt_matrix[int(float(currP[0])), int(float(currP[1])), int(float(currP[2]))] = float(node)
X = r_mat.shape[0]
Y = r_mat.shape[1]
Z = r_mat.shape[2]
# ---------- Calculate X points ----------
dv_mat[:X-1, :, :, 0] = volt_matrix[:X-1, :, :] - volt_matrix[1:, :, :]
dv_mat[1:, :, :, 1] = volt_matrix[1:, :, :] - volt_matrix[:X-1, :, :]
# ---------- Calculate Y points ----------
dv_mat[:, :Y-1, :, 2] = volt_matrix[:, :Y-1, :] - volt_matrix[:, 1:, :]
dv_mat[:, 1:, :, 3] = volt_matrix[:, 1:, :] - volt_matrix[:, :Y-1, :]
# ---------- Calculate Z points ----------
dv_mat[:, :, :Z-1, 4] = volt_matrix[:, :, :Z-1] - volt_matrix[:, :, 1:]
dv_mat[:, :, 1:, 5] = volt_matrix[:, :, 1:] - volt_matrix[:, :, :Z-1]
#print(dv_mat)
#print(volt_matrix)
return size_up(torch.sum(dv_mat * dv_mat, axis=3)/(r_mat * 4), scale), simulator, volt_matrix
def add_heat(mat_t, r_heat, hstate, startx, starty, x, y, z):
endx = startx + x
endy = starty + y
#print((r_heat*hstate))
mat_t[startx:endx, starty:endy, 0:z] = mat_t[startx:endx,starty:endy, 0:z] + (r_heat*hstate)
def get_hstate_elec(mat_d, dt, s):
X_GRID = mat_d.shape[0]
Y_GRID = mat_d.shape[1]
Z_GRID = mat_d.shape[2]
h_state = np.zeros((X_GRID, Y_GRID, Z_GRID))
mass = (8*mat_d[:,:,:,0]*mat_d[:,:,:,1]*mat_d[:,:,:,2])*mat_d[:,:,:,5]*(s**3)
h_state = dt/(mass * mat_d[:,:,:,4])
return h_state
# +------------------------------------------------+
# | PySpice Functions |
# +------------------------------------------------+
def update_resistors(circuit, r_mat, nodes, contact_length):
X_LEN = nodes.shape[0]
Y_LEN = nodes.shape[1]
Z_LEN = nodes.shape[2]
c = 0
if not isinstance(r_mat, np.ndarray):
r_mat = r_mat.numpy()
# Set resistors for main array IJK
for i in range(X_LEN-1):
for j in range(Y_LEN-1):
for k in range(Z_LEN-1):
circuit['R' + str(c)].resistance = r_mat[i, j, k] + r_mat[i+1, j, k]
c += 1
circuit['R' + str(c)].resistance = r_mat[i, j, k] + r_mat[i, j+1, k]
c += 1
circuit['R' + str(c)].resistance = r_mat[i,j, k] + r_mat[i, j, k+1]
c += 1
# set final JK
for j in range(Y_LEN-1):
for k in range(Z_LEN-1):
circuit['R' + str(c)].resistance = r_mat[X_LEN-1, j, k] + r_mat[X_LEN-1, j+1, k]
c += 1
circuit['R' + str(c)].resistance = r_mat[X_LEN-1, j, k] + r_mat[X_LEN-1, j, k+1]
c += 1
# set final IK
for i in range(X_LEN-1):
for k in range(Z_LEN-1):
circuit['R' + str(c)].resistance = r_mat[i, Y_LEN-1, k] + r_mat[i+1, Y_LEN-1, k]
c += 1
circuit['R' + str(c)].resistance = r_mat[i, Y_LEN-1, k] + r_mat[i, Y_LEN-1, k+1]
c += 1
# set final IJ
for i in range(X_LEN-1):
for j in range(Y_LEN-1):
circuit['R' + str(c)].resistance = r_mat[i, j, Z_LEN-1] + r_mat[i+1, j, Z_LEN-1]
c += 1
circuit['R' + str(c)].resistance = r_mat[i, j, Z_LEN-1] + r_mat[i, j+1, Z_LEN-1]
c += 1
# set final I
for i in range(X_LEN-1):
circuit['R' + str(c)].resistance = r_mat[i, Y_LEN-1, Z_LEN-1] + r_mat[i+1, Y_LEN-1, Z_LEN-1]
c += 1
# set final J
for j in range(Y_LEN-1):
circuit['R' + str(c)].resistance = r_mat[X_LEN-1, j, Z_LEN-1] + r_mat[X_LEN-1, j+1, Z_LEN-1]
c += 1
# set final K
for k in range(Z_LEN-1):
circuit['R' + str(c)].resistance = r_mat[X_LEN-1, Y_LEN-1, k] + r_mat[X_LEN-1, Y_LEN-1, k+1]
c += 1
# Setup the voltage sources and contacts
for j in range(Y_LEN):
for i in range(contact_length):
circuit['R' + str(c)].resistance = r_mat[i, j, 0]
c += 1
circuit['R' + str(c)].resistance = r_mat[i, j, 0]
c += 1
def setup_resistors(circuit, r_mat, nodes, contact_length):
X_LEN = nodes.shape[0]
Y_LEN = nodes.shape[1]
Z_LEN = nodes.shape[2]
c = 0
if not isinstance(r_mat, np.ndarray):
r_mat = r_mat.numpy()
# Set resistors for main array IJK
for i in range(X_LEN-1):
for j in range(Y_LEN-1):
for k in range(Z_LEN-1):
n = get_node(nodes, i, j, k)
circuit.R(c, n, get_node(nodes, i+1, j, k),
r_mat[i, j, k] + r_mat[i+1, j, k])
c += 1
circuit.R(c, n, get_node(nodes, i, j+1, k),
r_mat[i, j, k] + r_mat[i, j+1, k])
c += 1
circuit.R(c, n, get_node(nodes, i, j, k+1),
r_mat[i, j, k] + r_mat[i, j, k+1])
c += 1
# set final JK
for j in range(Y_LEN-1):
for k in range(Z_LEN-1):
circuit.R(c, get_node(nodes, X_LEN-1, j, k), get_node(nodes, X_LEN-1, j+1, k),
r_mat[X_LEN-1, j, k] + r_mat[X_LEN-1, j+1, k])
c += 1
circuit.R(c, get_node(nodes, X_LEN-1, j, k), get_node(nodes, X_LEN-1, j, k+1),
r_mat[X_LEN-1, j, k] + r_mat[X_LEN-1, j, k+1])
c += 1
# set final IK
for i in range(X_LEN-1):
for k in range(Z_LEN-1):
circuit.R(c, get_node(nodes, i, Y_LEN-1, k), get_node(nodes, i+1, Y_LEN-1, k),
r_mat[i, Y_LEN-1, k] + r_mat[i+1, Y_LEN-1, k])
c += 1
circuit.R(c, get_node(nodes, i, Y_LEN-1, k), get_node(nodes, i, Y_LEN-1, k+1),
r_mat[i, Y_LEN-1, k] + r_mat[i, Y_LEN-1, k+1])
c += 1
# set final IJ
for i in range(X_LEN-1):
for j in range(Y_LEN-1):
circuit.R(c, get_node(nodes, i, j, Z_LEN-1), get_node(nodes, i+1, j, Z_LEN-1),
r_mat[i, j, Z_LEN-1] + r_mat[i+1, j, Z_LEN-1])
c += 1
circuit.R(c, get_node(nodes, i, j, Z_LEN-1), get_node(nodes, i, j+1, Z_LEN-1),
r_mat[i, j, Z_LEN-1] + r_mat[i, j+1, Z_LEN-1])
c += 1
# set final I
for i in range(X_LEN-1):
circuit.R(c, get_node(nodes, i, Y_LEN-1, Z_LEN-1), get_node(nodes, i+1, Y_LEN-1, Z_LEN-1),
r_mat[i, Y_LEN-1, Z_LEN-1] + r_mat[i+1, Y_LEN-1, Z_LEN-1])
c += 1
# set final J
for j in range(Y_LEN-1):
circuit.R(c, get_node(nodes, X_LEN-1, j, Z_LEN-1), get_node(nodes, X_LEN-1, j+1, Z_LEN-1),
r_mat[X_LEN-1, j, Z_LEN-1] + r_mat[X_LEN-1, j+1, Z_LEN-1])
c += 1
# set final K
for k in range(Z_LEN-1):
circuit.R(c, get_node(nodes, X_LEN-1, Y_LEN-1, k), get_node(nodes, X_LEN-1, Y_LEN-1, k+1),
r_mat[X_LEN-1, Y_LEN-1, k] + r_mat[X_LEN-1, Y_LEN-1, k+1])
c += 1
# Setup the voltage sources and contacts
for j in range(Y_LEN):
for i in range(contact_length):
circuit.R(c, 'vin', get_node(nodes, i, j, 0), r_mat[i, j, 0])
c += 1
circuit.R(c, circuit.gnd, get_node(nodes, X_LEN - i- 1, j, 0), r_mat[i, j, 0])
c += 1
def get_node(nodes, i, j, k):
return str(float(nodes[i, j, k, 0])) + "/" + str(float(nodes[i, j, k, 1])) + "/" + str(float(nodes[i, j, k, 2]))
# +------------------------------------------------+
# | Helper Functions |
# +------------------------------------------------+
def get_selected_area(mat, startx, starty, x, y, z):
endx = startx + x
endy = starty + y
return mat[startx:endx, starty:endy, 0:z]
def size_up(m, s):
new_m = torch.zeros( (m.shape[0]*s, m.shape[1]*s, m.shape[2]*s) )
for i in range(m.shape[0]):
for j in range(m.shape[1]):
for k in range(m.shape[2]):
new_m[s*i:s*(i+1), s*j:s*(j+1), s*k:s*(k+1)] = m[i, j, k]
return new_m
def size_down(m, s):
new_m = torch.zeros((m.shape[0]//s, m.shape[1]//s, m.shape[2]//s))
for i in range(m.shape[0]//s):
for j in range(m.shape[1]//s):
for k in range(m.shape[2]//s):
new_m[i, j, k] = torch.mean(
m[s*i:s*(i+1), s*j:s*(j+1), s*k:s*(k+1)])
return new_m
# +------------------------------------------------+
# | Resistance Functions |
# +------------------------------------------------+
def get_res_matrix(mat_t, L, STARTX, STARTY, X_ESIM, Y_ESIM, Z_ESIM, S):
em = get_selected_area(mat_t, STARTX, STARTY, X_ESIM, Y_ESIM, Z_ESIM)
r_mat = get_resistivity(size_down(em, S))/(L * S * 4)
return r_mat
def get_resistivity(M):
r_mat = (9.225e-11) * M**5 + (-1.665e-7) * M**4 + (0.0001994) * M**3 + (-0.04253) * M**2 + (7.511) * M + (-525.9)
return torch.maximum(r_mat, torch.zeros(M.shape)).numpy()