Mercurial > ~darius > hgwebdir.cgi > pa
annotate sim.py @ 13:7cdfaa0b5ad2
Add GAN140-650EBE model
author | Daniel O'Connor <darius@dons.net.au> |
---|---|
date | Sat, 18 Nov 2023 17:57:00 +1030 |
parents | 49939e179c86 |
children | ac2ff6be22f7 |
rev | line source |
---|---|
10
2832aefd442c
Add 'no driver' version which can simulate in normal mode.
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
1 import datetime |
2832aefd442c
Add 'no driver' version which can simulate in normal mode.
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
2 import pathlib |
2832aefd442c
Add 'no driver' version which can simulate in normal mode.
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
3 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
|
4 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
|
5 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
|
6 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
|
7 #from spicelib.sim.sim_runner import SimRunner |
2832aefd442c
Add 'no driver' version which can simulate in normal mode.
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
8 #import shlex |
2832aefd442c
Add 'no driver' version which can simulate in normal mode.
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
9 import sqlite3 |
2832aefd442c
Add 'no driver' version which can simulate in normal mode.
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
10 import subprocess |
2832aefd442c
Add 'no driver' version which can simulate in normal mode.
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
11 |
2832aefd442c
Add 'no driver' version which can simulate in normal mode.
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
12 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
|
13 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
|
14 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
|
15 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
|
16 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
|
17 |
2832aefd442c
Add 'no driver' version which can simulate in normal mode.
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
18 e = SpiceEditor(src) |
2832aefd442c
Add 'no driver' version which can simulate in normal mode.
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
19 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
|
20 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
|
21 |
2832aefd442c
Add 'no driver' version which can simulate in normal mode.
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
22 #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
|
23 #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
|
24 #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
|
25 |
2832aefd442c
Add 'no driver' version which can simulate in normal mode.
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
26 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
|
27 |
2832aefd442c
Add 'no driver' version which can simulate in normal mode.
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
28 cmd = [simexe, '-b'] |
2832aefd442c
Add 'no driver' version which can simulate in normal mode.
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
29 cmd.extend(simflags) |
2832aefd442c
Add 'no driver' version which can simulate in normal mode.
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
30 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
|
31 #print(' '.join(map(shlex.quote, cmd))) |
2832aefd442c
Add 'no driver' version which can simulate in normal mode.
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
32 then = datetime.datetime.now() |
12
49939e179c86
Run 4 simulations at once.
Daniel O'Connor <darius@dons.net.au>
parents:
11
diff
changeset
|
33 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
|
34 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
|
35 p.communicate() |
2832aefd442c
Add 'no driver' version which can simulate in normal mode.
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
36 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
|
37 taken = now - then |
2832aefd442c
Add 'no driver' version which can simulate in normal mode.
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
38 |
2832aefd442c
Add 'no driver' version which can simulate in normal mode.
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
39 if p.returncode != 0: |
2832aefd442c
Add 'no driver' version which can simulate in normal mode.
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
40 raise Exception('failed') |
2832aefd442c
Add 'no driver' version which can simulate in normal mode.
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
41 |
2832aefd442c
Add 'no driver' version which can simulate in normal mode.
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
42 #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
|
43 #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
|
44 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
|
45 log = LTSpiceLogReader(logpath) |
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 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
|
48 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
|
49 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
|
50 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
|
51 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
|
52 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
|
53 |
2832aefd442c
Add 'no driver' version which can simulate in normal mode.
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
54 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
|
55 |
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
|
56 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
|
57 # 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
|
58 tpwr = 1500 |
10
2832aefd442c
Add 'no driver' version which can simulate in normal mode.
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
59 tthd = 2 |
2832aefd442c
Add 'no driver' version which can simulate in normal mode.
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
60 teff = 90 |
2832aefd442c
Add 'no driver' version which can simulate in normal mode.
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
61 imax = 11 |
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 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
|
64 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
|
65 cost += 100 |
2832aefd442c
Add 'no driver' version which can simulate in normal mode.
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
66 elif power < tpwr: |
2832aefd442c
Add 'no driver' version which can simulate in normal mode.
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
67 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
|
68 |
2832aefd442c
Add 'no driver' version which can simulate in normal mode.
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
69 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
|
70 cost += 100 |
2832aefd442c
Add 'no driver' version which can simulate in normal mode.
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
71 else: |
2832aefd442c
Add 'no driver' version which can simulate in normal mode.
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
72 thdinv = 100 - thd |
2832aefd442c
Add 'no driver' version which can simulate in normal mode.
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
73 tthdinv = 100 - tthd |
2832aefd442c
Add 'no driver' version which can simulate in normal mode.
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
74 if thdinv < tthdinv: |
2832aefd442c
Add 'no driver' version which can simulate in normal mode.
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
75 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
|
76 if eff < teff: |
2832aefd442c
Add 'no driver' version which can simulate in normal mode.
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
77 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
|
78 |
2832aefd442c
Add 'no driver' version which can simulate in normal mode.
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
79 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
|
80 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
|
81 |
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 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
|
83 |
12
49939e179c86
Run 4 simulations at once.
Daniel O'Connor <darius@dons.net.au>
parents:
11
diff
changeset
|
84 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
|
85 '''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
|
86 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
|
87 |
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 # 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
|
89 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
|
90 |
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
|
91 # 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
|
92 dbh = sqlite3.connect(dsn) |
49939e179c86
Run 4 simulations at once.
Daniel O'Connor <darius@dons.net.au>
parents:
11
diff
changeset
|
93 cur = dbh.cursor() |
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
|
94 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)) |
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
|
95 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
|
96 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
|
97 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
|
98 # 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
|
99 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
|
100 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
|
101 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
|
102 |
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
|
103 # Get next run number |
12
49939e179c86
Run 4 simulations at once.
Daniel O'Connor <darius@dons.net.au>
parents:
11
diff
changeset
|
104 cur.execute('BEGIN DEFERRED') |
49939e179c86
Run 4 simulations at once.
Daniel O'Connor <darius@dons.net.au>
parents:
11
diff
changeset
|
105 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
|
106 run = cur.fetchone()[0] |
49939e179c86
Run 4 simulations at once.
Daniel O'Connor <darius@dons.net.au>
parents:
11
diff
changeset
|
107 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
|
108 |
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 # 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
|
110 # 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
|
111 runname = pathlib.Path(circ).stem + '-' + str(run) + '.net' |
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 then, taken, power, eff, thd, ipeak = simulate(circ, simexe, simflags, rundir, runname, |
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 {'dutypct' : duty, 'C1' : c1 * 1e-12, 'C2' : c2 * 1e-12, 'L1' : l1 * 1e-6, 'L2' : l2 * 1e-9}) |
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 # 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
|
115 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
|
116 |
2832aefd442c
Add 'no driver' version which can simulate in normal mode.
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
117 # 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
|
118 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
|
119 taken = taken.seconds + taken.microseconds / 1e6 |
12
49939e179c86
Run 4 simulations at once.
Daniel O'Connor <darius@dons.net.au>
parents:
11
diff
changeset
|
120 cur.execute('BEGIN DEFERRED') |
49939e179c86
Run 4 simulations at once.
Daniel O'Connor <darius@dons.net.au>
parents:
11
diff
changeset
|
121 cur.execute('REPLACE INTO GAN190 (name, run, start, duration, duty, c1, c2, l1, l2, power, efficiency, thd, ipeak, cost) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)', |
10
2832aefd442c
Add 'no driver' version which can simulate in normal mode.
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
122 (runname, run, then, taken, duty, c1, c2, l1, l2, power, eff, thd, ipeak, cost)) |
2832aefd442c
Add 'no driver' version which can simulate in normal mode.
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
123 cur.execute('COMMIT') |
2832aefd442c
Add 'no driver' version which can simulate in normal mode.
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
124 |
2832aefd442c
Add 'no driver' version which can simulate in normal mode.
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
125 return cost |
2832aefd442c
Add 'no driver' version which can simulate in normal mode.
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
126 |
2832aefd442c
Add 'no driver' version which can simulate in normal mode.
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
127 def ev(): |
2832aefd442c
Add 'no driver' version which can simulate in normal mode.
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
128 # Bounds for parameters |
12
49939e179c86
Run 4 simulations at once.
Daniel O'Connor <darius@dons.net.au>
parents:
11
diff
changeset
|
129 # 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
|
130 bounds = [(10, 80), |
2832aefd442c
Add 'no driver' version which can simulate in normal mode.
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
131 (1, 20), |
2832aefd442c
Add 'no driver' version which can simulate in normal mode.
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
132 (10, 300), |
2832aefd442c
Add 'no driver' version which can simulate in normal mode.
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
133 (1, 20), |
2832aefd442c
Add 'no driver' version which can simulate in normal mode.
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
134 (10, 500)] |
12
49939e179c86
Run 4 simulations at once.
Daniel O'Connor <darius@dons.net.au>
parents:
11
diff
changeset
|
135 |
10
2832aefd442c
Add 'no driver' version which can simulate in normal mode.
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
136 # Initial solution |
2832aefd442c
Add 'no driver' version which can simulate in normal mode.
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
137 x0 = [36, 10, 155, 5, 140] |
12
49939e179c86
Run 4 simulations at once.
Daniel O'Connor <darius@dons.net.au>
parents:
11
diff
changeset
|
138 |
49939e179c86
Run 4 simulations at once.
Daniel O'Connor <darius@dons.net.au>
parents:
11
diff
changeset
|
139 # Where to save results |
49939e179c86
Run 4 simulations at once.
Daniel O'Connor <darius@dons.net.au>
parents:
11
diff
changeset
|
140 dsn = 'results.db' |
49939e179c86
Run 4 simulations at once.
Daniel O'Connor <darius@dons.net.au>
parents:
11
diff
changeset
|
141 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
|
142 cur = dbh.cursor() |
2832aefd442c
Add 'no driver' version which can simulate in normal mode.
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
143 cur.execute(''' |
2832aefd442c
Add 'no driver' version which can simulate in normal mode.
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
144 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
|
145 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
|
146 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
|
147 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
|
148 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
|
149 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
|
150 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
|
151 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
|
152 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
|
153 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
|
154 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
|
155 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
|
156 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
|
157 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
|
158 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
|
159 );''') |
12
49939e179c86
Run 4 simulations at once.
Daniel O'Connor <darius@dons.net.au>
parents:
11
diff
changeset
|
160 |
10
2832aefd442c
Add 'no driver' version which can simulate in normal mode.
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
161 return differential_evolution(fn, bounds, x0 = x0, |
12
49939e179c86
Run 4 simulations at once.
Daniel O'Connor <darius@dons.net.au>
parents:
11
diff
changeset
|
162 args = (dsn, '/Users/oconnd1/bin/runltspice', [], 'tmp', 'pa-GAN190-PP-nodriver.net'), |
49939e179c86
Run 4 simulations at once.
Daniel O'Connor <darius@dons.net.au>
parents:
11
diff
changeset
|
163 integrality = True, disp = True, seed = 12345, updating = 'deferred', workers = 4) |