@tscircuit/ngspice-spice-engine 0.0.2 → 0.0.4
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 +51 -15
- package/package.json +1 -1
package/dist/index.js
CHANGED
|
@@ -105,13 +105,24 @@ 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
|
+
for (const token of tokens) {
|
|
114
|
+
const lowerCaseToken = token.toLowerCase().replace(/\s/g, "");
|
|
115
|
+
if (!plotMap.has(lowerCaseToken)) {
|
|
116
|
+
plotMap.set(lowerCaseToken, token);
|
|
117
|
+
}
|
|
118
|
+
}
|
|
119
|
+
return plotMap;
|
|
113
120
|
};
|
|
114
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
|
+
}
|
|
115
126
|
const match = rawName.match(/^v\((.*)\)$/i);
|
|
116
127
|
if (!match) {
|
|
117
128
|
return rawName;
|
|
@@ -122,26 +133,51 @@ var eecircuitResultToVGraphs = (result, spiceString) => {
|
|
|
122
133
|
if (!result || !result.data || result.dataType !== "real") {
|
|
123
134
|
return [];
|
|
124
135
|
}
|
|
125
|
-
const requestedPlots = extractRequestedPlots(spiceString);
|
|
126
136
|
const timeData = result.data.find((item) => item.type === "time");
|
|
127
137
|
if (!timeData || !Array.isArray(timeData.values)) {
|
|
128
138
|
return [];
|
|
129
139
|
}
|
|
130
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
|
+
}
|
|
131
156
|
const graphs = [];
|
|
132
|
-
for (const
|
|
133
|
-
|
|
134
|
-
|
|
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);
|
|
135
173
|
}
|
|
136
|
-
if (
|
|
137
|
-
|
|
174
|
+
if (voltage) {
|
|
175
|
+
graphs.push({
|
|
176
|
+
netName: getNetName(originalToken),
|
|
177
|
+
time: timeValues,
|
|
178
|
+
voltage
|
|
179
|
+
});
|
|
138
180
|
}
|
|
139
|
-
const netName = getNetName(item.name);
|
|
140
|
-
graphs.push({
|
|
141
|
-
netName,
|
|
142
|
-
time: timeValues,
|
|
143
|
-
voltage: item.values
|
|
144
|
-
});
|
|
145
181
|
}
|
|
146
182
|
return graphs;
|
|
147
183
|
};
|