Skip to content

Commit 4a3e196

Browse files
committed
Change to ratiometric update
change to ratiometric update
1 parent a4b6799 commit 4a3e196

File tree

2 files changed

+28
-22
lines changed

2 files changed

+28
-22
lines changed

LTspice_opt.py

Lines changed: 24 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
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('\ncurrent 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**************\nEntering 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************\nDONE! 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}')

example2_setup.py

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -20,11 +20,10 @@ def setTarget(freqx, match_mode):
2020
deltai = f1i - f1ai
2121
rng = np.arange(f1ai, f1i)
2222
err_weights[rng] = 1 + 4 * (rng - f1ai) / deltai
23-
tmp = len(freqx)
24-
rng = np.arange(f2i,tmp)
25-
ll = len(rng);
26-
deltai = tmp-f2i
27-
err_weights[rng]=20- 10*(rng-rng[0]) / deltai
23+
#tmp = len(freqx)
24+
#rng = np.arange(f2i,tmp)
25+
#deltai = tmp-f2i
26+
#err_weights[rng]=20- 10*(rng-rng[0]) / deltai
2827

2928
# plot the target response and error weighting function
3029
plt.figure()

0 commit comments

Comments
 (0)