@tscircuit/ngspice-spice-engine 0.0.3 → 0.0.5
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.js +44 -18
- package/package.json +3 -1
package/dist/index.js
CHANGED
|
@@ -105,13 +105,13 @@ var extractRequestedPlots = (spiceString) => {
|
|
|
105
105
|
if (!match || !match[1]) {
|
|
106
106
|
return null;
|
|
107
107
|
}
|
|
108
|
-
const tokens = match[1].
|
|
109
|
-
if (tokens
|
|
108
|
+
const tokens = match[1].match(/[VI]\s*\([^)]+\)/gi);
|
|
109
|
+
if (!tokens) {
|
|
110
110
|
return null;
|
|
111
111
|
}
|
|
112
112
|
const plotMap = /* @__PURE__ */ new Map();
|
|
113
113
|
for (const token of tokens) {
|
|
114
|
-
const lowerCaseToken = token.toLowerCase();
|
|
114
|
+
const lowerCaseToken = token.toLowerCase().replace(/\s/g, "");
|
|
115
115
|
if (!plotMap.has(lowerCaseToken)) {
|
|
116
116
|
plotMap.set(lowerCaseToken, token);
|
|
117
117
|
}
|
|
@@ -119,6 +119,10 @@ var extractRequestedPlots = (spiceString) => {
|
|
|
119
119
|
return plotMap;
|
|
120
120
|
};
|
|
121
121
|
var getNetName = (rawName) => {
|
|
122
|
+
const diffMatch = rawName.match(/^v\(([^,]+),\s*([^)]+)\)$/i);
|
|
123
|
+
if (diffMatch?.[1] && diffMatch?.[2]) {
|
|
124
|
+
return `${diffMatch[1].trim()}-${diffMatch[2].trim()}`;
|
|
125
|
+
}
|
|
122
126
|
const match = rawName.match(/^v\((.*)\)$/i);
|
|
123
127
|
if (!match) {
|
|
124
128
|
return rawName;
|
|
@@ -129,29 +133,51 @@ var eecircuitResultToVGraphs = (result, spiceString) => {
|
|
|
129
133
|
if (!result || !result.data || result.dataType !== "real") {
|
|
130
134
|
return [];
|
|
131
135
|
}
|
|
132
|
-
const requestedPlots = extractRequestedPlots(spiceString);
|
|
133
136
|
const timeData = result.data.find((item) => item.type === "time");
|
|
134
137
|
if (!timeData || !Array.isArray(timeData.values)) {
|
|
135
138
|
return [];
|
|
136
139
|
}
|
|
137
140
|
const timeValues = timeData.values;
|
|
141
|
+
const voltageDataItems = result.data.filter(
|
|
142
|
+
(item) => item.type === "voltage" && Array.isArray(item.values)
|
|
143
|
+
);
|
|
144
|
+
const voltageDataMap = /* @__PURE__ */ new Map();
|
|
145
|
+
for (const item of voltageDataItems) {
|
|
146
|
+
voltageDataMap.set(item.name.toLowerCase(), item.values);
|
|
147
|
+
}
|
|
148
|
+
const requestedPlots = extractRequestedPlots(spiceString);
|
|
149
|
+
if (!requestedPlots) {
|
|
150
|
+
return voltageDataItems.map((item) => ({
|
|
151
|
+
netName: getNetName(item.name),
|
|
152
|
+
time: timeValues,
|
|
153
|
+
voltage: item.values
|
|
154
|
+
}));
|
|
155
|
+
}
|
|
138
156
|
const graphs = [];
|
|
139
|
-
for (const
|
|
140
|
-
|
|
141
|
-
|
|
157
|
+
for (const [lowerCaseToken, originalToken] of requestedPlots.entries()) {
|
|
158
|
+
const diffMatch = originalToken.match(/^v\(([^,]+),\s*([^)]+)\)$/i);
|
|
159
|
+
let voltage;
|
|
160
|
+
if (diffMatch?.[1] && diffMatch?.[2]) {
|
|
161
|
+
voltage = voltageDataMap.get(lowerCaseToken);
|
|
162
|
+
if (!voltage) {
|
|
163
|
+
const node1 = diffMatch[1].trim();
|
|
164
|
+
const node2 = diffMatch[2].trim();
|
|
165
|
+
const node1Data = voltageDataMap.get(`v(${node1.toLowerCase()})`);
|
|
166
|
+
const node2Data = voltageDataMap.get(`v(${node2.toLowerCase()})`);
|
|
167
|
+
if (node1Data && node2Data) {
|
|
168
|
+
voltage = node1Data.map((v, i) => v - (node2Data[i] ?? 0));
|
|
169
|
+
}
|
|
170
|
+
}
|
|
171
|
+
} else {
|
|
172
|
+
voltage = voltageDataMap.get(lowerCaseToken);
|
|
142
173
|
}
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
174
|
+
if (voltage) {
|
|
175
|
+
graphs.push({
|
|
176
|
+
netName: getNetName(originalToken),
|
|
177
|
+
time: timeValues,
|
|
178
|
+
voltage
|
|
179
|
+
});
|
|
146
180
|
}
|
|
147
|
-
const netName = getNetName(
|
|
148
|
-
requestedPlots ? requestedPlots.get(lowerCaseItemName) : item.name
|
|
149
|
-
);
|
|
150
|
-
graphs.push({
|
|
151
|
-
netName,
|
|
152
|
-
time: timeValues,
|
|
153
|
-
voltage: item.values
|
|
154
|
-
});
|
|
155
181
|
}
|
|
156
182
|
return graphs;
|
|
157
183
|
};
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@tscircuit/ngspice-spice-engine",
|
|
3
|
-
"version": "0.0.
|
|
3
|
+
"version": "0.0.5",
|
|
4
4
|
"description": "A tscircuit-compatible SPICE engine using ngspice.",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"main": "./dist/index.js",
|
|
@@ -21,7 +21,9 @@
|
|
|
21
21
|
},
|
|
22
22
|
"devDependencies": {
|
|
23
23
|
"@tscircuit/props": "^0.0.374",
|
|
24
|
+
"bun-match-svg": "^0.0.14",
|
|
24
25
|
"circuit-json": "^0.0.290",
|
|
26
|
+
"circuit-to-svg": "^0.0.217",
|
|
25
27
|
"@biomejs/biome": "^2.2.7",
|
|
26
28
|
"@types/bun": "latest",
|
|
27
29
|
"tscircuit": "^0.0.805",
|