99## See any of the example files in this distribution for an example
1010## Change the following line to point to your own setup file.
1111
12- from example1_setup import simControl , setTarget
12+ from example2_setup import simControl , setTarget
1313
1414# notes:
1515
@@ -85,11 +85,13 @@ def optLTspice(optParams, *args, **kwargs): # this is the evaluation function ca
8585
8686
8787 for k in range (numOptd ):
88- netlist [OptLine [k ]][3 ] = f'{ optParams [k ] * nomParams [k ]:.12e} '
88+ #netlist[OptLine[k]][3] = f'{optParams[k] * nomParams[k]:.12e}'
89+ netlist [OptLine [k ]][3 ] = f'{ nomParams [k ]* np .exp (optParams [k ]):.12e} '
8990
9091 print ('\n current component values' )
9192 for k in range (numOptd ):
92- print (f'{ netlist [OptLine [k ]][0 ]} { optParams [k ] * nomParams [k ]:.12e} ' )
93+ #print(f'{netlist[OptLine[k]][0]} {optParams[k] * nomParams[k]:.12e}')
94+ print (f'{ netlist [OptLine [k ]][0 ]} { nomParams [k ] * np .exp (optParams [k ]):.12e} ' )
9395
9496 with open (netlist_fname , 'w' ) as fid_wr_netlist :
9597 for k in range (numlines_netlist ):
@@ -220,7 +222,9 @@ def update_schematic(pass2schem, simctrl):
220222 xx = simctrlOptInstNames .index (instNm )
221223 # Next line has the value to change
222224 changeNext = True
223- instValNext = X [kk ] * nomParams [kk ]
225+ #instValNext = X[kk] * nomParams[kk]
226+ instValNext = nomParams [kk ] * np .exp (X [kk ])
227+
224228 roundStringNext = simctrlInstTol [xx ]
225229
226230 new_schem .append (' ' .join (line ) + '\n ' )
@@ -359,7 +363,6 @@ def main():
359363 simControlMaxVals = simControlData [5 ]
360364 simControlInstTol = simControlData [6 ]
361365 simControlOPtInstNames = simControlData [3 ]
362- #LTspice_simTime = simControlData[7] # How long to wait for LTspice to finish sim
363366 LTspice_output_node = simControlData [7 ]
364367 matchMode = simControlData [8 ] # 1 = ampl only, 2 = phase only, 3 = both
365368
@@ -476,8 +479,7 @@ def main():
476479 # need to search through the entire netlist each time
477480 numOptd = len (simControlOPtInstName ) # Number of instances being optimized
478481 OptLine = [0 ] * numOptd # An array that points to the netlist lines with the instance names to be optimized
479- UB = [0.0 ] * numOptd # Upper bound for optimizer
480- LB = [0.0 ] * numOptd # Lower bound for optimizer
482+
481483
482484 kkk = 1
483485 OptLine = [0 ] * numOptd # Initialize the OptLine array
@@ -544,15 +546,18 @@ def main():
544546
545547 print ('\n ****************\n **************\n Entering Optimization Loop, please be patient ...\n ************\n ***********\n ' )
546548
547- UB = [ub / nom for ub , nom in zip (UB , nomParams )] # Translate upper bounds into relative upper bounds
548- LB = [lb / nom for lb , nom in zip (LB , nomParams )] # Translate lower bounds into relative lower bounds
549-
550- optParams = np .ones (numOptd )
551- # note the starting values are all 1's. That's because we multiply the least-sq input
552- # starting values by the initial component values inside the evaluation function.
553- # I do this because the component values have an extreme range (from 1e-12 to 1e8 or so)
554- # this makes it hard for the least-sq function to calculate an appropriate step size
555- # when computing derivatives
549+ #UB = [ub / nom for ub, nom in zip(UB, nomParams)] # Translate upper bounds into relative upper bounds
550+ #LB = [lb / nom for lb, nom in zip(LB, nomParams)] # Translate lower bounds into relative lower bounds
551+ # val = nom_val*exp(X), log(val)=X+log(nom_val), X = log(val)-log(nom_val), Xmax=log(val_max)-log(nom_val)
552+ # Xmin = log(val_min)-log(nom_val)
553+ for k in range (numOptd ):
554+ UB [k ] = np .log (UB [k ]) - np .log (nomParams [k ])
555+ LB [k ] = np .log (LB [k ]) - np .log (nomParams [k ])
556+
557+ #optParams = np.ones(numOptd)
558+ optParams = np .zeros (numOptd ) # start at 0 for ratiometric, because e^0 = 1
559+ # note the starting values are all 0's. The actual component values
560+ # are current_Val=starting_Val*exp(X) where X is the optimizer variable.
556561
557562 # run least-squares, step size is set to 0.1% to make sure
558563 # that the ltspice sim actually can see a difference when the components are wiggled
@@ -565,7 +570,9 @@ def main():
565570
566571 print ('\n *************\n ************\n DONE! Generating outputs ...\n ***********\n *********\n ' )
567572 for k in range (numOptd ):
568- print (f'{ netlist [OptLine [k ]][0 ]} { X [k ] * nomParams [k ]:2.12e} ' )
573+ #print(f'{netlist[OptLine[k]][0]} {X[k] * nomParams[k]:2.12e}')
574+ print (f'{ netlist [OptLine [k ]][0 ]} { nomParams [k ] * np .exp (X [k ]):2.12e} ' )
575+
569576 time .sleep (0.1 )
570577 # Re-run simulation with current netlist
571578 print (f'Issuing command to run post-opt LTspice simulation\n { RunLTstring } ' )
0 commit comments