indierf-schematic-mcp 0.0.0-stage → 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +91 -0
- package/README.md +96 -2
- package/lib/analysis/plot-axis.ts +346 -0
- package/lib/cascade/filter-prototype.ts +442 -0
- package/lib/coax/cable-copy.ts +71 -0
- package/lib/coax/cables.ts +1366 -0
- package/lib/coax/explorer.ts +386 -0
- package/lib/coax/fit.ts +78 -0
- package/lib/coax/format.ts +85 -0
- package/lib/coax/loss.ts +150 -0
- package/lib/coax/plot.ts +72 -0
- package/lib/filter/ladder/built-form.ts +133 -0
- package/lib/filter/ladder/forms.ts +492 -0
- package/lib/filter/ladder/index.ts +110 -0
- package/lib/filter/ladder/linear-phase.ts +52 -0
- package/lib/filter/ladder/poly.ts +209 -0
- package/lib/filter/ladder/prototype.ts +91 -0
- package/lib/filter/ladder/synthesis.ts +431 -0
- package/lib/filter/ladder/terminations.ts +212 -0
- package/lib/filter/part-table.ts +317 -0
- package/lib/filter/roots.ts +305 -0
- package/lib/filter/stop-floor.ts +15 -0
- package/lib/import/delimited.ts +260 -0
- package/lib/import/types.ts +42 -0
- package/lib/match/cx.ts +72 -0
- package/lib/match/eseries.ts +166 -0
- package/lib/match/units.ts +178 -0
- package/lib/reflection/metrics.ts +118 -0
- package/lib/rf-engine/math.ts +434 -0
- package/lib/schematic/export/sparams.ts +323 -0
- package/lib/schematic/headless/node.ts +103 -0
- package/lib/schematic/headless/source.ts +97 -0
- package/lib/schematic/headless/tools.ts +867 -0
- package/lib/schematic/optimize/apply.ts +109 -0
- package/lib/schematic/optimize/differential-evolution.ts +129 -0
- package/lib/schematic/optimize/fold.ts +5 -0
- package/lib/schematic/optimize/goals.ts +483 -0
- package/lib/schematic/optimize/levenberg-marquardt.ts +178 -0
- package/lib/schematic/optimize/nelder-mead.ts +145 -0
- package/lib/schematic/optimize/optimize.ts +883 -0
- package/lib/schematic/optimize/random-search.ts +70 -0
- package/lib/schematic/optimize/runtime-host.ts +155 -0
- package/lib/schematic/sim/ac-results.ts +144 -0
- package/lib/schematic/sim/cache-key.ts +170 -0
- package/lib/schematic/sim/data-provider.ts +14 -0
- package/lib/schematic/sim/dc-results.ts +186 -0
- package/lib/schematic/sim/hash.ts +5 -0
- package/lib/schematic/sim/hb-results.ts +600 -0
- package/lib/schematic/sim/load-pull.ts +627 -0
- package/lib/schematic/sim/result-cache.ts +289 -0
- package/lib/schematic/sim/results.ts +185 -0
- package/lib/schematic/sim/runtime.ts +1003 -0
- package/lib/schematic/sim/stb-results.ts +21 -0
- package/lib/schematic/storage/file-name.ts +15 -0
- package/lib/schematic/yield/boards.ts +265 -0
- package/lib/schematic/yield/host.ts +120 -0
- package/lib/schematic/yield/knobs.ts +170 -0
- package/lib/schematic/yield/stats.ts +104 -0
- package/lib/schematic/yield/yield.ts +339 -0
- package/lib/schematic-doc/designs.ts +50 -0
- package/lib/schematic-doc/document.ts +374 -0
- package/lib/schematic-doc/equalizer-synthesis.ts +1039 -0
- package/lib/schematic-doc/exact-decimal.ts +47 -0
- package/lib/schematic-doc/filter-synthesis.ts +1734 -0
- package/lib/schematic-doc/fit-to-data.ts +442 -0
- package/lib/schematic-doc/fixes.ts +802 -0
- package/lib/schematic-doc/flatten.ts +787 -0
- package/lib/schematic-doc/goal-ripple.ts +48 -0
- package/lib/schematic-doc/ids.ts +18 -0
- package/lib/schematic-doc/ir.ts +97 -0
- package/lib/schematic-doc/layout-map.ts +317 -0
- package/lib/schematic-doc/layout-twin.ts +47 -0
- package/lib/schematic-doc/layout.ts +444 -0
- package/lib/schematic-doc/make-subcircuit.ts +463 -0
- package/lib/schematic-doc/migrate-line-pins.ts +317 -0
- package/lib/schematic-doc/migrations.ts +53 -0
- package/lib/schematic-doc/mixed-mode.ts +84 -0
- package/lib/schematic-doc/model-card.ts +542 -0
- package/lib/schematic-doc/models.ts +304 -0
- package/lib/schematic-doc/names.ts +59 -0
- package/lib/schematic-doc/netlist-text.ts +172 -0
- package/lib/schematic-doc/netlister.ts +273 -0
- package/lib/schematic-doc/opamp-extension.ts +376 -0
- package/lib/schematic-doc/ops/apply.ts +2158 -0
- package/lib/schematic-doc/ops/index.ts +19 -0
- package/lib/schematic-doc/ops/marker-search.ts +87 -0
- package/lib/schematic-doc/ops/schema.ts +1056 -0
- package/lib/schematic-doc/ops/transact.ts +148 -0
- package/lib/schematic-doc/optimizer-objective.ts +31 -0
- package/lib/schematic-doc/order.ts +65 -0
- package/lib/schematic-doc/sat-tilt.ts +68 -0
- package/lib/schematic-doc/schema.ts +637 -0
- package/lib/schematic-doc/share.ts +70 -0
- package/lib/schematic-doc/sp-step.ts +108 -0
- package/lib/schematic-doc/spice-model.ts +194 -0
- package/lib/schematic-doc/spice-subckt.ts +1483 -0
- package/lib/schematic-doc/subckt-import.ts +183 -0
- package/lib/schematic-doc/substrate-block.ts +109 -0
- package/lib/schematic-doc/swap-em.ts +312 -0
- package/lib/schematic-doc/swap.ts +168 -0
- package/lib/schematic-doc/targets.ts +559 -0
- package/lib/schematic-doc/test-states.ts +335 -0
- package/lib/schematic-doc/tolerance.ts +333 -0
- package/lib/schematic-engine/node-engine.ts +51 -0
- package/lib/schematic-engine/pkg/rfsim_wasm.js +748 -0
- package/lib/schematic-engine/pkg/rfsim_wasm_bg.wasm +0 -0
- package/lib/schematic-engine/protocol.ts +1074 -0
- package/lib/schematic-engine/quantities.ts +863 -0
- package/lib/schematic-engine/worker-core.ts +707 -0
- package/lib/schematic-engine/worker-inline.ts +69 -0
- package/lib/schematic-engine/worker-pool.ts +1042 -0
- package/lib/schematic-library/param-meaning.ts +75 -0
- package/lib/schematic-library/param-visibility.ts +110 -0
- package/lib/schematic-library/pin-geometry.ts +354 -0
- package/lib/schematic-library/planned-models.ts +18 -0
- package/lib/schematic-library/schemas.gen.ts +6857 -0
- package/lib/schematic-library/substrates.ts +24 -0
- package/lib/schematic-library/transform.ts +80 -0
- package/lib/share/base64url.ts +25 -0
- package/lib/share/compress.ts +103 -0
- package/lib/sparam/complex.ts +129 -0
- package/lib/sparam/convert.ts +223 -0
- package/lib/sparam/format.ts +160 -0
- package/lib/sparam/parse-touchstone.ts +1683 -0
- package/lib/sparam/renormalize.ts +100 -0
- package/lib/sparam/tags.ts +80 -0
- package/lib/sparam/write-touchstone.ts +194 -0
- package/mcp/schematic/package.json +5 -0
- package/mcp/schematic/server.mjs +112 -0
- package/mcp/schematic/tools.mjs +265 -0
- package/package.json +33 -4
- package/scripts/lib/ts-hooks.mjs +152 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,91 @@
|
|
|
1
|
+
Required Notice: Copyright IndieRF (https://www.indierf.com)
|
|
2
|
+
|
|
3
|
+
# PolyForm Shield License 1.0.0
|
|
4
|
+
|
|
5
|
+
<https://polyformproject.org/licenses/shield/1.0.0>
|
|
6
|
+
|
|
7
|
+
## Acceptance
|
|
8
|
+
|
|
9
|
+
In order to get any license under these terms, you must agree to them as both strict obligations and conditions to all your licenses.
|
|
10
|
+
|
|
11
|
+
## Copyright License
|
|
12
|
+
|
|
13
|
+
The licensor grants you a copyright license for the software to do everything you might do with the software that would otherwise infringe the licensor's copyright in it for any permitted purpose. However, you may only distribute the software according to [Distribution License](#distribution-license) and make changes or new works based on the software according to [Changes and New Works License](#changes-and-new-works-license).
|
|
14
|
+
|
|
15
|
+
## Distribution License
|
|
16
|
+
|
|
17
|
+
The licensor grants you an additional copyright license to distribute copies of the software. Your license to distribute covers distributing the software with changes and new works permitted by [Changes and New Works License](#changes-and-new-works-license).
|
|
18
|
+
|
|
19
|
+
## Notices
|
|
20
|
+
|
|
21
|
+
You must ensure that anyone who gets a copy of any part of the software from you also gets a copy of these terms or the URL for them above, as well as copies of any plain-text lines beginning with `Required Notice:` that the licensor provided with the software. For example:
|
|
22
|
+
|
|
23
|
+
> Required Notice: Copyright Yoyodyne, Inc. (http://example.com)
|
|
24
|
+
|
|
25
|
+
## Changes and New Works License
|
|
26
|
+
|
|
27
|
+
The licensor grants you an additional copyright license to make changes and new works based on the software for any permitted purpose.
|
|
28
|
+
|
|
29
|
+
## Patent License
|
|
30
|
+
|
|
31
|
+
The licensor grants you a patent license for the software that covers patent claims the licensor can license, or becomes able to license, that you would infringe by using the software.
|
|
32
|
+
|
|
33
|
+
## Noncompete
|
|
34
|
+
|
|
35
|
+
Any purpose is a permitted purpose, except for providing any product that competes with the software or any product the licensor or any of its affiliates provides using the software.
|
|
36
|
+
|
|
37
|
+
## Competition
|
|
38
|
+
|
|
39
|
+
Goods and services compete even when they provide functionality through different kinds of interfaces or for different technical platforms. Applications can compete with services, libraries with plugins, frameworks with development tools, and so on, even if they're written in different programming languages or for different computer architectures. Goods and services compete even when provided free of charge. If you market a product as a practical substitute for the software or another product, it definitely competes.
|
|
40
|
+
|
|
41
|
+
## New Products
|
|
42
|
+
|
|
43
|
+
If you are using the software to provide a product that does not compete, but the licensor or any of its affiliates brings your product into competition by providing a new version of the software or another product using the software, you may continue using versions of the software available under these terms beforehand to provide your competing product, but not any later versions.
|
|
44
|
+
|
|
45
|
+
## Discontinued Products
|
|
46
|
+
|
|
47
|
+
You may begin using the software to compete with a product or service that the licensor or any of its affiliates has stopped providing, unless the licensor includes a plain-text line beginning with `Licensor Line of Business:` with the software that mentions that line of business. For example:
|
|
48
|
+
|
|
49
|
+
> Licensor Line of Business: YoyodyneCMS Content Management System (http://example.com/cms)
|
|
50
|
+
|
|
51
|
+
## Sales of Business
|
|
52
|
+
|
|
53
|
+
If the licensor or any of its affiliates sells a line of business developing the software or using the software to provide a product, the buyer can also enforce [Noncompete](#noncompete) for that product.
|
|
54
|
+
|
|
55
|
+
## Fair Use
|
|
56
|
+
|
|
57
|
+
You may have "fair use" rights for the software under the law. These terms do not limit them.
|
|
58
|
+
|
|
59
|
+
## No Other Rights
|
|
60
|
+
|
|
61
|
+
These terms do not allow you to sublicense or transfer any of your licenses to anyone else, or prevent the licensor from granting licenses to anyone else. These terms do not imply any other licenses.
|
|
62
|
+
|
|
63
|
+
## Patent Defense
|
|
64
|
+
|
|
65
|
+
If you make any written claim that the software infringes or contributes to infringement of any patent, your patent license for the software granted under these terms ends immediately. If your company makes such a claim, your patent license ends immediately for work on behalf of your company.
|
|
66
|
+
|
|
67
|
+
## Violations
|
|
68
|
+
|
|
69
|
+
The first time you are notified in writing that you have violated any of these terms, or done anything with the software not covered by your licenses, your licenses can nonetheless continue if you come into full compliance with these terms, and take practical steps to correct past violations, within 32 days of receiving notice. Otherwise, all your licenses end immediately.
|
|
70
|
+
|
|
71
|
+
## No Liability
|
|
72
|
+
|
|
73
|
+
***As far as the law allows, the software comes as is, without any warranty or condition, and the licensor will not be liable to you for any damages arising out of these terms or the use or nature of the software, under any kind of legal claim.***
|
|
74
|
+
|
|
75
|
+
## Definitions
|
|
76
|
+
|
|
77
|
+
The **licensor** is the individual or entity offering these terms, and the **software** is the software the licensor makes available under these terms.
|
|
78
|
+
|
|
79
|
+
A **product** can be a good or service, or a combination of them.
|
|
80
|
+
|
|
81
|
+
**You** refers to the individual or entity agreeing to these terms.
|
|
82
|
+
|
|
83
|
+
**Your company** is any legal entity, sole proprietorship, or other kind of organization that you work for, plus all its affiliates.
|
|
84
|
+
|
|
85
|
+
**Affiliates** means the other organizations than an organization has control over, is under the control of, or is under common control with.
|
|
86
|
+
|
|
87
|
+
**Control** means ownership of substantially all the assets of an entity, or the power to direct its management and policies by vote, contract, or otherwise. Control can be direct or indirect.
|
|
88
|
+
|
|
89
|
+
**Your licenses** are all the licenses granted to you for the software under these terms.
|
|
90
|
+
|
|
91
|
+
**Use** means anything you do with the software requiring one of your licenses.
|
package/README.md
CHANGED
|
@@ -1,3 +1,97 @@
|
|
|
1
|
-
#
|
|
1
|
+
# IndieRF Schematic Simulator, local MCP server
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
Ask an MCP client for a 3rd-order 0.5 dB Chebyshev low-pass at 1 GHz and it places 5.081 pF, 8.727 nH and 5.081 pF between two 50 Ω ports, solves S21 at −0.50 dB at 1 GHz and −19.22 dB at 2 GHz, and hands back a link that opens the same schematic at indierf.com/schematic. The circuit is built through the simulator's own operations API and solved by its own engine on your machine (`lib/schematic/headless/`). Nothing leaves the machine: no hosting, no sign-in, no telemetry, no network call.
|
|
4
|
+
|
|
5
|
+
It needs Node 22.18 or later. Its two dependencies, `zod` and `immer` (MIT), come from the schematic document model; npx installs them, and the `.mcpb` carries them. The package ships the simulator's TypeScript source and the engine's WASM, and Node strips the types as it loads them, so there is no build step on your side. An older Node gets one line naming the version it needs.
|
|
6
|
+
|
|
7
|
+
## Install
|
|
8
|
+
|
|
9
|
+
Claude Code, from anywhere:
|
|
10
|
+
|
|
11
|
+
```bash
|
|
12
|
+
claude mcp add indierf-schematic -- npx -y indierf-schematic-mcp
|
|
13
|
+
```
|
|
14
|
+
|
|
15
|
+
Cursor (`~/.cursor/mcp.json` or `.cursor/mcp.json` in a project), Claude Desktop (`claude_desktop_config.json`), or any client that takes an `mcpServers` block:
|
|
16
|
+
|
|
17
|
+
```json
|
|
18
|
+
{
|
|
19
|
+
"mcpServers": {
|
|
20
|
+
"indierf-schematic": {
|
|
21
|
+
"command": "npx",
|
|
22
|
+
"args": ["-y", "indierf-schematic-mcp"]
|
|
23
|
+
}
|
|
24
|
+
}
|
|
25
|
+
}
|
|
26
|
+
```
|
|
27
|
+
|
|
28
|
+
On Windows, if the client cannot start `npx`, use `"command": "cmd"` with `"args": ["/c", "npx", "-y", "indierf-schematic-mcp"]`. Claude Desktop also takes the extension file `indierf-schematic-mcp-<version>.mcpb`, which holds the same package and runs it with `node`.
|
|
29
|
+
|
|
30
|
+
The npm package is published as `indierf-schematic-mcp`. The MCP Registry entry is not published yet.
|
|
31
|
+
|
|
32
|
+
## Licence
|
|
33
|
+
|
|
34
|
+
PolyForm Shield 1.0.0, as the cascade and Filter Bench servers: free to use at work and commercially, except to build a product that competes with it. The full text is in `LICENSE`.
|
|
35
|
+
|
|
36
|
+
## Where the schematic lives
|
|
37
|
+
|
|
38
|
+
The server keeps nothing between calls. Every tool that reads a schematic takes one of:
|
|
39
|
+
|
|
40
|
+
- `link`: an `https://www.indierf.com/schematic#s=schematic1.…` link, the page's Copy link, or the bare `schematic1.…` payload.
|
|
41
|
+
- `path`: a local `.indierf-schematic.json` file, the page's File, Download.
|
|
42
|
+
|
|
43
|
+
An editing tool (`edit`, `place_filter`, `optimize`) answers with the new link and, given `path`, writes the file back as the page writes it; a path that does not exist yet starts a new schematic there. With neither, `edit` and `place_filter` start a new empty schematic. A schematic that holds data files (Touchstone blocks) has no link, because file bytes never go in a URL: the answer says so, and the file is still written. A `.zip` is refused; open it on the page and download the `.json`.
|
|
44
|
+
|
|
45
|
+
## Tools
|
|
46
|
+
|
|
47
|
+
Frequencies are in Hz in tool arguments (1 GHz is 1e9). Inside an op, every value is an expression: `"1GHz"`, `"2.2p"`, `"10n"`, `"50"` or a variable name. Every answer opens with `answer`, one sentence to act on, then the numbers, then `link`.
|
|
48
|
+
|
|
49
|
+
| Tool | Input | Answer |
|
|
50
|
+
|---|---|---|
|
|
51
|
+
| `list_models` | `query`, `family`, `limit` | each matching model's id, name, pins and parameter names with units |
|
|
52
|
+
| `describe_model` | `model` | pins by number and name, each parameter's unit, default, meaning and limits, the substrate it needs |
|
|
53
|
+
| `edit` | `link` or `path`; `ops: [{ op, args }]`, `label` | one transaction, one undo step on the page: the parts added, removed and changed, the ERC count, the new link; a refusal is the failing op's own error |
|
|
54
|
+
| `describe_op` | `op` | one operation's description and its arguments as JSON Schema (`z.toJSONSchema`) |
|
|
55
|
+
| `place_filter` | `link` or `path`; the `placeFilter` spec | the LC ladder with its ports, grounds, sweep, plot and goals in one undo step, each part's value, the link |
|
|
56
|
+
| `get_netlist` | `link` or `path` | the netlist text, ERC diagnostics with hints and one-click fixes as ops, a summary of parts, ports, nodes, variables, simulations and goals |
|
|
57
|
+
| `simulate` | `link` or `path`; `simulations`, `frequencies_Hz`, `points`, `touchstone` | per S run, every S-parameter in dB and degrees and NF for a two-port; per DC operating point, node voltages, part currents, ports and probes |
|
|
58
|
+
| `optimize` | `link` or `path`; `maxIterations`, `method` | the cost before and after, each variable's old and new value, each goal's pass and margin, the link with the values applied |
|
|
59
|
+
| `run_yield` | `link` or `path`; `boards`, `seed` | passed boards of boards, the yield with its Wilson 95% interval, the most-failed goal, each goal's pass count |
|
|
60
|
+
|
|
61
|
+
The descriptions carry the rules an agent gets wrong: a port (`sys.port`) is a termination to ground with one pin; ground is `sys.gnd`; `connect` takes `"C1.2"` pin references; a microstrip, CPW or stripline part needs a substrate (`addSubstrate`) first; frequencies are in Hz. `designId` defaults to the top design, and a part placed without `pos` lands below the parts already there.
|
|
62
|
+
|
|
63
|
+
## Caps
|
|
64
|
+
|
|
65
|
+
- A document is at most 20 MB (`DOCUMENT_HARD_LIMIT_BYTES`), as on the page.
|
|
66
|
+
- One call stops at 60 s of engine time and says so; `optimize` then applies the best point so far, and `run_yield` counts the boards run.
|
|
67
|
+
- `simulate` returns at most 201 points per S run (a longer sweep is thinned evenly, both ends kept) and takes at most 201 `frequencies_Hz`.
|
|
68
|
+
- `optimize` runs at most 2000 iterations; `run_yield` draws at most 5000 boards (default the document's, 500 on a new one).
|
|
69
|
+
- AC, harmonic balance, load-pull, loop-gain runs and DC sweeps are refused with the reason: they are not in the local server yet, and the link runs them on the page.
|
|
70
|
+
|
|
71
|
+
Every clamp is named in the answer's `clamps`.
|
|
72
|
+
|
|
73
|
+
## From a checkout
|
|
74
|
+
|
|
75
|
+
Point the client at the repository's file instead of npx. `scripts/lib/ts-hooks.mjs` lets Node import the app's TypeScript in place; the checkout's `npm ci` provides `zod` and `immer`:
|
|
76
|
+
|
|
77
|
+
```bash
|
|
78
|
+
claude mcp add indierf-schematic -- node /path/to/rf-cascade-tool/mcp/schematic/server.mjs
|
|
79
|
+
```
|
|
80
|
+
|
|
81
|
+
On Windows write the path as `C:/Users/you/rf-cascade-tool/mcp/schematic/server.mjs` or with doubled backslashes.
|
|
82
|
+
|
|
83
|
+
`node mcp/schematic/build.mjs` builds the three install forms into `mcp/schematic/dist/` (git-ignored): the npm package folder and its `npm pack` tarball, the `.mcpb`, and the Registry's `server.json`. It copies every source file the server imports at run time with the repository's layout, the engine's `.wasm` beside the module that reads it, and puts `mcp/schematic/package-hooks.mjs` where `server.mjs` imports its hooks, so `@/` resolves inside the package, any file outside it is refused, and a bare import resolves only for `zod` and `immer`. It publishes nothing.
|
|
84
|
+
|
|
85
|
+
## Protocol
|
|
86
|
+
|
|
87
|
+
MCP specification revision 2025-06-18 over stdio: JSON-RPC 2.0, one message per line on stdin and stdout, logs on stderr, the same server as the Filter Bench package. Try it by hand:
|
|
88
|
+
|
|
89
|
+
```bash
|
|
90
|
+
printf '%s\n' \
|
|
91
|
+
'{"jsonrpc":"2.0","id":1,"method":"initialize","params":{"protocolVersion":"2025-06-18","capabilities":{}}}' \
|
|
92
|
+
'{"jsonrpc":"2.0","method":"notifications/initialized"}' \
|
|
93
|
+
'{"jsonrpc":"2.0","id":2,"method":"tools/call","params":{"name":"place_filter","arguments":{"response":"lowpass","approximation":"chebyshev","n":3,"rippleDb":0.5,"cutoffHz":1e9}}}' \
|
|
94
|
+
| node mcp/schematic/server.mjs
|
|
95
|
+
```
|
|
96
|
+
|
|
97
|
+
`lib/schematic/headless/__tests__/` holds each tool's numbers to hand values and scipy, the edit round trip through a file and a link, and the packed tarball run from a folder outside the repository. `mcp/schematic/eval/` holds the plain-language jobs the server is scored on.
|
|
@@ -0,0 +1,346 @@
|
|
|
1
|
+
import type { AnalysisAxis } from "@/lib/cascade/parameter-models";
|
|
2
|
+
|
|
3
|
+
export interface PlotAxisOptions {
|
|
4
|
+
logX?: boolean;
|
|
5
|
+
autoX?: boolean;
|
|
6
|
+
autoY?: boolean;
|
|
7
|
+
xMin?: number | "";
|
|
8
|
+
xMax?: number | "";
|
|
9
|
+
yMin?: number | "";
|
|
10
|
+
yMax?: number | "";
|
|
11
|
+
xInterval?: number | "";
|
|
12
|
+
yInterval?: number | "";
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
export const DEFAULT_PLOT_AXIS_OPTIONS: PlotAxisOptions = {
|
|
16
|
+
logX: false,
|
|
17
|
+
autoX: true,
|
|
18
|
+
autoY: true,
|
|
19
|
+
xMin: "",
|
|
20
|
+
xMax: "",
|
|
21
|
+
yMin: "",
|
|
22
|
+
yMax: "",
|
|
23
|
+
xInterval: "",
|
|
24
|
+
yInterval: "",
|
|
25
|
+
};
|
|
26
|
+
|
|
27
|
+
export interface ResolvedPlotBounds {
|
|
28
|
+
xMin: number;
|
|
29
|
+
xMax: number;
|
|
30
|
+
yMin: number;
|
|
31
|
+
yMax: number;
|
|
32
|
+
logX: boolean;
|
|
33
|
+
xTicks: number[];
|
|
34
|
+
yTicks: number[];
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
export function resolvePlotBounds(
|
|
38
|
+
xs: readonly number[],
|
|
39
|
+
ys: readonly number[],
|
|
40
|
+
options: PlotAxisOptions,
|
|
41
|
+
): ResolvedPlotBounds {
|
|
42
|
+
const logX = options.logX ?? false;
|
|
43
|
+
const finiteX = xs.filter(
|
|
44
|
+
(value) => Number.isFinite(value) && (!logX || value > 0),
|
|
45
|
+
);
|
|
46
|
+
const finiteY = ys.filter((value) => Number.isFinite(value));
|
|
47
|
+
|
|
48
|
+
let xMin: number;
|
|
49
|
+
let xMax: number;
|
|
50
|
+
let yMin: number;
|
|
51
|
+
let yMax: number;
|
|
52
|
+
|
|
53
|
+
if (options.autoX) {
|
|
54
|
+
const nice = niceAxisRange(
|
|
55
|
+
logX ? Math.min(...finiteX) : Math.min(...finiteX),
|
|
56
|
+
Math.max(...finiteX),
|
|
57
|
+
options.xInterval === "" ? undefined : Number(options.xInterval),
|
|
58
|
+
logX,
|
|
59
|
+
);
|
|
60
|
+
xMin = nice.min;
|
|
61
|
+
xMax = nice.max;
|
|
62
|
+
} else {
|
|
63
|
+
xMin = Number(options.xMin);
|
|
64
|
+
xMax = Number(options.xMax);
|
|
65
|
+
if (!Number.isFinite(xMin) || !Number.isFinite(xMax)) {
|
|
66
|
+
const fallback = niceAxisRange(
|
|
67
|
+
Math.min(...finiteX),
|
|
68
|
+
Math.max(...finiteX),
|
|
69
|
+
options.xInterval === "" ? undefined : Number(options.xInterval),
|
|
70
|
+
logX,
|
|
71
|
+
);
|
|
72
|
+
xMin = Number.isFinite(xMin) ? xMin : fallback.min;
|
|
73
|
+
xMax = Number.isFinite(xMax) ? xMax : fallback.max;
|
|
74
|
+
}
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
if (options.autoY) {
|
|
78
|
+
const nice = niceAxisRange(
|
|
79
|
+
Math.min(...finiteY),
|
|
80
|
+
Math.max(...finiteY),
|
|
81
|
+
options.yInterval === "" ? undefined : Number(options.yInterval),
|
|
82
|
+
false,
|
|
83
|
+
);
|
|
84
|
+
yMin = nice.min;
|
|
85
|
+
yMax = nice.max;
|
|
86
|
+
} else {
|
|
87
|
+
yMin = Number(options.yMin);
|
|
88
|
+
yMax = Number(options.yMax);
|
|
89
|
+
if (!Number.isFinite(yMin) || !Number.isFinite(yMax)) {
|
|
90
|
+
const fallback = niceAxisRange(
|
|
91
|
+
Math.min(...finiteY),
|
|
92
|
+
Math.max(...finiteY),
|
|
93
|
+
options.yInterval === "" ? undefined : Number(options.yInterval),
|
|
94
|
+
false,
|
|
95
|
+
);
|
|
96
|
+
yMin = Number.isFinite(yMin) ? yMin : fallback.min;
|
|
97
|
+
yMax = Number.isFinite(yMax) ? yMax : fallback.max;
|
|
98
|
+
}
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
const xInterval =
|
|
102
|
+
options.autoX || options.xInterval === ""
|
|
103
|
+
? undefined
|
|
104
|
+
: Number(options.xInterval);
|
|
105
|
+
const yInterval =
|
|
106
|
+
options.autoY || options.yInterval === ""
|
|
107
|
+
? undefined
|
|
108
|
+
: Number(options.yInterval);
|
|
109
|
+
|
|
110
|
+
const xTicks = buildTicks(xMin, xMax, xInterval, logX);
|
|
111
|
+
const yTicks = buildTicks(yMin, yMax, yInterval, false);
|
|
112
|
+
|
|
113
|
+
return {
|
|
114
|
+
xMin: Number.isFinite(xMin) ? xMin : 0,
|
|
115
|
+
xMax: Number.isFinite(xMax) ? xMax : 1,
|
|
116
|
+
yMin: Number.isFinite(yMin) ? yMin : 0,
|
|
117
|
+
yMax: Number.isFinite(yMax) ? yMax : 1,
|
|
118
|
+
logX,
|
|
119
|
+
xTicks,
|
|
120
|
+
yTicks,
|
|
121
|
+
};
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
export function suggestManualAxisLimits(
|
|
125
|
+
values: readonly number[],
|
|
126
|
+
log = false,
|
|
127
|
+
): {
|
|
128
|
+
min: number;
|
|
129
|
+
max: number;
|
|
130
|
+
interval: number;
|
|
131
|
+
} {
|
|
132
|
+
const finite = values.filter(
|
|
133
|
+
(value) => Number.isFinite(value) && (!log || value > 0),
|
|
134
|
+
);
|
|
135
|
+
if (finite.length === 0) return { min: 0, max: 1, interval: 0.2 };
|
|
136
|
+
const nice = niceAxisRange(
|
|
137
|
+
Math.min(...finite),
|
|
138
|
+
Math.max(...finite),
|
|
139
|
+
undefined,
|
|
140
|
+
log,
|
|
141
|
+
);
|
|
142
|
+
return { min: nice.min, max: nice.max, interval: nice.step };
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
function niceAxisRange(
|
|
146
|
+
min: number,
|
|
147
|
+
max: number,
|
|
148
|
+
interval: number | undefined,
|
|
149
|
+
log: boolean,
|
|
150
|
+
): { min: number; max: number; step: number } {
|
|
151
|
+
if (!Number.isFinite(min) || !Number.isFinite(max)) {
|
|
152
|
+
return { min: 0, max: 1, step: 0.2 };
|
|
153
|
+
}
|
|
154
|
+
if (min === max) {
|
|
155
|
+
const pad = Math.max(Math.abs(min) * 0.05, 0.2);
|
|
156
|
+
min -= pad;
|
|
157
|
+
max += pad;
|
|
158
|
+
}
|
|
159
|
+
if (log) {
|
|
160
|
+
const safeMin = Math.max(min, Number.EPSILON);
|
|
161
|
+
const safeMax = Math.max(max, safeMin * 1.01);
|
|
162
|
+
const step =
|
|
163
|
+
interval && Number.isFinite(interval) && interval > 0
|
|
164
|
+
? interval
|
|
165
|
+
: niceStep(safeMax / safeMin, 5, true);
|
|
166
|
+
const niceMin = Math.pow(10, Math.floor(Math.log10(safeMin)));
|
|
167
|
+
const niceMax = Math.pow(10, Math.ceil(Math.log10(safeMax)));
|
|
168
|
+
return { min: niceMin, max: niceMax, step };
|
|
169
|
+
}
|
|
170
|
+
|
|
171
|
+
const span = max - min;
|
|
172
|
+
const paddedMin = min - span * 0.05;
|
|
173
|
+
const paddedMax = max + span * 0.05;
|
|
174
|
+
const step =
|
|
175
|
+
interval && Number.isFinite(interval) && interval > 0
|
|
176
|
+
? interval
|
|
177
|
+
: niceStep(paddedMax - paddedMin, 6, false);
|
|
178
|
+
const niceMin = Math.floor(paddedMin / step) * step;
|
|
179
|
+
const niceMax = Math.ceil(paddedMax / step) * step;
|
|
180
|
+
return {
|
|
181
|
+
min: roundAxisValue(niceMin),
|
|
182
|
+
max: roundAxisValue(niceMax),
|
|
183
|
+
step: roundAxisValue(step),
|
|
184
|
+
};
|
|
185
|
+
}
|
|
186
|
+
|
|
187
|
+
function niceStep(
|
|
188
|
+
range: number,
|
|
189
|
+
targetTickCount: number,
|
|
190
|
+
log: boolean,
|
|
191
|
+
): number {
|
|
192
|
+
if (!Number.isFinite(range) || range <= 0) return log ? 2 : 0.2;
|
|
193
|
+
const rough = range / Math.max(targetTickCount - 1, 1);
|
|
194
|
+
const magnitude = Math.pow(10, Math.floor(Math.log10(rough)));
|
|
195
|
+
const residual = rough / magnitude;
|
|
196
|
+
let niceResidual: number;
|
|
197
|
+
if (residual <= 1) niceResidual = 1;
|
|
198
|
+
else if (residual <= 2) niceResidual = 2;
|
|
199
|
+
else if (residual <= 2.5) niceResidual = 2.5;
|
|
200
|
+
else if (residual <= 5) niceResidual = 5;
|
|
201
|
+
else niceResidual = 10;
|
|
202
|
+
return roundAxisValue(niceResidual * magnitude);
|
|
203
|
+
}
|
|
204
|
+
|
|
205
|
+
function buildTicks(
|
|
206
|
+
min: number,
|
|
207
|
+
max: number,
|
|
208
|
+
interval: number | undefined,
|
|
209
|
+
log: boolean,
|
|
210
|
+
): number[] {
|
|
211
|
+
if (!Number.isFinite(min) || !Number.isFinite(max) || min === max) {
|
|
212
|
+
return [roundAxisValue(min)];
|
|
213
|
+
}
|
|
214
|
+
const step =
|
|
215
|
+
interval && Number.isFinite(interval) && interval > 0
|
|
216
|
+
? interval
|
|
217
|
+
: niceStep(max - min, 6, log);
|
|
218
|
+
const ticks: number[] = [];
|
|
219
|
+
const seen = new Set<number>();
|
|
220
|
+
const pushTick = (value: number) => {
|
|
221
|
+
const rounded = roundAxisValue(value);
|
|
222
|
+
if (seen.has(rounded)) return;
|
|
223
|
+
seen.add(rounded);
|
|
224
|
+
ticks.push(rounded);
|
|
225
|
+
};
|
|
226
|
+
if (log) {
|
|
227
|
+
let value = min;
|
|
228
|
+
while (value <= max * 1.001) {
|
|
229
|
+
pushTick(value);
|
|
230
|
+
value *= step > 1 ? step : 10;
|
|
231
|
+
}
|
|
232
|
+
return ticks.length > 0 ? ticks : [roundAxisValue(min), roundAxisValue(max)];
|
|
233
|
+
}
|
|
234
|
+
const start = Math.ceil(min / step) * step;
|
|
235
|
+
for (let value = start; value <= max + step * 0.001; value += step) {
|
|
236
|
+
pushTick(value);
|
|
237
|
+
}
|
|
238
|
+
if (ticks.length === 0) {
|
|
239
|
+
pushTick(min);
|
|
240
|
+
pushTick(max);
|
|
241
|
+
}
|
|
242
|
+
return ticks;
|
|
243
|
+
}
|
|
244
|
+
|
|
245
|
+
export function roundAxisValue(value: number): number {
|
|
246
|
+
if (!Number.isFinite(value)) return 0;
|
|
247
|
+
if (value === 0) return 0;
|
|
248
|
+
const abs = Math.abs(value);
|
|
249
|
+
if (abs >= 100) return Math.round(value);
|
|
250
|
+
if (abs >= 10) return Math.round(value * 10) / 10;
|
|
251
|
+
if (abs >= 1) return Math.round(value * 100) / 100;
|
|
252
|
+
if (abs >= 0.1) return Math.round(value * 100) / 100;
|
|
253
|
+
const exp = Math.floor(Math.log10(abs));
|
|
254
|
+
const factor = 10 ** (3 - exp);
|
|
255
|
+
return Math.round(value * factor) / factor;
|
|
256
|
+
}
|
|
257
|
+
|
|
258
|
+
export function formatTickLabel(value: number): string {
|
|
259
|
+
if (!Number.isFinite(value)) return "—";
|
|
260
|
+
if (value === 0) return "0";
|
|
261
|
+
const abs = Math.abs(value);
|
|
262
|
+
if (abs >= 1e5 || abs < 0.01) {
|
|
263
|
+
return value.toExponential(2).replace(/\.?0+e/, "e");
|
|
264
|
+
}
|
|
265
|
+
const rounded = roundAxisValue(value);
|
|
266
|
+
if (Number.isInteger(rounded) && abs >= 1) return String(rounded);
|
|
267
|
+
if (abs >= 10) return rounded.toFixed(1);
|
|
268
|
+
return rounded.toFixed(2);
|
|
269
|
+
}
|
|
270
|
+
|
|
271
|
+
export function scaleX(
|
|
272
|
+
value: number,
|
|
273
|
+
xMin: number,
|
|
274
|
+
xMax: number,
|
|
275
|
+
logX: boolean,
|
|
276
|
+
plotLeft: number,
|
|
277
|
+
plotWidth: number,
|
|
278
|
+
): number {
|
|
279
|
+
if (logX) {
|
|
280
|
+
const safeMin = Math.max(xMin, Number.EPSILON);
|
|
281
|
+
const safeMax = Math.max(xMax, safeMin * 10);
|
|
282
|
+
const safeValue = Math.max(value, safeMin);
|
|
283
|
+
const minLog = Math.log10(safeMin);
|
|
284
|
+
const maxLog = Math.log10(safeMax);
|
|
285
|
+
const span = maxLog - minLog || 1;
|
|
286
|
+
return plotLeft + ((Math.log10(safeValue) - minLog) / span) * plotWidth;
|
|
287
|
+
}
|
|
288
|
+
const span = xMax - xMin || 1;
|
|
289
|
+
return plotLeft + ((value - xMin) / span) * plotWidth;
|
|
290
|
+
}
|
|
291
|
+
|
|
292
|
+
export function uniqueAxisValues(
|
|
293
|
+
result: import("@/lib/cascade/parameter-models").AnalysisResult,
|
|
294
|
+
axisId: string,
|
|
295
|
+
): number[] {
|
|
296
|
+
const index = result.axisIds?.indexOf(axisId) ?? -1;
|
|
297
|
+
if (index < 0) return [];
|
|
298
|
+
const values = new Set<number>();
|
|
299
|
+
for (const coordinate of result.coordinates) {
|
|
300
|
+
const tuple = Array.isArray(coordinate) ? coordinate : coordinate.values;
|
|
301
|
+
if (Number.isFinite(tuple[index])) values.add(tuple[index]);
|
|
302
|
+
}
|
|
303
|
+
return [...values].sort((a, b) => a - b);
|
|
304
|
+
}
|
|
305
|
+
|
|
306
|
+
export function axisUnitLabel(axis: AnalysisAxis | undefined): string {
|
|
307
|
+
if (!axis) return "";
|
|
308
|
+
if (axis.unit) return String(axis.unit);
|
|
309
|
+
if (axis.kind === "context" && axis.contextKey === "frequency_Hz") return "Hz";
|
|
310
|
+
if (axis.kind === "context" && axis.contextKey === "temperature_K") return "K";
|
|
311
|
+
return "";
|
|
312
|
+
}
|
|
313
|
+
|
|
314
|
+
export function countUniqueFinite(values: readonly (number | null)[]): number {
|
|
315
|
+
return new Set(
|
|
316
|
+
values.filter(
|
|
317
|
+
(value): value is number => value !== null && Number.isFinite(value),
|
|
318
|
+
),
|
|
319
|
+
).size;
|
|
320
|
+
}
|
|
321
|
+
|
|
322
|
+
export function defaultFrequencyAxisId(
|
|
323
|
+
axisIds: readonly string[],
|
|
324
|
+
analysis: import("@/lib/cascade/parameter-models").AnalysisDefinition | undefined,
|
|
325
|
+
): string {
|
|
326
|
+
if (!analysis?.axes) return axisIds[0] ?? "";
|
|
327
|
+
const frequencyContext = analysis.axes.find(
|
|
328
|
+
(axis) =>
|
|
329
|
+
axis.enabled &&
|
|
330
|
+
axis.kind === "context" &&
|
|
331
|
+
axis.contextKey === "frequency_Hz",
|
|
332
|
+
);
|
|
333
|
+
if (frequencyContext) return frequencyContext.id;
|
|
334
|
+
const loSetting = analysis.axes.find(
|
|
335
|
+
(axis) =>
|
|
336
|
+
axis.enabled &&
|
|
337
|
+
axis.kind === "setting" &&
|
|
338
|
+
axis.parameter === "loFrequency_Hz",
|
|
339
|
+
);
|
|
340
|
+
if (loSetting) return loSetting.id;
|
|
341
|
+
const named = analysis.axes.find(
|
|
342
|
+
(axis) => axis.enabled && /frequency/i.test(axis.label),
|
|
343
|
+
);
|
|
344
|
+
if (named) return named.id;
|
|
345
|
+
return axisIds[0] ?? "";
|
|
346
|
+
}
|