Mercurial > ~darius > hgwebdir.cgi > pa
annotate sim.py @ 20:3341ef03cb66
Add PWM modulator
author | Daniel O'Connor <darius@dons.net.au> |
---|---|
date | Mon, 27 Nov 2023 13:19:37 +1030 |
parents | 28475b505f1f |
children |
rev | line source |
---|---|
19 | 1 #!/usr/bin/env python3 |
2 | |
3 # Copyright (c) 2023 Daniel O'Connor <darius@dons.net.au | |
4 # | |
5 # Redistribution and use in source and binary forms, with or without | |
6 # modification, are permitted provided that the following conditions | |
7 # are met: | |
8 # 1. Redistributions of source code must retain the above copyright | |
9 # notice, this list of conditions and the following disclaimer. | |
10 # 2. Redistributions in binary form must reproduce the above copyright | |
11 # notice, this list of conditions and the following disclaimer in the | |
12 # documentation and/or other materials provided with the distribution. | |
13 | |
14 # THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND | |
15 # ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | |
16 # IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | |
17 # ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE | |
18 # FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | |
19 # DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | |
20 # OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | |
21 # HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | |
22 # LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | |
23 # OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | |
24 # SUCH DAMAGE. | |
25 | |
10
2832aefd442c
Add 'no driver' version which can simulate in normal mode.
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
26 import datetime |
2832aefd442c
Add 'no driver' version which can simulate in normal mode.
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
27 import pathlib |
2832aefd442c
Add 'no driver' version which can simulate in normal mode.
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
28 from scipy.optimize import differential_evolution |
2832aefd442c
Add 'no driver' version which can simulate in normal mode.
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
29 from spicelib.simulators.ltspice_simulator import LTspice |
2832aefd442c
Add 'no driver' version which can simulate in normal mode.
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
30 from spicelib.log.ltsteps import LTSpiceLogReader |
2832aefd442c
Add 'no driver' version which can simulate in normal mode.
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
31 from spicelib.editor.spice_editor import SpiceEditor |
2832aefd442c
Add 'no driver' version which can simulate in normal mode.
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
32 #from spicelib.sim.sim_runner import SimRunner |
14
ac2ff6be22f7
Take circuit name in top level function.
Daniel O'Connor <darius@dons.net.au>
parents:
12
diff
changeset
|
33 import shlex |
10
2832aefd442c
Add 'no driver' version which can simulate in normal mode.
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
34 import sqlite3 |
2832aefd442c
Add 'no driver' version which can simulate in normal mode.
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
35 import subprocess |
2832aefd442c
Add 'no driver' version which can simulate in normal mode.
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
36 |
2832aefd442c
Add 'no driver' version which can simulate in normal mode.
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
37 def simulate(src, simexe, simflags, rundir, runname, params): |
2832aefd442c
Add 'no driver' version which can simulate in normal mode.
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
38 src = pathlib.Path(src) |
2832aefd442c
Add 'no driver' version which can simulate in normal mode.
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
39 runname = pathlib.Path(runname) |
2832aefd442c
Add 'no driver' version which can simulate in normal mode.
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
40 assert(runname.suffix == '.net') |
2832aefd442c
Add 'no driver' version which can simulate in normal mode.
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
41 rundir = pathlib.Path(rundir) |
2832aefd442c
Add 'no driver' version which can simulate in normal mode.
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
42 |
2832aefd442c
Add 'no driver' version which can simulate in normal mode.
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
43 e = SpiceEditor(src) |
2832aefd442c
Add 'no driver' version which can simulate in normal mode.
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
44 for k in params: |
2832aefd442c
Add 'no driver' version which can simulate in normal mode.
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
45 e.set_parameter(k, params[k]) |
2832aefd442c
Add 'no driver' version which can simulate in normal mode.
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
46 |
2832aefd442c
Add 'no driver' version which can simulate in normal mode.
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
47 #s = SimRunner(simulator = LTspice, output_folder = 'tmp', verbose = True) |
2832aefd442c
Add 'no driver' version which can simulate in normal mode.
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
48 #s.simulator.spice_exe = [simexe] |
2832aefd442c
Add 'no driver' version which can simulate in normal mode.
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
49 #raw_file, log_file = s.run_now(e, run_filename = runname) |
2832aefd442c
Add 'no driver' version which can simulate in normal mode.
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
50 |
2832aefd442c
Add 'no driver' version which can simulate in normal mode.
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
51 e.write_netlist(rundir / runname) |
2832aefd442c
Add 'no driver' version which can simulate in normal mode.
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
52 |
2832aefd442c
Add 'no driver' version which can simulate in normal mode.
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
53 cmd = [simexe, '-b'] |
2832aefd442c
Add 'no driver' version which can simulate in normal mode.
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
54 cmd.extend(simflags) |
2832aefd442c
Add 'no driver' version which can simulate in normal mode.
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
55 cmd.append(runname.name) |
2832aefd442c
Add 'no driver' version which can simulate in normal mode.
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
56 then = datetime.datetime.now() |
17
f0665f53b854
Show FET power dissipation, don't bother with mod rail
Daniel O'Connor <darius@dons.net.au>
parents:
14
diff
changeset
|
57 #print(f'Starting run {runname} at {then}') |
10
2832aefd442c
Add 'no driver' version which can simulate in normal mode.
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
58 p = subprocess.Popen(cmd, cwd = rundir) |
2832aefd442c
Add 'no driver' version which can simulate in normal mode.
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
59 p.communicate() |
2832aefd442c
Add 'no driver' version which can simulate in normal mode.
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
60 now = datetime.datetime.now() |
2832aefd442c
Add 'no driver' version which can simulate in normal mode.
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
61 taken = now - then |
2832aefd442c
Add 'no driver' version which can simulate in normal mode.
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
62 |
2832aefd442c
Add 'no driver' version which can simulate in normal mode.
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
63 if p.returncode != 0: |
17
f0665f53b854
Show FET power dissipation, don't bother with mod rail
Daniel O'Connor <darius@dons.net.au>
parents:
14
diff
changeset
|
64 raise Exception(' '.join(map(shlex.quote, cmd)) + ' failed with code ' + str(p.returncode)) |
10
2832aefd442c
Add 'no driver' version which can simulate in normal mode.
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
65 |
2832aefd442c
Add 'no driver' version which can simulate in normal mode.
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
66 #rawpath = rundir / runname.with_suffix('.raw') |
2832aefd442c
Add 'no driver' version which can simulate in normal mode.
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
67 #raw = spicelib.RawRead(rawpath) |
2832aefd442c
Add 'no driver' version which can simulate in normal mode.
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
68 logpath = rundir / runname.with_suffix('.log') |
2832aefd442c
Add 'no driver' version which can simulate in normal mode.
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
69 log = LTSpiceLogReader(logpath) |
2832aefd442c
Add 'no driver' version which can simulate in normal mode.
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
70 |
2832aefd442c
Add 'no driver' version which can simulate in normal mode.
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
71 power = log.get_measure_value('pout') |
2832aefd442c
Add 'no driver' version which can simulate in normal mode.
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
72 eff = log.get_measure_value('efficiency') |
2832aefd442c
Add 'no driver' version which can simulate in normal mode.
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
73 thd = log.fourier['V(rfout)'][0].thd |
2832aefd442c
Add 'no driver' version which can simulate in normal mode.
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
74 ipeak_u2 = log.get_measure_value('ipeak_u2') |
2832aefd442c
Add 'no driver' version which can simulate in normal mode.
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
75 ipeak_u5 = log.get_measure_value('ipeak_u5') |
2832aefd442c
Add 'no driver' version which can simulate in normal mode.
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
76 ipeak = max(ipeak_u2, ipeak_u5) |
2832aefd442c
Add 'no driver' version which can simulate in normal mode.
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
77 |
2832aefd442c
Add 'no driver' version which can simulate in normal mode.
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
78 return then, taken, power, eff, thd, ipeak |
2832aefd442c
Add 'no driver' version which can simulate in normal mode.
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
79 |
11
8d34a9eec184
Split out cost function so we can re-run it on DB data to allow faster adjustment.
Daniel O'Connor <darius@dons.net.au>
parents:
10
diff
changeset
|
80 def calccost(power, eff, thd, ipeak): |
10
2832aefd442c
Add 'no driver' version which can simulate in normal mode.
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
81 # Calculate the cost |
11
8d34a9eec184
Split out cost function so we can re-run it on DB data to allow faster adjustment.
Daniel O'Connor <darius@dons.net.au>
parents:
10
diff
changeset
|
82 tpwr = 1500 |
10
2832aefd442c
Add 'no driver' version which can simulate in normal mode.
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
83 tthd = 2 |
2832aefd442c
Add 'no driver' version which can simulate in normal mode.
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
84 teff = 90 |
2832aefd442c
Add 'no driver' version which can simulate in normal mode.
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
85 imax = 11 |
2832aefd442c
Add 'no driver' version which can simulate in normal mode.
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
86 |
2832aefd442c
Add 'no driver' version which can simulate in normal mode.
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
87 cost = 0 |
11
8d34a9eec184
Split out cost function so we can re-run it on DB data to allow faster adjustment.
Daniel O'Connor <darius@dons.net.au>
parents:
10
diff
changeset
|
88 if power < tpwr * 0.80: |
10
2832aefd442c
Add 'no driver' version which can simulate in normal mode.
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
89 cost += 100 |
2832aefd442c
Add 'no driver' version which can simulate in normal mode.
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
90 elif power < tpwr: |
2832aefd442c
Add 'no driver' version which can simulate in normal mode.
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
91 cost += (tpwr - power) / tpwr / 100 |
2832aefd442c
Add 'no driver' version which can simulate in normal mode.
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
92 |
2832aefd442c
Add 'no driver' version which can simulate in normal mode.
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
93 if thd > 5 * tthd: |
2832aefd442c
Add 'no driver' version which can simulate in normal mode.
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
94 cost += 100 |
2832aefd442c
Add 'no driver' version which can simulate in normal mode.
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
95 else: |
2832aefd442c
Add 'no driver' version which can simulate in normal mode.
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
96 thdinv = 100 - thd |
2832aefd442c
Add 'no driver' version which can simulate in normal mode.
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
97 tthdinv = 100 - tthd |
2832aefd442c
Add 'no driver' version which can simulate in normal mode.
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
98 if thdinv < tthdinv: |
2832aefd442c
Add 'no driver' version which can simulate in normal mode.
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
99 cost += (tthdinv - thdinv) / tthdinv |
2832aefd442c
Add 'no driver' version which can simulate in normal mode.
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
100 if eff < teff: |
2832aefd442c
Add 'no driver' version which can simulate in normal mode.
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
101 cost += (teff - eff) / teff |
2832aefd442c
Add 'no driver' version which can simulate in normal mode.
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
102 |
2832aefd442c
Add 'no driver' version which can simulate in normal mode.
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
103 if ipeak > imax: |
11
8d34a9eec184
Split out cost function so we can re-run it on DB data to allow faster adjustment.
Daniel O'Connor <darius@dons.net.au>
parents:
10
diff
changeset
|
104 cost += (ipeak - imax) * 5 |
8d34a9eec184
Split out cost function so we can re-run it on DB data to allow faster adjustment.
Daniel O'Connor <darius@dons.net.au>
parents:
10
diff
changeset
|
105 |
8d34a9eec184
Split out cost function so we can re-run it on DB data to allow faster adjustment.
Daniel O'Connor <darius@dons.net.au>
parents:
10
diff
changeset
|
106 return cost |
8d34a9eec184
Split out cost function so we can re-run it on DB data to allow faster adjustment.
Daniel O'Connor <darius@dons.net.au>
parents:
10
diff
changeset
|
107 |
12
49939e179c86
Run 4 simulations at once.
Daniel O'Connor <darius@dons.net.au>
parents:
11
diff
changeset
|
108 def fn(v, dsn, simexe, simflags, rundir, circ): |
11
8d34a9eec184
Split out cost function so we can re-run it on DB data to allow faster adjustment.
Daniel O'Connor <darius@dons.net.au>
parents:
10
diff
changeset
|
109 '''Called by differential_evolution, v contains the evolved parameters, the rest are passed in from the args parameter'. |
8d34a9eec184
Split out cost function so we can re-run it on DB data to allow faster adjustment.
Daniel O'Connor <darius@dons.net.au>
parents:
10
diff
changeset
|
110 Returns the cost value which is being minimised''' |
8d34a9eec184
Split out cost function so we can re-run it on DB data to allow faster adjustment.
Daniel O'Connor <darius@dons.net.au>
parents:
10
diff
changeset
|
111 |
8d34a9eec184
Split out cost function so we can re-run it on DB data to allow faster adjustment.
Daniel O'Connor <darius@dons.net.au>
parents:
10
diff
changeset
|
112 # Get parameters for run |
8d34a9eec184
Split out cost function so we can re-run it on DB data to allow faster adjustment.
Daniel O'Connor <darius@dons.net.au>
parents:
10
diff
changeset
|
113 duty, c1, c2, l1, l2 = v |
8d34a9eec184
Split out cost function so we can re-run it on DB data to allow faster adjustment.
Daniel O'Connor <darius@dons.net.au>
parents:
10
diff
changeset
|
114 |
8d34a9eec184
Split out cost function so we can re-run it on DB data to allow faster adjustment.
Daniel O'Connor <darius@dons.net.au>
parents:
10
diff
changeset
|
115 # Check if this combination has already been tried |
12
49939e179c86
Run 4 simulations at once.
Daniel O'Connor <darius@dons.net.au>
parents:
11
diff
changeset
|
116 dbh = sqlite3.connect(dsn) |
49939e179c86
Run 4 simulations at once.
Daniel O'Connor <darius@dons.net.au>
parents:
11
diff
changeset
|
117 cur = dbh.cursor() |
14
ac2ff6be22f7
Take circuit name in top level function.
Daniel O'Connor <darius@dons.net.au>
parents:
12
diff
changeset
|
118 cur.execute('SELECT run, power, efficiency, thd, ipeak FROM GAN190 WHERE name = ? AND duty = ? AND c1 = ? AND c2 = ? AND l1 = ? AND l2 = ?', |
ac2ff6be22f7
Take circuit name in top level function.
Daniel O'Connor <darius@dons.net.au>
parents:
12
diff
changeset
|
119 (circ, duty, c1, c2, l1, l2)) |
11
8d34a9eec184
Split out cost function so we can re-run it on DB data to allow faster adjustment.
Daniel O'Connor <darius@dons.net.au>
parents:
10
diff
changeset
|
120 tmp = cur.fetchone() |
8d34a9eec184
Split out cost function so we can re-run it on DB data to allow faster adjustment.
Daniel O'Connor <darius@dons.net.au>
parents:
10
diff
changeset
|
121 if tmp is not None: |
8d34a9eec184
Split out cost function so we can re-run it on DB data to allow faster adjustment.
Daniel O'Connor <darius@dons.net.au>
parents:
10
diff
changeset
|
122 run, power, eff, thd, ipeak = tmp |
8d34a9eec184
Split out cost function so we can re-run it on DB data to allow faster adjustment.
Daniel O'Connor <darius@dons.net.au>
parents:
10
diff
changeset
|
123 # Recalculate the cost since it is cheap and might have been tweaked since the original run |
8d34a9eec184
Split out cost function so we can re-run it on DB data to allow faster adjustment.
Daniel O'Connor <darius@dons.net.au>
parents:
10
diff
changeset
|
124 cost = calccost(power, eff, thd, ipeak) |
8d34a9eec184
Split out cost function so we can re-run it on DB data to allow faster adjustment.
Daniel O'Connor <darius@dons.net.au>
parents:
10
diff
changeset
|
125 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}') |
8d34a9eec184
Split out cost function so we can re-run it on DB data to allow faster adjustment.
Daniel O'Connor <darius@dons.net.au>
parents:
10
diff
changeset
|
126 return cost |
8d34a9eec184
Split out cost function so we can re-run it on DB data to allow faster adjustment.
Daniel O'Connor <darius@dons.net.au>
parents:
10
diff
changeset
|
127 |
8d34a9eec184
Split out cost function so we can re-run it on DB data to allow faster adjustment.
Daniel O'Connor <darius@dons.net.au>
parents:
10
diff
changeset
|
128 # Get next run number |
12
49939e179c86
Run 4 simulations at once.
Daniel O'Connor <darius@dons.net.au>
parents:
11
diff
changeset
|
129 cur.execute('BEGIN DEFERRED') |
49939e179c86
Run 4 simulations at once.
Daniel O'Connor <darius@dons.net.au>
parents:
11
diff
changeset
|
130 cur.execute('INSERT INTO GAN190 DEFAULT VALUES RETURNING rowid') |
49939e179c86
Run 4 simulations at once.
Daniel O'Connor <darius@dons.net.au>
parents:
11
diff
changeset
|
131 run = cur.fetchone()[0] |
49939e179c86
Run 4 simulations at once.
Daniel O'Connor <darius@dons.net.au>
parents:
11
diff
changeset
|
132 cur.execute('COMMIT') |
11
8d34a9eec184
Split out cost function so we can re-run it on DB data to allow faster adjustment.
Daniel O'Connor <darius@dons.net.au>
parents:
10
diff
changeset
|
133 |
8d34a9eec184
Split out cost function so we can re-run it on DB data to allow faster adjustment.
Daniel O'Connor <darius@dons.net.au>
parents:
10
diff
changeset
|
134 # Run the simulation |
8d34a9eec184
Split out cost function so we can re-run it on DB data to allow faster adjustment.
Daniel O'Connor <darius@dons.net.au>
parents:
10
diff
changeset
|
135 # Need to convert units to suit |
8d34a9eec184
Split out cost function so we can re-run it on DB data to allow faster adjustment.
Daniel O'Connor <darius@dons.net.au>
parents:
10
diff
changeset
|
136 runname = pathlib.Path(circ).stem + '-' + str(run) + '.net' |
17
f0665f53b854
Show FET power dissipation, don't bother with mod rail
Daniel O'Connor <darius@dons.net.au>
parents:
14
diff
changeset
|
137 try: |
f0665f53b854
Show FET power dissipation, don't bother with mod rail
Daniel O'Connor <darius@dons.net.au>
parents:
14
diff
changeset
|
138 then, taken, power, eff, thd, ipeak = simulate(circ, simexe, simflags, rundir, runname, |
f0665f53b854
Show FET power dissipation, don't bother with mod rail
Daniel O'Connor <darius@dons.net.au>
parents:
14
diff
changeset
|
139 {'dutypct' : duty, 'C1' : c1 * 1e-12, 'C2' : c2 * 1e-12, 'L1' : l1 * 1e-6, 'L2' : l2 * 1e-9}) |
f0665f53b854
Show FET power dissipation, don't bother with mod rail
Daniel O'Connor <darius@dons.net.au>
parents:
14
diff
changeset
|
140 except: |
f0665f53b854
Show FET power dissipation, don't bother with mod rail
Daniel O'Connor <darius@dons.net.au>
parents:
14
diff
changeset
|
141 return 100000 |
11
8d34a9eec184
Split out cost function so we can re-run it on DB data to allow faster adjustment.
Daniel O'Connor <darius@dons.net.au>
parents:
10
diff
changeset
|
142 # Calculate the cost |
8d34a9eec184
Split out cost function so we can re-run it on DB data to allow faster adjustment.
Daniel O'Connor <darius@dons.net.au>
parents:
10
diff
changeset
|
143 cost = calccost(power, eff, thd, ipeak) |
10
2832aefd442c
Add 'no driver' version which can simulate in normal mode.
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
144 |
2832aefd442c
Add 'no driver' version which can simulate in normal mode.
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
145 # Log & save the results |
2832aefd442c
Add 'no driver' version which can simulate in normal mode.
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
146 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}') |
2832aefd442c
Add 'no driver' version which can simulate in normal mode.
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
147 taken = taken.seconds + taken.microseconds / 1e6 |
12
49939e179c86
Run 4 simulations at once.
Daniel O'Connor <darius@dons.net.au>
parents:
11
diff
changeset
|
148 cur.execute('BEGIN DEFERRED') |
49939e179c86
Run 4 simulations at once.
Daniel O'Connor <darius@dons.net.au>
parents:
11
diff
changeset
|
149 cur.execute('REPLACE INTO GAN190 (name, run, start, duration, duty, c1, c2, l1, l2, power, efficiency, thd, ipeak, cost) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)', |
14
ac2ff6be22f7
Take circuit name in top level function.
Daniel O'Connor <darius@dons.net.au>
parents:
12
diff
changeset
|
150 (circ, run, then, taken, duty, c1, c2, l1, l2, power, eff, thd, ipeak, cost)) |
10
2832aefd442c
Add 'no driver' version which can simulate in normal mode.
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
151 cur.execute('COMMIT') |
2832aefd442c
Add 'no driver' version which can simulate in normal mode.
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
152 |
2832aefd442c
Add 'no driver' version which can simulate in normal mode.
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
153 return cost |
2832aefd442c
Add 'no driver' version which can simulate in normal mode.
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
154 |
14
ac2ff6be22f7
Take circuit name in top level function.
Daniel O'Connor <darius@dons.net.au>
parents:
12
diff
changeset
|
155 def ev(circ): |
10
2832aefd442c
Add 'no driver' version which can simulate in normal mode.
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
156 # Bounds for parameters |
12
49939e179c86
Run 4 simulations at once.
Daniel O'Connor <darius@dons.net.au>
parents:
11
diff
changeset
|
157 # Note that the parameters are also constrained to be integral |
10
2832aefd442c
Add 'no driver' version which can simulate in normal mode.
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
158 bounds = [(10, 80), |
2832aefd442c
Add 'no driver' version which can simulate in normal mode.
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
159 (1, 20), |
2832aefd442c
Add 'no driver' version which can simulate in normal mode.
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
160 (10, 300), |
2832aefd442c
Add 'no driver' version which can simulate in normal mode.
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
161 (1, 20), |
2832aefd442c
Add 'no driver' version which can simulate in normal mode.
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
162 (10, 500)] |
12
49939e179c86
Run 4 simulations at once.
Daniel O'Connor <darius@dons.net.au>
parents:
11
diff
changeset
|
163 |
10
2832aefd442c
Add 'no driver' version which can simulate in normal mode.
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
164 # Initial solution |
2832aefd442c
Add 'no driver' version which can simulate in normal mode.
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
165 x0 = [36, 10, 155, 5, 140] |
12
49939e179c86
Run 4 simulations at once.
Daniel O'Connor <darius@dons.net.au>
parents:
11
diff
changeset
|
166 |
49939e179c86
Run 4 simulations at once.
Daniel O'Connor <darius@dons.net.au>
parents:
11
diff
changeset
|
167 # Where to save results |
49939e179c86
Run 4 simulations at once.
Daniel O'Connor <darius@dons.net.au>
parents:
11
diff
changeset
|
168 dsn = 'results.db' |
49939e179c86
Run 4 simulations at once.
Daniel O'Connor <darius@dons.net.au>
parents:
11
diff
changeset
|
169 dbh = sqlite3.connect(dsn) |
10
2832aefd442c
Add 'no driver' version which can simulate in normal mode.
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
170 cur = dbh.cursor() |
2832aefd442c
Add 'no driver' version which can simulate in normal mode.
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
171 cur.execute(''' |
2832aefd442c
Add 'no driver' version which can simulate in normal mode.
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
172 CREATE TABLE IF NOT EXISTS GAN190 ( |
2832aefd442c
Add 'no driver' version which can simulate in normal mode.
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
173 name TEXT, -- Circuit name |
2832aefd442c
Add 'no driver' version which can simulate in normal mode.
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
174 run INTEGER, -- Run number |
2832aefd442c
Add 'no driver' version which can simulate in normal mode.
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
175 start DATETIME, -- Datetime run started |
2832aefd442c
Add 'no driver' version which can simulate in normal mode.
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
176 duration REAL, -- Length of run (seconds) |
2832aefd442c
Add 'no driver' version which can simulate in normal mode.
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
177 duty REAL, -- Duty cyle (%) |
2832aefd442c
Add 'no driver' version which can simulate in normal mode.
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
178 c1 REAL, -- Value of C1 (pF) |
2832aefd442c
Add 'no driver' version which can simulate in normal mode.
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
179 c2 REAL, -- Value of C2 (pF) |
2832aefd442c
Add 'no driver' version which can simulate in normal mode.
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
180 l1 REAL, -- Value of L1 (uH) |
2832aefd442c
Add 'no driver' version which can simulate in normal mode.
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
181 l2 REAL, -- Value of L2 (nH) |
2832aefd442c
Add 'no driver' version which can simulate in normal mode.
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
182 power REAL, -- Measured power (Watts) |
2832aefd442c
Add 'no driver' version which can simulate in normal mode.
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
183 efficiency REAL, -- Measured efficiency (%) |
2832aefd442c
Add 'no driver' version which can simulate in normal mode.
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
184 thd REAL, -- Total harmonic distortion (%) |
2832aefd442c
Add 'no driver' version which can simulate in normal mode.
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
185 ipeak REAL, -- Peak drain current (A) |
2832aefd442c
Add 'no driver' version which can simulate in normal mode.
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
186 cost REAL -- Calculated cost metric |
2832aefd442c
Add 'no driver' version which can simulate in normal mode.
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
187 );''') |
12
49939e179c86
Run 4 simulations at once.
Daniel O'Connor <darius@dons.net.au>
parents:
11
diff
changeset
|
188 |
10
2832aefd442c
Add 'no driver' version which can simulate in normal mode.
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
189 return differential_evolution(fn, bounds, x0 = x0, |
14
ac2ff6be22f7
Take circuit name in top level function.
Daniel O'Connor <darius@dons.net.au>
parents:
12
diff
changeset
|
190 args = (dsn, '/Users/oconnd1/bin/runltspice', [], 'tmp', circ), |
12
49939e179c86
Run 4 simulations at once.
Daniel O'Connor <darius@dons.net.au>
parents:
11
diff
changeset
|
191 integrality = True, disp = True, seed = 12345, updating = 'deferred', workers = 4) |