comparison sim.py @ 11:8d34a9eec184

Split out cost function so we can re-run it on DB data to allow faster adjustment.
author Daniel O'Connor <darius@dons.net.au>
date Sat, 18 Nov 2023 10:37:35 +1030
parents 2832aefd442c
children 49939e179c86
comparison
equal deleted inserted replaced
10:2832aefd442c 11:8d34a9eec184
50 ipeak_u5 = log.get_measure_value('ipeak_u5') 50 ipeak_u5 = log.get_measure_value('ipeak_u5')
51 ipeak = max(ipeak_u2, ipeak_u5) 51 ipeak = max(ipeak_u2, ipeak_u5)
52 52
53 return then, taken, power, eff, thd, ipeak 53 return then, taken, power, eff, thd, ipeak
54 54
55 55 def calccost(power, eff, thd, ipeak):
56 def fn(v, cur, simexe, simflags, rundir, circ, ctr):
57 '''Called by differential_evolution, v contains the evolved parameters, the rest are passed in from the args parameter'.
58 Returns the cost value which is being minimised'''
59
60 # Get parameters for run
61 duty, c1, c2, l1, l2 = v
62
63 cur.execute('SELECT cost, run, power, efficiency, thd, ipeak FROM GAN190 WHERE duty = ? AND c1 = ? AND c2 = ? AND l1 = ? AND l2 = ?', (duty, c1, c2, l1, l2))
64 tmp = cur.fetchone()
65 if tmp is not None:
66 cost, run, power, eff, thd, ipeak = tmp
67 print(f'Found run {run:3d}: Duty {duty:3.0f}%, C1 {c1:3.0f}pF, C2 {c2:3.0f}pF, L1 {l1:3.0f}uH, L2 {l2:4.0f}nH -> Power: {power:6.1f}W Efficiency: {eff:5.1f}% THD: {thd:5.1f}% IPeak: {ipeak:4.1f}A Cost: {cost:6.2f}')
68 return cost
69
70 # Get next run number
71 run = next(ctr)
72
73 # Run the simulation
74 # Need to convert units to suit
75 runname = pathlib.Path(circ).stem + '-' + str(run) + '.net'
76 then, taken, power, eff, thd, ipeak = simulate(circ, simexe, simflags, rundir, runname,
77 {'dutypct' : duty, 'C1' : c1 * 1e-12, 'C2' : c2 * 1e-12, 'L1' : l1 * 1e-6, 'L2' : l2 * 1e-9})
78
79 # Calculate the cost 56 # Calculate the cost
80 tpwr = 2000 57 tpwr = 1500
81 tthd = 2 58 tthd = 2
82 teff = 90 59 teff = 90
83 imax = 11 60 imax = 11
84 61
85 cost = 0 62 cost = 0
86 if power < tpwr / 2: 63 if power < tpwr * 0.80:
87 cost += 100 64 cost += 100
88 elif power < tpwr: 65 elif power < tpwr:
89 cost += (tpwr - power) / tpwr / 100 66 cost += (tpwr - power) / tpwr / 100
90 67
91 if thd > 5 * tthd: 68 if thd > 5 * tthd:
97 cost += (tthdinv - thdinv) / tthdinv 74 cost += (tthdinv - thdinv) / tthdinv
98 if eff < teff: 75 if eff < teff:
99 cost += (teff - eff) / teff 76 cost += (teff - eff) / teff
100 77
101 if ipeak > imax: 78 if ipeak > imax:
102 cost += (ipeak - imax) * 2 79 cost += (ipeak - imax) * 5
80
81 return cost
82
83 def fn(v, cur, simexe, simflags, rundir, circ, ctr):
84 '''Called by differential_evolution, v contains the evolved parameters, the rest are passed in from the args parameter'.
85 Returns the cost value which is being minimised'''
86
87 # Get parameters for run
88 duty, c1, c2, l1, l2 = v
89
90 # Check if this combination has already been tried
91 cur.execute('SELECT run, power, efficiency, thd, ipeak FROM GAN190 WHERE duty = ? AND c1 = ? AND c2 = ? AND l1 = ? AND l2 = ?', (duty, c1, c2, l1, l2))
92 tmp = cur.fetchone()
93 if tmp is not None:
94 run, power, eff, thd, ipeak = tmp
95 # Recalculate the cost since it is cheap and might have been tweaked since the original run
96 cost = calccost(power, eff, thd, ipeak)
97 print(f'Found run {run:3d}: Duty {duty:3.0f}%, C1 {c1:3.0f}pF, C2 {c2:3.0f}pF, L1 {l1:3.0f}uH, L2 {l2:4.0f}nH -> Power: {power:6.1f}W Efficiency: {eff:5.1f}% THD: {thd:5.1f}% IPeak: {ipeak:4.1f}A Cost: {cost:6.2f}')
98 return cost
99
100 # Get next run number
101 run = next(ctr)
102
103 # Run the simulation
104 # Need to convert units to suit
105 runname = pathlib.Path(circ).stem + '-' + str(run) + '.net'
106 then, taken, power, eff, thd, ipeak = simulate(circ, simexe, simflags, rundir, runname,
107 {'dutypct' : duty, 'C1' : c1 * 1e-12, 'C2' : c2 * 1e-12, 'L1' : l1 * 1e-6, 'L2' : l2 * 1e-9})
108 # Calculate the cost
109 cost = calccost(power, eff, thd, ipeak)
103 110
104 # Log & save the results 111 # Log & save the results
105 print(f'Run {run:3d}: Duty {duty:3.0f}%, C1 {c1:3.0f}pF, C2 {c2:3.0f}pF, L1 {l1:3.0f}uH, L2 {l2:4.0f}nH -> Power: {power:6.1f}W Efficiency: {eff:5.1f}% THD: {thd:5.1f}% IPeak: {ipeak:4.1f}A Cost: {cost:6.2f}') 112 print(f'Run {run:3d}: Duty {duty:3.0f}%, C1 {c1:3.0f}pF, C2 {c2:3.0f}pF, L1 {l1:3.0f}uH, L2 {l2:4.0f}nH -> Power: {power:6.1f}W Efficiency: {eff:5.1f}% THD: {thd:5.1f}% IPeak: {ipeak:4.1f}A Cost: {cost:6.2f}')
106 taken = taken.seconds + taken.microseconds / 1e6 113 taken = taken.seconds + taken.microseconds / 1e6
107 cur.execute('INSERT INTO GAN190 (name, run, start, duration, duty, c1, c2, l1, l2, power, efficiency, thd, ipeak, cost) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)', 114 cur.execute('INSERT INTO GAN190 (name, run, start, duration, duty, c1, c2, l1, l2, power, efficiency, thd, ipeak, cost) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)',