This repository was archived by the owner on Jun 12, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathspice_util.py
More file actions
324 lines (288 loc) · 8.49 KB
/
spice_util.py
File metadata and controls
324 lines (288 loc) · 8.49 KB
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
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
# vim: set shiftwidth=2 softtabstop=2 ts=2 expandtab:
#
# Copyright 2022 Google LLC
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# https://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
import os
import numpy as np
import collections
import math
import re
from enum import Enum
FFTResultDataPoint = collections.namedtuple(
'FFTResultDataPoint', ['index', 'freq_hz', 'magnitude', 'phase_deg'])
class NumericalValue():
def __init__(self, value, unit=None):
self.value = value
self.unit = unit
def __repr__(self):
return f'{self.value} prefix:{self.unit}'
def __str__(self):
letter = self.unit.Letter() if self.unit else ''
return f'{self.value}{letter}'
def InBaseUnits(self):
new_value = self.value
unit = self.unit
if unit is not None:
shift = unit.value
new_value = new_value * math.pow(10, shift)
return NumericalValue(new_value, None)
def _InOtherUnits(self, other_unit):
if other_unit == self.unit:
return self
other_power = other_unit.value if other_unit else 0
self_power = self.unit.value if self.unit else 0
power_difference = self_power - other_power
new_value = self.value * math.pow(10, power_difference)
return NumericalValue(new_value, other_unit)
def __eq__(self, other):
return self._InOtherUnits(other.unit).value == other.value
def __lt__(self, other):
val = self._InOtherUnits(other.unit).value < other.value
#print(f'{self} < {other} = {val}')
return val
def __ge__(self, other):
return other < self
def SpiceFormat(self):
# Use:
# t
# g
# meg
# k
# m
# u
# n
# p
# f
pass
def XyceFormat(self):
return str(self.InBaseUnits().value).upper()
class SIUnitPrefix(Enum):
YOCTO = -24
ZEPTO = -21
ATTO = -18
FEMTO = -15
PICO = -12
NANO = -9
MICRO = -6
MILLI = -3
CENTI = -2
DECI = -1
DECA = 1
HECTO = 2
KILO = 3
MEGA = 6
GIGA = 9
TERA = 12
PETA = 15
EXA = 18
ZETTA = 21
YOTTA = 24
def Letter(self):
return _LETTER_BY_UNIT[self]
_LETTER_BY_UNIT = {
SIUnitPrefix.YOCTO: 'y',
SIUnitPrefix.ZEPTO: 'z',
SIUnitPrefix.ATTO: 'a',
SIUnitPrefix.FEMTO: 'f',
SIUnitPrefix.PICO: 'p',
SIUnitPrefix.NANO: 'n',
SIUnitPrefix.MICRO: 'u',
SIUnitPrefix.MILLI: 'm',
SIUnitPrefix.CENTI: 'c',
SIUnitPrefix.DECI: 'd',
SIUnitPrefix.DECA: 'da',
SIUnitPrefix.HECTO: 'h',
SIUnitPrefix.KILO: 'k',
SIUnitPrefix.MEGA: 'M',
SIUnitPrefix.GIGA: 'G',
SIUnitPrefix.TERA: 'T',
SIUnitPrefix.PETA: 'P',
SIUnitPrefix.EXA: 'E',
SIUnitPrefix.ZETTA: 'Z',
SIUnitPrefix.YOTTA: 'Y'
}
class SmallSignalParameters:
def __init__(self, str_tokens):
"""Consume the AC measurements from Spice, less the first column."""
dim = math.sqrt(len(str_tokens)/2.0)
if not dim.is_integer():
raise Exception(f'expecting square number of columns, not {dim}')
dim = int(dim)
values = []
i = 0
while i < len(str_tokens):
values.append(
np.cdouble(complex(float(str_tokens[i]), float(str_tokens[i+1]))))
i += 2
self.matrix = np.resize(np.matrix(values), (dim, dim))
self.type = None
def Print(self):
print(self.matrix)
def __getitem__(self, key):
butchered_key = tuple(k - 1 for k in key)
return self.matrix.__getitem__(butchered_key)
def ReadMeasurementsFile(file_name):
"""Interprets Xyce-generated .mt0 files."""
if not os.path.exists(file_name):
#print(f'file not found: {file_name}')
return {}
print(f'reading {file_name}')
variables = {}
with open(file_name, 'r') as f:
for line in f:
key, value = line.split(' = ')
variables[key] = float(value)
return variables
def ReadFFTFile(file_name):
if not os.path.exists(file_name):
return {}
print(f'reading {file_name}')
DC_LINE_RE = re.compile(
'^DC component.*(?:Norm. )?Mag= ([0-9.eE+-]+).*Phase= ([\d.eE+-]+).*$')
# NOTE(growly): We assume that we only every look at the V( ) voltage
# of a signal. Otherwise change this regex:
HEADER_LINE_RE = re.compile('^FFT analysis for V\((.*)\):$')
# The .fft0 file will contains, one after another, dumps of FFT
# measurements for each signal given to Spice. We collect the
# FFTResultDataPoint of each into this dict.
analyses = {}
with open(file_name, 'r') as f:
data = None
signal_name = None
for line in f:
if line.startswith('FFT analysis'):
# Store data for last signal being listed.
# Prep for more data.
match = HEADER_LINE_RE.match(line)
signal_name = match.group(1)
data = []
continue
if line.startswith('DC component'):
match = DC_LINE_RE.match(line)
if not match:
raise Exception(f'{line} did not match')
data.append(FFTResultDataPoint(
index = -1,
freq_hz = 0.0,
magnitude = float(match.group(1)),
phase_deg = float(match.group(2))))
continue
split = line.split()
try:
point = FFTResultDataPoint(
index = int(split[0]),
freq_hz = float(split[1]),
magnitude = float(split[2]),
phase_deg = float(split[3]))
except:
# Probably not a result line
continue
if data is not None:
data.append(point)
assert signal_name not in analyses
if signal_name and data:
analyses[signal_name] = data
return analyses
def ReadPrintFileForTimeSeries(file_name):
if not os.path.exists(file_name):
return {}
print(f'reading {file_name}')
data_by_header = {}
with open(file_name, 'r') as f:
headers = None
for line in f:
if line.startswith('End'):
continue
if headers is None:
headers = line.split()
for header in headers:
data_by_header[header] = []
continue
tokens = line.split()
for i, token in enumerate(tokens):
data_by_header[headers[i]].append(float(tokens[i]))
return data_by_header
def ReadPrintFile(file_name):
# We expect one line of headers.
if not os.path.exists(file_name):
return []
print(f'reading {file_name}')
with open(file_name, 'r') as f:
headers = None
data = []
for line in f:
if line.startswith('End'):
continue
if headers is None:
headers = line.split()
continue
tokens = line.split()
data.append({headers[i]: tokens[i] for i in range(len(tokens))})
return data
def ReadCSVFile(file_name):
if not os.path.exists(file_name):
return []
print(f'reading {file_name}')
with open(file_name, 'r') as f:
return [row for row in csv.DictReader(f)]
return []
def ReadStepFile(file_name):
header_to_column = {}
column_to_header = {}
if not os.path.exists(file_name):
return {}
params = {}
print(f'reading {file_name}')
with open(file_name, 'r') as f:
for line in f:
if line.startswith('End'):
break
if not header_to_column:
tokens = line.split()
for i, token in enumerate(tokens):
header_to_column[token] = i
column_to_header[i] = token
continue
tokens = line.split()
step = int(tokens[header_to_column['STEP']])
params[step] = {
column_to_header[k]: token for k, token in enumerate(tokens)}
return params
def ReadACAnalysisFile(file_name):
if not os.path.exists(file_name):
return {}
ac_analysis = collections.defaultdict(dict)
print(f'reading {file_name}')
with open(file_name) as f:
last_freq_hz = None
step = -1
read_parameters = False
for line in f:
if line.strip() == '[Network Data]':
read_parameters = True
continue
if line.strip()== '[End]':
read_parameters = False
continue
if line.startswith('!'):
continue
if read_parameters:
tokens = line.split()
freq_hz = float(tokens[0])
if last_freq_hz is None or freq_hz < last_freq_hz:
# We've moved onto the next step.
step += 1
last_freq_hz = freq_hz
ac_analysis[step][freq_hz] = SmallSignalParameters(tokens[1:])
return ac_analysis