@tscircuit/ngspice-spice-engine 0.0.13 → 0.0.14

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/dist/index.d.ts CHANGED
@@ -14,6 +14,14 @@ interface VoltageGraph {
14
14
  netName: string;
15
15
  time: number[];
16
16
  voltage: number[];
17
+ probeMetadata?: ProbeMetadata;
18
+ }
19
+ interface ProbeMetadata {
20
+ simulation_voltage_probe_id: string;
21
+ name?: string;
22
+ spice_vector: string;
23
+ source_node_name: string;
24
+ reference_node_name?: string;
17
25
  }
18
26
  declare const eecircuitResultToVGraphs: (result: ResultType, spiceString: string) => VoltageGraph[];
19
27
  declare const createNgspiceSpiceEngine: () => Promise<SpiceEngine>;
package/dist/index.js CHANGED
@@ -143,6 +143,30 @@ var extractRequestedPlots = (spiceString) => {
143
143
  }
144
144
  return plotMap;
145
145
  };
146
+ var normalizeSpiceVector = (value) => value.toLowerCase().replace(/\s/g, "");
147
+ var extractProbeMetadata = (spiceString) => {
148
+ const metadata = /* @__PURE__ */ new Map();
149
+ for (const line of spiceString.split(/\r?\n/)) {
150
+ const match = line.match(/^\s*\*\s*tscircuit_probe\s+(.+)\s*$/);
151
+ if (!match?.[1]) continue;
152
+ try {
153
+ const parsed = JSON.parse(match[1]);
154
+ if (typeof parsed.simulation_voltage_probe_id !== "string" || typeof parsed.spice_vector !== "string" || typeof parsed.source_node_name !== "string") {
155
+ continue;
156
+ }
157
+ metadata.set(normalizeSpiceVector(parsed.spice_vector), {
158
+ simulation_voltage_probe_id: parsed.simulation_voltage_probe_id,
159
+ name: typeof parsed.name === "string" ? parsed.name : void 0,
160
+ spice_vector: parsed.spice_vector,
161
+ source_node_name: parsed.source_node_name,
162
+ reference_node_name: typeof parsed.reference_node_name === "string" ? parsed.reference_node_name : void 0
163
+ });
164
+ } catch {
165
+ continue;
166
+ }
167
+ }
168
+ return metadata;
169
+ };
146
170
  var getNetName = (rawName) => {
147
171
  const diffMatch = rawName.match(/^v\(([^,]+),\s*([^)]+)\)$/i);
148
172
  if (diffMatch?.[1] && diffMatch?.[2]) {
@@ -171,12 +195,17 @@ var eecircuitResultToVGraphs = (result, spiceString) => {
171
195
  voltageDataMap.set(item.name.toLowerCase(), item.values);
172
196
  }
173
197
  const requestedPlots = extractRequestedPlots(spiceString);
198
+ const probeMetadata = extractProbeMetadata(spiceString);
174
199
  if (!requestedPlots) {
175
- return voltageDataItems.map((item) => ({
176
- netName: getNetName(item.name),
177
- time: timeValues,
178
- voltage: item.values
179
- }));
200
+ return voltageDataItems.map((item) => {
201
+ const metadata = probeMetadata.get(normalizeSpiceVector(item.name));
202
+ return {
203
+ netName: metadata?.name ?? getNetName(item.name),
204
+ time: timeValues,
205
+ voltage: item.values,
206
+ probeMetadata: metadata
207
+ };
208
+ });
180
209
  }
181
210
  const graphs = [];
182
211
  for (const [lowerCaseToken, originalToken] of requestedPlots.entries()) {
@@ -197,10 +226,12 @@ var eecircuitResultToVGraphs = (result, spiceString) => {
197
226
  voltage = voltageDataMap.get(lowerCaseToken);
198
227
  }
199
228
  if (voltage) {
229
+ const metadata = probeMetadata.get(lowerCaseToken);
200
230
  graphs.push({
201
- netName: getNetName(originalToken),
231
+ netName: metadata?.name ?? getNetName(originalToken),
202
232
  time: timeValues,
203
- voltage
233
+ voltage,
234
+ probeMetadata: metadata
204
235
  });
205
236
  }
206
237
  }
@@ -228,17 +259,25 @@ var eecircuitResultToVGraphs = (result, spiceString) => {
228
259
  };
229
260
  var voltageGraphsToCircuitJson = (graphs, spiceString) => {
230
261
  const tranParams = parseTranParams(spiceString);
231
- return graphs.map((graph) => ({
232
- type: "simulation_transient_voltage_graph",
233
- simulation_experiment_id: "placeholder_simulation_experiment_id",
234
- simulation_transient_voltage_graph_id: `simulation_graph_${graph.netName}`,
235
- name: graph.netName,
236
- voltage_levels: graph.voltage,
237
- timestamps_ms: graph.time.map((timePoint) => timePoint * 1e3),
238
- start_time_ms: (tranParams?.tstart ?? 0) * 1e3,
239
- time_per_step: (tranParams?.tstep ?? 0) * 1e3,
240
- end_time_ms: (tranParams?.tstop ?? 0) * 1e3
241
- }));
262
+ return graphs.map((graph, index) => {
263
+ const graphIdSource = graph.probeMetadata?.simulation_voltage_probe_id ?? `${index}_${graph.netName}`;
264
+ const graphElement = {
265
+ type: "simulation_transient_voltage_graph",
266
+ simulation_experiment_id: "placeholder_simulation_experiment_id",
267
+ simulation_transient_voltage_graph_id: `simulation_graph_${graphIdSource}`,
268
+ name: graph.netName,
269
+ voltage_levels: graph.voltage,
270
+ timestamps_ms: graph.time.map((timePoint) => timePoint * 1e3),
271
+ start_time_ms: (tranParams?.tstart ?? 0) * 1e3,
272
+ time_per_step: (tranParams?.tstep ?? 0) * 1e3,
273
+ end_time_ms: (tranParams?.tstop ?? 0) * 1e3,
274
+ source_probe_id: graph.probeMetadata?.simulation_voltage_probe_id,
275
+ source_probe_name: graph.probeMetadata?.name,
276
+ source_node_name: graph.probeMetadata?.source_node_name,
277
+ reference_node_name: graph.probeMetadata?.reference_node_name
278
+ };
279
+ return graphElement;
280
+ });
242
281
  };
243
282
  var simulate = async (spiceString) => {
244
283
  const simulation = await getSimulation();
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@tscircuit/ngspice-spice-engine",
3
- "version": "0.0.13",
3
+ "version": "0.0.14",
4
4
  "description": "A tscircuit-compatible SPICE engine using ngspice.",
5
5
  "type": "module",
6
6
  "main": "./dist/index.js",
@@ -22,8 +22,9 @@
22
22
  "devDependencies": {
23
23
  "@tscircuit/props": "^0.0.374",
24
24
  "bun-match-svg": "^0.0.14",
25
- "circuit-json": "^0.0.290",
25
+ "circuit-json": "^0.0.435",
26
26
  "circuit-to-svg": "^0.0.217",
27
+ "format-si-unit": "^0.0.7",
27
28
  "@biomejs/biome": "^2.2.7",
28
29
  "@types/bun": "latest",
29
30
  "tscircuit": "^0.0.805",