Advertisement
toweber

generate_simulate_meas_cost

Sep 12th, 2022 (edited)
665
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.08 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.  
  7. def generate_netlist(R2):
  8.     cirfile="generated_netlist.net"
  9.     f = open(cirfile,"w")  
  10.     f.write("* Generated Netlist \n")
  11.     f.write("V1 N001 0 10 \n")
  12.     f.write("R1 N001 out 10k \n")
  13.     f.write("R2 out 0 %f \n" %R2)
  14.     f.write(".tran 1s \n")
  15.     f.write(".meas TRAN vsaida FIND V(out) at 1 \n")
  16.     f.write(".end \n")
  17.     f.close()
  18.  
  19.  
  20. def meas_result():
  21.     logfile="generated_netlist.log"
  22.     with open(logfile) as f:
  23.         lines = f.readlines()        
  24.     f.close()
  25.  
  26.     for l in lines:
  27.         #print(l)
  28.         if 'vsaida' in l:
  29.             match =re.search(r"=(.*) at",l)
  30.             vout = float(match.group(1))
  31.             return vout
  32.  
  33. def simulate():
  34.     os.system('ltspice -b generated_netlist.net')
  35.  
  36. def cost_function(x):
  37.     #generate, simulate and read log
  38.     generate_netlist(x)
  39.     simulate()
  40.     voltage = meas_result()
  41.  
  42.     #calculate cost
  43.     cost = abs(5-voltage)
  44.     return cost
  45.  
  46.  
  47. cost = cost_function(1000)
  48. print(cost)
  49.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement