forked from huanlinberkeley/pycmg
-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_gradients.py
52 lines (40 loc) · 1.04 KB
/
test_gradients.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
import re
from bsimcmg import *
import tensorflow as tf
def read_mdl(file):
mdl = {}
with open(file,'r') as f:
lines = f.read().splitlines()
for line in lines:
param, value = re.split('[=\s]+', line)
mdl[param] = float(value)
return mdl
filepath = "modelcard_without_op.l"
param = read_mdl(filepath)
vd = tf.Variable(1.0)
vg = tf.Variable(1.0)
vs = tf.Variable(0.0)
vb = tf.Variable(0.0)
param['vd'] = vd
param['vg'] = vg
param['vs'] = vs
param['vb'] = vb
# Newton iteration on DC model requires gradients.
# Use tf autodiff to get these for free
with tf.GradientTape() as tape:
Id, Ig, Is, Ib = BSIMCMG(**param).calc()
print(f'Id = {Id:>16.9e} A')
print(f'Ig = {Ig:>16.9e} A')
print(f'Is = {Is:>16.9e} A')
print(f'Ib = {Ib:>16.9e} A')
# dId_dvg as an example
# 16 combinations can be retrieved in the same way
gm = tape.gradient(Id, vg)
print(f'gm = {gm.numpy()*1000:>16.9e} mS')
'''
Id = 3.592760768e-04 A
Ig = 0.000000000e+00 A
Is = -3.592760768e-04 A
Ib = 0.000000000e+00 A
gm = 1.375134569e+00 mS
'''