Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- from __future__ import print_function
- from numpy import *
- import subprocess
- import re
- from scipy.optimize import minimize, show_options
- import os
- with open('nand.net', 'r') as f:
- source = f.read()
- TMP_NAME = 'tmp.net'
- def test(p, v=False):
- if v:
- print (' '.join('{0: 13.7f}'.format(p[i]) for i in range(len(p))), end='')
- if any(p < 0):
- a, b, c, d = nan, nan, nan, nan
- else:
- with open(TMP_NAME, 'w+') as f:
- f.write(source.format(**locals()))
- f.flush()
- p = subprocess.Popen(['ngspice', TMP_NAME], stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
- out = p.communicate(input='exit')[0]
- try:
- a = re.findall(r'tplh_b1af *?= ([\d\.\-e]*)', out)[0]
- b = re.findall(r'tphl_b1ar *?= ([\d\.\-e]*)', out)[0]
- c = re.findall(r'tphl_a1br *?= ([\d\.\-e]*)', out)[0]
- d = re.findall(r'tplh_a1bf *?= ([\d\.\-e]*)', out)[0]
- except:
- a, b, c, d = nan, nan, nan, nan
- t = array([a, b, c, d], dtype=float128)
- if v:
- print (' ', t)
- return t
- a1, b1, _, _ = test([1])
- def cost(p, v=False):
- return sum((test(p, v)[2:] - 2)**2)
- if __name__ == '__main__':
- x0 = array([2], dtype=float128)
- res = minimize(cost, x0, args=(True,), method='Nelder-Mead', options=dict(maxiter=160))
- print (res)
- os.remove(TMP_NAME)
- #5 2 nand
Advertisement
Add Comment
Please, Sign In to add comment