@tscircuit/ngspice-spice-engine 0.0.21 → 0.0.23
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 +21 -0
- package/README.md +3 -3
- package/dist/index.js +94 -6
- package/package.json +2 -1
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2024 tscircuit Inc.
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
CHANGED
package/dist/index.js
CHANGED
|
@@ -87,6 +87,71 @@ var parseTranParams = (spiceString) => {
|
|
|
87
87
|
return params;
|
|
88
88
|
};
|
|
89
89
|
|
|
90
|
+
// lib/normalize-pspice-switch-models.ts
|
|
91
|
+
import {
|
|
92
|
+
Model,
|
|
93
|
+
parseSpiceNetlist as parseSpiceNetlist2,
|
|
94
|
+
tokenizeSpice
|
|
95
|
+
} from "spicets";
|
|
96
|
+
var getSwitchModels = (node) => node.getChildren().flatMap((child) => {
|
|
97
|
+
if (child instanceof Model && child.modelType.toLowerCase() === "vswitch") {
|
|
98
|
+
return [child];
|
|
99
|
+
}
|
|
100
|
+
return getSwitchModels(child);
|
|
101
|
+
});
|
|
102
|
+
var normalizePspiceSwitchModels = (spiceString) => {
|
|
103
|
+
const netlist = parseSpiceNetlist2(spiceString);
|
|
104
|
+
let normalizedSource = spiceString;
|
|
105
|
+
for (const model of getSwitchModels(netlist)) {
|
|
106
|
+
const ron = model.params.get("ron")?.getString();
|
|
107
|
+
const roff = model.params.get("roff")?.getString();
|
|
108
|
+
const von = model.params.get("von")?.getString();
|
|
109
|
+
const voff = model.params.get("voff")?.getString();
|
|
110
|
+
if (!ron || !roff || !von || !voff || !model.originalSource) continue;
|
|
111
|
+
const ronTokens = tokenizeSpice(ron).tokens;
|
|
112
|
+
const roffTokens = tokenizeSpice(roff).tokens;
|
|
113
|
+
const ronToken = ronTokens[0];
|
|
114
|
+
const roffToken = roffTokens[0];
|
|
115
|
+
if (ronTokens.length === 1 && roffTokens.length === 1 && ronToken?.type === "number" && roffToken?.type === "number" && ronToken.value !== null && roffToken.value !== null && ronToken.value <= roffToken.value) {
|
|
116
|
+
continue;
|
|
117
|
+
}
|
|
118
|
+
model.params.set("ron", `{min(${ron},${roff})}`);
|
|
119
|
+
model.params.set("roff", `{max(${ron},${roff})}`);
|
|
120
|
+
model.params.set("von", `{if(${ron}>${roff},${voff},${von})}`);
|
|
121
|
+
model.params.set("voff", `{if(${ron}>${roff},${von},${voff})}`);
|
|
122
|
+
normalizedSource = normalizedSource.replace(
|
|
123
|
+
model.originalSource,
|
|
124
|
+
() => model.toSource({ format: "pretty" })
|
|
125
|
+
);
|
|
126
|
+
}
|
|
127
|
+
return normalizedSource;
|
|
128
|
+
};
|
|
129
|
+
|
|
130
|
+
// lib/rewrite-pspice-current-source-units.ts
|
|
131
|
+
import { CurrentSource, parseSpiceNetlist as parseSpiceNetlist3 } from "spicets";
|
|
132
|
+
var getCurrentSources = (node) => node.getChildren().flatMap((child) => {
|
|
133
|
+
if (child instanceof CurrentSource) return [child];
|
|
134
|
+
return getCurrentSources(child);
|
|
135
|
+
});
|
|
136
|
+
var rewritePspiceCurrentSourceUnits = (spiceString) => {
|
|
137
|
+
const netlist = parseSpiceNetlist3(spiceString);
|
|
138
|
+
let rewrittenSource = spiceString;
|
|
139
|
+
for (const source of getCurrentSources(netlist)) {
|
|
140
|
+
const current = source.dc?.getString();
|
|
141
|
+
if (!current || !/^[+-]?(?:\d+(?:\.\d*)?|\.\d+)(?:e[+-]?\d+)?(?:meg|mil|[tgkmunpfa])?Adc$/i.test(
|
|
142
|
+
current
|
|
143
|
+
) || !source.originalSource) {
|
|
144
|
+
continue;
|
|
145
|
+
}
|
|
146
|
+
source.dc.raw = current.slice(0, -3);
|
|
147
|
+
rewrittenSource = rewrittenSource.replace(
|
|
148
|
+
source.originalSource,
|
|
149
|
+
() => source.toSource({ format: "pretty" })
|
|
150
|
+
);
|
|
151
|
+
}
|
|
152
|
+
return rewrittenSource;
|
|
153
|
+
};
|
|
154
|
+
|
|
90
155
|
// lib/rewrite-pspice-compatibility-syntax.ts
|
|
91
156
|
var pspiceNumberToken = String.raw`([+-]?(?:(?:\d+(?:\.\d*)?)|(?:\.\d+))(?:[eE][+-]?\d+)?)`;
|
|
92
157
|
var rewritePspiceResistorTcPairs = (spiceString) => spiceString.split(/\r?\n/).map((line) => {
|
|
@@ -150,7 +215,13 @@ var rewritePspiceValueBooleanCarets = (spiceString) => {
|
|
|
150
215
|
}
|
|
151
216
|
return result + spiceString.slice(cursor);
|
|
152
217
|
};
|
|
153
|
-
var rewritePspiceCompatibilitySyntax = (spiceString) =>
|
|
218
|
+
var rewritePspiceCompatibilitySyntax = (spiceString) => normalizePspiceSwitchModels(
|
|
219
|
+
rewritePspiceCurrentSourceUnits(
|
|
220
|
+
rewritePspiceValueBooleanCarets(
|
|
221
|
+
rewritePspiceResistorTcPairs(spiceString)
|
|
222
|
+
)
|
|
223
|
+
)
|
|
224
|
+
);
|
|
154
225
|
|
|
155
226
|
// lib/create-graph-from-requested-plot.ts
|
|
156
227
|
var getNetName = (spiceVector) => {
|
|
@@ -305,10 +376,10 @@ var extractCurrentProbeMetadata = (spiceString) => {
|
|
|
305
376
|
};
|
|
306
377
|
|
|
307
378
|
// lib/extract-requested-plots.ts
|
|
308
|
-
import { Print, parseSpiceNetlist as
|
|
379
|
+
import { Print, parseSpiceNetlist as parseSpiceNetlist4 } from "spicets";
|
|
309
380
|
var extractRequestedPlots = (spiceString) => {
|
|
310
381
|
const supportedAnalyses = /* @__PURE__ */ new Set(["tran", "op", "dc", "ac"]);
|
|
311
|
-
const print =
|
|
382
|
+
const print = parseSpiceNetlist4(spiceString, {
|
|
312
383
|
dialect: "ngspice"
|
|
313
384
|
}).directives.find(
|
|
314
385
|
(directive) => directive instanceof Print && supportedAnalyses.has(directive.analysis?.toLowerCase() ?? "")
|
|
@@ -634,9 +705,9 @@ var operatingPointResultToGraphs = ({
|
|
|
634
705
|
};
|
|
635
706
|
|
|
636
707
|
// lib/parse-simulation-analysis.ts
|
|
637
|
-
import { Ac, Dc, Op, parseSpiceNetlist as
|
|
708
|
+
import { Ac, Dc, Op, parseSpiceNetlist as parseSpiceNetlist5, Tran as Tran2 } from "spicets";
|
|
638
709
|
var parseSimulationAnalysis = (spiceString) => {
|
|
639
|
-
const analyses =
|
|
710
|
+
const analyses = parseSpiceNetlist5(spiceString, {
|
|
640
711
|
dialect: "ngspice"
|
|
641
712
|
}).analyses;
|
|
642
713
|
if (analyses.some((analysis) => analysis instanceof Tran2)) {
|
|
@@ -685,6 +756,24 @@ var eecircuitResultToSimulationGraphs = (result, spiceString) => {
|
|
|
685
756
|
return [];
|
|
686
757
|
}
|
|
687
758
|
const timeSeconds = timeOutput.values;
|
|
759
|
+
const tranParams = parseTranParams(spiceString);
|
|
760
|
+
if (tranParams?.tstop !== void 0) {
|
|
761
|
+
const tstartSeconds = tranParams.tstart ?? 0;
|
|
762
|
+
const lastRequestedTimeSeconds = tranParams.tstep ? tstartSeconds + Math.floor((tranParams.tstop - tstartSeconds) / tranParams.tstep) * tranParams.tstep : tranParams.tstop;
|
|
763
|
+
const lastTimeSeconds = timeSeconds.at(-1);
|
|
764
|
+
const toleranceSeconds = Math.max(
|
|
765
|
+
Math.abs(lastRequestedTimeSeconds) * 1e-9,
|
|
766
|
+
Number.EPSILON
|
|
767
|
+
);
|
|
768
|
+
if (lastTimeSeconds === void 0 || !Number.isFinite(lastTimeSeconds)) {
|
|
769
|
+
throw new Error("Transient simulation produced no finite time samples.");
|
|
770
|
+
}
|
|
771
|
+
if (lastTimeSeconds + toleranceSeconds < lastRequestedTimeSeconds) {
|
|
772
|
+
throw new Error(
|
|
773
|
+
`Transient simulation stopped at ${lastTimeSeconds} s before the requested output time ${lastRequestedTimeSeconds} s.`
|
|
774
|
+
);
|
|
775
|
+
}
|
|
776
|
+
}
|
|
688
777
|
const voltageOutputs = result.data.filter(
|
|
689
778
|
(simulationOutput) => simulationOutput.type === "voltage" && Array.isArray(simulationOutput.values)
|
|
690
779
|
);
|
|
@@ -760,7 +849,6 @@ var eecircuitResultToSimulationGraphs = (result, spiceString) => {
|
|
|
760
849
|
});
|
|
761
850
|
if (graph) graphs.push(graph);
|
|
762
851
|
}
|
|
763
|
-
const tranParams = parseTranParams(spiceString);
|
|
764
852
|
if (tranParams?.tstep && tranParams.tstep > 0 && tranParams.tstop && graphs.length > 0) {
|
|
765
853
|
const { tstep, tstop } = tranParams;
|
|
766
854
|
const tstart = tranParams.tstart ?? 0;
|