Advertisement
toweber

generate_simulate_meas_cost_optimize_ngspice_ann

Sep 12th, 2022
767
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.70 KB | None | 0 0
  1. import os
  2. import ipdb # ipdb.set_trace()
  3. import numpy as np
  4. import matplotlib.pyplot as plt
  5. import re
  6. from my_optimization import *
  7.  
  8.  
  9. def generate_netlist(R2):
  10.     print("Generating netlist")
  11.     cirfile="circuit/generated_netlist.net"
  12.  
  13.     f = open(cirfile,"w")
  14.     f.write("* Generated Netlist \n")
  15.     f.write("V1 in1 0 PWL file=pwl_input_xor_v1.txt\n")
  16.     f.write("V§W1 w2_l1n0 0 0\n")
  17.     f.write("V2 vreflow 0 0.5\n")
  18.     f.write("V3 vrefmid 0 1.2\n")
  19.     f.write("V§W2 w1_l1n0 0 5\n")
  20.     f.write("V4 in2 0 PWL file=pwl_input_xor_v2.txt\n")
  21.     f.write("XX1 in1 w1_l1n0 w2_l1n0 out_l1n0 vreflow vrefmid in2 two_input_neuron\n")
  22.     f.write("XX2 in1 w1_l1n1 w2_l1n1 out_l1n1 vreflow vrefmid in2 two_input_neuron\n")
  23.     f.write("XX3 out_l1n0 w1_l2n0 w2_l2n0 out1 vreflow vrefmid out_l1n1 two_input_neuron\n")
  24.     f.write("V§W3 w2_l1n1 0 5\n")
  25.     f.write("V§W4 w1_l1n1 0 -5\n")
  26.     f.write("V§W5 w2_l2n0 0 5\n")
  27.     f.write("V§W6 w1_l2n0 0 -5\n")
  28.     f.write("\n")
  29.     f.write("* block symbol definitions\n")
  30.     f.write(".subckt two_input_neuron in1 w1 w2 out vreflow vrefmid in2\n")
  31.     f.write("XU1 N001 N005 wsum opamp Aol=100K GBW=10Meg\n")
  32.     f.write("R1 N005 vreflow 100\n")
  33.     f.write("R2 wsum N001 100\n")
  34.     f.write("M1 N005 w1 in1 0 N_1u l=1u w=3u\n")
  35.     f.write("M2 N001 w1n in1 0 N_1u l=1u w=3u\n")
  36.     f.write("XU2 N002 N004 N003 opamp Aol=100K GBW=10Meg\n")
  37.     f.write("R3 N002 vreflow 1k\n")
  38.     f.write("R4 N003 N002 20k\n")
  39.     f.write("R5 N004 wsum 1k\n")
  40.     f.write("R6 N004 vrefmid 20k\n")
  41.     f.write("D1 out vrefmid D\n")
  42.     f.write("D2 vrefmid out D\n")
  43.     f.write("R7 out N003 100\n")
  44.     f.write("M3 N001 w2n in2 0 N_1u l=1u w=3u\n")
  45.     f.write("M4 N005 w2 in2 0 N_1u l=1u w=3u\n")
  46.     f.write("B1 W1n 0 V=-V(W1)\n")
  47.     f.write("B2 W2n 0 V=-V(W2)\n")
  48.     f.write(".lib opamp.sub\n")
  49.     f.write(".include cmosedu_models.txt\n")
  50.     f.write(".ends two_input_neuron\n")
  51.     f.write(".model D D\n")
  52.     #f.write(".lib C:\users\tweber\My Documents\LTspiceXVII\lib\cmp\standard.dio \n")
  53.     f.write(".lib standard.dio \n")
  54.     f.write(".model NMOS NMOS\n")
  55.     f.write(".model PMOS PMOS\n")
  56.     #f.write(".lib C:\users\tweber\My Documents\LTspiceXVII\libx1\cmp\standard.mos \n")
  57.     f.write(".lib standard.mos \n")
  58.     f.write(".include cmosedu_models.txt\n")
  59.     f.write(".tran 40ms\n")
  60.     f.write(".end\n")
  61.     f.close()
  62.  
  63.  
  64. def meas_result():
  65.     print("Reading log file")
  66.     logfile="generated_netlist.log"
  67.     with open(logfile) as f:
  68.         lines = f.readlines()        
  69.     f.close()
  70.  
  71.     for l in lines:
  72.         #print(l)
  73.         if 'vsaida' in l:
  74.             match =re.search(r"=(.*) at",l)  # LTSPICE
  75.             #match =re.search(r"=  (.*)",l)  # ngspice
  76.             vout = float(match.group(1))
  77.             return vout
  78.  
  79. def simulate():
  80.     print("Simulating")
  81.     #os.system('ngspice -b circuit/generated_netlist.net > generated_netlist.log')
  82.     os.system('ltspice -b circuit/generated_netlist.net')
  83.  
  84. def cost_function(x):
  85.     print("Evaluating cost function")
  86.     #generate, simulate and read log
  87.     generate_netlist(x)
  88.     simulate()
  89.     voltage = meas_result()
  90.  
  91.     #calculate cost
  92.     cost = abs(5-voltage)
  93.     return cost
  94.  
  95.  
  96.  
  97. # OPTIMIZATION
  98.  
  99. size_x = 1
  100. bound_i = (100,20000)
  101. bounds = []
  102. for i in range (0, size_x):
  103.       bounds.append(bound_i)
  104.  
  105. x0 = np.array([3000])
  106.  
  107. maxiter = 100
  108. step = 1000
  109. args = []
  110.  
  111. cost_list = []
  112. number_of_optimizations = 1
  113.  
  114.  
  115. res = my_hillclimbing(cost_function, x0, bounds, maxiter, step, args)
  116. print("Final x: [",)
  117. print("%f" % res.x[0],)
  118. for i in range(1,len(res.x)):
  119.     print(", %f" % res.x[i],)
  120.     print("]\n",)
  121.     #print res.x
  122. print("Final cost: %f \n" % res.cost)    
  123.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement