Spaces:
Running
Running
Deploy Xfly/C3L comparison GUI
Browse files
library/ngspice/studies/xfly6s_xfly8s_c3l_scaled_comparison/gui/xfly_compare_gui.py
CHANGED
|
@@ -51,6 +51,7 @@ DEFAULT_LOUT_NH = 3.0
|
|
| 51 |
DEFAULT_CFLY_NF = 100.0
|
| 52 |
DEFAULT_SWITCHING_FREQUENCY_MHZ = 100.0
|
| 53 |
DEFAULT_FREQUENCY_BY_TOPOLOGY_MHZ = {"xfly6s": 100.0, "xfly8s": 50.0, "c3l": 50.0}
|
|
|
|
| 54 |
COUT = 100e-9
|
| 55 |
CIN = 1e-6
|
| 56 |
MIN_DUTY = 0.12
|
|
@@ -370,6 +371,82 @@ HTML = r"""<!doctype html>
|
|
| 370 |
width: 100%;
|
| 371 |
height: auto;
|
| 372 |
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 373 |
.placeholder {
|
| 374 |
color: var(--muted);
|
| 375 |
font-size: 14px;
|
|
@@ -459,6 +536,7 @@ HTML = r"""<!doctype html>
|
|
| 459 |
aside { border-right: 0; border-bottom: 1px solid var(--line); }
|
| 460 |
.topology-strip { grid-template-columns: 1fr; }
|
| 461 |
.topology-image-frame { height: 240px; }
|
|
|
|
| 462 |
.meta { grid-template-columns: repeat(2, minmax(0, 1fr)); }
|
| 463 |
header { align-items: flex-start; flex-direction: column; }
|
| 464 |
.status { text-align: left; min-width: 0; }
|
|
@@ -553,7 +631,7 @@ HTML = r"""<!doctype html>
|
|
| 553 |
</div>
|
| 554 |
<div class="control">
|
| 555 |
<label for="step">Max Step (ps)</label>
|
| 556 |
-
<input id="step" name="step" type="number" min="5" max="
|
| 557 |
</div>
|
| 558 |
</div>
|
| 559 |
<div class="control">
|
|
@@ -617,6 +695,18 @@ HTML = r"""<!doctype html>
|
|
| 617 |
</thead>
|
| 618 |
<tbody></tbody>
|
| 619 |
</table>
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 620 |
</section>
|
| 621 |
</main>
|
| 622 |
<script>
|
|
@@ -627,10 +717,18 @@ HTML = r"""<!doctype html>
|
|
| 627 |
const metricsTable = document.getElementById('metricsTable');
|
| 628 |
const metricsBody = metricsTable.querySelector('tbody');
|
| 629 |
const summaryMetrics = document.getElementById('summaryMetrics');
|
|
|
|
|
|
|
|
|
|
| 630 |
const targetVout = document.getElementById('targetVout');
|
| 631 |
const k = document.getElementById('k');
|
| 632 |
const targetVoutValue = document.getElementById('targetVoutValue');
|
| 633 |
const kValue = document.getElementById('kValue');
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 634 |
|
| 635 |
function updateLabels() {
|
| 636 |
targetVoutValue.textContent = `${Number(targetVout.value).toFixed(2)} V`;
|
|
@@ -678,11 +776,132 @@ HTML = r"""<!doctype html>
|
|
| 678 |
metricsTable.hidden = false;
|
| 679 |
}
|
| 680 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 681 |
function showProgress(topologies) {
|
| 682 |
const started = Date.now();
|
| 683 |
metricsTable.hidden = true;
|
| 684 |
-
const
|
| 685 |
-
const topologyText = topologies.map(key => labels[key] || key).join(' / ');
|
| 686 |
plotWrap.innerHTML = `
|
| 687 |
<div class="progress-card" role="status" aria-live="polite">
|
| 688 |
<div class="progress-title">
|
|
@@ -732,7 +951,8 @@ HTML = r"""<!doctype html>
|
|
| 732 |
|
| 733 |
runButton.disabled = true;
|
| 734 |
statusEl.textContent = 'Running ngspice...';
|
| 735 |
-
|
|
|
|
| 736 |
try {
|
| 737 |
const response = await fetch('/api/run', {
|
| 738 |
method: 'POST',
|
|
@@ -741,15 +961,23 @@ HTML = r"""<!doctype html>
|
|
| 741 |
});
|
| 742 |
const data = await response.json();
|
| 743 |
if (!response.ok) throw new Error(data.error || 'Simulation failed');
|
|
|
|
|
|
|
| 744 |
statusEl.textContent = `Run ${data.run_id}`;
|
| 745 |
plotWrap.innerHTML = `<img src="${data.plot_url}?t=${Date.now()}" alt="Comparison plot">`;
|
| 746 |
renderMetrics(data.metrics);
|
| 747 |
setSummary(data.metrics, data.settings, data.elapsed_s);
|
|
|
|
|
|
|
| 748 |
} catch (error) {
|
| 749 |
statusEl.innerHTML = `<span class="error">${error.message}</span>`;
|
| 750 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 751 |
} finally {
|
| 752 |
-
stopProgress();
|
| 753 |
runButton.disabled = false;
|
| 754 |
}
|
| 755 |
});
|
|
@@ -1165,12 +1393,7 @@ def settings_for_operating_point(base_settings: dict[str, Any], topologies: list
|
|
| 1165 |
return settings
|
| 1166 |
|
| 1167 |
|
| 1168 |
-
def
|
| 1169 |
-
topologies = payload.get("topologies") or ["xfly6s", "xfly8s", "c3l"]
|
| 1170 |
-
topologies = [top for top in topologies if top in TOPOLOGIES]
|
| 1171 |
-
if not topologies:
|
| 1172 |
-
raise ValueError("Select at least one topology.")
|
| 1173 |
-
|
| 1174 |
stop_us = clamp_float(payload.get("stop_us"), 0.5, 20.0, 4.0)
|
| 1175 |
measure_width_us = min(0.2, stop_us * 0.25)
|
| 1176 |
base_settings = {
|
|
@@ -1186,12 +1409,21 @@ def run_simulation(payload: dict[str, Any]) -> dict[str, Any]:
|
|
| 1186 |
"switch_ron_mohm": clamp_float(payload.get("switch_ron_mohm"), 0.2, 100.0, 30.0),
|
| 1187 |
"switch_coff_pf": clamp_float(payload.get("switch_coff_pf"), 0.0, 500.0, 20.0),
|
| 1188 |
"stop_us": stop_us,
|
| 1189 |
-
"max_step_ps": clamp_float(payload.get("max_step_ps"), 5.0,
|
| 1190 |
"measure_from_us": max(0.0, stop_us - measure_width_us),
|
| 1191 |
}
|
| 1192 |
base_settings["lout_h"] = base_settings["lout_nh"] * 1e-9
|
| 1193 |
base_settings["cfly_f"] = base_settings["cfly_nf"] * 1e-9
|
| 1194 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1195 |
|
| 1196 |
RUN_ROOT.mkdir(parents=True, exist_ok=True)
|
| 1197 |
run_id = time.strftime("%Y%m%d-%H%M%S-") + uuid.uuid4().hex[:8]
|
|
@@ -1220,6 +1452,39 @@ def run_simulation(payload: dict[str, Any]) -> dict[str, Any]:
|
|
| 1220 |
return summary
|
| 1221 |
|
| 1222 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1223 |
class Handler(BaseHTTPRequestHandler):
|
| 1224 |
server_version = "XflyCompareGUI/1.0"
|
| 1225 |
|
|
@@ -1275,13 +1540,13 @@ class Handler(BaseHTTPRequestHandler):
|
|
| 1275 |
self.send_json({"error": "Not found"}, HTTPStatus.NOT_FOUND)
|
| 1276 |
|
| 1277 |
def do_POST(self) -> None:
|
| 1278 |
-
if self.path
|
| 1279 |
self.send_json({"error": "Not found"}, HTTPStatus.NOT_FOUND)
|
| 1280 |
return
|
| 1281 |
try:
|
| 1282 |
length = int(self.headers.get("Content-Length", "0"))
|
| 1283 |
payload = json.loads(self.rfile.read(length).decode("utf-8") or "{}")
|
| 1284 |
-
result = run_simulation(payload)
|
| 1285 |
self.send_json(result)
|
| 1286 |
except subprocess.TimeoutExpired as exc:
|
| 1287 |
self.send_json({"error": f"ngspice timed out after {exc.timeout} s"}, HTTPStatus.INTERNAL_SERVER_ERROR)
|
|
@@ -1401,7 +1666,7 @@ def main() -> None:
|
|
| 1401 |
"switch_ron_mohm": 30,
|
| 1402 |
"switch_coff_pf": 20,
|
| 1403 |
"stop_us": 4.0,
|
| 1404 |
-
"max_step_ps":
|
| 1405 |
"topologies": ["xfly6s", "xfly8s", "c3l"],
|
| 1406 |
}
|
| 1407 |
)
|
|
|
|
| 51 |
DEFAULT_CFLY_NF = 100.0
|
| 52 |
DEFAULT_SWITCHING_FREQUENCY_MHZ = 100.0
|
| 53 |
DEFAULT_FREQUENCY_BY_TOPOLOGY_MHZ = {"xfly6s": 100.0, "xfly8s": 50.0, "c3l": 50.0}
|
| 54 |
+
DEFAULT_MAX_STEP_PS = 1000.0
|
| 55 |
COUT = 100e-9
|
| 56 |
CIN = 1e-6
|
| 57 |
MIN_DUTY = 0.12
|
|
|
|
| 371 |
width: 100%;
|
| 372 |
height: auto;
|
| 373 |
}
|
| 374 |
+
.sweep-panel {
|
| 375 |
+
margin-top: 14px;
|
| 376 |
+
padding: 14px;
|
| 377 |
+
}
|
| 378 |
+
.sweep-header {
|
| 379 |
+
display: flex;
|
| 380 |
+
align-items: baseline;
|
| 381 |
+
justify-content: space-between;
|
| 382 |
+
gap: 14px;
|
| 383 |
+
margin-bottom: 10px;
|
| 384 |
+
}
|
| 385 |
+
.sweep-header h2 {
|
| 386 |
+
margin: 0;
|
| 387 |
+
font-size: 18px;
|
| 388 |
+
line-height: 1.25;
|
| 389 |
+
}
|
| 390 |
+
.sweep-status {
|
| 391 |
+
color: var(--muted);
|
| 392 |
+
font-size: 13px;
|
| 393 |
+
font-variant-numeric: tabular-nums;
|
| 394 |
+
text-align: right;
|
| 395 |
+
}
|
| 396 |
+
.sweep-progress-track {
|
| 397 |
+
height: 10px;
|
| 398 |
+
overflow: hidden;
|
| 399 |
+
border-radius: 999px;
|
| 400 |
+
background: #dfe7ec;
|
| 401 |
+
}
|
| 402 |
+
.sweep-progress-fill {
|
| 403 |
+
width: 0%;
|
| 404 |
+
height: 100%;
|
| 405 |
+
border-radius: inherit;
|
| 406 |
+
background: linear-gradient(90deg, var(--blue), #14b8a6);
|
| 407 |
+
transition: width 180ms ease;
|
| 408 |
+
}
|
| 409 |
+
.sweep-grid {
|
| 410 |
+
display: grid;
|
| 411 |
+
grid-template-columns: repeat(3, minmax(0, 1fr));
|
| 412 |
+
gap: 12px;
|
| 413 |
+
margin-top: 14px;
|
| 414 |
+
}
|
| 415 |
+
.sweep-chart {
|
| 416 |
+
border: 1px solid var(--line);
|
| 417 |
+
border-radius: 8px;
|
| 418 |
+
background: #ffffff;
|
| 419 |
+
padding: 12px;
|
| 420 |
+
min-width: 0;
|
| 421 |
+
}
|
| 422 |
+
.sweep-chart h3 {
|
| 423 |
+
margin: 0 0 8px;
|
| 424 |
+
font-size: 14px;
|
| 425 |
+
}
|
| 426 |
+
.sweep-chart svg {
|
| 427 |
+
display: block;
|
| 428 |
+
width: 100%;
|
| 429 |
+
height: auto;
|
| 430 |
+
}
|
| 431 |
+
.legend {
|
| 432 |
+
display: flex;
|
| 433 |
+
flex-wrap: wrap;
|
| 434 |
+
gap: 10px;
|
| 435 |
+
margin-top: 8px;
|
| 436 |
+
color: var(--muted);
|
| 437 |
+
font-size: 12px;
|
| 438 |
+
}
|
| 439 |
+
.legend span {
|
| 440 |
+
display: inline-flex;
|
| 441 |
+
align-items: center;
|
| 442 |
+
gap: 5px;
|
| 443 |
+
}
|
| 444 |
+
.legend i {
|
| 445 |
+
width: 14px;
|
| 446 |
+
height: 3px;
|
| 447 |
+
border-radius: 999px;
|
| 448 |
+
display: inline-block;
|
| 449 |
+
}
|
| 450 |
.placeholder {
|
| 451 |
color: var(--muted);
|
| 452 |
font-size: 14px;
|
|
|
|
| 536 |
aside { border-right: 0; border-bottom: 1px solid var(--line); }
|
| 537 |
.topology-strip { grid-template-columns: 1fr; }
|
| 538 |
.topology-image-frame { height: 240px; }
|
| 539 |
+
.sweep-grid { grid-template-columns: 1fr; }
|
| 540 |
.meta { grid-template-columns: repeat(2, minmax(0, 1fr)); }
|
| 541 |
header { align-items: flex-start; flex-direction: column; }
|
| 542 |
.status { text-align: left; min-width: 0; }
|
|
|
|
| 631 |
</div>
|
| 632 |
<div class="control">
|
| 633 |
<label for="step">Max Step (ps)</label>
|
| 634 |
+
<input id="step" name="step" type="number" min="5" max="1000" step="5" value="1000">
|
| 635 |
</div>
|
| 636 |
</div>
|
| 637 |
<div class="control">
|
|
|
|
| 695 |
</thead>
|
| 696 |
<tbody></tbody>
|
| 697 |
</table>
|
| 698 |
+
<div class="panel sweep-panel" id="sweepPanel">
|
| 699 |
+
<div class="sweep-header">
|
| 700 |
+
<h2>Efficiency Sweep</h2>
|
| 701 |
+
<div id="sweepStatus" class="sweep-status">Runs after the comparison plot.</div>
|
| 702 |
+
</div>
|
| 703 |
+
<div class="sweep-progress-track" aria-hidden="true">
|
| 704 |
+
<div id="sweepProgressFill" class="sweep-progress-fill"></div>
|
| 705 |
+
</div>
|
| 706 |
+
<div id="sweepCharts" class="sweep-grid">
|
| 707 |
+
<div class="placeholder">Load sweep: 20%, 40%, 60%, 80%, 100%. Output sweep: 0.65 V, 0.75 V, 0.85 V.</div>
|
| 708 |
+
</div>
|
| 709 |
+
</div>
|
| 710 |
</section>
|
| 711 |
</main>
|
| 712 |
<script>
|
|
|
|
| 717 |
const metricsTable = document.getElementById('metricsTable');
|
| 718 |
const metricsBody = metricsTable.querySelector('tbody');
|
| 719 |
const summaryMetrics = document.getElementById('summaryMetrics');
|
| 720 |
+
const sweepStatus = document.getElementById('sweepStatus');
|
| 721 |
+
const sweepProgressFill = document.getElementById('sweepProgressFill');
|
| 722 |
+
const sweepCharts = document.getElementById('sweepCharts');
|
| 723 |
const targetVout = document.getElementById('targetVout');
|
| 724 |
const k = document.getElementById('k');
|
| 725 |
const targetVoutValue = document.getElementById('targetVoutValue');
|
| 726 |
const kValue = document.getElementById('kValue');
|
| 727 |
+
const sweepLoads = [20, 40, 60, 80, 100];
|
| 728 |
+
const sweepOutputs = [0.65, 0.75, 0.85];
|
| 729 |
+
const sweepTopologies = ['xfly6s', 'xfly8s', 'c3l'];
|
| 730 |
+
const topologyLabels = {xfly6s: 'Xfly6S', xfly8s: 'Xfly8S', c3l: 'C3L'};
|
| 731 |
+
const outputColors = {'0.65': '#2563eb', '0.75': '#15803d', '0.85': '#b45309'};
|
| 732 |
|
| 733 |
function updateLabels() {
|
| 734 |
targetVoutValue.textContent = `${Number(targetVout.value).toFixed(2)} V`;
|
|
|
|
| 776 |
metricsTable.hidden = false;
|
| 777 |
}
|
| 778 |
|
| 779 |
+
function resetSweep() {
|
| 780 |
+
sweepStatus.textContent = 'Waiting for comparison run...';
|
| 781 |
+
sweepProgressFill.style.width = '0%';
|
| 782 |
+
sweepCharts.innerHTML = '<div class="placeholder">Efficiency sweep will start after the comparison plot is rendered.</div>';
|
| 783 |
+
}
|
| 784 |
+
|
| 785 |
+
function escapeHtml(value) {
|
| 786 |
+
return String(value).replace(/[&<>"']/g, (char) => ({
|
| 787 |
+
'&': '&', '<': '<', '>': '>', '"': '"', "'": ''',
|
| 788 |
+
}[char]));
|
| 789 |
+
}
|
| 790 |
+
|
| 791 |
+
function chartForTopology(topology, items) {
|
| 792 |
+
const width = 420;
|
| 793 |
+
const height = 270;
|
| 794 |
+
const left = 48;
|
| 795 |
+
const right = 18;
|
| 796 |
+
const top = 18;
|
| 797 |
+
const bottom = 42;
|
| 798 |
+
const values = items.map(item => item.efficiency_pct).filter(Number.isFinite);
|
| 799 |
+
let yMin = values.length ? Math.floor(Math.min(...values) - 1) : 0;
|
| 800 |
+
let yMax = values.length ? Math.ceil(Math.max(...values) + 1) : 100;
|
| 801 |
+
if (yMax - yMin < 4) {
|
| 802 |
+
yMin -= 2;
|
| 803 |
+
yMax += 2;
|
| 804 |
+
}
|
| 805 |
+
yMin = Math.max(0, yMin);
|
| 806 |
+
if (yMax <= yMin) yMax = yMin + 1;
|
| 807 |
+
const x = (load) => left + ((load - 20) / 80) * (width - left - right);
|
| 808 |
+
const y = (eff) => height - bottom - ((eff - yMin) / (yMax - yMin)) * (height - top - bottom);
|
| 809 |
+
const ticks = [yMin, yMin + (yMax - yMin) * 0.25, yMin + (yMax - yMin) * 0.5, yMin + (yMax - yMin) * 0.75, yMax];
|
| 810 |
+
const grid = ticks.map(tick => {
|
| 811 |
+
const yy = y(tick);
|
| 812 |
+
return `<line x1="${left}" y1="${yy}" x2="${width - right}" y2="${yy}" stroke="#e5ebef"/>` +
|
| 813 |
+
`<text x="${left - 8}" y="${yy + 4}" text-anchor="end" font-size="11" fill="#65727d">${tick.toFixed(0)}</text>`;
|
| 814 |
+
}).join('');
|
| 815 |
+
const xTicks = sweepLoads.map(load => (
|
| 816 |
+
`<line x1="${x(load)}" y1="${height - bottom}" x2="${x(load)}" y2="${height - bottom + 4}" stroke="#65727d"/>` +
|
| 817 |
+
`<text x="${x(load)}" y="${height - bottom + 19}" text-anchor="middle" font-size="11" fill="#65727d">${load}</text>`
|
| 818 |
+
)).join('');
|
| 819 |
+
const lines = sweepOutputs.map(vout => {
|
| 820 |
+
const key = vout.toFixed(2);
|
| 821 |
+
const points = sweepLoads.map(load => {
|
| 822 |
+
const match = items.find(item => item.load_pct === load && Number(item.target_vout).toFixed(2) === key);
|
| 823 |
+
if (!match || !Number.isFinite(match.efficiency_pct)) return null;
|
| 824 |
+
return `${x(load).toFixed(1)},${y(match.efficiency_pct).toFixed(1)}`;
|
| 825 |
+
}).filter(Boolean);
|
| 826 |
+
const circles = points.map(point => {
|
| 827 |
+
const [cx, cy] = point.split(',');
|
| 828 |
+
return `<circle cx="${cx}" cy="${cy}" r="3.2" fill="${outputColors[key]}"/>`;
|
| 829 |
+
}).join('');
|
| 830 |
+
return points.length
|
| 831 |
+
? `<polyline points="${points.join(' ')}" fill="none" stroke="${outputColors[key]}" stroke-width="2.4"/>${circles}`
|
| 832 |
+
: '';
|
| 833 |
+
}).join('');
|
| 834 |
+
const legend = sweepOutputs.map(vout => {
|
| 835 |
+
const key = vout.toFixed(2);
|
| 836 |
+
return `<span><i style="background:${outputColors[key]}"></i>${key} V</span>`;
|
| 837 |
+
}).join('');
|
| 838 |
+
return `
|
| 839 |
+
<div class="sweep-chart">
|
| 840 |
+
<h3>${escapeHtml(topologyLabels[topology])}</h3>
|
| 841 |
+
<svg viewBox="0 0 ${width} ${height}" role="img" aria-label="${escapeHtml(topologyLabels[topology])} efficiency sweep">
|
| 842 |
+
<rect x="0" y="0" width="${width}" height="${height}" fill="#ffffff"/>
|
| 843 |
+
${grid}
|
| 844 |
+
<line x1="${left}" y1="${top}" x2="${left}" y2="${height - bottom}" stroke="#31414c"/>
|
| 845 |
+
<line x1="${left}" y1="${height - bottom}" x2="${width - right}" y2="${height - bottom}" stroke="#31414c"/>
|
| 846 |
+
${xTicks}
|
| 847 |
+
<text x="${width / 2}" y="${height - 8}" text-anchor="middle" font-size="12" fill="#65727d">Load (%)</text>
|
| 848 |
+
<text x="14" y="${height / 2}" transform="rotate(-90 14 ${height / 2})" text-anchor="middle" font-size="12" fill="#65727d">Efficiency (%)</text>
|
| 849 |
+
${lines}
|
| 850 |
+
</svg>
|
| 851 |
+
<div class="legend">${legend}</div>
|
| 852 |
+
</div>`;
|
| 853 |
+
}
|
| 854 |
+
|
| 855 |
+
function renderSweepCharts(items) {
|
| 856 |
+
sweepCharts.innerHTML = sweepTopologies.map(topology => chartForTopology(
|
| 857 |
+
topology,
|
| 858 |
+
items.filter(item => item.key === topology),
|
| 859 |
+
)).join('');
|
| 860 |
+
}
|
| 861 |
+
|
| 862 |
+
async function runEfficiencySweep(basePayload) {
|
| 863 |
+
const total = sweepLoads.length * sweepOutputs.length * sweepTopologies.length;
|
| 864 |
+
const started = Date.now();
|
| 865 |
+
const baseCurrent = basePayload.output_current_a;
|
| 866 |
+
const results = [];
|
| 867 |
+
let complete = 0;
|
| 868 |
+
sweepCharts.innerHTML = '';
|
| 869 |
+
sweepProgressFill.style.width = '0%';
|
| 870 |
+
sweepStatus.textContent = `Sweep running: 0 / ${total}`;
|
| 871 |
+
|
| 872 |
+
for (const topology of sweepTopologies) {
|
| 873 |
+
for (const target of sweepOutputs) {
|
| 874 |
+
for (const load of sweepLoads) {
|
| 875 |
+
const elapsed = ((Date.now() - started) / 1000).toFixed(1);
|
| 876 |
+
sweepStatus.textContent = `Sweep running: ${complete + 1} / ${total} - ${topologyLabels[topology]}, ${target.toFixed(2)} V, ${load}% load, ${elapsed} s`;
|
| 877 |
+
const response = await fetch('/api/sweep-case', {
|
| 878 |
+
method: 'POST',
|
| 879 |
+
headers: {'Content-Type': 'application/json'},
|
| 880 |
+
body: JSON.stringify({
|
| 881 |
+
...basePayload,
|
| 882 |
+
topology,
|
| 883 |
+
target_vout: target,
|
| 884 |
+
output_current_a: baseCurrent * load / 100,
|
| 885 |
+
load_pct: load,
|
| 886 |
+
}),
|
| 887 |
+
});
|
| 888 |
+
const data = await response.json();
|
| 889 |
+
if (!response.ok) throw new Error(data.error || 'Sweep case failed');
|
| 890 |
+
results.push(data.metric);
|
| 891 |
+
complete += 1;
|
| 892 |
+
sweepProgressFill.style.width = `${Math.round((complete / total) * 100)}%`;
|
| 893 |
+
}
|
| 894 |
+
}
|
| 895 |
+
}
|
| 896 |
+
const elapsed = ((Date.now() - started) / 1000).toFixed(1);
|
| 897 |
+
sweepStatus.textContent = `Sweep complete: ${complete} / ${total}, ${elapsed} s`;
|
| 898 |
+
renderSweepCharts(results);
|
| 899 |
+
}
|
| 900 |
+
|
| 901 |
function showProgress(topologies) {
|
| 902 |
const started = Date.now();
|
| 903 |
metricsTable.hidden = true;
|
| 904 |
+
const topologyText = topologies.map(key => topologyLabels[key] || key).join(' / ');
|
|
|
|
| 905 |
plotWrap.innerHTML = `
|
| 906 |
<div class="progress-card" role="status" aria-live="polite">
|
| 907 |
<div class="progress-title">
|
|
|
|
| 951 |
|
| 952 |
runButton.disabled = true;
|
| 953 |
statusEl.textContent = 'Running ngspice...';
|
| 954 |
+
resetSweep();
|
| 955 |
+
let stopProgress = showProgress(topologies);
|
| 956 |
try {
|
| 957 |
const response = await fetch('/api/run', {
|
| 958 |
method: 'POST',
|
|
|
|
| 961 |
});
|
| 962 |
const data = await response.json();
|
| 963 |
if (!response.ok) throw new Error(data.error || 'Simulation failed');
|
| 964 |
+
stopProgress();
|
| 965 |
+
stopProgress = null;
|
| 966 |
statusEl.textContent = `Run ${data.run_id}`;
|
| 967 |
plotWrap.innerHTML = `<img src="${data.plot_url}?t=${Date.now()}" alt="Comparison plot">`;
|
| 968 |
renderMetrics(data.metrics);
|
| 969 |
setSummary(data.metrics, data.settings, data.elapsed_s);
|
| 970 |
+
await runEfficiencySweep(payload);
|
| 971 |
+
statusEl.textContent = `Run ${data.run_id} + sweep complete`;
|
| 972 |
} catch (error) {
|
| 973 |
statusEl.innerHTML = `<span class="error">${error.message}</span>`;
|
| 974 |
+
if (stopProgress) {
|
| 975 |
+
plotWrap.innerHTML = `<div class="placeholder error">${error.message}</div>`;
|
| 976 |
+
} else {
|
| 977 |
+
sweepStatus.innerHTML = `<span class="error">${error.message}</span>`;
|
| 978 |
+
}
|
| 979 |
} finally {
|
| 980 |
+
if (stopProgress) stopProgress();
|
| 981 |
runButton.disabled = false;
|
| 982 |
}
|
| 983 |
});
|
|
|
|
| 1393 |
return settings
|
| 1394 |
|
| 1395 |
|
| 1396 |
+
def settings_from_payload(payload: dict[str, Any], topologies: list[str]) -> dict[str, Any]:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1397 |
stop_us = clamp_float(payload.get("stop_us"), 0.5, 20.0, 4.0)
|
| 1398 |
measure_width_us = min(0.2, stop_us * 0.25)
|
| 1399 |
base_settings = {
|
|
|
|
| 1409 |
"switch_ron_mohm": clamp_float(payload.get("switch_ron_mohm"), 0.2, 100.0, 30.0),
|
| 1410 |
"switch_coff_pf": clamp_float(payload.get("switch_coff_pf"), 0.0, 500.0, 20.0),
|
| 1411 |
"stop_us": stop_us,
|
| 1412 |
+
"max_step_ps": clamp_float(payload.get("max_step_ps"), 5.0, 1000.0, DEFAULT_MAX_STEP_PS),
|
| 1413 |
"measure_from_us": max(0.0, stop_us - measure_width_us),
|
| 1414 |
}
|
| 1415 |
base_settings["lout_h"] = base_settings["lout_nh"] * 1e-9
|
| 1416 |
base_settings["cfly_f"] = base_settings["cfly_nf"] * 1e-9
|
| 1417 |
+
return settings_for_operating_point(base_settings, topologies)
|
| 1418 |
+
|
| 1419 |
+
|
| 1420 |
+
def run_simulation(payload: dict[str, Any]) -> dict[str, Any]:
|
| 1421 |
+
topologies = payload.get("topologies") or ["xfly6s", "xfly8s", "c3l"]
|
| 1422 |
+
topologies = [top for top in topologies if top in TOPOLOGIES]
|
| 1423 |
+
if not topologies:
|
| 1424 |
+
raise ValueError("Select at least one topology.")
|
| 1425 |
+
|
| 1426 |
+
settings = settings_from_payload(payload, topologies)
|
| 1427 |
|
| 1428 |
RUN_ROOT.mkdir(parents=True, exist_ok=True)
|
| 1429 |
run_id = time.strftime("%Y%m%d-%H%M%S-") + uuid.uuid4().hex[:8]
|
|
|
|
| 1452 |
return summary
|
| 1453 |
|
| 1454 |
|
| 1455 |
+
def run_sweep_case(payload: dict[str, Any]) -> dict[str, Any]:
|
| 1456 |
+
topology = payload.get("topology")
|
| 1457 |
+
if topology not in TOPOLOGIES:
|
| 1458 |
+
raise ValueError("Select one valid topology for the sweep case.")
|
| 1459 |
+
|
| 1460 |
+
settings = settings_from_payload(payload, [topology])
|
| 1461 |
+
|
| 1462 |
+
RUN_ROOT.mkdir(parents=True, exist_ok=True)
|
| 1463 |
+
run_id = time.strftime("%Y%m%d-%H%M%S-") + uuid.uuid4().hex[:8]
|
| 1464 |
+
run_dir = RUN_ROOT / run_id
|
| 1465 |
+
run_dir.mkdir(parents=True)
|
| 1466 |
+
(run_dir / "settings.json").write_text(json.dumps({"topologies": [topology], **settings}, indent=2))
|
| 1467 |
+
|
| 1468 |
+
started = time.perf_counter()
|
| 1469 |
+
deck = prepare_deck(topology, settings, run_dir)
|
| 1470 |
+
run_ngspice(deck)
|
| 1471 |
+
case = analyze_case(topology, run_dir, settings)
|
| 1472 |
+
elapsed = time.perf_counter() - started
|
| 1473 |
+
metric = case["metrics"]
|
| 1474 |
+
metric["target_vout"] = settings["target_vout"]
|
| 1475 |
+
metric["load_pct"] = int(clamp_float(payload.get("load_pct"), 0.0, 100.0, 100.0))
|
| 1476 |
+
metric["target_iout"] = settings["iout_target"]
|
| 1477 |
+
|
| 1478 |
+
summary = {
|
| 1479 |
+
"run_id": run_id,
|
| 1480 |
+
"elapsed_s": elapsed,
|
| 1481 |
+
"settings": settings,
|
| 1482 |
+
"metric": metric,
|
| 1483 |
+
}
|
| 1484 |
+
(run_dir / "summary.json").write_text(json.dumps(summary, indent=2))
|
| 1485 |
+
return summary
|
| 1486 |
+
|
| 1487 |
+
|
| 1488 |
class Handler(BaseHTTPRequestHandler):
|
| 1489 |
server_version = "XflyCompareGUI/1.0"
|
| 1490 |
|
|
|
|
| 1540 |
self.send_json({"error": "Not found"}, HTTPStatus.NOT_FOUND)
|
| 1541 |
|
| 1542 |
def do_POST(self) -> None:
|
| 1543 |
+
if self.path not in ("/api/run", "/api/sweep-case"):
|
| 1544 |
self.send_json({"error": "Not found"}, HTTPStatus.NOT_FOUND)
|
| 1545 |
return
|
| 1546 |
try:
|
| 1547 |
length = int(self.headers.get("Content-Length", "0"))
|
| 1548 |
payload = json.loads(self.rfile.read(length).decode("utf-8") or "{}")
|
| 1549 |
+
result = run_sweep_case(payload) if self.path == "/api/sweep-case" else run_simulation(payload)
|
| 1550 |
self.send_json(result)
|
| 1551 |
except subprocess.TimeoutExpired as exc:
|
| 1552 |
self.send_json({"error": f"ngspice timed out after {exc.timeout} s"}, HTTPStatus.INTERNAL_SERVER_ERROR)
|
|
|
|
| 1666 |
"switch_ron_mohm": 30,
|
| 1667 |
"switch_coff_pf": 20,
|
| 1668 |
"stop_us": 4.0,
|
| 1669 |
+
"max_step_ps": DEFAULT_MAX_STEP_PS,
|
| 1670 |
"topologies": ["xfly6s", "xfly8s", "c3l"],
|
| 1671 |
}
|
| 1672 |
)
|